示例#1
0
def test_empty_class():
    assert_parser_result(
        r"""
0001:0001    "class"(class)
0001:0007     SYMBOL(MyClass)
0001:0014      COLON(:)
0001:0015    NEWLINE
0002:0001     INDENT
0002:0005     SYMBOL(pass)
0002:0009    NEWLINE
0002:0009     DEDENT
0002:0009    NEWLINE
0003:0001    NEWLINE
0004:0001    NEWLINE
""",
        r"""
["class":class]
    [SYMBOL:MyClass]
    [COLON::]
        [SYMBOL:pass]
[EOF:]
""",
        r"""
PepClass(
    PepSymbol('MyClass'),
    (),
    (
        PepSymbol('pass'),
    )
)
""" )
示例#2
0
def test_calculated_type():
    assert_parser_result(
        r"""
0001:0001      "def"(def)
0001:0005     SYMBOL(int)
0001:0009     SYMBOL(myfn)
0001:0013     LPAREN
0001:0015     SYMBOL(fn2)
0001:0018     LPAREN
0001:0020     SYMBOL(a)
0001:0021      COMMA(,)
0001:0023     SYMBOL(b)
0001:0025     RPAREN
0001:0027     SYMBOL(cfg)
0001:0031     RPAREN
0001:0032      COLON(:)
0001:0033    NEWLINE
0002:0001     INDENT
0002:0005     SYMBOL(pass)
0002:0009    NEWLINE
0002:0009     DEDENT
0002:0009    NEWLINE
0003:0001    NEWLINE
""",
        r"""
["def":def]
    [SYMBOL:int]
    [SYMBOL:myfn]
    [LPAREN:]
        [LPAREN:]
            [SYMBOL:fn2]
            [SYMBOL:a]
            [COMMA:,]
            [SYMBOL:b]
        [SYMBOL:cfg]
    [COLON::]
        [SYMBOL:pass]
[EOF:]
""",
        r"""
PepDef(
    PepSymbol('int'),
    PepSymbol('myfn'),
    (
        (
            PepFunctionCall(
                PepSymbol('fn2'),
                (
                    PepSymbol('a'), 
                    PepSymbol('b')
                )
            ), 
            PepSymbol('cfg')
        ),
    ),
    (
        PepSymbol('pass'),
    )
)
""" )
示例#3
0
def test_hello_world():
    assert_parser_result(
        r"""
0001:0001    NEWLINE
0002:0001     SYMBOL(print)
0002:0006     LPAREN
0002:0008     STRING(Hello, world!)
0002:0024     RPAREN
0002:0025    NEWLINE
0003:0001    NEWLINE
0004:0001    NEWLINE
""",
        r"""
[LPAREN:]
    [SYMBOL:print]
    [STRING:Hello, world!]
[EOF:]
""",
        r"""
PepFunctionCall(
    PepSymbol('print'),
    (
        PepString('Hello, world!'),
    )
)
""" )
示例#4
0
def test_quoted_sum():
    assert_parser_result(
        r"""
0001:0001    "quote"(quote)
0001:0015      COLON(:)
0001:0016    NEWLINE
0002:0001     INDENT
0002:0005     SYMBOL(x)
0002:0012       PLUS(+)
0002:0013     SYMBOL(y)
0002:0013    NEWLINE
0002:0013     DEDENT
0002:0013    NEWLINE
0003:0001    NEWLINE

""",
        r"""
["quote":quote]
    [COLON::]
        [PLUS:+]
            [SYMBOL:x]
            [SYMBOL:y]
[EOF:]
""",
        r"""
PepQuote((PepPlus(PepSymbol('x'),PepSymbol('y')),))
""",
    )
示例#5
0
def test_function_with_array_lookup():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(print)
0001:0006     LPAREN
0001:0007     SYMBOL(myarr)
0001:0012     LSQUBR
0001:0013        INT(2)
0001:0014     RSQUBR
0001:0015     RPAREN
0001:0016    NEWLINE
""",
    r"""
[LPAREN:]
    [SYMBOL:print]
    [LSQUBR:]
        [SYMBOL:myarr]
        [INT:2]
[EOF:]
""",
    r"""
PepFunctionCall(
    PepSymbol('print'),
    (
        PepArrayLookup(
            PepSymbol('myarr'),
            PepInt('2')
        ),
    )
)
""" )
示例#6
0
def test_multiline():
    assert_parser_result(
        r"""
