Gyoji Compiler
|
Weakly-typed syntax node. More...
#include <syntax-node.hpp>
Public Member Functions | |
SyntaxNode (Gyoji::context::TokenID _type, specific_type_t _data, const Gyoji::context::SourceReference &_source_ref) | |
const std::vector< std::reference_wrapper< const SyntaxNode > > & | get_children () const |
const Gyoji::context::TokenID & | get_type () const |
template<class T > | |
bool | has_data () const |
template<class T > | |
const T & | get_data () const |
const SyntaxNode & | get_syntax_node () const |
const Gyoji::context::SourceReference & | get_source_ref () const |
Weakly-typed syntax node.
The syntax tree can be viewed as a tree of nodes that follow the structure of the parsed grammar.
This view of the syntax tree can be processed in a weakly-typed sense where each node of type SyntaxNode may have zero or more children that represent grammar productions below them.
Each of these nodes can also, optionally, be examined to find the specific strongly-typed tree node corresponding to the data parsed.
For example, the top-level SyntaxNode will hold a TranslationUnit so that it can be extracted:
SyntaxNode *s; if (s->holds_alternative<TranslationUnit>()) { const TranslationUnit & tu = s->get<TranslationUnit>(); }
The leaf nodes of the SyntaxTree will always be of type "Terminal" indicating that it is a "terminal" symbol of the grammer which corresponds to a parsed token from the lexer (gyoji.l).
SyntaxNode::SyntaxNode | ( | Gyoji::context::TokenID | _type, |
SyntaxNode::specific_type_t | _data, | ||
const Gyoji::context::SourceReference & | _source_ref | ||
) |
Create a new syntax node of the given type holding the data associated with that production in the BNF grammar.
_type | Type of node this represents. |
_data | The specific data associated with this node. |
const std::vector< std::reference_wrapper< const SyntaxNode > > & SyntaxNode::get_children | ( | ) | const |
This method returns a reference to an immutable array of children of this node.
|
inline |
This returns an immutable reference to the data of type T contained in this node. Note that this is only safe to use if the holds_alternative<T>() returns true for this specific type.
T | The type of data to return. |
const SourceReference & SyntaxNode::get_source_ref | ( | ) | const |
Returns the source reference for where this node appears in the source tree.
const SyntaxNode & SyntaxNode::get_syntax_node | ( | ) | const |
This method is provided so that callers of derived classes can be sure to get access to the SyntaxNode base-class instance.
const TokenID & SyntaxNode::get_type | ( | ) | const |
This method returns an immutable reference to the type of the node.
|
inline |
This interrogates the node to determine if it contains data of the specified type T.
T | Check type T to see if it is held in this node. |