Esempio n. 1
0
            /// <summary>
            /// Parse the input string and return a regular automaton
            /// </summary>
            /// <param name="pattern"></param>
            /// <returns></returns>
            public static RegularAutomaton parse(string pattern)
            {
                if (pattern == null)
                {
                    throw new PatternParseException("Pattern can not be null");
                }

                if (!pattern.StartsWith("^"))
                {
                    pattern = ".*" + pattern;
                }
                else
                {
                    pattern = pattern.Substring(1);
                }
                if (!pattern.EndsWith("$"))
                {
                    pattern = pattern + ".*";
                }
                else
                {
                    if (pattern.Length > 0)
                    {
                        pattern = pattern.Substring(0, pattern.Length - 1);
                    }
                }

                Lexer        lexer = new Lexer(pattern);
                RegularState end   = new FinalRegularState();
                RegularState start = pAlt(lexer, end);

                start.resolveGreedyEnd(new Set <RegularState>(), end);
                RegularAutomaton auto = new RegularAutomaton();

                auto.setStartState(start);
                auto.setEndState(end);
                if (lexer.token().type != Token.EOF)
                {
                    throw new PatternParseException(String.Format("Garbage token '%s' at #%d", lexer.token().text, lexer
                                                                  .charPos()));
                }
                return(auto);
            }
Esempio n. 2
0
 private RegularPattern(string pattern)
     : base(pattern)
 {
     automaton = Parser.parse(pattern);
 }