-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathparser.rs
More file actions
4850 lines (4464 loc) · 175 KB
/
parser.rs
File metadata and controls
4850 lines (4464 loc) · 175 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// EndBASIC
// Copyright 2020 Julio Merino
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//! Statement and expression parser for the EndBASIC language.
use crate::ast::*;
use crate::lexer::{Lexer, PeekableLexer, Token, TokenSpan};
use crate::reader::LineCol;
use std::cmp::Ordering;
use std::io;
/// Parser errors.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Bad syntax in the input program.
#[error("{}: {}", .0, .1)]
Bad(LineCol, String),
/// I/O error while parsing the input program.
#[error("{0}: {1}")]
Io(LineCol, io::Error),
}
impl From<(LineCol, io::Error)> for Error {
fn from(value: (LineCol, io::Error)) -> Self {
Self::Io(value.0, value.1)
}
}
/// Result for parser return values.
pub type Result<T> = std::result::Result<T, Error>;
/// Transforms a `VarRef` into an unannotated name.
///
/// This is only valid for references that have no annotations in them.
fn vref_to_unannotated_string(vref: VarRef, pos: LineCol) -> Result<String> {
if vref.ref_type.is_some() {
return Err(Error::Bad(pos, format!("Type annotation not allowed in {}", vref)));
}
Ok(vref.name)
}
/// Converts a collection of `ArgSpan`s passed to a function or array reference to a collection
/// of expressions with proper validation.
pub(crate) fn argspans_to_exprs(spans: Vec<ArgSpan>) -> Vec<Expr> {
let nargs = spans.len();
let mut exprs = Vec::with_capacity(spans.len());
for (i, span) in spans.into_iter().enumerate() {
debug_assert!(
(span.sep == ArgSep::End || i < nargs - 1)
|| (span.sep != ArgSep::End || i == nargs - 1)
);
match span.expr {
Some(expr) => exprs.push(expr),
None => unreachable!(),
}
}
exprs
}
/// Operators that can appear within an expression.
///
/// The main difference between this and `lexer::Token` is that, in here, we differentiate the
/// meaning of a minus sign and separate it in its two variants: the 2-operand `Minus` and the
/// 1-operand `Negate`.
///
/// That said, this type also is the right place to abstract away operator-related logic to
/// implement the expression parsing algorithm, so it's not completely useless.
#[derive(Debug, Eq, PartialEq)]
enum ExprOp {
LeftParen,
Add,
Subtract,
Multiply,
Divide,
Modulo,
Power,
Negate,
Equal,
NotEqual,
Less,
LessEqual,
Greater,
GreaterEqual,
And,
Not,
Or,
Xor,
ShiftLeft,
ShiftRight,
}
impl ExprOp {
/// Constructs a new operator based on a token, which must have a valid correspondence.
fn from(t: Token) -> Self {
match t {
Token::Equal => ExprOp::Equal,
Token::NotEqual => ExprOp::NotEqual,
Token::Less => ExprOp::Less,
Token::LessEqual => ExprOp::LessEqual,
Token::Greater => ExprOp::Greater,
Token::GreaterEqual => ExprOp::GreaterEqual,
Token::Plus => ExprOp::Add,
Token::Multiply => ExprOp::Multiply,
Token::Divide => ExprOp::Divide,
Token::Modulo => ExprOp::Modulo,
Token::Exponent => ExprOp::Power,
Token::And => ExprOp::And,
Token::Or => ExprOp::Or,
Token::Xor => ExprOp::Xor,
Token::ShiftLeft => ExprOp::ShiftLeft,
Token::ShiftRight => ExprOp::ShiftRight,
Token::Minus => panic!("Ambiguous token; cannot derive ExprOp"),
_ => panic!("Called on an non-operator"),
}
}
/// Returns the priority of this operator. The specific number's meaning is only valid when
/// comparing it against other calls to this function. Higher number imply higher priority.
fn priority(&self) -> i8 {
match self {
ExprOp::LeftParen => 6,
ExprOp::Power => 6,
ExprOp::Negate => 5,
ExprOp::Not => 5,
ExprOp::Multiply => 4,
ExprOp::Divide => 4,
ExprOp::Modulo => 4,
ExprOp::Add => 3,
ExprOp::Subtract => 3,
ExprOp::ShiftLeft => 2,
ExprOp::ShiftRight => 2,
ExprOp::Equal => 1,
ExprOp::NotEqual => 1,
ExprOp::Less => 1,
ExprOp::LessEqual => 1,
ExprOp::Greater => 1,
ExprOp::GreaterEqual => 1,
ExprOp::And => 0,
ExprOp::Or => 0,
ExprOp::Xor => 0,
}
}
}
/// Wrapper over an `ExprOp` to extend it with its position.
struct ExprOpSpan {
/// The wrapped expression operation.
op: ExprOp,
/// The position where the operation appears in the input.
pos: LineCol,
}
impl ExprOpSpan {
/// Creates a new span from its parts.
fn new(op: ExprOp, pos: LineCol) -> Self {
Self { op, pos }
}
/// Pops operands from the `expr` stack, applies this operation, and pushes the result back.
fn apply(&self, exprs: &mut Vec<Expr>) -> Result<()> {
fn apply1(
exprs: &mut Vec<Expr>,
pos: LineCol,
f: fn(Box<UnaryOpSpan>) -> Expr,
) -> Result<()> {
if exprs.is_empty() {
return Err(Error::Bad(pos, "Not enough values to apply operator".to_owned()));
}
let expr = exprs.pop().unwrap();
exprs.push(f(Box::from(UnaryOpSpan { expr, pos })));
Ok(())
}
fn apply2(
exprs: &mut Vec<Expr>,
pos: LineCol,
f: fn(Box<BinaryOpSpan>) -> Expr,
) -> Result<()> {
if exprs.len() < 2 {
return Err(Error::Bad(pos, "Not enough values to apply operator".to_owned()));
}
let rhs = exprs.pop().unwrap();
let lhs = exprs.pop().unwrap();
exprs.push(f(Box::from(BinaryOpSpan { lhs, rhs, pos })));
Ok(())
}
match self.op {
ExprOp::Add => apply2(exprs, self.pos, Expr::Add),
ExprOp::Subtract => apply2(exprs, self.pos, Expr::Subtract),
ExprOp::Multiply => apply2(exprs, self.pos, Expr::Multiply),
ExprOp::Divide => apply2(exprs, self.pos, Expr::Divide),
ExprOp::Modulo => apply2(exprs, self.pos, Expr::Modulo),
ExprOp::Power => apply2(exprs, self.pos, Expr::Power),
ExprOp::Equal => apply2(exprs, self.pos, Expr::Equal),
ExprOp::NotEqual => apply2(exprs, self.pos, Expr::NotEqual),
ExprOp::Less => apply2(exprs, self.pos, Expr::Less),
ExprOp::LessEqual => apply2(exprs, self.pos, Expr::LessEqual),
ExprOp::Greater => apply2(exprs, self.pos, Expr::Greater),
ExprOp::GreaterEqual => apply2(exprs, self.pos, Expr::GreaterEqual),
ExprOp::And => apply2(exprs, self.pos, Expr::And),
ExprOp::Or => apply2(exprs, self.pos, Expr::Or),
ExprOp::Xor => apply2(exprs, self.pos, Expr::Xor),
ExprOp::ShiftLeft => apply2(exprs, self.pos, Expr::ShiftLeft),
ExprOp::ShiftRight => apply2(exprs, self.pos, Expr::ShiftRight),
ExprOp::Negate => apply1(exprs, self.pos, Expr::Negate),
ExprOp::Not => apply1(exprs, self.pos, Expr::Not),
ExprOp::LeftParen => Ok(()),
}
}
}
/// Iterator over the statements of the language.
pub struct Parser<'a> {
lexer: PeekableLexer<'a>,
}
impl<'a> Parser<'a> {
/// Creates a new parser from the given readable.
fn from(input: &'a mut dyn io::Read) -> Self {
Self { lexer: Lexer::from(input).peekable() }
}
/// Expects the peeked token to be `t` and consumes it. Otherwise, leaves the token in the
/// stream and fails with error `err`.
fn expect_and_consume<E: Into<String>>(&mut self, t: Token, err: E) -> Result<TokenSpan> {
let peeked = self.lexer.peek()?;
if peeked.token != t {
return Err(Error::Bad(peeked.pos, err.into()));
}
Ok(self.lexer.consume_peeked())
}
/// Expects the peeked token to be `t` and consumes it. Otherwise, leaves the token in the
/// stream and fails with error `err`, pointing at `pos` as the original location of the
/// problem.
fn expect_and_consume_with_pos<E: Into<String>>(
&mut self,
t: Token,
pos: LineCol,
err: E,
) -> Result<()> {
let peeked = self.lexer.peek()?;
if peeked.token != t {
return Err(Error::Bad(pos, err.into()));
}
self.lexer.consume_peeked();
Ok(())
}
/// Reads statements until the `delim` keyword is found. The delimiter is not consumed.
fn parse_until(&mut self, delim: Token) -> Result<Vec<Statement>> {
let mut stmts = vec![];
loop {
let peeked = self.lexer.peek()?;
if peeked.token == delim {
break;
} else if peeked.token == Token::Eol {
self.lexer.consume_peeked();
continue;
}
match self.parse_one_safe()? {
Some(stmt) => stmts.push(stmt),
None => break,
}
}
Ok(stmts)
}
/// Parses an assignment for the variable reference `vref` already read.
fn parse_assignment(&mut self, vref: VarRef, vref_pos: LineCol) -> Result<Statement> {
let expr = self.parse_required_expr("Missing expression in assignment")?;
let next = self.lexer.peek()?;
match &next.token {
Token::Eof | Token::Eol | Token::Else => (),
t => return Err(Error::Bad(next.pos, format!("Unexpected {} in assignment", t))),
}
Ok(Statement::Assignment(AssignmentSpan { vref, vref_pos, expr }))
}
/// Parses an assignment to the array `varref` with `subscripts`, both of which have already
/// been read.
fn parse_array_assignment(
&mut self,
vref: VarRef,
vref_pos: LineCol,
subscripts: Vec<Expr>,
) -> Result<Statement> {
let expr = self.parse_required_expr("Missing expression in array assignment")?;
let next = self.lexer.peek()?;
match &next.token {
Token::Eof | Token::Eol | Token::Else => (),
t => return Err(Error::Bad(next.pos, format!("Unexpected {} in array assignment", t))),
}
Ok(Statement::ArrayAssignment(ArrayAssignmentSpan { vref, vref_pos, subscripts, expr }))
}
/// Parses a builtin call (things of the form `INPUT a`).
fn parse_builtin_call(
&mut self,
vref: VarRef,
vref_pos: LineCol,
mut first: Option<Expr>,
) -> Result<Statement> {
let mut name = vref_to_unannotated_string(vref, vref_pos)?;
name.make_ascii_uppercase();
let mut args = vec![];
loop {
let expr = self.parse_expr(first.take())?;
let peeked = self.lexer.peek()?;
match peeked.token {
Token::Eof | Token::Eol | Token::Else => {
if expr.is_some() || !args.is_empty() {
args.push(ArgSpan { expr, sep: ArgSep::End, sep_pos: peeked.pos });
}
break;
}
Token::Semicolon => {
let peeked = self.lexer.consume_peeked();
args.push(ArgSpan { expr, sep: ArgSep::Short, sep_pos: peeked.pos });
}
Token::Comma => {
let peeked = self.lexer.consume_peeked();
args.push(ArgSpan { expr, sep: ArgSep::Long, sep_pos: peeked.pos });
}
Token::As => {
let peeked = self.lexer.consume_peeked();
args.push(ArgSpan { expr, sep: ArgSep::As, sep_pos: peeked.pos });
}
_ => {
return Err(Error::Bad(
peeked.pos,
"Expected comma, semicolon, or end of statement".to_owned(),
));
}
}
}
Ok(Statement::Call(CallSpan { vref: VarRef::new(name, None), vref_pos, args }))
}
/// Starts processing either an array reference or a builtin call and disambiguates between the
/// two.
fn parse_array_or_builtin_call(
&mut self,
vref: VarRef,
vref_pos: LineCol,
) -> Result<Statement> {
match self.lexer.peek()?.token {
Token::LeftParen => {
let left_paren = self.lexer.consume_peeked();
let spans = self.parse_comma_separated_exprs()?;
let mut exprs = spans.into_iter().map(|span| span.expr.unwrap()).collect();
match self.lexer.peek()?.token {
Token::Equal => {
self.lexer.consume_peeked();
self.parse_array_assignment(vref, vref_pos, exprs)
}
_ => {
if exprs.len() != 1 {
return Err(Error::Bad(
left_paren.pos,
"Expected expression".to_owned(),
));
}
self.parse_builtin_call(vref, vref_pos, Some(exprs.remove(0)))
}
}
}
_ => self.parse_builtin_call(vref, vref_pos, None),
}
}
/// Parses the type name of an `AS` type definition.
///
/// The `AS` token has already been consumed, so all this does is read a literal type name and
/// convert it to the corresponding expression type.
fn parse_as_type(&mut self) -> Result<(ExprType, LineCol)> {
let token_span = self.lexer.read()?;
match token_span.token {
Token::BooleanName => Ok((ExprType::Boolean, token_span.pos)),
Token::DoubleName => Ok((ExprType::Double, token_span.pos)),
Token::IntegerName => Ok((ExprType::Integer, token_span.pos)),
Token::TextName => Ok((ExprType::Text, token_span.pos)),
t => Err(Error::Bad(
token_span.pos,
format!("Invalid type name {} in AS type definition", t),
)),
}
}
/// Parses a `DATA` statement.
fn parse_data(&mut self) -> Result<Statement> {
let mut values = vec![];
loop {
let peeked = self.lexer.peek()?;
match peeked.token {
Token::Eof | Token::Eol | Token::Else => {
values.push(None);
break;
}
_ => (),
}
let token_span = self.lexer.read()?;
match token_span.token {
Token::Boolean(b) => {
values.push(Some(Expr::Boolean(BooleanSpan { value: b, pos: token_span.pos })))
}
Token::Double(d) => {
values.push(Some(Expr::Double(DoubleSpan { value: d, pos: token_span.pos })))
}
Token::Integer(i) => {
values.push(Some(Expr::Integer(IntegerSpan { value: i, pos: token_span.pos })))
}
Token::Text(t) => {
values.push(Some(Expr::Text(TextSpan { value: t, pos: token_span.pos })))
}
Token::Minus => {
let token_span2 = self.lexer.read()?;
match token_span2.token {
Token::Double(d) => values.push(Some(Expr::Double(DoubleSpan {
value: -d,
pos: token_span.pos,
}))),
Token::Integer(i) => values.push(Some(Expr::Integer(IntegerSpan {
value: -i,
pos: token_span.pos,
}))),
_ => {
return Err(Error::Bad(
token_span.pos,
"Expected number after -".to_owned(),
));
}
}
}
Token::Eof | Token::Eol | Token::Else => {
panic!("Should not be consumed here; handled above")
}
Token::Comma => {
values.push(None);
continue;
}
t => {
return Err(Error::Bad(
token_span.pos,
format!("Unexpected {} in DATA statement", t),
));
}
}
let peeked = self.lexer.peek()?;
match &peeked.token {
Token::Eof | Token::Eol | Token::Else => {
break;
}
Token::Comma => {
self.lexer.consume_peeked();
}
t => {
return Err(Error::Bad(
peeked.pos,
format!("Expected comma after datum but found {}", t),
));
}
}
}
Ok(Statement::Data(DataSpan { values }))
}
/// Parses the `AS typename` clause of a `DIM` statement. The caller has already consumed the
/// `AS` token.
fn parse_dim_as(&mut self) -> Result<(ExprType, LineCol)> {
let peeked = self.lexer.peek()?;
let (vtype, vtype_pos) = match peeked.token {
Token::Eof | Token::Eol => (ExprType::Integer, peeked.pos),
Token::As => {
self.lexer.consume_peeked();
self.parse_as_type()?
}
_ => return Err(Error::Bad(peeked.pos, "Expected AS or end of statement".to_owned())),
};
let next = self.lexer.peek()?;
match &next.token {
Token::Eof | Token::Eol => (),
t => return Err(Error::Bad(next.pos, format!("Unexpected {} in DIM statement", t))),
}
Ok((vtype, vtype_pos))
}
/// Parses a `DIM` statement.
fn parse_dim(&mut self) -> Result<Statement> {
let peeked = self.lexer.peek()?;
let mut shared = false;
if peeked.token == Token::Shared {
self.lexer.consume_peeked();
shared = true;
}
let token_span = self.lexer.read()?;
let vref = match token_span.token {
Token::Symbol(vref) => vref,
_ => {
return Err(Error::Bad(
token_span.pos,
"Expected variable name after DIM".to_owned(),
));
}
};
// TODO(jmmv): Why do we require unannotated strings? We could also take one and then
// skip the `AS <type>` portion.
let name = vref_to_unannotated_string(vref, token_span.pos)?;
let name_pos = token_span.pos;
match self.lexer.peek()?.token {
Token::LeftParen => {
let peeked = self.lexer.consume_peeked();
let dimensions = self.parse_comma_separated_exprs()?;
if dimensions.is_empty() {
return Err(Error::Bad(
peeked.pos,
"Arrays require at least one dimension".to_owned(),
));
}
let (subtype, subtype_pos) = self.parse_dim_as()?;
Ok(Statement::DimArray(DimArraySpan {
name,
name_pos,
shared,
dimensions: argspans_to_exprs(dimensions),
subtype,
subtype_pos,
}))
}
_ => {
let (vtype, vtype_pos) = self.parse_dim_as()?;
Ok(Statement::Dim(DimSpan { name, name_pos, shared, vtype, vtype_pos }))
}
}
}
/// Parses the `UNTIL` or `WHILE` clause of a `DO` loop.
///
/// `part` is a string indicating where the clause is expected (either after `DO` or after
/// `LOOP`).
///
/// Returns the guard expression and a boolean indicating if this is an `UNTIL` clause.
fn parse_do_guard(&mut self, part: &str) -> Result<Option<(Expr, bool)>> {
let peeked = self.lexer.peek()?;
match peeked.token {
Token::Until => {
self.lexer.consume_peeked();
let expr = self.parse_required_expr("No expression in UNTIL clause")?;
Ok(Some((expr, true)))
}
Token::While => {
self.lexer.consume_peeked();
let expr = self.parse_required_expr("No expression in WHILE clause")?;
Ok(Some((expr, false)))
}
Token::Eof | Token::Eol => Ok(None),
_ => {
let token_span = self.lexer.consume_peeked();
Err(Error::Bad(
token_span.pos,
format!("Expecting newline, UNTIL or WHILE after {}", part),
))
}
}
}
/// Parses a `DO` statement.
fn parse_do(&mut self, do_pos: LineCol) -> Result<Statement> {
let pre_guard = self.parse_do_guard("DO")?;
self.expect_and_consume(Token::Eol, "Expecting newline after DO")?;
let stmts = self.parse_until(Token::Loop)?;
self.expect_and_consume_with_pos(Token::Loop, do_pos, "DO without LOOP")?;
let post_guard = self.parse_do_guard("LOOP")?;
let guard = match (pre_guard, post_guard) {
(None, None) => DoGuard::Infinite,
(Some((guard, true)), None) => DoGuard::PreUntil(guard),
(Some((guard, false)), None) => DoGuard::PreWhile(guard),
(None, Some((guard, true))) => DoGuard::PostUntil(guard),
(None, Some((guard, false))) => DoGuard::PostWhile(guard),
(Some(_), Some(_)) => {
return Err(Error::Bad(
do_pos,
"DO loop cannot have pre and post guards at the same time".to_owned(),
));
}
};
Ok(Statement::Do(DoSpan { guard, body: stmts }))
}
/// Advances until the next statement after failing to parse a `DO` statement.
fn reset_do(&mut self) -> Result<()> {
loop {
match self.lexer.peek()?.token {
Token::Eof => break,
Token::Loop => {
self.lexer.consume_peeked();
loop {
match self.lexer.peek()?.token {
Token::Eof | Token::Eol => break,
_ => {
self.lexer.consume_peeked();
}
}
}
break;
}
_ => {
self.lexer.consume_peeked();
}
}
}
self.reset()
}
/// Parses a potential `END` statement but, if this corresponds to a statement terminator such
/// as `END IF`, returns the token that followed `END`.
fn maybe_parse_end(&mut self) -> Result<std::result::Result<Statement, Token>> {
match self.lexer.peek()?.token {
Token::Function => Ok(Err(Token::Function)),
Token::If => Ok(Err(Token::If)),
Token::Select => Ok(Err(Token::Select)),
Token::Sub => Ok(Err(Token::Sub)),
_ => {
let code = self.parse_expr(None)?;
Ok(Ok(Statement::End(EndSpan { code })))
}
}
}
/// Parses an `END` statement.
fn parse_end(&mut self, pos: LineCol) -> Result<Statement> {
match self.maybe_parse_end()? {
Ok(stmt) => Ok(stmt),
Err(token) => Err(Error::Bad(pos, format!("END {} without {}", token, token))),
}
}
/// Parses an `EXIT` statement.
fn parse_exit(&mut self, pos: LineCol) -> Result<Statement> {
let peeked = self.lexer.peek()?;
let stmt = match peeked.token {
Token::Do => Statement::ExitDo(ExitSpan { pos }),
Token::For => Statement::ExitFor(ExitSpan { pos }),
Token::Function => Statement::ExitFunction(ExitSpan { pos }),
Token::Sub => Statement::ExitSub(ExitSpan { pos }),
_ => {
return Err(Error::Bad(
peeked.pos,
"Expecting DO, FOR, FUNCTION or SUB after EXIT".to_owned(),
));
}
};
self.lexer.consume_peeked();
Ok(stmt)
}
/// Parses a variable list of comma-separated expressions. The caller must have consumed the
/// open parenthesis and we stop processing when we encounter the terminating parenthesis (and
/// consume it). We expect at least one expression.
fn parse_comma_separated_exprs(&mut self) -> Result<Vec<ArgSpan>> {
let mut spans = vec![];
// The first expression is optional to support calls to functions without arguments.
let mut is_first = true;
let mut prev_expr = self.parse_expr(None)?;
loop {
let peeked = self.lexer.peek()?;
let pos = peeked.pos;
match &peeked.token {
Token::RightParen => {
self.lexer.consume_peeked();
if let Some(expr) = prev_expr.take() {
spans.push(ArgSpan { expr: Some(expr), sep: ArgSep::End, sep_pos: pos });
} else {
if !is_first {
return Err(Error::Bad(pos, "Missing expression".to_owned()));
}
}
break;
}
Token::Comma => {
self.lexer.consume_peeked();
if let Some(expr) = prev_expr.take() {
// The first expression is optional to support calls to functions without
// arguments.
spans.push(ArgSpan { expr: Some(expr), sep: ArgSep::Long, sep_pos: pos });
} else {
return Err(Error::Bad(pos, "Missing expression".to_owned()));
}
prev_expr = self.parse_expr(None)?;
}
t => return Err(Error::Bad(pos, format!("Unexpected {}", t))),
}
is_first = false;
}
Ok(spans)
}
/// Parses an expression.
///
/// Returns `None` if no expression was found. This is necessary to treat the case of empty
/// arguments to statements, as is the case in `PRINT a , , b`.
///
/// If the caller has already processed a parenthesized term of an expression like
/// `(first) + second`, then that term must be provided in `first`.
///
/// This is an implementation of the Shunting Yard Algorithm by Edgar Dijkstra.
fn parse_expr(&mut self, first: Option<Expr>) -> Result<Option<Expr>> {
let mut exprs: Vec<Expr> = vec![];
let mut op_spans: Vec<ExprOpSpan> = vec![];
let mut need_operand = true; // Also tracks whether an upcoming minus is unary.
if let Some(e) = first {
exprs.push(e);
need_operand = false;
}
loop {
let mut handle_operand = |e, pos| {
if !need_operand {
return Err(Error::Bad(pos, "Unexpected value in expression".to_owned()));
}
need_operand = false;
exprs.push(e);
Ok(())
};
// Stop processing if we encounter an expression separator, but don't consume it because
// the caller needs to have access to it.
match self.lexer.peek()?.token {
Token::Eof
| Token::Eol
| Token::As
| Token::Comma
| Token::Else
| Token::Semicolon
| Token::Then
| Token::To
| Token::Step => break,
Token::RightParen if !op_spans.iter().any(|eos| eos.op == ExprOp::LeftParen) => {
// We encountered an unbalanced parenthesis but we don't know if this is
// because we were called from within an argument list (in which case the
// caller consumed the opening parenthesis and is expecting to consume the
// closing parenthesis) or because we really found an invalid expression.
// Only the caller can know, so avoid consuming the token and exit.
break;
}
_ => (),
};
let ts = self.lexer.consume_peeked();
match ts.token {
Token::Boolean(value) => {
handle_operand(Expr::Boolean(BooleanSpan { value, pos: ts.pos }), ts.pos)?
}
Token::Double(value) => {
handle_operand(Expr::Double(DoubleSpan { value, pos: ts.pos }), ts.pos)?
}
Token::Integer(value) => {
handle_operand(Expr::Integer(IntegerSpan { value, pos: ts.pos }), ts.pos)?
}
Token::Text(value) => {
handle_operand(Expr::Text(TextSpan { value, pos: ts.pos }), ts.pos)?
}
Token::Symbol(vref) => {
handle_operand(Expr::Symbol(SymbolSpan { vref, pos: ts.pos }), ts.pos)?
}
Token::LeftParen => {
// If the last operand we encountered was a symbol, collapse it and the left
// parenthesis into the beginning of a function call.
match exprs.pop() {
Some(Expr::Symbol(span)) => {
if !need_operand {
exprs.push(Expr::Call(CallSpan {
vref: span.vref,
vref_pos: span.pos,
args: self.parse_comma_separated_exprs()?,
}));
need_operand = false;
} else {
// We popped out the last expression to see if it this left
// parenthesis started a function call... but it did not (it is a
// symbol following a parenthesis) so put both the expression and
// the token back.
op_spans.push(ExprOpSpan::new(ExprOp::LeftParen, ts.pos));
exprs.push(Expr::Symbol(span));
need_operand = true;
}
}
e => {
if let Some(e) = e {
// We popped out the last expression to see if this left
// parenthesis started a function call... but if it didn't, we have
// to put the expression back.
exprs.push(e);
}
if !need_operand {
return Err(Error::Bad(
ts.pos,
format!("Unexpected {} in expression", ts.token),
));
}
op_spans.push(ExprOpSpan::new(ExprOp::LeftParen, ts.pos));
need_operand = true;
}
};
}
Token::RightParen => {
let mut found = false;
while let Some(eos) = op_spans.pop() {
eos.apply(&mut exprs)?;
if eos.op == ExprOp::LeftParen {
found = true;
break;
}
}
assert!(found, "Unbalanced parenthesis should have been handled above");
need_operand = false;
}
Token::Not => {
op_spans.push(ExprOpSpan::new(ExprOp::Not, ts.pos));
need_operand = true;
}
Token::Minus => {
let op;
if need_operand {
op = ExprOp::Negate;
} else {
op = ExprOp::Subtract;
while let Some(eos2) = op_spans.last() {
if eos2.op == ExprOp::LeftParen || eos2.op.priority() < op.priority() {
break;
}
let eos2 = op_spans.pop().unwrap();
eos2.apply(&mut exprs)?;
}
}
op_spans.push(ExprOpSpan::new(op, ts.pos));
need_operand = true;
}
Token::Equal
| Token::NotEqual
| Token::Less
| Token::LessEqual
| Token::Greater
| Token::GreaterEqual
| Token::Plus
| Token::Multiply
| Token::Divide
| Token::Modulo
| Token::Exponent
| Token::And
| Token::Or
| Token::Xor
| Token::ShiftLeft
| Token::ShiftRight => {
let op = ExprOp::from(ts.token);
while let Some(eos2) = op_spans.last() {
if eos2.op == ExprOp::LeftParen || eos2.op.priority() < op.priority() {
break;
}
let eos2 = op_spans.pop().unwrap();
eos2.apply(&mut exprs)?;
}
op_spans.push(ExprOpSpan::new(op, ts.pos));
need_operand = true;
}
Token::Bad(e) => return Err(Error::Bad(ts.pos, e)),
Token::Eof
| Token::Eol
| Token::As
| Token::Comma
| Token::Else
| Token::Semicolon
| Token::Then
| Token::To
| Token::Step => {
panic!("Field separators handled above")
}
Token::BooleanName
| Token::Case
| Token::Data
| Token::Do
| Token::Dim
| Token::DoubleName
| Token::Elseif
| Token::End
| Token::Error
| Token::Exit
| Token::For
| Token::Function
| Token::Gosub
| Token::Goto
| Token::If
| Token::Is
| Token::IntegerName
| Token::Label(_)
| Token::Loop
| Token::Next
| Token::On
| Token::Resume
| Token::Return
| Token::Select
| Token::Shared
| Token::Sub
| Token::TextName
| Token::Until
| Token::Wend
| Token::While => {
return Err(Error::Bad(ts.pos, "Unexpected keyword in expression".to_owned()));
}
};
}
while let Some(eos) = op_spans.pop() {
match eos.op {
ExprOp::LeftParen => {
return Err(Error::Bad(eos.pos, "Unbalanced parenthesis".to_owned()));
}
_ => eos.apply(&mut exprs)?,
}
}
if let Some(expr) = exprs.pop() { Ok(Some(expr)) } else { Ok(None) }
}
/// Wrapper over `parse_expr` that requires an expression to be present and returns an error
/// with `msg` otherwise.
fn parse_required_expr(&mut self, msg: &'static str) -> Result<Expr> {
let next_pos = self.lexer.peek()?.pos;
match self.parse_expr(None)? {
Some(expr) => Ok(expr),
None => Err(Error::Bad(next_pos, msg.to_owned())),
}
}
/// Parses a `GOSUB` statement.