Search in sources :

Example 71 with CheckException

use of de.be4.classicalb.core.parser.exceptions.CheckException in project probparsers by bendisposto.

the class RulesReferencesFinder method determinePackage.

private String[] determinePackage(final TPragmaIdOrString packageTerminal, final Node node) {
    String text = packageTerminal.getText();
    // "foo.bar" or foo.bar
    if ((text.startsWith("\"") && text.endsWith("\""))) {
        text = text.replaceAll("\"", "");
    }
    final String[] packageNameArray = text.split("\\.");
    final Pattern VALID_IDENTIFIER = Pattern.compile("([\\p{L}][\\p{L}\\p{N}_]*)");
    for (int i = 0; i < packageNameArray.length; i++) {
        boolean matches = VALID_IDENTIFIER.matcher(packageNameArray[i]).matches();
        if (!matches) {
            errorList.add(new CheckException(String.format("Invalid folder name '%s' in package declaration.", text), node));
        }
    }
    return packageNameArray;
}
Also used : Pattern(java.util.regex.Pattern) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) TPragmaIdOrString(de.be4.classicalb.core.parser.node.TPragmaIdOrString)

Example 72 with CheckException

use of de.be4.classicalb.core.parser.exceptions.CheckException in project probparsers by bendisposto.

the class IdentListCheck method runChecks.

/**
 * <p>
 * See class description. First {@link AAssignSubstitution} nodes are
 * checked, then the other nodes.
 * </p>
 * <p>
 * An {@link CheckException} is thrown if there are
 * {@link AAssignSubstitution} or {@link AOperationCallSubstitution} nodes
 * with illegal elements in the LHS. Otherwise the other relevant nodes are
 * checked for illegal entries in their identifier lists.
 * </p>
 * <p>
 * In both cases the erroneous nodes are collected, so that only one
 * exception is thrown for the {@link AAssignSubstitution} and
 * {@link AOperationCallSubstitution} nodes respectively one for all other
 * nodes.
 * </p>
 *
 * @param rootNode
 *            the start node of the AST
 */
public void runChecks(final Start rootNode) {
    nonIdentifiers.clear();
    /*
		 * First check all assignment nodes if the LHS only contains identifiers
		 * or functions.
		 */
    final AssignCheck assignCheck = new AssignCheck();
    rootNode.apply(assignCheck);
    final Set<Node> assignErrorNodes = assignCheck.nonIdentifiers;
    if (!assignErrorNodes.isEmpty()) {
        exceptions.add(new CheckException("Identifier or function expected", assignErrorNodes.toArray(new Node[assignErrorNodes.size()])));
    }
    /*
		 * Then check other constructs which can only contain identifiers at
		 * special places.
		 */
    rootNode.apply(this);
    if (!nonIdentifiers.isEmpty()) {
        // at least one error was found
        exceptions.add(new CheckException("Identifier expected", nonIdentifiers.toArray(new Node[nonIdentifiers.size()])));
    }
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) Node(de.be4.classicalb.core.parser.node.Node)

Example 73 with CheckException

use of de.be4.classicalb.core.parser.exceptions.CheckException in project probparsers by bendisposto.

the class PrologExceptionPrinter method printCheckException.

private static void printCheckException(final IPrologTermOutput pto, final CheckException cause, final String filename, final boolean useIndentation, final boolean lineOneOff) {
    final Node[] nodes = cause.getNodes();
    final SourcePosition startPos;
    if (nodes != null && nodes.length > 0) {
        startPos = nodes[0].getStartPos();
    } else {
        startPos = null;
    }
    printExceptionWithSourcePosition(pto, cause, filename, startPos, useIndentation, lineOneOff);
}
Also used : Node(de.be4.classicalb.core.parser.node.Node) SourcePosition(de.hhu.stups.sablecc.patch.SourcePosition)

Example 74 with CheckException

use of de.be4.classicalb.core.parser.exceptions.CheckException in project probparsers by bendisposto.

the class PrologExceptionPrinter method printBException.

