[ \t\r\n] { /* whitespace is insignificant */ }
#.*[\r\n] { /* skip line comments */ }
-<<EOF>> { return T_END_FILE; }
-
"package" { return T_PACKAGE; }
"requires" { return T_REQUIRES; }
"provides" { return T_PROVIDES; }
return T_ID;
}
+<<EOF>> { return T_END_FILE; }
+
. { return T_ERROR; }
%%
static AST* definition_list(Parser* p) {
while (!matches(p, T_END_FILE)) {
TokType type = peek(p)->type;
- if (accept(p, T_LET) || accept(p, T_VAR)) {
+ if (matches(p, T_LET) || matches(p, T_VAR)) {
const_definition(p, (type == T_LET));
- } else if (accept(p, T_TYPE)) {
+ } else if (matches(p, T_TYPE)) {
type_definition(p);
- } else if (accept(p, T_FUN)) {
+ } else if (matches(p, T_FUN)) {
func_definition(p);
} else {
error(p, "only definitions are allowed at the toplevel");
}
static AST* const_definition(Parser* p, bool constant) {
+ if (!accept(p, T_LET) && !accept(p, T_VAR))
+ error(p, "constant or variable definition expected");
Tok* id = expect_val(p, T_ID);
Type* type = type_annotation(p);
expect(p, T_ASSIGN);
}
static AST* func_definition(Parser* p) {
+ expect(p, T_FUN);
+ expect(p, T_ID);
+ expect(p, T_LPAR);
+ expect(p, T_RPAR);
+ type_expression(p);
+ expect(p, T_LBRACE);
+ expect(p, T_RBRACE);
return NULL;
}