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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use super::{combinator::*, expression::*, identifier::*, types::*};
use crate::ast::*;

/// 309 stmt = [alias_stmt] | [assignment_stmt] | [case_stmt] | [compound_stmt] | [escape_stmt] | [if_stmt] | [null_stmt] | [procedure_call_stmt] | [repeat_stmt] | [return_stmt] | [skip_stmt] .
pub fn stmt(input: &str) -> ParseResult<Statement> {
    alt((
        alias_stmt,
        assignment_stmt,
        case_stmt,
        compound_stmt,
        escape_stmt,
        if_stmt,
        null_stmt,
        procedure_call_stmt,
        repeat_stmt,
        return_stmt,
        skip_stmt,
    ))
    .parse(input)
}

/// 174 alias_stmt = ALIAS [variable_id] FOR [general_ref] { [qualifier] } `;` [stmt] { [stmt] } END_ALIAS `;` .
pub fn alias_stmt(input: &str) -> ParseResult<Statement> {
    tuple((
        tag("ALIAS"),
        variable_id,
        tag("FOR"),
        general_ref,
        many0(qualifier),
        char(';'),
        many1(stmt),
        tag("END_ALIAS"),
        char(';'),
    ))
    .map(
        |(_alias, name, _for, dest, qualifiers, _semicolon1, statements, _end, _semicolon2)| {
            Statement::Alias {
                name,
                dest,
                qualifiers,
                statements,
            }
        },
    )
    .parse(input)
}

/// 176 assignment_stmt = [general_ref] { [qualifier] } `:=` [expression] `;` .
pub fn assignment_stmt(input: &str) -> ParseResult<Statement> {
    tuple((
        general_ref,
        many0(qualifier),
        tag(":="),
        expression,
        char(';'),
    ))
    .map(
        |(name, qualifiers, _def, expr, _semicolon)| Statement::Assignment {
            name,
            qualifiers,
            expr,
        },
    )
    .parse(input)
}

/// 191 case_stmt = CASE [selector] OF { [case_action] } \[ OTHERWISE `:` [stmt] \] END_CASE `;` .
pub fn case_stmt(input: &str) -> ParseResult<Statement> {
    tuple((
        tag("CASE"),
        selector,
        tag("OF"),
        many0(case_action),
        opt(tuple((tag("OTHERWISE"), char(':'), stmt))
            .map(|(_otherwise, _colon, stmt)| Box::new(stmt))),
        tag("END_CASE"),
        char(';'),
    ))
    .map(
        |(_case, selector, _of, actions, otherwise, _end, _semicolon)| Statement::Case {
            selector,
            actions,
            otherwise,
        },
    )
    .parse(input)
}

/// 299 selector = [expression] .
pub fn selector(input: &str) -> ParseResult<Expression> {
    expression(input)
}

/// 189 case_action = [case_label] { `,` [case_label] } `:` [stmt] .
pub fn case_action(input: &str) -> ParseResult<(Vec<Expression>, Statement)> {
    tuple((comma_separated(case_label), char(':'), stmt))
        .map(|(labels, _colon, statement)| (labels, statement))
        .parse(input)
}

/// 190 case_label = [expression] .
pub fn case_label(input: &str) -> ParseResult<Expression> {
    expression(input)
}

/// 192 compound_stmt = BEGIN [stmt] { [stmt] } END `;` .
pub fn compound_stmt(input: &str) -> ParseResult<Statement> {
    tuple((tag("BEGIN"), many1(stmt), tag("END"), char(';')))
        .map(|(_begin, statements, _end, _semicolon)| Statement::Compound { statements })
        .parse(input)
}

/// 214 escape_stmt = ESCAPE `;` .
pub fn escape_stmt(input: &str) -> ParseResult<Statement> {
    tuple((tag("ESCAPE"), char(';')))
        .map(|(_skip, _semicolon)| Statement::Escape)
        .parse(input)
}

