Once again fixed EOL handling...
This commit is contained in:
parent
99db57dcfd
commit
07a9bda9ba
2 changed files with 19 additions and 36 deletions
|
|
@ -54,70 +54,55 @@ proc finishBuild(builder: var nlTokBuilder) =
|
||||||
|
|
||||||
# Finish, return, and reset the build token
|
# Finish, return, and reset the build token
|
||||||
proc flushBuild(builder: var nlTokBuilder): nlTok =
|
proc flushBuild(builder: var nlTokBuilder): nlTok =
|
||||||
echo "Flush @", builder.pos
|
|
||||||
finishBuild(builder)
|
finishBuild(builder)
|
||||||
result = builder.tok
|
result = builder.tok
|
||||||
resetBuild(builder)
|
resetBuild(builder)
|
||||||
|
|
||||||
# Is the build token "compatible" with the current char?
|
# Is the build token "compatible" with the current char? (if not then flushbuild)
|
||||||
# NOTE: flushBuild() is called if incompatible
|
# NOTE: This implicitly handles Windows CRLF, Unix LF, & Mac OS CR compatability
|
||||||
proc isCompatibleBuild(builder: nlTokBuilder): bool =
|
# NOTE: since atEOL => '\n', but '\r' and '\n' are both tkEOL so they both flush.
|
||||||
result = (builder.cTKind == builder.tok.kind)
|
proc isIncompatibleBuild(builder: nlTokBuilder): bool =
|
||||||
|
result = (builder.cTKind != builder.tok.kind or builder.atEOL())
|
||||||
|
|
||||||
# Inherit the build token's type from current char
|
# Inherit the build token's type from current char
|
||||||
proc inherit(builder: var nlTokBuilder) =
|
proc inherit(builder: var nlTokBuilder) =
|
||||||
builder.tok.kind = builder.cTKind
|
builder.tok.kind = builder.cTKind
|
||||||
|
|
||||||
# Add a character to the nlTokBuilder's build token.
|
# Add a character to the nlTokBuilder's build token.
|
||||||
# Flushes and returns the build token if "fully built",
|
# Flushes and returns the build token if finished.
|
||||||
# and a boolean indicating whether the nlTokBuilder can progress.
|
proc appendBuild(builder: var nlTokBuilder): Option[nlTok] =
|
||||||
proc appendBuild(builder: var nlTokBuilder, flushed: var Option[nlTok]): bool =
|
|
||||||
# untyped build tokens inherit type immediately
|
# untyped build tokens inherit type immediately
|
||||||
if builder.tok.isUntyped():
|
if builder.tok.isUntyped():
|
||||||
builder.inherit()
|
builder.inherit()
|
||||||
|
|
||||||
# check if EOF reached
|
|
||||||
# if builder.atEOL():
|
|
||||||
# echo "EOL DETECT 1"
|
|
||||||
# result = false # DO NOT PROGRESS
|
|
||||||
# flushed = some(flushBuild(builder))
|
|
||||||
# check character and build token compatability
|
# check character and build token compatability
|
||||||
if not isCompatibleBuild(builder):
|
if isIncompatibleBuild(builder):
|
||||||
# flush old build token, the new one inherits type
|
# flush old build token, the new one inherits type
|
||||||
flushed = some(flushBuild(builder))
|
result = some(flushBuild(builder))
|
||||||
builder.inherit()
|
builder.inherit()
|
||||||
result = true # can progress
|
|
||||||
else:
|
else:
|
||||||
flushed = none(nlTok)
|
result = none(nlTok)
|
||||||
result = true # can progress
|
|
||||||
|
|
||||||
#[ ========================================== ]
|
#[ ========================================== ]
|
||||||
| nlTokBuilder Char Stream Reading Interface ]
|
| nlTokBuilder Char Stream Reading Interface ]
|
||||||
]#
|
]#
|
||||||
|
|
||||||
# Read the next char in the stream without
|
# Read the next char in the stream
|
||||||
# checking whether it is safe to do so
|
# NOTE: readChar raises IOError on error, returns \0 on EOF
|
||||||
proc forceReadChar(builder: var nlTokBuilder) {.inline.} =
|
proc readChar*(builder: var nlTokBuilder): bool =
|
||||||
echo "read"
|
if builder.atEOL():
|
||||||
inc builder.pos
|
inc builder.lineNum
|
||||||
|
# sets builder.char to '\0' if EOF
|
||||||
builder.char = builder.stream.readChar()
|
builder.char = builder.stream.readChar()
|
||||||
builder.cTKind = getTokKind(builder.char)
|
builder.cTKind = getTokKind(builder.char)
|
||||||
builder.line.add(builder.char)
|
builder.line.add(builder.char)
|
||||||
|
inc builder.pos
|
||||||
# Read the next char in the stream
|
|
||||||
# NOTE: readChar raises IOError on error, returns \0 on EOF
|
|
||||||
proc readChar(builder: var nlTokBuilder): bool =
|
|
||||||
if builder.atEOL():
|
|
||||||
echo "EOL DETECT 2"
|
|
||||||
inc builder.lineNum
|
|
||||||
# sets builder.char to '\0' if EOF
|
|
||||||
builder.forceReadChar()
|
|
||||||
result = builder.atEOF()
|
result = builder.atEOF()
|
||||||
|
|
||||||
# Read until EOL and return the current line
|
# Read until EOL and return the current line
|
||||||
# NOTE: Does NOT update the builder's state (unsafe)
|
# NOTE: Does NOT update the builder's state (unsafe)
|
||||||
# NOTE: ONLY call if a lex/parse error needs displaying
|
# NOTE: ONLY call if a lex/parse error needs displaying
|
||||||
proc unsafeGetLine(builder: var nlTokBuilder): string =
|
proc unsafeGetLine*(builder: var nlTokBuilder): string =
|
||||||
while not builder.atEOL() and builder.readChar():
|
while not builder.atEOL() and builder.readChar():
|
||||||
discard
|
discard
|
||||||
result = builder.line
|
result = builder.line
|
||||||
|
|
|
||||||
|
|
@ -31,12 +31,10 @@ proc progress*(stream: var nlTokStream): bool =
|
||||||
if stream.isClosed:
|
if stream.isClosed:
|
||||||
return false
|
return false
|
||||||
while true:
|
while true:
|
||||||
# echo "\nProgressing..."
|
|
||||||
var flushedTok: Option[nlTok]
|
|
||||||
let
|
let
|
||||||
atEOF = stream.builder.readChar()
|
atEOF = stream.builder.readChar()
|
||||||
|
flushedTok = stream.builder.appendBuild()
|
||||||
newTokBuilt = flushedTok.isSome
|
newTokBuilt = flushedTok.isSome
|
||||||
discard stream.builder.appendBuild(flushedTok)
|
|
||||||
echo flushedTok
|
echo flushedTok
|
||||||
echo "atEOF: ", atEOF, "\nnewTokBuilt: ", newTokBuilt
|
echo "atEOF: ", atEOF, "\nnewTokBuilt: ", newTokBuilt
|
||||||
# canProgress & EOF reached => no more tokens to build :)
|
# canProgress & EOF reached => no more tokens to build :)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue