1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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><-- (%p|.)* --></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
69
70
71 parser.setWhere( 'c' );
72
73 Expr e = parser.readExprTo( ENDCOMMENT );
74
75 parser.setWhere( '?' );
76
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
86 Expr e = readXMLComment( parser );
87
88
89 while ( parser.peekToken() == TokenType.NAME ) {
90
91
92
93 if ( parser.getName() == "<" ) {
94
95 parser.dropToken();
96
97 XMLElementSyntax xes = new XMLElementSyntax();
98
99 e = CommaExpr.make( e, xes.readXML( parser ) );
100 } else if ( parser.getName() == STARTCOMMENT ) {
101
102 parser.dropToken();
103
104 e = CommaExpr.make( e, readXMLComment( parser ) );
105 } else {
106
107 break;
108 }
109 }
110
111 return e;
112 }
113 }