parser: fix bug where comma was not required in function call

This commit is contained in:
Alex Zenla 2023-09-11 05:17:55 -04:00
parent 24d2ff5743
commit dbe4e7d5a5
Signed by: alex
GPG Key ID: C0780728420EBFE5

View File

@ -60,7 +60,11 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
private fun readSymbolCases(): Expression = within { private fun readSymbolCases(): Expression = within {
val symbol = readSymbolRaw() val symbol = readSymbolRaw()
if (next(TokenType.LeftParentheses)) { if (next(TokenType.LeftParentheses)) {
val arguments = collect(TokenType.RightParentheses, TokenType.Comma) { val arguments = collect(
TokenType.RightParentheses,
TokenType.Comma,
forceConsumeExceptLast = true
) {
readExpression() readExpression()
} }
expect(TokenType.RightParentheses) expect(TokenType.RightParentheses)
@ -359,13 +363,18 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
private fun <T> collect( private fun <T> collect(
peeking: TokenType, peeking: TokenType,
consuming: TokenType? = null, consuming: TokenType? = null,
forceConsumeExceptLast: Boolean = false,
read: () -> T read: () -> T
): List<T> { ): List<T> {
val items = mutableListOf<T>() val items = mutableListOf<T>()
while (!peek(peeking)) { while (!peek(peeking)) {
val item = read() val item = read()
if (consuming != null) { if (consuming != null) {
next(consuming) if (!next(consuming)) {
if (!peek(peeking)) {
expect(consuming)
}
}
} }
items.add(item) items.add(item)
} }