|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.tinkerpop.gremlin.console |
| 20 | + |
| 21 | +import org.apache.groovy.groovysh.ParseCode |
| 22 | +import org.apache.groovy.groovysh.ParseStatus |
| 23 | +import org.apache.groovy.groovysh.Parsing |
| 24 | +import org.codehaus.groovy.control.CompilerConfiguration |
| 25 | +import org.codehaus.groovy.control.MultipleCompilationErrorsException |
| 26 | +import org.codehaus.groovy.control.messages.Message |
| 27 | +import org.codehaus.groovy.control.messages.SyntaxErrorMessage |
| 28 | + |
| 29 | +/** |
| 30 | + * A {@link Parsing} implementation that detects if a Groovy script is complete or incomplete. This implementation uses |
| 31 | + * the Groovy compiler to try to compile the script and analyzes any compilation errors to determine if the script is |
| 32 | + * incomplete. It does not evaluate the script, which prevents local execution and potential local failure. |
| 33 | + */ |
| 34 | +class RemoteParser implements Parsing { |
| 35 | + |
| 36 | + // Try to compile the script |
| 37 | + def config = new CompilerConfiguration() |
| 38 | + |
| 39 | + // Create a custom GroovyClassLoader that doesn't write class files |
| 40 | + def gcl = new GroovyClassLoader(this.class.classLoader, config) { |
| 41 | + @Override |
| 42 | + public Class defineClass(String name, byte[] bytes) { |
| 43 | + // Skip writing to disk, just define the class in memory |
| 44 | + return super.defineClass(name, bytes, 0, bytes.length) |
| 45 | + } |
| 46 | + } |
| 47 | + /** |
| 48 | + * Parse the given buffer and determine if it's a complete Groovy script. |
| 49 | + * |
| 50 | + * @param buffer The list of strings representing the script lines |
| 51 | + * @return A ParseStatus indicating if the script is complete, incomplete, or has errors |
| 52 | + */ |
| 53 | + @Override |
| 54 | + ParseStatus parse(Collection<String> buffer) { |
| 55 | + if (!buffer) { |
| 56 | + return new ParseStatus(ParseCode.COMPLETE) |
| 57 | + } |
| 58 | + |
| 59 | + // join the buffer lines into a single script |
| 60 | + def script = buffer.join('\n') |
| 61 | + |
| 62 | + try { |
| 63 | + // Parse the script without generating .class files |
| 64 | + gcl.parseClass(script) |
| 65 | + // If we get here, the script compiled successfully |
| 66 | + return new ParseStatus(ParseCode.COMPLETE) |
| 67 | + } catch (MultipleCompilationErrorsException e) { |
| 68 | + // Check if the errors indicate an incomplete script |
| 69 | + if (isIncompleteScript(e)) { |
| 70 | + return new ParseStatus(ParseCode.INCOMPLETE) |
| 71 | + } else { |
| 72 | + // Other compilation errors |
| 73 | + return new ParseStatus(ParseCode.ERROR, e) |
| 74 | + } |
| 75 | + } catch (Exception e) { |
| 76 | + // Any other exception is considered an error |
| 77 | + return new ParseStatus(ParseCode.ERROR, e) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Determine if the compilation errors indicate an incomplete script. |
| 83 | + * |
| 84 | + * @param e The compilation errors exception |
| 85 | + * @return true if the script is incomplete, false otherwise |
| 86 | + */ |
| 87 | + private boolean isIncompleteScript(MultipleCompilationErrorsException e) { |
| 88 | + def errorCollector = e.errorCollector |
| 89 | + |
| 90 | + for (Message message : errorCollector.errors) { |
| 91 | + if (message instanceof SyntaxErrorMessage) { |
| 92 | + def syntaxException = message.cause |
| 93 | + def errorMessage = syntaxException.message |
| 94 | + |
| 95 | + // Check for common indicators of incomplete scripts |
| 96 | + if (isUnexpectedEOF(errorMessage) || |
| 97 | + isUnclosedBracket(errorMessage) || |
| 98 | + isUnclosedString(errorMessage) || |
| 99 | + isUnexpectedInput(errorMessage)) { |
| 100 | + return true |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return false |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Check if the error message indicates an unexpected end of file. |
| 110 | + */ |
| 111 | + private boolean isUnexpectedEOF(String errorMessage) { |
| 112 | + errorMessage.contains('unexpected end of file') || |
| 113 | + errorMessage.contains('Unexpected EOF') |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Check if the error message indicates an unclosed bracket. |
| 118 | + */ |
| 119 | + private boolean isUnclosedBracket(String errorMessage) { |
| 120 | + errorMessage.contains("Missing '}'") || |
| 121 | + errorMessage.contains("Missing ')'") || |
| 122 | + errorMessage.contains("Missing ']'") |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Check if the error message indicates an unclosed string. |
| 127 | + */ |
| 128 | + private boolean isUnclosedString(String errorMessage) { |
| 129 | + errorMessage.contains('String literal is not terminated') || |
| 130 | + errorMessage.contains('Unterminated string literal') |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Check if the error message indicates unexpected input that might suggest |
| 135 | + * an incomplete script rather than a syntax error. |
| 136 | + */ |
| 137 | + private boolean isUnexpectedInput(String errorMessage) { |
| 138 | + // Check for unexpected input but exclude cases where closing brackets are unexpected |
| 139 | + // as those typically indicate syntax errors rather than incomplete scripts |
| 140 | + (errorMessage.contains('Unexpected input: ') || |
| 141 | + errorMessage.contains('Unexpected character: ')) && |
| 142 | + !(errorMessage.contains("Unexpected input: '}'") || |
| 143 | + errorMessage.contains("Unexpected input: ')'") || |
| 144 | + errorMessage.contains("Unexpected input: ']'")) |
| 145 | + } |
| 146 | +} |
0 commit comments