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 ValidJidCorpusParser extends JidCorpusParser<ValidJid> {
025
026        private static final ValidJidCorpusParboiledParser PARSER = Parboiled.createParser(ValidJidCorpusParboiledParser.class);
027
028        /**
029         * Construct a new valid JID corpus parser.
030         *
031         * @param input the string to parse.
032         */
033        public ValidJidCorpusParser(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 ValidJidCorpusParser(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 ValidJidCorpusParboiledParser extends BaseParser<Object> {
054
055                Rule NoCtrlCharText() {
056                        return ZeroOrMore(NoneOf(CONTROL_CHARACTERS));
057                }
058
059                Rule NoCtrlCharNoNewLineText() {
060                        return ZeroOrMore(NoneOf(CONTROL_CHARACTERS_AND_NEWLINE));
061                }
062
063                Rule TextLine() {
064                        return ZeroOrMore(NoneOf("\n"));
065                }
066
067                Rule JidEntry() {
068                        return Sequence(
069                                        JidHeader(),
070                                        UnnormalizedJid(),
071                                        NormalizedJid(),
072                                        swap4(),
073                                        push(new ValidJid((String) pop(), (String) pop(), (String) pop(), (String) pop()))
074                                );
075                }
076
077                Rule JidHeader() {
078                        return String("jid:\n");
079                }
080
081                Rule UnnormalizedJid() {
082                        return Sequence(
083                                        // Match and push the unnormalized JID.
084                                        NoCtrlCharNoNewLineText(),
085                                        push(match()),
086                                        String(END_OF_RECORD)
087                                );
088                }
089
090                Rule NormalizedJid() {
091                        return Sequence(
092                                        // Match and push localpart.
093                                        NoCtrlCharText(),
094                                        push(match()),
095                                        String(ASCII_UNIT_SEPARATOR_UNICODE_INFORMATION_SEPARATOR_ONE),
096                                        // Match and push domainpart.
097                                        NoCtrlCharText(),
098                                        push(match()),
099                                        String(ASCII_UNIT_SEPARATOR_UNICODE_INFORMATION_SEPARATOR_ONE),
100                                        // Match and push resourcepart.
101                                        NoCtrlCharText(),
102                                        push(match()),
103                                        String(END_OF_RECORD)
104                                );
105                }
106
107                Rule CommentLine() {
108                        return Sequence(
109                                        TextLine(),
110                                        String("\n")
111                                );
112                }
113
114                Rule Entry() {
115                        return FirstOf(
116                                        JidEntry(),
117                                        CommentLine()
118                                );
119                }
120
121                Rule Corpus() {
122                        return ZeroOrMore(Entry());
123                }
124        }
125
126}