@@ -96,25 +96,25 @@ fn eol() -> impl Parser<RcStringView, Output = Token> {
9696
9797fn crlf ( ) -> impl Parser < RcStringView , Output = Token > {
9898 char_parsers:: specific ( '\r' )
99- . append_char ( char_parsers:: specific ( '\n' ) )
99+ . concat ( char_parsers:: specific ( '\n' ) )
100100 . to_token ( TokenType :: Eol )
101101}
102102
103103fn greater_or_equal ( ) -> impl Parser < RcStringView , Output = Token > {
104104 char_parsers:: specific ( '>' )
105- . append_char ( char_parsers:: specific ( '=' ) )
105+ . concat ( char_parsers:: specific ( '=' ) )
106106 . to_token ( TokenType :: GreaterEquals )
107107}
108108
109109fn less_or_equal ( ) -> impl Parser < RcStringView , Output = Token > {
110110 char_parsers:: specific ( '<' )
111- . append_char ( char_parsers:: specific ( '=' ) )
111+ . concat ( char_parsers:: specific ( '=' ) )
112112 . to_token ( TokenType :: LessEquals )
113113}
114114
115115fn not_equal ( ) -> impl Parser < RcStringView , Output = Token > {
116116 char_parsers:: specific ( '<' )
117- . append_char ( char_parsers:: specific ( '>' ) )
117+ . concat ( char_parsers:: specific ( '>' ) )
118118 . to_token ( TokenType :: NotEquals )
119119}
120120
@@ -153,16 +153,20 @@ where
153153 F : Fn ( & char ) -> bool ,
154154{
155155 char_parsers:: filter ( predicate)
156- . concatenate ( )
156+ . many_to_str ( )
157157 . to_token ( token_type)
158158}
159159
160160fn specific ( token_type : TokenType , needle : char ) -> impl Parser < RcStringView , Output = Token > {
161- char_parsers:: specific ( needle) . to_str ( ) . to_token ( token_type)
161+ char_parsers:: specific ( needle)
162+ . one_to_str ( )
163+ . to_token ( token_type)
162164}
163165
164166fn unknown ( ) -> impl Parser < RcStringView , Output = Token > {
165- char_parsers:: any ( ) . to_str ( ) . to_token ( TokenType :: Unknown )
167+ char_parsers:: any ( )
168+ . one_to_str ( )
169+ . to_token ( TokenType :: Unknown )
166170}
167171
168172fn oct_digits ( ) -> impl Parser < RcStringView , Output = Token > {
@@ -182,19 +186,16 @@ where
182186 F : Fn ( & char ) -> bool ,
183187{
184188 char_parsers:: specific ( '&' )
185- . append_char ( char_parsers:: specific ( radix) )
189+ . concat ( char_parsers:: specific ( radix) )
186190 . and (
187- char_parsers:: specific ( '-' ) . to_option ( ) . and (
188- char_parsers:: filter ( predicate) . concatenate ( ) ,
189- |opt_minus, mut digits| {
190- match opt_minus {
191- Some ( minus) => {
192- // TODO prevent insert
193- digits. insert ( 0 , minus) ;
194- digits
195- }
196- _ => digits,
191+ char_parsers:: specific ( '-' ) . one_to_str ( ) . to_option ( ) . and (
192+ char_parsers:: filter ( predicate) . many_to_str ( ) ,
193+ |opt_minus, digits| match opt_minus {
194+ Some ( mut minus) => {
195+ minus. push_str ( & digits) ;
196+ minus
197197 }
198+ _ => digits,
198199 } ,
199200 ) ,
200201 |mut left, right| {
@@ -249,14 +250,14 @@ mod string_parsers {
249250 use super :: * ;
250251
251252 pub trait CharToStringParser < I > {
252- fn concatenate ( self ) -> impl Parser < I , Output = String > ;
253+ /// Reads as many chars possible from the underlying parser and returns them as a string.
254+ fn many_to_str ( self ) -> impl Parser < I , Output = String > ;
253255
254- fn to_str ( self ) -> impl Parser < I , Output = String > ;
256+ /// Reads one char possible from the underlying parser and converts it into a string.
257+ fn one_to_str ( self ) -> impl Parser < I , Output = String > ;
255258
256- fn append_char (
257- self ,
258- other : impl Parser < I , Output = char > ,
259- ) -> impl Parser < I , Output = String >
259+ /// A parser that reads two chars together and returns them as a string.
260+ fn concat ( self , other : impl Parser < I , Output = char > ) -> impl Parser < I , Output = String >
260261 where
261262 I : Clone ;
262263 }
@@ -266,21 +267,18 @@ mod string_parsers {
266267 I : Clone ,
267268 P : Parser < I , Output = char > ,
268269 {
269- fn concatenate ( self ) -> impl Parser < I , Output = String > {
270+ fn many_to_str ( self ) -> impl Parser < I , Output = String > {
270271 self . many ( String :: from, |mut s : String , c| {
271272 s. push ( c) ;
272273 s
273274 } )
274275 }
275276
276- fn to_str ( self ) -> impl Parser < I , Output = String > {
277+ fn one_to_str ( self ) -> impl Parser < I , Output = String > {
277278 self . map ( String :: from)
278279 }
279280
280- fn append_char (
281- self ,
282- other : impl Parser < I , Output = char > ,
283- ) -> impl Parser < I , Output = String > {
281+ fn concat ( self , other : impl Parser < I , Output = char > ) -> impl Parser < I , Output = String > {
284282 self . and ( other, |l, r| {
285283 let mut s = String :: from ( l) ;
286284 s. push ( r) ;
0 commit comments