[svn] r7489: nemerle/trunk: lib/macros.n ncc/parsing/MainParser.n ncc/testsuite/positive/macrolib.n ncc/te...

Luntain svnadmin at nemerle.org
Sun Feb 25 10:05:09 CET 2007


Log:
changed implementation of custom member feature to improve code structure

Author: Luntain
Date: Sun Feb 25 10:05:09 2007
New Revision: 7489

Modified:
   nemerle/trunk/lib/macros.n
   nemerle/trunk/ncc/parsing/MainParser.n
   nemerle/trunk/ncc/testsuite/positive/macrolib.n
   nemerle/trunk/ncc/testsuite/positive/syntax_extension_custom_members.n

Modified: nemerle/trunk/lib/macros.n
==============================================================================
--- nemerle/trunk/lib/macros.n	(original)
+++ nemerle/trunk/lib/macros.n	Sun Feb 25 10:05:09 2007
@@ -29,6 +29,26 @@
 namespace Nemerle {
   public type MacroTargets = System.AttributeTargets;
 
+//  public enum MacroTargets { // values taken from System.AttributeTargets
+//      | Assembly = 1         // I think that we can be sure they won't change
+//      | Module = 2
+//      | Class = 4
+//      | Struct = 8
+//      | Enum = 0x10
+//      | Constructor = 0x20
+//      | Method = 0x40
+//      | Property = 0x80
+//      | Field = 0x100
+//      | Event = 0x200
+//      | Interface = 0x400
+//      | Parameter = 0x800
+//      | Delegate = 0x1000
+//      | ReturnValue = 0x2000
+//      | GenericParameter = 0x4000
+//      | WithinClass = 0x8000  // single value added beyod the ones in System.AttributeTargets
+//      | All = 0x7fff
+//  }
+
   [System.FlagsAttribute ()]
   public enum MacroPhase {
     | None
@@ -40,7 +60,8 @@
   [System.AttributeUsage (System.AttributeTargets.Class)]
   public sealed class MacroUsageAttribute : System.Attribute
   {
-    valid_on : System.AttributeTargets;
+//    valid_on : System.AttributeTargets;
+    valid_on: MacroTargets;
     mutable allow_multiple : bool;
     mutable inherited : bool;
     phase : MacroPhase;
@@ -65,8 +86,10 @@
       }
     }
 
-    public ValidOn : System.AttributeTargets {
-      get { valid_on; }
+    public ValidOn : MacroTargets {
+      get { 
+          valid_on
+      }
     }
   }
 

Modified: nemerle/trunk/ncc/parsing/MainParser.n
==============================================================================
--- nemerle/trunk/ncc/parsing/MainParser.n	(original)
+++ nemerle/trunk/ncc/parsing/MainParser.n	Sun Feb 25 10:05:09 2007
@@ -667,7 +667,7 @@
                         | _ =>
                           pop_stream ();
                           when (key == "variant") in_variant = true;
-                          members = process_groups (children, "type member", fun() {ParseClassMember(mods)}); 
+                          members = process_groups (children, "type member", ParseClassMember); 
                           when (members.Exists (_ is ClassMember.TypeDeclaration (TopDeclaration.Delegate (null))))
                             members = members.Filter (m => !(m 
                               is ClassMember.TypeDeclaration (TopDeclaration.Delegate (null))));
@@ -747,69 +747,24 @@
       res
     }
 
