Search in sources :

Example 1 with AExpressionDefinitionDefinition

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

the class MachineContext method caseADefinitionsMachineClause.

/**
 * Definitions
 */
@Override
public void caseADefinitionsMachineClause(ADefinitionsMachineClause node) {
    definitionMachineClause = node;
    DefinitionsSorter.sortDefinitions(node);
    List<PDefinition> copy = node.getDefinitions();
    /*
		 * The definitions are not in a predefined order. In particular
		 * definitions can depend on each other. First all definitions are added
		 * to the definitions context table. Then all definitions are visited.
		 */
    Collection<PDefinition> definitionsToRemove = new HashSet<PDefinition>();
    for (PDefinition e : copy) {
        if (e instanceof AExpressionDefinitionDefinition) {
            AExpressionDefinitionDefinition def = (AExpressionDefinitionDefinition) e;
            String name = def.getName().getText();
            if (name.startsWith("ASSERT_LTL")) {
                LTLFormulaVisitor visitor = new LTLFormulaVisitor(name, this);
                visitor.parseDefinition(def);
                this.ltlVisitors.add(visitor);
                definitionsToRemove.add(def);
            } else if (name.startsWith("ANIMATION_")) {
                definitionsToRemove.add(def);
            }
            evalDefinitionName(((AExpressionDefinitionDefinition) e).getName().getText().toString(), e);
        } else if (e instanceof APredicateDefinitionDefinition) {
            evalDefinitionName(((APredicateDefinitionDefinition) e).getName().getText().toString(), e);
        } else if (e instanceof ASubstitutionDefinitionDefinition) {
            evalDefinitionName(((ASubstitutionDefinitionDefinition) e).getName().getText().toString(), e);
        }
    }
    /*
		 * At this point all LTL definitions (ASSERT_LTL) are removed. LTL
		 * formulas are stored in the Arraylist {@value #ltlVisitors}.
		 */
    copy.removeAll(definitionsToRemove);
    this.contextTable = new ArrayList<LinkedHashMap<String, Node>>();
    ArrayList<MachineContext> list = lookupReferencedMachines();
    for (int i = 0; i < list.size(); i++) {
        MachineContext s = list.get(i);
        contextTable.add(s.getDeferredSets());
        contextTable.add(s.getEnumeratedSets());
        contextTable.add(s.getEnumValues());
        contextTable.add(s.getConstants());
        contextTable.add(s.getVariables());
        contextTable.add(s.getDefinitions());
    }
    for (PDefinition e : copy) {
        e.apply(this);
    }
}
Also used : APredicateDefinitionDefinition(de.be4.classicalb.core.parser.node.APredicateDefinitionDefinition) ASubstitutionDefinitionDefinition(de.be4.classicalb.core.parser.node.ASubstitutionDefinitionDefinition) LinkedHashMap(java.util.LinkedHashMap) PDefinition(de.be4.classicalb.core.parser.node.PDefinition) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) HashSet(java.util.HashSet)

Example 2 with AExpressionDefinitionDefinition

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

the class ASTBuilder method addFormatToStringDefinition.

