Skip to content

Commit 7b6ec13

Browse files
authored
refactor(definitions): replace macro with function (#24)
1 parent 4a335de commit 7b6ec13

File tree

10 files changed

+55
-66
lines changed

10 files changed

+55
-66
lines changed

flake.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/definitions/constructor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ impl Validate for ConstructorDefinition {
4545
}
4646

4747
fn extract(m: QueryMatch) -> Result<Definition> {
48-
let constructor = capture!(m, "constructor");
49-
let params = capture!(m, "constructor_params");
48+
let constructor = capture(&m, "constructor")?;
49+
let params = capture(&m, "constructor_params")?;
5050

5151
let span = params.text_range();
5252
let params = extract_params(params, NonterminalKind::Parameter);

src/definitions/enumeration.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ impl Validate for EnumDefinition {
4545
}
4646

4747
fn extract(m: QueryMatch) -> Result<Definition> {
48-
let enumeration = capture!(m, "enum");
49-
let name = capture!(m, "enum_name");
50-
let members = capture!(m, "enum_members");
48+
let enumeration = capture(&m, "enum")?;
49+
let name = capture(&m, "enum_name")?;
50+
let members = capture(&m, "enum_members")?;
5151

5252
let span = enumeration.text_range();
5353
let name = name.node().unparse().trim().to_string();

src/definitions/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ impl Validate for ErrorDefinition {
4545
}
4646

4747
fn extract(m: QueryMatch) -> Result<Definition> {
48-
let err = capture!(m, "err");
49-
let name = capture!(m, "err_name");
50-
let params = capture!(m, "err_params");
48+
let err = capture(&m, "err")?;
49+
let name = capture(&m, "err_name")?;
50+
let params = capture(&m, "err_params")?;
5151

5252
let span = err.text_range();
5353
let name = name.node().unparse().trim().to_string();

src/definitions/event.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ impl Validate for EventDefinition {
4545
}
4646

4747
fn extract(m: QueryMatch) -> Result<Definition> {
48-
let event = capture!(m, "event");
49-
let name = capture!(m, "event_name");
50-
let params = capture!(m, "event_params");
48+
let event = capture(&m, "event")?;
49+
let name = capture(&m, "event_name")?;
50+
let params = capture(&m, "event_params")?;
5151

5252
let span = event.text_range();
5353
let name = name.node().unparse().trim().to_string();

src/definitions/function.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use slang_solidity::cst::{NonterminalKind, Query, QueryMatch, TextRange};
22

33
use crate::{
4-
error::{Error, Result},
4+
error::Result,
55
lint::{Diagnostic, ItemDiagnostics, ItemType},
66
natspec::{NatSpec, NatSpecKind},
77
};
88

99
use super::{
10-
capture, check_params, check_returns, extract_attributes, extract_comment, extract_params,
11-
extract_parent_name, Attributes, Definition, Identifier, Parent, Validate, ValidationOptions,
12-
Visibility,
10+
capture, capture_opt, check_params, check_returns, extract_attributes, extract_comment,
11+
extract_params, extract_parent_name, Attributes, Definition, Identifier, Parent, Validate,
12+
ValidationOptions, Visibility,
1313
};
1414

1515
/// A function definition
@@ -70,19 +70,12 @@ impl Validate for FunctionDefinition {
7070
}
7171

7272
fn extract(m: QueryMatch) -> Result<Definition> {
73-
let func = capture!(m, "function");
74-
let keyword = capture!(m, "keyword");
75-
let name = capture!(m, "function_name");
76-
let params = capture!(m, "function_params");
77-
let attributes = capture!(m, "function_attr");
78-
let returns = match m
79-
.capture("function_returns")
80-
.map(|(_, mut captures)| captures.next())
81-
{
82-
Some(Some(ret)) => Some(ret),
83-
Some(None) => None,
84-
_ => return Err(Error::UnknownError),
85-
};
73+
let func = capture(&m, "function")?;
74+
let keyword = capture(&m, "keyword")?;
75+
let name = capture(&m, "function_name")?;
76+
let params = capture(&m, "function_params")?;
77+
let attributes = capture(&m, "function_attr")?;
78+
let returns = capture_opt(&m, "function_returns")?;
8679

8780
let span = if let Some(returns) = &returns {
8881
keyword.text_range().start..returns.text_range().end

src/definitions/mod.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,21 @@ pub mod structure;
2828
pub mod variable;
2929

3030
/// Retrieve and unwrap the first capture of a parser match, or return with an [`Error`]
31-
macro_rules! capture {
32-
($m:ident, $name:expr) => {
33-
match $m.capture($name).map(|(_, mut captures)| captures.next()) {
34-
Some(Some(res)) => res,
35-
_ => {
36-
return Err($crate::error::Error::UnknownError);
37-
}
38-
}
39-
};
31+
pub fn capture(m: &QueryMatch, name: &str) -> Result<Cursor> {
32+
match m.capture(name).map(|(_, mut captures)| captures.next()) {
33+
Some(Some(res)) => Ok(res),
34+
_ => Err(Error::UnknownError),
35+
}
4036
}
4137

42-
pub(crate) use capture;
38+
/// Retrieve and unwrap the first capture of a parser match if one exists.
39+
pub fn capture_opt(m: &QueryMatch, name: &str) -> Result<Option<Cursor>> {
40+
match m.capture(name).map(|(_, mut captures)| captures.next()) {
41+
Some(Some(res)) => Ok(Some(res)),
42+
Some(None) => Ok(None),
43+
_ => Err(Error::UnknownError),
44+
}
45+
}
4346

4447
/// Validation options to control which lints generate a diagnostic
4548
#[derive(Debug, Clone)]

src/definitions/modifier.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use slang_solidity::cst::{NonterminalKind, Query, QueryMatch, TextRange};
22

33
use crate::{
4-
error::{Error, Result},
4+
error::Result,
55
lint::{Diagnostic, ItemDiagnostics, ItemType},
66
natspec::{NatSpec, NatSpecKind},
77
};
88

99
use super::{
10-
capture, check_params, extract_comment, extract_params, extract_parent_name, Definition,
11-
Identifier, Parent, Validate, ValidationOptions,
10+
capture, capture_opt, check_params, extract_comment, extract_params, extract_parent_name,
11+
Definition, Identifier, Parent, Validate, ValidationOptions,
1212
};
1313

1414
/// A modifier definition
@@ -48,17 +48,10 @@ impl Validate for ModifierDefinition {
4848
}
4949

5050
fn extract(m: QueryMatch) -> Result<Definition> {
51-
let modifier = capture!(m, "modifier");
52-
let name = capture!(m, "modifier_name");
53-
let params = match m
54-
.capture("modifier_params")
55-
.map(|(_, mut captures)| captures.next())
56-
{
57-
Some(Some(ret)) => Some(ret),
58-
Some(None) => None,
59-
_ => return Err(Error::UnknownError),
60-
};
61-
let attr = capture!(m, "modifier_attr");
51+
let modifier = capture(&m, "modifier")?;
52+
let name = capture(&m, "modifier_name")?;
53+
let params = capture_opt(&m, "modifier_params")?;
54+
let attr = capture(&m, "modifier_attr")?;
6255

6356
let span = if let Some(params) = &params {
6457
name.text_range().start..params.text_range().end

src/definitions/structure.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ impl Validate for StructDefinition {
4545
}
4646

4747
fn extract(m: QueryMatch) -> Result<Definition> {
48-
let structure = capture!(m, "struct");
49-
let name = capture!(m, "struct_name");
50-
let members = capture!(m, "struct_members");
48+
let structure = capture(&m, "struct")?;
49+
let name = capture(&m, "struct_name")?;
50+
let members = capture(&m, "struct_members")?;
5151

5252
let span = structure.text_range();
5353
let name = name.node().unparse().trim().to_string();
@@ -96,7 +96,7 @@ fn extract_struct_members(cursor: Cursor) -> Result<Vec<Identifier>> {
9696
)
9797
.expect("query should compile");
9898
for m in cursor.query(vec![query]) {
99-
let member_name = capture!(m, "member_name");
99+
let member_name = capture(&m, "member_name")?;
100100
out.push(Identifier {
101101
name: Some(member_name.node().unparse().trim().to_string()),
102102
span: member_name.text_range(),

src/definitions/variable.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ impl Validate for VariableDeclaration {
5454
}
5555

5656
fn extract(m: QueryMatch) -> Result<Definition> {
57-
let variable = capture!(m, "variable");
58-
let var_type = capture!(m, "variable_type");
59-
let attributes = capture!(m, "variable_attr");
60-
let name = capture!(m, "variable_name");
57+
let variable = capture(&m, "variable")?;
58+
let var_type = capture(&m, "variable_type")?;
59+
let attributes = capture(&m, "variable_attr")?;
60+
let name = capture(&m, "variable_name")?;
6161

6262
let span = var_type.text_range().start..variable.text_range().end;
6363
let name = name.node().unparse().trim().to_string();

0 commit comments

Comments
 (0)