#include <skeleton.h>
|
| Skeleton (const dfa_t &dfa, const charset_t &cs, const rules_t &rs, const std::string &dfa_name, const std::string &dfa_cond, uint32_t dfa_line) |
|
| ~Skeleton () |
|
void | warn_undefined_control_flow () |
|
void | warn_unreachable_rules () |
|
void | warn_match_empty () |
|
void | emit_data (const char *fname) |
|
void | emit_start (OutputFile &o, size_t maxfill, bool backup, bool backupctx, bool accept) const |
|
void | emit_end (OutputFile &o, bool backup, bool backupctx) const |
|
void | emit_action (OutputFile &o, uint32_t ind, rule_rank_t rank) const |
|
uint32_t | rule2key (rule_rank_t r) const |
|
Definition at line 107 of file skeleton.h.
re2c::Skeleton::Skeleton |
( |
const dfa_t & |
dfa, |
|
|
const charset_t & |
cs, |
|
|
const rules_t & |
rs, |
|
|
const std::string & |
dfa_name, |
|
|
const std::string & |
dfa_cond, |
|
|
uint32_t |
dfa_line |
|
) |
| |
Definition at line 73 of file skeleton.cc.
88 const size_t nc = cs.size() - 1;
94 dfa_state_t *s = dfa.states[i];
95 std::vector<std::pair<Node*, uint32_t> > arcs;
96 for (
size_t c = 0; c < nc;)
98 const size_t j = s->
arcs[c];
99 for (;++c < nc && s->arcs[c] == j;);
103 arcs.push_back(std::make_pair(to, cs[c]));
106 if (arcs.size() == 1 && arcs[0].first == nil)
123 uint32_t maxrule = 0;
127 if (!r.is_none () && !r.is_def ())
129 maxrule = std::max (maxrule, r.uint32 ());
136 const uint32_t max = std::max (maxlen, maxrule);
137 if (max <= std::numeric_limits<uint8_t>::max())
141 else if (max <= std::numeric_limits<uint16_t>::max())
void error(const char *fmt,...)
void init(bool b, RuleOp *r, const std::vector< std::pair< Node *, uint32_t > > &arcs)
static const uint32_t DIST_MAX
std::string incond(const std::string &cond)
re2c::Skeleton::~Skeleton |
( |
| ) |
|
Definition at line 317 of file generate_code.cc.
319 o.wind(ind).ws(
"status = action_").wstring(
name).ws(
"(i, keys, input, token, &cursor, ").wu32(
rule2key (rank)).ws(
");\n");
320 o.wind(ind).ws(
"continue;\n");
static key_t rule2key(rule_rank_t r)
void re2c::Skeleton::emit_data |
( |
const char * |
fname | ) |
|
Definition at line 127 of file generate_data.cc.
129 const std::string input_name = std::string (fname) +
"." +
name +
".input";
130 FILE * input = fopen (input_name.c_str (),
"wb");
133 error (
"cannot open file: %s", input_name.c_str ());
136 const std::string keys_name = std::string (fname) +
"." +
name +
".keys";
137 FILE * keys = fopen (keys_name.c_str (),
"wb");
140 error (
"cannot open file: %s", keys_name.c_str ());
144 generate_paths (input, keys);
void error(const char *fmt,...)
void re2c::Skeleton::emit_end |
( |
OutputFile & |
o, |
|
|
bool |
backup, |
|
|
bool |
backupctx |
|
) |
| const |
Definition at line 256 of file generate_code.cc.
261 o.ws(
"\n").wind(1).ws(
"}");
262 o.ws(
"\n").wind(1).ws(
"if (status == 0) {");
263 o.ws(
"\n").wind(2).ws(
"if (cursor != eof) {");
264 o.ws(
"\n").wind(3).ws(
"status = 1;");
265 o.ws(
"\n").wind(3).ws(
"const long pos = token - input;");
266 o.ws(
"\n").wind(3).ws(
"fprintf(stderr, \"error: lex_").wstring(
name).ws(
": unused input strings left at position %ld\\n\", pos);");
267 o.ws(
"\n").wind(2).ws(
"}");
268 o.ws(
"\n").wind(2).ws(
"if (i != keys_count) {");
269 o.ws(
"\n").wind(3).ws(
"status = 1;");
270 o.ws(
"\n").wind(3).ws(
"fprintf(stderr, \"error: lex_").wstring(
name).ws(
": unused keys left after %u iterations\\n\", i);");
271 o.ws(
"\n").wind(2).ws(
"}");
272 o.ws(
"\n").wind(1).ws(
"}");
275 o.ws(
"\n").wind(1).ws(
"free(input);");
276 o.ws(
"\n").wind(1).ws(
"free(keys);");
278 o.ws(
"\n").wind(1).ws(
"return status;");
281 o.ws(
"\n#undef YYCTYPE");
282 o.ws(
"\n#undef YYKEYTYPE");
283 o.ws(
"\n#undef YYPEEK");
284 o.ws(
"\n#undef YYSKIP");
287 o.ws(
"\n#undef YYBACKUP");
288 o.ws(
"\n#undef YYRESTORE");
292 o.ws(
"\n#undef YYBACKUPCTX");
293 o.ws(
"\n#undef YYRESTORECTX");
295 o.ws(
"\n#undef YYLESSTHAN");
296 o.ws(
"\n#undef YYFILL");
void re2c::Skeleton::emit_epilog |
( |
OutputFile & |
o, |
|
|
const std::set< std::string > & |
names |
|
) |
| |
|
static |
Definition at line 300 of file generate_code.cc.
302 o.ws(
"\n").ws(
"int main()");
305 for (std::set<std::string>::const_iterator i = names.begin (); i != names.end (); ++i)
307 o.ws(
"\n").wind(1).ws(
"if(lex_").wstring(*i).ws(
"() != 0) {");
308 o.ws(
"\n").wind(2).ws(
"return 1;");
309 o.ws(
"\n").wind(1).ws(
"}");
312 o.ws(
"\n").wind(1).ws(
"return 0;");
void re2c::Skeleton::emit_prolog |
( |
OutputFile & |
o | ) |
|
|
static |
Definition at line 54 of file generate_code.cc.
56 o.ws(
"\n#include <stdio.h>");
57 o.ws(
"\n#include <stdlib.h> /* malloc, free */");
59 o.ws(
"\nstatic void *read_file");
60 o.ws(
"\n").wind(1).ws(
"( const char *fname");
61 o.ws(
"\n").wind(1).ws(
", size_t unit");
62 o.ws(
"\n").wind(1).ws(
", size_t padding");
63 o.ws(
"\n").wind(1).ws(
", size_t *pfsize");
64 o.ws(
"\n").wind(1).ws(
")");
66 o.ws(
"\n").wind(1).ws(
"void *buffer = NULL;");
67 o.ws(
"\n").wind(1).ws(
"size_t fsize = 0;");
69 o.ws(
"\n").wind(1).ws(
"/* open file */");
70 o.ws(
"\n").wind(1).ws(
"FILE *f = fopen(fname, \"rb\");");
71 o.ws(
"\n").wind(1).ws(
"if(f == NULL) {");
72 o.ws(
"\n").wind(2).ws(
"goto error;");
73 o.ws(
"\n").wind(1).ws(
"}");
75 o.ws(
"\n").wind(1).ws(
"/* get file size */");
76 o.ws(
"\n").wind(1).ws(
"fseek(f, 0, SEEK_END);");
77 o.ws(
"\n").wind(1).ws(
"fsize = (size_t) ftell(f) / unit;");
78 o.ws(
"\n").wind(1).ws(
"fseek(f, 0, SEEK_SET);");
80 o.ws(
"\n").wind(1).ws(
"/* allocate memory for file and padding */");
81 o.ws(
"\n").wind(1).ws(
"buffer = malloc(unit * (fsize + padding));");
82 o.ws(
"\n").wind(1).ws(
"if (buffer == NULL) {");
83 o.ws(
"\n").wind(2).ws(
"goto error;");
84 o.ws(
"\n").wind(1).ws(
"}");
86 o.ws(
"\n").wind(1).ws(
"/* read the whole file in memory */");
87 o.ws(
"\n").wind(1).ws(
"if (fread(buffer, unit, fsize, f) != fsize) {");
88 o.ws(
"\n").wind(2).ws(
"goto error;");
89 o.ws(
"\n").wind(1).ws(
"}");
91 o.ws(
"\n").wind(1).ws(
"fclose(f);");
92 o.ws(
"\n").wind(1).ws(
"*pfsize = fsize;");
93 o.ws(
"\n").wind(1).ws(
"return buffer;");
96 o.ws(
"\n").wind(1).ws(
"fprintf(stderr, \"error: cannot read file '%s'\\n\", fname);");
97 o.ws(
"\n").wind(1).ws(
"free(buffer);");
98 o.ws(
"\n").wind(1).ws(
"if (f != NULL) {");
99 o.ws(
"\n").wind(2).ws(
"fclose(f);");
100 o.ws(
"\n").wind(1).ws(
"}");
101 o.ws(
"\n").wind(1).ws(
"return NULL;");
void re2c::Skeleton::emit_start |
( |
OutputFile & |
o, |
|
|
size_t |
maxfill, |
|
|
bool |
backup, |
|
|
bool |
backupctx, |
|
|
bool |
accept |
|
) |
| const |
Definition at line 107 of file generate_code.cc.
117 o.ws(
"\n#define YYCTYPE ");
119 o.ws(
"\n#define YYKEYTYPE ");
121 o.ws(
"\n#define YYPEEK() *cursor");
122 o.ws(
"\n#define YYSKIP() ++cursor");
125 o.ws(
"\n#define YYBACKUP() marker = cursor");
126 o.ws(
"\n#define YYRESTORE() cursor = marker");
130 o.ws(
"\n#define YYBACKUPCTX() ctxmarker = cursor");
131 o.ws(
"\n#define YYRESTORECTX() cursor = ctxmarker");
133 o.ws(
"\n#define YYLESSTHAN(n) (limit - cursor) < n");
134 o.ws(
"\n#define YYFILL(n) { break; }");
136 o.ws(
"\nstatic int action_").wstring(
name);
137 o.ws(
"\n").wind(1).ws(
"( unsigned int i");
138 o.ws(
"\n").wind(1).ws(
", const YYKEYTYPE *keys");
139 o.ws(
"\n").wind(1).ws(
", const YYCTYPE *start");
140 o.ws(
"\n").wind(1).ws(
", const YYCTYPE *token");
141 o.ws(
"\n").wind(1).ws(
", const YYCTYPE **cursor");
142 o.ws(
"\n").wind(1).ws(
", YYKEYTYPE rule_act");
143 o.ws(
"\n").wind(1).ws(
")");
145 o.ws(
"\n").wind(1).ws(
"const long pos = token - start;");
146 o.ws(
"\n").wind(1).ws(
"const long len_act = *cursor - token;");
147 o.ws(
"\n").wind(1).ws(
"const long len_exp = (long) keys [3 * i + 1];");
148 o.ws(
"\n").wind(1).ws(
"const YYKEYTYPE rule_exp = keys [3 * i + 2];");
149 o.ws(
"\n").wind(1).ws(
"if (rule_exp == ").wu32(default_rule).ws(
") {");
150 o.ws(
"\n").wind(2).ws(
"fprintf");
151 o.ws(
"\n").wind(3).ws(
"( stderr");
152 o.ws(
"\n").wind(3).ws(
", \"warning: lex_").wstring(
name).ws(
": control flow is undefined for input\"");
153 o.ws(
"\n").wind(4).ws(
"\" at position %ld, rerun re2c with '-W'\\n\"");
154 o.ws(
"\n").wind(3).ws(
", pos");
155 o.ws(
"\n").wind(3).ws(
");");
156 o.ws(
"\n").wind(1).ws(
"}");
157 o.ws(
"\n").wind(1).ws(
"if (len_act == len_exp && rule_act == rule_exp) {");
158 o.ws(
"\n").wind(2).ws(
"const YYKEYTYPE offset = keys[3 * i];");
159 o.ws(
"\n").wind(2).ws(
"*cursor = token + offset;");
160 o.ws(
"\n").wind(2).ws(
"return 0;");
161 o.ws(
"\n").wind(1).ws(
"} else {");
162 o.ws(
"\n").wind(2).ws(
"fprintf");
163 o.ws(
"\n").wind(3).ws(
"( stderr");
164 o.ws(
"\n").wind(3).ws(
", \"error: lex_").wstring(
name).ws(
": at position %ld (iteration %u):\\n\"");
165 o.ws(
"\n").wind(4).ws(
"\"\\texpected: match length %ld, rule %u\\n\"");
166 o.ws(
"\n").wind(4).ws(
"\"\\tactual: match length %ld, rule %u\\n\"");
167 o.ws(
"\n").wind(3).ws(
", pos");
168 o.ws(
"\n").wind(3).ws(
", i");
169 o.ws(
"\n").wind(3).ws(
", len_exp");
170 o.ws(
"\n").wind(3).ws(
", rule_exp");
171 o.ws(
"\n").wind(3).ws(
", len_act");
172 o.ws(
"\n").wind(3).ws(
", rule_act");
173 o.ws(
"\n").wind(3).ws(
");");
174 o.ws(
"\n").wind(2).ws(
"return 1;");
175 o.ws(
"\n").wind(1).ws(
"}");
178 o.ws(
"\nint lex_").wstring(
name).ws(
"()");
180 o.ws(
"\n").wind(1).ws(
"const size_t padding = ").wu64(maxfill).ws(
"; /* YYMAXFILL */");
181 o.ws(
"\n").wind(1).ws(
"int status = 0;");
182 o.ws(
"\n").wind(1).ws(
"size_t input_len = 0;");
183 o.ws(
"\n").wind(1).ws(
"size_t keys_count = 0;");
184 o.ws(
"\n").wind(1).ws(
"YYCTYPE *input = NULL;");
185 o.ws(
"\n").wind(1).ws(
"YYKEYTYPE *keys = NULL;");
186 o.ws(
"\n").wind(1).ws(
"const YYCTYPE *cursor = NULL;");
187 o.ws(
"\n").wind(1).ws(
"const YYCTYPE *limit = NULL;");
188 o.ws(
"\n").wind(1).ws(
"const YYCTYPE *token = NULL;");
189 o.ws(
"\n").wind(1).ws(
"const YYCTYPE *eof = NULL;");
190 o.ws(
"\n").wind(1).ws(
"unsigned int i = 0;");
192 o.ws(
"\n").wind(1).ws(
"input = (YYCTYPE *) read_file");
193 o.ws(
"\n").wind(2).ws(
"(\"").wstring(o.file_name).ws(
".").wstring(
name).ws(
".input\"");
194 o.ws(
"\n").wind(2).ws(
", sizeof (YYCTYPE)");
195 o.ws(
"\n").wind(2).ws(
", padding");
196 o.ws(
"\n").wind(2).ws(
", &input_len");
197 o.ws(
"\n").wind(2).ws(
");");
198 o.ws(
"\n").wind(1).ws(
"if (input == NULL) {");
199 o.ws(
"\n").wind(2).ws(
"status = 1;");
200 o.ws(
"\n").wind(2).ws(
"goto end;");
201 o.ws(
"\n").wind(1).ws(
"}");
203 if (sizeof_cunit > 1)
205 o.ws(
"\n").wind(1).ws(
"for (i = 0; i < input_len; ++i) {");
206 from_le(o, 2, sizeof_cunit,
"input[i]");
207 o.ws(
"\n").wind(1).ws(
"}");
210 o.ws(
"\n").wind(1).ws(
"keys = (YYKEYTYPE *) read_file");
211 o.ws(
"\n").wind(2).ws(
"(\"").wstring(o.file_name).ws(
".").wstring(
name).ws(
".keys\"");
212 o.ws(
"\n").wind(2).ws(
", 3 * sizeof (YYKEYTYPE)");
213 o.ws(
"\n").wind(2).ws(
", 0");
214 o.ws(
"\n").wind(2).ws(
", &keys_count");
215 o.ws(
"\n").wind(2).ws(
");");
216 o.ws(
"\n").wind(1).ws(
"if (keys == NULL) {");
217 o.ws(
"\n").wind(2).ws(
"status = 1;");
218 o.ws(
"\n").wind(2).ws(
"goto end;");
219 o.ws(
"\n").wind(1).ws(
"}");
223 o.ws(
"\n").wind(1).ws(
"for (i = 0; i < 3 * keys_count; ++i) {");
225 o.ws(
"\n").wind(1).ws(
"}");
228 o.ws(
"\n").wind(1).ws(
"cursor = input;");
229 o.ws(
"\n").wind(1).ws(
"limit = input + input_len + padding;");
230 o.ws(
"\n").wind(1).ws(
"eof = input + input_len;");
232 o.ws(
"\n").wind(1).ws(
"for (i = 0; status == 0 && i < keys_count; ++i) {");
233 o.ws(
"\n").wind(2).ws(
"token = cursor;");
236 o.ws(
"\n").wind(2).ws(
"const YYCTYPE *marker = NULL;");
240 o.ws(
"\n").wind(2).ws(
"const YYCTYPE *ctxmarker = NULL;");
242 o.ws(
"\n").wind(2).ws(
"YYCTYPE yych;");
245 o.ws(
"\n").wind(2).ws(
"unsigned int yyaccept = 0;");
static void from_le(OutputFile &o, uint32_t ind, size_t size, const char *expr)
static key_t rule2key(rule_rank_t r)
static void gen(OutputFile &, uint32_t ind, uint32_t, uint32_t)
static void exact_uint(OutputFile &o, size_t width)
uint32_t szCodeUnit() const
uint32_t nCodeUnits() const
static rule_rank_t none()
template<typename key_t >
Definition at line 160 of file skeleton.h.
163 return std::numeric_limits<key_t>::max();
164 }
else if (r.is_def()) {
165 key_t k = std::numeric_limits<key_t>::max();
168 return static_cast<key_t
>(r.uint32());
uint32_t re2c::Skeleton::rule2key |
( |
rule_rank_t |
r | ) |
const |
Definition at line 152 of file skeleton.cc.
157 case 4:
return rule2key<uint32_t> (r);
158 case 2:
return rule2key<uint16_t> (r);
159 case 1:
return rule2key<uint8_t> (r);
void re2c::Skeleton::warn_match_empty |
( |
| ) |
|
Definition at line 14 of file match_empty.cc.
16 Node & head =
nodes[0];
19 const std::set<rule_t> & reach = head.reachable;
22 if (!head.rule.rank.is_none ())
24 bool reachable = head.end ();
25 for (std::set<rule_t>::const_iterator i = reach.begin ();
26 !reachable && i != reach.end (); ++i)
28 reachable |= i->rank.is_none ();
39 for (std::set<rule_t>::const_iterator i = reach.begin (); i != reach.end (); ++i)
void match_empty_string(uint32_t line)
void re2c::Skeleton::warn_undefined_control_flow |
( |
| ) |
|
Definition at line 43 of file control_flow.cc.
46 std::vector<way_t> ways;
55 else if (size.overflow ())
static u32lim_t from32(uint32_t x)
void undefined_control_flow(uint32_t line, const std::string &cond, std::vector< way_t > &ways, bool overflow)
u32lim_t< 1024 > nakeds_t
std::vector< const way_arc_t * > way_t
void fail(type_t t, uint32_t line, const char *s)
void naked_ways(way_t &prefix, std::vector< way_t > &ways, nakeds_t &size)
void re2c::Skeleton::warn_unreachable_rules |
( |
| ) |
|
Definition at line 37 of file unreachable.cc.
44 for (std::set<rule_t>::const_iterator j = rs.begin (); j != rs.end (); ++j)
46 const rule_rank_t r2 = j->rank;
47 if (r1 == r2 || r2.is_none ())
49 rules[r1].reachable =
true;
53 rules[r1].shadow.insert (r2);
63 for (rules_t::const_iterator i =
rules.begin (); i !=
rules.end (); ++i)
65 const rule_rank_t r = i->first;
66 if (!r.is_none () && !r.is_def () && !
rules[r].reachable)
std::set< rule_t > reachable
void unreachable_rule(const std::string &cond, const rule_info_t &rule, const rules_t &rules)
const std::string re2c::Skeleton::cond |
const uint32_t re2c::Skeleton::line |
const std::string re2c::Skeleton::name |
Node* re2c::Skeleton::nodes |
const size_t re2c::Skeleton::nodes_count |
size_t re2c::Skeleton::sizeof_key |
The documentation for this struct was generated from the following files: