Search in sources :

Example 1 with CompilationUnit

use of org.abs_models.frontend.ast.CompilationUnit in project abstools by abstools.

the class TraitTest method addAddModifierAtRuntimeBackComp.

@Test
public void addAddModifierAtRuntimeBackComp() {
    Model model = assertParse("module M;" + "class C { Unit m(){skip;} }");
    ClassDecl cls = (ClassDecl) findDecl(model, "M", "C");
    MethodSig sig = AbsASTBuilderUtil.createMethodSig("n", AbsASTBuilderUtil.getUnit());
    MethodImpl impl = new MethodImpl(sig, new Block(new List<>(), new List<>(new SkipStmt())));
    AddMethodModifier opr = new AddMethodModifier(impl);
    assertNotNull(opr.getMethodImpl());
    ModifyClassModifier mcn = new ModifyClassModifier();
    mcn.setName("M.C");
    DeltaAccess acc = new DeltaAccess(cls.getModuleDecl().getName());
    DeltaDecl dd = new DeltaDecl();
    dd.setName("MyDelta");
    dd.setImportedModule(acc);
    dd.addModuleModifier(mcn);
    mcn.addModifier(opr);
    mcn.setParent(dd);
    acc.setParent(dd);
    opr.setParent(mcn);
    sig.setParent(opr);
    CompilationUnit cu = model.getCompilationUnitList().getChild(0);
    cu.addDeltaDecl(dd);
    dd.setParent(cu);
    model.applyDelta(dd);
    assertEquals(2, cls.getMethods().getNumChild());
}
Also used : CompilationUnit(org.abs_models.frontend.ast.CompilationUnit) MethodSig(org.abs_models.frontend.ast.MethodSig) ClassDecl(org.abs_models.frontend.ast.ClassDecl) MethodImpl(org.abs_models.frontend.ast.MethodImpl) DeltaAccess(org.abs_models.frontend.ast.DeltaAccess) AddMethodModifier(org.abs_models.frontend.ast.AddMethodModifier) Model(org.abs_models.frontend.ast.Model) Block(org.abs_models.frontend.ast.Block) List(org.abs_models.frontend.ast.List) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) SkipStmt(org.abs_models.frontend.ast.SkipStmt) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) Test(org.junit.Test)

Example 2 with CompilationUnit

use of org.abs_models.frontend.ast.CompilationUnit in project abstools by abstools.

the class Main method parseUnit.

/**
 * Parse the content of `reader` into a CompilationUnit.
 *
 * @param file The filename of the input stream, or null
 * @param reader The stream to parse
 * @param raiseExceptions Raise parse errors as exceptions if true
 * @return The parsed content of `reader`, or an empty CompilationUnit with parse error information
 * @throws IOException
 */
private static CompilationUnit parseUnit(File file, Reader reader) throws IOException {
    try {
        SyntaxErrorCollector errorlistener = new SyntaxErrorCollector(file);
        ANTLRInputStream input = new ANTLRInputStream(reader);
        ABSLexer lexer = new ABSLexer(input);
        lexer.removeErrorListeners();
        lexer.addErrorListener(errorlistener);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ABSParser aparser = new ABSParser(tokens);
        aparser.removeErrorListeners();
        aparser.addErrorListener(errorlistener);
        ParseTree tree = aparser.goal();
        if (errorlistener.parserErrors.isEmpty()) {
            ParseTreeWalker walker = new ParseTreeWalker();
            CreateJastAddASTListener l = new CreateJastAddASTListener(file);
            walker.walk(l, tree);
            CompilationUnit u = new ASTPreProcessor().preprocess(l.getCompilationUnit());
            return u;
        } else {
            String path = "<unknown path>";
            if (file != null)
                path = file.getPath();
            CompilationUnit u = new CompilationUnit();
            u.setName(path);
            u.setParserErrors(errorlistener.parserErrors);
            return u;
        }
    } finally {
        reader.close();
    }
}
Also used : CompilationUnit(org.abs_models.frontend.ast.CompilationUnit) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ABSLexer(org.abs_models.frontend.antlr.parser.ABSLexer) ABSParser(org.abs_models.frontend.antlr.parser.ABSParser) CreateJastAddASTListener(org.abs_models.frontend.antlr.parser.CreateJastAddASTListener) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) SyntaxErrorCollector(org.abs_models.frontend.antlr.parser.SyntaxErrorCollector)

Example 3 with CompilationUnit

use of org.abs_models.frontend.ast.CompilationUnit in project abstools by abstools.

