@@ -22,15 +22,19 @@ pub fn parse(env: *base.ModuleEnv, source: []const u8) IR {
22
22
tokenizer .tokenize ();
23
23
const result = tokenizer .finishAndDeinit ();
24
24
25
- if (result .messages . len > 0 ) {
26
- tokenizeReport (env .gpa , source , result . messages );
25
+ for (result .messages ) | msg | {
26
+ _ = env . problems . append (env .gpa , .{ . tokenize = msg } );
27
27
}
28
28
29
29
var parser = Parser .init (result .tokens );
30
30
defer parser .deinit ();
31
31
32
32
parser .parseFile ();
33
33
34
+ for (parser .diagnostics .items ) | msg | {
35
+ _ = env .problems .append (env .gpa , .{ .parser = msg });
36
+ }
37
+
34
38
const errors = parser .diagnostics .toOwnedSlice (env .gpa ) catch | err | exitOnOom (err );
35
39
36
40
return .{
@@ -40,106 +44,3 @@ pub fn parse(env: *base.ModuleEnv, source: []const u8) IR {
40
44
.errors = errors ,
41
45
};
42
46
}
43
-
44
- fn lineNum (newlines : std .ArrayList (usize ), pos : u32 ) u32 {
45
- const pos_usize = @as (usize , @intCast (pos ));
46
- var lineno : u32 = 0 ;
47
- while (lineno < newlines .items .len ) {
48
- if (newlines .items [lineno + 1 ] > pos_usize ) {
49
- return lineno ;
50
- }
51
- lineno += 1 ;
52
- }
53
- return lineno ;
54
- }
55
-
56
- fn tokenizeReport (allocator : std.mem.Allocator , source : []const u8 , msgs : []const tokenize.Diagnostic ) void {
57
- std .debug .print ("Found the {d} following issues while tokenizing:\n " , .{msgs .len });
58
- var newlines = std .ArrayList (usize ).init (allocator );
59
- defer newlines .deinit ();
60
- newlines .append (0 ) catch | err | exitOnOom (err );
61
- var pos : usize = 0 ;
62
- for (source ) | c | {
63
- if (c == '\n ' ) {
64
- newlines .append (pos ) catch | err | exitOnOom (err );
65
- }
66
- pos += 1 ;
67
- }
68
- for (msgs ) | message | {
69
- switch (message .tag ) {
70
- .MismatchedBrace = > {
71
- const start_line_num = lineNum (newlines , message .begin );
72
- const start_col = message .begin - newlines .items [start_line_num ];
73
- const end_line_num = lineNum (newlines , message .end );
74
- const end_col = message .end - newlines .items [end_line_num ];
75
-
76
- const src = source [newlines .items [start_line_num ].. newlines .items [end_line_num + 1 ]];
77
- var spaces = std .ArrayList (u8 ).init (allocator );
78
- defer spaces .deinit ();
79
- for (0.. start_col ) | _ | {
80
- spaces .append (' ' ) catch | err | exitOnOom (err );
81
- }
82
-
83
- std .debug .print (
84
- "({d}:{d}-{d}:{d}) Expected the correct closing brace here:\n {s}\n {s}^\n " ,
85
- .{ start_line_num , start_col , end_line_num , end_col , src , spaces .toOwnedSlice () catch | err | exitOnOom (err ) },
86
- );
87
- },
88
- else = > {
89
- std .debug .print ("MSG: {any}\n " , .{message });
90
- },
91
- }
92
- }
93
- }
94
-
95
- // TODO move this somewhere better, for now it's here to keep it simple.
96
- fn testSExprHelper (source : []const u8 , expected : []const u8 ) ! void {
97
- var env = base .ModuleEnv .init (testing .allocator );
98
- defer env .deinit ();
99
-
100
- // parse our source
101
- var parse_ast = parse (& env , source );
102
- defer parse_ast .deinit ();
103
- std .testing .expectEqualSlices (IR .Diagnostic , &[_ ]IR.Diagnostic {}, parse_ast .errors ) catch {
104
- std .debug .print ("Tokens:\n {any}" , .{parse_ast .tokens .tokens .items (.tag )});
105
- std .debug .panic ("Test failed with parse errors" , .{});
106
- };
107
-
108
- // shouldn't be required in future
109
- parse_ast .store .emptyScratch ();
110
-
111
- // buffer to write our SExpr to
112
- var buf = std .ArrayList (u8 ).init (testing .allocator );
113
- defer buf .deinit ();
114
-
115
- // convert the AST to our SExpr
116
- try parse_ast .toSExprStr (& env , buf .writer ().any ());
117
-
118
- // TODO in future we should just write the SExpr to a file and snapshot it
119
- // for now we are comparing strings to keep it simple
120
- try testing .expectEqualStrings (expected , buf .items [0.. ]);
121
- }
122
-
123
- test "example s-expr" {
124
- const source =
125
- \\module [foo, bar]
126
- \\
127
- \\foo = "hey"
128
- \\bar = "yo"
129
- ;
130
-
131
- const expected =
132
- \\(file
133
- \\ (header
134
- \\ (exposed_item (lower_ident 'foo'))
135
- \\ (exposed_item (lower_ident 'bar')))
136
- \\ (decl
137
- \\ (ident 'foo')
138
- \\ (string 'hey'))
139
- \\ (decl
140
- \\ (ident 'bar')
141
- \\ (string 'yo')))
142
- ;
143
-
144
- try testSExprHelper (source , expected );
145
- }
0 commit comments