nlParser now exposes a subset of the nlTokStream interface
This commit is contained in:
parent
8e6c0bbbfc
commit
72a6075123
2 changed files with 29 additions and 14 deletions
|
|
@ -21,15 +21,15 @@ proc parseChrLit(parser: var nlParser): nlParseStat =
|
||||||
result = parser.parseMatchLine(tkSQUO)
|
result = parser.parseMatchLine(tkSQUO)
|
||||||
|
|
||||||
proc parseStmt(parser: var nlParser): nlParseStat =
|
proc parseStmt(parser: var nlParser): nlParseStat =
|
||||||
while parser.stream.progress():
|
while parser.progressStream():
|
||||||
echo "----- Current Token: ", parser.stream.currTok
|
echo "----- Current Token: ", parser.currTok
|
||||||
case parser.stream.currTok.tKind
|
case parser.currTok.tKind
|
||||||
of tkDQUO:
|
of tkDQUO:
|
||||||
# Attempt to parse string literal
|
# Attempt to parse string literal
|
||||||
if parser.parseStrLit() != nlParseStat.OK:
|
if parser.parseStrLit() != nlParseStat.OK:
|
||||||
echo "Unmatched Double Quotation! Malformed String Literal"
|
echo "Unmatched Double Quotation! Malformed String Literal"
|
||||||
echo parser.stream.line
|
echo parser.line
|
||||||
echo repeat(" ", parser.stream.currTok.startPos), '^', '\n'
|
echo repeat(" ", parser.currTok.startPos), '^', '\n'
|
||||||
else:
|
else:
|
||||||
echo "Parsed String Literal"
|
echo "Parsed String Literal"
|
||||||
echo parser.bnode[], '\n'
|
echo parser.bnode[], '\n'
|
||||||
|
|
@ -37,8 +37,8 @@ proc parseStmt(parser: var nlParser): nlParseStat =
|
||||||
# Attempt to parse string literal
|
# Attempt to parse string literal
|
||||||
if parser.parseChrLit() != nlParseStat.OK:
|
if parser.parseChrLit() != nlParseStat.OK:
|
||||||
echo "Unmatched Single Quotation! Malformed Character Literal"
|
echo "Unmatched Single Quotation! Malformed Character Literal"
|
||||||
echo parser.stream.line
|
echo parser.line
|
||||||
echo repeat(" ", parser.stream.currTok.startPos), '^', '\n'
|
echo repeat(" ", parser.currTok.startPos), '^', '\n'
|
||||||
else:
|
else:
|
||||||
echo "Parsed Character Literal"
|
echo "Parsed Character Literal"
|
||||||
echo parser.bnode[], '\n'
|
echo parser.bnode[], '\n'
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,9 @@ type
|
||||||
# the parser is currently modifying/building from
|
# the parser is currently modifying/building from
|
||||||
# NOTE: bnode changes frequently, it is NOT the root
|
# NOTE: bnode changes frequently, it is NOT the root
|
||||||
bnode: nlNode
|
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 =
|
proc `*`(stat: nlParseStat, b: bool): nlParseStat =
|
||||||
|
|
@ -38,6 +41,18 @@ proc newParser*(tokStream: var nlTokStream): nlParser =
|
||||||
bnode: rootNode,
|
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
|
#[ "Greed" refers to something I mentioned in my discussion on
|
||||||
| Noether's grammar (in an EBNF-like language). Greed just
|
| 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!!)
|
# Returns false if the greed was never satisfied (OMG!!)
|
||||||
proc greed(parser: var nlParser,
|
proc greed(parser: var nlParser,
|
||||||
satisfy: proc(tok: nlTok): bool): nlParseStat =
|
satisfy: proc(tok: nlTok): bool): nlParseStat =
|
||||||
while parser.stream.progress():
|
while parser.progressStream():
|
||||||
if satisfy(parser.stream.currTok):
|
if satisfy(parser.currTok):
|
||||||
return nlParseStat.OK
|
return nlParseStat.OK
|
||||||
# NOTE: the matched token is currently excluded
|
# NOTE: the matched token is currently excluded
|
||||||
parser.bnode.addTok(parser.stream.currTok)
|
parser.bnode.addTok(parser.currTok)
|
||||||
result = nlParseStat.UNMATCHED
|
result = nlParseStat.UNMATCHED
|
||||||
|
|
||||||
proc greedLine(parser: var nlParser,
|
proc greedLine(parser: var nlParser,
|
||||||
satisfy: proc(tok: nlTok): bool): nlParseStat =
|
satisfy: proc(tok: nlTok): bool): nlParseStat =
|
||||||
while parser.stream.progress():
|
while parser.progressStream():
|
||||||
if satisfy(parser.stream.currTok):
|
if satisfy(parser.currTok):
|
||||||
return nlParseStat.OK
|
return nlParseStat.OK
|
||||||
# NOTE: the matched token is currently excluded
|
# NOTE: the matched token is currently excluded
|
||||||
parser.bnode.addTok(parser.stream.currTok)
|
parser.bnode.addTok(parser.currTok)
|
||||||
if parser.stream.currTok.tKind == tkEOL:
|
if parser.currTok.tKind == tkEOL:
|
||||||
return nlParseStat.UNMATCHED
|
return nlParseStat.UNMATCHED
|
||||||
result = nlParseStat.UNMATCHED
|
result = nlParseStat.UNMATCHED
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue