changeset 1283:b04354e24d1b

ML-style comments inside XML
author Adam Chlipala <adam@chlipala.net>
date Tue, 10 Aug 2010 14:44:26 -0400
parents a9a500d22ebc
children 43ca083678f8
files src/urweb.lex tests/xcomments.ur tests/xcomments.urp tests/xcomments.urs
diffstat 4 files changed, 31 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/urweb.lex	Tue Jul 27 14:04:09 2010 -0400
+++ b/src/urweb.lex	Tue Aug 10 14:44:26 2010 -0400
@@ -34,6 +34,8 @@
 type ('a,'b) token = ('a,'b) Tokens.token
 type lexresult = (svalue,pos) Tokens.token
 
+val commentOut = ref (fn () => ())
+
 local
   val commentLevel = ref 0
   val commentPos = ref 0
@@ -47,7 +49,10 @@
     
   fun exitComment () =
       (ignore (commentLevel := !commentLevel - 1);
-       !commentLevel = 0)
+       if !commentLevel = 0 then
+           !commentOut ()
+       else
+           ())
 
   fun eof () = 
     let 
@@ -167,17 +172,14 @@
 ws = [\ \t\012];
 intconst = [0-9]+;
 realconst = [0-9]+\.[0-9]*;
-notags = [^<{\n]+;
+notags = [^<{\n(]+;
 oint = [0-9][0-9][0-9];
 xint = x[0-9a-fA-F][0-9a-fA-F];
 
 %%
 
-<INITIAL> \n          => (newline yypos;
-                          continue ());
-<COMMENT> \n          => (newline yypos;
-                          continue ());
-<XMLTAG> \n           => (newline yypos;
+<INITIAL,COMMENT,XMLTAG>
+      \n              => (newline yypos;
                           continue ());
 <XML> \n              => (newline yypos;
                           Tokens.NOTAGS (yytext, yypos, yypos + size yytext));
@@ -185,14 +187,24 @@
 <INITIAL> {ws}+       => (lex ());
 
 <INITIAL> "(*"        => (YYBEGIN COMMENT;
+                          commentOut := (fn () => YYBEGIN INITIAL);
                           enterComment (pos yypos);
                           continue ());
-<INITIAL> "*)"        => (ErrorMsg.errorAt' (pos yypos, pos yypos) "Unbalanced comments";
+<XML> "(*"            => (YYBEGIN COMMENT;
+                          commentOut := (fn () => YYBEGIN XML);
+                          enterComment (pos yypos);
+                          continue ());
+<XMLTAG> "(*"         => (YYBEGIN COMMENT;
+                          commentOut := (fn () => YYBEGIN XMLTAG);
+                          enterComment (pos yypos);
+                          continue ());
+<INITIAL,XML,XMLTAG>
+             "*)"     => (ErrorMsg.errorAt' (pos yypos, pos yypos) "Unbalanced comments";
 			  continue ());
 
 <COMMENT> "(*"        => (enterComment (pos yypos);
                           continue ());
-<COMMENT> "*)"        => (if exitComment () then YYBEGIN INITIAL else ();
+<COMMENT> "*)"        => (exitComment ();
 			  continue ());
 
 <STRING,CHAR> "\\\""  => (str := #"\"" :: !str; continue());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/xcomments.ur	Tue Aug 10 14:44:26 2010 -0400
@@ -0,0 +1,8 @@
+fun foo () = <xml>Hi!</xml>
+
+(* fun bar () = return (* No *)<xml>Yes!</xml> *)
+
+fun main () = return <xml><body>
+  A (* B *) C (* D (* E *) F *) D<br/>
+  <b>A</b> <i>(* B *) C <b>D (* E *) F {foo ()}</b></i>
+</body></xml>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/xcomments.urp	Tue Aug 10 14:44:26 2010 -0400
@@ -0,0 +1,1 @@
+xcomments
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/xcomments.urs	Tue Aug 10 14:44:26 2010 -0400
@@ -0,0 +1,1 @@
+val main : unit -> transaction page