You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ge...@apache.org on 2002/03/25 01:33:52 UTC

cvs commit: jakarta-velocity/src/java/org/apache/velocity/runtime/parser Parser.jjt

geirm       02/03/24 16:33:52

  Modified:    src/java/org/apache/velocity/runtime/parser Parser.jjt
  Log:
  Fixes for serveral bugs (7383, 7384, 7385)
  
  Revision  Changes    Path
  1.73      +104 -20   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.72
  retrieving revision 1.73
  diff -u -r1.72 -r1.73
  --- Parser.jjt	23 Mar 2002 13:38:32 -0000	1.72
  +++ Parser.jjt	25 Mar 2002 00:33:52 -0000	1.73
  @@ -1,7 +1,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -23,7 +23,7 @@
    *    Alternately, this acknowlegement may appear in the software itself,
    *    if and wherever such third-party acknowlegements normally appear.
    *
  - * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  + * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
    *    Foundation" must not be used to endorse or promote products derived
    *    from this software without prior written permission. For written
    *    permission, please contact apache@apache.org.
  @@ -121,6 +121,7 @@
   import org.apache.velocity.runtime.parser.node.*;
   import org.apache.velocity.runtime.directive.Directive;
   import org.apache.velocity.runtime.directive.Macro;
  +import org.apache.velocity.runtime.directive.MacroParseException;
   import org.apache.velocity.util.StringUtils;
   
   /**
  @@ -134,7 +135,7 @@
    *
    * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  - * @version $Id: Parser.jjt,v 1.72 2002/03/23 13:38:32 geirm Exp $
  + * @version $Id: Parser.jjt,v 1.73 2002/03/25 00:33:52 geirm Exp $
   */
   public class Parser
   {
  @@ -216,6 +217,15 @@
                */
               sn = process();
           }
  +        catch (MacroParseException mee)
  +        {
  +            /*
  +             *  thrown by the Macro class when something is amiss in the
  +             *  Macro specification
  +             */
  +            rsvc.error ("Parser Error:  #macro() : " + templateName + " : " + StringUtils.stackTrace(mee));
  +            throw new ParseException(mee.getMessage());
  +        }
           catch (ParseException pe)
           {
               rsvc.error ("Parser Exception: " + templateName + " : " + StringUtils.stackTrace(pe));
  @@ -415,7 +425,6 @@
           return;
       }
   
  -
       /**
        *  handles the dropdown logic when encountering a RPAREN
        */
  @@ -1158,17 +1167,37 @@
    *   easily reconstruct a macro body from the token stream
    *   See Directive()
    */
  -void DirectiveArg() #void : {}
  +int DirectiveArg() #void : {}
   {
       Reference()
  +    {
  +        return ParserTreeConstants.JJTREFERENCE;
  +    }
   |   Word()
  +    {
  +        return ParserTreeConstants.JJTWORD;
  +    }
   |   StringLiteral()
  +    {
  +        return ParserTreeConstants.JJTSTRINGLITERAL;
  +    }
   |   NumberLiteral()
  -|   LOOKAHEAD(  <LBRACKET> [<WHITESPACE>]    ( Reference() | NumberLiteral())     [<WHITESPACE>] <DOUBLEDOT> ) IntegerRange()
  +|   LOOKAHEAD(  <LBRACKET> [<WHITESPACE>] ( Reference() | NumberLiteral())     [<WHITESPACE>] <DOUBLEDOT> ) IntegerRange()
  +    {
  +        return ParserTreeConstants.JJTINTEGERRANGE;
  +    }
   |   ObjectArray()
  +    {
  +        return ParserTreeConstants.JJTOBJECTARRAY;
  +    }
   |   True()
  +    {
  +        return ParserTreeConstants.JJTTRUE;
  +    }
   |   False()
  -|   <WHITESPACE>
  +    {
  +        return ParserTreeConstants.JJTFALSE;
  +    }
   }
   
   /**
  @@ -1178,8 +1207,11 @@
   SimpleNode Directive() :
   {
       Token t = null;
  +    int argType;
  +    int argPos = 0;
       Directive d;
       int directiveType;
  +    boolean isVM = false;
       boolean doItNow = false;
   }
   {
  @@ -1191,7 +1223,7 @@
       {
           String directiveName = t.image.substring(1);
   
  -        d = (Directive) directives.get( directiveName );
  +        d = (Directive) directives.get(directiveName);
   
           /*
            *  Velocimacro support : if the directive is macro directive
  @@ -1199,7 +1231,7 @@
            *   right then. (So available if used w/in the current template )
            */
   
  -        if ( directiveName.equals("macro"))
  +        if (directiveName.equals("macro"))
           {
                doItNow = true;
           }
  @@ -1209,23 +1241,24 @@
            * about parser tokens
            */
   
  -        jjtThis.setDirectiveName( directiveName );
  +        jjtThis.setDirectiveName(directiveName);
   
           if ( d == null)
           {
               /*
                *  if null, then not a real directive, but maybe a Velocimacro
  -	         */
  +             */
   
  -            //d  =  (Directive) rsvc.getVelocimacro( directiveName, currentTemplateName );
  +            isVM = rsvc.isVelocimacro(directiveName, currentTemplateName);
   
  -            if ( !rsvc.isVelocimacro( directiveName, currentTemplateName ) )
  +            if (!isVM)
               {
                   token_source.stateStackPop();
                   token_source.inDirective = false;
                   return jjtThis;
               }
   
  +
               /*
                *  Currently, all VMs are LINE directives
                */
  @@ -1242,16 +1275,66 @@
            */
   
           token_source.SwitchTo(DIRECTIVE);
  +
  +        argPos = 0;
       }
   
       /*
        *  if this is indeed a token, match the #foo ( arg ) pattern
        */
   
  -    [<WHITESPACE>] <LPAREN> (  DirectiveArg()  )* <RPAREN>
  +    [<WHITESPACE>] <LPAREN>  ( LOOKAHEAD(2) [<WHITESPACE>]
  +
  +            argType = DirectiveArg()
  +            {
  +                if (argType == ParserTreeConstants.JJTWORD)
  +                {
  +                    if (doItNow && argPos == 0)
  +                    {
  +                        /* if a VM and it's the 0th arg... ok */
  +                        ;
  +                    }
  +                    else if( t.image.equals("#foreach") && argPos == 1)
  +                    {
  +                        /* if a foreach and it's the 2nd arg ok */
  +                        ;
  +                    }
  +                    else
  +                    {
  +                        throw new MacroParseException("Invalid arg #"
  +                            + argPos + " in "
  +                            + (isVM ? "VM " : "directive " )
  +                            + t.image
  +                            + " at line " + t.beginLine + ", column "
  +                            + t.beginColumn
  +                            + " in template " + currentTemplateName);
  +                    }
  +                }
  +                else
  +                {
  +                    if (doItNow && argPos == 0)
  +                    {
  +                        /* if a VM and it's the 0th arg, not ok */
  +
  +                        throw new MacroParseException("Invalid first arg "
  +                            + " in #macro() directive - must be a"
  +                            + " word token (no \' or \" surrounding)"
  +                            + " at line " + t.beginLine + ", column "
  +                            + t.beginColumn
  +                            + " in template " + currentTemplateName);
  +                    }
  +                }
  +
  +                argPos++;
  +            }
  +
  +    )* [<WHITESPACE>] <RPAREN>
       {
  -        if ( directiveType  == Directive.LINE)
  +
  +        if (directiveType  == Directive.LINE)
  +        {
               return jjtThis;
  +        }
       }
   
       /*
  @@ -1269,9 +1352,9 @@
            *     we don't have to worry about forward references and such...
            */
   
  -        if ( doItNow )
  +        if (doItNow)
           {
  -            Macro.processAndRegister( rsvc, jjtThis, currentTemplateName );
  +            Macro.processAndRegister(rsvc, jjtThis, currentTemplateName);
           }
   
           /*
  @@ -1417,7 +1500,7 @@
   {
       <SET_DIRECTIVE>
       [ LOOKAHEAD(2) <WHITESPACE> ]
  -    ( <LPAREN> Expression() <RPAREN>
  +    ( <LPAREN>  [<WHITESPACE>] Reference() [<WHITESPACE>] <EQUALS>  Expression() <RPAREN>
       {
           /*
            * ensure that inSet is false.  Leads to some amusing bugs...
  @@ -1448,8 +1531,9 @@
   
   void Expression() : {}
   {
  -    LOOKAHEAD( PrimaryExpression() <EQUALS>  ) Assignment()
  -|   ConditionalOrExpression()
  +//    LOOKAHEAD( PrimaryExpression() <EQUALS>  ) Assignment()
  +//|
  +ConditionalOrExpression()
   }
   
   void Assignment() #Assignment(2) : {}
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>