|
26 | 26 |
|
27 | 27 | namespace chaiscript { |
28 | 28 | struct AST_Node; |
| 29 | + struct AST_Node_Trace; |
| 30 | + namespace exception { |
| 31 | + struct eval_error; |
| 32 | + } |
29 | 33 | } // namespace chaiscript |
30 | 34 |
|
31 | 35 | namespace chaiscript { |
@@ -166,11 +170,104 @@ namespace chaiscript { |
166 | 170 | std::shared_ptr<std::string> filename; |
167 | 171 | }; |
168 | 172 |
|
| 173 | + /// \brief Struct that doubles as both a parser ast_node and an AST node. |
| 174 | + struct AST_Node { |
| 175 | + public: |
| 176 | + const AST_Node_Type identifier; |
| 177 | + const std::string text; |
| 178 | + Parse_Location location; |
| 179 | + |
| 180 | + const std::string &filename() const noexcept { return *location.filename; } |
| 181 | + |
| 182 | + const File_Position &start() const noexcept { return location.start; } |
| 183 | + |
| 184 | + const File_Position &end() const noexcept { return location.end; } |
| 185 | + |
| 186 | + std::string pretty_print() const { |
| 187 | + std::ostringstream oss; |
| 188 | + |
| 189 | + oss << text; |
| 190 | + |
| 191 | + for (auto &elem : get_children()) { |
| 192 | + oss << elem.get().pretty_print() << ' '; |
| 193 | + } |
| 194 | + |
| 195 | + return oss.str(); |
| 196 | + } |
| 197 | + |
| 198 | + virtual std::vector<std::reference_wrapper<AST_Node>> get_children() const = 0; |
| 199 | + virtual Boxed_Value eval(const chaiscript::detail::Dispatch_State &t_e) const = 0; |
| 200 | + |
| 201 | + /// Prints the contents of an AST node, including its children, recursively |
| 202 | + std::string to_string(const std::string &t_prepend = "") const { |
| 203 | + std::ostringstream oss; |
| 204 | + |
| 205 | + oss << t_prepend << "(" << ast_node_type_to_string(this->identifier) << ") " << this->text << " : " << this->location.start.line |
| 206 | + << ", " << this->location.start.column << '\n'; |
| 207 | + |
| 208 | + for (auto &elem : get_children()) { |
| 209 | + oss << elem.get().to_string(t_prepend + " "); |
| 210 | + } |
| 211 | + return oss.str(); |
| 212 | + } |
| 213 | + |
| 214 | + static inline bool get_bool_condition(const Boxed_Value &t_bv, const chaiscript::detail::Dispatch_State &t_ss); |
| 215 | + |
| 216 | + virtual ~AST_Node() noexcept = default; |
| 217 | + AST_Node(AST_Node &&) = default; |
| 218 | + AST_Node &operator=(AST_Node &&) = delete; |
| 219 | + AST_Node(const AST_Node &) = delete; |
| 220 | + AST_Node &operator=(const AST_Node &) = delete; |
| 221 | + |
| 222 | + protected: |
| 223 | + AST_Node(std::string t_ast_node_text, AST_Node_Type t_id, Parse_Location t_loc) |
| 224 | + : identifier(t_id) |
| 225 | + , text(std::move(t_ast_node_text)) |
| 226 | + , location(std::move(t_loc)) { |
| 227 | + } |
| 228 | + }; |
| 229 | + |
169 | 230 | /// \brief Typedef for pointers to AST_Node objects. Used in building of the AST_Node tree |
170 | 231 | using AST_NodePtr = std::unique_ptr<AST_Node>; |
171 | 232 | using AST_NodePtr_Const = std::unique_ptr<const AST_Node>; |
172 | 233 |
|
173 | | - struct AST_Node_Trace; |
| 234 | + struct AST_Node_Trace { |
| 235 | + const AST_Node_Type identifier; |
| 236 | + const std::string text; |
| 237 | + Parse_Location location; |
| 238 | + |
| 239 | + const std::string &filename() const noexcept { return *location.filename; } |
| 240 | + |
| 241 | + const File_Position &start() const noexcept { return location.start; } |
| 242 | + |
| 243 | + const File_Position &end() const noexcept { return location.end; } |
| 244 | + |
| 245 | + std::string pretty_print() const { |
| 246 | + std::ostringstream oss; |
| 247 | + |
| 248 | + oss << text; |
| 249 | + |
| 250 | + for (const auto &elem : children) { |
| 251 | + oss << elem.pretty_print() << ' '; |
| 252 | + } |
| 253 | + |
| 254 | + return oss.str(); |
| 255 | + } |
| 256 | + |
| 257 | + std::vector<AST_Node_Trace> get_children(const AST_Node &node) { |
| 258 | + const auto node_children = node.get_children(); |
| 259 | + return std::vector<AST_Node_Trace>(node_children.begin(), node_children.end()); |
| 260 | + } |
| 261 | + |
| 262 | + AST_Node_Trace(const AST_Node &node) |
| 263 | + : identifier(node.identifier) |
| 264 | + , text(node.text) |
| 265 | + , location(node.location) |
| 266 | + , children(get_children(node)) { |
| 267 | + } |
| 268 | + |
| 269 | + std::vector<AST_Node_Trace> children; |
| 270 | + }; |
174 | 271 |
|
175 | 272 | /// \brief Classes which may be thrown during error cases when ChaiScript is executing. |
176 | 273 | namespace exception { |
@@ -495,106 +592,14 @@ namespace chaiscript { |
495 | 592 |
|
496 | 593 | } // namespace exception |
497 | 594 |
|
498 | | - /// \brief Struct that doubles as both a parser ast_node and an AST node. |
499 | | - struct AST_Node { |
500 | | - public: |
501 | | - const AST_Node_Type identifier; |
502 | | - const std::string text; |
503 | | - Parse_Location location; |
504 | | - |
505 | | - const std::string &filename() const noexcept { return *location.filename; } |
506 | | - |
507 | | - const File_Position &start() const noexcept { return location.start; } |
508 | | - |
509 | | - const File_Position &end() const noexcept { return location.end; } |
510 | | - |
511 | | - std::string pretty_print() const { |
512 | | - std::ostringstream oss; |
513 | | - |
514 | | - oss << text; |
515 | | - |
516 | | - for (auto &elem : get_children()) { |
517 | | - oss << elem.get().pretty_print() << ' '; |
518 | | - } |
519 | | - |
520 | | - return oss.str(); |
521 | | - } |
522 | | - |
523 | | - virtual std::vector<std::reference_wrapper<AST_Node>> get_children() const = 0; |
524 | | - virtual Boxed_Value eval(const chaiscript::detail::Dispatch_State &t_e) const = 0; |
525 | | - |
526 | | - /// Prints the contents of an AST node, including its children, recursively |
527 | | - std::string to_string(const std::string &t_prepend = "") const { |
528 | | - std::ostringstream oss; |
529 | | - |
530 | | - oss << t_prepend << "(" << ast_node_type_to_string(this->identifier) << ") " << this->text << " : " << this->location.start.line |
531 | | - << ", " << this->location.start.column << '\n'; |
532 | | - |
533 | | - for (auto &elem : get_children()) { |
534 | | - oss << elem.get().to_string(t_prepend + " "); |
535 | | - } |
536 | | - return oss.str(); |
537 | | - } |
538 | | - |
539 | | - static bool get_bool_condition(const Boxed_Value &t_bv, const chaiscript::detail::Dispatch_State &t_ss) { |
| 595 | + //static |
| 596 | + bool AST_Node::get_bool_condition(const Boxed_Value &t_bv, const chaiscript::detail::Dispatch_State &t_ss) { |
540 | 597 | try { |
541 | 598 | return t_ss->boxed_cast<bool>(t_bv); |
542 | 599 | } catch (const exception::bad_boxed_cast &) { |
543 | 600 | throw exception::eval_error("Condition not boolean"); |
544 | 601 | } |
545 | | - } |
546 | | - |
547 | | - virtual ~AST_Node() noexcept = default; |
548 | | - AST_Node(AST_Node &&) = default; |
549 | | - AST_Node &operator=(AST_Node &&) = delete; |
550 | | - AST_Node(const AST_Node &) = delete; |
551 | | - AST_Node &operator=(const AST_Node &) = delete; |
552 | | - |
553 | | - protected: |
554 | | - AST_Node(std::string t_ast_node_text, AST_Node_Type t_id, Parse_Location t_loc) |
555 | | - : identifier(t_id) |
556 | | - , text(std::move(t_ast_node_text)) |
557 | | - , location(std::move(t_loc)) { |
558 | | - } |
559 | | - }; |
560 | | - |
561 | | - struct AST_Node_Trace { |
562 | | - const AST_Node_Type identifier; |
563 | | - const std::string text; |
564 | | - Parse_Location location; |
565 | | - |
566 | | - const std::string &filename() const noexcept { return *location.filename; } |
567 | | - |
568 | | - const File_Position &start() const noexcept { return location.start; } |
569 | | - |
570 | | - const File_Position &end() const noexcept { return location.end; } |
571 | | - |
572 | | - std::string pretty_print() const { |
573 | | - std::ostringstream oss; |
574 | | - |
575 | | - oss << text; |
576 | | - |
577 | | - for (const auto &elem : children) { |
578 | | - oss << elem.pretty_print() << ' '; |
579 | | - } |
580 | | - |
581 | | - return oss.str(); |
582 | | - } |
583 | | - |
584 | | - std::vector<AST_Node_Trace> get_children(const AST_Node &node) { |
585 | | - const auto node_children = node.get_children(); |
586 | | - return std::vector<AST_Node_Trace>(node_children.begin(), node_children.end()); |
587 | | - } |
588 | | - |
589 | | - AST_Node_Trace(const AST_Node &node) |
590 | | - : identifier(node.identifier) |
591 | | - , text(node.text) |
592 | | - , location(node.location) |
593 | | - , children(get_children(node)) { |
594 | | - } |
595 | | - |
596 | | - std::vector<AST_Node_Trace> children; |
597 | | - }; |
| 602 | + } |
598 | 603 |
|
599 | 604 | namespace parser { |
600 | 605 | class ChaiScript_Parser_Base { |
|
0 commit comments