11//! The reader. The part that reads plain text and parses it into Clojure structures, which are
2- //! themselves code.
2+ //! themselves code.
33//!
44//! Right now there's no sort of data kept track by the reader at any point, so there's no real
55//! reader data structure here -- this is just a plain module, a bag of functions. However,
66//! I believe this will change -- especially as, for instance, we define the idea of reader conditionals,
77//! or even reader macros, although the latter will likely be reserved for our interpreter here (but perhaps
88//! not; since this is about being a 'free-er' Clojure, especially since it can't compete with it in raw
9- //! power, neither speed or ecosystem, it might be worth it to leave in reader macros.
9+ //! power, neither speed or ecosystem, it might be worth it to leave in reader macros.
1010
1111use nom:: {
12- IResult ,
1312 branch:: alt,
14- error :: convert_error ,
13+ bytes :: complete :: { tag , take_while1 } ,
1514 character:: complete:: multispace0,
16- bytes:: complete:: { take_while1, tag} ,
17- take_until,
18- terminated,
19- map,
2015 combinator:: map_res,
21- sequence:: { preceded, terminated} } ;
16+ error:: convert_error,
17+ map,
18+ sequence:: { preceded, terminated} ,
19+ take_until, terminated, IResult ,
20+ } ;
2221
23- use crate :: value:: { Value , ToValue } ;
24- use crate :: persistent_list:: { ToPersistentList } ;
25- use crate :: persistent_vector:: { ToPersistentVector } ;
26- use crate :: persistent_list_map:: { PersistentListMap , ToPersistentListMap } ;
2722use crate :: maps:: MapEntry ;
23+ use crate :: persistent_list:: ToPersistentList ;
24+ use crate :: persistent_list_map:: { PersistentListMap , ToPersistentListMap } ;
25+ use crate :: persistent_vector:: ToPersistentVector ;
2826use crate :: symbol:: Symbol ;
29- use std:: {
30- iter:: FromIterator ,
31- rc:: Rc ,
32- } ;
27+ use crate :: value:: { ToValue , Value } ;
28+ use std:: { iter:: FromIterator , rc:: Rc } ;
3329
3430use std:: fs:: File ;
3531
@@ -120,24 +116,22 @@ pub fn identifier_parser(input: &str) -> IResult<&str, String> {
120116 named ! ( identifier_tail<& str , & str >, take_while!( is_identifier_char) ) ;
121117
122118 named ! ( identifier_ <& str , String >,
123- do_parse!(
124- head: identifier_head >>
125- rest_input: identifier_tail >>
126- ( cons_str( head, rest_input) )
127- )
128- ) ;
119+ do_parse!(
120+ head: identifier_head >>
121+ rest_input: identifier_tail >>
122+ ( cons_str( head, rest_input) )
123+ )
124+ ) ;
129125
130126 identifier_ ( input)
131127}
132128
133- /// Parses valid Clojure symbols, whose name is a valid identifier
129+ /// Parses valid Clojure symbols, whose name is a valid identifier
134130pub fn symbol_parser ( input : & str ) -> IResult < & str , Symbol > {
135- identifier_parser ( input) . map ( |( rest_input, name) | {
136- ( rest_input, Symbol :: intern ( & name) )
137- } )
131+ identifier_parser ( input) . map ( |( rest_input, name) | ( rest_input, Symbol :: intern ( & name) ) )
138132}
139133
140- // @TODO add negatives
134+ // @TODO add negatives
141135/// Parses valid integers
142136/// Example Successes: 1, 2, 4153, -12421
143137pub fn integer ( input : & str ) -> IResult < & str , i32 > {
@@ -151,13 +145,15 @@ pub fn integer(input: &str) -> IResult<&str, i32> {
151145// / reader will wrap.
152146/// Takes a parser, such as one that reads a &str and returns an
153147/// i32, and creates a new parser that instead returns a valid
154- /// ClojureRS Value instead
155- pub fn to_value_parser < I , O : ToValue > ( parser : impl Fn ( I ) -> IResult < I , O > ) -> impl Fn ( I ) -> IResult < I , Value > {
156- move |input : I | parser ( input) . map ( |( rest_input, thing) | ( rest_input, thing. to_value ( ) ) )
148+ /// ClojureRS Value instead
149+ pub fn to_value_parser < I , O : ToValue > (
150+ parser : impl Fn ( I ) -> IResult < I , O > ,
151+ ) -> impl Fn ( I ) -> IResult < I , Value > {
152+ move |input : I | parser ( input) . map ( |( rest_input, thing) | ( rest_input, thing. to_value ( ) ) )
157153}
158154
159155// @TODO make sure whitespace or 'nothing' is at the end, fail for
160- // float like numbers
156+ // float like numbers
161157/// Tries to parse &str into Value::I32
162158/// Expects:
163159/// Integers
@@ -177,19 +173,19 @@ pub fn try_read_i32(input: &str) -> IResult<&str, Value> {
177173/// cat-dog => Value::Symbol(Symbol { name: "cat-dog" })
178174/// +common-lisp-global+ => Value::Symbol(Symbol { name: "+common-lisp-global+" })
179175/// Example Failures:
180- /// 12cat, 'quoted, @at-is-for-references
176+ /// 12cat, 'quoted, @at-is-for-references
181177pub fn try_read_symbol ( input : & str ) -> IResult < & str , Value > {
182178 to_value_parser ( symbol_parser) ( input)
183179}
184180
185- // @TODO allow escaped strings
181+ // @TODO allow escaped strings
186182/// Tries to parse &str into Value::String
187183/// Example Successes:
188184/// "this is pretty straightforward" => Value::String("this is pretty straightforward")
189185pub fn try_read_string ( input : & str ) -> IResult < & str , Value > {
190186 named ! ( quotation<& str , & str >, preceded!( consume_clojure_whitespaces, tag!( "\" " ) ) ) ;
191187
192- let ( rest_input, _) = quotation ( input) ?;
188+ let ( rest_input, _) = quotation ( input) ?;
193189 named ! (
194190 string_parser<& str , String >,
195191 map!(
@@ -201,121 +197,124 @@ pub fn try_read_string(input: &str) -> IResult<&str, Value> {
201197 to_value_parser ( string_parser) ( input)
202198}
203199
204- // @TODO Perhaps generalize this, or even generalize it as a reader macro
200+ // @TODO Perhaps generalize this, or even generalize it as a reader macro
205201/// Tries to parse &str into Value::PersistentListMap, or some other Value::..Map
206202/// Example Successes:
207203/// {:a 1} => Value::PersistentListMap {PersistentListMap { MapEntry { :a, 1} .. ]})
208204pub fn try_read_map ( input : & str ) -> IResult < & str , Value > {
209205 named ! ( lbracep<& str , & str >, preceded!( consume_clojure_whitespaces, tag!( "{" ) ) ) ;
210206 named ! ( rbracep<& str , & str >, preceded!( consume_clojure_whitespaces, tag!( "}" ) ) ) ;
211- let ( map_inner_input, _) = lbracep ( input) ?;
212- let mut map_as_vec : Vec < MapEntry > = vec ! [ ] ;
207+ let ( map_inner_input, _) = lbracep ( input) ?;
208+ let mut map_as_vec: Vec < MapEntry > = vec ! [ ] ;
213209 let mut rest_input = map_inner_input;
214210 loop {
215- let right_brace = rbracep ( rest_input) ;
216- match right_brace {
217- Ok ( ( after_map_input, _) ) => {
218- break Ok ( ( after_map_input, map_as_vec. into_list_map ( ) . to_value ( ) ) ) ;
219- } ,
220- _ => {
221- let ( _rest_input, next_key) = try_read ( rest_input) ?;
222- let ( _rest_input, next_val) = try_read ( _rest_input) ?;
223- map_as_vec. push ( MapEntry { key : Rc :: new ( next_key) , val : Rc :: new ( next_val) } ) ;
224- rest_input = _rest_input;
225- }
226- }
211+ let right_brace = rbracep ( rest_input) ;
212+ match right_brace {
213+ Ok ( ( after_map_input, _) ) => {
214+ break Ok ( ( after_map_input, map_as_vec. into_list_map ( ) . to_value ( ) ) ) ;
215+ }
216+ _ => {
217+ let ( _rest_input, next_key) = try_read ( rest_input) ?;
218+ let ( _rest_input, next_val) = try_read ( _rest_input) ?;
219+ map_as_vec. push ( MapEntry {
220+ key : Rc :: new ( next_key) ,
221+ val : Rc :: new ( next_val) ,
222+ } ) ;
223+ rest_input = _rest_input;
224+ }
225+ }
227226 }
228227}
229228
230229// @TODO use nom functions in place of macro
231- /// Tries to parse &str into Value::PersistentVector
230+ /// Tries to parse &str into Value::PersistentVector
232231/// Example Successes:
233232/// [1 2 3] => Value::PersistentVector(PersistentVector { vals: [Rc(Value::I32(1) ... ]})
234233/// [1 2 [5 10 15] 3]
235234/// => Value::PersistentVector(PersistentVector { vals: [Rc(Value::I32(1) .. Rc(Value::PersistentVector..)]})
236235pub fn try_read_vector ( input : & str ) -> IResult < & str , Value > {
237236 named ! ( lbracketp<& str , & str >, preceded!( consume_clojure_whitespaces, tag!( "[" ) ) ) ;
238237 named ! ( rbracketp<& str , & str >, preceded!( consume_clojure_whitespaces, tag!( "]" ) ) ) ;
239- let ( vector_inner_input, _) = lbracketp ( input) ?;
238+ let ( vector_inner_input, _) = lbracketp ( input) ?;
240239 let mut vector_as_vec = vec ! [ ] ;
241- // What's left of our input as we read more of our PersistentVector
240+ // What's left of our input as we read more of our PersistentVector
242241 let mut rest_input = vector_inner_input;
243242 loop {
244- // Try parse end of vector
245- let right_paren = rbracketp ( rest_input) ;
246- match right_paren {
247- // If we succeeded, we can convert our vector of values into a PersistentVector and return our success
248- Ok ( ( after_vector_input, _) ) => {
249- break Ok ( ( after_vector_input, vector_as_vec. into_vector ( ) . to_value ( ) ) ) ;
250- } ,
251- // Otherwise, we need to keep reading until we get that closing bracket letting us know we're finished
252- _ => {
253- let next_form_parse = try_read ( rest_input) ;
254- match next_form_parse {
255- // Normal behavior; read our next element in the PersistentVector
256- Ok ( ( _rest_input, form) ) => {
257- vector_as_vec. push ( form. to_rc_value ( ) ) ;
258- rest_input = _rest_input;
259- } ,
260- // This parse failed, return overall read failure
261- _ => {
262- break next_form_parse;
263- }
264- }
265-
266- }
267- }
243+ // Try parse end of vector
244+ let right_paren = rbracketp ( rest_input) ;
245+ match right_paren {
246+ // If we succeeded, we can convert our vector of values into a PersistentVector and return our success
247+ Ok ( ( after_vector_input, _) ) => {
248+ break Ok ( ( after_vector_input, vector_as_vec. into_vector ( ) . to_value ( ) ) ) ;
249+ }
250+ // Otherwise, we need to keep reading until we get that closing bracket letting us know we're finished
251+ _ => {
252+ let next_form_parse = try_read ( rest_input) ;
253+ match next_form_parse {
254+ // Normal behavior; read our next element in the PersistentVector
255+ Ok ( ( _rest_input, form) ) => {
256+ vector_as_vec. push ( form. to_rc_value ( ) ) ;
257+ rest_input = _rest_input;
258+ }
259+ // This parse failed, return overall read failure
260+ _ => {
261+ break next_form_parse;
262+ }
263+ }
264+ }
265+ }
268266 }
269267}
270268
271269pub fn try_read_list ( input : & str ) -> IResult < & str , Value > {
272270 named ! ( lparenp<& str , & str >, preceded!( consume_clojure_whitespaces, tag!( "(" ) ) ) ;
273271 named ! ( rparenp<& str , & str >, preceded!( consume_clojure_whitespaces, tag!( ")" ) ) ) ;
274272
275- let ( list_inner_input, _) = lparenp ( input) ?;
273+ let ( list_inner_input, _) = lparenp ( input) ?;
276274 let mut list_as_vec = vec ! [ ] ;
277275 let mut rest_input = list_inner_input;
278276 loop {
279- let right_paren = rparenp ( rest_input) ;
280- match right_paren {
281- Ok ( ( after_list_input, _) ) => {
282- break Ok ( ( after_list_input, list_as_vec. into_list ( ) . to_value ( ) ) ) ;
283- } ,
284- _ => {
285- let next_form_parse = try_read ( rest_input) ;
286- match next_form_parse {
287- Ok ( ( _rest_input, form) ) => {
288- list_as_vec. push ( form. to_rc_value ( ) ) ;
289- rest_input = _rest_input;
290- } ,
291- // This parse failed, forward failure
292- _ => {
293- break next_form_parse;
294- }
295- }
296-
297- }
298- }
277+ let right_paren = rparenp ( rest_input) ;
278+ match right_paren {
279+ Ok ( ( after_list_input, _) ) => {
280+ break Ok ( ( after_list_input, list_as_vec. into_list ( ) . to_value ( ) ) ) ;
281+ }
282+ _ => {
283+ let next_form_parse = try_read ( rest_input) ;
284+ match next_form_parse {
285+ Ok ( ( _rest_input, form) ) => {
286+ list_as_vec. push ( form. to_rc_value ( ) ) ;
287+ rest_input = _rest_input;
288+ }
289+ // This parse failed, forward failure
290+ _ => {
291+ break next_form_parse;
292+ }
293+ }
294+ }
295+ }
299296 }
300297}
301298
302299pub fn try_read ( input : & str ) -> IResult < & str , Value > {
303- preceded ( consume_clojure_whitespaces, alt (
304- ( try_read_map,
305- try_read_string,
306- try_read_symbol,
307- try_read_i32,
308- try_read_list,
309- try_read_vector
310- ) ) ) ( input)
300+ preceded (
301+ consume_clojure_whitespaces,
302+ alt ( (
303+ try_read_map,
304+ try_read_string,
305+ try_read_symbol,
306+ try_read_i32,
307+ try_read_list,
308+ try_read_vector,
309+ ) ) ,
310+ ) ( input)
311311}
312312
313313pub fn debug_try_read ( input : & str ) -> IResult < & str , Value > {
314-
315314 let reading = try_read ( input) ;
316315 match & reading {
317- Ok ( ( _, value) ) => println ! ( "Reading: {}" , value) ,
318- _ => println ! ( "Reading: {:?}" , reading)
316+ Ok ( ( _, value) ) => println ! ( "Reading: {}" , value) ,
317+ _ => println ! ( "Reading: {:?}" , reading) ,
319318 } ;
320319 reading
321320}
0 commit comments