Search in sources :

Example 6 with Parser

use of de.be4.eventb.core.parser.parser.Parser in project probparsers by bendisposto.

the class RulesMachineChecker method caseADefinitionExpression.

@Override
public void caseADefinitionExpression(ADefinitionExpression node) {
    node.getDefLiteral().apply(this);
    final String defName = node.getDefLiteral().getText();
    if ("READ_XML_FROM_STRING".equals(defName)) {
        if (node.getParameters().size() != 1) {
            errorList.add(new CheckException("The external function 'READ_XML_FROM_STRING' requires exactly one argrument.", node));
            return;
        }
        PExpression pExpression = node.getParameters().get(0);
        if (pExpression instanceof AStringExpression) {
            AStringExpression aStringExpr = (AStringExpression) pExpression;
            TStringLiteral content = aStringExpr.getContent();
            String text = content.getText();
            int xmlStartIndex = text.indexOf("<?");
            if (xmlStartIndex == -1) {
                return;
            }
            String testString = text.substring(0, xmlStartIndex);
            int numberOfNewLines = testString.length() - testString.replace("\n", "").length();
            try {
                InputSource inputSource = new InputSource(new StringReader(text.trim()));
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser saxParser = factory.newSAXParser();
                Locale newLocale = new Locale("en", "GB");
                // Surprisingly, we need both of the following two lines in
                // order to obtain all error messages in English.
                java.util.Locale.setDefault(newLocale);
                saxParser.setProperty("http://apache.org/xml/properties/locale", newLocale);
                saxParser.parse(inputSource, new DefaultHandler());
            } catch (SAXParseException e) {
                final int line = content.getLine() + numberOfNewLines + e.getLineNumber() - 1;
                final int column = (numberOfNewLines == 0 && e.getLineNumber() == 1) ? content.getPos() + e.getColumnNumber() : e.getColumnNumber();
                TStringLiteral dummy = new TStringLiteral("", line, column);
                String message = e.getMessage();
                errorList.add(new CheckException(message, dummy, e));
            } catch (SAXException e) {
                String message = e.getMessage();
                errorList.add(new CheckException(message, aStringExpr, e));
            } catch (ParserConfigurationException | IOException e) {
            /*
					 * We do nothing. The error is not handled by the parser but
					 * will be handled by the ProB prolog kernel.
					 */
            }
        }
    }
    super.caseADefinitionExpression(node);
}
Also used : Locale(java.util.Locale) InputSource(org.xml.sax.InputSource) CheckException(de.be4.classicalb.core.parser.exceptions.CheckException) AStringExpression(de.be4.classicalb.core.parser.node.AStringExpression) IOException(java.io.IOException) PExpression(de.be4.classicalb.core.parser.node.PExpression) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) TStringLiteral(de.be4.classicalb.core.parser.node.TStringLiteral) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 7 with Parser

use of de.be4.eventb.core.parser.parser.Parser in project probparsers by bendisposto.

the class ASTPrologTest method checkProlog.

private void checkProlog(final int counter, final String bspec, final String expected) throws BCompoundException {
    final BParser parser = new BParser("testcase");
    if (remove_restrictions) {
        parser.getOptions().setRestrictProverExpressions(false);
    }
    final Start startNode = parser.parse(bspec, false, new NoContentProvider());
    checkAST(counter, expected, startNode);
}
Also used : Start(de.be4.classicalb.core.parser.node.Start)

Example 8 with Parser

use of de.be4.eventb.core.parser.parser.Parser in project probparsers by bendisposto.

the class ParseableButProblematicOnWindows method testParsable.

@Test
public void testParsable() throws Exception {
    final BParser parser = new BParser(machine.getName());
    Start start = parser.parseFile(machine, false);
    start.apply(new PositionTester());
    assertNotNull(start);
}
Also used : Start(de.be4.classicalb.core.parser.node.Start) AbstractParseMachineTest(util.AbstractParseMachineTest) Test(org.junit.Test)

Example 9 with Parser

use of de.be4.eventb.core.parser.parser.Parser in project probparsers by bendisposto.

the class ParseableButProblematicOnWindowsWindowsLF method testParsable.

@Test
public void testParsable() throws Exception {
    final BParser parser = new BParser(machine.getName());
    Start start = parser.parseFile(machine, false);
    start.apply(new PositionTester());
    assertNotNull(start);
}
Also used : Start(de.be4.classicalb.core.parser.node.Start) AbstractParseMachineTest(util.AbstractParseMachineTest) Test(org.junit.Test)

Example 10 with Parser

use of de.be4.eventb.core.parser.parser.Parser in project probparsers by bendisposto.

the class PreParser method parse.

public void parse() throws PreParseException, IOException, BException, BCompoundException {
    final PreLexer preLexer = new PreLexer(pushbackReader);
    final Parser preParser = new Parser(preLexer);
    Start rootNode = null;
    try {
        rootNode = preParser.parse();
    } catch (final ParserException e) {
        if (e.getToken() instanceof de.be4.classicalb.core.preparser.node.TDefinitions) {
            final Token errorToken = e.getToken();
            final String message = "[" + errorToken.getLine() + "," + errorToken.getPos() + "] " + "Clause 'DEFINITIONS' is used more than once";
            throw new PreParseException(e.getToken(), message);
        } else {
            throw new PreParseException(e.getToken(), e.getLocalizedMessage());
        }
    } catch (final LexerException e) {
        throw new PreParseException(e.getLocalizedMessage());
    }
    final DefinitionPreCollector collector = new DefinitionPreCollector();
    rootNode.apply(collector);
    evaluateDefinitionFiles(collector.getFileDefinitions());
    List<Token> sortedDefinitionList = sortDefinitionsByTopologicalOrderAndCheckForCycles(collector.getDefinitions());
    evaluateTypes(sortedDefinitionList, collector.getDefinitions());
}
Also used : ParserException(de.be4.classicalb.core.preparser.parser.ParserException) Start(de.be4.classicalb.core.preparser.node.Start) Token(de.be4.classicalb.core.preparser.node.Token) DefinitionPreCollector(de.be4.classicalb.core.parser.analysis.checking.DefinitionPreCollector) Parser(de.be4.classicalb.core.preparser.parser.Parser) PreParseException(de.be4.classicalb.core.parser.exceptions.PreParseException) BLexerException(de.be4.classicalb.core.parser.exceptions.BLexerException) LexerException(de.be4.classicalb.core.preparser.lexer.LexerException)

Aggregations

Start (de.be4.classicalb.core.parser.node.Start)51 BParser (de.be4.classicalb.core.parser.BParser)41 Test (org.junit.Test)31 Ast2String (util.Ast2String)20 File (java.io.File)15 IOException (java.io.IOException)13 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)10 StringReader (java.io.StringReader)9 PushbackReader (java.io.PushbackReader)8 AbstractParseMachineTest (util.AbstractParseMachineTest)7 PrintStream (java.io.PrintStream)6 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)5 ParsingBehaviour (de.be4.classicalb.core.parser.ParsingBehaviour)4 PreParseException (de.be4.classicalb.core.parser.exceptions.PreParseException)4 Reader (java.io.Reader)4 ArrayList (java.util.ArrayList)4 IDefinitions (de.be4.classicalb.core.parser.IDefinitions)3 NodeIdAssignment (de.be4.classicalb.core.parser.analysis.prolog.NodeIdAssignment)3 Start (de.be4.eventbalg.core.parser.node.Start)3 LtlParseException (de.be4.ltl.core.parser.LtlParseException)3