Adding inner comment support for scripts

This commit is contained in:
Sergey
2017-04-11 16:34:01 -07:00
parent 071c58dbee
commit 5d1d4031e6
5 changed files with 59 additions and 15 deletions

View File

@ -15,7 +15,7 @@ PARSER_BEGIN(VfParser)
package net.sourceforge.pmd.lang.vf.ast; package net.sourceforge.pmd.lang.vf.ast;
import net.sourceforge.pmd.lang.ast.CharStream; import net.sourceforge.pmd.lang.ast.CharStream;
import net.sourceforge.pmd.lang.ast.TokenMgrError; import net.sourceforge.pmd.lang.vf.ast.TokenMgrError;
public class VfParser { public class VfParser {
@ -115,7 +115,7 @@ PARSER_END(VfParser)
<ElTagState, ElAttribTagStateSQ, ElAttribTagStateDQ, ElAttribTagStateNQ, ElInScriptState> TOKEN : <ElTagState, ElAttribTagStateSQ, ElAttribTagStateDQ, ElAttribTagStateNQ, ElInScriptState> TOKEN :
{ {
<NULL: "null" > <NULL: "null" >
| <TRUE: "true" > | <TRUE: "true" >
| <FALSE: "false" > | <FALSE: "false" >
| <LPAREN: "(" > | <LPAREN: "(" >
@ -161,22 +161,23 @@ PARSER_END(VfParser)
<ElAttribTagStateSQ> TOKEN : <ElAttribTagStateSQ> TOKEN :
{ {
<END_OF_EL_ATTRIB_SQ: (<WHITESPACES>)? <CLOSEBRACE> > : AttrValueBetweenSingleQuotesState <END_OF_EL_ATTRIB_SQ: (<WHITESPACES>)? <CLOSEBRACE> > : AttrValueBetweenSingleQuotesState
} }
<ElAttribTagStateDQ> TOKEN : <ElAttribTagStateDQ> TOKEN :
{ {
<END_OF_EL_ATTRIB_DQ: (<WHITESPACES>)? <CLOSEBRACE> > : AttrValueBetweenDoubleQuotesState <END_OF_EL_ATTRIB_DQ: (<WHITESPACES>)? <CLOSEBRACE> > : AttrValueBetweenDoubleQuotesState
} }
<ElAttribTagStateNQ> TOKEN : <ElAttribTagStateNQ> TOKEN :
{ {
<END_OF_EL_ATTRIB_NQ: (<WHITESPACES>)? <CLOSEBRACE> > : AttrValueNoQuotesState <END_OF_EL_ATTRIB_NQ: (<WHITESPACES>)? <CLOSEBRACE> > : AttrValueNoQuotesState
} }
<ElInScriptState> TOKEN : <ElInScriptState> TOKEN :
{ {
<END_OF_EL_SCRIPT: (<WHITESPACES>)? <CLOSEBRACE> > : HtmlScriptContentState <COMMENT_OPEN_SCRIPT: "/*" > : InlineCommentStateScript
| <END_OF_EL_SCRIPT: (<WHITESPACES>)? <CLOSEBRACE> > : HtmlScriptContentState
} }
<DocTypeState, DocTypeExternalIdState> TOKEN : <DocTypeState, DocTypeExternalIdState> TOKEN :
@ -231,7 +232,7 @@ PARSER_END(VfParser)
{ {
<ENDING_WHITESPACE: " " >: InTagState <ENDING_WHITESPACE: " " >: InTagState
| <EL_EXPRESSION_IN_ATTRIBUTE_NQ: "{!" (<WHITESPACES>)? > : ElAttribTagStateNQ | <EL_EXPRESSION_IN_ATTRIBUTE_NQ: "{!" (<WHITESPACES>)? > : ElAttribTagStateNQ
| <UNPARSED_TEXT_NO_WHITESPACE: ( ~["{", " "] |(["{"] ~["!"]) )+ > | <UNPARSED_TEXT_NO_WHITESPACE: ( ~["{", " "] | (["{"] ~["!"]) )+ >
} }
@ -257,6 +258,12 @@ PARSER_END(VfParser)
| < COMMENT_TEXT: (~[]) > | < COMMENT_TEXT: (~[]) >
} }
<InlineCommentStateScript> TOKEN :
{
< COMMENT_CLOSE_SCRIPT: ("*/" ) > : ElInScriptState
| < COMMENT_INNER_TEXT_SCRIPT: (~[]) >
}
<HtmlScriptContentState> TOKEN : <HtmlScriptContentState> TOKEN :
{ {
<HTML_SCRIPT_END_TAG : "</script>" > : AfterTagState <HTML_SCRIPT_END_TAG : "</script>" > : AfterTagState
@ -401,7 +408,8 @@ void ElExpression() :
void Expression() : void Expression() :
{} {}
{ {
ConditionalExpression() [ AssignmentOperator() Expression() ] ConditionalExpression() [ AssignmentOperator() Expression() ]
| InlineCommentExpression() ( ConditionalExpression() | InlineCommentExpression() )*
} }
void AssignmentOperator() #void : void AssignmentOperator() #void :
@ -481,17 +489,25 @@ void UnaryExpressionNotPlusMinus() #void :
void PrimaryExpression() #void : void PrimaryExpression() #void :
{} {}
{ {
PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )* PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )*
}
void InlineCommentExpression() :
{
StringBuffer content = new StringBuffer();
Token t;
}
{
<COMMENT_OPEN_SCRIPT> ( t = <COMMENT_INNER_TEXT_SCRIPT> { content.append(t.image); })* <COMMENT_CLOSE_SCRIPT> { jjtThis.setImage(content.toString()); }
} }
void PrimaryPrefix() #void : void PrimaryPrefix() #void :
{} {}
{ {
Literal() Literal()
| Identifier() | Identifier()
| <LPAREN> Expression() <RPAREN> | <LPAREN> Expression() <RPAREN>
| <LSQUARE> Expression() (<COMMA> Expression())* <RSQUARE> | <LSQUARE> Expression() (<COMMA> Expression())* <RSQUARE>
} }
void PrimarySuffix() #void : void PrimarySuffix() #void :

View File

@ -34,6 +34,7 @@
<!-- Ensure generated using CharStream interface debugparser="true" <!-- Ensure generated using CharStream interface debugparser="true"
debugtokenmanager="true"--> debugtokenmanager="true"-->
<javacc static="false" <javacc static="false"
usercharstream="true" usercharstream="true"
unicodeinput="true" unicodeinput="true"
javaunicodeescape="false" javaunicodeescape="false"
@ -43,7 +44,6 @@
<delete file="${target}/net/sourceforge/pmd/lang/vf/ast/Node.java" /> <delete file="${target}/net/sourceforge/pmd/lang/vf/ast/Node.java" />
<delete file="${target}/net/sourceforge/pmd/lang/vf/ast/SimpleNode.java" /> <delete file="${target}/net/sourceforge/pmd/lang/vf/ast/SimpleNode.java" />
<delete file="${target}/net/sourceforge/pmd/lang/vf/ast/CharStream.java" /> <delete file="${target}/net/sourceforge/pmd/lang/vf/ast/CharStream.java" />
<delete file="${target}/net/sourceforge/pmd/lang/vf/ast/TokenMgrError.java" />
<replace file="${target}/net/sourceforge/pmd/lang/vf/ast/VfParserTokenManager.java" <replace file="${target}/net/sourceforge/pmd/lang/vf/ast/VfParserTokenManager.java"
token="class VfParserTokenManager" token="class VfParserTokenManager"
value="class VfParserTokenManager extends net.sourceforge.pmd.lang.ast.AbstractTokenManager" /> value="class VfParserTokenManager extends net.sourceforge.pmd.lang.ast.AbstractTokenManager" />

View File

@ -89,4 +89,8 @@ public class VfParserVisitorAdapter implements VfParserVisitor {
return visit((VfNode) node, data); return visit((VfNode) node, data);
} }
@Override
public Object visit(ASTInlineCommentExpression node, Object data) {
return visit((VfNode) node, data);
}
} }

