colonCall parsing
This commit is contained in:
parent
f721d0987d
commit
b858a066e2
|
@ -162,14 +162,40 @@ proc primary(parser: Parser): Node =
|
||||||
|
|
||||||
parser.errorAtCurrent("Primary expected, but something else found.")
|
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()
|
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:
|
if parser.previous.tokenType == tkLeftBracket:
|
||||||
let index = parser.expression()
|
let index = parser.expression()
|
||||||
parser.consume(tkRightBracket, "']' after index.")
|
parser.consume(tkRightBracket, "']' after index.")
|
||||||
result = Node(kind: nkGetIndex, gCollection: result, gIndex: 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:
|
else:
|
||||||
# dot
|
# dot
|
||||||
parser.consume(tkIdentifier, "Identifier expected after '.' index operator.")
|
parser.consume(tkIdentifier, "Identifier expected after '.' index operator.")
|
||||||
|
@ -178,13 +204,7 @@ proc parseIndex(parser:Parser): Node =
|
||||||
proc parseCall(parser: Parser): Node =
|
proc parseCall(parser: Parser): Node =
|
||||||
result = parser.parseIndex()
|
result = parser.parseIndex()
|
||||||
if parser.match(tkLeftParen):
|
if parser.match(tkLeftParen):
|
||||||
var args: seq[Node] = @[]
|
let args = parser.parseArgList()
|
||||||
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.")
|
|
||||||
result = Node(kind: nkCall, arguments: args, function: result)
|
result = Node(kind: nkCall, arguments: args, function: result)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue