Lexbase compatible with JS
This commit is contained in:
parent
9aa60add87
commit
589132065e
1 changed files with 19 additions and 6 deletions
|
|
@ -28,7 +28,10 @@ type
|
||||||
BaseLexer* = object of RootObj ## the base lexer. Inherit your lexer from
|
BaseLexer* = object of RootObj ## the base lexer. Inherit your lexer from
|
||||||
## this object.
|
## this object.
|
||||||
bufpos*: int ## the current position within the buffer
|
bufpos*: int ## the current position within the buffer
|
||||||
buf*: cstring ## the buffer itself
|
when defined(js): ## the buffer itself
|
||||||
|
buf*: string
|
||||||
|
else:
|
||||||
|
buf*: cstring
|
||||||
bufLen*: int ## length of buffer in characters
|
bufLen*: int ## length of buffer in characters
|
||||||
input: Stream ## the input stream
|
input: Stream ## the input stream
|
||||||
lineNumber*: int ## the current line number
|
lineNumber*: int ## the current line number
|
||||||
|
|
@ -43,6 +46,7 @@ const
|
||||||
|
|
||||||
proc close*(L: var BaseLexer) =
|
proc close*(L: var BaseLexer) =
|
||||||
## closes the base lexer. This closes `L`'s associated stream too.
|
## closes the base lexer. This closes `L`'s associated stream too.
|
||||||
|
when not defined(js):
|
||||||
dealloc(L.buf)
|
dealloc(L.buf)
|
||||||
close(L.input)
|
close(L.input)
|
||||||
|
|
||||||
|
|
@ -58,8 +62,11 @@ proc fillBuffer(L: var BaseLexer) =
|
||||||
toCopy = L.bufLen - L.sentinel - 1
|
toCopy = L.bufLen - L.sentinel - 1
|
||||||
assert(toCopy >= 0)
|
assert(toCopy >= 0)
|
||||||
if toCopy > 0:
|
if toCopy > 0:
|
||||||
moveMem(L.buf, addr(L.buf[L.sentinel + 1]), toCopy * chrSize)
|
when defined(js):
|
||||||
|
for i in 0 ..< toCopy: L.buf[i] = L.buf[L.sentinel + 1 + i]
|
||||||
|
else:
|
||||||
# "moveMem" handles overlapping regions
|
# "moveMem" handles overlapping regions
|
||||||
|
moveMem(L.buf, addr L.buf[L.sentinel + 1], toCopy * chrSize)
|
||||||
charsRead = readData(L.input, addr(L.buf[toCopy]),
|
charsRead = readData(L.input, addr(L.buf[toCopy]),
|
||||||
(L.sentinel + 1) * chrSize) div chrSize
|
(L.sentinel + 1) * chrSize) div chrSize
|
||||||
s = toCopy + charsRead
|
s = toCopy + charsRead
|
||||||
|
|
@ -81,6 +88,9 @@ proc fillBuffer(L: var BaseLexer) =
|
||||||
# double the buffer's size and try again:
|
# double the buffer's size and try again:
|
||||||
oldBufLen = L.bufLen
|
oldBufLen = L.bufLen
|
||||||
L.bufLen = L.bufLen * 2
|
L.bufLen = L.bufLen * 2
|
||||||
|
when defined(js):
|
||||||
|
L.buf.setLen(L.bufLen)
|
||||||
|
else:
|
||||||
L.buf = cast[cstring](realloc(L.buf, L.bufLen * chrSize))
|
L.buf = cast[cstring](realloc(L.buf, L.bufLen * chrSize))
|
||||||
assert(L.bufLen - oldBufLen == oldBufLen)
|
assert(L.bufLen - oldBufLen == oldBufLen)
|
||||||
charsRead = readData(L.input, addr(L.buf[oldBufLen]),
|
charsRead = readData(L.input, addr(L.buf[oldBufLen]),
|
||||||
|
|
@ -139,6 +149,9 @@ proc open*(L: var BaseLexer, input: Stream, bufLen: int = 8192;
|
||||||
L.bufpos = 0
|
L.bufpos = 0
|
||||||
L.bufLen = bufLen
|
L.bufLen = bufLen
|
||||||
L.refillChars = refillChars
|
L.refillChars = refillChars
|
||||||
|
when defined(js):
|
||||||
|
L.buf = newString(bufLen)
|
||||||
|
else:
|
||||||
L.buf = cast[cstring](alloc(bufLen * chrSize))
|
L.buf = cast[cstring](alloc(bufLen * chrSize))
|
||||||
L.sentinel = bufLen - 1
|
L.sentinel = bufLen - 1
|
||||||
L.lineStart = 0
|
L.lineStart = 0
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue