colonCall parsing

This commit is contained in:
prod2 2022-12-02 19:27:02 +01:00
parent f721d0987d
commit b858a066e2
1 changed files with 29 additions and 9 deletions

View File

@ -162,14 +162,40 @@ proc primary(parser: Parser): Node =
parser.errorAtCurrent("Primary expected, but something else found.")
proc parseIndex(parser:Parser): Node =
proc parseArgList(parser: Parser): seq[Node] =
# once ( has been consumed, consume args and the ) or just a )
var args: seq[Node] = @[]
while not parser.isAtEnd() and not parser.peekMatch(tkRightParen):
let arg = parser.expression()
if not parser.isAtEnd() and not parser.peekMatch(tkRightParen):
parser.consume(tkComma, "',' expected between arguments.")
args.add(arg)
parser.consume(tkRightParen, "')' expected after argument list.")
return args
proc parseIndex(parser: Parser): Node =
result = parser.primary()
while parser.match({tkLeftBracket, tkDot}):
while parser.match({tkLeftBracket, tkDot, tkIdentifier}):
# NOTE: :index is counted as a single identifier, so two identifiers after eachother will be handled here
if parser.previous.tokenType == tkLeftBracket:
let index = parser.expression()
parser.consume(tkRightBracket, "']' after index.")
result = Node(kind: nkGetIndex, gCollection: result, gIndex: index)
elif parser.previous.tokenType == tkIdentifier:
let identText = parser.previous.text
if identText[0] != ':':
parser.errorAtCurrent("';' expected after expression statement.")
# update this with whatever the original error when two idents follow eachother is
return
let ident = Node(kind: nkConst, constant: identText[1..^1].fromNimString())
# ident removes the : from it
var args: seq[Node] = @[]
if parser.match(tkLeftParen):
args = parser.parseArgList()
let funct = Node(kind: nkGetIndex, gCollection: result, gIndex: ident)
result = Node(kind: nkColonCall, arguments: args, function: funct)
else:
# dot
parser.consume(tkIdentifier, "Identifier expected after '.' index operator.")
@ -178,13 +204,7 @@ proc parseIndex(parser:Parser): Node =
proc parseCall(parser: Parser): Node =
result = parser.parseIndex()
if parser.match(tkLeftParen):
var args: seq[Node] = @[]
while not parser.isAtEnd() and not parser.peekMatch(tkRightParen):
let arg = parser.expression()
if not parser.isAtEnd() and not parser.peekMatch(tkRightParen):
parser.consume(tkComma, "',' expected between arguments.")
args.add(arg)
parser.consume(tkRightParen, "')' expected after argument list.")
let args = parser.parseArgList()
result = Node(kind: nkCall, arguments: args, function: result)