예제 #1
0
            /// <summary>
            ///  Process a word.
            /// </summary>
            /// <param name="lexer"></param>
            /// <param name="endState"></param>
            /// <returns></returns>
            private static RegularState pWord(Lexer lexer, RegularState endState)
            {
                Token t = lexer.token();

                lexer.nextToken();
                RegularState      result     = new RegularState();
                RegularTransition transition = new RegularTransition(result, endState);

                transition.addLabel(t.ToString());
                return(result);
            }
예제 #2
0
            /// <summary>
            /// Process sequences of words and subexpressions
            /// </summary>
            /// <param name="lexer"></param>
            /// <param name="endState"></param>
            /// <returns></returns>
            private static RegularState pSeq(Lexer lexer, RegularState endState)
            {
                RegularState result = null;
                RegularState lhs    = null;

                while (lexer.token().type == Token.WORD || lexer.token().type == Token.NOT ||
                       lexer.token().type == Token.PLEFT || lexer.token().type == Token.DOT)
                {
                    RegularState rhs;
                    if (lexer.token().type == Token.WORD)
                    {
                        rhs = pWord(lexer, endState);
                    }
                    else if (lexer.token().type == Token.DOT)
                    {
                        lexer.nextToken();
                        rhs = new RegularState();
                        RegularTransition transition = new RegularTransition(rhs, endState);
                        transition.addLabel(RegularTransition.WILDCARD);
                        multTransform(rhs, lexer, endState);
                    }
                    else if (lexer.token().type == Token.NOT)
                    {
                        // ![word1,word2,word3,word4,...]
                        rhs = new RegularState();
                        RegularTransition transition = new RegularTransition(rhs, endState);
                        transition.setNegation(true);
                        lexer.nextToken();
                        while (lexer.token().type == Token.WORD)
                        {
                            transition.addLabel(lexer.token().ToString());
                            lexer.nextToken();
                            if (lexer.token().type == Token.COMMA)
                            {
                                lexer.nextToken();
                            }
                        }
                        if (lexer.token().type != Token.SRIGHT)
                        {
                            throw new PatternParseException(String.Format("Missing right square bracket at #%d", lexer
                                                                          .charPos()));
                        }
                        lexer.nextToken();
                        multTransform(rhs, lexer, endState);
                    }
                    else
                    {
                        rhs = pSubexp(lexer, endState);
                    }
                    if (result == null)
                    {
                        // first item in the sequence is the return value;
                        result = rhs;
                    }
                    if (lhs != null)
                    {
                        // link end state of previous item in the list to current
                        // item
                        replaceStates(lhs, endState, rhs);
                    }
                    lhs = rhs;
                }
                if (lexer.token().type == Token.EOF && result == null)
                {
                    // empty regex
                    return(endState);
                }
                if (result == null)
                {
                    throw new PatternParseException(String.Format("Unexpected token '%s' at #%d", lexer.token().text, lexer
                                                                  .charPos()));
                }
                return(result);
            }