Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible @ refs #88

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions examples/wbnf.wbnf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ named -> (IDENT op="=")? atom;
quant -> op=[?*+]
| "{" min=INT? "," max=INT? "}"
| op=/{<:|:>?} opt_leading=","? named opt_trailing=","?;
atom -> IDENT | STR | RE | macrocall | ExtRef=("%%" IDENT) | REF | "(" term ")" | "(" ")";
atom -> at="@" offset=/{-?\d+}?
| IDENT ("." offset=\d+)?
| STR
| RE
| macrocall
| ExtRef=("%%" IDENT)
| REF
| "(" term ")"
| "(" ")";

macrocall -> "%!" name=IDENT "(" term:","? ")";
REF -> "%" IDENT ("=" default=STR)?;
Expand All @@ -19,7 +27,7 @@ REF -> "%" IDENT ("=" default=STR)?;
COMMENT -> /{ //.*$
| (?s: /\* (?: [^*] | \*+[^*/] ) \*/ )
};
IDENT -> /{@|\.?[A-Za-z_]\w*};
IDENT -> /{\.?[A-Za-z_]\w*};
INT -> \d+;
STR -> /{ " (?: \\. | [^\\"] )* "
| ' (?: \\. | [^\\'] )* '
Expand Down
7 changes: 7 additions & 0 deletions scripts/check-coverage.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -e

MIN_COVERAGE="$1"

if [ -z "$MIN_COVERAGE" ]; then
Expand All @@ -16,7 +18,12 @@ coverage_level() {
awk '//{sub(/(\.[0-9]+)?%$/,"",$3);print$3}'
}

rm_coverage_file() {
rm $COVERAGE_FILE
}

go test -coverprofile=$COVERAGE_FILE -covermode=atomic ./...
trap rm_coverage_file exit

COVERAGE_LEVEL="$(coverage_level)"
if [ "$COVERAGE_LEVEL" -lt "$MIN_COVERAGE" ]; then
Expand Down
4 changes: 3 additions & 1 deletion wbnf/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (gb grammarBuilder) expandMacro(node MacrocallNode) parser.Term {
}

func (gb grammarBuilder) buildAtom(atom AtomNode) parser.Term {
x, _ := ast.Which(atom.Node.(ast.Branch), "RE", "STR", "macrocall", "ExtRef", "IDENT", "REF", "term")
x, _ := ast.Which(atom.Node.(ast.Branch), "RE", "STR", "macrocall", "ExtRef", "at", "IDENT", "REF", "term")
name := ""
switch x {
case "term", "REF", "ExtRef", "macrocall", "":
Expand All @@ -125,6 +125,8 @@ func (gb grammarBuilder) buildAtom(atom AtomNode) parser.Term {
}

switch x {
case "at":
return parser.Rule(name)
case "IDENT":
return parser.Rule(name)
case "STR":
Expand Down
8 changes: 4 additions & 4 deletions wbnf/grammar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestParseNamedTerm(t *testing.T) {
stack(`_`).z(stack(`term@1`, parser.NonAssociative).a(`term@2`).a(`term@3`).z(
stack(`named`).z(
stack(`?`).a(`_`).z(*r.Slice(0, 3), *r.Slice(3, 4)),
stack(`atom`, parser.Choice(1)).z(*r.Slice(4, 6)),
stack(`atom`, parser.Choice(2)).z(*r.Slice(4, 6)),
), stack(`?`).z(),
), stack(`?`).z(),
))
Expand All @@ -122,14 +122,14 @@ func TestParseNamedTermInDelim(t *testing.T) {
stack(`_`).z(stack(`term@1`, parser.NonAssociative).a(`term@2`).a(`term@3`).z(
stack(`named`).z(
stack(`?`).z(),
stack(`atom`, parser.Choice(1)).z(*r.Slice(0, 3)),
stack(`atom`, parser.Choice(2)).z(*r.Slice(0, 3)),
),
stack(`?`).a(`quant`, parser.Choice(2)).a(`_`).z(
*r.Slice(3, 4),
stack(`?`).z(),
stack(`named`).z(
stack(`?`).a(`_`).z(*r.Slice(4, 6), *r.Slice(6, 7)),
stack(`atom`, parser.Choice(1)).z(*r.Slice(7, 10)),
stack(`atom`, parser.Choice(2)).z(*r.Slice(7, 10)),
),
stack(`?`).z(),
),
Expand Down Expand Up @@ -202,7 +202,7 @@ func TestGrammarSnippet(t *testing.T) {
v, err := parsers.Parse("term", r)
require.NoError(t, err)
assert.Equal(t,
`term║:[_[term@1║:[term@2[term@3[named[?[], atom║0[prod]], ?[quant║0[+]]]]], ?[]]]`,
`term║:[_[term@1║:[term@2[term@3[named[?[], atom║1[_[prod, ?[]]]], ?[quant║0[+]]]]], ?[]]]`,
fmt.Sprintf("%v", v),
)
assertUnparse(t, "prod+", parsers, v)
Expand Down
39 changes: 34 additions & 5 deletions wbnf/wbnfgrammar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.