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_define_expression_some_args( p: yacc.YaccProduction) -> yacc.YaccProduction: """ define_expression : PREPROCESSING_KEYWORD_DEFINE WHITESPACE IDENTIFIER '(' identifier_list ')' maybe_space macro_expansion """ print(f"Macro expansion for ident {p[3]} with args {p[5]}") p[0] = symtable.MacroExpansion(p[3], p[8], args=p[5])
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_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_define_expression_no_args(p: yacc.YaccProduction) -> yacc.YaccProduction: """ define_expression : PREPROCESSING_KEYWORD_DEFINE WHITESPACE IDENTIFIER WHITESPACE macro_expansion """ p[0] = symtable.MacroExpansion(p[3], p[5])
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"])])
def __init__(self, children: List[str] = []) -> None: super(WhiteSpaceNode, self).__init__("Whitespace", children) class ASCIILiteralNode(PrimitiveNode): def __init__(self, children: List[str] = []) -> None: super(ASCIILiteralNode, self).__init__('ASCIILit', children) class OperatorNode(PrimitiveNode): def __init__(self, children: List[str] = []) -> None: super(OperatorNode, self).__init__('2CharOperator', children) class StringLiteralNode(PrimitiveNode): def __init__(self, children: List[str] = []) -> None: super(StringLiteralNode, self).__init__('StringLit', children) class PreprocessingNumberNode(PrimitiveNode): def __init__(self, children: List[str] = []) -> None: super(PreprocessingNumberNode, self).__init__("PreprocessingNumber", children) # Predefined arguments symtable.TABLE['true'] = symtable.MacroExpansion( 'true', [PreprocessingNumberNode(['1'])]) symtable.TABLE['false'] = symtable.MacroExpansion( 'false', [PreprocessingNumberNode(['0'])])