def ClauseD(self, rule,tokens, _parent=None): _context = self.Context(_parent, self._scanner, 'ClauseD', [rule,tokens]) _token = self._peek('STR', 'ID', 'LP', 'LB', 'STMT', context=_context) if _token == 'STR': STR = self._scan('STR', context=_context) t = (STR, eval(STR,{},{})) if t not in tokens: tokens.insert( 0, t ) return parsetree.Terminal(rule, STR) elif _token == 'ID': ID = self._scan('ID', context=_context) OptParam = self.OptParam(_context) return resolve_name(rule,tokens, ID, OptParam) elif _token == 'LP': LP = self._scan('LP', context=_context) ClauseA = self.ClauseA(rule,tokens, _context) RP = self._scan('RP', context=_context) return ClauseA elif _token == 'LB': LB = self._scan('LB', context=_context) ClauseA = self.ClauseA(rule,tokens, _context) RB = self._scan('RB', context=_context) return parsetree.Option(rule, ClauseA) else: # == 'STMT' STMT = self._scan('STMT', context=_context) return parsetree.Eval(rule, STMT[2:-2])
def ClauseD(self, rule, tokens, _parent=None): _context = self.Context(_parent, self._scanner, self._pos, 'ClauseD', [rule, tokens]) _token = self._peek() if _token == 'STR': STR = self._scan('STR') t = (STR, eval(STR, {}, {})) if t not in tokens: tokens.insert(0, t) return parsetree.Terminal(rule, STR) elif _token == 'ID': ID = self._scan('ID') OptParam = self.OptParam(_context) return resolve_name(rule, tokens, ID, OptParam) elif _token == 'LP': LP = self._scan('LP') ClauseA = self.ClauseA(rule, tokens, _context) RP = self._scan('RP') return ClauseA elif _token == 'LB': LB = self._scan('LB') ClauseA = self.ClauseA(rule, tokens, _context) RB = self._scan('RB') return parsetree.Option(rule, ClauseA) elif _token == 'STMT': STMT = self._scan('STMT') return parsetree.Eval(rule, STMT[2:-2]) else: raise yappsrt.SyntaxError(_token[0], 'Could not match ClauseD')
def resolve_name(rule, tokens, id, args): if id in [x[0] for x in tokens]: # It's a token if args: print('Warning: ignoring parameters on TOKEN %s<<%s>>' % (id, args)) return parsetree.Terminal(rule, id) else: # It's a name, so assume it's a nonterminal return parsetree.NonTerminal(rule, id, args)