View File

@ -28,6 +28,7 @@ import net.sourceforge.pmd.lang.vf.ast.ASTElement;
import net.sourceforge.pmd.lang.vf.ast.ASTExpression; import net.sourceforge.pmd.lang.vf.ast.ASTExpression;
import net.sourceforge.pmd.lang.vf.ast.ASTHtmlScript; import net.sourceforge.pmd.lang.vf.ast.ASTHtmlScript;
import net.sourceforge.pmd.lang.vf.ast.ASTIdentifier; import net.sourceforge.pmd.lang.vf.ast.ASTIdentifier;
import net.sourceforge.pmd.lang.vf.ast.ASTInlineCommentExpression;
import net.sourceforge.pmd.lang.vf.ast.ASTLiteral; import net.sourceforge.pmd.lang.vf.ast.ASTLiteral;
import net.sourceforge.pmd.lang.vf.ast.ASTText; import net.sourceforge.pmd.lang.vf.ast.ASTText;
import net.sourceforge.pmd.lang.vf.ast.VfNode; import net.sourceforge.pmd.lang.vf.ast.VfNode;
@ -132,4 +133,7 @@ public abstract class AbstractVfRule extends AbstractRule implements VfParserVis
return visit((VfNode) node, data); return visit((VfNode) node, data);
} }
public Object visit(ASTInlineCommentExpression node, Object data) {
return visit((VfNode) node, data);
}
} }

