Correctly parse binary operations that can be interpreted as sign prefixes for numbers

This commit is contained in:
Jan 2021-11-28 15:16:43 +01:00
parent c3a44f60d3
commit e7eb43a955
12 changed files with 211 additions and 12 deletions

View File

@ -259,13 +259,15 @@ protected:
return floatingPointValue; return floatingPointValue;
} }
void ReadNumber(bool& isFloatingPoint, double& floatingPointValue, int& integerValue) void ReadNumber(bool& isFloatingPoint, bool& hasSignPrefix, double& floatingPointValue, int& integerValue)
{ {
const auto& currentLine = CurrentLine(); const auto& currentLine = CurrentLine();
assert(m_current_line_offset >= 1); assert(m_current_line_offset >= 1);
assert(isdigit(currentLine.m_line[m_current_line_offset - 1]) assert(isdigit(currentLine.m_line[m_current_line_offset - 1])
|| currentLine.m_line[m_current_line_offset - 1] == '.' || currentLine.m_line[m_current_line_offset - 1] == '.'
|| currentLine.m_line[m_current_line_offset - 1] == '+'
|| currentLine.m_line[m_current_line_offset - 1] == '-'); || currentLine.m_line[m_current_line_offset - 1] == '-');
hasSignPrefix = currentLine.m_line[m_current_line_offset - 1] == '+' || currentLine.m_line[m_current_line_offset - 1] == '-';
const auto lineLength = currentLine.m_line.size(); const auto lineLength = currentLine.m_line.size();
if (lineLength - m_current_line_offset >= 1 if (lineLength - m_current_line_offset >= 1
@ -341,9 +343,12 @@ public:
{ {
for (const auto& line : m_line_cache) for (const auto& line : m_line_cache)
{ {
if (*line.m_filename == pos.m_filename.get() if (line.m_filename
&& *line.m_filename == pos.m_filename.get()
&& line.m_line_number == pos.m_line) && line.m_line_number == pos.m_line)
{
return line; return line;
}
} }
return ParserLine(); return ParserLine();

View File

@ -1,5 +1,6 @@
#include "SimpleExpressionMatchers.h" #include "SimpleExpressionMatchers.h"
#include <algorithm>
#include <list> #include <list>
#include "SimpleExpressionConditionalOperator.h" #include "SimpleExpressionConditionalOperator.h"
@ -273,6 +274,30 @@ std::unique_ptr<SimpleExpressionMatchers::matcher_t> SimpleExpressionMatchers::P
} }
} }
const auto hasAddOperation = std::any_of(enabledBinaryOperations.begin(), enabledBinaryOperations.end(), [](const SimpleExpressionBinaryOperationType* type)
{
return type == &SimpleExpressionBinaryOperationType::OPERATION_ADD;
});
const auto hasSubtractOperation = std::any_of(enabledBinaryOperations.begin(), enabledBinaryOperations.end(), [](const SimpleExpressionBinaryOperationType* type)
{
return type == &SimpleExpressionBinaryOperationType::OPERATION_SUBTRACT;
});
if (hasAddOperation && hasSubtractOperation)
{
binaryOperationsMatchers.emplace_back(
create.Or({
create.IntegerWithSign(),
create.FloatingPointWithSign()
})
.NoConsume()
.Transform([](const SimpleMatcherFactory::token_list_t& values)
{
return SimpleParserValue::Integer(values[0].get().GetPos(), static_cast<int>(SimpleBinaryOperationId::ADD));
}));
}
return create.Or(std::move(binaryOperationsMatchers)).Capture(CAPTURE_BINARY_OPERATION_TYPE); return create.Or(std::move(binaryOperationsMatchers)).Capture(CAPTURE_BINARY_OPERATION_TYPE);
} }

View File

