Fix import statements

I just found out my project couldn't run anywhere but IDE.
master
Yury Kurlykov 2020-04-28 12:00:06 +10:00
parent 42b4e40d9e
commit ea5b03d93e
Signed by: t1meshift
GPG Key ID: B133F3167ABF94D8
2 changed files with 30 additions and 32 deletions

View File

@ -4,14 +4,14 @@ from enum import Enum
from typing import Union from typing import Union
from antlr4 import ParseTreeWalker from antlr4 import ParseTreeWalker
import lex.JavaScriptParser as Parser import jasminesnake.lex.JavaScriptParser as Parser
import ast.nodes from . import nodes
from .parse_tree_listeners import ASTListener from .parse_tree_listeners import ASTListener
JSP = Parser.JavaScriptParser JSP = Parser.JavaScriptParser
def from_parse_tree(tree: JSP.ProgramContext) -> ast.nodes.Program: def from_parse_tree(tree: JSP.ProgramContext) -> nodes.Program:
"""Generate AST from ANTLR parse tree. """Generate AST from ANTLR parse tree.
Args: Args:
@ -26,7 +26,7 @@ def from_parse_tree(tree: JSP.ProgramContext) -> ast.nodes.Program:
def to_ascii_tree( def to_ascii_tree(
node: Union[ast.nodes.Position, ast.nodes.SourceLocation, ast.nodes.Node], node: Union[nodes.Position, nodes.SourceLocation, nodes.Node],
name_prefix: str = "", name_prefix: str = "",
nesting_lvl: int = 0, nesting_lvl: int = 0,
): ):

View File

