1
+ /*
2
+ * Licensed under the Apache License, Version 2->0 (the "License");
3
+ * you may not use this file except in compliance with the License->
4
+ * You may obtain a copy of the License at
5
+ *
6
+ * http://www->apache->org/licenses/LICENSE-2->0
7
+ *
8
+ * Unless required by applicable law or agreed to in writing, software
9
+ * distributed under the License is distributed on an "AS IS" BASIS,
10
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied->
11
+ * See the License for the specific language governing permissions and
12
+ * limitations under the License->
13
+ */
14
+
15
+ #include " unparser.h"
16
+
17
+ namespace commonsql {
18
+ namespace parser {
19
+
20
+ void Unparser::printSpecialTokens (const Token* t)
21
+ {
22
+ // special tokens are chained in reverse->
23
+ Token* specialToken = t->specialToken ;
24
+ if (specialToken != nullptr ) {
25
+ while (specialToken->specialToken != nullptr ) {
26
+ specialToken = specialToken->specialToken ;
27
+ }
28
+ while (specialToken != nullptr ) {
29
+ stringBuilder << specialToken->image .c_str ();
30
+ specialToken = specialToken->next ;
31
+ }
32
+ }
33
+ }
34
+
35
+ void Unparser::printTrailingComments ()
36
+ {
37
+ if (lastToken != nullptr && lastToken->kind == EOF) {
38
+ printSpecialTokens (lastToken);
39
+ }
40
+ }
41
+
42
+ void Unparser::printToken (const std::string& s)
43
+ {
44
+ stringBuilder << " " << s.c_str () << " " ;
45
+ }
46
+
47
+ void Unparser::printToken (const Token* t)
48
+ {
49
+ while (lastToken != t) {
50
+ lastToken = lastToken->next ;
51
+ printSpecialTokens (lastToken);
52
+ stringBuilder << lastToken->image ;
53
+ }
54
+ }
55
+
56
+ void Unparser::printUptoToken (const Token* t)
57
+ {
58
+ while (lastToken->next != t) {
59
+ printToken (lastToken->next );
60
+ }
61
+ }
62
+
63
+ void Unparser::moveToEndOfNode (const AstNode* node)
64
+ {
65
+ lastToken = node->endToken ;
66
+ }
67
+
68
+ void Unparser::unparseUpto (const AstNode* node)
69
+ {
70
+ printUptoToken (node->beginToken );
71
+ }
72
+
73
+ void Unparser::unparseRestOfTheNode (const AstNode* node)
74
+ {
75
+ if (lastToken != node->endToken ) {
76
+ printUptoToken (node->endToken );
77
+ }
78
+ }
79
+
80
+ void Unparser::defaultVisit (const SimpleNode* node, void * data)
81
+ {
82
+ const AstNode* astNode = static_cast <const AstNode*>(node);
83
+ const AstNode* firstChild = astNode->firstChild ();
84
+ const AstNode* lastChild = astNode->lastChild ();
85
+
86
+ // Print the tokens->
87
+ if (firstChild == nullptr || astNode->beginToken != firstChild->beginToken ) {
88
+ printToken (astNode->beginToken );
89
+ }
90
+
91
+ astNode->VisitChildren (this );
92
+
93
+ if (lastChild != nullptr && astNode->endToken != lastChild->endToken ) {
94
+ printToken (astNode->endToken );
95
+ }
96
+ }
97
+
98
+ std::string Unparser::unparse (const AstNode* node)
99
+ {
100
+ stringBuilder.clear ();
101
+ lastToken->next = node->beginToken ;
102
+ node->VisitNode (this );
103
+ printTrailingComments ();
104
+ return getUnparsedString ();
105
+ }
106
+
107
+ std::string unparse (const AstNode* node, Unparser* unparser)
108
+ {
109
+ return unparser->unparse (node);
110
+ }
111
+
112
+ std::string unparse (const AstNode* node)
113
+ {
114
+ return unparse (node, new Unparser ());
115
+ }
116
+
117
+ }
118
+ }
0 commit comments