[svn] r6164: nemerle/trunk: macros/core.n ncc/testsuite/positive/two-ctors.n

nazgul svnadmin at nemerle.org
Sat Mar 25 22:51:10 CET 2006


Log:
Extend Record macro to allow patterns of fields inclusion

Author: nazgul
Date: Sat Mar 25 22:51:09 2006
New Revision: 6164

Modified:
   nemerle/trunk/macros/core.n
   nemerle/trunk/ncc/testsuite/positive/two-ctors.n

Modified: nemerle/trunk/macros/core.n
==============================================================================
--- nemerle/trunk/macros/core.n	(original)
+++ nemerle/trunk/macros/core.n	Sat Mar 25 22:51:09 2006
@@ -29,6 +29,7 @@
 using Nemerle.Compiler;
 using Nemerle.Compiler.Typedtree;
 using Nemerle;
+using System.Text.RegularExpressions;
 
 using Nemerle.Collections;
 using Nemerle.Utility;
@@ -520,7 +521,7 @@
   [Nemerle.MacroUsage (Nemerle.MacroPhase.BeforeInheritance,
                        Nemerle.MacroTargets.Class,
                        Inherited = false, AllowMultiple = false)]
-  macro Record (par : TypeBuilder)
+  macro Record (par : TypeBuilder, params _ : list [PExpr])
   {
       par.DisableImplicitConstructor ();
   }
@@ -529,11 +530,13 @@
   [Nemerle.MacroUsage (Nemerle.MacroPhase.WithTypedMembers,
                        Nemerle.MacroTargets.Class,
                        Inherited = false, AllowMultiple = false)]
-  macro Record (par : TypeBuilder)
+  macro Record (par : TypeBuilder, params options : list [PExpr])
   {
     def instance_flags = BindingFlags.Instance %| BindingFlags.Public %| 
       BindingFlags.NonPublic %| BindingFlags.DeclaredOnly;
 
+    def inclusion_regexs = MacrosHelper.AnalyseRecordFieldsPatterns (options);
+      
     def make_ctor (is_value_type, base_ctor : IMethod) {
       def (ctor_parms, base_call) =
         if (base_ctor == null)
@@ -552,11 +555,15 @@
       def flds = par.GetFields (instance_flags);
 
       def collect (mem : IField, acc) {
+        if (MacrosHelper.FieldNameMatchesPatterns (mem.Name, inclusion_regexs)) {
         def n = Macros.UseSiteSymbol (mem.Name);
         def fp = <[ parameter: $(n : name) : $(mem.GetMemType () : typed) ]>;
         def ex = <[ this.$(n : name) = $(n : name) ]>;
         def (es, ps) = acc;
         (ex :: es, fp :: ps)
+        }
+        else 
+          acc
       };
       def (assigns, parms) = List.FoldLeft (flds, ([], []), collect);
       
@@ -598,6 +605,39 @@
   }
 
   
+  module MacrosHelper {
+    public AnalyseRecordFieldsPatterns (options : list [PT.PExpr]) : Regex * Regex
+    {
+      mutable inclusion = null;
+      mutable exclusion = null;
+      foreach (e in options) {
+        | <[ Include = [..$names] ]> =>
+          inclusion = Regex ("^" + names.ToString ("$|^") + "$")
+        
+        | <[ Include = $(regexp : string) ]> =>
+          inclusion = Regex (regexp)
+          
+        | <[ Exclude = [..$names] ]> =>
+          exclusion = Regex ("^" + names.ToString ("$|^") + "$")
+        
+        | <[ Exclude = $(regexp : string) ]> =>
+          exclusion = Regex (regexp)
+          
+        | e =>
+          Message.Error (e.Location, $"unsupported argument `$e' in Record macro, please specify 'Include/Exclude = [name1,name2]/pattern")
+      }
+      (inclusion, exclusion)
+    }
+    
+    public FieldNameMatchesPatterns (name : string, patterns : Regex * Regex) : bool
+    {
+      def (inclusion, exclusion) = patterns;
+      (inclusion == null || inclusion.Match (name).Success) && 
+      (exclusion == null || !exclusion.Match (name).Success)
+    }
+  }
+  
+  
   [Nemerle.MacroUsage (Nemerle.MacroPhase.BeforeInheritance,
                        Nemerle.MacroTargets.Class,
                        Inherited = false, AllowMultiple = false)]

Modified: nemerle/trunk/ncc/testsuite/positive/two-ctors.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/two-ctors.n	(original)
+++ nemerle/trunk/ncc/testsuite/positive/two-ctors.n	Sat Mar 25 22:51:09 2006
@@ -14,6 +14,41 @@
   static this () {}
 }
 
+namespace RecordMacros 
+{
+  [Record (Include = [a, b, c])]
+  class X1 {
+    a : int; 
+    aa : int;
+    b : string;
+    bb : string;
+    c : double;
+    cc : list [int];
+  }
+  
+  [Record (Include = ".*sny", Exclude = "bra.*")]
+  class X2 {
+    x : int; // not included
+    krasny : double;
+    brasny : string; // not included
+    pomosny : list [int];
+  }
+  
+  [Record (Exclude = [xx])]
+  class X3 {
+    x : int;
+    xx : string;
+    xxx : double;
+  }
+
+  module A {
+    Run () : void {
+      def _ = X1(1, "a", 1.1);
+      def _ = X2(2.0, []);
+      def _ = X3(1, 1.1);  
+    }
+  }  
+}
 
 module M {
   public Main () : void {



More information about the svn mailing list