Search in sources :

Example 21 with BException

use of de.be4.classicalb.core.parser.exceptions.BException in project prob2 by bendisposto.

the class ModelGenerator method addComponent.

private ModelModifier addComponent(ModelModifier modelM, String name, String componentDesc, Map<String, String> components) throws BException {
    EventBParser parser = new EventBParser();
    Start ast = parser.parse(componentDesc, false);
    ReferenceExtractor e = new ReferenceExtractor();
    ast.apply(e);
    if (e.isContext()) {
        for (String s : e.getExtends()) {
            String fileN = s + ".ctx";
            if (modelM.getModel().getComponent(fileN) == null) {
                if (components.get(fileN) == null) {
                    throw new IllegalArgumentException("no component description for context " + s);
                }
                modelM = addComponent(modelM, s, components.get(fileN), components);
            }
        }
        modelM = addComponent(modelM, ast);
    } else if (e.isMachine()) {
        for (String s : e.getSees()) {
            String fileN = s + ".ctx";
            if (modelM.getModel().getComponent(fileN) == null) {
                if (components.get(fileN) == null) {
                    throw new IllegalArgumentException("no component description for context " + s);
                }
                modelM = addComponent(modelM, s, components.get(fileN), components);
            }
        }
        for (String s : e.getRefines()) {
            String fileN = s + ".emch";
            if (modelM.getModel().getComponent(fileN) == null) {
                if (components.get(fileN) == null) {
                    throw new IllegalArgumentException("no component description for machine " + s);
                }
                modelM = addComponent(modelM, s, components.get(fileN), components);
            }
        }
        modelM = addComponent(modelM, ast);
    }
    return modelM;
}
Also used : Start(de.be4.eventbalg.core.parser.node.Start) EventBParser(de.be4.eventbalg.core.parser.EventBParser)

Example 22 with BException

use of de.be4.classicalb.core.parser.exceptions.BException in project prob2 by bendisposto.

the class RulesMachineErrorsTest method testRulesMachineWithParseError.

@Test
public void testRulesMachineWithParseError() {
    RulesMachineRun rulesMachineRun = startRulesMachineRunWithOperations("RULE foo BODY ;; END");
    System.out.println(rulesMachineRun.getFirstError().getMessage());
    assertEquals(ERROR_TYPES.PARSE_ERROR, rulesMachineRun.getFirstError().getType());
    assertTrue(rulesMachineRun.getFirstError().getException() instanceof BException);
}
Also used : RulesMachineRun(de.prob.model.brules.RulesMachineRun) BException(de.be4.classicalb.core.parser.exceptions.BException) Test(org.junit.Test)

Example 23 with BException

use of de.be4.classicalb.core.parser.exceptions.BException in project prob2 by bendisposto.

the class ProBError method convertParserExceptionToErrorItems.

private static List<ErrorItem> convertParserExceptionToErrorItems(BCompoundException e) {
    List<ErrorItem> errorItems = new ArrayList<>();
    for (BException bException : e.getBExceptions()) {
        List<ErrorItem.Location> errorItemlocations = new ArrayList<>();
        if (bException.getFilename() != null && bException.getCause() != null) {
            List<BException.Location> parserlocations = bException.getLocations();
            for (BException.Location location : parserlocations) {
                ErrorItem.Location loc = new ErrorItem.Location(bException.getFilename(), location.getStartLine(), location.getStartColumn(), location.getEndLine(), location.getEndColumn());
                errorItemlocations.add(loc);
            }
        }
        ErrorItem item = new ErrorItem(bException.getMessage(), ErrorItem.Type.ERROR, errorItemlocations);
        errorItems.add(item);
    }
    return errorItems;
}
Also used : ArrayList(java.util.ArrayList) ErrorItem(de.prob.animator.domainobjects.ErrorItem) BException(de.be4.classicalb.core.parser.exceptions.BException)

Example 24 with BException

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

the class RulesUtil method getRulesProjectAsPrologTerm.

public static String getRulesProjectAsPrologTerm(final String content) {
    RulesProject rulesProject = new RulesProject();
    ParsingBehaviour pb = new ParsingBehaviour();
    pb.setAddLineNumbers(false);
    rulesProject.setParsingBehaviour(pb);
    rulesProject.parseRulesMachines(content, new String[] {});
    rulesProject.checkAndTranslateProject();
    List<IModel> bModels = rulesProject.getBModels();
    List<BException> bExceptionList = rulesProject.getBExceptionList();
    if (bExceptionList.isEmpty()) {
        IModel model = bModels.get(bModels.size() - 2);
        PrettyPrinter pp = new PrettyPrinter();
        model.getStart().apply(pp);
        System.out.println(pp.getPrettyPrint());
    }
    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        @Override
        public void write(int b) throws IOException {
            this.string.append((char) b);
        }

        public String toString() {
            return this.string.toString();
        }
    };
    rulesProject.printPrologOutput(new PrintStream(output), new PrintStream(output));
    return output.toString();
}
Also used : PrintStream(java.io.PrintStream) PrettyPrinter(de.be4.classicalb.core.parser.util.PrettyPrinter) OutputStream(java.io.OutputStream) BException(de.be4.classicalb.core.parser.exceptions.BException) ParsingBehaviour(de.be4.classicalb.core.parser.ParsingBehaviour)

Example 25 with BException

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

the class RulesUtil method getRulesMachineAsBMachine.

public static String getRulesMachineAsBMachine(File file) {
    RulesProject rulesProject = new RulesProject();
    rulesProject.parseProject(file);
    rulesProject.checkAndTranslateProject();
    List<IModel> bModels = rulesProject.getBModels();
    List<BException> bExceptionList = rulesProject.getBExceptionList();
    if (!bExceptionList.isEmpty()) {
        throw new RuntimeException(bExceptionList.get(0));
    }
    IModel model = bModels.get(bModels.size() - 2);
    PrettyPrinter pp = new PrettyPrinter();
    model.getStart().apply(pp);
    return pp.getPrettyPrint();
}
Also used : PrettyPrinter(de.be4.classicalb.core.parser.util.PrettyPrinter) BException(de.be4.classicalb.core.parser.exceptions.BException)

Aggregations

BException (de.be4.classicalb.core.parser.exceptions.BException)22 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)15 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 Start (de.be4.eventbalg.core.parser.node.Start)7 Test (org.junit.Test)7 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)6 Start (de.be4.eventb.core.parser.node.Start)6 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)4 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)4 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)3 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)3 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)3 PrettyPrinter (de.be4.classicalb.core.parser.util.PrettyPrinter)3 AMachineParseUnit (de.be4.eventb.core.parser.node.AMachineParseUnit)3 File (java.io.File)3 ParsingBehaviour (de.be4.classicalb.core.parser.ParsingBehaviour)2 BParseException (de.be4.classicalb.core.parser.exceptions.BParseException)2 Token (de.be4.classicalb.core.preparser.node.Token)2 AInvariant (de.be4.eventb.core.parser.node.AInvariant)2