type2/source/type2.ts

154 lines
3.7 KiB
TypeScript
Raw Normal View History

/*
This file is part of »type2«.
2016-12-26 17:56:02 +01:00
2025-09-23 20:56:22 +02:00
Copyright 2016-2021 kcf <fenris@folksprak.org>
»type2« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»type2« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with »type2«. If not, see <http://www.gnu.org/licenses/>.
*/
2016-12-26 17:56:02 +01:00
/**
* @author fenris
*/
2025-09-23 21:46:49 +02:00
type type_resulttree = lib_plankton.lang.type_tree<
lib_plankton.lang.type_terminal_default
>;
2016-12-26 17:56:02 +01:00
/**
* @author fenris
*/
2025-09-23 21:46:49 +02:00
type type_myreader = lib_plankton.lang.class_reader<
char,
lib_plankton.lang.type_terminal_default,
string,
type_resulttree
>;
/**
* @author fenris
*/
2021-09-19 01:14:32 +02:00
async function generate_reader(
path : string
2021-09-19 01:14:32 +02:00
) : Promise<type_myreader>
{
2021-09-19 01:57:25 +02:00
const content : string = await lib_plankton.file.read(path);
2021-09-19 01:14:32 +02:00
const data : any = JSON.parse(content);
const reader : type_myreader = lib_plankton.lang.class_reader.default_raw(data);
return Promise.resolve<type_myreader>(reader);
}
/**
* @author fenris
*/
2021-09-19 01:14:32 +02:00
async function main(
args_raw : Array<string>
) : Promise<void>
{
2025-09-23 21:46:49 +02:00
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",
}),
}
);
2021-09-19 01:14:32 +02:00
const args : Record<string, any> = args_handler.read(lib_args.enum_environment.cli, args_raw.join(" "));
2025-09-23 21:46:49 +02:00
if (args["help"])
{
2021-09-19 01:14:32 +02:00
process.stdout.write(
2025-09-23 21:46:49 +02:00
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",
}
)
);
2021-09-19 01:14:32 +02:00
return Promise.resolve<void>(undefined);
}
2025-09-23 21:46:49 +02:00
else
{
2021-09-19 01:14:32 +02:00
lib_plankton.log.conf_push(
(args["verbosity"] <= 0)
? []
: [
new lib_plankton.log.class_channel_minlevel(
new lib_plankton.log.class_channel_console(),
[
lib_plankton.log.enum_level.error,
lib_plankton.log.enum_level.warning,
lib_plankton.log.enum_level.notice,
lib_plankton.log.enum_level.info,
lib_plankton.log.enum_level.debug,
][args["verbosity"]-1]
),
]
);
// exec
const reader : type_myreader = await generate_reader(args["path"]);
2021-09-19 01:57:25 +02:00
const input_raw : string = await lib_plankton.file.read_stdin();
2021-09-19 01:14:32 +02:00
const result : type_resulttree = reader.run(input_raw.split(""));
2025-09-23 21:46:49 +02:00
if (result === null)
{
2021-09-19 01:14:32 +02:00
return Promise.reject<void>(new Error("reading failed"));
}
2025-09-23 21:46:49 +02:00
else
{
2021-09-19 01:14:32 +02:00
process.stdout.write(JSON.stringify(result, undefined, "\t") + "\n");
return Promise.resolve<void>(undefined);
}
}
}
2016-12-26 17:56:02 +01:00
/**
* @author fenris
*/
main(process.argv.slice(2))
.then(
2021-09-19 01:14:32 +02:00
() => {
// process.stderr.write("done");
},
(error) => {
2021-09-19 01:14:32 +02:00
process.stderr.write(error.toString() + "\n");
}
);
2016-12-26 17:56:02 +01:00