interthread 3.1.0

Auto implementation of the Actor Model
Documentation
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
use super::{pad,preceded_by};
use crate::LINE_ENDING;

pub struct ActiveTextParser {
    pub open:      Option<String>,
    data:   Option<(usize,usize)>,
    index:                  usize,
}

impl  ActiveTextParser {

    pub fn new(index: usize) -> Self {
        Self {
            open: None,
            data: None,
            index,
        }
    }

    // Original 
    pub fn open_multy_line<'b>(&mut self,  s:&'b str ) -> Option<(&'b str,String)> {

        let mut ss = s;
        let len;
        if s.is_empty(){ return None; } else { len = s.len();}
        let mut work = String::new();

        if s.contains("/"){
            // Single Line
            if let Some(pos) = ss.find("//") {
                work = pad(2,&s[pos+2..]);
                ss = &s[..pos];
            }
            // Multy Line
            if let Some(pos) = ss.find("/*") {
                work = pad(2,&s[pos+2..]);
                ss = &s[..pos];
                self.open = Some("*/".into());
            }
        } 
        // Char (')
        if let Some(pos) = ss.find("'"){
            work = pad(1,&s[pos+1..]);
            ss = &s[..pos];
            self.open = Some("'".into());

        }
        // String Literal (")
        if let Some(mut pos) = ss.find("\"") {

            // if pos < index {
            let mut slf_len = 1;
            let mut open   = String::from("\"");
            
            // Byte String Literal (b")
            if let Some(new_pos) = preceded_by(ss,pos,"b") {
                pos = new_pos;
                slf_len = 2;
            }
            
            // Raw String Literal   (r#")
            else if let Some(new_pos) = preceded_by(ss,pos,"#") {
                let mut loc_new_pos = new_pos;
                let mut loc_slf_len = 2;
                let mut loc_open = String::from("\"#");
                loop {
                    if let Some(new_pos) = preceded_by(ss,loc_new_pos,"#"){
                        loc_new_pos = new_pos;
                        loc_slf_len += 1;
                        loc_open += "#";
                    } else { break; }
                }   
                if let Some(new_pos) = preceded_by(ss,loc_new_pos,"r") {
                    loc_new_pos = new_pos;
                    loc_slf_len += 1;

                    // RawByteStringLiteral (br#")
                    if let Some(new_pos) = preceded_by(ss,loc_new_pos,"b") { 
                        loc_new_pos = new_pos;
                        loc_slf_len += 1;
                    }
                    pos     = loc_new_pos;
                    slf_len = loc_slf_len;
                    open    = loc_open;
                }
            }
            work = pad(slf_len,&s[pos+slf_len..]);
            ss = &s[..pos];
            self.open = Some(open);
        }

        // possible lifetime catch block 
        if let Some(open) = &self.open {
            if open.eq("'"){
                let pos = ss.len();
                for (i,c) in (&s[(pos+1)..]).chars().enumerate(){
                    match c {
                        ' ' | ',' => {
                            self.open = Some("".into());
                            let (new_ss,new_w)  = s.split_at(pos +i);
                            ss = new_ss;
                            work = new_w.into();
                        },
                        '\'' => break,
                        _ => (),
                    }
                }
            }
        }

        if len == ss.len() { None } else { Some((ss, work)) }
    }

    pub fn close_multy_line<'b>( &mut self, s:&'b str, cap: &str ) -> Option<(String,&'b str)> {
        
        if let Some(mut pos) = s.find(cap){
            if cap == "\"" {
                loop {
                    if preceded_by(s,pos,r#"\"#).is_some(){
                        pos += 1;
                        if pos  < s.len(){
                            if let Some(new_pos) = s[pos..].find(cap){
                                pos += new_pos;
                            } else {return None;}
                        } else {return None;}
                    } else {break;}
                }
            }
            pos = pos + cap.len() ;
            self.open = None;
            return Some((pad(pos,""), &s[pos..]));
        }
        None
    }
    
    fn record(&mut self, (i,s): (usize,&str)) -> usize{
        let (len,total) = self.data.unwrap_or((0,0));
        self.data = Some((s.len(),total+len));

        (i*LINE_ENDING.len())+len+total
    } 

    pub fn parse ( &mut self,  (i,s):(usize, String)) -> (usize,String) {
        let index = self.record((i,&s));
        let mut work = s;
        let mut code = String::new();
        loop {
            if let Some(cap) = &self.open {
                if !cap.is_empty() { 

                    if let Some((c,w)) = 
                        self.close_multy_line(&work,&cap.clone()){
                        if w.is_empty(){
                            code += &c;
                            break;
                        } else {
                            code += &c;
                            work = w.into();
                        }   
                    } else { 
                        // if is not close than is w_space
                        code += &pad(work.len(),"");
                        break;
                    }
                } else { self.open = None; }
                
            } else {
                if let Some((c, w)) = self.open_multy_line(&work){

                    if self.open.is_none() {
                        code += c;
                        code += &pad(w.len(),"");
                        break;

                    } else {
                        code += c;
                        work  = w; 
                    }
                } else {
                    // if is not open than is code 
                    code += &work;
                    break;
                }
            }
        }
        (self.index + index,code)
    }
}



