@ -6,6 +6,7 @@
from __future__ import annotations
import typing as T
from itertools import zip_longest
if T . TYPE_CHECKING :
from . . import mparser
@ -32,10 +33,10 @@ class AstVisitor:
def visit_FormatStringNode ( self , node : mparser . FormatStringNode ) - > None :
self . visit_default_func ( node )
def visit_MultilineStringNode ( self , node : mparser . MultilineFormat StringNode ) - > None :
def visit_MultilineStringNode ( self , node : mparser . MultilineStringNode ) - > None :
self . visit_default_func ( node )
def visit_FormatMultilineStringNode ( self , node : mparser . FormatStringNode ) - > None :
def visit_FormatMultilineStringNode ( self , node : mparser . Multiline FormatStringNode) - > None :
self . visit_default_func ( node )
def visit_ContinueNode ( self , node : mparser . ContinueNode ) - > None :
@ -159,3 +160,181 @@ class AstVisitor:
def visit_ParenthesizedNode ( self , node : mparser . ParenthesizedNode ) - > None :
self . visit_default_func ( node )
node . inner . accept ( self )
class FullAstVisitor ( AstVisitor ) :
""" Visit all nodes, including Symbol and Whitespaces """
def enter_node ( self , node : mparser . BaseNode ) - > None :
pass
def exit_node ( self , node : mparser . BaseNode ) - > None :
if node . whitespaces :
node . whitespaces . accept ( self )
def visit_default_func ( self , node : mparser . BaseNode ) - > None :
self . enter_node ( node )
self . exit_node ( node )
def visit_UnaryOperatorNode ( self , node : mparser . UnaryOperatorNode ) - > None :
self . enter_node ( node )
node . operator . accept ( self )
node . value . accept ( self )
self . exit_node ( node )
def visit_BinaryOperatorNode ( self , node : mparser . BinaryOperatorNode ) - > None :
self . enter_node ( node )
node . left . accept ( self )
node . operator . accept ( self )
node . right . accept ( self )
self . exit_node ( node )
def visit_ArrayNode ( self , node : mparser . ArrayNode ) - > None :
self . enter_node ( node )
node . lbracket . accept ( self )
node . args . accept ( self )
node . rbracket . accept ( self )
self . exit_node ( node )
def visit_DictNode ( self , node : mparser . DictNode ) - > None :
self . enter_node ( node )
node . lcurl . accept ( self )
node . args . accept ( self )
node . rcurl . accept ( self )
self . exit_node ( node )
def visit_OrNode ( self , node : mparser . OrNode ) - > None :
self . visit_BinaryOperatorNode ( node )
def visit_AndNode ( self , node : mparser . AndNode ) - > None :
self . visit_BinaryOperatorNode ( node )
def visit_ComparisonNode ( self , node : mparser . ComparisonNode ) - > None :
self . visit_BinaryOperatorNode ( node )
def visit_ArithmeticNode ( self , node : mparser . ArithmeticNode ) - > None :
self . visit_BinaryOperatorNode ( node )
def visit_NotNode ( self , node : mparser . NotNode ) - > None :
self . visit_UnaryOperatorNode ( node )
def visit_CodeBlockNode ( self , node : mparser . CodeBlockNode ) - > None :
self . enter_node ( node )
if node . pre_whitespaces :
node . pre_whitespaces . accept ( self )
for i in node . lines :
i . accept ( self )
self . exit_node ( node )
def visit_IndexNode ( self , node : mparser . IndexNode ) - > None :
self . enter_node ( node )
node . iobject . accept ( self )
node . lbracket . accept ( self )
node . index . accept ( self )
node . rbracket . accept ( self )
self . exit_node ( node )
def visit_MethodNode ( self , node : mparser . MethodNode ) - > None :
self . enter_node ( node )
node . source_object . accept ( self )
node . dot . accept ( self )
node . name . accept ( self )
node . lpar . accept ( self )
node . args . accept ( self )
node . rpar . accept ( self )
self . exit_node ( node )
def visit_FunctionNode ( self , node : mparser . FunctionNode ) - > None :
self . enter_node ( node )
node . func_name . accept ( self )
node . lpar . accept ( self )
node . args . accept ( self )
node . rpar . accept ( self )
self . exit_node ( node )
def visit_AssignmentNode ( self , node : mparser . AssignmentNode ) - > None :
self . enter_node ( node )
node . var_name . accept ( self )
node . operator . accept ( self )
node . value . accept ( self )
self . exit_node ( node )
def visit_PlusAssignmentNode ( self , node : mparser . PlusAssignmentNode ) - > None :
self . visit_AssignmentNode ( node )
def visit_ForeachClauseNode ( self , node : mparser . ForeachClauseNode ) - > None :
self . enter_node ( node )
node . foreach_ . accept ( self )
for varname , comma in zip_longest ( node . varnames , node . commas ) :
varname . accept ( self )
if comma is not None :
comma . accept ( self )
node . colon . accept ( self )
node . items . accept ( self )
node . block . accept ( self )
node . endforeach . accept ( self )
self . exit_node ( node )
def visit_IfClauseNode ( self , node : mparser . IfClauseNode ) - > None :
self . enter_node ( node )
for i in node . ifs :
i . accept ( self )
node . elseblock . accept ( self )
node . endif . accept ( self )
self . exit_node ( node )
def visit_UMinusNode ( self , node : mparser . UMinusNode ) - > None :
self . visit_UnaryOperatorNode ( node )
def visit_IfNode ( self , node : mparser . IfNode ) - > None :
self . enter_node ( node )
node . if_ . accept ( self )
node . condition . accept ( self )
node . block . accept ( self )
self . exit_node ( node )
def visit_ElseNode ( self , node : mparser . ElseNode ) - > None :
self . enter_node ( node )
node . else_ . accept ( self )
node . block . accept ( self )
self . exit_node ( node )
def visit_TernaryNode ( self , node : mparser . TernaryNode ) - > None :
self . enter_node ( node )
node . condition . accept ( self )
node . questionmark . accept ( self )
node . trueblock . accept ( self )
node . colon . accept ( self )
node . falseblock . accept ( self )
self . exit_node ( node )
def visit_ArgumentNode ( self , node : mparser . ArgumentNode ) - > None :
self . enter_node ( node )
commas_iter = iter ( node . commas )
for arg in node . arguments :
arg . accept ( self )
try :
comma = next ( commas_iter )
comma . accept ( self )
except StopIteration :
pass
assert len ( node . colons ) == len ( node . kwargs )
for ( key , val ) , colon in zip ( node . kwargs . items ( ) , node . colons ) :
key . accept ( self )
colon . accept ( self )
val . accept ( self )
try :
comma = next ( commas_iter )
comma . accept ( self )
except StopIteration :
pass
self . exit_node ( node )
def visit_ParenthesizedNode ( self , node : mparser . ParenthesizedNode ) - > None :
self . enter_node ( node )
node . lpar . accept ( self )
node . inner . accept ( self )
node . rpar . accept ( self )
self . exit_node ( node )