Gyoji Compiler
Loading...
Searching...
No Matches
Public Member Functions | List of all members
Gyoji::frontend::ast::SyntaxNode Class Reference

Weakly-typed syntax node. More...

#include <syntax-node.hpp>

Inheritance diagram for Gyoji::frontend::ast::SyntaxNode:
Inheritance graph
[legend]

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 SyntaxNodeget_syntax_node () const
 
const Gyoji::context::SourceReferenceget_source_ref () const
 

Detailed Description

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

Constructor & Destructor Documentation

◆ SyntaxNode()

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.

Parameters
_typeType of node this represents.
_dataThe specific data associated with this node.

Member Function Documentation

◆ get_children()

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.

◆ get_data()

template<class T >
const T & Gyoji::frontend::ast::SyntaxNode::get_data ( ) const
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.

Template Parameters
TThe type of data to return.

◆ get_source_ref()

const SourceReference & SyntaxNode::get_source_ref ( ) const

Returns the source reference for where this node appears in the source tree.

◆ get_syntax_node()

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.

◆ get_type()

const TokenID & SyntaxNode::get_type ( ) const

This method returns an immutable reference to the type of the node.

◆ has_data()

template<class T >
bool Gyoji::frontend::ast::SyntaxNode::has_data ( ) const
inline

This interrogates the node to determine if it contains data of the specified type T.

Template Parameters
TCheck type T to see if it is held in this node.
Returns
True if this class contains that specific type of data.

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