From: Ulya Trofimovich Date: Wed, 21 Dec 2016 23:14:17 +0000 (+0000) Subject: Parse rules in a uniform way regardless of the '-c' option. X-Git-Tag: 1.0~39^2~175 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f1ffda133aef6d65fcb2e04957f179332ab9df3b;p=re2c Parse rules in a uniform way regardless of the '-c' option. --- diff --git a/re2c/bootstrap/src/parse/lex.cc b/re2c/bootstrap/src/parse/lex.cc index 8e83ed28..d356a016 100644 --- a/re2c/bootstrap/src/parse/lex.cc +++ b/re2c/bootstrap/src/parse/lex.cc @@ -1,4 +1,4 @@ -/* Generated by re2c 0.16 on Wed Dec 21 22:10:36 2016 */ +/* Generated by re2c 0.16 on Wed Dec 21 23:11:42 2016 */ #line 1 "../src/parse/lex.re" #include "src/util/c99_stdint.h" #include diff --git a/re2c/bootstrap/src/parse/parser.cc b/re2c/bootstrap/src/parse/parser.cc index 721188e9..6224423a 100644 --- a/re2c/bootstrap/src/parse/parser.cc +++ b/re2c/bootstrap/src/parse/parser.cc @@ -111,7 +111,6 @@ void yyerror(Scanner &in, const char*); static std::vector condnames; static re2c::SpecMap specMap; -static Spec spec; static SetupMap ruleSetupMap; static bool foundRules; static symbol_table_t symbol_table; @@ -160,8 +159,11 @@ static void delay_default(Spec &spec) void context_check(Scanner &in, CondList *clist) { - if (!in.opts->cFlag) - { + if (clist && clist->size() == 1 && clist->begin()->empty()) { + if (in.opts->cFlag) { + in.fatal("condition or '<*>' required when using -c switch"); + } + } else if (!in.opts->cFlag) { delete clist; in.fatal("conditions are only allowed when using -c switch"); } @@ -170,7 +172,7 @@ void context_check(Scanner &in, CondList *clist) static void add_cond(std::vector &names, const std::string &name, re2c::SpecMap &specs) { - if (name != "*" && specs.find(name) == specs.end()) { + if (name != "" && name != "*" && specs.find(name) == specs.end()) { names.push_back(name); } } @@ -582,8 +584,8 @@ static const yytype_int8 yyrhs[] = /* YYRLINE[YYN] -- source line where rule number YYN was defined. */ static const yytype_uint16 yyrline[] = { - 0, 190, 190, 192, 193, 194, 199, 206, 211, 214, - 218, 218, 221, 229, 233, 238, 245, 253, 258, 264, + 0, 192, 192, 194, 195, 196, 201, 208, 213, 216, + 220, 220, 223, 229, 233, 238, 245, 253, 258, 264, 271, 272, 277, 280, 287, 291, 296, 301, 305, 312, 316, 323, 327, 334, 338, 355, 374, 378, 382, 386, 393, 403, 407 @@ -1583,11 +1585,9 @@ yyreduce: case 12: { - if (in.opts->cFlag) { - in.fatal("condition or '<*>' required when using -c switch"); - } - (yyvsp[(1) - (2)].rule)->info = new RuleInfo((yyvsp[(2) - (2)].code)->loc, (yyvsp[(2) - (2)].code), NULL); - spec.push_back((yyvsp[(1) - (2)].rule)); + CondList *cl = new CondList; + cl->insert(""); + context_rule(in, cl, (yyvsp[(2) - (2)].code)->loc, (yyvsp[(1) - (2)].rule), (yyvsp[(2) - (2)].code), NULL); ;} break; @@ -2106,9 +2106,9 @@ void parse(Scanner &in, Output & o) } else { + specMap.clear(); dfa_map.clear(); } - spec.clear (); in.set_in_parse(true); yyparse(in); in.set_in_parse(false); @@ -2122,7 +2122,7 @@ void parse(Scanner &in, Output & o) in.reuse(); dfa_map.clear(); parse_cleanup(); - spec.clear (); + specMap.clear(); in.set_in_parse(true); yyparse(in); in.set_in_parse(false); @@ -2138,89 +2138,60 @@ void parse(Scanner &in, Output & o) } o.source.block().line = in.get_cline(); uint32_t ind = opts->topIndent; - if (opts->cFlag) - { - SpecMap::iterator it; - for (it = specMap.begin(); it != specMap.end(); ++it) { - check(it->second, it->first); - } + SpecMap::iterator it; - Spec star; - if ((it = specMap.find("*")) != specMap.end()) { - star = it->second; - specMap.erase(it); - } - - if (mode != Scanner::Reuse) - { - // merge <*> rules to all conditions except "0" with lowest priority - for (it = specMap.begin(); it != specMap.end(); ++it) { - if (it->first == "0") continue; - for (size_t j = 0; j < star.size(); ++j) { - it->second.push_back(star[j]); - } - } - } - o.source.block().types = condnames; + for (it = specMap.begin(); it != specMap.end(); ++it) { + check(it->second, it->first); + } - size_t nCount = specMap.size(); + Spec star; + if ((it = specMap.find("*")) != specMap.end()) { + star = it->second; + specMap.erase(it); + } - for (it = specMap.begin(); it != specMap.end(); ++it) - { - delay_default(it->second); - if (mode != Scanner::Reuse) - { - o.source.block().setup_rule = find_setup_rule(ruleSetupMap, it->first); - dfa_map[it->first] = compile(it->second, o, it->first, opts->encoding.nCodeUnits ()); - } - if (mode != Scanner::Rules && dfa_map.find(it->first) != dfa_map.end()) - { - dfa_map[it->first]->emit(o, ind, !--nCount, bPrologBrace); + if (mode != Scanner::Reuse) { + // merge <*> rules to all conditions except "0" with lowest priority + for (it = specMap.begin(); it != specMap.end(); ++it) { + if (it->first == "0") continue; + for (size_t j = 0; j < star.size(); ++j) { + it->second.push_back(star[j]); } } } - else - { - check(spec, ""); - delay_default(spec); - if (!spec.empty() || !dfa_map.empty()) - { - if (mode != Scanner::Reuse) - { - dfa_map[""] = compile(spec, o, "", opts->encoding.nCodeUnits ()); - } - if (mode != Scanner::Rules && dfa_map.find("") != dfa_map.end()) - { - dfa_map[""]->emit(o, ind, 0, bPrologBrace); - } + o.source.block().types = condnames; + + size_t nCount = specMap.size(); + for (it = specMap.begin(); it != specMap.end(); ++it) { + delay_default(it->second); + if (mode != Scanner::Reuse) { + o.source.block().setup_rule = find_setup_rule(ruleSetupMap, it->first); + dfa_map[it->first] = compile(it->second, o, it->first, opts->encoding.nCodeUnits ()); + } + if (mode != Scanner::Rules && dfa_map.find(it->first) != dfa_map.end()) { + dfa_map[it->first]->emit(o, ind, !--nCount, bPrologBrace); } } + o.source.wline_info (in.get_cline (), in.get_fname ().c_str ()); /* restore original char handling mode*/ opts.reset_encoding (encodingOld); } - if (opts->cFlag) - { - SetupMap::const_iterator itRuleSetup; - for (itRuleSetup = ruleSetupMap.begin(); itRuleSetup != ruleSetupMap.end(); ++itRuleSetup) - { - if (itRuleSetup->first != "*" && specMap.find(itRuleSetup->first) == specMap.end()) - { - in.fatalf_at(itRuleSetup->second.first, "setup for non existing rule '%s' found", itRuleSetup->first.c_str()); - } + SetupMap::const_iterator itRuleSetup; + for (itRuleSetup = ruleSetupMap.begin(); itRuleSetup != ruleSetupMap.end(); ++itRuleSetup) { + if (itRuleSetup->first != "*" && specMap.find(itRuleSetup->first) == specMap.end()) { + in.fatalf_at(itRuleSetup->second.first, "setup for non existing rule '%s' found", itRuleSetup->first.c_str()); } - if (specMap.size() < ruleSetupMap.size()) - { - uint32_t line = in.get_cline(); - itRuleSetup = ruleSetupMap.find("*"); - if (itRuleSetup != ruleSetupMap.end()) - { - line = itRuleSetup->second.first; - } - in.fatalf_at(line, "setup for all rules with '*' not possible when all rules are setup explicitly"); + } + if (specMap.size() < ruleSetupMap.size()) { + uint32_t line = in.get_cline(); + itRuleSetup = ruleSetupMap.find("*"); + if (itRuleSetup != ruleSetupMap.end()) { + line = itRuleSetup->second.first; } + in.fatalf_at(line, "setup for all rules with '*' not possible when all rules are setup explicitly"); } if (opts->target == opt_t::SKELETON) diff --git a/re2c/src/parse/parser.ypp b/re2c/src/parse/parser.ypp index 4739ad11..0999cc2c 100644 --- a/re2c/src/parse/parser.ypp +++ b/re2c/src/parse/parser.ypp @@ -43,7 +43,6 @@ void yyerror(Scanner &in, const char*); static std::vector condnames; static re2c::SpecMap specMap; -static Spec spec; static SetupMap ruleSetupMap; static bool foundRules; static symbol_table_t symbol_table; @@ -92,8 +91,11 @@ static void delay_default(Spec &spec) void context_check(Scanner &in, CondList *clist) { - if (!in.opts->cFlag) - { + if (clist && clist->size() == 1 && clist->begin()->empty()) { + if (in.opts->cFlag) { + in.fatal("condition or '<*>' required when using -c switch"); + } + } else if (!in.opts->cFlag) { delete clist; in.fatal("conditions are only allowed when using -c switch"); } @@ -102,7 +104,7 @@ void context_check(Scanner &in, CondList *clist) static void add_cond(std::vector &names, const std::string &name, re2c::SpecMap &specs) { - if (name != "*" && specs.find(name) == specs.end()) { + if (name != "" && name != "*" && specs.find(name) == specs.end()) { names.push_back(name); } } @@ -219,11 +221,9 @@ enddef: ';' | TOKEN_FID_END; rule : trailexpr TOKEN_CODE { - if (in.opts->cFlag) { - in.fatal("condition or '<*>' required when using -c switch"); - } - $1->info = new RuleInfo($2->loc, $2, NULL); - spec.push_back($1); + CondList *cl = new CondList; + cl->insert(""); + context_rule(in, cl, $2->loc, $1, $2, NULL); } | '<' clist '>' trailexpr newcond TOKEN_CODE { @@ -468,9 +468,9 @@ void parse(Scanner &in, Output & o) } else { + specMap.clear(); dfa_map.clear(); } - spec.clear (); in.set_in_parse(true); yyparse(in); in.set_in_parse(false); @@ -484,7 +484,7 @@ void parse(Scanner &in, Output & o) in.reuse(); dfa_map.clear(); parse_cleanup(); - spec.clear (); + specMap.clear(); in.set_in_parse(true); yyparse(in); in.set_in_parse(false); @@ -500,89 +500,60 @@ void parse(Scanner &in, Output & o) } o.source.block().line = in.get_cline(); uint32_t ind = opts->topIndent; - if (opts->cFlag) - { - SpecMap::iterator it; - for (it = specMap.begin(); it != specMap.end(); ++it) { - check(it->second, it->first); - } + SpecMap::iterator it; - Spec star; - if ((it = specMap.find("*")) != specMap.end()) { - star = it->second; - specMap.erase(it); - } - - if (mode != Scanner::Reuse) - { - // merge <*> rules to all conditions except "0" with lowest priority - for (it = specMap.begin(); it != specMap.end(); ++it) { - if (it->first == "0") continue; - for (size_t j = 0; j < star.size(); ++j) { - it->second.push_back(star[j]); - } - } - } - o.source.block().types = condnames; + for (it = specMap.begin(); it != specMap.end(); ++it) { + check(it->second, it->first); + } - size_t nCount = specMap.size(); + Spec star; + if ((it = specMap.find("*")) != specMap.end()) { + star = it->second; + specMap.erase(it); + } - for (it = specMap.begin(); it != specMap.end(); ++it) - { - delay_default(it->second); - if (mode != Scanner::Reuse) - { - o.source.block().setup_rule = find_setup_rule(ruleSetupMap, it->first); - dfa_map[it->first] = compile(it->second, o, it->first, opts->encoding.nCodeUnits ()); - } - if (mode != Scanner::Rules && dfa_map.find(it->first) != dfa_map.end()) - { - dfa_map[it->first]->emit(o, ind, !--nCount, bPrologBrace); + if (mode != Scanner::Reuse) { + // merge <*> rules to all conditions except "0" with lowest priority + for (it = specMap.begin(); it != specMap.end(); ++it) { + if (it->first == "0") continue; + for (size_t j = 0; j < star.size(); ++j) { + it->second.push_back(star[j]); } } } - else - { - check(spec, ""); - delay_default(spec); - if (!spec.empty() || !dfa_map.empty()) - { - if (mode != Scanner::Reuse) - { - dfa_map[""] = compile(spec, o, "", opts->encoding.nCodeUnits ()); - } - if (mode != Scanner::Rules && dfa_map.find("") != dfa_map.end()) - { - dfa_map[""]->emit(o, ind, 0, bPrologBrace); - } + o.source.block().types = condnames; + + size_t nCount = specMap.size(); + for (it = specMap.begin(); it != specMap.end(); ++it) { + delay_default(it->second); + if (mode != Scanner::Reuse) { + o.source.block().setup_rule = find_setup_rule(ruleSetupMap, it->first); + dfa_map[it->first] = compile(it->second, o, it->first, opts->encoding.nCodeUnits ()); + } + if (mode != Scanner::Rules && dfa_map.find(it->first) != dfa_map.end()) { + dfa_map[it->first]->emit(o, ind, !--nCount, bPrologBrace); } } + o.source.wline_info (in.get_cline (), in.get_fname ().c_str ()); /* restore original char handling mode*/ opts.reset_encoding (encodingOld); } - if (opts->cFlag) - { - SetupMap::const_iterator itRuleSetup; - for (itRuleSetup = ruleSetupMap.begin(); itRuleSetup != ruleSetupMap.end(); ++itRuleSetup) - { - if (itRuleSetup->first != "*" && specMap.find(itRuleSetup->first) == specMap.end()) - { - in.fatalf_at(itRuleSetup->second.first, "setup for non existing rule '%s' found", itRuleSetup->first.c_str()); - } + SetupMap::const_iterator itRuleSetup; + for (itRuleSetup = ruleSetupMap.begin(); itRuleSetup != ruleSetupMap.end(); ++itRuleSetup) { + if (itRuleSetup->first != "*" && specMap.find(itRuleSetup->first) == specMap.end()) { + in.fatalf_at(itRuleSetup->second.first, "setup for non existing rule '%s' found", itRuleSetup->first.c_str()); } - if (specMap.size() < ruleSetupMap.size()) - { - uint32_t line = in.get_cline(); - itRuleSetup = ruleSetupMap.find("*"); - if (itRuleSetup != ruleSetupMap.end()) - { - line = itRuleSetup->second.first; - } - in.fatalf_at(line, "setup for all rules with '*' not possible when all rules are setup explicitly"); + } + if (specMap.size() < ruleSetupMap.size()) { + uint32_t line = in.get_cline(); + itRuleSetup = ruleSetupMap.find("*"); + if (itRuleSetup != ruleSetupMap.end()) { + line = itRuleSetup->second.first; } + in.fatalf_at(line, "setup for all rules with '*' not possible when all rules are setup explicitly"); } if (opts->target == opt_t::SKELETON)