Skip to content

Commit 64d1030

Browse files
committed
refactor: Use a struct for FunctionDeclaration and SubDeclaration
1 parent 32428fb commit 64d1030

8 files changed

Lines changed: 61 additions & 32 deletions

File tree

rusty_linter/src/converter/common/program_rules.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ impl ConvertibleIn<Position, Vec<GlobalStatementPos>> for GlobalStatement {
4949
ctx.resolver.set(&def_type);
5050
Ok(vec![])
5151
}
52-
Self::FunctionDeclaration(_, _)
53-
| Self::SubDeclaration(_, _)
54-
| Self::UserDefinedType(_) => Ok(vec![]),
52+
Self::FunctionDeclaration(_) | Self::SubDeclaration(_) | Self::UserDefinedType(_) => {
53+
Ok(vec![])
54+
}
5555
Self::FunctionImplementation(f) => on_function_implementation(f, ctx)
5656
.map(|f| vec![Self::FunctionImplementation(f).at_pos(pos.clone())]),
5757
Self::SubImplementation(s) => on_sub_implementation(s, ctx)

rusty_linter/src/pre_linter/main.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ impl MainContext {
4949
GlobalStatement::DefType(def_type) => {
5050
self.on_def_type(def_type);
5151
}
52-
GlobalStatement::FunctionDeclaration(name, params) => {
53-
self.on_function_declaration(name, params)?;
52+
GlobalStatement::FunctionDeclaration(f) => {
53+
self.on_function_declaration(f)?;
5454
}
5555
GlobalStatement::FunctionImplementation(f) => {
5656
self.on_function_implementation(f)?;
5757
}
5858
GlobalStatement::Statement(s) => {
5959
self.on_statement(s)?;
6060
}
61-
GlobalStatement::SubDeclaration(name, params) => {
62-
self.on_sub_declaration(name, params)?;
61+
GlobalStatement::SubDeclaration(s) => {
62+
self.on_sub_declaration(s)?;
6363
}
6464
GlobalStatement::SubImplementation(s) => {
6565
self.on_sub_implementation(s)?;
@@ -76,11 +76,11 @@ impl MainContext {
7676
self.resolver.set(def_type);
7777
}
7878

79-
fn on_function_declaration(
80-
&mut self,
81-
name: &NamePos,
82-
params: &Parameters,
83-
) -> Result<(), LintErrorPos> {
79+
fn on_function_declaration(&mut self, f: &FunctionDeclaration) -> Result<(), LintErrorPos> {
80+
let FunctionDeclaration {
81+
name,
82+
parameters: params,
83+
} = f;
8484
let param_types: ResolvedParamTypes = self.on_parameters(params)?;
8585
let bare_name = name.element.bare_name();
8686
let signature = name.element.to_signature(&self.resolver, param_types);
@@ -109,11 +109,11 @@ impl MainContext {
109109
}
110110
}
111111

112-
fn on_sub_declaration(
113-
&mut self,
114-
name: &BareNamePos,
115-
params: &Parameters,
116-
) -> Result<(), LintErrorPos> {
112+
fn on_sub_declaration(&mut self, s: &SubDeclaration) -> Result<(), LintErrorPos> {
113+
let SubDeclaration {
114+
name,
115+
parameters: params,
116+
} = s;
117117
let param_types: ResolvedParamTypes = self.on_parameters(params)?;
118118
let bare_name = &name.element;
119119
let signature = bare_name.to_signature(&self.resolver, param_types);

rusty_parser/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ mod tests {
6262
program,
6363
vec![
6464
// DECLARE FUNCTION Fib! (N!)
65-
GlobalStatement::FunctionDeclaration(
65+
GlobalStatement::function_declaration(
6666
"Fib!".as_name(1, 18),
6767
vec![Parameter::new(
6868
"N".into(),

rusty_parser/src/specific/core/declaration.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ pub fn declaration_p() -> impl Parser<RcStringView, Output = GlobalStatement> {
1919
keyword_followed_by_whitespace_p(Keyword::Declare).and_without_undo_keep_right(
2020
OrParser::new(vec![
2121
Box::new(
22-
function_declaration_p().map(|(n, p)| GlobalStatement::FunctionDeclaration(n, p)),
22+
function_declaration_p().map(|(n, p)| GlobalStatement::function_declaration(n, p)),
2323
),
24-
Box::new(sub_declaration_p().map(|(n, p)| GlobalStatement::SubDeclaration(n, p))),
24+
Box::new(sub_declaration_p().map(|(n, p)| GlobalStatement::sub_declaration(n, p))),
2525
])
2626
.or_syntax_error("Expected: FUNCTION or SUB after DECLARE"),
2727
)
@@ -107,7 +107,7 @@ mod tests {
107107
assert_eq!(
108108
program,
109109
vec![
110-
GlobalStatement::FunctionDeclaration(
110+
GlobalStatement::function_declaration(
111111
"Echo".as_name(2, 26),
112112
vec![Parameter::new("X".into(), ParamType::Bare).at_rc(2, 31)]
113113
)
@@ -179,7 +179,7 @@ mod tests {
179179
assert_eq!(
180180
program,
181181
vec![
182-
GlobalStatement::FunctionDeclaration(
182+
GlobalStatement::function_declaration(
183183
"Echo".as_name(2, 26),
184184
vec![Parameter::new(
185185
"X".into(),
@@ -218,7 +218,8 @@ mod tests {
218218
assert_eq!(
219219
program,
220220
vec![
221-
GlobalStatement::SubDeclaration("ScrollUp".as_bare_name(2, 21), vec![]).at_rc(2, 9)
221+
GlobalStatement::sub_declaration("ScrollUp".as_bare_name(2, 21), vec![])
222+
.at_rc(2, 9)
222223
]
223224
);
224225
}
@@ -231,7 +232,7 @@ mod tests {
231232
let program = parse(input);
232233
assert_eq!(
233234
program,
234-
vec![GlobalStatement::SubDeclaration(
235+
vec![GlobalStatement::sub_declaration(
235236
"LCenter".as_bare_name(2, 21),
236237
vec![Parameter::new(
237238
"text".into(),

rusty_parser/src/specific/core/global_statement.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum GlobalStatement {
4848
DefType(DefType),
4949

5050
/// A function declaration, e.g. `DECLARE FUNCTION Add(A, B)`
51-
FunctionDeclaration(NamePos, Parameters),
51+
FunctionDeclaration(FunctionDeclaration),
5252

5353
/// A function implementation
5454
FunctionImplementation(FunctionImplementation),
@@ -57,7 +57,7 @@ pub enum GlobalStatement {
5757
Statement(Statement),
5858

5959
/// A sub declaration, e.g. `DECLARE SUB Connect`
60-
SubDeclaration(BareNamePos, Parameters),
60+
SubDeclaration(SubDeclaration),
6161

6262
/// A sub implementation
6363
SubImplementation(SubImplementation),
@@ -66,12 +66,38 @@ pub enum GlobalStatement {
6666
UserDefinedType(UserDefinedType),
6767
}
6868

69+
impl GlobalStatement {
70+
pub fn function_declaration(name: NamePos, parameters: Parameters) -> Self {
71+
Self::FunctionDeclaration(FunctionDeclaration::new(name, parameters))
72+
}
73+
74+
pub fn sub_declaration(name: BareNamePos, parameters: Parameters) -> Self {
75+
Self::SubDeclaration(SubDeclaration::new(name, parameters))
76+
}
77+
}
78+
6979
impl From<Statement> for GlobalStatement {
7080
fn from(s: Statement) -> Self {
7181
Self::Statement(s)
7282
}
7383
}
7484

85+
#[derive(Clone, Debug, PartialEq)]
86+
pub struct SubprogramDeclaration<T> {
87+
pub name: Positioned<T>,
88+
pub parameters: Parameters,
89+
}
90+
91+
impl<T> SubprogramDeclaration<T> {
92+
pub fn new(name: Positioned<T>, parameters: Parameters) -> Self {
93+
Self { name, parameters }
94+
}
95+
}
96+
97+
pub type SubDeclaration = SubprogramDeclaration<BareName>;
98+
99+
pub type FunctionDeclaration = SubprogramDeclaration<Name>;
100+
75101
/// The implementation of a subprogram (FUNCTION or SUB).
76102
#[derive(Clone, Debug, PartialEq)]
77103
pub struct SubprogramImplementation<T> {
@@ -80,7 +106,7 @@ pub struct SubprogramImplementation<T> {
80106
pub name: Positioned<T>,
81107

82108
/// The parameters of the subprogram.
83-
pub params: Vec<Positioned<Parameter>>,
109+
pub params: Parameters,
84110

85111
/// The body (statements) of the subprogram.
86112
pub body: Statements,

rusty_parser/src/specific/core/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ pub use self::expression::{
5555
pub use self::expression_type::{ExpressionType, HasExpressionType};
5656
pub use self::file_constants::*;
5757
pub use self::global_statement::{
58-
program_parser_p, FunctionImplementation, GlobalStatement, GlobalStatementPos, Program,
59-
SubImplementation, SubprogramImplementation,
58+
program_parser_p, FunctionDeclaration, FunctionImplementation, GlobalStatement,
59+
GlobalStatementPos, Program, SubDeclaration, SubImplementation, SubprogramImplementation,
6060
};
6161
pub use self::keyword::{Keyword, SORTED_KEYWORDS_STR};
6262
pub use self::letter_range::LetterRange;

rusty_parser/src/specific/core/sub_call.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ mod tests {
210210
program,
211211
vec![
212212
// DECLARE SUB Hello
213-
GlobalStatement::SubDeclaration("Hello".as_bare_name(2, 21), vec![],),
213+
GlobalStatement::sub_declaration("Hello".as_bare_name(2, 21), vec![],),
214214
// Hello
215215
GlobalStatement::Statement(Statement::SubCall("Hello".into(), vec![])),
216216
// SUB Hello
@@ -242,7 +242,7 @@ mod tests {
242242
program,
243243
vec![
244244
// DECLARE SUB Hello
245-
GlobalStatement::SubDeclaration(
245+
GlobalStatement::sub_declaration(
246246
"Hello".as_bare_name(2, 21),
247247
vec![
248248
Parameter::new(

rusty_parser/src/test_utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ macro_rules! assert_global_assignment {
288288
macro_rules! assert_function_declaration {
289289
($input:expr, $expected_function_name:expr, $expected_params:expr) => {
290290
match $crate::parse($input).demand_single().element() {
291-
$crate::specific::GlobalStatement::FunctionDeclaration(name, parameters) => {
291+
$crate::specific::GlobalStatement::FunctionDeclaration(
292+
$crate::specific::FunctionDeclaration { name, parameters },
293+
) => {
292294
assert_eq!(
293295
name.element(),
294296
$expected_function_name,

0 commit comments

Comments
 (0)