Skip to content

Commit

Permalink
lemon++ - custom shift functions when shifting the error symbol (so t…
Browse files Browse the repository at this point in the history
…he token doesn't leak). Normal lemon not affected.
  • Loading branch information
ksherlock committed Jan 1, 2019
1 parent 776197a commit 620f5c4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
40 changes: 39 additions & 1 deletion lempar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,44 @@ static void yy_shift(
yyTraceShift(yypParser, yyNewState, "Shift");
}

#ifdef YYERRORSYMBOL
static void yy_shift_error(
yyParser *yypParser, /* The parser to be shifted */
YYACTIONTYPE yyNewState, /* The new state to shift in */
){
yyStackEntry *yytos;
yypParser->yytos++;
#ifdef YYTRACKMAXSTACKDEPTH
if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
yypParser->yyhwm++;
assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
}
#endif
#if YYSTACKDEPTH>0
if( yypParser->yytos>yypParser->yystackEnd ){
yypParser->yytos--;
yyStackOverflow(yypParser);
return;
}
#else
if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
if( yyGrowStack(yypParser) ){
yypParser->yytos--;
yyStackOverflow(yypParser);
return;
}
}
#endif
if( yyNewState > YY_MAX_SHIFT ){
yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
}
yytos = yypParser->yytos;
yytos->stateno = yyNewState;
yytos->major = YYERRORSYMBOL;
yytos->minor.YYERRSYMDT = 0;
}
#endif

/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
** of that rule */
static const YYCODETYPE yyRuleInfoLhs[] = {
Expand Down Expand Up @@ -1147,7 +1185,7 @@ void Parse(
#endif
yymajor = YYNOCODE;
}else if( yymx!=YYERRORSYMBOL ){
yy_shift(yypParser,yyact,YYERRORSYMBOL,std::forward<ParseTOKENTYPE>(yyminor));
yy_shift_error(yypParser,yyact);
}
}
yypParser->yyerrcnt = 3;
Expand Down
41 changes: 40 additions & 1 deletion lempar.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ class yypParser : public LEMON_SUPER {
YYACTIONTYPE yy_find_reduce_action(YYACTIONTYPE stateno, YYCODETYPE iLookAhead) const;

void yy_shift(YYACTIONTYPE yyNewState, YYCODETYPE yyMajor, ParseTOKENTYPE &&yypMinor);
#ifdef YYERRORSYMBOL
void yy_shift_error(YYACTIONTYPE yyNewState);
#endif
YYACTIONTYPE yy_reduce(unsigned int yyruleno, int yyLookahead, const ParseTOKENTYPE &yyLookaheadToken);
void yyStackOverflow();

Expand Down Expand Up @@ -826,6 +829,42 @@ void yypParser::yy_shift(
yyTraceShift(yyNewState, "Shift");
}

#ifdef YYERRORSYMBOL
void yypParser::yy_shift_error(
YYACTIONTYPE yyNewState, /* The new state to shift in */
){
yytos++;
#ifdef YYTRACKMAXSTACKDEPTH
if( yyidx()>yyhwm ){
yyhwm++;
assert(yyhwm == yyidx());
}
#endif
#if YYSTACKDEPTH>0
if( yytos>yystackEnd ){
yytos--;
yyStackOverflow();
return;
}
#else
if( yytos>=&yystack[yystksz] ){
if( yyGrowStack() ){
yytos--;
yyStackOverflow();
return;
}
}
#endif
if( yyNewState > YY_MAX_SHIFT ){
yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
}
yytos->stateno = yyNewState;
yytos->major = YYERRORSYMBOL;
yytos->minor.YYERRSYMDT = 0;
yyTraceShift(yyNewState, "Shift");
}
#endif

/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
** of that rule */
static const YYCODETYPE yyRuleInfoLhs[] = {
Expand Down Expand Up @@ -1115,7 +1154,7 @@ void yypParser::parse(
#endif
yymajor = YYNOCODE;
}else if( yymx!=YYERRORSYMBOL ){
yy_shift(yyact,YYERRORSYMBOL,std::move(yyminor));
yy_shift_error(yyact);
}
}
yyerrcnt = 3;
Expand Down

0 comments on commit 620f5c4

Please sign in to comment.