How to map Tree_sitter_java.CST.statement values to their corresponding source code lines?

Hi , I am having a question regarding method parse_input_tree present in tree_sitter_lang/java/Parse.ml. let parse_input_tree input_tree = let orig_root_node = Tree_sitter_parsing.root input_tree in let src = Tree_sitter_parsing.src input_tree in let errors = Run.extract_errors src orig_root_node in let root_node = Run.remove_extras ~extras orig_root_node in let matched_tree = Run.match_tree children_regexps src root_node in let opt_program = Option.map trans_program matched_tree in Parsing_result.create src opt_program errors where, input_tree is of type type t = Tree_sitter_run__Tree_sitter_parsing.t = { src : Tree_sitter_run.Src_file.t; root : Tree_sitter_bindings.Tree_sitter_output_t.node; } and parse_input_tree gives "Tree_sitter_java.CST.statement list" as output. Now, I would like to trace each statement in that list back to its corresponding line number in the original source code. Is there a built-in way in Tree-sitter to get the position (line/column or byte offset) of each CST.statement? If not,how can I approach to it? Any guidance or example code would be greatly appreciated. Thanks in advance!

Apr 30, 2025 - 08:27
 0
How to map Tree_sitter_java.CST.statement values to their corresponding source code lines?

Hi , I am having a question regarding method parse_input_tree present in tree_sitter_lang/java/Parse.ml.

let parse_input_tree input_tree =
  let orig_root_node = Tree_sitter_parsing.root input_tree in
  let src = Tree_sitter_parsing.src input_tree in
  let errors = Run.extract_errors src orig_root_node in
  let root_node = Run.remove_extras ~extras orig_root_node in
  let matched_tree = Run.match_tree children_regexps src root_node in
  let opt_program = Option.map trans_program matched_tree in
  Parsing_result.create src opt_program errors

where, input_tree is of type

type t =
  Tree_sitter_run__Tree_sitter_parsing.t = {
  src : Tree_sitter_run.Src_file.t;
  root : Tree_sitter_bindings.Tree_sitter_output_t.node;
} 

and parse_input_tree gives "Tree_sitter_java.CST.statement list" as output.

Now, I would like to trace each statement in that list back to its corresponding line number in the original source code.

Is there a built-in way in Tree-sitter to get the position (line/column or byte offset) of each CST.statement? If not,how can
I approach to it?

Any guidance or example code would be greatly appreciated.

Thanks in advance!