From 72a6075123cbb2749a58908766e718ccd2dc0991 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Thu, 19 Jun 2025 03:26:22 +1000 Subject: [PATCH] nlParser now exposes a subset of the nlTokStream interface --- src/noether/parser/parser.nim | 14 +++++++------- src/noether/parser/parseutil.nim | 29 ++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/noether/parser/parser.nim b/src/noether/parser/parser.nim index 7da349f..7daf91b 100644 --- a/src/noether/parser/parser.nim +++ b/src/noether/parser/parser.nim @@ -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' diff --git a/src/noether/parser/parseutil.nim b/src/noether/parser/parseutil.nim index 8b9ef20..d531490 100644 --- a/src/noether/parser/parseutil.nim +++ b/src/noether/parser/parseutil.nim @@ -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