From c2fded35db879fbe6279039b0751b02d3a64a1a0 Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Thu, 3 Sep 2015 15:18:45 +0100 Subject: [PATCH] Changed '.keys' file format (generated with '--skeleton'). Store length of strings instead of pointers to string start and end. Storing pointers requires us to remember total length of strings already written to file by the time we want to write next string. This is very inconvenient if we want to dump strings as we perform DFS on graph: we'll have to track size all the time (path-cover-generator already does that, but all-paths-generator doesn't). This adds a new local variable to the generated code ('token'), which is used to backup cursor position when enterind DFA. --- re2c/src/codegen/emit_action.cc | 8 ++++---- re2c/src/codegen/skeleton/generate_code.cc | 1 + re2c/src/codegen/skeleton/generate_data.cc | 13 +++++-------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/re2c/src/codegen/emit_action.cc b/re2c/src/codegen/emit_action.cc index ea4a9ca0..91bc76a8 100644 --- a/re2c/src/codegen/emit_action.cc +++ b/re2c/src/codegen/emit_action.cc @@ -251,12 +251,12 @@ void emit_rule (OutputFile & o, uint32_t ind, const State * const s, const RuleO if (flag_skeleton) { o << indent (ind) - << "{ if (cursor == &data[result[i].endpos] && result[i].rule == " << rule->rank << ") " - << "{ cursor = &data[result[i].startpos]; continue; }" + << "{ if (cursor == token + result[i].len_matching && result[i].match == " << rule->rank << ") " + << "{ cursor = token + result[i].len; continue; }" << " else " - << "{ printf (\"error: %lu/%u, %u/%u, '%s'\\n\", cursor - data, result[i].endpos, result[i].rule, " + << "{ printf (\"error: %ld/%lu, %u/%u, '%s'\\n\", cursor - token, result[i].len_matching, result[i].match, " << rule->rank - << ", &data[result[i].startpos]); return 1; } }\n"; + << ", token); return 1; } }\n"; } else { diff --git a/re2c/src/codegen/skeleton/generate_code.cc b/re2c/src/codegen/skeleton/generate_code.cc index a1286027..b077417e 100644 --- a/re2c/src/codegen/skeleton/generate_code.cc +++ b/re2c/src/codegen/skeleton/generate_code.cc @@ -13,6 +13,7 @@ void emit_prolog (OutputFile & o, uint32_t ind) o << indent (ind) << "{\n"; o << indent (ind + 1) << "for (unsigned int i = 0; i < count; ++i)\n"; o << indent (ind + 1) << "{\n"; + o << indent (ind + 2) << "const YYCTYPE * token = cursor;\n"; } void emit_epilog (OutputFile & o, uint32_t ind) diff --git a/re2c/src/codegen/skeleton/generate_data.cc b/re2c/src/codegen/skeleton/generate_data.cc index 268bc215..6a8b9fff 100644 --- a/re2c/src/codegen/skeleton/generate_data.cc +++ b/re2c/src/codegen/skeleton/generate_data.cc @@ -279,21 +279,18 @@ void Skeleton::emit_keys (const char * fname, const std::vector & paths) exit (1); } - size_t pos = 0; keys << "struct Result {\n"; - keys << indent (1) << "unsigned int endpos;\n"; - keys << indent (1) << "unsigned int startpos;\n"; - keys << indent (1) << "unsigned int rule;\n"; - keys << indent (1) << "Result (unsigned int e, unsigned int s, unsigned int r) : endpos (e), startpos (s), rule (r) {}\n"; + keys << indent (1) << "size_t len;\n"; + keys << indent (1) << "size_t len_matching;\n"; + keys << indent (1) << "unsigned int match;\n"; + keys << indent (1) << "Result (size_t n, size_t m, unsigned int r) : len (n), len_matching (m), match (r) {}\n"; keys << "};\n"; keys << "Result result [] =\n"; keys << "{\n"; const size_t count = paths.size (); for (size_t i = 0; i < count; ++i) { - const size_t new_pos = pos + paths[i].len (); - keys << indent (1) << "Result (" << pos + paths[i].len_matching () << "," << new_pos << "," << paths[i].match () << "),\n"; - pos = new_pos; + keys << indent (1) << "Result (" << paths[i].len () << "," << paths[i].len_matching () << "," << paths[i].match () << "),\n"; } keys << "};\n"; -- 2.40.0