From 366660dac49713db4cf9bce4f5d94e052a4241ac Mon Sep 17 00:00:00 2001 From: Joachim Klein Date: Thu, 10 Dec 2015 15:51:21 +0000 Subject: [PATCH] parser.visitor.ExpressionTraverseNonNested: Helper for traversing Expressions without recursing into nested P/R/SS formulas git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@11035 bbc10eb1-c90d-0410-af57-cb519fbb1720 --- .../visitor/ExpressionTraverseNonNested.java | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 prism/src/parser/visitor/ExpressionTraverseNonNested.java diff --git a/prism/src/parser/visitor/ExpressionTraverseNonNested.java b/prism/src/parser/visitor/ExpressionTraverseNonNested.java new file mode 100644 index 00000000..0fd7f1df --- /dev/null +++ b/prism/src/parser/visitor/ExpressionTraverseNonNested.java @@ -0,0 +1,117 @@ +//============================================================================== +// +// Copyright (c) 2015- +// Authors: +// * Joachim Klein (TU Dresden) +// +//------------------------------------------------------------------------------ +// +// This file is part of PRISM. +// +// PRISM is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// PRISM is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with PRISM; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +//============================================================================== + +package parser.visitor; + +import parser.ast.ExpressionProb; +import parser.ast.ExpressionReward; +import parser.ast.ExpressionSS; +import prism.PrismLangException; + +/** + * Base class for recursively traversing an Expression, but not + * entering nested P/R/SS subexpressions. + * + * By default, will not recurse into the P/R/SS elements, but + * can be configured to allow a certain amount of nested P/R/SS + * elements before stopping. + * + *
+ * Subclasses should not override
+ * + */ +public class ExpressionTraverseNonNested extends ASTTraverse +{ + /** the current nesting level */ + private int currentNesting; + /** the maximal nesting level that still allows recursion */ + private int nestingLimit; + + /** Constructor, defaulting to "no nesting" */ + public ExpressionTraverseNonNested() + { + this(0); + } + + /** Constructor, with "stop recursion after {@code nestingLimit} nestings" */ + public ExpressionTraverseNonNested(int nestingLimit) + { + currentNesting = 0; + this.nestingLimit = 0; + } + + /** Are we still allowed to recurse? */ + private boolean inLimit() + { + return currentNesting <= nestingLimit; + } + + @Override + public Object visit(ExpressionProb e) throws PrismLangException + { + currentNesting++; + // only visit if we are still in limit + if (!inLimit()) { + currentNesting--; + return null; + } + Object rv = super.visit(e); + currentNesting--; + return rv; + } + + @Override + public Object visit(ExpressionReward e) throws PrismLangException + { + currentNesting++; + // only visit if we are still in limit + if (!inLimit()) { + currentNesting--; + return null; + } + Object rv = super.visit(e); + currentNesting--; + return rv; + } + + @Override + public Object visit(ExpressionSS e) throws PrismLangException + { + currentNesting++; + // only visit if we are still in limit + if (!inLimit()) { + currentNesting--; + return null; + } + Object rv = super.visit(e); + currentNesting--; + return rv; + } +}