libdonut  2.3.2
Application framework for cross-platform game development in C++20
Classes | Public Member Functions | List of all members
donut::json::Parser< It > Class Template Reference

Syntactic analyzer for parsing input in the JSON5 format obtained from a json::Lexer. More...

#include <donut/json.hpp>

Classes

struct  ConcretePropertyVisitor
 Implementation of PropertyVisitor for freestanding classes that implement all or parts of its interface without directly inheriting from it. More...
 
struct  ConcreteValueVisitor
 Implementation of ValueVisitor for freestanding classes that implement all or parts of its interface without directly inheriting from it. More...
 
class  PropertyVisitor
 Polymorphic interface for visitation-based parsing of JSON object properties. More...
 
struct  SkipPropertyVisitor
 Implementation of PropertyVisitor that skips over the parsed property and discards the result. More...
 
struct  SkipValueVisitor
 Implementation of ValueVisitor that skips over the parsed value and discards the result. More...
 
class  ValueVisitor
 Polymorphic interface for visitation-based parsing of JSON values. More...
 

Public Member Functions

 Parser (Lexer< It > lexer)
 Construct a parser with an existing lexer as input. More...
 
 Parser (unicode::UTF8View codePoints) requires(std
 Construct a parser with a contiguous UTF-8 view as input. More...
 
 Parser (std::u8string_view jsonString) requires(std
 Construct a parser with a contiguous UTF-8 string as input. More...
 
 Parser (std::string_view jsonString) requires(std
 Construct a parser with a contiguous string of bytes, interpreted as UTF-8, as input. More...
 
 Parser (std::istream &stream) requires(std
 Construct a parser with an input stream as input. More...
 
 Parser (std::streambuf *streambuf) requires(std
 Construct a parser with an input stream buffer as input. More...
 
void parseFile (ValueVisitor &visitor)
 Read a single JSON value from the input and visit it, then make sure the rest of the input only consists of whitespace. More...
 
void parseValue (ValueVisitor &visitor)
 Read a single JSON value from the input and visit it. More...
 
void parseObject (PropertyVisitor &visitor)
 Read a single JSON object from the input and visit each of its properties. More...
 
void parseArray (ValueVisitor &visitor)
 Read a single JSON array from the input and visit each of its values. More...
 
void parseFile (ValueVisitor &&visitor)
 
void parseValue (ValueVisitor &&visitor)
 
void parseObject (PropertyVisitor &&visitor)
 
void parseArray (ValueVisitor &&visitor)
 
template<typename Visitor >
void parseFile (Visitor visitor)
 
template<typename Visitor >
void parseValue (Visitor visitor)
 
template<typename Visitor >
void parseObject (Visitor visitor)
 
template<typename Visitor >
void parseArray (Visitor visitor)
 
void skipFile ()
 Parse a single JSON value from the input and discard the result, then make sure the rest of the input only consists of whitespace. More...
 
void skipValue ()
 Parse a single JSON value from the input and discard the result. More...
 
Value parseFile ()
 Read a single JSON value from the input and make sure the rest of the input only consists of whitespace. More...
 
Value parseValue ()
 Read a single JSON value from the input. More...
 
Null parseNull ()
 Read a single JSON value of type Null from the input. More...
 
Boolean parseBoolean ()
 Read a single JSON value of type Boolean from the input. More...
 
String parseString ()
 Read a single JSON value of type String from the input. More...
 
Number parseNumber ()
 Read a single JSON value of type Number from the input. More...
 
Object parseObject ()
 Read a single JSON value of type Object from the input. More...
 
Array parseArray ()
 Read a single JSON value of type Array from the input. More...
 
void advance ()
 Advance the internal state of the underlying lexer by one token. More...
 
const Tokenpeek () const
 Peek the next token without advancing the internal state of the underlying lexer. More...
 
Token eat ()
 Scan and consume the next token from the input. More...
 

Detailed Description

template<typename It>
class donut::json::Parser< It >

Syntactic analyzer for parsing input in the JSON5 format obtained from a json::Lexer.

Template Parameters
Ititerator type of the underlying input source. Must be an input iterator.
Examples
example_game.cpp.

Constructor & Destructor Documentation

◆ Parser() [1/6]

template<typename It >
donut::json::Parser< It >::Parser ( Lexer< It >  lexer)
inlineexplicit

Construct a parser with an existing lexer as input.

Parameters
lexerlexer to scan JSON tokens from.

◆ Parser() [2/6]

template<typename It >
donut::json::Parser< It >::Parser ( unicode::UTF8View  codePoints)
inlineexplicit

Construct a parser with a contiguous UTF-8 view as input.

Parameters
codePointsnon-owning read-only view over the UTF-8 string to parse JSON tokens from.

◆ Parser() [3/6]

template<typename It >
donut::json::Parser< It >::Parser ( std::u8string_view  jsonString)
inlineexplicit

Construct a parser with a contiguous UTF-8 string as input.

Parameters
jsonStringnon-owning read-only view over the UTF-8 string to parse JSON tokens from.

◆ Parser() [4/6]

template<typename It >
donut::json::Parser< It >::Parser ( std::string_view  jsonString)
inlineexplicit

Construct a parser with a contiguous string of bytes, interpreted as UTF-8, as input.

Parameters
jsonStringnon-owning read-only view over the byte string to parse JSON tokens from.

◆ Parser() [5/6]

template<typename It >
donut::json::Parser< It >::Parser ( std::istream &  stream)
inlineexplicit

Construct a parser with an input stream as input.

Parameters
streaminput stream to parse JSON tokens from.

◆ Parser() [6/6]

template<typename It >
donut::json::Parser< It >::Parser ( std::streambuf *  streambuf)
inlineexplicit

Construct a parser with an input stream buffer as input.

Parameters
streambufinput stream buffer to parse JSON tokens from.

Member Function Documentation

◆ parseFile() [1/4]

template<typename It >
void donut::json::Parser< It >::parseFile ( ValueVisitor visitor)
inline

Read a single JSON value from the input and visit it, then make sure the rest of the input only consists of whitespace.

Parameters
visitorvisitor to give the parsed value to.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
anyexception thrown by the visitor.
See also
json::onNull()
json::onBoolean()
json::onString()
json::onNumber()
json::onObject()
json::onArray()
parseFile()
parseValue(ValueVisitor&)

◆ parseValue() [1/4]

template<typename It >
void donut::json::Parser< It >::parseValue ( ValueVisitor visitor)
inline

Read a single JSON value from the input and visit it.

Parameters
visitorvisitor to give the parsed value to.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
anyexception thrown by the visitor.
See also
json::onNull()
json::onBoolean()
json::onString()
json::onNumber()
json::onObject()
json::onArray()
parseValue()
parseFile(ValueVisitor&)
Examples
example_game.cpp.

◆ parseObject() [1/4]

template<typename It >
void donut::json::Parser< It >::parseObject ( PropertyVisitor visitor)
inline

Read a single JSON object from the input and visit each of its properties.

Parameters
visitorvisitor to give each parsed property of the object to.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
anyexception thrown by the visitor.
See also
json::onProperty()
parseObject()
Examples
example_game.cpp.

◆ parseArray() [1/4]

template<typename It >
void donut::json::Parser< It >::parseArray ( ValueVisitor visitor)
inline

Read a single JSON array from the input and visit each of its values.

Parameters
visitorvisitor to give each parsed value of the array to.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
anyexception thrown by the visitor.
See also
json::onNull()
json::onBoolean()
json::onString()
json::onNumber()
json::onObject()
json::onArray()
parseArray()
Examples
example_game.cpp.

◆ parseFile() [2/4]

template<typename It >
void donut::json::Parser< It >::parseFile ( ValueVisitor &&  visitor)
inline

◆ parseValue() [2/4]

template<typename It >
void donut::json::Parser< It >::parseValue ( ValueVisitor &&  visitor)
inline

◆ parseObject() [2/4]

template<typename It >
void donut::json::Parser< It >::parseObject ( PropertyVisitor &&  visitor)
inline

◆ parseArray() [2/4]

template<typename It >
void donut::json::Parser< It >::parseArray ( ValueVisitor &&  visitor)
inline

◆ parseFile() [3/4]

template<typename It >
template<typename Visitor >
void donut::json::Parser< It >::parseFile ( Visitor  visitor)
inline

◆ parseValue() [3/4]

template<typename It >
template<typename Visitor >
void donut::json::Parser< It >::parseValue ( Visitor  visitor)
inline

◆ parseObject() [3/4]

template<typename It >
template<typename Visitor >
void donut::json::Parser< It >::parseObject ( Visitor  visitor)
inline

◆ parseArray() [3/4]

template<typename It >
template<typename Visitor >
void donut::json::Parser< It >::parseArray ( Visitor  visitor)
inline

◆ skipFile()

template<typename It >
void donut::json::Parser< It >::skipFile ( )
inline

Parse a single JSON value from the input and discard the result, then make sure the rest of the input only consists of whitespace.

Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
See also
parseFile()
skipValue()

◆ skipValue()

template<typename It >
void donut::json::Parser< It >::skipValue ( )
inline

Parse a single JSON value from the input and discard the result.

Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
See also
parseValue()
skipFile()

◆ parseFile() [4/4]

template<typename It >
Value donut::json::Parser< It >::parseFile ( )
inline

Read a single JSON value from the input and make sure the rest of the input only consists of whitespace.

Returns
the parsed value.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
See also
parseFile(ValueVisitor&)
parseValue()
skipFile()

◆ parseValue() [4/4]

template<typename It >
Value donut::json::Parser< It >::parseValue ( )
inline

Read a single JSON value from the input.

Returns
the parsed value.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
See also
parseValue(ValueVisitor&)
parseFile()
parseNull()
parseBoolean()
parseString()
parseNumber()
parseObject()
parseArray()
skipValue()

◆ parseNull()

template<typename It >
Null donut::json::Parser< It >::parseNull ( )
inline

Read a single JSON value of type Null from the input.

Returns
the parsed value.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
See also
parseValue()

◆ parseBoolean()

template<typename It >
Boolean donut::json::Parser< It >::parseBoolean ( )
inline

Read a single JSON value of type Boolean from the input.

Returns
the parsed value.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
See also
parseValue()

◆ parseString()

template<typename It >
String donut::json::Parser< It >::parseString ( )
inline

Read a single JSON value of type String from the input.

Returns
the parsed value.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
See also
parseValue()

◆ parseNumber()

template<typename It >
Number donut::json::Parser< It >::parseNumber ( )
inline

Read a single JSON value of type Number from the input.

Returns
the parsed value.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
See also
parseValue()

◆ parseObject() [4/4]

template<typename It >
Object donut::json::Parser< It >::parseObject ( )
inline

Read a single JSON value of type Object from the input.

Returns
the parsed value.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
See also
parseObject(PropertyVisitor&)
parseValue()

◆ parseArray() [4/4]

template<typename It >
Array donut::json::Parser< It >::parseArray ( )
inline

Read a single JSON value of type Array from the input.

Returns
the parsed value.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
See also
parseArray(ValueVisitor&)
parseValue()

◆ advance()

template<typename It >
void donut::json::Parser< It >::advance ( )
inline

Advance the internal state of the underlying lexer by one token.

Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.

◆ peek()

template<typename It >
const Token& donut::json::Parser< It >::peek ( ) const
inline

Peek the next token without advancing the internal state of the underlying lexer.

Returns
a read-only reference to the next token to be read that is valid until the next call to advance() or eat(), or until the parser is moved from or destroyed, whichever happens first.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.
Warning
Although it is const, this function is not thread-safe since it mutates an internal lookahead buffer. Exclusive access is therefore required for safety.

◆ eat()

template<typename It >
Token donut::json::Parser< It >::eat ( )
inline

Scan and consume the next token from the input.

This advances the internal state of the underlying lexer.

Returns
the scanned token.
Exceptions
json::Erroron invalid input.
std::bad_allocon allocation failure.
anyexception thrown by the underlying input iterator.

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