Fixes pegs bugs, continue

This commit is contained in:
data-man
2018-05-23 10:23:19 +03:00
parent 6acbe6fb01
commit df37796d88

View File

@@ -1226,7 +1226,8 @@ proc skip(c: var PegLexer) =
of ' ', '\t':
inc(pos)
of '#':
while not (buf[pos] in {'\c', '\L', '\0'}): inc(pos)
while (pos < c.buf.len) and
not (buf[pos] in {'\c', '\L', '\0'}): inc(pos)
of '\c':
pos = handleCR(c, pos)
buf = c.buf
@@ -1285,7 +1286,7 @@ proc getCharSet(c: var PegLexer, tok: var Token) =
var ch: char
case buf[pos]
of ']':
inc(pos)
if pos < c.buf.len: inc(pos)
break
of '\\':
c.bufpos = pos
@@ -1304,7 +1305,10 @@ proc getCharSet(c: var PegLexer, tok: var Token) =
incl(tok.charset, '-')
inc(pos)
else:
inc(pos)
if pos+1 < c.buf.len:
inc(pos)
else:
break
var ch2: char
case buf[pos]
of '\\':
@@ -1316,8 +1320,11 @@ proc getCharSet(c: var PegLexer, tok: var Token) =
tok.kind = tkInvalid
break
else:
ch2 = buf[pos]
inc(pos)
if pos+1 < c.buf.len:
ch2 = buf[pos]
inc(pos)
else:
break
for i in ord(ch)+1 .. ord(ch2):
incl(tok.charset, chr(i))
c.bufpos = pos
@@ -1334,7 +1341,7 @@ proc getSymbol(c: var PegLexer, tok: var Token) =
tok.kind = tkIdentifier
proc getBuiltin(c: var PegLexer, tok: var Token) =
if c.buf[c.bufpos+1] in strutils.Letters:
if c.bufpos+1 < c.buf.len and c.buf[c.bufpos+1] in strutils.Letters:
inc(c.bufpos)
getSymbol(c, tok)
tok.kind = tkBuiltin
@@ -1347,10 +1354,12 @@ proc getTok(c: var PegLexer, tok: var Token) =
tok.modifier = modNone
setLen(tok.literal, 0)
skip(c)
case c.buf[c.bufpos]
of '{':
inc(c.bufpos)
if c.buf[c.bufpos] == '@' and c.buf[c.bufpos+1] == '}':
if c.buf[c.bufpos] == '@' and c.bufpos+2 < c.buf.len and
c.buf[c.bufpos+1] == '}':
tok.kind = tkCurlyAt
inc(c.bufpos, 2)
add(tok.literal, "{@}")
@@ -1383,13 +1392,11 @@ proc getTok(c: var PegLexer, tok: var Token) =
getBuiltin(c, tok)
of '\'', '"': getString(c, tok)
of '$': getDollar(c, tok)
of '\0':
tok.kind = tkEof
tok.literal = "[EOF]"
of 'a'..'z', 'A'..'Z', '\128'..'\255':
getSymbol(c, tok)
if c.buf[c.bufpos] in {'\'', '"'} or
c.buf[c.bufpos] == '$' and c.buf[c.bufpos+1] in {'0'..'9'}:
c.buf[c.bufpos] == '$' and c.bufpos+1 < c.buf.len and
c.buf[c.bufpos+1] in {'0'..'9'}:
case tok.literal
of "i": tok.modifier = modIgnoreCase
of "y": tok.modifier = modIgnoreStyle
@@ -1410,7 +1417,7 @@ proc getTok(c: var PegLexer, tok: var Token) =
inc(c.bufpos)
add(tok.literal, '+')
of '<':
if c.buf[c.bufpos+1] == '-':
if c.bufpos+2 < c.buf.len and c.buf[c.bufpos+1] == '-':
inc(c.bufpos, 2)
tok.kind = tkArrow
add(tok.literal, "<-")
@@ -1445,6 +1452,9 @@ proc getTok(c: var PegLexer, tok: var Token) =
inc(c.bufpos)
add(tok.literal, '^')
else:
if c.bufpos >= c.buf.len:
tok.kind = tkEof
tok.literal = "[EOF]"
add(tok.literal, c.buf[c.bufpos])
inc(c.bufpos)