Skip to content

ATN construction#65

Draft
Lotes wants to merge 25 commits intomainfrom
lotes/atn-construction
Draft

ATN construction#65
Lotes wants to merge 25 commits intomainfrom
lotes/atn-construction

Conversation

@Lotes
Copy link
Copy Markdown
Collaborator

@Lotes Lotes commented Apr 29, 2026

There is a big Compile Time ATN for construction and a slim Runtime ATN.

I also added a Markdown generator to be able to debug the ATN visually.

Lotes and others added 4 commits April 29, 2026 14:16
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Co-authored-by: Copilot <copilot@github.com>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Comment thread internal/grammar/atn.md
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Comment thread cmd/fastbelt/main.go Outdated
Comment thread parser/allstar/grammar.go Outdated
Comment thread parser/allstar/atn.go Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Since we need all of this code in other parts of the parser (not just the lookahead), it might make sense to move it to the parser parent directory.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTM a lot of stuff is inside the generator package. Only the Runtime ATN is in the parser package.

Comment thread parser/atn_runtime.go Outdated
Comment thread parser/allstar/grammar.go Outdated
Comment thread parser/allstar/atn.go Outdated
Comment thread parser/allstar/grammar.go Outdated
Comment thread internal/generator/atn_md_emit.go Outdated
"typefox.dev/fastbelt/generator"
)

func EmitMarkdownSource(pkgName string, rtn *RuntimeATN, tokenTypeNames map[int]string) generator.Node {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: This part of the code should live in the internal part of the framework - adopters shouldn't be able to call this function IMO.

Comment thread internal/generator/atn_emit.go Outdated
// pkgName – Go package name for the generated file (e.g. "myparser")
// funcName – name of the generated constructor function
// importPath – import path of the allstar package (e.g. "typefox.dev/fastbelt/parser/allstar")
func EmitGoSource(pkgName, funcName, importPath string, rtn *RuntimeATN) generator.Node {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: This part of the code should live in the internal part of the framework - adopters shouldn't be able to call this function IMO.

Comment thread internal/generator/atn_emit.go Outdated
Lotes and others added 12 commits April 30, 2026 17:22
Co-authored-by: Copilot <copilot@github.com>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Co-authored-by: Copilot <copilot@github.com>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Comment thread parser/atn_runtime.go Outdated
Comment thread internal/grammar/atn_gen.go
Comment thread internal/generator/atn_types.go Outdated
Lotes added 3 commits May 5, 2026 17:54
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Comment thread internal/grammar/atn_gen.go Outdated
Action_Alternatives_1 ATNDecisionId = 0
Action_Group_2 ATNDecisionId = 1
Alternatives_Group_2 ATNDecisionId = 2
Alternatives_Group_3 ATNDecisionId = 3
Copy link
Copy Markdown
Collaborator Author

@Lotes Lotes May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The counter inside the name looks broken...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The counter refers to the index of all parts in a rule. Only a few are used for the decision map.

Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>

func (rb *ATNRuleBuilderImpl) MakeBlock(alts []*ATNHandle) *ATNHandle {
for i := 0; i < len(alts)-1; i++ {
handle := alts[i]
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msujew Not very sure about the original implementation...
May need to redo it...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the code in the original implementation is supposed to simplify stuff like this:

image

In this case, all states 254-261 are not required for properly evaluating the ATN. We can go straight from 262 to 263 using the rule transitions.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MakeBlock is about concatenation, not alternatives.
Is this so much faster? I would prefer simple code.

Modelling middleware for the ATN could be a thing, that could be designed to optimize the ATN.

Copy link
Copy Markdown
Member

@msujew msujew May 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, and the idea is to skip 254-255, 256-257, etc. since they don't serve any value in the ATN. In the end, this makes the subset construction algorithm - and all other algorithm that need to traverse the ATN - slower. I would assume by quite a significant margin actually, with more gains the deeper the grammar becomes.

IMO, we should do everything, even if complicated, to improve the performance of the runtime-code in favor of more complexity in the compile-time code.
And to be honest, the simplification code isn't that difficult, though feel free to add further comments here.

Modelling middleware for the ATN could be a thing, that could be designed to optimize the ATN.

Sounds more complex than just adding the optimization in there actually ;)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. The middleware thingy sounds more complicated, when looking only at this single optimization. When we would add more of these optimizations the code becomes more unreadable. The middleware or middleend was an idea to introduce a optimization module.
But I remember that there is also a minimization strategy for DFAs. Maybe we can do something similar here?
Middleware or a big optimization step afterwards, so or so, I like this code here to be in a simple shape. But I could reintroduce the code with a explanation and a TODO, that we can refactor in a future PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be a bit hesitant to change the structure of the ATN too much. After all, we need it later to perform code completion and error recovery.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then let‘s postpone the refactoring until we have the necessary features with tests.

Start: Property+=Rule*;
Rule: Name=ID;
`))
RequireATNRecognizes(t, atn, rules, tokenTypes, "Start", []string{}, 1)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add tests with 2 or more paths…

Copy link
Copy Markdown
Member

@msujew msujew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking really promising already, just a few comments, see below 👍

Comment thread internal/generator/atn.go Outdated
Comment thread internal/generator/atn.go Outdated
Comment thread internal/grammar/atn_gen.go Outdated
Comment thread parser/atn_runtime.go Outdated
Comment thread internal/generator/atn_fixture.go
Comment thread internal/grammar/atn_gen.go
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Lotes added 3 commits May 8, 2026 10:46
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Signed-off-by: Markus Rudolph <markus.rudolph@typefox.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants