Search in sources :

Example 26 with SemanticConditionList

use of org.abs_models.frontend.analyser.SemanticConditionList in project abstools by abstools.

the class ABSTest method assertParseModelOk.

public static Model assertParseModelOk(Model m, Config... config) throws IOException {
    if (m != null) {
        final StringBuffer errs;
        String fileNames = m.getCompilationUnit(0).getFileName();
        for (int i = 1; i < m.getCompilationUnits().getNumChild(); i++) fileNames += " & " + m.getCompilationUnit(i).getFileName();
        int parseErrs = m.getParserErrors().size();
        if (parseErrs > 0) {
            errs = new StringBuffer("Parse errors: " + parseErrs + ". First error:\n");
            errs.append(m.getParserErrors().get(0));
            fail("Failed to parse: " + fileNames + "\n" + errs.toString());
            return m;
        }
        int numSemErrs = m.getErrors().getErrorCount();
        errs = new StringBuffer("Semantic errors: " + numSemErrs + "\n");
        if (numSemErrs > 0) {
            for (SemanticCondition error : m.getErrors()) errs.append(error.getHelpMessage() + "\n");
            fail("Failed to parse: " + fileNames + "\n" + errs.toString());
        } else if (isSet(TYPE_CHECK, config)) {
            SemanticConditionList l = m.typeCheck();
            if (l.containsErrors()) {
                for (SemanticCondition error : l) errs.append(error.getHelpMessage() + "\n");
                fail("Failed to typecheck: " + fileNames + "\n" + errs.toString());
            }
        }
    }
    return m;
}
Also used : SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) SemanticCondition(org.abs_models.frontend.analyser.SemanticCondition)

Example 27 with SemanticConditionList

use of org.abs_models.frontend.analyser.SemanticConditionList in project abstools by abstools.

the class LocationTypeTests method assertInfer.

private Model assertInfer(String code, LocationType expected, boolean fails) {
    Model m = assertParse(code);
    // m.setLocationTypingEnabled(true);
    LocationTypeInferenceExtension ltie = new LocationTypeInferenceExtension(m);
    m.registerTypeSystemExtension(ltie);
    SemanticConditionList e = m.typeCheck();
    // System.out.println(ltie.getConstraints());
    assertEquals(!e.containsErrors() ? "" : "Found error: " + e.getFirstError().getMessage() + e.getFirstError().getNode().getPositionString(), fails, e.containsErrors());
    // assertEquals(fails, generated == null);
    if (expected != null) {
        VarDeclStmt vds = ((VarDeclStmt) m.getMainBlock().getStmt(0));
        LocationTypeVar lv = LocationTypeInferenceExtension.getVar(vds.getVarDecl().getType());
        LocationType t = ltie.getResults().get(lv);
        assertTrue(t.toString(), expected == LocationType.FAR ? t == LocationType.FAR : expected == t);
    }
    return m;
}
Also used : SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) VarDeclStmt(org.abs_models.frontend.ast.VarDeclStmt) Model(org.abs_models.frontend.ast.Model)

Example 28 with SemanticConditionList

use of org.abs_models.frontend.analyser.SemanticConditionList in project abstools by abstools.

the class CaseStudyTypeChecking method assertParseFilesOk.

protected Model assertParseFilesOk(String srcFolder, Config... config) throws IOException, WrongProgramArgumentException, InternalBackendException {
    File srcFolderF = new File(srcFolder);
    assertTrue(srcFolder, srcFolderF.exists());
    Main main = new Main();
    Model m = main.parse(findAbsFiles(srcFolderF));
    if (m != null) {
        m.evaluateAllProductDeclarations();
        if (m.hasParserErrors())
            Assert.fail(m.getParserErrors().get(0).getMessage());
        int numSemErrs = m.getErrors().getErrorCount();
        StringBuffer errs = new StringBuffer("Semantic errors: " + numSemErrs + "\n");
        if (numSemErrs > 0) {
            for (SemanticCondition error : m.getErrors()) errs = errs.append(error.getHelpMessage() + "\n");
            fail("Failed to parse: " + srcFolder + "\n" + errs.toString());
        } else if (isSet(TYPE_CHECK, config)) {
            SemanticConditionList l = m.typeCheck();
            if (l.containsErrors()) {
                for (SemanticCondition error : l) errs = errs.append(error.getHelpMessage() + "\n");
                fail("Failed to typecheck: " + srcFolder + "\n" + errs.toString());
            }
        }
    }
    return m;
}
Also used : SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) SemanticCondition(org.abs_models.frontend.analyser.SemanticCondition) Model(org.abs_models.frontend.ast.Model) File(java.io.File) Main(org.abs_models.frontend.parser.Main)

