parser.h#
ONNX text-format parser declarations, including
onnx::ParserBase (cursor-based tokenizer) and
onnx::OnnxParser (builds protobuf structures from text).
Also defines utility maps onnx::PrimitiveTypeNameMap,
onnx::AttributeTypeNameMap, and onnx::KeyWordMap.
Declares the ONNX text-format parser and related helpers.
This header exposes ParserBase (cursor-based tokenizer) and OnnxParser (builds protobuf structures from text) for parsing ONNX models, graphs, functions, nodes, and types from their textual representation. It also defines utility maps (PrimitiveTypeNameMap, AttributeTypeNameMap, KeyWordMap) and convenience type aliases used throughout the parser.
Note
The ONNX text syntax is experimental and may change.
Defines
-
CHECK_PARSER_STATUS(status)#
-
namespace ONNX_LIGHT_NAMESPACE
Typedefs
-
using NodeList = utils::RepeatedProtoField<NodeProto>#
List of parsed node definitions.
-
using AttrList = utils::RepeatedProtoField<AttributeProto>#
List of parsed node attributes.
-
using ValueInfoList = utils::RepeatedProtoField<ValueInfoProto>#
List of value-info records used for graph/function signatures.
-
using TensorList = utils::RepeatedProtoField<TensorProto>#
List of tensor literals and initializers.
-
using OpsetIdList = utils::RepeatedProtoField<OperatorSetIdProto>#
List of opset imports.
-
using StringStringList = utils::RepeatedProtoField<StringStringEntryProto>#
List of key/value metadata entries.
-
class AttributeTypeNameMap : public ONNX_LIGHT_NAMESPACE::StringIntMap<AttributeTypeNameMap>#
- #include <parser.h>
Maps ONNX attribute type name strings to AttributeProto::AttributeType values.
Covers scalar types (“float”, “int”, “string”, “tensor”, “graph”, “sparse_tensor”, “type_proto”) and their list counterparts (“floats”, “ints”, “strings”, “tensors”, “graphs”, “sparse_tensors”, “type_protos”).
Public Functions
-
inline AttributeTypeNameMap()#
-
inline AttributeTypeNameMap()#
-
class KeyWordMap#
- #include <parser.h>
Singleton map from keyword identifier strings to KeyWord enum values.
Recognizes ONNX model-level keywords such as “ir_version”, “opset_import”, “domain”, “seq”, “map”, “optional”, “sparse_tensor”, and “overload”.
Public Types
-
enum class KeyWord : std::uint8_t#
Enumeration of all reserved ONNX text-format keywords.
Values:
-
enumerator NONE#
-
enumerator IR_VERSION#
-
enumerator OPSET_IMPORT#
-
enumerator PRODUCER_NAME#
-
enumerator PRODUCER_VERSION#
-
enumerator DOMAIN_KW#
-
enumerator MODEL_VERSION#
-
enumerator DOC_STRING#
-
enumerator METADATA_PROPS#
-
enumerator SEQ_TYPE#
-
enumerator MAP_TYPE#
-
enumerator OPTIONAL_TYPE#
-
enumerator SPARSE_TENSOR_TYPE#
-
enumerator OVERLOAD_KW#
-
enumerator NONE#
Public Functions
-
inline KeyWordMap()#
Constructs the keyword map and populates all reserved identifier entries.
-
enum class KeyWord : std::uint8_t#
-
class OnnxParser : public ONNX_LIGHT_NAMESPACE::ParserBase#
- #include <parser.h>
High-level ONNX text-format parser that builds protobuf structures.
Inherits from ParserBase and provides overloaded Parse() methods for every major ONNX protobuf type (ModelProto, GraphProto, FunctionProto, NodeProto, TensorProto, TypeProto, AttributeProto, etc.).
The most convenient entry point is the static Parse() helper:
ModelProto model; auto status = OnnxParser::Parse(model, text_buffer);
Public Functions
-
inline explicit OnnxParser(const char *cstr)#
Creates an ONNX text parser from a null-terminated buffer.
-
Common::Status Parse(TensorShapeProto &shape)#
Parses a
TensorShapeProto.
-
Common::Status Parse(StringStringList &stringStringList)#
Parses a metadata key/value list.
-
Common::Status Parse(TensorProto &tensorProto)#
Parses a
TensorProto.
-
Common::Status Parse(AttributeProto &attr)#
Parses an
AttributeProto.
-
Common::Status Parse(AttributeProto &attr, std::string &name)#
Parses a named
AttributeProto.
-
Common::Status Parse(GraphProto &graph)#
Parses a
GraphProto.
-
Common::Status Parse(FunctionProto &fn)#
Parses a
FunctionProto.
-
Common::Status Parse(ModelProto &model)#
Parses a
ModelProto.
Public Static Functions
Private Functions
-
Common::Status Parse(std::string name, GraphProto &graph)#
-
Common::Status ParseSingleAttributeValue(AttributeProto &attr, AttributeProto::AttributeType expected)#
-
Common::Status Parse(ValueInfoProto &valueinfo)#
-
Common::Status ParseGraphInputOutput(ValueInfoList &vilist)#
-
Common::Status ParseFunctionInputOutput(IdList &idlist, ValueInfoList &vilist)#
-
Common::Status Parse(char open, ValueInfoList &vilist, char close)#
-
Common::Status ParseInput(ValueInfoList &inputs, TensorList &initializers)#
-
Common::Status ParseValueInfo(ValueInfoList &value_infos, TensorList &initializers)#
-
Common::Status Parse(TensorProto &tensorProto, const TypeProto &tensorTypeProto)#
-
Common::Status Parse(OpsetIdList &opsets)#
-
bool NextIsType()#
-
bool NextIsIdentifier()#
-
inline explicit OnnxParser(const char *cstr)#
-
class ParserBase#
- #include <parser.h>
Cursor-based tokenizer that drives parsing of ONNX text format.
Maintains a read cursor over a character buffer and provides low-level helpers for skipping whitespace, matching characters, reading literals (integer, float, string), and parsing identifiers. OnnxParser inherits from this class to build higher-level protobuf parsing on top.
Subclassed by ONNX_LIGHT_NAMESPACE::OnnxParser
Public Types
Public Functions
-
inline explicit ParserBase(const std::string &str)#
Creates a parser from a string buffer. The underlying buffer must remain valid and unchanged during the parser lifetime.
-
inline explicit ParserBase(const char *cstr)#
Creates a parser from a null-terminated string. The referenced character buffer must outlive the parser instance.
-
inline void SavePos()#
Saves the current parser cursor position.
-
inline void RestorePos()#
Restores the parser cursor to the most recently saved position.
-
inline std::string GetErrorContext()#
Returns a line of source context around the current cursor position for error messages.
-
template<typename ...Args>
inline Common::Status ParseError(const Args&... args)# Generates a parse error with current position and local context.
-
inline void SkipWhiteSpace()#
Advances the cursor past whitespace characters and
#-prefixed line comments.
-
inline int NextChar(bool skipspace = true)#
Returns the next character without consuming it; returns 0 at end of input.
- Parameters:
skipspace – When true (default), skips whitespace before peeking.
-
inline bool Matches(char ch, bool skipspace = true)#
Consumes
chif it is the next character and returns true; otherwise returns false.- Parameters:
skipspace – When true (default), skips whitespace before matching.
-
inline Common::Status Match(char ch, bool skipspace = true)#
Consumes
chor returns a parse error if it is not the next character.- Parameters:
skipspace – When true (default), skips whitespace before matching.
-
inline bool EndOfInput()#
Returns true when all remaining input (after skipping whitespace) has been consumed.
-
inline Common::Status Parse(int64_t &val)#
Parses a required integer literal into
int64_t. Returns a parse error if the next token is not an integer literal.
-
inline Common::Status Parse(uint64_t &val)#
Parses a required integer literal into
uint64_t. Returns a parse error if the next token is not an integer literal.
-
inline Common::Status Parse(std::string &val)#
Parses a double-quoted string literal; returns a parse error if a string is not found.
-
inline std::string ParseOptionalIdentifier()#
Parses an optional identifier (including keywords); returns an empty string if none found.
-
inline Common::Status ParseIdentifier(std::string &id)#
Parses an identifier and fails if none is found.
-
inline Common::Status ParseQuotableIdentifier(std::string &id)#
Parses a quoted identifier or an unquoted identifier.
-
inline Common::Status ParseOptionalQuotableIdentifier(std::string &id)#
Parses an optional identifier that may be enclosed in double quotes. Sets
idto the identifier text (possibly empty) and returns OK.
-
inline Common::Status ParseOptionalQuotableIdentifier(std::string &id, bool &id_found)#
Parses an optional quotable identifier and sets
id_foundto indicate whether one was found.An empty string followed by a comma is treated as a valid but empty identifier to support operand-list patterns such as
Op(,x)(two operands, first empty) versusOp()(no operands). A trailing comma after a non-empty identifier is silently accepted. Using""as an explicit empty identifier is preferred over relying on this behavior.
-
inline Common::Status Parse(KeyWordMap::KeyWord &keyword)#
Parses a keyword token.
Protected Functions
-
bool NextIsValidFloatString()#
Returns true if the characters at the current position form a valid floating-point literal.
Protected Attributes
-
const char *start_#
Pointer to the beginning of the input buffer.
-
const char *next_#
Pointer to the current read position within the input buffer.
-
const char *end_#
Pointer to one past the last character of the input buffer.
-
const char *saved_pos_#
Cursor snapshot written by SavePos() and restored by RestorePos().
-
struct Literal#
- #include <parser.h>
Holds the raw text and classification of a parsed literal token.
-
inline explicit ParserBase(const std::string &str)#
-
class PrimitiveTypeNameMap : public ONNX_LIGHT_NAMESPACE::StringIntMap<PrimitiveTypeNameMap>#
- #include <parser.h>
Maps ONNX primitive type name strings to TensorProto::DataType values.
Supports all scalar element types such as “float”, “int64”, “bfloat16”, and the low-precision types “float8e4m3fn”, “uint4”, “float4e2m1”, etc.
Public Functions
-
inline PrimitiveTypeNameMap()#
-
inline PrimitiveTypeNameMap()#
-
template<typename Map>
class StringIntMap# - #include <parser.h>
CRTP singleton base that maps string names to integer codes.
Subclasses populate
map_in their constructor and gain static Instance(), Lookup(), and ToString() helpers automatically.- Template Parameters:
Map – Concrete subclass (CRTP pattern).
Subclassed by ONNX_LIGHT_NAMESPACE::AttributeTypeNameMap, ONNX_LIGHT_NAMESPACE::PrimitiveTypeNameMap
-
using NodeList = utils::RepeatedProtoField<NodeProto>#