Compare commits

..

2 commits

Author SHA1 Message Date
fenris 52b2aee6ea [mod] readme 2025-09-23 21:46:56 +02:00
fenris d2395d897e [sty] main 2025-09-23 21:46:49 +02:00
2 changed files with 56 additions and 53 deletions

View file

@ -54,25 +54,13 @@ record<
where:
- `version` should be `"2"`
- `parser_start` is one of the parser rule `label` values.
- `lexer_rules.*.type` is one of `ignore`, `void`, `boolean`, `int`, `float`, `string` (see section _Lexer rule types_)
- `lexer_rules.*.pass` controls, whether the read sequence shall be incorporated or not
- `parser_rules.*.conclusion.*.type` is one of `terminal`, `variable` (see section _Conclusion element types_)
- `parser_start` is one of the parser rule `label` values
The recommended extension for such files is `.tp2.json`.
### Lexer rule types
| type | meaning | parameters |
|-- |-- |-- |
| `ignore` | disregard matching strings | `pattern` |
| `void` | do not assign a value to matching strings | `pattern`, `id` |
| `boolean` | interpret matching strings as boolean values | `pattern`, `id`, `value` |
| `int` | interpret matching strings as integer number values | `pattern`, `id` |
| `float` | interpret matching strings as floating point number values | `pattern`, `id` |
| `string` | interpret matching strings as string values | `pattern`, `id` |
### Conclusion element types
| type | meaning | parameters |

View file

@ -21,13 +21,20 @@ along with »type2«. If not, see <http://www.gnu.org/licenses/>.
/**
* @author fenris
*/
type type_resulttree = lib_plankton.lang.type_tree<lib_plankton.lang.type_terminal_default>;
type type_resulttree = lib_plankton.lang.type_tree<
lib_plankton.lang.type_terminal_default
>;
/**
* @author fenris
*/
type type_myreader = lib_plankton.lang.class_reader<char, lib_plankton.lang.type_terminal_default, string, type_resulttree>;
type type_myreader = lib_plankton.lang.class_reader<
char,
lib_plankton.lang.type_terminal_default,
string,
type_resulttree
>;
/**
@ -51,46 +58,52 @@ async function main(
args_raw : Array<string>
) : Promise<void>
{
const args_handler : lib_args.class_handler = new lib_args.class_handler({
"path": lib_args.class_argument.positional({
"index": 0,
"type": lib_args.enum_type.string,
"mode": lib_args.enum_mode.replace,
"info": "the path to the grammar file",
"name": "path",
}),
"verbosity": lib_args.class_argument.volatile({
"indicators_short": ["v"],
"indicators_long": ["verbosity"],
"type": lib_args.enum_type.integer,
"mode": lib_args.enum_mode.replace,
"default": 1,
"info": "verbosity level",
"name": "verbosity",
}),
"help": lib_args.class_argument.volatile({
"indicators_short": ["h"],
"indicators_long": ["help"],
"type": lib_args.enum_type.boolean,
"mode": lib_args.enum_mode.replace,
"default": false,
"info": "show help and exit",
"name": "help",
}),
});
const args_handler : lib_args.class_handler = new lib_args.class_handler(
{
"path": lib_args.class_argument.positional({
"index": 0,
"type": lib_args.enum_type.string,
"mode": lib_args.enum_mode.replace,
"info": "the path to the grammar file",
"name": "path",
}),
"verbosity": lib_args.class_argument.volatile({
"indicators_short": ["v"],
"indicators_long": ["verbosity"],
"type": lib_args.enum_type.integer,
"mode": lib_args.enum_mode.replace,
"default": 1,
"info": "verbosity level",
"name": "verbosity",
}),
"help": lib_args.class_argument.volatile({
"indicators_short": ["h"],
"indicators_long": ["help"],
"type": lib_args.enum_type.boolean,
"mode": lib_args.enum_mode.replace,
"default": false,
"info": "show help and exit",
"name": "help",
}),
}
);
const args : Record<string, any> = args_handler.read(lib_args.enum_environment.cli, args_raw.join(" "));
if (args["help"]) {
if (args["help"])
{
process.stdout.write(
args_handler.generate_help({
"programname": "type2",
"description": "reads a string from stdin and parses it according to a given grammar to an abstract syntax tree as JSON",
"author": "kcf <fenris@folksprak.org>",
"executable": "type2",
})
args_handler.generate_help(
{
"programname": "type2",
"description": "reads a string from stdin and parses it according to a given grammar to an abstract syntax tree as JSON",
"author": "kcf <fenris@folksprak.org>",
"executable": "type2",
}
)
);
return Promise.resolve<void>(undefined);
}
else {
else
{
lib_plankton.log.conf_push(
(args["verbosity"] <= 0)
? []
@ -112,10 +125,12 @@ async function main(
const reader : type_myreader = await generate_reader(args["path"]);
const input_raw : string = await lib_plankton.file.read_stdin();
const result : type_resulttree = reader.run(input_raw.split(""));
if (result === null) {
if (result === null)
{
return Promise.reject<void>(new Error("reading failed"));
}
else {
else
{
process.stdout.write(JSON.stringify(result, undefined, "\t") + "\n");
return Promise.resolve<void>(undefined);
}