Package EasyExtend :: Module cst
[hide private]
[frames] | no frames]

Module cst

The cst module translates Python grammar rules into functions that can be combined in order to create Python CSTs expressed in list form. The specification of each function is therefore determined by the grammar rule string. This can be retrieved from the docstring.

Example :

  The grammar rule

            single_input:: NEWLINE | simple_stmt | compound_stmt NEWLINE

  is translated to a function single_input(*args) that accepts as input
  either the value NEWLINE(), a simple_stmt node or a
  compound_stmt node.

When the compound_stmt node is added you shall not add the NEWLINE node as well because the presence can be deduced by the function from the grammar rule.

Any rule function checks each input node. They are implemented to be NOT redundancy tolerant i.e. each terminal py_symbol that can be omitted shall be omitted. Only those terminals that are necessary to establish more context information are mandatory.

Examples :

   funcdef: [decorators] 'def' NAME parameters ':' suite

       'def' and ':' shall be dropped

   fpdef: NAME | '(' fplist ')'

       '(' and ')' shall be dropped.

   raise_stmt: 'raise' [test [',' test [',' test]]]

       the 'raise' keyword as well as all colons shall be dropped

   import_from: 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)

       The keywords 'from' and 'import' shall be dropped. Using either '*' '(' or import_as_names
       as subsequent py_symbol is mandatory.
Classes [hide private]
  CSTBuilder
  dict_of_dicts
Functions [hide private]
 
toText(node_id)
Returns textual representation of a node id.
 
py_cst(f)
Decorator used to filter arguments of cst module functions on langlet-specific nodes.
 
Symbol(tok, sym)
 
Name(name)
 
Number(number)
 
String(string)
 
single_input(*args)
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
 
file_input(*args)
file_input: (NEWLINE | stmt)* ENDMARKER
 
eval_input(*args)
eval_input: testlist NEWLINE* ENDMARKER
 
decorator(*args)
decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
 
decorators(*args)
decorators: decorator+
 
funcdef(*args)
funcdef: [decorators] 'def' NAME parameters ':' suite
 
parameters(*args)
parameters: '(' [varargslist] ')'
 
varargslist(*args)
varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
 
fpdef(*args)
fpdef: NAME | '(' fplist ')'
 
fplist(*args)
fplist: fpdef (',' fpdef)* [',']
 
stmt(*args)
stmt: simple_stmt | compound_stmt
 
simple_stmt(*args)
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
 
small_stmt(*args)
small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | exec_stmt | assert_stmt
 
expr_stmt(*args)
expr_stmt: testlist (augassign testlist | ('=' testlist)*)
 
augassign(*args)
augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=' | '//='
 
print_stmt(*args)
print_stmt: 'print' ( '>>' test [ (',' test)+ [','] ] | [ test (',' test)* [','] ] )
 
del_stmt(*args)
del_stmt: 'del' exprlist
 
pass_stmt(*args)
pass_stmt: 'pass'
 
flow_stmt(*args)
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
 
break_stmt(*args)
break_stmt: 'break'
 
continue_stmt(*args)
continue_stmt: 'continue'
 
return_stmt(*args)
return_stmt: 'return' [testlist]
 
yield_expr(*args)
yield_expr: 'yield' [testlist]
 
yield_stmt(*args)
yield_stmt: yield_expr
 
raise_stmt(*args)
raise_stmt: 'raise' [test [',' test [',' test]]]
 
import_stmt(*args)
import_stmt: import_name | import_from
 
import_name(*args)
import_name: 'import' dotted_as_names
 
import_from(*args)
import_from: ('from' ('.'* dotted_name | '.'+) 'import' ('*' | '(' import_as_names ')' | import_as_names))
 
import_as_name(*args)
import_as_name: NAME ['as' NAME]
 
import_as_names(*args)
import_as_names: import_as_name (',' import_as_name)* [',']
 
dotted_as_name(*args)
dotted_as_name: dotted_name [('as'|NAME) NAME]
 
dotted_as_names(*args)
dotted_as_names: dotted_as_name (',' dotted_as_name)*
 
dotted_name(*args)
dotted_name: NAME ('.' NAME)*
 
global_stmt(*args)
global_stmt: 'global' NAME (',' NAME)*
 
assert_stmt(*args)
assert_stmt: 'assert' test [',' test]
 
exec_stmt(*args)
exec_stmt: 'exec' expr ['in' test [',' test]]
 
compound_stmt(*args)
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
 
if_stmt(*args)
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
 
