Esempio n. 1
0
        public bool Parse()
        {
            if (_Lines.Count > 1)
            {
                Dictionary <string, NavNode <T, U> > nodes = new Dictionary <string, NavNode <T, U> >();
                foreach (var item in _Lines)
                {
                    var candidate = item.ToToken();
                    if (_NodeHS.Add(candidate))
                    {
                        NavNode <T, U> node = new NavNode <T, U>()
                        {
                            Display = item.Trim(), Token = candidate
                        };
                        nodes.Add(candidate, node);
                        _Nodes.Add(node);
                    }
                }
                Stack <LevelNode> stack   = new Stack <LevelNode>();
                int            step       = 4;
                int            max        = _Lines.Count - 1;
                int            linenumber = 0;
                string         line       = _Lines[linenumber];
                string         token      = line.ToToken();
                NavNode <T, U> master     = nodes[token];
                int            level      = Level(line, step);

                var levelnode = new LevelNode()
                {
                    Level = Level(line, step),
                    Token = token
                };
                stack.Push(levelnode);
                do
                {
                    line  = _Lines[++linenumber];
                    token = line.ToToken();
                    var next = new LevelNode()
                    {
                        Level = Level(line, step),
                        Token = token
                    };
                    if (next.Level == stack.Peek().Level) // next peer
                    {
                        stack.Pop();
                        if (stack.Count > 0)
                        {
                            var from = nodes[stack.Peek().Token];
                            var to   = nodes[next.Token];
                            _Links.Add(new NavLink <T, U>(from, to));
                        }
                        stack.Push(next);
                    }
                    else if (next.Level > stack.Peek().Level) // first minion
                    {
                        var from = nodes[stack.Peek().Token];
                        var to   = nodes[next.Token];
                        _Links.Add(new NavLink <T, U>(from, to));
                        stack.Push(next);
                    }
                    else if (next.Level < stack.Peek().Level) // ancestor peer
                    {
                        while (stack.Count > 0 && stack.Peek().Level >= next.Level)
                        {
                            stack.Pop();
                        }
                        if (stack.Count == 0)
                        {
                            stack.Push(next);
                        }
                        else
                        {
                            var from = nodes[stack.Peek().Token];
                            var to   = nodes[next.Token];
                            _Links.Add(new NavLink <T, U>(from, to));
                            stack.Push(next);
                        }
                    }
                } while (linenumber < max);

                _CanParse = true;
            }
            return(_CanParse);
        }
Esempio n. 2
0
 public NavLink(NavNode <T, U> from, NavNode <T, U> to)
 {
     From = from;
     To   = to;
 }