Search in sources :

Example 1 with TPragmaIdOrString

use of de.be4.classicalb.core.parser.node.TPragmaIdOrString in project probparsers by bendisposto.

the class ReferencedMachines 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) {
            throw new VisitorException(new CheckException("Invalid package pragma: " + 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) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException)

Example 2 with TPragmaIdOrString

use of de.be4.classicalb.core.parser.node.TPragmaIdOrString in project probparsers by bendisposto.

the class ReferencedMachines method determineRootDirectory.

private void determineRootDirectory(final TPragmaIdOrString packageTerminal, final Node node) {
    final String text = packageTerminal.getText();
    if ((text.startsWith("\"") && text.endsWith("\""))) {
        this.packageName = text.replaceAll("\"", "");
    } else {
        this.packageName = text;
    }
    final String[] packageNameArray = determinePackage(packageTerminal, node);
    File dir;
    try {
        dir = mainFile.getCanonicalFile();
    } catch (IOException e) {
        throw new VisitorIOException(e);
    }
    for (int i = packageNameArray.length - 1; i >= 0; i--) {
        final String name1 = packageNameArray[i];
        dir = dir.getParentFile();
        final String name2 = dir.getName();
        if (!name1.equals(name2)) {
            throw new VisitorException(new CheckException(String.format("Package declaration '%s' does not match the folder structure: %s vs %s", this.packageName, name1, name2), node));
        }
    }
    rootDirectory = dir.getParentFile();
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) TPragmaIdOrString(de.be4.classicalb.core.parser.node.TPragmaIdOrString) IOException(java.io.IOException) VisitorIOException(de.be4.classicalb.core.parser.exceptions.VisitorIOException) VisitorIOException(de.be4.classicalb.core.parser.exceptions.VisitorIOException) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException) File(java.io.File)

Example 3 with TPragmaIdOrString

use of de.be4.classicalb.core.parser.node.TPragmaIdOrString in project probparsers by bendisposto.

the class RulesReferencesFinder method determineRootDirectory.

private void determineRootDirectory(final TPragmaIdOrString packageTerminal, final Node node) {
    final String text = packageTerminal.getText();
    if ((text.startsWith("\"") && text.endsWith("\""))) {
        this.packageName = text.replaceAll("\"", "");
    } else {
        this.packageName = text;
    }
    final String[] packageNameArray = determinePackage(packageTerminal, node);
    File dir = null;
    try {
        dir = mainFile.getCanonicalFile();
    } catch (IOException e) {
        errorList.add(new CheckException(e.getMessage(), (Node) null, e));
        return;
    }
    for (int i = packageNameArray.length - 1; i >= 0; i--) {
        final String name1 = packageNameArray[i];
        dir = dir.getParentFile();
        final String name2 = dir.getName();
        if (!name1.equals(name2)) {
            errorList.add(new CheckException(String.format("Package declaration '%s' does not match the folder structure: '%s' vs '%s'", this.packageName, name1, name2), node));
        }
    }
    rootDirectory = dir.getParentFile();
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) TPragmaIdOrString(de.be4.classicalb.core.parser.node.TPragmaIdOrString) IOException(java.io.IOException) File(java.io.File)

Example 4 with TPragmaIdOrString

use of de.be4.classicalb.core.parser.node.TPragmaIdOrString 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)

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)4 TPragmaIdOrString (de.be4.classicalb.core.parser.node.TPragmaIdOrString)4 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)2 File (java.io.File)2 IOException (java.io.IOException)2 Pattern (java.util.regex.Pattern)2 VisitorIOException (de.be4.classicalb.core.parser.exceptions.VisitorIOException)1