1 |
commit: 500e3427ff9ec64473f052e8c11a992d34f5e09f |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Fri Jun 10 08:30:05 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Jun 11 08:09:12 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=500e3427 |
7 |
|
8 |
Walker: use namespace instead of static functions |
9 |
|
10 |
We removed the inline keyword as compiler will do it when proper |
11 |
optimization level/flag is on. |
12 |
|
13 |
--- |
14 |
bashast/libbashWalker.g | 137 ++++++++++++++++++++++++----------------------- |
15 |
1 files changed, 70 insertions(+), 67 deletions(-) |
16 |
|
17 |
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g |
18 |
index 5587bfa..962a57d 100644 |
19 |
--- a/bashast/libbashWalker.g |
20 |
+++ b/bashast/libbashWalker.g |
21 |
@@ -62,84 +62,87 @@ options |
22 |
walker = w; |
23 |
} |
24 |
|
25 |
- inline void set_index(const std::string& name, unsigned& index, int value) |
26 |
+ namespace |
27 |
{ |
28 |
- if(value < 0) |
29 |
- throw interpreter_exception((boost::format("Array index is less than 0: \%s[\%d]") \% name \% value).str()); |
30 |
- index = value; |
31 |
- } |
32 |
- |
33 |
- // seek to LT(2) and consume |
34 |
- static void seek_to_next_tree(plibbashWalker ctx) |
35 |
- { |
36 |
- // Current depth of the tree we are traversing |
37 |
- int depth = 1; |
38 |
- |
39 |
- // The beginning should always be ROOT DOWN ANY_TOKEN |
40 |
- // So we start from LA(4) |
41 |
- int index = 4; |
42 |
- for(; depth != 0; ++index) |
43 |
+ void set_index(const std::string& name, unsigned& index, int value) |
44 |
{ |
45 |
- // Go one level done if we encounter DOWN |
46 |
- if(LA(index) == DOWN) |
47 |
- ++depth; |
48 |
- // Go one level up if we encounter UP. When depth==0, we finishe one node |
49 |
- else if(LA(index) == UP) |
50 |
- --depth; |
51 |
+ if(value < 0) |
52 |
+ throw interpreter_exception((boost::format("Array index is less than 0: \%s[\%d]") \% name \% value).str()); |
53 |
+ index = value; |
54 |
} |
55 |
|
56 |
- // Seek to the correct offset and consume. |
57 |
- SEEK(INDEX() + index - 2); |
58 |
- CONSUME(); |
59 |
- } |
60 |
- |
61 |
- // The method is used to append a pattern with another one. Because it's not allowed to append an empty pattern, |
62 |
- // we need the argument 'do_append' to indicate whether the pattern is empty. 'do_append' will be set to true after |
63 |
- // the first assignment. |
64 |
- inline void append(boost::xpressive::sregex& pattern, const boost::xpressive::sregex& new_pattern, bool& do_append) |
65 |
- { |
66 |
- using namespace boost::xpressive; |
67 |
- if(do_append) |
68 |
+ // seek to LT(2) and consume |
69 |
+ void seek_to_next_tree(plibbashWalker ctx) |
70 |
{ |
71 |
- pattern = sregex(pattern >> new_pattern); |
72 |
+ // Current depth of the tree we are traversing |
73 |
+ int depth = 1; |
74 |
+ |
75 |
+ // The beginning should always be ROOT DOWN ANY_TOKEN |
76 |
+ // So we start from LA(4) |
77 |
+ int index = 4; |
78 |
+ for(; depth != 0; ++index) |
79 |
+ { |
80 |
+ // Go one level done if we encounter DOWN |
81 |
+ if(LA(index) == DOWN) |
82 |
+ ++depth; |
83 |
+ // Go one level up if we encounter UP. When depth==0, we finishe one node |
84 |
+ else if(LA(index) == UP) |
85 |
+ --depth; |
86 |
+ } |
87 |
+ |
88 |
+ // Seek to the correct offset and consume. |
89 |
+ SEEK(INDEX() + index - 2); |
90 |
+ CONSUME(); |
91 |
} |
92 |
- else |
93 |
+ |
94 |
+ // The method is used to append a pattern with another one. Because it's not allowed to append an empty pattern, |
95 |
+ // we need the argument 'do_append' to indicate whether the pattern is empty. 'do_append' will be set to true after |
96 |
+ // the first assignment. |
97 |
+ void append(boost::xpressive::sregex& pattern, const boost::xpressive::sregex& new_pattern, bool& do_append) |
98 |
{ |
99 |
- pattern = new_pattern; |
100 |
- do_append = true; |
101 |
+ using namespace boost::xpressive; |
102 |
+ if(do_append) |
103 |
+ { |
104 |
+ pattern = sregex(pattern >> new_pattern); |
105 |
+ } |
106 |
+ else |
107 |
+ { |
108 |
+ pattern = new_pattern; |
109 |
+ do_append = true; |
110 |
+ } |
111 |
} |
112 |
- } |
113 |
|
114 |
- bool match(const std::string& target, |
115 |
- const boost::xpressive::sregex& pattern) |
116 |
- { |
117 |
- return boost::xpressive::regex_match(target, pattern); |
118 |
- } |
119 |
+ bool match(const std::string& target, |
120 |
+ const boost::xpressive::sregex& pattern) |
121 |
+ { |
122 |
+ return boost::xpressive::regex_match(target, pattern); |
123 |
+ } |
124 |
|
125 |
- /// \brief parse the text value of a tree to integer |
126 |
- /// \param the target tree |
127 |
- /// \return the parsed value |
128 |
- static int parse_int(ANTLR3_BASE_TREE* tree) |
129 |
- { |
130 |
- return tree->getText(tree)->toInt32(tree->getText(tree)); |
131 |
- } |
132 |
+ /// \brief parse the text value of a tree to integer |
133 |
+ /// \param the target tree |
134 |
+ /// \return the parsed value |
135 |
+ int parse_int(ANTLR3_BASE_TREE* tree) |
136 |
+ { |
137 |
+ return tree->getText(tree)->toInt32(tree->getText(tree)); |
138 |
+ } |
139 |
|
140 |
- /// \brief a helper function that get the string value |
141 |
- /// of the given pANTLR3_BASE_TREE node. |
142 |
- /// \param the target tree node |
143 |
- /// \return the value of node->text |
144 |
- static std::string get_string(pANTLR3_BASE_TREE node) |
145 |
- { |
146 |
- pANTLR3_COMMON_TOKEN token = node->getToken(node); |
147 |
- // The tree walker may send null pointer here, so return an empty |
148 |
- // string if that's the case. |
149 |
- if(!token->start) |
150 |
- return ""; |
151 |
- // Use reinterpret_cast here because we have to cast C code. |
152 |
- // The real type here is int64_t which is used as a pointer. |
153 |
- // token->stop - token->start + 1 should be bigger than 0. |
154 |
- return std::string(reinterpret_cast<const char *>(token->start), |
155 |
- boost::numeric_cast<unsigned>(token->stop - token->start + 1)); |
156 |
+ /// \brief a helper function that get the string value |
157 |
+ /// of the given pANTLR3_BASE_TREE node. |
158 |
+ /// \param the target tree node |
159 |
+ /// \return the value of node->text |
160 |
+ std::string get_string(pANTLR3_BASE_TREE node) |
161 |
+ { |
162 |
+ pANTLR3_COMMON_TOKEN token = node->getToken(node); |
163 |
+ // The tree walker may send null pointer here, so return an empty |
164 |
+ // string if that's the case. |
165 |
+ if(!token->start) |
166 |
+ return ""; |
167 |
+ // Use reinterpret_cast here because we have to cast C code. |
168 |
+ // The real type here is int64_t which is used as a pointer. |
169 |
+ // token->stop - token->start + 1 should be bigger than 0. |
170 |
+ return std::string(reinterpret_cast<const char *>(token->start), |
171 |
+ boost::numeric_cast<unsigned>(token->stop - token->start + 1)); |
172 |
+ } |
173 |
} |
174 |
} |