1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15+ use std:: cell:: RefCell ;
16+ use std:: rc:: Rc ;
17+
1518pub use nom:: branch:: alt;
1619pub use nom:: branch:: permutation;
1720pub use nom:: combinator:: consumed;
1821pub use nom:: combinator:: map;
1922pub use nom:: combinator:: not;
2023pub use nom:: combinator:: value;
21- use nom:: error:: Error as NomError ;
22- use nom:: error:: ErrorKind as NomErrorKind ;
2324pub use nom:: multi:: many1;
2425use nom:: sequence:: terminated;
26+ use nom:: Offset ;
2527use nom:: Parser ;
2628use nom_rule:: rule;
29+ use pratt:: PrattError ;
30+ use pratt:: PrattParser ;
31+ use pratt:: Precedence ;
2732
2833pub fn parser_fn < ' a , O , P > ( mut parser : P ) -> impl FnMut ( Input < ' a > ) -> IResult < ' a , O >
2934where P : nom:: Parser < Input < ' a > , Output = O , Error = Error < ' a > > {
@@ -44,78 +49,11 @@ use crate::parser::query::with_options;
4449use crate :: parser:: token:: * ;
4550use crate :: parser:: Error ;
4651use crate :: parser:: ErrorKind ;
47- pub use crate :: precedence:: Affix ;
48- pub use crate :: precedence:: Associativity ;
49- pub use crate :: precedence:: Precedence ;
5052use crate :: Range ;
5153use crate :: Span ;
5254
5355pub type IResult < ' a , Output > = nom:: IResult < Input < ' a > , Output , Error < ' a > > ;
5456
55- pub type ElementsInput < ' a , T > = & ' a [ WithSpan < ' a , T > ] ;
56- pub type ElementsError < ' a , T > = NomError < ElementsInput < ' a , T > > ;
57- pub type ElementsResult < ' a , T , O > = nom:: IResult < ElementsInput < ' a , T > , O , ElementsError < ' a , T > > ;
58-
59- pub fn match_prefix < ' a , T > (
60- affix_fn : impl Fn ( & T ) -> Affix + Copy ,
61- precedence : Precedence ,
62- ) -> impl FnMut ( ElementsInput < ' a , T > ) -> ElementsResult < ' a , T , WithSpan < ' a , T > >
63- where
64- T : Clone ,
65- {
66- match_affix (
67- affix_fn,
68- move |affix| matches ! ( affix, Affix :: Prefix ( p) if p == precedence) ,
69- )
70- }
71-
72- pub fn match_postfix < ' a , T > (
73- affix_fn : impl Fn ( & T ) -> Affix + Copy ,
74- precedence : Precedence ,
75- ) -> impl FnMut ( ElementsInput < ' a , T > ) -> ElementsResult < ' a , T , WithSpan < ' a , T > >
76- where
77- T : Clone ,
78- {
79- match_affix (
80- affix_fn,
81- move |affix| matches ! ( affix, Affix :: Postfix ( p) if p == precedence) ,
82- )
83- }
84-
85- pub fn match_binary < ' a , T > (
86- affix_fn : impl Fn ( & T ) -> Affix + Copy ,
87- precedence : Precedence ,
88- associativity : Associativity ,
89- ) -> impl FnMut ( ElementsInput < ' a , T > ) -> ElementsResult < ' a , T , WithSpan < ' a , T > >
90- where
91- T : Clone ,
92- {
93- match_affix (
94- affix_fn,
95- move |affix| matches ! ( affix, Affix :: Infix ( p, assoc) if p == precedence && assoc == associativity) ,
96- )
97- }
98-
99- pub fn match_nilfix < ' a , T > (
100- affix_fn : impl Fn ( & T ) -> Affix + Copy ,
101- ) -> impl FnMut ( ElementsInput < ' a , T > ) -> ElementsResult < ' a , T , WithSpan < ' a , T > >
102- where T : Clone {
103- match_affix ( affix_fn, |affix| matches ! ( affix, Affix :: Nilfix ) )
104- }
105-
106- fn match_affix < ' a , T > (
107- affix_fn : impl Fn ( & T ) -> Affix + Copy ,
108- predicate : impl Fn ( Affix ) -> bool + Copy ,
109- ) -> impl FnMut ( ElementsInput < ' a , T > ) -> ElementsResult < ' a , T , WithSpan < ' a , T > >
110- where
111- T : Clone ,
112- {
113- move |input| match input. split_first ( ) {
114- Some ( ( elem, rest) ) if predicate ( affix_fn ( & elem. elem ) ) => Ok ( ( rest, elem. clone ( ) ) ) ,
115- _ => Err ( nom:: Err :: Error ( NomError :: new ( input, NomErrorKind :: Tag ) ) ) ,
116- }
117- }
118-
11957pub fn match_text ( text : & ' static str ) -> impl FnMut ( Input ) -> IResult < & Token > {
12058 move |i| match i. tokens . first ( ) . filter ( |token| token. text ( ) == text) {
12159 Some ( token) => Ok ( ( i. slice ( 1 ..) , token) ) ,
@@ -667,6 +605,104 @@ pub fn transform_span(tokens: &[Token]) -> Span {
667605 } )
668606}
669607
608+ pub ( crate ) trait IterProvider < ' a > {
609+ type Item ;
610+ type Iter : Iterator < Item = Self :: Item > + ExactSizeIterator ;
611+
612+ fn create_iter ( self , span : Rc < RefCell < Option < Input < ' a > > > > ) -> Self :: Iter ;
613+ }
614+
615+ impl < ' a , T > IterProvider < ' a > for Vec < WithSpan < ' a , T > >
616+ where T : Clone
617+ {
618+ type Item = WithSpan < ' a , T > ;
619+ type Iter = ErrorSpan < ' a , T , std:: vec:: IntoIter < WithSpan < ' a , T > > > ;
620+
621+ fn create_iter ( self , span : Rc < RefCell < Option < Input < ' a > > > > ) -> Self :: Iter {
622+ ErrorSpan :: new ( self . into_iter ( ) , span)
623+ }
624+ }
625+
626+ pub ( crate ) struct ErrorSpan < ' a , T , I : Iterator < Item = WithSpan < ' a , T > > > {
627+ iter : I ,
628+ span : Rc < RefCell < Option < Input < ' a > > > > ,
629+ }
630+
631+ impl < ' a , T , I : Iterator < Item = WithSpan < ' a , T > > > ErrorSpan < ' a , T , I > {
632+ fn new ( iter : I , span : Rc < RefCell < Option < Input < ' a > > > > ) -> Self {
633+ Self { iter, span }
634+ }
635+ }
636+
637+ impl < ' a , T , I : Iterator < Item = WithSpan < ' a , T > > > Iterator for ErrorSpan < ' a , T , I > {
638+ type Item = WithSpan < ' a , T > ;
639+
640+ fn next ( & mut self ) -> Option < Self :: Item > {
641+ self . iter
642+ . next ( )
643+ . inspect ( |item| * self . span . borrow_mut ( ) = Some ( item. span ) )
644+ }
645+ }
646+
647+ impl < ' a , T , I : Iterator < Item = WithSpan < ' a , T > > > ExactSizeIterator for ErrorSpan < ' a , T , I > { }
648+
649+ pub fn run_pratt_parser < ' a , I , P , E , T > (
650+ mut parser : P ,
651+ parsers : T ,
652+ rest : Input < ' a > ,
653+ input : Input < ' a > ,
654+ ) -> IResult < ' a , P :: Output >
655+ where
656+ E : std:: fmt:: Debug ,
657+ P : PrattParser < I , Input = WithSpan < ' a , E > , Error = & ' static str > ,
658+ I : Iterator < Item = P :: Input > + ExactSizeIterator ,
659+ T : IterProvider < ' a , Item = P :: Input , Iter = I > ,
660+ {
661+ let span = Rc :: new ( RefCell :: new ( None ) ) ;
662+ let mut iter = parsers. create_iter ( span. clone ( ) ) . peekable ( ) ;
663+ let expr = parser
664+ . parse_input ( & mut iter, Precedence ( 0 ) )
665+ . map_err ( |err| {
666+ // Rollback parsing footprint on unused expr elements.
667+ input. backtrace . clear ( ) ;
668+
669+ let err_kind = match err {
670+ PrattError :: EmptyInput => ErrorKind :: Other ( "expecting an operand" ) ,
671+ PrattError :: UnexpectedNilfix ( i) => {
672+ * span. borrow_mut ( ) = Some ( i. span ) ;
673+ ErrorKind :: Other ( "unable to parse the element" )
674+ }
675+ PrattError :: UnexpectedPrefix ( i) => {
676+ * span. borrow_mut ( ) = Some ( i. span ) ;
677+ ErrorKind :: Other ( "unable to parse the prefix operator" )
678+ }
679+ PrattError :: UnexpectedInfix ( i) => {
680+ * span. borrow_mut ( ) = Some ( i. span ) ;
681+ ErrorKind :: Other ( "missing lhs or rhs for the binary operator" )
682+ }
683+ PrattError :: UnexpectedPostfix ( i) => {
684+ * span. borrow_mut ( ) = Some ( i. span ) ;
685+ ErrorKind :: Other ( "unable to parse the postfix operator" )
686+ }
687+ PrattError :: UserError ( err) => ErrorKind :: Other ( err) ,
688+ } ;
689+
690+ let span = span
691+ . take ( )
692+ // It's safe to slice one more token because input must contain EOI.
693+ . unwrap_or_else ( || rest. slice ( ..1 ) ) ;
694+
695+ nom:: Err :: Error ( Error :: from_error_kind ( span, err_kind) )
696+ } ) ?;
697+ if let Some ( elem) = iter. peek ( ) {
698+ // Rollback parsing footprint on unused expr elements.
699+ input. backtrace . clear ( ) ;
700+ Ok ( ( input. slice ( input. offset ( & elem. span ) ..) , expr) )
701+ } else {
702+ Ok ( ( rest, expr) )
703+ }
704+ }
705+
670706pub fn check_template_mode < ' a , O , F > ( mut parser : F ) -> impl FnMut ( Input < ' a > ) -> IResult < ' a , O >
671707where F : nom:: Parser < Input < ' a > , Output = O , Error = Error < ' a > > {
672708 move |input : Input | {
0 commit comments