while_stmt(*args)
while_stmt: 'while' test ':' suite ['else' ':' suite]
 
for_stmt(*args)
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
 
try_stmt(*args)
try_stmt: ('try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] ['finally' ':' suite] | 'try' ':' suite 'finally' ':' suite)
 
with_stmt(*args)
with_stmt: 'with' test [ with_var ] ':' suite
 
with_var(*args)
with_var: ('as' | NAME) expr
 
except_clause(*args)
except_clause: 'except' [test [',' test]]
 
suite(*args)
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
 
NEWLINE()
 
INDENT()
 
DEDENT()
 
ENDMARKER()
 
old_test(*args)
old_test: or_test | old_lambdef
 
old_lambdef(*args)
old_lambdef: 'lambda' [varargslist] ':' old_test
 
test(*args)
test: or_test ['if' or_test 'else' test] | lambdef
 
or_test(*args)
or_test: and_test ('or' and_test)*
 
testlist(*args)
testlist: test (',' test)* [',']
 
testlist_safe(*args)
testlist_safe: old_test [(',' old_test)+ [',']]
 
and_test(*args)
and_test: not_test ('and' not_test)*
 
not_test(*args)
not_test: 'not' not_test | comparison
 
comp_op(comp)
 
comparison(*args)
comparison: expr (comp_op expr)*
 
expr(*args)
expr: xor_expr ('|' xor_expr)*
 
xor_expr(*args)
xor_expr: and_expr ('^' and_expr)*
 
and_expr(*args)
and_expr: shift_expr ('&' shift_expr)*
 
shift_expr(*args)
shift_expr: arith_expr (('<<'|'>>') arith_expr)*
 
arith_expr(*args)
arith_expr: term (('+'|'-') term)*
 
term(*args)
term: factor (('*'|'/'|'%'|'//') factor)*
 
factor(*args)
factor: ('+'|'-'|'~') factor | power
 
power(*args)
power: atom trailer* ['**' factor]
 
atom(*args)
atom: '(' [testlist_gexp | yield_expr] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}' | '`' testlist1 '`' | NAME | NUMBER | STRING+
 
exprlist(*args)
exprlist: expr (',' expr)* [',']
 
lambdef(*args)
lambdef: 'lambda' [varargslist] ':' test
 
testlist_gexp(*args)
test ( gen_for | (',' test)* [','] )
 
listmaker(*args)
listmaker: test ( list_for | (',' test)* [','] )
 
trailer(*args)
trailer : '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
 
dictmaker(*args)
dictmaker: test ':' test (',' test ':' test)* [',']
 
subscriptlist(*args)
subscriptlist: subscript (',' subscript)* [',']
 
subscript(*args)
subscript: '.' '.' '.' | [test] ':' [test] [sliceop] | test
 
sliceop(*args)
sliceop: ':' [test]
 
classdef(*args)
classdef: 'class' NAME ['(' testlist ')'] ':' suite
 
arglist(*args)
arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
 
argument(*args)
argument: test [gen_for] | test '=' test
 
list_iter(*args)
list_iter: list_for | list_if
 
list_for(*args)
list_for: 'for' exprlist 'in' testlist_safe [list_iter]
 
list_if(*args)
list_if: 'if' old_test [list_iter]
 
gen_iter(*args)
gen_iter: gen_for | gen_if
 
gen_for(*args)
gen_for: 'for' exprlist 'in' or_test [gen_iter]
 
gen_if(*args)
gen_if: 'if' old_test [gen_iter]
 
