Skip to content

Commit 8e35a5a

Browse files
committed
refactor: Combined all VarType* traits into one VarType trait
1 parent cc6f8d8 commit 8e35a5a

8 files changed

Lines changed: 97 additions & 141 deletions

File tree

rusty_linter/src/converter/dim_rules/dim_type_rules.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn on_dim_type(
2828
}
2929
}
3030

31-
pub fn bare_to_dim_type<T: VarTypeNewBuiltInCompact>(
31+
pub fn bare_to_dim_type<T: VarType>(
3232
ctx: &mut Context,
3333
bare_name: &BareName,
3434
pos: Position,
@@ -61,7 +61,7 @@ fn require_compact_can_be_defined(
6161
})
6262
}
6363

64-
pub fn built_in_to_dim_type<T: VarTypeNewBuiltInCompact + VarTypeNewBuiltInExtended>(
64+
pub fn built_in_to_dim_type<T: VarType>(
6565
ctx: &mut Context,
6666
bare_name: &BareName,
6767
q: TypeQualifier,
@@ -104,7 +104,7 @@ fn fixed_length_string_to_dim_type(
104104
))
105105
}
106106

107-
pub fn user_defined_to_dim_type<T: VarTypeNewUserDefined>(
107+
pub fn user_defined_to_dim_type<T: VarType>(
108108
ctx: &mut Context,
109109
bare_name: &BareName,
110110
user_defined_type: BareNamePos,

rusty_linter/src/converter/dim_rules/validation.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ use crate::core::{HasSubprograms, HasUserDefinedTypes};
44
use crate::core::{LintError, LintErrorPos};
55
use crate::names::ManyNamesTrait;
66
use rusty_common::{AtPos, Position, Positioned};
7-
use rusty_parser::{
8-
DimVar, Parameter, TypedName, VarTypeIsExtended, VarTypeQualifier,
9-
VarTypeToUserDefinedRecursively,
10-
};
7+
use rusty_parser::{DimVar, Parameter, TypedName, VarType};
118

129
pub fn validate<T>(
1310
var_name: &TypedName<T>,
1411
ctx: &Context,
1512
pos: Position,
1613
) -> Result<(), LintErrorPos>
1714
where
18-
T: VarTypeIsExtended + VarTypeQualifier + VarTypeToUserDefinedRecursively,
15+
T: VarType,
1916
TypedName<T>: CannotClashWithFunctions,
2017
{
2118
cannot_clash_with_subs(var_name, ctx, pos)?;
@@ -24,7 +21,7 @@ where
2421
cannot_clash_with_local_constants(var_name, ctx, pos)
2522
}
2623

27-
fn cannot_clash_with_subs<T, C: HasSubprograms>(
24+
fn cannot_clash_with_subs<T: VarType, C: HasSubprograms>(
2825
var_name: &TypedName<T>,
2926
ctx: &C,
3027
pos: Position,
@@ -36,7 +33,7 @@ fn cannot_clash_with_subs<T, C: HasSubprograms>(
3633
}
3734
}
3835

39-
fn cannot_clash_with_local_constants<T>(
36+
fn cannot_clash_with_local_constants<T: VarType>(
4037
var_name: &TypedName<T>,
4138
ctx: &Context,
4239
pos: Position,
@@ -98,7 +95,7 @@ fn user_defined_type_must_exist<T>(
9895
ctx: &Context,
9996
) -> Result<(), LintErrorPos>
10097
where
101-
T: VarTypeToUserDefinedRecursively,
98+
T: VarType,
10299
{
103100
match var_name.var_type.as_user_defined_recursively() {
104101
Some(Positioned {

rusty_linter/src/names/names.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use crate::names::traits::ManyNamesTrait;
66
use crate::{core::ConstLookup, names::ImplicitVars};
77
use rusty_common::CaseInsensitiveString;
88
use rusty_parser::{
9-
BareName, BuiltInStyle, HasExpressionType, Name, QualifiedName, RedimInfo, TypeQualifier,
10-
VarTypeIsExtended, VariableInfo,
9+
BareName, BuiltInStyle, Name, QualifiedName, RedimInfo, TypeQualifier, VarType, VariableInfo,
1110
};
1211
use rusty_variant::Variant;
1312

@@ -134,7 +133,7 @@ impl Names {
134133
self.get_const_value_recursively(bare_name).is_some()
135134
}
136135

137-
pub fn insert<T: HasExpressionType + VarTypeIsExtended>(
136+
pub fn insert<T: VarType>(
138137
&mut self,
139138
bare_name: BareName,
140139
dim_type: &T,

rusty_linter/src/post_linter/dots_linter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl Visitor<Statement> for UserDefinedNamesCollector {
221221

222222
impl<T> Visitor<TypedName<T>> for UserDefinedNamesCollector
223223
where
224-
T: VarTypeToUserDefinedRecursively,
224+
T: VarType,
225225
{
226226
fn visit(&mut self, element: &TypedName<T>) -> VisitResult {
227227
if element.var_type.as_user_defined_recursively().is_some() {

rusty_parser/src/specific/core/dim_name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::specific::core::var_name;
12
use crate::specific::*;
23
use rusty_common::*;
34

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::specific::{
22
ArrayDimensions, BareNamePos, BuiltInStyle, Expression, ExpressionPos, ExpressionType,
3-
HasExpressionType, TypeQualifier, VarTypeIsExtended, VarTypeNewBuiltInCompact,
4-
VarTypeNewBuiltInExtended, VarTypeNewUserDefined, VarTypeQualifier, VarTypeToArray,
5-
VarTypeToUserDefinedRecursively,
3+
HasExpressionType, TypeQualifier, VarType,
64
};
75
use rusty_common::{AtPos, Position};
86

@@ -16,53 +14,36 @@ pub enum DimType {
1614
Array(ArrayDimensions, Box<Self>),
1715
}
1816

19-
impl VarTypeNewBuiltInCompact for DimType {
17+
impl VarType for DimType {
2018
fn new_built_in_compact(q: TypeQualifier) -> Self {
2119
Self::BuiltIn(q, BuiltInStyle::Compact)
2220
}
23-
}
2421

25-
impl VarTypeNewBuiltInExtended for DimType {
2622
fn new_built_in_extended(q: TypeQualifier) -> Self {
2723
Self::BuiltIn(q, BuiltInStyle::Extended)
2824
}
29-
}
30-
31-
impl VarTypeToArray for DimType {
32-
type ArrayType = ArrayDimensions;
3325

34-
fn to_array(self, array_type: Self::ArrayType) -> Self {
35-
if array_type.is_empty() {
36-
self
37-
} else {
38-
Self::Array(array_type, Box::new(self))
39-
}
40-
}
41-
}
42-
43-
impl VarTypeNewUserDefined for DimType {
4426
fn new_user_defined(bare_name_pos: BareNamePos) -> Self {
4527
Self::UserDefined(bare_name_pos)
4628
}
47-
}
4829

49-
impl VarTypeToUserDefinedRecursively for DimType {
5030
fn as_user_defined_recursively(&self) -> Option<&BareNamePos> {
5131
match self {
5232
Self::UserDefined(n) => Some(n),
5333
Self::Array(_, e) => e.as_user_defined_recursively(),
5434
_ => None,
5535
}
5636
}
57-
}
5837

59-
impl DimType {
60-
pub fn fixed_length_string(len: u16, pos: Position) -> Self {
61-
Self::FixedLengthString(Expression::IntegerLiteral(len as i32).at_pos(pos), len)
38+
fn to_qualifier_recursively(&self) -> Option<TypeQualifier> {
39+
match self {
40+
Self::BuiltIn(q, _) => Some(*q),
41+
Self::FixedLengthString(_, _) => Some(TypeQualifier::DollarString),
42+
Self::Array(_, e) => e.to_qualifier_recursively(),
43+
_ => None,
44+
}
6245
}
63-
}
6446

65-
impl VarTypeIsExtended for DimType {
6647
fn is_extended(&self) -> bool {
6748
match self {
6849
Self::BuiltIn(_, BuiltInStyle::Extended)
@@ -74,6 +55,12 @@ impl VarTypeIsExtended for DimType {
7455
}
7556
}
7657

58+
impl DimType {
59+
pub fn fixed_length_string(len: u16, pos: Position) -> Self {
60+
Self::FixedLengthString(Expression::IntegerLiteral(len as i32).at_pos(pos), len)
61+
}
62+
}
63+
7764
impl HasExpressionType for DimType {
7865
fn expression_type(&self) -> ExpressionType {
7966
match self {
@@ -87,14 +74,3 @@ impl HasExpressionType for DimType {
8774
}
8875
}
8976
}
90-
91-
impl VarTypeQualifier for DimType {
92-
fn to_qualifier_recursively(&self) -> Option<TypeQualifier> {
93-
match self {
94-
Self::BuiltIn(q, _) => Some(*q),
95-
Self::FixedLengthString(_, _) => Some(TypeQualifier::DollarString),
96-
Self::Array(_, e) => e.to_qualifier_recursively(),
97-
_ => None,
98-
}
99-
}
100-
}

rusty_parser/src/specific/core/param_name.rs

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::pc::*;
2+
use crate::specific::core::var_name;
23
use crate::specific::pc_specific::*;
34
use crate::specific::Keyword;
45
use crate::specific::*;
@@ -19,72 +20,34 @@ pub enum ParamType {
1920
Array(Box<Self>),
2021
}
2122

22-
impl VarTypeNewBuiltInCompact for ParamType {
23+
impl VarType for ParamType {
2324
fn new_built_in_compact(q: TypeQualifier) -> Self {
2425
Self::BuiltIn(q, BuiltInStyle::Compact)
2526
}
26-
}
27-
28-
impl VarTypeNewBuiltInExtended for ParamType {
2927
fn new_built_in_extended(q: TypeQualifier) -> Self {
3028
Self::BuiltIn(q, BuiltInStyle::Extended)
3129
}
32-
}
3330

34-
impl VarTypeToArray for ParamType {
35-
type ArrayType = Option<(Token, Token)>;
36-
37-
fn to_array(self, array_type: Self::ArrayType) -> Self {
38-
if array_type.is_none() {
39-
self
40-
} else {
41-
Self::Array(Box::new(self))
42-
}
43-
}
44-
}
45-
46-
impl VarTypeNewUserDefined for ParamType {
4731
fn new_user_defined(bare_name_pos: BareNamePos) -> Self {
4832
Self::UserDefined(bare_name_pos)
4933
}
50-
}
5134

52-
impl VarTypeToUserDefinedRecursively for ParamType {
5335
fn as_user_defined_recursively(&self) -> Option<&BareNamePos> {
5436
match self {
5537
Self::UserDefined(n) => Some(n),
5638
Self::Array(e) => e.as_user_defined_recursively(),
5739
_ => None,
5840
}
5941
}
60-
}
6142

62-
impl HasExpressionType for ParamType {
63-
fn expression_type(&self) -> ExpressionType {
64-
match self {
65-
Self::BuiltIn(qualifier, _) => ExpressionType::BuiltIn(*qualifier),
66-
Self::UserDefined(Positioned { element, .. }) => {
67-
ExpressionType::UserDefined(element.clone())
68-
}
69-
Self::Array(boxed_element_type) => {
70-
ExpressionType::Array(Box::new(boxed_element_type.expression_type()))
71-
}
72-
_ => ExpressionType::Unresolved,
73-
}
74-
}
75-
}
76-
77-
impl VarTypeQualifier for ParamType {
7843
fn to_qualifier_recursively(&self) -> Option<TypeQualifier> {
7944
match self {
8045
Self::BuiltIn(q, _) => Some(*q),
8146
Self::Array(e) => e.to_qualifier_recursively(),
8247
_ => None,
8348
}
8449
}
85-
}
8650

87-
impl VarTypeIsExtended for ParamType {
8851
fn is_extended(&self) -> bool {
8952
match self {
9053
Self::BuiltIn(_, BuiltInStyle::Extended) | Self::UserDefined(_) => true,
@@ -94,6 +57,21 @@ impl VarTypeIsExtended for ParamType {
9457
}
9558
}
9659

60+
impl HasExpressionType for ParamType {
61+
fn expression_type(&self) -> ExpressionType {
62+
match self {
63+
Self::BuiltIn(qualifier, _) => ExpressionType::BuiltIn(*qualifier),
64+
Self::UserDefined(Positioned { element, .. }) => {
65+
ExpressionType::UserDefined(element.clone())
66+
}
67+
Self::Array(boxed_element_type) => {
68+
ExpressionType::Array(Box::new(boxed_element_type.expression_type()))
69+
}
70+
_ => ExpressionType::Unresolved,
71+
}
72+
}
73+
}
74+
9775
/// Parses a Param name. Possible options:
9876
/// A
9977
/// A%

0 commit comments

Comments
 (0)