You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In other words, it's somehow getting stuck on parsing something as a VersionRange, and not backing out and parsing it simply as a VersionSingle.
But this only happens if both Version and VersionSingle have at least two ways to be interpreted. If I replace the ProjectVersion with a single regexp that matches the same string, it works (here replacing the Version lexer token with a ProjectVersion token with the appropriate regexp).
// With the range, without the "project version"typeVersionSingleNoProjectVersionstruct {
TipVersion*string`parser:"@TipVersion"`ProjectVersion*string`parser:"| @ProjectVersion"`
}
typeVersionRangeNoProjectVersionstruct {
FromVersionSingleNoProjectVersion`parser:"@@"`ToVersionSingleNoProjectVersion`parser:"VersionDash @@"`
}
typeVersionNoProjectVersionstruct {
Range*VersionRangeNoProjectVersion`parser:"@@"`Single*VersionSingleNoProjectVersion`parser:"| @@"`
}
The problem for single items goes away if I get rid of the RangeVersion; but then of course you can't parse ranges:
// With "project version", without rangetypeVersionNoRangestruct {
Single*VersionSingle`parser:"@@"`
}
Any idea what's going on?
For completeness, here's a complete testing function you can use to trigger the issue:
package participletest_test
import (
"testing""github.com/alecthomas/participle/v2""github.com/alecthomas/participle/v2/lexer"
)
// What I'd like:typeProjectVersionstruct {
Projectstring`parser:"@Project"`Versionstring`parser:"Space @(VersionString | RCVersionString)"`
}
typeVersionSinglestruct {
TipVersion*string`parser:"@TipVersion"`ProjectVersion*ProjectVersion`parser:"| @@"`
}
typeVersionRangestruct {
FromVersionSingle`parser:"@@"`ToVersionSingle`parser:"VersionDash @@"`
}
typeVersionstruct {
Range*VersionRange`parser:"@@"`Single*VersionSingle`parser:"| @@"`
}
// With "project version", without rangetypeVersionNoRangestruct {
Single*VersionSingle`parser:"@@"`
}
// With the range, without the "project version"typeVersionSingleNoProjectVersionstruct {
TipVersion*string`parser:"@TipVersion"`ProjectVersion*string`parser:"| @ProjectVersion"`
}
typeVersionRangeNoProjectVersionstruct {
FromVersionSingleNoProjectVersion`parser:"@@"`ToVersionSingleNoProjectVersion`parser:"VersionDash @@"`
}
typeVersionNoProjectVersionstruct {
Range*VersionRangeNoProjectVersion`parser:"@@"`Single*VersionSingleNoProjectVersion`parser:"| @@"`
}
varrulesCommon= []lexer.SimpleRule{
{"TipVersion", `xen-unstable`},
{"RCVersionString", `\d+\.\d+-RC series`},
{"VersionString", `\d+\.\d+\.x`},
{"VersionDash", " - "},
{"Space", ` `},
}
funcTestVersion(t*testing.T) {
simpletests:= []string{
"Xen 4.18.x",
"Linux 4.7.x",
"QEMU 4.7-RC series",
"xen-unstable",
}
rangetests:= []string{
"Linux 4.7.x - Linux 4.9.x",
"Xen 4.18.x - xen-unstable",
"QEMU 4.7-RC series - QEMU 4.10.x",
}
lexProject:=lexer.MustSimple(append(rulesCommon, lexer.SimpleRule{"Project", `Xen|Linux|QEMU|xapi`}))
lexProjectVersion:=lexer.MustSimple(append(rulesCommon,
lexer.SimpleRule{"ProjectVersion", `(Xen|Linux|QEMU|xapi) (\d+\.\d+\.x|\d+\.\d+-RC series)`}))
pVersion:= participle.MustBuild[Version](participle.Lexer(lexProject))
t.Log("Testing pVersion with simple and range")
for_, in:=rangeappend(simpletests, rangetests...) {
out, err:=pVersion.ParseString("", in)
iferr!=nil {
t.Errorf("ERROR: Parsing %v: %v", in, err)
} else {
t.Logf("Parsing %v resulted in %v", in, out)
}
}
pVersionNoRange:= participle.MustBuild[VersionNoRange](participle.Lexer(lexProject))
t.Log("Testing pVersionNoRange with simple only")
for_, in:=rangesimpletests {
out, err:=pVersionNoRange.ParseString("", in)
iferr!=nil {
t.Errorf("ERROR: Parsing %v: %v", in, err)
} else {
t.Logf("Parsing %v resulted in %v", in, out)
}
}
pVersionNoProjcetVersion:= participle.MustBuild[VersionNoProjectVersion](participle.Lexer(lexProjectVersion))
t.Log("Testing pVersionNoProjectVersion with simple and range")
for_, in:=rangeappend(simpletests, rangetests...) {
out, err:=pVersionNoProjcetVersion.ParseString("", in)
iferr!=nil {
t.Errorf("ERROR: Parsing %v: %v", in, err)
} else {
t.Logf("Parsing %v resulted in %v", in, out)
}
}
}
The text was updated successfully, but these errors were encountered:
First, thank you so much for this library -- made my first grammar on Friday and I think the setup really helped make things straightforward.
I'm using participle v2.10.0, and trying to write a parser for strings like the following:
i.e., a version that may be a single version, or a range (separated by
-
); that may be a single string, or<project> <version number>
.I wrote the following
participle
structures:Unfortunately, I get errors like the following:
In other words, it's somehow getting stuck on parsing something as a VersionRange, and not backing out and parsing it simply as a VersionSingle.
But this only happens if both Version and VersionSingle have at least two ways to be interpreted. If I replace the ProjectVersion with a single regexp that matches the same string, it works (here replacing the
Version
lexer token with aProjectVersion
token with the appropriate regexp).The problem for single items goes away if I get rid of the RangeVersion; but then of course you can't parse ranges:
Any idea what's going on?
For completeness, here's a complete testing function you can use to trigger the issue:
The text was updated successfully, but these errors were encountered: