[svn] r5945: nemerle/trunk/tools/cs2n: Emit.n cs2n.n

malekith svnadmin at nemerle.org
Sat Nov 19 17:44:43 CET 2005


Log:
Add kinda hackish support for ASMX/ASPX.

Author: malekith
Date: Sat Nov 19 17:44:42 2005
New Revision: 5945

Modified:
   nemerle/trunk/tools/cs2n/Emit.n
   nemerle/trunk/tools/cs2n/cs2n.n

Modified: nemerle/trunk/tools/cs2n/Emit.n
==============================================================================
--- nemerle/trunk/tools/cs2n/Emit.n	(original)
+++ nemerle/trunk/tools/cs2n/Emit.n	Sat Nov 19 17:44:42 2005
@@ -28,6 +28,7 @@
 using System;
 using System.IO;
 using System.Collections;
+using Nemerle.IO;
 
 namespace Nemerle.CSharp
 {
@@ -41,9 +42,23 @@
         /**
          *  Opens output buffer
          */
-        public static Initialize (file_name : string) : void
+        public static Initialize (file_name : string, need_output_filter : bool) : void
         {                        
             out_stream = FileStream (file_name, FileMode.Create);
+
+            def output_filter (s) {
+              if (s.Trim () == "/*ASPX-REMOVE-BRACE*/}") ""
+              else {
+                def s = s.Replace ("class AspxRemoveMe {", "");
+                if (s.Trim ().StartsWith ("/*ASPX-UNCOMMENT:"))
+                  s.Replace ("/*ASPX-UNCOMMENT:", "").Replace ("*/", "")
+                else s
+              }
+            }
+
+            if (need_output_filter)
+              writer = PipeWriter (StreamWriter (out_stream), output_filter);
+            else
             writer = StreamWriter (out_stream);            
             buffer = System.Text.StringBuilder ("");
         }
@@ -122,7 +137,7 @@
         /* -- PRIVATE FIELDS -------------------------------------------------- */
         
         private mutable static out_stream : FileStream;
-        private mutable static writer : StreamWriter;
+        private mutable static writer : TextWriter;
         
         private mutable static buffer : System.Text.StringBuilder;        
         private mutable static is_buffered : bool = false;

Modified: nemerle/trunk/tools/cs2n/cs2n.n
==============================================================================
--- nemerle/trunk/tools/cs2n/cs2n.n	(original)
+++ nemerle/trunk/tools/cs2n/cs2n.n	Sat Nov 19 17:44:42 2005
@@ -29,11 +29,66 @@
 using System.IO;
 using antlr;
 using Nemerle.Utility;
+using Nemerle.IO;
 
 namespace Nemerle.CSharp
 {
     public class CMain
     {
+        enum InputKind {
+          | CSharp
+          | ASPX
+          | ASMX
+        }
+
+
+        static get_input_kind (in_file : string) : InputKind
+        {
+            if (in_file.EndsWith (".asmx"))
+              InputKind.ASMX
+            else if (in_file.EndsWith (".aspx"))
+              InputKind.ASPX
+            else
+              InputKind.CSharp
+        }
+
+        static make_lexer (in_file : string) : CSharpLexer
+        {
+            def uncomment (s) {
+              "/*ASPX-UNCOMMENT:" + s.TrimEnd () + "*/"
+            }
+
+            def asmx_filter (s) {
+              if (s.TrimStart ().StartsWith ("<%"))
+                uncomment (s) + "\n"
+              else s
+            }
+
+            mutable in_aspx_code = false;
+            def aspx_filter (s) {
+              def s' = s.Trim ().ToLower ();
+              if (s' == "<script runat=\"server\">") {
+                in_aspx_code = true;
+                uncomment (s) + "class AspxRemoveMe {\n"
+              } else if (s' == "</script>") {
+                in_aspx_code = false;
+                "/*ASPX-REMOVE-BRACE*/}" + uncomment (s) + "\n"
+              } else if (in_aspx_code)
+                s
+              else
+                uncomment (s) + "\n"
+            }
+
+            match (get_input_kind (in_file)) {
+              | CSharp =>
+                CSharpLexer (FileStream (in_file, FileMode.Open, FileAccess.Read))
+              | ASPX =>
+                CSharpLexer (PipeReader (StreamReader (in_file), aspx_filter))
+              | ASMX =>
+                CSharpLexer (PipeReader (StreamReader (in_file), asmx_filter))
+            }
+        }
+
         public static Main() : void
         {
 	    mutable files = [];
@@ -42,11 +97,12 @@
 	    {
 		Message.InFile = in_file;
 
-		Emit.Initialize (out_file);
+		Emit.Initialize (out_file,
+                                 need_output_filter = get_input_kind (in_file) != InputKind.CSharp);
 
 		ExtendedToken.ClearWhitespaces ();
               
-		def lexer = CSharpLexer (FileStream (in_file, FileMode.Open, FileAccess.Read));
+                def lexer = make_lexer (in_file);
 		lexer.setTokenObjectClass("Nemerle.CSharp.ExtendedToken");
               
 		def parser = CSharpParser (lexer);



More information about the svn mailing list