Skip to content

Commit 7231fa6

Browse files
refactor(cst): Rename TreeGen to CstGen
1 parent 2baa4c2 commit 7231fa6

File tree

9 files changed

+24
-27
lines changed

9 files changed

+24
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
- Importing from outside this project: no (we don't know what extra stuff other projects put in their module namespace that might break our code).
4242
- Importing internally within the project:
4343
- Useful when you need *everything* from a module that has many *similar* classes
44-
- (e.g. importing all the nodes (`parser/cst/nodes.py`) in the CST generation code (`parser/cst/treegen.py`) is good).
44+
- (e.g. importing all the nodes (`parser/cst/nodes.py`) in the CST generation code (`parser/cst/cstgen.py`) is good).
4545
- The threshold for 'many' here is around 10-15 classes.
4646
- Otherwise (if the classes/functions are unrelated (e.g. in `utils.py`), or you don't need all/most of them, or the module has few classes), just list them out.
4747
- You must not `import *` from a module without a `__all__` (to avoid polluting your globals with their internal stuff, like the stuff they import from elsewhere). The one exception to this is re-exporting stuff (e.g. in `__init__.py`).

benchmark.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from parser.astgen.astgen import AstGen
77
from parser.common.tree_print import tformat
88
from parser.cst.nodes import ProgramNode
9-
from parser.cst.treegen import TreeGen
9+
from parser.cst.cstgen import CstGen
1010
from parser.lexer import Tokenizer, format_tokens
1111
from util import readfile
1212

@@ -29,7 +29,7 @@ def get(self):
2929

3030
class BenchOnce:
3131
_tokenizer: Tokenizer
32-
_cstgen: TreeGen
32+
_cstgen: CstGen
3333
_cst: ProgramNode
3434
_ast: AstProgramNode
3535

@@ -79,7 +79,7 @@ def do_token_fmt(self):
7979

8080
def do_cst(self):
8181
with _Timer() as t:
82-
self._cstgen = TreeGen(self._tokenizer)
82+
self._cstgen = CstGen(self._tokenizer)
8383
self._cst = self._cstgen.parse()
8484
self._add_line(1.0, 'CST', t.get())
8585

fuzz.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from parser.astgen.astgen import AstGen
44
from parser.lexer.tokenizer import Tokenizer
5-
from parser.cst.treegen import TreeGen
5+
from parser.cst.cstgen import CstGen
66
from parser.common.error import BaseParseError
77

88
from pythonfuzz.fuzzer import Fuzzer
@@ -30,7 +30,7 @@ def fuzz(buf):
3030
try:
3131
string = buf.decode("ascii")
3232
try:
33-
AstGen(TreeGen(Tokenizer(string))).parse()
33+
AstGen(CstGen(Tokenizer(string))).parse()
3434
except BaseParseError:
3535
pass
3636
except UnicodeDecodeError:

parser/astgen/astgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ..cst.base_node import AnyNode, Node, Leaf
1313
from ..cst.named_node import NamedLeafCls, NamedNodeCls, NamedSizedNodeCls
1414
from ..cst.nodes import *
15-
from ..cst.treegen import CstGen
15+
from ..cst.cstgen import CstGen
1616

1717
# Final syntax lowering/codegen: (???)
1818
# AST -> blocks, intrinsics, functions -> blocks & intrinsics -> intrinsics
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class LocatedCstError(BaseLocatedError, CstParseError):
2727
pass
2828

2929

30-
class TreeGen:
30+
class CstGen:
3131
def __init__(self, tokenizer: Tokenizer):
3232
self.tokenizer = tokenizer
3333
self.src = self.tokenizer.src
@@ -542,9 +542,6 @@ def node_from_children(cls, name_or_type: str | type[AnyNamedNode],
542542
return klass(region, parent, children)
543543

544544

545-
CstGen = TreeGen
546-
547-
548545
# operator precedence (most to least binding):
549546
# 1. () [] {} (parens)
550547
# 2. string literal auto-concatenation ("ab" "xy" => "abxy")

test/common/common.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from parser.common.error import BaseParseError
1212
from parser.common.tree_print import tformat
1313
from parser.cst.base_node import Leaf, AnyNode, Node
14-
from parser.cst.treegen import TreeGen, CstParseError
14+
from parser.cst.cstgen import CstGen, CstParseError
1515
from parser.lexer import Tokenizer
1616
from parser.lexer.tokens import Token, OpToken
1717
from test.common.snapshottest import SnapshotTestCase
@@ -83,44 +83,44 @@ def assertTokensEqual(
8383
self.assertTokenStreamEquals(t.tokens, expected, check_regions)
8484

8585
def assertValidParseCST(self, src: str):
86-
self.assertIsNotNone(TreeGen(Tokenizer(src)).parse())
86+
self.assertIsNotNone(CstGen(Tokenizer(src)).parse())
8787

8888
def assertFailsGracefullyCST(self, src: str):
89-
t = TreeGen(Tokenizer(src))
89+
t = CstGen(Tokenizer(src))
9090
with self.assertRaises(CstParseError) as ctx:
9191
t.parse()
9292
return ctx.exception
9393

9494
def assertNotInternalErrorCST(self, src: str):
9595
try:
96-
TreeGen(Tokenizer(src)).parse()
96+
CstGen(Tokenizer(src)).parse()
9797
except BaseParseError:
9898
self.assertTrue(True)
9999
self.assertTrue(True)
100100

101101
@classmethod
102102
def raiseInternalErrorsOnlyCST(cls, src: str):
103103
try:
104-
TreeGen(Tokenizer(src)).parse()
104+
CstGen(Tokenizer(src)).parse()
105105
except BaseParseError:
106106
return None
107107
except Exception:
108108
raise
109109
return None
110110

111111
def assertCstMatchesSnapshot(self, src: str):
112-
t = TreeGen(Tokenizer(src))
112+
t = CstGen(Tokenizer(src))
113113
self.assertMatchesSnapshot(t.parse())
114114

115115
def assertAstMatchesSnapshot(self, src: str):
116-
t = AstGen(TreeGen(Tokenizer(src)))
116+
t = AstGen(CstGen(Tokenizer(src)))
117117
self.assertMatchesSnapshot(t.parse())
118118

119119
def assertValidParseAST(self, src: str):
120-
self.assertIsNotNone(AstGen(TreeGen(Tokenizer(src))).parse())
120+
self.assertIsNotNone(AstGen(CstGen(Tokenizer(src))).parse())
121121

122122
def assertFailsGracefullyAST(self, src: str):
123-
a = AstGen(TreeGen(Tokenizer(src)))
123+
a = AstGen(CstGen(Tokenizer(src)))
124124
with self.assertRaises(LocatedAstError) as ctx:
125125
a.parse()
126126
return ctx.exception

test/test_cstgen/test_treegen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22

33
from parser.lexer.tokenizer import Tokenizer
4-
from parser.cst.treegen import TreeGen, LocatedCstError, CstGen
4+
from parser.cst.cstgen import CstGen, LocatedCstError
55
from parser.common import StrRegion
66
from test.common import CommonTestCase
77

@@ -24,7 +24,7 @@ def test_fn_call_in_lvalue(self):
2424

2525
def test_empty_sqb_error(self):
2626
with self.assertRaises(LocatedCstError) as err:
27-
TreeGen(Tokenizer('v=a[]+b')).parse()
27+
CstGen(Tokenizer('v=a[]+b')).parse()
2828
exc = err.exception
2929
self.assertBetweenIncl(3, 4, exc.region.start)
3030
self.assertEqual(4, exc.region.end - 1)
@@ -121,7 +121,7 @@ def test_else_if_else(self):
121121

122122
def test_else_cond_null(self):
123123
src = 'if 0==1{exit(); } startup();'
124-
n = TreeGen(Tokenizer(src)).parse()
124+
n = CstGen(Tokenizer(src)).parse()
125125
node = n.children[0].children[-1]
126126
self.assertLessEqual(node.region.start, node.region.end)
127127
self.assertEqual(StrRegion(17, 18), node.region)

test/test_e2e/test_from_fuzzer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import unittest
33
from pathlib import Path
44

5-
from parser.cst.treegen import TreeGen
5+
from parser.cst.cstgen import CstGen
66
from parser.lexer.tokenizer import Tokenizer
77
from test.common import CommonTestCase, TestCaseUtils
88
from util import timeout_decor, timeout_decor_async
@@ -60,7 +60,7 @@ def test_a32460d584fd8a20d1e62007db570eaf41342f248e42c21a33780fd976e45290(self):
6060
@staticmethod
6161
@timeout_decor(5, debug=0)
6262
def inner_ed988ae940f54542ec54fd3c402a009fe2fdb660bd558d76a3612781a5ef6aa2():
63-
TreeGen(Tokenizer('a;//')).parse()
63+
CstGen(Tokenizer('a;//')).parse()
6464
return CommonTestCase.raiseInternalErrorsOnlyCST('a<//')
6565

6666
def test_ed988ae940f54542ec54fd3c402a009fe2fdb660bd558d76a3612781a5ef6aa2(self):

test/test_e2e/test_main_examples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22

33
from parser.astgen.astgen import AstGen
4-
from parser.cst.treegen import TreeGen
4+
from parser.cst.cstgen import CstGen
55
from parser.lexer import Tokenizer
66
from test.common import CommonTestCase
77
from util import readfile
@@ -16,7 +16,7 @@ def _test_main_example_n(self, n: int, do_ast: bool = True):
1616
src = readfile(f'./main_example_{n}.st')
1717
tk = Tokenizer(src).tokenize()
1818
self.assertMatchesSnapshot(tk.tokens, 'tokens')
19-
t = TreeGen(tk)
19+
t = CstGen(tk)
2020
self.assertMatchesSnapshot(t.parse(), 'cst')
2121
if do_ast:
2222
self.assertMatchesSnapshot(AstGen(t).parse(), 'ast')

0 commit comments

Comments
 (0)