@ -7,6 +7,7 @@
#include "SimpleMatcherKeywordPrefix.h" #include "SimpleMatcherKeywordPrefix.h"
#include "SimpleMatcherMultiCharacter.h" #include "SimpleMatcherMultiCharacter.h"
#include "SimpleMatcherValueType.h" #include "SimpleMatcherValueType.h"
#include "SimpleMatcherValueTypeAndHasSignPrefix.h"
SimpleMatcherFactory::SimpleMatcherFactory(const IMatcherForLabelSupplier<SimpleParserValue>* labelSupplier) SimpleMatcherFactory::SimpleMatcherFactory(const IMatcherForLabelSupplier<SimpleParserValue>* labelSupplier)
: AbstractMatcherFactory(labelSupplier) : AbstractMatcherFactory(labelSupplier)
@ -48,11 +49,21 @@ MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::Integer() const
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherValueType>(SimpleParserValueType::INTEGER)); return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherValueType>(SimpleParserValueType::INTEGER));
} }
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::IntegerWithSign() const
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherValueTypeAndHasSignPrefix>(SimpleParserValueType::INTEGER, true));
}
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::FloatingPoint() const MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::FloatingPoint() const
{ {
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherValueType>(SimpleParserValueType::FLOATING_POINT)); return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherValueType>(SimpleParserValueType::FLOATING_POINT));
} }
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::FloatingPointWithSign() const
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherValueTypeAndHasSignPrefix>(SimpleParserValueType::FLOATING_POINT, true));
}
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::Char(char c) const MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::Char(char c) const
{ {
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherCharacter>(c)); return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherCharacter>(c));

View File

@ -17,7 +17,9 @@ public:
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Identifier() const; _NODISCARD MatcherFactoryWrapper<SimpleParserValue> Identifier() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> String() const; _NODISCARD MatcherFactoryWrapper<SimpleParserValue> String() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Integer() const; _NODISCARD MatcherFactoryWrapper<SimpleParserValue> Integer() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> IntegerWithSign() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> FloatingPoint() const; _NODISCARD MatcherFactoryWrapper<SimpleParserValue> FloatingPoint() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> FloatingPointWithSign() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Char(char c) const; _NODISCARD MatcherFactoryWrapper<SimpleParserValue> Char(char c) const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> MultiChar(int multiCharacterSequenceId) const; _NODISCARD MatcherFactoryWrapper<SimpleParserValue> MultiChar(int multiCharacterSequenceId) const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> AnyCharBesides(std::vector<char> chars) const; _NODISCARD MatcherFactoryWrapper<SimpleParserValue> AnyCharBesides(std::vector<char> chars) const;

View File

