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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
//! Adapted from [`nom`](https://github.com/Geal/nom) by removing the
//! `IResult::Incomplete` variant which:
//!
//! - we don't need,
//! - is an unintuitive footgun when working with non-streaming use cases, and
//! - more than doubles compilation time.
//!
//! ## Whitespace handling strategy
//!
//! As (sy)nom is a parser combinator library, the parsers provided here and
//! that you implement yourself are all made up of successively more primitive
//! parsers, eventually culminating in a small number of fundamental parsers
//! that are implemented in Rust. Among these are `punct!` and `keyword!`.
//!
//! All synom fundamental parsers (those not combined out of other parsers)
//! should be written to skip over leading whitespace in their input. This way,
//! as long as every parser eventually boils down to some combination of
//! fundamental parsers, we get correct whitespace handling at all levels for
//! free.
//!
//! For our use case, this strategy is a huge improvement in usability,
//! correctness, and compile time over nom's `ws!` strategy.
extern crate unicode_xid;
/// The result of a parser.
/// Define a function from a parser combination.
///
/// - **Syntax:** `named!(NAME -> TYPE, PARSER)` or `named!(pub NAME -> TYPE, PARSER)`
///
/// ```rust
/// # extern crate syn;
/// # #[macro_use] extern crate synom;
/// # use syn::Ty;
/// # use syn::parse::ty;
/// // One or more Rust types separated by commas.
/// named!(pub comma_separated_types -> Vec<Ty>,
/// separated_nonempty_list!(punct!(","), ty)
/// );
/// # fn main() {}
/// ```
};
=> ;
}
/// Invoke the given parser function with the passed in arguments.
///
/// - **Syntax:** `call!(FUNCTION, ARGS...)`
///
/// where the signature of the function is `fn(&str, ARGS...) -> IResult<&str, T>`
/// - **Output:** `T`, the result of invoking the function `FUNCTION`
///
/// ```rust
/// #[macro_use] extern crate synom;
///
/// use synom::IResult;
///
/// // Parses any string up to but not including the given character, returning
/// // the content up to the given character. The given character is required to
/// // be present in the input string.
/// fn skip_until(input: &str, ch: char) -> IResult<&str, &str> {
/// if let Some(pos) = input.find(ch) {
/// IResult::Done(&input[pos..], &input[..pos])
/// } else {
/// IResult::Error
/// }
/// }
///
/// // Parses any string surrounded by tilde characters '~'. Returns the content
/// // between the tilde characters.
/// named!(surrounded_by_tilde -> &str, delimited!(
/// punct!("~"),
/// call!(skip_until, '~'),
/// punct!("~")
/// ));
///
/// fn main() {
/// let input = "~ abc def ~";
///
/// let inner = surrounded_by_tilde(input).expect("surrounded by tilde");
///
/// println!("{:?}", inner);
/// }
/// ```
/// Transform the result of a parser by applying a function or closure.
///
/// - **Syntax:** `map!(THING, FN)`
/// - **Output:** the return type of function FN applied to THING
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::{Item, Ident};
/// use syn::parse::item;
///
/// fn get_item_ident(item: Item) -> Ident {
/// item.ident
/// }
///
/// // Parses an item and returns the name (identifier) of the item only.
/// named!(item_ident -> Ident,
/// map!(item, get_item_ident)
/// );
///
/// // Or equivalently:
/// named!(item_ident2 -> Ident,
/// map!(item, |i: Item| i.ident)
/// );
///
/// fn main() {
/// let input = "fn foo() {}";
///
/// let parsed = item_ident(input).expect("item");
///
/// assert_eq!(parsed, "foo");
/// }
/// ```
;
=> ;
}
/// Parses successfully if the given parser fails to parse. Does not consume any
/// of the input.
///
/// - **Syntax:** `not!(THING)`
/// - **Output:** `()`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
/// use synom::IResult;
///
/// // Parses a shebang line like `#!/bin/bash` and returns the part after `#!`.
/// // Note that a line starting with `#![` is an inner attribute, not a
/// // shebang.
/// named!(shebang -> &str, preceded!(
/// tuple!(tag!("#!"), not!(tag!("["))),
/// take_until!("\n")
/// ));
///
/// fn main() {
/// let bin_bash = "#!/bin/bash\n";
/// let parsed = shebang(bin_bash).expect("shebang");
/// assert_eq!(parsed, "/bin/bash");
///
/// let inner_attr = "#![feature(specialization)]\n";
/// let err = shebang(inner_attr);
/// assert_eq!(err, IResult::Error);
/// }
/// ```
;
}
/// Conditionally execute the given parser.
///
/// If you are familiar with nom, this is nom's `cond_with_error` parser.
///
/// - **Syntax:** `cond!(CONDITION, THING)`
/// - **Output:** `Some(THING)` if the condition is true, else `None`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::parse::boolean;
///
/// // Parses a tuple of booleans like `(true, false, false)`, possibly with a
/// // dotdot indicating omitted values like `(true, true, .., true)`. Returns
/// // separate vectors for the booleans before and after the dotdot. The second
/// // vector is None if there was no dotdot.
/// named!(bools_with_dotdot -> (Vec<bool>, Option<Vec<bool>>), do_parse!(
/// punct!("(") >>
/// before: separated_list!(punct!(","), boolean) >>
/// after: option!(do_parse!(
/// // Only allow comma if there are elements before dotdot, i.e. cannot
/// // be `(, .., true)`.
/// cond!(!before.is_empty(), punct!(",")) >>
/// punct!("..") >>
/// after: many0!(preceded!(punct!(","), boolean)) >>
/// // Only allow trailing comma if there are elements after dotdot,
/// // i.e. cannot be `(true, .., )`.
/// cond!(!after.is_empty(), option!(punct!(","))) >>
/// (after)
/// )) >>
/// // Allow trailing comma if there is no dotdot but there are elements.
/// cond!(!before.is_empty() && after.is_none(), option!(punct!(","))) >>
/// punct!(")") >>
/// (before, after)
/// ));
///
/// fn main() {
/// let input = "(true, false, false)";
/// let parsed = bools_with_dotdot(input).expect("bools with dotdot");
/// assert_eq!(parsed, (vec![true, false, false], None));
///
/// let input = "(true, true, .., true)";
/// let parsed = bools_with_dotdot(input).expect("bools with dotdot");
/// assert_eq!(parsed, (vec![true, true], Some(vec![true])));
///
/// let input = "(.., true)";
/// let parsed = bools_with_dotdot(input).expect("bools with dotdot");
/// assert_eq!(parsed, (vec![], Some(vec![true])));
///
/// let input = "(true, true, ..)";
/// let parsed = bools_with_dotdot(input).expect("bools with dotdot");
/// assert_eq!(parsed, (vec![true, true], Some(vec![])));
///
/// let input = "(..)";
/// let parsed = bools_with_dotdot(input).expect("bools with dotdot");
/// assert_eq!(parsed, (vec![], Some(vec![])));
/// }
/// ```
else
};
=> ;
}
/// Fail to parse if condition is false, otherwise parse the given parser.
///
/// This is typically used inside of `option!` or `alt!`.
///
/// - **Syntax:** `cond_reduce!(CONDITION, THING)`
/// - **Output:** `THING`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::parse::boolean;
///
/// #[derive(Debug, PartialEq)]
/// struct VariadicBools {
/// data: Vec<bool>,
/// variadic: bool,
/// }
///
/// // Parse one or more comma-separated booleans, possibly ending in "..." to
/// // indicate there may be more.
/// named!(variadic_bools -> VariadicBools, do_parse!(
/// data: separated_nonempty_list!(punct!(","), boolean) >>
/// trailing_comma: option!(punct!(",")) >>
/// // Only allow "..." if there is a comma after the last boolean. Using
/// // `cond_reduce!` is more convenient here than using `cond!`. The
/// // alternatives are:
/// //
/// // - `cond!(c, option!(p))` or `option!(cond!(c, p))`
/// // Gives `Some(Some("..."))` for variadic and `Some(None)` or `None`
/// // which both mean not variadic.
/// // - `cond_reduce!(c, option!(p))`
/// // Incorrect; would fail to parse if there is no trailing comma.
/// // - `option!(cond_reduce!(c, p))`
/// // Gives `Some("...")` for variadic and `None` otherwise. Perfect!
/// variadic: option!(cond_reduce!(trailing_comma.is_some(), punct!("..."))) >>
/// (VariadicBools {
/// data: data,
/// variadic: variadic.is_some(),
/// })
/// ));
///
/// fn main() {
/// let input = "true, true";
/// let parsed = variadic_bools(input).expect("variadic bools");
/// assert_eq!(parsed, VariadicBools {
/// data: vec![true, true],
/// variadic: false,
/// });
///
/// let input = "true, ...";
/// let parsed = variadic_bools(input).expect("variadic bools");
/// assert_eq!(parsed, VariadicBools {
/// data: vec![true],
/// variadic: true,
/// });
/// }
/// ```
else
};
=> ;
}
/// Parse two things, returning the value of the second.
///
/// - **Syntax:** `preceded!(BEFORE, THING)`
/// - **Output:** `THING`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::Expr;
/// use syn::parse::expr;
///
/// // An expression preceded by ##.
/// named!(pound_pound_expr -> Expr,
/// preceded!(punct!("##"), expr)
/// );
///
/// fn main() {
/// let input = "## 1 + 1";
///
/// let parsed = pound_pound_expr(input).expect("pound pound expr");
///
/// println!("{:?}", parsed);
/// }
/// ```
};
=> ;
=> ;
=> ;
}
/// Parse two things, returning the value of the first.
///
/// - **Syntax:** `terminated!(THING, AFTER)`
/// - **Output:** `THING`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::Expr;
/// use syn::parse::expr;
///
/// // An expression terminated by ##.
/// named!(expr_pound_pound -> Expr,
/// terminated!(expr, punct!("##"))
/// );
///
/// fn main() {
/// let input = "1 + 1 ##";
///
/// let parsed = expr_pound_pound(input).expect("expr pound pound");
///
/// println!("{:?}", parsed);
/// }
/// ```
};
=> ;
=> ;
=> ;
}
/// Parse zero or more values using the given parser.
///
/// - **Syntax:** `many0!(THING)`
/// - **Output:** `Vec<THING>`
///
/// You may also be looking for:
///
/// - `separated_list!` - zero or more values with separator
/// - `separated_nonempty_list!` - one or more values
/// - `terminated_list!` - zero or more, allows trailing separator
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::Item;
/// use syn::parse::item;
///
/// named!(items -> Vec<Item>, many0!(item));
///
/// fn main() {
/// let input = "
/// fn a() {}
/// fn b() {}
/// ";
///
/// let parsed = items(input).expect("items");
///
/// assert_eq!(parsed.len(), 2);
/// println!("{:?}", parsed);
/// }
/// ```
ret
}};
=> ;
}
// Improve compile time by compiling this loop only once per type it is used
// with.
//
// Not public API.
/// Parse a value without consuming it from the input data.
///
/// - **Syntax:** `peek!(THING)`
/// - **Output:** `THING`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::Expr;
/// use syn::parse::{ident, expr};
/// use synom::IResult;
///
/// // Parse an expression that begins with an identifier.
/// named!(ident_expr -> Expr,
/// preceded!(peek!(ident), expr)
/// );
///
/// fn main() {
/// // begins with an identifier
/// let input = "banana + 1";
/// let parsed = ident_expr(input).expect("ident");
/// println!("{:?}", parsed);
///
/// // does not begin with an identifier
/// let input = "1 + banana";
/// let err = ident_expr(input);
/// assert_eq!(err, IResult::Error);
/// }
/// ```
;
=> ;
}
/// Parse the part of the input up to but not including the given string. Fail
/// to parse if the given string is not present in the input.
///
/// - **Syntax:** `take_until!("...")`
/// - **Output:** `&str`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
/// use synom::IResult;
///
/// // Parse a single line doc comment: /// ...
/// named!(single_line_doc -> &str,
/// preceded!(punct!("///"), take_until!("\n"))
/// );
///
/// fn main() {
/// let comment = "/// comment\n";
/// let parsed = single_line_doc(comment).expect("single line doc comment");
/// assert_eq!(parsed, " comment");
/// }
/// ```
/// Parse the given string from exactly the current position in the input. You
/// almost always want `punct!` or `keyword!` instead of this.
///
/// The `tag!` parser is equivalent to `punct!` but does not ignore leading
/// whitespace. Both `punct!` and `keyword!` skip over leading whitespace. See
/// an explanation of synom's whitespace handling strategy in the top-level
/// crate documentation.
///
/// - **Syntax:** `tag!("...")`
/// - **Output:** `"..."`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::StrLit;
/// use syn::parse::string;
/// use synom::IResult;
///
/// // Parse a proposed syntax for an owned string literal: "abc"s
/// named!(owned_string -> String,
/// map!(
/// terminated!(string, tag!("s")),
/// |lit: StrLit| lit.value
/// )
/// );
///
/// fn main() {
/// let input = r#" "abc"s "#;
/// let parsed = owned_string(input).expect("owned string literal");
/// println!("{:?}", parsed);
///
/// let input = r#" "abc" s "#;
/// let err = owned_string(input);
/// assert_eq!(err, IResult::Error);
/// }
/// ```
/// Pattern-match the result of a parser to select which other parser to run.
///
/// - **Syntax:** `switch!(TARGET, PAT1 => THEN1 | PAT2 => THEN2 | ...)`
/// - **Output:** `T`, the return type of `THEN1` and `THEN2` and ...
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::{Ident, Ty};
/// use syn::parse::{ident, ty};
///
/// #[derive(Debug)]
/// enum UnitType {
/// Struct {
/// name: Ident,
/// },
/// Enum {
/// name: Ident,
/// variant: Ident,
/// },
/// }
///
/// // Parse a unit struct or enum: either `struct S;` or `enum E { V }`.
/// named!(unit_type -> UnitType, do_parse!(
/// which: alt!(keyword!("struct") | keyword!("enum")) >>
/// id: ident >>
/// item: switch!(value!(which),
/// "struct" => map!(
/// punct!(";"),
/// move |_| UnitType::Struct {
/// name: id,
/// }
/// )
/// |
/// "enum" => map!(
/// delimited!(punct!("{"), ident, punct!("}")),
/// move |variant| UnitType::Enum {
/// name: id,
/// variant: variant,
/// }
/// )
/// ) >>
/// (item)
/// ));
///
/// fn main() {
/// let input = "struct S;";
/// let parsed = unit_type(input).expect("unit struct or enum");
/// println!("{:?}", parsed);
///
/// let input = "enum E { V }";
/// let parsed = unit_type(input).expect("unit struct or enum");
/// println!("{:?}", parsed);
/// }
/// ```
;
}
/// Produce the given value without parsing anything. Useful as an argument to
/// `switch!`.
///
/// - **Syntax:** `value!(VALUE)`
/// - **Output:** `VALUE`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::{Ident, Ty};
/// use syn::parse::{ident, ty};
///
/// #[derive(Debug)]
/// enum UnitType {
/// Struct {
/// name: Ident,
/// },
/// Enum {
/// name: Ident,
/// variant: Ident,
/// },
/// }
///
/// // Parse a unit struct or enum: either `struct S;` or `enum E { V }`.
/// named!(unit_type -> UnitType, do_parse!(
/// which: alt!(keyword!("struct") | keyword!("enum")) >>
/// id: ident >>
/// item: switch!(value!(which),
/// "struct" => map!(
/// punct!(";"),
/// move |_| UnitType::Struct {
/// name: id,
/// }
/// )
/// |
/// "enum" => map!(
/// delimited!(punct!("{"), ident, punct!("}")),
/// move |variant| UnitType::Enum {
/// name: id,
/// variant: variant,
/// }
/// )
/// ) >>
/// (item)
/// ));
///
/// fn main() {
/// let input = "struct S;";
/// let parsed = unit_type(input).expect("unit struct or enum");
/// println!("{:?}", parsed);
///
/// let input = "enum E { V }";
/// let parsed = unit_type(input).expect("unit struct or enum");
/// println!("{:?}", parsed);
/// }
/// ```
/// Value surrounded by a pair of delimiters.
///
/// - **Syntax:** `delimited!(OPEN, THING, CLOSE)`
/// - **Output:** `THING`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::Expr;
/// use syn::parse::expr;
///
/// // An expression surrounded by [[ ... ]].
/// named!(double_bracket_expr -> Expr,
/// delimited!(punct!("[["), expr, punct!("]]"))
/// );
///
/// fn main() {
/// let input = "[[ 1 + 1 ]]";
///
/// let parsed = double_bracket_expr(input).expect("double bracket expr");
///
/// println!("{:?}", parsed);
/// }
/// ```
};
=> ;
}
/// One or more values separated by some separator. Does not allow a trailing
/// separator.
///
/// - **Syntax:** `separated_nonempty_list!(SEPARATOR, THING)`
/// - **Output:** `Vec<THING>`
///
/// You may also be looking for:
///
/// - `separated_list!` - one or more values
/// - `terminated_list!` - zero or more, allows trailing separator
/// - `many0!` - zero or more, no separator
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::Ty;
/// use syn::parse::ty;
///
/// // One or more Rust types separated by commas.
/// named!(comma_separated_types -> Vec<Ty>,
/// separated_nonempty_list!(punct!(","), ty)
/// );
///
/// fn main() {
/// let input = "&str, Map<K, V>, String";
///
/// let parsed = comma_separated_types(input).expect("comma-separated types");
///
/// assert_eq!(parsed.len(), 3);
/// println!("{:?}", parsed);
/// }
/// ```
};
=> ;
=> ;
=> ;
}
/// Run a series of parsers and produce all of the results in a tuple.
///
/// - **Syntax:** `tuple!(A, B, C, ...)`
/// - **Output:** `(A, B, C, ...)`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::Ty;
/// use syn::parse::ty;
///
/// named!(two_types -> (Ty, Ty), tuple!(ty, ty));
///
/// fn main() {
/// let input = "&str Map<K, V>";
///
/// let parsed = two_types(input).expect("two types");
///
/// println!("{:?}", parsed);
/// }
/// ```
/// Internal parser, do not use directly.
;
=> ;
=> ;
=> ;
=> ;
=> ;
=> ;
}
/// Run a series of parsers, returning the result of the first one which
/// succeeds.
///
/// Optionally allows for the result to be transformed.
///
/// - **Syntax:** `alt!(THING1 | THING2 => { FUNC } | ...)`
/// - **Output:** `T`, the return type of `THING1` and `FUNC(THING2)` and ...
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::Ident;
/// use syn::parse::ident;
///
/// named!(ident_or_bang -> Ident,
/// alt!(
/// ident
/// |
/// punct!("!") => { |_| "BANG".into() }
/// )
/// );
///
/// fn main() {
/// let input = "foo";
/// let parsed = ident_or_bang(input).expect("identifier or `!`");
/// assert_eq!(parsed, "foo");
///
/// let input = "!";
/// let parsed = ident_or_bang(input).expect("identifier or `!`");
/// assert_eq!(parsed, "BANG");
/// }
/// ```
;
=> ;
=> ;
=> ;
=> ;
=> ;
=> ;
}
/// Run a series of parsers, one after another, optionally assigning the results
/// a name. Fail if any of the parsers fails.
///
/// Produces the result of evaluating the final expression in parentheses with
/// all of the previously named results bound.
///
/// - **Syntax:** `do_parse!(name: THING1 >> THING2 >> (RESULT))`
/// - **Output:** `RESULT`
///
/// ```rust
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
/// use syn::{Ident, TokenTree};
/// use syn::parse::{ident, tt};
///
/// // Parse a macro invocation like `stringify!($args)`.
/// named!(simple_mac -> (Ident, TokenTree), do_parse!(
/// name: ident >>
/// punct!("!") >>
/// body: tt >>
/// (name, body)
/// ));
///
/// fn main() {
/// let input = "stringify!($args)";
/// let (name, body) = simple_mac(input).expect("macro invocation");
/// println!("{:?}", name);
/// println!("{:?}", body);
/// }
/// ```
;
=> ;
=> ;
=> ;
=> ;
=> ;
=> ;
}