Search in sources :

Example 6 with IDefinitions

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);
    }
}
Also used : ClassicalPositionPrinter(de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter) ASTProlog(de.be4.classicalb.core.parser.analysis.prolog.ASTProlog) Start(de.be4.classicalb.core.parser.node.Start) PrologTermStringOutput(de.prob.prolog.output.PrologTermStringOutput) BParser(de.be4.classicalb.core.parser.BParser) NodeIdAssignment(de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 7 with IDefinitions

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);
    }
}
Also used : ClassicalPositionPrinter(de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter) ASTProlog(de.be4.classicalb.core.parser.analysis.prolog.ASTProlog) Start(de.be4.classicalb.core.parser.node.Start) PrologTermStringOutput(de.prob.prolog.output.PrologTermStringOutput) BParser(de.be4.classicalb.core.parser.BParser) IOException(java.io.IOException) LexerException(de.be4.classicalb.core.parser.lexer.LexerException) NodeIdAssignment(de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException)

Example 8 with IDefinitions

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

Example 9 with IDefinitions

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);
    }
}
Also used : PDefinition(de.be4.classicalb.core.parser.node.PDefinition)

Example 10 with IDefinitions

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);
}
Also used : APredicateDefinitionDefinition(de.be4.classicalb.core.parser.node.APredicateDefinitionDefinition) IDefinitions(de.be4.classicalb.core.parser.IDefinitions) BParser(de.be4.classicalb.core.parser.BParser) PPredicate(de.be4.classicalb.core.parser.node.PPredicate) File(java.io.File) Test(org.junit.Test)

Aggregations

AExpressionDefinitionDefinition (de.be4.classicalb.core.parser.node.AExpressionDefinitionDefinition)9 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)8 BParser (de.be4.classicalb.core.parser.BParser)5 ATotalFunctionExpression (de.be4.classicalb.core.parser.node.ATotalFunctionExpression)5 ArrayList (java.util.ArrayList)5 IDefinitions (de.be4.classicalb.core.parser.IDefinitions)4 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)4 IOException (java.io.IOException)4 AStringExpression (de.be4.classicalb.core.parser.node.AStringExpression)3 Start (de.be4.classicalb.core.parser.node.Start)3 TStringLiteral (de.be4.classicalb.core.parser.node.TStringLiteral)3 File (java.io.File)3 Test (org.junit.Test)3 ASTProlog (de.be4.classicalb.core.parser.analysis.prolog.ASTProlog)2 ClassicalPositionPrinter (de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter)2 NodeIdAssignment (de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment)2 BException (de.be4.classicalb.core.parser.exceptions.BException)2 CheckException (de.be4.classicalb.core.parser.exceptions.CheckException)2 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)2 AIdentifierExpression (de.be4.classicalb.core.parser.node.AIdentifierExpression)2