/// 233 if_stmt = IF [logical_expression] THEN [stmt] { [stmt] } \[ ELSE [stmt] { [stmt] } \] END_IF `;` .
pub fn if_stmt(input: &str) -> ParseResult<Statement> {
    tuple((
        tag("IF"),
        logical_expression,
        tag("THEN"),
        many1(stmt),
        opt(tuple((tag("ELSE"), many1(stmt))).map(|(_else, stmts)| stmts)),
        tag("END_IF"),
        char(';'),
    ))
    .map(
        |(_if, condition, _then, then_branch, else_branch, _endif, _semicolon)| Statement::If {
            condition,
            then_branch,
            else_branch,
        },
    )
    .parse(input)
}

/// 260 null_stmt = `;` .
pub fn null_stmt(input: &str) -> ParseResult<Statement> {
    char(';').map(|_c| Statement::Null).parse(input)
}

/// 270 procedure_call_stmt = ( [built_in_procedure] | [procedure_ref] ) \[ [actual_parameter_list] \] `;` .
pub fn procedure_call_stmt(input: &str) -> ParseResult<Statement> {
    tuple((
        alt((
            built_in_procedure,
            procedure_ref.map(ProcedureCallName::Reference),
        )),
        opt(actual_parameter_list),
        char(';'),
    ))
    .map(
        |(procedure, parameters, _semicolon)| Statement::ProcedureCall {
            procedure,
            parameters,
        },
    )
    .parse(input)
}

/// 188 built_in_procedure = INSERT | REMOVE .
pub fn built_in_procedure(input: &str) -> ParseResult<ProcedureCallName> {
    alt((
        tag("INSERT").map(|_| ProcedureCallName::Insert),
        tag("REMOVE").map(|_| ProcedureCallName::Remove),
    ))
    .parse(input)
}

/// 286 repeat_stmt = REPEAT [repeat_control] `;` [stmt] { [stmt] } END_REPEAT `;` .
pub fn repeat_stmt(input: &str) -> ParseResult<Statement> {
    tuple((
        tag("REPEAT"),
        repeat_control,
        char(';'),
        many1(stmt),
        tag("END_REPEAT"),
        char(';'),
    ))
    .map(
        |(_repeat, control, _semicolon1, statements, _end, _semicolon2)| Statement::Repeat {
            control,
            statements,
        },
    )
    .parse(input)
}

/// 285 repeat_control = \[ [increment_control] \] \[ [while_control] \] \[ [until_control] \] .
pub fn repeat_control(input: &str) -> ParseResult<RepeatControl> {
    tuple((
        opt(increment_control),
        opt(while_control),
        opt(until_control),
    ))
    .map(|(increment, while_, until)| RepeatControl {
        increment,
        while_,
        until,
    })
    .parse(input)
}

/// 235 increment_control = [variable_id] `:=` [bound_1] TO [bound_2] \[ BY [increment] \] .
pub fn increment_control(input: &str) -> ParseResult<RepeatIncrement> {
    tuple((
        variable_id,
        tag(":="),
        bound_1,
        tag("TO"),
        bound_2,
        opt(tuple((tag("BY"), increment)).map(|(_by, inc)| inc)),
    ))
    .map(
        |(variable, _def, begin, _to, end, increment)| RepeatIncrement {
            variable,
            begin,
            end,
            increment,
        },
    )
    .parse(input)
}

/// 234 increment = [numeric_expression] .
pub fn increment(input: &str) -> ParseResult<Expression> {
    numeric_expression(input)
}

/// 339 while_control = WHILE [logical_expression] .
pub fn while_control(input: &str) -> ParseResult<Expression> {
    tuple((tag("WHILE"), logical_expression))
        .map(|(_where, expr)| expr)
        .parse(input)
}

/// 335 until_control = UNTIL [logical_expression] .
pub fn until_control(input: &str) -> ParseResult<Expression> {
    tuple((tag("UNTIL"), logical_expression))
        .map(|(_where, expr)| expr)
        .parse(input)
}

/// 290 return_stmt = RETURN \[ `(` [expression] `)` \] `;` .
pub fn return_stmt(input: &str) -> ParseResult<Statement> {
    tuple((
        tag("RETURN"),
        opt(tuple((char('('), expression, char(')'))).map(|(_open, expr, _close)| expr)),
        char(';'),
    ))
    .map(|(_return, value, _semicolon)| Statement::Return { value })
    .parse(input)
}

/// 308 skip_stmt = SKIP `;` .
pub fn skip_stmt(input: &str) -> ParseResult<Statement> {
    tuple((tag("SKIP"), char(';')))
        .map(|(_skip, _semicolon)| Statement::Skip)
        .parse(input)
}

#[cfg(test)]
mod tests {
    use nom::Finish;

    #[test]
    fn alias() {
        // From ISO-10303-11 p.112
        let exp_str = r#"
        ALIAS s FOR the_line.start_point;
            ALIAS e FOR the_line.end_point;
                RETURN (SQRT((s.x - e.x)**2 + (s.y - e.y)**2 + (s.z - e.z)**2));
            END_ALIAS;
        END_ALIAS;
        "#
        .trim();
        let (residual, (result, _remark)) = super::alias_stmt(exp_str).finish().unwrap();
        dbg!(&result);
        assert_eq!(residual, "");
    }

    #[test]
    fn if_then() {
        let exp_str = r#"
        IF SIZEOF (the_bag) > 0 THEN
            REPEAT i := 1 TO HIINDEX (the_bag);
                the_set := the_set + the_bag [i];
            END_REPEAT;
        END_IF;
        "#
        .trim();
        let (residual, (result, _remark)) = super::if_stmt(exp_str).finish().unwrap();
        dbg!(&result);
        assert_eq!(residual, "");
    }

    #[test]
    fn if_then_else() {
        // From ISO-10303-11 p.128
        let exp_str = r#"
        IF a < 10 THEN
            c := c + 1;
        ELSE
            c := c - 1;
        END_IF;
        "#
        .trim();
        let (residual, (result, _remark)) = super::if_stmt(exp_str).finish().unwrap();
        dbg!(&result);
        assert_eq!(residual, "");
    }

    #[test]
    fn procedure_call() {
        // From ISO-10303-11 p.128
        let exp_str = r#"
        INSERT (point_list, this_point, here );
        "#
        .trim();
        let (residual, (result, _remark)) = super::procedure_call_stmt(exp_str).finish().unwrap();
        dbg!(&result);
        assert_eq!(residual, "");
    }

    #[test]
    fn return_expr1() {
        // From ISO-10303-11 p.122, part of alias_stmt
        let exp_str = r#"
        RETURN (SQRT((s.x - e.x)**2 + (s.y - e.y)**2 + (s.z - e.z)**2));
        "#
        .trim();
        let (residual, (result, _remark)) = super::return_stmt(exp_str).finish().unwrap();
        dbg!(&result);
        assert_eq!(residual, "");
    }

    #[test]
    fn return_expr2() {
        // From AP201
        let exp_str = r#"
        RETURN ((type1 IN USEDIN(sample, '')) AND (type2 IN USEDIN(sample, '')));
        "#
        .trim();
        let (residual, (result, _remark)) = super::return_stmt(exp_str).finish().unwrap();
        dbg!(&result);
        assert_eq!(residual, "");
    }

    #[test]
    fn return_none() {
        // From ISO-10303-11 p.131
        let exp_str = r#"
        RETURN;
        "#
        .trim();
        let (residual, (result, _remark)) = super::return_stmt(exp_str).finish().unwrap();
        dbg!(&result);
        assert_eq!(residual, "");
    }

    #[test]
    fn skip() {
        // From ISO-10303-11 p.131
        let exp_str = r#"
        SKIP;
        "#
        .trim();
        let (residual, (result, _remark)) = super::skip_stmt(exp_str).finish().unwrap();
        dbg!(&result);
        assert_eq!(residual, "");
    }

    #[test]
    fn repeat_until() {
        // From ISO-10303-11 p.131
        let exp_str = r#"
        REPEAT UNTIL (a=1);
            IF (a < 0) THEN
                SKIP;
            END_IF;
        END_REPEAT;
        "#
        .trim();
        let (residual, (result, _remark)) = super::repeat_stmt(exp_str).finish().unwrap();
        dbg!(&result);
        assert_eq!(residual, "");
    }

    #[test]
    fn repeat_increment() {
        // From AP201
        let exp_str = r#"
        REPEAT i := 1 TO HIINDEX (the_bag);
            the_set := the_set + the_bag [i];
        END_REPEAT;
        "#
        .trim();
        let (residual, (result, _remark)) = super::repeat_stmt(exp_str).finish().unwrap();
        dbg!(&result);
        assert_eq!(residual, "");
    }
}