testlist1(*args)
testlist1: test (',' test)*
Variables [hide private]
  MAX_PY_SYMBOL = 339
  MAX_PY_TOKEN = 60
  LANGLET_OFFSET_SCALE = 512
  PY_NT_OFFSET = 256
  AUGASSIGN_MAP = {'%=': 41, '&=': 42, '**=': 47, '*=': 39, '+='...
Function Details [hide private]

py_cst(f)

 

Decorator used to filter arguments of cst module functions on langlet-specific nodes. If a node was found that could be projected into Pythons node range apply projection by LANGLET_OFFSET_SCALE.

single_input(*args)

 

single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE

Decorators:
  • @py_cst

file_input(*args)

 

file_input: (NEWLINE | stmt)* ENDMARKER

Decorators:
  • @py_cst

eval_input(*args)

 

eval_input: testlist NEWLINE* ENDMARKER

Decorators:
  • @py_cst

decorator(*args)

 

decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE

Decorators:
  • @py_cst

decorators(*args)

 

decorators: decorator+

Decorators:
  • @py_cst

funcdef(*args)

 

funcdef: [decorators] 'def' NAME parameters ':' suite

Decorators:
  • @py_cst

parameters(*args)

 

parameters: '(' [varargslist] ')'

Decorators:
  • @py_cst

varargslist(*args)

 

varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']

Decorators:
  • @py_cst

fpdef(*args)

 

fpdef: NAME | '(' fplist ')'

Decorators:
  • @py_cst

fplist(*args)

 

fplist: fpdef (',' fpdef)* [',']

Decorators:
  • @py_cst

stmt(*args)

 

stmt: simple_stmt | compound_stmt

Decorators:
  • @py_cst

simple_stmt(*args)

 

simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE

Decorators:
  • @py_cst

small_stmt(*args)

 

small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | exec_stmt | assert_stmt

Decorators:
  • @py_cst

expr_stmt(*args)

 

expr_stmt: testlist (augassign testlist | ('=' testlist)*)

Decorators:
  • @py_cst

augassign(*args)

 

augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=' | '//='

Decorators:
  • @py_cst

print_stmt(*args)

 

print_stmt: 'print' ( '>>' test [ (',' test)+ [','] ] | [ test (',' test)* [','] ] )

Decorators:
  • @py_cst

del_stmt(*args)

 

del_stmt: 'del' exprlist

Decorators:
  • @py_cst

pass_stmt(*args)

 

pass_stmt: 'pass'

Decorators:
  • @py_cst

flow_stmt(*args)

 

flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt

Decorators:
  • @py_cst

break_stmt(*args)

 

break_stmt: 'break'

Decorators:
  • @py_cst

continue_stmt(*args)

 

continue_stmt: 'continue'

Decorators:
  • @py_cst

return_stmt(*args)

 

return_stmt: 'return' [testlist]

Decorators:
  • @py_cst

yield_expr(*args)

 

yield_expr: 'yield' [testlist]

Decorators:
  • @py_cst

yield_stmt(*args)

 

yield_stmt: yield_expr

Decorators:
  • @py_cst

raise_stmt(*args)

 

raise_stmt: 'raise' [test [',' test [',' test]]]

Decorators:
  • @py_cst

import_stmt(*args)

 

import_stmt: import_name | import_from

Decorators:
  • @py_cst

import_name(*args)

 

import_name: 'import' dotted_as_names

Decorators:
  • @py_cst

import_from(*args)

 

import_from: ('from' ('.'* dotted_name | '.'+) 'import' ('*' | '(' import_as_names ')' | import_as_names))

Decorators:
  • @py_cst

import_as_name(*args)

 

import_as_name: NAME ['as' NAME]

Decorators:
  • @py_cst

import_as_names(*args)

 

import_as_names: import_as_name (',' import_as_name)* [',']

Decorators:
  • @py_cst

dotted_as_name(*args)

 

dotted_as_name: dotted_name [('as'|NAME) NAME]

Decorators:
  • @py_cst

dotted_as_names(*args)

 

dotted_as_names: dotted_as_name (',' dotted_as_name)*

Decorators:
  • @py_cst

dotted_name(*args)

 

dotted_name: NAME ('.' NAME)*

Decorators:
  • @py_cst

global_stmt(*args)

 

global_stmt: 'global' NAME (',' NAME)*

Decorators:
  • @py_cst

assert_stmt(*args)

 

assert_stmt: 'assert' test [',' test]

Decorators:
  • @py_cst

exec_stmt(*args)

 

exec_stmt: 'exec' expr ['in' test [',' test]]

Decorators:
  • @py_cst

compound_stmt(*args)

 

compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef

Decorators:
  • @py_cst

if_stmt(*args)

 

if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]

Decorators:
  • @py_cst

while_stmt(*args)

 

while_stmt: 'while' test ':' suite ['else' ':' suite]

Decorators:
  • @py_cst

for_stmt(*args)

 

for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]

Decorators:
  • @py_cst

try_stmt(*args)

 

