def __init__(self,text): #print "Parsing: '{}'".format(text) self.lexer=Lexer(text) self.root=Node(self.expect('IDENT')) self.expect('EQUALS') self.parse(self.root,'')
class Parser: def __init__(self,text): #print "Parsing: '{}'".format(text) self.lexer=Lexer(text) self.root=Node(self.expect('IDENT')) self.expect('EQUALS') self.parse(self.root,'') def expect(self,token): t,v=self.lexer.analyze() if t!=token: rest=self.lexer.get_rest() raise ParseException("Syntax error. Expected {}, got {}\n{}".format(token,t,rest)) return v def readUntil(self,token): res='' while True: t,v=self.lexer.analyze(True) if t==token: break res=res+v return res def parseName(self,tagdepth): name='' try: while True: t,v=self.lexer.analyze(True) if t=='LTAG': tagdepth=tagdepth+1 if t=='RTAG': tagdepth=tagdepth-1 if tagdepth<1: break name=name+v except EndOfText: t="END" return name def parse(self,node,initvalue): value=initvalue tagdepth=0 try: while True: t,v=self.lexer.analyze() if t=='LTAG': tagdepth=tagdepth+1 if t=='RTAG': tagdepth=tagdepth-1 if tagdepth==0: if not t or t=='COMMA' or t=='RBRACE': break if t=='LBRACE': value='struct' self.lexer.push() self.parse_children(node) break if t=='EQUALS': self.parse_children(node) break if len(value)>0: value=value+' ' value=value+v node.value=value except EndOfText,e: t="END" node.value=value return (value,t)
class Parser: def __init__(self,text): #print "Parsing: '{}'".format(text) self.lexer=Lexer(text) self.root=Node(self.expect('IDENT')) self.expect('EQUALS') self.parse(self.root,'') def expect(self,token): t,v=self.lexer.analyze() if t!=token: rest=self.lexer.get_rest() raise ParseException("Syntax error. Expected {}, got {}\n{}".format(token,t,rest)) return v def readUntil(self,token): res='' while True: t,v=self.lexer.analyze(True) if t==token: break res=res+v return res def parseName(self,tagdepth): name='' try: while True: t,v=self.lexer.analyze(True) if t=='LTAG': tagdepth=tagdepth+1 if t=='RTAG': tagdepth=tagdepth-1 if tagdepth<1: break name=name+v except EndOfText: t="END" return name def parse(self,node,initvalue): value=initvalue tagdepth=0 try: while True: t,v=self.lexer.analyze() if t=='LTAG': tagdepth=tagdepth+1 if t=='RTAG': tagdepth=tagdepth-1 if tagdepth==0: #if not t or t=='COMMA' or t=='RBRACE': if not t or t=='RBRACE': break if t=='LBRACE': value='struct' self.lexer.push() self.parse_children(node) break if t=='EQUALS': self.parse_children(node) break if len(value)>0: value=value+' ' value=value+v node.value=value except EndOfText: t="END" node.value=value if node.value=='struct' and len(node.children)==1 and node.children[0].name.startswith('std::') and node.children[0].value: node.value=node.children[0].value node.children=[] return (value,t) def parse_children(self,node): self.expect('LBRACE') count=0 while True: (t,v)=self.lexer.analyze() if t=='COMMA': continue if t=='RBRACE': break if t=='LBRACE': self.lexer.push() child=Node(str(count)) count=count+1 node.add_child(child) self.parse_children(child) continue elif t=='LBRACKET': parts=[] while t!='RBRACKET': t,name=self.lexer.analyze() if t!='RBRACKET': parts.append(name) #self.expect('RBRACKET') self.expect('EQUALS') child=Node(''.join(parts)) v='' elif t=='LTAG': # base class name=self.parseName(1) if name=='No data fields': continue child=Node(name) self.expect('EQUALS') v='' elif t=='IDENT': v=v+self.readUntil('EQUALS') child=Node(v) v='' else: child=Node(str(count)) count=count+1 node.add_child(child) (cv,t)=self.parse(child,v) if t=='RBRACE': break if not node.value and len(node.children)>0: for c in node.children: if node.value: node.value=node.value+", " if c.value: node.value=node.value+c.value else: node.value=node.value+'{}'
def __init__(self, text): seq = [Node(t.token, t.value) for t in Lexer(text).all()] collapse_all(seq) self.root = self.parse(seq)