001/**
002 *
003 * Copyright 2019 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jxmpp.strings.testframework;
018
019import org.parboiled.BaseParser;
020import org.parboiled.Parboiled;
021import org.parboiled.Rule;
022import org.parboiled.annotations.BuildParseTree;
023
024public class InvalidJidCorpusParser extends JidCorpusParser<InvalidJid> {
025
026        private static final InvalidJidCorpusParboiledParser PARSER = Parboiled.createParser(InvalidJidCorpusParboiledParser.class);
027
028        /**
029         * Construct a new valid JID corpus parser.
030         *
031         * @param input the string to parse.
032         */
033        public InvalidJidCorpusParser(String input) {
034                super(input, false);
035        }
036
037        /**
038         * Construct a new valid JID corpus parser.
039         *
040         * @param input the string to parse.
041         * @param tracing if tracing of the parser should be enabled.
042         */
043        public InvalidJidCorpusParser(String input, boolean tracing) {
044                super(input, tracing);
045        }
046
047        @Override
048        protected Rule getParserRootRule() {
049                return PARSER.Corpus();
050        }
051
052        @BuildParseTree
053        static class InvalidJidCorpusParboiledParser extends BaseParser<Object> {
054
055                Rule NoEndOfRecordText() {
056                        // TODO: Not sure if this does what it should do: Match text until the end of record sequence "<RS> <NL>" is
057                        // encountered.
058                        return ZeroOrMore(TestNot(String(END_OF_RECORD)), ANY);
059                }
060
061                Rule TextLine() {
062                        return ZeroOrMore(NoneOf("\n"));
063                }
064
065                Rule InvalidJidEntry() {
066                        return Sequence(
067                                        InvalidJidHeader(),
068                                        InvalidJid(),
069                                        push(new InvalidJid((String) pop()))
070                                );
071                }
072
073                Rule InvalidJidHeader() {
074                        return String("invalid jid:\n");
075                }
076
077                Rule InvalidJid() {
078                        return Sequence(
079                                        // Match and push the unnormalized JID.
080                                        NoEndOfRecordText(),
081                                        push(match()),
082                                        String(END_OF_RECORD)
083                                );
084                }
085
086                Rule CommentLine() {
087                        return Sequence(
088                                        TextLine(),
089                                        String("\n")
090                                );
091                }
092
093                Rule Entry() {
094                        return FirstOf(
095                                        InvalidJidEntry(),
096                                        CommentLine()
097                                );
098                }
099
100                Rule Corpus() {
101                        return ZeroOrMore(Entry());
102                }
103        }
104
105}