8000 Fix parser shift/reduce conflicts by Morriar · Pull Request #4178 · sorbet/sorbet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix parser shift/reduce conflicts #4178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions third_party/parser/cc/grammars/typedruby.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ using namespace std::string_literals;
tANDDOT 1142 "&."
tRATIONAL_IMAGINARY 1143
tFLOAT_IMAGINARY 1144
tBDOT2 1145 // Used to avoid shift/reduce conflicts with productions using `tDOT2`
tBDOT3 1146 // Used to avoid shift/reduce conflicts with productions using `tDOT3`
;

%type <node>
Expand Down Expand Up @@ -388,7 +390,7 @@ using namespace std::string_literals;
%right tEQL tOP_ASGN
%left kRESCUE_MOD
%right tEH tCOLON
%nonassoc tDOT2 tDOT3
%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
%left tOROP
%left tANDOP
%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
Expand Down Expand Up @@ -432,14 +434,22 @@ namespace typedruby27 {
} while(false);

void parser::error(const std::string &msg) {
std::string error_message = msg;

int token_type = static_cast<int>(driver.last_token->type());
const char* token_str_name = yytname_[yytranslate_(token_type)];

if (token_str_name != nullptr) {
error_message = token_str_name;
}
std::string error_message = msg;

int token_type = static_cast<int>(driver.last_token->type());
const char* token_str_name = yytname_[yytranslate_(token_type)];

if (token_type == token_type::tBDOT2) {
// Because we have two tokens matching `..`, we can't set the token name as `..` directly.
// This hack allows to display the real token string instead of `tBDOT2` in parsing errors.
error_message = "\"..\"";
} else if (token_type == token_type::tBDOT3) {
// Because we have two tokens matching `...`, we can't set the token name as `...` directly.
// This hack allows to display the real token string instead of `tBDOT3` in parsing errors.
error_message = "\"...\"";
} else if (token_str_name != nullptr) {
error_message = token_str_name;
}

driver.diagnostics.emplace_back(
dlevel::ERROR, dclass::UnexpectedToken,
Expand Down Expand Up @@ -714,8 +724,7 @@ int yylex(parser::semantic_type *lval, ruby_parser::typedruby27 &driver) {
driver.lex.in_kwarg = $<boolean>3;
$$ = driver.build.in_match(self, $1, $2, $4);
}
| arg tLBRACE_ARG
| arg
| arg %prec tLBRACE_ARG

expr_value: expr

Expand Down Expand Up @@ -1127,11 +1136,11 @@ int yylex(parser::semantic_type *lval, ruby_parser::typedruby27 &driver) {
{
$$ = driver.build.range_exclusive(self, $1, $2, nullptr);
}
| tDOT2 arg
| tBDOT2 arg
{
$$ = driver.build.range_inclusive(self, nullptr, $1, $2);
}
| tDOT3 arg
| tBDOT3 arg
{
$$ = driver.build.range_exclusive(self, nullptr, $1, $2);
}
Expand Down Expand Up @@ -2619,11 +2628,11 @@ opt_block_args_tail:
| p_variable
| p_var_ref
| p_const
| tDOT2 p_primitive
| tBDOT2 p_primitive
{
$$ = driver.build.range_inclusive(self, nullptr, $1, $2);
}
| tDOT3 p_primitive
| tBDOT3 p_primitive
{
$$ = driver.build.range_exclusive(self, nullptr, $1, $2);
}
Expand Down Expand Up @@ -3225,7 +3234,7 @@ keyword_variable: kNIL
{
$$ = driver.alloc.node_list();
}
args_forward: tDOT3
args_forward: tBDOT3
{
$$ = $1;
}
Expand Down
27 changes: 27 additions & 0 deletions third_party/parser/cc/lexer.rl
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,33 @@ void lexer::set_state_expr_value() {
fbreak;
};

#
# RUBY 2.7 BEGINLESS RANGE

'..'
=> {
auto ident = tok(ts, te - 2);
if (version >= ruby_version::RUBY_27) {
emit(token_type::tBDOT2, ident, ts, te);
} else {
emit(token_type::tDOT2, ident, ts, te);
}

fnext expr_beg; fbreak;
};

'...'
=> {
auto ident = tok(ts, te - 2);
if (version >= ruby_version::RUBY_27) {
emit(token_type::tBDOT3, ident, ts, te);
} else {
emit(token_type::tDOT3, ident, ts, te);
}

fnext expr_beg; fbreak;
};

#
# CONTEXT-DEPENDENT VARIABLE LOOKUP OR COMMAND INVOCATION
#
Expand Down
4 changes: 3 additions & 1 deletion third_party/parser/include/ruby_parser/token.hh
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@
XX(tLABEL_END, 1141) \
XX(tANDDOT, 1142) \
XX(tRATIONAL_IMAGINARY, 1143) \
XX(tFLOAT_IMAGINARY, 1144)
XX(tFLOAT_IMAGINARY, 1144) \
XX(tBDOT2, 1145) \
XX(tBDOT3, 1146)

namespace ruby_parser {
enum class token_type : int {
Expand Down
0