[svn] r6856: nemerle/trunk/ncc: completion/CodeCompletionEngine.n completion/CompletionEngineError.n hiera...

VladD2 svnadmin at nemerle.org
Thu Nov 9 01:48:43 CET 2006


Log:
Store error messages for methods bodies to MethodBuilders (in completion mode).

Author: VladD2
Date: Thu Nov  9 01:48:40 2006
New Revision: 6856

Modified:
   nemerle/trunk/ncc/completion/CodeCompletionEngine.n
   nemerle/trunk/ncc/completion/CompletionEngineError.n
   nemerle/trunk/ncc/hierarchy/ClassMembers.n
   nemerle/trunk/ncc/passes.n

Modified: nemerle/trunk/ncc/completion/CodeCompletionEngine.n
==============================================================================
--- nemerle/trunk/ncc/completion/CodeCompletionEngine.n	(original)
+++ nemerle/trunk/ncc/completion/CodeCompletionEngine.n	Thu Nov  9 01:48:40 2006
@@ -48,6 +48,29 @@
 
 namespace Nemerle.Compiler
 {
+  // Compiler messages
+  public enum MessageKind
+  {
+    | Error
+    | Warning
+    | Hint
+  }
+
+  [Record]
+  public class CompilerMessage
+  {
+    public mutable Message     : string;
+    public mutable Location    : Location;
+    public mutable MessageKind : MessageKind;
+
+    public override ToString() : string
+    {
+      $"$(this.MessageKind): $Message ($(this.Location))"
+    }
+  }
+
+  // Relocation
+
   public interface ISupportRelocation
   {
     RelocateImpl (info : RelocationInfo/*, ident : string*/) : void;
@@ -240,548 +263,3 @@
     }
   }
 }
