mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 16:15:43 +00:00
Add SimpleExpressionInterpreter that just parses a plain expression
This commit is contained in:
parent
6702856399
commit
7cce0e387e
79
src/Parser/Parsing/Simple/SimpleExpressionInterpreter.cpp
Normal file
79
src/Parser/Parsing/Simple/SimpleExpressionInterpreter.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "SimpleExpressionInterpreter.h"
|
||||
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include "Expression/SimpleExpressionMatchers.h"
|
||||
#include "Matcher/SimpleMatcherFactory.h"
|
||||
#include "Parsing/Impl/AbstractParser.h"
|
||||
|
||||
class SimpleExpressionInterpreterParserState
|
||||
{
|
||||
public:
|
||||
std::unique_ptr<ISimpleExpression> m_expression;
|
||||
};
|
||||
|
||||
class SimpleExpressionInterpreterExpressionSequence final : public AbstractSequence<SimpleParserValue, SimpleExpressionInterpreterParserState>
|
||||
{
|
||||
public:
|
||||
SimpleExpressionInterpreterExpressionSequence()
|
||||
{
|
||||
const SimpleMatcherFactory create(this);
|
||||
AddLabeledMatchers(m_expression_matchers.Expression(this), SimpleExpressionMatchers::LABEL_EXPRESSION);
|
||||
AddMatchers(create.Label(SimpleExpressionMatchers::LABEL_EXPRESSION));
|
||||
}
|
||||
|
||||
protected:
|
||||
void ProcessMatch(SimpleExpressionInterpreterParserState* state, SequenceResult<SimpleParserValue>& result) const override
|
||||
{
|
||||
state->m_expression = m_expression_matchers.ProcessExpression(result);
|
||||
}
|
||||
|
||||
private:
|
||||
SimpleExpressionMatchers m_expression_matchers;
|
||||
};
|
||||
|
||||
class SimpleExpressionInterpreterParser final : public AbstractParser<SimpleParserValue, SimpleExpressionInterpreterParserState>
|
||||
{
|
||||
public:
|
||||
explicit SimpleExpressionInterpreterParser(ILexer<SimpleParserValue>* lexer)
|
||||
: AbstractParser<SimpleParserValue, SimpleExpressionInterpreterParserState>(lexer, std::make_unique<SimpleExpressionInterpreterParserState>())
|
||||
{
|
||||
}
|
||||
|
||||
_NODISCARD std::unique_ptr<ISimpleExpression> GetParsedExpression() const
|
||||
{
|
||||
return std::move(m_state->m_expression);
|
||||
}
|
||||
|
||||
protected:
|
||||
const std::vector<sequence_t*>& GetTestsForState() override
|
||||
{
|
||||
static std::vector<sequence_t*> sequences
|
||||
{
|
||||
new SimpleExpressionInterpreterExpressionSequence()
|
||||
};
|
||||
return sequences;
|
||||
}
|
||||
};
|
||||
|
||||
SimpleExpressionInterpreter::SimpleExpressionInterpreter(IParserLineStream* input)
|
||||
: m_input(input)
|
||||
{
|
||||
}
|
||||
|
||||
std::unique_ptr<ISimpleExpression> SimpleExpressionInterpreter::Evaluate() const
|
||||
{
|
||||
SimpleLexer::Config lexerConfig;
|
||||
lexerConfig.m_emit_new_line_tokens = false;
|
||||
lexerConfig.m_read_integer_numbers = true;
|
||||
lexerConfig.m_read_floating_point_numbers = true;
|
||||
lexerConfig.m_read_strings = true;
|
||||
SimpleExpressionMatchers().ApplyTokensToLexerConfig(lexerConfig);
|
||||
|
||||
SimpleLexer lexer(m_input, std::move(lexerConfig));
|
||||
SimpleExpressionInterpreterParser parser(&lexer);
|
||||
|
||||
if (!parser.Parse())
|
||||
return nullptr;
|
||||
|
||||
return parser.GetParsedExpression();
|
||||
}
|
14
src/Parser/Parsing/Simple/SimpleExpressionInterpreter.h
Normal file
14
src/Parser/Parsing/Simple/SimpleExpressionInterpreter.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "Expression/ISimpleExpression.h"
|
||||
#include "Parsing/IParserLineStream.h"
|
||||
|
||||
class SimpleExpressionInterpreter
|
||||
{
|
||||
public:
|
||||
explicit SimpleExpressionInterpreter(IParserLineStream* input);
|
||||
|
||||
std::unique_ptr<ISimpleExpression> Evaluate() const;
|
||||
|
||||
private:
|
||||
IParserLineStream* m_input;
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user