-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
53 lines (40 loc) · 1.48 KB
/
test.py
File metadata and controls
53 lines (40 loc) · 1.48 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
#!/usr/bin/env python
# TODO:
# - Validate token tagging by comparing the lexer output on an
# example file against expected tokens.
try:
from pygments import highlight, lex
from pygments.formatters import HtmlFormatter
from pygments.token import Error as LexerError
except ModuleNotFoundError:
raise ImportError(
"Missing required modules. Please install the required dependencies using `pip install -r requirements.txt`!"
) from None
from gdscript_lexer import GDScriptLexer
from gdscript_style import GDScriptStyle
from typing import Optional
import os
EXAMPLE_FILES = "tests/examplefiles/"
def collect_lexer_errors(tokens) -> Optional[list[tuple[int, str]]]:
line_num = 1
errors = []
for type, value in tokens:
line_num += value.count('\n')
if type is LexerError:
errors.append((line_num, value))
return None if len(errors) == 0 else errors
for file_name in os.listdir("%s" % EXAMPLE_FILES):
if not file_name.endswith(".gd"):
continue
with open(EXAMPLE_FILES + file_name) as f:
code = f.read()
tokens = lex(code, GDScriptLexer())
errors = collect_lexer_errors(tokens)
if errors is not None:
print("There are", len(errors), "errors in the lexed code:")
for line, string in errors:
print("\tLINE", line, "AT", string)
raise ValueError("Lexing errors found")
formatter = HtmlFormatter(full=True, linenos=True, style=GDScriptStyle)
with open(EXAMPLE_FILES + file_name.rstrip(".gd") + ".html", "w") as f:
f.write(highlight(code, GDScriptLexer(), formatter))