aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/ast.hpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/MoonP/ast.hpp (renamed from MoonParser/ast.hpp)58
1 files changed, 24 insertions, 34 deletions
diff --git a/MoonParser/ast.hpp b/src/MoonP/ast.hpp
index b388e77..f2ef76c 100644
--- a/MoonParser/ast.hpp
+++ b/src/MoonP/ast.hpp
@@ -1,3 +1,13 @@
1/* Copyright (c) 2012, Achilleas Margaritis, modified by Jin Li
2All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
6 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
9THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
10
1#pragma once 11#pragma once
2 12
3 13
@@ -5,7 +15,7 @@
5#include <list> 15#include <list>
6#include <stdexcept> 16#include <stdexcept>
7#include <type_traits> 17#include <type_traits>
8#include "parser.hpp" 18#include "MoonP/parser.hpp"
9 19
10 20
11namespace parserlib { 21namespace parserlib {
@@ -57,7 +67,7 @@ public:
57 from a node stack. 67 from a node stack.
58 @param st stack. 68 @param st stack.
59 */ 69 */
60 virtual void construct(ast_stack& st) {} 70 virtual void construct(ast_stack&) {}
61 71
62 /** interface for visiting AST tree use. 72 /** interface for visiting AST tree use.
63 */ 73 */
@@ -78,11 +88,9 @@ public:
78 88
79 virtual bool visitChild(const std::function<bool (ast_node*)>& func); 89 virtual bool visitChild(const std::function<bool (ast_node*)>& func);
80 90
81 virtual size_t getId() const { return "ast_node"_id; } 91 virtual size_t getId() const = 0;
82 92
83 virtual const char* getName() const { return "ast_node"; } 93 virtual int get_type() = 0;
84
85 virtual int get_type() { return ast_type<ast_node>(); }
86 94
87 template<class T> 95 template<class T>
88 inline ast_ptr<false, T> new_ptr() { 96 inline ast_ptr<false, T> new_ptr() {
@@ -151,10 +159,6 @@ public:
151 virtual traversal traverse(const std::function<traversal (ast_node*)>& func) override; 159 virtual traversal traverse(const std::function<traversal (ast_node*)>& func) override;
152 160
153 virtual bool visitChild(const std::function<bool (ast_node*)>& func) override; 161 virtual bool visitChild(const std::function<bool (ast_node*)>& func) override;
154
155 virtual size_t getId() const override { return "ast_container"_id; }
156
157 virtual const char* getName() const override { return "ast_container"; }
158private: 162private:
159 ast_member_vector m_members; 163 ast_member_vector m_members;
160 164
@@ -285,14 +289,14 @@ public:
285 // check the stack node 289 // check the stack node
286 if (st.empty()) { 290 if (st.empty()) {
287 if (!Required) return; 291 if (!Required) return;
288 throw std::logic_error("Invalid AST stack"); 292 throw std::logic_error("Invalid AST stack.");
289 } 293 }
290 ast_node* node = st.back(); 294 ast_node* node = st.back();
291 if (!ast_ptr::accept(node)) { 295 if (!ast_ptr::accept(node)) {
292 // if the object is not required, simply return 296 // if the object is not required, simply return
293 if (!Required) return; 297 if (!Required) return;
294 // else if the object is mandatory, throw an exception 298 // else if the object is mandatory, throw an exception
295 throw std::logic_error("Invalid AST node"); 299 throw std::logic_error("Invalid AST node.");
296 } 300 }
297 st.pop_back(); 301 st.pop_back();
298 m_ptr = node; 302 m_ptr = node;
@@ -327,12 +331,12 @@ public:
327 virtual void construct(ast_stack& st) override { 331 virtual void construct(ast_stack& st) override {
328 if (st.empty()) { 332 if (st.empty()) {
329 if (!Required) return; 333 if (!Required) return;
330 throw std::logic_error("Invalid AST stack"); 334 throw std::logic_error("Invalid AST stack.");
331 } 335 }
332 ast_node* node = st.back(); 336 ast_node* node = st.back();
333 if (!ast_sel::accept(node)) { 337 if (!ast_sel::accept(node)) {
334 if (!Required) return; 338 if (!Required) return;
335 throw std::logic_error("Invalid AST node"); 339 throw std::logic_error("Invalid AST node.");
336 } 340 }
337 st.pop_back(); 341 st.pop_back();
338 m_ptr = node; 342 m_ptr = node;
@@ -388,20 +392,6 @@ public:
388 node->release(); 392 node->release();
389 } 393 }
390 394
391 void set_front(ast_node* node) {
392 assert(node && accept(node));
393 m_objects.front()->release();
394 m_objects.front() = node;
395 node->retain();
396 }
397
398 void set_back(ast_node* node) {
399 assert(node && accept(node));
400 m_objects.back()->release();
401 m_objects.back() = node;
402 node->retain();
403 }
404
405 const node_container& objects() const { 395 const node_container& objects() const {
406 return m_objects; 396 return m_objects;
407 } 397 }
@@ -455,7 +445,7 @@ public:
455 // end the list parsing 445 // end the list parsing
456 if (!ast_list::accept(node)) { 446 if (!ast_list::accept(node)) {
457 if (Required && m_objects.empty()) { 447 if (Required && m_objects.empty()) {
458 throw std::logic_error("Invalid AST node"); 448 throw std::logic_error("Invalid AST node.");
459 } 449 }
460 return; 450 return;
461 } 451 }
@@ -465,7 +455,7 @@ public:
465 node->retain(); 455 node->retain();
466 } 456 }
467 if (Required && m_objects.empty()) { 457 if (Required && m_objects.empty()) {
468 throw std::logic_error("Invalid AST stack"); 458 throw std::logic_error("Invalid AST stack.");
469 } 459 }
470 } 460 }
471private: 461private:
@@ -493,7 +483,7 @@ public:
493 ast_node* node = st.back(); 483 ast_node* node = st.back();
494 if (!ast_sel_list::accept(node)) { 484 if (!ast_sel_list::accept(node)) {
495 if (Required && m_objects.empty()) { 485 if (Required && m_objects.empty()) {
496 throw std::logic_error("Invalid AST node"); 486 throw std::logic_error("Invalid AST node.");
497 } 487 }
498 return; 488 return;
499 } 489 }
@@ -502,7 +492,7 @@ public:
502 node->retain(); 492 node->retain();
503 } 493 }
504 if (Required && m_objects.empty()) { 494 if (Required && m_objects.empty()) {
505 throw std::logic_error("Invalid AST stack"); 495 throw std::logic_error("Invalid AST stack.");
506 } 496 }
507 } 497 }
508private: 498private:
@@ -548,7 +538,7 @@ private:
548 @return pointer to ast node created, or null if there was an error. 538 @return pointer to ast node created, or null if there was an error.
549 The return object must be deleted by the caller. 539 The return object must be deleted by the caller.
550 */ 540 */
551ast_node* _parse(input &i, rule &g, error_list &el, void* ud); 541ast_node* _parse(input& i, rule& g, error_list& el, void* ud);
552 542
553 543
554/** parses the given input. 544/** parses the given input.
@@ -558,7 +548,7 @@ ast_node* _parse(input &i, rule &g, error_list &el, void* ud);
558 @param ud user data, passed to the parse procedures. 548 @param ud user data, passed to the parse procedures.
559 @return ast nodes. 549 @return ast nodes.
560 */ 550 */
561template <class T> ast_ptr<false, T> parse(input &i, rule &g, error_list &el, void* ud = nullptr) { 551template <class T> ast_ptr<false, T> parse(input& i, rule& g, error_list& el, void* ud = nullptr) {
562 ast_node* node = _parse(i, g, el, ud); 552 ast_node* node = _parse(i, g, el, ud);
563 T* ast = ast_cast<T>(node); 553 T* ast = ast_cast<T>(node);
564 ast_ptr<false, T> ptr; 554 ast_ptr<false, T> ptr;