public static void printBException(IPrologTermOutput pto, final BException e, boolean useIndentation, boolean lineOneOff) {
    Throwable cause = e.getCause();
    String filename = e.getFilename();
    if (cause == null) {
        printGeneralException(pto, e, filename, useIndentation, lineOneOff, true);
    } else {
        while (cause.getClass().equals(BException.class) && cause.getCause() != null) {
            BException bex = (BException) cause;
            cause = bex.getCause();
            filename = bex.getFilename();
        }
        if (cause instanceof BLexerException) {
            printBLexerException(pto, (BLexerException) cause, filename, useIndentation, lineOneOff);
        } else if (cause instanceof LexerException) {
            printLexerException(pto, (LexerException) cause, filename, useIndentation, lineOneOff);
        } else if (cause instanceof BParseException) {
            printBParseException(pto, (BParseException) cause, filename, useIndentation, lineOneOff);
        } else if (cause instanceof PreParseException) {
            printPreParseException(pto, (PreParseException) cause, filename, useIndentation, lineOneOff);
        } else if (cause instanceof CheckException) {
            printCheckException(pto, (CheckException) cause, filename, useIndentation, lineOneOff);
        } else {
            printGeneralException(pto, cause, filename, useIndentation, lineOneOff, false);
        }
    }
}
Also used : BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) BParseException(de.be4.classicalb.core.parser.exceptions.BParseException) BException(de.be4.classicalb.core.parser.exceptions.BException) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) LexerException(de.be4.classicalb.core.parser.lexer.LexerException) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException)

Example 75 with CheckException

use of de.be4.classicalb.core.parser.exceptions.CheckException in project probparsers by bendisposto.

the class RecursiveMachineLoader method lookupFile.

/**
 * Tries to find a file containing the machine with the given file name.
 *
 * @param ancestors
 *
 * @param machineName
 *            Name of the machine to include, never <code>null</code>
 * @param paths
 *
 * @return reference to a file containing the machine, may be non-existent
 *         but never <code>null</code>.
 * @throws CheckException
 *             if the file cannot be found
 */
private File lookupFile(final File parentMachineDirectory, final MachineReference machineRef, List<String> ancestors, List<String> paths) throws CheckException {
    for (final String suffix : SUFFICES) {
        try {
            final String directoryString = machineRef.getDirectoryPath() != null ? machineRef.getDirectoryPath() : parentMachineDirectory.getAbsolutePath();
            return new FileSearchPathProvider(directoryString, machineRef.getName() + suffix, paths).resolve();
        } catch (FileNotFoundException e) {
        // could not resolve the combination of prefix, machineName and
        // suffix, trying next one
        }
    }
    StringBuilder sb = new StringBuilder();
    sb.append("Machine not found: '");
    sb.append(machineRef.getName());
    sb.append("'");
    String fileNameOfErrorMachine = parsedFiles.get(ancestors.get(ancestors.size() - 1)).getName();
    sb.append(" in '").append(fileNameOfErrorMachine).append("'");
    for (int i = ancestors.size() - 2; i >= 0; i--) {
        String name = ancestors.get(i);
        String fileName = parsedFiles.get(name).getName();
        sb.append(" loaded by ").append("'").append(fileName).append("'");
    }
    throw new CheckException(sb.toString(), machineRef.getNode());
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) FileNotFoundException(java.io.FileNotFoundException) FileSearchPathProvider(de.be4.classicalb.core.parser.FileSearchPathProvider)

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)78 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)19 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)17 PExpression (de.be4.classicalb.core.parser.node.PExpression)16 Test (org.junit.Test)15 BException (de.be4.classicalb.core.parser.exceptions.BException)14 ArrayList (java.util.ArrayList)13 TPragmaIdOrString (de.be4.classicalb.core.parser.node.TPragmaIdOrString)12 Ast2String (util.Ast2String)10 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)9 Node (de.be4.classicalb.core.parser.node.Node)8 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)8 File (java.io.File)8 HashSet (java.util.HashSet)8 IOException (java.io.IOException)7 ARuleOperation (de.be4.classicalb.core.parser.node.ARuleOperation)4 PositionedNode (de.hhu.stups.sablecc.patch.PositionedNode)4 Helpers.getTreeAsString (util.Helpers.getTreeAsString)4 Type (de.be4.classicalb.core.parser.IDefinitions.Type)3 AIntegerExpression (de.be4.classicalb.core.parser.node.AIntegerExpression)3