Gyoji Compiler
Loading...
Searching...
No Matches
syntax-node.hpp
1/* Copyright 2025 Jonathan S. Arney
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * https://github.com/jarney/gyoji/blob/master/LICENSE
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef _GYOJI_INTERNAL
16#error "This header is intended to be used internally as a part of the Gyoji front-end. Please include frontend.hpp instead."
17#endif
18#pragma once
19
37
69 class SyntaxNode {
70 public:
71 typedef std::variant<GYOJI_SYNTAX_NODE_VARIANT_LIST> specific_type_t;
72
80 SyntaxNode(Gyoji::context::TokenID _type, specific_type_t _data, const Gyoji::context::SourceReference & _source_ref);
82
83
89
94 const Gyoji::context::TokenID & get_type() const;
95
102 template <class T> bool has_data() const {
103 return std::holds_alternative<T*>(data);
104 }
105
114 template <class T> const T &get_data() const {
115 const T *d = std::get<T*>(data);
116 return *d;
117 }
118
124 const SyntaxNode & get_syntax_node() const;
125
131
132 private:
133 // This list does NOT own its children, so
134 // the class deriving from this one must
135 // agree to own the pointers separately.
137 const Gyoji::context::SourceReference & source_ref;
138
139 protected:
140 // Children are owned by their parents, so this is
141 // private and can only be called by the
142 // deriving class.
143 void add_child(const SyntaxNode & node);
144 void prepend_child(const SyntaxNode & node);
145
146 Gyoji::context::TokenID type;
147 specific_type_t data;
148
149 };
150
151};
References a location in the source-file.
Definition source-reference.hpp:30
Weakly-typed syntax node.
Definition syntax-node.hpp:69
const Gyoji::context::TokenID & get_type() const
Definition syntax-node.cpp:48
bool has_data() const
Definition syntax-node.hpp:102
const SyntaxNode & get_syntax_node() const
Definition syntax-node.cpp:53
const T & get_data() const
Definition syntax-node.hpp:114
const std::vector< std::reference_wrapper< const SyntaxNode > > & get_children() const
Definition syntax-node.cpp:44
const Gyoji::context::SourceReference & get_source_ref() const
Definition syntax-node.cpp:57
Abstract syntax tree.
Definition syntax-node.hpp:36