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;
}
void ReadNumber(bool& isFloatingPoint, double& floatingPointValue, int& integerValue)
void ReadNumber(bool& isFloatingPoint, bool& hasSignPrefix, double& floatingPointValue, int& integerValue)
{
const auto& currentLine = CurrentLine();
assert(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] == '-');
hasSignPrefix = currentLine.m_line[m_current_line_offset - 1] == '+' || currentLine.m_line[m_current_line_offset - 1] == '-';
const auto lineLength = currentLine.m_line.size();
if (lineLength - m_current_line_offset >= 1
@ -341,9 +343,12 @@ public:
{
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)
{
return line;
}
}
return ParserLine();

View File

@ -1,5 +1,6 @@
#include "SimpleExpressionMatchers.h"
#include <algorithm>
#include <list>
#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);
}

View File

@ -7,6 +7,7 @@
#include "SimpleMatcherKeywordPrefix.h"
#include "SimpleMatcherMultiCharacter.h"
#include "SimpleMatcherValueType.h"
#include "SimpleMatcherValueTypeAndHasSignPrefix.h"
SimpleMatcherFactory::SimpleMatcherFactory(const IMatcherForLabelSupplier<SimpleParserValue>* labelSupplier)
: AbstractMatcherFactory(labelSupplier)
@ -48,11 +49,21 @@ MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::Integer() const
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
{
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
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherCharacter>(c));

View File

@ -17,7 +17,9 @@ public:
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Identifier() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> String() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Integer() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> IntegerWithSign() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> FloatingPoint() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> FloatingPointWithSign() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Char(char c) const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> MultiChar(int multiCharacterSequenceId) 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 == '\"')
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 hasSignPrefix;
double doubleValue;
int integerValue;
ReadNumber(isFloatingPointValue, doubleValue, integerValue);
ReadNumber(isFloatingPointValue, hasSignPrefix, doubleValue, integerValue);
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 == '_')

View File

@ -41,6 +41,14 @@ SimpleParserValue SimpleParserValue::Integer(const TokenPos pos, const int value
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 pv(pos, SimpleParserValueType::FLOATING_POINT);
@ -48,6 +56,14 @@ SimpleParserValue SimpleParserValue::FloatingPoint(const TokenPos pos, const dou
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 pv(pos, SimpleParserValueType::STRING);
@ -67,6 +83,7 @@ SimpleParserValue::SimpleParserValue(const TokenPos pos, const SimpleParserValue
: m_pos(pos),
m_type(type),
m_hash(0),
m_has_sign_prefix(false),
m_value{}
{
}
@ -91,6 +108,7 @@ SimpleParserValue::SimpleParserValue(SimpleParserValue&& other) noexcept
: m_pos(other.m_pos),
m_type(other.m_type),
m_hash(other.m_hash),
m_has_sign_prefix(other.m_has_sign_prefix),
m_value(other.m_value)
{
other.m_value = ValueType();
@ -102,6 +120,7 @@ SimpleParserValue& SimpleParserValue::operator=(SimpleParserValue&& other) noexc
m_type = other.m_type;
m_value = other.m_value;
m_hash = other.m_hash;
m_has_sign_prefix = other.m_has_sign_prefix;
other.m_value = ValueType();
return *this;

View File

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