Spicy
Classes | Public Member Functions | Friends | List of all members
spicy::detail::codegen::ParserBuilder Class Reference

#include <parser-builder.h>

Classes

struct  ScopeGuard
 

Public Member Functions

 ParserBuilder (CodeGen *cg)
 
void pushState (ParserState p)
 
void popState ()
 
const ParserStatestate () 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::BuilderpushBuilder ()
 
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)
 
CodeGencg () const
 
std::shared_ptr< hilti::Contextcontext () const
 
const hilti::Optionsoptions () const
 

Friends

struct spicy::detail::codegen::ProductionVisitor
 

Detailed Description

Generates the parsing logic for a unit type.

Member Function Documentation

◆ addParserMethods()

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.

◆ advanceInput()

void ParserBuilder::advanceInput ( const Expression &  i)

Generates code that advances the current view to a new start position. This implicitly calls advancedInput() afterwards.

Parameters
iexpression 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.

◆ advanceToNextData()

void ParserBuilder::advanceToNextData ( )

Generates code that advances the current view to the next position which is not a gap. This implicitly calls advancedInput() afterwards.

◆ afterHook()

void ParserBuilder::afterHook ( )

Inserts code that needs to run after a user hook was executed.

◆ applyConvertExpression()

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).

◆ atEod()

Expression ParserBuilder::atEod ( )

Returns a boolean expression that's true if EOD has been reached.

◆ beforeHook()

void ParserBuilder::beforeHook ( )

Inserts code that needs to run before a user hook gets executed.

◆ builder()

auto spicy::detail::codegen::ParserBuilder::builder ( )
inline

Returns statement builder currently being active.

◆ consumeLookAhead()

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.

Parameters
dstA RHS expression of type bytes to store the token into.

◆ contextNewFunction()

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.

◆ enableDefaultNewValueForField()

void spicy::detail::codegen::ParserBuilder::enableDefaultNewValueForField ( bool  enable)
inline

Signal that new values for fields are reported through custom logic, disable default reporting for current field.

◆ finalizeUnit()

void ParserBuilder::finalizeUnit ( bool  success,
const Location l 
)

Generates code that cleans up a unit instances after parsing has finished, normally or abnormally.

Parameters
successtrue if parsing was successful, false if an error occurred.
llocation to associate with the generated code

◆ finishBacktracking()

void ParserBuilder::finishBacktracking ( )

Clean up after potential backtracking via &try.

◆ finishLoopBody()

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.

Parameters
cookieopaque cookie received from initLoopBody()
llocation associated with the loop body

◆ guardFeatureCode()

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.

Parameters
unitunit the code is added for
featuresidentifiers of the feature, will be combined with OR.
fcallback building the feature-dependent code.

◆ initBacktracking()

void ParserBuilder::initBacktracking ( )

Prepare for backtracking via &try.

◆ initializeUnit()

void ParserBuilder::initializeUnit ( const Location l)

Generates code that initializes a unit instance just before parsing begins.

Parameters
llocation to associate with the generated code

◆ initLoopBody()

Expression ParserBuilder::initLoopBody ( )

Prepare for parsing the body of a loop of "something". Must be followed by calling finishLoopBody() once parsing is done.

Returns
an opaque cookie to pass into finishLoopBody().

◆ isEnabledDefaultNewValueForField()

bool spicy::detail::codegen::ParserBuilder::isEnabledDefaultNewValueForField ( )
inline

Returns true if default reporting of new value is enabled for the current field.

◆ makeScopeGuard()

ScopeGuard spicy::detail::codegen::ParserBuilder::makeScopeGuard ( )
inline

Returns an object whose destructor pops the most recent statement builder.

◆ newContainerItem()

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.

◆ newValueForField()

void ParserBuilder::newValueForField ( const production::Meta meta,
const Expression &  value,
const Expression &  dd 
)

Called when a field has been updated.

◆ parseError() [1/3]

void ParserBuilder::parseError ( const std::string &  error_msg,
const Meta location 
)

Generates code that triggers a parse error exception.

◆ parseError() [2/3]

void ParserBuilder::parseError ( const Expression &  error_msg,
const Meta location 
)

Generates code that triggers a parse error exception.

◆ parseError() [3/3]

void ParserBuilder::parseError ( const std::string &  fmt,
const std::vector< Expression > &  args,
const Meta location 
)

Generates code that triggers a parse error exception.

◆ parseLiteral()

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.

◆ parseMethodExternalOverload1()

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.

◆ parseMethodExternalOverload2()

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.

◆ parseMethodExternalOverload3()

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.

◆ parseMethodFunctionType()

hilti::type::Function ParserBuilder::parseMethodFunctionType ( std::optional< type::function::Parameter >  addl_param = {},
const Meta m = {} 
)

Returns the type for a parse_stageX unit method.

◆ parseType()

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.

◆ parseTypeTry()

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.

◆ popBuilder()

auto spicy::detail::codegen::ParserBuilder::popBuilder ( )
inline

Deactivates the most recent statement builder.

◆ popState()

void spicy::detail::codegen::ParserBuilder::popState ( )
inline

Remove the top element from the parsing state stack, switching back to the previous state.

◆ pushBuilder() [1/3]

auto spicy::detail::codegen::ParserBuilder::pushBuilder ( std::shared_ptr< hilti::builder::Builder b)
inline

Activates a statement builder for subsequent code.

◆ pushBuilder() [2/3]

std::shared_ptr< hilti::builder::Builder > ParserBuilder::pushBuilder ( )

Creates a new statement builder and activates it for subsequent code.

◆ pushBuilder() [3/3]

auto spicy::detail::codegen::ParserBuilder::pushBuilder ( std::shared_ptr< hilti::builder::Builder b,
const std::function< void()> &  func 
)
inline

Activates a statement builder for subsequent code.

◆ pushState()

void spicy::detail::codegen::ParserBuilder::pushState ( ParserState  p)
inline

Pushes new parsing state onto the stack. The new state will then be used by any subsequent code generation.

◆ saveParsePosition()

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.

◆ setInput()

void ParserBuilder::setInput ( const Expression &  i)

Generates code that sets the current view.

Parameters
iexpression that's the new view to use.

◆ state()

const ParserState& spicy::detail::codegen::ParserBuilder::state ( ) const
inline

Returns the current parsing state.

◆ trimInput()

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.

Parameters
forcealways trim, independent of the parsing state's trimming state

◆ waitForEod()

void ParserBuilder::waitForEod ( )

Generates code that waits for end-of-data to be obtained (but not necessarily reached).

◆ waitForInput() [1/2]

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.

Parameters
minunsigned integer expression specifying the requited number of bytes.
error_msgmessage to report with parse error if end-of-data is reached
locationlocation associated with the operation.

◆ waitForInput() [2/2]

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.

Parameters
error_msgmessage to report with parse error if end-of-data is reached
locationlocation associated with the operation

◆ waitForInputOrEod() [1/2]

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.

Parameters
minunsigned integer expression specifying the requited number of bytes.
Returns
A boolean expression that's true if sufficient bytes are available, and false if end-of-data has been reached.

◆ waitForInputOrEod() [2/2]

Expression ParserBuilder::waitForInputOrEod ( )

Generates code that waits for either more input becoming available or end of data being reached..

Parameters
locationlocation associated with the operation
Returns
A boolean expression that's true if more bytes have become available, and false if end-of-data has been reached.

The documentation for this class was generated from the following files: