-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeclaration.rs
More file actions
244 lines (226 loc) · 8.14 KB
/
declaration.rs
File metadata and controls
244 lines (226 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use rusty_pc::*;
use crate::core::name::{bare_name_p, name_p};
use crate::core::param_name::parameter_pos_p;
use crate::input::StringView;
use crate::pc_specific::*;
use crate::{ParserError, *};
// Declaration ::= DECLARE<ws+>(FunctionDeclaration|SubDeclaration)
// FunctionDeclaration ::= FUNCTION<ws+><Name><ws*><DeclarationParameters>
// SubDeclaration ::= SUB<ws+><BareName><ws*><DeclarationParameters>
// DeclarationParameters ::= <eof> | <eol> | '(' <DeclaredNames> ')'
// DeclaredNames ::= <> | <DeclaredName> | <DeclaredName><ws*>,<ws*><DeclaredNames>
// DeclaredName ::= <BareName> | <CompactBuiltIn> | <ExtendedBuiltIn> | <UserDefined>
// BareName ::= [a-zA-Z]([a-zA-Z0-9\.]*) ! Keyword
// CompactBuiltIn ::= <BareName>[!#$%&]
// ExtendedBuiltIn ::= <BareName><ws+>AS<ws+>(SINGLE|DOUBLE|STRING|INTEGER|LONG)
// UserDefined ::= <BareName><ws+>AS<ws+><BareName>
pub fn declaration_p() -> impl Parser<StringView, Output = GlobalStatement, Error = ParserError> {
keyword_ws_p(Keyword::Declare).and_keep_right(
OrParser::new(vec![
Box::new(
function_declaration_p().map(|(n, p)| GlobalStatement::function_declaration(n, p)),
),
Box::new(sub_declaration_p().map(|(n, p)| GlobalStatement::sub_declaration(n, p))),
])
.or_expected("FUNCTION or SUB after DECLARE"),
)
}
pub fn function_declaration_p()
-> impl Parser<StringView, Output = (NamePos, Parameters), Error = ParserError> {
seq3(
keyword_ws_p(Keyword::Function),
name_p().with_pos().or_expected("function name"),
declaration_parameters_p(),
|_, function_name_pos, declaration_parameters| (function_name_pos, declaration_parameters),
)
}
pub fn sub_declaration_p()
-> impl Parser<StringView, Output = (BareNamePos, Parameters), Error = ParserError> {
seq3(
keyword_ws_p(Keyword::Sub),
bare_name_p().with_pos().or_expected("sub name"),
declaration_parameters_p(),
|_, sub_name_pos, declaration_parameters| (sub_name_pos, declaration_parameters),
)
}
// result ::= "" | "(" ")" | "(" parameter (,parameter)* ")"
fn declaration_parameters_p() -> impl Parser<StringView, Output = Parameters, Error = ParserError> {
lead_opt_ws(in_parenthesis(csv(parameter_pos_p()).or_default())).or_default()
}
#[cfg(test)]
mod tests {
use rusty_common::*;
use crate::test_utils::*;
use crate::{assert_function_declaration, assert_parser_err, *};
#[test]
fn test_fn() {
assert_function_declaration!(
"DECLARE FUNCTION Fib! (N!)",
Name::from("Fib!"),
vec![Parameter::new(
"N".into(),
ParamType::BuiltIn(TypeQualifier::BangSingle, BuiltInStyle::Compact)
)]
);
}
#[test]
fn test_lower_case() {
assert_function_declaration!(
"declare function echo$(msg$)",
Name::from("echo$"),
vec![Parameter::new(
"msg".into(),
ParamType::BuiltIn(TypeQualifier::DollarString, BuiltInStyle::Compact)
)]
);
}
#[test]
fn test_inline_comment() {
let input = r#"
DECLARE FUNCTION Echo(X) ' Echoes stuff back
FUNCTION Echo(X) ' Implementation of Echo
END FUNCTION ' End of implementation
"#;
let program = parse(input);
assert_eq!(
program,
vec![
GlobalStatement::function_declaration(
"Echo".as_name(2, 26),
vec![Parameter::new("X".into(), ParamType::Bare).at_rc(2, 31)]
)
.at_rc(2, 9),
GlobalStatement::Statement(Statement::Comment(" Echoes stuff back".to_string()))
.at_rc(2, 34),
GlobalStatement::FunctionImplementation(FunctionImplementation {
name: "Echo".as_name(3, 18),
params: vec![Parameter::new("X".into(), ParamType::Bare).at_rc(3, 23)],
body: vec![
Statement::Comment(" Implementation of Echo".to_string()).at_rc(3, 26)
],
is_static: false
})
.at_rc(3, 9),
GlobalStatement::Statement(Statement::Comment(
" End of implementation".to_string()
))
.at_rc(4, 22),
]
);
}
#[test]
fn test_string_fixed_length_function_param_not_allowed() {
let input = "DECLARE FUNCTION Echo(X AS STRING * 5)";
assert_parser_err!(input, expected(")"));
}
#[test]
fn test_string_fixed_length_sub_param_not_allowed() {
let input = "DECLARE SUB Echo(X AS STRING * 5)";
assert_parser_err!(input, expected(")"));
}
#[test]
fn test_user_defined_param_name_cannot_include_period() {
let inputs = [
"DECLARE FUNCTION Echo(X.Y AS Card)",
"DECLARE SUB Echo(X.Y AS Card)",
];
for input in inputs {
// TODO should also be reported as IdentifierCannotIncludePeriod
assert_parser_err!(
input,
"Expected: DOUBLE or INTEGER or LONG or SINGLE or STRING"
);
}
}
#[test]
fn test_user_defined_param_type_cannot_include_period() {
let inputs = [
"DECLARE FUNCTION Echo(XY AS Ca.rd)",
"DECLARE SUB Echo(XY AS Ca.rd)",
];
for input in inputs {
assert_parser_err!(input, ParserError::IdentifierCannotIncludePeriod);
}
}
#[test]
fn test_array_parameter() {
let input = r#"
DECLARE FUNCTION Echo(X$())
FUNCTION Echo(X$())
END FUNCTION
"#;
let program = parse(input);
assert_eq!(
program,
vec![
GlobalStatement::function_declaration(
"Echo".as_name(2, 26),
vec![
Parameter::new(
"X".into(),
ParamType::Array(Box::new(ParamType::BuiltIn(
TypeQualifier::DollarString,
BuiltInStyle::Compact
)))
)
.at_rc(2, 31)
]
)
.at_rc(2, 9),
GlobalStatement::FunctionImplementation(FunctionImplementation {
name: "Echo".as_name(3, 18),
params: vec![
Parameter::new(
"X".into(),
ParamType::Array(Box::new(ParamType::BuiltIn(
TypeQualifier::DollarString,
BuiltInStyle::Compact
)))
)
.at_rc(3, 23)
],
body: vec![],
is_static: false
})
.at_rc(3, 9),
]
);
}
#[test]
fn test_sub_no_args_space_after_sub_name() {
let input = r#"
DECLARE SUB ScrollUp ()
"#;
let program = parse(input);
assert_eq!(
program,
vec![
GlobalStatement::sub_declaration("ScrollUp".as_bare_name(2, 21), vec![])
.at_rc(2, 9)
]
);
}
#[test]
fn test_sub_one_arg_space_after_sub_name() {
let input = r#"
DECLARE SUB LCenter (text$)
"#;
let program = parse(input);
assert_eq!(
program,
vec![
GlobalStatement::sub_declaration(
"LCenter".as_bare_name(2, 21),
vec![
Parameter::new(
"text".into(),
ParamType::BuiltIn(TypeQualifier::DollarString, BuiltInStyle::Compact)
)
.at_rc(2, 30)
]
)
.at_rc(2, 9)
]
);
}
}