-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_pylasu_antlr_parser.py
More file actions
53 lines (42 loc) · 1.96 KB
/
test_pylasu_antlr_parser.py
File metadata and controls
53 lines (42 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import unittest
from typing import List, Optional
from antlr4 import TokenStream, InputStream
from pylasu.model import Source, Node, Position, Point
from pylasu.parsing.antlr import PylasuANTLRParser
from pylasu.validation import Issue
from tests.simple_lang.SimpleLangLexer import SimpleLangLexer
from tests.simple_lang.SimpleLangParser import SimpleLangParser
class SimpleLangPylasuParser(PylasuANTLRParser):
def create_antlr_lexer(self, input_stream: InputStream):
return SimpleLangLexer(input_stream)
def create_antlr_parser(self, token_stream: TokenStream):
return SimpleLangParser(token_stream)
def parse_tree_to_ast(self, root, consider_range: bool, issues: List[Issue], source: Source) -> Optional[Node]:
return None
class KolasuParserTest(unittest.TestCase):
def test_lexing(self):
parser = SimpleLangPylasuParser()
result = parser.parse("""set a = 10
set b = ""
display c
""")
self.assertIsNotNone(result)
# TODO we don't have Pylasu Tokens yet
def test_issues_are_capitalized(self):
parser = SimpleLangPylasuParser()
result = parser.parse("""set set a = 10
display c
""")
self.assertTrue(result.issues)
self.assertTrue([i for i in result.issues if i.message.startswith("Extraneous input 'set'")])
self.assertTrue([i for i in result.issues if i.message.startswith("Mismatched input 'c'")])
def test_issues_have_not_flat_position(self):
parser = SimpleLangPylasuParser()
result = parser.parse("""set set a = 10
display c
""")
self.assertTrue(result.issues)
extraneous_input = [i for i in result.issues if i.message.startswith("Extraneous input 'set'")][0]
self.assertEqual(Position(Point(1, 4), Point(1, 7)), extraneous_input.position)
mismatched_input = [i for i in result.issues if i.message.startswith("Mismatched input 'c'")][0]
self.assertEqual(Position(Point(2, 8), Point(2, 9)), mismatched_input.position)