the class ASTBasedABSTestRunnerGenerator method generateTestRunner.

@Override
public void generateTestRunner(PrintStream stream) {
    // In order to safely call module.doPrettyPrint() we need a complete AST
    Model model = new Model();
    CompilationUnit compilationunit = new CompilationUnit();
    ModuleDecl module = new ModuleDecl();
    model.addCompilationUnitNoTransform(compilationunit);
    compilationunit.addModuleDeclNoTransform(module);
    module.setName(RUNNER_MAIN);
    module.setImportList(generateImportsAST());
    module.setBlock(generateMainBlockAST(module.getImportList()));
    PrintWriter writer = new PrintWriter(stream, true);
    ABSFormatter formatter = new DefaultABSFormatter(writer);
    module.doPrettyPrint(writer, formatter);
}
Also used : CompilationUnit(org.abs_models.frontend.ast.CompilationUnit) DefaultABSFormatter(org.abs_models.backend.prettyprint.DefaultABSFormatter) ABSFormatter(org.abs_models.backend.prettyprint.ABSFormatter) DefaultABSFormatter(org.abs_models.backend.prettyprint.DefaultABSFormatter) Model(org.abs_models.frontend.ast.Model) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) PrintWriter(java.io.PrintWriter)

Example 4 with CompilationUnit

use of org.abs_models.frontend.ast.CompilationUnit in project abstools by abstools.

the class DeadlockPreanalysis method analyzeModel.

/**
 * Applies the deadlock preanalysis
 */
public void analyzeModel() {
    try {
        // CompilationUnit cu = model.getCompilationUnits().getChild(0);
        for (CompilationUnit cu : model.getCompilationUnits()) {
            computeInstructions(cu, 0);
            if (cu.hasMainBlock()) {
                System.out.println("Processing main block" + cu.getName());
                computeInstructions(cu.getMainBlock(), 0);
            }
        }
        System.out.println(getExpressions);
        System.out.println(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CompilationUnit(org.abs_models.frontend.ast.CompilationUnit)

Example 5 with CompilationUnit

use of org.abs_models.frontend.ast.CompilationUnit in project abstools by abstools.

the class Main method parseFiles.

private static Model parseFiles(boolean verbose, final java.util.List<File> fileNames) throws IOException, InternalBackendException {
    if (fileNames.isEmpty()) {
        throw new IllegalArgumentException("Please provide at least one input file");
    }
    java.util.List<CompilationUnit> units = new ArrayList<>();
    for (File f : fileNames) {
        if (!f.canRead()) {
            throw new IllegalArgumentException("File " + f + " cannot be read");
        }
        if (!f.isDirectory() && !isABSSourceFile(f) && !isABSPackageFile(f)) {
            throw new IllegalArgumentException("File " + f + " is not a legal ABS file");
        }
    }
    for (File f : fileNames) {
        parseFileOrDirectory(units, f, verbose);
    }
    units.add(getStdLib());
    List<CompilationUnit> unitList = new List<>();
    for (CompilationUnit u : units) {
        unitList.add(u);
    }
    Model m = new Model(unitList);
    return m;
}
Also used : CompilationUnit(org.abs_models.frontend.ast.CompilationUnit) ArrayList(java.util.ArrayList) Model(org.abs_models.frontend.ast.Model) SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) ArrayList(java.util.ArrayList) List(org.abs_models.frontend.ast.List) File(java.io.File)

Aggregations

CompilationUnit (org.abs_models.frontend.ast.CompilationUnit)9 Model (org.abs_models.frontend.ast.Model)5 DeltaDecl (org.abs_models.frontend.ast.DeltaDecl)4 List (org.abs_models.frontend.ast.List)4 Test (org.junit.Test)4 Block (org.abs_models.frontend.ast.Block)3 ClassDecl (org.abs_models.frontend.ast.ClassDecl)3 DeltaAccess (org.abs_models.frontend.ast.DeltaAccess)3 MethodSig (org.abs_models.frontend.ast.MethodSig)3 ModifyClassModifier (org.abs_models.frontend.ast.ModifyClassModifier)3 MethodImpl (org.abs_models.frontend.ast.MethodImpl)2 ModuleDecl (org.abs_models.frontend.ast.ModuleDecl)2 SkipStmt (org.abs_models.frontend.ast.SkipStmt)2 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 ABSFormatter (org.abs_models.backend.prettyprint.ABSFormatter)1 DefaultABSFormatter (org.abs_models.backend.prettyprint.DefaultABSFormatter)1 FrontendTest (org.abs_models.frontend.FrontendTest)1 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)1