All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cdc.applic.expressions.SyntacticException Maven / Gradle / Ivy

There is a newer version: 0.13.3
Show newest version
package cdc.applic.expressions;

import cdc.util.lang.Checks;

/**
 * Exception raised to indicate that an expression is syntactically invalid.
 * 

* Information on the location of the problem is embedded. *

* Error is found during parsing. * * @author Damien Carbonne */ public class SyntacticException extends ApplicException { private static final long serialVersionUID = 1L; private final Detail detail; private final String info; private final String expression; private final int beginIndex; private final int endIndex; /** * Enumeration of exception details. * * @author Damien Carbonne */ public enum Detail { /** During parsing, an invalid range is found. */ INVALID_RANGE, /** During parsing, an unexpected token is found. */ UNEXPECTED_TOKEN, /** During parsing, end of expression is reached and more tokens are expected. */ UNEXPECTED_END } public SyntacticException(Detail detail, String info, String expression, int beginIndex, int endIndex) { super(info + ": '" + expression + "'", null); Checks.isNotNull(expression, "expression"); Checks.isTrue(beginIndex >= 0, ""); this.info = info; this.detail = detail; this.expression = expression; this.beginIndex = beginIndex; this.endIndex = endIndex; } public SyntacticException(Detail detail, String info, String expression) { this(detail, info, expression, 0, expression.length()); } public SyntacticException(Detail detail, String expression) { this(detail, "Invalid expression", expression); } public String getInfo() { return info; } /** * @return The exception detail. */ public Detail getDetail() { return detail; } /** * @return The expression text. */ public String getExpression() { return expression; } /** * @return The begin index (inclusive) in {@code expression} of the error location. */ public int getBeginIndex() { return beginIndex; } /** * @return The end index (exclusive) in {@code expression} of the error location. */ public int getEndIndex() { return endIndex; } public String getSlice() { final StringBuilder builder = new StringBuilder(); final int begin = getBeginIndex(); final int end = getEndIndex() >= 0 ? getEndIndex() : getExpression().length(); for (int index = 0; index < begin; index++) { builder.append(" "); } for (int index = begin; index < end; index++) { builder.append("^"); } return builder.toString(); } public String getFullMessage() { final StringBuilder builder = new StringBuilder(); builder.append(getDetail()); builder.append(" "); builder.append(getInfo()); builder.append("\n"); builder.append(" '" + getExpression() + "'\n"); builder.append(" " + getSlice()); return builder.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy