Tweak impossible pattern error messages

This commit is contained in:
Max Brunsfeld 2020-08-20 13:24:42 -07:00
parent 4301110c12
commit 9daec9cb22
10 changed files with 19 additions and 17 deletions

View file

@ -70,7 +70,7 @@ impl<'a> From<QueryError> 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
)),

View file

@ -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))", //

View file

@ -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) {

View file

@ -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."]

View file

@ -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)),
}

View file

@ -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;

View file

@ -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",

View file

@ -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", () => {

View file

@ -130,7 +130,7 @@ typedef enum {
TSQueryErrorNodeType,
TSQueryErrorField,
TSQueryErrorCapture,
TSQueryErrorPattern,
TSQueryErrorStructure,
} TSQueryError;
/********************/

View file

@ -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;
}