From c8d040ca26e50de818059cbd707085d5f53713de Mon Sep 17 00:00:00 2001 From: Jacob Mitchell Date: Thu, 14 Mar 2019 12:59:29 -0700 Subject: [PATCH] Use 1-indexed rows in CLI and log output (resolves #287) --- cli/src/tests/parser_test.rs | 4 ++++ cli/src/util.rs | 19 +++++++++++++++++++ docs/section-3-creating-parsers.md | 2 +- lib/src/get_changed_ranges.c | 10 +++++----- lib/src/parser.c | 6 +++--- lib/src/stack.c | 2 +- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/cli/src/tests/parser_test.rs b/cli/src/tests/parser_test.rs index afa86167..01724daf 100644 --- a/cli/src/tests/parser_test.rs +++ b/cli/src/tests/parser_test.rs @@ -56,6 +56,10 @@ fn test_parsing_with_logging() { "reduce sym:struct_item, child_count:3".to_string() ))); assert!(messages.contains(&(LogType::Lex, "skip character:' '".to_string()))); + + for (_, m) in &messages { + assert!(!m.contains("row:0")); + } } #[test] diff --git a/cli/src/util.rs b/cli/src/util.rs index e880bea1..c4d4f9c9 100644 --- a/cli/src/util.rs +++ b/cli/src/util.rs @@ -55,6 +55,9 @@ impl Drop for LogSession { { Command::new("open").arg(&self.0).output().unwrap(); } + + #[cfg(any(debug_assertions, test))] + validate_graph_log(&self); } else { eprintln!( "Dot failed: {} {}", @@ -64,3 +67,19 @@ impl Drop for LogSession { } } } + +#[cfg(all(unix, any(debug_assertions, test)))] +fn validate_graph_log(session: &LogSession) { + use std::io::{BufRead, BufReader}; + + let has_zero_indexed_row = |s: &str| s.contains("position: 0,"); + + let graph_log = std::fs::File::open(&session.0) + .expect("Failed to open graph log"); + let log_reader = BufReader::new(graph_log) + .lines() + .map(|l| l.expect("Failed to read line from graph log")); + for line in log_reader { + assert!(!has_zero_indexed_row(&line), "Graph log output includes zero-indexed row: {}", line); + } +} diff --git a/docs/section-3-creating-parsers.md b/docs/section-3-creating-parsers.md index 1e7989fa..37bccb65 100644 --- a/docs/section-3-creating-parsers.md +++ b/docs/section-3-creating-parsers.md @@ -86,7 +86,7 @@ tree-sitter parse ./the-file This should print the following: ``` -(source_file [0, 0] - [0, 5]) +(source_file [1, 0] - [1, 5]) ``` You might notice that the first time you run `tree-sitter parse`, it takes a few seconds. This is because Tree-sitter automatically compiles your C code into a dynamically-loadable library. Whenever you make changes to your grammar, you can update the parser simply by re-running `tree-sitter generate`. When the parser changes, Tree-sitter will recompile it as needed. diff --git a/lib/src/get_changed_ranges.c b/lib/src/get_changed_ranges.c index b1df72cc..c618dc9e 100644 --- a/lib/src/get_changed_ranges.c +++ b/lib/src/get_changed_ranges.c @@ -331,8 +331,8 @@ static inline void iterator_print_state(Iterator *self) { "(%-25s %s\t depth:%u [%u, %u] - [%u, %u])", name, self->in_padding ? "(p)" : " ", self->visible_depth, - start.row, start.column, - end.row, end.column + start.row + 1, start.column, + end.row + 1, end.column ); } #endif @@ -361,7 +361,7 @@ unsigned ts_subtree_get_changed_ranges(const Subtree *old_tree, const Subtree *n do { #ifdef DEBUG_GET_CHANGED_RANGES - printf("At [%-2u, %-2u] Compare ", position.extent.row, position.extent.column); + printf("At [%-2u, %-2u] Compare ", position.extent.row + 1, position.extent.column); iterator_print_state(&old_iter); printf("\tvs\t"); iterator_print_state(&new_iter); @@ -443,8 +443,8 @@ unsigned ts_subtree_get_changed_ranges(const Subtree *old_tree, const Subtree *n #ifdef DEBUG_GET_CHANGED_RANGES printf( " change: [[%u, %u] - [%u, %u]]\n", - position.extent.row, position.extent.column, - next_position.extent.row, next_position.extent.column + position.extent.row + 1, position.extent.column, + next_position.extent.row + 1, next_position.extent.column ); #endif diff --git a/lib/src/parser.c b/lib/src/parser.c index 7125faa9..f5cdb3cc 100644 --- a/lib/src/parser.c +++ b/lib/src/parser.c @@ -332,7 +332,7 @@ static Subtree ts_parser__lex(TSParser *self, StackVersion version, TSStateId pa LOG( "lex_external state:%d, row:%u, column:%u", lex_mode.external_lex_state, - current_position.extent.row, + current_position.extent.row + 1, current_position.extent.column ); ts_lexer_start(&self->lexer); @@ -370,7 +370,7 @@ static Subtree ts_parser__lex(TSParser *self, StackVersion version, TSStateId pa LOG( "lex_internal state:%d, row:%u, column:%u", lex_mode.lex_state, - current_position.extent.row, + current_position.extent.row + 1, current_position.extent.column ); ts_lexer_start(&self->lexer); @@ -1674,7 +1674,7 @@ TSTree *ts_parser_parse(TSParser *self, const TSTree *old_tree, TSInput input) { LOG("process version:%d, version_count:%u, state:%d, row:%u, col:%u", version, ts_stack_version_count(self->stack), ts_stack_state(self->stack, version), - ts_stack_position(self->stack, version).extent.row, + ts_stack_position(self->stack, version).extent.row + 1, ts_stack_position(self->stack, version).extent.column); if (!ts_parser__advance(self, version, allow_node_reuse)) return NULL; diff --git a/lib/src/stack.c b/lib/src/stack.c index 9e351d4e..73c06454 100644 --- a/lib/src/stack.c +++ b/lib/src/stack.c @@ -785,7 +785,7 @@ bool ts_stack_print_dot_graph(Stack *self, const TSLanguage *language, FILE *f) fprintf( f, " tooltip=\"position: %u,%u\nnode_count:%u\nerror_cost: %u\ndynamic_precedence: %d\"];\n", - node->position.extent.row, + node->position.extent.row + 1, node->position.extent.column, node->node_count, node->error_cost,