authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-16 00:43:28-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-16 00:43:28-04:00
log288fc3a8d361972daeded19d207b410128d70d67
tree49ecb7a09c27306ca3f8e28fed82cd95f2b81772
parent5cfabdd493c6602243f47e24320bae940a3c417a

convert more std lib files to postfix pointer deref


6 files changed, 1211 insertions(+), 1378 deletions(-)

std/crypto/test.zig+1-2
...@@ -14,9 +14,8 @@ pub fn assertEqualHash(comptime Hasher: var, comptime expected: []const u8, inpu...@@ -14,9 +14,8 @@ pub fn assertEqualHash(comptime Hasher: var, comptime expected: []const u8, inpu
14pub fn assertEqual(comptime expected: []const u8, input: []const u8) void {14pub fn assertEqual(comptime expected: []const u8, input: []const u8) void {
15 var expected_bytes: [expected.len / 2]u8 = undefined;15 var expected_bytes: [expected.len / 2]u8 = undefined;
16 for (expected_bytes) |*r, i| {16 for (expected_bytes) |*r, i| {
17 *r = fmt.parseInt(u8, expected[2*i .. 2*i+2], 16) catch unreachable;17 r.* = fmt.parseInt(u8, expected[2 * i .. 2 * i + 2], 16) catch unreachable;
18 }18 }
1919
20 debug.assert(mem.eql(u8, expected_bytes, input));20 debug.assert(mem.eql(u8, expected_bytes, input));
21}21}
22
std/fmt/errol/index.zig+22-32
...@@ -86,7 +86,7 @@ pub fn errol3(value: f64, buffer: []u8) FloatDecimal {...@@ -86,7 +86,7 @@ pub fn errol3(value: f64, buffer: []u8) FloatDecimal {
86 const data = enum3_data[i];86 const data = enum3_data[i];
87 const digits = buffer[1..data.str.len + 1];87 const digits = buffer[1..data.str.len + 1];
88 mem.copy(u8, digits, data.str);88 mem.copy(u8, digits, data.str);
89 return FloatDecimal {89 return FloatDecimal{
90 .digits = digits,90 .digits = digits,
91 .exp = data.exp,91 .exp = data.exp,
92 };92 };
...@@ -105,7 +105,6 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {...@@ -105,7 +105,6 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
105 return errolFixed(val, buffer);105 return errolFixed(val, buffer);
106 }106 }
107107
108
109 // normalize the midpoint108 // normalize the midpoint
110109
111 const e = math.frexp(val).exponent;110 const e = math.frexp(val).exponent;
...@@ -137,11 +136,11 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {...@@ -137,11 +136,11 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
137 }136 }
138137
139 // compute boundaries138 // compute boundaries
140 var high = HP {139 var high = HP{
141 .val = mid.val,140 .val = mid.val,
142 .off = mid.off + (fpnext(val) - val) * lten * ten / 2.0,141 .off = mid.off + (fpnext(val) - val) * lten * ten / 2.0,
143 };142 };
144 var low = HP {143 var low = HP{
145 .val = mid.val,144 .val = mid.val,
146 .off = mid.off + (fpprev(val) - val) * lten * ten / 2.0,145 .off = mid.off + (fpprev(val) - val) * lten * ten / 2.0,
147 };146 };
...@@ -171,15 +170,12 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {...@@ -171,15 +170,12 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
171 var buf_index: usize = 1;170 var buf_index: usize = 1;
172 while (true) {171 while (true) {
173 var hdig = u8(math.floor(high.val));172 var hdig = u8(math.floor(high.val));
174 if ((high.val == f64(hdig)) and (high.off < 0))173 if ((high.val == f64(hdig)) and (high.off < 0)) hdig -= 1;
175 hdig -= 1;
176174
177 var ldig = u8(math.floor(low.val));175 var ldig = u8(math.floor(low.val));
178 if ((low.val == f64(ldig)) and (low.off < 0))176 if ((low.val == f64(ldig)) and (low.off < 0)) ldig -= 1;
179 ldig -= 1;
180177
181 if (ldig != hdig)178 if (ldig != hdig) break;
182 break;
183179
184 buffer[buf_index] = hdig + '0';180 buffer[buf_index] = hdig + '0';
185 buf_index += 1;181 buf_index += 1;
...@@ -191,13 +187,12 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {...@@ -191,13 +187,12 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
191187
192 const tmp = (high.val + low.val) / 2.0;188 const tmp = (high.val + low.val) / 2.0;
193 var mdig = u8(math.floor(tmp + 0.5));189 var mdig = u8(math.floor(tmp + 0.5));
194 if ((f64(mdig) - tmp) == 0.5 and (mdig & 0x1) != 0)190 if ((f64(mdig) - tmp) == 0.5 and (mdig & 0x1) != 0) mdig -= 1;
195 mdig -= 1;
196191
197 buffer[buf_index] = mdig + '0';192 buffer[buf_index] = mdig + '0';
198 buf_index += 1;193 buf_index += 1;
199194
200 return FloatDecimal {195 return FloatDecimal{
201 .digits = buffer[1..buf_index],196 .digits = buffer[1..buf_index],
202 .exp = exp,197 .exp = exp,
203 };198 };
...@@ -235,7 +230,7 @@ fn hpProd(in: &const HP, val: f64) HP {...@@ -235,7 +230,7 @@ fn hpProd(in: &const HP, val: f64) HP {
235 const p = in.val * val;230 const p = in.val * val;
236 const e = ((hi * hi2 - p) + lo * hi2 + hi * lo2) + lo * lo2;231 const e = ((hi * hi2 - p) + lo * hi2 + hi * lo2) + lo * lo2;
237232
238 return HP {233 return HP{
239 .val = p,234 .val = p,
240 .off = in.off * val + e,235 .off = in.off * val + e,
241 };236 };
...@@ -246,8 +241,8 @@ fn hpProd(in: &const HP, val: f64) HP {...@@ -246,8 +241,8 @@ fn hpProd(in: &const HP, val: f64) HP {
246/// @hi: The high bits.241/// @hi: The high bits.
247/// @lo: The low bits.242/// @lo: The low bits.
248fn split(val: f64, hi: &f64, lo: &f64) void {243fn split(val: f64, hi: &f64, lo: &f64) void {
249 *hi = gethi(val);244 hi.* = gethi(val);
250 *lo = val - *hi;245 lo.* = val - hi.*;
251}246}
252247
253fn gethi(in: f64) f64 {248fn gethi(in: f64) f64 {
...@@ -301,7 +296,6 @@ fn hpMul10(hp: &HP) void {...@@ -301,7 +296,6 @@ fn hpMul10(hp: &HP) void {
301 hpNormalize(hp);296 hpNormalize(hp);
302}297}
303298
304
305/// Integer conversion algorithm, guaranteed correct, optimal, and best.299/// Integer conversion algorithm, guaranteed correct, optimal, and best.
306/// @val: The val.300/// @val: The val.
307/// @buf: The output buffer.301/// @buf: The output buffer.
...@@ -343,8 +337,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {...@@ -343,8 +337,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
343 }337 }
344 const m64 = @truncate(u64, @divTrunc(mid, x));338 const m64 = @truncate(u64, @divTrunc(mid, x));
345339
346 if (lf != hf)340 if (lf != hf) mi += 19;
347 mi += 19;
348341
349 var buf_index = u64toa(m64, buffer) - 1;342 var buf_index = u64toa(m64, buffer) - 1;
350343
...@@ -354,7 +347,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {...@@ -354,7 +347,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
354 buf_index += 1;347 buf_index += 1;
355 }348 }
356349
357 return FloatDecimal {350 return FloatDecimal{
358 .digits = buffer[0..buf_index],351 .digits = buffer[0..buf_index],
359 .exp = i32(buf_index) + mi,352 .exp = i32(buf_index) + mi,
360 };353 };
...@@ -396,25 +389,24 @@ fn errolFixed(val: f64, buffer: []u8) FloatDecimal {...@@ -396,25 +389,24 @@ fn errolFixed(val: f64, buffer: []u8) FloatDecimal {
396 buffer[j] = u8(mdig + '0');389 buffer[j] = u8(mdig + '0');
397 j += 1;390 j += 1;
398391
399 if(hdig != ldig or j > 50)392 if (hdig != ldig or j > 50) break;
400 break;
401 }393 }
402394
403 if (mid > 0.5) {395 if (mid > 0.5) {
404 buffer[j-1] += 1;396 buffer[j - 1] += 1;
405 } else if ((mid == 0.5) and (buffer[j-1] & 0x1) != 0) {397 } else if ((mid == 0.5) and (buffer[j - 1] & 0x1) != 0) {
406 buffer[j-1] += 1;398 buffer[j - 1] += 1;
407 }399 }
408 } else {400 } else {
409 while (buffer[j-1] == '0') {401 while (buffer[j - 1] == '0') {
410 buffer[j-1] = 0;402 buffer[j - 1] = 0;
411 j -= 1;403 j -= 1;
412 }404 }
413 }405 }
414406
415 buffer[j] = 0;407 buffer[j] = 0;
416408
417 return FloatDecimal {409 return FloatDecimal{
418 .digits = buffer[0..j],410 .digits = buffer[0..j],
419 .exp = exp,411 .exp = exp,
420 };412 };
...@@ -587,7 +579,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize {...@@ -587,7 +579,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
587 buffer[buf_index] = c_digits_lut[d8 + 1];579 buffer[buf_index] = c_digits_lut[d8 + 1];
588 buf_index += 1;580 buf_index += 1;
589 } else {581 } else {
590 const a = u32(value / kTen16); // 1 to 1844582 const a = u32(value / kTen16); // 1 to 1844
591 value %= kTen16;583 value %= kTen16;
592584
593 if (a < 10) {585 if (a < 10) {
...@@ -686,7 +678,6 @@ fn fpeint(from: f64) u128 {...@@ -686,7 +678,6 @@ fn fpeint(from: f64) u128 {
686 return u128(1) << @truncate(u7, (bits >> 52) -% 1023);678 return u128(1) << @truncate(u7, (bits >> 52) -% 1023);
687}679}
688680
689
690/// Given two different integers with the same length in terms of the number681/// Given two different integers with the same length in terms of the number
691/// of decimal digits, index the digits from the right-most position starting682/// of decimal digits, index the digits from the right-most position starting
692/// from zero, find the first index where the digits in the two integers683/// from zero, find the first index where the digits in the two integers
...@@ -713,7 +704,6 @@ fn mismatch10(a: u64, b: u64) i32 {...@@ -713,7 +704,6 @@ fn mismatch10(a: u64, b: u64) i32 {
713 a_copy /= 10;704 a_copy /= 10;
714 b_copy /= 10;705 b_copy /= 10;
715706
716 if (a_copy == b_copy)707 if (a_copy == b_copy) return i;
717 return i;
718 }708 }
719}709}
std/os/darwin.zig+182-100
...@@ -10,33 +10,56 @@ pub const STDIN_FILENO = 0;...@@ -10,33 +10,56 @@ pub const STDIN_FILENO = 0;
10pub const STDOUT_FILENO = 1;10pub const STDOUT_FILENO = 1;
11pub const STDERR_FILENO = 2;11pub const STDERR_FILENO = 2;
1212
13pub const PROT_NONE = 0x00; /// [MC2] no permissions13/// [MC2] no permissions
14pub const PROT_READ = 0x01; /// [MC2] pages can be read14pub const PROT_NONE = 0x00;
15pub const PROT_WRITE = 0x02; /// [MC2] pages can be written15/// [MC2] pages can be read
16pub const PROT_EXEC = 0x04; /// [MC2] pages can be executed16pub const PROT_READ = 0x01;
1717/// [MC2] pages can be written
18pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space18pub const PROT_WRITE = 0x02;
19pub const MAP_FILE = 0x0000; /// map from file (default)19/// [MC2] pages can be executed
20pub const MAP_FIXED = 0x0010; /// interpret addr exactly20pub const PROT_EXEC = 0x04;
21pub const MAP_HASSEMAPHORE = 0x0200; /// region may contain semaphores21
22pub const MAP_PRIVATE = 0x0002; /// changes are private22/// allocated from memory, swap space
23pub const MAP_SHARED = 0x0001; /// share changes23pub const MAP_ANONYMOUS = 0x1000;
24pub const MAP_NOCACHE = 0x0400; /// don't cache pages for this mapping24/// map from file (default)
25pub const MAP_NORESERVE = 0x0040; /// don't reserve needed swap area25pub const MAP_FILE = 0x0000;
26/// interpret addr exactly
27pub const MAP_FIXED = 0x0010;
28/// region may contain semaphores
29pub const MAP_HASSEMAPHORE = 0x0200;
30/// changes are private
31pub const MAP_PRIVATE = 0x0002;
32/// share changes
33pub const MAP_SHARED = 0x0001;
34/// don't cache pages for this mapping
35pub const MAP_NOCACHE = 0x0400;
36/// don't reserve needed swap area
37pub const MAP_NORESERVE = 0x0040;
26pub const MAP_FAILED = @maxValue(usize);38pub const MAP_FAILED = @maxValue(usize);
2739
28pub const WNOHANG = 0x00000001; /// [XSI] no hang in wait/no child to reap40/// [XSI] no hang in wait/no child to reap
29pub const WUNTRACED = 0x00000002; /// [XSI] notify on stop, untraced child41pub const WNOHANG = 0x00000001;
3042/// [XSI] notify on stop, untraced child
31pub const SA_ONSTACK = 0x0001; /// take signal on signal stack43pub const WUNTRACED = 0x00000002;
32pub const SA_RESTART = 0x0002; /// restart system on signal return44
33pub const SA_RESETHAND = 0x0004; /// reset to SIG_DFL when taking signal45/// take signal on signal stack
34pub const SA_NOCLDSTOP = 0x0008; /// do not generate SIGCHLD on child stop46pub const SA_ONSTACK = 0x0001;
35pub const SA_NODEFER = 0x0010; /// don't mask the signal we're delivering47/// restart system on signal return
36pub const SA_NOCLDWAIT = 0x0020; /// don't keep zombies around48pub const SA_RESTART = 0x0002;
37pub const SA_SIGINFO = 0x0040; /// signal handler with SA_SIGINFO args49/// reset to SIG_DFL when taking signal
38pub const SA_USERTRAMP = 0x0100; /// do not bounce off kernel's sigtramp50pub const SA_RESETHAND = 0x0004;
39pub const SA_64REGSET = 0x0200; /// signal handler with SA_SIGINFO args with 64bit regs information51/// do not generate SIGCHLD on child stop
52pub const SA_NOCLDSTOP = 0x0008;
53/// don't mask the signal we're delivering
54pub const SA_NODEFER = 0x0010;
55/// don't keep zombies around
56pub const SA_NOCLDWAIT = 0x0020;
57/// signal handler with SA_SIGINFO args
58pub const SA_SIGINFO = 0x0040;
59/// do not bounce off kernel's sigtramp
60pub const SA_USERTRAMP = 0x0100;
61/// signal handler with SA_SIGINFO args with 64bit regs information
62pub const SA_64REGSET = 0x0200;
4063
41pub const O_LARGEFILE = 0x0000;64pub const O_LARGEFILE = 0x0000;
42pub const O_PATH = 0x0000;65pub const O_PATH = 0x0000;
...@@ -46,20 +69,34 @@ pub const X_OK = 1;...@@ -46,20 +69,34 @@ pub const X_OK = 1;
46pub const W_OK = 2;69pub const W_OK = 2;
47pub const R_OK = 4;70pub const R_OK = 4;
4871
49pub const O_RDONLY = 0x0000; /// open for reading only72/// open for reading only
50pub const O_WRONLY = 0x0001; /// open for writing only73pub const O_RDONLY = 0x0000;
51pub const O_RDWR = 0x0002; /// open for reading and writing74/// open for writing only
52pub const O_NONBLOCK = 0x0004; /// do not block on open or for data to become available75pub const O_WRONLY = 0x0001;
53pub const O_APPEND = 0x0008; /// append on each write76/// open for reading and writing
54pub const O_CREAT = 0x0200; /// create file if it does not exist77pub const O_RDWR = 0x0002;
55pub const O_TRUNC = 0x0400; /// truncate size to 078/// do not block on open or for data to become available
56pub const O_EXCL = 0x0800; /// error if O_CREAT and the file exists79pub const O_NONBLOCK = 0x0004;
57pub const O_SHLOCK = 0x0010; /// atomically obtain a shared lock80/// append on each write
58pub const O_EXLOCK = 0x0020; /// atomically obtain an exclusive lock81pub const O_APPEND = 0x0008;
59pub const O_NOFOLLOW = 0x0100; /// do not follow symlinks82/// create file if it does not exist
60pub const O_SYMLINK = 0x200000; /// allow open of symlinks83pub const O_CREAT = 0x0200;
61pub const O_EVTONLY = 0x8000; /// descriptor requested for event notifications only84/// truncate size to 0
62pub const O_CLOEXEC = 0x1000000; /// mark as close-on-exec85pub const O_TRUNC = 0x0400;
86/// error if O_CREAT and the file exists
87pub const O_EXCL = 0x0800;
88/// atomically obtain a shared lock
89pub const O_SHLOCK = 0x0010;
90/// atomically obtain an exclusive lock
91pub const O_EXLOCK = 0x0020;
92/// do not follow symlinks
93pub const O_NOFOLLOW = 0x0100;
94/// allow open of symlinks
95pub const O_SYMLINK = 0x200000;
96/// descriptor requested for event notifications only
97pub const O_EVTONLY = 0x8000;
98/// mark as close-on-exec
99pub const O_CLOEXEC = 0x1000000;
63100
64pub const O_ACCMODE = 3;101pub const O_ACCMODE = 3;
65pub const O_ALERT = 536870912;102pub const O_ALERT = 536870912;
...@@ -87,52 +124,102 @@ pub const DT_LNK = 10;...@@ -87,52 +124,102 @@ pub const DT_LNK = 10;
87pub const DT_SOCK = 12;124pub const DT_SOCK = 12;
88pub const DT_WHT = 14;125pub const DT_WHT = 14;
89126
90pub const SIG_BLOCK = 1; /// block specified signal set127/// block specified signal set
91pub const SIG_UNBLOCK = 2; /// unblock specified signal set128pub const SIG_BLOCK = 1;
92pub const SIG_SETMASK = 3; /// set specified signal set129/// unblock specified signal set
93130pub const SIG_UNBLOCK = 2;
94pub const SIGHUP = 1; /// hangup131/// set specified signal set
95pub const SIGINT = 2; /// interrupt132pub const SIG_SETMASK = 3;
96pub const SIGQUIT = 3; /// quit133
97pub const SIGILL = 4; /// illegal instruction (not reset when caught)134/// hangup
98pub const SIGTRAP = 5; /// trace trap (not reset when caught)135pub const SIGHUP = 1;
99pub const SIGABRT = 6; /// abort()136/// interrupt
100pub const SIGPOLL = 7; /// pollable event ([XSR] generated, not supported)137pub const SIGINT = 2;
101pub const SIGIOT = SIGABRT; /// compatibility138/// quit
102pub const SIGEMT = 7; /// EMT instruction139pub const SIGQUIT = 3;
103pub const SIGFPE = 8; /// floating point exception140/// illegal instruction (not reset when caught)
104pub const SIGKILL = 9; /// kill (cannot be caught or ignored)141pub const SIGILL = 4;
105pub const SIGBUS = 10; /// bus error142/// trace trap (not reset when caught)
106pub const SIGSEGV = 11; /// segmentation violation143pub const SIGTRAP = 5;
107pub const SIGSYS = 12; /// bad argument to system call144/// abort()
108pub const SIGPIPE = 13; /// write on a pipe with no one to read it145pub const SIGABRT = 6;
109pub const SIGALRM = 14; /// alarm clock146/// pollable event ([XSR] generated, not supported)
110pub const SIGTERM = 15; /// software termination signal from kill147pub const SIGPOLL = 7;
111pub const SIGURG = 16; /// urgent condition on IO channel148/// compatibility
112pub const SIGSTOP = 17; /// sendable stop signal not from tty149pub const SIGIOT = SIGABRT;
113pub const SIGTSTP = 18; /// stop signal from tty150/// EMT instruction
114pub const SIGCONT = 19; /// continue a stopped process151pub const SIGEMT = 7;
115pub const SIGCHLD = 20; /// to parent on child stop or exit152/// floating point exception
116pub const SIGTTIN = 21; /// to readers pgrp upon background tty read153pub const SIGFPE = 8;
117pub const SIGTTOU = 22; /// like TTIN for output if (tp->t_local&LTOSTOP)154/// kill (cannot be caught or ignored)
118pub const SIGIO = 23; /// input/output possible signal155pub const SIGKILL = 9;
119pub const SIGXCPU = 24; /// exceeded CPU time limit156/// bus error
120pub const SIGXFSZ = 25; /// exceeded file size limit157pub const SIGBUS = 10;
121pub const SIGVTALRM = 26; /// virtual time alarm158/// segmentation violation
122pub const SIGPROF = 27; /// profiling time alarm159pub const SIGSEGV = 11;
123pub const SIGWINCH = 28; /// window size changes160/// bad argument to system call
124pub const SIGINFO = 29; /// information request161pub const SIGSYS = 12;
125pub const SIGUSR1 = 30; /// user defined signal 1162/// write on a pipe with no one to read it
126pub const SIGUSR2 = 31; /// user defined signal 2163pub const SIGPIPE = 13;
127164/// alarm clock
128fn wstatus(x: i32) i32 { return x & 0o177; }165pub const SIGALRM = 14;
166/// software termination signal from kill
167pub const SIGTERM = 15;
168/// urgent condition on IO channel
169pub const SIGURG = 16;
170/// sendable stop signal not from tty
171pub const SIGSTOP = 17;
172/// stop signal from tty
173pub const SIGTSTP = 18;
174/// continue a stopped process
175pub const SIGCONT = 19;
176/// to parent on child stop or exit
177pub const SIGCHLD = 20;
178/// to readers pgrp upon background tty read
179pub const SIGTTIN = 21;
180/// like TTIN for output if (tp->t_local&LTOSTOP)
181pub const SIGTTOU = 22;
182/// input/output possible signal
183pub const SIGIO = 23;
184/// exceeded CPU time limit
185pub const SIGXCPU = 24;
186/// exceeded file size limit
187pub const SIGXFSZ = 25;
188/// virtual time alarm
189pub const SIGVTALRM = 26;
190/// profiling time alarm
191pub const SIGPROF = 27;
192/// window size changes
193pub const SIGWINCH = 28;
194/// information request
195pub const SIGINFO = 29;
196/// user defined signal 1
197pub const SIGUSR1 = 30;
198/// user defined signal 2
199pub const SIGUSR2 = 31;
200
201fn wstatus(x: i32) i32 {
202 return x & 0o177;
203}
129const wstopped = 0o177;204const wstopped = 0o177;
130pub fn WEXITSTATUS(x: i32) i32 { return x >> 8; }205pub fn WEXITSTATUS(x: i32) i32 {
131pub fn WTERMSIG(x: i32) i32 { return wstatus(x); }206 return x >> 8;
132pub fn WSTOPSIG(x: i32) i32 { return x >> 8; }207}
133pub fn WIFEXITED(x: i32) bool { return wstatus(x) == 0; }208pub fn WTERMSIG(x: i32) i32 {
134pub fn WIFSTOPPED(x: i32) bool { return wstatus(x) == wstopped and WSTOPSIG(x) != 0x13; }209 return wstatus(x);
135pub fn WIFSIGNALED(x: i32) bool { return wstatus(x) != wstopped and wstatus(x) != 0; }210}
211pub fn WSTOPSIG(x: i32) i32 {
212 return x >> 8;
213}
214pub fn WIFEXITED(x: i32) bool {
215 return wstatus(x) == 0;
216}
217pub fn WIFSTOPPED(x: i32) bool {
218 return wstatus(x) == wstopped and WSTOPSIG(x) != 0x13;
219}
220pub fn WIFSIGNALED(x: i32) bool {
221 return wstatus(x) != wstopped and wstatus(x) != 0;
222}
136223
137/// Get the errno from a syscall return value, or 0 for no error.224/// Get the errno from a syscall return value, or 0 for no error.
138pub fn getErrno(r: usize) usize {225pub fn getErrno(r: usize) usize {
...@@ -184,11 +271,8 @@ pub fn write(fd: i32, buf: &const u8, nbyte: usize) usize {...@@ -184,11 +271,8 @@ pub fn write(fd: i32, buf: &const u8, nbyte: usize) usize {
184 return errnoWrap(c.write(fd, @ptrCast(&const c_void, buf), nbyte));271 return errnoWrap(c.write(fd, @ptrCast(&const c_void, buf), nbyte));
185}272}
186273
187pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: u32, fd: i32,274pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
188 offset: isize) usize275 const ptr_result = c.mmap(@ptrCast(&c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
189{
190 const ptr_result = c.mmap(@ptrCast(&c_void, address), length,
191 @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
192 const isize_result = @bitCast(isize, @ptrToInt(ptr_result));276 const isize_result = @bitCast(isize, @ptrToInt(ptr_result));
193 return errnoWrap(isize_result);277 return errnoWrap(isize_result);
194}278}
...@@ -202,7 +286,7 @@ pub fn unlink(path: &const u8) usize {...@@ -202,7 +286,7 @@ pub fn unlink(path: &const u8) usize {
202}286}
203287
204pub fn getcwd(buf: &u8, size: usize) usize {288pub fn getcwd(buf: &u8, size: usize) usize {
205 return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(*c._errno())) else 0;289 return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(c._errno().*)) else 0;
206}290}
207291
208pub fn waitpid(pid: i32, status: &i32, options: u32) usize {292pub fn waitpid(pid: i32, status: &i32, options: u32) usize {
...@@ -223,7 +307,6 @@ pub fn pipe(fds: &[2]i32) usize {...@@ -223,7 +307,6 @@ pub fn pipe(fds: &[2]i32) usize {
223 return errnoWrap(c.pipe(@ptrCast(&c_int, fds)));307 return errnoWrap(c.pipe(@ptrCast(&c_int, fds)));
224}308}
225309
226
227pub fn getdirentries64(fd: i32, buf_ptr: &u8, buf_len: usize, basep: &i64) usize {310pub fn getdirentries64(fd: i32, buf_ptr: &u8, buf_len: usize, basep: &i64) usize {
228 return errnoWrap(@bitCast(isize, c.__getdirentries64(fd, buf_ptr, buf_len, basep)));311 return errnoWrap(@bitCast(isize, c.__getdirentries64(fd, buf_ptr, buf_len, basep)));
229}312}
...@@ -269,7 +352,7 @@ pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize {...@@ -269,7 +352,7 @@ pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize {
269}352}
270353
271pub fn realpath(noalias filename: &const u8, noalias resolved_name: &u8) usize {354pub fn realpath(noalias filename: &const u8, noalias resolved_name: &u8) usize {
272 return if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(*c._errno())) else 0;355 return if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(c._errno().*)) else 0;
273}356}
274357
275pub fn setreuid(ruid: u32, euid: u32) usize {358pub fn setreuid(ruid: u32, euid: u32) usize {
...@@ -287,8 +370,8 @@ pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&s...@@ -287,8 +370,8 @@ pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&s
287pub fn sigaction(sig: u5, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize {370pub fn sigaction(sig: u5, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize {
288 assert(sig != SIGKILL);371 assert(sig != SIGKILL);
289 assert(sig != SIGSTOP);372 assert(sig != SIGSTOP);
290 var cact = c.Sigaction {373 var cact = c.Sigaction{
291 .handler = @ptrCast(extern fn(c_int)void, act.handler),374 .handler = @ptrCast(extern fn(c_int) void, act.handler),
292 .sa_flags = @bitCast(c_int, act.flags),375 .sa_flags = @bitCast(c_int, act.flags),
293 .sa_mask = act.mask,376 .sa_mask = act.mask,
294 };377 };
...@@ -298,8 +381,8 @@ pub fn sigaction(sig: u5, noalias act: &const Sigaction, noalias oact: ?&Sigacti...@@ -298,8 +381,8 @@ pub fn sigaction(sig: u5, noalias act: &const Sigaction, noalias oact: ?&Sigacti
298 return result;381 return result;
299 }382 }
300 if (oact) |old| {383 if (oact) |old| {
301 *old = Sigaction {384 old.* = Sigaction{
302 .handler = @ptrCast(extern fn(i32)void, coact.handler),385 .handler = @ptrCast(extern fn(i32) void, coact.handler),
303 .flags = @bitCast(u32, coact.sa_flags),386 .flags = @bitCast(u32, coact.sa_flags),
304 .mask = coact.sa_mask,387 .mask = coact.sa_mask,
305 };388 };
...@@ -319,23 +402,22 @@ pub const sockaddr = c.sockaddr;...@@ -319,23 +402,22 @@ pub const sockaddr = c.sockaddr;
319402
320/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.403/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
321pub const Sigaction = struct {404pub const Sigaction = struct {
322 handler: extern fn(i32)void,405 handler: extern fn(i32) void,
323 mask: sigset_t,406 mask: sigset_t,
324 flags: u32,407 flags: u32,
325};408};
326409
327pub fn sigaddset(set: &sigset_t, signo: u5) void {410pub fn sigaddset(set: &sigset_t, signo: u5) void {
328 *set |= u32(1) << (signo - 1);411 set.* |= u32(1) << (signo - 1);
329}412}
330413
331/// Takes the return value from a syscall and formats it back in the way414/// Takes the return value from a syscall and formats it back in the way
332/// that the kernel represents it to libc. Errno was a mistake, let's make415/// that the kernel represents it to libc. Errno was a mistake, let's make
333/// it go away forever.416/// it go away forever.
334fn errnoWrap(value: isize) usize {417fn errnoWrap(value: isize) usize {
335 return @bitCast(usize, if (value == -1) -isize(*c._errno()) else value);418 return @bitCast(usize, if (value == -1) -isize(c._errno().*) else value);
336}419}
337420
338
339pub const timezone = c.timezone;421pub const timezone = c.timezone;
340pub const timeval = c.timeval;422pub const timeval = c.timeval;
341pub const mach_timebase_info_data = c.mach_timebase_info_data;423pub const mach_timebase_info_data = c.mach_timebase_info_data;
std/zig/ast.zig+32-39
...@@ -40,7 +40,7 @@ pub const Tree = struct {...@@ -40,7 +40,7 @@ pub const Tree = struct {
40 };40 };
4141
42 pub fn tokenLocationPtr(self: &Tree, start_index: usize, token: &const Token) Location {42 pub fn tokenLocationPtr(self: &Tree, start_index: usize, token: &const Token) Location {
43 var loc = Location {43 var loc = Location{
44 .line = 0,44 .line = 0,
45 .column = 0,45 .column = 0,
46 .line_start = start_index,46 .line_start = start_index,
...@@ -71,7 +71,6 @@ pub const Tree = struct {...@@ -71,7 +71,6 @@ pub const Tree = struct {
71 pub fn dump(self: &Tree) void {71 pub fn dump(self: &Tree) void {
72 self.root_node.base.dump(0);72 self.root_node.base.dump(0);
73 }73 }
74
75};74};
7675
77pub const Error = union(enum) {76pub const Error = union(enum) {
...@@ -95,7 +94,7 @@ pub const Error = union(enum) {...@@ -95,7 +94,7 @@ pub const Error = union(enum) {
95 ExpectedCommaOrEnd: ExpectedCommaOrEnd,94 ExpectedCommaOrEnd: ExpectedCommaOrEnd,
9695
97 pub fn render(self: &Error, tokens: &Tree.TokenList, stream: var) !void {96 pub fn render(self: &Error, tokens: &Tree.TokenList, stream: var) !void {
98 switch (*self) {97 switch (self.*) {
99 // TODO https://github.com/zig-lang/zig/issues/68398 // TODO https://github.com/zig-lang/zig/issues/683
100 @TagType(Error).InvalidToken => |*x| return x.render(tokens, stream),99 @TagType(Error).InvalidToken => |*x| return x.render(tokens, stream),
101 @TagType(Error).ExpectedVarDeclOrFn => |*x| return x.render(tokens, stream),100 @TagType(Error).ExpectedVarDeclOrFn => |*x| return x.render(tokens, stream),
...@@ -119,7 +118,7 @@ pub const Error = union(enum) {...@@ -119,7 +118,7 @@ pub const Error = union(enum) {
119 }118 }
120119
121 pub fn loc(self: &Error) TokenIndex {120 pub fn loc(self: &Error) TokenIndex {
122 switch (*self) {121 switch (self.*) {
123 // TODO https://github.com/zig-lang/zig/issues/683122 // TODO https://github.com/zig-lang/zig/issues/683
124 @TagType(Error).InvalidToken => |x| return x.token,123 @TagType(Error).InvalidToken => |x| return x.token,
125 @TagType(Error).ExpectedVarDeclOrFn => |x| return x.token,124 @TagType(Error).ExpectedVarDeclOrFn => |x| return x.token,
...@@ -144,15 +143,12 @@ pub const Error = union(enum) {...@@ -144,15 +143,12 @@ pub const Error = union(enum) {
144143
145 pub const InvalidToken = SingleTokenError("Invalid token {}");144 pub const InvalidToken = SingleTokenError("Invalid token {}");
146 pub const ExpectedVarDeclOrFn = SingleTokenError("Expected variable declaration or function, found {}");145 pub const ExpectedVarDeclOrFn = SingleTokenError("Expected variable declaration or function, found {}");
147 pub const ExpectedAggregateKw = SingleTokenError("Expected " ++146 pub const ExpectedAggregateKw = SingleTokenError("Expected " ++ @tagName(Token.Id.Keyword_struct) ++ ", " ++ @tagName(Token.Id.Keyword_union) ++ ", or " ++ @tagName(Token.Id.Keyword_enum) ++ ", found {}");
148 @tagName(Token.Id.Keyword_struct) ++ ", " ++ @tagName(Token.Id.Keyword_union) ++ ", or " ++
149 @tagName(Token.Id.Keyword_enum) ++ ", found {}");
150 pub const ExpectedEqOrSemi = SingleTokenError("Expected '=' or ';', found {}");147 pub const ExpectedEqOrSemi = SingleTokenError("Expected '=' or ';', found {}");
151 pub const ExpectedSemiOrLBrace = SingleTokenError("Expected ';' or '{{', found {}");148 pub const ExpectedSemiOrLBrace = SingleTokenError("Expected ';' or '{{', found {}");
152 pub const ExpectedLabelable = SingleTokenError("Expected 'while', 'for', 'inline', 'suspend', or '{{', found {}");149 pub const ExpectedLabelable = SingleTokenError("Expected 'while', 'for', 'inline', 'suspend', or '{{', found {}");
153 pub const ExpectedInlinable = SingleTokenError("Expected 'while' or 'for', found {}");150 pub const ExpectedInlinable = SingleTokenError("Expected 'while' or 'for', found {}");
154 pub const ExpectedAsmOutputReturnOrType = SingleTokenError("Expected '->' or " ++151 pub const ExpectedAsmOutputReturnOrType = SingleTokenError("Expected '->' or " ++ @tagName(Token.Id.Identifier) ++ ", found {}");
155 @tagName(Token.Id.Identifier) ++ ", found {}");
156 pub const ExpectedSliceOrRBracket = SingleTokenError("Expected ']' or '..', found {}");152 pub const ExpectedSliceOrRBracket = SingleTokenError("Expected ']' or '..', found {}");
157 pub const ExpectedPrimaryExpr = SingleTokenError("Expected primary expression, found {}");153 pub const ExpectedPrimaryExpr = SingleTokenError("Expected primary expression, found {}");
158154
...@@ -165,8 +161,7 @@ pub const Error = union(enum) {...@@ -165,8 +161,7 @@ pub const Error = union(enum) {
165 node: &Node,161 node: &Node,
166162
167 pub fn render(self: &ExpectedCall, tokens: &Tree.TokenList, stream: var) !void {163 pub fn render(self: &ExpectedCall, tokens: &Tree.TokenList, stream: var) !void {
168 return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ ", found {}",164 return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ ", found {}", @tagName(self.node.id));
169 @tagName(self.node.id));
170 }165 }
171 };166 };
172167
...@@ -174,8 +169,7 @@ pub const Error = union(enum) {...@@ -174,8 +169,7 @@ pub const Error = union(enum) {
174 node: &Node,169 node: &Node,
175170
176 pub fn render(self: &ExpectedCallOrFnProto, tokens: &Tree.TokenList, stream: var) !void {171 pub fn render(self: &ExpectedCallOrFnProto, tokens: &Tree.TokenList, stream: var) !void {
177 return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ " or " ++172 return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ " or " ++ @tagName(Node.Id.FnProto) ++ ", found {}", @tagName(self.node.id));
178 @tagName(Node.Id.FnProto) ++ ", found {}", @tagName(self.node.id));
179 }173 }
180 };174 };
181175
...@@ -445,17 +439,17 @@ pub const Node = struct {...@@ -445,17 +439,17 @@ pub const Node = struct {
445439
446 pub fn iterate(self: &Root, index: usize) ?&Node {440 pub fn iterate(self: &Root, index: usize) ?&Node {
447 if (index < self.decls.len) {441 if (index < self.decls.len) {
448 return *self.decls.at(index);442 return self.decls.at(index).*;
449 }443 }
450 return null;444 return null;
451 }445 }
452446
453 pub fn firstToken(self: &Root) TokenIndex {447 pub fn firstToken(self: &Root) TokenIndex {
454 return if (self.decls.len == 0) self.eof_token else (*self.decls.at(0)).firstToken();448 return if (self.decls.len == 0) self.eof_token else (self.decls.at(0).*).firstToken();
455 }449 }
456450
457 pub fn lastToken(self: &Root) TokenIndex {451 pub fn lastToken(self: &Root) TokenIndex {
458 return if (self.decls.len == 0) self.eof_token else (*self.decls.at(self.decls.len - 1)).lastToken();452 return if (self.decls.len == 0) self.eof_token else (self.decls.at(self.decls.len - 1).*).lastToken();
459 }453 }
460 };454 };
461455
...@@ -545,7 +539,7 @@ pub const Node = struct {...@@ -545,7 +539,7 @@ pub const Node = struct {
545 pub fn iterate(self: &ErrorSetDecl, index: usize) ?&Node {539 pub fn iterate(self: &ErrorSetDecl, index: usize) ?&Node {
546 var i = index;540 var i = index;
547541
548 if (i < self.decls.len) return *self.decls.at(i);542 if (i < self.decls.len) return self.decls.at(i).*;
549 i -= self.decls.len;543 i -= self.decls.len;
550544
551 return null;545 return null;
...@@ -598,10 +592,10 @@ pub const Node = struct {...@@ -598,10 +592,10 @@ pub const Node = struct {
598 i -= 1;592 i -= 1;
599 },593 },
600 InitArg.None,594 InitArg.None,
601 InitArg.Enum => { }595 InitArg.Enum => {},
602 }596 }
603597
604 if (i < self.fields_and_decls.len) return *self.fields_and_decls.at(i);598 if (i < self.fields_and_decls.len) return self.fields_and_decls.at(i).*;
605 i -= self.fields_and_decls.len;599 i -= self.fields_and_decls.len;
606600
607 return null;601 return null;
...@@ -814,7 +808,7 @@ pub const Node = struct {...@@ -814,7 +808,7 @@ pub const Node = struct {
814 i -= 1;808 i -= 1;
815 }809 }
816810
817 if (i < self.params.len) return *self.params.at(self.params.len - i - 1);811 if (i < self.params.len) return self.params.at(self.params.len - i - 1).*;
818 i -= self.params.len;812 i -= self.params.len;
819813
820 if (self.align_expr) |align_expr| {814 if (self.align_expr) |align_expr| {
...@@ -839,7 +833,6 @@ pub const Node = struct {...@@ -839,7 +833,6 @@ pub const Node = struct {
839 i -= 1;833 i -= 1;
840 }834 }
841835
842
843 return null;836 return null;
844 }837 }
845838
...@@ -934,7 +927,7 @@ pub const Node = struct {...@@ -934,7 +927,7 @@ pub const Node = struct {
934 pub fn iterate(self: &Block, index: usize) ?&Node {927 pub fn iterate(self: &Block, index: usize) ?&Node {
935 var i = index;928 var i = index;
936929
937 if (i < self.statements.len) return *self.statements.at(i);930 if (i < self.statements.len) return self.statements.at(i).*;
938 i -= self.statements.len;931 i -= self.statements.len;
939932
940 return null;933 return null;
...@@ -1119,6 +1112,7 @@ pub const Node = struct {...@@ -1119,6 +1112,7 @@ pub const Node = struct {
1119 base: Node,1112 base: Node,
1120 switch_token: TokenIndex,1113 switch_token: TokenIndex,
1121 expr: &Node,1114 expr: &Node,
1115
1122 /// these can be SwitchCase nodes or LineComment nodes1116 /// these can be SwitchCase nodes or LineComment nodes
1123 cases: CaseList,1117 cases: CaseList,
1124 rbrace: TokenIndex,1118 rbrace: TokenIndex,
...@@ -1131,7 +1125,7 @@ pub const Node = struct {...@@ -1131,7 +1125,7 @@ pub const Node = struct {
1131 if (i < 1) return self.expr;1125 if (i < 1) return self.expr;
1132 i -= 1;1126 i -= 1;
11331127
1134 if (i < self.cases.len) return *self.cases.at(i);1128 if (i < self.cases.len) return self.cases.at(i).*;
1135 i -= self.cases.len;1129 i -= self.cases.len;
11361130
1137 return null;1131 return null;
...@@ -1157,7 +1151,7 @@ pub const Node = struct {...@@ -1157,7 +1151,7 @@ pub const Node = struct {
1157 pub fn iterate(self: &SwitchCase, index: usize) ?&Node {1151 pub fn iterate(self: &SwitchCase, index: usize) ?&Node {
1158 var i = index;1152 var i = index;
11591153
1160 if (i < self.items.len) return *self.items.at(i);1154 if (i < self.items.len) return self.items.at(i).*;
1161 i -= self.items.len;1155 i -= self.items.len;
11621156
1163 if (self.payload) |payload| {1157 if (self.payload) |payload| {
...@@ -1172,7 +1166,7 @@ pub const Node = struct {...@@ -1172,7 +1166,7 @@ pub const Node = struct {
1172 }1166 }
11731167
1174 pub fn firstToken(self: &SwitchCase) TokenIndex {1168 pub fn firstToken(self: &SwitchCase) TokenIndex {
1175 return (*self.items.at(0)).firstToken();1169 return (self.items.at(0).*).firstToken();
1176 }1170 }
11771171
1178 pub fn lastToken(self: &SwitchCase) TokenIndex {1172 pub fn lastToken(self: &SwitchCase) TokenIndex {
...@@ -1616,7 +1610,7 @@ pub const Node = struct {...@@ -1616,7 +1610,7 @@ pub const Node = struct {
16161610
1617 switch (self.op) {1611 switch (self.op) {
1618 @TagType(Op).Call => |*call_info| {1612 @TagType(Op).Call => |*call_info| {
1619 if (i < call_info.params.len) return *call_info.params.at(i);1613 if (i < call_info.params.len) return call_info.params.at(i).*;
1620 i -= call_info.params.len;1614 i -= call_info.params.len;
1621 },1615 },
1622 Op.ArrayAccess => |index_expr| {1616 Op.ArrayAccess => |index_expr| {
...@@ -1633,11 +1627,11 @@ pub const Node = struct {...@@ -1633,11 +1627,11 @@ pub const Node = struct {
1633 }1627 }
1634 },1628 },
1635 Op.ArrayInitializer => |*exprs| {1629 Op.ArrayInitializer => |*exprs| {
1636 if (i < exprs.len) return *exprs.at(i);1630 if (i < exprs.len) return exprs.at(i).*;
1637 i -= exprs.len;1631 i -= exprs.len;
1638 },1632 },
1639 Op.StructInitializer => |*fields| {1633 Op.StructInitializer => |*fields| {
1640 if (i < fields.len) return *fields.at(i);1634 if (i < fields.len) return fields.at(i).*;
1641 i -= fields.len;1635 i -= fields.len;
1642 },1636 },
1643 }1637 }
...@@ -1830,7 +1824,7 @@ pub const Node = struct {...@@ -1830,7 +1824,7 @@ pub const Node = struct {
1830 pub fn iterate(self: &BuiltinCall, index: usize) ?&Node {1824 pub fn iterate(self: &BuiltinCall, index: usize) ?&Node {
1831 var i = index;1825 var i = index;
18321826
1833 if (i < self.params.len) return *self.params.at(i);1827 if (i < self.params.len) return self.params.at(i).*;
1834 i -= self.params.len;1828 i -= self.params.len;
18351829
1836 return null;1830 return null;
...@@ -1873,11 +1867,11 @@ pub const Node = struct {...@@ -1873,11 +1867,11 @@ pub const Node = struct {
1873 }1867 }
18741868
1875 pub fn firstToken(self: &MultilineStringLiteral) TokenIndex {1869 pub fn firstToken(self: &MultilineStringLiteral) TokenIndex {
1876 return *self.lines.at(0);1870 return self.lines.at(0).*;
1877 }1871 }
18781872
1879 pub fn lastToken(self: &MultilineStringLiteral) TokenIndex {1873 pub fn lastToken(self: &MultilineStringLiteral) TokenIndex {
1880 return *self.lines.at(self.lines.len - 1);1874 return self.lines.at(self.lines.len - 1).*;
1881 }1875 }
1882 };1876 };
18831877
...@@ -1974,7 +1968,7 @@ pub const Node = struct {...@@ -1974,7 +1968,7 @@ pub const Node = struct {
19741968
1975 const Kind = union(enum) {1969 const Kind = union(enum) {
1976 Variable: &Identifier,1970 Variable: &Identifier,
1977 Return: &Node1971 Return: &Node,
1978 };1972 };
19791973
1980 pub fn iterate(self: &AsmOutput, index: usize) ?&Node {1974 pub fn iterate(self: &AsmOutput, index: usize) ?&Node {
...@@ -1994,7 +1988,7 @@ pub const Node = struct {...@@ -1994,7 +1988,7 @@ pub const Node = struct {
1994 Kind.Return => |return_type| {1988 Kind.Return => |return_type| {
1995 if (i < 1) return return_type;1989 if (i < 1) return return_type;
1996 i -= 1;1990 i -= 1;
1997 }1991 },
1998 }1992 }
19991993
2000 return null;1994 return null;
...@@ -2059,13 +2053,13 @@ pub const Node = struct {...@@ -2059,13 +2053,13 @@ pub const Node = struct {
2059 pub fn iterate(self: &Asm, index: usize) ?&Node {2053 pub fn iterate(self: &Asm, index: usize) ?&Node {
2060 var i = index;2054 var i = index;
20612055
2062 if (i < self.outputs.len) return &(*self.outputs.at(index)).base;2056 if (i < self.outputs.len) return &(self.outputs.at(index).*).base;
2063 i -= self.outputs.len;2057 i -= self.outputs.len;
20642058
2065 if (i < self.inputs.len) return &(*self.inputs.at(index)).base;2059 if (i < self.inputs.len) return &(self.inputs.at(index).*).base;
2066 i -= self.inputs.len;2060 i -= self.inputs.len;
20672061
2068 if (i < self.clobbers.len) return *self.clobbers.at(index);2062 if (i < self.clobbers.len) return self.clobbers.at(index).*;
2069 i -= self.clobbers.len;2063 i -= self.clobbers.len;
20702064
2071 return null;2065 return null;
...@@ -2159,11 +2153,11 @@ pub const Node = struct {...@@ -2159,11 +2153,11 @@ pub const Node = struct {
2159 }2153 }
21602154
2161 pub fn firstToken(self: &DocComment) TokenIndex {2155 pub fn firstToken(self: &DocComment) TokenIndex {
2162 return *self.lines.at(0);2156 return self.lines.at(0).*;
2163 }2157 }
21642158
2165 pub fn lastToken(self: &DocComment) TokenIndex {2159 pub fn lastToken(self: &DocComment) TokenIndex {
2166 return *self.lines.at(self.lines.len - 1);2160 return self.lines.at(self.lines.len - 1).*;
2167 }2161 }
2168 };2162 };
21692163
...@@ -2192,4 +2186,3 @@ pub const Node = struct {...@@ -2192,4 +2186,3 @@ pub const Node = struct {
2192 }2186 }
2193 };2187 };
2194};2188};
2195
std/zig/parse.zig+930-1161
...@@ -17,15 +17,15 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -17,15 +17,15 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
17 defer stack.deinit();17 defer stack.deinit();
1818
19 const arena = &tree_arena.allocator;19 const arena = &tree_arena.allocator;
20 const root_node = try arena.construct(ast.Node.Root {20 const root_node = try arena.construct(ast.Node.Root{
21 .base = ast.Node { .id = ast.Node.Id.Root },21 .base = ast.Node{ .id = ast.Node.Id.Root },
22 .decls = ast.Node.Root.DeclList.init(arena),22 .decls = ast.Node.Root.DeclList.init(arena),
23 .doc_comments = null,23 .doc_comments = null,
24 // initialized when we get the eof token24 // initialized when we get the eof token
25 .eof_token = undefined,25 .eof_token = undefined,
26 });26 });
2727
28 var tree = ast.Tree {28 var tree = ast.Tree{
29 .source = source,29 .source = source,
30 .root_node = root_node,30 .root_node = root_node,
31 .arena_allocator = tree_arena,31 .arena_allocator = tree_arena,
...@@ -36,9 +36,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -36,9 +36,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
36 var tokenizer = Tokenizer.init(tree.source);36 var tokenizer = Tokenizer.init(tree.source);
37 while (true) {37 while (true) {
38 const token_ptr = try tree.tokens.addOne();38 const token_ptr = try tree.tokens.addOne();
39 *token_ptr = tokenizer.next();39 token_ptr.* = tokenizer.next();
40 if (token_ptr.id == Token.Id.Eof)40 if (token_ptr.id == Token.Id.Eof) break;
41 break;
42 }41 }
43 var tok_it = tree.tokens.iterator(0);42 var tok_it = tree.tokens.iterator(0);
4443
...@@ -63,33 +62,27 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -63,33 +62,27 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
63 Token.Id.Keyword_test => {62 Token.Id.Keyword_test => {
64 stack.append(State.TopLevel) catch unreachable;63 stack.append(State.TopLevel) catch unreachable;
6564
66 const block = try arena.construct(ast.Node.Block {65 const block = try arena.construct(ast.Node.Block{
67 .base = ast.Node {66 .base = ast.Node{ .id = ast.Node.Id.Block },
68 .id = ast.Node.Id.Block,
69 },
70 .label = null,67 .label = null,
71 .lbrace = undefined,68 .lbrace = undefined,
72 .statements = ast.Node.Block.StatementList.init(arena),69 .statements = ast.Node.Block.StatementList.init(arena),
73 .rbrace = undefined,70 .rbrace = undefined,
74 });71 });
75 const test_node = try arena.construct(ast.Node.TestDecl {72 const test_node = try arena.construct(ast.Node.TestDecl{
76 .base = ast.Node {73 .base = ast.Node{ .id = ast.Node.Id.TestDecl },
77 .id = ast.Node.Id.TestDecl,
78 },
79 .doc_comments = comments,74 .doc_comments = comments,
80 .test_token = token_index,75 .test_token = token_index,
81 .name = undefined,76 .name = undefined,
82 .body_node = &block.base,77 .body_node = &block.base,
83 });78 });
84 try root_node.decls.push(&test_node.base);79 try root_node.decls.push(&test_node.base);
85 try stack.append(State { .Block = block });80 try stack.append(State{ .Block = block });
86 try stack.append(State {81 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
87 .ExpectTokenSave = ExpectTokenSave {82 .id = Token.Id.LBrace,
88 .id = Token.Id.LBrace,83 .ptr = &block.rbrace,
89 .ptr = &block.rbrace,84 } });
90 }85 try stack.append(State{ .StringLiteral = OptionalCtx{ .Required = &test_node.name } });
91 });
92 try stack.append(State { .StringLiteral = OptionalCtx { .Required = &test_node.name } });
93 continue;86 continue;
94 },87 },
95 Token.Id.Eof => {88 Token.Id.Eof => {
...@@ -99,29 +92,25 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -99,29 +92,25 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
99 },92 },
100 Token.Id.Keyword_pub => {93 Token.Id.Keyword_pub => {
101 stack.append(State.TopLevel) catch unreachable;94 stack.append(State.TopLevel) catch unreachable;
102 try stack.append(State {95 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
103 .TopLevelExtern = TopLevelDeclCtx {96 .decls = &root_node.decls,
104 .decls = &root_node.decls,97 .visib_token = token_index,
105 .visib_token = token_index,98 .extern_export_inline_token = null,
106 .extern_export_inline_token = null,99 .lib_name = null,
107 .lib_name = null,100 .comments = comments,
108 .comments = comments,101 } });
109 }
110 });
111 continue;102 continue;
112 },103 },
113 Token.Id.Keyword_comptime => {104 Token.Id.Keyword_comptime => {
114 const block = try arena.construct(ast.Node.Block {105 const block = try arena.construct(ast.Node.Block{
115 .base = ast.Node {.id = ast.Node.Id.Block },106 .base = ast.Node{ .id = ast.Node.Id.Block },
116 .label = null,107 .label = null,
117 .lbrace = undefined,108 .lbrace = undefined,
118 .statements = ast.Node.Block.StatementList.init(arena),109 .statements = ast.Node.Block.StatementList.init(arena),
119 .rbrace = undefined,110 .rbrace = undefined,
120 });111 });
121 const node = try arena.construct(ast.Node.Comptime {112 const node = try arena.construct(ast.Node.Comptime{
122 .base = ast.Node {113 .base = ast.Node{ .id = ast.Node.Id.Comptime },
123 .id = ast.Node.Id.Comptime,
124 },
125 .comptime_token = token_index,114 .comptime_token = token_index,
126 .expr = &block.base,115 .expr = &block.base,
127 .doc_comments = comments,116 .doc_comments = comments,
...@@ -129,27 +118,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -129,27 +118,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
129 try root_node.decls.push(&node.base);118 try root_node.decls.push(&node.base);
130119
131 stack.append(State.TopLevel) catch unreachable;120 stack.append(State.TopLevel) catch unreachable;
132 try stack.append(State { .Block = block });121 try stack.append(State{ .Block = block });
133 try stack.append(State {122 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
134 .ExpectTokenSave = ExpectTokenSave {123 .id = Token.Id.LBrace,
135 .id = Token.Id.LBrace,124 .ptr = &block.rbrace,
136 .ptr = &block.rbrace,125 } });
137 }
138 });
139 continue;126 continue;
140 },127 },
141 else => {128 else => {
142 putBackToken(&tok_it, &tree);129 putBackToken(&tok_it, &tree);
143 stack.append(State.TopLevel) catch unreachable;130 stack.append(State.TopLevel) catch unreachable;
144 try stack.append(State {131 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
145 .TopLevelExtern = TopLevelDeclCtx {132 .decls = &root_node.decls,
146 .decls = &root_node.decls,133 .visib_token = null,
147 .visib_token = null,134 .extern_export_inline_token = null,
148 .extern_export_inline_token = null,135 .lib_name = null,
149 .lib_name = null,136 .comments = comments,
150 .comments = comments,137 } });
151 }
152 });
153 continue;138 continue;
154 },139 },
155 }140 }
...@@ -159,41 +144,38 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -159,41 +144,38 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
159 const token_index = token.index;144 const token_index = token.index;
160 const token_ptr = token.ptr;145 const token_ptr = token.ptr;
161 switch (token_ptr.id) {146 switch (token_ptr.id) {
162 Token.Id.Keyword_export, Token.Id.Keyword_inline => {147 Token.Id.Keyword_export,
163 stack.append(State {148 Token.Id.Keyword_inline => {
164 .TopLevelDecl = TopLevelDeclCtx {149 stack.append(State{ .TopLevelDecl = TopLevelDeclCtx{
165 .decls = ctx.decls,150 .decls = ctx.decls,
166 .visib_token = ctx.visib_token,151 .visib_token = ctx.visib_token,
167 .extern_export_inline_token = AnnotatedToken {152 .extern_export_inline_token = AnnotatedToken{
168 .index = token_index,153 .index = token_index,
169 .ptr = token_ptr,154 .ptr = token_ptr,
170 },
171 .lib_name = null,
172 .comments = ctx.comments,
173 },155 },
174 }) catch unreachable;156 .lib_name = null,
157 .comments = ctx.comments,
158 } }) catch unreachable;
175 continue;159 continue;
176 },160 },
177 Token.Id.Keyword_extern => {161 Token.Id.Keyword_extern => {
178 stack.append(State {162 stack.append(State{ .TopLevelLibname = TopLevelDeclCtx{
179 .TopLevelLibname = TopLevelDeclCtx {163 .decls = ctx.decls,
180 .decls = ctx.decls,164 .visib_token = ctx.visib_token,
181 .visib_token = ctx.visib_token,165 .extern_export_inline_token = AnnotatedToken{
182 .extern_export_inline_token = AnnotatedToken {166 .index = token_index,
183 .index = token_index,167 .ptr = token_ptr,
184 .ptr = token_ptr,
185 },
186 .lib_name = null,
187 .comments = ctx.comments,
188 },168 },
189 }) catch unreachable;169 .lib_name = null,
170 .comments = ctx.comments,
171 } }) catch unreachable;
190 continue;172 continue;
191 },173 },
192 else => {174 else => {
193 putBackToken(&tok_it, &tree);175 putBackToken(&tok_it, &tree);
194 stack.append(State { .TopLevelDecl = ctx }) catch unreachable;176 stack.append(State{ .TopLevelDecl = ctx }) catch unreachable;
195 continue;177 continue;
196 }178 },
197 }179 }
198 },180 },
199 State.TopLevelLibname => |ctx| {181 State.TopLevelLibname => |ctx| {
...@@ -207,15 +189,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -207,15 +189,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
207 };189 };
208 };190 };
209191
210 stack.append(State {192 stack.append(State{ .TopLevelDecl = TopLevelDeclCtx{
211 .TopLevelDecl = TopLevelDeclCtx {193 .decls = ctx.decls,
212 .decls = ctx.decls,194 .visib_token = ctx.visib_token,
213 .visib_token = ctx.visib_token,195 .extern_export_inline_token = ctx.extern_export_inline_token,
214 .extern_export_inline_token = ctx.extern_export_inline_token,196 .lib_name = lib_name,
215 .lib_name = lib_name,197 .comments = ctx.comments,
216 .comments = ctx.comments,198 } }) catch unreachable;
217 },
218 }) catch unreachable;
219 continue;199 continue;
220 },200 },
221 State.TopLevelDecl => |ctx| {201 State.TopLevelDecl => |ctx| {
...@@ -225,14 +205,12 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -225,14 +205,12 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
225 switch (token_ptr.id) {205 switch (token_ptr.id) {
226 Token.Id.Keyword_use => {206 Token.Id.Keyword_use => {
227 if (ctx.extern_export_inline_token) |annotated_token| {207 if (ctx.extern_export_inline_token) |annotated_token| {
228 *(try tree.errors.addOne()) = Error {208 ((try tree.errors.addOne())).* = Error{ .InvalidToken = Error.InvalidToken{ .token = annotated_token.index } };
229 .InvalidToken = Error.InvalidToken { .token = annotated_token.index },
230 };
231 return tree;209 return tree;
232 }210 }
233211
234 const node = try arena.construct(ast.Node.Use {212 const node = try arena.construct(ast.Node.Use{
235 .base = ast.Node {.id = ast.Node.Id.Use },213 .base = ast.Node{ .id = ast.Node.Id.Use },
236 .visib_token = ctx.visib_token,214 .visib_token = ctx.visib_token,
237 .expr = undefined,215 .expr = undefined,
238 .semicolon_token = undefined,216 .semicolon_token = undefined,
...@@ -240,44 +218,39 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -240,44 +218,39 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
240 });218 });
241 try ctx.decls.push(&node.base);219 try ctx.decls.push(&node.base);
242220
243 stack.append(State {221 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
244 .ExpectTokenSave = ExpectTokenSave {222 .id = Token.Id.Semicolon,
245 .id = Token.Id.Semicolon,223 .ptr = &node.semicolon_token,
246 .ptr = &node.semicolon_token,224 } }) catch unreachable;
247 }225 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
248 }) catch unreachable;
249 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });
250 continue;226 continue;
251 },227 },
252 Token.Id.Keyword_var, Token.Id.Keyword_const => {228 Token.Id.Keyword_var,
229 Token.Id.Keyword_const => {
253 if (ctx.extern_export_inline_token) |annotated_token| {230 if (ctx.extern_export_inline_token) |annotated_token| {
254 if (annotated_token.ptr.id == Token.Id.Keyword_inline) {231 if (annotated_token.ptr.id == Token.Id.Keyword_inline) {
255 *(try tree.errors.addOne()) = Error {232 ((try tree.errors.addOne())).* = Error{ .InvalidToken = Error.InvalidToken{ .token = annotated_token.index } };
256 .InvalidToken = Error.InvalidToken { .token = annotated_token.index },
257 };
258 return tree;233 return tree;
259 }234 }
260 }235 }
261236
262 try stack.append(State {237 try stack.append(State{ .VarDecl = VarDeclCtx{
263 .VarDecl = VarDeclCtx {238 .comments = ctx.comments,
264 .comments = ctx.comments,239 .visib_token = ctx.visib_token,
265 .visib_token = ctx.visib_token,240 .lib_name = ctx.lib_name,
266 .lib_name = ctx.lib_name,241 .comptime_token = null,
267 .comptime_token = null,242 .extern_export_token = if (ctx.extern_export_inline_token) |at| at.index else null,
268 .extern_export_token = if (ctx.extern_export_inline_token) |at| at.index else null,243 .mut_token = token_index,
269 .mut_token = token_index,244 .list = ctx.decls,
270 .list = ctx.decls245 } });
271 }246 continue;
272 });247 },
273 continue;248 Token.Id.Keyword_fn,
274 },249 Token.Id.Keyword_nakedcc,
275 Token.Id.Keyword_fn, Token.Id.Keyword_nakedcc,250 Token.Id.Keyword_stdcallcc,
276 Token.Id.Keyword_stdcallcc, Token.Id.Keyword_async => {251 Token.Id.Keyword_async => {
277 const fn_proto = try arena.construct(ast.Node.FnProto {252 const fn_proto = try arena.construct(ast.Node.FnProto{
278 .base = ast.Node {253 .base = ast.Node{ .id = ast.Node.Id.FnProto },
279 .id = ast.Node.Id.FnProto,
280 },
281 .doc_comments = ctx.comments,254 .doc_comments = ctx.comments,
282 .visib_token = ctx.visib_token,255 .visib_token = ctx.visib_token,
283 .name_token = null,256 .name_token = null,
...@@ -293,36 +266,33 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -293,36 +266,33 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
293 .align_expr = null,266 .align_expr = null,
294 });267 });
295 try ctx.decls.push(&fn_proto.base);268 try ctx.decls.push(&fn_proto.base);
296 stack.append(State { .FnDef = fn_proto }) catch unreachable;269 stack.append(State{ .FnDef = fn_proto }) catch unreachable;
297 try stack.append(State { .FnProto = fn_proto });270 try stack.append(State{ .FnProto = fn_proto });
298271
299 switch (token_ptr.id) {272 switch (token_ptr.id) {
300 Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => {273 Token.Id.Keyword_nakedcc,
274 Token.Id.Keyword_stdcallcc => {
301 fn_proto.cc_token = token_index;275 fn_proto.cc_token = token_index;
302 try stack.append(State {276 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
303 .ExpectTokenSave = ExpectTokenSave {277 .id = Token.Id.Keyword_fn,
304 .id = Token.Id.Keyword_fn,278 .ptr = &fn_proto.fn_token,
305 .ptr = &fn_proto.fn_token,279 } });
306 }
307 });
308 continue;280 continue;
309 },281 },
310 Token.Id.Keyword_async => {282 Token.Id.Keyword_async => {
311 const async_node = try arena.construct(ast.Node.AsyncAttribute {283 const async_node = try arena.construct(ast.Node.AsyncAttribute{
312 .base = ast.Node {.id = ast.Node.Id.AsyncAttribute },284 .base = ast.Node{ .id = ast.Node.Id.AsyncAttribute },
313 .async_token = token_index,285 .async_token = token_index,
314 .allocator_type = null,286 .allocator_type = null,
315 .rangle_bracket = null,287 .rangle_bracket = null,
316 });288 });
317 fn_proto.async_attr = async_node;289 fn_proto.async_attr = async_node;
318290
319 try stack.append(State {291 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
320 .ExpectTokenSave = ExpectTokenSave {292 .id = Token.Id.Keyword_fn,
321 .id = Token.Id.Keyword_fn,293 .ptr = &fn_proto.fn_token,
322 .ptr = &fn_proto.fn_token,294 } });
323 }295 try stack.append(State{ .AsyncAllocator = async_node });
324 });
325 try stack.append(State { .AsyncAllocator = async_node });
326 continue;296 continue;
327 },297 },
328 Token.Id.Keyword_fn => {298 Token.Id.Keyword_fn => {
...@@ -333,9 +303,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -333,9 +303,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
333 }303 }
334 },304 },
335 else => {305 else => {
336 *(try tree.errors.addOne()) = Error {306 ((try tree.errors.addOne())).* = Error{ .ExpectedVarDeclOrFn = Error.ExpectedVarDeclOrFn{ .token = token_index } };
337 .ExpectedVarDeclOrFn = Error.ExpectedVarDeclOrFn { .token = token_index },
338 };
339 return tree;307 return tree;
340 },308 },
341 }309 }
...@@ -343,34 +311,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -343,34 +311,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
343 State.TopLevelExternOrField => |ctx| {311 State.TopLevelExternOrField => |ctx| {
344 if (eatToken(&tok_it, &tree, Token.Id.Identifier)) |identifier| {312 if (eatToken(&tok_it, &tree, Token.Id.Identifier)) |identifier| {
345 std.debug.assert(ctx.container_decl.kind == ast.Node.ContainerDecl.Kind.Struct);313 std.debug.assert(ctx.container_decl.kind == ast.Node.ContainerDecl.Kind.Struct);
346 const node = try arena.construct(ast.Node.StructField {314 const node = try arena.construct(ast.Node.StructField{
347 .base = ast.Node {315 .base = ast.Node{ .id = ast.Node.Id.StructField },
348 .id = ast.Node.Id.StructField,
349 },
350 .doc_comments = ctx.comments,316 .doc_comments = ctx.comments,
351 .visib_token = ctx.visib_token,317 .visib_token = ctx.visib_token,
352 .name_token = identifier,318 .name_token = identifier,
353 .type_expr = undefined,319 .type_expr = undefined,
354 });320 });
355 const node_ptr = try ctx.container_decl.fields_and_decls.addOne();321 const node_ptr = try ctx.container_decl.fields_and_decls.addOne();
356 *node_ptr = &node.base;322 node_ptr.* = &node.base;
357323
358 stack.append(State { .FieldListCommaOrEnd = ctx.container_decl }) catch unreachable;324 stack.append(State{ .FieldListCommaOrEnd = ctx.container_decl }) catch unreachable;
359 try stack.append(State { .Expression = OptionalCtx { .Required = &node.type_expr } });325 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.type_expr } });
360 try stack.append(State { .ExpectToken = Token.Id.Colon });326 try stack.append(State{ .ExpectToken = Token.Id.Colon });
361 continue;327 continue;
362 }328 }
363329
364 stack.append(State{ .ContainerDecl = ctx.container_decl }) catch unreachable;330 stack.append(State{ .ContainerDecl = ctx.container_decl }) catch unreachable;
365 try stack.append(State {331 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
366 .TopLevelExtern = TopLevelDeclCtx {332 .decls = &ctx.container_decl.fields_and_decls,
367 .decls = &ctx.container_decl.fields_and_decls,333 .visib_token = ctx.visib_token,
368 .visib_token = ctx.visib_token,334 .extern_export_inline_token = null,
369 .extern_export_inline_token = null,335 .lib_name = null,
370 .lib_name = null,336 .comments = ctx.comments,
371 .comments = ctx.comments,337 } });
372 }
373 });
374 continue;338 continue;
375 },339 },
376340
...@@ -382,7 +346,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -382,7 +346,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
382 putBackToken(&tok_it, &tree);346 putBackToken(&tok_it, &tree);
383 continue;347 continue;
384 }348 }
385 stack.append(State { .Expression = ctx }) catch unreachable;349 stack.append(State{ .Expression = ctx }) catch unreachable;
386 continue;350 continue;
387 },351 },
388352
...@@ -390,8 +354,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -390,8 +354,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
390 const token = nextToken(&tok_it, &tree);354 const token = nextToken(&tok_it, &tree);
391 const token_index = token.index;355 const token_index = token.index;
392 const token_ptr = token.ptr;356 const token_ptr = token.ptr;
393 const node = try arena.construct(ast.Node.ContainerDecl {357 const node = try arena.construct(ast.Node.ContainerDecl{
394 .base = ast.Node {.id = ast.Node.Id.ContainerDecl },358 .base = ast.Node{ .id = ast.Node.Id.ContainerDecl },
395 .ltoken = ctx.ltoken,359 .ltoken = ctx.ltoken,
396 .layout = ctx.layout,360 .layout = ctx.layout,
397 .kind = switch (token_ptr.id) {361 .kind = switch (token_ptr.id) {
...@@ -399,9 +363,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -399,9 +363,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
399 Token.Id.Keyword_union => ast.Node.ContainerDecl.Kind.Union,363 Token.Id.Keyword_union => ast.Node.ContainerDecl.Kind.Union,
400 Token.Id.Keyword_enum => ast.Node.ContainerDecl.Kind.Enum,364 Token.Id.Keyword_enum => ast.Node.ContainerDecl.Kind.Enum,
401 else => {365 else => {
402 *(try tree.errors.addOne()) = Error {366 ((try tree.errors.addOne())).* = Error{ .ExpectedAggregateKw = Error.ExpectedAggregateKw{ .token = token_index } };
403 .ExpectedAggregateKw = Error.ExpectedAggregateKw { .token = token_index },
404 };
405 return tree;367 return tree;
406 },368 },
407 },369 },
...@@ -411,9 +373,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -411,9 +373,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
411 });373 });
412 ctx.opt_ctx.store(&node.base);374 ctx.opt_ctx.store(&node.base);
413375
414 stack.append(State { .ContainerDecl = node }) catch unreachable;376 stack.append(State{ .ContainerDecl = node }) catch unreachable;
415 try stack.append(State { .ExpectToken = Token.Id.LBrace });377 try stack.append(State{ .ExpectToken = Token.Id.LBrace });
416 try stack.append(State { .ContainerInitArgStart = node });378 try stack.append(State{ .ContainerInitArgStart = node });
417 continue;379 continue;
418 },380 },
419381
...@@ -422,8 +384,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -422,8 +384,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
422 continue;384 continue;
423 }385 }
424386
425 stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable;387 stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable;
426 try stack.append(State { .ContainerInitArg = container_decl });388 try stack.append(State{ .ContainerInitArg = container_decl });
427 continue;389 continue;
428 },390 },
429391
...@@ -433,23 +395,21 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -433,23 +395,21 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
433 const init_arg_token_ptr = init_arg_token.ptr;395 const init_arg_token_ptr = init_arg_token.ptr;
434 switch (init_arg_token_ptr.id) {396 switch (init_arg_token_ptr.id) {
435 Token.Id.Keyword_enum => {397 Token.Id.Keyword_enum => {
436 container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg {.Enum = null};398 container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg{ .Enum = null };
437 const lparen_tok = nextToken(&tok_it, &tree);399 const lparen_tok = nextToken(&tok_it, &tree);
438 const lparen_tok_index = lparen_tok.index;400 const lparen_tok_index = lparen_tok.index;
439 const lparen_tok_ptr = lparen_tok.ptr;401 const lparen_tok_ptr = lparen_tok.ptr;
440 if (lparen_tok_ptr.id == Token.Id.LParen) {402 if (lparen_tok_ptr.id == Token.Id.LParen) {
441 try stack.append(State { .ExpectToken = Token.Id.RParen } );403 try stack.append(State{ .ExpectToken = Token.Id.RParen });
442 try stack.append(State { .Expression = OptionalCtx {404 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &container_decl.init_arg_expr.Enum } });
443 .RequiredNull = &container_decl.init_arg_expr.Enum,
444 } });
445 } else {405 } else {
446 putBackToken(&tok_it, &tree);406 putBackToken(&tok_it, &tree);
447 }407 }
448 },408 },
449 else => {409 else => {
450 putBackToken(&tok_it, &tree);410 putBackToken(&tok_it, &tree);
451 container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg { .Type = undefined };411 container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg{ .Type = undefined };
452 stack.append(State { .Expression = OptionalCtx { .Required = &container_decl.init_arg_expr.Type } }) catch unreachable;412 stack.append(State{ .Expression = OptionalCtx{ .Required = &container_decl.init_arg_expr.Type } }) catch unreachable;
453 },413 },
454 }414 }
455 continue;415 continue;
...@@ -468,26 +428,24 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -468,26 +428,24 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
468 Token.Id.Identifier => {428 Token.Id.Identifier => {
469 switch (container_decl.kind) {429 switch (container_decl.kind) {
470 ast.Node.ContainerDecl.Kind.Struct => {430 ast.Node.ContainerDecl.Kind.Struct => {
471 const node = try arena.construct(ast.Node.StructField {431 const node = try arena.construct(ast.Node.StructField{
472 .base = ast.Node {432 .base = ast.Node{ .id = ast.Node.Id.StructField },
473 .id = ast.Node.Id.StructField,
474 },
475 .doc_comments = comments,433 .doc_comments = comments,
476 .visib_token = null,434 .visib_token = null,
477 .name_token = token_index,435 .name_token = token_index,
478 .type_expr = undefined,436 .type_expr = undefined,
479 });437 });
480 const node_ptr = try container_decl.fields_and_decls.addOne();438 const node_ptr = try container_decl.fields_and_decls.addOne();
481 *node_ptr = &node.base;439 node_ptr.* = &node.base;
482440
483 try stack.append(State { .FieldListCommaOrEnd = container_decl });441 try stack.append(State{ .FieldListCommaOrEnd = container_decl });
484 try stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.type_expr } });442 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.type_expr } });
485 try stack.append(State { .ExpectToken = Token.Id.Colon });443 try stack.append(State{ .ExpectToken = Token.Id.Colon });
486 continue;444 continue;
487 },445 },
488 ast.Node.ContainerDecl.Kind.Union => {446 ast.Node.ContainerDecl.Kind.Union => {
489 const node = try arena.construct(ast.Node.UnionTag {447 const node = try arena.construct(ast.Node.UnionTag{
490 .base = ast.Node {.id = ast.Node.Id.UnionTag },448 .base = ast.Node{ .id = ast.Node.Id.UnionTag },
491 .name_token = token_index,449 .name_token = token_index,
492 .type_expr = null,450 .type_expr = null,
493 .value_expr = null,451 .value_expr = null,
...@@ -495,24 +453,24 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -495,24 +453,24 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
495 });453 });
496 try container_decl.fields_and_decls.push(&node.base);454 try container_decl.fields_and_decls.push(&node.base);
497455
498 stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable;456 stack.append(State{ .FieldListCommaOrEnd = container_decl }) catch unreachable;
499 try stack.append(State { .FieldInitValue = OptionalCtx { .RequiredNull = &node.value_expr } });457 try stack.append(State{ .FieldInitValue = OptionalCtx{ .RequiredNull = &node.value_expr } });
500 try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &node.type_expr } });458 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .RequiredNull = &node.type_expr } });
501 try stack.append(State { .IfToken = Token.Id.Colon });459 try stack.append(State{ .IfToken = Token.Id.Colon });
502 continue;460 continue;
503 },461 },
504 ast.Node.ContainerDecl.Kind.Enum => {462 ast.Node.ContainerDecl.Kind.Enum => {
505 const node = try arena.construct(ast.Node.EnumTag {463 const node = try arena.construct(ast.Node.EnumTag{
506 .base = ast.Node { .id = ast.Node.Id.EnumTag },464 .base = ast.Node{ .id = ast.Node.Id.EnumTag },
507 .name_token = token_index,465 .name_token = token_index,
508 .value = null,466 .value = null,
509 .doc_comments = comments,467 .doc_comments = comments,
510 });468 });
511 try container_decl.fields_and_decls.push(&node.base);469 try container_decl.fields_and_decls.push(&node.base);
512470
513 stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable;471 stack.append(State{ .FieldListCommaOrEnd = container_decl }) catch unreachable;
514 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &node.value } });472 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &node.value } });
515 try stack.append(State { .IfToken = Token.Id.Equal });473 try stack.append(State{ .IfToken = Token.Id.Equal });
516 continue;474 continue;
517 },475 },
518 }476 }
...@@ -520,48 +478,40 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -520,48 +478,40 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
520 Token.Id.Keyword_pub => {478 Token.Id.Keyword_pub => {
521 switch (container_decl.kind) {479 switch (container_decl.kind) {
522 ast.Node.ContainerDecl.Kind.Struct => {480 ast.Node.ContainerDecl.Kind.Struct => {
523 try stack.append(State {481 try stack.append(State{ .TopLevelExternOrField = TopLevelExternOrFieldCtx{
524 .TopLevelExternOrField = TopLevelExternOrFieldCtx {482 .visib_token = token_index,
525 .visib_token = token_index,483 .container_decl = container_decl,
526 .container_decl = container_decl,484 .comments = comments,
527 .comments = comments,485 } });
528 }
529 });
530 continue;486 continue;
531 },487 },
532 else => {488 else => {
533 stack.append(State{ .ContainerDecl = container_decl }) catch unreachable;489 stack.append(State{ .ContainerDecl = container_decl }) catch unreachable;
534 try stack.append(State {490 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
535 .TopLevelExtern = TopLevelDeclCtx {491 .decls = &container_decl.fields_and_decls,
536 .decls = &container_decl.fields_and_decls,492 .visib_token = token_index,
537 .visib_token = token_index,493 .extern_export_inline_token = null,
538 .extern_export_inline_token = null,494 .lib_name = null,
539 .lib_name = null,495 .comments = comments,
540 .comments = comments,496 } });
541 }
542 });
543 continue;497 continue;
544 }498 },
545 }499 }
546 },500 },
547 Token.Id.Keyword_export => {501 Token.Id.Keyword_export => {
548 stack.append(State{ .ContainerDecl = container_decl }) catch unreachable;502 stack.append(State{ .ContainerDecl = container_decl }) catch unreachable;
549 try stack.append(State {503 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
550 .TopLevelExtern = TopLevelDeclCtx {504 .decls = &container_decl.fields_and_decls,
551 .decls = &container_decl.fields_and_decls,505 .visib_token = token_index,
552 .visib_token = token_index,506 .extern_export_inline_token = null,
553 .extern_export_inline_token = null,507 .lib_name = null,
554 .lib_name = null,508 .comments = comments,
555 .comments = comments,509 } });
556 }
557 });
558 continue;510 continue;
559 },511 },
560 Token.Id.RBrace => {512 Token.Id.RBrace => {
561 if (comments != null) {513 if (comments != null) {
562 *(try tree.errors.addOne()) = Error {514 ((try tree.errors.addOne())).* = Error{ .UnattachedDocComment = Error.UnattachedDocComment{ .token = token_index } };
563 .UnattachedDocComment = Error.UnattachedDocComment { .token = token_index },
564 };
565 return tree;515 return tree;
566 }516 }
567 container_decl.rbrace_token = token_index;517 container_decl.rbrace_token = token_index;
...@@ -570,26 +520,21 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -570,26 +520,21 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
570 else => {520 else => {
571 putBackToken(&tok_it, &tree);521 putBackToken(&tok_it, &tree);
572 stack.append(State{ .ContainerDecl = container_decl }) catch unreachable;522 stack.append(State{ .ContainerDecl = container_decl }) catch unreachable;
573 try stack.append(State {523 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
574 .TopLevelExtern = TopLevelDeclCtx {524 .decls = &container_decl.fields_and_decls,
575 .decls = &container_decl.fields_and_decls,525 .visib_token = null,
576 .visib_token = null,526 .extern_export_inline_token = null,
577 .extern_export_inline_token = null,527 .lib_name = null,
578 .lib_name = null,528 .comments = comments,
579 .comments = comments,529 } });
580 }
581 });
582 continue;530 continue;
583 }531 },
584 }532 }
585 },533 },
586534
587
588 State.VarDecl => |ctx| {535 State.VarDecl => |ctx| {
589 const var_decl = try arena.construct(ast.Node.VarDecl {536 const var_decl = try arena.construct(ast.Node.VarDecl{
590 .base = ast.Node {537 .base = ast.Node{ .id = ast.Node.Id.VarDecl },
591 .id = ast.Node.Id.VarDecl,
592 },
593 .doc_comments = ctx.comments,538 .doc_comments = ctx.comments,
594 .visib_token = ctx.visib_token,539 .visib_token = ctx.visib_token,
595 .mut_token = ctx.mut_token,540 .mut_token = ctx.mut_token,
...@@ -606,27 +551,25 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -606,27 +551,25 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
606 });551 });
607 try ctx.list.push(&var_decl.base);552 try ctx.list.push(&var_decl.base);
608553
609 try stack.append(State { .VarDeclAlign = var_decl });554 try stack.append(State{ .VarDeclAlign = var_decl });
610 try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &var_decl.type_node} });555 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .RequiredNull = &var_decl.type_node } });
611 try stack.append(State { .IfToken = Token.Id.Colon });556 try stack.append(State{ .IfToken = Token.Id.Colon });
612 try stack.append(State {557 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
613 .ExpectTokenSave = ExpectTokenSave {558 .id = Token.Id.Identifier,
614 .id = Token.Id.Identifier,559 .ptr = &var_decl.name_token,
615 .ptr = &var_decl.name_token,560 } });
616 }
617 });
618 continue;561 continue;
619 },562 },
620 State.VarDeclAlign => |var_decl| {563 State.VarDeclAlign => |var_decl| {
621 try stack.append(State { .VarDeclEq = var_decl });564 try stack.append(State{ .VarDeclEq = var_decl });
622565
623 const next_token = nextToken(&tok_it, &tree);566 const next_token = nextToken(&tok_it, &tree);
624 const next_token_index = next_token.index;567 const next_token_index = next_token.index;
625 const next_token_ptr = next_token.ptr;568 const next_token_ptr = next_token.ptr;
626 if (next_token_ptr.id == Token.Id.Keyword_align) {569 if (next_token_ptr.id == Token.Id.Keyword_align) {
627 try stack.append(State { .ExpectToken = Token.Id.RParen });570 try stack.append(State{ .ExpectToken = Token.Id.RParen });
628 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.align_node} });571 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &var_decl.align_node } });
629 try stack.append(State { .ExpectToken = Token.Id.LParen });572 try stack.append(State{ .ExpectToken = Token.Id.LParen });
630 continue;573 continue;
631 }574 }
632575
...@@ -640,8 +583,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -640,8 +583,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
640 switch (token_ptr.id) {583 switch (token_ptr.id) {
641 Token.Id.Equal => {584 Token.Id.Equal => {
642 var_decl.eq_token = token_index;585 var_decl.eq_token = token_index;
643 stack.append(State { .VarDeclSemiColon = var_decl }) catch unreachable;586 stack.append(State{ .VarDeclSemiColon = var_decl }) catch unreachable;
644 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.init_node } });587 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &var_decl.init_node } });
645 continue;588 continue;
646 },589 },
647 Token.Id.Semicolon => {590 Token.Id.Semicolon => {
...@@ -649,11 +592,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -649,11 +592,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
649 continue;592 continue;
650 },593 },
651 else => {594 else => {
652 *(try tree.errors.addOne()) = Error {595 ((try tree.errors.addOne())).* = Error{ .ExpectedEqOrSemi = Error.ExpectedEqOrSemi{ .token = token_index } };
653 .ExpectedEqOrSemi = Error.ExpectedEqOrSemi { .token = token_index },
654 };
655 return tree;596 return tree;
656 }597 },
657 }598 }
658 },599 },
659600
...@@ -661,12 +602,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -661,12 +602,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
661 const semicolon_token = nextToken(&tok_it, &tree);602 const semicolon_token = nextToken(&tok_it, &tree);
662603
663 if (semicolon_token.ptr.id != Token.Id.Semicolon) {604 if (semicolon_token.ptr.id != Token.Id.Semicolon) {
664 *(try tree.errors.addOne()) = Error {605 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
665 .ExpectedToken = Error.ExpectedToken {606 .token = semicolon_token.index,
666 .token = semicolon_token.index,607 .expected_id = Token.Id.Semicolon,
667 .expected_id = Token.Id.Semicolon,608 } };
668 },
669 };
670 return tree;609 return tree;
671 }610 }
672611
...@@ -686,32 +625,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -686,32 +625,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
686 const token = nextToken(&tok_it, &tree);625 const token = nextToken(&tok_it, &tree);
687 const token_index = token.index;626 const token_index = token.index;
688 const token_ptr = token.ptr;627 const token_ptr = token.ptr;
689 switch(token_ptr.id) {628 switch (token_ptr.id) {
690 Token.Id.LBrace => {629 Token.Id.LBrace => {
691 const block = try arena.construct(ast.Node.Block {630 const block = try arena.construct(ast.Node.Block{
692 .base = ast.Node { .id = ast.Node.Id.Block },631 .base = ast.Node{ .id = ast.Node.Id.Block },
693 .label = null,632 .label = null,
694 .lbrace = token_index,633 .lbrace = token_index,
695 .statements = ast.Node.Block.StatementList.init(arena),634 .statements = ast.Node.Block.StatementList.init(arena),
696 .rbrace = undefined,635 .rbrace = undefined,
697 });636 });
698 fn_proto.body_node = &block.base;637 fn_proto.body_node = &block.base;
699 stack.append(State { .Block = block }) catch unreachable;638 stack.append(State{ .Block = block }) catch unreachable;
700 continue;639 continue;
701 },640 },
702 Token.Id.Semicolon => continue,641 Token.Id.Semicolon => continue,
703 else => {642 else => {
704 *(try tree.errors.addOne()) = Error {643 ((try tree.errors.addOne())).* = Error{ .ExpectedSemiOrLBrace = Error.ExpectedSemiOrLBrace{ .token = token_index } };
705 .ExpectedSemiOrLBrace = Error.ExpectedSemiOrLBrace { .token = token_index },
706 };
707 return tree;644 return tree;
708 },645 },
709 }646 }
710 },647 },
711 State.FnProto => |fn_proto| {648 State.FnProto => |fn_proto| {
712 stack.append(State { .FnProtoAlign = fn_proto }) catch unreachable;649 stack.append(State{ .FnProtoAlign = fn_proto }) catch unreachable;
713 try stack.append(State { .ParamDecl = fn_proto });650 try stack.append(State{ .ParamDecl = fn_proto });
714 try stack.append(State { .ExpectToken = Token.Id.LParen });651 try stack.append(State{ .ExpectToken = Token.Id.LParen });
715652
716 if (eatToken(&tok_it, &tree, Token.Id.Identifier)) |name_token| {653 if (eatToken(&tok_it, &tree, Token.Id.Identifier)) |name_token| {
717 fn_proto.name_token = name_token;654 fn_proto.name_token = name_token;
...@@ -719,12 +656,12 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -719,12 +656,12 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
719 continue;656 continue;
720 },657 },
721 State.FnProtoAlign => |fn_proto| {658 State.FnProtoAlign => |fn_proto| {
722 stack.append(State { .FnProtoReturnType = fn_proto }) catch unreachable;659 stack.append(State{ .FnProtoReturnType = fn_proto }) catch unreachable;
723660
724 if (eatToken(&tok_it, &tree, Token.Id.Keyword_align)) |align_token| {661 if (eatToken(&tok_it, &tree, Token.Id.Keyword_align)) |align_token| {
725 try stack.append(State { .ExpectToken = Token.Id.RParen });662 try stack.append(State{ .ExpectToken = Token.Id.RParen });
726 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &fn_proto.align_expr } });663 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &fn_proto.align_expr } });
727 try stack.append(State { .ExpectToken = Token.Id.LParen });664 try stack.append(State{ .ExpectToken = Token.Id.LParen });
728 }665 }
729 continue;666 continue;
730 },667 },
...@@ -734,42 +671,37 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -734,42 +671,37 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
734 const token_ptr = token.ptr;671 const token_ptr = token.ptr;
735 switch (token_ptr.id) {672 switch (token_ptr.id) {
736 Token.Id.Bang => {673 Token.Id.Bang => {
737 fn_proto.return_type = ast.Node.FnProto.ReturnType { .InferErrorSet = undefined };674 fn_proto.return_type = ast.Node.FnProto.ReturnType{ .InferErrorSet = undefined };
738 stack.append(State {675 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &fn_proto.return_type.InferErrorSet } }) catch unreachable;
739 .TypeExprBegin = OptionalCtx { .Required = &fn_proto.return_type.InferErrorSet },
740 }) catch unreachable;
741 continue;676 continue;
742 },677 },
743 else => {678 else => {
744 // TODO: this is a special case. Remove this when #760 is fixed679 // TODO: this is a special case. Remove this when #760 is fixed
745 if (token_ptr.id == Token.Id.Keyword_error) {680 if (token_ptr.id == Token.Id.Keyword_error) {
746 if ((??tok_it.peek()).id == Token.Id.LBrace) {681 if ((??tok_it.peek()).id == Token.Id.LBrace) {
747 const error_type_node = try arena.construct(ast.Node.ErrorType {682 const error_type_node = try arena.construct(ast.Node.ErrorType{
748 .base = ast.Node { .id = ast.Node.Id.ErrorType },683 .base = ast.Node{ .id = ast.Node.Id.ErrorType },
749 .token = token_index,684 .token = token_index,
750 });685 });
751 fn_proto.return_type = ast.Node.FnProto.ReturnType {686 fn_proto.return_type = ast.Node.FnProto.ReturnType{ .Explicit = &error_type_node.base };
752 .Explicit = &error_type_node.base,
753 };
754 continue;687 continue;
755 }688 }
756 }689 }
757690
758 putBackToken(&tok_it, &tree);691 putBackToken(&tok_it, &tree);
759 fn_proto.return_type = ast.Node.FnProto.ReturnType { .Explicit = undefined };692 fn_proto.return_type = ast.Node.FnProto.ReturnType{ .Explicit = undefined };
760 stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &fn_proto.return_type.Explicit }, }) catch unreachable;693 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &fn_proto.return_type.Explicit } }) catch unreachable;
761 continue;694 continue;
762 },695 },
763 }696 }
764 },697 },
765698
766
767 State.ParamDecl => |fn_proto| {699 State.ParamDecl => |fn_proto| {
768 if (eatToken(&tok_it, &tree, Token.Id.RParen)) |_| {700 if (eatToken(&tok_it, &tree, Token.Id.RParen)) |_| {
769 continue;701 continue;
770 }702 }
771 const param_decl = try arena.construct(ast.Node.ParamDecl {703 const param_decl = try arena.construct(ast.Node.ParamDecl{
772 .base = ast.Node {.id = ast.Node.Id.ParamDecl },704 .base = ast.Node{ .id = ast.Node.Id.ParamDecl },
773 .comptime_token = null,705 .comptime_token = null,
774 .noalias_token = null,706 .noalias_token = null,
775 .name_token = null,707 .name_token = null,
...@@ -778,14 +710,12 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -778,14 +710,12 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
778 });710 });
779 try fn_proto.params.push(&param_decl.base);711 try fn_proto.params.push(&param_decl.base);
780712
781 stack.append(State {713 stack.append(State{ .ParamDeclEnd = ParamDeclEndCtx{
782 .ParamDeclEnd = ParamDeclEndCtx {714 .param_decl = param_decl,
783 .param_decl = param_decl,715 .fn_proto = fn_proto,
784 .fn_proto = fn_proto,716 } }) catch unreachable;
785 }717 try stack.append(State{ .ParamDeclName = param_decl });
786 }) catch unreachable;718 try stack.append(State{ .ParamDeclAliasOrComptime = param_decl });
787 try stack.append(State { .ParamDeclName = param_decl });
788 try stack.append(State { .ParamDeclAliasOrComptime = param_decl });
789 continue;719 continue;
790 },720 },
791 State.ParamDeclAliasOrComptime => |param_decl| {721 State.ParamDeclAliasOrComptime => |param_decl| {
...@@ -811,21 +741,19 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -811,21 +741,19 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
811 State.ParamDeclEnd => |ctx| {741 State.ParamDeclEnd => |ctx| {
812 if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {742 if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {
813 ctx.param_decl.var_args_token = ellipsis3;743 ctx.param_decl.var_args_token = ellipsis3;
814 stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable;744 stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable;
815 continue;745 continue;
816 }746 }
817747
818 try stack.append(State { .ParamDeclComma = ctx.fn_proto });748 try stack.append(State{ .ParamDeclComma = ctx.fn_proto });
819 try stack.append(State {749 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &ctx.param_decl.type_node } });
820 .TypeExprBegin = OptionalCtx { .Required = &ctx.param_decl.type_node }
821 });
822 continue;750 continue;
823 },751 },
824 State.ParamDeclComma => |fn_proto| {752 State.ParamDeclComma => |fn_proto| {
825 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RParen)) {753 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RParen)) {
826 ExpectCommaOrEndResult.end_token => |t| {754 ExpectCommaOrEndResult.end_token => |t| {
827 if (t == null) {755 if (t == null) {
828 stack.append(State { .ParamDecl = fn_proto }) catch unreachable;756 stack.append(State{ .ParamDecl = fn_proto }) catch unreachable;
829 }757 }
830 continue;758 continue;
831 },759 },
...@@ -838,12 +766,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -838,12 +766,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
838766
839 State.MaybeLabeledExpression => |ctx| {767 State.MaybeLabeledExpression => |ctx| {
840 if (eatToken(&tok_it, &tree, Token.Id.Colon)) |_| {768 if (eatToken(&tok_it, &tree, Token.Id.Colon)) |_| {
841 stack.append(State {769 stack.append(State{ .LabeledExpression = LabelCtx{
842 .LabeledExpression = LabelCtx {770 .label = ctx.label,
843 .label = ctx.label,771 .opt_ctx = ctx.opt_ctx,
844 .opt_ctx = ctx.opt_ctx,772 } }) catch unreachable;
845 }
846 }) catch unreachable;
847 continue;773 continue;
848 }774 }
849775
...@@ -856,69 +782,59 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -856,69 +782,59 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
856 const token_ptr = token.ptr;782 const token_ptr = token.ptr;
857 switch (token_ptr.id) {783 switch (token_ptr.id) {
858 Token.Id.LBrace => {784 Token.Id.LBrace => {
859 const block = try arena.construct(ast.Node.Block {785 const block = try arena.construct(ast.Node.Block{
860 .base = ast.Node {.id = ast.Node.Id.Block},786 .base = ast.Node{ .id = ast.Node.Id.Block },
861 .label = ctx.label,787 .label = ctx.label,
862 .lbrace = token_index,788 .lbrace = token_index,
863 .statements = ast.Node.Block.StatementList.init(arena),789 .statements = ast.Node.Block.StatementList.init(arena),
864 .rbrace = undefined,790 .rbrace = undefined,
865 });791 });
866 ctx.opt_ctx.store(&block.base);792 ctx.opt_ctx.store(&block.base);
867 stack.append(State { .Block = block }) catch unreachable;793 stack.append(State{ .Block = block }) catch unreachable;
868 continue;794 continue;
869 },795 },
870 Token.Id.Keyword_while => {796 Token.Id.Keyword_while => {
871 stack.append(State {797 stack.append(State{ .While = LoopCtx{
872 .While = LoopCtx {798 .label = ctx.label,
873 .label = ctx.label,799 .inline_token = null,
874 .inline_token = null,800 .loop_token = token_index,
875 .loop_token = token_index,801 .opt_ctx = ctx.opt_ctx.toRequired(),
876 .opt_ctx = ctx.opt_ctx.toRequired(),802 } }) catch unreachable;
877 }
878 }) catch unreachable;
879 continue;803 continue;
880 },804 },
881 Token.Id.Keyword_for => {805 Token.Id.Keyword_for => {
882 stack.append(State {806 stack.append(State{ .For = LoopCtx{
883 .For = LoopCtx {807 .label = ctx.label,
884 .label = ctx.label,808 .inline_token = null,
885 .inline_token = null,809 .loop_token = token_index,
886 .loop_token = token_index,810 .opt_ctx = ctx.opt_ctx.toRequired(),
887 .opt_ctx = ctx.opt_ctx.toRequired(),811 } }) catch unreachable;
888 }
889 }) catch unreachable;
890 continue;812 continue;
891 },813 },
892 Token.Id.Keyword_suspend => {814 Token.Id.Keyword_suspend => {
893 const node = try arena.construct(ast.Node.Suspend {815 const node = try arena.construct(ast.Node.Suspend{
894 .base = ast.Node {816 .base = ast.Node{ .id = ast.Node.Id.Suspend },
895 .id = ast.Node.Id.Suspend,
896 },
897 .label = ctx.label,817 .label = ctx.label,
898 .suspend_token = token_index,818 .suspend_token = token_index,
899 .payload = null,819 .payload = null,
900 .body = null,820 .body = null,
901 });821 });
902 ctx.opt_ctx.store(&node.base);822 ctx.opt_ctx.store(&node.base);
903 stack.append(State { .SuspendBody = node }) catch unreachable;823 stack.append(State{ .SuspendBody = node }) catch unreachable;
904 try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } });824 try stack.append(State{ .Payload = OptionalCtx{ .Optional = &node.payload } });
905 continue;825 continue;
906 },826 },
907 Token.Id.Keyword_inline => {827 Token.Id.Keyword_inline => {
908 stack.append(State {828 stack.append(State{ .Inline = InlineCtx{
909 .Inline = InlineCtx {829 .label = ctx.label,
910 .label = ctx.label,830 .inline_token = token_index,
911 .inline_token = token_index,831 .opt_ctx = ctx.opt_ctx.toRequired(),
912 .opt_ctx = ctx.opt_ctx.toRequired(),832 } }) catch unreachable;
913 }
914 }) catch unreachable;
915 continue;833 continue;
916 },834 },
917 else => {835 else => {
918 if (ctx.opt_ctx != OptionalCtx.Optional) {836 if (ctx.opt_ctx != OptionalCtx.Optional) {
919 *(try tree.errors.addOne()) = Error {837 ((try tree.errors.addOne())).* = Error{ .ExpectedLabelable = Error.ExpectedLabelable{ .token = token_index } };
920 .ExpectedLabelable = Error.ExpectedLabelable { .token = token_index },
921 };
922 return tree;838 return tree;
923 }839 }
924840
...@@ -933,32 +849,26 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -933,32 +849,26 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
933 const token_ptr = token.ptr;849 const token_ptr = token.ptr;
934 switch (token_ptr.id) {850 switch (token_ptr.id) {
935 Token.Id.Keyword_while => {851 Token.Id.Keyword_while => {
936 stack.append(State {852 stack.append(State{ .While = LoopCtx{
937 .While = LoopCtx {853 .inline_token = ctx.inline_token,
938 .inline_token = ctx.inline_token,854 .label = ctx.label,
939 .label = ctx.label,855 .loop_token = token_index,
940 .loop_token = token_index,856 .opt_ctx = ctx.opt_ctx.toRequired(),
941 .opt_ctx = ctx.opt_ctx.toRequired(),857 } }) catch unreachable;
942 }
943 }) catch unreachable;
944 continue;858 continue;
945 },859 },
946 Token.Id.Keyword_for => {860 Token.Id.Keyword_for => {
947 stack.append(State {861 stack.append(State{ .For = LoopCtx{
948 .For = LoopCtx {862 .inline_token = ctx.inline_token,
949 .inline_token = ctx.inline_token,863 .label = ctx.label,
950 .label = ctx.label,864 .loop_token = token_index,
951 .loop_token = token_index,865 .opt_ctx = ctx.opt_ctx.toRequired(),
952 .opt_ctx = ctx.opt_ctx.toRequired(),866 } }) catch unreachable;
953 }
954 }) catch unreachable;
955 continue;867 continue;
956 },868 },
957 else => {869 else => {
958 if (ctx.opt_ctx != OptionalCtx.Optional) {870 if (ctx.opt_ctx != OptionalCtx.Optional) {
959 *(try tree.errors.addOne()) = Error {871 ((try tree.errors.addOne())).* = Error{ .ExpectedInlinable = Error.ExpectedInlinable{ .token = token_index } };
960 .ExpectedInlinable = Error.ExpectedInlinable { .token = token_index },
961 };
962 return tree;872 return tree;
963 }873 }
964874
...@@ -968,8 +878,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -968,8 +878,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
968 }878 }
969 },879 },
970 State.While => |ctx| {880 State.While => |ctx| {
971 const node = try arena.construct(ast.Node.While {881 const node = try arena.construct(ast.Node.While{
972 .base = ast.Node {.id = ast.Node.Id.While },882 .base = ast.Node{ .id = ast.Node.Id.While },
973 .label = ctx.label,883 .label = ctx.label,
974 .inline_token = ctx.inline_token,884 .inline_token = ctx.inline_token,
975 .while_token = ctx.loop_token,885 .while_token = ctx.loop_token,
...@@ -980,25 +890,25 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -980,25 +890,25 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
980 .@"else" = null,890 .@"else" = null,
981 });891 });
982 ctx.opt_ctx.store(&node.base);892 ctx.opt_ctx.store(&node.base);
983 stack.append(State { .Else = &node.@"else" }) catch unreachable;893 stack.append(State{ .Else = &node.@"else" }) catch unreachable;
984 try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } });894 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } });
985 try stack.append(State { .WhileContinueExpr = &node.continue_expr });895 try stack.append(State{ .WhileContinueExpr = &node.continue_expr });
986 try stack.append(State { .IfToken = Token.Id.Colon });896 try stack.append(State{ .IfToken = Token.Id.Colon });
987 try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });897 try stack.append(State{ .PointerPayload = OptionalCtx{ .Optional = &node.payload } });
988 try stack.append(State { .ExpectToken = Token.Id.RParen });898 try stack.append(State{ .ExpectToken = Token.Id.RParen });
989 try stack.append(State { .Expression = OptionalCtx { .Required = &node.condition } });899 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.condition } });
990 try stack.append(State { .ExpectToken = Token.Id.LParen });900 try stack.append(State{ .ExpectToken = Token.Id.LParen });
991 continue;901 continue;
992 },902 },
993 State.WhileContinueExpr => |dest| {903 State.WhileContinueExpr => |dest| {
994 stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable;904 stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable;
995 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .RequiredNull = dest } });905 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .RequiredNull = dest } });
996 try stack.append(State { .ExpectToken = Token.Id.LParen });906 try stack.append(State{ .ExpectToken = Token.Id.LParen });
997 continue;907 continue;
998 },908 },
999 State.For => |ctx| {909 State.For => |ctx| {
1000 const node = try arena.construct(ast.Node.For {910 const node = try arena.construct(ast.Node.For{
1001 .base = ast.Node {.id = ast.Node.Id.For },911 .base = ast.Node{ .id = ast.Node.Id.For },
1002 .label = ctx.label,912 .label = ctx.label,
1003 .inline_token = ctx.inline_token,913 .inline_token = ctx.inline_token,
1004 .for_token = ctx.loop_token,914 .for_token = ctx.loop_token,
...@@ -1008,33 +918,32 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1008,33 +918,32 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1008 .@"else" = null,918 .@"else" = null,
1009 });919 });
1010 ctx.opt_ctx.store(&node.base);920 ctx.opt_ctx.store(&node.base);
1011 stack.append(State { .Else = &node.@"else" }) catch unreachable;921 stack.append(State{ .Else = &node.@"else" }) catch unreachable;
1012 try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } });922 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } });
1013 try stack.append(State { .PointerIndexPayload = OptionalCtx { .Optional = &node.payload } });923 try stack.append(State{ .PointerIndexPayload = OptionalCtx{ .Optional = &node.payload } });
1014 try stack.append(State { .ExpectToken = Token.Id.RParen });924 try stack.append(State{ .ExpectToken = Token.Id.RParen });
1015 try stack.append(State { .Expression = OptionalCtx { .Required = &node.array_expr } });925 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.array_expr } });
1016 try stack.append(State { .ExpectToken = Token.Id.LParen });926 try stack.append(State{ .ExpectToken = Token.Id.LParen });
1017 continue;927 continue;
1018 },928 },
1019 State.Else => |dest| {929 State.Else => |dest| {
1020 if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| {930 if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| {
1021 const node = try arena.construct(ast.Node.Else {931 const node = try arena.construct(ast.Node.Else{
1022 .base = ast.Node {.id = ast.Node.Id.Else },932 .base = ast.Node{ .id = ast.Node.Id.Else },
1023 .else_token = else_token,933 .else_token = else_token,
1024 .payload = null,934 .payload = null,
1025 .body = undefined,935 .body = undefined,
1026 });936 });
1027 *dest = node;937 dest.* = node;
1028938
1029 stack.append(State { .Expression = OptionalCtx { .Required = &node.body } }) catch unreachable;939 stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } }) catch unreachable;
1030 try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } });940 try stack.append(State{ .Payload = OptionalCtx{ .Optional = &node.payload } });
1031 continue;941 continue;
1032 } else {942 } else {
1033 continue;943 continue;
1034 }944 }
1035 },945 },
1036946
1037
1038 State.Block => |block| {947 State.Block => |block| {
1039 const token = nextToken(&tok_it, &tree);948 const token = nextToken(&tok_it, &tree);
1040 const token_index = token.index;949 const token_index = token.index;
...@@ -1046,7 +955,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1046,7 +955,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1046 },955 },
1047 else => {956 else => {
1048 putBackToken(&tok_it, &tree);957 putBackToken(&tok_it, &tree);
1049 stack.append(State { .Block = block }) catch unreachable;958 stack.append(State{ .Block = block }) catch unreachable;
1050959
1051 var any_comments = false;960 var any_comments = false;
1052 while (try eatLineComment(arena, &tok_it, &tree)) |line_comment| {961 while (try eatLineComment(arena, &tok_it, &tree)) |line_comment| {
...@@ -1055,7 +964,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1055,7 +964,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1055 }964 }
1056 if (any_comments) continue;965 if (any_comments) continue;
1057966
1058 try stack.append(State { .Statement = block });967 try stack.append(State{ .Statement = block });
1059 continue;968 continue;
1060 },969 },
1061 }970 }
...@@ -1066,33 +975,29 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1066,33 +975,29 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1066 const token_ptr = token.ptr;975 const token_ptr = token.ptr;
1067 switch (token_ptr.id) {976 switch (token_ptr.id) {
1068 Token.Id.Keyword_comptime => {977 Token.Id.Keyword_comptime => {
1069 stack.append(State {978 stack.append(State{ .ComptimeStatement = ComptimeStatementCtx{
1070 .ComptimeStatement = ComptimeStatementCtx {979 .comptime_token = token_index,
1071 .comptime_token = token_index,980 .block = block,
1072 .block = block,981 } }) catch unreachable;
1073 }
1074 }) catch unreachable;
1075 continue;
1076 },
1077 Token.Id.Keyword_var, Token.Id.Keyword_const => {
1078 stack.append(State {
1079 .VarDecl = VarDeclCtx {
1080 .comments = null,
1081 .visib_token = null,
1082 .comptime_token = null,
1083 .extern_export_token = null,
1084 .lib_name = null,
1085 .mut_token = token_index,
1086 .list = &block.statements,
1087 }
1088 }) catch unreachable;
1089 continue;982 continue;
1090 },983 },
1091 Token.Id.Keyword_defer, Token.Id.Keyword_errdefer => {984 Token.Id.Keyword_var,
1092 const node = try arena.construct(ast.Node.Defer {985 Token.Id.Keyword_const => {
1093 .base = ast.Node {986 stack.append(State{ .VarDecl = VarDeclCtx{
1094 .id = ast.Node.Id.Defer,987 .comments = null,
1095 },988 .visib_token = null,
989 .comptime_token = null,
990 .extern_export_token = null,
991 .lib_name = null,
992 .mut_token = token_index,
993 .list = &block.statements,
994 } }) catch unreachable;
995 continue;
996 },
997 Token.Id.Keyword_defer,
998 Token.Id.Keyword_errdefer => {
999 const node = try arena.construct(ast.Node.Defer{
1000 .base = ast.Node{ .id = ast.Node.Id.Defer },
1096 .defer_token = token_index,1001 .defer_token = token_index,
1097 .kind = switch (token_ptr.id) {1002 .kind = switch (token_ptr.id) {
1098 Token.Id.Keyword_defer => ast.Node.Defer.Kind.Unconditional,1003 Token.Id.Keyword_defer => ast.Node.Defer.Kind.Unconditional,
...@@ -1102,15 +1007,15 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1102,15 +1007,15 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1102 .expr = undefined,1007 .expr = undefined,
1103 });1008 });
1104 const node_ptr = try block.statements.addOne();1009 const node_ptr = try block.statements.addOne();
1105 *node_ptr = &node.base;1010 node_ptr.* = &node.base;
11061011
1107 stack.append(State { .Semicolon = node_ptr }) catch unreachable;1012 stack.append(State{ .Semicolon = node_ptr }) catch unreachable;
1108 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx{ .Required = &node.expr } });1013 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .Required = &node.expr } });
1109 continue;1014 continue;
1110 },1015 },
1111 Token.Id.LBrace => {1016 Token.Id.LBrace => {
1112 const inner_block = try arena.construct(ast.Node.Block {1017 const inner_block = try arena.construct(ast.Node.Block{
1113 .base = ast.Node { .id = ast.Node.Id.Block },1018 .base = ast.Node{ .id = ast.Node.Id.Block },
1114 .label = null,1019 .label = null,
1115 .lbrace = token_index,1020 .lbrace = token_index,
1116 .statements = ast.Node.Block.StatementList.init(arena),1021 .statements = ast.Node.Block.StatementList.init(arena),
...@@ -1118,16 +1023,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1118,16 +1023,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1118 });1023 });
1119 try block.statements.push(&inner_block.base);1024 try block.statements.push(&inner_block.base);
11201025
1121 stack.append(State { .Block = inner_block }) catch unreachable;1026 stack.append(State{ .Block = inner_block }) catch unreachable;
1122 continue;1027 continue;
1123 },1028 },
1124 else => {1029 else => {
1125 putBackToken(&tok_it, &tree);1030 putBackToken(&tok_it, &tree);
1126 const statement = try block.statements.addOne();1031 const statement = try block.statements.addOne();
1127 try stack.append(State { .Semicolon = statement });1032 try stack.append(State{ .Semicolon = statement });
1128 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx{ .Required = statement } });1033 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .Required = statement } });
1129 continue;1034 continue;
1130 }1035 },
1131 }1036 }
1132 },1037 },
1133 State.ComptimeStatement => |ctx| {1038 State.ComptimeStatement => |ctx| {
...@@ -1135,34 +1040,33 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1135,34 +1040,33 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1135 const token_index = token.index;1040 const token_index = token.index;
1136 const token_ptr = token.ptr;1041 const token_ptr = token.ptr;
1137 switch (token_ptr.id) {1042 switch (token_ptr.id) {
1138 Token.Id.Keyword_var, Token.Id.Keyword_const => {1043 Token.Id.Keyword_var,
1139 stack.append(State {1044 Token.Id.Keyword_const => {
1140 .VarDecl = VarDeclCtx {1045 stack.append(State{ .VarDecl = VarDeclCtx{
1141 .comments = null,1046 .comments = null,
1142 .visib_token = null,1047 .visib_token = null,
1143 .comptime_token = ctx.comptime_token,1048 .comptime_token = ctx.comptime_token,
1144 .extern_export_token = null,1049 .extern_export_token = null,
1145 .lib_name = null,1050 .lib_name = null,
1146 .mut_token = token_index,1051 .mut_token = token_index,
1147 .list = &ctx.block.statements,1052 .list = &ctx.block.statements,
1148 }1053 } }) catch unreachable;
1149 }) catch unreachable;
1150 continue;1054 continue;
1151 },1055 },
1152 else => {1056 else => {
1153 putBackToken(&tok_it, &tree);1057 putBackToken(&tok_it, &tree);
1154 putBackToken(&tok_it, &tree);1058 putBackToken(&tok_it, &tree);
1155 const statement = try ctx.block.statements.addOne();1059 const statement = try ctx.block.statements.addOne();
1156 try stack.append(State { .Semicolon = statement });1060 try stack.append(State{ .Semicolon = statement });
1157 try stack.append(State { .Expression = OptionalCtx { .Required = statement } });1061 try stack.append(State{ .Expression = OptionalCtx{ .Required = statement } });
1158 continue;1062 continue;
1159 }1063 },
1160 }1064 }
1161 },1065 },
1162 State.Semicolon => |node_ptr| {1066 State.Semicolon => |node_ptr| {
1163 const node = *node_ptr;1067 const node = node_ptr.*;
1164 if (node.requireSemiColon()) {1068 if (node.requireSemiColon()) {
1165 stack.append(State { .ExpectToken = Token.Id.Semicolon }) catch unreachable;1069 stack.append(State{ .ExpectToken = Token.Id.Semicolon }) catch unreachable;
1166 continue;1070 continue;
1167 }1071 }
1168 continue;1072 continue;
...@@ -1177,22 +1081,22 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1177,22 +1081,22 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1177 continue;1081 continue;
1178 }1082 }
11791083
1180 const node = try arena.construct(ast.Node.AsmOutput {1084 const node = try arena.construct(ast.Node.AsmOutput{
1181 .base = ast.Node {.id = ast.Node.Id.AsmOutput },1085 .base = ast.Node{ .id = ast.Node.Id.AsmOutput },
1182 .symbolic_name = undefined,1086 .symbolic_name = undefined,
1183 .constraint = undefined,1087 .constraint = undefined,
1184 .kind = undefined,1088 .kind = undefined,
1185 });1089 });
1186 try items.push(node);1090 try items.push(node);
11871091
1188 stack.append(State { .AsmOutputItems = items }) catch unreachable;1092 stack.append(State{ .AsmOutputItems = items }) catch unreachable;
1189 try stack.append(State { .IfToken = Token.Id.Comma });1093 try stack.append(State{ .IfToken = Token.Id.Comma });
1190 try stack.append(State { .ExpectToken = Token.Id.RParen });1094 try stack.append(State{ .ExpectToken = Token.Id.RParen });
1191 try stack.append(State { .AsmOutputReturnOrType = node });1095 try stack.append(State{ .AsmOutputReturnOrType = node });
1192 try stack.append(State { .ExpectToken = Token.Id.LParen });1096 try stack.append(State{ .ExpectToken = Token.Id.LParen });
1193 try stack.append(State { .StringLiteral = OptionalCtx { .Required = &node.constraint } });1097 try stack.append(State{ .StringLiteral = OptionalCtx{ .Required = &node.constraint } });
1194 try stack.append(State { .ExpectToken = Token.Id.RBracket });1098 try stack.append(State{ .ExpectToken = Token.Id.RBracket });
1195 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.symbolic_name } });1099 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.symbolic_name } });
1196 continue;1100 continue;
1197 },1101 },
1198 State.AsmOutputReturnOrType => |node| {1102 State.AsmOutputReturnOrType => |node| {
...@@ -1201,20 +1105,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1201,20 +1105,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1201 const token_ptr = token.ptr;1105 const token_ptr = token.ptr;
1202 switch (token_ptr.id) {1106 switch (token_ptr.id) {
1203 Token.Id.Identifier => {1107 Token.Id.Identifier => {
1204 node.kind = ast.Node.AsmOutput.Kind { .Variable = try createLiteral(arena, ast.Node.Identifier, token_index) };1108 node.kind = ast.Node.AsmOutput.Kind{ .Variable = try createLiteral(arena, ast.Node.Identifier, token_index) };
1205 continue;1109 continue;
1206 },1110 },
1207 Token.Id.Arrow => {1111 Token.Id.Arrow => {
1208 node.kind = ast.Node.AsmOutput.Kind { .Return = undefined };1112 node.kind = ast.Node.AsmOutput.Kind{ .Return = undefined };
1209 try stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.kind.Return } });1113 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.kind.Return } });
1210 continue;1114 continue;
1211 },1115 },
1212 else => {1116 else => {
1213 *(try tree.errors.addOne()) = Error {1117 ((try tree.errors.addOne())).* = Error{ .ExpectedAsmOutputReturnOrType = Error.ExpectedAsmOutputReturnOrType{ .token = token_index } };
1214 .ExpectedAsmOutputReturnOrType = Error.ExpectedAsmOutputReturnOrType {
1215 .token = token_index,
1216 },
1217 };
1218 return tree;1118 return tree;
1219 },1119 },
1220 }1120 }
...@@ -1228,49 +1128,48 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1228,49 +1128,48 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1228 continue;1128 continue;
1229 }1129 }
12301130
1231 const node = try arena.construct(ast.Node.AsmInput {1131 const node = try arena.construct(ast.Node.AsmInput{
1232 .base = ast.Node {.id = ast.Node.Id.AsmInput },1132 .base = ast.Node{ .id = ast.Node.Id.AsmInput },
1233 .symbolic_name = undefined,1133 .symbolic_name = undefined,
1234 .constraint = undefined,1134 .constraint = undefined,
1235 .expr = undefined,1135 .expr = undefined,
1236 });1136 });
1237 try items.push(node);1137 try items.push(node);
12381138
1239 stack.append(State { .AsmInputItems = items }) catch unreachable;1139 stack.append(State{ .AsmInputItems = items }) catch unreachable;
1240 try stack.append(State { .IfToken = Token.Id.Comma });1140 try stack.append(State{ .IfToken = Token.Id.Comma });
1241 try stack.append(State { .ExpectToken = Token.Id.RParen });1141 try stack.append(State{ .ExpectToken = Token.Id.RParen });
1242 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });1142 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
1243 try stack.append(State { .ExpectToken = Token.Id.LParen });1143 try stack.append(State{ .ExpectToken = Token.Id.LParen });
1244 try stack.append(State { .StringLiteral = OptionalCtx { .Required = &node.constraint } });1144 try stack.append(State{ .StringLiteral = OptionalCtx{ .Required = &node.constraint } });
1245 try stack.append(State { .ExpectToken = Token.Id.RBracket });1145 try stack.append(State{ .ExpectToken = Token.Id.RBracket });
1246 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.symbolic_name } });1146 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.symbolic_name } });
1247 continue;1147 continue;
1248 },1148 },
1249 State.AsmClobberItems => |items| {1149 State.AsmClobberItems => |items| {
1250 stack.append(State { .AsmClobberItems = items }) catch unreachable;1150 stack.append(State{ .AsmClobberItems = items }) catch unreachable;
1251 try stack.append(State { .IfToken = Token.Id.Comma });1151 try stack.append(State{ .IfToken = Token.Id.Comma });
1252 try stack.append(State { .StringLiteral = OptionalCtx { .Required = try items.addOne() } });1152 try stack.append(State{ .StringLiteral = OptionalCtx{ .Required = try items.addOne() } });
1253 continue;1153 continue;
1254 },1154 },
12551155
1256
1257 State.ExprListItemOrEnd => |list_state| {1156 State.ExprListItemOrEnd => |list_state| {
1258 if (eatToken(&tok_it, &tree, list_state.end)) |token_index| {1157 if (eatToken(&tok_it, &tree, list_state.end)) |token_index| {
1259 *list_state.ptr = token_index;1158 (list_state.ptr).* = token_index;
1260 continue;1159 continue;
1261 }1160 }
12621161
1263 stack.append(State { .ExprListCommaOrEnd = list_state }) catch unreachable;1162 stack.append(State{ .ExprListCommaOrEnd = list_state }) catch unreachable;
1264 try stack.append(State { .Expression = OptionalCtx { .Required = try list_state.list.addOne() } });1163 try stack.append(State{ .Expression = OptionalCtx{ .Required = try list_state.list.addOne() } });
1265 continue;1164 continue;
1266 },1165 },
1267 State.ExprListCommaOrEnd => |list_state| {1166 State.ExprListCommaOrEnd => |list_state| {
1268 switch (expectCommaOrEnd(&tok_it, &tree, list_state.end)) {1167 switch (expectCommaOrEnd(&tok_it, &tree, list_state.end)) {
1269 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {1168 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {
1270 *list_state.ptr = end;1169 (list_state.ptr).* = end;
1271 continue;1170 continue;
1272 } else {1171 } else {
1273 stack.append(State { .ExprListItemOrEnd = list_state }) catch unreachable;1172 stack.append(State{ .ExprListItemOrEnd = list_state }) catch unreachable;
1274 continue;1173 continue;
1275 },1174 },
1276 ExpectCommaOrEndResult.parse_error => |e| {1175 ExpectCommaOrEndResult.parse_error => |e| {
...@@ -1285,44 +1184,38 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1285,44 +1184,38 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1285 }1184 }
12861185
1287 if (eatToken(&tok_it, &tree, Token.Id.RBrace)) |rbrace| {1186 if (eatToken(&tok_it, &tree, Token.Id.RBrace)) |rbrace| {
1288 *list_state.ptr = rbrace;1187 (list_state.ptr).* = rbrace;
1289 continue;1188 continue;
1290 }1189 }
12911190
1292 const node = try arena.construct(ast.Node.FieldInitializer {1191 const node = try arena.construct(ast.Node.FieldInitializer{
1293 .base = ast.Node {1192 .base = ast.Node{ .id = ast.Node.Id.FieldInitializer },
1294 .id = ast.Node.Id.FieldInitializer,
1295 },
1296 .period_token = undefined,1193 .period_token = undefined,
1297 .name_token = undefined,1194 .name_token = undefined,
1298 .expr = undefined,1195 .expr = undefined,
1299 });1196 });
1300 try list_state.list.push(&node.base);1197 try list_state.list.push(&node.base);
13011198
1302 stack.append(State { .FieldInitListCommaOrEnd = list_state }) catch unreachable;1199 stack.append(State{ .FieldInitListCommaOrEnd = list_state }) catch unreachable;
1303 try stack.append(State { .Expression = OptionalCtx{ .Required = &node.expr } });1200 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
1304 try stack.append(State { .ExpectToken = Token.Id.Equal });1201 try stack.append(State{ .ExpectToken = Token.Id.Equal });
1305 try stack.append(State {1202 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1306 .ExpectTokenSave = ExpectTokenSave {1203 .id = Token.Id.Identifier,
1307 .id = Token.Id.Identifier,1204 .ptr = &node.name_token,
1308 .ptr = &node.name_token,1205 } });
1309 }1206 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1310 });1207 .id = Token.Id.Period,
1311 try stack.append(State {1208 .ptr = &node.period_token,
1312 .ExpectTokenSave = ExpectTokenSave {1209 } });
1313 .id = Token.Id.Period,
1314 .ptr = &node.period_token,
1315 }
1316 });
1317 continue;1210 continue;
1318 },1211 },
1319 State.FieldInitListCommaOrEnd => |list_state| {1212 State.FieldInitListCommaOrEnd => |list_state| {
1320 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RBrace)) {1213 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RBrace)) {
1321 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {1214 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {
1322 *list_state.ptr = end;1215 (list_state.ptr).* = end;
1323 continue;1216 continue;
1324 } else {1217 } else {
1325 stack.append(State { .FieldInitListItemOrEnd = list_state }) catch unreachable;1218 stack.append(State{ .FieldInitListItemOrEnd = list_state }) catch unreachable;
1326 continue;1219 continue;
1327 },1220 },
1328 ExpectCommaOrEndResult.parse_error => |e| {1221 ExpectCommaOrEndResult.parse_error => |e| {
...@@ -1337,7 +1230,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1337,7 +1230,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1337 container_decl.rbrace_token = end;1230 container_decl.rbrace_token = end;
1338 continue;1231 continue;
1339 } else {1232 } else {
1340 try stack.append(State { .ContainerDecl = container_decl });1233 try stack.append(State{ .ContainerDecl = container_decl });
1341 continue;1234 continue;
1342 },1235 },
1343 ExpectCommaOrEndResult.parse_error => |e| {1236 ExpectCommaOrEndResult.parse_error => |e| {
...@@ -1352,23 +1245,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1352,23 +1245,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1352 }1245 }
13531246
1354 if (eatToken(&tok_it, &tree, Token.Id.RBrace)) |rbrace| {1247 if (eatToken(&tok_it, &tree, Token.Id.RBrace)) |rbrace| {
1355 *list_state.ptr = rbrace;1248 (list_state.ptr).* = rbrace;
1356 continue;1249 continue;
1357 }1250 }
13581251
1359 const node_ptr = try list_state.list.addOne();1252 const node_ptr = try list_state.list.addOne();
13601253
1361 try stack.append(State { .ErrorTagListCommaOrEnd = list_state });1254 try stack.append(State{ .ErrorTagListCommaOrEnd = list_state });
1362 try stack.append(State { .ErrorTag = node_ptr });1255 try stack.append(State{ .ErrorTag = node_ptr });
1363 continue;1256 continue;
1364 },1257 },
1365 State.ErrorTagListCommaOrEnd => |list_state| {1258 State.ErrorTagListCommaOrEnd => |list_state| {
1366 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RBrace)) {1259 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RBrace)) {
1367 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {1260 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {
1368 *list_state.ptr = end;1261 (list_state.ptr).* = end;
1369 continue;1262 continue;
1370 } else {1263 } else {
1371 stack.append(State { .ErrorTagListItemOrEnd = list_state }) catch unreachable;1264 stack.append(State{ .ErrorTagListItemOrEnd = list_state }) catch unreachable;
1372 continue;1265 continue;
1373 },1266 },
1374 ExpectCommaOrEndResult.parse_error => |e| {1267 ExpectCommaOrEndResult.parse_error => |e| {
...@@ -1383,24 +1276,22 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1383,24 +1276,22 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1383 }1276 }
13841277
1385 if (eatToken(&tok_it, &tree, Token.Id.RBrace)) |rbrace| {1278 if (eatToken(&tok_it, &tree, Token.Id.RBrace)) |rbrace| {
1386 *list_state.ptr = rbrace;1279 (list_state.ptr).* = rbrace;
1387 continue;1280 continue;
1388 }1281 }
13891282
1390 const comments = try eatDocComments(arena, &tok_it, &tree);1283 const comments = try eatDocComments(arena, &tok_it, &tree);
1391 const node = try arena.construct(ast.Node.SwitchCase {1284 const node = try arena.construct(ast.Node.SwitchCase{
1392 .base = ast.Node {1285 .base = ast.Node{ .id = ast.Node.Id.SwitchCase },
1393 .id = ast.Node.Id.SwitchCase,
1394 },
1395 .items = ast.Node.SwitchCase.ItemList.init(arena),1286 .items = ast.Node.SwitchCase.ItemList.init(arena),
1396 .payload = null,1287 .payload = null,
1397 .expr = undefined,1288 .expr = undefined,
1398 });1289 });
1399 try list_state.list.push(&node.base);1290 try list_state.list.push(&node.base);
1400 try stack.append(State { .SwitchCaseCommaOrEnd = list_state });1291 try stack.append(State{ .SwitchCaseCommaOrEnd = list_state });
1401 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .Required = &node.expr } });1292 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .Required = &node.expr } });
1402 try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });1293 try stack.append(State{ .PointerPayload = OptionalCtx{ .Optional = &node.payload } });
1403 try stack.append(State { .SwitchCaseFirstItem = &node.items });1294 try stack.append(State{ .SwitchCaseFirstItem = &node.items });
14041295
1405 continue;1296 continue;
1406 },1297 },
...@@ -1408,10 +1299,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1408,10 +1299,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1408 State.SwitchCaseCommaOrEnd => |list_state| {1299 State.SwitchCaseCommaOrEnd => |list_state| {
1409 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RParen)) {1300 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RParen)) {
1410 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {1301 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {
1411 *list_state.ptr = end;1302 (list_state.ptr).* = end;
1412 continue;1303 continue;
1413 } else {1304 } else {
1414 try stack.append(State { .SwitchCaseOrEnd = list_state });1305 try stack.append(State{ .SwitchCaseOrEnd = list_state });
1415 continue;1306 continue;
1416 },1307 },
1417 ExpectCommaOrEndResult.parse_error => |e| {1308 ExpectCommaOrEndResult.parse_error => |e| {
...@@ -1426,29 +1317,29 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1426,29 +1317,29 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1426 const token_index = token.index;1317 const token_index = token.index;
1427 const token_ptr = token.ptr;1318 const token_ptr = token.ptr;
1428 if (token_ptr.id == Token.Id.Keyword_else) {1319 if (token_ptr.id == Token.Id.Keyword_else) {
1429 const else_node = try arena.construct(ast.Node.SwitchElse {1320 const else_node = try arena.construct(ast.Node.SwitchElse{
1430 .base = ast.Node{ .id = ast.Node.Id.SwitchElse},1321 .base = ast.Node{ .id = ast.Node.Id.SwitchElse },
1431 .token = token_index,1322 .token = token_index,
1432 });1323 });
1433 try case_items.push(&else_node.base);1324 try case_items.push(&else_node.base);
14341325
1435 try stack.append(State { .ExpectToken = Token.Id.EqualAngleBracketRight });1326 try stack.append(State{ .ExpectToken = Token.Id.EqualAngleBracketRight });
1436 continue;1327 continue;
1437 } else {1328 } else {
1438 putBackToken(&tok_it, &tree);1329 putBackToken(&tok_it, &tree);
1439 try stack.append(State { .SwitchCaseItem = case_items });1330 try stack.append(State{ .SwitchCaseItem = case_items });
1440 continue;1331 continue;
1441 }1332 }
1442 },1333 },
1443 State.SwitchCaseItem => |case_items| {1334 State.SwitchCaseItem => |case_items| {
1444 stack.append(State { .SwitchCaseItemCommaOrEnd = case_items }) catch unreachable;1335 stack.append(State{ .SwitchCaseItemCommaOrEnd = case_items }) catch unreachable;
1445 try stack.append(State { .RangeExpressionBegin = OptionalCtx { .Required = try case_items.addOne() } });1336 try stack.append(State{ .RangeExpressionBegin = OptionalCtx{ .Required = try case_items.addOne() } });
1446 },1337 },
1447 State.SwitchCaseItemCommaOrEnd => |case_items| {1338 State.SwitchCaseItemCommaOrEnd => |case_items| {
1448 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.EqualAngleBracketRight)) {1339 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.EqualAngleBracketRight)) {
1449 ExpectCommaOrEndResult.end_token => |t| {1340 ExpectCommaOrEndResult.end_token => |t| {
1450 if (t == null) {1341 if (t == null) {
1451 stack.append(State { .SwitchCaseItem = case_items }) catch unreachable;1342 stack.append(State{ .SwitchCaseItem = case_items }) catch unreachable;
1452 }1343 }
1453 continue;1344 continue;
1454 },1345 },
...@@ -1460,10 +1351,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1460,10 +1351,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1460 continue;1351 continue;
1461 },1352 },
14621353
1463
1464 State.SuspendBody => |suspend_node| {1354 State.SuspendBody => |suspend_node| {
1465 if (suspend_node.payload != null) {1355 if (suspend_node.payload != null) {
1466 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .RequiredNull = &suspend_node.body } });1356 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .RequiredNull = &suspend_node.body } });
1467 }1357 }
1468 continue;1358 continue;
1469 },1359 },
...@@ -1473,13 +1363,11 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1473,13 +1363,11 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1473 }1363 }
14741364
1475 async_node.rangle_bracket = TokenIndex(0);1365 async_node.rangle_bracket = TokenIndex(0);
1476 try stack.append(State {1366 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1477 .ExpectTokenSave = ExpectTokenSave {1367 .id = Token.Id.AngleBracketRight,
1478 .id = Token.Id.AngleBracketRight,1368 .ptr = &??async_node.rangle_bracket,
1479 .ptr = &??async_node.rangle_bracket,1369 } });
1480 }1370 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .RequiredNull = &async_node.allocator_type } });
1481 });
1482 try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &async_node.allocator_type } });
1483 continue;1371 continue;
1484 },1372 },
1485 State.AsyncEnd => |ctx| {1373 State.AsyncEnd => |ctx| {
...@@ -1498,27 +1386,20 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1498,27 +1386,20 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1498 continue;1386 continue;
1499 }1387 }
15001388
1501 *(try tree.errors.addOne()) = Error {1389 ((try tree.errors.addOne())).* = Error{ .ExpectedCall = Error.ExpectedCall{ .node = node } };
1502 .ExpectedCall = Error.ExpectedCall { .node = node },
1503 };
1504 return tree;1390 return tree;
1505 },1391 },
1506 else => {1392 else => {
1507 *(try tree.errors.addOne()) = Error {1393 ((try tree.errors.addOne())).* = Error{ .ExpectedCallOrFnProto = Error.ExpectedCallOrFnProto{ .node = node } };
1508 .ExpectedCallOrFnProto = Error.ExpectedCallOrFnProto { .node = node },
1509 };
1510 return tree;1394 return tree;
1511 }1395 },
1512 }1396 }
1513 },1397 },
15141398
1515
1516 State.ExternType => |ctx| {1399 State.ExternType => |ctx| {
1517 if (eatToken(&tok_it, &tree, Token.Id.Keyword_fn)) |fn_token| {1400 if (eatToken(&tok_it, &tree, Token.Id.Keyword_fn)) |fn_token| {
1518 const fn_proto = try arena.construct(ast.Node.FnProto {1401 const fn_proto = try arena.construct(ast.Node.FnProto{
1519 .base = ast.Node {1402 .base = ast.Node{ .id = ast.Node.Id.FnProto },
1520 .id = ast.Node.Id.FnProto,
1521 },
1522 .doc_comments = ctx.comments,1403 .doc_comments = ctx.comments,
1523 .visib_token = null,1404 .visib_token = null,
1524 .name_token = null,1405 .name_token = null,
...@@ -1534,17 +1415,15 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1534,17 +1415,15 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1534 .align_expr = null,1415 .align_expr = null,
1535 });1416 });
1536 ctx.opt_ctx.store(&fn_proto.base);1417 ctx.opt_ctx.store(&fn_proto.base);
1537 stack.append(State { .FnProto = fn_proto }) catch unreachable;1418 stack.append(State{ .FnProto = fn_proto }) catch unreachable;
1538 continue;1419 continue;
1539 }1420 }
15401421
1541 stack.append(State {1422 stack.append(State{ .ContainerKind = ContainerKindCtx{
1542 .ContainerKind = ContainerKindCtx {1423 .opt_ctx = ctx.opt_ctx,
1543 .opt_ctx = ctx.opt_ctx,1424 .ltoken = ctx.extern_token,
1544 .ltoken = ctx.extern_token,1425 .layout = ast.Node.ContainerDecl.Layout.Extern,
1545 .layout = ast.Node.ContainerDecl.Layout.Extern,1426 } }) catch unreachable;
1546 },
1547 }) catch unreachable;
1548 continue;1427 continue;
1549 },1428 },
1550 State.SliceOrArrayAccess => |node| {1429 State.SliceOrArrayAccess => |node| {
...@@ -1554,20 +1433,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1554,20 +1433,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1554 switch (token_ptr.id) {1433 switch (token_ptr.id) {
1555 Token.Id.Ellipsis2 => {1434 Token.Id.Ellipsis2 => {
1556 const start = node.op.ArrayAccess;1435 const start = node.op.ArrayAccess;
1557 node.op = ast.Node.SuffixOp.Op {1436 node.op = ast.Node.SuffixOp.Op{ .Slice = ast.Node.SuffixOp.Op.Slice{
1558 .Slice = ast.Node.SuffixOp.Op.Slice {1437 .start = start,
1559 .start = start,1438 .end = null,
1560 .end = null,1439 } };
1561 }
1562 };
15631440
1564 stack.append(State {1441 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1565 .ExpectTokenSave = ExpectTokenSave {1442 .id = Token.Id.RBracket,
1566 .id = Token.Id.RBracket,1443 .ptr = &node.rtoken,
1567 .ptr = &node.rtoken,1444 } }) catch unreachable;
1568 }1445 try stack.append(State{ .Expression = OptionalCtx{ .Optional = &node.op.Slice.end } });
1569 }) catch unreachable;
1570 try stack.append(State { .Expression = OptionalCtx { .Optional = &node.op.Slice.end } });
1571 continue;1446 continue;
1572 },1447 },
1573 Token.Id.RBracket => {1448 Token.Id.RBracket => {
...@@ -1575,33 +1450,29 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1575,33 +1450,29 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1575 continue;1450 continue;
1576 },1451 },
1577 else => {1452 else => {
1578 *(try tree.errors.addOne()) = Error {1453 ((try tree.errors.addOne())).* = Error{ .ExpectedSliceOrRBracket = Error.ExpectedSliceOrRBracket{ .token = token_index } };
1579 .ExpectedSliceOrRBracket = Error.ExpectedSliceOrRBracket { .token = token_index },
1580 };
1581 return tree;1454 return tree;
1582 }1455 },
1583 }1456 }
1584 },1457 },
1585 State.SliceOrArrayType => |node| {1458 State.SliceOrArrayType => |node| {
1586 if (eatToken(&tok_it, &tree, Token.Id.RBracket)) |_| {1459 if (eatToken(&tok_it, &tree, Token.Id.RBracket)) |_| {
1587 node.op = ast.Node.PrefixOp.Op {1460 node.op = ast.Node.PrefixOp.Op{ .SliceType = ast.Node.PrefixOp.AddrOfInfo{
1588 .SliceType = ast.Node.PrefixOp.AddrOfInfo {1461 .align_expr = null,
1589 .align_expr = null,1462 .bit_offset_start_token = null,
1590 .bit_offset_start_token = null,1463 .bit_offset_end_token = null,
1591 .bit_offset_end_token = null,1464 .const_token = null,
1592 .const_token = null,1465 .volatile_token = null,
1593 .volatile_token = null,1466 } };
1594 }1467 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
1595 };1468 try stack.append(State{ .AddrOfModifiers = &node.op.SliceType });
1596 stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.rhs } }) catch unreachable;
1597 try stack.append(State { .AddrOfModifiers = &node.op.SliceType });
1598 continue;1469 continue;
1599 }1470 }
16001471
1601 node.op = ast.Node.PrefixOp.Op { .ArrayType = undefined };1472 node.op = ast.Node.PrefixOp.Op{ .ArrayType = undefined };
1602 stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.rhs } }) catch unreachable;1473 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
1603 try stack.append(State { .ExpectToken = Token.Id.RBracket });1474 try stack.append(State{ .ExpectToken = Token.Id.RBracket });
1604 try stack.append(State { .Expression = OptionalCtx { .Required = &node.op.ArrayType } });1475 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.op.ArrayType } });
1605 continue;1476 continue;
1606 },1477 },
1607 State.AddrOfModifiers => |addr_of_info| {1478 State.AddrOfModifiers => |addr_of_info| {
...@@ -1612,22 +1483,18 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1612,22 +1483,18 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1612 Token.Id.Keyword_align => {1483 Token.Id.Keyword_align => {
1613 stack.append(state) catch unreachable;1484 stack.append(state) catch unreachable;
1614 if (addr_of_info.align_expr != null) {1485 if (addr_of_info.align_expr != null) {
1615 *(try tree.errors.addOne()) = Error {1486 ((try tree.errors.addOne())).* = Error{ .ExtraAlignQualifier = Error.ExtraAlignQualifier{ .token = token_index } };
1616 .ExtraAlignQualifier = Error.ExtraAlignQualifier { .token = token_index },
1617 };
1618 return tree;1487 return tree;
1619 }1488 }
1620 try stack.append(State { .ExpectToken = Token.Id.RParen });1489 try stack.append(State{ .ExpectToken = Token.Id.RParen });
1621 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &addr_of_info.align_expr} });1490 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &addr_of_info.align_expr } });
1622 try stack.append(State { .ExpectToken = Token.Id.LParen });1491 try stack.append(State{ .ExpectToken = Token.Id.LParen });
1623 continue;1492 continue;
1624 },1493 },
1625 Token.Id.Keyword_const => {1494 Token.Id.Keyword_const => {
1626 stack.append(state) catch unreachable;1495 stack.append(state) catch unreachable;
1627 if (addr_of_info.const_token != null) {1496 if (addr_of_info.const_token != null) {
1628 *(try tree.errors.addOne()) = Error {1497 ((try tree.errors.addOne())).* = Error{ .ExtraConstQualifier = Error.ExtraConstQualifier{ .token = token_index } };
1629 .ExtraConstQualifier = Error.ExtraConstQualifier { .token = token_index },
1630 };
1631 return tree;1498 return tree;
1632 }1499 }
1633 addr_of_info.const_token = token_index;1500 addr_of_info.const_token = token_index;
...@@ -1636,9 +1503,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1636,9 +1503,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1636 Token.Id.Keyword_volatile => {1503 Token.Id.Keyword_volatile => {
1637 stack.append(state) catch unreachable;1504 stack.append(state) catch unreachable;
1638 if (addr_of_info.volatile_token != null) {1505 if (addr_of_info.volatile_token != null) {
1639 *(try tree.errors.addOne()) = Error {1506 ((try tree.errors.addOne())).* = Error{ .ExtraVolatileQualifier = Error.ExtraVolatileQualifier{ .token = token_index } };
1640 .ExtraVolatileQualifier = Error.ExtraVolatileQualifier { .token = token_index },
1641 };
1642 return tree;1507 return tree;
1643 }1508 }
1644 addr_of_info.volatile_token = token_index;1509 addr_of_info.volatile_token = token_index;
...@@ -1651,19 +1516,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1651,19 +1516,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1651 }1516 }
1652 },1517 },
16531518
1654
1655 State.Payload => |opt_ctx| {1519 State.Payload => |opt_ctx| {
1656 const token = nextToken(&tok_it, &tree);1520 const token = nextToken(&tok_it, &tree);
1657 const token_index = token.index;1521 const token_index = token.index;
1658 const token_ptr = token.ptr;1522 const token_ptr = token.ptr;
1659 if (token_ptr.id != Token.Id.Pipe) {1523 if (token_ptr.id != Token.Id.Pipe) {
1660 if (opt_ctx != OptionalCtx.Optional) {1524 if (opt_ctx != OptionalCtx.Optional) {
1661 *(try tree.errors.addOne()) = Error {1525 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
1662 .ExpectedToken = Error.ExpectedToken {1526 .token = token_index,
1663 .token = token_index,1527 .expected_id = Token.Id.Pipe,
1664 .expected_id = Token.Id.Pipe,1528 } };
1665 },
1666 };
1667 return tree;1529 return tree;
1668 }1530 }
16691531
...@@ -1671,21 +1533,19 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1671,21 +1533,19 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1671 continue;1533 continue;
1672 }1534 }
16731535
1674 const node = try arena.construct(ast.Node.Payload {1536 const node = try arena.construct(ast.Node.Payload{
1675 .base = ast.Node {.id = ast.Node.Id.Payload },1537 .base = ast.Node{ .id = ast.Node.Id.Payload },
1676 .lpipe = token_index,1538 .lpipe = token_index,
1677 .error_symbol = undefined,1539 .error_symbol = undefined,
1678 .rpipe = undefined1540 .rpipe = undefined,
1679 });1541 });
1680 opt_ctx.store(&node.base);1542 opt_ctx.store(&node.base);
16811543
1682 stack.append(State {1544 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1683 .ExpectTokenSave = ExpectTokenSave {1545 .id = Token.Id.Pipe,
1684 .id = Token.Id.Pipe,1546 .ptr = &node.rpipe,
1685 .ptr = &node.rpipe,1547 } }) catch unreachable;
1686 }1548 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.error_symbol } });
1687 }) catch unreachable;
1688 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.error_symbol } });
1689 continue;1549 continue;
1690 },1550 },
1691 State.PointerPayload => |opt_ctx| {1551 State.PointerPayload => |opt_ctx| {
...@@ -1694,12 +1554,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1694,12 +1554,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1694 const token_ptr = token.ptr;1554 const token_ptr = token.ptr;
1695 if (token_ptr.id != Token.Id.Pipe) {1555 if (token_ptr.id != Token.Id.Pipe) {
1696 if (opt_ctx != OptionalCtx.Optional) {1556 if (opt_ctx != OptionalCtx.Optional) {
1697 *(try tree.errors.addOne()) = Error {1557 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
1698 .ExpectedToken = Error.ExpectedToken {1558 .token = token_index,
1699 .token = token_index,1559 .expected_id = Token.Id.Pipe,
1700 .expected_id = Token.Id.Pipe,1560 } };
1701 },
1702 };
1703 return tree;1561 return tree;
1704 }1562 }
17051563
...@@ -1707,28 +1565,24 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1707,28 +1565,24 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1707 continue;1565 continue;
1708 }1566 }
17091567
1710 const node = try arena.construct(ast.Node.PointerPayload {1568 const node = try arena.construct(ast.Node.PointerPayload{
1711 .base = ast.Node {.id = ast.Node.Id.PointerPayload },1569 .base = ast.Node{ .id = ast.Node.Id.PointerPayload },
1712 .lpipe = token_index,1570 .lpipe = token_index,
1713 .ptr_token = null,1571 .ptr_token = null,
1714 .value_symbol = undefined,1572 .value_symbol = undefined,
1715 .rpipe = undefined1573 .rpipe = undefined,
1716 });1574 });
1717 opt_ctx.store(&node.base);1575 opt_ctx.store(&node.base);
17181576
1719 try stack.append(State {1577 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1720 .ExpectTokenSave = ExpectTokenSave {1578 .id = Token.Id.Pipe,
1721 .id = Token.Id.Pipe,1579 .ptr = &node.rpipe,
1722 .ptr = &node.rpipe,1580 } });
1723 }1581 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.value_symbol } });
1724 });1582 try stack.append(State{ .OptionalTokenSave = OptionalTokenSave{
1725 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.value_symbol } });1583 .id = Token.Id.Asterisk,
1726 try stack.append(State {1584 .ptr = &node.ptr_token,
1727 .OptionalTokenSave = OptionalTokenSave {1585 } });
1728 .id = Token.Id.Asterisk,
1729 .ptr = &node.ptr_token,
1730 }
1731 });
1732 continue;1586 continue;
1733 },1587 },
1734 State.PointerIndexPayload => |opt_ctx| {1588 State.PointerIndexPayload => |opt_ctx| {
...@@ -1737,12 +1591,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1737,12 +1591,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1737 const token_ptr = token.ptr;1591 const token_ptr = token.ptr;
1738 if (token_ptr.id != Token.Id.Pipe) {1592 if (token_ptr.id != Token.Id.Pipe) {
1739 if (opt_ctx != OptionalCtx.Optional) {1593 if (opt_ctx != OptionalCtx.Optional) {
1740 *(try tree.errors.addOne()) = Error {1594 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
1741 .ExpectedToken = Error.ExpectedToken {1595 .token = token_index,
1742 .token = token_index,1596 .expected_id = Token.Id.Pipe,
1743 .expected_id = Token.Id.Pipe,1597 } };
1744 },
1745 };
1746 return tree;1598 return tree;
1747 }1599 }
17481600
...@@ -1750,61 +1602,58 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1750,61 +1602,58 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1750 continue;1602 continue;
1751 }1603 }
17521604
1753 const node = try arena.construct(ast.Node.PointerIndexPayload {1605 const node = try arena.construct(ast.Node.PointerIndexPayload{
1754 .base = ast.Node {.id = ast.Node.Id.PointerIndexPayload },1606 .base = ast.Node{ .id = ast.Node.Id.PointerIndexPayload },
1755 .lpipe = token_index,1607 .lpipe = token_index,
1756 .ptr_token = null,1608 .ptr_token = null,
1757 .value_symbol = undefined,1609 .value_symbol = undefined,
1758 .index_symbol = null,1610 .index_symbol = null,
1759 .rpipe = undefined1611 .rpipe = undefined,
1760 });1612 });
1761 opt_ctx.store(&node.base);1613 opt_ctx.store(&node.base);
17621614
1763 stack.append(State {1615 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1764 .ExpectTokenSave = ExpectTokenSave {1616 .id = Token.Id.Pipe,
1765 .id = Token.Id.Pipe,1617 .ptr = &node.rpipe,
1766 .ptr = &node.rpipe,1618 } }) catch unreachable;
1767 }1619 try stack.append(State{ .Identifier = OptionalCtx{ .RequiredNull = &node.index_symbol } });
1768 }) catch unreachable;1620 try stack.append(State{ .IfToken = Token.Id.Comma });
1769 try stack.append(State { .Identifier = OptionalCtx { .RequiredNull = &node.index_symbol } });1621 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.value_symbol } });
1770 try stack.append(State { .IfToken = Token.Id.Comma });1622 try stack.append(State{ .OptionalTokenSave = OptionalTokenSave{
1771 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.value_symbol } });1623 .id = Token.Id.Asterisk,
1772 try stack.append(State {1624 .ptr = &node.ptr_token,
1773 .OptionalTokenSave = OptionalTokenSave {1625 } });
1774 .id = Token.Id.Asterisk,
1775 .ptr = &node.ptr_token,
1776 }
1777 });
1778 continue;1626 continue;
1779 },1627 },
17801628
1781
1782 State.Expression => |opt_ctx| {1629 State.Expression => |opt_ctx| {
1783 const token = nextToken(&tok_it, &tree);1630 const token = nextToken(&tok_it, &tree);
1784 const token_index = token.index;1631 const token_index = token.index;
1785 const token_ptr = token.ptr;1632 const token_ptr = token.ptr;
1786 switch (token_ptr.id) {1633 switch (token_ptr.id) {
1787 Token.Id.Keyword_return, Token.Id.Keyword_break, Token.Id.Keyword_continue => {1634 Token.Id.Keyword_return,
1788 const node = try arena.construct(ast.Node.ControlFlowExpression {1635 Token.Id.Keyword_break,
1789 .base = ast.Node {.id = ast.Node.Id.ControlFlowExpression },1636 Token.Id.Keyword_continue => {
1637 const node = try arena.construct(ast.Node.ControlFlowExpression{
1638 .base = ast.Node{ .id = ast.Node.Id.ControlFlowExpression },
1790 .ltoken = token_index,1639 .ltoken = token_index,
1791 .kind = undefined,1640 .kind = undefined,
1792 .rhs = null,1641 .rhs = null,
1793 });1642 });
1794 opt_ctx.store(&node.base);1643 opt_ctx.store(&node.base);
17951644
1796 stack.append(State { .Expression = OptionalCtx { .Optional = &node.rhs } }) catch unreachable;1645 stack.append(State{ .Expression = OptionalCtx{ .Optional = &node.rhs } }) catch unreachable;
17971646
1798 switch (token_ptr.id) {1647 switch (token_ptr.id) {
1799 Token.Id.Keyword_break => {1648 Token.Id.Keyword_break => {
1800 node.kind = ast.Node.ControlFlowExpression.Kind { .Break = null };1649 node.kind = ast.Node.ControlFlowExpression.Kind{ .Break = null };
1801 try stack.append(State { .Identifier = OptionalCtx { .RequiredNull = &node.kind.Break } });1650 try stack.append(State{ .Identifier = OptionalCtx{ .RequiredNull = &node.kind.Break } });
1802 try stack.append(State { .IfToken = Token.Id.Colon });1651 try stack.append(State{ .IfToken = Token.Id.Colon });
1803 },1652 },
1804 Token.Id.Keyword_continue => {1653 Token.Id.Keyword_continue => {
1805 node.kind = ast.Node.ControlFlowExpression.Kind { .Continue = null };1654 node.kind = ast.Node.ControlFlowExpression.Kind{ .Continue = null };
1806 try stack.append(State { .Identifier = OptionalCtx { .RequiredNull = &node.kind.Continue } });1655 try stack.append(State{ .Identifier = OptionalCtx{ .RequiredNull = &node.kind.Continue } });
1807 try stack.append(State { .IfToken = Token.Id.Colon });1656 try stack.append(State{ .IfToken = Token.Id.Colon });
1808 },1657 },
1809 Token.Id.Keyword_return => {1658 Token.Id.Keyword_return => {
1810 node.kind = ast.Node.ControlFlowExpression.Kind.Return;1659 node.kind = ast.Node.ControlFlowExpression.Kind.Return;
...@@ -1813,56 +1662,58 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1813,56 +1662,58 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1813 }1662 }
1814 continue;1663 continue;
1815 },1664 },
1816 Token.Id.Keyword_try, Token.Id.Keyword_cancel, Token.Id.Keyword_resume => {1665 Token.Id.Keyword_try,
1817 const node = try arena.construct(ast.Node.PrefixOp {1666 Token.Id.Keyword_cancel,
1818 .base = ast.Node {.id = ast.Node.Id.PrefixOp },1667 Token.Id.Keyword_resume => {
1668 const node = try arena.construct(ast.Node.PrefixOp{
1669 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
1819 .op_token = token_index,1670 .op_token = token_index,
1820 .op = switch (token_ptr.id) {1671 .op = switch (token_ptr.id) {
1821 Token.Id.Keyword_try => ast.Node.PrefixOp.Op { .Try = void{} },1672 Token.Id.Keyword_try => ast.Node.PrefixOp.Op{ .Try = void{} },
1822 Token.Id.Keyword_cancel => ast.Node.PrefixOp.Op { .Cancel = void{} },1673 Token.Id.Keyword_cancel => ast.Node.PrefixOp.Op{ .Cancel = void{} },
1823 Token.Id.Keyword_resume => ast.Node.PrefixOp.Op { .Resume = void{} },1674 Token.Id.Keyword_resume => ast.Node.PrefixOp.Op{ .Resume = void{} },
1824 else => unreachable,1675 else => unreachable,
1825 },1676 },
1826 .rhs = undefined,1677 .rhs = undefined,
1827 });1678 });
1828 opt_ctx.store(&node.base);1679 opt_ctx.store(&node.base);
18291680
1830 stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } }) catch unreachable;1681 stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
1831 continue;1682 continue;
1832 },1683 },
1833 else => {1684 else => {
1834 if (!try parseBlockExpr(&stack, arena, opt_ctx, token_ptr, token_index)) {1685 if (!try parseBlockExpr(&stack, arena, opt_ctx, token_ptr, token_index)) {
1835 putBackToken(&tok_it, &tree);1686 putBackToken(&tok_it, &tree);
1836 stack.append(State { .UnwrapExpressionBegin = opt_ctx }) catch unreachable;1687 stack.append(State{ .UnwrapExpressionBegin = opt_ctx }) catch unreachable;
1837 }1688 }
1838 continue;1689 continue;
1839 }1690 },
1840 }1691 }
1841 },1692 },
1842 State.RangeExpressionBegin => |opt_ctx| {1693 State.RangeExpressionBegin => |opt_ctx| {
1843 stack.append(State { .RangeExpressionEnd = opt_ctx }) catch unreachable;1694 stack.append(State{ .RangeExpressionEnd = opt_ctx }) catch unreachable;
1844 try stack.append(State { .Expression = opt_ctx });1695 try stack.append(State{ .Expression = opt_ctx });
1845 continue;1696 continue;
1846 },1697 },
1847 State.RangeExpressionEnd => |opt_ctx| {1698 State.RangeExpressionEnd => |opt_ctx| {
1848 const lhs = opt_ctx.get() ?? continue;1699 const lhs = opt_ctx.get() ?? continue;
18491700
1850 if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {1701 if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {
1851 const node = try arena.construct(ast.Node.InfixOp {1702 const node = try arena.construct(ast.Node.InfixOp{
1852 .base = ast.Node {.id = ast.Node.Id.InfixOp },1703 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
1853 .lhs = lhs,1704 .lhs = lhs,
1854 .op_token = ellipsis3,1705 .op_token = ellipsis3,
1855 .op = ast.Node.InfixOp.Op.Range,1706 .op = ast.Node.InfixOp.Op.Range,
1856 .rhs = undefined,1707 .rhs = undefined,
1857 });1708 });
1858 opt_ctx.store(&node.base);1709 opt_ctx.store(&node.base);
1859 stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } }) catch unreachable;1710 stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
1860 continue;1711 continue;
1861 }1712 }
1862 },1713 },
1863 State.AssignmentExpressionBegin => |opt_ctx| {1714 State.AssignmentExpressionBegin => |opt_ctx| {
1864 stack.append(State { .AssignmentExpressionEnd = opt_ctx }) catch unreachable;1715 stack.append(State{ .AssignmentExpressionEnd = opt_ctx }) catch unreachable;
1865 try stack.append(State { .Expression = opt_ctx });1716 try stack.append(State{ .Expression = opt_ctx });
1866 continue;1717 continue;
1867 },1718 },
18681719
...@@ -1873,16 +1724,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1873,16 +1724,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1873 const token_index = token.index;1724 const token_index = token.index;
1874 const token_ptr = token.ptr;1725 const token_ptr = token.ptr;
1875 if (tokenIdToAssignment(token_ptr.id)) |ass_id| {1726 if (tokenIdToAssignment(token_ptr.id)) |ass_id| {
1876 const node = try arena.construct(ast.Node.InfixOp {1727 const node = try arena.construct(ast.Node.InfixOp{
1877 .base = ast.Node {.id = ast.Node.Id.InfixOp },1728 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
1878 .lhs = lhs,1729 .lhs = lhs,
1879 .op_token = token_index,1730 .op_token = token_index,
1880 .op = ass_id,1731 .op = ass_id,
1881 .rhs = undefined,1732 .rhs = undefined,
1882 });1733 });
1883 opt_ctx.store(&node.base);1734 opt_ctx.store(&node.base);
1884 stack.append(State { .AssignmentExpressionEnd = opt_ctx.toRequired() }) catch unreachable;1735 stack.append(State{ .AssignmentExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1885 try stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } });1736 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } });
1886 continue;1737 continue;
1887 } else {1738 } else {
1888 putBackToken(&tok_it, &tree);1739 putBackToken(&tok_it, &tree);
...@@ -1891,8 +1742,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1891,8 +1742,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1891 },1742 },
18921743
1893 State.UnwrapExpressionBegin => |opt_ctx| {1744 State.UnwrapExpressionBegin => |opt_ctx| {
1894 stack.append(State { .UnwrapExpressionEnd = opt_ctx }) catch unreachable;1745 stack.append(State{ .UnwrapExpressionEnd = opt_ctx }) catch unreachable;
1895 try stack.append(State { .BoolOrExpressionBegin = opt_ctx });1746 try stack.append(State{ .BoolOrExpressionBegin = opt_ctx });
1896 continue;1747 continue;
1897 },1748 },
18981749
...@@ -1903,8 +1754,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1903,8 +1754,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1903 const token_index = token.index;1754 const token_index = token.index;
1904 const token_ptr = token.ptr;1755 const token_ptr = token.ptr;
1905 if (tokenIdToUnwrapExpr(token_ptr.id)) |unwrap_id| {1756 if (tokenIdToUnwrapExpr(token_ptr.id)) |unwrap_id| {
1906 const node = try arena.construct(ast.Node.InfixOp {1757 const node = try arena.construct(ast.Node.InfixOp{
1907 .base = ast.Node {.id = ast.Node.Id.InfixOp },1758 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
1908 .lhs = lhs,1759 .lhs = lhs,
1909 .op_token = token_index,1760 .op_token = token_index,
1910 .op = unwrap_id,1761 .op = unwrap_id,
...@@ -1912,11 +1763,11 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1912,11 +1763,11 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1912 });1763 });
1913 opt_ctx.store(&node.base);1764 opt_ctx.store(&node.base);
19141765
1915 stack.append(State { .UnwrapExpressionEnd = opt_ctx.toRequired() }) catch unreachable;1766 stack.append(State{ .UnwrapExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1916 try stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } });1767 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } });
19171768
1918 if (node.op == ast.Node.InfixOp.Op.Catch) {1769 if (node.op == ast.Node.InfixOp.Op.Catch) {
1919 try stack.append(State { .Payload = OptionalCtx { .Optional = &node.op.Catch } });1770 try stack.append(State{ .Payload = OptionalCtx{ .Optional = &node.op.Catch } });
1920 }1771 }
1921 continue;1772 continue;
1922 } else {1773 } else {
...@@ -1926,8 +1777,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1926,8 +1777,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1926 },1777 },
19271778
1928 State.BoolOrExpressionBegin => |opt_ctx| {1779 State.BoolOrExpressionBegin => |opt_ctx| {
1929 stack.append(State { .BoolOrExpressionEnd = opt_ctx }) catch unreachable;1780 stack.append(State{ .BoolOrExpressionEnd = opt_ctx }) catch unreachable;
1930 try stack.append(State { .BoolAndExpressionBegin = opt_ctx });1781 try stack.append(State{ .BoolAndExpressionBegin = opt_ctx });
1931 continue;1782 continue;
1932 },1783 },
19331784
...@@ -1935,23 +1786,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1935,23 +1786,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1935 const lhs = opt_ctx.get() ?? continue;1786 const lhs = opt_ctx.get() ?? continue;
19361787
1937 if (eatToken(&tok_it, &tree, Token.Id.Keyword_or)) |or_token| {1788 if (eatToken(&tok_it, &tree, Token.Id.Keyword_or)) |or_token| {
1938 const node = try arena.construct(ast.Node.InfixOp {1789 const node = try arena.construct(ast.Node.InfixOp{
1939 .base = ast.Node {.id = ast.Node.Id.InfixOp },1790 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
1940 .lhs = lhs,1791 .lhs = lhs,
1941 .op_token = or_token,1792 .op_token = or_token,
1942 .op = ast.Node.InfixOp.Op.BoolOr,1793 .op = ast.Node.InfixOp.Op.BoolOr,
1943 .rhs = undefined,1794 .rhs = undefined,
1944 });1795 });
1945 opt_ctx.store(&node.base);1796 opt_ctx.store(&node.base);
1946 stack.append(State { .BoolOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable;1797 stack.append(State{ .BoolOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1947 try stack.append(State { .BoolAndExpressionBegin = OptionalCtx { .Required = &node.rhs } });1798 try stack.append(State{ .BoolAndExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
1948 continue;1799 continue;
1949 }1800 }
1950 },1801 },
19511802
1952 State.BoolAndExpressionBegin => |opt_ctx| {1803 State.BoolAndExpressionBegin => |opt_ctx| {
1953 stack.append(State { .BoolAndExpressionEnd = opt_ctx }) catch unreachable;1804 stack.append(State{ .BoolAndExpressionEnd = opt_ctx }) catch unreachable;
1954 try stack.append(State { .ComparisonExpressionBegin = opt_ctx });1805 try stack.append(State{ .ComparisonExpressionBegin = opt_ctx });
1955 continue;1806 continue;
1956 },1807 },
19571808
...@@ -1959,23 +1810,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1959,23 +1810,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1959 const lhs = opt_ctx.get() ?? continue;1810 const lhs = opt_ctx.get() ?? continue;
19601811
1961 if (eatToken(&tok_it, &tree, Token.Id.Keyword_and)) |and_token| {1812 if (eatToken(&tok_it, &tree, Token.Id.Keyword_and)) |and_token| {
1962 const node = try arena.construct(ast.Node.InfixOp {1813 const node = try arena.construct(ast.Node.InfixOp{
1963 .base = ast.Node {.id = ast.Node.Id.InfixOp },1814 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
1964 .lhs = lhs,1815 .lhs = lhs,
1965 .op_token = and_token,1816 .op_token = and_token,
1966 .op = ast.Node.InfixOp.Op.BoolAnd,1817 .op = ast.Node.InfixOp.Op.BoolAnd,
1967 .rhs = undefined,1818 .rhs = undefined,
1968 });1819 });
1969 opt_ctx.store(&node.base);1820 opt_ctx.store(&node.base);
1970 stack.append(State { .BoolAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable;1821 stack.append(State{ .BoolAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1971 try stack.append(State { .ComparisonExpressionBegin = OptionalCtx { .Required = &node.rhs } });1822 try stack.append(State{ .ComparisonExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
1972 continue;1823 continue;
1973 }1824 }
1974 },1825 },
19751826
1976 State.ComparisonExpressionBegin => |opt_ctx| {1827 State.ComparisonExpressionBegin => |opt_ctx| {
1977 stack.append(State { .ComparisonExpressionEnd = opt_ctx }) catch unreachable;1828 stack.append(State{ .ComparisonExpressionEnd = opt_ctx }) catch unreachable;
1978 try stack.append(State { .BinaryOrExpressionBegin = opt_ctx });1829 try stack.append(State{ .BinaryOrExpressionBegin = opt_ctx });
1979 continue;1830 continue;
1980 },1831 },
19811832
...@@ -1986,16 +1837,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1986,16 +1837,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1986 const token_index = token.index;1837 const token_index = token.index;
1987 const token_ptr = token.ptr;1838 const token_ptr = token.ptr;
1988 if (tokenIdToComparison(token_ptr.id)) |comp_id| {1839 if (tokenIdToComparison(token_ptr.id)) |comp_id| {
1989 const node = try arena.construct(ast.Node.InfixOp {1840 const node = try arena.construct(ast.Node.InfixOp{
1990 .base = ast.Node {.id = ast.Node.Id.InfixOp },1841 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
1991 .lhs = lhs,1842 .lhs = lhs,
1992 .op_token = token_index,1843 .op_token = token_index,
1993 .op = comp_id,1844 .op = comp_id,
1994 .rhs = undefined,1845 .rhs = undefined,
1995 });1846 });
1996 opt_ctx.store(&node.base);1847 opt_ctx.store(&node.base);
1997 stack.append(State { .ComparisonExpressionEnd = opt_ctx.toRequired() }) catch unreachable;1848 stack.append(State{ .ComparisonExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1998 try stack.append(State { .BinaryOrExpressionBegin = OptionalCtx { .Required = &node.rhs } });1849 try stack.append(State{ .BinaryOrExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
1999 continue;1850 continue;
2000 } else {1851 } else {
2001 putBackToken(&tok_it, &tree);1852 putBackToken(&tok_it, &tree);
...@@ -2004,8 +1855,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2004,8 +1855,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2004 },1855 },
20051856
2006 State.BinaryOrExpressionBegin => |opt_ctx| {1857 State.BinaryOrExpressionBegin => |opt_ctx| {
2007 stack.append(State { .BinaryOrExpressionEnd = opt_ctx }) catch unreachable;1858 stack.append(State{ .BinaryOrExpressionEnd = opt_ctx }) catch unreachable;
2008 try stack.append(State { .BinaryXorExpressionBegin = opt_ctx });1859 try stack.append(State{ .BinaryXorExpressionBegin = opt_ctx });
2009 continue;1860 continue;
2010 },1861 },
20111862
...@@ -2013,23 +1864,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2013,23 +1864,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2013 const lhs = opt_ctx.get() ?? continue;1864 const lhs = opt_ctx.get() ?? continue;
20141865
2015 if (eatToken(&tok_it, &tree, Token.Id.Pipe)) |pipe| {1866 if (eatToken(&tok_it, &tree, Token.Id.Pipe)) |pipe| {
2016 const node = try arena.construct(ast.Node.InfixOp {1867 const node = try arena.construct(ast.Node.InfixOp{
2017 .base = ast.Node {.id = ast.Node.Id.InfixOp },1868 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
2018 .lhs = lhs,1869 .lhs = lhs,
2019 .op_token = pipe,1870 .op_token = pipe,
2020 .op = ast.Node.InfixOp.Op.BitOr,1871 .op = ast.Node.InfixOp.Op.BitOr,
2021 .rhs = undefined,1872 .rhs = undefined,
2022 });1873 });
2023 opt_ctx.store(&node.base);1874 opt_ctx.store(&node.base);
2024 stack.append(State { .BinaryOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable;1875 stack.append(State{ .BinaryOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2025 try stack.append(State { .BinaryXorExpressionBegin = OptionalCtx { .Required = &node.rhs } });1876 try stack.append(State{ .BinaryXorExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
2026 continue;1877 continue;
2027 }1878 }
2028 },1879 },
20291880
2030 State.BinaryXorExpressionBegin => |opt_ctx| {1881 State.BinaryXorExpressionBegin => |opt_ctx| {
2031 stack.append(State { .BinaryXorExpressionEnd = opt_ctx }) catch unreachable;1882 stack.append(State{ .BinaryXorExpressionEnd = opt_ctx }) catch unreachable;
2032 try stack.append(State { .BinaryAndExpressionBegin = opt_ctx });1883 try stack.append(State{ .BinaryAndExpressionBegin = opt_ctx });
2033 continue;1884 continue;
2034 },1885 },
20351886
...@@ -2037,23 +1888,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2037,23 +1888,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2037 const lhs = opt_ctx.get() ?? continue;1888 const lhs = opt_ctx.get() ?? continue;
20381889
2039 if (eatToken(&tok_it, &tree, Token.Id.Caret)) |caret| {1890 if (eatToken(&tok_it, &tree, Token.Id.Caret)) |caret| {
2040 const node = try arena.construct(ast.Node.InfixOp {1891 const node = try arena.construct(ast.Node.InfixOp{
2041 .base = ast.Node {.id = ast.Node.Id.InfixOp },1892 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
2042 .lhs = lhs,1893 .lhs = lhs,
2043 .op_token = caret,1894 .op_token = caret,
2044 .op = ast.Node.InfixOp.Op.BitXor,1895 .op = ast.Node.InfixOp.Op.BitXor,
2045 .rhs = undefined,1896 .rhs = undefined,
2046 });1897 });
2047 opt_ctx.store(&node.base);1898 opt_ctx.store(&node.base);
2048 stack.append(State { .BinaryXorExpressionEnd = opt_ctx.toRequired() }) catch unreachable;1899 stack.append(State{ .BinaryXorExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2049 try stack.append(State { .BinaryAndExpressionBegin = OptionalCtx { .Required = &node.rhs } });1900 try stack.append(State{ .BinaryAndExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
2050 continue;1901 continue;
2051 }1902 }
2052 },1903 },
20531904
2054 State.BinaryAndExpressionBegin => |opt_ctx| {1905 State.BinaryAndExpressionBegin => |opt_ctx| {
2055 stack.append(State { .BinaryAndExpressionEnd = opt_ctx }) catch unreachable;1906 stack.append(State{ .BinaryAndExpressionEnd = opt_ctx }) catch unreachable;
2056 try stack.append(State { .BitShiftExpressionBegin = opt_ctx });1907 try stack.append(State{ .BitShiftExpressionBegin = opt_ctx });
2057 continue;1908 continue;
2058 },1909 },
20591910
...@@ -2061,23 +1912,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2061,23 +1912,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2061 const lhs = opt_ctx.get() ?? continue;1912 const lhs = opt_ctx.get() ?? continue;
20621913
2063 if (eatToken(&tok_it, &tree, Token.Id.Ampersand)) |ampersand| {1914 if (eatToken(&tok_it, &tree, Token.Id.Ampersand)) |ampersand| {
2064 const node = try arena.construct(ast.Node.InfixOp {1915 const node = try arena.construct(ast.Node.InfixOp{
2065 .base = ast.Node {.id = ast.Node.Id.InfixOp },1916 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
2066 .lhs = lhs,1917 .lhs = lhs,
2067 .op_token = ampersand,1918 .op_token = ampersand,
2068 .op = ast.Node.InfixOp.Op.BitAnd,1919 .op = ast.Node.InfixOp.Op.BitAnd,
2069 .rhs = undefined,1920 .rhs = undefined,
2070 });1921 });
2071 opt_ctx.store(&node.base);1922 opt_ctx.store(&node.base);
2072 stack.append(State { .BinaryAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable;1923 stack.append(State{ .BinaryAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2073 try stack.append(State { .BitShiftExpressionBegin = OptionalCtx { .Required = &node.rhs } });1924 try stack.append(State{ .BitShiftExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
2074 continue;1925 continue;
2075 }1926 }
2076 },1927 },
20771928
2078 State.BitShiftExpressionBegin => |opt_ctx| {1929 State.BitShiftExpressionBegin => |opt_ctx| {
2079 stack.append(State { .BitShiftExpressionEnd = opt_ctx }) catch unreachable;1930 stack.append(State{ .BitShiftExpressionEnd = opt_ctx }) catch unreachable;
2080 try stack.append(State { .AdditionExpressionBegin = opt_ctx });1931 try stack.append(State{ .AdditionExpressionBegin = opt_ctx });
2081 continue;1932 continue;
2082 },1933 },
20831934
...@@ -2088,16 +1939,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2088,16 +1939,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2088 const token_index = token.index;1939 const token_index = token.index;
2089 const token_ptr = token.ptr;1940 const token_ptr = token.ptr;
2090 if (tokenIdToBitShift(token_ptr.id)) |bitshift_id| {1941 if (tokenIdToBitShift(token_ptr.id)) |bitshift_id| {
2091 const node = try arena.construct(ast.Node.InfixOp {1942 const node = try arena.construct(ast.Node.InfixOp{
2092 .base = ast.Node {.id = ast.Node.Id.InfixOp },1943 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
2093 .lhs = lhs,1944 .lhs = lhs,
2094 .op_token = token_index,1945 .op_token = token_index,
2095 .op = bitshift_id,1946 .op = bitshift_id,
2096 .rhs = undefined,1947 .rhs = undefined,
2097 });1948 });
2098 opt_ctx.store(&node.base);1949 opt_ctx.store(&node.base);
2099 stack.append(State { .BitShiftExpressionEnd = opt_ctx.toRequired() }) catch unreachable;1950 stack.append(State{ .BitShiftExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2100 try stack.append(State { .AdditionExpressionBegin = OptionalCtx { .Required = &node.rhs } });1951 try stack.append(State{ .AdditionExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
2101 continue;1952 continue;
2102 } else {1953 } else {
2103 putBackToken(&tok_it, &tree);1954 putBackToken(&tok_it, &tree);
...@@ -2106,8 +1957,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2106,8 +1957,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2106 },1957 },
21071958
2108 State.AdditionExpressionBegin => |opt_ctx| {1959 State.AdditionExpressionBegin => |opt_ctx| {
2109 stack.append(State { .AdditionExpressionEnd = opt_ctx }) catch unreachable;1960 stack.append(State{ .AdditionExpressionEnd = opt_ctx }) catch unreachable;
2110 try stack.append(State { .MultiplyExpressionBegin = opt_ctx });1961 try stack.append(State{ .MultiplyExpressionBegin = opt_ctx });
2111 continue;1962 continue;
2112 },1963 },
21131964
...@@ -2118,16 +1969,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2118,16 +1969,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2118 const token_index = token.index;1969 const token_index = token.index;
2119 const token_ptr = token.ptr;1970 const token_ptr = token.ptr;
2120 if (tokenIdToAddition(token_ptr.id)) |add_id| {1971 if (tokenIdToAddition(token_ptr.id)) |add_id| {
2121 const node = try arena.construct(ast.Node.InfixOp {1972 const node = try arena.construct(ast.Node.InfixOp{
2122 .base = ast.Node {.id = ast.Node.Id.InfixOp },1973 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
2123 .lhs = lhs,1974 .lhs = lhs,
2124 .op_token = token_index,1975 .op_token = token_index,
2125 .op = add_id,1976 .op = add_id,
2126 .rhs = undefined,1977 .rhs = undefined,
2127 });1978 });
2128 opt_ctx.store(&node.base);1979 opt_ctx.store(&node.base);
2129 stack.append(State { .AdditionExpressionEnd = opt_ctx.toRequired() }) catch unreachable;1980 stack.append(State{ .AdditionExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2130 try stack.append(State { .MultiplyExpressionBegin = OptionalCtx { .Required = &node.rhs } });1981 try stack.append(State{ .MultiplyExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
2131 continue;1982 continue;
2132 } else {1983 } else {
2133 putBackToken(&tok_it, &tree);1984 putBackToken(&tok_it, &tree);
...@@ -2136,8 +1987,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2136,8 +1987,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2136 },1987 },
21371988
2138 State.MultiplyExpressionBegin => |opt_ctx| {1989 State.MultiplyExpressionBegin => |opt_ctx| {
2139 stack.append(State { .MultiplyExpressionEnd = opt_ctx }) catch unreachable;1990 stack.append(State{ .MultiplyExpressionEnd = opt_ctx }) catch unreachable;
2140 try stack.append(State { .CurlySuffixExpressionBegin = opt_ctx });1991 try stack.append(State{ .CurlySuffixExpressionBegin = opt_ctx });
2141 continue;1992 continue;
2142 },1993 },
21431994
...@@ -2148,16 +1999,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2148,16 +1999,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2148 const token_index = token.index;1999 const token_index = token.index;
2149 const token_ptr = token.ptr;2000 const token_ptr = token.ptr;
2150 if (tokenIdToMultiply(token_ptr.id)) |mult_id| {2001 if (tokenIdToMultiply(token_ptr.id)) |mult_id| {
2151 const node = try arena.construct(ast.Node.InfixOp {2002 const node = try arena.construct(ast.Node.InfixOp{
2152 .base = ast.Node {.id = ast.Node.Id.InfixOp },2003 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
2153 .lhs = lhs,2004 .lhs = lhs,
2154 .op_token = token_index,2005 .op_token = token_index,
2155 .op = mult_id,2006 .op = mult_id,
2156 .rhs = undefined,2007 .rhs = undefined,
2157 });2008 });
2158 opt_ctx.store(&node.base);2009 opt_ctx.store(&node.base);
2159 stack.append(State { .MultiplyExpressionEnd = opt_ctx.toRequired() }) catch unreachable;2010 stack.append(State{ .MultiplyExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2160 try stack.append(State { .CurlySuffixExpressionBegin = OptionalCtx { .Required = &node.rhs } });2011 try stack.append(State{ .CurlySuffixExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
2161 continue;2012 continue;
2162 } else {2013 } else {
2163 putBackToken(&tok_it, &tree);2014 putBackToken(&tok_it, &tree);
...@@ -2166,9 +2017,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2166,9 +2017,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2166 },2017 },
21672018
2168 State.CurlySuffixExpressionBegin => |opt_ctx| {2019 State.CurlySuffixExpressionBegin => |opt_ctx| {
2169 stack.append(State { .CurlySuffixExpressionEnd = opt_ctx }) catch unreachable;2020 stack.append(State{ .CurlySuffixExpressionEnd = opt_ctx }) catch unreachable;
2170 try stack.append(State { .IfToken = Token.Id.LBrace });2021 try stack.append(State{ .IfToken = Token.Id.LBrace });
2171 try stack.append(State { .TypeExprBegin = opt_ctx });2022 try stack.append(State{ .TypeExprBegin = opt_ctx });
2172 continue;2023 continue;
2173 },2024 },
21742025
...@@ -2176,51 +2027,43 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2176,51 +2027,43 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2176 const lhs = opt_ctx.get() ?? continue;2027 const lhs = opt_ctx.get() ?? continue;
21772028
2178 if ((??tok_it.peek()).id == Token.Id.Period) {2029 if ((??tok_it.peek()).id == Token.Id.Period) {
2179 const node = try arena.construct(ast.Node.SuffixOp {2030 const node = try arena.construct(ast.Node.SuffixOp{
2180 .base = ast.Node { .id = ast.Node.Id.SuffixOp },2031 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
2181 .lhs = lhs,2032 .lhs = lhs,
2182 .op = ast.Node.SuffixOp.Op {2033 .op = ast.Node.SuffixOp.Op{ .StructInitializer = ast.Node.SuffixOp.Op.InitList.init(arena) },
2183 .StructInitializer = ast.Node.SuffixOp.Op.InitList.init(arena),
2184 },
2185 .rtoken = undefined,2034 .rtoken = undefined,
2186 });2035 });
2187 opt_ctx.store(&node.base);2036 opt_ctx.store(&node.base);
21882037
2189 stack.append(State { .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable;2038 stack.append(State{ .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2190 try stack.append(State { .IfToken = Token.Id.LBrace });2039 try stack.append(State{ .IfToken = Token.Id.LBrace });
2191 try stack.append(State {2040 try stack.append(State{ .FieldInitListItemOrEnd = ListSave(@typeOf(node.op.StructInitializer)){
2192 .FieldInitListItemOrEnd = ListSave(@typeOf(node.op.StructInitializer)) {2041 .list = &node.op.StructInitializer,
2193 .list = &node.op.StructInitializer,2042 .ptr = &node.rtoken,
2194 .ptr = &node.rtoken,2043 } });
2195 }
2196 });
2197 continue;2044 continue;
2198 }2045 }
21992046
2200 const node = try arena.construct(ast.Node.SuffixOp {2047 const node = try arena.construct(ast.Node.SuffixOp{
2201 .base = ast.Node {.id = ast.Node.Id.SuffixOp },2048 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
2202 .lhs = lhs,2049 .lhs = lhs,
2203 .op = ast.Node.SuffixOp.Op {2050 .op = ast.Node.SuffixOp.Op{ .ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(arena) },
2204 .ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(arena),
2205 },
2206 .rtoken = undefined,2051 .rtoken = undefined,
2207 });2052 });
2208 opt_ctx.store(&node.base);2053 opt_ctx.store(&node.base);
2209 stack.append(State { .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable;2054 stack.append(State{ .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2210 try stack.append(State { .IfToken = Token.Id.LBrace });2055 try stack.append(State{ .IfToken = Token.Id.LBrace });
2211 try stack.append(State {2056 try stack.append(State{ .ExprListItemOrEnd = ExprListCtx{
2212 .ExprListItemOrEnd = ExprListCtx {2057 .list = &node.op.ArrayInitializer,
2213 .list = &node.op.ArrayInitializer,2058 .end = Token.Id.RBrace,
2214 .end = Token.Id.RBrace,2059 .ptr = &node.rtoken,
2215 .ptr = &node.rtoken,2060 } });
2216 }
2217 });
2218 continue;2061 continue;
2219 },2062 },
22202063
2221 State.TypeExprBegin => |opt_ctx| {2064 State.TypeExprBegin => |opt_ctx| {
2222 stack.append(State { .TypeExprEnd = opt_ctx }) catch unreachable;2065 stack.append(State{ .TypeExprEnd = opt_ctx }) catch unreachable;
2223 try stack.append(State { .PrefixOpExpression = opt_ctx });2066 try stack.append(State{ .PrefixOpExpression = opt_ctx });
2224 continue;2067 continue;
2225 },2068 },
22262069
...@@ -2228,16 +2071,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2228,16 +2071,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2228 const lhs = opt_ctx.get() ?? continue;2071 const lhs = opt_ctx.get() ?? continue;
22292072
2230 if (eatToken(&tok_it, &tree, Token.Id.Bang)) |bang| {2073 if (eatToken(&tok_it, &tree, Token.Id.Bang)) |bang| {
2231 const node = try arena.construct(ast.Node.InfixOp {2074 const node = try arena.construct(ast.Node.InfixOp{
2232 .base = ast.Node {.id = ast.Node.Id.InfixOp },2075 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
2233 .lhs = lhs,2076 .lhs = lhs,
2234 .op_token = bang,2077 .op_token = bang,
2235 .op = ast.Node.InfixOp.Op.ErrorUnion,2078 .op = ast.Node.InfixOp.Op.ErrorUnion,
2236 .rhs = undefined,2079 .rhs = undefined,
2237 });2080 });
2238 opt_ctx.store(&node.base);2081 opt_ctx.store(&node.base);
2239 stack.append(State { .TypeExprEnd = opt_ctx.toRequired() }) catch unreachable;2082 stack.append(State{ .TypeExprEnd = opt_ctx.toRequired() }) catch unreachable;
2240 try stack.append(State { .PrefixOpExpression = OptionalCtx { .Required = &node.rhs } });2083 try stack.append(State{ .PrefixOpExpression = OptionalCtx{ .Required = &node.rhs } });
2241 continue;2084 continue;
2242 }2085 }
2243 },2086 },
...@@ -2247,8 +2090,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2247,8 +2090,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2247 const token_index = token.index;2090 const token_index = token.index;
2248 const token_ptr = token.ptr;2091 const token_ptr = token.ptr;
2249 if (tokenIdToPrefixOp(token_ptr.id)) |prefix_id| {2092 if (tokenIdToPrefixOp(token_ptr.id)) |prefix_id| {
2250 var node = try arena.construct(ast.Node.PrefixOp {2093 var node = try arena.construct(ast.Node.PrefixOp{
2251 .base = ast.Node {.id = ast.Node.Id.PrefixOp },2094 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
2252 .op_token = token_index,2095 .op_token = token_index,
2253 .op = prefix_id,2096 .op = prefix_id,
2254 .rhs = undefined,2097 .rhs = undefined,
...@@ -2257,8 +2100,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2257,8 +2100,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
22572100
2258 // Treat '**' token as two derefs2101 // Treat '**' token as two derefs
2259 if (token_ptr.id == Token.Id.AsteriskAsterisk) {2102 if (token_ptr.id == Token.Id.AsteriskAsterisk) {
2260 const child = try arena.construct(ast.Node.PrefixOp {2103 const child = try arena.construct(ast.Node.PrefixOp{
2261 .base = ast.Node {.id = ast.Node.Id.PrefixOp},2104 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
2262 .op_token = token_index,2105 .op_token = token_index,
2263 .op = prefix_id,2106 .op = prefix_id,
2264 .rhs = undefined,2107 .rhs = undefined,
...@@ -2267,40 +2110,38 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2267,40 +2110,38 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2267 node = child;2110 node = child;
2268 }2111 }
22692112
2270 stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.rhs } }) catch unreachable;2113 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
2271 if (node.op == ast.Node.PrefixOp.Op.AddrOf) {2114 if (node.op == ast.Node.PrefixOp.Op.AddrOf) {
2272 try stack.append(State { .AddrOfModifiers = &node.op.AddrOf });2115 try stack.append(State{ .AddrOfModifiers = &node.op.AddrOf });
2273 }2116 }
2274 continue;2117 continue;
2275 } else {2118 } else {
2276 putBackToken(&tok_it, &tree);2119 putBackToken(&tok_it, &tree);
2277 stack.append(State { .SuffixOpExpressionBegin = opt_ctx }) catch unreachable;2120 stack.append(State{ .SuffixOpExpressionBegin = opt_ctx }) catch unreachable;
2278 continue;2121 continue;
2279 }2122 }
2280 },2123 },
22812124
2282 State.SuffixOpExpressionBegin => |opt_ctx| {2125 State.SuffixOpExpressionBegin => |opt_ctx| {
2283 if (eatToken(&tok_it, &tree, Token.Id.Keyword_async)) |async_token| {2126 if (eatToken(&tok_it, &tree, Token.Id.Keyword_async)) |async_token| {
2284 const async_node = try arena.construct(ast.Node.AsyncAttribute {2127 const async_node = try arena.construct(ast.Node.AsyncAttribute{
2285 .base = ast.Node {.id = ast.Node.Id.AsyncAttribute},2128 .base = ast.Node{ .id = ast.Node.Id.AsyncAttribute },
2286 .async_token = async_token,2129 .async_token = async_token,
2287 .allocator_type = null,2130 .allocator_type = null,
2288 .rangle_bracket = null,2131 .rangle_bracket = null,
2289 });2132 });
2290 stack.append(State {2133 stack.append(State{ .AsyncEnd = AsyncEndCtx{
2291 .AsyncEnd = AsyncEndCtx {2134 .ctx = opt_ctx,
2292 .ctx = opt_ctx,2135 .attribute = async_node,
2293 .attribute = async_node,2136 } }) catch unreachable;
2294 }2137 try stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() });
2295 }) catch unreachable;2138 try stack.append(State{ .PrimaryExpression = opt_ctx.toRequired() });
2296 try stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() });2139 try stack.append(State{ .AsyncAllocator = async_node });
2297 try stack.append(State { .PrimaryExpression = opt_ctx.toRequired() });
2298 try stack.append(State { .AsyncAllocator = async_node });
2299 continue;2140 continue;
2300 }2141 }
23012142
2302 stack.append(State { .SuffixOpExpressionEnd = opt_ctx }) catch unreachable;2143 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx }) catch unreachable;
2303 try stack.append(State { .PrimaryExpression = opt_ctx });2144 try stack.append(State{ .PrimaryExpression = opt_ctx });
2304 continue;2145 continue;
2305 },2146 },
23062147
...@@ -2312,48 +2153,42 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2312,48 +2153,42 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2312 const token_ptr = token.ptr;2153 const token_ptr = token.ptr;
2313 switch (token_ptr.id) {2154 switch (token_ptr.id) {
2314 Token.Id.LParen => {2155 Token.Id.LParen => {
2315 const node = try arena.construct(ast.Node.SuffixOp {2156 const node = try arena.construct(ast.Node.SuffixOp{
2316 .base = ast.Node {.id = ast.Node.Id.SuffixOp },2157 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
2317 .lhs = lhs,2158 .lhs = lhs,
2318 .op = ast.Node.SuffixOp.Op {2159 .op = ast.Node.SuffixOp.Op{ .Call = ast.Node.SuffixOp.Op.Call{
2319 .Call = ast.Node.SuffixOp.Op.Call {2160 .params = ast.Node.SuffixOp.Op.Call.ParamList.init(arena),
2320 .params = ast.Node.SuffixOp.Op.Call.ParamList.init(arena),2161 .async_attr = null,
2321 .async_attr = null,2162 } },
2322 }
2323 },
2324 .rtoken = undefined,2163 .rtoken = undefined,
2325 });2164 });
2326 opt_ctx.store(&node.base);2165 opt_ctx.store(&node.base);
23272166
2328 stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;2167 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2329 try stack.append(State {2168 try stack.append(State{ .ExprListItemOrEnd = ExprListCtx{
2330 .ExprListItemOrEnd = ExprListCtx {2169 .list = &node.op.Call.params,
2331 .list = &node.op.Call.params,2170 .end = Token.Id.RParen,
2332 .end = Token.Id.RParen,2171 .ptr = &node.rtoken,
2333 .ptr = &node.rtoken,2172 } });
2334 }
2335 });
2336 continue;2173 continue;
2337 },2174 },
2338 Token.Id.LBracket => {2175 Token.Id.LBracket => {
2339 const node = try arena.construct(ast.Node.SuffixOp {2176 const node = try arena.construct(ast.Node.SuffixOp{
2340 .base = ast.Node {.id = ast.Node.Id.SuffixOp },2177 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
2341 .lhs = lhs,2178 .lhs = lhs,
2342 .op = ast.Node.SuffixOp.Op {2179 .op = ast.Node.SuffixOp.Op{ .ArrayAccess = undefined },
2343 .ArrayAccess = undefined,2180 .rtoken = undefined,
2344 },
2345 .rtoken = undefined
2346 });2181 });
2347 opt_ctx.store(&node.base);2182 opt_ctx.store(&node.base);
23482183
2349 stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;2184 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2350 try stack.append(State { .SliceOrArrayAccess = node });2185 try stack.append(State{ .SliceOrArrayAccess = node });
2351 try stack.append(State { .Expression = OptionalCtx { .Required = &node.op.ArrayAccess }});2186 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.op.ArrayAccess } });
2352 continue;2187 continue;
2353 },2188 },
2354 Token.Id.Period => {2189 Token.Id.Period => {
2355 const node = try arena.construct(ast.Node.InfixOp {2190 const node = try arena.construct(ast.Node.InfixOp{
2356 .base = ast.Node {.id = ast.Node.Id.InfixOp },2191 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
2357 .lhs = lhs,2192 .lhs = lhs,
2358 .op_token = token_index,2193 .op_token = token_index,
2359 .op = ast.Node.InfixOp.Op.Period,2194 .op = ast.Node.InfixOp.Op.Period,
...@@ -2361,8 +2196,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2361,8 +2196,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2361 });2196 });
2362 opt_ctx.store(&node.base);2197 opt_ctx.store(&node.base);
23632198
2364 stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;2199 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2365 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.rhs } });2200 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.rhs } });
2366 continue;2201 continue;
2367 },2202 },
2368 else => {2203 else => {
...@@ -2391,7 +2226,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2391,7 +2226,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2391 _ = try createToCtxLiteral(arena, opt_ctx, ast.Node.UndefinedLiteral, token.index);2226 _ = try createToCtxLiteral(arena, opt_ctx, ast.Node.UndefinedLiteral, token.index);
2392 continue;2227 continue;
2393 },2228 },
2394 Token.Id.Keyword_true, Token.Id.Keyword_false => {2229 Token.Id.Keyword_true,
2230 Token.Id.Keyword_false => {
2395 _ = try createToCtxLiteral(arena, opt_ctx, ast.Node.BoolLiteral, token.index);2231 _ = try createToCtxLiteral(arena, opt_ctx, ast.Node.BoolLiteral, token.index);
2396 continue;2232 continue;
2397 },2233 },
...@@ -2412,10 +2248,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2412,10 +2248,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2412 continue;2248 continue;
2413 },2249 },
2414 Token.Id.Keyword_promise => {2250 Token.Id.Keyword_promise => {
2415 const node = try arena.construct(ast.Node.PromiseType {2251 const node = try arena.construct(ast.Node.PromiseType{
2416 .base = ast.Node {2252 .base = ast.Node{ .id = ast.Node.Id.PromiseType },
2417 .id = ast.Node.Id.PromiseType,
2418 },
2419 .promise_token = token.index,2253 .promise_token = token.index,
2420 .result = null,2254 .result = null,
2421 });2255 });
...@@ -2427,121 +2261,108 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2427,121 +2261,108 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2427 putBackToken(&tok_it, &tree);2261 putBackToken(&tok_it, &tree);
2428 continue;2262 continue;
2429 }2263 }
2430 node.result = ast.Node.PromiseType.Result {2264 node.result = ast.Node.PromiseType.Result{
2431 .arrow_token = next_token_index,2265 .arrow_token = next_token_index,
2432 .return_type = undefined,2266 .return_type = undefined,
2433 };2267 };
2434 const return_type_ptr = &((??node.result).return_type);2268 const return_type_ptr = &((??node.result).return_type);
2435 try stack.append(State { .Expression = OptionalCtx { .Required = return_type_ptr, } });2269 try stack.append(State{ .Expression = OptionalCtx{ .Required = return_type_ptr } });
2436 continue;2270 continue;
2437 },2271 },
2438 Token.Id.StringLiteral, Token.Id.MultilineStringLiteralLine => {2272 Token.Id.StringLiteral,
2273 Token.Id.MultilineStringLiteralLine => {
2439 opt_ctx.store((try parseStringLiteral(arena, &tok_it, token.ptr, token.index, &tree)) ?? unreachable);2274 opt_ctx.store((try parseStringLiteral(arena, &tok_it, token.ptr, token.index, &tree)) ?? unreachable);
2440 continue;2275 continue;
2441 },2276 },
2442 Token.Id.LParen => {2277 Token.Id.LParen => {
2443 const node = try arena.construct(ast.Node.GroupedExpression {2278 const node = try arena.construct(ast.Node.GroupedExpression{
2444 .base = ast.Node {.id = ast.Node.Id.GroupedExpression },2279 .base = ast.Node{ .id = ast.Node.Id.GroupedExpression },
2445 .lparen = token.index,2280 .lparen = token.index,
2446 .expr = undefined,2281 .expr = undefined,
2447 .rparen = undefined,2282 .rparen = undefined,
2448 });2283 });
2449 opt_ctx.store(&node.base);2284 opt_ctx.store(&node.base);
24502285
2451 stack.append(State {2286 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
2452 .ExpectTokenSave = ExpectTokenSave {2287 .id = Token.Id.RParen,
2453 .id = Token.Id.RParen,2288 .ptr = &node.rparen,
2454 .ptr = &node.rparen,2289 } }) catch unreachable;
2455 }2290 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
2456 }) catch unreachable;
2457 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });
2458 continue;2291 continue;
2459 },2292 },
2460 Token.Id.Builtin => {2293 Token.Id.Builtin => {
2461 const node = try arena.construct(ast.Node.BuiltinCall {2294 const node = try arena.construct(ast.Node.BuiltinCall{
2462 .base = ast.Node {.id = ast.Node.Id.BuiltinCall },2295 .base = ast.Node{ .id = ast.Node.Id.BuiltinCall },
2463 .builtin_token = token.index,2296 .builtin_token = token.index,
2464 .params = ast.Node.BuiltinCall.ParamList.init(arena),2297 .params = ast.Node.BuiltinCall.ParamList.init(arena),
2465 .rparen_token = undefined,2298 .rparen_token = undefined,
2466 });2299 });
2467 opt_ctx.store(&node.base);2300 opt_ctx.store(&node.base);
24682301
2469 stack.append(State {2302 stack.append(State{ .ExprListItemOrEnd = ExprListCtx{
2470 .ExprListItemOrEnd = ExprListCtx {2303 .list = &node.params,
2471 .list = &node.params,2304 .end = Token.Id.RParen,
2472 .end = Token.Id.RParen,2305 .ptr = &node.rparen_token,
2473 .ptr = &node.rparen_token,2306 } }) catch unreachable;
2474 }2307 try stack.append(State{ .ExpectToken = Token.Id.LParen });
2475 }) catch unreachable;
2476 try stack.append(State { .ExpectToken = Token.Id.LParen, });
2477 continue;2308 continue;
2478 },2309 },
2479 Token.Id.LBracket => {2310 Token.Id.LBracket => {
2480 const node = try arena.construct(ast.Node.PrefixOp {2311 const node = try arena.construct(ast.Node.PrefixOp{
2481 .base = ast.Node {.id = ast.Node.Id.PrefixOp },2312 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
2482 .op_token = token.index,2313 .op_token = token.index,
2483 .op = undefined,2314 .op = undefined,
2484 .rhs = undefined,2315 .rhs = undefined,
2485 });2316 });
2486 opt_ctx.store(&node.base);2317 opt_ctx.store(&node.base);
24872318
2488 stack.append(State { .SliceOrArrayType = node }) catch unreachable;2319 stack.append(State{ .SliceOrArrayType = node }) catch unreachable;
2489 continue;2320 continue;
2490 },2321 },
2491 Token.Id.Keyword_error => {2322 Token.Id.Keyword_error => {
2492 stack.append(State {2323 stack.append(State{ .ErrorTypeOrSetDecl = ErrorTypeOrSetDeclCtx{
2493 .ErrorTypeOrSetDecl = ErrorTypeOrSetDeclCtx {2324 .error_token = token.index,
2494 .error_token = token.index,2325 .opt_ctx = opt_ctx,
2495 .opt_ctx = opt_ctx2326 } }) catch unreachable;
2496 }
2497 }) catch unreachable;
2498 continue;2327 continue;
2499 },2328 },
2500 Token.Id.Keyword_packed => {2329 Token.Id.Keyword_packed => {
2501 stack.append(State {2330 stack.append(State{ .ContainerKind = ContainerKindCtx{
2502 .ContainerKind = ContainerKindCtx {2331 .opt_ctx = opt_ctx,
2503 .opt_ctx = opt_ctx,2332 .ltoken = token.index,
2504 .ltoken = token.index,2333 .layout = ast.Node.ContainerDecl.Layout.Packed,
2505 .layout = ast.Node.ContainerDecl.Layout.Packed,2334 } }) catch unreachable;
2506 },
2507 }) catch unreachable;
2508 continue;2335 continue;
2509 },2336 },
2510 Token.Id.Keyword_extern => {2337 Token.Id.Keyword_extern => {
2511 stack.append(State {2338 stack.append(State{ .ExternType = ExternTypeCtx{
2512 .ExternType = ExternTypeCtx {2339 .opt_ctx = opt_ctx,
2513 .opt_ctx = opt_ctx,2340 .extern_token = token.index,
2514 .extern_token = token.index,2341 .comments = null,
2515 .comments = null,2342 } }) catch unreachable;
2516 },
2517 }) catch unreachable;
2518 continue;2343 continue;
2519 },2344 },
2520 Token.Id.Keyword_struct, Token.Id.Keyword_union, Token.Id.Keyword_enum => {2345 Token.Id.Keyword_struct,
2346 Token.Id.Keyword_union,
2347 Token.Id.Keyword_enum => {
2521 putBackToken(&tok_it, &tree);2348 putBackToken(&tok_it, &tree);
2522 stack.append(State {2349 stack.append(State{ .ContainerKind = ContainerKindCtx{
2523 .ContainerKind = ContainerKindCtx {2350 .opt_ctx = opt_ctx,
2524 .opt_ctx = opt_ctx,2351 .ltoken = token.index,
2525 .ltoken = token.index,2352 .layout = ast.Node.ContainerDecl.Layout.Auto,
2526 .layout = ast.Node.ContainerDecl.Layout.Auto,2353 } }) catch unreachable;
2527 },
2528 }) catch unreachable;
2529 continue;2354 continue;
2530 },2355 },
2531 Token.Id.Identifier => {2356 Token.Id.Identifier => {
2532 stack.append(State {2357 stack.append(State{ .MaybeLabeledExpression = MaybeLabeledExpressionCtx{
2533 .MaybeLabeledExpression = MaybeLabeledExpressionCtx {2358 .label = token.index,
2534 .label = token.index,2359 .opt_ctx = opt_ctx,
2535 .opt_ctx = opt_ctx2360 } }) catch unreachable;
2536 }
2537 }) catch unreachable;
2538 continue;2361 continue;
2539 },2362 },
2540 Token.Id.Keyword_fn => {2363 Token.Id.Keyword_fn => {
2541 const fn_proto = try arena.construct(ast.Node.FnProto {2364 const fn_proto = try arena.construct(ast.Node.FnProto{
2542 .base = ast.Node {2365 .base = ast.Node{ .id = ast.Node.Id.FnProto },
2543 .id = ast.Node.Id.FnProto,
2544 },
2545 .doc_comments = null,2366 .doc_comments = null,
2546 .visib_token = null,2367 .visib_token = null,
2547 .name_token = null,2368 .name_token = null,
...@@ -2557,14 +2378,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2557,14 +2378,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2557 .align_expr = null,2378 .align_expr = null,
2558 });2379 });
2559 opt_ctx.store(&fn_proto.base);2380 opt_ctx.store(&fn_proto.base);
2560 stack.append(State { .FnProto = fn_proto }) catch unreachable;2381 stack.append(State{ .FnProto = fn_proto }) catch unreachable;
2561 continue;2382 continue;
2562 },2383 },
2563 Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => {2384 Token.Id.Keyword_nakedcc,
2564 const fn_proto = try arena.construct(ast.Node.FnProto {2385 Token.Id.Keyword_stdcallcc => {
2565 .base = ast.Node {2386 const fn_proto = try arena.construct(ast.Node.FnProto{
2566 .id = ast.Node.Id.FnProto,2387 .base = ast.Node{ .id = ast.Node.Id.FnProto },
2567 },
2568 .doc_comments = null,2388 .doc_comments = null,
2569 .visib_token = null,2389 .visib_token = null,
2570 .name_token = null,2390 .name_token = null,
...@@ -2580,18 +2400,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2580,18 +2400,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2580 .align_expr = null,2400 .align_expr = null,
2581 });2401 });
2582 opt_ctx.store(&fn_proto.base);2402 opt_ctx.store(&fn_proto.base);
2583 stack.append(State { .FnProto = fn_proto }) catch unreachable;2403 stack.append(State{ .FnProto = fn_proto }) catch unreachable;
2584 try stack.append(State {2404 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
2585 .ExpectTokenSave = ExpectTokenSave {2405 .id = Token.Id.Keyword_fn,
2586 .id = Token.Id.Keyword_fn,2406 .ptr = &fn_proto.fn_token,
2587 .ptr = &fn_proto.fn_token2407 } });
2588 }
2589 });
2590 continue;2408 continue;
2591 },2409 },
2592 Token.Id.Keyword_asm => {2410 Token.Id.Keyword_asm => {
2593 const node = try arena.construct(ast.Node.Asm {2411 const node = try arena.construct(ast.Node.Asm{
2594 .base = ast.Node {.id = ast.Node.Id.Asm },2412 .base = ast.Node{ .id = ast.Node.Id.Asm },
2595 .asm_token = token.index,2413 .asm_token = token.index,
2596 .volatile_token = null,2414 .volatile_token = null,
2597 .template = undefined,2415 .template = undefined,
...@@ -2602,94 +2420,77 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2602,94 +2420,77 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2602 });2420 });
2603 opt_ctx.store(&node.base);2421 opt_ctx.store(&node.base);
26042422
2605 stack.append(State {2423 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
2606 .ExpectTokenSave = ExpectTokenSave {2424 .id = Token.Id.RParen,
2607 .id = Token.Id.RParen,2425 .ptr = &node.rparen,
2608 .ptr = &node.rparen,2426 } }) catch unreachable;
2609 }2427 try stack.append(State{ .AsmClobberItems = &node.clobbers });
2610 }) catch unreachable;2428 try stack.append(State{ .IfToken = Token.Id.Colon });
2611 try stack.append(State { .AsmClobberItems = &node.clobbers });2429 try stack.append(State{ .AsmInputItems = &node.inputs });
2612 try stack.append(State { .IfToken = Token.Id.Colon });2430 try stack.append(State{ .IfToken = Token.Id.Colon });
2613 try stack.append(State { .AsmInputItems = &node.inputs });2431 try stack.append(State{ .AsmOutputItems = &node.outputs });
2614 try stack.append(State { .IfToken = Token.Id.Colon });2432 try stack.append(State{ .IfToken = Token.Id.Colon });
2615 try stack.append(State { .AsmOutputItems = &node.outputs });2433 try stack.append(State{ .StringLiteral = OptionalCtx{ .Required = &node.template } });
2616 try stack.append(State { .IfToken = Token.Id.Colon });2434 try stack.append(State{ .ExpectToken = Token.Id.LParen });
2617 try stack.append(State { .StringLiteral = OptionalCtx { .Required = &node.template } });2435 try stack.append(State{ .OptionalTokenSave = OptionalTokenSave{
2618 try stack.append(State { .ExpectToken = Token.Id.LParen });2436 .id = Token.Id.Keyword_volatile,
2619 try stack.append(State {2437 .ptr = &node.volatile_token,
2620 .OptionalTokenSave = OptionalTokenSave {2438 } });
2621 .id = Token.Id.Keyword_volatile,
2622 .ptr = &node.volatile_token,
2623 }
2624 });
2625 },2439 },
2626 Token.Id.Keyword_inline => {2440 Token.Id.Keyword_inline => {
2627 stack.append(State {2441 stack.append(State{ .Inline = InlineCtx{
2628 .Inline = InlineCtx {2442 .label = null,
2629 .label = null,2443 .inline_token = token.index,
2630 .inline_token = token.index,2444 .opt_ctx = opt_ctx,
2631 .opt_ctx = opt_ctx,2445 } }) catch unreachable;
2632 }
2633 }) catch unreachable;
2634 continue;2446 continue;
2635 },2447 },
2636 else => {2448 else => {
2637 if (!try parseBlockExpr(&stack, arena, opt_ctx, token.ptr, token.index)) {2449 if (!try parseBlockExpr(&stack, arena, opt_ctx, token.ptr, token.index)) {
2638 putBackToken(&tok_it, &tree);2450 putBackToken(&tok_it, &tree);
2639 if (opt_ctx != OptionalCtx.Optional) {2451 if (opt_ctx != OptionalCtx.Optional) {
2640 *(try tree.errors.addOne()) = Error {2452 ((try tree.errors.addOne())).* = Error{ .ExpectedPrimaryExpr = Error.ExpectedPrimaryExpr{ .token = token.index } };
2641 .ExpectedPrimaryExpr = Error.ExpectedPrimaryExpr { .token = token.index },
2642 };
2643 return tree;2453 return tree;
2644 }2454 }
2645 }2455 }
2646 continue;2456 continue;
2647 }2457 },
2648 }2458 }
2649 },2459 },
26502460
2651
2652 State.ErrorTypeOrSetDecl => |ctx| {2461 State.ErrorTypeOrSetDecl => |ctx| {
2653 if (eatToken(&tok_it, &tree, Token.Id.LBrace) == null) {2462 if (eatToken(&tok_it, &tree, Token.Id.LBrace) == null) {
2654 _ = try createToCtxLiteral(arena, ctx.opt_ctx, ast.Node.ErrorType, ctx.error_token);2463 _ = try createToCtxLiteral(arena, ctx.opt_ctx, ast.Node.ErrorType, ctx.error_token);
2655 continue;2464 continue;
2656 }2465 }
26572466
2658 const node = try arena.construct(ast.Node.ErrorSetDecl {2467 const node = try arena.construct(ast.Node.ErrorSetDecl{
2659 .base = ast.Node {2468 .base = ast.Node{ .id = ast.Node.Id.ErrorSetDecl },
2660 .id = ast.Node.Id.ErrorSetDecl,
2661 },
2662 .error_token = ctx.error_token,2469 .error_token = ctx.error_token,
2663 .decls = ast.Node.ErrorSetDecl.DeclList.init(arena),2470 .decls = ast.Node.ErrorSetDecl.DeclList.init(arena),
2664 .rbrace_token = undefined,2471 .rbrace_token = undefined,
2665 });2472 });
2666 ctx.opt_ctx.store(&node.base);2473 ctx.opt_ctx.store(&node.base);
26672474
2668 stack.append(State {2475 stack.append(State{ .ErrorTagListItemOrEnd = ListSave(@typeOf(node.decls)){
2669 .ErrorTagListItemOrEnd = ListSave(@typeOf(node.decls)) {2476 .list = &node.decls,
2670 .list = &node.decls,2477 .ptr = &node.rbrace_token,
2671 .ptr = &node.rbrace_token,2478 } }) catch unreachable;
2672 }
2673 }) catch unreachable;
2674 continue;2479 continue;
2675 },2480 },
2676 State.StringLiteral => |opt_ctx| {2481 State.StringLiteral => |opt_ctx| {
2677 const token = nextToken(&tok_it, &tree);2482 const token = nextToken(&tok_it, &tree);
2678 const token_index = token.index;2483 const token_index = token.index;
2679 const token_ptr = token.ptr;2484 const token_ptr = token.ptr;
2680 opt_ctx.store(2485 opt_ctx.store((try parseStringLiteral(arena, &tok_it, token_ptr, token_index, &tree)) ?? {
2681 (try parseStringLiteral(arena, &tok_it, token_ptr, token_index, &tree)) ?? {2486 putBackToken(&tok_it, &tree);
2682 putBackToken(&tok_it, &tree);2487 if (opt_ctx != OptionalCtx.Optional) {
2683 if (opt_ctx != OptionalCtx.Optional) {2488 ((try tree.errors.addOne())).* = Error{ .ExpectedPrimaryExpr = Error.ExpectedPrimaryExpr{ .token = token_index } };
2684 *(try tree.errors.addOne()) = Error {2489 return tree;
2685 .ExpectedPrimaryExpr = Error.ExpectedPrimaryExpr { .token = token_index },
2686 };
2687 return tree;
2688 }
2689
2690 continue;
2691 }2490 }
2692 );2491
2492 continue;
2493 });
2693 },2494 },
26942495
2695 State.Identifier => |opt_ctx| {2496 State.Identifier => |opt_ctx| {
...@@ -2702,12 +2503,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2702,12 +2503,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2702 const token = nextToken(&tok_it, &tree);2503 const token = nextToken(&tok_it, &tree);
2703 const token_index = token.index;2504 const token_index = token.index;
2704 const token_ptr = token.ptr;2505 const token_ptr = token.ptr;
2705 *(try tree.errors.addOne()) = Error {2506 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
2706 .ExpectedToken = Error.ExpectedToken {2507 .token = token_index,
2707 .token = token_index,2508 .expected_id = Token.Id.Identifier,
2708 .expected_id = Token.Id.Identifier,2509 } };
2709 },
2710 };
2711 return tree;2510 return tree;
2712 }2511 }
2713 },2512 },
...@@ -2718,23 +2517,19 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2718,23 +2517,19 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2718 const ident_token_index = ident_token.index;2517 const ident_token_index = ident_token.index;
2719 const ident_token_ptr = ident_token.ptr;2518 const ident_token_ptr = ident_token.ptr;
2720 if (ident_token_ptr.id != Token.Id.Identifier) {2519 if (ident_token_ptr.id != Token.Id.Identifier) {
2721 *(try tree.errors.addOne()) = Error {2520 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
2722 .ExpectedToken = Error.ExpectedToken {2521 .token = ident_token_index,
2723 .token = ident_token_index,2522 .expected_id = Token.Id.Identifier,
2724 .expected_id = Token.Id.Identifier,2523 } };
2725 },
2726 };
2727 return tree;2524 return tree;
2728 }2525 }
27292526
2730 const node = try arena.construct(ast.Node.ErrorTag {2527 const node = try arena.construct(ast.Node.ErrorTag{
2731 .base = ast.Node {2528 .base = ast.Node{ .id = ast.Node.Id.ErrorTag },
2732 .id = ast.Node.Id.ErrorTag,
2733 },
2734 .doc_comments = comments,2529 .doc_comments = comments,
2735 .name_token = ident_token_index,2530 .name_token = ident_token_index,
2736 });2531 });
2737 *node_ptr = &node.base;2532 node_ptr.* = &node.base;
2738 continue;2533 continue;
2739 },2534 },
27402535
...@@ -2743,12 +2538,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2743,12 +2538,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2743 const token_index = token.index;2538 const token_index = token.index;
2744 const token_ptr = token.ptr;2539 const token_ptr = token.ptr;
2745 if (token_ptr.id != token_id) {2540 if (token_ptr.id != token_id) {
2746 *(try tree.errors.addOne()) = Error {2541 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
2747 .ExpectedToken = Error.ExpectedToken {2542 .token = token_index,
2748 .token = token_index,2543 .expected_id = token_id,
2749 .expected_id = token_id,2544 } };
2750 },
2751 };
2752 return tree;2545 return tree;
2753 }2546 }
2754 continue;2547 continue;
...@@ -2758,15 +2551,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2758,15 +2551,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2758 const token_index = token.index;2551 const token_index = token.index;
2759 const token_ptr = token.ptr;2552 const token_ptr = token.ptr;
2760 if (token_ptr.id != expect_token_save.id) {2553 if (token_ptr.id != expect_token_save.id) {
2761 *(try tree.errors.addOne()) = Error {2554 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
2762 .ExpectedToken = Error.ExpectedToken {2555 .token = token_index,
2763 .token = token_index,2556 .expected_id = expect_token_save.id,
2764 .expected_id = expect_token_save.id,2557 } };
2765 },
2766 };
2767 return tree;2558 return tree;
2768 }2559 }
2769 *expect_token_save.ptr = token_index;2560 (expect_token_save.ptr).* = token_index;
2770 continue;2561 continue;
2771 },2562 },
2772 State.IfToken => |token_id| {2563 State.IfToken => |token_id| {
...@@ -2779,7 +2570,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2779,7 +2570,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2779 },2570 },
2780 State.IfTokenSave => |if_token_save| {2571 State.IfTokenSave => |if_token_save| {
2781 if (eatToken(&tok_it, &tree, if_token_save.id)) |token_index| {2572 if (eatToken(&tok_it, &tree, if_token_save.id)) |token_index| {
2782 *if_token_save.ptr = token_index;2573 (if_token_save.ptr).* = token_index;
2783 continue;2574 continue;
2784 }2575 }
27852576
...@@ -2788,7 +2579,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -2788,7 +2579,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
2788 },2579 },
2789 State.OptionalTokenSave => |optional_token_save| {2580 State.OptionalTokenSave => |optional_token_save| {
2790 if (eatToken(&tok_it, &tree, optional_token_save.id)) |token_index| {2581 if (eatToken(&tok_it, &tree, optional_token_save.id)) |token_index| {
2791 *optional_token_save.ptr = token_index;2582 (optional_token_save.ptr).* = token_index;
2792 continue;2583 continue;
2793 }2584 }
27942585
...@@ -2911,28 +2702,28 @@ const OptionalCtx = union(enum) {...@@ -2911,28 +2702,28 @@ const OptionalCtx = union(enum) {
2911 Required: &&ast.Node,2702 Required: &&ast.Node,
29122703
2913 pub fn store(self: &const OptionalCtx, value: &ast.Node) void {2704 pub fn store(self: &const OptionalCtx, value: &ast.Node) void {
2914 switch (*self) {2705 switch (self.*) {
2915 OptionalCtx.Optional => |ptr| *ptr = value,2706 OptionalCtx.Optional => |ptr| ptr.* = value,
2916 OptionalCtx.RequiredNull => |ptr| *ptr = value,2707 OptionalCtx.RequiredNull => |ptr| ptr.* = value,
2917 OptionalCtx.Required => |ptr| *ptr = value,2708 OptionalCtx.Required => |ptr| ptr.* = value,
2918 }2709 }
2919 }2710 }
29202711
2921 pub fn get(self: &const OptionalCtx) ?&ast.Node {2712 pub fn get(self: &const OptionalCtx) ?&ast.Node {
2922 switch (*self) {2713 switch (self.*) {
2923 OptionalCtx.Optional => |ptr| return *ptr,2714 OptionalCtx.Optional => |ptr| return ptr.*,
2924 OptionalCtx.RequiredNull => |ptr| return ??*ptr,2715 OptionalCtx.RequiredNull => |ptr| return ??ptr.*,
2925 OptionalCtx.Required => |ptr| return *ptr,2716 OptionalCtx.Required => |ptr| return ptr.*,
2926 }2717 }
2927 }2718 }
29282719
2929 pub fn toRequired(self: &const OptionalCtx) OptionalCtx {2720 pub fn toRequired(self: &const OptionalCtx) OptionalCtx {
2930 switch (*self) {2721 switch (self.*) {
2931 OptionalCtx.Optional => |ptr| {2722 OptionalCtx.Optional => |ptr| {
2932 return OptionalCtx { .RequiredNull = ptr };2723 return OptionalCtx{ .RequiredNull = ptr };
2933 },2724 },
2934 OptionalCtx.RequiredNull => |ptr| return *self,2725 OptionalCtx.RequiredNull => |ptr| return self.*,
2935 OptionalCtx.Required => |ptr| return *self,2726 OptionalCtx.Required => |ptr| return self.*,
2936 }2727 }
2937 }2728 }
2938};2729};
...@@ -3054,7 +2845,6 @@ const State = union(enum) {...@@ -3054,7 +2845,6 @@ const State = union(enum) {
3054 Identifier: OptionalCtx,2845 Identifier: OptionalCtx,
3055 ErrorTag: &&ast.Node,2846 ErrorTag: &&ast.Node,
30562847
3057
3058 IfToken: @TagType(Token.Id),2848 IfToken: @TagType(Token.Id),
3059 IfTokenSave: ExpectTokenSave,2849 IfTokenSave: ExpectTokenSave,
3060 ExpectToken: @TagType(Token.Id),2850 ExpectToken: @TagType(Token.Id),
...@@ -3064,16 +2854,14 @@ const State = union(enum) {...@@ -3064,16 +2854,14 @@ const State = union(enum) {
30642854
3065fn pushDocComment(arena: &mem.Allocator, line_comment: TokenIndex, result: &?&ast.Node.DocComment) !void {2855fn pushDocComment(arena: &mem.Allocator, line_comment: TokenIndex, result: &?&ast.Node.DocComment) !void {
3066 const node = blk: {2856 const node = blk: {
3067 if (*result) |comment_node| {2857 if (result.*) |comment_node| {
3068 break :blk comment_node;2858 break :blk comment_node;
3069 } else {2859 } else {
3070 const comment_node = try arena.construct(ast.Node.DocComment {2860 const comment_node = try arena.construct(ast.Node.DocComment{
3071 .base = ast.Node {2861 .base = ast.Node{ .id = ast.Node.Id.DocComment },
3072 .id = ast.Node.Id.DocComment,
3073 },
3074 .lines = ast.Node.DocComment.LineList.init(arena),2862 .lines = ast.Node.DocComment.LineList.init(arena),
3075 });2863 });
3076 *result = comment_node;2864 result.* = comment_node;
3077 break :blk comment_node;2865 break :blk comment_node;
3078 }2866 }
3079 };2867 };
...@@ -3094,24 +2882,20 @@ fn eatDocComments(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, t...@@ -3094,24 +2882,20 @@ fn eatDocComments(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, t
30942882
3095fn eatLineComment(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) !?&ast.Node.LineComment {2883fn eatLineComment(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) !?&ast.Node.LineComment {
3096 const token = eatToken(tok_it, tree, Token.Id.LineComment) ?? return null;2884 const token = eatToken(tok_it, tree, Token.Id.LineComment) ?? return null;
3097 return try arena.construct(ast.Node.LineComment {2885 return try arena.construct(ast.Node.LineComment{
3098 .base = ast.Node {2886 .base = ast.Node{ .id = ast.Node.Id.LineComment },
3099 .id = ast.Node.Id.LineComment,
3100 },
3101 .token = token,2887 .token = token,
3102 });2888 });
3103}2889}
31042890
3105fn parseStringLiteral(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator,2891fn parseStringLiteral(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, token_ptr: &const Token, token_index: TokenIndex, tree: &ast.Tree) !?&ast.Node {
3106 token_ptr: &const Token, token_index: TokenIndex, tree: &ast.Tree) !?&ast.Node
3107{
3108 switch (token_ptr.id) {2892 switch (token_ptr.id) {
3109 Token.Id.StringLiteral => {2893 Token.Id.StringLiteral => {
3110 return &(try createLiteral(arena, ast.Node.StringLiteral, token_index)).base;2894 return &(try createLiteral(arena, ast.Node.StringLiteral, token_index)).base;
3111 },2895 },
3112 Token.Id.MultilineStringLiteralLine => {2896 Token.Id.MultilineStringLiteralLine => {
3113 const node = try arena.construct(ast.Node.MultilineStringLiteral {2897 const node = try arena.construct(ast.Node.MultilineStringLiteral{
3114 .base = ast.Node { .id = ast.Node.Id.MultilineStringLiteral },2898 .base = ast.Node{ .id = ast.Node.Id.MultilineStringLiteral },
3115 .lines = ast.Node.MultilineStringLiteral.LineList.init(arena),2899 .lines = ast.Node.MultilineStringLiteral.LineList.init(arena),
3116 });2900 });
3117 try node.lines.push(token_index);2901 try node.lines.push(token_index);
...@@ -3135,12 +2919,11 @@ fn parseStringLiteral(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterato...@@ -3135,12 +2919,11 @@ fn parseStringLiteral(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterato
3135 }2919 }
3136}2920}
31372921
3138fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &const OptionalCtx,2922fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &const OptionalCtx, token_ptr: &const Token, token_index: TokenIndex) !bool {
3139 token_ptr: &const Token, token_index: TokenIndex) !bool {
3140 switch (token_ptr.id) {2923 switch (token_ptr.id) {
3141 Token.Id.Keyword_suspend => {2924 Token.Id.Keyword_suspend => {
3142 const node = try arena.construct(ast.Node.Suspend {2925 const node = try arena.construct(ast.Node.Suspend{
3143 .base = ast.Node {.id = ast.Node.Id.Suspend },2926 .base = ast.Node{ .id = ast.Node.Id.Suspend },
3144 .label = null,2927 .label = null,
3145 .suspend_token = token_index,2928 .suspend_token = token_index,
3146 .payload = null,2929 .payload = null,
...@@ -3148,13 +2931,13 @@ fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &con...@@ -3148,13 +2931,13 @@ fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &con
3148 });2931 });
3149 ctx.store(&node.base);2932 ctx.store(&node.base);
31502933
3151 stack.append(State { .SuspendBody = node }) catch unreachable;2934 stack.append(State{ .SuspendBody = node }) catch unreachable;
3152 try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } });2935 try stack.append(State{ .Payload = OptionalCtx{ .Optional = &node.payload } });
3153 return true;2936 return true;
3154 },2937 },
3155 Token.Id.Keyword_if => {2938 Token.Id.Keyword_if => {
3156 const node = try arena.construct(ast.Node.If {2939 const node = try arena.construct(ast.Node.If{
3157 .base = ast.Node {.id = ast.Node.Id.If },2940 .base = ast.Node{ .id = ast.Node.Id.If },
3158 .if_token = token_index,2941 .if_token = token_index,
3159 .condition = undefined,2942 .condition = undefined,
3160 .payload = null,2943 .payload = null,
...@@ -3163,41 +2946,35 @@ fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &con...@@ -3163,41 +2946,35 @@ fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &con
3163 });2946 });
3164 ctx.store(&node.base);2947 ctx.store(&node.base);
31652948
3166 stack.append(State { .Else = &node.@"else" }) catch unreachable;2949 stack.append(State{ .Else = &node.@"else" }) catch unreachable;
3167 try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } });2950 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } });
3168 try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });2951 try stack.append(State{ .PointerPayload = OptionalCtx{ .Optional = &node.payload } });
3169 try stack.append(State { .ExpectToken = Token.Id.RParen });2952 try stack.append(State{ .ExpectToken = Token.Id.RParen });
3170 try stack.append(State { .Expression = OptionalCtx { .Required = &node.condition } });2953 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.condition } });
3171 try stack.append(State { .ExpectToken = Token.Id.LParen });2954 try stack.append(State{ .ExpectToken = Token.Id.LParen });
3172 return true;2955 return true;
3173 },2956 },
3174 Token.Id.Keyword_while => {2957 Token.Id.Keyword_while => {
3175 stack.append(State {2958 stack.append(State{ .While = LoopCtx{
3176 .While = LoopCtx {2959 .label = null,
3177 .label = null,2960 .inline_token = null,
3178 .inline_token = null,2961 .loop_token = token_index,
3179 .loop_token = token_index,2962 .opt_ctx = ctx.*,
3180 .opt_ctx = *ctx,2963 } }) catch unreachable;
3181 }
3182 }) catch unreachable;
3183 return true;2964 return true;
3184 },2965 },
3185 Token.Id.Keyword_for => {2966 Token.Id.Keyword_for => {
3186 stack.append(State {2967 stack.append(State{ .For = LoopCtx{
3187 .For = LoopCtx {2968 .label = null,
3188 .label = null,2969 .inline_token = null,
3189 .inline_token = null,2970 .loop_token = token_index,
3190 .loop_token = token_index,2971 .opt_ctx = ctx.*,
3191 .opt_ctx = *ctx,2972 } }) catch unreachable;
3192 }
3193 }) catch unreachable;
3194 return true;2973 return true;
3195 },2974 },
3196 Token.Id.Keyword_switch => {2975 Token.Id.Keyword_switch => {
3197 const node = try arena.construct(ast.Node.Switch {2976 const node = try arena.construct(ast.Node.Switch{
3198 .base = ast.Node {2977 .base = ast.Node{ .id = ast.Node.Id.Switch },
3199 .id = ast.Node.Id.Switch,
3200 },
3201 .switch_token = token_index,2978 .switch_token = token_index,
3202 .expr = undefined,2979 .expr = undefined,
3203 .cases = ast.Node.Switch.CaseList.init(arena),2980 .cases = ast.Node.Switch.CaseList.init(arena),
...@@ -3205,45 +2982,43 @@ fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &con...@@ -3205,45 +2982,43 @@ fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &con
3205 });2982 });
3206 ctx.store(&node.base);2983 ctx.store(&node.base);
32072984
3208 stack.append(State {2985 stack.append(State{ .SwitchCaseOrEnd = ListSave(@typeOf(node.cases)){
3209 .SwitchCaseOrEnd = ListSave(@typeOf(node.cases)) {2986 .list = &node.cases,
3210 .list = &node.cases,2987 .ptr = &node.rbrace,
3211 .ptr = &node.rbrace,2988 } }) catch unreachable;
3212 },2989 try stack.append(State{ .ExpectToken = Token.Id.LBrace });
3213 }) catch unreachable;2990 try stack.append(State{ .ExpectToken = Token.Id.RParen });
3214 try stack.append(State { .ExpectToken = Token.Id.LBrace });2991 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
3215 try stack.append(State { .ExpectToken = Token.Id.RParen });2992 try stack.append(State{ .ExpectToken = Token.Id.LParen });
3216 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });
3217 try stack.append(State { .ExpectToken = Token.Id.LParen });
3218 return true;2993 return true;
3219 },2994 },
3220 Token.Id.Keyword_comptime => {2995 Token.Id.Keyword_comptime => {
3221 const node = try arena.construct(ast.Node.Comptime {2996 const node = try arena.construct(ast.Node.Comptime{
3222 .base = ast.Node {.id = ast.Node.Id.Comptime },2997 .base = ast.Node{ .id = ast.Node.Id.Comptime },
3223 .comptime_token = token_index,2998 .comptime_token = token_index,
3224 .expr = undefined,2999 .expr = undefined,
3225 .doc_comments = null,3000 .doc_comments = null,
3226 });3001 });
3227 ctx.store(&node.base);3002 ctx.store(&node.base);
32283003
3229 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });3004 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
3230 return true;3005 return true;
3231 },3006 },
3232 Token.Id.LBrace => {3007 Token.Id.LBrace => {
3233 const block = try arena.construct(ast.Node.Block {3008 const block = try arena.construct(ast.Node.Block{
3234 .base = ast.Node {.id = ast.Node.Id.Block },3009 .base = ast.Node{ .id = ast.Node.Id.Block },
3235 .label = null,3010 .label = null,
3236 .lbrace = token_index,3011 .lbrace = token_index,
3237 .statements = ast.Node.Block.StatementList.init(arena),3012 .statements = ast.Node.Block.StatementList.init(arena),
3238 .rbrace = undefined,3013 .rbrace = undefined,
3239 });3014 });
3240 ctx.store(&block.base);3015 ctx.store(&block.base);
3241 stack.append(State { .Block = block }) catch unreachable;3016 stack.append(State{ .Block = block }) catch unreachable;
3242 return true;3017 return true;
3243 },3018 },
3244 else => {3019 else => {
3245 return false;3020 return false;
3246 }3021 },
3247 }3022 }
3248}3023}
32493024
...@@ -3257,20 +3032,16 @@ fn expectCommaOrEnd(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, end:...@@ -3257,20 +3032,16 @@ fn expectCommaOrEnd(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, end:
3257 const token_index = token.index;3032 const token_index = token.index;
3258 const token_ptr = token.ptr;3033 const token_ptr = token.ptr;
3259 switch (token_ptr.id) {3034 switch (token_ptr.id) {
3260 Token.Id.Comma => return ExpectCommaOrEndResult { .end_token = null},3035 Token.Id.Comma => return ExpectCommaOrEndResult{ .end_token = null },
3261 else => {3036 else => {
3262 if (end == token_ptr.id) {3037 if (end == token_ptr.id) {
3263 return ExpectCommaOrEndResult { .end_token = token_index };3038 return ExpectCommaOrEndResult{ .end_token = token_index };
3264 }3039 }
32653040
3266 return ExpectCommaOrEndResult {3041 return ExpectCommaOrEndResult{ .parse_error = Error{ .ExpectedCommaOrEnd = Error.ExpectedCommaOrEnd{
3267 .parse_error = Error {3042 .token = token_index,
3268 .ExpectedCommaOrEnd = Error.ExpectedCommaOrEnd {3043 .end_id = end,
3269 .token = token_index,3044 } } };
3270 .end_id = end,
3271 },
3272 },
3273 };
3274 },3045 },
3275 }3046 }
3276}3047}
...@@ -3278,103 +3049,102 @@ fn expectCommaOrEnd(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, end:...@@ -3278,103 +3049,102 @@ fn expectCommaOrEnd(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, end:
3278fn tokenIdToAssignment(id: &const Token.Id) ?ast.Node.InfixOp.Op {3049fn tokenIdToAssignment(id: &const Token.Id) ?ast.Node.InfixOp.Op {
3279 // TODO: We have to cast all cases because of this:3050 // TODO: We have to cast all cases because of this:
3280 // error: expected type '?InfixOp', found '?@TagType(InfixOp)'3051 // error: expected type '?InfixOp', found '?@TagType(InfixOp)'
3281 return switch (*id) {3052 return switch (id.*) {
3282 Token.Id.AmpersandEqual => ast.Node.InfixOp.Op { .AssignBitAnd = {} },3053 Token.Id.AmpersandEqual => ast.Node.InfixOp.Op{ .AssignBitAnd = {} },
3283 Token.Id.AngleBracketAngleBracketLeftEqual => ast.Node.InfixOp.Op { .AssignBitShiftLeft = {} },3054 Token.Id.AngleBracketAngleBracketLeftEqual => ast.Node.InfixOp.Op{ .AssignBitShiftLeft = {} },
3284 Token.Id.AngleBracketAngleBracketRightEqual => ast.Node.InfixOp.Op { .AssignBitShiftRight = {} },3055 Token.Id.AngleBracketAngleBracketRightEqual => ast.Node.InfixOp.Op{ .AssignBitShiftRight = {} },
3285 Token.Id.AsteriskEqual => ast.Node.InfixOp.Op { .AssignTimes = {} },3056 Token.Id.AsteriskEqual => ast.Node.InfixOp.Op{ .AssignTimes = {} },
3286 Token.Id.AsteriskPercentEqual => ast.Node.InfixOp.Op { .AssignTimesWarp = {} },3057 Token.Id.AsteriskPercentEqual => ast.Node.InfixOp.Op{ .AssignTimesWarp = {} },
3287 Token.Id.CaretEqual => ast.Node.InfixOp.Op { .AssignBitXor = {} },3058 Token.Id.CaretEqual => ast.Node.InfixOp.Op{ .AssignBitXor = {} },
3288 Token.Id.Equal => ast.Node.InfixOp.Op { .Assign = {} },3059 Token.Id.Equal => ast.Node.InfixOp.Op{ .Assign = {} },
3289 Token.Id.MinusEqual => ast.Node.InfixOp.Op { .AssignMinus = {} },3060 Token.Id.MinusEqual => ast.Node.InfixOp.Op{ .AssignMinus = {} },
3290 Token.Id.MinusPercentEqual => ast.Node.InfixOp.Op { .AssignMinusWrap = {} },3061 Token.Id.MinusPercentEqual => ast.Node.InfixOp.Op{ .AssignMinusWrap = {} },
3291 Token.Id.PercentEqual => ast.Node.InfixOp.Op { .AssignMod = {} },3062 Token.Id.PercentEqual => ast.Node.InfixOp.Op{ .AssignMod = {} },
3292 Token.Id.PipeEqual => ast.Node.InfixOp.Op { .AssignBitOr = {} },3063 Token.Id.PipeEqual => ast.Node.InfixOp.Op{ .AssignBitOr = {} },
3293 Token.Id.PlusEqual => ast.Node.InfixOp.Op { .AssignPlus = {} },3064 Token.Id.PlusEqual => ast.Node.InfixOp.Op{ .AssignPlus = {} },
3294 Token.Id.PlusPercentEqual => ast.Node.InfixOp.Op { .AssignPlusWrap = {} },3065 Token.Id.PlusPercentEqual => ast.Node.InfixOp.Op{ .AssignPlusWrap = {} },
3295 Token.Id.SlashEqual => ast.Node.InfixOp.Op { .AssignDiv = {} },3066 Token.Id.SlashEqual => ast.Node.InfixOp.Op{ .AssignDiv = {} },
3296 else => null,3067 else => null,
3297 };3068 };
3298}3069}
32993070
3300fn tokenIdToUnwrapExpr(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {3071fn tokenIdToUnwrapExpr(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {
3301 return switch (id) {3072 return switch (id) {
3302 Token.Id.Keyword_catch => ast.Node.InfixOp.Op { .Catch = null },3073 Token.Id.Keyword_catch => ast.Node.InfixOp.Op{ .Catch = null },
3303 Token.Id.QuestionMarkQuestionMark => ast.Node.InfixOp.Op { .UnwrapMaybe = void{} },3074 Token.Id.QuestionMarkQuestionMark => ast.Node.InfixOp.Op{ .UnwrapMaybe = void{} },
3304 else => null,3075 else => null,
3305 };3076 };
3306}3077}
33073078
3308fn tokenIdToComparison(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {3079fn tokenIdToComparison(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {
3309 return switch (id) {3080 return switch (id) {
3310 Token.Id.BangEqual => ast.Node.InfixOp.Op { .BangEqual = void{} },3081 Token.Id.BangEqual => ast.Node.InfixOp.Op{ .BangEqual = void{} },
3311 Token.Id.EqualEqual => ast.Node.InfixOp.Op { .EqualEqual = void{} },3082 Token.Id.EqualEqual => ast.Node.InfixOp.Op{ .EqualEqual = void{} },
3312 Token.Id.AngleBracketLeft => ast.Node.InfixOp.Op { .LessThan = void{} },3083 Token.Id.AngleBracketLeft => ast.Node.InfixOp.Op{ .LessThan = void{} },
3313 Token.Id.AngleBracketLeftEqual => ast.Node.InfixOp.Op { .LessOrEqual = void{} },3084 Token.Id.AngleBracketLeftEqual => ast.Node.InfixOp.Op{ .LessOrEqual = void{} },
3314 Token.Id.AngleBracketRight => ast.Node.InfixOp.Op { .GreaterThan = void{} },3085 Token.Id.AngleBracketRight => ast.Node.InfixOp.Op{ .GreaterThan = void{} },
3315 Token.Id.AngleBracketRightEqual => ast.Node.InfixOp.Op { .GreaterOrEqual = void{} },3086 Token.Id.AngleBracketRightEqual => ast.Node.InfixOp.Op{ .GreaterOrEqual = void{} },
3316 else => null,3087 else => null,
3317 };3088 };
3318}3089}
33193090
3320fn tokenIdToBitShift(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {3091fn tokenIdToBitShift(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {
3321 return switch (id) {3092 return switch (id) {
3322 Token.Id.AngleBracketAngleBracketLeft => ast.Node.InfixOp.Op { .BitShiftLeft = void{} },3093 Token.Id.AngleBracketAngleBracketLeft => ast.Node.InfixOp.Op{ .BitShiftLeft = void{} },
3323 Token.Id.AngleBracketAngleBracketRight => ast.Node.InfixOp.Op { .BitShiftRight = void{} },3094 Token.Id.AngleBracketAngleBracketRight => ast.Node.InfixOp.Op{ .BitShiftRight = void{} },
3324 else => null,3095 else => null,
3325 };3096 };
3326}3097}
33273098
3328fn tokenIdToAddition(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {3099fn tokenIdToAddition(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {
3329 return switch (id) {3100 return switch (id) {
3330 Token.Id.Minus => ast.Node.InfixOp.Op { .Sub = void{} },3101 Token.Id.Minus => ast.Node.InfixOp.Op{ .Sub = void{} },
3331 Token.Id.MinusPercent => ast.Node.InfixOp.Op { .SubWrap = void{} },3102 Token.Id.MinusPercent => ast.Node.InfixOp.Op{ .SubWrap = void{} },
3332 Token.Id.Plus => ast.Node.InfixOp.Op { .Add = void{} },3103 Token.Id.Plus => ast.Node.InfixOp.Op{ .Add = void{} },
3333 Token.Id.PlusPercent => ast.Node.InfixOp.Op { .AddWrap = void{} },3104 Token.Id.PlusPercent => ast.Node.InfixOp.Op{ .AddWrap = void{} },
3334 Token.Id.PlusPlus => ast.Node.InfixOp.Op { .ArrayCat = void{} },3105 Token.Id.PlusPlus => ast.Node.InfixOp.Op{ .ArrayCat = void{} },
3335 else => null,3106 else => null,
3336 };3107 };
3337}3108}
33383109
3339fn tokenIdToMultiply(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {3110fn tokenIdToMultiply(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {
3340 return switch (id) {3111 return switch (id) {
3341 Token.Id.Slash => ast.Node.InfixOp.Op { .Div = void{} },3112 Token.Id.Slash => ast.Node.InfixOp.Op{ .Div = void{} },
3342 Token.Id.Asterisk => ast.Node.InfixOp.Op { .Mult = void{} },3113 Token.Id.Asterisk => ast.Node.InfixOp.Op{ .Mult = void{} },
3343 Token.Id.AsteriskAsterisk => ast.Node.InfixOp.Op { .ArrayMult = void{} },3114 Token.Id.AsteriskAsterisk => ast.Node.InfixOp.Op{ .ArrayMult = void{} },
3344 Token.Id.AsteriskPercent => ast.Node.InfixOp.Op { .MultWrap = void{} },3115 Token.Id.AsteriskPercent => ast.Node.InfixOp.Op{ .MultWrap = void{} },
3345 Token.Id.Percent => ast.Node.InfixOp.Op { .Mod = void{} },3116 Token.Id.Percent => ast.Node.InfixOp.Op{ .Mod = void{} },
3346 Token.Id.PipePipe => ast.Node.InfixOp.Op { .MergeErrorSets = void{} },3117 Token.Id.PipePipe => ast.Node.InfixOp.Op{ .MergeErrorSets = void{} },
3347 else => null,3118 else => null,
3348 };3119 };
3349}3120}
33503121
3351fn tokenIdToPrefixOp(id: @TagType(Token.Id)) ?ast.Node.PrefixOp.Op {3122fn tokenIdToPrefixOp(id: @TagType(Token.Id)) ?ast.Node.PrefixOp.Op {
3352 return switch (id) {3123 return switch (id) {
3353 Token.Id.Bang => ast.Node.PrefixOp.Op { .BoolNot = void{} },3124 Token.Id.Bang => ast.Node.PrefixOp.Op{ .BoolNot = void{} },
3354 Token.Id.Tilde => ast.Node.PrefixOp.Op { .BitNot = void{} },3125 Token.Id.Tilde => ast.Node.PrefixOp.Op{ .BitNot = void{} },
3355 Token.Id.Minus => ast.Node.PrefixOp.Op { .Negation = void{} },3126 Token.Id.Minus => ast.Node.PrefixOp.Op{ .Negation = void{} },
3356 Token.Id.MinusPercent => ast.Node.PrefixOp.Op { .NegationWrap = void{} },3127 Token.Id.MinusPercent => ast.Node.PrefixOp.Op{ .NegationWrap = void{} },
3357 Token.Id.Asterisk, Token.Id.AsteriskAsterisk => ast.Node.PrefixOp.Op { .Deref = void{} },3128 Token.Id.Asterisk,
3358 Token.Id.Ampersand => ast.Node.PrefixOp.Op {3129 Token.Id.AsteriskAsterisk => ast.Node.PrefixOp.Op{ .Deref = void{} },
3359 .AddrOf = ast.Node.PrefixOp.AddrOfInfo {3130 Token.Id.Ampersand => ast.Node.PrefixOp.Op{ .AddrOf = ast.Node.PrefixOp.AddrOfInfo{
3360 .align_expr = null,3131 .align_expr = null,
3361 .bit_offset_start_token = null,3132 .bit_offset_start_token = null,
3362 .bit_offset_end_token = null,3133 .bit_offset_end_token = null,
3363 .const_token = null,3134 .const_token = null,
3364 .volatile_token = null,3135 .volatile_token = null,
3365 },3136 } },
3366 },3137 Token.Id.QuestionMark => ast.Node.PrefixOp.Op{ .MaybeType = void{} },
3367 Token.Id.QuestionMark => ast.Node.PrefixOp.Op { .MaybeType = void{} },3138 Token.Id.QuestionMarkQuestionMark => ast.Node.PrefixOp.Op{ .UnwrapMaybe = void{} },
3368 Token.Id.QuestionMarkQuestionMark => ast.Node.PrefixOp.Op { .UnwrapMaybe = void{} },3139 Token.Id.Keyword_await => ast.Node.PrefixOp.Op{ .Await = void{} },
3369 Token.Id.Keyword_await => ast.Node.PrefixOp.Op { .Await = void{} },3140 Token.Id.Keyword_try => ast.Node.PrefixOp.Op{ .Try = void{} },
3370 Token.Id.Keyword_try => ast.Node.PrefixOp.Op { .Try = void{ } },
3371 else => null,3141 else => null,
3372 };3142 };
3373}3143}
33743144
3375fn createLiteral(arena: &mem.Allocator, comptime T: type, token_index: TokenIndex) !&T {3145fn createLiteral(arena: &mem.Allocator, comptime T: type, token_index: TokenIndex) !&T {
3376 return arena.construct(T {3146 return arena.construct(T{
3377 .base = ast.Node {.id = ast.Node.typeToId(T)},3147 .base = ast.Node{ .id = ast.Node.typeToId(T) },
3378 .token = token_index,3148 .token = token_index,
3379 });3149 });
3380}3150}
...@@ -3389,15 +3159,14 @@ fn createToCtxLiteral(arena: &mem.Allocator, opt_ctx: &const OptionalCtx, compti...@@ -3389,15 +3159,14 @@ fn createToCtxLiteral(arena: &mem.Allocator, opt_ctx: &const OptionalCtx, compti
3389fn eatToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, id: @TagType(Token.Id)) ?TokenIndex {3159fn eatToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, id: @TagType(Token.Id)) ?TokenIndex {
3390 const token = nextToken(tok_it, tree);3160 const token = nextToken(tok_it, tree);
33913161
3392 if (token.ptr.id == id)3162 if (token.ptr.id == id) return token.index;
3393 return token.index;
33943163
3395 putBackToken(tok_it, tree);3164 putBackToken(tok_it, tree);
3396 return null;3165 return null;
3397}3166}
33983167
3399fn nextToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) AnnotatedToken {3168fn nextToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) AnnotatedToken {
3400 const result = AnnotatedToken {3169 const result = AnnotatedToken{
3401 .index = tok_it.index,3170 .index = tok_it.index,
3402 .ptr = ??tok_it.next(),3171 .ptr = ??tok_it.next(),
3403 };3172 };
std/zig/render.zig+44-44
...@@ -7,7 +7,7 @@ const Token = std.zig.Token;...@@ -7,7 +7,7 @@ const Token = std.zig.Token;
77
8const indent_delta = 4;8const indent_delta = 4;
99
10pub const Error = error {10pub const Error = error{
11 /// Ran out of memory allocating call stack frames to complete rendering.11 /// Ran out of memory allocating call stack frames to complete rendering.
12 OutOfMemory,12 OutOfMemory,
13};13};
...@@ -17,9 +17,9 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(...@@ -17,9 +17,9 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(
1717
18 var it = tree.root_node.decls.iterator(0);18 var it = tree.root_node.decls.iterator(0);
19 while (it.next()) |decl| {19 while (it.next()) |decl| {
20 try renderTopLevelDecl(allocator, stream, tree, 0, *decl);20 try renderTopLevelDecl(allocator, stream, tree, 0, decl.*);
21 if (it.peek()) |next_decl| {21 if (it.peek()) |next_decl| {
22 const n = if (nodeLineOffset(tree, *decl, *next_decl) >= 2) u8(2) else u8(1);22 const n = if (nodeLineOffset(tree, decl.*, next_decl.*) >= 2) u8(2) else u8(1);
23 try stream.writeByteNTimes('\n', n);23 try stream.writeByteNTimes('\n', n);
24 }24 }
25 }25 }
...@@ -154,10 +154,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -154,10 +154,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
154 var it = block.statements.iterator(0);154 var it = block.statements.iterator(0);
155 while (it.next()) |statement| {155 while (it.next()) |statement| {
156 try stream.writeByteNTimes(' ', block_indent);156 try stream.writeByteNTimes(' ', block_indent);
157 try renderStatement(allocator, stream, tree, block_indent, *statement);157 try renderStatement(allocator, stream, tree, block_indent, statement.*);
158158
159 if (it.peek()) |next_statement| {159 if (it.peek()) |next_statement| {
160 const n = if (nodeLineOffset(tree, *statement, *next_statement) >= 2) u8(2) else u8(1);160 const n = if (nodeLineOffset(tree, statement.*, next_statement.*) >= 2) u8(2) else u8(1);
161 try stream.writeByteNTimes('\n', n);161 try stream.writeByteNTimes('\n', n);
162 }162 }
163 }163 }
...@@ -203,7 +203,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -203,7 +203,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
203 try stream.write(" ");203 try stream.write(" ");
204 try renderExpression(allocator, stream, tree, indent, body);204 try renderExpression(allocator, stream, tree, indent, body);
205 }205 }
206
207 },206 },
208207
209 ast.Node.Id.InfixOp => {208 ast.Node.Id.InfixOp => {
...@@ -335,7 +334,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -335,7 +334,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
335334
336 var it = call_info.params.iterator(0);335 var it = call_info.params.iterator(0);
337 while (it.next()) |param_node| {336 while (it.next()) |param_node| {
338 try renderExpression(allocator, stream, tree, indent, *param_node);337 try renderExpression(allocator, stream, tree, indent, param_node.*);
339 if (it.peek() != null) {338 if (it.peek() != null) {
340 try stream.write(", ");339 try stream.write(", ");
341 }340 }
...@@ -351,7 +350,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -351,7 +350,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
351 try stream.write("]");350 try stream.write("]");
352 },351 },
353352
354 ast.Node.SuffixOp.Op.SuffixOp {353 ast.Node.SuffixOp.Op.SuffixOp => {
355 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);354 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
356 try stream.write(".*");355 try stream.write(".*");
357 },356 },
...@@ -375,7 +374,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -375,7 +374,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
375 }374 }
376375
377 if (field_inits.len == 1) {376 if (field_inits.len == 1) {
378 const field_init = *field_inits.at(0);377 const field_init = field_inits.at(0).*;
379378
380 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);379 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
381 try stream.write("{ ");380 try stream.write("{ ");
...@@ -392,12 +391,12 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -392,12 +391,12 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
392 var it = field_inits.iterator(0);391 var it = field_inits.iterator(0);
393 while (it.next()) |field_init| {392 while (it.next()) |field_init| {
394 try stream.writeByteNTimes(' ', new_indent);393 try stream.writeByteNTimes(' ', new_indent);
395 try renderExpression(allocator, stream, tree, new_indent, *field_init);394 try renderExpression(allocator, stream, tree, new_indent, field_init.*);
396 if ((*field_init).id != ast.Node.Id.LineComment) {395 if ((field_init.*).id != ast.Node.Id.LineComment) {
397 try stream.write(",");396 try stream.write(",");
398 }397 }
399 if (it.peek()) |next_field_init| {398 if (it.peek()) |next_field_init| {
400 const n = if (nodeLineOffset(tree, *field_init, *next_field_init) >= 2) u8(2) else u8(1);399 const n = if (nodeLineOffset(tree, field_init.*, next_field_init.*) >= 2) u8(2) else u8(1);
401 try stream.writeByteNTimes('\n', n);400 try stream.writeByteNTimes('\n', n);
402 }401 }
403 }402 }
...@@ -408,14 +407,13 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -408,14 +407,13 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
408 },407 },
409408
410 ast.Node.SuffixOp.Op.ArrayInitializer => |*exprs| {409 ast.Node.SuffixOp.Op.ArrayInitializer => |*exprs| {
411
412 if (exprs.len == 0) {410 if (exprs.len == 0) {
413 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);411 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
414 try stream.write("{}");412 try stream.write("{}");
415 return;413 return;
416 }414 }
417 if (exprs.len == 1) {415 if (exprs.len == 1) {
418 const expr = *exprs.at(0);416 const expr = exprs.at(0).*;
419417
420 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);418 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
421 try stream.write("{");419 try stream.write("{");
...@@ -432,11 +430,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -432,11 +430,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
432 var it = exprs.iterator(0);430 var it = exprs.iterator(0);
433 while (it.next()) |expr| {431 while (it.next()) |expr| {
434 try stream.writeByteNTimes(' ', new_indent);432 try stream.writeByteNTimes(' ', new_indent);
435 try renderExpression(allocator, stream, tree, new_indent, *expr);433 try renderExpression(allocator, stream, tree, new_indent, expr.*);
436 try stream.write(",");434 try stream.write(",");
437435
438 if (it.peek()) |next_expr| {436 if (it.peek()) |next_expr| {
439 const n = if (nodeLineOffset(tree, *expr, *next_expr) >= 2) u8(2) else u8(1);437 const n = if (nodeLineOffset(tree, expr.*, next_expr.*) >= 2) u8(2) else u8(1);
440 try stream.writeByteNTimes('\n', n);438 try stream.writeByteNTimes('\n', n);
441 }439 }
442 }440 }
...@@ -469,7 +467,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -469,7 +467,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
469 ast.Node.ControlFlowExpression.Kind.Return => {467 ast.Node.ControlFlowExpression.Kind.Return => {
470 try stream.print("return");468 try stream.print("return");
471 },469 },
472
473 }470 }
474471
475 if (flow_expr.rhs) |rhs| {472 if (flow_expr.rhs) |rhs| {
...@@ -575,7 +572,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -575,7 +572,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
575 switch (container_decl.layout) {572 switch (container_decl.layout) {
576 ast.Node.ContainerDecl.Layout.Packed => try stream.print("packed "),573 ast.Node.ContainerDecl.Layout.Packed => try stream.print("packed "),
577 ast.Node.ContainerDecl.Layout.Extern => try stream.print("extern "),574 ast.Node.ContainerDecl.Layout.Extern => try stream.print("extern "),
578 ast.Node.ContainerDecl.Layout.Auto => { },575 ast.Node.ContainerDecl.Layout.Auto => {},
579 }576 }
580577
581 switch (container_decl.kind) {578 switch (container_decl.kind) {
...@@ -611,10 +608,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -611,10 +608,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
611 var it = container_decl.fields_and_decls.iterator(0);608 var it = container_decl.fields_and_decls.iterator(0);
612 while (it.next()) |decl| {609 while (it.next()) |decl| {
613 try stream.writeByteNTimes(' ', new_indent);610 try stream.writeByteNTimes(' ', new_indent);
614 try renderTopLevelDecl(allocator, stream, tree, new_indent, *decl);611 try renderTopLevelDecl(allocator, stream, tree, new_indent, decl.*);
615612
616 if (it.peek()) |next_decl| {613 if (it.peek()) |next_decl| {
617 const n = if (nodeLineOffset(tree, *decl, *next_decl) >= 2) u8(2) else u8(1);614 const n = if (nodeLineOffset(tree, decl.*, next_decl.*) >= 2) u8(2) else u8(1);
618 try stream.writeByteNTimes('\n', n);615 try stream.writeByteNTimes('\n', n);
619 }616 }
620 }617 }
...@@ -634,7 +631,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -634,7 +631,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
634 }631 }
635632
636 if (err_set_decl.decls.len == 1) blk: {633 if (err_set_decl.decls.len == 1) blk: {
637 const node = *err_set_decl.decls.at(0);634 const node = err_set_decl.decls.at(0).*;
638635
639 // if there are any doc comments or same line comments636 // if there are any doc comments or same line comments
640 // don't try to put it all on one line637 // don't try to put it all on one line
...@@ -644,7 +641,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -644,7 +641,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
644 break :blk;641 break :blk;
645 }642 }
646643
647
648 try stream.write("error{");644 try stream.write("error{");
649 try renderTopLevelDecl(allocator, stream, tree, indent, node);645 try renderTopLevelDecl(allocator, stream, tree, indent, node);
650 try stream.write("}");646 try stream.write("}");
...@@ -657,12 +653,12 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -657,12 +653,12 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
657 var it = err_set_decl.decls.iterator(0);653 var it = err_set_decl.decls.iterator(0);
658 while (it.next()) |node| {654 while (it.next()) |node| {
659 try stream.writeByteNTimes(' ', new_indent);655 try stream.writeByteNTimes(' ', new_indent);
660 try renderTopLevelDecl(allocator, stream, tree, new_indent, *node);656 try renderTopLevelDecl(allocator, stream, tree, new_indent, node.*);
661 if ((*node).id != ast.Node.Id.LineComment) {657 if ((node.*).id != ast.Node.Id.LineComment) {
662 try stream.write(",");658 try stream.write(",");
663 }659 }
664 if (it.peek()) |next_node| {660 if (it.peek()) |next_node| {
665 const n = if (nodeLineOffset(tree, *node, *next_node) >= 2) u8(2) else u8(1);661 const n = if (nodeLineOffset(tree, node.*, next_node.*) >= 2) u8(2) else u8(1);
666 try stream.writeByteNTimes('\n', n);662 try stream.writeByteNTimes('\n', n);
667 }663 }
668 }664 }
...@@ -676,9 +672,9 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -676,9 +672,9 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
676 const multiline_str_literal = @fieldParentPtr(ast.Node.MultilineStringLiteral, "base", base);672 const multiline_str_literal = @fieldParentPtr(ast.Node.MultilineStringLiteral, "base", base);
677 try stream.print("\n");673 try stream.print("\n");
678674
679 var i : usize = 0;675 var i: usize = 0;
680 while (i < multiline_str_literal.lines.len) : (i += 1) {676 while (i < multiline_str_literal.lines.len) : (i += 1) {
681 const t = *multiline_str_literal.lines.at(i);677 const t = multiline_str_literal.lines.at(i).*;
682 try stream.writeByteNTimes(' ', indent + indent_delta);678 try stream.writeByteNTimes(' ', indent + indent_delta);
683 try stream.print("{}", tree.tokenSlice(t));679 try stream.print("{}", tree.tokenSlice(t));
684 }680 }
...@@ -695,7 +691,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -695,7 +691,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
695691
696 var it = builtin_call.params.iterator(0);692 var it = builtin_call.params.iterator(0);
697 while (it.next()) |param_node| {693 while (it.next()) |param_node| {
698 try renderExpression(allocator, stream, tree, indent, *param_node);694 try renderExpression(allocator, stream, tree, indent, param_node.*);
699 if (it.peek() != null) {695 if (it.peek() != null) {
700 try stream.write(", ");696 try stream.write(", ");
701 }697 }
...@@ -740,7 +736,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -740,7 +736,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
740736
741 var it = fn_proto.params.iterator(0);737 var it = fn_proto.params.iterator(0);
742 while (it.next()) |param_decl_node| {738 while (it.next()) |param_decl_node| {
743 try renderParamDecl(allocator, stream, tree, indent, *param_decl_node);739 try renderParamDecl(allocator, stream, tree, indent, param_decl_node.*);
744740
745 if (it.peek() != null) {741 if (it.peek() != null) {
746 try stream.write(", ");742 try stream.write(", ");
...@@ -764,7 +760,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -764,7 +760,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
764 try renderExpression(allocator, stream, tree, indent, node);760 try renderExpression(allocator, stream, tree, indent, node);
765 },761 },
766 }762 }
767
768 },763 },
769764
770 ast.Node.Id.PromiseType => {765 ast.Node.Id.PromiseType => {
...@@ -801,10 +796,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -801,10 +796,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
801 var it = switch_node.cases.iterator(0);796 var it = switch_node.cases.iterator(0);
802 while (it.next()) |node| {797 while (it.next()) |node| {
803 try stream.writeByteNTimes(' ', new_indent);798 try stream.writeByteNTimes(' ', new_indent);
804 try renderExpression(allocator, stream, tree, new_indent, *node);799 try renderExpression(allocator, stream, tree, new_indent, node.*);
805800
806 if (it.peek()) |next_node| {801 if (it.peek()) |next_node| {
807 const n = if (nodeLineOffset(tree, *node, *next_node) >= 2) u8(2) else u8(1);802 const n = if (nodeLineOffset(tree, node.*, next_node.*) >= 2) u8(2) else u8(1);
808 try stream.writeByteNTimes('\n', n);803 try stream.writeByteNTimes('\n', n);
809 }804 }
810 }805 }
...@@ -819,7 +814,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -819,7 +814,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
819814
820 var it = switch_case.items.iterator(0);815 var it = switch_case.items.iterator(0);
821 while (it.next()) |node| {816 while (it.next()) |node| {
822 try renderExpression(allocator, stream, tree, indent, *node);817 try renderExpression(allocator, stream, tree, indent, node.*);
823818
824 if (it.peek() != null) {819 if (it.peek() != null) {
825 try stream.write(",\n");820 try stream.write(",\n");
...@@ -846,8 +841,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -846,8 +841,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
846 try stream.print("{}", tree.tokenSlice(else_node.else_token));841 try stream.print("{}", tree.tokenSlice(else_node.else_token));
847842
848 const block_body = switch (else_node.body.id) {843 const block_body = switch (else_node.body.id) {
849 ast.Node.Id.Block, ast.Node.Id.If,844 ast.Node.Id.Block,
850 ast.Node.Id.For, ast.Node.Id.While,845 ast.Node.Id.If,
846 ast.Node.Id.For,
847 ast.Node.Id.While,
851 ast.Node.Id.Switch => true,848 ast.Node.Id.Switch => true,
852 else => false,849 else => false,
853 };850 };
...@@ -972,7 +969,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -972,7 +969,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
972 try renderExpression(allocator, stream, tree, indent, if_node.body);969 try renderExpression(allocator, stream, tree, indent, if_node.body);
973970
974 switch (if_node.body.id) {971 switch (if_node.body.id) {
975 ast.Node.Id.Block, ast.Node.Id.If, ast.Node.Id.For, ast.Node.Id.While, ast.Node.Id.Switch => {972 ast.Node.Id.Block,
973 ast.Node.Id.If,
974 ast.Node.Id.For,
975 ast.Node.Id.While,
976 ast.Node.Id.Switch => {
976 if (if_node.@"else") |@"else"| {977 if (if_node.@"else") |@"else"| {
977 if (if_node.body.id == ast.Node.Id.Block) {978 if (if_node.body.id == ast.Node.Id.Block) {
978 try stream.write(" ");979 try stream.write(" ");
...@@ -995,7 +996,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -995,7 +996,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
995996
996 try renderExpression(allocator, stream, tree, indent, @"else".body);997 try renderExpression(allocator, stream, tree, indent, @"else".body);
997 }998 }
998 }999 },
999 }1000 }
1000 },1001 },
10011002
...@@ -1018,11 +1019,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -1018,11 +1019,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
1018 {1019 {
1019 var it = asm_node.outputs.iterator(0);1020 var it = asm_node.outputs.iterator(0);
1020 while (it.next()) |asm_output| {1021 while (it.next()) |asm_output| {
1021 const node = &(*asm_output).base;1022 const node = &(asm_output.*).base;
1022 try renderExpression(allocator, stream, tree, indent_extra, node);1023 try renderExpression(allocator, stream, tree, indent_extra, node);
10231024
1024 if (it.peek()) |next_asm_output| {1025 if (it.peek()) |next_asm_output| {
1025 const next_node = &(*next_asm_output).base;1026 const next_node = &(next_asm_output.*).base;
1026 const n = if (nodeLineOffset(tree, node, next_node) >= 2) u8(2) else u8(1);1027 const n = if (nodeLineOffset(tree, node, next_node) >= 2) u8(2) else u8(1);
1027 try stream.writeByte(',');1028 try stream.writeByte(',');
1028 try stream.writeByteNTimes('\n', n);1029 try stream.writeByteNTimes('\n', n);
...@@ -1038,11 +1039,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -1038,11 +1039,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
1038 {1039 {
1039 var it = asm_node.inputs.iterator(0);1040 var it = asm_node.inputs.iterator(0);
1040 while (it.next()) |asm_input| {1041 while (it.next()) |asm_input| {
1041 const node = &(*asm_input).base;1042 const node = &(asm_input.*).base;
1042 try renderExpression(allocator, stream, tree, indent_extra, node);1043 try renderExpression(allocator, stream, tree, indent_extra, node);
10431044
1044 if (it.peek()) |next_asm_input| {1045 if (it.peek()) |next_asm_input| {
1045 const next_node = &(*next_asm_input).base;1046 const next_node = &(next_asm_input.*).base;
1046 const n = if (nodeLineOffset(tree, node, next_node) >= 2) u8(2) else u8(1);1047 const n = if (nodeLineOffset(tree, node, next_node) >= 2) u8(2) else u8(1);
1047 try stream.writeByte(',');1048 try stream.writeByte(',');
1048 try stream.writeByteNTimes('\n', n);1049 try stream.writeByteNTimes('\n', n);
...@@ -1058,7 +1059,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -1058,7 +1059,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
1058 {1059 {
1059 var it = asm_node.clobbers.iterator(0);1060 var it = asm_node.clobbers.iterator(0);
1060 while (it.next()) |node| {1061 while (it.next()) |node| {
1061 try renderExpression(allocator, stream, tree, indent_once, *node);1062 try renderExpression(allocator, stream, tree, indent_once, node.*);
10621063
1063 if (it.peek() != null) {1064 if (it.peek() != null) {
1064 try stream.write(", ");1065 try stream.write(", ");
...@@ -1220,8 +1221,7 @@ fn renderComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@type...@@ -1220,8 +1221,7 @@ fn renderComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@type
1220 const comment = node.doc_comments ?? return;1221 const comment = node.doc_comments ?? return;
1221 var it = comment.lines.iterator(0);1222 var it = comment.lines.iterator(0);
1222 while (it.next()) |line_token_index| {1223 while (it.next()) |line_token_index| {
1223 try stream.print("{}\n", tree.tokenSlice(*line_token_index));1224 try stream.print("{}\n", tree.tokenSlice(line_token_index.*));
1224 try stream.writeByteNTimes(' ', indent);1225 try stream.writeByteNTimes(' ', indent);
1225 }1226 }
1226}1227}
1227