def Tokens(self, _parent=None): _context = self.Context(_parent, self._scanner, self._pos, 'Tokens', []) tok = [] while self._peek() in ['"token"', '"ignore"']: _token = self._peek() if _token == '"token"': self._scan('"token"') ID = self._scan('ID') self._scan('":"') Str = self.Str(_context) tok.append((ID, Str)) elif _token == '"ignore"': self._scan('"ignore"') self._scan('":"') Str = self.Str(_context) tok.append(('#ignore', Str)) else: raise yappsrt.SyntaxError(_token[0], 'Could not match Tokens') if self._peek() not in ['"token"', '"ignore"', 'EOF', '"rule"']: raise yappsrt.SyntaxError( charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['"token"', '"ignore"', 'EOF', '"rule"'])) return tok
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 OptParam(self, _parent=None): _context = self.Context(_parent, self._scanner, self._pos, 'OptParam', []) _token = self._peek() if _token == 'ATTR': ATTR = self._scan('ATTR') return ATTR[2:-2] elif _token not in [ '"ignore"', '"token"', '"option"', '"parser"', 'COLON' ]: return '' else: raise yappsrt.SyntaxError(_token[0], 'Could not match OptParam')
def ClauseA(self, rule, tokens, _parent=None): _context = self.Context(_parent, self._scanner, self._pos, 'ClauseA', [rule, tokens]) ClauseB = self.ClauseB(rule, tokens, _context) v = [ClauseB] while self._peek() == 'OR': OR = self._scan('OR') ClauseB = self.ClauseB(rule, tokens, _context) v.append(ClauseB) if self._peek() not in ['OR', 'RP', 'RB', '"rule"', 'EOF']: raise yappsrt.SyntaxError( charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['OR', 'RP', 'RB', '"rule"', 'EOF'])) return cleanup_choice(rule, v)
def factor(self, _parent=None): _context = self.Context(_parent, self._scanner, self._pos, 'factor', []) funccall = self.funccall(_context) v = funccall while self._peek('MUL', 'DIV', 'PLUS', 'SUB', 'COMMA', 'CLOSEB', 'EQUAL', 'END', 'OPENB', 'IN', 'RD', 'INP', 'RDP', 'COLLECT', 'COPYCOLLECT', 'INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE') in ['MUL', 'DIV']: _token = self._peek('MUL', 'DIV') if _token == 'MUL': MUL = self._scan('MUL') factor = self.factor(_context) v = ("*", v, factor) else: # == 'DIV' DIV = self._scan('DIV') factor = self.factor(_context) v = ("/", v, factor) if self._peek() not in ['MUL', 'DIV', 'PLUS', 'SUB', 'COMMA', 'CLOSEB', 'EQUAL', 'END', 'OPENB', 'IN', 'RD', 'INP', 'RDP', 'COLLECT', 'COPYCOLLECT', 'INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE']: raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['MUL', 'DIV', 'PLUS', 'SUB', 'COMMA', 'CLOSEB', 'EQUAL', 'END', 'OPENB', 'IN', 'RD', 'INP', 'RDP', 'COLLECT', 'COPYCOLLECT', 'INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE'])) return v
def sum(self, _parent=None): _context = self.Context(_parent, self._scanner, self._pos, 'sum', []) factor = self.factor(_context) v = factor while self._peek('PLUS', 'SUB', 'COMMA', 'CLOSEB', 'EQUAL', 'END', 'OPENB', 'IN', 'RD', 'INP', 'RDP', 'COLLECT', 'COPYCOLLECT', 'INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE') in ['PLUS', 'SUB']: _token = self._peek('PLUS', 'SUB') if _token == 'PLUS': PLUS = self._scan('PLUS') expr = self.expr(_context) v = ("+", v, expr) else: # == 'SUB' SUB = self._scan('SUB') expr = self.expr(_context) v = ("-", v, expr) if self._peek() not in ['PLUS', 'SUB', 'COMMA', 'CLOSEB', 'EQUAL', 'END', 'OPENB', 'IN', 'RD', 'INP', 'RDP', 'COLLECT', 'COPYCOLLECT', 'INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE']: raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['PLUS', 'SUB', 'COMMA', 'CLOSEB', 'EQUAL', 'END', 'OPENB', 'IN', 'RD', 'INP', 'RDP', 'COLLECT', 'COPYCOLLECT', 'INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE'])) return v
def tuple(self, _parent=None): _context = self.Context(_parent, self._scanner, self._pos, 'tuple', []) sum = self.sum(_context) v = sum if self._peek('COMMA', 'CLOSEB', 'EQUAL', 'END', 'OPENB', 'PLUS', 'SUB', 'IN', 'RD', 'INP', 'RDP', 'COLLECT', 'COPYCOLLECT', 'INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE') == 'COMMA': COMMA = self._scan('COMMA') v = ("tuple", (v, )) if self._peek('COMMA', 'INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE', 'CLOSEB', 'EQUAL', 'END', 'OPENB', 'PLUS', 'SUB', 'IN', 'RD', 'INP', 'RDP', 'COLLECT', 'COPYCOLLECT') in ['INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE']: sum = self.sum(_context) v = ("tuple", v[1] + (sum, )) while self._peek('COMMA', 'CLOSEB', 'EQUAL', 'END', 'OPENB', 'PLUS', 'SUB', 'IN', 'RD', 'INP', 'RDP', 'COLLECT', 'COPYCOLLECT', 'INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE') == 'COMMA': COMMA = self._scan('COMMA') sum = self.sum(_context) v = ("tuple", v[1] + (sum, )) if self._peek() not in ['COMMA', 'CLOSEB', 'EQUAL', 'END', 'OPENB', 'PLUS', 'SUB', 'IN', 'RD', 'INP', 'RDP', 'COLLECT', 'COPYCOLLECT', 'INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE']: raise yappsrt.SyntaxError(charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['COMMA', 'CLOSEB', 'EQUAL', 'END', 'OPENB', 'PLUS', 'SUB', 'IN', 'RD', 'INP', 'RDP', 'COLLECT', 'COPYCOLLECT', 'INT', 'FLOAT', 'STRING', 'TSID', 'VAR', 'USCORE'])) return v
def Rules(self, tokens, _parent=None): _context = self.Context(_parent, self._scanner, self._pos, 'Rules', [tokens]) rul = [] while self._peek() == '"rule"': LINENO = self.LINENO(_context) self._scan('"rule"') ID = self._scan('ID') OptParam = self.OptParam(_context) self._scan('":"') ClauseA = self.ClauseA(ID, tokens, _context) rul.append((ID, OptParam, ClauseA)) if self._peek() not in ['"rule"', 'EOF']: raise yappsrt.SyntaxError( charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join(['"rule"', 'EOF'])) return rul
def Options(self, _parent=None): _context = self.Context(_parent, self._scanner, self._pos, 'Options', []) opt = {} while self._peek() == '"option"': self._scan('"option"') self._scan('":"') Str = self.Str(_context) opt[Str] = 1 if self._peek() not in [ '"option"', '"token"', '"ignore"', 'EOF', '"rule"' ]: raise yappsrt.SyntaxError( charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join( ['"option"', '"token"', '"ignore"', 'EOF', '"rule"'])) return opt
def ClauseB(self, rule, tokens, _parent=None): _context = self.Context(_parent, self._scanner, self._pos, 'ClauseB', [rule, tokens]) v = [] while self._peek() in ['STR', 'ID', 'LP', 'LB', 'STMT']: ClauseC = self.ClauseC(rule, tokens, _context) v.append(ClauseC) if self._peek() not in [ 'STR', 'ID', 'LP', 'LB', 'STMT', 'OR', 'RP', 'RB', '"rule"', 'EOF' ]: raise yappsrt.SyntaxError( charpos=self._scanner.get_prev_char_pos(), context=_context, msg='Need one of ' + ', '.join([ 'STR', 'ID', 'LP', 'LB', 'STMT', 'OR', 'RP', 'RB', '"rule"', 'EOF' ])) return cleanup_sequence(rule, v)
def ClauseC(self, rule, tokens, _parent=None): _context = self.Context(_parent, self._scanner, self._pos, 'ClauseC', [rule, tokens]) ClauseD = self.ClauseD(rule, tokens, _context) _token = self._peek() if _token == 'PLUS': PLUS = self._scan('PLUS') return parsetree.Plus(rule, ClauseD) elif _token == 'STAR': STAR = self._scan('STAR') return parsetree.Star(rule, ClauseD) elif _token == 'QUEST': QUEST = self._scan('QUEST') return parsetree.Option(rule, ClauseD) elif _token not in [ '"ignore"', '"token"', '"option"', '":"', '"parser"', 'ATTR', 'COLON' ]: return ClauseD else: raise yappsrt.SyntaxError(_token[0], 'Could not match ClauseC')