def unravel_list(exp: Union[List[List[Node]], List[Node], Node]) -> List[Node]: parsing: List[Node] = [] if isinstance(exp, list): for tok in exp: while isinstance(tok, list): temp = tok.pop(0) if not tok and not isinstance(temp, list): tok = temp elif not tok: parsing.append(ast.ASCIILiteralNode(['('])) tok = temp parsing.append(ast.ASCIILiteralNode([')'])) else: parsing.append(ast.ASCIILiteralNode(['('])) parsing.extend(unravel_list(temp)) parsing.append(ast.ASCIILiteralNode([')'])) parsing.append(tok) i = 0 while i + 2 < len(parsing): found = False if isinstance(parsing[i], ast.ASCIILiteralNode) \ and isinstance(parsing[i+1], ast.WhiteSpaceNode) \ and isinstance(parsing[i+2], ast.ASCIILiteralNode): parsing.pop(i) parsing.pop(i) parsing.pop(i) found = True if not found: i += 1 return parsing
def p_safe_code_expression_to_parens( p: yacc.YaccProduction) -> yacc.YaccProduction: """ safe_code_expression : code_expression_parenthetical """ p[0] = ast.LinesNode([ ast.ASCIILiteralNode(['(']), ast.LinesNode(p[1]), ast.ASCIILiteralNode([')']) ])
def p_safe_code_expressions_ascii_literal( p: yacc.YaccProduction) -> yacc.YaccProduction: """ safe_code_expression : '<' | '>' | '+' | '-' | '%' | '^' | '&' | '*' | '{' | '}' | '[' | ']' | '=' | ';' | ':' | '#' | '.' | '?' | '~' | '/' """ p[0] = ast.ASCIILiteralNode(p[1])
def test_macro_expansion_variadic_too_few_args(self): alpha = ast.IdentifierNode(["alpha"]) omega = ast.IdentifierNode(["omega"]) vary_expand = ast.IdentifierNode(["vary"]) whitespace = ast.WhiteSpaceNode([' ']) plus = ast.ASCIILiteralNode(['+']) macro = symtable.MacroExpansion( 'notfoo', [alpha, whitespace, plus, whitespace, omega, plus, vary_expand], ['alpha', 'omega', 'varia...']) testutils.assert_raises( macro.expand, symtable.PepperSyntaxError, ["notfoo was given 2 arguments, but takes a minimum of 4"], # NOQA ['1', '2']) testutils.assert_raises( macro.expand, symtable.PepperSyntaxError, ["notfoo was given 0 arguments, but takes a minimum of 4"], []) testutils.assert_raises( macro.expand, symtable.PepperSyntaxError, ["Macro notfoo invoked without args, but is variadic"], None)
def test_macro_expansion_bad_variadic_position(self): alpha = ast.IdentifierNode(["alpha"]) omega = ast.IdentifierNode(["omega"]) vary_expand = ast.IdentifierNode(["vary"]) whitespace = ast.WhiteSpaceNode([' ']) plus = ast.ASCIILiteralNode(['+']) testutils.assert_raises( symtable.MacroExpansion, symtable.PepperSyntaxError, [ "Variadic macro argument must be at the end of the argument definition list" ], # NOQA 'foo', [alpha, whitespace, plus, whitespace, omega, plus, vary_expand], ['alpha', 'omega', 'varia...', 'extra...']) testutils.assert_raises( symtable.MacroExpansion, symtable.PepperSyntaxError, [ "Variadic macro argument must be at the end of the argument definition list" ], # NOQA 'foo', [alpha, whitespace, plus, whitespace, omega, plus, vary_expand], ['alpha', 'omega', 'varia...', 'notvaria'])
def test_macro_expansion_bad_number_of_args(self): alpha = ast.IdentifierNode(['alpha']) omega = ast.IdentifierNode(['omega']) whitespace = ast.WhiteSpaceNode([' ']) plus = ast.ASCIILiteralNode(['+']) macro = symtable.MacroExpansion( 'foo', [alpha, whitespace, plus, whitespace, omega], ['alpha', 'omega']) testutils.assert_raises( macro.expand, symtable.PepperSyntaxError, [ "Wrong number of arguments in macro expansion for foo", "expected 2", "got 0" ], args=[]) testutils.assert_raises(macro.expand, symtable.PepperSyntaxError, ["Macro foo expects args, but was given none"], args=None) newmacro = symtable.MacroExpansion( 'otherfoo', [alpha, whitespace, plus, whitespace, omega], None) testutils.assert_raises( newmacro.expand, symtable.PepperSyntaxError, ["Macro otherfoo doesn't take any args, but was given 2"], ["1", "2"])
def p_statement_to_ascii_literal( p: yacc.YaccProduction) -> yacc.YaccProduction: """ code_expression : | ',' | '(' | ')' """ p[0] = ast.ASCIILiteralNode(p[1])
def test_macro_expansion_good_number_of_args(self): alpha = ast.IdentifierNode(['alpha']) omega = ast.IdentifierNode(['omega']) whitespace = ast.WhiteSpaceNode([' ']) plus = ast.ASCIILiteralNode(['+']) macro = symtable.MacroExpansion( 'foo', [alpha, whitespace, plus, whitespace, omega], ['alpha', 'omega']) expansion = macro.expand(args=['1', '2']) assert (expansion == "1 + 2")
def test_macro_expansion_variadic(self): alpha = ast.IdentifierNode(["alpha"]) omega = ast.IdentifierNode(["omega"]) vary_expand = ast.IdentifierNode(["varia"]) whitespace = ast.WhiteSpaceNode([' ']) plus = ast.ASCIILiteralNode(['+']) macro = symtable.MacroExpansion('alsonotfoo', [ alpha, whitespace, plus, whitespace, omega, whitespace, plus, whitespace, vary_expand ], ['alpha', 'omega', 'varia...']) expansion = macro.expand( args=['1', '2', '3', '4', '5', '6', '7', '8', '9']) assert (expansion == "1 + 2 + 3, 4, 5, 6, 7, 8, 9")