try_stmt: ('try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] ['finally' ':' suite] | 'try' ':' suite 'finally' ':' suite)

Decorators:
  • @py_cst

with_stmt(*args)

 

with_stmt: 'with' test [ with_var ] ':' suite

Decorators:
  • @py_cst

with_var(*args)

 

with_var: ('as' | NAME) expr

Decorators:
  • @py_cst

except_clause(*args)

 

except_clause: 'except' [test [',' test]]

Decorators:
  • @py_cst

suite(*args)

 

suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT

Decorators:
  • @py_cst

old_test(*args)

 

old_test: or_test | old_lambdef

Decorators:
  • @py_cst

old_lambdef(*args)

 

old_lambdef: 'lambda' [varargslist] ':' old_test

Decorators:
  • @py_cst

test(*args)

 

test: or_test ['if' or_test 'else' test] | lambdef

Decorators:
  • @py_cst

or_test(*args)

 

or_test: and_test ('or' and_test)*

Decorators:
  • @py_cst

testlist(*args)

 

testlist: test (',' test)* [',']

Decorators:
  • @py_cst

testlist_safe(*args)

 

testlist_safe: old_test [(',' old_test)+ [',']]

Decorators:
  • @py_cst

and_test(*args)

 

and_test: not_test ('and' not_test)*

Decorators:
  • @py_cst

not_test(*args)

 

not_test: 'not' not_test | comparison

Decorators:
  • @py_cst

comparison(*args)

 

comparison: expr (comp_op expr)*

Decorators:
  • @py_cst

expr(*args)

 

expr: xor_expr ('|' xor_expr)*

Decorators:
  • @py_cst

xor_expr(*args)

 

xor_expr: and_expr ('^' and_expr)*

Decorators:
  • @py_cst

and_expr(*args)

 

and_expr: shift_expr ('&' shift_expr)*

Decorators:
  • @py_cst

shift_expr(*args)

 

shift_expr: arith_expr (('<<'|'>>') arith_expr)*

Decorators:
  • @py_cst

arith_expr(*args)

 

arith_expr: term (('+'|'-') term)*

Decorators:
  • @py_cst

term(*args)

 

term: factor (('*'|'/'|'%'|'//') factor)*

Decorators:
  • @py_cst

factor(*args)

 

factor: ('+'|'-'|'~') factor | power

Decorators:
  • @py_cst

power(*args)

 

power: atom trailer* ['**' factor]

Decorators:
  • @py_cst

atom(*args)

 

atom: '(' [testlist_gexp | yield_expr] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}' | '`' testlist1 '`' | NAME | NUMBER | STRING+

Decorators:
  • @py_cst

exprlist(*args)

 

exprlist: expr (',' expr)* [',']

Decorators:
  • @py_cst

lambdef(*args)

 

lambdef: 'lambda' [varargslist] ':' test

Decorators:
  • @py_cst

testlist_gexp(*args)

 

test ( gen_for | (',' test)* [','] )

Decorators:
  • @py_cst

listmaker(*args)

 

listmaker: test ( list_for | (',' test)* [','] )

Decorators:
  • @py_cst

trailer(*args)

 

trailer : '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME

Decorators:
  • @py_cst

dictmaker(*args)

 

dictmaker: test ':' test (',' test ':' test)* [',']

Decorators:
  • @py_cst

subscriptlist(*args)

 

subscriptlist: subscript (',' subscript)* [',']

Decorators:
  • @py_cst

subscript(*args)

 

subscript: '.' '.' '.' | [test] ':' [test] [sliceop] | test

Decorators:
  • @py_cst

sliceop(*args)

 

sliceop: ':' [test]

Decorators:
  • @py_cst

classdef(*args)

 

classdef: 'class' NAME ['(' testlist ')'] ':' suite

Decorators:
  • @py_cst

arglist(*args)

 

arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)

Decorators:
  • @py_cst

argument(*args)

 

argument: test [gen_for] | test '=' test

Decorators:
  • @py_cst

list_iter(*args)

 

list_iter: list_for | list_if

Decorators:
  • @py_cst

list_for(*args)

 

list_for: 'for' exprlist 'in' testlist_safe [list_iter]

Decorators:
  • @py_cst

list_if(*args)

 

list_if: 'if' old_test [list_iter]

Decorators:
  • @py_cst

gen_iter(*args)

 

gen_iter: gen_for | gen_if

Decorators:
  • @py_cst

gen_for(*args)

 

gen_for: 'for' exprlist 'in' or_test [gen_iter]

Decorators:
  • @py_cst

gen_if(*args)

 

gen_if: 'if' old_test [gen_iter]

Decorators:
  • @py_cst

testlist1(*args)

 

testlist1: test (',' test)*

Decorators:
  • @py_cst

Variables Details [hide private]

AUGASSIGN_MAP

Value:
{'%=': 41,
 '&=': 42,
 '**=': 47,
 '*=': 39,
 '+=': 37,
 '-=': 38,
 '//=': 49,
 '/=': 40,
...