-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_json.rb
More file actions
128 lines (109 loc) · 3.1 KB
/
java_json.rb
File metadata and controls
128 lines (109 loc) · 3.1 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
#!/usr/bin/env ruby
# frozen_string_literal: true
# Example: Java Backend with JSON
#
# Forces the Java backend (JRuby native integration).
# Uses JNI to call tree-sitter - optimal for JRuby.
# Includes row number validation to verify line tracking works correctly.
require "bundler/inline"
gemfile do
source "https://gem.coop"
gem "tree_haver", path: File.expand_path("..", __dir__)
end
require "tree_haver"
unless RUBY_ENGINE == "jruby"
puts "⚠️ This example requires JRuby"
puts "Current engine: #{RUBY_ENGINE}"
puts
puts "To run with JRuby:"
puts " jruby examples/java_json.rb"
puts
puts "Or install JRuby:"
puts " rbenv install jruby-9.4.0.0"
puts " rbenv shell jruby-9.4.0.0"
puts " ruby examples/java_json.rb"
exit 1
end
puts "=" * 70
puts "TreeHaver Java Backend - JSON Parsing (JRuby)"
puts "=" * 70
puts
# Multiline source for row number testing
json_source = <<~JSON
{
"backend": "Java",
"engine": "JRuby",
"line": 4
}
JSON
puts "JSON Source:"
puts "-" * 40
puts json_source
puts "-" * 40
puts
# Register JSON
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
# Force Java backend
TreeHaver.backend = :java
puts "Backend: #{TreeHaver.backend_module}"
puts "Capabilities: #{TreeHaver.capabilities.inspect}"
puts "Ruby Engine: #{RUBY_ENGINE} #{JRUBY_VERSION}"
puts
# Parse
parser = TreeHaver::Parser.new
parser.language = TreeHaver::Language.json
tree = parser.parse(json_source)
root = tree.root_node
puts "✓ Parsed: #{root.type} with #{root.child_count} children"
puts
# Row number validation
puts "=== Row Number Validation ==="
row_errors = []
def validate_node_rows(node, depth, row_errors)
indent = " " * depth
start_row = node.start_point.row
end_row = node.end_point.row
start_col = node.start_point.column
end_col = node.end_point.column
puts "#{indent}#{node.type}: rows #{start_row}-#{end_row}, cols #{start_col}-#{end_col}"
# 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_node_rows(child, depth + 1, row_errors) }
end
validate_node_rows(root, 0, row_errors)
puts
if row_errors.empty?
puts "✓ Row numbers look correct!"
puts
puts "=" * 70
puts "Java Backend:"
puts " - Native JRuby integration"
puts " - Uses JNI to call tree-sitter"
puts " - Optimal performance on JRuby"
puts " - Takes advantage of JVM optimizations"
puts "=" * 70
else
puts "✗ Row number issues detected:"
row_errors.each { |err| puts " - #{err}" }
exit 1
end
puts " - Native JRuby integration"
puts " - Uses JNI to call tree-sitter"
puts " - Optimal performance on JRuby"
puts " - Takes advantage of JVM optimizations"
puts "=" * 70
puts " - Best choice for JRuby applications"
puts "=" * 70