public static void addFormatToStringDefinition(IDefinitions iDefinitions) {
    if (iDefinitions.containsDefinition(FORMAT_TO_STRING)) {
        return;
    }
    /*-
		 * FORMAT_TO_STRING(MyFormatString,ListOfValues) == "0";
		 * EXTERNAL_FUNCTION_FORMAT_TO_STRING(TO_STRING_TYPE) == STRING*seq(TO_STRING_TYPE) --> STRING;
		 */
    AExpressionDefinitionDefinition formatDef = new AExpressionDefinitionDefinition();
    formatDef.setName(new TIdentifierLiteral(FORMAT_TO_STRING));
    formatDef.setParameters(createExpressionList("S", "T"));
    formatDef.setRhs(new AStringExpression(new TStringLiteral("abc")));
    iDefinitions.addDefinition(formatDef, IDefinitions.Type.Expression);
    AExpressionDefinitionDefinition formatType = new AExpressionDefinitionDefinition();
    formatType.setName(new TIdentifierLiteral("EXTERNAL_FUNCTION_FORMAT_TO_STRING"));
    formatType.setParameters(createExpressionList("T"));
    formatType.setRhs(new ATotalFunctionExpression(new AMultOrCartExpression(new AStringSetExpression(), new ASeqExpression(createIdentifier("T"))), new AStringSetExpression()));
    iDefinitions.addDefinition(formatType, IDefinitions.Type.Expression);
}
Also used : AStringSetExpression(de.be4.classicalb.core.parser.node.AStringSetExpression) AMultOrCartExpression(de.be4.classicalb.core.parser.node.AMultOrCartExpression) ASeqExpression(de.be4.classicalb.core.parser.node.ASeqExpression) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) AStringExpression(de.be4.classicalb.core.parser.node.AStringExpression) TStringLiteral(de.be4.classicalb.core.parser.node.TStringLiteral) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) ATotalFunctionExpression(de.be4.classicalb.core.parser.node.ATotalFunctionExpression)

Example 3 with AExpressionDefinitionDefinition

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

the class ASTBuilder method addToStringDefinition.

public static void addToStringDefinition(IDefinitions definitions) {
    if (definitions.containsDefinition(TO_STRING)) {
        return;
    }
    /*-
		 * TO_STRING(S) == "0"; 
		 * EXTERNAL_FUNCTION_TO_STRING(X) == X -->STRING;
		 */
    AExpressionDefinitionDefinition toStringDef = new AExpressionDefinitionDefinition();
    toStringDef.setName(new TIdentifierLiteral(TO_STRING));
    toStringDef.setParameters(createIdentifierList("S"));
    toStringDef.setRhs(new AStringExpression(new TStringLiteral("0")));
    definitions.addDefinition(toStringDef, IDefinitions.Type.Expression);
    AExpressionDefinitionDefinition toStringTypeDef = new AExpressionDefinitionDefinition();
    toStringTypeDef.setName(new TIdentifierLiteral("EXTERNAL_FUNCTION_TO_STRING"));
    toStringTypeDef.setParameters(createIdentifierList("X"));
    toStringTypeDef.setRhs(new ATotalFunctionExpression(createIdentifier("X"), new AStringSetExpression()));
    definitions.addDefinition(toStringTypeDef, IDefinitions.Type.Expression);
}
Also used : AStringSetExpression(de.be4.classicalb.core.parser.node.AStringSetExpression) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) AStringExpression(de.be4.classicalb.core.parser.node.AStringExpression) TStringLiteral(de.be4.classicalb.core.parser.node.TStringLiteral) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) ATotalFunctionExpression(de.be4.classicalb.core.parser.node.ATotalFunctionExpression)

Example 4 with AExpressionDefinitionDefinition

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

the class ASTBuilder method addSortDefinition.

public static void addSortDefinition(IDefinitions iDefinitions) {
    if (iDefinitions.containsDefinition(SORT)) {
        return;
    }
    /*- SORT
		 *  SORT(X) == [];
		 *  EXTERNAL_FUNCTION_SORT(T) == POW(T)-->seq(T);
		 */
    AExpressionDefinitionDefinition sortDef = new AExpressionDefinitionDefinition();
    sortDef.setName(new TIdentifierLiteral(SORT));
    sortDef.setParameters(createExpressionList("X"));
    sortDef.setRhs(new AEmptySequenceExpression());
    iDefinitions.addDefinition(sortDef, IDefinitions.Type.Expression);
    AExpressionDefinitionDefinition sortType = new AExpressionDefinitionDefinition();
    sortType.setName(new TIdentifierLiteral("EXTERNAL_FUNCTION_SORT"));
    sortType.setParameters(createExpressionList("T"));
    sortType.setRhs(new ATotalFunctionExpression(new APowSubsetExpression(createIdentifier("T")), new ASeqExpression(createIdentifier("T"))));
    iDefinitions.addDefinition(sortType, IDefinitions.Type.Expression);
}
Also used : AEmptySequenceExpression(de.be4.classicalb.core.parser.node.AEmptySequenceExpression) APowSubsetExpression(de.be4.classicalb.core.parser.node.APowSubsetExpression) ASeqExpression(de.be4.classicalb.core.parser.node.ASeqExpression) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) ATotalFunctionExpression(de.be4.classicalb.core.parser.node.ATotalFunctionExpression)