View File

@ -168,6 +168,25 @@ public class VfDocStyleTest extends AbstractVfNodesTest {
assertEquals("Correct EL content expected!", "elInScript", id.getImage()); assertEquals("Correct EL content expected!", "elInScript", id.getImage());
} }
/**
* Test parsing of inline comment in EL.
*/
@Test
public void testInlineCommentInEL() {
Set<ASTHtmlScript> scripts = getNodes(ASTHtmlScript.class, TEST_EL_IN_HTML_SCRIPT_WITH_COMMENT);
assertEquals("One script expected!", 1, scripts.size());
ASTHtmlScript script = scripts.iterator().next();
ASTText text = script.getFirstChildOfType(ASTText.class);
assertEquals("Correct script content expected!", "vartext=", text.getImage());
ASTElExpression el = script.getFirstChildOfType(ASTElExpression.class);
List<ASTInlineCommentExpression> comments = el.findDescendantsOfType(ASTInlineCommentExpression.class);
assertEquals("Correct comment size expected!", 2, comments.size());
ASTIdentifier id = el.getFirstDescendantOfType(ASTIdentifier.class);
assertEquals("Correct EL content expected!", "elInScript", id.getImage());
}
/** /**
* Test parsing of quoted EL in HTML &lt;script&gt; element. * Test parsing of quoted EL in HTML &lt;script&gt; element.
*/ */
@ -653,8 +672,9 @@ public class VfDocStyleTest extends AbstractVfNodesTest {
private static final String TEST_ATTRIBUTE_VALUE_CONTAINING_HASH = "<tag:if something=\"#yes#\" foo=\"CREATE\"> <a href=\"#\">foo</a> </tag:if>"; private static final String TEST_ATTRIBUTE_VALUE_CONTAINING_HASH = "<tag:if something=\"#yes#\" foo=\"CREATE\"> <a href=\"#\">foo</a> </tag:if>";
private static final String TEST_HTML_SCRIPT = "<html><head><script>Script!</script></head></html>"; private static final String TEST_HTML_SCRIPT = "<html><head><script>Script!</script></head></html>";
private static final String TEST_EL_IN_HTML_SCRIPT = "<html><head><script>var text={!elInScript};</script></head></html>"; private static final String TEST_EL_IN_HTML_SCRIPT = "<html><head><script>var text={!elInScript};</script></head></html>";
private static final String TEST_EL_IN_HTML_SCRIPT_WITH_COMMENT = "<html><head><script>var text={!/*junk1*/elInScript/*junk2*/};</script></head></html>";
private static final String TEST_QUOTED_EL_IN_HTML_SCRIPT = "<html><head><script>var text='textHere{!elInScript}';</script></head></html>"; private static final String TEST_QUOTED_EL_IN_HTML_SCRIPT = "<html><head><script>var text='textHere{!elInScript}';</script></head></html>";