interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / osh / osh_lex_ref.c

443 lines · 19307 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * osh_lex_ref.c -- independent reference tokenizer (see osh_lex_ref.h). Direct scanning with lookahead,
19 * deliberately not a state machine. Rules, all from the spec and from bash:
20 * - a word is emitted when the byte after it is seen; an operator when its last byte is known (a one-byte
21 * operator needs the next byte); a buffer ending earlier is incomplete (status 100)
22 * - status 0 only when the last thing consumed was a newline token at the end of the buffer
23 * - refusals are decided at the first byte that decides them (offsets per ABI section 5.4)
24 * - reading a byte at offset >= 4096 is CAP_LINE at offset 4096; a 129th token is CAP_TOKENS at its start
25 * - bash removes every backslash-newline pair before it classifies the byte after it, except inside single
26 * quotes and comments (and a backslash that is itself escaped is not the start of a pair). Here that is
27 * skipc(): every place that looks at the "next byte" of a word, operator, `$` or `${` construct first steps
28 * over such pairs. A final lone backslash with no byte after it is incomplete, unless the end-of-input flag
29 * is set, when it is a literal backslash. Offsets always point into the original buffer; a token's span is
30 * the raw source from its first to its last byte.
31 */
32#include "osh_lex_ref.h"
33
34#include <string.h>
35
36enum { K_WORD = 1, K_PIPE, K_OR, K_AND, K_SEMI, K_LT, K_GT, K_APPEND, K_NL, K_DUP_OUT, K_DUP_IN };
37enum { E_CAP_LINE = 201, E_CAP_TOKENS = 202, E_NUL = 220, E_BACKTICK, E_GLOB, E_TILDE, E_PARAM_OP, E_SPECIAL_PARAM,
38 E_CMDSUB, E_BACKGROUND, E_SUBSHELL, E_HEREDOC, E_CASEEND, E_REDIR_OTHER, E_FD_RANGE, E_GROUP = 236, E_BRACE = 246,
39 E_POSITIONAL = 247, E_SYNTAX_EOF = 248, E_APPEND_ASSIGN = 249, E_FD_VARIABLE = 250 };
40#define INCOMPLETE (-1)
41#define CAPLINE (-2)
42
43typedef struct {
44 const uint8_t *in;
45 size_t n;
46 OshRefLex *o;
47 uint64_t line; /* 1-based current line */
48 int last_was_nl; /* the last thing consumed was a newline token */
49 int eoi; /* end-of-input flag */
50} Ref;
51
52/* byte at j, or INCOMPLETE past the end of the buffer, or CAPLINE for offset >= 4096 */
53static int peek(const Ref *r, size_t j)
54{
55 if (j >= OSH_REF_LINE_CAP && r->n > OSH_REF_LINE_CAP) return CAPLINE;
56 if (j >= r->n) return INCOMPLETE;
57 return r->in[j];
58}
59
60static void refuse(Ref *r, unsigned code, uint64_t off)
61{
62 r->o->status = code;
63 r->o->err_off = off;
64}
65
66/* a negative peek result becomes the outcome */
67static int stop(Ref *r, int pk)
68{
69 if (pk == INCOMPLETE) { if (r->eoi) refuse(r, E_SYNTAX_EOF, r->n); else r->o->status = 100; }
70 else refuse(r, E_CAP_LINE, OSH_REF_LINE_CAP);
71 return 1;
72}
73
74/* same as stop, for the functions that return 0 on an outcome */
75static int stopz(Ref *r, int pk) { stop(r, pk); return 0; }
76
77static int is_alpha_(int c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_'; }
78static int is_digit(int c) { return c >= '0' && c <= '9'; }
79static int is_glob(int c) { return c == '*' || c == '?' || c == '['; }
80static int is_opch(int c) { return c == '|' || c == '&' || c == ';' || c == '<' || c == '>' || c == '(' || c == ')'; }
81
82/* Step *j over backslash-newline pairs, counting them in *np. Returns INCOMPLETE when a final lone backslash
83 * must wait for the next byte (no end-of-input flag), else 0. The lookahead reads the raw buffer: whether a
84 * pair starts at the final byte below the line cap is not a cap question; the cap is reported by peek(). */
85static int skipc(const Ref *r, size_t *j, uint64_t *np)
86{
87 while (*j < r->n && *j < OSH_REF_LINE_CAP && r->in[*j] == '\\') {
88 if (*j + 1 >= r->n) return r->eoi ? 0 : INCOMPLETE;
89 if (r->in[*j + 1] != '\n') break;
90 *j += 2;
91 (*np)++;
92 }
93 return 0;
94}
95
96/* skipc inside a word: the pairs set flag 32 and count lines */
97static int sk(Ref *r, size_t *j, uint64_t *fl, uint64_t *line)
98{
99 uint64_t np = 0;
100 int rc = skipc(r, j, &np);
101 if (np) { *fl |= 32; *line += np; }
102 return rc;
103}
104
105/* offset of the last byte before j that is not part of a backslash-newline pair (j > 0) */
106static size_t prev_sig(const Ref *r, size_t j)
107{
108 while (j >= 3 && r->in[j - 1] == '\n' && r->in[j - 2] == '\\') j -= 2;
109 return j - 1;
110}
111
112/* append a token; 1 and CAP_TOKENS if full */
113static int add(Ref *r, uint64_t kind, uint64_t flags, uint64_t nseg, uint64_t line, uint64_t start, uint64_t len, uint64_t aux)
114{
115 if (r->o->ntok >= OSH_REF_TOKEN_CAP) { refuse(r, E_CAP_TOKENS, start); return 1; }
116 OshRefTok *t = &r->o->tok[r->o->ntok++];
117 t->kind = kind; t->flags = flags; t->nseg = nseg; t->line = line; t->start = start; t->len = len; t->aux = aux;
118 return 0;
119}
120
121/* parameter expansion after a `$` at j (inside double quotes when dq); returns the offset after the
122 * construct, or 0 with the outcome set. A `$` with nothing special after it is literal: returns j + 1 and the
123 * caller sees the next byte itself. Pairs stepped over inside a consumed construct set *fl and *line. */
124static size_t dollar(Ref *r, size_t j, int dq, uint64_t *fl, uint64_t *line)
125{
126 uint64_t f2 = 0, l2 = 0;
127 size_t q = j + 1;
128 int rc = sk(r, &q, &f2, &l2);
129 if (rc < 0) { stop(r, rc); return 0; }
130 int x = peek(r, q);
131 if (x < 0) {
132 if (x == INCOMPLETE && r->eoi) return j + 1; /* a literal `$` at the end of the input */
133 stop(r, x);
134 return 0;
135 }
136 if (x == 0) { refuse(r, E_NUL, q); return 0; }
137 if (is_alpha_(x) || is_digit(x) || x == '?' || x == '#' || x == '@' || x == '*') {
138 if (x == '_') {
139 /* `$_` is the "last argument" special parameter unless the name goes on (`$_x`) */
140 uint64_t f3 = 0, l3 = 0;
141 size_t q2 = q + 1;
142 rc = sk(r, &q2, &f3, &l3);
143 if (rc < 0) { stop(r, rc); return 0; }
144 int nb = peek(r, q2);
145 if (nb == INCOMPLETE && r->eoi) { refuse(r, E_SPECIAL_PARAM, q); return 0; } /* `$_` ends the input */
146 if (nb < 0) { stop(r, nb); return 0; }
147 if (nb != 0 && !is_alpha_(nb) && !is_digit(nb)) { refuse(r, E_SPECIAL_PARAM, q); return 0; }
148 }
149 *fl |= f2; *line += l2;
150 return q + 1;
151 }
152 if (x == '(') { refuse(r, E_CMDSUB, q); return 0; }
153 if (x == '$' || x == '!' || x == '-') { refuse(r, E_SPECIAL_PARAM, q); return 0; }
154 if (!dq && (x == '\'' || x == '"')) { refuse(r, E_SPECIAL_PARAM, q); return 0; }
155 if (x != '{') return j + 1; /* a literal `$` */
156 size_t k = q + 1;
157 int numeric = 0; /* the name began with a digit: it must be that one digit ($0..$9 only) */
158 int named = 0, us = 0;
159 size_t us_off = 0;
160 for (;;) {
161 rc = sk(r, &k, &f2, &l2);
162 if (rc < 0) { stop(r, rc); return 0; }
163 int b = peek(r, k);
164 if (b < 0) { stop(r, b); return 0; }
165 if (b == 0) { refuse(r, E_NUL, k); return 0; }
166 if (is_alpha_(b) || is_digit(b)) {
167 if (!named) { numeric = is_digit(b); us = b == '_'; us_off = k; }
168 else if (numeric) { refuse(r, is_digit(b) ? E_POSITIONAL : E_PARAM_OP, k); return 0; }
169 else us = 0;
170 named = 1;
171 k++;
172 continue;
173 }
174 if (b == '}' && named) {
175 if (us) { refuse(r, E_SPECIAL_PARAM, us_off); return 0; } /* `${_}` */
176 *fl |= f2; *line += l2;
177 return k + 1;
178 }
179 refuse(r, E_PARAM_OP, k);
180 return 0;
181 }
182}
183
184/* Scan the word that starts at b. On a normal end returns 1 with *end set; else 0 (outcome set). */
185static int word(Ref *r, size_t b, size_t *end, uint64_t *flags, uint64_t *nseg, uint64_t *aux, uint64_t *line_out)
186{
187 uint64_t fl = 0, ns = 0, line = r->line;
188 size_t eq = 0; /* offset of the `=` of a leading NAME=, 0 if none */
189 size_t app = 0; /* offset of the `=` of a leading NAME+= (append assignment), 0 if none */
190 {
191 size_t j = b;
192 int first = 1;
193 for (;;) {
194 while (j + 1 < r->n && r->in[j] == '\\' && r->in[j + 1] == '\n') j += 2;
195 if (j >= r->n) break;
196 int c = r->in[j];
197 if (c == '=' && !first) { eq = j; break; }
198 if (c == '+' && !first) {
199 size_t q = j + 1;
200 while (q + 1 < r->n && r->in[q] == '\\' && r->in[q + 1] == '\n') q += 2;
201 if (q < r->n && r->in[q] == '=') app = q;
202 break;
203 }
204 if (first ? is_alpha_(c) : (is_alpha_(c) || is_digit(c))) { first = 0; j++; continue; }
205 break;
206 }
207 }
208 size_t j = b;
209 /* unquoted brace expansion detector: depth, a bit per open depth for "has an unquoted , or .. directly inside" */
210 unsigned bd = 0;
211 uint64_t bm = 0;
212 int pdot = 0;
213 for (;;) {
214 int rc = sk(r, &j, &fl, &line);
215 int pd = pdot;
216 pdot = 0;
217 if (rc < 0) return stopz(r, rc);
218 int c = peek(r, j);
219 if (c < 0) {
220 if (c == INCOMPLETE && r->eoi) break; /* the input ends the word */
221 return stopz(r, c);
222 }
223 if (c == 0) { refuse(r, E_NUL, j); return 0; }
224 if (c == ' ' || c == '\t' || c == '\n' || is_opch(c)) break;
225 if (app && j == app) { refuse(r, E_APPEND_ASSIGN, j); return 0; }
226 if (c == '\'') {
227 ns++; fl |= 1;
228 size_t k = j + 1;
229 for (;;) {
230 int d = peek(r, k);
231 if (d < 0) return stopz(r, d);
232 if (d == 0) { refuse(r, E_NUL, k); return 0; }
233 if (d == '\n') line++;
234 k++;
235 if (d == '\'') break;
236 }
237 j = k;
238 } else if (c == '"') {
239 ns++; fl |= 2;
240 size_t k = j + 1;
241 for (;;) {
242 rc = sk(r, &k, &fl, &line);
243 if (rc < 0) return stopz(r, rc);
244 int d = peek(r, k);
245 if (d < 0) return stopz(r, d);
246 if (d == 0) { refuse(r, E_NUL, k); return 0; }
247 if (d == '"') { k++; break; }
248 if (d == '`') { refuse(r, E_BACKTICK, k); return 0; }
249 if (d == '\\') {
250 int e = peek(r, k + 1);
251 if (e == INCOMPLETE && r->eoi) { k++; continue; } /* a final lone backslash is literal */
252 if (e < 0) return stopz(r, e);
253 if (e == 0) { refuse(r, E_NUL, k + 1); return 0; }
254 fl |= 4;
255 k += 2;
256 } else if (d == '$') {
257 fl |= 8;
258 size_t nk = dollar(r, k, 1, &fl, &line);
259 if (!nk) return 0;
260 k = nk;
261 } else {
262 if (d == '\n') line++;
263 k++;
264 }
265 }
266 j = k;
267 } else if (c == '\\') {
268 int e = peek(r, j + 1);
269 if (e == INCOMPLETE && r->eoi) { j++; continue; } /* a final lone backslash is literal */
270 if (e < 0) return stopz(r, e);
271 if (e == 0) { refuse(r, E_NUL, j + 1); return 0; }
272 fl |= 4;
273 j += 2;
274 } else if (c == '$') {
275 fl |= 8;
276 size_t nk = dollar(r, j, 0, &fl, &line);
277 if (!nk) return 0;
278 j = nk;
279 } else if (c == '`') { refuse(r, E_BACKTICK, j); return 0;
280 } else if (is_glob(c)) { refuse(r, E_GLOB, j); return 0;
281 } else if (c == '~' && eq && j > eq) {
282 size_t pp = prev_sig(r, j);
283 if (pp == eq || r->in[pp] == ':') { refuse(r, E_TILDE, j); return 0; }
284 j++;
285 } else {
286 if (c == '{') {
287 if (bd >= 62) { refuse(r, E_BRACE, j); return 0; }
288 bm &= ~(1ULL << bd);
289 bd++;
290 } else if (bd > 0) {
291 if (c == '}') {
292 bd--;
293 if (bm & (1ULL << bd)) { refuse(r, E_BRACE, j); return 0; }
294 } else if (c == ',') {
295 bm |= 1ULL << (bd - 1);
296 } else if (c == '.') {
297 if (pd) bm |= 1ULL << (bd - 1);
298 else pdot = 1;
299 }
300 }
301 j++;
302 }
303 }
304 *end = j;
305 *flags = fl;
306 *nseg = ns;
307 *aux = eq ? (uint64_t)eq + 1 : 0;
308 *line_out = line;
309 return 1;
310}
311
312void osh_lex_ref(const uint8_t *in, size_t n, int eoi, OshRefLex *out)
313{
314 memset(out, 0, sizeof *out);
315 Ref r = { in, n, out, 1, 0, eoi };
316 size_t i = 0;
317 for (;;) {
318 if (i == n) { out->status = r.eoi || r.last_was_nl || n == 0 ? 0 : 100; return; }
319 int c = peek(&r, i);
320 if (c < 0) { stop(&r, c); return; }
321 if (c == 0) { refuse(&r, E_NUL, i); return; }
322 if (c == ' ' || c == '\t') { r.last_was_nl = 0; i++; continue; }
323 if (c == '\n') {
324 if (add(&r, K_NL, 0, 0, r.line, i, 1, 0)) return;
325 r.line++; r.last_was_nl = 1; i++; continue;
326 }
327 r.last_was_nl = 0;
328 if (c == '#') {
329 size_t k = i + 1;
330 for (;;) {
331 int d = peek(&r, k);
332 if (d < 0) {
333 if (d == INCOMPLETE && r.eoi) { k = n; break; } /* a comment ends with the input */
334 stop(&r, d);
335 return;
336 }
337 if (d == 0) { refuse(&r, E_NUL, k); return; }
338 if (d == '\n') break;
339 k++;
340 }
341 i = k; /* the newline is handled by the loop head */
342 continue;
343 }
344 if (c == '\\') { /* a continuation between words is only white space */
345 uint64_t np = 0;
346 size_t k = i;
347 if (skipc(&r, &k, &np) < 0) { r.o->status = 100; return; }
348 if (np) { r.line += np; i = k; continue; }
349 }
350 if (c == '(' || c == ')') { refuse(&r, E_SUBSHELL, i); return; }
351 if (c == '~') { refuse(&r, E_TILDE, i); return; }
352 if (is_opch(c)) {
353 uint64_t np = 0; /* pairs between the first operator byte and the byte that follows it */
354 size_t q = i + 1;
355 if (skipc(&r, &q, &np) < 0) { r.o->status = 100; return; }
356 int c2 = peek(&r, q);
357 if (c2 < 0) {
358 if (c2 == INCOMPLETE && r.eoi) c2 = -1; /* the operator is complete at the end of the input */
359 else { stop(&r, c2); return; }
360 }
361 if (c2 == 0) { refuse(&r, E_NUL, q); return; }
362 unsigned kind = 0;
363 int two = 0;
364 uint64_t aux = 0;
365 if (c == '|') { if (c2 == '|') { kind = K_OR; two = 1; } else kind = K_PIPE; }
366 else if (c == '&') {
367 if (c2 == '&') { kind = K_AND; two = 1; } else { refuse(&r, E_BACKGROUND, i); return; }
368 }
369 else if (c == ';') { if (c2 == ';') { refuse(&r, E_CASEEND, q); return; } kind = K_SEMI; }
370 else if (c == '<') {
371 if (c2 == '<') { refuse(&r, E_HEREDOC, q); return; }
372 if (c2 == '>') { refuse(&r, E_REDIR_OTHER, q); return; }
373 if (c2 == '&') { kind = K_DUP_IN; two = 1; } else kind = K_LT;
374 } else { /* '>' */
375 aux = 1;
376 if (c2 == '|') { refuse(&r, E_REDIR_OTHER, q); return; }
377 if (c2 == '>') { kind = K_APPEND; two = 1; } else if (c2 == '&') { kind = K_DUP_OUT; two = 1; } else kind = K_GT;
378 }
379 size_t oend = two ? q + 1 : i + 1; /* the token ends after its last byte */
380 if (kind == K_LT || kind == K_DUP_IN || kind == K_GT || kind == K_APPEND || kind == K_DUP_OUT) {
381 OshRefTok *pt = out->ntok ? &out->tok[out->ntok - 1] : NULL;
382 int adjacent = pt && pt->kind == K_WORD && (pt->flags & ~32ULL) == 0 && pt->start + pt->len == i;
383 if (adjacent) {
384 unsigned sig = 0;
385 int v = 0, digits = 1;
386 for (uint64_t z = 0; z < pt->len; z++) {
387 uint64_t o = pt->start + z;
388 if (in[o] == '\\' && z + 1 < pt->len && in[o + 1] == '\n') { z++; continue; }
389 if (!is_digit(in[o])) digits = 0;
390 if (!sig) v = in[o];
391 sig++;
392 }
393 if (digits && (sig != 1 || v > '2')) { refuse(&r, E_FD_RANGE, i); return; }
394 if (digits) { /* IO number: becomes the operator */
395 pt->kind = kind; pt->len = oend - pt->start; pt->aux = (uint64_t)(v - '0');
396 if (two) r.line += np;
397 i = oend;
398 continue;
399 }
400 /* `{NAME}` directly before the operator: bash's descriptor-variable redirection, refused */
401 char nb[4096];
402 size_t nn = 0;
403 for (uint64_t z = 0; z < pt->len && nn < sizeof nb; z++) {
404 uint64_t o = pt->start + z;
405 if (in[o] == '\\' && z + 1 < pt->len && in[o + 1] == '\n') { z++; continue; }
406 nb[nn++] = (char)in[o];
407 }
408 int fdv = nn >= 3 && nb[0] == '{' && nb[nn - 1] == '}' && is_alpha_(nb[1]);
409 for (size_t z = 2; fdv && z + 1 < nn; z++) fdv = is_alpha_(nb[z]) || is_digit(nb[z]);
410 if (fdv) { refuse(&r, E_FD_VARIABLE, pt->start); return; }
411 }
412 }
413 if (add(&r, kind, 0, 0, r.line, i, oend - i, aux)) return;
414 if (two) r.line += np;
415 i = oend;
416 continue;
417 }
418 size_t end;
419 uint64_t fl, ns, aux, wl;
420 if (!word(&r, i, &end, &fl, &ns, &aux, &wl)) return;
421 if ((fl & ~32ULL) == 0 && !aux) { /* a lone `{` or `}` where a command may start is the reserved group syntax */
422 int cs = out->ntok == 0;
423 if (!cs) {
424 uint64_t pk = out->tok[out->ntok - 1].kind;
425 cs = pk == K_NL || pk == K_PIPE || pk == K_OR || pk == K_AND || pk == K_SEMI;
426 }
427 if (cs) {
428 unsigned sig = 0;
429 int v = 0;
430 for (size_t q = i; q < end; q++) {
431 if (in[q] == '\\' && q + 1 < end && in[q + 1] == '\n') { q++; continue; }
432 sig++; v = in[q];
433 }
434 if (sig == 1 && (v == '{' || v == '}')) { refuse(&r, E_GROUP, i); return; }
435 }
436 }
437 if (aux) fl |= 16;
438 if (add(&r, K_WORD, fl, ns, r.line, i, end - i, aux)) return;
439 r.line = wl;
440 i = end;
441 }
442}
443