@ -0,0 +1,15 @@
#include "SimpleMatcherValueTypeAndHasSignPrefix.h"
SimpleMatcherValueTypeAndHasSignPrefix::SimpleMatcherValueTypeAndHasSignPrefix(const SimpleParserValueType type, bool hasSignPrefix)
: m_type(type),
m_has_sign_prefix(hasSignPrefix)
{
}
MatcherResult<SimpleParserValue> SimpleMatcherValueTypeAndHasSignPrefix::CanMatch(ILexer<SimpleParserValue>* lexer, const unsigned tokenOffset)
{
const auto& token = lexer->GetToken(tokenOffset);
return token.m_type == m_type && token.m_has_sign_prefix == m_has_sign_prefix
? MatcherResult<SimpleParserValue>::Match(1)
: MatcherResult<SimpleParserValue>::NoMatch();
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "Parsing/Simple/SimpleParserValue.h"
#include "Parsing/Matcher/AbstractMatcher.h"
class SimpleMatcherValueTypeAndHasSignPrefix final : public AbstractMatcher<SimpleParserValue>
{
SimpleParserValueType m_type;
bool m_has_sign_prefix;
protected:
MatcherResult<SimpleParserValue> CanMatch(ILexer<SimpleParserValue>* lexer, unsigned tokenOffset) override;
public:
explicit SimpleMatcherValueTypeAndHasSignPrefix(SimpleParserValueType type, bool hasSignPrefix);
};

View File

@ -120,18 +120,19 @@ SimpleParserValue SimpleLexer::GetNextToken()
if (m_config.m_read_strings && c == '\"') if (m_config.m_read_strings && c == '\"')
return SimpleParserValue::String(pos, new std::string(ReadString())); return SimpleParserValue::String(pos, new std::string(ReadString()));
if (m_config.m_read_numbers && (isdigit(c) || (c == '-' || c == '.') && isdigit(PeekChar()))) if (m_config.m_read_numbers && (isdigit(c) || (c == '+' || c == '-' || c == '.') && isdigit(PeekChar())))
{ {
bool isFloatingPointValue; bool isFloatingPointValue;
bool hasSignPrefix;
double doubleValue; double doubleValue;
int integerValue; int integerValue;
ReadNumber(isFloatingPointValue, doubleValue, integerValue); ReadNumber(isFloatingPointValue, hasSignPrefix, doubleValue, integerValue);
if (isFloatingPointValue) if (isFloatingPointValue)
return SimpleParserValue::FloatingPoint(pos, doubleValue); return SimpleParserValue::FloatingPoint(pos, doubleValue, hasSignPrefix);
return SimpleParserValue::Integer(pos, integerValue); return SimpleParserValue::Integer(pos, integerValue, hasSignPrefix);
} }
if (isalpha(c) || c == '_') if (isalpha(c) || c == '_')

View File

@ -41,6 +41,14 @@ SimpleParserValue SimpleParserValue::Integer(const TokenPos pos, const int value
return pv; return pv;
} }
SimpleParserValue SimpleParserValue::Integer(const TokenPos pos, const int value, const bool hasSignPrefix)
{
SimpleParserValue pv(pos, SimpleParserValueType::INTEGER);
pv.m_value.int_value = value;
pv.m_has_sign_prefix = hasSignPrefix;
return pv;
}
SimpleParserValue SimpleParserValue::FloatingPoint(const TokenPos pos, const double value) SimpleParserValue SimpleParserValue::FloatingPoint(const TokenPos pos, const double value)
{ {
SimpleParserValue pv(pos, SimpleParserValueType::FLOATING_POINT); SimpleParserValue pv(pos, SimpleParserValueType::FLOATING_POINT);
@ -48,6 +56,14 @@ SimpleParserValue SimpleParserValue::FloatingPoint(const TokenPos pos, const dou
return pv; return pv;
} }
SimpleParserValue SimpleParserValue::FloatingPoint(const TokenPos pos, const double value, const bool hasSignPrefix)
{
SimpleParserValue pv(pos, SimpleParserValueType::FLOATING_POINT);
pv.m_value.double_value = value;
pv.m_has_sign_prefix = hasSignPrefix;
return pv;
}
SimpleParserValue SimpleParserValue::String(const TokenPos pos, std::string* stringValue) SimpleParserValue SimpleParserValue::String(const TokenPos pos, std::string* stringValue)
{ {
SimpleParserValue pv(pos, SimpleParserValueType::STRING); SimpleParserValue pv(pos, SimpleParserValueType::STRING);
@ -67,6 +83,7 @@ SimpleParserValue::SimpleParserValue(const TokenPos pos, const SimpleParserValue
: m_pos(pos), : m_pos(pos),
m_type(type), m_type(type),
m_hash(0), m_hash(0),
m_has_sign_prefix(false),
m_value{} m_value{}
{ {
} }
@ -91,6 +108,7 @@ SimpleParserValue::SimpleParserValue(SimpleParserValue&& other) noexcept
: m_pos(other.m_pos), : m_pos(other.m_pos),
m_type(other.m_type), m_type(other.m_type),
m_hash(other.m_hash), m_hash(other.m_hash),
m_has_sign_prefix(other.m_has_sign_prefix),
m_value(other.m_value) m_value(other.m_value)
{ {
other.m_value = ValueType(); other.m_value = ValueType();
@ -102,6 +120,7 @@ SimpleParserValue& SimpleParserValue::operator=(SimpleParserValue&& other) noexc
m_type = other.m_type; m_type = other.m_type;
m_value = other.m_value; m_value = other.m_value;
m_hash = other.m_hash; m_hash = other.m_hash;
m_has_sign_prefix = other.m_has_sign_prefix;
other.m_value = ValueType(); other.m_value = ValueType();
return *this; return *this;

View File

@ -33,6 +33,7 @@ public:
TokenPos m_pos; TokenPos m_pos;
SimpleParserValueType m_type; SimpleParserValueType m_type;
size_t m_hash; size_t m_hash;
bool m_has_sign_prefix;
union ValueType union ValueType
{ {
char char_value; char char_value;
@ -48,7 +49,9 @@ public:
static SimpleParserValue Character(TokenPos pos, char c); static SimpleParserValue Character(TokenPos pos, char c);
static SimpleParserValue MultiCharacter(TokenPos pos, int multiCharacterSequenceId); static SimpleParserValue MultiCharacter(TokenPos pos, int multiCharacterSequenceId);
static SimpleParserValue Integer(TokenPos pos, int value); static SimpleParserValue Integer(TokenPos pos, int value);
static SimpleParserValue Integer(TokenPos pos, int value, bool hasSignPrefix);
static SimpleParserValue FloatingPoint(TokenPos pos, double value); static SimpleParserValue FloatingPoint(TokenPos pos, double value);
static SimpleParserValue FloatingPoint(TokenPos pos, double value, bool hasSignPrefix);
static SimpleParserValue String(TokenPos pos, std::string* stringValue); static SimpleParserValue String(TokenPos pos, std::string* stringValue);
static SimpleParserValue Identifier(TokenPos pos, std::string* identifier); static SimpleParserValue Identifier(TokenPos pos, std::string* identifier);

View File

@ -119,10 +119,11 @@ CommandsParserValue CommandsLexer::GetNextToken()
if (isdigit(c)) if (isdigit(c))
{ {
bool isFloatingPointValue; bool isFloatingPointValue;
bool hasSignPrefix;
double doubleValue; double doubleValue;
int integerValue; int integerValue;
ReadNumber(isFloatingPointValue, doubleValue, integerValue); ReadNumber(isFloatingPointValue, hasSignPrefix, doubleValue, integerValue);
if (isFloatingPointValue) if (isFloatingPointValue)
return CommandsParserValue::FloatingPoint(pos, doubleValue); return CommandsParserValue::FloatingPoint(pos, doubleValue);

View File

@ -138,10 +138,11 @@ HeaderParserValue HeaderLexer::GetNextToken()
if(isdigit(c)) if(isdigit(c))
{ {
bool isFloatingPointValue; bool isFloatingPointValue;
bool hasSignPrefix;
double doubleValue; double doubleValue;
int integerValue; int integerValue;
ReadNumber(isFloatingPointValue, doubleValue, integerValue); ReadNumber(isFloatingPointValue, hasSignPrefix, doubleValue, integerValue);
if (isFloatingPointValue) if (isFloatingPointValue)
return HeaderParserValue::FloatingPoint(pos, doubleValue); return HeaderParserValue::FloatingPoint(pos, doubleValue);

View File

@ -1,6 +1,9 @@
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
#include <sstream>
#include "Parsing/Impl/AbstractParser.h" #include "Parsing/Impl/AbstractParser.h"
#include "Parsing/Impl/ParserSingleInputStream.h"
#include "Utils/ClassUtils.h" #include "Utils/ClassUtils.h"
#include "Parsing/Mock/MockLexer.h" #include "Parsing/Mock/MockLexer.h"
#include "Parsing/Simple/SimpleParserValue.h" #include "Parsing/Simple/SimpleParserValue.h"
@ -33,22 +36,54 @@ namespace test::parsing::simple::expression
protected: protected:
void ProcessMatch(SimpleExpressionTestState* state, SequenceResult<SimpleParserValue>& result) const override void ProcessMatch(SimpleExpressionTestState* state, SequenceResult<SimpleParserValue>& result) const override
{ {
if (state->m_expression)
throw ParsingException(TokenPos(), "Expression already set");
state->m_expression = m_expression_matchers.ProcessExpression(result); state->m_expression = m_expression_matchers.ProcessExpression(result);
} }
}; };
class SimpleExpressionParser final : public AbstractParser<SimpleParserValue, SimpleExpressionTestState>
{
public:
explicit SimpleExpressionParser(ILexer<SimpleParserValue>* lexer)
: AbstractParser(lexer, std::make_unique<SimpleExpressionTestState>())
{
}
_NODISCARD SimpleExpressionTestState* GetState() const
{
return m_state.get();
}
protected:
const std::vector<sequence_t*>& GetTestsForState() override
{
static std::vector<sequence_t*> tests({
new SimpleExpressionSequence()
});
return tests;
}
};
class SimpleExpressionTestsHelper class SimpleExpressionTestsHelper
{ {
public: public:
std::unique_ptr<SimpleExpressionTestState> m_state; SimpleExpressionTestState* m_state;
std::unique_ptr<SimpleExpressionTestState> m_state_holder;
std::string m_str;
std::istringstream m_ss;
std::unique_ptr<IParserLineStream> m_stream;
std::unique_ptr<ILexer<SimpleParserValue>> m_lexer; std::unique_ptr<ILexer<SimpleParserValue>> m_lexer;
std::unique_ptr<SimpleExpressionParser> m_parser;
std::unique_ptr<SimpleExpressionSequence> m_sequence; std::unique_ptr<SimpleExpressionSequence> m_sequence;
unsigned m_consumed_token_count; unsigned m_consumed_token_count;
explicit SimpleExpressionTestsHelper() SimpleExpressionTestsHelper()
: m_state(std::make_unique<SimpleExpressionTestState>()), : m_state(nullptr),
m_sequence(std::make_unique<SimpleExpressionSequence>()), m_sequence(std::make_unique<SimpleExpressionSequence>()),
m_consumed_token_count(0u) m_consumed_token_count(0u)
{ {
@ -56,20 +91,48 @@ namespace test::parsing::simple::expression
void Tokens(std::initializer_list<Movable<SimpleParserValue>> tokens) void Tokens(std::initializer_list<Movable<SimpleParserValue>> tokens)
{ {
m_state_holder = std::make_unique<SimpleExpressionTestState>();
m_state = m_state_holder.get();
m_lexer = std::make_unique<MockLexer<SimpleParserValue>>(tokens, SimpleParserValue::EndOfFile(TokenPos())); m_lexer = std::make_unique<MockLexer<SimpleParserValue>>(tokens, SimpleParserValue::EndOfFile(TokenPos()));
} }
void Tokens(std::vector<SimpleParserValue> tokens) void Tokens(std::vector<SimpleParserValue> tokens)
{ {
m_state_holder = std::make_unique<SimpleExpressionTestState>();
m_state = m_state_holder.get();
m_lexer = std::make_unique<MockLexer<SimpleParserValue>>(std::move(tokens), SimpleParserValue::EndOfFile(TokenPos())); m_lexer = std::make_unique<MockLexer<SimpleParserValue>>(std::move(tokens), SimpleParserValue::EndOfFile(TokenPos()));
} }
void String(std::string str)
{
m_str = std::move(str);
m_ss = std::istringstream(m_str);
m_stream = std::make_unique<ParserSingleInputStream>(m_ss, "InputString");
SimpleLexer::Config lexerConfig;
lexerConfig.m_read_strings = true;
lexerConfig.m_read_numbers = true;
lexerConfig.m_emit_new_line_tokens = false;
SimpleExpressionMatchers(true, true, true, true, true).ApplyTokensToLexerConfig(lexerConfig);
m_lexer = std::make_unique<SimpleLexer>(m_stream.get(), std::move(lexerConfig));
m_parser = std::make_unique<SimpleExpressionParser>(m_lexer.get());
m_state = m_parser->GetState();
}
bool PerformTest() bool PerformTest()
{ {
REQUIRE(m_lexer); REQUIRE(m_lexer);
m_consumed_token_count = 0; m_consumed_token_count = 0;
return m_sequence->MatchSequence(m_lexer.get(), m_state.get(), m_consumed_token_count); return m_sequence->MatchSequence(m_lexer.get(), m_state, m_consumed_token_count);
}
_NODISCARD bool PerformIntegrationTest() const
{
REQUIRE(m_lexer);
REQUIRE(m_parser);
return m_parser->Parse();
} }
}; };
@ -738,4 +801,41 @@ namespace test::parsing::simple::expression
REQUIRE(value.m_type == SimpleExpressionValue::Type::INT); REQUIRE(value.m_type == SimpleExpressionValue::Type::INT);
REQUIRE(value.m_int_value == 1337); REQUIRE(value.m_int_value == 1337);
} }
namespace it
{
TEST_CASE("SimpleExpressionsIT: Can parse subtraction without space", "[parsing][simple][expression][it]")
{
SimpleExpressionTestsHelper helper;
helper.String("6-5");
const auto result = helper.PerformIntegrationTest();
REQUIRE(result);
const auto& expression = helper.m_state->m_expression;
REQUIRE(expression->IsStatic());
const auto value = expression->Evaluate();
REQUIRE(value.m_type == SimpleExpressionValue::Type::INT);
REQUIRE(value.m_int_value == 1);
}
TEST_CASE("SimpleExpressionsIT: Can parse addition without space", "[parsing][simple][expression][it]")
{
SimpleExpressionTestsHelper helper;
helper.String("6+5");
const auto result = helper.PerformIntegrationTest();
REQUIRE(result);
const auto& expression = helper.m_state->m_expression;
REQUIRE(expression->IsStatic());
const auto value = expression->Evaluate();
REQUIRE(value.m_type == SimpleExpressionValue::Type::INT);
REQUIRE(value.m_int_value == 11);
}
}
} }