Skip to content

Commit

Permalink
test: add property grammar example
Browse files Browse the repository at this point in the history
  • Loading branch information
bcho committed Jan 24, 2024
1 parent 9aa01b6 commit 1817c50
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions samples/property/property_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package property

import (
"testing"

"github.com/b4fun/parsimonious-go"
)

const grammarText = `
Item = "-" _ KeyValuePairs _
KeyValuePairs = 'item(' KeyValuePair ("," _ KeyValuePair)* ')'
KeyValuePair = Key _ "=" _ Value
Key = ~r"[a-zA-Z][a-zA-Z0-9_]*"
Value = String / Number / KeyValuePairs
String = StringLiteral / StringQuoted
StringLiteral = "string(" ~r'[^)]+' ")"
StringQuoted = "string(" _ '"' ~r'[^"]*' '"' _ ")"
Number = "number(" _ ~r"[0-9]+(\.[0-9]+)?" _ ")"
_ = Whitespace*
Whitespace = " " / "\t" / EOL
EOL = "\n" / "\r\n" / "\r"
`

func Test_Property(t *testing.T) {
withDebug := parsimonious.ParseWithDebug(true)

grammar, err := parsimonious.NewGrammar(grammarText, withDebug)
if err != nil {
t.Errorf("parse grammar failed: %v", err)
return
}

program := `- item(name=string( Energy中文 ), subitem=item(value=number(997), unit=string("value")))`
t.Logf("%q\n", program)

tree, err := grammar.Parse(program, withDebug)
if err != nil {
t.Errorf("parse sample failed: %v", err)
return
}
t.Log("\n" + parsimonious.DumpNodeExprTree(tree))
}

0 comments on commit 1817c50

Please sign in to comment.