-
-    ParseClassMember(): ClassMember
-    {
-        ParseClassMember(null)
-    }
-    
-    // class_modifiers might be null. It is used only in case of "custom" member parsed.
-    // Custom member is added as a custom attribute to parent class.
-    ParseClassMember (class_modifiers: Modifiers) : ClassMember
+    ParseClassMember () : ClassMember
     {
       def start_tok = get_token ();
       match (start_tok) {
         | Token.LooseGroup (toks) =>
           push_stream (toks);
           mutable customs = get_customs ();
-
-          def first_modifier = peek_token();
           def mods = Modifiers (get_modifiers (), []);
-          def first_no_modifier = peek_token();
-          def last_modifier = last_tok;
 
-          mutable parsed_custom_member = false;
-          when (class_modifiers != null) {
+          mutable parsed_some_top_extension = false;
             def collect_type_macro_inv = Modifiers();
-            parse_top_extensions (collect_type_macro_inv, MacroTargets.Class);
-            if (!collect_type_macro_inv.IsEmpty && stream == null) {
-                // End of the current "member" loose group; there is nothing left 
-                // to be parsed as a regular member, so we treat it as a macro invocation with special syntax.
-                parsed_custom_member = true;
-                foreach (type_macro_attr in collect_type_macro_inv.GetCustomAttributes()) {
-                    match (type_macro_attr) {
-                        | PExpr.MacroCall(name,name_space,parms) =>
-                            def modifiers_group = Token.LooseGroup(if (!first_no_modifier.Equals(first_modifier)) {
-                                    last_modifier.Next = null;
-                                    first_modifier
-                                } else {
-                                    null });
-                            def custom_attrs_group = Token.LooseGroup(if (customs.Length>0) {
-                                    customs.Last.Next = null;
-                                    customs.Head
-                                } else
-                                    null);
-                            def new_parms = parms+[SyntaxElement.RawToken(modifiers_group),SyntaxElement.RawToken(custom_attrs_group)];
-                            class_modifiers.AddCustomAttribute(PExpr.MacroCall(name,name_space,new_parms))
-                        | _ => ()
-                    }
-                }
-            }
-            else {
-                // There is sth more in this loose group. Even if a class targeted macro was parsed, this
-                // does not mean it is a custom member. It could be a macro targeted at a nested class.
-                foreach (type_macro_attr in collect_type_macro_inv.GetCustomAttributes()) {
+          parse_top_extensions (collect_type_macro_inv, MacroTargets.All & ~MacroTargets.Parameter);
+          when (!collect_type_macro_inv.IsEmpty) {
+            parsed_some_top_extension = true;
+            foreach (type_macro_attr in collect_type_macro_inv.GetCustomAttributes())
                     mods.AddCustomAttribute(type_macro_attr)
                 }
-            }
-          }
 
-          if (parsed_custom_member) {
-            pop_stream();
-            null
-          }
-          else {
-          parse_top_extensions (mods, MacroTargets.All & ~MacroTargets.Parameter);
           def tok = peek_token ();
           def result = 
             match (tok) {
@@ -878,7 +833,16 @@
                 unless (mods.Attributes == NemerleAttributes.None)
                   Error (tok, "modifiers not allowed on options"); 
                 parse_option (mods)
-
+              | Token.EndOfGroup as end when parsed_some_top_extension =>
+                    take_attributes_out (ref customs, System.AttributeTargets.Method, true, mods);
+                  def h = Fun_header (start_tok.Location + end.Location,
+                                           name = Splicable.Name (tok.Location,mkname(Util.tmpname("custom_member"))),
+                                           ret_type = PExpr.Void(),
+                                           parms = [], 
+                                           typarms = Typarms.Empty);
+                  def dummy_method = ClassMember.Function (header = h, name = h.name,  modifiers = mods,
+                                         loc = start_tok.Location, body = FunBody.Parsed(<[]>), implemented = []);
+                  dummy_method
               | _ =>
                 take_attributes_out (ref customs, System.AttributeTargets.Class, true, mods);
                 def td = ParseTypeDeclaration (mods, start_tok.Location);
@@ -889,8 +853,6 @@
             }
           pop_stream ("class member");
           result
-          }
-            
         | _ => null
       }
     }

Modified: nemerle/trunk/ncc/testsuite/positive/macrolib.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/macrolib.n	(original)
+++ nemerle/trunk/ncc/testsuite/positive/macrolib.n	Sun Feb 25 10:05:09 2007
@@ -448,14 +448,14 @@
 
 
 // for syntax_extension_custom_members.n
-[Nemerle.MacroUsage(Nemerle.MacroPhase.BeforeInheritance, Nemerle.MacroTargets.Class)]
-macro abrakadabra( type_builder: TypeBuilder,name: Token, _body:Token, modifiers: Token, custom_attributes: Token)
+[Nemerle.MacroUsage(Nemerle.MacroPhase.BeforeInheritance, Nemerle.MacroTargets.Method)]
+macro abrakadabra( type_builder: TypeBuilder, method_builder:ParsedMethod, name: Token, _body:Token)
 syntax("abrakadabra","(",name,")",_body) {
     def prop = <[ 
         decl:
         public static $(name.ToString().Trim(): dyn): string {
             get {
-                $(modifiers.ToString(): string)+ $(custom_attributes.ToString(): string)
+                $(method_builder.modifiers.ToString(): string)
             }
         }
     ]>;

Modified: nemerle/trunk/ncc/testsuite/positive/syntax_extension_custom_members.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/syntax_extension_custom_members.n	(original)
+++ nemerle/trunk/ncc/testsuite/positive/syntax_extension_custom_members.n	Sun Feb 25 10:05:09 2007
@@ -1,22 +1,30 @@
 // REFERENCE: macrolib.dll
 using Nemerle.IO;
 
+public class AttrA: System.Attribute {
+}
+
+[System.AttributeUsage (System.AttributeTargets.Method)]
+public class AttrB: System.Attribute {
+    public override ToString(): string {"aksjdflaskdfj"}
+}
+
 public class Foo {
 
     public MethodOfFoo(): void {
 		print("From base method\n");
 	}
 
-    public internal protected private abrakadabra(WithModifiers) {  //;)
+    private abrakadabra(WithModifiers) {
     }
 
     abrakadabra(WithoutModifiers) {
     }
 
-    [Bla] abrakadabra(WithCustomAttributes){
+    [AttrA]abrakadabra(WithCustomAttributes){
     }
 
-    [Bla][Bla2] public abrakadabra(WithCustomAttributesAndModifiers){
+    [AttrA][AttrB]public abrakadabra(WithCustomAttributesAndModifiers){
     }
 
 	public static Main(): void {
@@ -33,13 +41,13 @@
 
 /*
 BEGIN-OUTPUT
-public internal protected private
+[] Private
 --
-
+[] None
 --
-[Bla , ] 
+[AttrA] None
 --
-public [Bla , ] [Bla2 , ]
+[AttrB, AttrA] Public
 --
 END-OUTPUT
 */



More information about the svn mailing list