Example 29 with SemanticConditionList

use of org.abs_models.frontend.analyser.SemanticConditionList in project abstools by abstools.

the class ExamplesTypeChecking method parse.

protected Model parse(String input) throws Exception {
    Model m = assertTypeCheckFileOk(input);
    if (product != null) {
        m.collapseTraitModifiers();
        m.flattenForProduct(product);
        final SemanticConditionList errors = m.getErrors();
        if (errors.containsErrors())
            onError(errors.getFirstError().getMessage());
        m.typeCheck(errors);
        if (errors.containsErrors())
            onError(errors.getFirstError().getMessage());
    }
    return m;
}
Also used : SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) Model(org.abs_models.frontend.ast.Model)

Example 30 with SemanticConditionList

use of org.abs_models.frontend.analyser.SemanticConditionList in project abstools by abstools.

the class ASTBasedABSTestRunnerGeneratorTest method testGenerateTestRunner.

@Test
public final void testGenerateTestRunner() {
    final Model model;
    try {
        model = ABSTest.parseString(ABS_UNIT + TEST_CODE);
    } catch (Exception e) {
        throw new IllegalStateException("Cannot parse test code", e);
    }
    ABSTestRunnerGenerator generator = new ASTBasedABSTestRunnerGenerator(model);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    PrintStream print = new PrintStream(stream);
    generator.generateTestRunner(print);
    String runner = stream.toString();
    try {
        Model result = ABSTest.parseString(ABS_UNIT + TEST_CODE + runner);
        StringBuilder parseErrors = new StringBuilder();
        if (result.hasParserErrors()) {
            parseErrors.append("Syntactic errors: ");
            List<ParserError> es = result.getParserErrors();
            parseErrors.append(es.size());
            parseErrors.append("\n");
            for (ParserError e : es) {
                parseErrors.append(e.getHelpMessage());
                parseErrors.append("\n");
            }
        }
        assertFalse("Generated code must not have parse error: " + parseErrors, result.hasParserErrors());
        StringBuilder errors = new StringBuilder();
        if (result.hasErrors()) {
            SemanticConditionList el = result.getErrors();
            errors.append("Semantic errors: ");
            errors.append(el.getErrorCount());
            errors.append("\n");
            for (SemanticCondition error : el) {
                errors.append(error.getHelpMessage());
                errors.append("\n");
            }
        }
        assertFalse("Generated code must not have semantic error: " + errors, result.hasErrors());
        result.typeCheck();
        assertFalse("Generated code must not have type error", result.hasTypeErrors());
        assertThat("Has one module that has the name 'AbsUnit.TestRunner' and a main block", result.getModuleDecls(), hasItem(new ModuleMatcher()));
    } catch (Exception e) {
        fail("Cannot throw an exception ");
    }
}
Also used : PrintStream(java.io.PrintStream) ParserError(org.abs_models.frontend.parser.ParserError) SemanticCondition(org.abs_models.frontend.analyser.SemanticCondition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) Model(org.abs_models.frontend.ast.Model) ABSTest(org.abs_models.ABSTest) Test(org.junit.Test)

Aggregations

SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)42 Model (org.abs_models.frontend.ast.Model)27 Test (org.junit.Test)27 ProductLine (org.abs_models.frontend.ast.ProductLine)7 SemanticCondition (org.abs_models.frontend.analyser.SemanticCondition)6 FrontendTest (org.abs_models.frontend.FrontendTest)5 ProductDecl (org.abs_models.frontend.ast.ProductDecl)3 ABSTest (org.abs_models.ABSTest)2 TypeError (org.abs_models.frontend.analyser.TypeError)2 Constraint (choco.kernel.model.constraints.Constraint)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 InternalBackendException (org.abs_models.backend.common.InternalBackendException)1 DefaultABSFormatter (org.abs_models.backend.prettyprint.DefaultABSFormatter)1