Search in sources :

Example 6 with EOF

use of de.be4.classicalb.core.parser.node.EOF in project prob2 by bendisposto.

the class DomBuilder method createPredicateAST.

private Start createPredicateAST(final PPredicate pPredicate) {
    Start start = new Start();
    APredicateParseUnit node2 = new APredicateParseUnit();
    start.setPParseUnit(node2);
    start.setEOF(EOF);
    node2.setPredicate((PPredicate) pPredicate.clone());
    node2.getPredicate().apply(new RenameIdentifiers());
    return start;
}
Also used : Start(de.be4.classicalb.core.parser.node.Start) APredicateParseUnit(de.be4.classicalb.core.parser.node.APredicateParseUnit)

Example 7 with EOF

use of de.be4.classicalb.core.parser.node.EOF in project probparsers by bendisposto.

the class UnitPragmaTest method testLexer.

@Test
public void testLexer() throws Exception {
    String input = "MACHINE UnitPragmaExpressions1 VARIABLES   lala, /*@ unit \"10**1 * m**1\" */ xx,   /*@ unit \"10**1 * m**1\" */ yy,   /*@ unit \"10**2 * m**2\" */ zz,   test  INVARIANT  /*@ label \"lol\" */  lala = \"trololo\" &xx:NAT &   yy:NAT &   zz:NAT &   test:NAT INITIALISATION xx,yy,zz,test:=1,2,3,4 OPERATIONS   multiply = zz := xx*yy;   add      = xx := yy+1;   sub      = xx := yy-1;   type     = test := yy END";
    BLexer lex = new BLexer(new PushbackReader(new StringReader(input), 500));
    Token t;
    while (!((t = lex.next()) instanceof EOF)) {
        System.out.print(t.getClass().getSimpleName() + "(" + t.getText() + ")");
        System.out.print(" ");
    }
    BParser p = new BParser();
    Start ast = p.parse(input, false);
    ASTPrinter pr = new ASTPrinter();
    ast.apply(pr);
    System.out.println(printAST(ast));
}
Also used : BLexer(de.be4.classicalb.core.parser.BLexer) Start(de.be4.classicalb.core.parser.node.Start) ASTPrinter(de.be4.classicalb.core.parser.visualisation.ASTPrinter) StringReader(java.io.StringReader) Token(de.be4.classicalb.core.parser.node.Token) BParser(de.be4.classicalb.core.parser.BParser) Helpers.getTreeAsString(util.Helpers.getTreeAsString) EOF(de.be4.classicalb.core.parser.node.EOF) PushbackReader(java.io.PushbackReader) Test(org.junit.Test)

Example 8 with EOF

use of de.be4.classicalb.core.parser.node.EOF in project probparsers by bendisposto.

the class PragmaTest method testLexer.

@Test
public void testLexer() throws Exception {
    // String input = "/*@ generated */ MACHINE foo(x) \n"
    // + "/* look at me. */ \n"
    // + "DEFINITIONS \n"
    // + " /*@ conversion */ foo(m) == m \n"
    // + "PROPERTIES \n"
    // + "/*@ label foo */ \n"
    // + "/*@ label bar */ \n"
    // + "x = /*@ symbolic */ {y|->z| y < z } \n"
    // + "/*@ desc prop */ \n"
    // + "SETS A;B={a,b} /*@ desc trololo !!! */;C END";
    // String input =
    // "MACHINE foo PROPERTIES /*@ label foo */ x = /*@ symbolic */ {y|->z|
    // y < z } END";
    String input = "MACHINE foo CONSTANTS c /*@ desc konstante nummero uno */ PROPERTIES c = 5  VARIABLES x /*@ desc Hallo du variable */ INVARIANT x=1 INITIALISATION x:= 1 END";
    BLexer lex = new BLexer(new PushbackReader(new StringReader(input), 500));
    Token t;
    while (!((t = lex.next()) instanceof EOF)) {
        System.out.print(t.getClass().getSimpleName() + "(" + t.getText() + ")");
        System.out.print(" ");
    }
    BParser p = new BParser();
    System.out.println("\n" + input);
    Start ast = p.parse(input, false);
    ASTPrinter pr = new ASTPrinter();
    ast.apply(pr);
    System.out.println(printAST(ast));
}
Also used : BLexer(de.be4.classicalb.core.parser.BLexer) Start(de.be4.classicalb.core.parser.node.Start) ASTPrinter(de.be4.classicalb.core.parser.visualisation.ASTPrinter) StringReader(java.io.StringReader) Token(de.be4.classicalb.core.parser.node.Token) BParser(de.be4.classicalb.core.parser.BParser) EOF(de.be4.classicalb.core.parser.node.EOF) PushbackReader(java.io.PushbackReader) Test(org.junit.Test)

Example 9 with EOF

use of de.be4.classicalb.core.parser.node.EOF in project probparsers by bendisposto.

the class PreParser method determineDependencies.

