View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.millscript.syntax;
22  
23  import org.millscript.millscript.expr.CommaExpr;
24  import org.millscript.millscript.expr.Expr;
25  
26  /**
27   * This class implements XML comment syntax. An XML comment is not a valid
28   * MillScript comment, it is a valid datatype, whereas a MillScript comment
29   * would be discarded.
30   *
31   * <p>
32   * <code>&lt;-- (%p|.)* --&gt;</code>
33   * </p>
34   *
35   * <p>
36   * The contents of an XML comment are treated like a special multi-line String
37   * in MillScript. This allows easier inclusion of JavaScript code within
38   * MillScript files. The XML comment are read in just the same way as a String,
39   * so all the same escape sequences are available. XML comments can also be
40   * formatted via the format function, hence be careful with "%p"s.
41   * </p>
42   *
43   * @see org.millscript.millscript.expr.CommaExpr
44   * @see org.millscript.millscript.expr.ConstantExpr
45   * @see org.millscript.millscript.expr.XMLExpr
46   */
47  public final class XMLCommentSyntax extends PrefixSyntax {
48  
49      /**
50       * The closing character sequence for an XML comment.
51       */
52      static final String ENDCOMMENT = "-->";
53  
54      /**
55       * The opening character sequence for an XML comment.
56       */
57      static final String STARTCOMMENT = "<!--";
58  
59      /**
60       * Parses an XML comment. This is done by informing the parser we're going
61       * to read an XML comment, and then reading to the closing character
62       * sequence.
63       *
64       * @param   parser  the Parser to parse XML from
65       * @return  a <code>ConstantExpr</code> for the parsed syntax
66       */
67      public Expr readXMLComment( final Parser parser ) {
68          // Let the parser know we're about to read an XML comment.
69          // TODO - This adjusts the meaning of "-", but is probably the wrong way to do
70          //        it.
71          parser.setWhere( 'c' );
72          // Read the comment
73          Expr e = parser.readExprTo( ENDCOMMENT );
74          // Reset the parser to it's default
75          parser.setWhere( '?' );
76          // Return the XML comment.
77          return e;
78      }
79  
80      /**
81       * @see org.millscript.millscript.syntax.PrefixSyntaxInterface#prefix(java.lang.String, org.millscript.millscript.syntax.Parser)
82       */
83      @Override
84      public Expr prefix( final String sym, final Parser parser ) {
85          // Read the XML comment
86          Expr e = readXMLComment( parser );
87  
88          // Now check if the next token is XML or another comment
89          while ( parser.peekToken() == TokenType.NAME ) {
90              // TODO - Note this might be broken. What does:
91              //        <!-- hello --> < 5
92              //        mean?
93              if ( parser.getName() == "<" ) {
94                  // The next token may be the start of an XML expression
95                  parser.dropToken();
96                  // Get a new XML element syntax
97                  XMLElementSyntax xes = new XMLElementSyntax();
98                  // Read the XML expression
99                  e = CommaExpr.make( e, xes.readXML( parser ) );
100             } else if ( parser.getName() == STARTCOMMENT ) {
101                 // The next token is the start of another XML comment
102                 parser.dropToken();
103                 // XML comment. Re-parse using this.prefix().
104                 e = CommaExpr.make( e, readXMLComment( parser ) );
105             } else {
106                 // Not XML, break out and continue.
107                 break;
108             }
109         }
110         // Return our expression
111         return e;
112     }
113 }