Skip to content

Commit

Permalink
Add starlark load statement to grammar
Browse files Browse the repository at this point in the history
To support starlark files using existing python tooling, we have to transform file load() calls into python import statements.
Adding a load() call to the recoggnized grammar allows us to easily transform the AST node, and support file autocomplete later.
  • Loading branch information
Peter Hagen committed Nov 3, 2019
1 parent ee2995c commit 0374e50
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 9 deletions.
5 changes: 4 additions & 1 deletion parso/python/grammar26.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ dictorsetmaker: test ':' test (',' test ':' test)* [',']
classdef: 'class' NAME ['(' [testlist] ')'] ':' suite

arglist: (argument ',')* (argument [',']
|'*' test (',' argument)* [',' '**' test]
|'*' test (',' argument)* [',' '**' test]
|'**' test)
argument: test [gen_for] | test '=' test # Really [keyword '='] test

Expand All @@ -157,3 +157,6 @@ testlist1: test (',' test)*
encoding_decl: NAME

yield_expr: 'yield' [testlist]

import_from_starlark: 'load' '(' STRING ',' starlark_imported_names ')'
starlark_imported_names: STRING (',' STRING)*
5 changes: 4 additions & 1 deletion parso/python/grammar27.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ dictorsetmaker: ( (test ':' test (sync_comp_for | (',' test ':' test)* [','])) |
classdef: 'class' NAME ['(' [testlist] ')'] ':' suite

arglist: (argument ',')* (argument [',']
|'*' test (',' argument)* [',' '**' test]
|'*' test (',' argument)* [',' '**' test]
|'**' test)
# The reason that keywords are test nodes instead of NAME is that using NAME
# results in an ambiguity. ast.c makes sure it's a NAME.
Expand All @@ -141,3 +141,6 @@ testlist1: test (',' test)*
encoding_decl: NAME

yield_expr: 'yield' [testlist]

import_from_starlark: 'load' '(' STRING ',' starlark_imported_names ')'
starlark_imported_names: STRING (',' STRING)*
5 changes: 4 additions & 1 deletion parso/python/grammar33.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ dictorsetmaker: ( (test ':' test (sync_comp_for | (',' test ':' test)* [','])) |
classdef: 'class' NAME ['(' [arglist] ')'] ':' suite

arglist: (argument ',')* (argument [',']
|'*' test (',' argument)* [',' '**' test]
|'*' test (',' argument)* [',' '**' test]
|'**' test)
# The reason that keywords are test nodes instead of NAME is that using NAME
# results in an ambiguity. ast.c makes sure it's a NAME.
Expand All @@ -132,3 +132,6 @@ encoding_decl: NAME

yield_expr: 'yield' [yield_arg]
yield_arg: 'from' test | testlist

import_from_starlark: 'load' '(' STRING ',' starlark_imported_names ')'
starlark_imported_names: STRING (',' STRING)*
5 changes: 4 additions & 1 deletion parso/python/grammar34.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ dictorsetmaker: ( (test ':' test (sync_comp_for | (',' test ':' test)* [','])) |
classdef: 'class' NAME ['(' [arglist] ')'] ':' suite

arglist: (argument ',')* (argument [',']
|'*' test (',' argument)* [',' '**' test]
|'*' test (',' argument)* [',' '**' test]
|'**' test)
# The reason that keywords are test nodes instead of NAME is that using NAME
# results in an ambiguity. ast.c makes sure it's a NAME.
Expand All @@ -132,3 +132,6 @@ encoding_decl: NAME

yield_expr: 'yield' [yield_arg]
yield_arg: 'from' test | testlist

import_from_starlark: 'load' '(' STRING ',' starlark_imported_names ')'
starlark_imported_names: STRING (',' STRING)*
3 changes: 3 additions & 0 deletions parso/python/grammar35.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,6 @@ encoding_decl: NAME

yield_expr: 'yield' [yield_arg]
yield_arg: 'from' test | testlist

import_from_starlark: 'load' '(' STRING ',' starlark_imported_names ')'
starlark_imported_names: STRING (',' STRING)*
3 changes: 3 additions & 0 deletions parso/python/grammar36.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,6 @@ fstring_content: FSTRING_STRING | fstring_expr
fstring_conversion: '!' NAME
fstring_expr: '{' testlist_comp [ fstring_conversion ] [ fstring_format_spec ] '}'
fstring_format_spec: ':' fstring_content*

import_from_starlark: 'load' '(' STRING ',' starlark_imported_names ')'
starlark_imported_names: STRING (',' STRING)*
5 changes: 4 additions & 1 deletion parso/python/grammar37.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ continue_stmt: 'continue'
return_stmt: 'return' [testlist]
yield_stmt: yield_expr
raise_stmt: 'raise' [test ['from' test]]
import_stmt: import_name | import_from
import_stmt: import_name | import_from | import_from_starlark
import_name: 'import' dotted_as_names
# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
Expand Down Expand Up @@ -154,3 +154,6 @@ fstring_content: FSTRING_STRING | fstring_expr
fstring_conversion: '!' NAME
fstring_expr: '{' testlist [ fstring_conversion ] [ fstring_format_spec ] '}'
fstring_format_spec: ':' fstring_content*

import_from_starlark: 'load' '(' STRING ',' starlark_imported_names ')'
starlark_imported_names: STRING (',' STRING)*
3 changes: 3 additions & 0 deletions parso/python/grammar38.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,6 @@ fstring_content: FSTRING_STRING | fstring_expr
fstring_conversion: '!' NAME
fstring_expr: '{' testlist ['='] [ fstring_conversion ] [ fstring_format_spec ] '}'
fstring_format_spec: ':' fstring_content*

import_from_starlark: 'load' '(' STRING ',' starlark_imported_names ')'
starlark_imported_names: STRING (',' STRING)*
3 changes: 3 additions & 0 deletions parso/python/grammar39.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,6 @@ fstring_content: FSTRING_STRING | fstring_expr
fstring_conversion: '!' NAME
fstring_expr: '{' testlist ['='] [ fstring_conversion ] [ fstring_format_spec ] '}'
fstring_format_spec: ':' fstring_content*

import_from_starlark: 'load' '(' STRING ',' starlark_imported_names ')'
starlark_imported_names: STRING (',' STRING)*
2 changes: 1 addition & 1 deletion parso/python/pep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from parso.python.tree import search_ancestor, Flow, Scope


_IMPORT_TYPES = ('import_name', 'import_from')
_IMPORT_TYPES = ('import_name', 'import_from', 'import_from_starlark')
_SUITE_INTRODUCERS = ('classdef', 'funcdef', 'if_stmt', 'while_stmt',
'for_stmt', 'try_stmt', 'with_stmt')
_NON_STAR_TYPES = ('term', 'import_from', 'power')
Expand Down
6 changes: 3 additions & 3 deletions parso/python/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
_FUNC_CONTAINERS = set(['suite', 'simple_stmt', 'decorated']) | _FLOW_CONTAINERS
_GET_DEFINITION_TYPES = set([
'expr_stmt', 'sync_comp_for', 'with_stmt', 'for_stmt', 'import_name',
'import_from', 'param'
'import_from', 'import_from_starlark', 'param'
])
_IMPORTS = set(['import_name', 'import_from'])
_IMPORTS = set(['import_name', 'import_from', 'import_from_starlark'])


class DocstringMixin(object):
Expand Down Expand Up @@ -352,7 +352,7 @@ def iter_imports(self):
"""
Returns a generator of `import_name` and `import_from` nodes.
"""
return self._search_in_scope('import_name', 'import_from')
return self._search_in_scope('import_name', 'import_from', 'import_from_starlark')

def _search_in_scope(self, *names):
def scan(children):
Expand Down

0 comments on commit 0374e50

Please sign in to comment.