private Map<String, Set<String>> determineDependencies(Set<String> definitionNames, Map<Token, Token> definitions) throws PreParseException {
    HashMap<String, Set<String>> dependencies = new HashMap<>();
    for (Entry<Token, Token> entry : definitions.entrySet()) {
        Token nameToken = entry.getKey();
        Token rhsToken = entry.getValue();
        // The FORMULA_PREFIX is needed to switch the lexer state from
        // section to normal. Note, that we do not parse the right hand side
        // of the definition here. Hence FORMULA_PREFIX has no further
        // meaning and substitutions can also be handled by the lexer.
        final Reader reader = new StringReader(BParser.FORMULA_PREFIX + "\n" + rhsToken.getText());
        final BLexer lexer = new BLexer(new PushbackReader(reader, BLexer.PUSHBACK_BUFFER_SIZE), new DefinitionTypes());
        lexer.setParseOptions(parseOptions);
        Set<String> set = new HashSet<>();
        de.be4.classicalb.core.parser.node.Token next = null;
        try {
            next = lexer.next();
            while (!(next instanceof EOF)) {
                if (next instanceof TIdentifierLiteral) {
                    TIdentifierLiteral id = (TIdentifierLiteral) next;
                    String name = id.getText();
                    if (definitionNames.contains(name)) {
                        set.add(name);
                    }
                }
                next = lexer.next();
            }
        } catch (IOException e) {
        } catch (BLexerException e) {
            de.be4.classicalb.core.parser.node.Token errorToken = e.getLastToken();
            final String newMessage = determineNewErrorMessageWithCorrectedPositionInformations(nameToken, rhsToken, errorToken, e.getMessage());
            throw new PreParseException(newMessage);
        } catch (de.be4.classicalb.core.parser.lexer.LexerException e) {
            final String newMessage = determineNewErrorMessageWithCorrectedPositionInformationsWithoutToken(nameToken, rhsToken, e.getMessage());
            throw new PreParseException(newMessage);
        }
        dependencies.put(nameToken.getText(), set);
    }
    return dependencies;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Reader(java.io.Reader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) Token(de.be4.classicalb.core.preparser.node.Token) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) StringReader(java.io.StringReader) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) EOF(de.be4.classicalb.core.parser.node.EOF) HashSet(java.util.HashSet) IOException(java.io.IOException) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) PushbackReader(java.io.PushbackReader)

Example 10 with EOF

use of de.be4.classicalb.core.parser.node.EOF in project probparsers by bendisposto.

the class BParser method eparse.

public Start eparse(String input, IDefinitions context) throws BCompoundException, LexerException, IOException {
    final Reader reader = new StringReader(input);
    Start ast = null;
    List<String> ids = new ArrayList<>();
    final DefinitionTypes defTypes = new DefinitionTypes();
    defTypes.addAll(context.getTypes());
    BLexer bLexer = new BLexer(new PushbackReader(reader, BLexer.PUSHBACK_BUFFER_SIZE), defTypes);
    bLexer.setParseOptions(parseOptions);
    Token t;
    do {
        t = bLexer.next();
        if (t instanceof TIdentifierLiteral) {
            if (!ids.contains(t.getText())) {
                ids.add(t.getText());
            }
        }
    } while (!(t instanceof EOF));
    Parser p = new Parser(new EBLexer(input, BigInteger.ZERO, ids, defTypes));
    boolean ok;
    try {
        ast = p.parse();
        ok = true;
    } catch (Exception e) {
        handleException(e);
        ok = false;
    }
    BigInteger b = new BigInteger("2");
    b = b.pow(ids.size());
    b = b.subtract(BigInteger.ONE);
    while (!ok && b.compareTo(BigInteger.ZERO) > 0) {
        p = new Parser(new EBLexer(input, b, ids, defTypes));
        try {
            ast = p.parse();
            ok = true;
        } catch (ParserException e) {
            b = b.subtract(BigInteger.ONE);
            handleException(e);
        }
    }
    return ast;
}
Also used : ParserException(de.be4.classicalb.core.parser.parser.ParserException) Start(de.be4.classicalb.core.parser.node.Start) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) Token(de.be4.classicalb.core.parser.node.Token) TIdentifierLiteral(de.be4.classicalb.core.parser.node.TIdentifierLiteral) LexerException(de.be4.classicalb.core.parser.lexer.LexerException) IOException(java.io.IOException) ParserException(de.be4.classicalb.core.parser.parser.ParserException) PushbackReader(java.io.PushbackReader) Parser(de.be4.classicalb.core.parser.parser.Parser) StringReader(java.io.StringReader) BigInteger(java.math.BigInteger) EOF(de.be4.classicalb.core.parser.node.EOF)

Aggregations

Start (de.be4.classicalb.core.parser.node.Start)8 EOF (de.be4.classicalb.core.parser.node.EOF)6 PushbackReader (java.io.PushbackReader)5 StringReader (java.io.StringReader)5 Token (de.be4.classicalb.core.parser.node.Token)4 BLexer (de.be4.classicalb.core.parser.BLexer)3 BParser (de.be4.classicalb.core.parser.BParser)3 ASTPrinter (de.be4.classicalb.core.parser.visualisation.ASTPrinter)3 Test (org.junit.Test)3 TIdentifierLiteral (de.be4.classicalb.core.parser.node.TIdentifierLiteral)2 IOException (java.io.IOException)2 Reader (java.io.Reader)2 Helpers.getTreeAsString (util.Helpers.getTreeAsString)2 BLexerException (de.be4.classicalb.core.parser.exceptions.BLexerException)1 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)1 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)1 AAbstractMachineParseUnit (de.be4.classicalb.core.parser.node.AAbstractMachineParseUnit)1 AConstantsMachineClause (de.be4.classicalb.core.parser.node.AConstantsMachineClause)1 AEmptySetExpression (de.be4.classicalb.core.parser.node.AEmptySetExpression)1 AExpressionParseUnit (de.be4.classicalb.core.parser.node.AExpressionParseUnit)1