-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathparser.rs
1671 lines (1570 loc) · 55.9 KB
/
parser.rs
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
use cfgrammar::Span;
use lazy_static::lazy_static;
use lrpar::LexerTypes;
use num_traits::AsPrimitive;
use regex::Regex;
use std::borrow::{Borrow as _, Cow};
use std::ops::Not;
use crate::{
lexer::{RegexOptions, Rule, DEFAULT_REGEX_OPTIONS},
LexBuildError, LexBuildResult, LexErrorKind,
};
type LexInternalBuildResult<T> = Result<T, LexBuildError>;
lazy_static! {
static ref RE_START_STATE_NAME: Regex = Regex::new(r"^[a-zA-Z][a-zA-Z0-9_.]*$").unwrap();
static ref RE_INCLUSIVE_START_STATE_DECLARATION: Regex =
Regex::new(r"^%[sS][a-zA-Z0-9]*$").unwrap();
static ref RE_EXCLUSIVE_START_STATE_DECLARATION: Regex =
Regex::new(r"^%[xX][a-zA-Z0-9]*$").unwrap();
// Documented in `Escape sequences` in lexcompatibility.m
static ref RE_LEX_ESC_LITERAL: Regex =
Regex::new(r"^(([xuU][[:xdigit:]])|[[:digit:]]|[afnrtv\\]|[pP]|[dDsSwW]|[AbBz])").unwrap();
// Vertical line separators.
static ref RE_LINE_SEP: Regex = Regex::new(r"[\p{Pattern_White_Space}&&[\p{Zl}\p{Zp}\n\r\v]]").unwrap();
static ref RE_LEADING_LINE_SEPS: Regex = Regex::new(r"^[\p{Pattern_White_Space}&&[\p{Zl}\p{Zp}\n\r\v]]*").unwrap();
// Horizontal space separators
static ref RE_SPACE_SEP: Regex = Regex::new(r"[\p{Pattern_White_Space}&&[\p{Zs}\t]]").unwrap();
static ref RE_LEADING_WS: Regex = Regex::new(r"^[\p{Pattern_White_Space}]*").unwrap();
static ref RE_WS: Regex = Regex::new(r"\p{Pattern_White_Space}").unwrap();
}
const INITIAL_START_STATE_NAME: &str = "INITIAL";
#[derive(Debug)]
#[doc(hidden)]
pub struct StartState {
/// Associated id of this start state - rules which have this start state
/// as a prerequisite, or which transition to this start state will store
/// this id in their appropriate fields.
pub(super) id: usize,
/// Name of this start state, as supplied in the declaration section, and
/// used in prerequisite and target start state sections of the rules.
pub(super) name: String,
pub(super) name_span: Span,
/// If false, a rule with _no_ start state will match when this state is active.
/// If true, only rules which have include this start state will match when
/// this state is active.
pub(super) exclusive: bool,
}
impl StartState {
pub fn new(id: usize, name: &str, exclusive: bool, name_span: Span) -> Self {
Self {
id,
name: name.to_string(),
name_span,
exclusive,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn name_span(&self) -> Span {
self.name_span
}
}
#[derive(Debug, Eq, PartialEq)]
#[doc(hidden)]
pub enum StartStateOperation {
ReplaceStack,
Push,
Pop,
}
pub(super) struct LexParser<LexerTypesT: LexerTypes>
where
usize: AsPrimitive<LexerTypesT::StorageT>,
{
src: String,
pub(super) rules: Vec<Rule<LexerTypesT::StorageT>>,
pub(super) start_states: Vec<StartState>,
pub(super) regex_options: RegexOptions,
}
fn add_duplicate_occurrence(
errs: &mut Vec<LexBuildError>,
kind: LexErrorKind,
orig_span: Span,
dup_span: Span,
) {
if !errs.iter_mut().any(|e| {
if e.kind == kind && e.spans[0] == orig_span {
e.spans.push(dup_span);
true
} else {
false
}
}) {
errs.push(LexBuildError {
kind,
spans: vec![orig_span, dup_span],
});
}
}
fn matches_whitespace(ch: char) -> bool {
let mut cbuf = [0; 4];
RE_WS.is_match(ch.encode_utf8(&mut cbuf))
}
impl<LexerTypesT: LexerTypes> LexParser<LexerTypesT>
where
usize: AsPrimitive<LexerTypesT::StorageT>,
LexerTypesT::StorageT: TryFrom<usize>,
{
pub(super) fn new(src: String) -> LexBuildResult<LexParser<LexerTypesT>> {
let mut p = LexParser {
src,
rules: Vec::new(),
start_states: vec![StartState::new(
0,
INITIAL_START_STATE_NAME,
false,
Span::new(0, 0),
)],
regex_options: DEFAULT_REGEX_OPTIONS,
};
p.parse()?;
Ok(p)
}
pub(super) fn new_with_regex_options(
src: String,
re_opt: RegexOptions,
) -> LexBuildResult<LexParser<LexerTypesT>> {
let mut p = LexParser {
src,
rules: Vec::new(),
start_states: vec![StartState::new(
0,
INITIAL_START_STATE_NAME,
false,
Span::new(0, 0),
)],
regex_options: re_opt,
};
p.parse()?;
Ok(p)
}
fn mk_error(&self, kind: LexErrorKind, off: usize) -> LexBuildError {
let span = Span::new(off, off);
LexBuildError {
kind,
spans: vec![span],
}
}
fn parse(&mut self) -> LexBuildResult<usize> {
let mut errs = Vec::new();
let mut i = match self.parse_declarations(0, &mut errs) {
Ok(i) => i,
Err(e) => {
errs.push(e);
return Err(errs);
}
};
// We don't currently support the subroutines part of a specification. One day we might...
i = match self.parse_rules(i, &mut errs) {
Ok(i) => i,
Err(e) => {
errs.push(e);
return Err(errs);
}
};
match self.lookahead_is("%%", i) {
Some(j) => {
let k = match self.parse_ws(j) {
Ok(k) => k,
Err(e) => {
errs.push(e);
return Err(errs);
}
};
if k == self.src.len() {
if errs.is_empty() {
Ok(i)
} else {
Err(errs)
}
} else {
errs.push(self.mk_error(LexErrorKind::RoutinesNotSupported, i));
Err(errs)
}
}
None => {
assert_eq!(i, self.src.len());
if errs.is_empty() {
Ok(i)
} else {
Err(errs)
}
}
}
}
fn parse_declarations(
&mut self,
mut i: usize,
errs: &mut Vec<LexBuildError>,
) -> LexInternalBuildResult<usize> {
loop {
i = self.parse_ws(i)?;
if i == self.src.len() {
break Err(self.mk_error(LexErrorKind::PrematureEnd, i));
}
if let Some(j) = self.lookahead_is("%%", i) {
break Ok(j);
}
i = self.parse_declaration(i, errs)?;
}
}
fn parse_declaration(
&mut self,
i: usize,
errs: &mut Vec<LexBuildError>,
) -> LexInternalBuildResult<usize> {
let line_len = RE_LINE_SEP
.find(&self.src[i..])
.map(|m| m.start())
.unwrap_or(self.src.len() - i);
let line = self.src[i..i + line_len].trim_end_matches(matches_whitespace);
let declaration_len = RE_WS.find(line).map(|m| m.start()).unwrap_or(line_len);
let declaration = self.src[i..i + declaration_len].trim_end_matches(matches_whitespace);
// Any line beginning with a '%' (percent sign) character and followed by an alphanumeric word
// beginning with either 's' or 'S' shall define a set of start conditions.
// Any line beginning with a '%' followed by an alphanumeric word beginning with either
// 'x' or 'X' shall define a set of exclusive start conditions.
// The rest of the line, after the first word, is considered to be one or more
// blank-character-separated names of start conditions.
if RE_INCLUSIVE_START_STATE_DECLARATION.is_match(declaration) {
self.declare_start_states(false, i, declaration_len, line_len, errs)
} else if RE_EXCLUSIVE_START_STATE_DECLARATION.is_match(declaration) {
self.declare_start_states(true, i, declaration_len, line_len, errs)
} else {
Err(self.mk_error(LexErrorKind::UnknownDeclaration, i))
}
}
fn declare_start_states(
&mut self,
exclusive: bool,
mut i: usize,
declaration_len: usize,
line_len: usize,
errs: &mut Vec<LexBuildError>,
) -> LexInternalBuildResult<usize> {
let line_end = i + line_len;
// Start state declarations are REQUIRED to have at least one start state name
let declaration_parameters =
self.src[i + declaration_len..line_end].trim_matches(matches_whitespace);
if declaration_parameters.is_empty() {
return Err(self.mk_error(LexErrorKind::UnknownDeclaration, i));
}
let start_states = RE_WS
.split(declaration_parameters)
.map(|name| {
let off = name.as_ptr() as usize - self.src.as_ptr() as usize;
i = off + name.len();
let span = Span::new(off, i);
(name, span)
})
.collect::<Vec<_>>();
for (name, name_span) in start_states {
let id = self.start_states.len();
if self.validate_start_state(name_span, name, errs)? {
let start_state = StartState::new(id, name, exclusive, name_span);
self.start_states.push(start_state);
}
}
self.parse_ws(i)
}
/// Validates a `StartState`
///
/// Return `Ok(true)` if the start state is valid.
///
/// A return value of `Ok(false)` indicates the state state is invalid. The site of error will be added to `errs`,
/// and may be coalesced with related errors. After which it is safe to continue parsing handling the error
/// in the future.
///
/// An `Err()` value returned will *not* be added to `errs`.
fn validate_start_state(
&self,
span: Span,
name: &str,
errs: &mut Vec<LexBuildError>,
) -> LexInternalBuildResult<bool> {
self.validate_start_state_name(span, name)?;
if let Some(state) = self.start_states.iter().find(|state| state.name == name) {
add_duplicate_occurrence(
errs,
LexErrorKind::DuplicateStartState,
state.name_span,
span,
);
Ok(false)
} else {
Ok(true)
}
}
fn validate_start_state_name(&self, span: Span, name: &str) -> LexInternalBuildResult<()> {
if !RE_START_STATE_NAME.is_match(name) {
return Err(self.mk_error(LexErrorKind::InvalidStartStateName, span.start()));
}
Ok(())
}
fn parse_rules(
&mut self,
mut i: usize,
errs: &mut Vec<LexBuildError>,
) -> LexInternalBuildResult<usize> {
loop {
// We should be at newline of the previous section separator '%%<here>\n upon entry,
// otherwise after iterating before the newline of the previous iterations rule or at eof.
i = self.parse_nl(i)?;
// According to posix lex:
//
// > Any such input (beginning with a <blank> or within "%{" and "%}" delimiter lines)
// > appearing at the beginning of the Rules section before any rules are specified
// > shall be written to lex.yy.c
//
// > The reason for the undefined condition associated with text beginning with a <blank> or
// > within "%{" and "%}" delimiter lines appearing in the Rules section is historical practice.
//
// > Both the BSD and System V lex copy the indented (or enclosed) input in the Rules section (except at the beginning)
// > to unreachable areas of the yylex() function (the code is written directly after a break statement).
// > In some cases, the System V lex generates an error message or a syntax error, depending on the form of indented input.
//
// Previously we allowed these, and trimmed leading spaces, parsing any rules after them. Currently we will emit an error.
let j = self.parse_ws(i)?;
if j != i {
let line_len = RE_LINE_SEP
.find(&self.src[j..])
.map(|m| m.start())
.unwrap_or(self.src.len() - j);
let err = LexBuildError {
kind: LexErrorKind::VerbatimNotSupported,
spans: vec![Span::new(i, i + line_len)],
};
errs.push(err);
i = j + line_len;
continue;
}
if i == self.src.len() {
break;
}
if self.lookahead_is("%%", i).is_some() {
break;
}
i = self.parse_rule(i, errs)?;
}
Ok(i)
}
fn parse_rule(
&mut self,
i: usize,
errs: &mut Vec<LexBuildError>,
) -> LexInternalBuildResult<usize> {
let mut cbuf = [0; 4];
let line_len = RE_LINE_SEP
.find(&self.src[i..])
.map(|m| m.start())
.unwrap_or(self.src.len() - i);
let line = self.src[i..i + line_len].trim_end_matches(matches_whitespace);
let rspace = match line.rfind(|ch: char| RE_SPACE_SEP.is_match(ch.encode_utf8(&mut cbuf))) {
Some(j) => j,
None => return Err(self.mk_error(LexErrorKind::MissingSpace, i)),
};
let name;
let target_state;
let orig_name = if line[rspace + 1..].starts_with('<') {
match line[rspace + 1..].find('>') {
Some(l) => {
// Get operation from state
let (state_name, operation) =
self.parse_start_state_ops(&line[rspace + 2..rspace + 1 + l]);
let state = self.get_start_state_by_name(i + rspace + 1, state_name)?;
target_state = Some((state.id, operation));
&line[rspace + 1 + l + 1..]
}
None => return Err(self.mk_error(LexErrorKind::InvalidStartState, rspace + i)),
}
} else {
target_state = None;
&line[rspace + 1..]
};
let name_span;
let dupe = if orig_name == ";" || orig_name == r#""""# || orig_name == "''" {
name = None;
let pos = i + rspace + 1;
name_span = Span::new(pos, pos);
false
} else {
if orig_name.len() <= 2
|| !((orig_name.starts_with('\'') && orig_name.ends_with('\''))
|| (orig_name.starts_with('\"') && orig_name.ends_with('"')))
{
return Err(self.mk_error(LexErrorKind::InvalidName, i + rspace + 1));
}
name = Some(orig_name[1..orig_name.len() - 1].to_string());
name_span = Span::new(i + rspace + 2, i + rspace + orig_name.len());
self.rules.iter().any(|r| {
let dupe = r
.name
.as_ref()
.map_or(false, |n| n == name.as_ref().unwrap());
if dupe {
add_duplicate_occurrence(
errs,
LexErrorKind::DuplicateName,
r.name_span,
name_span,
);
}
dupe
})
};
if !dupe {
let (start_states, re_str) =
self.parse_start_states(i, line[..rspace].trim_end_matches(matches_whitespace))?;
let rules_len = self.rules.len();
let tok_id = LexerTypesT::StorageT::try_from(rules_len)
.unwrap_or_else(|_| panic!("StorageT::try_from \
failed on {} (if StorageT is an unsigned integer type, this probably means that {} exceeds the type's maximum value)", rules_len, rules_len));
let rule = Rule::new(
Some(tok_id),
name,
name_span,
re_str.to_string(),
start_states,
target_state,
&self.regex_options,
)
.map_err(|_| self.mk_error(LexErrorKind::RegexError, i))?;
self.rules.push(rule);
}
Ok(i + line_len)
}
fn parse_start_state_ops<'a>(
&self,
start_state_str: &'a str,
) -> (&'a str, StartStateOperation) {
let (left_delta, operation) = match start_state_str.chars().next().unwrap_or_default() {
'+' => (1, StartStateOperation::Push),
'-' => (1, StartStateOperation::Pop),
_ => (0, StartStateOperation::ReplaceStack),
};
(&start_state_str[left_delta..], operation)
}
fn parse_start_states<'a>(
&self,
off: usize,
re_str: &'a str,
) -> LexInternalBuildResult<(Vec<usize>, std::borrow::Cow<'a, str>)> {
if !re_str.starts_with('<') {
/// This implements the 'Table: Escape Sequences in lex' from POSIX lex specification
///
/// Most of the escape handling is left to regex, except this part:
///
/// Escape: \c
///
/// Description:
/// A <backslash> character followed by any character not described in this table or in the table in
/// XBD File Format Notation ( '\\', '\a', '\b', '\f' , '\n', '\r', '\t', '\v' ).
///
/// Meaning: The character 'c', unchanged.
fn unescape(re: Cow<str>) -> Cow<str> {
// POSIX lex has two layers of escaping, there are escapes for the regular
// expressions themselves and the escapes which get handled by lex directly.
// We can find what the `regex` crate needs to be escaped with `is_meta_character`.
//
// We need to avoid sending Regex an escaped character which it does not consider
// a meta_character. As that would fail to compile. While ensuring we retain
// the escape sequences for the meta characters that it does require.
//
// '<' is interesting because when used at the beginning of a regex, intended to
// be part of the regex, it needs to be escaped, otherwise lex will interpret it
// as the beginning of a [`StartState`](StartState)
//
// If regex_syntax changes behavior here, it is highly likely that we should just
// remove this assertion, and send regex `\<`. Instead of unescaping `\<` into `<`.
// still it may be worthwhile to ensure that we notice such a change.
debug_assert!(regex_syntax::is_meta_character('<').not());
let re_str: &str = re.borrow();
let mut re_chars = re_str.char_indices();
// Look for an escape sequence which needs unescaping
let mut cursor = loop {
if let Some((i, c)) = re_chars.next() {
if c == '\\' {
// Look at the next character and whether it is something we need to unescape
if let Some((j, c2)) = re_chars.next() {
let s = &re_str[j..];
if !(regex_syntax::is_meta_character(c2)
|| RE_LEX_ESC_LITERAL.is_match(s))
{
break (Some((i, s, j, c2)));
}
}
}
} else {
break None;
}
};
if cursor.is_none() {
// There is nothing to unescape, return the original parameter
return re;
}
// At this point we have found something to unescape
let mut unescaped = String::new();
let mut last_pos = 0;
'outer: while let Some((i, s, j, c)) = cursor {
if regex_syntax::is_meta_character(c) || RE_LEX_ESC_LITERAL.is_match(s) {
// For both meta characters and literals we want to push the entire substring
// up to and including the c match back into the string still escaped.
unescaped.push_str(&re_str[last_pos..j + c.len_utf8()]);
last_pos = j + c.len_utf8();
} else {
// Given '\c' in the original string, push 'c' to the new string.
unescaped.push_str(&re_str[last_pos..i]);
last_pos = j + c.len_utf8();
unescaped.push_str(&re_str[j..last_pos]);
}
// Continue looking for the next escape sequence in the original unmodified string.
loop {
if let Some((step1_pos, step1)) = re_chars.next() {
if step1 == '\\' {
cursor = re_chars.next().map(|(step2_pos, step2)| {
(step1_pos, &re_str[step2_pos..], step2_pos, step2)
});
continue 'outer;
}
} else {
// Hit the end without finding another escape sequence. Copy over the trailing string.
unescaped.push_str(&re_str[last_pos..]);
break 'outer;
}
}
}
Cow::from(unescaped)
}
Ok((vec![], unescape(Cow::from(re_str))))
} else {
match re_str.find('>') {
None => Err(self.mk_error(LexErrorKind::InvalidStartState, off)),
Some(j) => {
let start_states = re_str[1..j]
.split(',')
.map(|s| s.trim_matches(matches_whitespace))
.map(|s| self.get_start_state_by_name(off, s))
.map(|s| s.map(|ss| ss.id))
.collect::<LexInternalBuildResult<Vec<usize>>>()?;
Ok((start_states, Cow::from(&re_str[j + 1..])))
}
}
}
}
fn get_start_state_by_name(
&self,
off: usize,
state: &str,
) -> LexInternalBuildResult<&StartState> {
self.start_states
.iter()
.find(|r| r.name == state)
.ok_or_else(|| self.mk_error(LexErrorKind::UnknownStartState, off))
}
fn parse_ws(&mut self, i: usize) -> LexInternalBuildResult<usize> {
Ok(RE_LEADING_WS
.find(&self.src[i..])
.map(|m| m.end() + i)
.unwrap_or(i))
}
fn parse_nl(&mut self, i: usize) -> LexInternalBuildResult<usize> {
Ok(RE_LEADING_LINE_SEPS
.find(&self.src[i..])
.map(|m| m.end() + i)
.unwrap_or(i))
}
fn lookahead_is(&self, s: &'static str, i: usize) -> Option<usize> {
if self.src[i..].starts_with(s) {
Some(i + s.len())
} else {
None
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::{
lexer::{LRNonStreamingLexerDef, LexerDef},
DefaultLexerTypes,
};
use cfgrammar::Spanned as _;
use std::collections::HashMap;
use std::fmt::Write as _;
macro_rules! incorrect_errs {
($src:ident, $errs:expr) => {{
for e in $errs {
let mut line_cache = ::cfgrammar::newlinecache::NewlineCache::new();
line_cache.feed(&$src);
if let Some((line, column)) = line_cache
.byte_to_line_num_and_col_num(&$src, e.spans().first().unwrap().start())
{
panic!(
"Incorrect error returned {} at line {line} column {column}",
e
)
} else {
panic!("{}", e)
}
}
}};
}
macro_rules! line_col {
($src:ident, $span: expr) => {{
let mut line_cache = ::cfgrammar::newlinecache::NewlineCache::new();
line_cache.feed(&$src);
line_cache
.byte_to_line_num_and_col_num(&$src, $span.start())
.unwrap()
}};
}
fn line_of_offset(s: &str, off: usize) -> usize {
s[..off].lines().count()
}
trait ErrorsHelper {
fn expect_error_at_line(self, src: &str, kind: LexErrorKind, line: usize);
fn expect_error_at_line_col(self, src: &str, kind: LexErrorKind, line: usize, col: usize);
fn expect_error_at_lines_cols(
self,
src: &str,
kind: LexErrorKind,
lines_cols: &mut dyn Iterator<Item = (usize, usize)>,
);
fn expect_multiple_errors(
self,
src: &str,
expected: &mut dyn Iterator<Item = (LexErrorKind, Vec<(usize, usize)>)>,
);
}
impl ErrorsHelper for Result<LRNonStreamingLexerDef<DefaultLexerTypes<u8>>, Vec<LexBuildError>> {
fn expect_error_at_line(self, src: &str, kind: LexErrorKind, line: usize) {
match self.as_ref().map_err(Vec::as_slice) {
Ok(_) => panic!("Parsed ok while expecting error"),
Err([e])
if e.kind == kind
&& line_of_offset(src, e.spans().first().unwrap().start()) == line
&& e.spans.len() == 1 => {}
Err(e) => incorrect_errs!(src, e),
}
}
fn expect_error_at_line_col(self, src: &str, kind: LexErrorKind, line: usize, col: usize) {
self.expect_error_at_lines_cols(src, kind, &mut std::iter::once((line, col)))
}
fn expect_error_at_lines_cols(
self,
src: &str,
kind: LexErrorKind,
lines_cols: &mut dyn Iterator<Item = (usize, usize)>,
) {
match self.as_ref().map_err(Vec::as_slice) {
Ok(_) => panic!("Parsed ok while expecting error"),
Err([e])
if e.kind == kind
&& line_col!(src, e.spans().first().unwrap())
== lines_cols.next().unwrap() =>
{
assert_eq!(
e.spans()
.iter()
.skip(1)
.map(|span| line_col!(src, span))
.collect::<Vec<(usize, usize)>>(),
lines_cols.collect::<Vec<(usize, usize)>>()
);
// Check that it is valid to slice the source with the spans.
for span in e.spans() {
let _ = &src[span.start()..span.end()];
}
}
Err(e) => incorrect_errs!(src, e),
}
}
fn expect_multiple_errors(
self,
src: &str,
expected: &mut dyn Iterator<Item = (LexErrorKind, Vec<(usize, usize)>)>,
) {
match self {
Ok(_) => panic!("Parsed ok while expecting error"),
Err(errs) => {
let linecol_errs = errs
.iter()
.map(|e| {
// Check that it is valid to slice the source with the spans.
for span in e.spans() {
let _ = &src[span.start()..span.end()];
}
(
e.kind.clone(),
e.spans()
.iter()
.map(|span| line_col!(src, span))
.collect::<Vec<_>>(),
)
})
.collect::<Vec<_>>();
assert_eq!(expected.collect::<Vec<_>>(), linecol_errs);
}
}
}
}
#[test]
fn test_nooptions() {
let src = "
%option nounput
"
.to_string();
assert!(LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).is_err());
}
#[test]
fn test_minimum() {
let src = "%%".to_string();
assert!(LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).is_ok());
}
#[test]
fn test_empty() {
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str("").expect_error_at_line_col(
"",
LexErrorKind::PrematureEnd,
1,
1,
);
}
#[test]
fn test_premature_end_multibyte() {
// Ends in LineSeparator multibyte whitespace.
let src = "%S X
".to_string();
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).expect_error_at_line_col(
&src,
LexErrorKind::PrematureEnd,
1,
6,
);
}
#[test]
fn test_rules() {
let src = "%%
[0-9]+ 'int'
[a-zA-Z]+ 'id'
\\+ '+'
"
.to_string();
let ast = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
let intrule = ast.get_rule_by_name("int").unwrap();
assert_eq!("int", intrule.name.as_ref().unwrap());
assert_eq!("[0-9]+", intrule.re_str);
let idrule = ast.get_rule_by_name("id").unwrap();
assert_eq!("id", idrule.name.as_ref().unwrap());
assert_eq!("[a-zA-Z]+", idrule.re_str);
let plusrule = ast.get_rule_by_name("+").unwrap();
assert_eq!("+", plusrule.name.as_ref().unwrap());
assert_eq!("\\+", plusrule.re_str);
}
#[test]
fn test_no_name() {
let src = "%%
[0-9]+ ;
"
.to_string();
let ast = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
let intrule = ast.get_rule(0).unwrap();
assert!(intrule.name.is_none());
assert_eq!("[0-9]+", intrule.re_str);
}
#[test]
fn test_broken_rule() {
let src = "%%
[0-9]
'int'"
.to_string();
assert!(LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).is_err());
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).expect_error_at_line_col(
&src,
LexErrorKind::MissingSpace,
2,
1,
)
}
#[test]
fn test_broken_rule2() {
let src = "%%
[0-9] "
.to_string();
assert!(LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).is_err());
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).expect_error_at_line_col(
&src,
LexErrorKind::MissingSpace,
2,
1,
)
}
#[test]
fn test_broken_rule3() {
let src = "%%
[0-9] int"
.to_string();
assert!(LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).is_err());
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).expect_error_at_line_col(
&src,
LexErrorKind::InvalidName,
2,
7,
)
}
#[test]
fn test_broken_rule4() {
let src = "%%
[0-9] 'int"
.to_string();
assert!(LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).is_err());
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).expect_error_at_line_col(
&src,
LexErrorKind::InvalidName,
2,
7,
)
}
#[test]
fn test_broken_rule_names_with_spaces() {
let srcs = [r#"a ""#, "a '", r#"a '""#, r#"a "'"#];
for line_two in srcs {
let src = format!("%%\n{line_two}");
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src)
.expect_error_at_line_col(&src, LexErrorKind::InvalidName, 2, 3);
}
let srcs = [
r#"a " ""#,
"a ' '",
r#"a ' "'"#,
r#"a " Name"#,
r#"a " Name""#,
"a ' Name",
"a ' Name'",
r#"a " '""#,
];
for line_two in srcs {
let src = format!("%%\n{line_two}");
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src)
.expect_error_at_line_col(&src, LexErrorKind::InvalidName, 2, 5);
}
}
#[test]
fn test_unusual_ok_rules() {
// Unusual cases of valid regexes and names, that make splitting them tricky.
let srcs = [
r#"[ "] 'a'"#,
r#"[ "] "a""#,
r#"[ "] ;"#,
r#"[ "] """#,
r#"[ "] ''"#,
r#"[ '] "a""#,
r#"[ '] ;"#,
r#"[ '] """#,
"[ '] ''",
"[ '] 'a'",
"[ '] ;",
];
for line_two in srcs {
let src = format!("%%\n{line_two}");
assert!(LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).is_ok());
}
}
#[test]
fn test_duplicate_rule() {
let src = "%%
[0-9] 'int'
[0-9] 'int'
[0-9] 'int'"
.to_string();
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).expect_error_at_lines_cols(
&src,
LexErrorKind::DuplicateName,
&mut [(2, 8), (3, 8), (4, 8)].into_iter(),
)
}
#[test]
fn multiple_duplicate_rules() {
let src = "%%
[0-9] 'int'
[A-Z] 'ALPHA'
[0-9] 'int'
[A-Z] 'ALPHA'
[0-9] 'int'
[A-Z] 'ALPHA'"
.to_string();
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).expect_multiple_errors(
&src,
&mut [
(LexErrorKind::DuplicateName, vec![(2, 8), (4, 8), (6, 8)]),
(LexErrorKind::DuplicateName, vec![(3, 8), (5, 8), (7, 8)]),
]
.into_iter(),
)
}
#[test]
fn start_state_declaration_containing_underscore() {
let src = "%start_state KNOWN
%%
<KNOWN>. 'known'"
.to_string();
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).expect_error_at_line_col(
&src,
LexErrorKind::UnknownDeclaration,
1,
1,
)
}
#[test]
fn start_state_declaration_missing() {
let src = "%s
%%
<KNOWN>. 'known'"
.to_string();
LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).expect_error_at_line_col(
&src,
LexErrorKind::UnknownDeclaration,
1,
1,
)
}
#[test]