Search in sources :

Example 1 with VisitorException

use of de.be4.classicalb.core.parser.exceptions.VisitorException 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 VisitorException

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

the class ReferencedMachines method caseAImportPackage.

@Override
public void caseAImportPackage(AImportPackage node) {
    final String[] packageArray = determinePackage(node.getPackage(), node);
    final File pathFile = getFileStartingAtRootDirectory(packageArray);
    final String path = pathFile.getAbsolutePath();
    if (pathFile.exists()) {
        if (!pathFile.isDirectory()) {
            throw new VisitorException(new CheckException(String.format("Imported package is not a directory: %s", path), node));
        }
    } else {
        throw new VisitorException(new CheckException(String.format("Imported package does not exist: %s", path), node));
    }
    if (this.pathList.contains(path)) {
        throw new VisitorException(new CheckException(String.format("Duplicate import statement: %s", node.getPackage().getText()), node));
    }
    this.pathList.add(path);
}
Also used : CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) TPragmaIdOrString(de.be4.classicalb.core.parser.node.TPragmaIdOrString) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException) File(java.io.File)

Example 3 with VisitorException

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

the class OpSubstitutions method caseAFuncOpSubstitution.

@Override
public void caseAFuncOpSubstitution(final AFuncOpSubstitution node) {
    final PExpression expression = node.getFunction();
    PExpression idExpr = null;
    LinkedList<PExpression> parameters = null;
    Type type = null;
    TIdentifierLiteral idToken = null;
    String idString = null;
    if (expression instanceof AFunctionExpression) {
        // the operation was parsed as a function expression
        final AFunctionExpression function = (AFunctionExpression) expression;
        final PExpression funcId = function.getIdentifier();
        if (funcId instanceof AIdentifierExpression) {
            final AIdentifierExpression identifier = (AIdentifierExpression) funcId;
            idString = Utils.getTIdentifierListAsString(identifier.getIdentifier());
            idToken = identifier.getIdentifier().get(0);
            type = definitions.getType(idString);
        } else {
            type = Type.NoDefinition;
        }
        idExpr = function.getIdentifier();
        parameters = new LinkedList<>(function.getParameters());
    } else if (expression instanceof AIdentifierExpression) {
        // the operation was parsed as an identifier expression
        final AIdentifierExpression identifier = (AIdentifierExpression) expression;
        idString = Utils.getTIdentifierListAsString(identifier.getIdentifier());
        idToken = identifier.getIdentifier().get(0);
        type = definitions.getType(idString);
        idExpr = expression;
        parameters = new LinkedList<>();
    } else {
        // some other expression was parsed (NOT allowed)
        throw new BParseException(null, "Expecting operation");
    }
    if (type != Type.NoDefinition && idToken != null) {
        if (type == Type.Substitution || type == Type.ExprOrSubst) {
            // create DefinitionSubstitution
            final ADefinitionSubstitution defSubst = new ADefinitionSubstitution(new TDefLiteralSubstitution(idToken.getText(), idToken.getLine(), idToken.getPos()), parameters);
            if (type == Type.ExprOrSubst) {
                // type is determined now => set to Substitution
                setTypeSubstDef(node, idString);
            }
            // transfer position information
            final PositionedNode posNode = node;
            final PositionedNode newPosNode = defSubst;
            newPosNode.setStartPos(posNode.getStartPos());
            newPosNode.setEndPos(posNode.getEndPos());
            node.replaceBy(defSubst);
            defSubst.apply(this);
        } else {
            // finding some other type here is an error!
            throw new VisitorException(new CheckException("Expecting substitution here but found definition with type '" + type + "'", node));
        }
    } else {
        // no def, no problem ;-)
        final AOpSubstitution opSubst = new AOpSubstitution(idExpr, parameters);
        opSubst.setStartPos(idExpr.getStartPos());
        opSubst.setEndPos(idExpr.getEndPos());
        node.replaceBy(opSubst);
        opSubst.apply(this);
    }
}
Also used : TDefLiteralSubstitution(de.be4.classicalb.core.parser.node.TDefLiteralSubstitution) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) BParseException(de.be4.classicalb.core.parser.exceptions.BParseException) PositionedNode(de.hhu.stups.sablecc.patch.PositionedNode) PExpression(de.be4.classicalb.core.parser.node.PExpression) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) LinkedList(java.util.LinkedList) Type(de.be4.classicalb.core.parser.IDefinitions.Type) AFunctionExpression(de.be4.classicalb.core.parser.node.AFunctionExpression) ADefinitionSubstitution(de.be4.classicalb.core.parser.node.ADefinitionSubstitution) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException) AOpSubstitution(de.be4.classicalb.core.parser.node.AOpSubstitution)

