Skip to content

Commit ba4fc11

Browse files
authored
wrap QueryError fields with getters (#1434)
1 parent 32b48f8 commit ba4fc11

File tree

7 files changed

+62
-46
lines changed

7 files changed

+62
-46
lines changed

crates/metaslang/cst/generated/public_api.txt

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/metaslang/cst/src/query/model.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ impl<T: KindTypes> Query<T> {
3636
ASTNode::Capture(capture) => {
3737
// If the capture has already been used, return an error
3838
if capture_quantifiers.contains_key(&capture.name) {
39-
return Err(QueryError {
40-
message: format!("Capture name '{}' used more than once", capture.name),
41-
text_range: TextIndex::ZERO..TextIndex::ZERO,
42-
});
39+
return Err(QueryError::create(
40+
format!("Capture name '{}' used more than once", capture.name),
41+
TextIndex::ZERO..TextIndex::ZERO,
42+
));
4343
}
4444
capture_quantifiers.insert(capture.name.clone(), quantifier);
4545
collect_capture_quantifiers(&capture.child, quantifier, capture_quantifiers)?;
@@ -79,11 +79,10 @@ impl<T: KindTypes> Query<T> {
7979
CaptureQuantifier::One => CaptureQuantifier::OneOrMore,
8080
CaptureQuantifier::ZeroOrOne => CaptureQuantifier::ZeroOrMore,
8181
CaptureQuantifier::OneOrMore | CaptureQuantifier::ZeroOrMore => {
82-
return Err(QueryError {
83-
message: "Quantification over quantification is not allowed"
84-
.to_string(),
85-
text_range: TextIndex::ZERO..TextIndex::ZERO,
86-
})
82+
return Err(QueryError::create(
83+
"Quantification over quantification is not allowed".to_string(),
84+
TextIndex::ZERO..TextIndex::ZERO,
85+
))
8786
}
8887
};
8988
collect_capture_quantifiers(

crates/metaslang/cst/src/query/parser.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,28 @@ use crate::text_index::{TextIndex, TextRange};
2626
/// Error from parsing a query.
2727
#[derive(Clone, Debug, Error)]
2828
pub struct QueryError {
29+
message: String,
30+
text_range: TextRange,
31+
}
32+
33+
impl QueryError {
2934
/// The error message describing the parsing issue.
30-
pub message: String,
35+
pub fn message(&self) -> &str {
36+
&self.message
37+
}
38+
3139
/// The range of text where the error occurred.
32-
pub text_range: TextRange,
40+
pub fn text_range(&self) -> &TextRange {
41+
&self.text_range
42+
}
43+
44+
/// Creates a new `QueryError` with the given message and text range.
45+
pub fn create(message: String, text_range: TextRange) -> Self {
46+
QueryError {
47+
message,
48+
text_range,
49+
}
50+
}
3351
}
3452

3553
impl std::fmt::Display for QueryError {

crates/metaslang/graph_builder/src/parser.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum ParseError {
4848
InvalidRegex(String, Location),
4949
#[error("Expected integer constant in regex capture at {0}")]
5050
InvalidRegexCapture(Location),
51-
#[error("Invalid query pattern: {}", _0.message)]
51+
#[error("Invalid query pattern: {}", _0.message())]
5252
QueryError(#[from] QueryError),
5353
#[error("Unexpected character '{0}' in {1} at {2}")]
5454
UnexpectedCharacter(char, &'static str, Location),
@@ -94,8 +94,8 @@ impl std::fmt::Display for DisplayParseErrorPretty<'_> {
9494
ParseError::InvalidRegex(_, location) => *location,
9595
ParseError::InvalidRegexCapture(location) => *location,
9696
ParseError::QueryError(err) => Location {
97-
row: err.text_range.start.line,
98-
column: err.text_range.start.column,
97+
row: err.text_range().start.line,
98+
column: err.text_range().start.column,
9999
},
100100
ParseError::UnexpectedCharacter(_, _, location) => *location,
101101
ParseError::UnexpectedEOF(location) => *location,
@@ -371,21 +371,18 @@ impl<'a> Parser<'a> {
371371

372372
let query_source = &self.source[query_start..query_end];
373373

374-
let query = Query::create(query_source).map_err(|mut e| {
375-
let start_offset = query_start + e.text_range.start.utf8;
376-
let end_offset = query_start + e.text_range.end.utf8;
374+
let query = Query::create(query_source).map_err(|e| {
375+
let message = e.message().to_string();
376+
let start_offset = query_start + e.text_range().start.utf8;
377+
let end_offset = query_start + e.text_range().end.utf8;
377378

378-
e.text_range.start = TextIndex::ZERO;
379-
e.text_range
380-
.start
381-
.advance_str(&self.source[0..start_offset]);
379+
let mut start_index = TextIndex::ZERO;
380+
start_index.advance_str(&self.source[0..start_offset]);
382381

383-
e.text_range.end = e.text_range.start;
384-
e.text_range
385-
.end
386-
.advance_str(&self.source[start_offset..end_offset]);
382+
let mut end_index = start_index.clone();
383+
end_index.advance_str(&self.source[start_offset..end_offset]);
387384

388-
e
385+
QueryError::create(message, start_index..end_index)
389386
})?;
390387

391388
Ok(query)

crates/metaslang/graph_builder/tests/parser.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,9 +1585,9 @@ fn query_parse_errors_have_file_location() {
15851585
Err(ParseError::QueryError(e)) => e,
15861586
Err(e) => panic!("Unexpected error: {e}"),
15871587
};
1588-
assert_eq!(err.message, "Parse error:\n'NonExistingNode' is not a valid node kind at: NonExistingNode ] ]\n \n");
1588+
assert_eq!(err.message(), "Parse error:\n'NonExistingNode' is not a valid node kind at: NonExistingNode ] ]\n \n");
15891589
assert_eq!(
1590-
format!("{:?}", err.text_range),
1590+
format!("{:?}", err.text_range()),
15911591
"TextIndex { utf8: 50, utf16: 50, line: 2, column: 19 }..TextIndex { utf8: 78, utf16: 78, line: 3, column: 8 }",
15921592
);
15931593
}
@@ -1608,9 +1608,9 @@ fn multiline_query_parse_errors_have_file_location() {
16081608
Err(ParseError::QueryError(e)) => e,
16091609
Err(e) => panic!("Unexpected error: {e}"),
16101610
};
1611-
assert_eq!(err.message, "Parse error:\n'NonExistingNode' is not a valid node kind at: NonExistingNode ] ]\n )\n \n");
1611+
assert_eq!(err.message(), "Parse error:\n'NonExistingNode' is not a valid node kind at: NonExistingNode ] ]\n )\n \n");
16121612
assert_eq!(
1613-
format!("{:?}", err.text_range),
1613+
format!("{:?}", err.text_range()),
16141614
"TextIndex { utf8: 112, utf16: 112, line: 5, column: 23 }..TextIndex { utf8: 150, utf16: 150, line: 7, column: 8 }",
16151615
);
16161616
}

crates/solidity/outputs/cargo/tests/src/cst/queries/parser_tests.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ fn test_parsing_error() {
5353
match result {
5454
Ok(_) => panic!("Expected error"),
5555
Err(e) => {
56-
assert_eq!(e.message, "Parse error:\nexpected ']' at: \nAlt at: [_\n");
56+
assert_eq!(e.message(), "Parse error:\nexpected ']' at: \nAlt at: [_\n");
5757
assert_eq!(
58-
format!("{:?}", e.text_range),
58+
format!("{:?}", e.text_range()),
5959
"TextIndex { utf8: 8, utf16: 8, line: 0, column: 8 }..TextIndex { utf8: 8, utf16: 8, line: 0, column: 8 }",
6060
);
6161
}
@@ -70,11 +70,11 @@ fn test_parsing_error_with_invalid_edge_label() {
7070
Ok(_) => panic!("Expected error"),
7171
Err(e) => {
7272
assert_eq!(
73-
e.message,
73+
e.message(),
7474
"Parse error:\n'Name' is not a valid edge label at: Name: [_]]\n",
7575
);
7676
assert_eq!(
77-
format!("{:?}", e.text_range),
77+
format!("{:?}", e.text_range()),
7878
"TextIndex { utf8: 18, utf16: 18, line: 0, column: 18 }..TextIndex { utf8: 28, utf16: 28, line: 0, column: 28 }",
7979
);
8080
}
@@ -88,11 +88,11 @@ fn test_parsing_error_with_invalid_node_kind() {
8888
Ok(_) => panic!("Expected error"),
8989
Err(e) => {
9090
assert_eq!(
91-
e.message,
91+
e.message(),
9292
"Parse error:\n'tree_node' is not a valid node kind at: tree_node]]\n",
9393
);
9494
assert_eq!(
95-
format!("{:?}", e.text_range),
95+
format!("{:?}", e.text_range()),
9696
"TextIndex { utf8: 13, utf16: 13, line: 0, column: 13 }..TextIndex { utf8: 24, utf16: 24, line: 0, column: 24 }",
9797
);
9898
}
@@ -106,11 +106,11 @@ fn test_parsing_error_with_kind_beginning_with_underscore() {
106106
Ok(_) => panic!("Expected error"),
107107
Err(e) => {
108108
assert_eq!(
109-
e.message,
109+
e.message(),
110110
"Parse error:\n'_tree_node' is not a valid node kind at: _tree_node]]\n",
111111
);
112112
assert_eq!(
113-
format!("{:?}", e.text_range),
113+
format!("{:?}", e.text_range()),
114114
"TextIndex { utf8: 13, utf16: 13, line: 0, column: 13 }..TextIndex { utf8: 25, utf16: 25, line: 0, column: 25 }",
115115
);
116116
}
@@ -123,7 +123,7 @@ fn test_fails_parsing_ellipsis() {
123123
match result {
124124
Ok(_) => panic!("Expected parse failure"),
125125
Err(e) => assert_eq!(
126-
e.message,
126+
e.message(),
127127
"Parse error:\nThe ellipsis `...` operator is deprecated, and replaced with a new adjacency `.` operator. at: ...]\n",
128128
),
129129
}
@@ -134,7 +134,7 @@ fn test_fails_consecutive_adjacency_operators() {
134134
let result = Query::create(r#"[_ [Identifier] . .]"#);
135135
match result {
136136
Ok(_) => panic!("Expected parse failure"),
137-
Err(e) => assert_eq!(e.message, "Parse error:\nNoneOf at: .]\n"),
137+
Err(e) => assert_eq!(e.message(), "Parse error:\nNoneOf at: .]\n"),
138138
}
139139
}
140140

@@ -144,7 +144,7 @@ fn test_fails_sole_adjacency() {
144144
match result {
145145
Ok(_) => panic!("Expected parse failure"),
146146
Err(e) => assert_eq!(
147-
e.message,
147+
e.message(),
148148
"Parse error:\nexpected ']' at: .]\nAlt at: [_ .]\n"
149149
),
150150
}
@@ -162,7 +162,7 @@ fn test_fails_parsing_trivia_node_selector() {
162162
match result {
163163
Ok(_) => panic!("Expected parse failure"),
164164
Err(e) => assert_eq!(
165-
e.message,
165+
e.message(),
166166
"Parse error:\nMatching trivia nodes directly is forbidden. at: EndOfLine]\n"
167167
),
168168
}

crates/solidity/outputs/cargo/wasm/src/wasm_crate/wrappers/cst/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ impl IntoFFI<ffi::QueryError> for rust::QueryError {
403403
#[inline]
404404
fn _into_ffi(self) -> ffi::QueryError {
405405
ffi::QueryError {
406-
message: self.message,
407-
text_range: self.text_range._into_ffi(),
406+
message: self.message().to_string(),
407+
text_range: self.text_range()._into_ffi(),
408408
}
409409
}
410410
}

0 commit comments

Comments
 (0)