2023-06-28 18:12:14 +00:00
|
|
|
namespace Finn;
|
|
|
|
|
|
|
|
using Finn.AST;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
class Parser
|
|
|
|
{
|
|
|
|
private class ParseError : Exception { }
|
|
|
|
|
|
|
|
private readonly List<Token> tokens;
|
|
|
|
private int current = 0;
|
|
|
|
|
|
|
|
public Parser(List<Token> tokens)
|
|
|
|
{
|
|
|
|
this.tokens = tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Expr? parse()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return expression();
|
|
|
|
}
|
|
|
|
catch (ParseError)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsing primitives
|
|
|
|
|
|
|
|
private bool match(params TokenType[] types)
|
|
|
|
{
|
|
|
|
foreach (TokenType type in types)
|
|
|
|
{
|
|
|
|
if (check(type))
|
|
|
|
{
|
|
|
|
advance();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-02 17:38:48 +00:00
|
|
|
private bool check(params TokenType[] types)
|
2023-06-28 18:12:14 +00:00
|
|
|
{
|
|
|
|
if (isAtEnd()) return false;
|
2023-07-25 19:19:07 +00:00
|
|
|
return Array.IndexOf(types, peek().Type) >= 0;
|
2023-06-28 18:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Token advance()
|
|
|
|
{
|
|
|
|
if (!isAtEnd()) current++;
|
|
|
|
return previous();
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool isAtEnd()
|
|
|
|
{
|
2023-07-25 19:19:07 +00:00
|
|
|
return peek().Type == TokenType.EOF;
|
2023-06-28 18:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Token peek()
|
|
|
|
{
|
|
|
|
return tokens[current];
|
|
|
|
}
|
|
|
|
|
|
|
|
private Token previous()
|
|
|
|
{
|
|
|
|
return tokens[current - 1];
|
|
|
|
}
|
|
|
|
|
2023-07-17 05:30:20 +00:00
|
|
|
private Token consume(string message, params TokenType[] types)
|
2023-06-28 18:12:14 +00:00
|
|
|
{
|
2023-07-17 05:30:20 +00:00
|
|
|
if (check(types)) return advance();
|
2023-06-28 18:12:14 +00:00
|
|
|
throw error(peek(), message);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ParseError error(Token token, string message)
|
|
|
|
{
|
|
|
|
Program.error(token, message);
|
|
|
|
return new ParseError();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void synchronize()
|
|
|
|
{
|
|
|
|
advance();
|
|
|
|
|
|
|
|
while (!isAtEnd())
|
|
|
|
{
|
2023-07-25 19:19:07 +00:00
|
|
|
switch (peek().Type)
|
2023-06-28 18:12:14 +00:00
|
|
|
{
|
|
|
|
case TokenType.Def:
|
|
|
|
case TokenType.Type:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
advance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
|
|
|
private Expr binaryLeft(Func<Expr> next, params TokenType[] types)
|
|
|
|
{
|
|
|
|
Expr expr = next();
|
|
|
|
|
|
|
|
while (match(types))
|
|
|
|
{
|
|
|
|
Token op = previous();
|
|
|
|
Expr right = next();
|
2023-07-02 20:27:12 +00:00
|
|
|
expr = new Binary(expr, op, right);
|
2023-06-28 18:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rules
|
|
|
|
|
|
|
|
private Expr expression()
|
|
|
|
{
|
2023-06-28 23:18:31 +00:00
|
|
|
var expr = equality();
|
|
|
|
while (match(TokenType.Semicolon))
|
|
|
|
{
|
|
|
|
var right = equality();
|
2023-07-02 20:27:12 +00:00
|
|
|
expr = new Sequence(expr, right);
|
2023-06-28 23:18:31 +00:00
|
|
|
}
|
|
|
|
return expr;
|
2023-06-28 18:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-02 20:27:12 +00:00
|
|
|
private Expr equality() =>
|
|
|
|
binaryLeft(comparison, TokenType.BangEqual, TokenType.DoubleEqual);
|
2023-06-28 18:12:14 +00:00
|
|
|
|
|
|
|
private Expr comparison() =>
|
|
|
|
binaryLeft(sum, TokenType.Greater, TokenType.GreaterEqual, TokenType.Less, TokenType.LessEqual);
|
|
|
|
|
|
|
|
private Expr sum() => binaryLeft(product, TokenType.Minus, TokenType.Plus);
|
|
|
|
|
2023-06-28 22:38:00 +00:00
|
|
|
private Expr product() => binaryLeft(concat, TokenType.Slash, TokenType.Asterisk);
|
|
|
|
|
|
|
|
private Expr concat() => binaryLeft(unary, TokenType.PlusPlus);
|
2023-06-28 22:26:08 +00:00
|
|
|
|
2023-06-28 18:12:14 +00:00
|
|
|
private Expr unary()
|
|
|
|
{
|
|
|
|
if (match(TokenType.Bang, TokenType.Minus))
|
|
|
|
{
|
|
|
|
Token op = previous();
|
|
|
|
Expr right = unary();
|
2023-07-02 20:27:12 +00:00
|
|
|
return new Unary(op, right);
|
2023-06-28 18:12:14 +00:00
|
|
|
}
|
2023-07-02 22:37:06 +00:00
|
|
|
return let();
|
|
|
|
}
|
|
|
|
|
|
|
|
private RecordPattern? recordPattern()
|
|
|
|
{
|
|
|
|
if (!match(TokenType.LBrace))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-02 23:07:46 +00:00
|
|
|
|
|
|
|
List<FieldPattern> fields = new List<FieldPattern>();
|
|
|
|
while (!check(TokenType.RBrace, TokenType.Pipe))
|
|
|
|
{
|
2023-07-17 05:30:20 +00:00
|
|
|
var fieldName = consume("Expect identifier as field name.", TokenType.Identifier, TokenType.QuotedIdentifier);
|
2023-07-02 23:07:46 +00:00
|
|
|
var pat = match(TokenType.Equal) ? pattern() : null;
|
|
|
|
fields.Add(new(fieldName, pat));
|
|
|
|
if (!match(TokenType.Comma))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var restPattern = match(TokenType.Pipe) ? simplePattern() : null;
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect '}' at end of record pattern.", TokenType.RBrace);
|
2023-07-02 23:07:46 +00:00
|
|
|
return new(fields.ToArray(), restPattern);
|
2023-07-02 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private VariantPattern? variantPattern()
|
|
|
|
{
|
|
|
|
if (!match(TokenType.Backtick))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-17 05:30:20 +00:00
|
|
|
var tag = consume("Expect identifier as tag name.", TokenType.Identifier, TokenType.QuotedIdentifier);
|
2023-07-02 22:37:06 +00:00
|
|
|
if (!match(TokenType.LParen))
|
|
|
|
{
|
|
|
|
return new(tag, null);
|
|
|
|
}
|
|
|
|
Pattern argument = pattern();
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect ')' after variant argument.", TokenType.RParen);
|
2023-07-02 22:37:06 +00:00
|
|
|
return new(tag, argument);
|
|
|
|
}
|
|
|
|
|
2023-07-02 23:07:46 +00:00
|
|
|
private SimplePattern? simplePattern()
|
2023-07-02 22:37:06 +00:00
|
|
|
{
|
|
|
|
if (match(TokenType.Blank))
|
|
|
|
{
|
|
|
|
return new SimplePattern(null);
|
|
|
|
}
|
|
|
|
|
2023-07-17 05:30:20 +00:00
|
|
|
return match(TokenType.Identifier, TokenType.QuotedIdentifier) ? new(previous()) : null;
|
2023-07-02 23:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Pattern pattern()
|
|
|
|
{
|
|
|
|
Pattern? p;
|
|
|
|
if ((p = simplePattern()) != null)
|
|
|
|
{
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
if ((p = recordPattern()) != null)
|
2023-07-02 22:37:06 +00:00
|
|
|
{
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
if ((p = variantPattern()) != null)
|
|
|
|
{
|
|
|
|
return p;
|
|
|
|
}
|
2023-07-02 23:07:46 +00:00
|
|
|
if (check(TokenType.String))
|
|
|
|
{
|
|
|
|
throw error(peek(), "Cannot pattern-match string literals.");
|
|
|
|
}
|
|
|
|
if (check(TokenType.Number))
|
|
|
|
{
|
|
|
|
throw error(peek(), "Cannot pattern-match numbers.");
|
|
|
|
}
|
|
|
|
throw error(peek(), "Expect pattern.");
|
2023-07-02 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Expr let()
|
|
|
|
{
|
|
|
|
if (!match(TokenType.Let))
|
|
|
|
{
|
2023-07-05 14:52:14 +00:00
|
|
|
return ifExpr();
|
2023-07-02 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List<Binding> bindings = new List<Binding>();
|
2023-07-05 16:18:27 +00:00
|
|
|
bindings.Add(parseBinding());
|
2023-07-02 22:37:06 +00:00
|
|
|
while (match(TokenType.And))
|
|
|
|
{
|
2023-07-05 16:18:27 +00:00
|
|
|
bindings.Add(parseBinding());
|
2023-07-02 22:37:06 +00:00
|
|
|
}
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect 'in' after let-bindings.", TokenType.In);
|
2023-07-02 22:37:06 +00:00
|
|
|
Expr body = expression();
|
|
|
|
return new Let(bindings.ToArray(), body);
|
2023-07-05 16:18:27 +00:00
|
|
|
|
|
|
|
Binding parseBinding()
|
|
|
|
{
|
|
|
|
Pattern p = pattern();
|
|
|
|
switch (p)
|
|
|
|
{
|
|
|
|
case (SimplePattern(var funcName)) when funcName != null && match(TokenType.LParen):
|
2023-07-05 16:23:34 +00:00
|
|
|
List<Pattern> funcParams = new List<Pattern>();
|
2023-07-05 16:18:27 +00:00
|
|
|
while (!check(TokenType.RParen))
|
|
|
|
{
|
2023-07-05 16:23:34 +00:00
|
|
|
funcParams.Add(pattern());
|
2023-07-05 16:18:27 +00:00
|
|
|
if (!match(TokenType.Comma))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect ')' at end of parameters.", TokenType.RParen);
|
|
|
|
consume("Expect '=' after parameters.", TokenType.Equal);
|
2023-07-05 16:18:27 +00:00
|
|
|
return new FuncBinding(funcName, funcParams.ToArray(), expression());
|
|
|
|
default:
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect '=' after pattern.", TokenType.Equal);
|
2023-07-05 16:18:27 +00:00
|
|
|
return new VarBinding(p, expression());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-28 22:26:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 14:52:14 +00:00
|
|
|
private Expr ifExpr()
|
2023-06-28 23:25:28 +00:00
|
|
|
{
|
2023-07-05 14:52:14 +00:00
|
|
|
if (!match(TokenType.If))
|
2023-06-28 23:25:28 +00:00
|
|
|
{
|
2023-07-05 14:52:14 +00:00
|
|
|
return when();
|
|
|
|
}
|
|
|
|
Expr condition = expression();
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect 'then' after condition.", TokenType.Then);
|
2023-07-05 14:52:14 +00:00
|
|
|
Expr thenCase = expression();
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect 'else' after 'then' case.", TokenType.Else);
|
2023-07-05 14:52:14 +00:00
|
|
|
Expr elseCase = expression();
|
|
|
|
return new If(condition, thenCase, elseCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Expr when()
|
|
|
|
{
|
|
|
|
if (!match(TokenType.When))
|
|
|
|
{
|
|
|
|
return primary();
|
2023-06-28 23:25:28 +00:00
|
|
|
}
|
2023-07-05 14:52:14 +00:00
|
|
|
Expr head = expression();
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect 'is' after expression.", TokenType.Is);
|
2023-06-28 23:25:28 +00:00
|
|
|
|
2023-07-11 19:55:06 +00:00
|
|
|
List<VarBinding> cases = new List<VarBinding>();
|
2023-07-05 14:52:14 +00:00
|
|
|
cases.Add(parseCase());
|
|
|
|
while (match(TokenType.Comma))
|
|
|
|
{
|
|
|
|
cases.Add(parseCase());
|
|
|
|
}
|
|
|
|
return new When(head, cases.ToArray());
|
|
|
|
|
2023-07-11 19:55:06 +00:00
|
|
|
VarBinding parseCase()
|
2023-07-05 14:52:14 +00:00
|
|
|
{
|
|
|
|
Pattern pat = pattern();
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect '=>' after pattern.", TokenType.DoubleArrow);
|
2023-07-05 14:52:14 +00:00
|
|
|
Expr value = expression();
|
2023-07-05 16:18:27 +00:00
|
|
|
return new VarBinding(pat, value);
|
2023-07-05 14:52:14 +00:00
|
|
|
}
|
2023-06-28 23:25:28 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 18:12:14 +00:00
|
|
|
private Expr primary()
|
2023-07-01 06:12:02 +00:00
|
|
|
{
|
|
|
|
Expr expr = operand();
|
2023-07-01 06:26:32 +00:00
|
|
|
|
2023-07-01 06:12:02 +00:00
|
|
|
if (match(TokenType.Period))
|
|
|
|
{
|
2023-07-17 05:30:20 +00:00
|
|
|
var ident = consume("Expect identifier after dot.", TokenType.Identifier, TokenType.QuotedIdentifier);
|
2023-07-02 20:27:12 +00:00
|
|
|
return new Selector(expr, ident);
|
2023-07-01 06:12:02 +00:00
|
|
|
}
|
2023-07-01 06:26:32 +00:00
|
|
|
|
2023-07-01 06:12:02 +00:00
|
|
|
if (match(TokenType.LBracket))
|
|
|
|
{
|
2023-07-02 17:46:18 +00:00
|
|
|
var index = expression();
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect '[' after expression.", TokenType.RBracket);
|
2023-07-02 20:27:12 +00:00
|
|
|
return new Indexer(expr, index);
|
2023-07-01 06:12:02 +00:00
|
|
|
}
|
2023-07-01 06:26:32 +00:00
|
|
|
|
2023-07-01 06:12:02 +00:00
|
|
|
if (match(TokenType.LParen))
|
|
|
|
{
|
2023-07-02 18:05:46 +00:00
|
|
|
var args = new List<Expr?>();
|
|
|
|
while (!check(TokenType.RParen))
|
|
|
|
{
|
|
|
|
if (match(TokenType.Blank))
|
|
|
|
{
|
|
|
|
args.Add(null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
args.Add(expression());
|
|
|
|
}
|
|
|
|
if (!match(TokenType.Comma))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect ')' after arguments.", TokenType.RParen);
|
2023-07-02 20:27:12 +00:00
|
|
|
return new Call(expr, args.ToArray());
|
2023-07-01 06:12:02 +00:00
|
|
|
}
|
2023-07-01 06:26:32 +00:00
|
|
|
|
2023-07-01 06:12:02 +00:00
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Expr operand()
|
2023-06-28 18:12:14 +00:00
|
|
|
{
|
|
|
|
if (match(TokenType.Number, TokenType.String))
|
|
|
|
{
|
2023-07-25 19:19:07 +00:00
|
|
|
object literal = previous().Literal!;
|
|
|
|
return new Literal(previous().Literal!);
|
2023-06-28 18:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 05:30:20 +00:00
|
|
|
if (match(TokenType.Identifier, TokenType.QuotedIdentifier))
|
2023-07-01 06:12:02 +00:00
|
|
|
{
|
2023-07-17 05:30:20 +00:00
|
|
|
return new Variable(previous());
|
2023-07-01 06:12:02 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 18:12:14 +00:00
|
|
|
if (match(TokenType.LParen))
|
|
|
|
{
|
2023-07-02 05:13:24 +00:00
|
|
|
Expr groupedExpr = expression();
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect ')' after expression.", TokenType.RParen);
|
2023-07-02 20:27:12 +00:00
|
|
|
return new Grouping(groupedExpr);
|
2023-06-28 18:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-01 06:26:32 +00:00
|
|
|
Expr? expr;
|
|
|
|
if ((expr = record()) != null)
|
|
|
|
{
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
if ((expr = variant()) != null)
|
|
|
|
{
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
if ((expr = list()) != null)
|
|
|
|
{
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2023-06-28 18:12:14 +00:00
|
|
|
throw error(peek(), "Expect expression.");
|
|
|
|
}
|
2023-07-01 06:26:32 +00:00
|
|
|
|
|
|
|
private Expr? list()
|
|
|
|
{
|
2023-07-02 05:13:24 +00:00
|
|
|
if (!match(TokenType.LBracket))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Expr> elements = new List<Expr>();
|
|
|
|
|
|
|
|
if (!match(TokenType.RBracket))
|
|
|
|
{
|
|
|
|
elements.Add(expression());
|
|
|
|
while (!match(TokenType.RBracket))
|
|
|
|
{
|
|
|
|
if (match(TokenType.Comma))
|
|
|
|
{
|
|
|
|
if (match(TokenType.RBracket))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
elements.Add(expression());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw error(previous(), "Expect comma between list elements.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-02 20:27:12 +00:00
|
|
|
return new Finn.AST.List(elements.ToArray());
|
2023-07-01 06:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Expr? variant()
|
|
|
|
{
|
2023-07-02 05:30:24 +00:00
|
|
|
if (!match(TokenType.Backtick))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-17 05:30:20 +00:00
|
|
|
var tag = consume("Expect identifier as tag name.", TokenType.QuotedIdentifier, TokenType.Identifier);
|
2023-07-02 05:30:24 +00:00
|
|
|
Expr? argument = null;
|
|
|
|
if (match(TokenType.LParen))
|
|
|
|
{
|
|
|
|
if (!match(TokenType.RParen))
|
|
|
|
{
|
|
|
|
argument = expression();
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect ')' after variant argument.", TokenType.RParen);
|
2023-07-02 05:30:24 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-02 20:27:12 +00:00
|
|
|
return new Variant(tag, argument);
|
2023-07-01 06:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Expr? record()
|
|
|
|
{
|
2023-07-02 17:38:48 +00:00
|
|
|
if (!match(TokenType.LBrace))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var extensions = parseFields(TokenType.RBrace, TokenType.Pipe);
|
|
|
|
BaseRecord? baseRecord = null;
|
|
|
|
|
|
|
|
if (match(TokenType.Pipe))
|
|
|
|
{
|
|
|
|
var baseExpr = expression();
|
|
|
|
var updates = match(TokenType.With) ? parseFields(TokenType.RBrace) : Array.Empty<Field>();
|
|
|
|
baseRecord = new(baseExpr, updates);
|
|
|
|
}
|
|
|
|
|
2023-07-17 05:30:20 +00:00
|
|
|
consume("Expect '}' at end of record literal.", TokenType.RBrace);
|
2023-07-02 20:27:12 +00:00
|
|
|
return new Record(extensions, baseRecord);
|
2023-07-02 17:38:48 +00:00
|
|
|
|
|
|
|
Field[] parseFields(params TokenType[] endAt)
|
|
|
|
{
|
|
|
|
List<Field> fields = new List<Field>();
|
|
|
|
|
|
|
|
while (!check(endAt))
|
|
|
|
{
|
2023-07-17 05:30:20 +00:00
|
|
|
var fieldName = consume("Expect identifier as field name.", TokenType.Identifier, TokenType.QuotedIdentifier);
|
2023-07-02 17:38:48 +00:00
|
|
|
var value = match(TokenType.Equal) ? expression() : null;
|
|
|
|
fields.Add(new(fieldName, value));
|
|
|
|
if (!match(TokenType.Comma))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fields.ToArray();
|
|
|
|
}
|
2023-07-01 06:26:32 +00:00
|
|
|
}
|
2023-06-28 18:12:14 +00:00
|
|
|
}
|