4
Copyright (c) 2021 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
6
https://bmstu.codes/lsx/simodo/loom
9
#include "simodo/variable/json/LexicalParametersLoader.h"
10
#include "simodo/variable/json/parser/JsonRdp.h"
12
namespace simodo::variable
16
inout::LexemeType loadLexemeType(const std::u16string & type_string)
18
inout::LexemeType type;
20
if (type_string == u"Compound")
21
type = inout::LexemeType::Compound;
22
else if (type_string == u"Empty")
23
type = inout::LexemeType::Empty;
24
else if (type_string == u"Punctuation")
25
type = inout::LexemeType::Punctuation;
26
else if (type_string == u"Id")
27
type = inout::LexemeType::Id;
28
else if (type_string == u"Annotation")
29
type = inout::LexemeType::Annotation;
30
else if (type_string == u"Number")
31
type = inout::LexemeType::Number;
32
else if (type_string == u"Comment")
33
type = inout::LexemeType::Comment;
35
type = inout::LexemeType::Error;
40
bool loadMarkups(const Value & value, std::vector<inout::MarkupSymbol> & markups)
42
if (value.type() != ValueType::Array)
45
const std::shared_ptr<Array> array = value.getArray();
47
for(const Value & v : array->values()) {
48
if (v.type() != ValueType::Object)
51
const std::shared_ptr<Object> obj = v.getObject();
53
inout::MarkupSymbol markup;
55
if (obj->exists(u"start"))
56
markup.start = obj->find(u"start").getString();
58
if (obj->exists(u"end"))
59
markup.end = obj->find(u"end").getString();
61
if (obj->exists(u"ignore_sign"))
62
markup.ignore_sign = obj->find(u"ignore_sign").getString();
64
std::u16string type_string;
65
if (obj->exists(u"type"))
66
type_string = obj->find(u"type").getString();
68
if (type_string.empty())
71
markup.type = loadLexemeType(type_string);
73
markups.push_back(markup);
79
bool loadMasks(const Value & value, std::vector<inout::NumberMask> & masks)
81
if (value.type() != ValueType::Array)
84
const std::shared_ptr<Array> array = value.getArray();
86
for(const Value & v : array->values())
88
if (v.type() != ValueType::Object)
91
const std::shared_ptr<Object> obj = v.getObject();
93
inout::NumberMask mask;
95
if (obj->exists(u"chars"))
96
mask.chars = obj->find(u"chars").getString();
98
std::u16string type_string;
99
if (obj->exists(u"type"))
100
type_string = obj->find(u"type").getString();
102
if (type_string.empty())
105
mask.type = loadLexemeType(type_string);
107
if (obj->exists(u"system"))
109
int system_number = obj->find(u"system").getInt();
111
if (system_number < 2 || system_number > 16)
114
mask.system = static_cast<inout::number_system_t>(system_number);
117
/// \todo cppcheck: error: Uninitialized struct member: mask.system [uninitStructMember]
118
masks.push_back(mask);
124
bool loadPunctuationWords(const Value & value, std::vector<std::u16string> & punctuation_words)
126
if (value.type() != ValueType::Array)
129
const std::shared_ptr<Array> array = value.getArray();
131
for(const Value & v : array->values()) {
132
if (v.type() != ValueType::String)
135
punctuation_words.push_back(v.getString());
144
bool loadLexicalParameters(const std::string &file_name, inout::LexicalParameters &lex)
146
inout::NullReporter null_reporter;
148
JsonRdp parser(null_reporter, file_name, value);
150
bool ok = parser.parse();
155
if (value.type() != ValueType::Object)
158
const std::shared_ptr<Object> lex_object = value.getObject();
162
if (lex_object->exists(u"markups") && !loadMarkups(lex_object->find(u"markups"), lex.markups))
165
if (lex_object->exists(u"masks") && !loadMasks(lex_object->find(u"masks"), lex.masks))
168
if (lex_object->exists(u"punctuation_chars"))
169
lex.punctuation_chars = lex_object->find(u"punctuation_chars").getString();
171
if (lex_object->exists(u"punctuation_words") && !loadPunctuationWords(lex_object->find(u"punctuation_words"), lex.punctuation_words))
174
if (lex_object->exists(u"digits"))
175
lex.digits = lex_object->find(u"digits").getString();
177
if (lex_object->exists(u"latin_alphabet"))
178
lex.latin_alphabet = lex_object->find(u"latin_alphabet").getString();
180
if (lex_object->exists(u"national_alphabet"))
181
lex.national_alphabet = lex_object->find(u"national_alphabet").getString();
183
if (lex_object->exists(u"id_extra_symbols"))
184
lex.id_extra_symbols = lex_object->find(u"id_extra_symbols").getString();
186
if (lex_object->exists(u"may_national_letters_use"))
187
lex.may_national_letters_use = lex_object->find(u"may_national_letters_use").getBool();
189
if (lex_object->exists(u"may_national_letters_mix"))
190
lex.may_national_letters_mix = lex_object->find(u"may_national_letters_mix").getBool();
192
if (lex_object->exists(u"is_case_sensitive"))
193
lex.is_case_sensitive = lex_object->find(u"is_case_sensitive").getBool();
195
if (lex_object->exists(u"eof_symbol"))
196
lex.eof_symbol = lex_object->find(u"eof_symbol").getString();
198
if (lex_object->exists(u"nl_substitution"))
199
lex.nl_substitution = lex_object->find(u"nl_substitution").getString();
201
catch(const std::exception &)