0001:0001   "import"(import)
0001:0008     SYMBOL(sys)
0001:0011    NEWLINE
0002:0001     SYMBOL(print)
0002:0006     LPAREN
0002:0008     SYMBOL(sys.argv)
0002:0017     RPAREN
0002:0018    NEWLINE
""",
        r"""
["import":import]
    [SYMBOL:sys]
[LPAREN:]
    [SYMBOL:print]
    [SYMBOL:sys.argv]
[EOF:]
""",
        r"""
PepImport('sys')
PepFunctionCall(
    PepSymbol('print'),
    (
        PepSymbol('sys.argv'),
    )
)
""" )
示例#7
0
def test_call_function_with_args():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(f)
0001:0002     LPAREN
0001:0004        INT(1)
0001:0005      COMMA(,)
0001:0007        INT(2)
0001:0008      COMMA(,)
0001:0010        INT(3)
0001:0012     RPAREN
0001:0013    NEWLINE
""",
        r"""
[LPAREN:]
    [SYMBOL:f]
    [INT:1]
    [COMMA:,]
    [INT:2]
    [COMMA:,]
    [INT:3]
[EOF:]
""",
        r"""
PepFunctionCall(
    PepSymbol('f'),
    (
        PepInt('1'), 
        PepInt('2'), 
        PepInt('3')
    )
)
""" )
示例#8
0
def test_plus_in_function_call():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(print)
0001:0006     LPAREN
0001:0008        INT(3)
0001:0010       PLUS(+)
0001:0012     SYMBOL(b)
0001:0014     RPAREN
0001:0015    NEWLINE
""",
        r"""
[LPAREN:]
    [SYMBOL:print]
    [PLUS:+]
        [INT:3]
        [SYMBOL:b]
[EOF:]
""",
        r"""
PepFunctionCall(
    PepSymbol('print'),
    (
        PepPlus(
            PepInt('3'),
            PepSymbol('b')
        ),
    )
)
""" )
示例#9
0
def test_define_function_with_args():
    assert_parser_result(
        r"""
0001:0001      "def"(def)
0001:0005     SYMBOL(void)
0001:0010     SYMBOL(myfn)
0001:0014     LPAREN
0001:0016     SYMBOL(int)
0001:0020     SYMBOL(x)
0001:0021      COMMA(,)
0001:0023     SYMBOL(bool)
0001:0028     SYMBOL(y)
0001:0029      COMMA(,)
0001:0031     SYMBOL(int)
0001:0035     SYMBOL(z)
0001:0037     RPAREN
0001:0038      COLON(:)
0001:0039    NEWLINE
0002:0001     INDENT
0002:0005     SYMBOL(pass)
0002:0009    NEWLINE
0002:0009     DEDENT
0002:0009    NEWLINE
0003:0001    NEWLINE
""",
        r"""
["def":def]
    [SYMBOL:void]
    [SYMBOL:myfn]
    [LPAREN:]
        [SYMBOL:int]
        [SYMBOL:x]
        [COMMA:,]
        [SYMBOL:bool]
        [SYMBOL:y]
        [COMMA:,]
        [SYMBOL:int]
        [SYMBOL:z]
    [COLON::]
        [SYMBOL:pass]
[EOF:]
""",
        r"""
PepDef(
    PepSymbol('void'),
    PepSymbol('myfn'),
    (
        (PepSymbol('int'), PepSymbol('x')), 
        (PepSymbol('bool'), PepSymbol('y')), 
        (PepSymbol('int'), PepSymbol('z'))
    ),
    (
        PepSymbol('pass'),
    )
)
""" )
示例#10
0
def test_init_method():
    assert_parser_result(
        r"""
0001:0001    "class"(class)
0001:0007     SYMBOL(MyClass)
0001:0014      COLON(:)
0001:0015    NEWLINE
0002:0001     INDENT
0002:0005 "def_init"(def_init)
0002:0014     LPAREN
0002:0016     SYMBOL(int)
0002:0020     SYMBOL(x)
0002:0022     RPAREN
0002:0023      COLON(:)
0002:0024    NEWLINE
0003:0001     INDENT
0003:0009     SYMBOL(pass)
0003:0013    NEWLINE
0003:0013     DEDENT
0003:0013    NEWLINE
0003:0013     DEDENT
0003:0013    NEWLINE
""",
        r"""
["class":class]
    [SYMBOL:MyClass]
    [COLON::]
        ["def_init":def_init]
            [LPAREN:]
                [SYMBOL:int]
                [SYMBOL:x]
            [COLON::]
                [SYMBOL:pass]
[EOF:]
""",
        r"""
