-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_json.rb
More file actions
156 lines (132 loc) · 3.6 KB
/
auto_json.rb
File metadata and controls
156 lines (132 loc) · 3.6 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env ruby
# frozen_string_literal: true
# Example: Auto Backend Selection with JSON
#
# This demonstrates tree_haver's automatic backend selection.
# tree_haver will choose the best available backend for your Ruby implementation.
require "bundler/inline"
gemfile do
source "https://gem.coop"
gem "tree_haver", path: File.expand_path("..", __dir__)
gem "ffi" # FFI backend - most portable, works on MRI/JRuby/TruffleRuby
end
require "tree_haver"
puts "=" * 70
puts "TreeHaver Auto Backend - JSON Parsing"
puts "=" * 70
puts
# Example JSON
json_source = <<~JSON
{
"name": "TreeHaver",
"version": "2.0.0",
"description": "Cross-Ruby tree-sitter adapter",
"features": ["MRI", "Rust", "FFI", "Java", "Citrus"],
"config": {
"auto_select": true,
"backends_available": 5
}
}
JSON
puts "JSON Source:"
puts "-" * 70
puts json_source
puts
# Register JSON grammar
puts "Registering JSON grammar..."
finder = TreeHaver::GrammarFinder.new(:json)
if finder.available?
finder.register!
puts "✓ Registered from: #{finder.find_library_path}"
else
puts "✗ tree-sitter-json not found"
puts finder.not_found_message
exit 1
end
puts
# Auto backend selection
puts "Backend Selection:"
puts "-" * 70
puts "Mode: auto (best backend for your platform)"
backend = TreeHaver.backend_module
if backend
puts "Selected: #{backend}"
puts "Capabilities: #{TreeHaver.capabilities.inspect}"
else
puts "✗ No backend available"
exit 1
end
puts
# Parse JSON
puts "Parsing JSON..."
parser = TreeHaver::Parser.new
parser.language = TreeHaver::Language.json
tree = parser.parse(json_source)
puts "✓ Parsed successfully"
puts
# Explore AST
root = tree.root_node
puts "Root Node:"
puts " Type: #{root.type}"
puts " Children: #{root.child_count}"
puts
# Row number validation
puts "=== Row Number Validation ==="
row_errors = []
def validate_rows(node, depth, row_errors)
indent = " " * depth
start_row = node.start_point.row
end_row = node.end_point.row
puts "#{indent}#{node.type}: rows #{start_row}-#{end_row}"
# For multiline JSON, the root object should span multiple rows
if node.type.to_s == "object" && depth == 1
if end_row == start_row && node.to_s.include?("\n")
row_errors << "Object spans multiple lines but end_row == start_row (#{end_row})"
end
end
node.each { |child| validate_rows(child, depth + 1, row_errors) } if depth < 2
end
validate_rows(root, 0, row_errors)
puts
if row_errors.empty?
puts "✓ Row numbers look correct!"
else
puts "✗ Row number issues detected:"
row_errors.each { |err| puts " - #{err}" }
exit 1
end
puts
# Show tree structure
def show_tree(node, indent = 0, max_depth = 3)
return if indent > max_depth
prefix = " " * indent
text = node.text[0..40].gsub("\n", "\\n")
puts "#{prefix}#{node.type}: #{text.inspect}"
if node.child_count > 0 && node.child_count < 10
node.children.each { |child| show_tree(child, indent + 1, max_depth) }
end
end
puts "AST Structure:"
puts "-" * 70
show_tree(root)
puts
# Find all object members
def find_objects(node, results = [])
results << node if node.type == "object"
node.children.each { |child| find_objects(child, results) }
results
end
objects = find_objects(root)
puts "JSON Objects Found: #{objects.count}"
objects.each_with_index do |obj, i|
puts " #{i + 1}. #{obj.text[0..50].tr("\n", " ")}"
end
puts
puts "=" * 70
puts "Auto Backend Benefits:"
puts "=" * 70
puts "✓ Automatically selects fastest available backend"
puts "✓ No manual configuration needed"
puts "✓ Works across MRI, JRuby, TruffleRuby"
puts "✓ Priority: MRI > Rust > FFI > Java > Citrus"
puts "=" * 70