def parse_builtin_rule(string): ''' Parse rules specified in builtin docstrings/attributes. Every symbol in the input is created in the System` context. ''' return parse(SystemDefinitions(), SingleLineFeeder(string, '<builtin_rules>'))
def find_symbol_name(code, cursor_pos): ''' Given a string of code tokenize it until cursor_pos and return the final symbol name. returns None if no symbol is found at cursor_pos. >>> MathicsKernel.find_symbol_name('1 + Sin', 6) 'System`Sin' >>> MathicsKernel.find_symbol_name('1 + ` Sin[Cos[2]] + x', 8) 'System`Sin' >>> MathicsKernel.find_symbol_name('Sin `', 4) ''' tokeniser = Tokeniser(SingleLineFeeder(code)) start_pos = None end_pos = None name = None while True: try: token = tokeniser.next() except ScanError: continue if token.tag == 'END': break # ran out of tokens # find first token which contains cursor_pos if tokeniser.pos >= cursor_pos: if token.tag == 'Symbol': name = token.text start_pos = token.pos end_pos = tokeniser.pos break return start_pos, end_pos, name
def tokens(self, code): tokeniser = Tokeniser(SingleLineFeeder(code)) tokens = [] while True: token = tokeniser.next() if token.tag == 'END': break else: tokens.append(token) return tokens
def parse_lines(lines, definitions): ''' Given some lines of code try to construct a list of expressions. In the case of incomplete lines append more lines until a complete expression is found. If the end is reached and no complete expression is found then reraise the exception. We use a generator so that each expression can be evaluated (as the parser is dependent on defintions and evaluation may change the definitions). ''' query = '' lines = lines.splitlines() incomplete_exc = None for line in lines: if not line: query += ' ' continue query += line if query.endswith('\\'): query = query.rstrip('\\') incomplete_exc = IncompleteSyntaxError(len(query) - 1) continue try: expression = parse(definitions, SingleLineFeeder(query)) except IncompleteSyntaxError as exc: incomplete_exc = exc else: if expression is not None: yield expression query = '' incomplete_exc = None if incomplete_exc is not None: # ran out of lines raise incomplete_exc raise StopIteration
def parse(self, s): return self.parser.parse(SingleLineFeeder(s))
def prescan(self, code): prescanner = Prescanner(SingleLineFeeder(code)) return prescanner.scan()
def parse_builtin_rule(string, definitions=SystemDefinitions()): """ Parse rules specified in builtin docstrings/attributes. Every symbol in the input is created in the System` context. """ return parse(definitions, SingleLineFeeder(string, "<builtin_rules>"))
def parse(self, code): return parse(definitions, SingleLineFeeder(code))
def test_single(self): feeder = SingleLineFeeder('abc\ndef') self.assertEqual(feeder.feed(), 'abc\ndef') self.assertTrue(feeder.empty()) self.assertEqual(feeder.feed(), '')