From 3040c3461e103038b2a9ceb3b3b7b50f494c8040 Mon Sep 17 00:00:00 2001 From: hvbargen <37015738+hvbargen@users.noreply.github.com> Date: Wed, 10 Mar 2021 11:36:03 +0100 Subject: [PATCH] Support for detecting excluded ranges in the top-level Input node with rules. --- pmd-plsql/etc/grammar/PldocAST.jjt | 74 +++++++++++++++++-- .../pmd/lang/plsql/ast/ASTInput.java | 21 ++++++ 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/pmd-plsql/etc/grammar/PldocAST.jjt b/pmd-plsql/etc/grammar/PldocAST.jjt index a2fea5fea5..37cf0704f6 100644 --- a/pmd-plsql/etc/grammar/PldocAST.jjt +++ b/pmd-plsql/etc/grammar/PldocAST.jjt @@ -135,6 +135,7 @@ options { NODE_CLASS = "net.sourceforge.pmd.lang.plsql.ast.AbstractPLSQLNode"; } + PARSER_BEGIN(PLSQLParser) /* Copyright (C) 2002 Albert Tumanov @@ -158,6 +159,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA package net.sourceforge.pmd.lang.plsql.ast; import java.io.*; +import java.util.List; +import java.util.ArrayList; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.SimpleCharStream; import net.sourceforge.pmd.lang.ast.TokenMgrError; @@ -241,6 +244,35 @@ public class PLSQLParser { } PARSER_END(PLSQLParser) + + + +TOKEN_MGR_DECLS : { + + public static final class Exclusion { + + public int beginLine; + public int endLine; + public String source = null; + public String reason = null; + + public Exclusion(int b, int e, String s) { + this.beginLine = b; + this.endLine = e; + this.source = s; + int i1 = s.toUpperCase().indexOf("PMD-EXCLUDE-BEGIN"); + int i2 = s.toUpperCase().indexOf("\n"); + this.reason = s.substring(i1 + 17, i2).trim(); + if (this.reason.startsWith(":")) { + this.reason = this.reason.substring(2).trim(); + } + } + } + + public List exclusions = new ArrayList(); +} + + /** * 2006-05-22 - Matthias Hendler - Added parsing of triggers and global functions/procedures * Refactored printing of custom tags into the XML/DOM. @@ -251,7 +283,10 @@ PARSER_END(PLSQLParser) /** * 2006-05-22 - Matthias Hendler - added globalBody() */ -ASTInput Input(String sourcecode) : {} +ASTInput Input(String sourcecode) : +{ + token_source.exclusions.clear(); +} { // SRT 2011-04-17 This syntax breaks the parser when fields of record.attach* are referenced (attachLibrary())* ( @@ -277,7 +312,23 @@ ASTInput Input(String sourcecode) : {} ("/")* )* - { jjtThis.setSourcecode(sourcecode); return jjtThis ; } + { jjtThis.setSourcecode(sourcecode); + List exclusions = token_source.exclusions; + if (exclusions != null) { + // System.err.println("Exclusions:"); + for (int i=0; i SPECIAL_TOKEN : { - : DEFAULT + + { + String excluded_source = matchedToken.getImage(); + exclusions.add(new Exclusion(matchedToken.getBeginLine(), matchedToken.getEndLine(), excluded_source)); + } : DEFAULT } SPECIAL_TOKEN : diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/ASTInput.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/ASTInput.java index bcfbe65f3b..15f02191d6 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/ASTInput.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/ASTInput.java @@ -34,4 +34,25 @@ public class ASTInput extends net.sourceforge.pmd.lang.plsql.ast.AbstractPLSQLNo public String getSourcecode() { return sourcecode; } + + private int excludedRangesCount = 0; + private int excludedLinesCount = 0; + + /** + Let the user know that a range of lines were excluded from parsing. + @param first First line of the exlucded line range (1-based). + @param last Last line of the exlucded line range (1-based). + */ + void addExcludedLineRange(int first, int last) { + excludedLinesCount += (last - first + 1); + excludedRangesCount += 1; + } + + public int getExcludedLinesCount() { + return excludedLinesCount; + } + + public int getExcludedRangesCount() { + return excludedRangesCount; + } }