Пример #1
0
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>'))
Пример #2
0
    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
Пример #3
0
 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
Пример #4
0
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
Пример #5
0
 def parse(self, s):
     return self.parser.parse(SingleLineFeeder(s))
Пример #6
0
 def prescan(self, code):
     prescanner = Prescanner(SingleLineFeeder(code))
     return prescanner.scan()
Пример #7
0
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>"))
Пример #8
0
 def parse(self, code):
     return parse(definitions, SingleLineFeeder(code))
Пример #9
0
 def test_single(self):
     feeder = SingleLineFeeder('abc\ndef')
     self.assertEqual(feeder.feed(), 'abc\ndef')
     self.assertTrue(feeder.empty())
     self.assertEqual(feeder.feed(), '')