add synchronization to the parser

This commit is contained in:
Luna 2019-09-20 12:22:46 -03:00
parent 084c5cc6ee
commit 7ddd37b725
2 changed files with 41 additions and 24 deletions

View File

@ -38,10 +38,12 @@ pub fn run(allocator: *std.mem.Allocator, slice: []const u8) !Result {
var parser = parsers.Parser.init(allocator, &scan); var parser = parsers.Parser.init(allocator, &scan);
var root = try parser.parse(); var root = try parser.parse();
var it = root.Root.iterator(); if (root == null) {
return Result.ParseError;
}
std.debug.warn("parse tree\n"); std.debug.warn("parse tree\n");
//printer.printNode(root, 0); printer.printNode(root.?, 0);
return Result.Ok; return Result.Ok;
} }

View File

@ -3,7 +3,7 @@ const scanners = @import("scanners.zig");
const main = @import("main.zig"); const main = @import("main.zig");
const ast = @import("ast.zig"); const ast = @import("ast.zig");
const tokens = @import("tokens.zig"); const tokens = @import("tokens.zig");
const err = @import("errors.zig"); const ereport = @import("errors.zig");
const printer = @import("ast_printer.zig"); const printer = @import("ast_printer.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
@ -44,12 +44,31 @@ pub const Parser = struct {
self.tokens.deinit(); self.tokens.deinit();
} }
fn doError(self: *Parser, comptime fmt: []const u8, args: ...) void { fn doError(self: *Parser, comptime fmt: []const u8, args: ...) ParseError {
self.hadError = true; self.hadError = true;
std.debug.warn("parser error at line {}\n\t", self.scanner.line); std.debug.warn("parser error at line {}\n\t", self.scanner.line);
std.debug.warn(fmt, args); std.debug.warn(fmt, args);
std.debug.warn("\n"); std.debug.warn("\n");
return ParseError.CompileError;
}
fn synchronize(self: *Parser) !void {
// TODO is it nextToken()?
_ = try self.nextToken();
while (!self.isAtEnd()) {
if (self.previous().typ == .Semicolon) return;
// TODO add more "stmt"-"starting" tokens here
switch (self.peek().typ) {
.Fn => return,
else => {},
}
_ = try self.nextToken();
}
} }
fn peek(self: *Parser) Token { fn peek(self: *Parser) Token {
@ -60,11 +79,11 @@ pub const Parser = struct {
return self.tokens.at(self.tokens.len - 2); return self.tokens.at(self.tokens.len - 2);
} }
fn tokenError(self: *Parser, token: Token, msg: []const u8) ParseError!void { fn tokenError(self: *Parser, token: Token, msg: []const u8) ParseError {
if (token.typ == .EOF) { if (token.typ == .EOF) {
err.report(token.line, " at end", msg); ereport.report(token.line, " at end", msg);
} else { } else {
err.reportFmt(token.line, " at '{}': {}", token.lexeme, msg); ereport.reportFmt(token.line, " at '{}': {}", token.lexeme, msg);
} }
return ParseError.CompileError; return ParseError.CompileError;
@ -104,8 +123,7 @@ pub const Parser = struct {
return tok; return tok;
} }
try self.tokenError(self.peek(), msg); return self.tokenError(self.peek(), msg);
return ParseError.CompileError;
} }
/// Consume the current token. Gives default error messages /// Consume the current token. Gives default error messages
@ -125,8 +143,7 @@ pub const Parser = struct {
self.peek().typ, self.peek().typ,
); );
try self.tokenError(self.peek(), buf); return self.tokenError(self.peek(), buf);
return ParseError.CompileError;
} }
/// check() against multiple tokens /// check() against multiple tokens
@ -369,7 +386,7 @@ pub const Parser = struct {
return expr; return expr;
} }
pub fn parse(self: *Parser) !*ast.Node { pub fn parse(self: *Parser) !?*ast.Node {
var root = try Node.mkRoot(self.allocator); var root = try Node.mkRoot(self.allocator);
var token_opt: ?Token = null; var token_opt: ?Token = null;
@ -384,7 +401,10 @@ pub const Parser = struct {
var token = token_opt.?; var token = token_opt.?;
if (token.typ == .EOF) break; if (token.typ == .EOF) break;
var node = try self.parseTopDecl(); var node = self.parseTopDecl() catch |err| {
if (err == ParseError.CompileError) return null;
return err;
};
try root.Root.append(node.*); try root.Root.append(node.*);
} }
@ -610,8 +630,7 @@ pub const Parser = struct {
.Enum => try self.parseEnumDecl(), .Enum => try self.parseEnumDecl(),
else => |typ| blk: { else => |typ| blk: {
self.doError("expected Fn, Const, Struct, got {}\n", typ); return self.doError("expected Fn, Const, Struct, got {}\n", typ);
return ParseError.CompileError;
}, },
}; };
} }
@ -840,8 +859,7 @@ pub const Parser = struct {
.Get => |get| { .Get => |get| {
switch (op.typ) { switch (op.typ) {
.ColonEqual => { .ColonEqual => {
self.doError("can not initialize struct field"); return self.doError("can not initialize struct field");
return ParseError.CompileError;
}, },
.Equal => return try self.mkSet(get.struc, get.name, value), .Equal => return try self.mkSet(get.struc, get.name, value),
@ -860,8 +878,7 @@ pub const Parser = struct {
}, },
else => |expr_typ| { else => |expr_typ| {
self.doError("Invalid assignment target {}", expr_typ); return self.doError("Invalid assignment target {}", expr_typ);
return ParseError.CompileError;
}, },
} }
} }
@ -997,7 +1014,7 @@ pub const Parser = struct {
expr = try self.mkGet(expr, name); expr = try self.mkGet(expr, name);
} }
} else { } else {
break; return self.tokenError(self.peek(), "Expect expression.");
} }
} }
@ -1031,8 +1048,7 @@ pub const Parser = struct {
// <expr>{a: 10 b: 10} // <expr>{a: 10 b: 10}
// for this to work properly, <expr> must be Variable, since its a type. // for this to work properly, <expr> must be Variable, since its a type.
if (ast.ExprType(expr.*) != .Variable) { if (ast.ExprType(expr.*) != .Variable) {
self.doError("Expected variable for struct type, got {}", ast.ExprType(expr.*)); return self.doError("Expected variable for struct type, got {}", ast.ExprType(expr.*));
return ParseError.CompileError;
} }
var inits = ast.StructInitList.init(self.allocator); var inits = ast.StructInitList.init(self.allocator);
@ -1096,8 +1112,7 @@ pub const Parser = struct {
}, },
else => blk: { else => blk: {
self.doError("expected literal, got {}", curtype); return self.doError("expected literal, got {}", curtype);
return ParseError.CompileError;
}, },
}; };