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_parse.osc.in

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