-
-namespace Nemerle.Completion
-{
-    public delegate CompletionStageHandler (node : TypeInfo) : void;
-    
-    public enum EngineState {
-      | Pure
-      | LoadedLibs
-    }
-  
-    public class CodeCompletionEngineException : System.Exception
-    {
-        public mutable ParsingException : System.Exception;
-        
-        public this (innerException : System.Exception)
-        {
-            ParsingException = innerException;
-        }
-    }
-    
-    internal variant InternalReference
-    {
-    | Library { path : string }
-    | Assembly { assembly : System.Reflection.Assembly }
-    }
-    
-    public class DefineCollection : System.Collections.Generic.IEnumerable [string]
-    {
-        internal this (eng : Engine)
-        {
-            defines = [];
-            engine = eng;
-        }
-        
-        internal mutable defines : list[string];
-        engine : Completion.Engine;
-        
-        public Add (define : string) : void
-        {
-            unless (defines.Contains (define))
-            {
-                defines ::= define;
-                engine.Options.DefineConstant (define);
-                engine.Sources.set_unparsed_state ();
-            }
-        }
-        
-        public Remove (define : string) : void
-        {
-            when (defines.Contains (define))
-            {
-                _ = defines.Remove (define);
-                engine.Options.UndefineConstant (define);
-                engine.Sources.set_unparsed_state ();
-            }
-        }
-        
-        public Contains (define : string) : bool
-        {
-            defines.Contains (define)
-        }
-        
-        public Clear () : void
-        {
-            unless (defines.Length == 0)
-            {
-                foreach (define in defines)
-                    engine.Options.UndefineConstant (define);
-                defines = [];
-                engine.Sources.set_unparsed_state();
-            }
-        }
-        
-        public GetEnumerator () : Nemerle.Collections.IEnumerator[string]
-        {
-            Nemerle.Collections.ListEnumerator (defines)
-        }
-    }
-    
-    public class ReferenceCollection
-    {
-        internal this (eng : Engine)
-        {
-            references = Hashtable ();
-            engine = eng;
-        }
-        
-        internal mutable references : Hashtable[string, InternalReference];
-        engine : Completion.Engine;
-        
-        public Add (key : string, path : string) : void
-        {
-            unless (references.Contains (key))
-            {
-                references.Add (key, InternalReference.Library (path));
-                engine.LibrariesManager.AddLibrary (path);
-            }
-        }
-        
-        public Add (key : string, loadedAssembly : System.Reflection.Assembly) : void
-        {
-            unless (references.Contains (key))
-            {
-                references.Add (key, InternalReference.Assembly (loadedAssembly));
-                engine.LibrariesManager.AddAssembly (loadedAssembly);
-            }
-        }
-        
-        public Remove (key : string) : void
-        {
-            when (references.Contains (key))
-            {
-                references.Remove (key);
-                engine.state = EngineState.Pure;
-                engine.Sources.set_unparsed_state ();
-                
-                foreach (reference in references.Values)
-                {
-                | Library as l => engine.LibrariesManager.AddLibrary (l.path);
-                | Assembly as a => engine.LibrariesManager.AddAssembly (a.assembly);
-                }
-            }
-        }
-        
-        public Clear () : void
-        {
-            references = Hashtable ();
-            engine.state = EngineState.Pure;
-            engine.Sources.set_unparsed_state ();
-        }
-        
-        public ContainsKey (key : string) : bool
-        {
-            references.ContainsKey (key)
-        }
-        
-        public GetKeys () : System.Collections.Generic.IEnumerable[string]
-        {
-            references.Keys
-        }
-    }
-    
-    internal variant ParsedFile
-    {
-        | NotParsed { code : string }
-        | Parsed { decls : list [TopDeclaration]; code : string; }
-    }
-    
-    public class SourceCollection
-    {
-        internal this ()
-        {
-            sources = Hashtable ();
-        }
-        
-        internal mutable sources : Hashtable[string, ParsedFile];
-
-        public Add (file : string, contents : string) : void
-        {
-            if (sources.ContainsKey (file))
-                sources [file] = ParsedFile.NotParsed (contents);
-            else
-                sources.Add (file, ParsedFile.NotParsed (contents));
-        }
-        
-        public Remove (file : string) : void
-        {
-            sources.Remove (file);
-            set_unparsed_state ();
-        }
-        
-        public Clear () : void
-        {
-            sources = Hashtable ();
-            set_unparsed_state();
-        }       
-        
-        internal set_unparsed_state () : void
-        {
-            mutable fileList : list[string * string] = [];
-            foreach (file in sources) {
-                match (file.Value) {
-                | Parsed as p => fileList ::= (file.Key, p.code);
-                | _ => ();
-                }
-            }
-            foreach (item in fileList) {
-                sources [item [0]] = ParsedFile.NotParsed (item [1]);
-            }
-        }
-        
-        public ContainsKey (file : string) : bool
-        {
-            sources.ContainsKey (file)
-        }
-        
-        public GetKeys () : System.Collections.Generic.IEnumerable[string]
-        {
-            sources.Keys
-        }
-    }
-   
-    public class Engine : ManagerClass
-    {
-        public mutable Defines : DefineCollection;
-        public mutable References : ReferenceCollection;
-        public Sources : SourceCollection;
-        
-        [Accessor]
-        internal mutable state : EngineState;
-        
-        syncObject : object;
-
-        public this()
-        {
-            this (CompilationOptions ());
-        }
-        
-        public this (options : CompilationOptions)
-        {
-            base (options);
-            state = EngineState.Pure;
-            _isInCompletionMode = true;
-            syncObject = object();
-            Defines = DefineCollection (this);
-            References = ReferenceCollection (this);
-            Sources = SourceCollection ();
-            MessageOccured += process_error_message;
-            Options.GreedyReferences = true;
-            Options.ColorMessages = false;
-            Options.IgnoreConfusion = true;
-        }
-        
-        public Init () : void
-        {
-          Options.PersistentLibraries = (state == EngineState.LoadedLibs); // if we are not in loaded libs state, it will reload them
-          
-          // we must clean the nodes from current program - note that this behaviour is automatically provided by Run, but completion
-          // engine does not use it at the moment.. :(
-          when (Hierarchy != null)
-            Hierarchy.RemoveProgramTypes();
-         
-          InitCompiler ();
-          LoadExternalLibraries ();
-          
-          state = EngineState.LoadedLibs; // next time Init is called, we won't rebuild external types
-          // Sources.set_unparsed_state ();
-          listMessages = [];
-          referencesLocation = Hashtable ();
-        }
-        
-        public LesserInit () : void
-        {
-            if (shouldCreate (this.NameTree))        
-              this.NameTree = NamespaceTree (this);
-            else
-              this.NameTree.Init ();
-            when (shouldCreate (this.LibrariesManager))
-              this.LibrariesManager = LibraryReferenceManager (this, Options.LibraryPaths);     
-
-            this.Solver = Solver (this);
-      
-            this.CoreEnv = GlobalEnv.CreateCore(this.NameTree);
-        }
-        
-        mutable referencesLocation : Hashtable[IMember, list[Location]];
-        
-        public ReferencesToMembers : Hashtable[IMember, list[Location]]
-        {
-            get { referencesLocation }
-        }
-        
-        public override MarkAsUsed (member : IMember, location : Location) : void
-        {
-            member.HasBeenUsed = true;
-            add_to_ref (member, location);
-        }
-    
-        public override MarkAsAssigned (member : IField, location : Location) : void
-        {
-            member.HasBeenAssigned = true;
-            add_to_ref (member, location);
-        }
-        
-        static get_real_location (location : Location, name : string) : Location
-        {
-          // What for chemistry?
-          def col = location.Column - name.Length + 1;
-          def col = if (col >= 0) col else location.Column;
-          Location (location.FileIndex, location.Line, col,
-              location.EndLine, location.EndColumn)
-        }
-        
-        add_to_ref (member : IMember, location : Location) : void
-        {
-            try {
-                unless (referencesLocation.ContainsKey (member))
-                    referencesLocation.Add (member, []);
-            
-                referencesLocation [member] ::= get_real_location (location, member.Name);
-            }
-            catch {
-            | ex => System.Console.WriteLine (ex.Message);
-                   System.Console.WriteLine (ex.StackTrace);
-            }
-        }
-        
-        // If you want to recover the messages done by the parser/typer
-        public Output : System.IO.TextWriter
-        {
-            get { Message_output }
-            set { Message_output = value }
-        }
-        
-        public CompilerMessages : array[CompilerMessage]
-        {
-            get
-            {
-                if (listMessages == null)
-                    array(0)
-                else
-                    listMessages.ToArray ()
-            }
-        }
-        
-        [Obsolete ("Please use the overload which takes a position.")]
-        public RunCompletionEngine (
-          [NotNull] member : MethodBuilder,
-          [NotNull] contents : string)
-          : CompletionResult
-        {
-            def my_body = contents.Trim ();
-            RunCompletionEngine (member, my_body, my_body.Length)
-        }
-
-        public RunCompletionEngine (
-          [NotNull] observedMethod : MethodBuilder, 
-          [NotNull] contents       : string,
-          completionPosition       : int) 
-          : CompletionResult
-            requires completionPosition <= contents.Length
-        {
-            // Tell the methods we are in completion mode
-            mutable completionList = null;
-            def env = observedMethod.DeclaringType.GlobalEnv;
-
-            def contents = if (completionPosition == contents.Length)
-              contents + " " else contents;
-
-            def lexer = LexerCompletion (this, contents, completionPosition);
-            observedMethod.GetHeader().body =
-              FunBody.Parsed (MainParser.ParseExpr (env, lexer, true));
-        
-            try
-            {
-              observedMethod.RunBodyTyper ();
-            }
-            catch
-            {
-            | e is CompletionResult => completionList = e;
-            | e => System.Console.WriteLine (e.Message);
-            }
-            
-            completionList
-        }
-
-        mutable listMessages : list[CompilerMessage];
-        process_error_message (location : Location, message : string) : void
-        {
-            mutable error = CompilerMessage();
-            error.Location = location;
-            if (message.IndexOf ("error: ") != -1)
-            {
-                error.Message = message.Substring (message.IndexOf ("error: ")).Replace ("error: ", "");
-                error.MessageKind = MessageKind.Error;
-                listMessages ::= error;
-            }
-            else if (message.IndexOf ("warning: ") != -1)
-            {
-                error.Message = message.Substring (message.IndexOf ("warning: ")).Replace ("warning: ", "");
-                error.MessageKind = MessageKind.Warning;
-                listMessages ::= error;
-            }
-            else when (message.IndexOf ("hint: ") != -1)
-            {
-                error.Message = message.Substring (message.IndexOf ("hint: ")).Replace ("hint: ", "");
-                error.MessageKind = MessageKind.Hint;
-                listMessages ::= error;
-            }
-        }
-        
-        // This method should be used when iterating all along the type tree
-        // because looping inside the tree from other thread throws an 'out of sync' exception
-        public GetTypeTree (handler : CompletionStageHandler) : void
-        {
-            def tree = GetTypeTree ();
-            
-            def loop (x : NamespaceTree.Node)
-            {
-                foreach ( pair in x.children )
-                {
-                    def node = pair.Value;
-                    match (node.Value) {
-                    | NamespaceReference => loop (node);
-                    | Cached as c =>
-                         match (c.tycon) {
-                         | tb is TypeBuilder =>
-                            handler (tb);
-                         | _ => ();
-                         }
-                     | _ => ()
-                     }
-                 }
-            }
-            
-            loop (tree);
-        }
-       
-        public GetTypeTree () : NamespaceTree.Node
-        {
-            Instance = this;
-            Init ();
-        
-            // lexing of the NotParsed files
-            // we save the parsed files to improve performance
-
-            this.Hierarchy = TypesManager (this);
-    
-            mutable trees = [];
-            try
-            {
-                def filenames = Sources.sources.Fold ([], fun (k, _, acc) { k :: acc });
-              
-                foreach (filename in filenames)
-                {
-                    match (Sources.sources [filename])
-                    {
-                    | NotParsed as np =>
-                        def contents = np.code;
-                        def lexer = LexerString (this, contents, Location (Location.GetFileIndex (filename), 1, 1));
-                        def decls = this.ParsingPipeline (lexer);
-                        Sources.sources[filename] = ParsedFile.Parsed (decls, contents);
-                        trees ::= decls;
-                    | Parsed as p => trees ::= p.decls;
-                    }
-                }
-            }
-            catch
-            {
-                | _e => 
-                  #if DEBUG
-                  System.Console.WriteLine (_e);
-                  #endif
-                  {}
-            }
-             
-            // create N.C.TypeBuilders for all parsed types and add them to namespace hierarchy
-            try
-            {
-                foreach (group in trees) {
-                    List.Iter (group, this.ScanningPipeline);
-                }
-            }
-            catch
-            {
-                | _e => 
-                  #if DEBUG
-                  System.Console.WriteLine (_e);
-                  #endif
-                  {}
-            }
-            
-            try
-            {
-                this.Hierarchy.Run();
-            }
-            catch
-            {
-                | _e => 
-                  #if DEBUG
-                  System.Console.WriteLine (_e);
-                  #endif
-                  {}
-            }
-            
-            this.NameTree.namespace_tree
-         }
-         
-        public static GetNameFromType (t : MType) : string
-        {
-          | Class as c =>
-              mutable name = LookupSpecialName (c.tycon.FrameworkTypeName);
-              
-              when (c.args.Length > 0)
-              {
-                  name += "[";
-                  foreach (tyvar in c.args)
-                  {
-                      | x is MType => name += GetNameFromType (x) + ", ";
-                      | _ => ();
-                  }
-                  name = name.Trim(',', ' ') + "]";
-              }
-              name
-              
-          | TyVarRef as r => r.tyvar.Name
-          | Fun as f => GetNameFromType (f.from.Fix ()) + " -> " + GetNameFromType (f.to.Fix ())
-          | Tuple as tuple => mutable name = "(";
-              foreach (tx in tuple.args)
-              {
-                  | x is MType => name += GetNameFromType (x) + ", ";
-                  | _ => ();
-              }
-              name = name.Trim(',', ' ') + ")";
-              name
-          | Array as a => "array[" + GetNameFromType (a.t.Fix ()) + "]"
-          | Void => "void"
-          | Ref as rf => "ref " + GetNameFromType (rf.t.Fix ())
-          | Out as ut => "out " + GetNameFromType (ut.t.Fix ())
-          | _ => ""
-        }
-
-        /// Look for special names
-        private static LookupSpecialName (name : string) : string
-        {
-        | "System.Byte"    => "byte"
-        | "System.SByte"   => "sbyte"
-        | "System.Int16"   => "short"
-        | "System.UInt16"  => "ushort"
-        | "System.Int32"   => "int"
-        | "System.UInt32"  => "uint"
-        | "System.Int64"   => "long"
-        | "System.UInt64"  => "ulong"
-        | "System.Single"  => "float"
-        | "System.Double"  => "double"
-        | "System.Decimal" => "decimal"
-        | "System.String"  => "string"
-        | "System.Object"  => "object"
-        | "System.Boolean" => "bool"
-        | "System.Char"    => "char"
-        | _                => name
-        }
-    } // end class CodeCompletionEngine
-} // end namespace
-

