Search in sources :

Example 1 with ClassicalPositionPrinter

use of de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter in project probparsers by bendisposto.

the class ASTPrologTest method printAST.

private String printAST(final Node node) {
    final StringWriter swriter = new StringWriter();
    NodeIdAssignment nodeids = new NodeIdAssignment();
    node.apply(nodeids);
    IPrologTermOutput pout = new PrologTermOutput(new PrintWriter(swriter), false);
    PositionPrinter pprinter = new ClassicalPositionPrinter(nodeids);
    ASTProlog prolog = new ASTProlog(pout, pprinter);
    node.apply(prolog);
    swriter.flush();
    System.out.println(swriter.toString());
    return swriter.toString();
}
Also used : ClassicalPositionPrinter(de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter) ASTProlog(de.be4.classicalb.core.parser.analysis.prolog.ASTProlog) StringWriter(java.io.StringWriter) PrologTermOutput(de.prob.prolog.output.PrologTermOutput) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput) PositionPrinter(de.be4.classicalb.core.parser.analysis.prolog.PositionPrinter) ClassicalPositionPrinter(de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter) NodeIdAssignment(de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput) PrintWriter(java.io.PrintWriter)

Example 2 with ClassicalPositionPrinter

use of de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter in project probparsers by bendisposto.

the class ASTPrologTest method testFreeType.

@Test
public void testFreeType() throws BCompoundException {
    final AConstructorFreetypeConstructor multi = new AConstructorFreetypeConstructor(new TIdentifierLiteral("multi"), new APowSubsetExpression(new AIntegerSetExpression()));
    final AConstructorFreetypeConstructor single = new AConstructorFreetypeConstructor(new TIdentifierLiteral("single"), new AIntegerSetExpression());
    final AFreetype freetype = new AFreetype(new TIdentifierLiteral("T"), Arrays.<PFreetypeConstructor>asList(multi, single));
    AFreetypesMachineClause clause = new AFreetypesMachineClause(Arrays.<PFreetype>asList(freetype));
    final StringWriter swriter = new StringWriter();
    NodeIdAssignment nodeids = new NodeIdAssignment();
    clause.apply(nodeids);
    IPrologTermOutput pout = new PrologTermOutput(new PrintWriter(swriter), false);
    PositionPrinter pprinter = new ClassicalPositionPrinter(nodeids);
    ASTProlog prolog = new ASTProlog(pout, pprinter);
    clause.apply(prolog);
    String code = swriter.toString();
    assertFalse(code.isEmpty());
    assertEquals("freetypes(0,[freetype(1,'T',[constructor(2,multi,pow_subset(3,integer_set(4))),constructor(5,single,integer_set(6))])])", code);
}
Also used : ClassicalPositionPrinter(de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter) PrologTermOutput(de.prob.prolog.output.PrologTermOutput) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput) PositionPrinter(de.be4.classicalb.core.parser.analysis.prolog.PositionPrinter) ClassicalPositionPrinter(de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter) AIntegerSetExpression(de.be4.classicalb.core.parser.node.AIntegerSetExpression) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) NodeIdAssignment(de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment) ASTProlog(de.be4.classicalb.core.parser.analysis.prolog.ASTProlog) StringWriter(java.io.StringWriter) AConstructorFreetypeConstructor(de.be4.classicalb.core.parser.node.AConstructorFreetypeConstructor) APowSubsetExpression(de.be4.classicalb.core.parser.node.APowSubsetExpression) AFreetypesMachineClause(de.be4.classicalb.core.parser.node.AFreetypesMachineClause) AFreetype(de.be4.classicalb.core.parser.node.AFreetype) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 3 with ClassicalPositionPrinter

use of de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter 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();
}
Also used : Start(de.be4.classicalb.core.parser.node.Start) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 4 with ClassicalPositionPrinter

use of de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter 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 5 with ClassicalPositionPrinter

use of de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter 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)

Aggregations

ASTProlog (de.be4.classicalb.core.parser.analysis.prolog.ASTProlog)10 ClassicalPositionPrinter (de.be4.classicalb.core.parser.analysis.prolog.ClassicalPositionPrinter)10 NodeIdAssignment (de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment)8 PositionPrinter (de.be4.classicalb.core.parser.analysis.prolog.PositionPrinter)4 IPrologTermOutput (de.prob.prolog.output.IPrologTermOutput)4 PrologTermOutput (de.prob.prolog.output.PrologTermOutput)4 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 Start (de.be4.classicalb.core.parser.node.Start)3 BParser (de.be4.classicalb.core.parser.BParser)2 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)2 PrologTermStringOutput (de.prob.prolog.output.PrologTermStringOutput)2 IOException (java.io.IOException)2 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)1 AConstructorFreetypeConstructor (de.be4.classicalb.core.parser.node.AConstructorFreetypeConstructor)1 AFreetype (de.be4.classicalb.core.parser.node.AFreetype)1 AFreetypesMachineClause (de.be4.classicalb.core.parser.node.AFreetypesMachineClause)1 AIntegerSetExpression (de.be4.classicalb.core.parser.node.AIntegerSetExpression)1 APowSubsetExpression (de.be4.classicalb.core.parser.node.APowSubsetExpression)1 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)1