xaizek / zograscope (License: AGPLv3 only) (since 2018-12-07)
Mainly a syntax-aware diff that also provides a number of additional tools.
Commit 71bbd5354b4766db2a0ed1f6e0a4d88db7712768

Propagate types of nodes to leafs in TSTransformer
The type is applied only if leaf has no type of its own.
Author: xaizek
Author date (UTC): 2022-08-28 12:29
Committer name: xaizek
Committer date (UTC): 2022-08-28 12:29
Parent(s): 2dbb98ef1a406f883b4c901f4ac777546ac17102
Signing key: 99DC5E4DB05F6BE2
Tree: 3992a7149acc2be79647156e3d7357017ec6f8c7
File Lines added Lines deleted
src/ts/TSTransformer.cpp 16 5
src/ts/TSTransformer.hpp 4 3
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
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/zograscope

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/zograscope

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master