-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser.lua
More file actions
67 lines (58 loc) · 2 KB
/
test_parser.lua
File metadata and controls
67 lines (58 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
local ltreesitter = require("ltreesitter")
-- Load TypeScript parser
local parser = ltreesitter.load("tree-sitter/typescript.so", "typescript")
print("Parser loaded:", parser)
local source = [[
function login(username, password) {
const user = fetchUser(username);
if (user.isValid(password)) {
return createSession(user);
}
throw new Error("Invalid credentials");
}
const logout = () => {
clearSession();
redirect("/home");
};
]]
local tree = parser:parse_string(source)
print("Tree root:", tree:root():type())
-- Test query: find function declarations
local q = parser:query("(function_declaration name: (identifier) @name) @def")
print("\nFunction declarations:")
for match in q:match(tree:root()) do
print(" Match id:", match.id)
for name, node in pairs(match.captures) do
print(" capture:", name, "type:", node:type(), "source:", node:source():sub(1, 50))
end
end
-- Test query: find call expressions
local q2 = parser:query("(call_expression function: (identifier) @call_name) @call_expr")
print("\nCall expressions:")
for match in q2:match(tree:root()) do
for name, node in pairs(match.captures) do
print(" capture:", name, "type:", node:type(), "text:", node:source():sub(1, 50))
end
end
-- Test: find arrow functions
local q3 = parser:query([[
(variable_declarator
name: (identifier) @name
value: (arrow_function) @def)
]])
print("\nArrow functions:")
for match in q3:match(tree:root()) do
for name, node in pairs(match.captures) do
print(" capture:", name, "type:", node:type(), "text:", node:source():sub(1, 50))
end
end
-- Test node position API
print("\nNode position API:")
local root = tree:root()
local first_child = root:child(0)
print(" start_point:", first_child:start_point())
print(" end_point:", first_child:end_point())
print(" start_byte:", first_child:start_byte())
print(" end_byte:", first_child:end_byte())
print(" named_child_count:", first_child:named_child_count())
print("\nSUCCESS: All API tests passed!")