Modified: nemerle/trunk/ncc/completion/CompletionEngineError.n
==============================================================================
--- nemerle/trunk/ncc/completion/CompletionEngineError.n	(original)
+++ nemerle/trunk/ncc/completion/CompletionEngineError.n	Thu Nov  9 01:48:40 2006
@@ -38,18 +38,18 @@
 
 namespace Nemerle.Completion
 {
-    public class CompilerMessage
-    {
-        public mutable Message : string;
-        public mutable Location : Location;
-        public mutable MessageKind : MessageKind;
-    }
-    
-    public enum MessageKind
-    {
-    | Error
-    | Warning
-    | Hint
-    }
+    //public class CompilerMessage
+    //{
+    //    public mutable Message : string;
+    //    public mutable Location : Location;
+    //    public mutable MessageKind : MessageKind;
+    //}
+    //
+    //public enum MessageKind
+    //{
+    //| Error
+    //| Warning
+    //| Hint
+    //}
 }
 

Modified: nemerle/trunk/ncc/hierarchy/ClassMembers.n
==============================================================================
--- nemerle/trunk/ncc/hierarchy/ClassMembers.n	(original)
+++ nemerle/trunk/ncc/hierarchy/ClassMembers.n	Thu Nov  9 01:48:40 2006
@@ -30,6 +30,7 @@
 using Nemerle.Utility;
 using Nemerle.Compiler.Typedtree;
 
