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
omega-mv / src / osh / osh_parse.osc
| 1 | // GENERATED by src/osh/osh_gen.sh from osh_layout.lst and osh_parse.osc.in. Do not edit; edit the .in or the list. |
| 2 | // osh parser unit (aien-architecture#158, cut 4b): a bounded, resumable, non-recursive shell parser. |
| 3 | // Entry: parse_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 3. Needs the external slices (OSC-EXT-BYTES). |
| 5 | // |
| 6 | // It reads the token records the lexer unit (osh_lex.osc) left in the same workspace and builds the command |
| 7 | // and pipeline tables. It does NOT expand anything and does not write the request record: argv, assignment |
| 8 | // and redirection lists stay token ranges, the expander (cut 4c) resolves them per pipeline. |
| 9 | // |
| 10 | // Grammar (flat; no recursion and no stack are needed in release 1): |
| 11 | // list := pipeline ( ( '&&' | '||' | ';' ) pipeline )* [ ';' ] terminated by NL (or end of input) |
| 12 | // pipeline := command ( '|' command )* |
| 13 | // command := ( assignment | word | redirection )+ assignments only before the first word |
| 14 | // redirection := ( '<' | '>' | '>>' ) word | ( '>&' | '<&' ) word word = one digit 0..2 for '>&' '<&' |
| 15 | // A NL after '|', '&&' or '||' is skipped (the list continues). A NL, or the end of input, after a command or |
| 16 | // after ';' completes the list. Tokens after the completing NL are not consumed: PX_TOK is the index of the |
| 17 | // first one, the host starts a new session for them. |
| 18 | // |
| 19 | // parse_run returns one status: |
| 20 | // 0 PHASE_DONE the list is complete (tables valid; zero pipelines for a blank line) |
| 21 | // 100 NEED_MORE_INPUT the list ends in '|', '&&' or '||' (host appends a line and calls lex_run, then parse_run) |
| 22 | // 101 BUDGET_EXHAUSTED 64 tokens consumed, call again |
| 23 | // 203 CAP_CMDS 204 CAP_PIPELINES 205 CAP_PIPE_LEN 206 CAP_WORDS 207 CAP_ASSIGNS 208 CAP_REDIRS |
| 24 | // 213 WORKSPACE_SIZE 214 ABI_MAGIC 215 ABI_VERSION 216 ABI_LENGTH 217 ABI_RESERVED (217 also: the lexer has |
| 25 | // not completed, LX_DONE is not 1, or a parser cell is corrupt) |
| 26 | // 231 REDIR_OTHER (a '>&' '<&' target that is not a plain digit) 232 FD_RANGE (a digit target above 2) |
| 27 | // 233 IF_COMPOUND 234 LOOP 235 CASE 237 FUNCTION 238 NEGATION 239 UNSUPPORTED_BUILTIN (time, coproc) |
| 28 | // 240 IFS_ASSIGN 241 SYNTAX_EMPTY_CMD 242 SYNTAX_REDIR_TARGET 248 SYNTAX_EOF (the end of input follows |
| 29 | // '|', '&&' or '||') |
| 30 | // A refusal stores code and byte offset (the start of the offending token; the buffer length for an |
| 31 | // end-of-input refusal) in the session and makes it terminal (phase 255); a repeated call returns the same code. |
| 32 | // A reserved word is a flagless word that is the first non-assignment word of a command. |
| 33 | // |
| 34 | // Command entry, 8 cells at CMDS + 8*c: {first token, end token (exclusive: the terminator), nassign, nword, |
| 35 | // nredir, pipeline index, position in the pipeline, 0} |
| 36 | // Pipeline entry, 4 cells at PIPES + 4*p: {first command, ncmds, connector_after (0 none, 1 ; or NL, 2 &&, |
| 37 | // 3 ||), first token} |
| 38 | |
| 39 | // Record a refusal: code and byte offset into the session, session terminal. Returns the code. |
| 40 | fn fail(w: cells, code: u64, off: u64) -> u64 { |
| 41 | w[4] = code; |
| 42 | w[5] = off; |
| 43 | w[2] = 255; |
| 44 | return code; |
| 45 | } |
| 46 | |
| 47 | // ABI header check (OSH_PLATFORM_ABI.md section 3): first failure wins, nothing is written. |
| 48 | fn abi_check(w: cells) -> u64 { |
| 49 | if w.len < 2 { return 216; } |
| 50 | if w[0] != 1330858071 { return 214; } |
| 51 | if w[1] != 1 { return 215; } |
| 52 | if w.len < 11456 { return 213; } |
| 53 | for j in 48 .. 64 { |
| 54 | if w[j as u64] != 0 { return 217; } |
| 55 | } |
| 56 | if w[28] > 1 { return 217; } |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | // The significant bytes of inp[s..e) (backslash-newline pairs removed) packed big-endian into a u64; 0 when |
| 61 | // there are more than 8 of them. |
| 62 | fn pack8(inp: bytes, s: u64, e: u64) -> u64 { |
| 63 | let mut i: u64 = s; |
| 64 | let mut n: u64 = 0; |
| 65 | let mut v: u64 = 0; |
| 66 | while i < e bound 4096 { |
| 67 | if inp[i] == 92 && i + 1 < e && inp[i + 1] == 10 { |
| 68 | i = i + 2; |
| 69 | } else { |
| 70 | n = n + 1; |
| 71 | if n <= 8 { v = (v << 8) | inp[i]; } |
| 72 | i = i + 1; |
| 73 | } |
| 74 | } |
| 75 | if n > 8 { return 0; } |
| 76 | return v; |
| 77 | } |
| 78 | |
| 79 | // A reserved word (packed) is refused by family; 0 for any other word. |
| 80 | fn reserved(v: u64) -> u64 { |
| 81 | if v == 0x6966 || v == 0x7468656e || v == 0x656c6966 || v == 0x656c7365 || v == 0x6669 { return 233; } |
| 82 | if v == 0x666f72 || v == 0x7768696c65 || v == 0x756e74696c || v == 0x646f || v == 0x646f6e65 || v == 0x696e { return 234; } |
| 83 | if v == 0x73656c656374 { return 234; } |
| 84 | if v == 0x63617365 || v == 0x65736163 { return 235; } |
| 85 | if v == 0x66756e6374696f6e { return 237; } |
| 86 | if v == 0x21 { return 238; } |
| 87 | if v == 0x74696d65 || v == 0x636f70726f63 { return 239; } |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | // 1 when all n bytes at s are decimal digits. |
| 92 | fn alldig(inp: bytes, s: u64, n: u64) -> u64 { |
| 93 | let mut i: u64 = 0; |
| 94 | let mut r: u64 = 1; |
| 95 | while i < n && r == 1 bound 4096 { |
| 96 | let c: u64 = inp[s + i]; |
| 97 | if c < 48 || c > 57 { r = 0; } |
| 98 | i = i + 1; |
| 99 | } |
| 100 | return r; |
| 101 | } |
| 102 | |
| 103 | // The word after '>&' or '<&': one flagless digit 0..2 is fine (0); another digit or digits is FD_RANGE; anything |
| 104 | // else (a name, '-', a quoted or expanding word) is REDIR_OTHER. |
| 105 | fn dup_check(inp: bytes, hdr: u64, s: u64, n: u64) -> u64 { |
| 106 | if ((hdr >> 8) & 65535) != 0 { return 231; } |
| 107 | if n == 1 { |
| 108 | let c: u64 = inp[s]; |
| 109 | if c >= 48 && c <= 50 { return 0; } |
| 110 | } |
| 111 | if alldig(inp, s, n) == 1 { return 232; } |
| 112 | return 231; |
| 113 | } |
| 114 | |
| 115 | // Start a command at token t (byte offset s). Capacities are checked before anything is written. |
| 116 | fn new_cmd(w: cells, t: u64, s: u64) -> u64 { |
| 117 | let pc: u64 = w[38]; |
| 118 | if pc >= 8 { return fail(w, 205, s); } |
| 119 | let np: u64 = w[37]; |
| 120 | if pc == 0 && np >= 16 { return fail(w, 204, s); } |
| 121 | let c: u64 = w[36]; |
| 122 | if c >= 32 { return fail(w, 203, s); } |
| 123 | let mut p: u64 = np; |
| 124 | if pc == 0 { |
| 125 | let pb: u64 = 832 + np * 4; |
| 126 | w[pb] = c; |
| 127 | w[pb + 1] = 0; |
| 128 | w[pb + 2] = 0; |
| 129 | w[pb + 3] = t; |
| 130 | w[37] = np + 1; |
| 131 | } else { |
| 132 | p = np - 1; |
| 133 | } |
| 134 | let cb: u64 = 576 + c * 8; |
| 135 | w[cb] = t; |
| 136 | w[cb + 1] = 0; |
| 137 | w[cb + 2] = 0; |
| 138 | w[cb + 3] = 0; |
| 139 | w[cb + 4] = 0; |
| 140 | w[cb + 5] = p; |
| 141 | w[cb + 6] = pc; |
| 142 | w[cb + 7] = 0; |
| 143 | w[832 + p * 4 + 1] = pc + 1; |
| 144 | w[38] = pc + 1; |
| 145 | w[36] = c + 1; |
| 146 | w[39] = 0; |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | // The current command ends before token te. |
| 151 | fn end_cmd(w: cells, te: u64) { |
| 152 | w[576 + (w[36] - 1) * 8 + 1] = te; |
| 153 | } |
| 154 | |
| 155 | // The current pipeline ends with connector conn. |
| 156 | fn end_pipe(w: cells, conn: u64) { |
| 157 | w[832 + (w[37] - 1) * 4 + 2] = conn; |
| 158 | w[38] = 0; |
| 159 | } |
| 160 | |
| 161 | // A word token inside a command: prefix assignment, reserved word, or argument. |
| 162 | fn on_word(inp: bytes, w: cells, t: u64, hdr: u64) -> u64 { |
| 163 | let b: u64 = 64 + t * 4; |
| 164 | let f: u64 = (hdr >> 8) & 65535; |
| 165 | let s: u64 = w[b + 1]; |
| 166 | let cb: u64 = 576 + (w[36] - 1) * 8; |
| 167 | if (f & 16) != 0 && w[39] == 0 { |
| 168 | if pack8(inp, s, w[b + 3] - 1) == 0x494653 { return fail(w, 240, s); } |
| 169 | let na: u64 = w[cb + 2]; |
| 170 | if na >= 16 { return fail(w, 207, s); } |
| 171 | w[cb + 2] = na + 1; |
| 172 | return 0; |
| 173 | } |
| 174 | if w[39] == 0 && (f & 65503) == 0 { |
| 175 | let rc: u64 = reserved(pack8(inp, s, s + w[b + 2])); |
| 176 | if rc != 0 { return fail(w, rc, s); } |
| 177 | } |
| 178 | let nw: u64 = w[cb + 3]; |
| 179 | if nw >= 32 { return fail(w, 206, s); } |
| 180 | w[cb + 3] = nw + 1; |
| 181 | w[39] = 1; |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | // The token after a redirection operator must be a word (and a valid descriptor for '>&' '<&'). |
| 186 | fn on_target(inp: bytes, w: cells, t: u64, hdr: u64) -> u64 { |
| 187 | let b: u64 = 64 + t * 4; |
| 188 | let s: u64 = w[b + 1]; |
| 189 | if (hdr & 255) != 1 { return fail(w, 242, s); } |
| 190 | let rk: u64 = w[40]; |
| 191 | if rk == 10 || rk == 11 { |
| 192 | let rc: u64 = dup_check(inp, hdr, s, w[b + 2]); |
| 193 | if rc != 0 { return fail(w, rc, s); } |
| 194 | } |
| 195 | w[34] = 2; |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | // Consume token t. 0 = continue, 1 = the list is complete, >= 200 = refusal (recorded). |
| 200 | // States: 0 FRESH (start, or after ';') 1 AFTER_OP (after | && ||) 2 IN_CMD 3 NEED_TARGET 4 DONE |
| 201 | fn parse_step(inp: bytes, w: cells, t: u64) -> u64 { |
| 202 | let b: u64 = 64 + t * 4; |
| 203 | let hdr: u64 = w[b]; |
| 204 | let k: u64 = hdr & 255; |
| 205 | let s: u64 = w[b + 1]; |
| 206 | let st: u64 = w[34]; |
| 207 | if st == 3 { return on_target(inp, w, t, hdr); } |
| 208 | if k == 9 { |
| 209 | if st == 2 { |
| 210 | end_cmd(w, t); |
| 211 | end_pipe(w, 1); |
| 212 | w[34] = 4; |
| 213 | return 1; |
| 214 | } |
| 215 | if st == 0 { |
| 216 | w[34] = 4; |
| 217 | return 1; |
| 218 | } |
| 219 | return 0; |
| 220 | } |
| 221 | if k >= 2 && k <= 5 { |
| 222 | if st != 2 { return fail(w, 241, s); } |
| 223 | end_cmd(w, t); |
| 224 | w[34] = 1; |
| 225 | if k == 2 { return 0; } |
| 226 | let mut conn: u64 = 1; |
| 227 | if k == 4 { conn = 2; } |
| 228 | if k == 3 { conn = 3; } |
| 229 | end_pipe(w, conn); |
| 230 | if k == 5 { w[34] = 0; } |
| 231 | return 0; |
| 232 | } |
| 233 | if st == 0 || st == 1 { |
| 234 | let rc: u64 = new_cmd(w, t, s); |
| 235 | if rc != 0 { return rc; } |
| 236 | w[34] = 2; |
| 237 | } |
| 238 | if k == 1 { return on_word(inp, w, t, hdr); } |
| 239 | let cb: u64 = 576 + (w[36] - 1) * 8; |
| 240 | let nr: u64 = w[cb + 4]; |
| 241 | if nr >= 8 { return fail(w, 208, s); } |
| 242 | w[cb + 4] = nr + 1; |
| 243 | w[40] = k; |
| 244 | w[34] = 3; |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | // Every token is consumed and the list is not complete. At end of input a list that ends in `;` |
| 249 | // (state 0 with a command parsed) is done. |
| 250 | fn at_end(inp: bytes, w: cells) -> u64 { |
| 251 | let st: u64 = w[34]; |
| 252 | if st == 0 { |
| 253 | if w[28] == 1 && w[36] > 0 { w[34] = 4; } |
| 254 | return 0; |
| 255 | } |
| 256 | if w[28] == 0 { return 100; } |
| 257 | if st == 2 { |
| 258 | end_cmd(w, w[17]); |
| 259 | end_pipe(w, 0); |
| 260 | w[34] = 4; |
| 261 | return 0; |
| 262 | } |
| 263 | if st == 3 { return fail(w, 242, inp.len); } |
| 264 | return fail(w, 248, inp.len); |
| 265 | } |
| 266 | |
| 267 | fn parse_run(inp: bytes, w: cells) -> u64 { |
| 268 | let ac: u64 = abi_check(w); |
| 269 | if ac != 0 { return ac; } |
| 270 | if w[2] == 255 { return w[4]; } |
| 271 | if w[27] != 1 { return 217; } |
| 272 | if w[34] > 4 { return 217; } |
| 273 | if w[34] == 4 { return 0; } |
| 274 | let ntok: u64 = w[17]; |
| 275 | let mut t: u64 = w[35]; |
| 276 | if t > ntok { return 217; } |
| 277 | let mut n: u64 = 0; |
| 278 | let mut rc: u64 = 0; |
| 279 | while t < ntok && n < 64 && rc == 0 bound 64 { |
| 280 | rc = parse_step(inp, w, t); |
| 281 | if rc < 2 { |
| 282 | t = t + 1; |
| 283 | n = n + 1; |
| 284 | w[35] = t; |
| 285 | } |
| 286 | } |
| 287 | if rc == 1 { return 0; } |
| 288 | if rc >= 2 { return rc; } |
| 289 | if t < ntok { return 101; } |
| 290 | return at_end(inp, w); |
| 291 | } |