nondescript/src/ndspkg/compv2/parser/utils.nim

53 lines
1.4 KiB
Nim

import ../types
import ../../config
proc errorAt*(par: Parser, line: int, msg: string, at: string = "") =
if par.panicMode:
return
write stderr, &"[line {line}] Error "
if at.len > 0:
write stderr, &"at {at} "
write stderr, msg
write stderr, "\n"
par.hadError = true
par.panicMode = true
proc error*(par: Parser, msg: string) =
## create a simple error message
par.errorAt(par.previous.line, msg, par.previous.text)
proc errorAtCurrent*(par: Parser, msg: string, supress: bool = false) =
par.errorAt(par.current.line, msg, if supress: "" else: par.current.text)
proc advance*(par: Parser) =
par.previous = par.current
while true:
par.current = par.scanner.scanToken()
when debugScanner:
par.current.debugPrint()
if (par.current.tokenType != tkError):
break
par.errorAtCurrent(par.current.text, true)
proc match*(par: Parser, tokenType: TokenType): bool =
if par.current.tokenType == tokenType:
par.advance()
true
else:
false
proc consume*(par: Parser, tokenType: TokenType, msg: string) =
if par.current.tokenType == tokenType:
par.advance()
else:
par.errorAtCurrent(msg)
proc synchronize*(par: Parser) =
par.panicMode = false
while par.current.tokenType != tkEof:
if par.previous.tokenType in {tkSemicolon, tkRightBrace}:
return
if par.current.tokenType in {tkFunct, tkVar, tkFor, tkIf, tkWhile}:
return
par.advance()