+using SCG = System.Collections.Generic;
 using SR = System.Reflection;
 using SRE = System.Reflection.Emit;
 using PT = Nemerle.Compiler.Parsetree;
@@ -740,8 +741,28 @@
   //***********************************************************************
   // This members available only under completion engine.
 
+  public EnsureCompiled() : void
+  {
+    _ = BodyTyped;
+  }
+
+  mutable _bodyMessages : SCG.List[CompilerMessage];
+  public BodyMessages : SCG.List[CompilerMessage]
+  {
+    get
+    {
+      when (_bodyMessages == null)
+        _bodyMessages = SCG.List();
+
+      _bodyMessages
+    }
+  }
+
   public ResetBody () : void
   {
+    when (_bodyMessages != null)
+      _bodyMessages.Clear();
+
     _bodyTokens     = null;
     _bodyParsed     = null;
     _bodyTyped      = null;
@@ -759,7 +780,10 @@
     {
       System.Diagnostics.Trace.Assert(Manager.IsInCompletionMode);
       when (_bodyTokens == null)
+      {
+        Manager.SetCompiletMessages (BodyMessages);
         _bodyTokens = Manager.PreParseMethodBody (this);
+      }
 
       _bodyTokens
     }
@@ -777,6 +801,7 @@
       System.Diagnostics.Trace.Assert(Manager.IsInCompletionMode);
       when (_bodyParsed == null)
       {
+        Manager.SetCompiletMessages (BodyMessages);
         _bodyParsed = MainParser.ParseFunctionBody (Env, Ast.header, BodyTokens);
         fun_header.body = FunBody.Parsed (_bodyParsed);
       }
@@ -795,10 +820,15 @@
       System.Diagnostics.Trace.Assert(Manager.IsInCompletionMode);
       when (_bodyTyped == null)
       {
+        Manager.SetCompiletMessages (BodyMessages);
+        try
+        {
         _ = BodyParsed; // Use side affect
         RunBodyTyper();
         _bodyTyped = (fun_header.body :> FunBody.Typed).expr;
       }
+        finally { Manager.SetCompiletMessages (null); }
+      }
 
       _bodyTyped
     }

Modified: nemerle/trunk/ncc/passes.n
==============================================================================
--- nemerle/trunk/ncc/passes.n	(original)
+++ nemerle/trunk/ncc/passes.n	Thu Nov  9 01:48:40 2006
@@ -450,9 +450,19 @@
       }
     }
 
-    protected internal virtual PreParseMethodBody (_method : MethodBuilder)
+    /// This method must be overridden in intellisece engine.
+    /// It must obtain methode body code frome sorce. Parse it. And return result tokens.
+    protected internal virtual PreParseMethodBody (method : MethodBuilder)
       : Token.BracesGroup
     {
+      _ = method;
+      throw System.NotImplementedException ();
+    }
+
+    /// This method must be overridden in intellisece engine.
+    protected internal virtual SetCompiletMessages (messages : SCG.List[CompilerMessage]) : void
+    {
+      _ = messages;
       throw System.NotImplementedException ();
     }
   }



More information about the svn mailing list