use of de.be4.classicalb.core.parser.IDefinitions in project probparsers by bendisposto.
the class CliBParser method parseFormula.
private static void parseFormula(String theFormula, IDefinitions context) {
try {
BParser parser = new BParser();
parser.setDefinitions(context);
Start start = parser.parse(theFormula, false);
PrologTermStringOutput strOutput = new PrologTermStringOutput();
NodeIdAssignment na = new NodeIdAssignment();
start.apply(na);
ClassicalPositionPrinter pprinter = new ClassicalPositionPrinter(na, -1, 0);
ASTProlog printer = new ASTProlog(strOutput, pprinter);
start.apply(printer);
strOutput.fullstop();
// A Friendly Reminder: strOutput includes a newline!
String output = strOutput.toString();
print(output);
} catch (NullPointerException e) {
// Not Parseable - Sadly, calling e.getLocalizedMessage() on the
// NullPointerException returns NULL itself, thus triggering another
// NullPointerException in the catch statement. Therefore we need a
// second catch statement with a special case for the
// NullPointerException instead of catching a general Exception
// print("EXCEPTION NullPointerException" + System.lineSeparator());
PrologTermStringOutput strOutput = new PrologTermStringOutput();
strOutput.openTerm("exception").printAtom("NullPointerException").closeTerm();
strOutput.fullstop();
strOutput.flush();
String output = strOutput.toString();
print(output);
} catch (BCompoundException e) {
PrologExceptionPrinter.printException(socketOutputStream, e, false, true);
}
}
use of de.be4.classicalb.core.parser.IDefinitions in project probparsers by bendisposto.
the class CliBParser method parseExtendedFormula.
private static void parseExtendedFormula(String theFormula, IDefinitions context) {
try {
BParser parser = new BParser();
parser.setDefinitions(context);
Start start = parser.eparse(theFormula, context);
PrologTermStringOutput strOutput = new PrologTermStringOutput();
NodeIdAssignment na = new NodeIdAssignment();
start.apply(na);
ClassicalPositionPrinter pprinter = new ClassicalPositionPrinter(na, -1, 0);
ASTProlog printer = new ASTProlog(strOutput, pprinter);
start.apply(printer);
strOutput.fullstop();
// A Friendly Reminder: strOutput includes a newline!
print(strOutput.toString());
} catch (NullPointerException e) {
// Not Parseable - Sadly, calling e.getLocalizedMessage() on the
// NullPointerException returns NULL itself, thus triggering another
// NullPointerException in the catch statement. Therefore we need a
// second catch statement with a special case for the
// NullPointerException instead of catching a general Exception
// print("EXCEPTION NullPointerException" + System.lineSeparator());
PrologTermStringOutput strOutput = new PrologTermStringOutput();
strOutput.openTerm("exception").printAtom("NullPointerException").closeTerm();
strOutput.fullstop();
strOutput.flush();
print(strOutput.toString());
} catch (BCompoundException e) {
PrologExceptionPrinter.printException(socketOutputStream, e, false, true);
} catch (LexerException e) {
PrologTermStringOutput strOutput = new PrologTermStringOutput();
strOutput.openTerm("exception").printAtom(e.getLocalizedMessage()).closeTerm();
strOutput.fullstop();
strOutput.flush();
print(strOutput.toString());
} catch (IOException e) {
PrologExceptionPrinter.printException(socketOutputStream, e, theFormula, false, true);
}
}
use of de.be4.classicalb.core.parser.IDefinitions 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);
}
use of de.be4.classicalb.core.parser.IDefinitions in project probparsers by bendisposto.
the class Definitions method assignIdsToNodes.
@Override
public void assignIdsToNodes(NodeIdAssignment nodeIdMapping, List<File> machineFilesLoaded) {
if (file != null) {
machineFilesLoaded.add(file);
final int fileNumber = machineFilesLoaded.indexOf(file) + 1;
for (PDefinition def : definitionsMap.values()) {
nodeIdMapping.assignIdentifiers(fileNumber, def);
}
}
for (IDefinitions defintions : super.referencedDefinitions) {
defintions.assignIdsToNodes(nodeIdMapping, machineFilesLoaded);
}
}
use of de.be4.classicalb.core.parser.IDefinitions in project probparsers by bendisposto.
the class DefinitionFilesTest method testRealFiles.
/*
* test with real files
*/
@Test
public void testRealFiles() throws Exception {
final BParser parser = new BParser("testcase");
File machine = new File("src/test/resources/parsable/DefinitionFileTest.mch");
parser.parseFile(machine, false);
final IDefinitions definitions = parser.getDefinitions();
final APredicateDefinitionDefinition def1 = (APredicateDefinitionDefinition) definitions.getDefinition("GRD2");
assertEquals("GRD2", def1.getName().getText());
assertEquals(0, def1.getParameters().size());
assertTrue(def1.getRhs() instanceof PPredicate);
final APredicateDefinitionDefinition def2 = (APredicateDefinitionDefinition) definitions.getDefinition("GRD1");
assertEquals("GRD1", def2.getName().getText());
assertEquals(0, def2.getParameters().size());
assertTrue(def2.getRhs() instanceof PPredicate);
}
Aggregations