Fixes pegs bugs

This commit is contained in:
data-man 2018-05-23 09:45:51 +03:00
commit 6acbe6fb01

View file

@ -1125,7 +1125,7 @@ proc handleCR(L: var PegLexer, pos: int): int =
assert(L.buf[pos] == '\c') assert(L.buf[pos] == '\c')
inc(L.lineNumber) inc(L.lineNumber)
result = pos+1 result = pos+1
if L.buf[result] == '\L': inc(result) if result < L.buf.len and L.buf[result] == '\L': inc(result)
L.lineStart = result L.lineStart = result
proc handleLF(L: var PegLexer, pos: int): int = proc handleLF(L: var PegLexer, pos: int): int =
@ -1221,7 +1221,7 @@ proc getEscapedChar(c: var PegLexer, tok: var Token) =
proc skip(c: var PegLexer) = proc skip(c: var PegLexer) =
var pos = c.bufpos var pos = c.bufpos
var buf = c.buf var buf = c.buf
while true: while pos < c.buf.len:
case buf[pos] case buf[pos]
of ' ', '\t': of ' ', '\t':
inc(pos) inc(pos)
@ -1242,7 +1242,7 @@ proc getString(c: var PegLexer, tok: var Token) =
var pos = c.bufpos + 1 var pos = c.bufpos + 1
var buf = c.buf var buf = c.buf
var quote = buf[pos-1] var quote = buf[pos-1]
while true: while pos < c.buf.len:
case buf[pos] case buf[pos]
of '\\': of '\\':
c.bufpos = pos c.bufpos = pos
@ -1265,7 +1265,7 @@ proc getDollar(c: var PegLexer, tok: var Token) =
if buf[pos] in {'0'..'9'}: if buf[pos] in {'0'..'9'}:
tok.kind = tkBackref tok.kind = tkBackref
tok.index = 0 tok.index = 0
while buf[pos] in {'0'..'9'}: while pos < c.buf.len and buf[pos] in {'0'..'9'}:
tok.index = tok.index * 10 + ord(buf[pos]) - ord('0') tok.index = tok.index * 10 + ord(buf[pos]) - ord('0')
inc(pos) inc(pos)
else: else:
@ -1281,7 +1281,7 @@ proc getCharSet(c: var PegLexer, tok: var Token) =
if buf[pos] == '^': if buf[pos] == '^':
inc(pos) inc(pos)
caret = true caret = true
while true: while pos < c.buf.len:
var ch: char var ch: char
case buf[pos] case buf[pos]
of ']': of ']':
@ -1300,7 +1300,7 @@ proc getCharSet(c: var PegLexer, tok: var Token) =
inc(pos) inc(pos)
incl(tok.charset, ch) incl(tok.charset, ch)
if buf[pos] == '-': if buf[pos] == '-':
if buf[pos+1] == ']': if pos+1 < c.buf.len and buf[pos+1] == ']':
incl(tok.charset, '-') incl(tok.charset, '-')
inc(pos) inc(pos)
else: else:
@ -1326,10 +1326,10 @@ proc getCharSet(c: var PegLexer, tok: var Token) =
proc getSymbol(c: var PegLexer, tok: var Token) = proc getSymbol(c: var PegLexer, tok: var Token) =
var pos = c.bufpos var pos = c.bufpos
var buf = c.buf var buf = c.buf
while true: while pos < c.buf.len:
add(tok.literal, buf[pos]) add(tok.literal, buf[pos])
inc(pos) inc(pos)
if buf[pos] notin strutils.IdentChars: break if pos < buf.len and buf[pos] notin strutils.IdentChars: break
c.bufpos = pos c.bufpos = pos
tok.kind = tkIdentifier tok.kind = tkIdentifier
@ -1451,8 +1451,8 @@ proc getTok(c: var PegLexer, tok: var Token) =
proc arrowIsNextTok(c: PegLexer): bool = proc arrowIsNextTok(c: PegLexer): bool =
# the only look ahead we need # the only look ahead we need
var pos = c.bufpos var pos = c.bufpos
while c.buf[pos] in {'\t', ' '}: inc(pos) while pos < c.buf.len and c.buf[pos] in {'\t', ' '}: inc(pos)
result = c.buf[pos] == '<' and c.buf[pos+1] == '-' result = c.buf[pos] == '<' and (pos+1 < c.buf.len) and c.buf[pos+1] == '-'
# ----------------------------- parser ---------------------------------------- # ----------------------------- parser ----------------------------------------
@ -1475,7 +1475,7 @@ proc pegError(p: PegParser, msg: string, line = -1, col = -1) =
proc getTok(p: var PegParser) = proc getTok(p: var PegParser) =
getTok(p, p.tok) getTok(p, p.tok)
if p.tok.kind == tkInvalid: pegError(p, "invalid token") if p.tok.kind == tkInvalid: pegError(p, "'" & p.tok.literal & "' is invalid token")
proc eat(p: var PegParser, kind: TokKind) = proc eat(p: var PegParser, kind: TokKind) =
if p.tok.kind == kind: getTok(p) if p.tok.kind == kind: getTok(p)