Beispiel #1
0
def run_tests():
    # type: () -> None
    lex = Lexer('abc')
    while True:
        tok_type, tok_val = lex.Read()
        if tok_type == tok_e.Eof:
            break
        #print('%s %s' % (tok_type, tok_val))
        log('tok_val %s', tok_val)

    CASES = [
        '1+2',
        '1+2*3',
        '1*2+3',
        '(1+2)*3',
        'a+b+c+d',
        'a*b*3*4',

        # expect errors here:
        '(',
        ')',
        '(a+b',
        ' ',
        ' $$ ',
    ]
    for expr in CASES:
        lex = Lexer(expr)
        p = Parser(lex)
        log('')
        log('--')
        log('%s =>', expr)

        tree = None  # type: Optional[expr_t]
        try:
            tree = p.Parse()
        except ParseError as e:
            log('Parse error: %s', e.msg)
            continue

        #log('%s', tree)

        htree = tree.AbbreviatedTree()
        ast_f = fmt.AnsiOutput(mylib.Stdout())

        fmt.PrintTree(htree, ast_f)
        ast_f.write('\n')
Beispiel #2
0
def run_tests():
    # type: () -> None
    lex = Lexer('abc')
    while True:
        tok_type, tok_val = lex.Read()
        if tok_type == tok_e.Eof:
            break
        #print('%s %s' % (tok_type, tok_val))
        log('tok_val %s', tok_val)

    CASES = [
        '1+2',
        '1+2*3',
        '1*2+3',
        '(1+2)*3',
        'a+b+c+d',
        'a*b*3*4',
        '1',
        'a',

        # expect errors here:
        '(',
        ')',
        '(a+b',
        ' ',
        ' $$ ',
    ]
    for expr in CASES:
        lex = Lexer(expr)
        p = Parser(lex)
        log('')
        log('--')
        log('%s =>', expr)

        node = None  # type: Optional[expr_t]
        try:
            node = p.Parse()
        except ParseError as e:
            log('Parse error: %s', e.msg)
            continue

        #log('%s', tree)

        htree = node.AbbreviatedTree()
        ast_f = fmt.AnsiOutput(mylib.Stdout())

        fmt.PrintTree(htree, ast_f)
        ast_f.write('\n')

        UP_node = node
        with tagswitch(UP_node) as case:
            if case(expr_e.Const):
                node = cast(expr__Const, UP_node)
                log('Const %d', node.i)

            elif case(expr_e.Var):
                node = cast(expr__Var, UP_node)
                log('Var %s', node.name)

            else:
                log('Other')
Beispiel #3
0
def _ColorPrint(n):
  from asdl import format as fmt
  ast_f = fmt.AnsiOutput(sys.stdout)
  tree = fmt.MakeTree(n)
  fmt.PrintTree(tree, ast_f)