-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_jsonc.rb
More file actions
103 lines (84 loc) · 2.3 KB
/
java_jsonc.rb
File metadata and controls
103 lines (84 loc) · 2.3 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
#!/usr/bin/env ruby
# frozen_string_literal: true
# Example: Java Backend with JSONC
# Forces Java backend for JSONC parsing (JRuby only).
# 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}"
exit 1
end
puts "=" * 70
puts "TreeHaver Java Backend - JSONC Parsing (JRuby)"
puts "=" * 70
puts
# Multiline source for row number testing
jsonc_source = <<~JSONC
{
"backend": "Java",
/* Block comment */
"jni": true,
// Line comment
"line": 6
}
JSONC
puts "JSONC Source:"
puts "-" * 40
puts jsonc_source
puts "-" * 40
puts
finder = TreeHaver::GrammarFinder.new(:jsonc)
if finder.available?
finder.register!
puts "✓ Registered JSONC from: #{finder.find_library_path}"
else
finder = TreeHaver::GrammarFinder.new(:json)
finder.register! if finder.available?
puts "Using JSON grammar (JSONC not found)"
end
TreeHaver.backend = :java
puts "Backend: #{TreeHaver.backend_module}"
puts
parser = TreeHaver::Parser.new
parser.language = begin
TreeHaver::Language.jsonc
rescue
TreeHaver::Language.json
end
tree = parser.parse(jsonc_source)
root = tree.root_node
puts "✓ Parsed: #{root.type} with #{root.child_count} children"
# Row number validation
puts
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
puts "#{indent}#{node.type}: rows #{start_row}-#{end_row}"
# For multiline JSONC, 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 "✓ Java backend handles JSONC comments correctly"
else
puts "✗ Row number issues detected:"
row_errors.each { |err| puts " - #{err}" }
exit 1
end