public Statement Parse(ParserArgument args)
 {
     return(_parse(args));
 }
Exemple #2
0
        public List <Statement> Parse(string text)
        {
            List <Statement> list = new List <Statement>();
            var lines             = text.Replace("\r", "").Split('\n');
            int indentnum         = 0;

            for (int i = 0; i < lines.Length; i++)
            {
                text = lines[i];
                Match  m;
                string indent;
                string comment;
                // get indent
                m      = Regex.Match(text, @"^(\s*)([^\s]?.*)$");
                indent = m.Groups[1].Value;
                text   = m.Groups[2].Value;
                // get comment
                m = Regex.Match(text, @"(\s*#.*)$");
                if (m.Success)
                {
                    comment = m.Groups[1].Value;
                    text    = text.Substring(0, text.Length - comment.Length);
                }
                else
                {
                    comment = string.Empty;
                    text    = text.Trim();
                }
                try
                {
                    // enumerate generators
                    var args = new ParserArgument();
                    args.Text      = text;
                    args.Formatter = new Formats.Formatter(_constants);
                    Statement st = null;
                    foreach (var parser in _parsers)
                    {
                        st = parser.Parse(args);
                        if (st != null)
                        {
                            indentnum += st.IndentThis;
                            st.Indent  = new string(' ', indentnum < 0 ? 0 : indentnum * 4);
                            st.Comment = comment;
                            list.Add(st);
                            indentnum += st.IndentNext;
                            break;
                        }
                    }
                    if (st == null)
                    {
                        throw new ParseException("格式错误", i);
                    }
                }
                catch (OverflowException)
                {
                    throw new ParseException("数值溢出", i);
                }
                catch (ParseException ex)
                {
                    ex.Index = i;
                    throw;
                }
            }

            // update address
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Address = i;
            }

            // pair For & Next
            Stack <For> _fors = new Stack <For>();

            for (int i = 0; i < list.Count; i++)
            {
                var st = list[i];
                if (st is For)
                {
                    _fors.Push(st as For);
                }
                else if (st is Next)
                {
                    if (_fors.Count == 0)
                    {
                        throw new ParseException("找不到对应的For语句", i);
                    }
                    var @for = _fors.Pop();
                    var next = st as Next;
                    next.For  = @for;
                    @for.Next = next;
                }
                else if (st is LoopControl)
                {
                    if ((st as LoopControl).Level.Val > _fors.Count)
                    {
                        throw new ParseException("循环层数不足", i);
                    }
                }
            }
            if (_fors.Count > 0)
            {
                throw new ParseException("For语句需要Next结束", _fors.Peek().Address);
            }

            // pair If & Else & Endif
            Stack <If> _ifs = new Stack <If>();

            for (int i = 0; i < list.Count; i++)
            {
                var st = list[i];
                if (st is If)
                {
                    _ifs.Push(st as If);
                }
                else if (st is Else || st is EndIf)
                {
                    if (_ifs.Count == 0)
                    {
                        throw new ParseException("找不到对应的If语句", i);
                    }
                    var @if = _ifs.Peek();
                    if (st is Else)
                    {
                        if (@if.Else != null)
                        {
                            throw new ParseException("一个If只能对应一个Else", i);
                        }
                        var @else = st as Else;
                        @if.Else = @else;
                        @else.If = @if;
                    }
                    else
                    {
                        var endif = st as EndIf;
                        @if.EndIf = endif;
                        endif.If  = @if;
                        _ifs.Pop();
                    }
                }
            }
            if (_ifs.Count > 0)
            {
                throw new ParseException("If语句需要Endif结束", _ifs.Peek().Address);
            }

            return(list);
        }