def test_string(self):
     '''
     Тестирует строки
     '''
     print("\ntest_string")
     new_file('"str"."str"' + '\n' + '"str"a"str"' + '\n' + '"str"5"str"' +
              '\n' + '"str""st#r"#none"')
     lis = [
         [  # line 0
             [1, 1, "STRING", "str"],
             [1, 6, "DOT", None],
             [1, 7, "STRING", "str"],
         ],
         [  # line 1
             [2, 1, "STRING", "str"],
             [2, 6, "IDENTIFIER", "a"],
             [2, 7, "STRING", "str"],
         ],
         [  # line 2
             [3, 1, "STRING", "str"],
             [3, 6, "NUMBER", 5],
             [3, 7, "STRING", "str"],
         ],
         [  # line 3
             [4, 1, "STRING", "str"],
             [4, 6, "STRING", "st#r"],
         ]
     ]
     new_tokens(lis)
     found_tokens = find_tokens(F_NAME)
     self.assertTrue(are_equal(found_tokens, tokens))
     print("test_string passed")
def create_tree(text):
    '''
    Создает словарь с комнатами из входного текста
    '''
    file = open(F_NAME, "w")
    file.write(text)
    file.close()
    tokens = find_tokens(F_NAME)
    tree = build_tree(tokens)
    return tree
 def test_error_string(self):
     '''
     Тестирует на ошибку строки
     '''
     print("\ntest_error_string")
     new_file('not4"me')
     print("should be string error: line 1, char 7")
     found_tokens = find_tokens(F_NAME)
     self.assertEqual(found_tokens, None)
     print("test_error_string passed")
 def test_error_wrong_symbol(self):
     '''
     Тестирует на ошибку неправильного символа
     '''
     print("\ntest_error_wrong_symbol")
     new_file('not4 5year old')
     print("should be wrong symbol error: line 1, char 6")
     found_tokens = find_tokens(F_NAME)
     self.assertEqual(found_tokens, None)
     print("test_error_wrong_symbol passed")
Example #5
0
def create_tokens(text):
    '''
    Открывает и записывает текст в файл
    Скинирует файл и возвращает токены
    Принимает текст
    '''
    file = open(F_NAME, "w")
    file.write(text)
    file.close()
    return find_tokens(F_NAME)
 def test_comments_symbols(self):
     '''
     Тестирует комментарии и символы
     '''
     print("\ntest_comments_symbols")
     new_file(': spam:: .# . : spam spam spam')
     lis = [[  # line 0
         [1, 1, "COLON", None],
         [1, 3, "IDENTIFIER", "spam"],
         [1, 7, "COLON", None],
         [1, 8, "COLON", None],
         [1, 10, "DOT", None],
     ]]
     new_tokens(lis)
     found_tokens = find_tokens(F_NAME)
     self.assertTrue(are_equal(found_tokens, tokens))
     print("test_comments_symbols passed")
Example #7
0
def compile():
    '''
    Компилирует файл (собирает дерево и анализирует его, 
    используя модули scanner, parser, analyzer)
    В случае успешной компиляции возвращает собранное дерево
    В случае ошибки возвращает None
    '''
    print("compiling:")
    tokens = find_tokens()
    if tokens == None: 
        return None
    tree = build_tree(tokens)
    if tree == None:
        return None
    if analyse_all_errors(tree):
        return None
    print("compiled successfully")
    return tree
 def test_words_numbers(self):
     '''
     Тестирует слова и цифры
     '''
     print("\ntest_words_numbers")
     new_file('no no  r2d2' + '\n' + 'room   end ' + '\n' + 'height width' +
              '\n' + 'left right up down' + '\n' + 'img  1 22.333')
     lis = [
         [  # line 0
             [1, 1, "IDENTIFIER", "no"],
             [1, 4, "IDENTIFIER", "no"],
             [1, 8, "IDENTIFIER", "r2d2"],
         ],
         [  # line 1
             [2, 1, "ROOM", None],
             [2, 8, "END", None],
         ],
         [  # line 2
             [3, 1, "HEIGHT", None],
             [3, 8, "WIDTH", None],
         ],
         [  # line 3
             [4, 1, "LEFT", None],
             [4, 6, "RIGHT", None],
             [4, 12, "UP", None],
             [4, 15, "DOWN", None],
         ],
         [  # line 4
             [5, 1, "IMG", None],
             [5, 6, "NUMBER", 1],
             [5, 8, "NUMBER", 22],
             [5, 10, "DOT", None],
             [5, 11, "NUMBER", 333],
         ]
     ]
     new_tokens(lis)
     found_tokens = find_tokens(F_NAME)
     self.assertTrue(are_equal(found_tokens, tokens))
     print("test_words_numbers passed")
Example #9
0
            pass
        elif match_down(line):
            pass
        elif match_img(line):
            pass
        else:
            print_error("syntax error", line[0].line_num, line[0].char_num)
    if (not room_closed()) and (not error):
        print_error("room not closed with END",
                    tokens[len(tokens) - 1][0].line_num, 1)
        return None
    elif error:
        return None
    else:
        print("The tree was built")
        return tree


if __name__ == "__main__":
    print("run parser\n")
    from scanner import find_tokens
    from scanner import print_tokens
    from scanner import error as scaner_error
    tokens = find_tokens()
    #print_tokens()
    print()
    build_tree(tokens)
    print()
    tests()
else:
    print("added parser [script]")