Correct 3.7 "impor"t and "from .. import"

This commit is contained in:
rocky
2022-04-20 20:03:28 -04:00
parent e2ff909603
commit c88d9de316
7 changed files with 80 additions and 26 deletions

View File

@@ -152,7 +152,10 @@ class Python37Parser(Python37BaseParser):
expr ::= LOAD_NAME
expr ::= LOAD_STR
expr ::= _lambda_body
expr ::= and
expr ::= attribute37
expr ::= bin_op
expr ::= call
expr ::= compare
@@ -346,28 +349,43 @@ class Python37Parser(Python37BaseParser):
def p_import37(self, args):
"""
stmt ::= import_as37
import_as37 ::= LOAD_CONST LOAD_CONST importlist37 store POP_TOP
importlist37 ::= importlist37 ROT_TWO IMPORT_FROM
importlist37 ::= importlist37 ROT_TWO POP_TOP IMPORT_FROM
importlist37 ::= importattr37
importattr37 ::= IMPORT_NAME_ATTR IMPORT_FROM
# The 3.7base scanner adds IMPORT_NAME_ATTR
alias ::= IMPORT_NAME_ATTR attributes store
alias ::= IMPORT_NAME_ATTR store
alias ::= IMPORT_NAME_ATTR attributes store
alias ::= IMPORT_NAME_ATTR store
alias37 ::= IMPORT_NAME store
alias37 ::= IMPORT_FROM store
attribute37 ::= expr LOAD_METHOD
import_as37 ::= LOAD_CONST LOAD_CONST importlist37 store POP_TOP
import_from ::= LOAD_CONST LOAD_CONST importlist POP_TOP
import_from37 ::= LOAD_CONST LOAD_CONST IMPORT_NAME_ATTR importlist37 POP_TOP
import_from_as37 ::= LOAD_CONST LOAD_CONST import_from_attr37 store POP_TOP
expr ::= attribute37
attribute37 ::= expr LOAD_METHOD
# A single entry in a dotted import a.b.c.d
import_one ::= importlists ROT_TWO IMPORT_FROM
import_one ::= importlists ROT_TWO POP_TOP IMPORT_FROM
# Semantic checks distinguish importattr37 from import_from_attr37
# in the former the "from" slot in a prior LOAD_CONST is null.
# Used in: import .. as ..
importattr37 ::= IMPORT_NAME_ATTR IMPORT_FROM
# Used in: from xx import .. as ..
import_from_attr37 ::= IMPORT_NAME_ATTR IMPORT_FROM
importlist37 ::= import_one
importlist37 ::= importattr37
importlist37 ::= alias37+
importlists ::= importlist37+
stmt ::= import_as37
stmt ::= import_from37
importlist37 ::= importlist37 alias37
importlist37 ::= alias37
alias37 ::= IMPORT_NAME store
alias37 ::= IMPORT_FROM store
import_from37 ::= LOAD_CONST LOAD_CONST IMPORT_NAME_ATTR importlist37 POP_TOP
stmt ::= import_from_as37
"""

View File

@@ -1134,6 +1134,9 @@ class Python37BaseParser(PythonParser):
self.check_reduce["while1stmt"] = "noAST"
self.check_reduce["while1elsestmt"] = "noAST"
self.check_reduce["_ifstmts_jump"] = "AST"
self.check_reduce["import_as37"] = "tokens"
self.check_reduce["import_from_as37"] = "tokens"
self.check_reduce["import_from_as37"] = "tokens"
self.check_reduce["ifelsestmt"] = "AST"
self.check_reduce["ifelsestmtl"] = "AST"
self.check_reduce["iflaststmt"] = "AST"
@@ -1263,5 +1266,9 @@ class Python37BaseParser(PythonParser):
assert store == "store"
return alias37[0].attr != store[0].attr
return False
elif lhs == "import_as37":
return tokens[first + 1].pattr is not None
elif lhs == "import_from_as37":
return tokens[first + 1].pattr is None
return False