⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 regexp.cs

📁 Grammatica is a C# and Java parser generator (compiler compiler). It improves upon simlar tools (lik
💻 CS
📖 第 1 页 / 共 2 页
字号:
         * @return the element representing this character set         *          * @throws RegExpException if an error was encountered in the          *             pattern string         */        private Element ParseCharSet() {            CharacterSetElement  charset;            Element              elem;            bool                 repeat = true;            char                 start;            char                 end;                        if (PeekChar(0) == '^') {                ReadChar('^');                charset = new CharacterSetElement(true);            } else {                charset = new CharacterSetElement(false);            }                        while (PeekChar(0) > 0 && repeat) {                start = (char) PeekChar(0);                switch (start) {                case ']':                    repeat = false;                    break;                case '\\':                    elem = ParseEscapeChar();                    if (elem is StringElement) {                        charset.AddCharacters((StringElement) elem);                    } else {                        charset.AddCharacterSet((CharacterSetElement) elem);                    }                    break;                default:                    ReadChar(start);                    if (PeekChar(0) == '-'                        && PeekChar(1) > 0                         && PeekChar(1) != ']') {                                                ReadChar('-');                        end = ReadChar();                        charset.AddRange(start, end);                    } else {                        charset.AddCharacter(start);                    }                    break;                }            }                    return charset;        }        /**         * Parses a regular expression character. This method handles          * a single normal character in a regular expression.         *          * @return the element representing this character         *          * @throws RegExpException if an error was encountered in the          *             pattern string         */        private Element ParseChar() {            switch (PeekChar(0)) {            case '\\':                return ParseEscapeChar();            case '^':            case '$':                throw new RegExpException(                    RegExpException.ErrorType.UNSUPPORTED_SPECIAL_CHARACTER,                    pos,                    pattern);            default:                return new StringElement(ReadChar());            }        }        /**         * Parses a regular expression character escape. This method          * handles a single character escape in a regular expression.         *          * @return the element representing this character escape         *          * @throws RegExpException if an error was encountered in the          *             pattern string         */        private Element ParseEscapeChar() {            char    c;            string  str;            int     value;                    ReadChar('\\');            c = ReadChar();            switch (c) {            case '0':                c = ReadChar();                if (c < '0' || c > '3') {                    throw new RegExpException(                        RegExpException.ErrorType.UNSUPPORTED_ESCAPE_CHARACTER,                        pos - 3,                        pattern);                }                value = c - '0';                c = (char) PeekChar(0);                if ('0' <= c && c <= '7') {                    value *= 8;                    value += ReadChar() - '0';                    c = (char) PeekChar(0);                    if ('0' <= c && c <= '7') {                        value *= 8;                        value += ReadChar() - '0';                    }                }                return new StringElement((char) value);            case 'x':                str = ReadChar().ToString() +                       ReadChar().ToString();                try {                    value = Int32.Parse(str,                                         NumberStyles.AllowHexSpecifier);                    return new StringElement((char) value);                } catch (FormatException) {                    throw new RegExpException(                        RegExpException.ErrorType.UNSUPPORTED_ESCAPE_CHARACTER,                        pos - str.Length - 2,                        pattern);                }            case 'u':                str = ReadChar().ToString() +                       ReadChar().ToString() +                      ReadChar().ToString() +                      ReadChar().ToString();                try {                    value = Int32.Parse(str,                                         NumberStyles.AllowHexSpecifier);                    return new StringElement((char) value);                } catch (FormatException) {                    throw new RegExpException(                        RegExpException.ErrorType.UNSUPPORTED_ESCAPE_CHARACTER,                        pos - str.Length - 2,                        pattern);                }            case 't':                return new StringElement('\t');            case 'n':                return new StringElement('\n');            case 'r':                return new StringElement('\r');            case 'f':                return new StringElement('\f');            case 'a':                return new StringElement('\u0007');            case 'e':                return new StringElement('\u001B');            case 'd':                return CharacterSetElement.DIGIT;            case 'D':                return CharacterSetElement.NON_DIGIT;            case 's':                return CharacterSetElement.WHITESPACE;            case 'S':                return CharacterSetElement.NON_WHITESPACE;            case 'w':                return CharacterSetElement.WORD;            case 'W':                return CharacterSetElement.NON_WORD;            default:                if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')) {                    throw new RegExpException(                        RegExpException.ErrorType.UNSUPPORTED_ESCAPE_CHARACTER,                        pos - 2,                        pattern);                             }                return new StringElement(c);            }        }        /**         * Reads a number from the pattern. If the next character isn't a         * numeric character, an exception is thrown. This method reads         * several consecutive numeric characters.          *          * @return the numeric value read         *          * @throws RegExpException if an error was encountered in the          *             pattern string         */        private int ReadNumber() {            StringBuilder  buf = new StringBuilder();            int            c;                        c = PeekChar(0);            while ('0' <= c && c <= '9') {                buf.Append(ReadChar());                c = PeekChar(0);            }            if (buf.Length <= 0) {                throw new RegExpException(                    RegExpException.ErrorType.UNEXPECTED_CHARACTER,                    pos,                    pattern);            }            return Int32.Parse(buf.ToString());        }        /**         * Reads the next character in the pattern. If no next character         * exists, an exception is thrown.         *          * @return the character read          *          * @throws RegExpException if no next character was available in           *             the pattern string         */        private char ReadChar() {            int  c = PeekChar(0);                        if (c < 0) {                throw new RegExpException(                    RegExpException.ErrorType.UNTERMINATED_PATTERN,                     pos,                    pattern);            } else {                pos++;                return (char) c;            }        }        /**         * Reads the next character in the pattern. If the character          * wasn't the specified one, an exception is thrown.         *          * @param c              the character to read         *          * @return the character read          *          * @throws RegExpException if the character read didn't match the         *             specified one, or if no next character was          *             available in the pattern string         */        private char ReadChar(char c) {            if (c != ReadChar()) {                throw new RegExpException(                    RegExpException.ErrorType.UNEXPECTED_CHARACTER,                     pos - 1,                    pattern);            }            return c;        }        /**         * Returns a character that has not yet been read from the          * pattern. If the requested position is beyond the end of the          * pattern string, -1 is returned.         *          * @param count          the preview position, from zero (0)         *          * @return the character found, or         *         -1 if beyond the end of the pattern string         */        private int PeekChar(int count) {            if (pos + count < pattern.Length) {                return pattern[pos + count];            } else {                return -1;            }        }            /**         * Combines a list of elements. This method takes care to always          * concatenate adjacent string elements into a single string          * element.           *          * @param list           the list with elements         *          * @return the combined element         */        private Element CombineElements(ArrayList list) {            Element  prev;            Element  elem;            string   str;            int      i;            // Concatenate string elements            prev = (Element) list[0];            for (i = 1; i < list.Count; i++) {                elem = (Element) list[i];                if (prev is StringElement                  && elem is StringElement) {                    str = ((StringElement) prev).GetString() +                          ((StringElement) elem).GetString();                    elem = new StringElement(str);                    list.RemoveAt(i);                    list[i - 1] = elem;                    i--;                }                prev = elem;            }            // Combine all remaining elements            elem = (Element) list[list.Count - 1];            for (i = list.Count - 2; i >= 0; i--) {                prev = (Element) list[i];                elem = new CombineElement(prev, elem);            }            return elem;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -