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 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")
def p_statement_to_identifier(p: yacc.YaccProduction) -> yacc.YaccProduction: """ safe_code_expression : IDENTIFIER """ p[0] = ast.IdentifierNode([p[1]])
def p_identifier_call(p: yacc.YaccProduction) -> yacc.YaccProduction: """ safe_code_expression : IDENTIFIER code_expression_parenthetical """ print(f"macro call with ident {p[1]} and args {p[2]}") p[0] = ast.IdentifierNode([p[1]], args=p[2])
def p_define_expression_no_expansion( p: yacc.YaccProduction) -> yacc.YaccProduction: """ define_expression : PREPROCESSING_KEYWORD_DEFINE WHITESPACE IDENTIFIER """ p[0] = symtable.MacroExpansion(p[3], [ast.IdentifierNode(["true"])])