+#!/usr/bin/env python2.7
+# First implementation in Python for GDMO syntax parsing
+# vim: et sw=4 sws=4 ts=4 list
+from ply import *
+import gdmolex
+import sys, os.path
+from os.path import exists, join,abspath
+import hashlib
+from gdmolex import *
+from GDMOUtils import *
+
+class Label(object):
+ kind = 'label'
+ name = ""
+ def __init__(self,reference,name,location):
+ self.name = name
+ self.reference = reference
+ self.location = location
+
+ def __eq__(self,other):
+ return self.name == other.name
+
+ def getName(self):
+ return self.name
+
+ def __str__(self):
+ if self.reference == None:
+ return "%s" % (self.name)
+ return "%s:%s" % (self.reference, self.name)
+
+ def __hash__(self):
+ return hash(self.name)
+
+class Typeref(object):
+ def __init__(self,ref1,ref2):
+ self.ref1 = ref1
+ self.ref2 = ref2
+ def getType(self):
+ return self.ref2
+
+ def getScope(self):
+ return self.ref1
+
+ def __str__(self):
+ return "%s.%s" % (self.ref1, self.ref2)
+
+
+class Template(object):
+ kind = 'package'
+
+ def __init__(self,label,oid):
+ self.name = label
+ self.oid = oid
+
+ def real_package(self,p):
+ if p.kind == 'package_template':
+ return p
+ return self.gdmo.getName(p.name)
+
+ def real_attribute(self,attr):
+ if attr.kind == 'attribute_template':
+ return p
+ return self.gdmo.getName(attr.name)
+
+ def __eq__(self,other):
+ return self.name == other.name and self.oid == other.oid
+
+ def getName(self):
+ return self.name
+
+ def getOid(self):
+ return self.oid
+
+ def isRegistered(self):
+ return self.oid != None
+
+class MObject(Template):
+ kind = 'managed_object_template'
+
+ def __init__(self,label,derives,packages,cond_pkgs,oid):
+ Template.__init__(self,label,oid)
+ self.packages = packages
+ self.derives = derives
+ self.cond_packages = cond_pkgs
+
+ def attributes(self,template = True):
+ ''' Get All attributes supported by the managed object class'''
+ def invar(obj,lst):
+ for p in lst:
+ rp = self.real_package(p)
+ np_idx = lst.index(p)
+ np = lst[np_idx + 1] if np_idx + 1 < len(lst) else None
+ if rp.attributes is not None:
+ for a in rp.attributes:
+ idx = rp.attributes.index(a)
+ next = rp.attributes[idx + 1] if idx + 1 < len(rp.attributes) else None
+ if not next and np:
+ nrp = self.real_package(np)
+ if nrp.attributes is not None:
+ next = nrp.attributes[0]
+ if template:
+ yield ( self.real_attribute(a) , next)
+ else:
+ yield ( a , next)
+ pkgs = []
+ if self.packages is not None:
+ pkgs += self.packages
+
+
+ if self.cond_packages is not None:
+ pkgs += self.cond_packages
+
+ for i in invar(self,pkgs):
+ yield i
+
+
+ def first_attribute(self):
+ for a,n in self.attributes():
+ return a
+
+ def resolve(self,parent):
+ parent.setName(self)
+ self.gdmo = parent
+
+ def __str__(self):
+ sc = "\n\tCONDITIONAL PACKAGES "
+ if (self.cond_packages != None):
+ sc = sc + "\n\t, ".join([str(p) for p in self.cond_packages])
+ else:
+ sc = ""
+ sp = "\n\tCHARACTERIZED BY\n\t"
+ if (self.packages != None):
+ sp = sp + "\n\t ".join([str(p) for p in self.packages])
+ else:
+ so = ""
+ sd = "\n\tDERIVED FROM "
+ if (self.derives != None):
+ sd = sd + ", ".join([str(p) for p in self.derives])
+
+ return "%s MANAGED OBJECT CLASS %s %s;%s\n" % (self.name ,sd, sp,sc)
+
+class Package(object):
+ kind = 'package'
+
+ def __init__(self,label , presentif = None):
+ self.name = label
+ self.present_if = presentif
+
+ def isConditional(self):
+ return self.present_if != None
+
+ def setPresentIf(selft , presentif):
+ selft.present_if = presentif
+
+ def __str__(self):
+ return "Pkg %s" % ( str(self.name) )
+
+class PackageTemplate(Template):
+ kind = 'package_template'
+
+
+ def __init__(self,label,attrs,behav,attr_grp,actions,notif,oid):
+ Template.__init__(self,label,oid)
+ self.attributes = attrs
+ self.behaviours = behav
+ self.attr_group = attr_grp
+ self.actions = actions
+ self.notifications = notif
+ self.present_id = None
+
+ def resolve(self,parent):
+ parent.setName(self)
+
+ def __str__(self):
+ sa = "\n\tACTIONS "
+ if (self.actions != None):
+ sa = sa + "\n\t ".join([str(p) for p in self.actions])
+ else:
+ sa = ""
+ sattr = "\n\tATTRIBUTES "
+ if (self.attributes != None):
+ sattr = sattr + "\n, ".join([str(p) for p in self.attributes])
+ else:
+ sattr = ""
+
+ sb = "\n\tBEHAVIOUR "
+ if (self.behaviours != None):
+ sb = sb + "\n ".join([str(p) for p in self.behaviours])
+ else:
+ sb = ""
+ sb+=";"
+ return "%s PACKAGE %s %s %s ;" % ( str(self.name) ,sb, sattr,sa )
+
+ def setPresentIf(selft , presentif):
+ selft.present_if = presentif
+
+ def isConditional(self):
+ return self.present_if != None
+#
+# There is a difference between an Attribute and Attribute Template
+#
+class Attribute(object):
+ kind = 'attribute'
+ prop_get = False
+ prop_replace = False
+ prop_add = False
+ prop_remove = False
+
+ def __init__(self,label,props = None,params = None ):
+ self.name = label
+ self.properties = props
+ self.parameters = params
+ if (self.properties != None):
+ p = self.properties['get_replace']
+ #print ("Property : %s " % (p))
+ if ( p == "GET"):
+ self.prop_get = True
+ if (p == "REPLACE"):
+ self.prop_replace = True
+ if ( p == "GET-REPLACE"):
+ self.prop_replace = True
+ self.prop_get = True
+
+ p = self.properties['add_remove']
+ if ( p == "ADD-REMOVE"):
+ self.prop_add = True
+ self.prop_remove = True
+
+
+ def __str__(self):
+ prop = ""
+ if (self.prop_get):
+ prop = prop + "GET"
+ if (self.prop_replace):
+ prop = prop + " REPLACE"
+ return "%s %s" % ( str(self.name) , prop )
+
+class AttributeTemplate(Template):
+ kind = 'attribute_template'
+
+ def __init__(self,label,typ,match,behav,params,oid):
+ Template.__init__(self,label,oid)
+ self.type = typ
+ self.match = match
+ self.behaviours = behav
+ self.parameters = params
+
+ def resolve(self,parent):
+ parent.setName(self)
+ if self.hasSyntax():
+ parent.scopes.add(self.getSyntax(parent).getScope())
+
+ def hasSyntax(self):
+ return self.type['syntax'] is not None
+
+ def hasDerivedSyntax(self):
+ return self.type['derived'] is not None
+
+ def getDerived(self):
+ return self.type['derived']
+
+ def getSyntax(self,model):
+ if (self.hasDerivedSyntax()):
+ par = model.getName(self.getDerived())
+ return par.getSyntax(model)
+ return self.type['syntax']
+
+
+ def __str__(self):
+ sTy = ""
+ if (self.type['syntax'] != None):
+ sTy = "WITH ATTRIBUTE SYNTAX " + str(self.type['syntax'])
+ else:
+ sTy = "DERIVED FROM " + str(self.type['derived'])
+
+ return "%s ATTRIBUTE\n\t %s " % ( str(self.name) , sTy)
+
+class AttributeGroupeTemplate(Template):
+ kind = 'attribute_groupe_template'
+
+ def __init__(self,label,elements,fixed,desc,oid):
+ Template.__init__(self,label,oid)
+ self.elements = elements
+ self.fixed = fixed
+ self.desc = desc
+
+ def resolve(self,parent):
+ parent.setName(self)
+
+ def __str__(self):
+ se = "\n\tELEMENTS\n\t"
+ if (self.elements != None):
+ se = se + "\n\t, ".join([str(p) for p in self.elements])
+ return "%s ATTRIBUTE GROUP %s" % ( str(self.name),se )
+
+
+class ParameterTemplate(Template):
+ kind = 'parameter_template'
+
+ def __init__(self,label,ctx,type,behavior,oid):
+ Template.__init__(self,label,oid)
+ self.context = ctx
+ self.type = type
+ self.behavior= behavior
+
+ def resolve(self,parent):
+ parent.setName(self)
+
+ def __str__(self):
+ return "%s PARAMETER ;" % ( str(self.name) )
+
+class Parameter(object):
+ kind = 'parameter'
+
+ def __init__(self,label):
+ # Missing parameters
+ self.name = label
+
+
+# Action & Action Template
+class Action(object):
+ kind = 'action'
+
+ def __init__(self,label):
+ # Missing parameters
+ self.name = label
+ def __str__(self):
+ return "ACT(%s)" % ( str(self.name) )
+
+
+class ActionTemplate(Template):
+ kind = 'action_template'
+
+ def __init__(self,label,behaviour,mode,params,wi,wr,oid):
+ Template.__init__(self,label,oid)
+ self.behaviour = behaviour
+ self.mode = mode
+ self.parameters = params
+ self.with_info = wi
+ self.with_reply = wr
+
+ def resolve(self,parent):
+ parent.setName(self)
+
+ def hasWithInfoSyntax(self):
+ return self.wi['INFORMATION'] is not None
+
+ def hasWithReplySyntax(self):
+ return self.wr['REPLY'] is not None
+
+ def getWithInfoSyntax(self):
+ return self.wi['INFORMATION']
+
+ def getWithReplySyntax(self):
+ return self.wi['INFORMATION']
+
+
+ def __str__(self):
+ return "%s ACTION ;" % ( str(self.name) )
+
+# NameBinding Template
+class NameBindingTemplate(Template):
+ kind = 'namebinding_template'
+
+ def __init__(self,label,sup,sub,attr,behav,create,delete,oid):
+ Template.__init__(self,label,oid)
+ self.subordinate = sub['label']
+ self.superior = sup['label']
+ self.attribute = attr
+ self.behaviour = behav
+ self.create = create
+ self.delete = delete
+
+ def __str__(self):
+ ssoc = "SUBORDINATE OBJECT CLASS " + str(self.subordinate)
+ ssup = "SUPERIOR OBJECT CLASS " + str(self.superior)
+ swa = "WITH ATTRIBUTE " + str(self.attribute)
+ return "%s NAME BINDING\n %s;\n NAME BY\n\t%s;\n\t%s;\n " % ( str(self.name) ,ssoc ,ssup,swa )
+
+ def resolve(self,parent):
+ parent.setName(self)
+
+# Behaviour and BehaviourTemplate
+
+class Behaviour(object):
+ kind = 'behaviour'
+
+ def __init__(self,label):
+ self.name =label
+
+ def __str__(self):
+ return "BehaviourTplt(%s)" % ( str(self.name) )
+
+class BehaviourTemplate(Template):
+ kind = 'behavior_template'
+
+ def __init__(self,label,str):
+ Template.__init__(self,label,None)
+ self.message = str
+
+ def resolve(self,parent):
+ parent.setName(self)
+
+ def __str__(self):
+ return "%s BEHAVIOUR\n\tDEFINED AS %s;\n" % ( str(self.name),self.message )
+
+class ObjectIdentifier(object):
+
+ def __init__(self,ids):
+ self.ids = ids
+
+
+class NotificationTemplate(Template):
+ kind = 'notification_template'
+
+ def __init__(self,label,behaviours,params,syntax,reply,oid):
+ Template.__init__(self,label,oid)
+ self.behaviours = behaviours
+ self.params = params
+ self.syntax = syntax
+ self.reply = reply
+
+ def resolve(self,parent):
+ parent.setName(self)
+
+ def __str__(self):
+ return "%s NOTIFICATION" % str(self.name)
+
+class Notification(object):
+ kind = 'notification'
+
+ def __init__(self,label):
+ self.name = label
+
+ def __str__(self):
+ return "Noti(%s)" % ( str(self.name) )
+
+# Map of names and Objects
+class NameMap(object):
+ def __init__(self):
+ self._d = {}
+
+ def __getitem__(self,key):
+ #print("NameMap(%d).__getitem__ <%s> type de key %s" % (len(self._d), key,type(key)))
+ #for i in self._d.keys():
+ # print "NameMap has key %s type = %s" % (i , type(i))
+ return self._d[key]
+
+ def __iter__(self):
+ return self._d.itervalues()
+
+ def __contains__(self,key):
+ return key in self._d
+
+ def __len__(self):
+ return len(self._d)
+
+ def set(self,object):
+ if object.name in self._d:
+ print "NameMap Object Allready in map: <%s>" % object.name
+ pass
+ else:
+ self._d[ object.name ] = object
+
+ def get(self,id):
+ print "NameMap get : %s" % id
+ try:
+ return self[id]
+ except:
+ raise GDMOError('NameMap get error ')
+
+# Missing Location
+class Location(object):
+ _line = False
+
+ def __init__(self,lexer,lineno,lexpos):
+ self._lineno = lineno
+ self._lexpos = lexpos
+ self._lexdata = lexer.lexdata
+ self._file = getattr(lexer,'filename','<unknown>')
+
+ def pointerline(self):
+ def i():
+ for i in xrange(0,self._colno):
+ yield " "
+ yield "^"
+
+ return "".join(i())
+
+ def resolve(self):
+ if (self._line):
+ return
+
+ startofline = self._lexdata.rfind('\n',0,self._lexpos) + 1
+ endofline = self._lexdata.find('\n',self._lexpos,self._lexpos + 80)
+ self._line = self._lexdata[startofline:endofline]
+ self._colno = self._lexpos - startofline
+
+ def __str__(self):
+ self.resolve()
+ return "%s: line %s:%s\n%s\n%s" % (self._file,self._lineno,self._colno
+ ,self._line,self.pointerline())
+
+class GDMOError(Exception):
+ def __init__(self,message,lineno = 0,warning = False):
+ self.message = message
+ self.warning = warning
+ self.lineno = lineno
+
+ def __str__(self):
+ return "GDMO %s (%d): %s " % ( self.warning and 'warning' or 'error' ,self.lineno, self.message)
+
+class TranslationUnit(object):
+ templates = []
+ document = ""
+ alias = []
+ imports = []
+
+ def __init__(self,header,template):
+ self.templates.append(template)
+
+ def addTemplate(self, template):
+ self.templates.append(template)
+
+ def resolve(self,parent):
+ self.gdmo = parent
+ for t in self.templates:
+ if t.kind is 'managed_object_template':
+ parent.mo.append(t)
+ if t.kind is 'attribute_template':
+ parent.attlist.append(t)
+ if t.kind is 'action_template':
+ parent.actions.append(t)
+ if t.kind is 'package_template':
+ parent.packages.append(t)
+ if t.kind is 'notification_template':
+ parent.notifs.append(t)
+ if t.kind is 'namebinding_template':
+ parent.nbindings.append(t)
+ # Needs to be changed
+ t.resolve(parent)
+
+#
+# Object Returned my parser.
+#
+class GDMO(object):
+ templates = []
+ def __init__(self,files):
+ self.files = files
+ self.mo = []
+ self.attlist = []
+ self.actions = []
+ self.packages = []
+ self.notifs = []
+ self.nbindings = []
+ # namespaces for asn1 types
+ self.scopes = set()
+ def setName(self,object):
+ self.namemap.set(object)
+
+ def getName(self,key):
+ try:
+ return self.namemap[key]
+ except KeyError:
+ raise GDMOError("GDMO.getName type %s not found\n%s " % (key,str(key.location )))
+
+ def hasName(self,id):
+ print ("GDMO.hasName %s length de namemap = %d" % (id,len(self.namemap)))
+ return id in self.namemap
+
+ def resolve(self):
+ print "GDMO.resolve resolving ..."
+ self.namemap = NameMap()
+
+ for f in self.files:
+ f.resolve(self)
+
+ print "GDMO.resolve Done"
+ print "\n\tTU %d\n\tMO %d\n\tAT %d\n\tAC %d\n\tNO %d\n" % \
+ (len(self.files),len(self.mo),len(self.attlist),len(self.actions),len(self.notifs))
+ def __str__(self):
+ return "".join([str(p) for p in self.templates])
+
+
+
+#
+# GDMOParser Function. May be I should transform it into a class
+#
+#
+
+class GDMOParser(object):
+
+ tokens = gdmolex.tokens
+
+ def p_gdmo(self,p):
+ '''gdmo : definition_files'''
+ p[0] = GDMO(p[1])
+ p[0].resolve()
+
+ def p_definition_files_init(self,p):
+ '''definition_files : empty'''
+ print ("GDMOParser start empty")
+ p[0] = []
+
+ def p_definition_files_start(self,p):
+ '''definition_files : file'''
+ p[0] = [p[1]]
+
+ def p_definition_files_continue(self,p):
+ '''definition_files : definition_files file'''
+ print "New File "
+ p[0] = list(p[1])
+ p[0].append(p[2])
+
+ def p_file(self,p):
+ '''file : opt_gdmo_header template'''
+ header,template = p[1:]
+ p[0] = TranslationUnit(header,template)
+
+ def p_file_cont(self,p):
+ '''file : file template'''
+ p[0] = p[1]
+ p[0].addTemplate(p[2])
+
+ def p_opt_gdmo_header(self,p):
+ '''opt_gdmo_header : empty
+ | opt_alias gdmo_doc opt_alias'''
+ if len(p) > 2:
+ p[0] = { 'alias' : p[1] , 'document' : p[2] , 'imports' : p[3] }
+
+ def p_opt_alias(self,p):
+ '''opt_alias : empty
+ | gdmo_aliases'''
+ p[0] = p[1]
+
+ def p_gdmo_aliases_start(self,p):
+ '''gdmo_aliases : gdmo_alias'''
+ p[0] = [p[1]]
+
+ def p_gdmo_aliases_next(self,p):
+ '''gdmo_aliases : gdmo_aliases gdmo_alias'''
+ p[0] = list(p[1])
+ p[0].append(p[2])
+
+ def p_gdmo_alias_start(self,p):
+ '''gdmo_alias : ALIAS STRING'''
+ p[0] = [p[2]]
+
+ def p_gdmo_alias_next(self,p):
+ '''gdmo_alias : gdmo_alias STRING'''
+ p[0] = list(p[1])
+ p[0].append(p[2])
+
+ def p_gdmo_doc(self,p):
+ '''gdmo_doc : DOCUMENT STRING'''
+ p[0] = p[2]
+
+ def p_labels_start(self,p):
+ '''labels : label'''
+ p[0] = [p[1]]
+
+ def p_labels_continue(self,p):
+ '''labels : labels COMMA label '''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_label(self,p):
+ '''label : IDENTIFIER'''
+ p[0] = Label(None,p[1],self.getLocation(p,1))
+
+ def p_label_alias(self,p):
+ '''label : alias_label'''
+ p[0] = p[1]
+
+ def p_alias_label(self,p):
+ '''alias_label : STRING COLON IDENTIFIER'''
+ p[0] = Label(p[1],p[3], self.getLocation(p,1) )
+
+ def p_template(self,p):
+ '''template : mo_template
+ | package_template
+ | parameter_template
+ | nbind_template
+ | attribute_template
+ | attribute_group_template
+ | behaviour_template
+ | action_template
+ | notification_template'''
+ p[0] = p[1]
+
+ def p_mo_template(self,p):
+ '''mo_template : mo_header mo_constructs registered_as SIMILICON'''
+ c = p[2]
+ p[0] = MObject(p[1],c['derived'],c['pkgs'],c['cond_pkgs'],p[3])
+
+ def p_mo_header(self,p):
+ ''' mo_header : label MANAGED OBJECT CLASS'''
+ p[0] = p[1]
+
+ def p_registered_as(self,p):
+ '''registered_as : REGISTERED AS oid'''
+ p[0] = Asn1ObjectIdentifier(p[3])
+
+ def p_mo_constructs(self,p):
+ '''mo_constructs : mo_opt_derived \
+ mo_opt_characterized \
+ mo_opt_conditional_pkg '''
+ p[0] = {'derived' : p[1] , 'pkgs' : p[2] , 'cond_pkgs' : p[3]}
+
+ def p_mo_opt_derived(self,p):
+ '''mo_opt_derived : empty
+ | DERIVED FROM labels SIMILICON '''
+ if (len(p)> 3):
+ p[0] = list(p[3])
+
+ def p_mo_opt_characterized(self,p):
+ '''mo_opt_characterized : empty
+ | CHARACTERIZED BY mo_pkgs SIMILICON '''
+ if (len(p) > 3):
+ p[0] = list(p[3])
+
+ def p_mo_pkgs_start(self,p):
+ '''mo_pkgs : mo_pkg'''
+ p[0] = [p[1]]
+
+ def p_mo_pkgs_cont(self,p):
+ '''mo_pkgs : mo_pkgs COMMA mo_pkg '''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_mo_pkg_label(self,p):
+ '''mo_pkg : label'''
+ p[0] = Package(p[1])
+
+ def p_mo_pkg_tplt(self,p):
+ '''mo_pkg : package_template'''
+ p[0] = p[1]
+
+ def p_mo_opt_conditional_pkg(self,p):
+ '''mo_opt_conditional_pkg : empty
+ | CONDITIONAL PACKAGES mo_conditional_pkgs SIMILICON'''
+ if (len(p) > 3):
+ p[0] = list(p[3])
+
+ def p_mo_conditional_pkgs_start(self,p):
+ '''mo_conditional_pkgs : mo_conditional_pkg'''
+ p[0] = [p[1]]
+
+ def p_mo_conditional_pkgs_cont(self,p):
+ '''mo_conditional_pkgs : mo_conditional_pkgs COMMA mo_conditional_pkg'''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_mo_conditional_pkg(self,p):
+ '''mo_conditional_pkg : package_template present_if'''
+ p[1].setPresentIf(p[2])
+ p[0] = p[1]
+
+ def p_mo_conditional_pkg_lbl(self,p):
+ '''mo_conditional_pkg : label present_if'''
+ p[0] = Package(p[1],p[2])
+ p[0].setPresentIf(p[2])
+
+ def p_present_if(self,p):
+ '''present_if : PRESENT IF STRING'''
+ p[0] = p[3]
+
+ def p_package_template(self,p):
+ '''package_template : package_header pkg_constructs SIMILICON'''
+ c= p[2]
+ p[0] = PackageTemplate(p[1] \
+ ,c['attrs'] \
+ ,c['behavior'] \
+ ,c['attr_grp'] \
+ ,c['actions'] \
+ ,c['notifications'] \
+ ,c['oid'] \
+ )
+
+ def p_package_header(self,p):
+ '''package_header : label PACKAGE'''
+ p[0] = p[1]
+
+ def p_pkg_constructs(self,p):
+ '''pkg_constructs : pkg_opt_behaviour \
+ pkg_opt_attrs \
+ pkg_opt_attr_grp \
+ pkg_opt_act \
+ pkg_opt_notif \
+ pkg_opt_registered'''
+ behavior,attrs,attr_grp,actions,notif,registered = p[1:]
+ p[0] = { 'attrs' : attrs \
+ , 'behavior' : behavior \
+ , 'attr_grp' : attr_grp \
+ , 'actions' : actions \
+ , 'notifications': notif \
+ , 'oid' : registered \
+ }
+
+ def p_pkg_opt_behaviour(self,p):
+ '''pkg_opt_behaviour : empty
+ | BEHAVIOUR pkg_behaviour SIMILICON'''
+ if (len(p) > 2):
+ p[0] = p[2]
+
+ def p_pkg_behaviour_start(self,p):
+ '''pkg_behaviour : pkg_behaviour_def'''
+ p[0] = [p[1]]
+
+ def p_pkg_behaviour_cont(self,p):
+ '''pkg_behaviour : pkg_behaviour COMMA pkg_behaviour_def'''
+ p[0] = list(p[1])
+ p[0].append(p[2])
+
+ def p_pkg_behaviour_def(self,p):
+ '''pkg_behaviour_def : label
+ | behaviour_template'''
+ p[0] = p[1]
+
+ def p_pkg_opt_attrs(self,p):
+ '''pkg_opt_attrs : empty
+ | ATTRIBUTES pkg_attributes SIMILICON'''
+ if (len(p) > 2):
+ p[0] = p[2]
+
+ def p_pkg_attributes_start(self,p):
+ '''pkg_attributes : pkg_attribute'''
+ p[0] = [p[1]]
+
+ def p_pkg_attributes_cont(self,p):
+ '''pkg_attributes : pkg_attributes COMMA pkg_attribute'''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_pkg_attribute(self,p):
+ '''pkg_attribute : label pkg_attr_proplist pkg_attr_param_labels'''
+ p[0] = Attribute(p[1],p[2],p[3])
+
+ def p_pkg_attr_proplist(self,p):
+ '''pkg_attr_proplist : pkg_proplist_replace_default \
+ pkg_proplist_default \
+ pkg_proplist_initial \
+ pkg_proplist_permitted \
+ pkg_proplist_required \
+ pkg_proplist_get_replace \
+ pkg_proplist_add_remove \
+ pkg_proplist_set_by_created \
+ pkg_proplist_no_modify'''
+ p[0] = { 'default' : p[2] \
+ , 'replace_default': p[1] \
+ , 'initial' : p[3] \
+ , 'permitted' : p[4] \
+ , 'required' : p[5] \
+ , 'get_replace' : p[6] \
+ , 'add_remove' : p[7] \
+ , 'set_by_created' : p[8] \
+ , 'set_no_modify' : p[9] \
+ }
+
+ def p_pkg_proplist_replace_default(self,p):
+ '''pkg_proplist_replace_default : empty
+ | REPLACE_WITH_DEFAULT pkg_proplist_default'''
+
+ def p_pkg_proplist_default(self,p):
+ '''pkg_proplist_default : empty
+ | DEFAULT VALUE pkg_proplist_value_spec'''
+ if (len(p) > 2):
+ p[0] = p[3]
+
+ def p_pkg_proplist_initial(self,p):
+ '''pkg_proplist_initial : empty
+ | INITIAL VALUE pkg_proplist_value_spec'''
+ if (len(p) > 2):
+ p[0] = p[3]
+
+ def p_pkg_proplist_value_spec_ident(self,p):
+ '''pkg_proplist_value_spec : IDENTIFIER'''
+ p[0] = p[1]
+
+ def p_pkg_proplist_value_spec_tv(self,p):
+ '''pkg_proplist_value_spec : TYPEREF DOT IDENTIFIER'''
+
+ def p_pkg_proplist_value_spec_tt(self,p):
+ '''pkg_proplist_value_spec : TYPEREF DOT TYPEREF'''
+ p[0] = Typeref(p[1],p[3])
+
+ def p_pkg_proplist_value_spec_dr(self,p):
+ '''pkg_proplist_value_spec : DERIVATION RULE'''
+ p[0] = p[1]
+
+ def p_pkg_proplist_permitted(self,p):
+ '''pkg_proplist_permitted : empty
+ | PERMITTED VALUES pkg_proplist_typeref '''
+ if (len(p) > 2):
+ p[0] = p[3]
+
+ def p_pkg_proplist_required(self,p):
+ '''pkg_proplist_required : empty
+ | REQUIRED VALUES pkg_proplist_typeref'''
+ if (len(p) > 2):
+ p[0] = p[3]
+
+ def p_pkg_proplist_typeref(self,p):
+ '''pkg_proplist_typeref : TYPEREF DOT TYPEREF'''
+ p[0] = Typeref(p[1],p[3])
+
+
+ def p_pkg_proplist_get_replace(self,p):
+ '''pkg_proplist_get_replace : empty
+ | GET
+ | GET_REPLACE
+ | REPLACE'''
+ p[0] = p[1]
+
+ def p_pkg_proplist_add_remove(self,p):
+ '''pkg_proplist_add_remove : empty
+ | ADD
+ | REMOVE
+ | ADD_REMOVE'''
+ p[0] = p[1]
+
+ def p_pkg_proplist_set_by_created(self,p):
+ '''pkg_proplist_set_by_created : empty
+ | SET_BY_CREATE'''
+ p[0] = p[1]
+
+ def p_pkg_proplist_no_modify(self,p):
+ '''pkg_proplist_no_modify : empty
+ | NO_MODIFY'''
+ p[0] = p[1]
+
+ def p_pkg_attr_param_labels_empty(self,p):
+ '''pkg_attr_param_labels : empty'''
+ p[0] = None
+
+ def p_pkg_attr_param_labels_start(self,p):
+ '''pkg_attr_param_labels : pkg_attr_param_label'''
+ p[0] = [p[1]]
+
+ def p_pkg_attr_param_labels_cont(self,p):
+ '''pkg_attr_param_labels : pkg_attr_param_labels pkg_attr_param_label'''
+ p[0] = list(p[1])
+ p[0].append(p[2])
+
+ def p_pkg_attr_param_label(self,p):
+ '''pkg_attr_param_label : label '''
+ p[0] = Parameter(p[1])
+
+ def p_pkg_opt_attr_grp(self,p):
+ '''pkg_opt_attr_grp : empty
+ | ATTRIBUTE GROUP'''
+
+ def p_pkg_opt_act(self,p):
+ '''pkg_opt_act : empty
+ | ACTIONS pkg_actions SIMILICON'''
+ if (len(p) > 2):
+ p[0] = p[2]
+
+ def p_pkg_actions_start(self,p):
+ '''pkg_actions : pkg_action'''
+ p[0] = [p[1]]
+
+ def p_pkg_actions_cont(self,p):
+ '''pkg_actions : pkg_actions COMMA pkg_action'''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_pkg_action(self,p):
+ '''pkg_action : label pkg_attr_param_labels'''
+ p[0] = Action(p[1])
+
+ def p_pkg_opt_notif(self,p):
+ '''pkg_opt_notif : empty
+ | NOTIFICATIONS pkg_notification SIMILICON'''
+ if (len(p) > 2):
+ p[0] = p[2]
+
+ def p_pkg_notification_start(self,p):
+ '''pkg_notification : pkg_notif_item'''
+ p[0] = [p[1]]
+
+ def p_pkg_notification_cont(self,p):
+ '''pkg_notification : pkg_notification COMMA pkg_notif_item'''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_pkg_notif_item(self,p):
+ '''pkg_notif_item : label pkg_attr_param_labels'''
+ p[0] = Notification(p[1])
+
+ def p_pkg_opt_registered(self,p):
+ '''pkg_opt_registered : empty
+ | registered_as'''
+
+ def p_parameter_template(self,p):
+ '''parameter_template : parameter_header CONTEXT param_ctx_type SIMILICON \
+ param_syntax_or_attr SIMILICON \
+ param_opt_behaviour \
+ param_opt_register SIMILICON'''
+ label,CTX,ctx,SIMI,type,SIMI1,behavior,oid,SIMI2 = p[1:]
+ p[0] = ParameterTemplate(label,ctx,type,behavior,oid)
+
+ def p_parameter_header(self,p):
+ '''parameter_header : label PARAMETER'''
+ p[0] = p[1]
+
+ def p_param_ctx_type(self,p):
+ '''param_ctx_type : ACTION_INFO
+ | ACTION_REPLY
+ | EVENT_INFO
+ | EVENT_REPLY
+ | SPECIFIC_ERROR
+ | TYPEREF DOT IDENTIFIER'''
+ if (len (p) > 2):
+ p[0] = Typeref(p[1],p[3])
+ print "Error param_ctx_type to sovle"
+ else:
+ p[0] = p[1]
+
+ def p_param_syntax_or_attr(self,p):
+ '''param_syntax_or_attr : WITH SYNTAX pkg_proplist_typeref
+ | ATTRIBUTE label ''';
+ if (len (p) > 2):
+ p[0] = p[3]
+ else:
+ p[0] = Attribute(p[2])
+
+ def p_param_opt_behaviour(self,p):
+ '''param_opt_behaviour : empty
+ | BEHAVIOUR param_behaviours SIMILICON'''
+ if (len(p) > 2 ):
+ p[0] = p[2]
+
+ def p_param_behaviours_start(self,p):
+ '''param_behaviours : param_behaviour'''
+ p[0] = [p[1]]
+
+ def p_param_behaviours_cont(self,p):
+ '''param_behaviours : param_behaviours COMMA param_behaviour'''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_param_behaviour_lbl(self,p):
+ '''param_behaviour : label'''
+ p[0] = Behaviour(p[1])
+
+ def p_param_behaviour(self,p):
+ '''param_behaviour : behaviour_template'''
+ p[0] = p[1]
+
+ def p_param_opt_register(self,p):
+ '''param_opt_register : empty
+ | registered_as'''
+ p[0] = p[1]
+
+ def p_nbind_template(self,p):
+ '''nbind_template : nbind_header nbind_core'''
+ c = p[2]
+ # Missing oid and the rest
+ p[0] = NameBindingTemplate(p[1],
+ c['superior'],
+ c['subordinate'],
+ c['attribute'],
+ c['behavior'],
+ c['create'],
+ c['delete'],
+ c['oid'])
+
+ def p_nbind_header(self,p):
+ '''nbind_header : label NAME BINDING'''
+ p[0] = p[1]
+
+ def p_nbind_core(self,p):
+ '''nbind_core : SUBORDINATE OBJECT CLASS label opt_subclass SIMILICON \
+ NAMED BY SUPERIOR OBJECT CLASS label opt_subclass SIMILICON \
+ WITH ATTRIBUTE nbind_attrs SIMILICON \
+ nbind_opt_behavior \
+ nbind_opt_create \
+ nbind_opt_delete \
+ registered_as SIMILICON'''
+ p[0] = { 'subordinate' : { 'label' : p[4] ,'subclasses' : p[5] } \
+ ,'superior' : { 'label' : p[12] ,'subclasses' : p[13] } \
+ ,'attribute' : p[17] \
+ ,'behavior' : p[19] \
+ ,'create' : p[20] \
+ ,'delete' : p[21] \
+ ,'oid' : p[22] \
+ }
+
+ def p_nbind_attrs(self,p):
+ '''nbind_attrs : label'''
+ p[0] = Attribute(p[1])
+
+ def p_opt_subclass(self,p):
+ '''opt_subclass : empty
+ | AND SUBCLASSES'''
+
+ def p_nbind_opt_behavior(self,p):
+ '''nbind_opt_behavior : empty
+ | BEHAVIOUR nbind_behaviours SIMILICON'''
+ if (len(p) > 2):
+ p[0] = p[2]
+
+ def p_nbind_behaviours(self,p):
+ '''nbind_behaviours : nbind_behaviour
+ | nbind_behaviours COMMA nbind_behaviour'''
+
+ def p_nbind_behaviour_lbl(self,p):
+ '''nbind_behaviour : label'''
+ p[0] = Behaviour(p[1])
+
+ def p_nbind_behaviour_bhv(self,p):
+ '''nbind_behaviour : behaviour_template'''
+ p[0] = p[1]
+
+ def p_nbind_opt_create(self,p):
+ '''nbind_opt_create : empty
+ | CREATE nbind_create_modifiers nbind_opt_params SIMILICON'''
+ if ( len(p) > 3 ):
+ p[0] = { 'modifiers' : p[2] , 'parameters' : p[3] }
+
+ def p_nbind_opt_params(self,p):
+ '''nbind_opt_params : empty
+ | nbind_params'''
+ p[0] = p[1]
+
+ def p_nbind_params_start(self,p):
+ '''nbind_params : label'''
+ p[0] = [Parameter(p[1])]
+
+ def p_nbind_params(self,p):
+ '''nbind_params : nbind_params label'''
+ p[0] = list(p[1])
+ p[0].append(Parameter(p[2]))
+
+ def p_nbind_create_modifiers_start(self,p):
+ '''nbind_create_modifiers : nbind_create_modifier'''
+ p[0] = [p[1]]
+
+ def p_nbind_create_modifiers(self,p):
+ '''nbind_create_modifiers : nbind_create_modifiers COMMA nbind_create_modifier'''
+ p[0] = list(p[1])
+ if p[3] != None:
+ p[0].append(p[3])
+
+ def p_nbind_create_modifier(self,p):
+ '''nbind_create_modifier : empty
+ | WITH_REFERENCE_OBJECT
+ | WITH_AUTOMATIC_INSTANCE_NAMING'''
+ p[0] = [p[1]]
+
+ def p_nbind_opt_delete(self,p):
+ '''nbind_opt_delete : empty
+ | DELETE nbind_del_modifier nbind_opt_params SIMILICON'''
+ if ( len(p) > 3 ):
+ p[0] = { 'modifiers' : p[2] , 'parameters' : p[3] }
+
+ def p_nbind_del_modifier(self,p):
+ '''nbind_del_modifier : empty
+ | ONLY_IF_NO_CONTAINED_OBJECTS
+ | DELETES_CONTAINED_OBJECTS'''
+ p[0] = [p[1]]
+
+ def p_attribute_template(self,p):
+ '''attribute_template : attribute_header derive_or_syntax SIMILICON \
+ attr_opt_match attr_opt_behaviour attr_opt_parameters \
+ attr_opt_registered SIMILICON'''
+ p[0] = AttributeTemplate(p[1],p[2],p[4],p[5],p[6],p[7])
+
+ def p_attribute_header(self,p):
+ '''attribute_header : label ATTRIBUTE'''
+ p[0] = p[1]
+
+ def p_derive_or_syntax(self,p):
+ '''derive_or_syntax : DERIVED FROM label
+ | WITH ATTRIBUTE SYNTAX pkg_proplist_typeref'''
+ if (len(p) > 4):
+ p[0] = {'derived' : None , 'syntax' : p[4] }
+ else:
+ p[0] = {'derived' : p[3] , 'syntax' : None }
+
+ def p_attr_opt_match(self,p):
+ '''attr_opt_match : empty
+ | MATCHES FOR attr_qualifiers SIMILICON'''
+ if (len(p) > 3):
+ p[0] = p[3]
+
+ def p_attr_qualifiers_start(self,p):
+ '''attr_qualifiers : attr_qualifier'''
+ p[0] = [p[1]]
+
+ def p_attr_qualifiers_cont(self,p):
+ '''attr_qualifiers : attr_qualifiers COMMA attr_qualifier'''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_attr_qualifier(self,p):
+ '''attr_qualifier : EQUALITY
+ | ORDERING
+ | SUBSTRINGS
+ | SET_COMPARISON
+ | SET_INTERSECTION'''
+ p[0] = p[1]
+
+ def p_attr_opt_behaviour(self,p):
+ '''attr_opt_behaviour : empty
+ | BEHAVIOUR attr_behaviours SIMILICON'''
+ if (len(p) > 2):
+ p[0] = p[2]
+
+ def p_attr_behaviours_start(self,p):
+ '''attr_behaviours : attr_behaviour'''
+ p[0] = [p[1]]
+
+ def p_attr_behaviours_cont(self,p):
+ '''attr_behaviours : attr_behaviours COMMA attr_behaviour'''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_attr_behaviour_lbl(self,p):
+ '''attr_behaviour : label'''
+ p[0] = Behaviour(p[1])
+
+ def p_attr_behaviour_bh(self,p):
+ '''attr_behaviour : behaviour_template'''
+ p[0] = p[1]
+
+ def p_attr_opt_parameters(self,p):
+ '''attr_opt_parameters : empty
+ | PARAMETERS attr_parameters SIMILICON'''
+ if (len(p) > 2):
+ p[0] = p[2]
+
+ def p_attr_parameters_start(self,p):
+ '''attr_parameters : label'''
+ p[0] = [p[1]]
+ def p_attr_parameters_cont(self,p):
+ '''attr_parameters : attr_parameters COMMA label'''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_attr_opt_registered(self,p):
+ '''attr_opt_registered : empty
+ | registered_as'''
+ p[0] = p[1]
+
+ def p_attribute_group_template(self,p):
+ '''attribute_group_template : attribute_group_header attr_grp_elems attr_grp_fixed attr_grp_desc registered_as SIMILICON'''
+ name , elements,fixed,desc,oid ,SIM = p[1:]
+ p[0] = AttributeGroupeTemplate(name,elements,fixed,desc,oid)
+
+ def p_attribute_group_header(self,p):
+ '''attribute_group_header : label ATTRIBUTE GROUP'''
+ p[0] = p[1]
+
+ def p_attr_grp_elems(self,p):
+ '''attr_grp_elems : empty
+ | GROUP ELEMENTS labels SIMILICON'''
+ if (len(p) > 3):
+ p[0] = p[3]
+
+ def p_attr_grp_fixed(self,p):
+ '''attr_grp_fixed : empty
+ | FIXED SIMILICON '''
+ p[0] = p[1]
+
+ def p_attr_grp_desc(self,p):
+ '''attr_grp_desc : empty
+ | DESCRIPTION STRING SIMILICON'''
+ if (len(p) > 2):
+ p[0] = p[2]
+
+ def p_behaviour_template(self,p):
+ '''behaviour_template : behaviour_header behaviour_construct'''
+ p[0] = BehaviourTemplate(p[1],p[2])
+
+ def p_behaviour_header(self,p):
+ '''behaviour_header : label BEHAVIOUR'''
+ p[0] = p[1]
+
+ def p_behaviour_construct(self,p):
+ '''behaviour_construct : DEFINED AS STRING SIMILICON'''
+ p[0] = p[3]
+
+ def p_action_template(self,p):
+ '''action_template : action_header act_opt_behaviour act_opt_mode act_opt_params act_opt_with act_opt_with registered_as SIMILICON'''
+ label,behaviour,mode,params,wi,wr,oid,SIM = p[1:]
+ p[0] = ActionTemplate(label,behaviour,mode,params,wi,wr,oid)
+
+ def p_action_header(self,p):
+ '''action_header : label ACTION'''
+ p[0] = p[1]
+
+ def p_act_opt_behaviour(self,p):
+ '''act_opt_behaviour : empty
+ | BEHAVIOUR act_behaviours SIMILICON'''
+ if (len(p) > 2):
+ p[0] = p[2]
+
+ def p_act_behaviours_start(self,p):
+ '''act_behaviours : act_behaviour'''
+ p[0] = [p[1]]
+
+ def p_act_behaviours_cont(self,p):
+ '''act_behaviours : act_behaviours COMMA act_behaviour'''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_act_behaviour_lbl(self,p):
+ '''act_behaviour : label'''
+ p[0] = Behaviour(p[1])
+
+ def p_act_behaviour_tlpt(self,p):
+ '''act_behaviour : behaviour_template'''
+ p[0] = p[1]
+
+ def p_act_opt_mode(self,p):
+ '''act_opt_mode : empty
+ | MODE CONFIRMED SIMILICON'''
+
+ def p_act_opt_params(self,p):
+ '''act_opt_params : empty
+ | PARAMETERS act_parameters SIMILICON'''
+ if (len(p)>2):
+ p[0] = p[2]
+
+ def p_act_parameters_start(self,p):
+ '''act_parameters : act_parameter'''
+ p[0] = [p[1]]
+
+ def p_act_parameters_cont(self,p):
+ '''act_parameters : act_parameters COMMA act_parameter'''
+ p[0] = list(p[1])
+ p[0].append(p[3])
+
+ def p_act_parameter_lbl(self,p):
+ '''act_parameter : label'''
+ p[0] = Parameter(p[1])
+
+ def p_act_parameter_tplt(self,p):
+ '''act_parameter : parameter_template'''
+ p[0] = p[1]
+
+ def p_act_opt_with(self,p):
+ '''act_opt_with : empty
+ | WITH act_with_info_reply'''
+ if (len(p)>2):
+ p[0] = p[2]
+
+ def p_act_with_info_reply_info(self,p):
+ '''act_with_info_reply : INFORMATION SYNTAX pkg_proplist_typeref SIMILICON'''
+ p[0] = { p[1] : p[3] }
+
+ def p_act_with_info_reply_reply(self,p):
+ '''act_with_info_reply : REPLY SYNTAX pkg_proplist_typeref SIMILICON'''
+ p[0] = { p[1] : p[3] }
+
+ def p_notification_template(self,p):
+ '''notification_template : notification_header notif_opt_behaviour notif_opt_params\
+ notif_opt_with \
+ opt_notif_reply registered_as SIMILICON'''
+ behav,params,syntax,reply,oid,SIM = p[2:]
+ p[0] = NotificationTemplate(p[1],behav,params,syntax,reply,oid)
+
+ def p_notification_header(self,p):
+ '''notification_header : label NOTIFICATION'''
+ p[0] = p[1]
+
+ def p_notif_opt_behaviour(self,p):
+ '''notif_opt_behaviour : empty
+ | BEHAVIOUR notif_behaviours SIMILICON'''
+ if (len(p)>2):
+ p[0] = p[2]
+
+ def p_notif_behaviours_start(self,p):
+ '''notif_behaviours : notif_behaviour'''
+ p[0] = [p[1]]
+
+ def p_notif_behaviours_cont(self,p):
+ '''notif_behaviours : notif_behaviours COMMA notif_behaviour'''
+ p[0] = list(P[1])
+ p[0].append(p[3])
+
+ def p_notif_behaviour_lbl(self,p):
+ '''notif_behaviour : label'''
+ p[0] = Behaviour(p[1])
+
+ def p_notif_behaviour_bh(self,p):
+ '''notif_behaviour : behaviour_template'''
+ p[0] = p[1]
+
+ def p_notif_opt_params(self,p):
+ '''notif_opt_params : empty
+ | PARAMETERS notif_parameters SIMILICON'''
+ if (len(p)>2):
+ p[0] = p[2]
+
+ def p_notif_parameters_start(self,p):
+ '''notif_parameters : label'''
+ p[0] = [Parameter(p[1])]
+
+ def p_notif_parameters_cont(self,p):
+ '''notif_parameters : notif_parameters COMMA label'''
+ p[0] = list(P[1])
+ p[0].append(Parameter(p[3]))
+
+ def p_notif_opt_with(self,p):
+ '''notif_opt_with : empty
+ | WITH notif_info'''
+ if ( len(p ) > 1):
+ p[0] = p[2]
+
+ def p_notif_info(self,p):
+ '''notif_info : INFORMATION SYNTAX pkg_proplist_typeref notif_opt_attr_id SIMILICON'''
+ p[0] = {'syntax' : p[3] , 'attributes' : p[4] }
+
+ def p_opt_notif_reply(self,p):
+ '''opt_notif_reply : empty
+ | WITH REPLY SYNTAX pkg_proplist_typeref SIMILICON'''
+ if ( len(p) > 4):
+ p[0] = p[4]
+
+ def p_notif_opt_attr_id(self,p):
+ '''notif_opt_attr_id : empty
+ | AND ATTRIBUTE IDS notif_opt_fields'''
+ if (len(p) > 3):
+ p[0] = p[4]
+
+ def p_notif_opt_fields(self,p):
+ '''notif_opt_fields : empty
+ | notif_fields'''
+ p[0] = p[1]
+
+ def p_notif_fields_start(self,p):
+ '''notif_fields : notif_field'''
+ p[0] = [p[1]]
+
+ def p_notif_fields_cont(self,p):
+ '''notif_fields : notif_fields COMMA notif_field '''
+ p[0] = list(p[1])
+ p[0].append(p[2])
+
+ def p_notif_field(self,p):
+ '''notif_field : IDENTIFIER label'''
+ p[0] = p[1]
+
+ def p_oid(self,p):
+ '''oid : LBRACET oid_list RBRACET'''
+ p[0] = p[2]
+
+ def p_oid_list_start(self,p):
+ '''oid_list : oid_item'''
+ p[0] = [p[1]]
+
+ def p_oid_list_cont(self,p):
+ '''oid_list : oid_list oid_item'''
+ p[0] = list(p[1])
+ p[0].append(p[2])
+
+ def p_oid_item_id(self,p):
+ '''oid_item : IDENTIFIER'''
+ p[0] = p[1]
+
+ def p_oid_item_idnum(self,p):
+ '''oid_item : IDENTIFIER LPARENT NUM RPARENT'''
+ p[0] = p[3]
+
+ def p_oid_item_num(self,p):
+ '''oid_item : NUM'''
+ p[0] = p[1]
+
+ def p_oid_item_module_dot_ident(self,p):
+ '''oid_item : TYPEREF DOT IDENTIFIER '''
+ p[0] = p[1]
+
+ def p_empty(self,t):
+ 'empty :'
+ pass
+
+ # Catastrophic error handler
+
+ def p_error(self,p):
+ if not p:
+ raise GDMOError(" parser SYNTAX ERROR AT EOF")
+ print "p_error ERROR at line:%d %s\n" % (p.lineno,p.value)
+
+ raise GDMOError(" Syntax error ",p.lineno )
+
+ def __init__(self,outputdir='',debug = 0):
+ self.lexer = gdmolex.GDMOLexer(outputdir,debug)
+
+ self.parser = yacc.yacc(module = self,
+ debug = debug,
+ outputdir = outputdir,
+ tabmodule ='gdmoparseryacc')
+
+ def search_file(self,filename):
+ found = 0
+ for path in self.incdirs:
+ if exists(join(path,filename)):
+ found = 1
+ break
+ if found:
+ return abspath(join(path,filename))
+ return None
+
+ def parse(self,data, debug=0):
+ self.parser.error = 0
+ p = self.parser.parse(data, debug=debug)
+ if self.parser.error:
+ return None
+ return p
+
+ def parse_files(self,files,incdirs,debug = 0):
+ self.parser.error = 0
+ self.incdirs = incdirs
+ first = files.pop()
+ self.lexer.filename = first
+ self.lexer.files = files
+ self.lexer.parser = self
+ fd = open(self.search_file(first))
+ p = self.parser.parse(fd.read(), debug=debug)
+ fd.close()
+ if self.parser.error:
+ return None
+ return p
+
+ def getLocation(self,p,i):
+ return Location(self.lexer,p.lineno(i),p.lexpos(i))
+
+
+if __name__ == '__main__':
+ from optparse import OptionParser
+ o = OptionParser()
+ o.add_option('-I',action='append',dest='incdirs',default=['.'],
+ help="Directory to search for imported files")
+
+ o.add_option('-D',dest='outdir',default=None,
+ help="Directory to generate files")
+
+ o.add_option('-d',action='store_true', dest='debug', default=False,
+ help='Set Debug for parser')
+
+ o.add_option('-f',action="append",dest='files', default=[],
+ help="File to parse")
+
+ options,args = o.parse_args()
+
+ if options.outdir is not None:
+ if not os.path.isdir(options.outdir):
+ os.mkdir(options.outdir)
+
+ if options.files is None:
+ print >>sys.stderr, "-f file required"
+ sys.exit(1)
+
+ print "GDMO Parsing Start"
+ p = GDMOParser()
+ p.parse_files(options.files,0);