PepClass(
    PepSymbol('MyClass'),
    (),
    (
        PepDefInit(
            (
                (
                    PepSymbol('int'), 
                    PepSymbol('x')
                ),
            ),
            (
                PepSymbol('pass'),
            )
        ),
    )
)
""" )
示例#11
0
def test_define_function_two_lines():
    assert_parser_result(
        r"""
0001:0001      "def"(def)
0001:0005     SYMBOL(void)
0001:0010     SYMBOL(myfn)
0001:0014     LPAREN
0001:0015     RPAREN
0001:0016      COLON(:)
0001:0017    NEWLINE
0002:0001     INDENT
0002:0005     SYMBOL(int)
0002:0009     SYMBOL(a)
0002:0011     EQUALS(=)
0002:0013        INT(7)
0002:0014    NEWLINE
0003:0005   "return"(return)
0003:0012     SYMBOL(a)
0003:0013    NEWLINE
0003:0013     DEDENT
0003:0013    NEWLINE
0004:0001    NEWLINE
""",
        r"""
["def":def]
    [SYMBOL:void]
    [SYMBOL:myfn]
    [LPAREN:]
    [COLON::]
        [EQUALS:=]
            [SYMBOL:int]
            [SYMBOL:a]
            [INT:7]
        ["return":return]
            [SYMBOL:a]
[EOF:]
""",
        """
PepDef(
    PepSymbol('void'),
    PepSymbol('myfn'),
    (),
    (
        PepInit(
            PepSymbol('int'),
            PepSymbol('a'),
            PepInt('7')
        ), 
        PepReturn(
            PepSymbol('a')
        )
    )
)
""" )
示例#12
0
def test_method():
    assert_parser_result(
        r"""
0001:0001    "class"(class)
0001:0007     SYMBOL(MyClass)
0001:0014      COLON(:)
0001:0015    NEWLINE
0002:0001     INDENT
0002:0005      "def"(def)
0002:0009     SYMBOL(void)
0002:0014     SYMBOL(myfn)
0002:0018     LPAREN
0002:0019     RPAREN
0002:0020      COLON(:)
0002:0021    NEWLINE
0003:0001     INDENT
0003:0009     SYMBOL(pass)
0003:0013    NEWLINE
0003:0013     DEDENT
0003:0013    NEWLINE
0003:0013     DEDENT
0003:0013    NEWLINE
""",
        r"""
["class":class]
    [SYMBOL:MyClass]
    [COLON::]
        ["def":def]
            [SYMBOL:void]
            [SYMBOL:myfn]
            [LPAREN:]
            [COLON::]
                [SYMBOL:pass]
[EOF:]
""",
        r"""
PepClass(
    PepSymbol('MyClass'),
    (),
    (
        PepDef(
            PepSymbol('void'),
            PepSymbol('myfn'),
            (),
            (
                PepSymbol('pass'),
            )
        ),
    )
)
""" )
示例#13
0
def test_import():
    assert_parser_result(
        r"""
0001:0001   "import"(import)
0001:0008     SYMBOL(sys)
0001:0011    NEWLINE
""",
        r"""
["import":import]
    [SYMBOL:sys]
[EOF:]
""",
        r"""
PepImport('sys')
""" )
示例#14
0
def test_if_operator_and_function():
    assert_parser_result(
        r"""
0001:0001       "if"(if)
0001:0004     SYMBOL(f)
0001:0005     LPAREN
0001:0007     SYMBOL(a)
0001:0009     RPAREN
0001:0011         GT(>)
0001:0013        INT(4)
0001:0014      COLON(:)
0001:0015    NEWLINE
0002:0001     INDENT
0002:0005        INT(3)
0002:0006    NEWLINE
0002:0006     DEDENT
0002:0006    NEWLINE
0003:0001    NEWLINE
""",
        r"""
["if":if]
    [GT:>]
        [LPAREN:]
            [SYMBOL:f]
            [SYMBOL:a]
        [INT:4]
    [COLON::]
        [INT:3]
[EOF:]
""",
        r"""
