diff --git a/cli/src/error.rs b/cli/src/error.rs index c30e3647..075de3a6 100644 --- a/cli/src/error.rs +++ b/cli/src/error.rs @@ -70,7 +70,7 @@ impl<'a> From for Error { "Query error on line {}. Invalid syntax:\n{}", row, l )), - QueryError::Pattern(row, l) => Error::new(format!( + QueryError::Structure(row, l) => Error::new(format!( "Query error on line {}. Impossible pattern:\n{}", row, l )), diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 1e4ea8cc..e7231ef0 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -195,7 +195,7 @@ fn test_query_errors_on_impossible_patterns() { js_lang, "(binary_expression left: (identifier) left: (identifier))" ), - Err(QueryError::Pattern( + Err(QueryError::Structure( 1, [ "(binary_expression left: (identifier) left: (identifier))", @@ -212,7 +212,7 @@ fn test_query_errors_on_impossible_patterns() { .unwrap(); assert_eq!( Query::new(js_lang, "(function_declaration name: (statement_block))"), - Err(QueryError::Pattern( + Err(QueryError::Structure( 1, [ "(function_declaration name: (statement_block))", @@ -225,7 +225,7 @@ fn test_query_errors_on_impossible_patterns() { Query::new(rb_lang, "(call receiver:(call))").unwrap(); assert_eq!( Query::new(rb_lang, "(call receiver:(binary))"), - Err(QueryError::Pattern( + Err(QueryError::Structure( 1, [ "(call receiver:(binary))", // diff --git a/docs/assets/js/playground.js b/docs/assets/js/playground.js index 686be90d..137bb352 100644 --- a/docs/assets/js/playground.js +++ b/docs/assets/js/playground.js @@ -277,7 +277,7 @@ let tree; const startPosition = queryEditor.posFromIndex(error.index); const endPosition = { line: startPosition.line, - ch: startPosition.ch + (error.length || 1) + ch: startPosition.ch + (error.length || Infinity) }; if (error.index === queryText.length) { diff --git a/lib/binding_rust/bindings.rs b/lib/binding_rust/bindings.rs index 81cc6f9a..f28d3461 100644 --- a/lib/binding_rust/bindings.rs +++ b/lib/binding_rust/bindings.rs @@ -132,7 +132,7 @@ pub const TSQueryError_TSQueryErrorSyntax: TSQueryError = 1; pub const TSQueryError_TSQueryErrorNodeType: TSQueryError = 2; pub const TSQueryError_TSQueryErrorField: TSQueryError = 3; pub const TSQueryError_TSQueryErrorCapture: TSQueryError = 4; -pub const TSQueryError_TSQueryErrorPattern: TSQueryError = 5; +pub const TSQueryError_TSQueryErrorStructure: TSQueryError = 5; pub type TSQueryError = u32; extern "C" { #[doc = " Create a new parser."] diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 10cd9fc2..ea5893b4 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -163,7 +163,7 @@ pub enum QueryError { Field(usize, String), Capture(usize, String), Predicate(String), - Pattern(usize, String), + Structure(usize, String), } #[derive(Debug)] @@ -1206,8 +1206,8 @@ impl Query { "Unexpected EOF".to_string() }; match error_type { - ffi::TSQueryError_TSQueryErrorPattern => { - Err(QueryError::Pattern(row, message)) + ffi::TSQueryError_TSQueryErrorStructure => { + Err(QueryError::Structure(row, message)) } _ => Err(QueryError::Syntax(row, message)), } diff --git a/lib/binding_web/binding.js b/lib/binding_web/binding.js index 404beeb6..f731e8f8 100644 --- a/lib/binding_web/binding.js +++ b/lib/binding_web/binding.js @@ -667,8 +667,8 @@ class Language { const errorId = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32'); const errorByte = getValue(TRANSFER_BUFFER, 'i32'); const errorIndex = UTF8ToString(sourceAddress, errorByte).length; - const suffix = source.substr(errorIndex, 100); - const word = suffix.match(QUERY_WORD_REGEX)[0]; + const suffix = source.substr(errorIndex, 100).split('\n')[0]; + let word = suffix.match(QUERY_WORD_REGEX)[0]; let error; switch (errorId) { case 2: @@ -681,10 +681,12 @@ class Language { error = new RangeError(`Bad capture name @${word}`); break; case 5: - error = new SyntaxError(`Impossible pattern at offset ${errorIndex}: '${suffix}'...`); + error = new TypeError(`Bad pattern structure at offset ${errorIndex}: '${suffix}'...`); + word = ""; break; default: error = new SyntaxError(`Bad syntax at offset ${errorIndex}: '${suffix}'...`); + word = ""; break; } error.index = errorIndex; diff --git a/lib/binding_web/exports.json b/lib/binding_web/exports.json index 2c638249..72105158 100644 --- a/lib/binding_web/exports.json +++ b/lib/binding_web/exports.json @@ -15,7 +15,6 @@ "__ZNSt3__212basic_stringIwNS_11char_traitsIwEENS_9allocatorIwEEED2Ev", "__ZdlPv", "__Znwm", - "___assert_fail", "_abort", "_iswalnum", "_iswalpha", @@ -73,8 +72,6 @@ "_ts_query_capture_count", "_ts_query_capture_name_for_id", "_ts_query_captures_wasm", - "_ts_query_context_delete", - "_ts_query_context_new", "_ts_query_delete", "_ts_query_matches_wasm", "_ts_query_new", diff --git a/lib/binding_web/test/query-test.js b/lib/binding_web/test/query-test.js index 9d1e24e1..23663e9a 100644 --- a/lib/binding_web/test/query-test.js +++ b/lib/binding_web/test/query-test.js @@ -30,6 +30,9 @@ describe("Query", () => { assert.throws(() => { JavaScript.query("(function_declaration non_existent:(identifier))"); }, "Bad field name 'non_existent'"); + assert.throws(() => { + JavaScript.query("(function_declaration name:(statement_block))"); + }, "Bad pattern structure at offset 22: 'name:(statement_block))'"); }); it("throws an error on invalid predicates", () => { diff --git a/lib/include/tree_sitter/api.h b/lib/include/tree_sitter/api.h index 1e60e4b5..b85380d1 100644 --- a/lib/include/tree_sitter/api.h +++ b/lib/include/tree_sitter/api.h @@ -130,7 +130,7 @@ typedef enum { TSQueryErrorNodeType, TSQueryErrorField, TSQueryErrorCapture, - TSQueryErrorPattern, + TSQueryErrorStructure, } TSQueryError; /********************/ diff --git a/lib/src/query.c b/lib/src/query.c index 5a2bb2fb..60c892d3 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -1903,7 +1903,7 @@ TSQuery *ts_query_new( if (self->language->version >= TREE_SITTER_LANGUAGE_VERSION_WITH_STATE_COUNT) { if (!ts_query__analyze_patterns(self, error_offset)) { - *error_type = TSQueryErrorPattern; + *error_type = TSQueryErrorStructure; ts_query_delete(self); return NULL; }