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
}
}
node.Start = startpos
node.End = ps.Pos
})
}
@ -27,7 +29,10 @@ func NoAutoWS(parser Parserish) Parser {
return func(ps *State, node *Result) {
oldWS := ps.WS
ps.WS = NoWhitespace
startpos := ps.Pos
parserfied(ps, node)
node.Start = startpos
node.End = ps.Pos
ps.WS = oldWS
}
}
@ -57,6 +62,8 @@ func Any(parsers ...Parserish) Parser {
ps.Recover()
continue
}
node.Start = startpos
node.End = ps.Pos
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 {
ps.Recover()
}
node.Start = startpos
node.End = ps.Pos
})
}
@ -133,11 +144,14 @@ func Bind(parser Parserish, val interface{}) Parser {
p := Parsify(parser)
return func(ps *State, node *Result) {
startpos := ps.Pos
p(ps, node)
if ps.Errored() {
return
}
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)
return func(ps *State, node *Result) {
startpos := ps.Pos
p(ps, node)
if ps.Errored() {
return
}
node.Start = startpos
node.End = ps.Pos
f(node)
}
}

View File

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

View File

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

View File

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