def adjlocust(l, o): "Recursively shift location tags by the given offset." start, end = l.loc v = l.value if isinstance(v, list) or isinstance(v, tuple): v = [adjlocust(u, o) for u in v] return ast.Located([start + o, end + o], v)
def produce(self, token, value=None, length=None): if token is None: # EOF token = 0 if value is None: value = self.text if length is None: length = len(self.text) end = self.cur_pos start = end - length Plex.Scanner.produce(self, token, ast.Located([start, end - 1], value))
def p_instruction_labelled(self, l, open, d, close): label = ast.map_value(val.symbol, l) if d['instruction'].value == 'evaluate': # The grammar only permits expressions that are calls to # the 'assume', 'observe', or 'predict' macros to be # labeled with syntactic sugar. locexp = d['expression'] exp = locexp.value new_exp = exp + [label] new_locexp = ast.Located(locexp.loc, new_exp) new_d = expression_evaluation_instruction(new_locexp) return ast.locmerge(l, close, new_d) else: d['label'] = label d['instruction'] = ast.map_value(lambda i: 'labeled_' + i, d['instruction']) return ast.locmerge(l, close, d)
def parse(f, context, languages=None): scanner = scan.Scanner(f, context, languages) semantics = Semantics() parser = grammar.Parser(semantics) while True: token = scanner.read() if token[0] == -1: # error semantics.syntax_error(token) else: if token[0] == 0: # EOF # Implicit ; at EOF. loc = (scanner.cur_pos, scanner.cur_pos) parser.feed((grammar.T_SEMI, ast.Located(loc, ';'))) parser.feed(token) if token[0] == 0: # EOF break assert isinstance(semantics.answer, list) for i in semantics.answer: assert ast.isloc(i) return semantics.answer
def string_complete_p(string, languages=None): # XXX This protocol won't work very well with embedded languages. scanner = scan.Scanner(StringIO.StringIO(string), '(string)', languages) semantics = Semantics() parser = grammar.Parser(semantics) while True: token = scanner.read() if token[0] == -1: # scan error return True else: if token[0] == 0: # EOF # Implicit ; at EOF. semi = ast.Located([scanner.cur_pos, scanner.cur_pos], ';') try: parser.feed((grammar.T_SEMI, semi)) # If the semi parses, then we had a complete string return True except VentureException: # Parse was not complete return False else: parser.feed(token)
def locquoted(located_quoter, located_value, f): (vstart, vend) = located_value.loc (start, _end) = located_quoter.loc assert start < vstart return ast.Located([start, vend], f(located_value))
def read(self): char = self.stream.read(1) while char in [" ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]: char = self.stream.read(1) return (ast.Located([0, 0], e.number(9)), )
def doit(char): if char in [" ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]: return (False, None) else: return (True, ast.Located([0, 0], e.number(9)))