Search in sources :

Example 1 with DefaultParserNotice

use of org.fife.ui.rsyntaxtextarea.parser.DefaultParserNotice in project knime-core by knime.

the class AbstractRuleParser method addWarningNotice.

/**
 * Adds a warning about unreachability or FALSE conditions.
 *
 * @param doc The document.
 * @param res The {@link ParseResult}.
 * @param wasCatchAllRule Whether there was a catchAll rule before.
 * @param line The line index starting from {@code 0}.
 * @param lineText The rule text.
 */
protected void addWarningNotice(final RSyntaxDocument doc, final DefaultParseResult res, final boolean wasCatchAllRule, final int line, final String lineText) {
    DefaultParserNotice notice = new DefaultParserNotice(this, wasCatchAllRule ? "There was a rule that might always match, this rule will probably never be used." : "This rule might never match.", line, doc.getTokenListForLine(line).getOffset(), lineText.length());
    notice.setLevel(ParserNotice.Level.WARNING);
    res.addNotice(notice);
}
Also used : DefaultParserNotice(org.fife.ui.rsyntaxtextarea.parser.DefaultParserNotice)

Example 2 with DefaultParserNotice

use of org.fife.ui.rsyntaxtextarea.parser.DefaultParserNotice in project knime-core by knime.

the class JSnippetParser method parse.

/**
 * {@inheritDoc}
 */
@Override
public ParseResult parse(final RSyntaxDocument doc, final String style) {
    assert m_snippet.getDocument() == doc;
    JavaSnippetCompiler compiler = new JavaSnippetCompiler(m_snippet);
    StringWriter log = new StringWriter();
    DiagnosticCollector<JavaFileObject> digsCollector = new DiagnosticCollector<>();
    CompilationTask compileTask = null;
    try {
        compileTask = compiler.getTask(log, digsCollector);
    } catch (IOException e) {
        LOGGER.error("Cannot create an compile task.", e);
        return new DefaultParseResult(this);
    }
    compileTask.call();
    DefaultParseResult parseResult = new DefaultParseResult(this);
    parseResult.setError(null);
    for (Diagnostic<? extends JavaFileObject> d : digsCollector.getDiagnostics()) {
        boolean isSnippet = m_snippet.isSnippetSource(d.getSource());
        if (isSnippet) {
            DefaultParserNotice notice = new DefaultParserNotice(this, d.getMessage(Locale.US), (int) d.getLineNumber(), (int) d.getStartPosition(), (int) (d.getEndPosition() - d.getStartPosition() + 1));
            if (d.getKind().equals(Kind.ERROR)) {
                notice.setLevel(ParserNotice.Level.ERROR);
            // LOGGER.error(d.getMessage(Locale.US));
            } else if (d.getKind().equals(Kind.WARNING)) {
                notice.setLevel(ParserNotice.Level.WARNING);
            // LOGGER.warn(d.getMessage(Locale.US));
            } else {
                notice.setLevel(ParserNotice.Level.INFO);
            // LOGGER.debug(d.getMessage(Locale.US));
            }
            parseResult.addNotice(notice);
        }
    }
    return parseResult;
}
Also used : JavaFileObject(javax.tools.JavaFileObject) DefaultParseResult(org.fife.ui.rsyntaxtextarea.parser.DefaultParseResult) StringWriter(java.io.StringWriter) DiagnosticCollector(javax.tools.DiagnosticCollector) DefaultParserNotice(org.fife.ui.rsyntaxtextarea.parser.DefaultParserNotice) IOException(java.io.IOException) JavaSnippetCompiler(org.knime.base.node.jsnippet.util.JavaSnippetCompiler) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 3 with DefaultParserNotice

use of org.fife.ui.rsyntaxtextarea.parser.DefaultParserNotice in project knime-core by knime.

the class AbstractRuleParser method parse.

/**
 * {@inheritDoc}
 */
@Override
public ParseResult parse(final RSyntaxDocument doc, final String style) {
    if (isNotApplicable(style)) {
        return new DefaultParseResult(this);
    }
    DefaultParseResult res = new DefaultParseResult(this);
    Element rootElement = doc.getDefaultRootElement();
    boolean wasCatchAllRule = false;
    for (int line = 0; line < rootElement.getElementCount(); ++line) {
        String lastString = null;
        StringBuilder sb = new StringBuilder();
        // Go through the tokens
        Token token = doc.getTokenListForLine(line);
        while (token != null && token.isPaintable()) {
            String lexeme = token.getLexeme();
            // If it is an operator, make it a reserved word.
            if (getOperators().contains(lexeme)) {
                token.setType(TokenTypes.RESERVED_WORD);
                token.setLanguageIndex(0);
            }
            sb.append(lexeme);
            if (lexeme.length() > 0 && lexeme.charAt(0) == '"' && lexeme.charAt(lexeme.length() - 1) == '"') {
                lastString = lexeme;
            }
            token = token.getNextToken();
        }
        try {
            String lineText = sb.toString();
            // Check for potential error in outcomes.
            if (lineText.trim().endsWith("\"") && m_warnOnColRefsInStrings) {
                String lastContent = lastString != null && lastString.length() > 2 ? lastString.substring(1, lastString.length() - 1) : lastString;
                // ENH better check
                if (lastContent != null && lastContent.endsWith("$") && lastContent.charAt(0) == '$') {
                    DefaultParserNotice notice = new DefaultParserNotice(this, "You might be referring to a column or flow variable, although no String " + "interpolation is implemented, you might want to " + "remove the quotes around the reference.", line, doc.getTokenListForLine(line).getOffset() + lineText.lastIndexOf(lastContent), lastContent.length());
                    notice.setLevel(ParserNotice.Level.WARNING);
                    res.addNotice(notice);
                }
            }
            if (!lineText.isEmpty()) {
                wasCatchAllRule |= parseAndWarn(doc, res, wasCatchAllRule, line, lineText);
            }
        } catch (ParseException e) {
            DefaultParserNotice notice;
            if (e.getErrorOffset() >= sb.length()) {
                notice = new DefaultParserNotice(this, e.getMessage(), line);
            } else {
                notice = new DefaultParserNotice(this, e.getMessage(), line, doc.getTokenListForLine(line).getOffset() + e.getErrorOffset(), sb.length() - e.getErrorOffset());
            }
            res.addNotice(notice);
        }
    }
    return res;
}
Also used : DefaultParseResult(org.fife.ui.rsyntaxtextarea.parser.DefaultParseResult) Element(javax.swing.text.Element) Token(org.fife.ui.rsyntaxtextarea.Token) DefaultParserNotice(org.fife.ui.rsyntaxtextarea.parser.DefaultParserNotice) ParseException(java.text.ParseException)

Aggregations

DefaultParserNotice (org.fife.ui.rsyntaxtextarea.parser.DefaultParserNotice)3 DefaultParseResult (org.fife.ui.rsyntaxtextarea.parser.DefaultParseResult)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ParseException (java.text.ParseException)1 Element (javax.swing.text.Element)1 DiagnosticCollector (javax.tools.DiagnosticCollector)1 CompilationTask (javax.tools.JavaCompiler.CompilationTask)1 JavaFileObject (javax.tools.JavaFileObject)1 Token (org.fife.ui.rsyntaxtextarea.Token)1 JavaSnippetCompiler (org.knime.base.node.jsnippet.util.JavaSnippetCompiler)1