geirm 00/11/05 11:20:21
Modified: src/java/org/apache/velocity/runtime/parser Parser.jjt
Log:
More precision in how whitespace is handled, and a change or two :
1) the #end will now swallow whitespace between it and a trailing \n if it has one.
2) #set will eat preceeding whitespace -> #set renders nothing to the output at all,
but it has to be newline terminated.
3) Added \t to the notion of whitespace (master of the obvious, eh?)
Will post a summary of parser stuff to the lists later.
Revision Changes Path
1.22 +42 -14
jakarta-velocity/src/java/org/apache/velocity/runtime/parser/Parser.jjt
Index: Parser.jjt
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/Parser.jjt,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- Parser.jjt 2000/11/05 16:28:18 1.21
+++ Parser.jjt 2000/11/05 19:20:19 1.22
@@ -127,7 +127,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
- * @version $Id: Parser.jjt,v 1.21 2000/11/05 16:28:18 geirm Exp $
+ * @version $Id: Parser.jjt,v 1.22 2000/11/05 19:20:19 geirm Exp $
*/
public class Parser
{
@@ -477,12 +477,15 @@
}
/*
- * we never will see a ) in anything but DIRECTIVE and REFMOD2. Each have their
own
+ * we never will see a ')' in anything but DIRECTIVE and REFMOD2. Each have their
own
*/
<DIRECTIVE>
TOKEN:
{
- <RPAREN: ")" ((" ")* ( "\n" | "\r" | "\r\n" ))?>
+ /*
+ * We will eat any whitespace upto and including a newline for directives
+ */
+ <RPAREN: ")" ( ( " " | "\t" )* ( "\n" | "\r" | "\r\n" ))?>
{
RPARENHandler();
}
@@ -540,6 +543,30 @@
}
+
+/*
+ * needed because #set is so wacky in it's desired behavior. We want set to eat
any preceeding whitespace
+ * so it is invisible in formatting. (As it should be.) If this works well, I am
going to chuck the whole MORE:
+ * token abomination.
+ */
+TOKEN:
+{
+ <SET_DIRECTIVE: (" "|"\t")* "#set" >
+ {
+ if (! inComment)
+ {
+ inDirective = true;
+
+ if ( bDebugPrint_ )
+ System.out.print("#set : going to " + DIRECTIVE );
+
+ stateStackPush();
+ inSet = true;
+ SwitchTo(DIRECTIVE);
+ }
+ }
+}
+
<*>
MORE :
{
@@ -750,7 +777,7 @@
<PRE_DIRECTIVE>
TOKEN :
{
- <END: "end" ( "\n" | "\r" | "\r\n" )?>
+ <END: "end" ( ( " " | "\t" )* ( "\n" | "\r" | "\r\n" ) )? >
{
inDirective = false;
stateStackPop();
@@ -778,11 +805,11 @@
stateStackPop();
}
-| <SET_DIRECTIVE: "set" >
- {
- inSet = true;
- SwitchTo(DIRECTIVE);
- }
+//| <SET_DIRECTIVE: (" "|"\t")* "set" >
+// {
+// inSet = true;
+// SwitchTo(DIRECTIVE);
+// }
| <STOP_DIRECTIVE: "stop">
{
@@ -1150,7 +1177,7 @@
void SetDirective() : {}
{
- ( <SET_DIRECTIVE> Expression() [<NEWLINE>] )
+ (<SET_DIRECTIVE> Expression() [<NEWLINE>] )
}
/**
@@ -1336,9 +1363,11 @@
What You Expect
---------------
The recent versions of the parser are trying to support precise output to
support general template use.
- So the #set statment no longer renders to a \n in the output, nor do the
control statements. So if you
- had a template
-
+ The directives do not render trailing whitespace and newlines if followed by a
newline. They will render preceeding whitespace.
+ The only exception is #set, which also eats preceeding whitespace.
+
+ So, with a template :
+
------
#set $foo="foo"
#if($foo)
@@ -1351,7 +1380,6 @@
------
$foo = foo
------
-
*/