⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.jjt

📁 velocity 的脚本语言的全部代码集合
💻 JJT
📖 第 1 页 / 共 3 页
字号:
        if (!inComment)        {            lparen++;            /*             * If in REFERENCE and we have seen the dot, then move             * to REFMOD2 -> Modifier()             */            if (curLexState == REFMODIFIER )                SwitchTo( REFMOD2 );        }   }}<*>MORE :{    /*     *   Note : DOLLARBANG is a duplicate of DOLLAR.  They must be identical.     */    <DOLLAR: ("\\")* "$">    {        if (! inComment)        {            /*             * if we find ourselves in REFERENCE, we need to pop down             * to end the previous ref             */            if (curLexState == REFERENCE)            {                inReference = false;                stateStackPop();            }            inReference = true;            if ( debugPrint )                System.out.print( "$  : going to " + REFERENCE );            stateStackPush();            SwitchTo(REFERENCE);        }    }|   <DOLLARBANG: ("\\")* "$" ("\\")* "!">    {        if (! inComment)        {            /*             * if we find ourselves in REFERENCE, we need to pop down             * to end the previous ref             */            if (curLexState == REFERENCE)            {                inReference = false;                stateStackPop();            }            inReference = true;            if ( debugPrint )                System.out.print( "$!  : going to " + REFERENCE );            stateStackPush();            SwitchTo(REFERENCE);        }    }|   "##"   {        if (!inComment)        {            if (curLexState == REFERENCE)            {                inReference = false;                stateStackPop();            }            inComment = true;            stateStackPush();            SwitchTo(IN_SINGLE_LINE_COMMENT);        }     }|   <"#**" ~["#"]>    {        input_stream.backup(1);        inComment = true;        stateStackPush();        SwitchTo( IN_FORMAL_COMMENT);    }|   "#*"    {        inComment=true;        stateStackPush();        SwitchTo( IN_MULTI_LINE_COMMENT );    }|   <HASH : "#" >    {        if (! inComment)        {            /*             * We can have the situation where #if($foo)$foo#end.             * We need to transition out of REFERENCE before going to DIRECTIVE.             * I don't really like this, but I can't think of a legal way             * you are going into DIRECTIVE while in REFERENCE.  -gmj             */            if (curLexState == REFERENCE || curLexState == REFMODIFIER )            {                inReference = false;                stateStackPop();            }            inDirective = true;            if ( debugPrint )                System.out.print("# :  going to " + DIRECTIVE );            stateStackPush();            SwitchTo(PRE_DIRECTIVE);        }    }}TOKEN :{    <DOUBLE_ESCAPE : "\\\\">|   <ESCAPE: "\\" >|   <TEXT: (~["$", "#", "\\"])+ >}/* ----------------------------------------------------------------------- * *   *_COMMENT Lexical tokens * *-----------------------------------------------------------------------*/<IN_SINGLE_LINE_COMMENT>TOKEN :{  <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" >  {     inComment = false;     stateStackPop();  }}<IN_FORMAL_COMMENT>TOKEN :{  <FORMAL_COMMENT: "*#" >  {    inComment = false;    stateStackPop();  }}<IN_MULTI_LINE_COMMENT>TOKEN :{  <MULTI_LINE_COMMENT: "*#" >  {    inComment = false;    stateStackPop();  }}<IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>MORE :{  < ~[] >}/* ----------------------------------------------------------------------- * *  DIRECTIVE Lexical State (some of it, anyway) * * ---------------------------------------------------------------------- */<DIRECTIVE,REFMOD2>TOKEN:{    <WHITESPACE : ([" ","\t"])+ >}<DIRECTIVE,REFMOD2>TOKEN :{//   <STRING_LITERAL: ( "\"" ( ~["\"","\n","\r"] )* "\"" ) | ( "'" ( ~["'","\n","\r"] )* "'" ) >  < STRING_LITERAL:      ("\""        (   (~["\"","\\","\n","\r"])          | ("\\"              ( ["n","t","b","r","f","\\","'","\""]              | ["0"-"7"] ( ["0"-"7"] )?              | ["0"-"3"] ["0"-"7"] ["0"-"7"]              )            )          | ( "\\" (" ")* "\n")        )*        "\""      )    |    ("\'"        (   (~["\'","\n","\r"])          | ( "\\" (" ")* "\n")        )*        "\'"      )  >    {        /*         *  - if we are in DIRECTIVE and haven't seen ( yet, then also drop out.         *      don't forget to account for the beloved yet wierd #set         *  - finally, if we are in REFMOD2 (remember : $foo.bar( ) then " is ok!         */         if( curLexState == DIRECTIVE && !inSet && lparen == 0)            stateStackPop();    }}<REFERENCE,DIRECTIVE,REFMODIFIER,REFMOD2>TOKEN:{   <TRUE: "true">|   <FALSE: "false">}<DIRECTIVE>TOKEN :{    <NEWLINE: "\n" | "\r" | "\r\n" >    {        if ( debugPrint )            System.out.println(" NEWLINE :");        stateStackPop();        if (inSet)            inSet = false;        if (inDirective)            inDirective = false;    }}<DIRECTIVE>TOKEN :{    <MINUS: "-">|   <PLUS: "+">|   <MULTIPLY: "*">|   <DIVIDE: "/">|   <MODULUS: "%">|   <LOGICAL_AND: "&&">|   <LOGICAL_OR: "||">|   <LOGICAL_LT: "<">|   <LOGICAL_LE: "<=">|   <LOGICAL_GT: ">">|   <LOGICAL_GE: ">=">|   <LOGICAL_EQUALS: "==">|   <LOGICAL_NOT_EQUALS: "!=">|   <LOGICAL_NOT: "!">|   <EQUALS: "=" >}<PRE_DIRECTIVE>TOKEN :{    <END: "end" ( ( " " | "\t" )* ( "\n" | "\r" | "\r\n" ) )? >    {        inDirective = false;        stateStackPop();    }|   <IF_DIRECTIVE: "if">    {        SwitchTo(DIRECTIVE);    }|   <ELSEIF_DIRECTIVE: "elseif">    {        SwitchTo(DIRECTIVE);    }|   <ELSE_DIRECTIVE: "else" ( ( " " | "\t" )* ( "\n" | "\r" | "\r\n" ) )?  >     {        inDirective = false;        stateStackPop();    }|   <STOP_DIRECTIVE: "stop">    {        matchedToken.kind = EOF;        fileDepth = 0;    }}<PRE_DIRECTIVE,DIRECTIVE,REFMOD2>TOKEN:{   <#DIGIT: [ "0"-"9" ] >|   <NUMBER_LITERAL: ("-")? (<DIGIT>)+ >    {        /*         * check to see if we are in set         *    ex.  #set $foo = $foo + 3         *  because we want to handle the \n after         */        if ( lparen == 0 && !inSet && curLexState != REFMOD2)        {            stateStackPop();        }    }}<PRE_DIRECTIVE,DIRECTIVE>TOKEN:{    <#LETTER: [ "a"-"z", "A" - "Z" ] >|   <#DIRECTIVE_CHAR: [ "a"-"z", "A"-"Z", "0"-"9", "_" ] >|   <WORD:  ( <LETTER> | ["_"]) (<DIRECTIVE_CHAR>)* >}/* ----------------------------------------------------------------------- * *  REFERENCE Lexical States * *  This is more than a single state, because of the  structure of *  the VTL references.  We use three states because the set of tokens *  for each state can be different. * *  $foo.bar( "arg" ) *  ^   ^   ^ *  |   |   | *  ----------- >  REFERENCE : state initiated by the '$' character.  Continues *      |   |       until end of the reference, or the . character. *      |------ >  REFMODIFIER : state switched to when the <DOT> is encountered. *          |       note that this is a switch, not a push. See notes at bottom *          |       re stateStack. *          |-- >  REFMOD2 : state switch to when the LPAREN is encountered. *                  again, this is a switch, not a push. * * ---------------------------------------------------------------------------- */<REFERENCE,REFMODIFIER,REFMOD2>TOKEN :{    <#ALPHA_CHAR: ["a"-"z", "A"-"Z"] >|   <#ALPHANUM_CHAR: [ "a"-"z", "A"-"Z", "0"-"9" ] >|   <#IDENTIFIER_CHAR: [ "a"-"z", "A"-"Z", "0"-"9", "-", "_" ] >|   <IDENTIFIER:  ( <ALPHA_CHAR> | ["_"]) (<IDENTIFIER_CHAR>)* >|   <DOT: "." <ALPHA_CHAR>>    {        /*         * push the alpha char back into the stream so the following identifier         * is complete         */        input_stream.backup(1);        /*         * and munge the <DOT> so we just get a . when we have normal text that         * looks like a ref.ident         */        matchedToken.image = ".";        if ( debugPrint )            System.out.print("DOT : switching to " + REFMODIFIER);        SwitchTo(REFMODIFIER);    }|   <LCURLY: "{">|   <RCURLY: "}">    {        stateStackPop();    }}<REFERENCE,REFMODIFIER,REFMOD2>SPECIAL_TOKEN :{    <REFERENCE_TERMINATOR: ~[] >    {        /*         * push every terminator character back into the stream         */        input_stream.backup(1);        inReference = false;        if ( debugPrint )            System.out.print("REF_TERM :");        stateStackPop();    }}<PRE_DIRECTIVE>SPECIAL_TOKEN :{    <DIRECTIVE_TERMINATOR: ~[] >    {        if ( debugPrint )            System.out.print("DIRECTIVE_TERM :");        input_stream.backup(1);        inDirective = false;        stateStackPop();    }}/** * This method is what starts the whole parsing * process. After the parsing is complete and * the template has been turned into an AST, * this method returns the root of AST which * can subsequently be traversed by a visitor * which implements the ParserVisitor interface * which is generated automatically by JavaCC */SimpleNode process() : {}{   ( Statement() )* <EOF>   { return jjtThis; }}/** * These are the types of statements that * are acceptable in Velocity templates. */void Statement() #void : {}{    IfStatement()|   StopStatement()|   LOOKAHEAD(2) Reference()|   Comment()|   SetDirective()|   EscapedDirective()|   Escape()|   Directive()|   Text()}/** *  used to separate the notion of a valid directive that has been *  escaped, versus something that looks like a directive and *  is just schmoo.  This is important to do as a separate production *  that creates a node, because we want this, in either case, to stop *  the further parsing of the Directive() tree. */void EscapedDirective() : {}{    {        Token t = null;    }    t = <ESCAPE_DIRECTIVE>    {        /*         *  churn and burn..         */        t.image = escapedDirective( t.image );    }}/** *  Used to catch and process escape sequences in grammatical constructs *  as escapes outside of VTL are just characters.  Right now we have both *  this and the EscapeDirective() construction because in the EscapeDirective() *  case, we want to suck in the #<directive> and here we don't.  We just want *  the escapes to render correctly */void Escape() : {}{    {        Token t = null;        int count = 0;        boolean control = false;    }   ( LOOKAHEAD(2)  t = <DOUBLE_ESCAPE>    {        count++;    }   )+    {        /*         * first, check to see if we have a control directive         */        switch(t.next.kind ) {            case IF_DIRECTIVE :            case ELSE_DIRECTIVE :            case ELSEIF_DIRECTIVE :            case END :            case STOP_DIRECTIVE :                control = true;                break;        }        /*         * if that failed, lets lookahead to see if we matched a PD or a VM         */        if ( isDirective( t.next.image.substring(1)))            control = true;        else if ( rsvc.isVelocimacro( t.next.image.substring(1), currentTemplateName))            control = true;        jjtThis.val = "";        for( int i = 0; i < count; i++)            jjtThis.val += ( control ? "\\" : "\\\\");    }}void Comment() : {}{    <SINGLE_LINE_COMMENT>|   <MULTI_LINE_COMMENT>|   <FORMAL_COMMENT>}void NumberLiteral() : {}{    <NUMBER_LITERAL>}void StringLiteral() : {}{    <STRING_LITERAL>}/** * This method corresponds to variable * references in Velocity templates. * The following are examples of variable * references that may be found in a * template: * * $foo * $bar * */void Identifier() : {}{    <IDENTIFIER>}void Word() : {}{    <WORD>}/** *   Supports the arguments for the Pluggable Directives

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -