File src/ts/TSTransformer.cpp changed (mode: 100644) (index cedce55..752427d) |
... |
... |
TSTransformer::transform() |
62 |
62 |
line = 1; |
line = 1; |
63 |
63 |
col = 1; |
col = 1; |
64 |
64 |
|
|
65 |
|
tb.setRoot(visit(ts_tree_root_node(tree.get()))); |
|
|
65 |
|
tb.setRoot(visit(ts_tree_root_node(tree.get()), Type::Other)); |
66 |
66 |
|
|
67 |
67 |
if (debug) { |
if (debug) { |
68 |
68 |
for (const std::string &type : badSTypes) { |
for (const std::string &type : badSTypes) { |
|
... |
... |
TSTransformer::transform() |
75 |
75 |
} |
} |
76 |
76 |
|
|
77 |
77 |
PNode * |
PNode * |
78 |
|
TSTransformer::visit(const TSNode &node) |
|
|
78 |
|
TSTransformer::visit(const TSNode &node, Type defType) |
79 |
79 |
{ |
{ |
80 |
80 |
SType stype = {}; |
SType stype = {}; |
81 |
81 |
const char *type = ts_node_type(node); |
const char *type = ts_node_type(node); |
|
... |
... |
TSTransformer::visit(const TSNode &node) |
89 |
89 |
badSTypes.insert(type + (": `" + val.to_string() + '`')); |
badSTypes.insert(type + (": `" + val.to_string() + '`')); |
90 |
90 |
} |
} |
91 |
91 |
|
|
|
92 |
|
auto typeIt = types.find(type); |
|
93 |
|
if (typeIt != types.end()) { |
|
94 |
|
defType = typeIt->second; |
|
95 |
|
} |
|
96 |
|
|
92 |
97 |
PNode *pnode = tb.addNode({}, stype); |
PNode *pnode = tb.addNode({}, stype); |
93 |
98 |
|
|
94 |
99 |
uint32_t childCount = ts_node_child_count(node); |
uint32_t childCount = ts_node_child_count(node); |
|
... |
... |
TSTransformer::visit(const TSNode &node) |
101 |
106 |
stype = it->second; |
stype = it->second; |
102 |
107 |
} |
} |
103 |
108 |
|
|
104 |
|
visitLeaf(stype, pnode, child); |
|
|
109 |
|
visitLeaf(stype, pnode, child, defType); |
105 |
110 |
} else { |
} else { |
106 |
|
tb.append(pnode, visit(child)); |
|
|
111 |
|
tb.append(pnode, visit(child, defType)); |
107 |
112 |
} |
} |
108 |
113 |
} |
} |
109 |
114 |
|
|
|
... |
... |
TSTransformer::visit(const TSNode &node) |
111 |
116 |
} |
} |
112 |
117 |
|
|
113 |
118 |
void |
void |
114 |
|
TSTransformer::visitLeaf(SType stype, PNode *pnode, const TSNode &leaf) |
|
|
119 |
|
TSTransformer::visitLeaf(SType stype, |
|
120 |
|
PNode *pnode, |
|
121 |
|
const TSNode &leaf, |
|
122 |
|
Type defType) |
115 |
123 |
{ |
{ |
116 |
124 |
if (badNodes.find(ts_node_type(leaf)) != badNodes.end()) { |
if (badNodes.find(ts_node_type(leaf)) != badNodes.end()) { |
117 |
125 |
return; |
return; |
|
... |
... |
TSTransformer::visitLeaf(SType stype, PNode *pnode, const TSNode &leaf) |
125 |
133 |
|
|
126 |
134 |
boost::string_ref val(contents.c_str() + from, to - from); |
boost::string_ref val(contents.c_str() + from, to - from); |
127 |
135 |
Type type = determineType(leaf); |
Type type = determineType(leaf); |
|
136 |
|
if (type == Type::Other) { |
|
137 |
|
type = defType; |
|
138 |
|
} |
128 |
139 |
|
|
129 |
140 |
if (stype == SType{} && isSeparator(type)) { |
if (stype == SType{} && isSeparator(type)) { |
130 |
141 |
stype = stypes.at("separator"); |
stype = stypes.at("separator"); |
File src/ts/TSTransformer.hpp changed (mode: 100644) (index c78985c..1a8b1f9) |
... |
... |
public: |
49 |
49 |
void transform(); |
void transform(); |
50 |
50 |
|
|
51 |
51 |
private: |
private: |
52 |
|
// Transforms a single node. |
|
53 |
|
PNode * visit(const TSNode &node); |
|
|
52 |
|
// Transforms a single node while propagating last node type to leafs |
|
53 |
|
// without type. |
|
54 |
|
PNode * visit(const TSNode &node, Type defType); |
54 |
55 |
// Transforms a leaf. |
// Transforms a leaf. |
55 |
|
void visitLeaf(SType stype, PNode *pnode, const TSNode &leaf); |
|
|
56 |
|
void visitLeaf(SType stype, PNode *pnode, const TSNode &leaf, Type defType); |
56 |
57 |
// Determines type of a child of the specified node. |
// Determines type of a child of the specified node. |
57 |
58 |
Type determineType(const TSNode &node); |
Type determineType(const TSNode &node); |
58 |
59 |
|
|