PepIf(
    PepGreaterThan(
        PepFunctionCall(
            PepSymbol('f'),
            (
                PepSymbol('a'),
            )
        ),
        PepInt('4')
    ),
    (
        PepInt('3'),
    ),
    None
)
""" )
示例#15
0
def test_call_function():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(f)
0001:0002     LPAREN
0001:0003     RPAREN
0001:0004    NEWLINE
""",
        r"""
[LPAREN:]
    [SYMBOL:f]
[EOF:]
""",
        r"""
PepFunctionCall(
    PepSymbol('f'),
    ()
)
""" )
示例#16
0
def test_if_else():

    assert_parser_result(
        r"""
0001:0001       "if"(if)
0001:0004     SYMBOL(True)
0001:0008      COLON(:)
0001:0009    NEWLINE
0002:0001     INDENT
0002:0005        INT(1)
0002:0006    NEWLINE
0002:0006     DEDENT
0002:0006    NEWLINE
0003:0001     "else"(else)
0003:0005      COLON(:)
0003:0006    NEWLINE
0004:0001     INDENT
0004:0005        INT(0)
0004:0006    NEWLINE
0004:0006     DEDENT
0004:0006    NEWLINE
0005:0001    NEWLINE
""",
        r"""
["if":if]
    [SYMBOL:True]
    [COLON::]
        [INT:1]
    ["else":else]
    [COLON::]
        [INT:0]
[EOF:]
""",
        r"""
PepIf(
    PepSymbol('True'),
    (
        PepInt('1'),
    ),
    (
        PepInt('0'),
    )
)""" )
示例#17
0
def test_simple_modification():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(i)
0001:0003 PLUSEQUALS(+=)
0001:0009        INT(7)
0001:0010    NEWLINE
""",
        r"""
[PLUSEQUALS:+=]
    [SYMBOL:i]
    [INT:7]
[EOF:]
""",
        r"""
PepModification(
    PepSymbol('i'),
    PepInt('7')
)
""" )
示例#18
0
def test_operator_less_than():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(a)
0001:0002         LT(<)
0001:0003     SYMBOL(b)
0001:0004    NEWLINE
""",
        r"""
[LT:<]
    [SYMBOL:a]
    [SYMBOL:b]
[EOF:]
""",
        r"""
PepLessThan(
    PepSymbol('a'),
    PepSymbol('b')
)
""" )
示例#19
0
def test_operator_greater_than():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(a)
0001:0002         GT(>)
0001:0003     SYMBOL(b)
0001:0004    NEWLINE
""",
        r"""
[GT:>]
    [SYMBOL:a]
    [SYMBOL:b]
[EOF:]
""",
        r"""
PepGreaterThan(
    PepSymbol('a'),
    PepSymbol('b')
)
""" )
示例#20
0
def test_operator_plus():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(a)
0001:0002       PLUS(+)
0001:0003     SYMBOL(b)
0001:0004    NEWLINE
""",
        r"""
[PLUS:+]
    [SYMBOL:a]
    [SYMBOL:b]
[EOF:]
""",
        r"""
PepPlus(
    PepSymbol('a'),
    PepSymbol('b')
)
""" )
示例#21
0
def test_operator_times():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(a)
0001:0002      TIMES(*)
0001:0003     SYMBOL(b)
0001:0004    NEWLINE
""",
        r"""
[TIMES:*]
    [SYMBOL:a]
    [SYMBOL:b]
[EOF:]
""",
        r"""
PepTimes(
    PepSymbol('a'),
    PepSymbol('b')
)
""" )
示例#22
0
def test_operator_minus():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(a)
0001:0002      MINUS(-)
0001:0003     SYMBOL(b)
0001:0004    NEWLINE
""",
        r"""
[MINUS:-]
    [SYMBOL:a]
    [SYMBOL:b]
[EOF:]
""",
        r"""
PepMinus(
    PepSymbol('a'),
    PepSymbol('b')
)
""" )
示例#23
0
def test_array_lookup_qualified():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(a.b)
0001:0006     LSQUBR
0001:0007        INT(3)
0001:0008     RSQUBR
0001:0009    NEWLINE
""",
    r"""
[LSQUBR:]
    [SYMBOL:a.b]
    [INT:3]
[EOF:]
""",
    r"""
