Add an error message for empty literal/regex declarations, also fix to accept "'\''" literal.

This commit is contained in:
mingodad 2023-06-22 15:04:47 +02:00 • committed by Charles Baker
parent 6ab3cd01d3
commit 031419b0cf
2 changed files with 19 additions and 0 deletions

View file

@ -12,6 +12,7 @@ enum ErrorCode
PARSER_ERROR_NONE, ///< No %error.
LALR_ERROR_SYNTAX, ///< Syntax %error occured while parsing input.
LALR_ERROR_UNTERMINATED_LITERAL, ///< Unterminated literal in an lalr grammar.
LALR_ERROR_EMPTY_LITERAL, ///< Empty literal in an lalr grammar.
LEXER_ERROR_MISSING_ACTION_HANDLER, ///< A lexer action hasn't been bound to a function.
LEXER_ERROR_SYNTAX, ///< Syntax %error occured while parsing some input.
LEXER_ERROR_SYMBOL_CONFLICT, ///< A lexer state matches more than one symbol.

View file

@ -228,12 +228,21 @@ bool GrammarParser::match_literal()
{
escaped = *position == '\\';
++position;
if ( *position == '\\' && escaped )
{
++position;
escaped = false;
}
}
if ( position == end_ || !is_new_line(position) )
{
lexeme_.assign( position_, position );
position_ = position;
expect( "'" );
if ( lexeme_.empty() )
{
error( LALR_ERROR_EMPTY_LITERAL, "empty literal" );
}
return true;
}
error( LALR_ERROR_UNTERMINATED_LITERAL, "unterminated literal" );
@ -253,10 +262,19 @@ bool GrammarParser::match_regex()
{
escaped = *position == '\\';
++position;
if (*position == '\\' && escaped)
{
++position;
escaped = false;
}
}
lexeme_.assign( position_, position );
position_ = position;
expect( "\"" );
if ( lexeme_.empty() )
{
error( LALR_ERROR_EMPTY_LITERAL, "empty regex" );
}
return true;
}
return false;