Example 5 with AExpressionDefinitionDefinition

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

the class DefinitionFilesTest method testRecursiveReference.

// TODO test two files
/*
	 * test recursive references from def file to def file
	 */
@Test
public void testRecursiveReference() throws Exception {
    final String testMachine = "MACHINE Test\nDEFINITIONS \"DefFile1\"; def1 == xx; def02 == aa\nEND";
    final BParser parser = new BParser("testcase");
    parser.parse(testMachine, false, this);
    final IDefinitions definitions = parser.getDefinitions();
    final AExpressionDefinitionDefinition def1 = (AExpressionDefinitionDefinition) definitions.getDefinition("def1");
    assertEquals("def1", def1.getName().getText());
    assertEquals(0, def1.getParameters().size());
    assertTrue(def1.getRhs() instanceof AIdentifierExpression);
    String ident = Utils.getTIdentifierListAsString(((AIdentifierExpression) def1.getRhs()).getIdentifier());
    assertEquals("xx", ident);
    final AExpressionDefinitionDefinition def2 = (AExpressionDefinitionDefinition) definitions.getDefinition("def2");
    assertEquals("def2", def2.getName().getText());
    assertEquals(0, def2.getParameters().size());
    assertTrue(def2.getRhs() instanceof AIdentifierExpression);
    ident = Utils.getTIdentifierListAsString(((AIdentifierExpression) def2.getRhs()).getIdentifier());
    assertEquals("yy", ident);
    final AExpressionDefinitionDefinition def3 = (AExpressionDefinitionDefinition) definitions.getDefinition("def3");
    assertEquals("def3", def3.getName().getText());
    assertEquals(0, def3.getParameters().size());
    assertTrue(def3.getRhs() instanceof AIdentifierExpression);
    ident = Utils.getTIdentifierListAsString(((AIdentifierExpression) def3.getRhs()).getIdentifier());
    // definition in outer def file should overwrite the one in referenced
    // def file
    assertEquals("bb", ident);
}
Also used : AIdentifierExpression(de.be4.classicalb.core.parser.node.AIdentifierExpression) IDefinitions(de.be4.classicalb.core.parser.IDefinitions) AExpressionDefinitionDefinition(de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition) BParser(de.be4.classicalb.core.parser.BParser) Test(org.junit.Test)

Aggregations

AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)12 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)8 ATotalFunctionExpression (de.be4.classicalb.core.parser.node.ATotalFunctionExpression)5 AStringExpression (de.be4.classicalb.core.parser.node.AStringExpression)4 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)3 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)3 ASubstitutionDefinitionDefinition (de.be4.classicalb.core.parser.node.ASubstitutionDefinitionDefinition)3 TStringLiteral (de.be4.classicalb.core.parser.node.TStringLiteral)3 BParser (de.be4.classicalb.core.parser.BParser)2 IDefinitions (de.be4.classicalb.core.parser.IDefinitions)2 Type (de.be4.classicalb.core.parser.IDefinitions.Type)2 APowSubsetExpression (de.be4.classicalb.core.parser.node.APowSubsetExpression)2 ASeqExpression (de.be4.classicalb.core.parser.node.ASeqExpression)2 AStringSetExpression (de.be4.classicalb.core.parser.node.AStringSetExpression)2 PDefinition (de.be4.classicalb.core.parser.node.PDefinition)2 PExpression (de.be4.classicalb.core.parser.node.PExpression)2 TDefLiteralSubstitution (de.be4.classicalb.core.parser.node.TDefLiteralSubstitution)2 Test (org.junit.Test)2 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)1 VisitorException (de.be4.classicalb.core.parser.exceptions.VisitorException)1