![]() |
Spicy
|
#include <parser-builder.h>
Classes | |
struct | ScopeGuard |
Public Member Functions | |
ParserBuilder (CodeGen *cg) | |
void | pushState (ParserState p) |
void | popState () |
ParserState | state () const |
Expression | parseMethodExternalOverload1 (const type::Unit &t) |
Expression | parseMethodExternalOverload2 (const type::Unit &t) |
Expression | parseMethodExternalOverload3 (const type::Unit &t) |
Expression | contextNewFunction (const type::Unit &t) |
hilti::type::Struct | addParserMethods (hilti::type::Struct s, const type::Unit &t, bool declare_only) |
auto | builder () |
auto | pushBuilder (std::shared_ptr< hilti::builder::Builder > b) |
std::shared_ptr< hilti::builder::Builder > | pushBuilder () |
auto | popBuilder () |
ScopeGuard | makeScopeGuard () |
auto | pushBuilder (std::shared_ptr< hilti::builder::Builder > b, const std::function< void()> &func) |
Expression | parseType (const Type &t, const production::Meta &meta, const std::optional< Expression > &dst) |
Expression | parseTypeTry (const Type &t, const production::Meta &meta, const std::optional< Expression > &dst) |
hilti::type::Function | parseMethodFunctionType (std::optional< type::function::Parameter > addl_param={}, const Meta &m={}) |
Expression | parseLiteral (const Production &p, const std::optional< Expression > &dst) |
void | waitForInput (const Expression &min, const std::string &error_msg, const Meta &location) |
Expression | waitForInputOrEod (const Expression &min) |
void | waitForInput (const std::string &error_msg, const Meta &location) |
Expression | waitForInputOrEod () |
void | waitForEod () |
Expression | atEod () |
void | advanceToNextData () |
void | advanceInput (const Expression &i) |
void | setInput (const Expression &i) |
void | saveParsePosition () |
void | beforeHook () |
void | afterHook () |
void | consumeLookAhead (std::optional< Expression > dst={}) |
void | parseError (const std::string &error_msg, const Meta &location) |
void | parseError (const Expression &error_msg, const Meta &location) |
void | parseError (const std::string &fmt, const std::vector< Expression > &args, const Meta &location) |
void | newValueForField (const production::Meta &meta, const Expression &value, const Expression &dd) |
void | enableDefaultNewValueForField (bool enable) |
bool | isEnabledDefaultNewValueForField () |
Expression | newContainerItem (const type::unit::item::Field &field, const Expression &self, const Expression &item, bool need_value) |
Expression | applyConvertExpression (const type::unit::item::Field &field, const Expression &value, std::optional< Expression > dst={}) |
void | trimInput (bool force=false) |
void | initializeUnit (const Location &l) |
void | finalizeUnit (bool success, const Location &l) |
void | initBacktracking () |
void | finishBacktracking () |
Expression | initLoopBody () |
void | finishLoopBody (const Expression &cookie, const Location &l) |
void | guardFeatureCode (const type::Unit &unit, const std::vector< std::string_view > &features, const std::function< void()> &f) |
CodeGen * | cg () const |
std::shared_ptr< hilti::Context > | context () const |
const hilti::Options & | options () const |
Friends | |
struct | spicy::detail::codegen::ProductionVisitor |
Generates the parsing logic for a unit type.
hilti::type::Struct ParserBuilder::addParserMethods | ( | hilti::type::Struct | s, |
const type::Unit & | t, | ||
bool | declare_only | ||
) |
Adds a unit's external parsing methods to the HILTI struct corresponding to the parse object. Returns the modified type.
void ParserBuilder::advanceInput | ( | const Expression & | i | ) |
Generates code that advances the current view to a new start position. This implicitly calls advancedInput() afterwards.
i | expression that's either the number of bytes to move ahead, a stream iterator to move to, or a new stream view to use from now on. |
void ParserBuilder::advanceToNextData | ( | ) |
Generates code that advances the current view to the next position which is not a gap. This implicitly calls advancedInput() afterwards.
void ParserBuilder::afterHook | ( | ) |
Inserts code that needs to run after a user hook was executed.
Expression ParserBuilder::applyConvertExpression | ( | const type::unit::item::Field & | field, |
const Expression & | value, | ||
std::optional< Expression > | dst = {} |
||
) |
Applies a field's &convert
expression to a value, and returns the converted result. If the field does not have that attribute set, returns the original value. If destination is given, also saves the result to that destination (and then it might not need create a tmp to store the result in).
Expression ParserBuilder::atEod | ( | ) |
Returns a boolean expression that's true if EOD has been reached.
void ParserBuilder::beforeHook | ( | ) |
Inserts code that needs to run before a user hook gets executed.
|
inline |
Returns statement builder currently being active.
void ParserBuilder::consumeLookAhead | ( | std::optional< Expression > | dst = {} | ) |
Generates code that consumes the current look-ahead symbol. It clears lahead
, move cur
to lahead_end
, and optionally stores the look-ahead token itself into a custom destination.
dst | A RHS expression of type bytes to store the token into. |
Expression ParserBuilder::contextNewFunction | ( | const type::Unit & | t | ) |
Returns an expression referencing a publicly visible function instantiating a unit's context
type. If the unit does not set context
, the returned expression will evaluate to null at runtime.
|
inline |
Signal that new values for fields are reported through custom logic, disable default reporting for current field.
void ParserBuilder::finalizeUnit | ( | bool | success, |
const Location & | l | ||
) |
Generates code that cleans up a unit instances after parsing has finished, normally or abnormally.
success | true if parsing was successful, false if an error occurred. |
l | location to associate with the generated code |
void ParserBuilder::finishBacktracking | ( | ) |
Clean up after potential backtracking via &try
.
void ParserBuilder::finishLoopBody | ( | const Expression & | cookie, |
const Location & | l | ||
) |
Wrap up parsing the body of loop of "something". Must only be called after an earlier initLoopBody()
. This will abort with a parsing error if the input pointer hasn't moved.
cookie | opaque cookie received from initLoopBody() |
l | location associated with the loop body |
void ParserBuilder::guardFeatureCode | ( | const type::Unit & | unit, |
const std::vector< std::string_view > & | features, | ||
const std::function< void()> & | f | ||
) |
Add a guard block around feature-dependent unit code. This helper typically will put feature-dependent code into a conditional which is only executed if the feature is enabled.
unit | unit the code is added for |
features | identifiers of the feature, will be combined with OR. |
f | callback building the feature-dependent code. |
void ParserBuilder::initBacktracking | ( | ) |
Prepare for backtracking via &try
.
void ParserBuilder::initializeUnit | ( | const Location & | l | ) |
Generates code that initializes a unit instance just before parsing begins.
l | location to associate with the generated code |
Expression ParserBuilder::initLoopBody | ( | ) |
Prepare for parsing the body of a loop of "something". Must be followed by calling finishLoopBody()
once parsing is done.
finishLoopBody()
.
|
inline |
Returns true if default reporting of new value is enabled for the current field.
|
inline |
Returns an object whose destructor pops the most recent statement builder.
Expression ParserBuilder::newContainerItem | ( | const type::unit::item::Field & | field, |
const Expression & | self, | ||
const Expression & | item, | ||
bool | need_value | ||
) |
Called when a container item has been parsed. Returns a boolean expression that true if container parsing is to continue.
void ParserBuilder::newValueForField | ( | const production::Meta & | meta, |
const Expression & | value, | ||
const Expression & | dd | ||
) |
Called when a field has been updated.
void ParserBuilder::parseError | ( | const std::string & | error_msg, |
const Meta & | location | ||
) |
Generates code that triggers a parse error exception.
void ParserBuilder::parseError | ( | const Expression & | error_msg, |
const Meta & | location | ||
) |
Generates code that triggers a parse error exception.
void ParserBuilder::parseError | ( | const std::string & | fmt, |
const std::vector< Expression > & | args, | ||
const Meta & | location | ||
) |
Generates code that triggers a parse error exception.
Expression ParserBuilder::parseLiteral | ( | const Production & | p, |
const std::optional< Expression > & | dst | ||
) |
Generates code that parses an instance of a specific literal, meaning it matches the value against the input.
In literal mode Default
, returns the parsed value and advances cur
, consuming the current look-ahead symbol if any, and throwing a parse error if it couldn't parse it.
In literal mode Try
, returns an iterator pointing right after the parsed literal, with an iterator equal to begin(cur)
meaning no match (and does not advance cur
).
Literal mode Search
behaves like Try
, but will advance the input until a match has been found or EOD is reached.
Expression ParserBuilder::parseMethodExternalOverload1 | ( | const type::Unit & | t | ) |
Returns an expression referencing the 1st version of a publicly visible method that implements a unit's parsing logic, to be called from a host application. This version returns just the data remaining after parsing the unit.
Expression ParserBuilder::parseMethodExternalOverload2 | ( | const type::Unit & | t | ) |
Returns an expression referencing the 2nd version of a publicly visible method that implements a unit's parsing logic, to be called from a host application. This version returns the parsed object plus the data remaining after parsing the unit.
Expression ParserBuilder::parseMethodExternalOverload3 | ( | const type::Unit & | t | ) |
Returns an expression referencing the 3rd version of a publicly visible method that implements a unit's parsing logic, to be called from a host application. This version returns a generic parse object of type spicy::rt::ParsedUnit
, plus the data remaining after parsing the unit.
hilti::type::Function ParserBuilder::parseMethodFunctionType | ( | std::optional< type::function::Parameter > | addl_param = {} , |
const Meta & | m = {} |
||
) |
Returns the type for a parse_stageX
unit method.
Expression ParserBuilder::parseType | ( | const Type & | t, |
const production::Meta & | meta, | ||
const std::optional< Expression > & | dst | ||
) |
Generates code that parses an instance of a specific type.
Expression ParserBuilder::parseTypeTry | ( | const Type & | t, |
const production::Meta & | meta, | ||
const std::optional< Expression > & | dst | ||
) |
Generates code that parses an instance of a specific type into an expression yielding a Result
of t
.
|
inline |
Deactivates the most recent statement builder.
|
inline |
Remove the top element from the parsing state stack, switching back to the previous state.
|
inline |
Activates a statement builder for subsequent code.
std::shared_ptr< hilti::builder::Builder > ParserBuilder::pushBuilder | ( | ) |
Creates a new statement builder and activates it for subsequent code.
|
inline |
Activates a statement builder for subsequent code.
|
inline |
Pushes new parsing state onto the stack. The new state will then be used by any subsequent code generation.
void ParserBuilder::saveParsePosition | ( | ) |
Generates code that saves the current parsing position inside the current parse object. This only has an effect for unit types that support random access, it's a no-op for others.
void ParserBuilder::setInput | ( | const Expression & | i | ) |
Generates code that sets the current view.
i | expression that's the new view to use. |
|
inline |
Returns the current parsing state.
void ParserBuilder::trimInput | ( | bool | force = false | ) |
Trims the input's beginning to the current parsing position, discarding all data preceding it. By default, this does not do anything if the current parsing state does not allow trimming.
force | always trim, independent of the parsing state's trimming state |
void ParserBuilder::waitForEod | ( | ) |
Generates code that waits for end-of-data to be obtained (but not necessarily reached).
void ParserBuilder::waitForInput | ( | const Expression & | min, |
const std::string & | error_msg, | ||
const Meta & | location | ||
) |
Generates code that ensures that a minimum amount of data is available for parsing. The generated code will wait until enough data becomes available before proceeding. It will abort parsing if end-of-data is reached before that.
min | unsigned integer expression specifying the requited number of bytes. |
error_msg | message to report with parse error if end-of-data is reached |
location | location associated with the operation. |
void ParserBuilder::waitForInput | ( | const std::string & | error_msg, |
const Meta & | location | ||
) |
Generates code that waits for more input. If end-of-data is reached before additional input becomes available, it triggers a parse error.
error_msg | message to report with parse error if end-of-data is reached |
location | location associated with the operation |
Expression ParserBuilder::waitForInputOrEod | ( | const Expression & | min | ) |
Generates code that ensures that either a minimum amount of data is available for parsing, or end-of-data is reached. The generated code will wait until either happens.
min | unsigned integer expression specifying the requited number of bytes. |
Expression ParserBuilder::waitForInputOrEod | ( | ) |
Generates code that waits for either more input becoming available or end of data being reached..
location | location associated with the operation |