Пример #1
0
    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)
Пример #2
0
    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'])
Пример #3
0
    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"])
Пример #4
0
    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")
Пример #5
0
    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")
Пример #6
0
def p_whitespace_unsafe(p: yacc.YaccProduction) -> yacc.YaccProduction:
    """
    safe_code_expression : WHITESPACE
    """
    p[0] = ast.WhiteSpaceNode([p[1]])
Пример #7
0
def p_maybe_whitespace_some(p: yacc.YaccProduction) -> yacc.YaccProduction:
    """
    maybe_space : WHITESPACE
    """
    p[0] = ast.WhiteSpaceNode([p[1]])
Пример #8
0
def p_maybe_whitespace_none(p: yacc.YaccProduction) -> yacc.YaccProduction:
    """
    maybe_space :
    """
    p[0] = ast.WhiteSpaceNode([""])
Пример #9
0
def p_spaces(p: yacc.YaccProduction) -> yacc.YaccProduction:
    '''
    spaces : WHITESPACE spaces
    '''

    p[0] = ast.WhiteSpaceNode([p[1]])