loom

Форк
0
/
LexicalParametersLoader.cpp 
208 строк · 6.8 Кб
1
/*
2
MIT License
3

4
Copyright (c) 2021 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
5

6
https://bmstu.codes/lsx/simodo/loom
7
*/
8

9
#include "simodo/variable/json/LexicalParametersLoader.h"
10
#include "simodo/variable/json/parser/JsonRdp.h"
11

12
namespace simodo::variable
13
{
14
    namespace
15
    {
16
        inout::LexemeType loadLexemeType(const std::u16string & type_string)
17
        {
18
            inout::LexemeType type;
19

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;
34
            else
35
                type = inout::LexemeType::Error;
36

37
            return type;
38
        }
39

40
        bool loadMarkups(const Value & value, std::vector<inout::MarkupSymbol> & markups)
41
        {
42
            if (value.type() != ValueType::Array)
43
                return false;
44

45
            const std::shared_ptr<Array> array = value.getArray();
46

47
            for(const Value & v : array->values()) {
48
                if (v.type() != ValueType::Object)
49
                    return false;
50

51
                const std::shared_ptr<Object> obj = v.getObject();
52

53
                inout::MarkupSymbol markup;
54

55
                if (obj->exists(u"start"))
56
                    markup.start = obj->find(u"start").getString();
57

58
                if (obj->exists(u"end"))
59
                    markup.end = obj->find(u"end").getString();
60

61
                if (obj->exists(u"ignore_sign"))
62
                    markup.ignore_sign = obj->find(u"ignore_sign").getString();
63

64
                std::u16string type_string;
65
                if (obj->exists(u"type"))
66
                    type_string = obj->find(u"type").getString();
67

68
                if (type_string.empty())
69
                    return false;
70

71
                markup.type = loadLexemeType(type_string);
72

73
                markups.push_back(markup);
74
            }
75

76
            return true;
77
        }
78

79
        bool loadMasks(const Value & value, std::vector<inout::NumberMask> & masks)
80
        {
81
            if (value.type() != ValueType::Array)
82
                return false;
83

84
            const std::shared_ptr<Array> array = value.getArray();
85

86
            for(const Value & v : array->values())
87
            {
88
                if (v.type() != ValueType::Object)
89
                    return false;
90

91
                const std::shared_ptr<Object> obj = v.getObject();
92

93
                inout::NumberMask mask;
94

95
                if (obj->exists(u"chars"))
96
                    mask.chars = obj->find(u"chars").getString();
97

98
                std::u16string type_string;
99
                if (obj->exists(u"type"))
100
                    type_string = obj->find(u"type").getString();
101

102
                if (type_string.empty())
103
                    return false;
104

105
                mask.type = loadLexemeType(type_string);
106

107
                if (obj->exists(u"system"))
108
                {
109
                    int system_number = obj->find(u"system").getInt();
110

111
                    if (system_number < 2 || system_number > 16)
112
                        return false;
113

114
                    mask.system = static_cast<inout::number_system_t>(system_number);
115
                }
116

117
                /// \todo cppcheck: error: Uninitialized struct member: mask.system [uninitStructMember]
118
                masks.push_back(mask);
119
            }
120

121
            return true;
122
        }
123

124
        bool loadPunctuationWords(const Value & value, std::vector<std::u16string> & punctuation_words)
125
        {
126
            if (value.type() != ValueType::Array)
127
                return false;
128

129
            const std::shared_ptr<Array> array = value.getArray();
130

131
            for(const Value & v : array->values()) {
132
                if (v.type() != ValueType::String)
133
                    return false;
134

135
                punctuation_words.push_back(v.getString());
136
            }
137

138
            return true;
139
        }
140

141
    }
142

143

144
    bool loadLexicalParameters(const std::string &file_name, inout::LexicalParameters &lex)
145
    {
146
        inout::NullReporter null_reporter;
147
        Value               value;
148
        JsonRdp             parser(null_reporter, file_name, value);
149

150
        bool                ok = parser.parse();
151

152
        if (!ok)
153
            return false;
154

155
        if (value.type() != ValueType::Object)
156
            return false;
157

158
        const std::shared_ptr<Object> lex_object = value.getObject();
159

160
        try
161
        {
162
            if (lex_object->exists(u"markups") && !loadMarkups(lex_object->find(u"markups"), lex.markups))
163
                return false;
164

165
            if (lex_object->exists(u"masks") && !loadMasks(lex_object->find(u"masks"), lex.masks))
166
                return false;
167

168
            if (lex_object->exists(u"punctuation_chars"))
169
                lex.punctuation_chars = lex_object->find(u"punctuation_chars").getString();
170

171
            if (lex_object->exists(u"punctuation_words") && !loadPunctuationWords(lex_object->find(u"punctuation_words"), lex.punctuation_words))
172
                return false;
173

174
            if (lex_object->exists(u"digits"))
175
                lex.digits = lex_object->find(u"digits").getString();
176

177
            if (lex_object->exists(u"latin_alphabet"))
178
                lex.latin_alphabet = lex_object->find(u"latin_alphabet").getString();
179

180
            if (lex_object->exists(u"national_alphabet"))
181
                lex.national_alphabet = lex_object->find(u"national_alphabet").getString();
182

183
            if (lex_object->exists(u"id_extra_symbols"))
184
                lex.id_extra_symbols = lex_object->find(u"id_extra_symbols").getString();
185

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();
188

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();
191

192
            if (lex_object->exists(u"is_case_sensitive"))
193
                lex.is_case_sensitive = lex_object->find(u"is_case_sensitive").getBool();
194

195
            if (lex_object->exists(u"eof_symbol"))
196
                lex.eof_symbol = lex_object->find(u"eof_symbol").getString();
197

198
            if (lex_object->exists(u"nl_substitution"))
199
                lex.nl_substitution = lex_object->find(u"nl_substitution").getString();
200
        }
201
        catch(const std::exception &)
202
        {
203
            return false;
204
        }
205

206
        return true;
207
    }
208
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.