nlParser now exposes a subset of the nlTokStream interface

This commit is contained in:
Emile Clark-Boman 2025-06-19 03:26:22 +10:00
parent 8e6c0bbbfc
commit 72a6075123
2 changed files with 29 additions and 14 deletions

View file

@ -21,15 +21,15 @@ proc parseChrLit(parser: var nlParser): nlParseStat =
result = parser.parseMatchLine(tkSQUO)
proc parseStmt(parser: var nlParser): nlParseStat =
while parser.stream.progress():
echo "----- Current Token: ", parser.stream.currTok
case parser.stream.currTok.tKind
while parser.progressStream():
echo "----- Current Token: ", parser.currTok
case parser.currTok.tKind
of tkDQUO:
# Attempt to parse string literal
if parser.parseStrLit() != nlParseStat.OK:
echo "Unmatched Double Quotation! Malformed String Literal"
echo parser.stream.line
echo repeat(" ", parser.stream.currTok.startPos), '^', '\n'
echo parser.line
echo repeat(" ", parser.currTok.startPos), '^', '\n'
else:
echo "Parsed String Literal"
echo parser.bnode[], '\n'
@ -37,8 +37,8 @@ proc parseStmt(parser: var nlParser): nlParseStat =
# Attempt to parse string literal
if parser.parseChrLit() != nlParseStat.OK:
echo "Unmatched Single Quotation! Malformed Character Literal"
echo parser.stream.line
echo repeat(" ", parser.stream.currTok.startPos), '^', '\n'
echo parser.line
echo repeat(" ", parser.currTok.startPos), '^', '\n'
else:
echo "Parsed Character Literal"
echo parser.bnode[], '\n'

View file

@ -20,6 +20,9 @@ type
# the parser is currently modifying/building from
# NOTE: bnode changes frequently, it is NOT the root
bnode: nlNode
# flag indicating whether the parser is at
# the start of a new line (aka checking indentation)
inIndent: bool
proc `*`(stat: nlParseStat, b: bool): nlParseStat =
@ -38,6 +41,18 @@ proc newParser*(tokStream: var nlTokStream): nlParser =
bnode: rootNode,
)
# Exposes a subset of the nlTokStream interface
proc currTok(parser: var nlParser): nlTok = parser.stream.currTok
proc line(parser: var nlParser): string = parser.stream.line
# Extends upon the functionality of nlTokStream.progress()
proc progressStream*(parser: var nlParser): bool =
result = parser.stream.progress()
if result and parser.currTok.tKind == tkEOL:
parser.inIndent = true
if
proc setNewLine()
#[ "Greed" refers to something I mentioned in my discussion on
| Noether's grammar (in an EBNF-like language). Greed just
@ -49,21 +64,21 @@ proc newParser*(tokStream: var nlTokStream): nlParser =
# Returns false if the greed was never satisfied (OMG!!)
proc greed(parser: var nlParser,
satisfy: proc(tok: nlTok): bool): nlParseStat =
while parser.stream.progress():
if satisfy(parser.stream.currTok):
while parser.progressStream():
if satisfy(parser.currTok):
return nlParseStat.OK
# NOTE: the matched token is currently excluded
parser.bnode.addTok(parser.stream.currTok)
parser.bnode.addTok(parser.currTok)
result = nlParseStat.UNMATCHED
proc greedLine(parser: var nlParser,
satisfy: proc(tok: nlTok): bool): nlParseStat =
while parser.stream.progress():
if satisfy(parser.stream.currTok):
while parser.progressStream():
if satisfy(parser.currTok):
return nlParseStat.OK
# NOTE: the matched token is currently excluded
parser.bnode.addTok(parser.stream.currTok)
if parser.stream.currTok.tKind == tkEOL:
parser.bnode.addTok(parser.currTok)
if parser.currTok.tKind == tkEOL:
return nlParseStat.UNMATCHED
result = nlParseStat.UNMATCHED