lexing successfully with larger example
This commit is contained in:
parent
9247710ccb
commit
7eb0b697dc
25
tcl.go
25
tcl.go
@ -20,29 +20,40 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
comment = NewParser("comment", Merge(Seq("#", NotChars("\r\n")))).Map(drop)
|
comment = NewParser("comment", Merge(Seq("#", NotChars("\r\n"), Chars("\r\n")))).Map(drop)
|
||||||
wordExp = NewParser("{*}word", Seq("{*}", &word)).Map(resultchild(1))
|
wordExp = NewParser("{*}word", Seq("{*}", &word)).Map(resultchild(1))
|
||||||
wordSub = NewParser("[*]word", Seq("[*]", &word)).Map(resultchild(1))
|
wordSub = NewParser("[*]word", Seq("[*]", &word)).Map(resultchild(1))
|
||||||
wordBrc = NewParser("{command}", Seq("{", Many(&command), "}")).Map(func(r *Result) { collectchildren(&r.Child[1]); r.Result = r.Child[1].Result })
|
|
||||||
wordQtd = NewParser(`"word"`, StringLit(`"`)).Map(token)
|
wordQtd = NewParser(`"word"`, StringLit(`"`)).Map(token)
|
||||||
wordSmp = NewParser("simple", NotChars("{}[]* ;\t\r\n")).Map(token)
|
wordSmp = NewParser("simple", NotChars("{} \t\r\n")).Map(token)
|
||||||
|
group = NewParser("{group}", Seq("{", Maybe(Chars("\r\n")), Some(Any(&command, &words)), "}")).Map(func(r *Result) { collectchildren(&r.Child[2]); r.Result = r.Child[2].Result })
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
word = NewParser("word", Any(&wordSmp, &wordQtd, &wordBrc, &wordSub, &wordExp))
|
word = NewParser("word", Any(&wordQtd, &group, &wordSmp, &wordSub, &wordExp))
|
||||||
words = NewParser("words", Many(&word)).Map(collectchildren)
|
words = NewParser("words", Many(&word)).Map(collectchildren)
|
||||||
command = NewParser("command", Seq(&words, Maybe(";"))).Map(resultchild(0))
|
command = NewParser("command", Seq(&words, Any(";", Chars("\r\n")))).Map(resultchild(0))
|
||||||
script = NewParser("script", Many(Any(&comment, &command))).Map(collectchildren)
|
script = NewParser("script", Many(Any(&comment, &command))).Map(collectchildren)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func simpleWhitespace(s *State) {
|
||||||
|
for s.Pos < len(s.Input) {
|
||||||
|
switch s.Input[s.Pos] {
|
||||||
|
case '\t', '\v', '\f', ' ':
|
||||||
|
s.Pos++
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Parse(input string) (data interface{}, e error) {
|
func Parse(input string) (data interface{}, e error) {
|
||||||
data, e = Run(&script, input, ASCIIWhitespace)
|
data, e = Run(&script, input, simpleWhitespace)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseDebug(input string) (data interface{}, e error) {
|
func ParseDebug(input string) (data interface{}, e error) {
|
||||||
EnableLogging(os.Stdout)
|
EnableLogging(os.Stdout)
|
||||||
data, e = Run(&script, input, ASCIIWhitespace)
|
data, e = Run(&script, input, simpleWhitespace)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
tcl_test.go
22
tcl_test.go
@ -1,6 +1,9 @@
|
|||||||
package tcl
|
package tcl
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestFirst(t *testing.T) {
|
func TestFirst(t *testing.T) {
|
||||||
input := `#TMSH-VERSION: 12.1.2
|
input := `#TMSH-VERSION: 12.1.2
|
||||||
@ -21,7 +24,8 @@ auth user admin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
shell bash
|
shell bash
|
||||||
}`
|
}
|
||||||
|
`
|
||||||
|
|
||||||
d, e := ParseDebug(input)
|
d, e := ParseDebug(input)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
@ -31,3 +35,17 @@ auth user admin {
|
|||||||
t.Logf("got d: %#v\n", d)
|
t.Logf("got d: %#v\n", d)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExample2(t *testing.T) {
|
||||||
|
input, err := ioutil.ReadFile("testdata/example2.conf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
d, e := ParseDebug(string(input))
|
||||||
|
if e != nil {
|
||||||
|
t.Fatal(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("got d: %#v\n", d)
|
||||||
|
|
||||||
|
}
|
||||||
|
1500
testdata/example2.conf
vendored
Normal file
1500
testdata/example2.conf
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user