#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_lifetime() {

        let inp = 
r#"
fn example❤️<'a,'b,'c>(x: &'a char, y: &'b char, z: &'c char ) -> String {
    let mut s = String::new();
    s.push(*x);
    s.push(*y);
    s.push(*z);
    s
}
example❤️(&'a',&'\n',&'\u{2764}');
"#;
        let exp = 
r#"
fn example❤️<'a,'b,'c>(x: &'a char, y: &'b char, z: &'c char ) -> String {
    let mut s = String::new();
    s.push(*x);
    s.push(*y);
    s.push(*z);
    s
}
example❤️(&   ,&    ,&          );
"#;
        
        let mut atp = ActiveTextParser::new(0);
        let (_,code) = atp.parse((0,inp.to_string()));

        assert_eq!(exp,code);

    }
        

   #[test]
   fn explicit_chars_in_str(){
       let mut atp = ActiveTextParser::new(0);

       let s =r#"
       let a = '#'  ;
       let b = '\n' ;
       let c = '"'  ;
       let d = "foo";
       let g ="'\"'";
       let e =     1;
       "#;
       let r =r#"
       let a =      ;
       let b =      ;
       let c =      ;
       let d =      ;
       let g =      ;
       let e =     1;
       "#;
       
       let mut loc = Vec::new();
       for (index,line) in s.lines().enumerate(){

           let code_line = atp.parse((index,line.to_string()));
           loc.push(code_line);
       }
       let loc_new = 
       loc.into_iter().map(|x| x.1).collect::<Vec<_>>();
       let result = loc_new.join(LINE_ENDING);

       assert_eq!(&result,r)
   }


   #[test]
   fn  open_comment_test() { 
       let mut atp = ActiveTextParser::new(0);
       // let s = r###"br#"r##"r#"b""/*end"###;
                         
       if let Some((code,work)) = 
           atp.open_multy_line(r###"0//br#"r##"r#"b""/*end"###){
                   assert_eq!(code,"0");
                   assert_eq!(work,r###"  br#"r##"r#"b""/*end"###.to_string());
       }
       if let Some((code,work)) = 
           atp.open_multy_line(r###"1/*br#"r##"r#"b""end"###){
                   assert_eq!(code,"1");
                   assert_eq!(work,r###"  br#"r##"r#"b""end"###.to_string());
       }
       if let Some((code,work)) = 
           atp.open_multy_line(r###"2"br#"r##"r#"b"end"###){
                   assert_eq!(code,"2");
                   assert_eq!(work,r###" br#"r##"r#"b"end"###.to_string());
       }
       if let Some((code,work)) = 
           atp.open_multy_line(r###"3b"br#"r##"r#"end"###){
                   assert_eq!(code,"3");
                   assert_eq!(work,r###"  br#"r##"r#"end"###.to_string());
       }
       if let Some((code,work)) = 
           atp.open_multy_line(r###"4r#"br#"r##"end"###){
                   assert_eq!(code,"4");
                   assert_eq!(work,r###"   br#"r##"end"###.to_string());
       }
       if let Some((code,work)) = 
           atp.open_multy_line(r###"5r##"br#"end"###){
                   assert_eq!(code,"5");
                   assert_eq!(work,r###"    br#"end"###.to_string());
       }
       if let Some((code,work)) = 
           atp.open_multy_line(r###"6br#"end"###){
                   assert_eq!(code,"6");
                   assert_eq!(work,r###"    end"###.to_string());
       }
   }

   #[test]
   fn  close_cap_test() { 
       let mut atp = ActiveTextParser::new(0);
       let s = r###"123\"#45"*\"##end"###;
                         
       if let Some((code,work)) = 
           atp.close_multy_line(s, "\"#"){
           //               123\"#45"*\"##end
           assert_eq!(code,"      ".to_string());
           assert_eq!(work,r###"45"*\"##end"###);
       }
       if let Some((code,work)) = 
           atp.close_multy_line(s, "\""){
           //               123\"#45"*\"##end
           assert_eq!(code,"         ".to_string());
           assert_eq!(work,r###"*\"##end"###);
       }
       if let Some((code,work)) = 
           atp.close_multy_line(s, "*\\"){
           //               123\"#45"*\"##end
           assert_eq!(code,"           ".to_string());
           assert_eq!(work,r###""##end"###);
       }
       if let Some((code,work)) = 
           atp.close_multy_line(s, "\"##"){
           //               123\"#45"*\"##end
           assert_eq!(code,"              ".to_string());
           assert_eq!(work,r###"end"###);
       }
   }

   #[test]
   fn  parser_close_open() { 

       let mut atp = ActiveTextParser::new(0);
       let s = r#"eprintln!("12345");"#;

       let close = atp.close_multy_line(s, "\"");
       let open = atp.open_multy_line(s);

       let r_close = Some(("           ".to_string(),  "12345\");")) ;
       let r_open  = Some(("eprintln!("             , " 12345\");".to_string())) ;
       
       assert_eq!(close, r_close);
       assert_eq!(open ,  r_open);

       let ( cc,cw) = close.unwrap();
       let ( oc,ow) = open.unwrap();

       assert!( s.len() == (cc.len() + cw.len()));
       assert!( s.len() == (oc.len() + ow.len()));
   }
   

   // Old test for text parser 


  
   #[test]
   fn  parser_close_open_inline() { 
       let mut atp = ActiveTextParser::new(0);

       let s =r###"
       println!(   "12\"34🌍"  );
       println!(   "12🌍3\"4"  );
       println!(   "12🌍34\""  );
       println!(   "\"12🌍34"  );
       println!(  b"12🌍3\"4"  );
       println!(r##"1234\"🌍"##);
       println!(r#"🌍1234\""#  );
       println!(br#"\"1234🌍"# );
       println!("\"");
       println!("");
       "###;

       let r =r#"
       println!(                 );
       println!(                 );
       println!(                 );
       println!(                 );
       println!(                 );
       println!(                 );
       println!(                 );
       println!(                 );
       println!(    );
       println!(  );
       "#;

       let mut loc = Vec::new();
       for (index,line) in s.lines().enumerate(){
           loc.push(atp.parse((index,line.to_string())));
       }
       let loc_new = 
       loc.into_iter().map(|x| x.1).collect::<Vec<_>>();
       let result = loc_new.join(LINE_ENDING);

       assert_eq!(&result,r)
   }
   
}