Compare commits

..

No commits in common. "041f4c31fb05b1ce6be0a6eac332568fe7bbe3a8" and "c09b52638c99f258448675116fc2980d19147110" have entirely different histories.

6 changed files with 19 additions and 239 deletions

View File

@ -3,7 +3,7 @@ So here it is.
""" """
import logging import logging
__version__ = "0.0.7" __version__ = "0.0.2"
__snake__ = r""" __snake__ = r"""
_________ _________ _________ _________
/ \ / \ / \ / \

View File

@ -10,7 +10,7 @@ import coloredlogs
from jasminesnake import __version__, __snake__, LOG_LEVELS from jasminesnake import __version__, __snake__, LOG_LEVELS
from .js_stream import JSBaseStream, JSStringStream, JSFileStream from .js_stream import JSBaseStream, JSStringStream, JSFileStream
from .lex.ErrorListeners import LogErrorListener from .lex.ErrorListeners import LogErrorListener
from ast import nodes, to_ascii_tree, from_parse_tree import ast
def create_argument_parser(): def create_argument_parser():
@ -70,11 +70,8 @@ def main():
stream = JSFileStream(args.infile, LogErrorListener()) stream = JSFileStream(args.infile, LogErrorListener())
tree = stream.parse() tree = stream.parse()
ast_tree = ast.from_parse_tree(tree)
ast_tree = from_parse_tree(tree)
ascii_ast = to_ascii_tree(ast_tree)
logging.info("Got an AST!\n%s", ascii_ast)
# TODO: run logic # TODO: run logic
sys.exit(0) sys.exit(0)
@ -95,11 +92,7 @@ def main():
tree = stream.parse() tree = stream.parse()
logging.debug("Got tree %s", tree.toStringTree(stream.parser.ruleNames)) logging.debug("Got tree %s", tree.toStringTree(stream.parser.ruleNames))
ast_tree = from_parse_tree(tree) ast_tree = ast.from_parse_tree(tree)
ascii_ast = to_ascii_tree(ast_tree)
logging.info("Got an AST!")
logging.info(ascii_ast)
# TODO: run logic # TODO: run logic
except EOFError: except EOFError:
print("Ctrl-D received, shutting down...") print("Ctrl-D received, shutting down...")

View File

@ -1,8 +1,7 @@
"""AST module.""" """AST module."""
from enum import Enum
from typing import Union
from antlr4 import ParseTreeWalker from antlr4 import ParseTreeWalker
from tree_format import format_tree
import lex.JavaScriptParser as Parser import lex.JavaScriptParser as Parser
import ast.nodes import ast.nodes
@ -25,46 +24,6 @@ def from_parse_tree(tree: JSP.ProgramContext) -> ast.nodes.Program:
return ast_listener.program_node return ast_listener.program_node
def to_ascii_tree(
node: Union[ast.nodes.Position, ast.nodes.SourceLocation, ast.nodes.Node],
name_prefix: str = "",
nesting_lvl: int = 0,
):
if nesting_lvl < 0:
raise ValueError("Nesting level can't be below 0")
FORK = "+"
VERTICAL = "|"
HORIZONTAL = "-"
SUBENTRY_PREFIX = f"{FORK}{HORIZONTAL}{HORIZONTAL} "
NESTED_PREFIX = f"{VERTICAL} "
value = str(node)
children = None
if isinstance(node, Enum):
value = str(node.value)
if isinstance(node, list):
value = ""
children = [(index, val) for index, val in enumerate(node)]
if hasattr(node, "fields"):
children = [(k, node.fields[k]) for k in node.fields.keys()]
result = f"{NESTED_PREFIX * (nesting_lvl - 1)}{SUBENTRY_PREFIX * (nesting_lvl > 0)}"
result += f"{name_prefix}{value}\n"
if children is not None:
for (child_name, child_value) in children:
result += to_ascii_tree(child_value, f"{child_name}: ", nesting_lvl + 1)
# result += "\n"
return result
# Delete temporary imports # Delete temporary imports
del JSP del JSP
del Parser del Parser

View File

