-
Notifications
You must be signed in to change notification settings - Fork 25
Unidirectional Compilation #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
86f6c5b
most of unidirectional compilation
mr-martian ce7251e
acx-applier
mr-martian b282bab
some typos
mr-martian 6b3e35e
allow multiple variants per entry, invert on `lt-restrict rl`
mr-martian 4f7d8a7
Merge branch 'master' into uni-comp
mr-martian 9d1d402
add tests + random cleanup
mr-martian 7aa5256
cli.h-ify lt_proc.cc
mr-martian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright (C) 2022 Apertium | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU General Public License as | ||
| * published by the Free Software Foundation; either version 2 of the | ||
| * License, or (at your option) any later version. | ||
| * | ||
| * This program 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 | ||
| * General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
| #include <lttoolbox/acx.h> | ||
| #include <lttoolbox/xml_walk_util.h> | ||
|
|
||
| const xmlChar* CHAR_NODE = (const xmlChar*)"char"; | ||
| const xmlChar* EQUIV_NODE = (const xmlChar*)"equiv-char"; | ||
| const char* VALUE_ATTR = "value"; | ||
|
|
||
| int32_t get_val(xmlNode* node) | ||
| { | ||
| UString s = getattr(node, VALUE_ATTR); | ||
| if (s.empty()) { | ||
| error_and_die(node, "Missing value attribute."); | ||
| } | ||
| std::vector<int32_t> v; | ||
| ustring_to_vec32(s, v); | ||
| if (v.size() > 1) { | ||
| error_and_die(node, "Expected a single character in value attribute, but found %d.", v.size()); | ||
| } | ||
| return v[0]; | ||
| } | ||
|
|
||
| std::map<int32_t, sorted_vector<int32_t>> readACX(const char* file) | ||
| { | ||
| std::map<int32_t, sorted_vector<int32_t>> acx; | ||
| xmlNode* top_node = load_xml(file); | ||
| for (auto char_node : children(top_node)) { | ||
| if (!xmlStrEqual(char_node->name, CHAR_NODE)) { | ||
| error_and_die(char_node, "Expected <char> but found <%s>.", | ||
| (const char*)char_node->name); | ||
| } | ||
| int32_t key = get_val(char_node); | ||
| sorted_vector<int32_t> vec; | ||
| for (auto equiv_node : children(char_node)) { | ||
| if (!xmlStrEqual(equiv_node->name, EQUIV_NODE)) { | ||
| error_and_die(char_node, "Expected <equiv-char> but found <%s>.", | ||
| (const char*)equiv_node->name); | ||
| } | ||
| vec.insert(get_val(equiv_node)); | ||
| } | ||
| if (!vec.empty()) { | ||
| acx.insert(std::make_pair(key, vec)); | ||
| } | ||
| } | ||
| return acx; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright (C) 2022 Apertium | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU General Public License as | ||
| * published by the Free Software Foundation; either version 2 of the | ||
| * License, or (at your option) any later version. | ||
| * | ||
| * This program 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 | ||
| * General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
| #ifndef _ACXPARSEUTIL_ | ||
| #define _ACXPARSEUTIL_ | ||
|
|
||
| #include <lttoolbox/sorted_vector.hpp> | ||
| #include <map> | ||
|
|
||
| std::map<int32_t, sorted_vector<int32_t>> readACX(const char* file); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| /* | ||
| * Copyright (C) 2022 Apertium | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU General Public License as | ||
| * published by the Free Software Foundation; either version 2 of the | ||
| * License, or (at your option) any later version. | ||
| * | ||
| * This program 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 | ||
| * General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include <lttoolbox/cli.h> | ||
|
|
||
| #include <cstdlib> | ||
| #include <iostream> | ||
| #include <libgen.h> | ||
| #include <string> | ||
| #include <getopt.h> | ||
|
|
||
| CLI::CLI(std::string desc, std::string ver) | ||
| { | ||
| description = desc; | ||
| version = ver; | ||
| } | ||
|
|
||
| CLI::CLI(std::string desc) | ||
| { | ||
| description = desc; | ||
| } | ||
|
|
||
| CLI::~CLI() | ||
| { | ||
| } | ||
|
|
||
| void CLI::add_str_arg(char short_flag, std::string long_flag, | ||
| std::string desc, std::string arg) | ||
| { | ||
| options.push_back({.short_opt=short_flag, .long_opt=long_flag, | ||
| .desc=desc, .is_bool=false, .var=arg}); | ||
| } | ||
|
|
||
| void CLI::add_bool_arg(char short_flag, std::string long_flag, | ||
| std::string desc) | ||
| { | ||
| options.push_back({.short_opt=short_flag, .long_opt=long_flag, | ||
| .desc=desc, .is_bool=true, .var=""}); | ||
| } | ||
|
|
||
| void CLI::add_file_arg(std::string name, bool optional) | ||
| { | ||
| file_args.push_back(std::make_pair(name, optional)); | ||
| if (!optional) min_file_args++; | ||
| } | ||
|
|
||
| void CLI::set_epilog(std::string e) | ||
| { | ||
| epilog = e; | ||
| } | ||
|
|
||
| void CLI::print_usage() | ||
| { | ||
| if (!prog_name.empty()) { | ||
| std::cout << prog_name; | ||
| if (!version.empty()) { | ||
| std::cout << " v" << version; | ||
| } | ||
| std::cout << ": " << description << std::endl; | ||
| std::cout << "USAGE: " << prog_name; | ||
| std::string bargs; | ||
| std::string sargs; | ||
| for (auto& it : options) { | ||
| if (it.is_bool) { | ||
| bargs += it.short_opt; | ||
| } else { | ||
| sargs += " [-"; | ||
| sargs += it.short_opt; | ||
| sargs += ' '; | ||
| sargs += it.var; | ||
| sargs += ']'; | ||
| } | ||
| } | ||
| if (!bargs.empty()) { | ||
| std::cout << " [-" << bargs << "]"; | ||
| } | ||
| std::cout << sargs; | ||
| int depth = 0; | ||
| for (auto& it : file_args) { | ||
| std::cout << ' '; | ||
| if (it.second) { | ||
| std::cout << '['; | ||
| depth += 1; | ||
| } | ||
| std::cout << it.first; | ||
| } | ||
| while (depth-- > 0) std::cout << "]"; | ||
| std::cout << std::endl; | ||
| for (auto& it : options) { | ||
| std::cout << " -" << it.short_opt; | ||
| #if HAVE_GETOPT_LONG | ||
| std::cout << ", --" << it.long_opt << ':'; | ||
| for (size_t i = it.long_opt.size(); i < 20; i++) { | ||
| std::cout << ' '; | ||
| } | ||
| #else | ||
| std::cout << ": "; | ||
| #endif | ||
| std::cout << it.desc << std::endl; | ||
| } | ||
| if (!epilog.empty()) { | ||
| std::cout << epilog << std::endl; | ||
| } | ||
| } | ||
| exit(EXIT_FAILURE); | ||
| } | ||
|
|
||
| void CLI::parse_args(int argc, char* argv[]) | ||
| { | ||
| prog_name = basename(argv[0]); | ||
| std::string arg_str; | ||
| #if HAVE_GETOPT_LONG | ||
| struct option long_options[options.size()]; | ||
| int option_index = 0; | ||
| #endif | ||
| for (size_t i = 0; i < options.size(); i++) { | ||
| arg_str += options[i].short_opt; | ||
| if (!options[i].is_bool) arg_str += ':'; | ||
| #if HAVE_GETOPT_LONG | ||
| long_options[i].name = options[i].long_opt.c_str(); | ||
| long_options[i].has_arg = (options[i].is_bool ? no_argument : required_argument); | ||
| long_options[i].flag = 0; | ||
| long_options[i].val = options[i].short_opt; | ||
| #endif | ||
| } | ||
|
|
||
| while (true) { | ||
| #if HAVE_GETOPT_LONG | ||
| int cnt = getopt_long(argc, argv, arg_str.c_str(), long_options, &option_index); | ||
| #else | ||
| int cnt = getopt(argc, argv, arg_str.c_str()); | ||
| #endif | ||
| if (cnt == -1) break; | ||
|
|
||
| bool found = false; | ||
| for (auto& it : options) { | ||
| if (it.short_opt == cnt) { | ||
| found = true; | ||
| if (it.short_opt == 'v' && it.long_opt == "version") { | ||
| std::cout << prog_name << " version " << version << std::endl; | ||
| exit(EXIT_SUCCESS); | ||
| } | ||
| if (it.is_bool) { | ||
| bools[it.long_opt] = true; | ||
| } else { | ||
| strs[it.long_opt].push_back(optarg); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| if (!found || cnt == 'h') { | ||
| print_usage(); | ||
| } | ||
| } | ||
| while (optind < argc) { | ||
| files.push_back(argv[optind++]); | ||
| } | ||
| if (files.size() < min_file_args || files.size() > file_args.size()) { | ||
| print_usage(); | ||
| } | ||
| while (files.size() < file_args.size()) { | ||
| files.push_back(""); | ||
| } | ||
| } | ||
|
|
||
| std::map<std::string, std::vector<std::string>>& CLI::get_strs() | ||
| { | ||
| return strs; | ||
| } | ||
|
|
||
| std::map<std::string, bool>& CLI::get_bools() | ||
| { | ||
| return bools; | ||
| } | ||
|
|
||
| std::vector<std::string>& CLI::get_files() | ||
| { | ||
| return files; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright (C) 2022 Apertium | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU General Public License as | ||
| * published by the Free Software Foundation; either version 2 of the | ||
| * License, or (at your option) any later version. | ||
| * | ||
| * This program 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 | ||
| * General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
| #include <map> | ||
|
|
||
| class CLI { | ||
| private: | ||
| struct CLIOption { | ||
| char short_opt; | ||
| std::string long_opt; | ||
| std::string desc; | ||
| bool is_bool; | ||
| std::string var; | ||
| }; | ||
|
|
||
| std::string description; | ||
| std::string version; | ||
| std::string epilog; | ||
|
|
||
| std::vector<CLIOption> options; | ||
| std::vector<std::pair<std::string, bool>> file_args; | ||
| size_t min_file_args = 0; | ||
|
|
||
| std::map<std::string, std::vector<std::string>> strs; | ||
| std::map<std::string, bool> bools; | ||
| std::vector<std::string> files; | ||
|
|
||
| std::string prog_name; | ||
|
|
||
| public: | ||
| CLI(std::string desc, std::string version); | ||
| CLI(std::string desc); | ||
| ~CLI(); | ||
| void add_str_arg(char short_flag, std::string long_flag, std::string desc, | ||
| std::string arg); | ||
| void add_bool_arg(char short_flag, std::string long_flag, std::string desc); | ||
| void add_file_arg(std::string name, bool optional = true); | ||
| void set_epilog(std::string e); | ||
| void print_usage(); | ||
| void parse_args(int argc, char* argv[]); | ||
| std::map<std::string, std::vector<std::string>>& get_strs(); | ||
| std::map<std::string, bool>& get_bools(); | ||
| std::vector<std::string>& get_files(); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.