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

Fix function matcher in Lua
Author: xaizek
Author date (UTC): 2022-11-27 22:33
Committer name: xaizek
Committer date (UTC): 2022-12-03 11:37
Parent(s): a38b6e355ed257e4becca10d5e39b56db851d6ba
Signing key: 99DC5E4DB05F6BE2
Tree: 796f0077ab4546976bec19803f6f42972627e58f
File Lines added Lines deleted
src/ts/lua/TSLuaLanguage.cpp 2 0
tests/tests.cpp 6 0
tests/tests.hpp 3 0
tests/tooling/Matcher.cpp 35 0
File src/ts/lua/TSLuaLanguage.cpp changed (mode: 100644) (index ff4fe6d..1834018)
... ... TsLuaLanguage::classify(SType stype) const
324 324 case TSLuaSType::VariableAssignment: case TSLuaSType::VariableAssignment:
325 325 return MType::Statement; return MType::Statement;
326 326
327 case TSLuaSType::FunctionDefinitionStatement:
328 case TSLuaSType::LocalFunctionDefinitionStatement:
327 329 case TSLuaSType::FunctionDefinition: case TSLuaSType::FunctionDefinition:
328 330 return MType::Function; return MType::Function;
329 331
File tests/tests.cpp changed (mode: 100644) (index 899fafd..77967d4)
... ... parseLua(const std::string &str)
134 134 return parse("test-input.lua", str, true); return parse("test-input.lua", str, true);
135 135 } }
136 136
137 Tree
138 parseBash(const std::string &str)
139 {
140 return parse("test-input.sh", str, true);
141 }
142
137 143 // Parses source into a tree. // Parses source into a tree.
138 144 static Tree static Tree
139 145 parse(const std::string &fileName, const std::string &str, bool coarse) parse(const std::string &fileName, const std::string &str, bool coarse)
File tests/tests.hpp changed (mode: 100644) (index 0853d55..2b1cdfb)
... ... Tree parseCxx(const std::string &str);
142 142 // Parses Lua source into a tree. // Parses Lua source into a tree.
143 143 Tree parseLua(const std::string &str); Tree parseLua(const std::string &str);
144 144
145 // Parses Lua source into a tree.
146 Tree parseBash(const std::string &str);
147
145 148 // Finds the first node of specified type which has a matching value of its // Finds the first node of specified type which has a matching value of its
146 149 // label (or any label if `label` is an empty string). // label (or any label if `label` is an empty string).
147 150 const Node * findNode(const Tree &tree, Type type, const Node * findNode(const Tree &tree, Type type,
File tests/tooling/Matcher.cpp changed (mode: 100644) (index 2b8e66d..47fc5d7)
... ... TEST_CASE("Declaration matcher works", "[tooling][matcher][.srcml]")
220 220 CHECK(nMatches == 4); CHECK(nMatches == 4);
221 221 } }
222 222 } }
223
224 TEST_CASE("Function matcher works", "[tooling][matcher][.srcml]")
225 {
226 Matcher matcher(MType::Function, nullptr);
227
228 int nMatches = 0;
229
230 auto matchHandler = [&](Node */*node*/) {
231 ++nMatches;
232 };
233
234 SECTION("In C") {
235 Tree tree = parseC("void f() {} void g() {}", true);
236 CHECK(matcher.match(tree.getRoot(), *tree.getLanguage(), matchHandler));
237 CHECK(nMatches == 2);
238 }
239 SECTION("In C++") {
240 Tree tree = parseCxx("void f() {} void g() {}");
241 CHECK(matcher.match(tree.getRoot(), *tree.getLanguage(), matchHandler));
242 CHECK(nMatches == 2);
243 }
244 SECTION("In Lua") {
245 Tree tree = parseLua("function f() end local "
246 "function g() end "
247 "a = function() end");
248 CHECK(matcher.match(tree.getRoot(), *tree.getLanguage(), matchHandler));
249 CHECK(nMatches == 3);
250 }
251 SECTION("In Bash") {
252 Tree tree = parseBash("function f() {\n}\n"
253 "g() {\n}");
254 CHECK(matcher.match(tree.getRoot(), *tree.getLanguage(), matchHandler));
255 CHECK(nMatches == 2);
256 }
257 }
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