@ -21,9 +21,8 @@ Todo:
* Add support for lacking features * Add support for lacking features
""" """
from typing import List, Union, Optional, Literal as TypeLiteral, TypedDict, Any from typing import List, Union, Optional, Literal as TypeLiteral, TypedDict
from enum import Enum from enum import Enum
from collections import OrderedDict
# The Lord sees I actually wanted to split it up, but ESTree hierarchy is so messed up... No. It's actually *fucked up* # The Lord sees I actually wanted to split it up, but ESTree hierarchy is so messed up... No. It's actually *fucked up*
# that much that I couldn't even resolve circular dependencies in the submodules. I have to reap what I've sown. # that much that I couldn't even resolve circular dependencies in the submodules. I have to reap what I've sown.
@ -141,10 +140,6 @@ class Property:
... ...
class Identifier:
...
# "Node objects" block # "Node objects" block
@ -160,9 +155,6 @@ class Position:
self.line = line self.line = line
self.column = column self.column = column
def __str__(self):
return f"{self.line}:{self.column}"
class SourceLocation: class SourceLocation:
""" """
@ -180,14 +172,6 @@ class SourceLocation:
self.start = start self.start = start
self.end = end self.end = end
@property
def fields(self):
return OrderedDict({"start": self.start, "end": self.end})
def __str__(self):
src = "" if self.source is None else f"{self.source}:"
return f"{src}{str(self.start)}"
class Node: class Node:
"""ESTree AST nodes are represented as Node objects, which may have any prototype inheritance but which implement """ESTree AST nodes are represented as Node objects, which may have any prototype inheritance but which implement
@ -207,15 +191,16 @@ class Node:
self.type = node_type self.type = node_type
self.loc = loc self.loc = loc
self._fields: OrderedDict[str, Any] = OrderedDict()
self._fields.update({"type": self.type, "loc": self.loc})
def __str__(self): # "Identifier" block
return f"{self.type} at {str(self.loc)}"
@property
def fields(self): class Identifier(Expression, Pattern):
return self._fields """An identifier. Note that an identifier may be an expression or a destructuring pattern."""
def __init__(self, loc: Optional[SourceLocation], name: str):
super(Identifier, self).__init__("Identifier", loc)
self.name = name
# "Literal" block # "Literal" block
@ -229,7 +214,6 @@ class Literal(Expression):
): ):
super().__init__("Literal", loc) super().__init__("Literal", loc)
self.value = value self.value = value
self._fields.update({"value": self.value})
# "Programs" block # "Programs" block
@ -247,7 +231,6 @@ class Program(Node):
super().__init__("Program", loc) super().__init__("Program", loc)
self.body = body self.body = body
self.source_type = source_type self.source_type = source_type
self._fields.update({"sourceType": self.source_type, "body": self.body})
# "Functions" block # "Functions" block
@ -274,7 +257,6 @@ class Function(Node):
self.id = function_id self.id = function_id
self.params = params self.params = params
self.body = body self.body = body
self._fields.update({"id": self.id, "params": self.params, "body": self.body})
# "Statements" block # "Statements" block
@ -300,7 +282,6 @@ class BlockStatement(Statement):
def __init__(self, loc: Optional[SourceLocation], body: List[Statement]): def __init__(self, loc: Optional[SourceLocation], body: List[Statement]):
super().__init__("BlockStatement", loc) super().__init__("BlockStatement", loc)
self.body = body self.body = body
self._fields.update({"body": self.body})
class ExpressionStatement(Statement): class ExpressionStatement(Statement):
@ -309,7 +290,6 @@ class ExpressionStatement(Statement):
def __init__(self, loc: Optional[SourceLocation], expression: Expression): def __init__(self, loc: Optional[SourceLocation], expression: Expression):
super().__init__("ExpressionStatement", loc) super().__init__("ExpressionStatement", loc)
self.expression = expression self.expression = expression
self._fields.update({"expression": self.expression})
class Directive(Node): class Directive(Node):
@ -323,9 +303,6 @@ class Directive(Node):
super().__init__("Directive", loc) super().__init__("Directive", loc)
self.expression = expression self.expression = expression
self.directive = directive self.directive = directive
self._fields.update(
{"expression": self.expression, "directive": self.directive}
)
class FunctionBody(BlockStatement): class FunctionBody(BlockStatement):
@ -343,7 +320,6 @@ class ReturnStatement(Statement):
def __init__(self, loc: Optional[SourceLocation], argument: Optional[Expression]): def __init__(self, loc: Optional[SourceLocation], argument: Optional[Expression]):
super().__init__("ReturnStatement", loc) super().__init__("ReturnStatement", loc)
self.argument = argument self.argument = argument
self._fields.update({"argument": self.argument})
class BreakStatement(Statement): class BreakStatement(Statement):
@ -352,7 +328,6 @@ class BreakStatement(Statement):
def __init__(self, loc: Optional[SourceLocation], label: Optional[Identifier]): def __init__(self, loc: Optional[SourceLocation], label: Optional[Identifier]):
super().__init__("BreakStatement", loc) super().__init__("BreakStatement", loc)
self.label = label self.label = label
self._fields.update({"label": self.label})
class ContinueStatement(Statement): class ContinueStatement(Statement):
@ -361,7 +336,6 @@ class ContinueStatement(Statement):
def __init__(self, loc: Optional[SourceLocation], label: Optional[Identifier]): def __init__(self, loc: Optional[SourceLocation], label: Optional[Identifier]):
super().__init__("ContinueStatement", loc) super().__init__("ContinueStatement", loc)
self.label = label self.label = label
self._fields.update({"label": self.label})
class IfStatement(Statement): class IfStatement(Statement):
@ -378,13 +352,6 @@ class IfStatement(Statement):
self.test = test self.test = test
self.consequent = consequent self.consequent = consequent
self.alternate = alternate self.alternate = alternate
self._fields.update(
{
"test": self.test,
"consequent": self.consequent,
"alternate": self.alternate,
}
)
class WhileStatement(Statement): class WhileStatement(Statement):
@ -396,7 +363,6 @@ class WhileStatement(Statement):
super().__init__("WhileStatement", loc) super().__init__("WhileStatement", loc)
self.test = test self.test = test
self.body = body self.body = body
self._fields.update({"test": self.test, "body": self.body})
class DoWhileStatement(Statement): class DoWhileStatement(Statement):
@ -408,7 +374,6 @@ class DoWhileStatement(Statement):
super().__init__("DoWhileStatement", loc) super().__init__("DoWhileStatement", loc)
self.body = body self.body = body
self.test = test self.test = test
self._fields.update({"body": self.body, "test": self.test})
class ForStatement(Statement): class ForStatement(Statement):
@ -427,14 +392,6 @@ class ForStatement(Statement):
self.test = test self.test = test
self.update = update self.update = update
self.body = body self.body = body
self._fields.update(
{
"init": self.init,
"test": self.test,
"update": self.update,
"body": self.body,
}
)
class ForInStatement(Statement): class ForInStatement(Statement):
@ -451,7 +408,6 @@ class ForInStatement(Statement):
self.left = left self.left = left
self.right = right self.right = right
self.body = body self.body = body
self._fields.update({"left": self.left, "right": self.right, "body": self.body})
# "Declarations" block # "Declarations" block
@ -482,12 +438,11 @@ class VariableDeclarator(Node):
"""A variable declarator.""" """A variable declarator."""
def __init__( def __init__(
self, loc: Optional[SourceLocation], var_id: Pattern, init: Optional[Expression] self, loc: Optional[SourceLocation], var_id: Pattern, init: Optional[Exception]
): ):
super().__init__("VariableDeclarator", loc) super().__init__("VariableDeclarator", loc)
self.id = var_id self.id = var_id
self.init = init self.init = init
self._fields.update({"id": self.id, "init": self.init})
class VariableDeclaration(Declaration): class VariableDeclaration(Declaration):
@ -502,7 +457,6 @@ class VariableDeclaration(Declaration):
super().__init__("VariableDeclaration", loc) super().__init__("VariableDeclaration", loc)
self.declarations = declarations self.declarations = declarations
self.kind = kind self.kind = kind
self._fields.update({"kind": self.kind, "declarations": self.declarations})
# "Expressions" block # "Expressions" block
@ -533,7 +487,6 @@ class SpreadElement(Node):
def __init__(self, loc: Optional[SourceLocation], argument: Expression): def __init__(self, loc: Optional[SourceLocation], argument: Expression):
super().__init__("SpreadElement", loc) super().__init__("SpreadElement", loc)
self.argument = argument self.argument = argument
self._fields.update({"argument": self.argument})
class ThisExpression(Expression): class ThisExpression(Expression):
@ -553,7 +506,6 @@ class ArrayExpression(Expression):
): ):
super().__init__("ArrayExpression", loc) super().__init__("ArrayExpression", loc)
self.elements = elements self.elements = elements
self._fields.update({"elements": self.elements})
class ObjectExpression(Expression): class ObjectExpression(Expression):
@ -562,7 +514,6 @@ class ObjectExpression(Expression):
def __init__(self, loc: Optional[SourceLocation], properties: List[Property]): def __init__(self, loc: Optional[SourceLocation], properties: List[Property]):
super().__init__("ObjectExpression", loc) super().__init__("ObjectExpression", loc)
self.properties = properties self.properties = properties
self._fields.update({"properties": self.properties})
class FunctionExpression(Function, Expression): class FunctionExpression(Function, Expression):
@ -590,7 +541,6 @@ class ArrowFunctionExpression(Function, Expression):
): ):
super().__init__("ArrowFunctionExpression", loc, None, params, body) super().__init__("ArrowFunctionExpression", loc, None, params, body)
self.expression = expression self.expression = expression
self._fields.update({"expression": self.expression})
class UnaryExpression(Expression): class UnaryExpression(Expression):
@ -607,13 +557,6 @@ class UnaryExpression(Expression):
self.operator = operator self.operator = operator
self.prefix = prefix self.prefix = prefix
self.argument = argument self.argument = argument
self._fields.update(
{
"operator": self.operator,
"prefix": self.prefix,
"argument": self.argument,
}
)
class UpdateExpression(Expression): class UpdateExpression(Expression):
@ -630,13 +573,6 @@ class UpdateExpression(Expression):
self.operator = operator self.operator = operator
self.argument = argument self.argument = argument
self.prefix = prefix self.prefix = prefix
self._fields.update(
{
"operator": self.operator,
"argument": self.argument,
"prefix": self.prefix,
}
)
class BinaryExpression(Expression): class BinaryExpression(Expression):
@ -653,9 +589,6 @@ class BinaryExpression(Expression):
self.operator = operator self.operator = operator
self.left = left self.left = left
self.right = right self.right = right
self._fields.update(
{"operator": self.operator, "left": self.left, "right": self.right}
)
class AssignmentExpression(Expression): class AssignmentExpression(Expression):
@ -674,9 +607,6 @@ class AssignmentExpression(Expression):
self.operator = operator self.operator = operator
self.left = left self.left = left
self.right = right self.right = right
self._fields.update(
{"operator": self.operator, "left": self.left, "right": self.right}
)
class LogicalExpression(Expression): class LogicalExpression(Expression):
@ -693,9 +623,6 @@ class LogicalExpression(Expression):
self.operator = operator self.operator = operator
self.left = left self.left = left
self.right = right self.right = right
self._fields.update(
{"operator": self.operator, "left": self.left, "right": self.right}
)
class MemberExpression(Expression, Pattern): class MemberExpression(Expression, Pattern):
@ -714,13 +641,6 @@ class MemberExpression(Expression, Pattern):
self.object = member_object self.object = member_object
self.property = member_property self.property = member_property
self.computed = computed self.computed = computed
self._fields.update(
{
"object": self.object,
"property": self.property,
"computed": self.computed,
}
)
class ConditionalExpression(Expression): class ConditionalExpression(Expression):
@ -737,13 +657,6 @@ class ConditionalExpression(Expression):
self.test = test self.test = test
self.alternate = alternate self.alternate = alternate
self.consequent = consequent self.consequent = consequent
self._fields.update(
{
"test": self.test,
"alternate": self.alternate,
"consequent": self.consequent,
}
)
class CallExpression(Expression): class CallExpression(Expression):
@ -758,7 +671,6 @@ class CallExpression(Expression):
super().__init__("CallExpression", loc) super().__init__("CallExpression", loc)
self.callee = callee self.callee = callee
self.arguments = arguments self.arguments = arguments
self._fields.update({"callee": self.callee, "arguments": self.arguments})
class NewExpression(Expression): class NewExpression(Expression):
@ -773,7 +685,6 @@ class NewExpression(Expression):
super().__init__("NewExpression", loc) super().__init__("NewExpression", loc)
self.callee = callee self.callee = callee
self.arguments = arguments self.arguments = arguments
self._fields.update({"callee": self.callee, "arguments": self.arguments})
class SequenceExpression(Expression): class SequenceExpression(Expression):
@ -782,7 +693,6 @@ class SequenceExpression(Expression):
def __init__(self, loc: Optional[SourceLocation], expressions: List[Expression]): def __init__(self, loc: Optional[SourceLocation], expressions: List[Expression]):
super().__init__("SequenceExpression", loc) super().__init__("SequenceExpression", loc)
self.expressions = expressions self.expressions = expressions
self._fields.update({"expressions": self.expressions})
def _generate_unary_expression(operator: UnaryOperator, docstring: str): def _generate_unary_expression(operator: UnaryOperator, docstring: str):
@ -1030,16 +940,6 @@ class Property(Node):
self.method = method self.method = method
self.shorthand = shorthand self.shorthand = shorthand
self.computed = computed self.computed = computed
self._fields.update(
{
"key": self.key,
"value": self.value,
"kind": self.kind,
"method": self.method,
"shorthand": self.shorthand,
"computed": self.computed,
}
)
class AssignmentProperty(Property): class AssignmentProperty(Property):
@ -1078,7 +978,6 @@ class ObjectPattern(Pattern):
): ):
super().__init__("ObjectPattern", loc) super().__init__("ObjectPattern", loc)
self.properties = properties self.properties = properties
self._fields.update({"properties": self.properties})
class ArrayPattern(Pattern): class ArrayPattern(Pattern):
@ -1087,15 +986,3 @@ class ArrayPattern(Pattern):
): ):
super().__init__("ArrayPattern", loc) super().__init__("ArrayPattern", loc)
self.elements = elements self.elements = elements
self._fields.update({"elements": self.elements})
# "Identifier" block
class Identifier(Expression, Pattern):
"""An identifier. Note that an identifier may be an expression or a destructuring pattern."""
def __init__(self, loc: Optional[SourceLocation], name: str):
super().__init__("Identifier", loc)
self.name = name