PepArrayLookup(
    PepSymbol('a.b'),
    PepInt('3')
)
""" )
示例#24
0
def test_array_lookup():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(myarr)
0001:0006     LSQUBR
0001:0007        INT(1)
0001:0008     RSQUBR
0001:0009    NEWLINE
""",
        r"""
[LSQUBR:]
    [SYMBOL:myarr]
    [INT:1]
[EOF:]
""",
        r"""
PepArrayLookup(
    PepSymbol('myarr'),
    PepInt('1')
)
""" )
示例#25
0
def test_if():
    assert_parser_result(
        r"""
0001:0001       "if"(if)
0001:0004     SYMBOL(True)
0001:0008      COLON(:)
0001:0009    NEWLINE
0002:0001     INDENT
0002:0005     SYMBOL(print)
0002:0010     LPAREN
0002:0012        INT(3)
0002:0014     RPAREN
0002:0015    NEWLINE
0002:0015     DEDENT
0002:0015    NEWLINE
0003:0001    NEWLINE
""",
        r"""
["if":if]
    [SYMBOL:True]
    [COLON::]
        [LPAREN:]
            [SYMBOL:print]
            [INT:3]
[EOF:]
""",
        r"""
PepIf(
    PepSymbol('True'),
    (
        PepFunctionCall(
            PepSymbol('print'),
            (
                PepInt('3'),
            )
        ),
    ),
    None
)
""" )
示例#26
0
def test_define_function():
    assert_parser_result(
        r"""
0001:0001      "def"(def)
0001:0005     SYMBOL(int)
0001:0009     SYMBOL(myfn)
0001:0013     LPAREN
0001:0014     RPAREN
0001:0015      COLON(:)
0001:0016    NEWLINE
0002:0001     INDENT
0002:0005   "return"(return)
0002:0012        INT(1)
0002:0013    NEWLINE
0002:0013     DEDENT
0002:0013    NEWLINE
0003:0001    NEWLINE
""",
        r"""
["def":def]
    [SYMBOL:int]
    [SYMBOL:myfn]
    [LPAREN:]
    [COLON::]
        ["return":return]
            [INT:1]
[EOF:]
""",
        r"""
PepDef(
    PepSymbol('int'),
    PepSymbol('myfn'),
    (),
    (
        PepReturn(
            PepInt('1')
        ),
    )
)
""" )
示例#27
0
def test_if_function_call():
    assert_parser_result(
        r"""
0001:0001       "if"(if)
0001:0004     SYMBOL(f)
0001:0005     LPAREN
0001:0007        INT(3)
0001:0009     RPAREN
0001:0010      COLON(:)
0001:0011    NEWLINE
0002:0001     INDENT
0002:0005        INT(3)
0002:0006    NEWLINE
0002:0006     DEDENT
0002:0006    NEWLINE
0003:0001    NEWLINE
""",
        r"""
["if":if]
    [LPAREN:]
        [SYMBOL:f]
        [INT:3]
    [COLON::]
        [INT:3]
[EOF:]
""",
        r"""
PepIf(
    PepFunctionCall(
        PepSymbol('f'),
        (
            PepInt('3'),
        )
    ),
    (
        PepInt('3'),
    ),
    None
)
""" )
示例#28
0
def test_static_member_variable():
    assert_parser_result(
        r"""
0001:0001    "class"(class)
0001:0007     SYMBOL(MyClass)
0001:0014      COLON(:)
0001:0015    NEWLINE
0002:0001     INDENT
0002:0005     SYMBOL(int)
0002:0009     SYMBOL(x)
0002:0011     EQUALS(=)
0002:0013        INT(3)
0002:0014    NEWLINE
0002:0014     DEDENT
0002:0014    NEWLINE
""",
        r"""
["class":class]
    [SYMBOL:MyClass]
    [COLON::]
        [EQUALS:=]
            [SYMBOL:int]
            [SYMBOL:x]
            [INT:3]
[EOF:]
""",
        r"""
PepClass(
    PepSymbol('MyClass'),
    (),
    (
        PepInit(
            PepSymbol('int'),
            PepSymbol('x'),
            PepInt('3')
        ),
    )
)
""" )
示例#29
0
def test_float_initialisation():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(float)
0001:0007     SYMBOL(f)
0001:0009     EQUALS(=)
0001:0011      FLOAT(7.4)
0001:0014    NEWLINE
""",
        r"""
[EQUALS:=]
    [SYMBOL:float]
    [SYMBOL:f]
    [FLOAT:7.4]
[EOF:]
""",
        r"""
PepInit(
    PepSymbol('float'),
    PepSymbol('f'),
    PepFloat('7.4')
)
""" )
示例#30
0
def test_simple_initialisation():
    assert_parser_result(
        r"""
0001:0001     SYMBOL(int)
0001:0005     SYMBOL(i)
0001:0007     EQUALS(=)
0001:0009        INT(7)
0001:0010    NEWLINE
""",
        r"""
[EQUALS:=]
    [SYMBOL:int]
    [SYMBOL:i]
    [INT:7]
[EOF:]
""",
        r"""
PepInit(
    PepSymbol('int'),
    PepSymbol('i'),
    PepInt('7')
)
""" )