From 1d190605e53653b7e5085ff67cc727bde108d398 Mon Sep 17 00:00:00 2001 From: amitbitcse Date: Tue, 6 Oct 2015 11:04:22 +0530 Subject: [PATCH 1/2] Added capability in Java and JSP parser for tracking tokens. It is often useful to keep track of each node's first and last token so that input can be easily reproduced again. Ref - https://javacc.java.net/doc/JJTree.html --- .../pmd/lang/ast/AbstractNode.java | 19 +++++++++++++++++++ .../pmd/lang/ast/GenericToken.java | 5 +++++ pmd-java/etc/grammar/Java.jjt | 5 +++++ pmd-java/src/main/ant/alljavacc.xml | 6 ++++++ pmd-jsp/etc/grammar/JspParser.jjt | 8 +++++++- pmd-jsp/src/main/ant/alljavacc.xml | 6 ++++++ 6 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java index ad80a49d1f..64762d30ef 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java @@ -14,6 +14,7 @@ import javax.xml.parsers.ParserConfigurationException; import net.sourceforge.pmd.lang.ast.xpath.Attribute; import net.sourceforge.pmd.lang.ast.xpath.DocumentNavigator; import net.sourceforge.pmd.lang.dfa.DataFlowNode; +import net.sourceforge.pmd.lang.ast.GenericToken; import org.jaxen.BaseXPath; import org.jaxen.JaxenException; @@ -397,4 +398,22 @@ public abstract class AbstractNode implements Node { public void setUserData(Object userData) { this.userData = userData; } + + protected GenericToken firstToken, lastToken; + + public GenericToken jjtGetFirstToken() { + return firstToken; + } + + public void jjtSetFirstToken(GenericToken token) { + this.firstToken = token; + } + + public GenericToken jjtGetLastToken() { + return lastToken; + } + + public void jjtSetLastToken(GenericToken token) { + this.lastToken = token; + } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java new file mode 100644 index 0000000000..a76018596b --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java @@ -0,0 +1,5 @@ +package net.sourceforge.pmd.lang.ast; + +public class GenericToken { + +} diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index feddd07397..5e2b33def4 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1,4 +1,8 @@ /** + * Added capability for Tracking Tokens. + * + * Amit Kumar Prasad 10/2015 + *==================================================================== * Fix for Lambda expressions without variables. * * Andreas Dangel 11/2014 @@ -104,6 +108,7 @@ options { MULTI = true; VISITOR = true; NODE_USES_PARSER = true; + TRACK_TOKENS = true; NODE_PACKAGE="net.sourceforge.pmd.lang.java.ast"; //DEBUG_PARSER = true; diff --git a/pmd-java/src/main/ant/alljavacc.xml b/pmd-java/src/main/ant/alljavacc.xml index 5534549057..45c05dac70 100644 --- a/pmd-java/src/main/ant/alljavacc.xml +++ b/pmd-java/src/main/ant/alljavacc.xml @@ -81,6 +81,12 @@ public class]]> } ]]> + + public class Token + + diff --git a/pmd-jsp/etc/grammar/JspParser.jjt b/pmd-jsp/etc/grammar/JspParser.jjt index bb5cd931f7..d4022677d4 100644 --- a/pmd-jsp/etc/grammar/JspParser.jjt +++ b/pmd-jsp/etc/grammar/JspParser.jjt @@ -1,4 +1,9 @@ - /* JSP Parser for PMD. +/* + * Added capability for Tracking Tokens. + * + * Amit Kumar Prasad 10/2015 + *==================================================================== + * JSP Parser for PMD. * It supports supports more-or-less well written JSP files. * The JSP Document style is supported, except for inline DTD. * The JSP Page style (<% ... %>) is supported. @@ -16,6 +21,7 @@ options { MULTI=true; VISITOR=true; + TRACK_TOKENS = true; } PARSER_BEGIN(JspParser) diff --git a/pmd-jsp/src/main/ant/alljavacc.xml b/pmd-jsp/src/main/ant/alljavacc.xml index 39402a662a..da338412b9 100644 --- a/pmd-jsp/src/main/ant/alljavacc.xml +++ b/pmd-jsp/src/main/ant/alljavacc.xml @@ -61,6 +61,12 @@ public class]]> + + public class Token + + From d788b6ce74aee262b4ef365acebcda6a03f8d8c0 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 10 Oct 2015 17:37:58 +0200 Subject: [PATCH 2/2] Update changelog for pr-72 --- .../main/java/net/sourceforge/pmd/lang/ast/GenericToken.java | 3 +++ src/site/markdown/overview/changelog.md | 2 ++ 2 files changed, 5 insertions(+) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java index a76018596b..63404be451 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/GenericToken.java @@ -1,3 +1,6 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ package net.sourceforge.pmd.lang.ast; public class GenericToken { diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 766a34317e..18cf1506f4 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -10,6 +10,8 @@ **Pull Requests:** +* [#72](https://github.com/pmd/pmd/pull/72): Added capability in Java and JSP parser for tracking tokens. + **Bugfixes:** * [#1429](https://sourceforge.net/p/pmd/bugs/1429/): Java - Parse Error: Cast in return expression