Example #1
0
        WTState processNode(WTReNode node, WTState state, int length)
        {
            if (node is WTReEndOfString)
            {
                WTState finalState = new WTState();;
                finalState.isFinal = true;

                WTTransition tran = new WTTransition();
                tran.node      = (WTReCharacterBase)node;
                tran.nextState = finalState;
                state.transitions.Add(tran);

                return(finalState);
            }
            else if (node is WTReCharacterBase)
            {
                WTState finalState = new WTState();

                WTTransition tran = new WTTransition();;
                tran.node      = (WTReCharacterBase)node;
                tran.nextState = finalState;
                state.transitions.Add(tran);

                return(finalState);
            }
            else if (node is WTReQuantifier)
            {
                WTReQuantifier qtf = (WTReQuantifier)node;

                WTState curState = state;
                for (int i = 0; i < qtf.countFrom; i++)
                {
                    curState = processNode(qtf.child, curState, length);
                }

                if (qtf.countTo == qtf.countFrom)
                {
                    // strict quantifier
                    return(curState);
                }

                WTState finalState = new WTState();;

                for (int i = qtf.countFrom; i < Math.Min(qtf.countTo, length); i++)
                {
                    WTState nextState = processNode(qtf.child, curState, length);

                    WTTransition _tran = new WTTransition();
                    _tran.node      = null;
                    _tran.nextState = finalState;

                    if (qtf.greedy)
                    {
                        curState.transitions.Add(_tran);
                    }
                    else
                    {
                        curState.transitions.Insert(0, _tran);
                    }

                    curState = nextState;
                }

                WTTransition tran = new WTTransition();
                tran.node      = null;
                tran.nextState = finalState;
                curState.transitions.Add(tran);

                return(finalState);
            }
            else if (node is WTReGroup)
            {
                WTReGroup grp = (WTReGroup)node;

                WTState curState = state;
                for (int i = 0; i < grp.children.Count; i++)
                {
                    curState = processNode(grp.children[i], curState, length);
                }

                if (!grp.capturing && grp.children.Count == 1 && (grp.children[0] is WTReLiteral))
                {
                    WTTransition tran = new WTTransition();
                    tran.node       = null;
                    tran.bypassNode = (WTReLiteral)grp.children[0];
                    tran.nextState  = curState;
                    state.transitions.Add(tran);
                }

                return(curState);
            }
            else if (node is WTReAlteration)
            {
                WTReAlteration alt = (WTReAlteration)node;

                WTState finalState = new WTState();

                for (int i = 0; i < alt.children.Count; i++)
                {
                    WTState curState = processNode(alt.children[i], state, length);

                    WTTransition tran = new WTTransition();
                    tran.node      = null;
                    tran.nextState = finalState;
                    curState.transitions.Add(tran);
                }

                return(finalState);
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        WTState stateDSF(WTState initial, WTState fin, StringBuilder input, int startPos)
        {
            List <PathEntry> path = new List <PathEntry>();

            path.Add(new PathEntry(initial, startPos));

            while (path.Count > 0)
            {
                int depth = path.Count - 1;

                WTState parent    = path[depth].getState();
                int     vertexNum = path[depth].getVertex();
                int     position  = path[depth].getPosition();

                if (parent != null && parent.isFinal)
                {
                    try {
                        for (int i = path.Count - 1; i > 0; i--)
                        {
                            WTTransition tran = path[i - 1].getState().transitions[path[i - 1].getVertex() - 1];
                            if (tran.bypassNode != null)
                            {
                                input.Insert(path[i].getPosition(), tran.bypassNode.character.ToString());
                            }
                        }
                    } catch (Exception e) {
                        //e.printStackTrace();
                    }
                    return(parent);
                }

                if (position > input.Length)
                {
                    path[depth].setVertex(vertexNum + 1);
                    path.Add(new PathEntry(fin));
                    continue;
                }

                if (parent != null && parent.transitions.Count > vertexNum)
                {
                    WTTransition nextTransition = parent.transitions[vertexNum];

                    if (nextTransition.node != null)
                    {
                        char c = (position < input.Length) ? input[position] : (char)0;
                        if (!nextTransition.node.matchesCharacter(c))
                        {
                            if (c == 0)
                            {
                                path[depth].setVertex(vertexNum + 1);
                                path.Add(new PathEntry(fin));
                                continue;
                            }
                            else
                            {
                                path[depth].setVertex(vertexNum + 1);
                                continue;
                            }
                        }
                        else
                        {
                        }
                        position++;
                    }

                    path[depth].setVertex(vertexNum + 1);
                    path.Add(new PathEntry(nextTransition.nextState, position));
                }
                else
                {
                    path.RemoveAt(depth);
                }
            }
            return(null);
        }