Skip to content

Commit

Permalink
Merge branch 'rework-parser'
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed May 10, 2018
2 parents bf21747 + bbae626 commit 4438c5e
Show file tree
Hide file tree
Showing 12 changed files with 5,467 additions and 5,114 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Expand Up @@ -576,7 +576,8 @@ set(ZIG_STD_FILES
"unicode.zig"
"zig/ast.zig"
"zig/index.zig"
"zig/parser.zig"
"zig/parse.zig"
"zig/render.zig"
"zig/tokenizer.zig"
)

Expand Down
47 changes: 29 additions & 18 deletions src-self-hosted/main.zig
Expand Up @@ -671,34 +671,45 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {
};
defer allocator.free(source_code);

var tokenizer = std.zig.Tokenizer.init(source_code);
var parser = std.zig.Parser.init(&tokenizer, allocator, file_path);
defer parser.deinit();

var tree = parser.parse() catch |err| {
var tree = std.zig.parse(allocator, source_code) catch |err| {
try stderr.print("error parsing file '{}': {}\n", file_path, err);
continue;
};
defer tree.deinit();

var original_file_backup = try Buffer.init(allocator, file_path);
defer original_file_backup.deinit();
try original_file_backup.append(".backup");

try os.rename(allocator, file_path, original_file_backup.toSliceConst());
var error_it = tree.errors.iterator(0);
while (error_it.next()) |parse_error| {
const token = tree.tokens.at(parse_error.loc());
const loc = tree.tokenLocation(0, parse_error.loc());
try stderr.print("{}:{}:{}: error: ", file_path, loc.line + 1, loc.column + 1);
try tree.renderError(parse_error, stderr);
try stderr.print("\n{}\n", source_code[loc.line_start..loc.line_end]);
{
var i: usize = 0;
while (i < loc.column) : (i += 1) {
try stderr.write(" ");
}
}
{
const caret_count = token.end - token.start;
var i: usize = 0;
while (i < caret_count) : (i += 1) {
try stderr.write("~");
}
}
try stderr.write("\n");
}
if (tree.errors.len != 0) {
continue;
}

try stderr.print("{}\n", file_path);

// TODO: BufferedAtomicFile has some access problems.
var out_file = try os.File.openWrite(allocator, file_path);
defer out_file.close();
const baf = try io.BufferedAtomicFile.create(allocator, file_path);
defer baf.destroy();

var out_file_stream = io.FileOutStream.init(&out_file);
try parser.renderSource(out_file_stream.stream, tree.root_node);

if (!flags.present("keep-backups")) {
try os.deleteFile(allocator, original_file_backup.toSliceConst());
}
try std.zig.render(allocator, baf.stream(), &tree);
}
}

Expand Down
23 changes: 2 additions & 21 deletions src-self-hosted/module.zig
Expand Up @@ -8,9 +8,7 @@ const c = @import("c.zig");
const builtin = @import("builtin");
const Target = @import("target.zig").Target;
const warn = std.debug.warn;
const Tokenizer = std.zig.Tokenizer;
const Token = std.zig.Token;
const Parser = std.zig.Parser;
const ArrayList = std.ArrayList;

pub const Module = struct {
Expand Down Expand Up @@ -246,34 +244,17 @@ pub const Module = struct {

warn("{}", source_code);

warn("====tokenization:====\n");
{
var tokenizer = Tokenizer.init(source_code);
while (true) {
const token = tokenizer.next();
tokenizer.dump(token);
if (token.id == Token.Id.Eof) {
break;
}
}
}

warn("====parse:====\n");

var tokenizer = Tokenizer.init(source_code);
var parser = Parser.init(&tokenizer, self.allocator, root_src_real_path);
defer parser.deinit();

var tree = try parser.parse();
var tree = try std.zig.parse(self.allocator, source_code);
defer tree.deinit();

var stderr_file = try std.io.getStdErr();
var stderr_file_out_stream = std.io.FileOutStream.init(&stderr_file);
const out_stream = &stderr_file_out_stream.stream;
try parser.renderAst(out_stream, tree.root_node);

warn("====fmt:====\n");
try parser.renderSource(out_stream, tree.root_node);
try std.zig.render(self.allocator, out_stream, &tree);

warn("====ir:====\n");
warn("TODO\n\n");
Expand Down
2 changes: 1 addition & 1 deletion src/ir.cpp
Expand Up @@ -14709,7 +14709,7 @@ static IrInstruction *ir_analyze_union_tag(IrAnalyze *ira, IrInstruction *source
}

if (value->value.type->id != TypeTableEntryIdUnion) {
ir_add_error(ira, source_instr,
ir_add_error(ira, value,
buf_sprintf("expected enum or union type, found '%s'", buf_ptr(&value->value.type->name)));
return ira->codegen->invalid_instruction;
}
Expand Down
11 changes: 11 additions & 0 deletions std/segmented_list.zig
Expand Up @@ -91,6 +91,8 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
allocator: &Allocator,
len: usize,

pub const prealloc_count = prealloc_item_count;

/// Deinitialize with `deinit`
pub fn init(allocator: &Allocator) Self {
return Self {
Expand Down Expand Up @@ -287,6 +289,15 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type

return &it.list.dynamic_segments[it.shelf_index][it.box_index];
}

pub fn peek(it: &Iterator) ?&T {
if (it.index >= it.list.len)
return null;
if (it.index < prealloc_item_count)
return &it.list.prealloc_segment[it.index];

return &it.list.dynamic_segments[it.shelf_index][it.box_index];
}
};

pub fn iterator(self: &Self, start_index: usize) Iterator {
Expand Down

0 comments on commit 4438c5e

Please sign in to comment.