changeset 45:8ffaa9bacff3

Finish repetition parsing
author Lewin Bormann <lbo@spheniscida.de>
date Wed, 17 Jul 2019 10:00:58 +0200
parents 11af4959e69a
children cfc2c894ef2c
files src/matching.rs src/parse.rs src/repr.rs
diffstat 3 files changed, 24 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/matching.rs	Wed Jul 17 09:35:29 2019 +0200
+++ b/src/matching.rs	Wed Jul 17 10:00:58 2019 +0200
@@ -74,7 +74,6 @@
     while i < len {
         ms.reset(i);
         let m = start_match(ms.clone());
-        println!("{:?}", m);
         match m {
             // If the match fails, we skip as many characters as were matched at first.
             (false, skip, _) => i = skip + 1,
@@ -183,7 +182,7 @@
     use state::*;
 
     fn simple_re0() -> Pattern {
-        (parse::parse("a(b+|bb|bbb|c+)$c$").unwrap())
+        (parse::parse("a(b+|bb|bbb|c+){1,3}$c$").unwrap())
     }
 
     // /a(b|c)(xx)?$/
@@ -191,12 +190,12 @@
         Pattern::Concat(vec![
             Pattern::CharRange('a', 'a'),
             Pattern::Submatch(Box::new(
-                (Pattern::Alternate(vec![(Pattern::Char('b')), (Pattern::Char('c'))])),
+                Pattern::Alternate(vec![(Pattern::Char('b')), (Pattern::Char('c'))]),
             )),
             Pattern::Submatch(Box::new(
-                (Pattern::Repeated(Box::new(Repetition::ZeroOrOnce(Pattern::Str(
+                Pattern::Repeated(Box::new(Repetition::ZeroOrOnce(Pattern::Str(
                     "xx".to_string(),
-                ))))),
+                )))),
             )),
             Pattern::Anchor(AnchorLocation::End),
         ])
--- a/src/parse.rs	Wed Jul 17 09:35:29 2019 +0200
+++ b/src/parse.rs	Wed Jul 17 10:00:58 2019 +0200
@@ -130,7 +130,6 @@
             break;
         }
 
-        println!("{:?} {}", &s[..], s.pos);
         match s[0] {
             c if c.is_alphanumeric() => {
                 stack.push(Pattern::Char(c));
@@ -207,7 +206,7 @@
                     Some((rep, newst)) => {
                         if let Some(p) = stack.pop() {
                             let rep = parse_specific_repetition(rep, p)?;
-
+                            stack.push(Pattern::Repeated(Box::new(rep)));
                             s = newst;
                         } else {
                             return s.err("repetition {} without pattern to repeat", 0);
@@ -292,12 +291,19 @@
             ));
         }
     } else if nparts == 2 {
+        fn errtostr(r: Result<u32, std::num::ParseIntError>) -> Result<u32, String> {
+            match r {
+                Ok(u) => Ok(u),
+                Err(e) => Err(format!("{}", e)),
+            }
+        }
         // {2,3}
-        let min = u32::from_str(&String::from_iter(parts[0].unwrap().iter()));
-        let max = u32::from_str(&String::from_iter(parts[1].unwrap().iter()));
+        let min = errtostr(u32::from_str(&String::from_iter(parts[0].unwrap().iter())))?;
+        let max = errtostr(u32::from_str(&String::from_iter(parts[1].unwrap().iter())))?;
+        return Ok(Repetition::Specific(p, min, Some(max)))
     }
 
-    Err(String::from("abc"))
+    Err(format!("invalid repetition pattern {:?}", &rep[..]))
 }
 
 const ROUND_PARENS: (char, char) = ('(', ')');
@@ -451,6 +457,10 @@
     }
 
     #[test]
+    fn test_parse_repetition_manual() {
+        println!("digraph st {{ {} }}", dot(start_compile(&parse("[abc]{1,5}").unwrap())));
+    }
+    #[test]
     fn test_parse_manual() {
         let rep = parse("a|[bed]|(c|d|e)|f").unwrap();
         println!("{:?}", rep.clone());
--- a/src/repr.rs	Wed Jul 17 09:35:29 2019 +0200
+++ b/src/repr.rs	Wed Jul 17 10:00:58 2019 +0200
@@ -229,10 +229,10 @@
                 ])),
             )))),
             Pattern::Submatch(Box::new(
-                (Pattern::Repeated(Box::new(Repetition::OnceOrMore(Pattern::Alternate(vec![
+                Pattern::Repeated(Box::new(Repetition::OnceOrMore(Pattern::Alternate(vec![
                     (Pattern::Char('e')),
                     (Pattern::Char('f')),
-                ]))))),
+                ])))),
             )),
             Pattern::Repeated(Box::new(Repetition::Specific(
                 Pattern::Char('x'),
@@ -240,12 +240,12 @@
                 Some(3),
             ))),
             Pattern::Alternate(vec![
-                (Pattern::Char('g')),
-                (Pattern::Repeated(Box::new(Repetition::Specific(
+                Pattern::Char('g'),
+                Pattern::Repeated(Box::new(Repetition::Specific(
                     Pattern::Char('h'),
                     2,
                     Some(2),
-                )))),
+                ))),
                 (Pattern::Char('i')),
             ]),
             Pattern::Repeated(Box::new(Repetition::Specific(Pattern::Char('j'), 2, None))),