Skip to content

Support infix rules - #134

Open
msujew wants to merge 7 commits into
mainfrom
msujew/infix-operator
Open

Support infix rules#134
msujew wants to merge 7 commits into
mainfrom
msujew/infix-operator

Conversation

@msujew

@msujew msujew commented Jul 27, 2026

Copy link
Copy Markdown
Member

Closes #115 (as a side effect).

Adds infix rule support, similar to how Langium did it (but a bit simpler, since we don't need CST support).

Implements the support by rewriting infix rules into parser rules of the shape Expr (op Expr)*.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 6c2d06c Previous: d803417 Ratio
BenchmarkWorkspaceCycle (typefox.dev/fastbelt/examples/statemachine) - MB/s 22 MB/s 5.21 MB/s 4.22
BenchmarkParser (typefox.dev/fastbelt/examples/statemachine) - MB/s 90.81 MB/s 37.68 MB/s 2.41
BenchmarkLexer (typefox.dev/fastbelt/examples/statemachine) - MB/s 119.62 MB/s 64.67 MB/s 1.85
BenchmarkLexerAndParser (typefox.dev/fastbelt/examples/statemachine) - MB/s 50.43 MB/s 23.82 MB/s 2.12
BenchmarkLocalLinking (typefox.dev/fastbelt/examples/statemachine) - MB/s 25.57 MB/s 16.69 MB/s 1.53

This comment was automatically generated by workflow using github-action-benchmark.

@ssmifi ssmifi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks well-designed and -tested. There are one design and some minor questions left.

Comment thread internal/grammar/grammar.fb Outdated
Comment thread internal/grammar/validator.go
Comment thread internal/generator/parser_generator.go
Comment thread internal/generator/infix_generator.go
Comment thread parser/infix.go Outdated
Comment thread cmd/fastbelt/generate.go Outdated
@msujew
msujew force-pushed the msujew/infix-operator branch from fe3f042 to b6735dc Compare August 10, 2026 16:31
@msujew
msujew force-pushed the msujew/infix-operator branch from 6c2d06c to efe4301 Compare August 12, 2026 08:46
Comment thread examples/arithmetics/arithmetics.fb Outdated
Comment thread examples/arithmetics/infix_test.go
Comment thread grammar/doc.go Outdated
Comment thread grammar/doc.go
Comment thread internal/grammar/infix.go
Comment thread internal/grammar/infix.go
Comment thread parser/infix_test.go
Comment thread internal/grammar/infix_validator_test.go

@sailingKieler sailingKieler left a comment

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.

Thanks @msujew for this additional feature.

I've just minor remarks, mainly doc and a few nitpicks, see below.
Otherwise it's good to go.

For curiosity: Does the infix operator have measurable performance benefits?

Comment thread cmd/fastbelt/generate.go

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.

Why the renaming here? Will cause merge conflict with changes of #130.

` + commonTokens)
diag := doc.ExpectDiagnostic("1")
diag.WithSeverity(core.SeverityError)
diag.WithCode(ValidateInfixReturnType)

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.

is diag used anywhere? otherwise, please make consistent with other tests.

Comment thread grammar/doc.go
// | Expression "*" Expression
// | PrimaryExpression
//
// In this example, the "*" operator binds tighter than "+", so "a + b * c" parses

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.

You meant this, right?

Suggested change
// In this example, the "*" operator binds tighter than "+", so "a + b * c" parses
// In this example, the "*" operator is supposed to bind tighter than "+", so "a + b * c" parses

Comment thread grammar/doc.go
//
// Expression: Addition
// Addition: Multiplication ("+" Multiplication)*
// Multiplication: PrimaryExpression ("*" PrimaryExpression)*

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.

Please add a dummy definition of PrimaryExpression for completeness.

Comment thread grammar/doc.go
// > "+" | "-"
//
// The rule name must resolve to an interface declaring the fields Left,
// Right (of an interface type both the rule's return type and node type are

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.

What is node referring to?
Did you mean this? Then reorder the subsequent sentences, please.

Suggested change
// Right (of an interface type both the rule's return type and node type are
// Right (of an interface type both the rule's return type and operand rule's type are

Comment thread grammar/doc.go
// > "+" | "-"
//
// The return type of an infix rule defaults to the operand rule's return
// type; "returns" overrides it. A lone operand is returned unchanged - no

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.

Suggested change
// type; "returns" overrides it. A lone operand is returned unchanged - no
// type; an additional "returns" clause overrides it. A lone operand is returned unchanged - no

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.

Arithmetics example: Exponentiation not right-associative yet

4 participants