pycep.analyzer
Module¶
-
pycep.analyzer.
parse
(source)¶ The analyzer takes a string containing the source code as an input and returns an abstract syntax tree.
>>> import pycep.analyzer >>> import ast >>> tree = pycep.analyzer.parse('print "Hello, world!"') >>> ast.dump(tree) "Module(body=[Print(dest=None, values=[Str(s='Hello, world!')], nl=True)])"
Parameters: source (string) – Source code Returns: Abstract Syntax Tree Return type: ast.AST Raises: SyntaxError
– Syntax ErrorAbstract Syntax Tree of Hello World Example:
See also
- Python Abstract Grammar: https://docs.python.org/2/library/ast.html
- Green Tree Snakes - the missing Python AST docs: https://greentreesnakes.readthedocs.org/
-
class
pycep.analyzer.
Analyzer
¶ Convert a parse tree into an abstract syntax tree.
See also
- Visitor Design Pattern https://sourcemaking.com/design_patterns/visitor
-
visit_single_input
(values, ctx)¶ single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
-
visit_file_input
(values, ctx)¶ file_input: (NEWLINE | stmt)* ENDMARKER
-
visit_eval_input
(values, ctx)¶ eval_input: testlist NEWLINE* ENDMARKER
-
visit_decorator
(values, ctx)¶ decorator: ‘@’ dotted_name [ ‘(‘ [arglist] ‘)’ ] NEWLINE
-
visit_decorators
(values, ctx)¶ decorators: decorator+
-
visit_decorated
(values, ctx)¶ decorated: decorators (classdef | funcdef)
-
visit_funcdef
(values, ctx)¶ funcdef: ‘def’ NAME parameters ‘:’ suite
-
visit_parameters
(values, ctx)¶ parameters: ‘(‘ [varargslist] ‘)’
-
visit_varargslist
(values, ctx)¶ varargslist: ((fpdef [‘=’ test] ‘,’)* (‘*’ NAME [‘,’ ‘**’ NAME] | ‘**’ NAME) | fpdef [‘=’ test] (‘,’ fpdef [‘=’ test])* [‘,’])
-
visit_fpdef
(values, ctx)¶ fpdef: NAME | ‘(‘ fplist ‘)’
-
visit_fplist
(values, ctx)¶ fplist: fpdef (‘,’ fpdef)* [‘,’]
-
visit_stmt
(values, ctx)¶ stmt: simple_stmt | compound_stmt
-
visit_simple_stmt
(values, ctx)¶ simple_stmt: small_stmt (‘;’ small_stmt)* [‘;’] NEWLINE
-
visit_small_stmt
(values, ctx)¶ small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | exec_stmt | assert_stmt)
-
visit_expr_stmt
(values, ctx)¶ expr_stmt: testlist (augassign (yield_expr|testlist) | (‘=’ (yield_expr|testlist))*)
-
visit_augassign
(values, ctx)¶ augassign: (‘+=’ | ‘-=’ | ‘*=’ | ‘/=’ | ‘%=’ | ‘&=’ | ‘|=’ | ‘^=’ | ‘<<=’ | ‘>>=’ | ‘**=’ | ‘//=’)
-
visit_print_stmt
(values, ctx)¶ print_stmt: ‘print’ ( [ test (‘,’ test)* [‘,’] ] | ‘>>’ test [ (‘,’ test)+ [‘,’] ] )
-
visit_del_stmt
(values, ctx)¶ del_stmt: ‘del’ exprlist
-
visit_pass_stmt
(values, ctx)¶ pass_stmt: ‘pass’
-
visit_flow_stmt
(values, ctx)¶ flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
-
visit_break_stmt
(values, ctx)¶ break_stmt: ‘break’
-
visit_continue_stmt
(values, ctx)¶ continue_stmt: ‘continue’
-
visit_return_stmt
(values, ctx)¶ return_stmt: ‘return’ [testlist]
-
visit_yield_stmt
(values, ctx)¶ yield_stmt: yield_expr
-
visit_raise_stmt
(values, ctx)¶ raise_stmt: ‘raise’ [test [‘,’ test [‘,’ test]]]
-
visit_import_stmt
(values, ctx)¶ import_stmt: import_name | import_from
-
visit_import_name
(values, ctx)¶ import_name: ‘import’ dotted_as_names
-
visit_import_from
(values, ctx)¶ import_from: (‘from’ (‘.’* dotted_name | ‘.’+) ‘import’ (‘*’ | ‘(‘ import_as_names ‘)’ | import_as_names))
-
visit_import_as_name
(values, ctx)¶ import_as_name: NAME [‘as’ NAME]
-
visit_dotted_as_name
(values, ctx)¶ dotted_as_name: dotted_name [‘as’ NAME]
-
visit_import_as_names
(values, ctx)¶ import_as_names: import_as_name (‘,’ import_as_name)* [‘,’]
-
visit_dotted_as_names
(values, ctx)¶ dotted_as_names: dotted_as_name (‘,’ dotted_as_name)*
-
visit_dotted_name
(values, ctx)¶ dotted_name: NAME (‘.’ NAME)*
-
visit_global_stmt
(values, ctx)¶ global_stmt: ‘global’ NAME (‘,’ NAME)*
-
visit_exec_stmt
(values, ctx)¶ exec_stmt: ‘exec’ expr [‘in’ test [‘,’ test]]
-
visit_assert_stmt
(values, ctx)¶ assert_stmt: ‘assert’ test [‘,’ test]
-
visit_compound_stmt
(values, ctx)¶ compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
-
visit_if_stmt
(values, ctx)¶ if_stmt: ‘if’ test ‘:’ suite (‘elif’ test ‘:’ suite)* [‘else’ ‘:’ suite]
-
visit_while_stmt
(values, ctx)¶ while_stmt: ‘while’ test ‘:’ suite [‘else’ ‘:’ suite]
-
visit_for_stmt
(values, ctx)¶ for_stmt: ‘for’ exprlist ‘in’ testlist ‘:’ suite [‘else’ ‘:’ suite]
-
visit_try_stmt
(values, ctx)¶ try_stmt: (‘try’ ‘:’ suite ((except_clause ‘:’ suite)+
[‘else’ ‘:’ suite] [‘finally’ ‘:’ suite] |‘finally’ ‘:’ suite))
-
visit_with_stmt
(values, ctx)¶ with_stmt: ‘with’ with_item (‘,’ with_item)* ‘:’ suite
-
visit_with_item
(values, ctx)¶ with_item: test [‘as’ expr]
-
visit_except_clause
(values, ctx)¶ except_clause: ‘except’ [test [(‘as’ | ‘,’) test]]
-
visit_suite
(values, ctx)¶ suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
-
visit_testlist_safe
(values, ctx)¶ testlist_safe: old_test [(‘,’ old_test)+ [‘,’]]
-
visit_old_test
(values, ctx)¶ old_test: or_test | old_lambdef
-
visit_old_lambdef
(values, ctx)¶ old_lambdef: ‘lambda’ [varargslist] ‘:’ old_test
-
visit_test
(values, ctx)¶ test: or_test [‘if’ or_test ‘else’ test] | lambdef
-
visit_or_test
(values, ctx)¶ or_test: and_test (‘or’ and_test)*
-
visit_and_test
(values, ctx)¶ and_test: not_test (‘and’ not_test)*
-
visit_not_test
(values, ctx)¶ not_test: ‘not’ not_test | comparison
-
visit_comparison
(values, ctx)¶ comparison: expr (comp_op expr)*
-
visit_comp_op
(values, ctx)¶ comp_op: ‘<’|’>’|’==’|’>=’|’<=’|’<>’|’!=’|’in’|’not’ ‘in’|’is’|’is’ ‘not’
-
visit_expr
(values, ctx)¶ expr: xor_expr (‘|’ xor_expr)*
-
visit_xor_expr
(values, ctx)¶ xor_expr: and_expr (‘^’ and_expr)*
-
visit_and_expr
(values, ctx)¶ and_expr: shift_expr (‘&’ shift_expr)*
-
visit_shift_expr
(values, ctx)¶ shift_expr: arith_expr ((‘<<’|’>>’) arith_expr)*
-
visit_arith_expr
(values, ctx)¶ arith_expr: term ((‘+’|’-‘) term)*
-
visit_term
(values, ctx)¶ term: factor ((‘*’|’/’|’%’|’//’) factor)*
-
visit_factor
(values, ctx)¶ factor: (‘+’|’-‘|’~’) factor | power
-
visit_power
(values, ctx)¶ power: atom trailer* [‘**’ factor]
-
visit_atom
(values, ctx)¶ atom: (‘(‘ [yield_expr|testlist_comp] ‘)’ | ‘[‘ [listmaker] ‘]’ | ‘{‘ [dictorsetmaker] ‘}’ | ‘`’ testlist1 ‘`’ | NAME | NUMBER | STRING+)
-
visit_listmaker
(values, ctx)¶ listmaker: test ( list_for | (‘,’ test)* [‘,’] )
-
visit_testlist_comp
(values, ctx)¶ testlist_comp: test ( comp_for | (‘,’ test)* [‘,’] )
-
visit_lambdef
(values, ctx)¶ lambdef: ‘lambda’ [varargslist] ‘:’ test
-
visit_trailer
(values, ctx)¶ trailer: ‘(‘ [arglist] ‘)’ | ‘[‘ subscriptlist ‘]’ | ‘.’ NAME
-
visit_subscriptlist
(values, ctx)¶ subscriptlist: subscript (‘,’ subscript)* [‘,’]
-
visit_subscript
(values, ctx)¶ subscript: ‘.’ ‘.’ ‘.’ | test | [test] ‘:’ [test] [sliceop]
-
visit_sliceop
(values, ctx)¶ sliceop: ‘:’ [test]
-
visit_exprlist
(values, ctx)¶ exprlist: expr (‘,’ expr)* [‘,’]
-
visit_testlist
(values, ctx)¶ testlist: test (‘,’ test)* [‘,’]
-
visit_dictorsetmaker
(values, ctx)¶ dictorsetmaker: ( (test ‘:’ test (comp_for | (‘,’ test ‘:’ test)* [‘,’])) | (test (comp_for | (‘,’ test)* [‘,’])) )
-
visit_classdef
(values, ctx)¶ classdef: ‘class’ NAME [‘(‘ [testlist] ‘)’] ‘:’ suite
-
visit_arglist
(values, ctx)¶ arglist: (argument ‘,’)* (argument [‘,’] |’*’ test (‘,’ argument)* [‘,’ ‘**’ test] |’**’ test)
-
visit_argument
(values, ctx)¶ argument: test [comp_for] | test ‘=’ test
-
visit_list_iter
(values, ctx)¶ list_iter: list_for | list_if
-
visit_list_for
(values, ctx)¶ list_for: ‘for’ exprlist ‘in’ testlist_safe [list_iter]
-
visit_list_if
(values, ctx)¶ list_if: ‘if’ old_test [list_iter]
-
visit_comp_iter
(values, ctx)¶ comp_iter: comp_for | comp_if
-
visit_comp_for
(values, ctx)¶ comp_for: ‘for’ exprlist ‘in’ or_test [comp_iter]
-
visit_testlist1
(values, ctx)¶ testlist1: test (‘,’ test)*
-
visit_encoding_decl
(values, ctx)¶ encoding_decl: NAME
-
visit_yield_expr
(values, ctx)¶ yield_expr: ‘yield’ [testlist]