Example 4 with VisitorException

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

the class ReferencedMachines method caseAMachineHeader.

@Override
public void caseAMachineHeader(AMachineHeader node) {
    machineName = Utils.getTIdentifierListAsString(node.getName());
    final String fileNameWithoutExtension = Utils.getFileWithoutExtension(mainFile.getName());
    if (isMachineNameMustMatchFileName && !machineName.equals(fileNameWithoutExtension)) {
        CheckException ch = new CheckException(String.format("Machine name does not match the file name: '%s' vs '%s'", machineName, fileNameWithoutExtension), node);
        throw new VisitorException(ch);
    }
}
Also used : 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 5 with VisitorException

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

the class ReferencedMachines method caseAMachineReference.

/**
 * INCLUDES, EXTENDS, IMPORTS
 */
@Override
public void caseAMachineReference(AMachineReference node) {
    String name = getIdentifier(node.getMachineName());
    if (node.parent() instanceof AFileMachineReference) {
        final AFileMachineReference fileNode = (AFileMachineReference) node.parent();
        String file = fileNode.getFile().getText().replaceAll("\"", "");
        MachineReference ref;
        try {
            ref = new MachineReference(name, node, file);
            referncesTable.put(name, ref);
        } catch (CheckException e) {
            throw new VisitorException(e);
        }
    } else {
        MachineReference machineReference = new MachineReference(name, node);
        if (this.filePathTable.containsKey(name)) {
            machineReference.setDirectoryPath(filePathTable.get(name));
        }
        referncesTable.put(name, machineReference);
    }
}
Also used : AFileMachineReference(de.be4.classicalb.core.parser.node.AFileMachineReference) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) TPragmaIdOrString(de.be4.classicalb.core.parser.node.TPragmaIdOrString) VisitorException(de.be4.classicalb.core.parser.exceptions.VisitorException) AFileMachineReference(de.be4.classicalb.core.parser.node.AFileMachineReference) AMachineReference(de.be4.classicalb.core.parser.node.AMachineReference)

Aggregations

CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)9 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)9 TPragmaIdOrString (de.be4.classicalb.core.parser.node.TPragmaIdOrString)6 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)3 PExpression (de.be4.classicalb.core.parser.node.PExpression)3 Type (de.be4.classicalb.core.parser.IDefinitions.Type)2 AFileMachineReference (de.be4.classicalb.core.parser.node.AFileMachineReference)2 AFunctionExpression (de.be4.classicalb.core.parser.node.AFunctionExpression)2 AMachineReference (de.be4.classicalb.core.parser.node.AMachineReference)2 AOpSubstitution (de.be4.classicalb.core.parser.node.AOpSubstitution)2 TDefLiteralSubstitution (de.be4.classicalb.core.parser.node.TDefLiteralSubstitution)2 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)2 PositionedNode (de.hhu.stups.sablecc.patch.PositionedNode)2 File (java.io.File)2 LinkedList (java.util.LinkedList)2 BParseException (de.be4.classicalb.core.parser.exceptions.BParseException)1 VisitorIOException (de.be4.classicalb.core.parser.exceptions.VisitorIOException)1 ADefinitionSubstitution (de.be4.classicalb.core.parser.node.ADefinitionSubstitution)1 AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)1 AFileExpression (de.be4.classicalb.core.parser.node.AFileExpression)1