View File

@ -1,5 +1,5 @@
import logging import logging
from typing import Optional, List, Union from typing import Optional, List
import antlr4.ParserRuleContext import antlr4.ParserRuleContext
from lex.JavaScriptParser import JavaScriptParser from lex.JavaScriptParser import JavaScriptParser
@ -23,52 +23,6 @@ def _get_source_location(
return ast.nodes.SourceLocation(source=source, start=start_pos, end=end_pos) return ast.nodes.SourceLocation(source=source, start=start_pos, end=end_pos)
class AssignableListener(JSBaseListener):
_result: Union[
ast.nodes.Identifier, ast.nodes.ObjectPattern, ast.nodes.ArrayPattern
]
@property
def result(self):
return self._result
def enterAssignable(self, ctx: JavaScriptParser.AssignableContext):
logging.debug("Entered section Assignable")
ctx.getChild(0).enterRule(self)
def enterIdentifier(self, ctx: JavaScriptParser.IdentifierContext):
logging.debug("Entered section Identifier")
loc = _get_source_location(ctx, None)
self._result = ast.nodes.Identifier(loc, ctx.getText())
def enterArrayLiteral(self, ctx: JavaScriptParser.ArrayLiteralContext):
logging.debug("Entered section ArrayLiteral")
pass # TODO
def enterObjectLiteral(self, ctx: JavaScriptParser.ObjectLiteralContext):
logging.debug("Entered section ObjectLiteral")
pass # TODO
class VariableDeclarationListener(JSBaseListener):
_var_decl: ast.nodes.VariableDeclarator
@property
def var_declarator(self):
return self._var_decl
def enterVariableDeclaration(
self, ctx: JavaScriptParser.VariableDeclarationContext
):
loc = _get_source_location(ctx, None)
assign_listener = AssignableListener()
ctx.assignable().enterRule(assign_listener)
# ctx.singleExpression().enterRule(expression_listener) # FIXME No ExpressionListener yet
self._var_decl = ast.nodes.VariableDeclarator(
loc, assign_listener.result, None
) # FIXME
class StatementListener(JSBaseListener): class StatementListener(JSBaseListener):
_stmt: ast.nodes.Statement _stmt: ast.nodes.Statement
@ -96,26 +50,12 @@ class StatementListener(JSBaseListener):
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 = ast.nodes.BlockStatement(loc, stmt_list)
def enterVariableStatement(self, ctx: JavaScriptParser.VariableStatementContext):
logging.debug("Entered section VariableStatement")
ctx.variableDeclarationList().enterRule(self)
def enterVariableDeclarationList( def enterVariableDeclarationList(
self, ctx: JavaScriptParser.VariableDeclarationListContext self, ctx: JavaScriptParser.VariableDeclarationListContext
): ):
"""Listener for VariableDeclaration.""" """Listener for VariableDeclaration."""
logging.debug("Entered section VariableDeclaration") logging.debug("Entered section VariableDeclaration")
pass
var_modifier: ast.nodes.VarDeclKind = ctx.varModifier().getText()
var_decls: List[ast.nodes.VariableDeclarator] = []
for var_decl in ctx.variableDeclaration():
var_decl_listener = VariableDeclarationListener()
var_decl.enterRule(var_decl_listener)
var_decls.append(var_decl_listener.var_declarator)
loc = _get_source_location(ctx, None)
self._stmt = ast.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."""

View File

@ -1,3 +1,4 @@
antlr4-python3-runtime==4.8 antlr4-python3-runtime==4.8
colorama==0.4.3 colorama==0.4.3
coloredlogs==14.0 coloredlogs==14.0
tree_format==0.1.2