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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations