lexing successfully with larger example

This commit is contained in:
Özgür Kesim 2019-06-17 08:47:04 +02:00
parent 9247710ccb
commit 7eb0b697dc
3 changed files with 1538 additions and 9 deletions

25
tcl.go
View File

@ -20,29 +20,40 @@ 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))
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)
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() {
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)
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)
}
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) {
data, e = Run(&script, input, ASCIIWhitespace)
data, e = Run(&script, input, simpleWhitespace)
return
}
func ParseDebug(input string) (data interface{}, e error) {
EnableLogging(os.Stdout)
data, e = Run(&script, input, ASCIIWhitespace)
data, e = Run(&script, input, simpleWhitespace)
return
}

View File

@ -1,6 +1,9 @@
package tcl
import "testing"
import (
"io/ioutil"
"testing"
)
func TestFirst(t *testing.T) {
input := `#TMSH-VERSION: 12.1.2
@ -21,7 +24,8 @@ auth user admin {
}
}
shell bash
}`
}
`
d, e := ParseDebug(input)
if e != nil {
@ -31,3 +35,17 @@ auth user admin {
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

File diff suppressed because it is too large Load Diff