diff options
| author | Li Jin <dragon-fly@qq.com> | 2017-07-13 16:03:11 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2017-07-13 16:03:11 +0800 |
| commit | cb906e739f27931e9798510cd83725131ed55209 (patch) | |
| tree | 52b465c5eb2250dec3ed3d5f02b86db79653b838 /MoonParser/ast.cpp | |
| parent | 975c3c7dfa032229272c3b225de1127f1605e2d2 (diff) | |
| download | yuescript-cb906e739f27931e9798510cd83725131ed55209.tar.gz yuescript-cb906e739f27931e9798510cd83725131ed55209.tar.bz2 yuescript-cb906e739f27931e9798510cd83725131ed55209.zip | |
rewrite parsing codes with parserlib.
Diffstat (limited to '')
| -rw-r--r-- | MoonParser/ast.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/MoonParser/ast.cpp b/MoonParser/ast.cpp new file mode 100644 index 0000000..66f6493 --- /dev/null +++ b/MoonParser/ast.cpp | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | #include <cassert> | ||
| 2 | #include "ast.hpp" | ||
| 3 | |||
| 4 | |||
| 5 | namespace parserlib { | ||
| 6 | |||
| 7 | |||
| 8 | //current AST container. | ||
| 9 | static ast_container *_current = 0; | ||
| 10 | |||
| 11 | |||
| 12 | /** sets the container under construction to be this. | ||
| 13 | */ | ||
| 14 | ast_container::ast_container() { | ||
| 15 | _current = this; | ||
| 16 | } | ||
| 17 | |||
| 18 | |||
| 19 | /** sets the container under construction to be this. | ||
| 20 | @param src source object. | ||
| 21 | */ | ||
| 22 | ast_container::ast_container(const ast_container &src) { | ||
| 23 | _current = this; | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | /** Asks all members to construct themselves from the stack. | ||
| 28 | The members are asked to construct themselves in reverse order. | ||
| 29 | from a node stack. | ||
| 30 | @param st stack. | ||
| 31 | */ | ||
| 32 | void ast_container::construct(ast_stack &st) { | ||
| 33 | for(ast_member_vector::reverse_iterator it = m_members.rbegin(); | ||
| 34 | it != m_members.rend(); | ||
| 35 | ++it) | ||
| 36 | { | ||
| 37 | ast_member *member = *it; | ||
| 38 | member->construct(st); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | //register the AST member to the current container. | ||
| 44 | void ast_member::_init() { | ||
| 45 | assert(_current); | ||
| 46 | m_container = _current; | ||
| 47 | _current->m_members.push_back(this); | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | /** parses the given input. | ||
| 52 | @param i input. | ||
| 53 | @param g root rule of grammar. | ||
| 54 | @param ws whitespace rule. | ||
| 55 | @param el list of errors. | ||
| 56 | @param ud user data, passed to the parse procedures. | ||
| 57 | @return pointer to ast node created, or null if there was an error. | ||
| 58 | The return object must be deleted by the caller. | ||
| 59 | */ | ||
| 60 | ast_node *parse(input &i, rule &g, error_list &el, void* ud) { | ||
| 61 | ast_stack st; | ||
| 62 | if (!parse(i, g, el, &st, ud)) return 0; | ||
| 63 | assert(st.size() == 1); | ||
| 64 | return st[0]; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | } //namespace parserlib | ||
