Example #1
0
        WTState nextState(WTState state, WTState fin, StringBuilder input, int pos)
        {
            if (state.isFinal)
            {
                return(state);
            }

            if (pos > input.Length)
            {
                return(fin);
            }

            foreach (WTTransition tran in state.transitions)
            {
                int nextPos = pos;

                if (tran.node != null)
                {
                    char c = (pos < input.Length) ? input[pos] : (char)0;
                    if (!tran.node.matchesCharacter(c))
                    {
                        if (c == 0)
                        {
                            return(fin);
                        }
                        continue;
                    }
                    else
                    {
                    }
                    nextPos += 1;
                }

                WTState s = nextState(tran.nextState, fin, input, nextPos);
                if (s != null && s.isFinal)
                {
                    if (tran.bypassNode != null)
                    {
                        input.Insert(nextPos, tran.bypassNode.character.ToString());
                    }

                    return(s);
                }
            }

            return(null);
        }
Example #2
0
        public string reformatString(string input)
        {
            StringBuilder builder = new StringBuilder(input);

            // empty strings are ok
            if (input == null || input.Equals(""))
            {
                return(input);
            }


            WTState initialState = new WTState();
            WTState finalState   = processNode(node, initialState, input.Length);

            WTState x = stateDSF(initialState, finalState, builder, 0);

            if (x == null)
            {
                return(null);
            }
            return(x.isFinal? builder.ToString() : null);
        }
Example #3
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 #4
0
 public PathEntry(WTState state, int position)
 {
     this.state       = state;
     this.vertexIndex = 0;
     this.position    = position;
 }
Example #5
0
 public PathEntry(WTState state)
 {
     this.state       = state;
     this.vertexIndex = 0;
     this.position    = 0;
 }
Example #6
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);
        }