src/src.bend
bolt@1.12.0.0 · 20 lines · 0 definitions
1# src/src: one file, read once and parsed once. A rule is handed this, not
2# the text: a tree parse of a 156 KB file costs 1.5 s here (the lexer alone
3# 54 ms, and `Bind.bound` parses again), so twenty rules that each parsed
4# the text made the linter twenty times slower than it needs to be.
5import Base
6import bolt@1.12.0.0/src/syntax/lex.bend as Lex
7import bolt@1.12.0.0/src/syntax/tree.bend as Tree
8import bolt@1.12.0.0/src/syntax/bind.bend as Bind
9import bolt@1.12.0.0/src/syntax/outline.bend as Outline
10
11# the path, the text, and the three readings of it every rule shares
12type Src is Data:
13 Src{path: String, text: String, toks: List<&2, Lex.Tok>, tree: Tree.Node, bound: Bind.Bound,
14 items: List<&2, Outline.Item>}
15
16# read a file's text once
17def of(+path: String, +text: String) -> Src:
18 +toks = Lex.tokens(text)
19 +tree = Tree.of_tokens(toks)
20 Src{path, text, toks, tree, Bind.of_tree(tree, String.lines(text)), Outline.of_tokens(text, toks)}