use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.
the class AntlrParserPlugin method transformCSTIntoAST.
protected void transformCSTIntoAST(SourceUnit sourceUnit, Reader reader, SourceBuffer sourceBuffer) throws CompilationFailedException {
ast = null;
setController(sourceUnit);
// TODO find a way to inject any GroovyLexer/GroovyRecognizer
UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(reader, sourceBuffer);
UnicodeLexerSharedInputState inputState = new UnicodeLexerSharedInputState(unicodeReader);
GroovyLexer lexer = new GroovyLexer(inputState);
unicodeReader.setLexer(lexer);
GroovyRecognizer parser = GroovyRecognizer.make(lexer);
parser.setSourceBuffer(sourceBuffer);
tokenNames = parser.getTokenNames();
parser.setFilename(sourceUnit.getName());
// start parsing at the compilationUnit rule
try {
parser.compilationUnit();
} catch (TokenStreamRecognitionException tsre) {
RecognitionException e = tsre.recog;
SyntaxException se = new SyntaxException(e.getMessage(), e, e.getLine(), e.getColumn());
se.setFatal(true);
sourceUnit.addError(se);
} catch (RecognitionException e) {
SyntaxException se = new SyntaxException(e.getMessage(), e, e.getLine(), e.getColumn());
se.setFatal(true);
sourceUnit.addError(se);
} catch (TokenStreamException e) {
sourceUnit.addException(e);
}
ast = parser.getAST();
}
use of org.codehaus.groovy.syntax.SyntaxException in project OpenAM by OpenRock.
the class StandardScriptValidator method getScriptErrors.
/**
* Convert from ScriptException to ScriptError.
*
* @param script the script that was validated.
* @param se the error thrown by the validation task.
* @return the converted script error.
*/
private List<ScriptError> getScriptErrors(ScriptObject script, ScriptException se) {
final List<ScriptError> scriptErrorList = new ArrayList<ScriptError>();
final Throwable cause = se.getCause();
// with useful information so we have to retrieve it from the cause exception.
if (cause instanceof EvaluatorException) {
final EvaluatorException ee = (EvaluatorException) cause;
final ScriptError error = new ScriptError();
error.setScriptName(script.getName());
error.setMessage(ee.details());
error.setLineNumber(ee.lineNumber());
error.setColumnNumber(ee.columnNumber());
scriptErrorList.add(error);
} else if (cause instanceof MultipleCompilationErrorsException) {
ErrorCollector errorCollector = ((MultipleCompilationErrorsException) cause).getErrorCollector();
for (int i = 0; i < errorCollector.getErrorCount(); i++) {
final SyntaxException syntaxException = errorCollector.getSyntaxError(i);
final ScriptError error = new ScriptError();
error.setScriptName(script.getName());
error.setMessage(syntaxException.getOriginalMessage());
error.setLineNumber(syntaxException.getLine());
error.setColumnNumber(syntaxException.getStartColumn());
scriptErrorList.add(error);
}
} else {
final ScriptError error = new ScriptError();
error.setScriptName(script.getName());
error.setMessage(se.getMessage());
error.setLineNumber(se.getLineNumber());
error.setColumnNumber(se.getColumnNumber());
scriptErrorList.add(error);
}
return scriptErrorList;
}
use of org.codehaus.groovy.syntax.SyntaxException in project intellij-community by JetBrains.
the class ScriptSupport method checkValidScript.
public static String checkValidScript(String scriptText) {
try {
final File scriptFile = new File(scriptText);
final GroovyShell shell = new GroovyShell();
final Script script = scriptFile.exists() ? shell.parse(scriptFile) : shell.parse(scriptText);
return null;
} catch (IOException e) {
return e.getMessage();
} catch (MultipleCompilationErrorsException e) {
final ErrorCollector errorCollector = e.getErrorCollector();
final List<Message> errors = errorCollector.getErrors();
for (Message error : errors) {
if (error instanceof SyntaxErrorMessage) {
final SyntaxErrorMessage errorMessage = (SyntaxErrorMessage) error;
final SyntaxException cause = errorMessage.getCause();
return cause.getMessage();
}
}
return e.getMessage();
} catch (CompilationFailedException ex) {
return ex.getLocalizedMessage();
}
}
use of org.codehaus.groovy.syntax.SyntaxException in project groovy by apache.
the class BinaryExpressionHelper method eval.
public void eval(BinaryExpression expression) {
switch(expression.getOperation().getType()) {
case // = assignment
EQUAL:
evaluateEqual(expression, false);
break;
case // ==
COMPARE_EQUAL:
evaluateCompareExpression(compareEqualMethod, expression);
break;
case COMPARE_NOT_EQUAL:
evaluateCompareExpression(compareNotEqualMethod, expression);
break;
case COMPARE_TO:
evaluateCompareTo(expression);
break;
case COMPARE_GREATER_THAN:
evaluateCompareExpression(compareGreaterThanMethod, expression);
break;
case COMPARE_GREATER_THAN_EQUAL:
evaluateCompareExpression(compareGreaterThanEqualMethod, expression);
break;
case COMPARE_LESS_THAN:
evaluateCompareExpression(compareLessThanMethod, expression);
break;
case COMPARE_LESS_THAN_EQUAL:
evaluateCompareExpression(compareLessThanEqualMethod, expression);
break;
case LOGICAL_AND:
evaluateLogicalAndExpression(expression);
break;
case LOGICAL_OR:
evaluateLogicalOrExpression(expression);
break;
case BITWISE_AND:
evaluateBinaryExpression("and", expression);
break;
case BITWISE_AND_EQUAL:
evaluateBinaryExpressionWithAssignment("and", expression);
break;
case BITWISE_OR:
evaluateBinaryExpression("or", expression);
break;
case BITWISE_OR_EQUAL:
evaluateBinaryExpressionWithAssignment("or", expression);
break;
case BITWISE_XOR:
evaluateBinaryExpression("xor", expression);
break;
case BITWISE_XOR_EQUAL:
evaluateBinaryExpressionWithAssignment("xor", expression);
break;
case PLUS:
evaluateBinaryExpression("plus", expression);
break;
case PLUS_EQUAL:
evaluateBinaryExpressionWithAssignment("plus", expression);
break;
case MINUS:
evaluateBinaryExpression("minus", expression);
break;
case MINUS_EQUAL:
evaluateBinaryExpressionWithAssignment("minus", expression);
break;
case MULTIPLY:
evaluateBinaryExpression("multiply", expression);
break;
case MULTIPLY_EQUAL:
evaluateBinaryExpressionWithAssignment("multiply", expression);
break;
case DIVIDE:
evaluateBinaryExpression("div", expression);
break;
case DIVIDE_EQUAL:
//SPG don't use divide since BigInteger implements directly
//and we want to dispatch through DefaultGroovyMethods to get a BigDecimal result
evaluateBinaryExpressionWithAssignment("div", expression);
break;
case INTDIV:
evaluateBinaryExpression("intdiv", expression);
break;
case INTDIV_EQUAL:
evaluateBinaryExpressionWithAssignment("intdiv", expression);
break;
case MOD:
evaluateBinaryExpression("mod", expression);
break;
case MOD_EQUAL:
evaluateBinaryExpressionWithAssignment("mod", expression);
break;
case POWER:
evaluateBinaryExpression("power", expression);
break;
case POWER_EQUAL:
evaluateBinaryExpressionWithAssignment("power", expression);
break;
case LEFT_SHIFT:
evaluateBinaryExpression("leftShift", expression);
break;
case LEFT_SHIFT_EQUAL:
evaluateBinaryExpressionWithAssignment("leftShift", expression);
break;
case RIGHT_SHIFT:
evaluateBinaryExpression("rightShift", expression);
break;
case RIGHT_SHIFT_EQUAL:
evaluateBinaryExpressionWithAssignment("rightShift", expression);
break;
case RIGHT_SHIFT_UNSIGNED:
evaluateBinaryExpression("rightShiftUnsigned", expression);
break;
case RIGHT_SHIFT_UNSIGNED_EQUAL:
evaluateBinaryExpressionWithAssignment("rightShiftUnsigned", expression);
break;
case KEYWORD_INSTANCEOF:
evaluateInstanceof(expression);
break;
case FIND_REGEX:
evaluateCompareExpression(findRegexMethod, expression);
break;
case MATCH_REGEX:
evaluateCompareExpression(matchRegexMethod, expression);
break;
case LEFT_SQUARE_BRACKET:
if (controller.getCompileStack().isLHS()) {
evaluateEqual(expression, false);
} else {
evaluateBinaryExpression("getAt", expression);
}
break;
case KEYWORD_IN:
evaluateCompareExpression(isCaseMethod, expression);
break;
case COMPARE_IDENTICAL:
case COMPARE_NOT_IDENTICAL:
Token op = expression.getOperation();
Throwable cause = new SyntaxException("Operator " + op + " not supported", op.getStartLine(), op.getStartColumn(), op.getStartLine(), op.getStartColumn() + 3);
throw new GroovyRuntimeException(cause);
default:
throw new GroovyBugError("Operation: " + expression.getOperation() + " not supported");
}
}
use of org.codehaus.groovy.syntax.SyntaxException in project workflow-cps-plugin by jenkinsci.
the class CpsFlowDefinitionValidator method toCheckStatus.
public static List<CheckStatus> toCheckStatus(CompilationFailedException x) {
List<CheckStatus> errors = new ArrayList<CheckStatus>();
if (x instanceof MultipleCompilationErrorsException) {
for (Object o : ((MultipleCompilationErrorsException) x).getErrorCollector().getErrors()) {
if (o instanceof SyntaxErrorMessage) {
SyntaxException cause = ((SyntaxErrorMessage) o).getCause();
CheckStatus st = new CheckStatus(cause.getOriginalMessage(), FAIL_KEY);
st.setLine(cause.getLine());
st.setColumn(cause.getStartColumn());
errors.add(st);
}
}
}
if (errors.isEmpty()) {
// Fallback to simple message
CheckStatus st = new CheckStatus(x.getMessage(), FAIL_KEY);
st.setLine(1);
st.setColumn(0);
errors.add(st);
}
return errors;
}
Aggregations