[svn] r6447: nemerle/trunk: macros/text.n ncc/testsuite/positive/basic-macros.n

nazgul svnadmin at nemerle.org
Wed Jul 12 21:48:36 CEST 2006


Log:
Fix regexp macro. Patch by Snaury

Author: nazgul
Date: Wed Jul 12 21:48:34 2006
New Revision: 6447

Modified:
   nemerle/trunk/macros/text.n
   nemerle/trunk/ncc/testsuite/positive/basic-macros.n

Modified: nemerle/trunk/macros/text.n
==============================================================================
--- nemerle/trunk/macros/text.n	(original)
+++ nemerle/trunk/macros/text.n	Wed Jul 12 21:48:34 2006
@@ -106,19 +106,34 @@
                   // check if this group is optional (it's context information)
                   // so we must count [(] and [)]
                   def stack = Stack();
+                  mutable slashes_in_row = 0;
+                  mutable found_group = false;
                   for (mutable i = 0; !end && i < str.Length; ++i)
                   {
                     match (str[i]) {
-                      | '(' => stack.Push(i <= m.Index)
-                      | ')' =>
-                        when(stack.Pop() && i > m.Index && i + 1 < str.Length && str[i + 1] == '?') {
+                      | '\\' => slashes_in_row++
+                      | '(' when (slashes_in_row % 2 == 0) =>
+                        slashes_in_row = 0;
+                        if(i > m.Index && !found_group)
+                          end = true;
+                        else {
+                          when(i == m.Index)
+                            found_group = true;
+                          stack.Push(i <= m.Index);
+                        }
+                      | ')' when (slashes_in_row % 2 == 0) =>
+                        slashes_in_row = 0;
+                        when(stack.Pop() && i > m.Index && i + 1 < str.Length && (str[i + 1] == '?' || str[i + 1] == '*')) {
                           end = true;
                           optional = true;
                         }
-                      | _ => ()
+                      | _ => slashes_in_row = 0
                     }
                   }
 
+                  // the group can actually be escaped as in @"(\(?<something>.*)"
+                  // we can't properly check such escapes with regex thus check here
+                  when(found_group) {
                   // store this existing named group with its name, information 
                   // if it's optional and name of its enforced type  
                   // - [(?<name : type>..)?]
@@ -131,6 +146,7 @@
                                   Some (m.Groups[3].ToString ())) :: patnames;
                     | _ => ()
                   }
+                  }
 
                   m = m.NextMatch ()
                 }
@@ -223,6 +239,8 @@
 
           match (cass) {
             // creates entire expression checking if one case have succeeded
+            | [(_, patnames, expr)] =>
+              <[ { .. $(List.Append (build_bindings (patnames, []), [expr])) } ]>
             | (grds, patnames, expr) :: xs =>
               <[ 
                 match ($(build_alts (grds))) { 
@@ -233,7 +251,7 @@
                 }
               ]>
 
-            | [] => <[ throw MatchFailureException () ]>
+            | [] => Message.FatalError("empty list of guards?")
           }
         }
 
@@ -242,7 +260,7 @@
           match (default) {
             | None =>
               Message.Warning ("this pattern might be not exhaustive");
-              <[ () ]>
+              <[ throw MatchFailureException() ]>
 
             | Some (defexpr) => defexpr
           }

Modified: nemerle/trunk/ncc/testsuite/positive/basic-macros.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/basic-macros.n	(original)
+++ nemerle/trunk/ncc/testsuite/positive/basic-macros.n	Wed Jul 12 21:48:34 2006
@@ -85,6 +85,11 @@
         }
       | _ => printf ("default\n");
     }
+    
+    regexp match (s) {
+      | @"(?<_a>.*)\)" => printf ("parens %s\n", _a);
+      | _ => ()
+    }
   }
 
   TestRegexpMatch () : void 
@@ -98,6 +103,7 @@
     make ("22-");
     make (" ma kota");
     make ("Ala ma kota");
+    make ("abba)");
   }
 
 
@@ -357,6 +363,8 @@
 default
 noname?
 Ala
+a
+parens abba
 ----- Select from tuple -----
 d
 3 7 gfd b2



More information about the svn mailing list