Skip to content

Commit 1817c50

Browse files
committed
test: add property grammar example
1 parent 9aa01b6 commit 1817c50

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Diff for: samples/property/property_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package property
2+
3+
import (
4+
"testing"
5+
6+
"github.com/b4fun/parsimonious-go"
7+
)
8+
9+
const grammarText = `
10+
Item = "-" _ KeyValuePairs _
11+
12+
KeyValuePairs = 'item(' KeyValuePair ("," _ KeyValuePair)* ')'
13+
14+
KeyValuePair = Key _ "=" _ Value
15+
16+
Key = ~r"[a-zA-Z][a-zA-Z0-9_]*"
17+
18+
Value = String / Number / KeyValuePairs
19+
20+
String = StringLiteral / StringQuoted
21+
22+
StringLiteral = "string(" ~r'[^)]+' ")"
23+
StringQuoted = "string(" _ '"' ~r'[^"]*' '"' _ ")"
24+
25+
Number = "number(" _ ~r"[0-9]+(\.[0-9]+)?" _ ")"
26+
27+
_ = Whitespace*
28+
29+
Whitespace = " " / "\t" / EOL
30+
31+
EOL = "\n" / "\r\n" / "\r"
32+
`
33+
34+
func Test_Property(t *testing.T) {
35+
withDebug := parsimonious.ParseWithDebug(true)
36+
37+
grammar, err := parsimonious.NewGrammar(grammarText, withDebug)
38+
if err != nil {
39+
t.Errorf("parse grammar failed: %v", err)
40+
return
41+
}
42+
43+
program := `- item(name=string( Energy中文 ), subitem=item(value=number(997), unit=string("value")))`
44+
t.Logf("%q\n", program)
45+
46+
tree, err := grammar.Parse(program, withDebug)
47+
if err != nil {
48+
t.Errorf("parse sample failed: %v", err)
49+
return
50+
}
51+
t.Log("\n" + parsimonious.DumpNodeExprTree(tree))
52+
}

0 commit comments

Comments
 (0)