use of de.be4.ltl.core.ctlparser.node.Start in project probparsers by bendisposto.
the class RecursiveMachineLoader method printAsProlog.
public void printAsProlog(final IPrologTermOutput pout) {
final ClassicalPositionPrinter pprinter = new ClassicalPositionPrinter(getNodeIdMapping());
pprinter.printSourcePositions(parsingBehaviour.isAddLineNumbers());
final ASTProlog prolog = new ASTProlog(pout, pprinter);
// parser version
pout.openTerm("parser_version");
pout.printAtom(BParser.getBuildRevision());
pout.closeTerm();
pout.fullstop();
// machine
pout.openTerm("classical_b");
pout.printAtom(main);
pout.openList();
List<File> allFiles = new ArrayList<>();
allFiles.addAll(machineFilesLoaded);
allFiles.addAll(definitionFilesLoaded);
for (final File file : allFiles) {
try {
pout.printAtom(file.getCanonicalPath());
} catch (IOException e) {
pout.printAtom(file.getPath());
}
}
pout.closeList();
pout.closeTerm();
pout.fullstop();
for (final Map.Entry<String, Start> entry : getParsedMachines().entrySet()) {
pout.openTerm("machine");
entry.getValue().apply(prolog);
pout.closeTerm();
pout.fullstop();
}
pout.flush();
}
use of de.be4.ltl.core.ctlparser.node.Start in project probparsers by bendisposto.
the class RecursiveMachineLoader method recursivlyLoadMachine.
private void recursivlyLoadMachine(final File machineFile, final Start currentAst, final List<String> ancestors, final boolean isMain, File directory, final IDefinitions definitions) throws BCompoundException {
// make a copy of the referencing machines
List<String> newAncestors = new ArrayList<>(ancestors);
ReferencedMachines refMachines = new ReferencedMachines(machineFile, currentAst, parsingBehaviour.isMachineNameMustMatchFileName());
try {
refMachines.findReferencedMachines();
} catch (BException e) {
throw new BCompoundException(e);
}
final String name = refMachines.getName();
if (name == null) {
/*
* the parsed file is a definition file, hence the name of the
* machine is null
*/
throw new BCompoundException(new BException(machineFile.getName(), "Expecting a B machine but was a definition file in file: '" + machineFile.getName() + "\'", null));
}
machineFilesLoaded.add(machineFile);
final int fileNumber = machineFilesLoaded.indexOf(machineFile) + 1;
getNodeIdMapping().assignIdentifiers(fileNumber, currentAst);
definitions.assignIdsToNodes(getNodeIdMapping(), machineFilesLoaded);
injectDefinitions(currentAst, definitions);
getParsedMachines().put(name, currentAst);
parsedFiles.put(name, machineFile);
if (name != null) {
newAncestors.add(name);
}
if (isMain) {
main = name;
}
final Set<String> referencesSet = refMachines.getSetOfReferencedMachines();
try {
checkForCycles(newAncestors, referencesSet);
} catch (BException e) {
throw new BCompoundException(e);
}
final List<MachineReference> references = refMachines.getReferences();
for (final MachineReference refMachine : references) {
try {
final String filePragma = refMachine.getPath();
File file = null;
if (filePragma == null) {
file = lookupFile(directory, refMachine, newAncestors, refMachines.getPathList());
} else {
File p = new File(filePragma);
if (p.isAbsolute()) {
file = p;
} else {
file = new File(directory, filePragma);
}
}
if (file.exists() && parsedFiles.containsKey(refMachine.getName()) && !parsedFiles.get(refMachine.getName()).getCanonicalPath().equals(file.getCanonicalPath())) {
final String message = "Two files with the same name are referenced:\n" + parsedFiles.get(refMachine.getName()).getCanonicalPath() + "\n" + file.getCanonicalPath();
throw new BException(machineFile.getCanonicalPath(), new CheckException(message, refMachine.getNode()));
}
if (!getParsedMachines().containsKey(refMachine.getName())) {
try {
loadMachine(newAncestors, file);
} catch (IOException e) {
throw new BException(machineFile.getCanonicalPath(), new CheckException(e.getMessage(), refMachine.getNode(), e));
}
}
} catch (final BException e) {
// we do not longer wrap a B Exception in a B Exception
throw new BCompoundException(e);
} catch (final IOException e) {
throw new BCompoundException(new BException(machineFile.getAbsolutePath(), e));
} catch (final CheckException e) {
throw new BCompoundException(new BException(machineFile.getAbsolutePath(), e));
}
}
}
use of de.be4.ltl.core.ctlparser.node.Start in project probparsers by bendisposto.
the class StructuralTest method testUnclosedComment.
@Test
public void testUnclosedComment() {
final String emptyMachine = "MACHINE ClassicalB\n SETS pp ; qq\n /* CONSTANTS ccc,ddd\n VARIABLES xxx,yyy\n OPERATIONS\n op1 = BEGIN xxx := 1; v <-- op2(2) END;\n op2 = ANY q WHERE q : NAT THEN yyy := ccc END\nEND";
try {
getTreeAsString(emptyMachine);
fail("Expected exception was not thrown");
} catch (final BCompoundException e) {
final BLexerException ex = (BLexerException) e.getCause();
// checking the start position of the comment
assertEquals(3, ex.getLastLine());
assertEquals(2, ex.getLastPos());
assertTrue(e.getMessage().contains("Comment not closed."));
}
}
use of de.be4.ltl.core.ctlparser.node.Start in project probparsers by bendisposto.
the class StructuralTest method testConstantsClause.
@Test
public void testConstantsClause() throws Exception {
final String testMachine = "MACHINE SimplyStructure\nCONSTANTS dd, e, Ff\nPROPERTIES dd : BOOL\nEND";
// System.out.println("Parsing: \"" + testMachine + "\":");
final BParser parser = new BParser("testcase");
final Start startNode = parser.parse(testMachine, false);
assertNotNull(startNode);
// TODO more tests
}
use of de.be4.ltl.core.ctlparser.node.Start in project probparsers by bendisposto.
the class StructuralTest method testClausesStructure.
@Test
public void testClausesStructure() throws Exception {
final String testMachine = "MACHINE SimplyStructure\n" + "VARIABLES aa, b, Cc\n" + "INVARIANT aa : NAT\n" + "INITIALISATION aa:=1\n" + "CONSTANTS dd, e, Ff\n" + "PROPERTIES dd : NAT\n" + "SETS GGG; Hhh; JJ = {dada, dudu, TUTUT}; iII; kkk = {LLL}\nEND";
// System.out.println("Parsing: \"" + testMachine + "\":");
final BParser parser = new BParser("testcase");
final Start startNode = parser.parse(testMachine, true);
assertNotNull(startNode);
// TODO more tests
}
Aggregations