script returns reasonable results
This commit is contained in:
parent
e3ea0f0201
commit
9247710ccb
62
tcl.go
62
tcl.go
@ -14,24 +14,25 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
word Parser
|
word Parser
|
||||||
commands Parser
|
words Parser
|
||||||
|
command Parser
|
||||||
script Parser
|
script Parser
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
comment = NewParser("comment", Merge(NoAutoWS(Seq("#", NotChars("\r\n"))))).Map(found("comment"))
|
comment = NewParser("comment", Merge(Seq("#", NotChars("\r\n")))).Map(drop)
|
||||||
wordExp = NewParser("{*}word", NoAutoWS(Seq("{*}", &word))).Map(found("wordExp"))
|
wordExp = NewParser("{*}word", Seq("{*}", &word)).Map(resultchild(1))
|
||||||
wordSub = NewParser("[*]word", NoAutoWS(Seq("[*]", &word))).Map(found("wordSub"))
|
wordSub = NewParser("[*]word", Seq("[*]", &word)).Map(resultchild(1))
|
||||||
wordBrc = NewParser("{word}", Seq("{", &word, "}")).Map(found("wordBrc"))
|
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(found("wordQtd"))
|
wordQtd = NewParser(`"word"`, StringLit(`"`)).Map(token)
|
||||||
wordSmp = NewParser("simple", NotChars("{}[]*")).Map(found("simple"))
|
wordSmp = NewParser("simple", NotChars("{}[]* ;\t\r\n")).Map(token)
|
||||||
command = NewParser("command", Many(&word)).Map(found("command")).Map(found("command"))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
word = NewParser("word", Any(&wordSmp, &wordQtd, &wordBrc, &wordSub, &wordExp)).Map(found("word"))
|
word = NewParser("word", Any(&wordSmp, &wordQtd, &wordBrc, &wordSub, &wordExp))
|
||||||
commands = NewParser("commands", Any(&command, Maybe(Chars(";"))))
|
words = NewParser("words", Many(&word)).Map(collectchildren)
|
||||||
script = NewParser("script", Many(Any(&comment, &commands)))
|
command = NewParser("command", Seq(&words, Maybe(";"))).Map(resultchild(0))
|
||||||
|
script = NewParser("script", Many(Any(&comment, &command))).Map(collectchildren)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Parse(input string) (data interface{}, e error) {
|
func Parse(input string) (data interface{}, e error) {
|
||||||
@ -47,11 +48,44 @@ func ParseDebug(input string) (data interface{}, e error) {
|
|||||||
|
|
||||||
func found(typ string) func(*Result) {
|
func found(typ string) func(*Result) {
|
||||||
return func(r *Result) {
|
return func(r *Result) {
|
||||||
log.Println("found a", typ)
|
log.Printf("found a %s: %q (r.R: %v)\n", typ, r.Token, r.Result)
|
||||||
spew.Dump(r)
|
r.Result = r.Token
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func drop(r *Result) {
|
||||||
|
r.Token = ""
|
||||||
|
r.Result = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func token(r *Result) {
|
||||||
|
r.Result = r.Token
|
||||||
|
// log.Println("token:", r.Token)
|
||||||
|
}
|
||||||
|
|
||||||
|
func collectchildren(r *Result) {
|
||||||
|
data := []string{}
|
||||||
|
for _, ch := range r.Child {
|
||||||
|
switch v := ch.Result.(type) {
|
||||||
|
case string:
|
||||||
|
data = append(data, v)
|
||||||
|
case []string:
|
||||||
|
data = append(data, v...)
|
||||||
|
default:
|
||||||
|
// log.Println("oops:", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// log.Println("data: ", data)
|
||||||
|
r.Result = data
|
||||||
|
}
|
||||||
|
|
||||||
|
func resultchild(c int) func(*Result) {
|
||||||
|
return func(r *Result) {
|
||||||
|
r.Result = r.Child[c].Result
|
||||||
|
// log.Printf("child[%d]: %v\n", c, r.Result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func dump(r *Result) {
|
func dump(r *Result) {
|
||||||
spew.Dump(r)
|
spew.Dump(r)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user