forked from BenHanson/gram_grep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.cpp
181 lines (150 loc) · 5.54 KB
/
types.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "pch.h"
#include <lexertl/generator.hpp>
#include <parsertl/generator.hpp>
#include "gg_error.hpp"
#include <parsertl/lookup.hpp>
#include "types.hpp"
extern config_parser g_config_parser;
extern parser* g_curr_parser;
extern uparser* g_curr_uparser;
extern bool g_icase;
extern bool g_force_unicode;
void config_state::parse(const std::string& config_pathname)
{
lexertl::citerator iter;
lexertl::citerator end;
if (g_icase)
{
using enum lexertl::regex_flags;
if (g_force_unicode)
_lurules.flags(*icase | *dot_not_cr_lf);
else
_lrules.flags(*icase | *dot_not_cr_lf);
}
_mf.open(config_pathname.c_str());
if (!_mf.data())
{
std::ostringstream ss;
ss << "Unable to open " << config_pathname;
throw gg_error(ss.str());
}
iter = lexertl::citerator(_mf.data(), _mf.data() + _mf.size(),
g_config_parser._lsm);
_results.reset(iter->id, g_config_parser._gsm);
while (_results.entry.action != parsertl::action::error &&
_results.entry.action != parsertl::action::accept)
{
if (_results.entry.action == parsertl::action::reduce)
{
auto i = g_config_parser._actions.find(_results.entry.param);
if (i != g_config_parser._actions.end())
{
try
{
i->second(*this, g_config_parser);
}
catch (const std::exception& e)
{
std::ostringstream ss;
const auto idx =
_results.production_size(g_config_parser._gsm,
_results.entry.param) - 1;
const auto& token =
_results.dollar(idx, g_config_parser._gsm,
_productions);
const std::size_t line =
1 + std::count(_mf.data(), token.first, '\n');
// Column makes no sense here as we are
// already at the end of the line
ss << config_pathname << '(' << line << "): " << e.what();
throw gg_error(ss.str());
}
}
}
parsertl::lookup(iter, g_config_parser._gsm, _results,
_productions);
}
if (_results.entry.action == parsertl::action::error)
{
const std::size_t line =
1 + std::count(_mf.data(), iter->first, '\n');
const char endl[] = { '\n' };
const std::size_t column = iter->first - std::find_end(_mf.data(),
iter->first, endl, endl + 1);
std::ostringstream ss;
ss << config_pathname << '(' << line << ':' << column << "): Parse error";
throw gg_error(ss.str());
}
_mf.close();
if (_grules.grammar().empty())
{
if (g_force_unicode)
_lurules.push(".{+}[\r\n]", lurules::skip());
else
_lrules.push(".{+}[\r\n]", lexertl::rules::skip());
}
else
{
std::string warnings;
parsertl::rules::string_vector terminals;
const auto& grammar = _grules.grammar();
const auto& ids = g_force_unicode ? _lurules.ids() : _lrules.ids();
std::set<std::size_t> used_tokens;
std::set<std::size_t> used_prec;
if (g_force_unicode)
parsertl::generator::build(_grules, g_curr_uparser->_gsm,
&warnings);
else
parsertl::generator::build(_grules, g_curr_parser->_gsm,
&warnings);
_grules.terminals(terminals);
if (!warnings.empty())
std::cerr << "Warnings from config " << config_pathname << " : " <<
warnings;
for (const auto& p : grammar)
{
// Check for %prec TOKEN
if (!p._rhs._prec.empty())
{
const auto pos = std::ranges::find(terminals, p._rhs._prec);
if (pos != terminals.end())
{
const size_t idx = pos - terminals.begin();
used_prec.insert(idx);
}
}
for (const auto& rhs : p._rhs._symbols)
{
if (rhs._type == parsertl::rules::symbol::type::TERMINAL)
used_tokens.insert(rhs._id);
}
}
for (std::size_t i = 1, size = terminals.size(); i < size; ++i)
{
bool found_id = false;
for (const auto& curr_ids : ids)
{
found_id = std::ranges::find(curr_ids, i) != curr_ids.end() ||
used_prec.contains(i);
if (found_id) break;
}
if (!found_id)
std::cerr << "Warning: Token \"" << terminals[i] <<
"\" does not have a lexer definiton.\n";
if (!used_tokens.contains(i) && !used_prec.contains(i))
{
std::cerr << "Warning: Token \"" << terminals[i] <<
"\" is not used in the grammar.\n";
}
}
}
if (g_force_unicode)
{
using rules_type = lexertl::basic_rules<char, char32_t>;
using generator = lexertl::basic_generator<rules_type,
lexertl::u32state_machine>;
generator::build(_lurules, g_curr_uparser->_lsm);
}
else
lexertl::generator::build(_lrules, g_curr_parser->_lsm);
}