feat: add Alias option for reusable named grammar fragments - #474
Open
Pastalikek65 wants to merge 1 commit into
Open
feat: add Alias option for reusable named grammar fragments#474Pastalikek65 wants to merge 1 commit into
Pastalikek65 wants to merge 1 commit into
Conversation
Alias(name, grammar) registers a named, reusable grammar fragment referenced from struct tags as <name>. Aliases are non-capturing: they match literals and named tokens (and compose via normal tag operators such as ? * + and grouping) but cannot bind values, since captures (@ and @@) always belong to a struct field of the surrounding production — such use is rejected at build time with a clear error. - options.go: Alias option appends aliasDef. - grammar.go: <name> syntax in parseTermNoModifiers via parseAlias, generatorContext.aliases map, addAliasDefs with extraction guard. - struct.go: lexStructFromTag parses a bare grammar string. - alias_test.go: 10 tests (pattern, reuse across fields, multiple aliases, modifiers, capture/struct-capture rejection, unknown ref, duplicate, empty name). Closes alecthomas#22.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #22
Adds
participle.Alias(name, grammar)— named, reusable grammar fragments referenced from struct tags as<name>:Design
? * + !, groups, disjunction), but cannot bind values. Captures (@and@@) always target a struct field of the surrounding production — an alias has no field, so@/@@in an alias body is rejected at build time with a clear message (aliases cannot capture (@); move captures to a struct field). This avoids the index-out-of-range panic that naive attempts hit during development.<name>inparseTermNoModifiers(grammar.go), resolved fromgeneratorContext.aliases.<was otherwise unused in the tag grammar, so no ambiguity with named token references.lexStructFromTag(struct.go): a bare grammar string is lexed without a backing struct type.Tests
10 tests: literal patterns, alias reuse across fields, multiple aliases,
?modifier,@/@@rejection, unknown reference, duplicate, empty name.One open question for maintainer: whether aliases should be capturing by binding to an implicit "value" the referenced field receives (e.g. a special syntax
<name>meaning the fragment's captures flow into the field). This would require threads of field context through alias resolution beyond build time; offered as a follow-up rather than blocking this PR.