Skip to content

Commit

Permalink
go/import: Labels must be serched as well for recovery rules
Browse files Browse the repository at this point in the history
The dependency finder of a rule was alware of Identifiers used within
any subexpressions.  But we also need to check for labels which might
have recovery rules defined with the same name and add them as
dependencies as well.  e.g.:

  // File leaf.peg
  @import Val from "./dependecy.peg"
  Main <- Val ("," Val)*

  // File dependency.peg
  Val <- Hex / Dec / Str / Bool
  Hex <- '0x' [a-fA-F0-9]+^LabelHex
  LabelHex <- (!Val .)*

The importer wasn't pulling "LabelHex" into the file "leaf.peg"
without this change.
  • Loading branch information
clarete committed May 5, 2024
1 parent 9e63604 commit 71c702a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions go/grammar_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ func (f *importerResolverFrame) doFindDefinitionDeps(node Node, deps *sortedDeps
case *LexNode:
f.doFindDefinitionDeps(n.Expr, deps)
case *LabeledNode:
// save definition as a dependency and recurse into it
if def, ok := f.Grammar.DefsByName[n.Label]; ok {
deps.nodes[n.Label] = def
deps.names = append(deps.names, n.Label)
f.doFindDefinitionDeps(def.Expr, deps)
}
f.doFindDefinitionDeps(n.Expr, deps)
}
}
6 changes: 5 additions & 1 deletion go/tests/import_gr_value.peg
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ Boolean <- 'true' / 'false'

// overrides what's defined within Number
Binary <- '0b' [0-1]+
Hexadecimal <- '0x' [a-fA-F0-9]+
Hexadecimal <- '0x' [a-fA-F0-9]+^LabelHex

// recovery expression for failure on the last subexpression of the
// Hexadecimal rule
LabelHex <- (!Value .)*
4 changes: 4 additions & 0 deletions go/tests/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func TestImport(t *testing.T) {
Error string
}{
{
Name: " accept overriden",
Input: "0xLOL",
Error: "LabelHex @ 2",
}, {
Name: "Dont accept overriden",
Input: "3+#xC0FFEE",
Error: "term_right_operand @ 2",
Expand Down

0 comments on commit 71c702a

Please sign in to comment.