示例#1
0
    def test_continues_after_reserved_word(self):
        lut = Lexer('FUNCTION main')

        lut.get_next_token()
        result = lut.get_next_token()

        self.assertEqual(result.type, TOKEN_MAIN)
示例#2
0
    def test_continues_after_single_character_symbol(self):
        integer = 12345
        lut = Lexer('+ {integer}'.format(integer=integer))

        lut.get_next_token()
        result = lut.get_next_token()

        self.assertEqual(result.type, TOKEN_INTEGER_LITERAL)
        self.assertEqual(result.value, integer)
示例#3
0
    def test_continues_after_integer_literal(self):
        lut = Lexer('12345 54321')

        result = lut.get_next_token()
        self.assertEqual(result.type, TOKEN_INTEGER_LITERAL)
        self.assertEqual(result.value, 12345)

        result = lut.get_next_token()
        self.assertEqual(result.type, TOKEN_INTEGER_LITERAL)
        self.assertEqual(result.value, 54321)
示例#4
0
    def test_continues_after_inline_comment(self):
        comment = 'this is a string !@#$^woooo 1234'
        integer = 12345
        lut = Lexer("""//{comment}
        {integer}""".format(comment=comment, integer=integer))
        lut.get_next_token()  #comment
        self.assertEqual(lut.get_next_token().type, TOKEN_EOL)

        result = lut.get_next_token()
        self.assertEqual(result.type, TOKEN_INTEGER_LITERAL)
        self.assertEqual(result.value, integer)
示例#5
0
    def test_continues_after_string_literal(self):
        string = 'this is a string !@#$^woooo 1234'
        integer = 12345
        lut = Lexer('"{string}" {integer}'.format(string=string,
                                                  integer=integer))
        result = lut.get_next_token()
        self.assertEqual(result.type, TOKEN_STRING_LITERAL)
        self.assertEqual(result.value, string)

        result = lut.get_next_token()
        self.assertEqual(result.type, TOKEN_INTEGER_LITERAL)
        self.assertEqual(result.value, integer)
示例#6
0
    def test_continues_after_multi_line_comment(self):
        comment = """this is
                going to be a comment
                that spans many lines"""
        integer = 12345
        lut = Lexer('  /*{comment}*/  {integer}'.format(comment=comment,
                                                        integer=integer))

        lut.get_next_token()
        result = lut.get_next_token()

        self.assertEqual(result.type, TOKEN_INTEGER_LITERAL)
        self.assertEqual(result.value, integer)
def eat_tokens(line):
    lex = Lexer(line)
    line = ""
    while lex.get_next_token():
        line += lex.peek_token().test()
        if lex.peek_token().type in ["EOF", "ERROR"]:
            break
    return line
示例#8
0
 def test_handles_multi_line_comment(self):
     comment = """this is
     going to be a comment
     that spans many lines"""
     lut = Lexer('  /*{comment}*/  '.format(comment=comment))
     result = lut.get_next_token()
     self.assertEqual(result.type, TOKEN_COMMENT)
     self.assertEqual(result.value, comment)
示例#9
0
    def test_assigns_correct_line_number_to_token(self):
        lut = Lexer("""12345
        54321
        /*
        long
        comment
        */
        12345
        """)
        integer = lut.get_next_token()
        self.assertEqual(integer.line_number, 1)

        eol = lut.get_next_token()
        self.assertEqual(eol.line_number, 1)

        integer2 = lut.get_next_token()
        self.assertEqual(integer2.line_number, 2)

        eol = lut.get_next_token()
        self.assertEqual(eol.line_number, 2)

        comment = lut.get_next_token()
        self.assertEqual(comment.line_number, 6)

        eol = lut.get_next_token()
        self.assertEqual(eol.line_number, 6)

        integer2 = lut.get_next_token()
        self.assertEqual(integer2.line_number, 7)
示例#10
0
 def test_handles_whitespace(self):
     lut = Lexer('        ')
     result = lut.get_next_token()
     self.assertEqual(result.type, TOKEN_EOF)
示例#11
0
 def test_handles_valid_double_character_symbol(self):
     lut = Lexer('==')
     result = lut.get_next_token()
     self.assertEqual(result.type, TOKEN_EQ)
示例#12
0
 def test_handles_inline_comment(self):
     comment = 'comment'
     lut = Lexer('//{comment}'.format(comment=comment))
     result = lut.get_next_token()
     self.assertEqual(result.type, TOKEN_COMMENT)
     self.assertEqual(result.value, comment)
示例#13
0
 def test_handles_invalid_double_character_symbol(self):
     lut = Lexer('[]')
     self.assertEqual(lut.get_next_token().type, TOKEN_LBRACKET)
     self.assertEqual(lut.get_next_token().type, TOKEN_RBRACKET)
示例#14
0
 def test_handles_string_literal(self):
     string1 = 'this is a string !@#$^woooo 1234'
     lut = Lexer('"{string1}"'.format(string1=string1))
     result = lut.get_next_token()
     self.assertEqual(result.type, TOKEN_STRING_LITERAL)
     self.assertEqual(result.value, string1)
示例#15
0
 def test_handles_empty_string(self):
     lut = Lexer('')
     result = lut.get_next_token()
     self.assertEqual(result.type, TOKEN_EOF)
示例#16
0
 def test_handles_integer_literal(self):
     lut = Lexer('12345')
     result = lut.get_next_token()
     self.assertEqual(result.type, TOKEN_INTEGER_LITERAL)
     self.assertEqual(result.value, 12345)
示例#17
0
 def test_handles_identifier_abc_123(self):
     lut = Lexer('abc_123')
     result = lut.get_next_token()
     self.assertEqual(result.type, TOKEN_IDENTIFIER)
     self.assertEqual(result.value, 'abc_123')
def eat_tokens(line):
    lex = Lexer(line)
    line = ""
    while lex.get_next_token():
        line += lex.peek_token().test()
    return line
示例#19
0
 def test_handles_reserved_word(self):
     lut = Lexer('BEGIN')
     result = lut.get_next_token()
     self.assertEqual(result.type, TOKEN_BEGINFUNCTION)
示例#20
0
 def test_collects_errors(self):
     lut = Lexer('&')
     lut.get_next_token()
     self.assertEqual(len(lut.errors()), 1)
示例#21
0
 def test_handles_single_character_symbol(self):
     lut = Lexer('+')
     result = lut.get_next_token()
     self.assertEqual(result.type, TOKEN_ADD)