added .Start and .End to Result

This commit is contained in:
Özgür Kesim 2019-11-28 15:34:30 +01:00
parent 9bc60a2311
commit e2fa16706c
4 changed files with 37 additions and 1 deletions

View File

@ -18,6 +18,8 @@ func Seq(parsers ...Parserish) Parser {
return return
} }
} }
node.Start = startpos
node.End = ps.Pos
}) })
} }
@ -27,7 +29,10 @@ func NoAutoWS(parser Parserish) Parser {
return func(ps *State, node *Result) { return func(ps *State, node *Result) {
oldWS := ps.WS oldWS := ps.WS
ps.WS = NoWhitespace ps.WS = NoWhitespace
startpos := ps.Pos
parserfied(ps, node) parserfied(ps, node)
node.Start = startpos
node.End = ps.Pos
ps.WS = oldWS ps.WS = oldWS
} }
} }
@ -57,6 +62,8 @@ func Any(parsers ...Parserish) Parser {
ps.Recover() ps.Recover()
continue continue
} }
node.Start = startpos
node.End = ps.Pos
return return
} }
@ -110,6 +117,8 @@ func manyImpl(min int, op Parserish, sep ...Parserish) Parser {
} }
} }
} }
node.Start = startpos
node.End = ps.Pos
} }
} }
@ -123,6 +132,8 @@ func Maybe(parser Parserish) Parser {
if ps.Errored() && ps.Cut <= startpos { if ps.Errored() && ps.Cut <= startpos {
ps.Recover() ps.Recover()
} }
node.Start = startpos
node.End = ps.Pos
}) })
} }
@ -133,11 +144,14 @@ func Bind(parser Parserish, val interface{}) Parser {
p := Parsify(parser) p := Parsify(parser)
return func(ps *State, node *Result) { return func(ps *State, node *Result) {
startpos := ps.Pos
p(ps, node) p(ps, node)
if ps.Errored() { if ps.Errored() {
return return
} }
node.Result = val node.Result = val
node.Start = startpos
node.End = ps.Pos
} }
} }
@ -147,10 +161,13 @@ func Map(parser Parserish, f func(n *Result)) Parser {
p := Parsify(parser) p := Parsify(parser)
return func(ps *State, node *Result) { return func(ps *State, node *Result) {
startpos := ps.Pos
p(ps, node) p(ps, node)
if ps.Errored() { if ps.Errored() {
return return
} }
node.Start = startpos
node.End = ps.Pos
f(node) f(node)
} }
} }

View File

@ -59,12 +59,16 @@ func StringLit(allowedQuotes string) Parser {
} }
case quote: case quote:
if buf == nil { if buf == nil {
node.Start = ps.Pos + 1
node.End = end
node.Token = ps.Input[ps.Pos+1 : end] node.Token = ps.Input[ps.Pos+1 : end]
ps.Pos = end + 1 ps.Pos = end + 1
return return
} }
ps.Pos = end + 1
node.Token = buf.String() node.Token = buf.String()
node.Start = ps.Pos
node.End = ps.Pos + len(node.Token)
ps.Pos = end + 1
return return
default: default:
if buf == nil { if buf == nil {
@ -139,6 +143,8 @@ func NumberLit() Parser {
ps.ErrorHere("number") ps.ErrorHere("number")
return return
} }
node.Start = ps.Pos
node.End = end
ps.Pos = end ps.Pos = end
}) })
} }

View File

@ -109,6 +109,8 @@ func Regex(pattern string) Parser {
return NewParser(pattern, func(ps *State, node *Result) { return NewParser(pattern, func(ps *State, node *Result) {
ps.WS(ps) ps.WS(ps)
if match := re.FindString(ps.Get()); match != "" { if match := re.FindString(ps.Get()); match != "" {
node.Start = ps.Pos
node.End = ps.Pos + len(match)
ps.Advance(len(match)) ps.Advance(len(match))
node.Token = match node.Token = match
return return
@ -128,6 +130,8 @@ func Exact(match string) Parser {
return return
} }
node.Start = ps.Pos
node.End = ps.Pos + 1
ps.Advance(1) ps.Advance(1)
node.Token = match node.Token = match
@ -141,6 +145,9 @@ func Exact(match string) Parser {
return return
} }
node.Start = ps.Pos
node.End = ps.Pos + len(match)
ps.Advance(len(match)) ps.Advance(len(match))
node.Token = match node.Token = match
@ -241,6 +248,8 @@ func charsImpl(matcher string, stopOn bool, repetition ...int) Parser {
return return
} }
node.Start = ps.Pos
node.End = ps.Pos + matched
node.Token = ps.Input[ps.Pos : ps.Pos+matched] node.Token = ps.Input[ps.Pos : ps.Pos+matched]
ps.Advance(matched) ps.Advance(matched)
} }
@ -265,6 +274,8 @@ func Until(terminators ...string) Parser {
if ps.Pos == startPos { if ps.Pos == startPos {
ps.ErrorHere("something") ps.ErrorHere("something")
} }
node.Start = startPos
node.End = ps.Pos
node.Token = ps.Input[startPos:ps.Pos] node.Token = ps.Input[startPos:ps.Pos]
}) })
} }

View File

@ -15,6 +15,8 @@ type Result struct {
Token string Token string
Child []Result Child []Result
Result interface{} Result interface{}
Start int
End int
} }
// String stringifies a node. This is only called from debug code. // String stringifies a node. This is only called from debug code.