Make menu and item rect accept expressions as static values

This commit is contained in:
Jan
2021-11-26 17:43:27 +01:00
parent 2a4768e5b0
commit fb70d9538a
5 changed files with 184 additions and 31 deletions

View File

@ -1,5 +1,7 @@
#include "MenuMatcherFactory.h"
#include "MenuExpressionMatchers.h"
using namespace menu;
MenuMatcherFactory::MenuMatcherFactory(const IMatcherForLabelSupplier<SimpleParserValue>* labelSupplier)
@ -9,17 +11,41 @@ MenuMatcherFactory::MenuMatcherFactory(const IMatcherForLabelSupplier<SimplePars
MatcherFactoryWrapper<SimpleParserValue> MenuMatcherFactory::Text() const
{
return MatcherFactoryWrapper<SimpleParserValue>(Or({String(), Identifier()}));
return MatcherFactoryWrapper(Or({String(), Identifier()}));
}
MatcherFactoryWrapper<SimpleParserValue> MenuMatcherFactory::Numeric() const
{
return MatcherFactoryWrapper<SimpleParserValue>(Or({FloatingPoint(), Integer()}));
return MatcherFactoryWrapper(Or({FloatingPoint(), Integer()}));
}
MatcherFactoryWrapper<SimpleParserValue> MenuMatcherFactory::IntExpression() const
{
return MatcherFactoryWrapper(Or({
Integer().Tag(TAG_INT).Capture(CAPTURE_INT),
And({
Char('(').Capture(CAPTURE_FIRST_TOKEN),
Label(MenuExpressionMatchers::LABEL_EXPRESSION),
Char(')'),
}).Tag(TAG_EXPRESSION)
}));
}
MatcherFactoryWrapper<SimpleParserValue> MenuMatcherFactory::NumericExpression() const
{
return MatcherFactoryWrapper(Or({
Numeric().Tag(TAG_NUMERIC).Capture(CAPTURE_NUMERIC),
And({
Char('(').Capture(CAPTURE_FIRST_TOKEN),
Label(MenuExpressionMatchers::LABEL_EXPRESSION),
Char(')'),
}).Tag(TAG_EXPRESSION)
}));
}
int MenuMatcherFactory::TokenNumericIntValue(const SimpleParserValue& value)
{
if(value.m_type == SimpleParserValueType::FLOATING_POINT)
if (value.m_type == SimpleParserValueType::FLOATING_POINT)
{
return static_cast<int>(value.FloatingPointValue());
}
@ -31,7 +57,7 @@ double MenuMatcherFactory::TokenNumericFloatingPointValue(const SimpleParserValu
{
if (value.m_type == SimpleParserValueType::INTEGER)
{
return static_cast<double>(value.IntegerValue());
return value.IntegerValue();
}
return value.FloatingPointValue();
@ -39,10 +65,72 @@ double MenuMatcherFactory::TokenNumericFloatingPointValue(const SimpleParserValu
std::string& MenuMatcherFactory::TokenTextValue(const SimpleParserValue& value)
{
if(value.m_type == SimpleParserValueType::IDENTIFIER)
if (value.m_type == SimpleParserValueType::IDENTIFIER)
{
return value.IdentifierValue();
}
return value.StringValue();
}
int MenuMatcherFactory::TokenIntExpressionValue(SequenceResult<SimpleParserValue>& result)
{
const auto nextTag = result.PeekTag();
assert(nextTag == TAG_INT || nextTag == TAG_EXPRESSION);
if (nextTag == TAG_INT)
{
result.NextTag();
return result.NextCapture(CAPTURE_INT).IntegerValue();
}
if (nextTag == TAG_EXPRESSION)
{
result.NextTag();
const auto expression = MenuExpressionMatchers().ProcessExpression(result);
if (!expression || !expression->IsStatic())
throw ParsingException(result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos(), "Not a valid static expression");
const auto value = expression->Evaluate();
if (value.m_type != SimpleExpressionValue::Type::INT)
throw ParsingException(result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos(), "Expression MUST be int type");
return value.m_int_value;
}
throw ParsingException(TokenPos(), "TokenIntExpressionValue must be expression or int");
}
double MenuMatcherFactory::TokenNumericExpressionValue(SequenceResult<SimpleParserValue>& result)
{
const auto nextTag = result.PeekTag();
assert(nextTag == TAG_NUMERIC || nextTag == TAG_EXPRESSION);
if (nextTag == TAG_NUMERIC)
{
result.NextTag();
return TokenNumericFloatingPointValue(result.NextCapture(CAPTURE_NUMERIC));
}
if (nextTag == TAG_EXPRESSION)
{
result.NextTag();
const auto expression = MenuExpressionMatchers().ProcessExpression(result);
if (!expression || !expression->IsStatic())
throw ParsingException(result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos(), "Not a valid static expression");
const auto value = expression->Evaluate();
if (value.m_type == SimpleExpressionValue::Type::INT)
return value.m_int_value;
if (value.m_type == SimpleExpressionValue::Type::DOUBLE)
return value.m_double_value;
throw ParsingException(result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos(), "Expression MUST be numeric type");
}
throw ParsingException(TokenPos(), "TokenNumericExpressionValue must be expression or numeric");
}

View File

@ -1,19 +1,34 @@
#pragma once
#include "Parsing/Sequence/SequenceResult.h"
#include "Parsing/Simple/Matcher/SimpleMatcherFactory.h"
namespace menu
{
class MenuMatcherFactory : public SimpleMatcherFactory
{
static constexpr auto TAG_INT = 1420;
static constexpr auto TAG_NUMERIC = 1421;
static constexpr auto TAG_EXPRESSION = 1422;
static constexpr auto CAPTURE_FIRST_TOKEN = 1420;
static constexpr auto CAPTURE_INT = 1421;
static constexpr auto CAPTURE_NUMERIC = 1422;
public:
explicit MenuMatcherFactory(const IMatcherForLabelSupplier<SimpleParserValue>* labelSupplier);
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Text() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Numeric() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> IntExpression() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> NumericExpression() const;
_NODISCARD static int TokenNumericIntValue(const SimpleParserValue& value);
_NODISCARD static double TokenNumericFloatingPointValue(const SimpleParserValue& value);
_NODISCARD static std::string& TokenTextValue(const SimpleParserValue& value);
_NODISCARD static int TokenIntExpressionValue(SequenceResult<SimpleParserValue>& result);
_NODISCARD static double TokenNumericExpressionValue(SequenceResult<SimpleParserValue>& result);
};
}