[poincare] More elegant prevention of parsing "3=4>x" and "4<x=3"

This commit is contained in:
Léa Saviot
2018-11-09 16:48:52 +01:00
committed by Émilie Feral
parent d411ff292e
commit fb484bf941
2 changed files with 9 additions and 6 deletions

View File

@@ -15,8 +15,8 @@ Expression Parser::parseUntil(Token::Type stoppingType) {
typedef void (Parser::*TokenParser)(Expression & leftHandSide);
static constexpr TokenParser tokenParsers[] = {
&Parser::parseUnexpected, // Token::EndOfStream
&Parser::parseEqual, // Token::Equal
&Parser::parseStore, // Token::Store
&Parser::parseEqual, // Token::Equal
&Parser::parseUnexpected, // Token::RightBracket
&Parser::parseUnexpected, // Token::RightParenthesis
&Parser::parseUnexpected, // Token::RightBrace
@@ -170,10 +170,8 @@ void Parser::parseEqual(Expression & leftHandSide) {
}
Expression rightHandSide;
if (parseBinaryOperator(leftHandSide, rightHandSide, Token::Equal)) {
if (rightHandSide.type() == ExpressionNode::Type::Store) {
m_status = Status::Error; // Equal cannot have a Store on the right
return;
}
/* We parse until finding a token of lesser precedence than Equal. The next
* token is thus either EndOfStream or Store. */
leftHandSide = Equal(leftHandSide, rightHandSide);
}
if (!m_nextToken.is(Token::EndOfStream)) {

View File

@@ -17,8 +17,13 @@ public:
enum Type {
// Ordered from lower to higher precedence to make Parser's job easier
EndOfStream = 0, // Must be the first
Equal,
Store,
Equal,
/* Equal should have a higher precedence than Store, because
* Tokenizer::parseEqual looks for a right hand side until it finds a
* token of lesser precedence than Equal, and this prevents expressions
* such as "3=4>a". Tokenizer::parseStore uses a special algorithm that
* prevents (3>4=a). */
RightBracket,
RightParenthesis,
RightBrace,