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