Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/osh/osh_lex.osc.in # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / osh / osh_lex.osc.in` 607 lines ยท 22633 bytes Raw Blame History 1 // osh lexer unit (aien-architecture#158, cut 4a): a bounded, resumable shell tokenizer. 2 // Entry: lex_run(inp, w). Contract: aien-protocols specs/osh-platform/OSH_PLATFORM_ABI.md (v1 DRAFT); 3 // design: handoffs/osh-audit/E-parser-design.md section 2. Needs the external slices (OSC-EXT-BYTES). 4 // 5 // inp: the accumulated command text (append-only). w: the 11456-cell session workspace. All state is in 6 // w; the unit stores no pointer. One call lexes at most @LEX_BUDGET@ input bytes, writes a token record 7 // first and advances the cursor after, and returns one status: 8 // 0 PHASE_DONE every buffered byte is lexed and the last byte ended a line (newline at rest), or the 9 // host set S_EOI = 1 and the pending word or operator was finished (end of input) 10 // 100 NEED_MORE_INPUT the buffer ends inside an incomplete line (open quote, `${`, trailing backslash, 11 // pending operator or word, no final newline); append and call again 12 // 101 BUDGET_EXHAUSTED budget spent, call again with no new input 13 // 201 CAP_LINE (buffer byte @LINE_CAP@ reached) 202 CAP_TOKENS (@TOKEN_CAP@ tokens) 14 // 213 WORKSPACE_SIZE 214 ABI_MAGIC 215 ABI_VERSION 216 ABI_LENGTH 217 ABI_RESERVED (also S_EOI above 1) 15 // 248 SYNTAX_EOF (S_EOI = 1 and the buffer ends inside a quote or `${`; a final lone backslash is a literal) 16 // 220 NUL_BYTE 221 BACKTICK 222 GLOB 223 TILDE 224 PARAM_OP 225 SPECIAL_PARAM 226 CMDSUB 227 BACKGROUND 17 // 228 SUBSHELL 229 HEREDOC 230 CASEEND 231 REDIR_OTHER 232 FD_RANGE 18 // 236 GROUP (a lone `{` or `}` word at command start) 246 BRACE_EXPANSION 247 POSITIONAL_RANGE (${10}, ${00}) 19 // 249 APPEND_ASSIGN (a NAME+= word; also `$_` and `${_}` are refused as 225 SPECIAL_PARAM) 20 // 250 FD_VARIABLE (a `{NAME}` word directly before a redirection operator: bash allocates a descriptor) 21 // Line continuation: a backslash-newline pair is removed before the byte after it is classified, in every state 22 // except single quotes and comments (bash rule). A chunk that ends in a lone `\` waits (status 100, the `\` 23 // is not consumed) unless the host set S_EOI, when that `\` is a literal byte. Refusal offsets and token spans 24 // are always in the original input. 25 // A refusal stores code and byte offset in the session and makes it terminal (phase 255); a repeated call 26 // returns the same code. ABI header refusals write nothing. 27 // 28 // Token record, 4 cells at TOKENS + 4*t: 29 // 0: kind bits 0-7 | flags bits 8-23 | quoted segments bits 24-31 | line (1-based) bits 32-47 30 // 1: start offset into inp 2: length in bytes (raw source span) 31 // 3: WORD: offset+1 of the `=` when the word starts with a NAME= (else 0) 32 // redirection operator: source fd (explicit IO number, else 1 for > >> >&, 0 for < <&); 0 otherwise 33 // Kinds: 1 WORD 2 PIPE 3 OR 4 AND 5 SEMI 6 LT 7 GT 8 APPEND 9 NL 10 DUP_OUT(>&) 11 DUP_IN(<&) 34 // Flags: 1 has ' 2 has " 4 has backslash escape 8 has $ 16 assignment word 32 has backslash-newline 35 // An unquoted single digit 0..2 directly before < > >> >& <& is merged into that operator (IO number); 36 // any other all-digit word directly before one of them is refused FD_RANGE. 37 // 38 // Character classes: 0 NUL 1 BLANK 2 NL 3 ' 4 " 5 \ 6 $ 7 ` 8 OPCH(| & ; < > ( )) 9 # 10 { 11 } 12 ALPHA/_ 39 // 13 = 14 GLOB(* ? [) 15 OTHER (bytes >= 0x80 and CR included) 16 DIGIT 17 ~ 18 ~ refused inside NAME=value 40 // States: 0 START 1 WORD 2 SQ 3 DQ 4 BS_W 5 BS_DQ 6 DOLLAR_W 7 DOLLAR_DQ 8 BRACE_W 9 BRACE_DQ 10 COMMENT 41 // 11 OP 12 BS_S (backslash at the start of a word) 42 // step() returns next_state | action << 8 | refusal << 16 | token_flags << 24. Actions: 1 begin word here, 43 // 2 end word before this byte and re-dispatch it from START, 3 end a one-byte operator and re-dispatch, 44 // 4 two-byte operator ends here, 6 emit NL, 7 begin operator, 8 begin word at the previous byte. 45 46 fn cls(c: u64) -> u64 { 47 if c == 0 { return 0; } 48 if c == 32 || c == 9 { return 1; } 49 if c == 10 { return 2; } 50 if c == 39 { return 3; } 51 if c == 34 { return 4; } 52 if c == 92 { return 5; } 53 if c == 36 { return 6; } 54 if c == 96 { return 7; } 55 if c == 124 || c == 38 || c == 59 || c == 60 || c == 62 || c == 40 || c == 41 { return 8; } 56 if c == 35 { return 9; } 57 if c == 123 { return 10; } 58 if c == 125 { return 11; } 59 if c == 61 { return 13; } 60 if c == 42 || c == 63 || c == 91 { return 14; } 61 if c == 126 { return 17; } 62 if c >= 48 && c <= 57 { return 16; } 63 if c >= 65 && c <= 90 { return 12; } 64 if c >= 97 && c <= 122 { return 12; } 65 if c == 95 { return 12; } 66 return 15; 67 } 68 69 fn step(st: u64, k: u64, c: u64, p: u64) -> u64 { 70 let mut s: u64 = st; 71 let mut act: u64 = 0; 72 if k == 0 { return s | (220 << 16); } 73 if s == 12 { 74 if c == 10 { return 0; } 75 return 1 | (8 << 8) | (4 << 24); 76 } 77 if s == 0 { 78 if k == 1 { return 0; } 79 if k == 2 { return 0 | (6 << 8); } 80 if k == 9 { return 10; } 81 if k == 5 { return 12; } 82 if k == 8 { 83 if c == 40 || c == 41 { return 0 | (228 << 16); } 84 return 11 | (7 << 8); 85 } 86 if k == 17 { return 223 << 16; } 87 act = 1; 88 s = 1; 89 } 90 if s == 6 || s == 7 { 91 let mut ctx: u64 = 1; 92 if s == 7 { ctx = 3; } 93 if k == 12 || k == 16 { return ctx; } 94 if c == 63 || c == 35 || c == 64 || c == 42 { return ctx; } 95 if c == 123 { return s + 2; } 96 if c == 40 { return s | (226 << 16); } 97 if c == 36 || c == 33 || c == 45 { return s | (225 << 16); } 98 if s == 6 && (c == 39 || c == 34) { return s | (225 << 16); } 99 s = ctx; 100 } 101 if s == 8 || s == 9 { 102 let mut ctx2: u64 = 1; 103 if s == 9 { ctx2 = 3; } 104 if k == 12 || k == 16 { return s; } 105 if k == 11 && p != 123 { return ctx2; } 106 return s | (224 << 16); 107 } 108 if s == 1 { 109 if k == 1 || k == 2 || k == 8 { return 0 | (2 << 8); } 110 if k == 3 { return 2 | (act << 8) | (1 << 24); } 111 if k == 4 { return 3 | (act << 8) | (2 << 24); } 112 if k == 5 { return 4 | (act << 8); } 113 if k == 6 { return 6 | (act << 8) | (8 << 24); } 114 if k == 7 { return 221 << 16; } 115 if k == 14 { return 222 << 16; } 116 if k == 18 { return 223 << 16; } 117 return 1 | (act << 8); 118 } 119 if s == 2 { 120 if k == 3 { return 1; } 121 return 2; 122 } 123 if s == 3 { 124 if k == 4 { return 1; } 125 if k == 5 { return 5; } 126 if k == 6 { return 7 | (8 << 24); } 127 if k == 7 { return 221 << 16; } 128 return 3; 129 } 130 if s == 4 { 131 if c == 10 { return 1 | (32 << 24); } 132 return 1 | (4 << 24); 133 } 134 if s == 5 { 135 if c == 10 { return 3 | (32 << 24); } 136 return 3 | (4 << 24); 137 } 138 if s == 10 { 139 if k == 2 { return 0 | (6 << 8); } 140 return 10; 141 } 142 if s == 11 { 143 if p == 62 && c == 62 { return 0 | (4 << 8); } 144 if p == 124 && c == 124 { return 0 | (4 << 8); } 145 if p == 38 && c == 38 { return 0 | (4 << 8); } 146 if p == 62 && c == 38 { return 0 | (4 << 8); } 147 if p == 60 && c == 38 { return 0 | (4 << 8); } 148 if p == 60 && c == 60 { return 229 << 16; } 149 if p == 59 && c == 59 { return 230 << 16; } 150 if (p == 62 && c == 124) || (p == 60 && c == 62) { return 231 << 16; } 151 return 0 | (3 << 8); 152 } 153 return 217 << 16; 154 } 155 156 // Operator kind from its first byte and the second byte (0 = none); a value >= 200 is a refusal code. 157 fn opkind(p: u64, c2: u64) -> u64 { 158 if p == 124 { 159 if c2 == 124 { return 3; } 160 return 2; 161 } 162 if p == 38 { 163 if c2 == 38 { return 4; } 164 return 227; 165 } 166 if p == 59 { return 5; } 167 if p == 60 { 168 if c2 == 38 { return 11; } 169 return 6; 170 } 171 if p == 62 { 172 if c2 == 62 { return 8; } 173 if c2 == 38 { return 10; } 174 return 7; 175 } 176 return 217; 177 } 178 179 // Record a refusal: code and byte offset into the session, session terminal. Returns the code. 180 fn fail(w: cells, code: u64, off: u64) -> u64 { 181 w[@S_ERR_CODE@] = code; 182 w[@S_ERR_OFF@] = off; 183 w[@S_PHASE@] = 255; 184 return code; 185 } 186 187 // Write token record t = ntok (record first, count after); CAP_TOKENS before writing past capacity. 188 fn emit_tok(w: cells, hdr: u64, s: u64, n: u64, aux: u64) -> u64 { 189 let t: u64 = w[@LX_NTOK@]; 190 if t >= @TOKEN_CAP@ { return fail(w, 202, s); } 191 let b: u64 = @TOKENS@ + t * 4; 192 w[b] = hdr; 193 w[b + 1] = s; 194 w[b + 2] = n; 195 w[b + 3] = aux; 196 w[@LX_NTOK@] = t + 1; 197 return 0; 198 } 199 200 // The single significant byte of the word inp[s..e) after removing backslash-newline pairs, else 0. 201 fn lone(inp: bytes, s: u64, e: u64) -> u64 { 202 let mut i: u64 = s; 203 let mut n: u64 = 0; 204 let mut v: u64 = 0; 205 while i < e && n < 2 bound 4096 { 206 if inp[i] == 92 && i + 1 < e && inp[i + 1] == 10 { 207 i = i + 2; 208 } else { 209 n = n + 1; 210 v = inp[i]; 211 i = i + 1; 212 } 213 } 214 if n != 1 { return 0; } 215 return v; 216 } 217 218 // A lone `{` or `}` word where a command may start (start of input, or after NL | || && ;) is the reserved 219 // group syntax (release 1.2): refused GROUP. Elsewhere it is an ordinary word, as in bash. 220 fn group_check(inp: bytes, w: cells, s: u64, end: u64) -> u64 { 221 if (w[@LX_WFLAGS@] & 65503) != 0 || w[@LX_EQPOS@] != 0 { return 0; } 222 let t: u64 = w[@LX_NTOK@]; 223 let mut cs: u64 = 0; 224 if t == 0 { cs = 1; } 225 if t > 0 { 226 let pk: u64 = w[@TOKENS@ + (t - 1) * 4] & 255; 227 if pk == 9 || pk == 2 || pk == 3 || pk == 4 || pk == 5 { cs = 1; } 228 } 229 if cs == 0 { return 0; } 230 let v: u64 = lone(inp, s, end); 231 if v == 123 || v == 125 { return fail(w, 236, s); } 232 return 0; 233 } 234 235 // The word in progress ends before offset `end`. 236 fn emit_word(inp: bytes, w: cells, end: u64) -> u64 { 237 let s: u64 = w[@LX_WS@]; 238 let gc: u64 = group_check(inp, w, s, end); 239 if gc != 0 { return gc; } 240 let mut f: u64 = w[@LX_WFLAGS@]; 241 let eq: u64 = w[@LX_EQPOS@]; 242 if eq != 0 { f = f | 16; } 243 let mut ns: u64 = w[@LX_NSEG@]; 244 if ns > 255 { ns = 255; } 245 let hdr: u64 = 1 | (f << 8) | (ns << 24) | (w[@LX_WLINE@] << 32); 246 return emit_tok(w, hdr, s, end - s, eq); 247 } 248 249 fn emit_nl(w: cells, i: u64) -> u64 { 250 return emit_tok(w, 9 | ((w[@LX_LINE@] + 1) << 32), i, 1, 0); 251 } 252 253 // IO number: 0 none; 1..3 = the single digit 0..2 word that ends at ws (a backslash-newline pair inside it does 254 // not count), plus one; 100 = an all-digit unquoted word that ends at ws but is not a digit 0..2 (FD_RANGE). 255 fn iomerge(inp: bytes, w: cells, ws: u64) -> u64 { 256 let t: u64 = w[@LX_NTOK@]; 257 if t == 0 { return 0; } 258 let b: u64 = @TOKENS@ + (t - 1) * 4; 259 if (w[b] & `4294959103`) != 1 { return 0; } 260 if w[b + 1] + w[b + 2] != ws { return 0; } 261 if w[@LX_ALLDIG@] != 1 { return 0; } 262 let v: u64 = lone(inp, w[b + 1], ws); 263 if v == 0 { return 100; } 264 let d: u64 = v - 48; 265 if d > 2 { return 100; } 266 return d + 1; 267 } 268 269 // bash's `{NAME}` redirection (the shell allocates a descriptor and stores it in NAME): an unquoted word that is 270 // exactly `{` NAME `}` (backslash-newline pairs skipped) and ends at ws, directly before a redirection operator. 271 // R1 does not implement it, so it is refused (250 FD_VARIABLE) instead of being passed on as a literal word. 272 fn fdvar(inp: bytes, w: cells, ws: u64) -> u64 { 273 let t: u64 = w[@LX_NTOK@]; 274 if t == 0 { return 0; } 275 let b: u64 = @TOKENS@ + (t - 1) * 4; 276 if (w[b] & 255) != 1 { return 0; } 277 let fl: u64 = (w[b] >> 8) & 65535; 278 if fl != 0 && fl != 32 { return 0; } 279 let s: u64 = w[b + 1]; 280 let e: u64 = s + w[b + 2]; 281 if e != ws { return 0; } 282 let mut i: u64 = s; 283 let mut n: u64 = 0; 284 let mut ok: u64 = 1; 285 let mut last: u64 = 0; 286 while i < e && ok == 1 bound 4096 { 287 if inp[i] == 92 && i + 1 < e && inp[i + 1] == 10 { 288 i = i + 2; 289 } else { 290 let c: u64 = inp[i]; 291 if n == 0 { 292 if c != 123 { ok = 0; } 293 } else { 294 if last == 125 { ok = 0; } 295 let k: u64 = cls(c); 296 if c != 125 && k != 12 && !(k == 16 && n > 1) { ok = 0; } 297 if c == 125 && n < 2 { ok = 0; } 298 } 299 last = c; 300 n = n + 1; 301 i = i + 1; 302 } 303 } 304 if ok == 1 && last == 125 && n >= 3 { return 1; } 305 return 0; 306 } 307 308 // An operator of `kind` is complete: it starts at LX_WS and its last byte is at oe - 1 (a backslash-newline pair 309 // may sit between its bytes, so the span is the raw source). 310 fn emit_op(inp: bytes, w: cells, kind: u64, oe: u64) -> u64 { 311 let ws: u64 = w[@LX_WS@]; 312 if kind >= 200 { return fail(w, kind, ws); } 313 let mut aux: u64 = 0; 314 if kind == 7 || kind == 8 || kind == 10 { aux = 1; } 315 if kind == 6 || kind == 7 || kind == 8 || kind == 10 || kind == 11 { 316 let m: u64 = iomerge(inp, w, ws); 317 if m == 100 { return fail(w, 232, ws); } 318 if m == 0 && fdvar(inp, w, ws) == 1 { return fail(w, 250, w[@TOKENS@ + (w[@LX_NTOK@] - 1) * 4 + 1]); } 319 if m != 0 { 320 let b: u64 = @TOKENS@ + (w[@LX_NTOK@] - 1) * 4; 321 w[b] = w[b] - 1 + kind; 322 w[b + 2] = oe - w[b + 1]; 323 w[b + 3] = m - 1; 324 return 0; 325 } 326 } 327 return emit_tok(w, kind | (w[@LX_WLINE@] << 32), ws, oe - ws, aux); 328 } 329 330 // Per-word bookkeeping after a byte inside a word: valid NAME so far, the `=` of NAME=, all digits. 331 fn track(w: cells, st: u64, nst: u64, k: u64, i: u64) { 332 let ws: u64 = w[@LX_WS@]; 333 if w[@LX_NAMEOK@] == 1 { 334 if st == 4 { 335 if k != 2 { w[@LX_NAMEOK@] = 0; } 336 } else { 337 if st != 1 || k != 5 { 338 if k != 12 && !(k == 16 && i > ws) { 339 if k == 13 && i > ws && nst == 1 { w[@LX_EQPOS@] = i + 1; } 340 w[@LX_NAMEOK@] = 0; 341 } 342 } 343 } 344 } 345 if w[@LX_ALLDIG@] == 1 { 346 if k != 16 || nst != 1 { w[@LX_ALLDIG@] = 0; } 347 } 348 } 349 350 // Unquoted brace expansion detector. Called for each byte that is a plain (unquoted, unescaped) part of a 351 // word. bash expands a word with an unquoted `{ ... }` pair that has an unquoted `,` or `..` directly inside; 352 // that expansion is refused by name (BRACE_EXPANSION). Braces without either stay literal, as in bash. This 353 // over-refuses a few shapes bash leaves literal (`{a..bb}`); it never lets an expansion through. 354 fn brace(w: cells, c: u64, i: u64) -> u64 { 355 let d: u64 = w[@LX_BRD@]; 356 let pd: u64 = w[@LX_PDOT@]; 357 w[@LX_PDOT@] = 0; 358 if c == 123 { 359 if d >= 62 { return fail(w, 246, i); } 360 let bit: u64 = 1 << d; 361 w[@LX_BRM@] = w[@LX_BRM@] & ~bit; 362 w[@LX_BRD@] = d + 1; 363 return 0; 364 } 365 if d == 0 { return 0; } 366 if c == 125 { 367 let nd: u64 = d - 1; 368 w[@LX_BRD@] = nd; 369 let bit: u64 = 1 << nd; 370 if (w[@LX_BRM@] & bit) != 0 { return fail(w, 246, i); } 371 return 0; 372 } 373 if c == 44 { 374 let bit: u64 = 1 << (d - 1); 375 w[@LX_BRM@] = w[@LX_BRM@] | bit; 376 return 0; 377 } 378 if c == 46 { 379 if pd == 1 { 380 let bit: u64 = 1 << (d - 1); 381 w[@LX_BRM@] = w[@LX_BRM@] | bit; 382 } else { 383 w[@LX_PDOT@] = 1; 384 } 385 } 386 return 0; 387 } 388 389 // Byte k (class) of a ${...} name. The name is NAME or one digit ($0..$9 only): a second digit is 390 // POSITIONAL_RANGE, a letter after a leading digit is PARAM_OP. 391 fn bname(w: cells, k: u64, p: u64, i: u64) -> u64 { 392 if p == 123 { 393 w[@LX_BRN@] = 0; 394 if k == 16 { w[@LX_BRN@] = 1; } 395 return 0; 396 } 397 if w[@LX_BRN@] == 1 { 398 if k == 16 { return fail(w, 247, i); } 399 if k == 12 { return fail(w, 224, i); } 400 } 401 return 0; 402 } 403 404 // Lex the byte at offset i. Returns 0, a refusal code, or 99 (wait: a final backslash needs the next byte). 405 // Backslash-newline is removed before the next byte is classified, in every state except single quotes (and 406 // comments, where a backslash is literal): a `\` that is followed by a newline only sets LX_CONT and changes 407 // nothing else; the newline then counts the line, flags a word in progress (32) and is otherwise invisible, 408 // so the previous-byte cells (LX_PREV, LX_PO), the state and the pending operator or `$` all see through it. 409 fn lex_byte(inp: bytes, w: cells, i: u64) -> u64 { 410 let c: u64 = inp[i]; 411 let st: u64 = w[@LX_STATE@]; 412 if w[@LX_CONT@] == 1 { 413 w[@LX_CONT@] = 0; 414 if st == 1 || st == 3 || st == 6 || st == 7 || st == 8 || st == 9 { 415 w[@LX_WFLAGS@] = w[@LX_WFLAGS@] | 32; 416 } 417 w[@LX_LINE@] = w[@LX_LINE@] + 1; 418 w[@LX_MID@] = 1; 419 return 0; 420 } 421 let mut k: u64 = cls(c); 422 if c == 92 && st != 2 && st != 4 && st != 5 && st != 10 && st != 12 { 423 if i + 1 < inp.len { 424 if inp[i + 1] == 10 { 425 w[@LX_CONT@] = 1; 426 return 0; 427 } 428 } else { 429 if w[@S_EOI@] == 0 { return 99; } 430 k = 15; 431 } 432 } 433 let p: u64 = w[@LX_PREV@]; 434 let eq: u64 = w[@LX_EQPOS@]; 435 if st == 1 && k == 17 && eq != 0 { 436 if (p == 61 && eq == w[@LX_PO@] + 1) || p == 58 { k = 18; } 437 } 438 let usc: u64 = w[@LX_USC@]; 439 if usc != 0 && c != 0 { 440 if (st == 1 || st == 3) { 441 if k == 12 || k == 16 { w[@LX_USC@] = 0; } else { return fail(w, 225, usc - 1); } 442 } 443 if (st == 8 || st == 9) && k == 11 { return fail(w, 225, usc - 1); } 444 } 445 let mut r: u64 = step(st, k, c, p); 446 let mut act: u64 = (r >> 8) & 255; 447 let mut err: u64 = (r >> 16) & 255; 448 if err != 0 { return fail(w, err, i); } 449 let mut rc: u64 = 0; 450 if act == 2 { 451 rc = emit_word(inp, w, i); 452 if rc != 0 { return rc; } 453 r = step(0, k, c, p); 454 } 455 if act == 3 { 456 rc = emit_op(inp, w, opkind(p, 0), w[@LX_PO@] + 1); 457 if rc != 0 { return rc; } 458 r = step(0, k, c, p); 459 } 460 act = (r >> 8) & 255; 461 err = (r >> 16) & 255; 462 if err != 0 { return fail(w, err, i); } 463 if act == 4 { 464 rc = emit_op(inp, w, opkind(p, c), i + 1); 465 if rc != 0 { return rc; } 466 } 467 if act == 6 { 468 rc = emit_nl(w, i); 469 if rc != 0 { return rc; } 470 } 471 if act == 7 { 472 w[@LX_WS@] = i; 473 w[@LX_WLINE@] = w[@LX_LINE@] + 1; 474 } 475 if act == 1 || act == 8 { 476 let mut b: u64 = i; 477 let mut nm: u64 = 1; 478 if act == 8 { 479 b = i - 1; 480 nm = 0; 481 } 482 w[@LX_WS@] = b; 483 w[@LX_WLINE@] = w[@LX_LINE@] + 1; 484 w[@LX_WFLAGS@] = 0; 485 w[@LX_NAMEOK@] = nm; 486 w[@LX_EQPOS@] = 0; 487 w[@LX_NSEG@] = 0; 488 w[@LX_ALLDIG@] = nm; 489 w[@LX_BRD@] = 0; 490 w[@LX_BRM@] = 0; 491 w[@LX_PDOT@] = 0; 492 w[@LX_PLUS@] = 0; 493 w[@LX_USC@] = 0; 494 } 495 if (st == 8 || st == 9) && (k == 12 || k == 16) { 496 rc = bname(w, k, p, i); 497 if rc != 0 { return rc; } 498 w[@LX_USC@] = 0; 499 if p == 123 && c == 95 { w[@LX_USC@] = i + 1; } 500 } 501 let nst: u64 = r & 255; 502 if (st == 6 || st == 7) && c == 95 { w[@LX_USC@] = i + 1; } 503 let pl: u64 = w[@LX_PLUS@]; 504 w[@LX_PLUS@] = 0; 505 if st == 1 && nst == 1 { 506 if pl == 1 && k == 13 { return fail(w, 249, i); } 507 if c == 43 && w[@LX_NAMEOK@] == 1 && i > w[@LX_WS@] { w[@LX_PLUS@] = 1; } 508 } 509 let wfl: u64 = (r >> 24) & 255; 510 if wfl != 0 { 511 w[@LX_WFLAGS@] = w[@LX_WFLAGS@] | wfl; 512 if (wfl & 3) != 0 { w[@LX_NSEG@] = w[@LX_NSEG@] + 1; } 513 } 514 if nst >= 1 && nst <= 9 { track(w, st, nst, k, i); } 515 if nst == 1 && (st == 0 || st == 1 || st == 6 || st == 11) { 516 rc = brace(w, c, i); 517 if rc != 0 { return rc; } 518 } else { 519 w[@LX_PDOT@] = 0; 520 } 521 w[@LX_STATE@] = nst; 522 w[@LX_MID@] = 1; 523 if act == 6 { w[@LX_MID@] = 0; } 524 if c == 10 { w[@LX_LINE@] = w[@LX_LINE@] + 1; } 525 w[@LX_PREV@] = c; 526 w[@LX_PO@] = i; 527 return 0; 528 } 529 530 // Lex up to @LEX_BUDGET@ bytes from the cursor. Returns 0, a refusal code, or 99 (wait for the next byte). 531 fn lex_chunk(inp: bytes, w: cells) -> u64 { 532 let mut i: u64 = w[@S_CURSOR@]; 533 let mut n: u64 = 0; 534 let mut rc: u64 = 0; 535 while i < inp.len && n < @LEX_BUDGET@ && rc == 0 bound @LEX_BUDGET@ { 536 if i >= @LINE_CAP@ { 537 rc = fail(w, 201, @LINE_CAP@); 538 } else { 539 rc = lex_byte(inp, w, i); 540 if rc == 0 { 541 i = i + 1; 542 n = n + 1; 543 w[@S_CURSOR@] = i; 544 } 545 } 546 } 547 return rc; 548 } 549 550 // ABI header check (OSH_PLATFORM_ABI.md section 3): first failure wins, nothing is written. 551 fn abi_check(w: cells) -> u64 { 552 if w.len < 2 { return 216; } 553 if w[@S_MAGIC@] != @ABI_MAGIC@ { return 214; } 554 if w[@S_VERSION@] != @ABI_VERSION@ { return 215; } 555 if w.len < @WS_CELLS@ { return 213; } 556 for j in @S_RESERVED@ .. 64 { 557 if w[j as u64] != 0 { return 217; } 558 } 559 if w[@S_EOI@] > 1 { return 217; } 560 return 0; 561 } 562 563 // End of input (S_EOI = 1, set by the host once no more bytes will be appended): finish the pending word or 564 // operator. A final lone `\` is a literal byte (bash), already consumed as one. Anything still open (a quote or 565 // `${`) is an unexpected end of input, SYNTAX_EOF. A `$_` that ends the input is the `$_` special parameter. 566 fn lex_flush(inp: bytes, w: cells) -> u64 { 567 let st: u64 = w[@LX_STATE@]; 568 let n: u64 = inp.len; 569 if st == 0 || st == 10 { return 0; } 570 let usc: u64 = w[@LX_USC@]; 571 if usc != 0 && (st == 1 || st == 3) { return fail(w, 225, usc - 1); } 572 if st == 1 || st == 6 { 573 let rc: u64 = emit_word(inp, w, n); 574 if rc != 0 { return rc; } 575 w[@LX_STATE@] = 0; 576 return 0; 577 } 578 if st == 11 { 579 let rc2: u64 = emit_op(inp, w, opkind(w[@LX_PREV@], 0), w[@LX_PO@] + 1); 580 if rc2 != 0 { return rc2; } 581 w[@LX_STATE@] = 0; 582 return 0; 583 } 584 return fail(w, 248, n); 585 } 586 587 fn lex_run(inp: bytes, w: cells) -> u64 { 588 let ac: u64 = abi_check(w); 589 if ac != 0 { return ac; } 590 if w[@S_PHASE@] == 255 { return w[@S_ERR_CODE@]; } 591 if w[@S_CURSOR@] > inp.len { return 216; } 592 w[@LX_DONE@] = 0; 593 let rc: u64 = lex_chunk(inp, w); 594 if rc == 99 { return 100; } 595 if rc != 0 { return rc; } 596 if w[@S_CURSOR@] < inp.len { return 101; } 597 if w[@S_EOI@] == 1 && w[@LX_MID@] != 0 { 598 let fr: u64 = lex_flush(inp, w); 599 if fr != 0 { return fr; } 600 w[@LX_MID@] = 0; 601 } 602 if w[@LX_MID@] == 0 { 603 w[@LX_DONE@] = 1; 604 return 0; 605 } 606 return 100; 607 }