@ -2,31 +2,29 @@ import logging
from typing import Optional, List, Union from typing import Optional, List, Union
import antlr4.ParserRuleContext import antlr4.ParserRuleContext
from lex.JavaScriptParser import JavaScriptParser from ..lex.JavaScriptParser import JavaScriptParser
from lex.JavaScriptParserListener import JavaScriptParserListener as JSBaseListener from ..lex.JavaScriptParserListener import JavaScriptParserListener as JSBaseListener
import ast.nodes from . import nodes
def _get_source_location( def _get_source_location(
ctx: antlr4.ParserRuleContext, source: Optional[str] ctx: antlr4.ParserRuleContext, source: Optional[str]
) -> ast.nodes.SourceLocation: ) -> nodes.SourceLocation:
"""Internal function to obtain `SourceObject` from parser context.""" """Internal function to obtain `SourceObject` from parser context."""
start_pos = ast.nodes.Position(ctx.start.line, ctx.start.column) start_pos = nodes.Position(ctx.start.line, ctx.start.column)
end_pos = ast.nodes.Position(ctx.stop.line, ctx.stop.column) end_pos = nodes.Position(ctx.stop.line, ctx.stop.column)
# If an end is not on a newline, shift end position column by 1 # If an end is not on a newline, shift end position column by 1
# to match exact token end, not the last character # to match exact token end, not the last character
if end_pos.column != 0: if end_pos.column != 0:
end_pos.column += 1 end_pos.column += 1
return ast.nodes.SourceLocation(source=source, start=start_pos, end=end_pos) return nodes.SourceLocation(source=source, start=start_pos, end=end_pos)
class AssignableListener(JSBaseListener): class AssignableListener(JSBaseListener):
_result: Union[ _result: Union[nodes.Identifier, nodes.ObjectPattern, nodes.ArrayPattern]
ast.nodes.Identifier, ast.nodes.ObjectPattern, ast.nodes.ArrayPattern
]
@property @property
def result(self): def result(self):
@ -39,7 +37,7 @@ class AssignableListener(JSBaseListener):
def enterIdentifier(self, ctx: JavaScriptParser.IdentifierContext): def enterIdentifier(self, ctx: JavaScriptParser.IdentifierContext):
logging.debug("Entered section Identifier") logging.debug("Entered section Identifier")
loc = _get_source_location(ctx, None) loc = _get_source_location(ctx, None)
self._result = ast.nodes.Identifier(loc, ctx.getText()) self._result = nodes.Identifier(loc, ctx.getText())
def enterArrayLiteral(self, ctx: JavaScriptParser.ArrayLiteralContext): def enterArrayLiteral(self, ctx: JavaScriptParser.ArrayLiteralContext):
logging.debug("Entered section ArrayLiteral") logging.debug("Entered section ArrayLiteral")
@ -51,7 +49,7 @@ class AssignableListener(JSBaseListener):
class VariableDeclarationListener(JSBaseListener): class VariableDeclarationListener(JSBaseListener):
_var_decl: ast.nodes.VariableDeclarator _var_decl: nodes.VariableDeclarator
@property @property
def var_declarator(self): def var_declarator(self):
@ -64,16 +62,16 @@ class VariableDeclarationListener(JSBaseListener):
assign_listener = AssignableListener() assign_listener = AssignableListener()
ctx.assignable().enterRule(assign_listener) ctx.assignable().enterRule(assign_listener)
# ctx.singleExpression().enterRule(expression_listener) # FIXME No ExpressionListener yet # ctx.singleExpression().enterRule(expression_listener) # FIXME No ExpressionListener yet
self._var_decl = ast.nodes.VariableDeclarator( self._var_decl = nodes.VariableDeclarator(
loc, assign_listener.result, None loc, assign_listener.result, None
) # FIXME ) # FIXME
class StatementListener(JSBaseListener): class StatementListener(JSBaseListener):
_stmt: ast.nodes.Statement _stmt: nodes.Statement
@property @property
def statement(self) -> ast.nodes.Statement: def statement(self) -> nodes.Statement:
"""Statement AST node generated after parse tree walking.""" """Statement AST node generated after parse tree walking."""
return self._stmt return self._stmt
@ -87,14 +85,14 @@ class StatementListener(JSBaseListener):
"""Listener for BlockStatement.""" """Listener for BlockStatement."""
logging.debug("Entered section Block") logging.debug("Entered section Block")
stmt_list: List[ast.nodes.Statement] = [] stmt_list: List[nodes.Statement] = []
for stmt in ctx.statementList().children: for stmt in ctx.statementList().children:
stmt_listener = StatementListener() stmt_listener = StatementListener()
stmt.enterRule(stmt_listener) stmt.enterRule(stmt_listener)
stmt_list.append(stmt_listener.statement) stmt_list.append(stmt_listener.statement)
loc = _get_source_location(ctx, None) # FIXME source param is None loc = _get_source_location(ctx, None) # FIXME source param is None
self._stmt = ast.nodes.BlockStatement(loc, stmt_list) self._stmt = nodes.BlockStatement(loc, stmt_list)
def enterVariableStatement(self, ctx: JavaScriptParser.VariableStatementContext): def enterVariableStatement(self, ctx: JavaScriptParser.VariableStatementContext):
logging.debug("Entered section VariableStatement") logging.debug("Entered section VariableStatement")
@ -106,8 +104,8 @@ class StatementListener(JSBaseListener):
"""Listener for VariableDeclaration.""" """Listener for VariableDeclaration."""
logging.debug("Entered section VariableDeclaration") logging.debug("Entered section VariableDeclaration")
var_modifier: ast.nodes.VarDeclKind = ctx.varModifier().getText() var_modifier: nodes.VarDeclKind = ctx.varModifier().getText()
var_decls: List[ast.nodes.VariableDeclarator] = [] var_decls: List[nodes.VariableDeclarator] = []
for var_decl in ctx.variableDeclaration(): for var_decl in ctx.variableDeclaration():
var_decl_listener = VariableDeclarationListener() var_decl_listener = VariableDeclarationListener()
@ -115,7 +113,7 @@ class StatementListener(JSBaseListener):
var_decls.append(var_decl_listener.var_declarator) var_decls.append(var_decl_listener.var_declarator)
loc = _get_source_location(ctx, None) loc = _get_source_location(ctx, None)
self._stmt = ast.nodes.VariableDeclaration(loc, var_modifier, var_decls) self._stmt = nodes.VariableDeclaration(loc, var_modifier, var_decls)
def enterEmptyStatement(self, ctx: JavaScriptParser.EmptyStatementContext): def enterEmptyStatement(self, ctx: JavaScriptParser.EmptyStatementContext):
"""Listener for EmptyStatement.""" """Listener for EmptyStatement."""
@ -149,10 +147,10 @@ class StatementListener(JSBaseListener):
class SourceElementListener(JSBaseListener): class SourceElementListener(JSBaseListener):
"""The proxy between Program and Statement.""" """The proxy between Program and Statement."""
_elems: List[ast.nodes.Statement] = [] _elems: List[nodes.Statement] = []
@property @property
def source_elements(self) -> List[ast.nodes.Statement]: def source_elements(self) -> List[nodes.Statement]:
"""Source elements AST nodes generated after parse tree walking.""" """Source elements AST nodes generated after parse tree walking."""
return self._elems return self._elems
@ -168,22 +166,22 @@ class SourceElementListener(JSBaseListener):
class ASTListener(JSBaseListener): class ASTListener(JSBaseListener):
"""AST listener.""" """AST listener."""
_program_node: Optional[ast.nodes.Program] = None _program_node: Optional[nodes.Program] = None
_source_type: ast.nodes.SourceTypeLiteral _source_type: nodes.SourceTypeLiteral
@property @property
def program_node(self) -> ast.nodes.Program: def program_node(self) -> nodes.Program:
"""The `Program` AST node generated after parse tree walking.""" """The `Program` AST node generated after parse tree walking."""
if self._program_node is None: if self._program_node is None:
raise ValueError("Program AST node is None, did you run the listener?") raise ValueError("Program AST node is None, did you run the listener?")
return self._program_node return self._program_node
def __init__(self, source_type: ast.nodes.SourceTypeLiteral = "script"): def __init__(self, source_type: nodes.SourceTypeLiteral = "script"):
"""AST listener constructor. """AST listener constructor.
Args: Args:
source_type (ast.nodes.SourceTypeLiteral): source type. Could be `script` or `module`. Set to source_type (nodes.SourceTypeLiteral): source type. Could be `script` or `module`. Set to
`script` by default. `script` by default.
""" """
self._source_type = source_type self._source_type = source_type
@ -204,6 +202,6 @@ class ASTListener(JSBaseListener):
elem.enterRule(source_elem_listener) elem.enterRule(source_elem_listener)
loc = _get_source_location(ctx, None) # FIXME add source name loc = _get_source_location(ctx, None) # FIXME add source name
self._program_node = ast.nodes.Program( self._program_node = nodes.Program(
loc, self._source_type, source_elem_listener.source_elements loc, self._source_type, source_elem_listener.source_elements
) )