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 / visor / visor_parse_command.h
| 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 | * visor_parse_command.h -- Omega Visor console line parser (lane 2). |
| 19 | * |
| 20 | * Pure tokenizer: no semantic meaning, no session access, deterministic. |
| 21 | * |
| 22 | * Rules: |
| 23 | * - A trailing "\n" / "\r\n" is stripped; leading/trailing blanks are ignored. |
| 24 | * - Blank lines and lines whose first non-blank byte is '#' are EMPTY. |
| 25 | * - Bytes must be valid UTF-8; control bytes (< 0x20 except tab, and 0x7F) are rejected. |
| 26 | * - Lines longer than VISOR_LINE_MAX-1 bytes are rejected (never truncated). |
| 27 | * - If the first word is a command word (help, quit, inspect, ...) the rest of |
| 28 | * the line is split on blanks into at most VISOR_MAX_ARGS arguments, each at |
| 29 | * most VISOR_TOKEN_MAX-1 bytes; arity is checked against the command table. |
| 30 | * `realize <x> for current.machine` sets for_current_machine and leaves |
| 31 | * args = { x }. Any other `for <target>` is rejected. |
| 32 | * - Anything else (e.g. `let x = 3 * 6`, `fn f(x) = x + 1`, `x + 1`) is SOURCE: |
| 33 | * the whole trimmed line is kept in `line` and NOT tokenized. |
| 34 | * - Malformed input returns rc -1 with an error string (no command-name prefix): |
| 35 | * control bytes, invalid UTF-8 or an over-long line -> cmd = UNKNOWN; |
| 36 | * a recognised command word with a bad/over-long argument or wrong arity -> |
| 37 | * cmd keeps that command so the console can name it. A mistyped command word |
| 38 | * ("helpp") is SOURCE, because it is not a command word. |
| 39 | * - `_` is passed through as an ordinary token; resolution happens in the console. |
| 40 | */ |
| 41 | #ifndef OMEGA_VISOR_PARSE_COMMAND_H |
| 42 | #define OMEGA_VISOR_PARSE_COMMAND_H |
| 43 | |
| 44 | #include <stdbool.h> |
| 45 | #include <stddef.h> |
| 46 | |
| 47 | #define VISOR_MAX_ARGS 4 |
| 48 | #define VISOR_TOKEN_MAX 256 /* max token length 255 bytes + NUL */ |
| 49 | #define VISOR_LINE_MAX 4096 /* max line length 4095 bytes + NUL */ |
| 50 | |
| 51 | typedef enum { |
| 52 | VISOR_CMD_HELP = 0, |
| 53 | VISOR_CMD_QUIT, |
| 54 | VISOR_CMD_INSPECT, |
| 55 | VISOR_CMD_TYPE, |
| 56 | VISOR_CMD_ID, |
| 57 | VISOR_CMD_GRAPH, |
| 58 | VISOR_CMD_VERIFY, |
| 59 | VISOR_CMD_MACHINE, |
| 60 | VISOR_CMD_REALIZE, |
| 61 | VISOR_CMD_COST, |
| 62 | VISOR_CMD_RUN, |
| 63 | VISOR_CMD_EVIDENCE, |
| 64 | VISOR_CMD_BINDINGS, |
| 65 | VISOR_CMD_CLEAR, |
| 66 | VISOR_CMD_WORLD, |
| 67 | VISOR_CMD_EFFECTS, |
| 68 | VISOR_CMD_ALTERNATIVES, |
| 69 | VISOR_CMD_COMPARE, |
| 70 | VISOR_CMD_WHY, |
| 71 | VISOR_CMD_SOURCE, /* not a command word: a source line for the language lane */ |
| 72 | VISOR_CMD_EMPTY, /* blank or comment */ |
| 73 | VISOR_CMD_UNKNOWN, /* malformed (see rules above) */ |
| 74 | VISOR_CMD__COUNT |
| 75 | } VisorCmd; |
| 76 | |
| 77 | typedef struct { |
| 78 | VisorCmd cmd; |
| 79 | char args[VISOR_MAX_ARGS][VISOR_TOKEN_MAX]; |
| 80 | size_t argc; |
| 81 | bool for_current_machine; /* `realize x for current.machine` */ |
| 82 | char line[VISOR_LINE_MAX]; /* trimmed line (always set when rc == 0) */ |
| 83 | } VisorCommand; |
| 84 | |
| 85 | /* Parse one line. Returns 0 on success; -1 on malformed input, in which case |
| 86 | * out->cmd is VISOR_CMD_UNKNOWN unless a command word was recognised (then it |
| 87 | * keeps that command so the error can name it) and `err` holds a message. */ |
| 88 | int visor_parse_command(const char *line, VisorCommand *out, char *err, size_t errn); |
| 89 | |
| 90 | /* Command word for a VisorCmd ("help", ..., "source", "empty", "unknown"). */ |
| 91 | const char *visor_cmd_name(VisorCmd c); |
| 92 | |
| 93 | /* Arity bounds (number of args after the command word). Returns -1 for |
| 94 | * SOURCE/EMPTY/UNKNOWN. */ |
| 95 | int visor_cmd_arity(VisorCmd c, size_t *min_args, size_t *max_args); |
| 96 | |
| 97 | #endif /* OMEGA_VISOR_PARSE_COMMAND_H */ |