Author: mkrueger
Date: 2007-10-09 04:07:58 -0400 (Tue, 09 Oct 2007)
New Revision: 87149

Modified:
   trunk/monodevelop/Core/src/NRefactory/ChangeLog
   trunk/monodevelop/Core/src/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs
Log:
* Project/Src/Lexer/CSharp/Lexer.cs: C# lexer can now handle #if ...
  #else ... #endif pre processing directives.

  (Needed to fix bug 331772 - Unable to handle conditional class
  definition)

Modified: trunk/monodevelop/Core/src/NRefactory/ChangeLog
===================================================================
--- trunk/monodevelop/Core/src/NRefactory/ChangeLog     2007-10-09 08:02:43 UTC 
(rev 87148)
+++ trunk/monodevelop/Core/src/NRefactory/ChangeLog     2007-10-09 08:07:58 UTC 
(rev 87149)
@@ -1,3 +1,11 @@
+2007-10-09  Mike Krüger <[EMAIL PROTECTED]> 
+
+       * Project/Src/Lexer/CSharp/Lexer.cs: C# lexer can now handle #if ... 
#else
+         ... #endif pre processing directives.
+       
+         (Needed to fix bug 331772 - Unable to handle conditional class
+         definition)
+
 2007-09-15  Mike Krüger <[EMAIL PROTECTED]> 
 
        * Project/Src/Parser/CSharp/cs.ATG, Project/Src/Parser/CSharp/Parser.cs:

Modified: 
trunk/monodevelop/Core/src/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs
===================================================================
--- trunk/monodevelop/Core/src/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs     
2007-10-09 08:02:43 UTC (rev 87148)
+++ trunk/monodevelop/Core/src/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs     
2007-10-09 08:07:58 UTC (rev 87149)
@@ -6,6 +6,7 @@
 // </file>
 
 using System;
+using System.Collections.Generic;
 using System.Globalization;
 using System.IO;
 using System.Text;
@@ -13,17 +14,59 @@
 namespace ICSharpCode.NRefactory.Parser.CSharp
 {
        internal sealed class Lexer : AbstractLexer
-       {
+       {
+               bool skipToken = false;
+               bool readIf    = false;
+               Dictionary<string, bool> defines = new Dictionary<string,bool> 
(); 
                public Lexer(TextReader reader) : base(reader)
                {
-               }
+               }
+               
+               public void ClearDefinedSymbols ()
+               {
+                       defines.Clear ();
+               }
+               
+               public void AddDefinedSymbol (string symbol)
+               {
+                       defines[symbol] = true;
+               }
                
+               public void RemoveDefinedSymbol (string symbol)
+               {
+                       if (defines.ContainsKey (symbol))
+                               defines.Remove (symbol);
+               }
+               
+               public bool IsDefined (string symbol)
+               {
+                       return defines.ContainsKey (symbol);
+               }
+               
                void ReadPreProcessingDirective()
                {
                        Location start = new Location(Col - 1, Line);
                        bool canBeKeyword;
-                       string directive = ReadIdent('#', out canBeKeyword);
+                       string directive = ReadIdent('#', out canBeKeyword);
                        string argument  = ReadToEndOfLine();
+                       switch (directive) {
+                       case "#if":
+                               readIf    = true;
+                               string define = argument.Trim ();
+                               int idx = define.IndexOfAny (new char [] { ' ', 
'\t' });
+                               if (idx > 0)
+                                       define = define.Substring (0, idx);
+                               skipToken = !IsDefined (define);
+                               break;
+                       case "#endif":
+                               skipToken = false;
+                               readIf    = false;
+                               break;
+                       case "#else":
+                               if (readIf)
+                                       skipToken = !skipToken;
+                               break;
+                       }
                        
this.specialTracker.AddPreprocessingDirective(directive, argument.Trim(), 
start, new Location(start.X + directive.Length + argument.Length, start.Y));
                }
                
@@ -98,22 +141,25 @@
                                                        string s = 
ReadIdent(ch, out canBeKeyword);
                                                        if (canBeKeyword) {
                                                                int 
keyWordToken = Keywords.GetToken(s);
-                                                               if 
(keyWordToken >= 0) {
-                                                                       return 
new Token(keyWordToken, x, y);
+                                                               if 
(keyWordToken >= 0) {
+                                                                       token = 
 new Token(keyWordToken, x, y);
+                                                                       break;
                                                                }
-                                                       }
-                                                       return new 
Token(Tokens.Identifier, x, y, s);
+                                                       }
+                                                       token = new 
Token(Tokens.Identifier, x, y, s);
                                                } else if (Char.IsDigit(ch)) {
                                                        token = ReadDigit(ch, 
Col - 1);
                                                } else {
                                                        token = 
ReadOperator(ch);
                                                }
                                                break;
-                               }
+                               }
                                
                                // try error recovery (token = null -> continue 
with next char)
-                               if (token != null) {
-                                       return token;
+                               if (token != null) {
+                                       if (skipToken)
+                                               continue;
+                                       return token;
                                }
                        }
                        

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to