コード例 #1
0
ファイル: ZingParser.cs プロジェクト: zhaowei19920524/P
 public bool Parse(
     Configuration config,
     Stream quoteStream,
     SourcePositioner positioner,
     out AST <Node> results,
     out List <Flag> flags)
 {
     throw new NotImplementedException();
 }
コード例 #2
0
 public bool Parse(
     Configuration config,
     System.IO.Stream quoteStream,
     SourcePositioner positioner,
     out AST <Microsoft.Formula.API.Nodes.Node> results,
     out List <Flag> flags)
 {
     flags = new List <Flag>();
     throw new NotImplementedException();
 }
コード例 #3
0
ファイル: ListParser.cs プロジェクト: thisiscam/formula
        public bool Parse(
            Configuration config,
            Stream quoteStream,
            SourcePositioner positioner,
            out AST <Node> results,
            out List <Flag> flags)
        {
            results = null;
            flags   = new List <Flag>();
            var lines   = new LinkedList <string>();
            var revList = new LinkedList <AST <Node> >();

            using (var sr = new StreamReader(quoteStream))
            {
                while (!sr.EndOfStream)
                {
                    lines.AddLast(sr.ReadLine());
                }
            }

            char   c;
            string token = string.Empty;
            var    state = ParserState.None;
            int    lineNum = 0, colNum = 0;
            int    sL = 0, sC = 0;

            foreach (var line in lines)
            {
                for (colNum = 0; colNum < line.Length; ++colNum)
                {
                    c = line[colNum];
                    if (char.IsDigit(c))
                    {
                        token += c;
                        if (state == ParserState.None)
                        {
                            state = ParserState.Nat;
                            sL    = lineNum;
                            sC    = colNum;
                        }
                    }
                    else if (c == idPrefix)
                    {
                        if (state == ParserState.None)
                        {
                            token = idPrefixStr;
                            state = ParserState.Id;
                            sL    = lineNum;
                            sC    = colNum;
                        }
                        else
                        {
                            flags.Add(new Flag(
                                          SeverityKind.Error,
                                          positioner.GetSourcePosition(lineNum, colNum, lineNum, colNum),
                                          Constants.QuotationError.ToString("Unexpected character " + c),
                                          Constants.QuotationError.Code));
                            return(false);
                        }
                    }
                    else if (c == ' ' || c == '\t')
                    {
                        if (state == ParserState.Nat)
                        {
                            Contract.Assert(token.Length > 0);
                            Rational r;
                            Rational.TryParseFraction(token, out r);
                            revList.AddFirst(Factory.Instance.MkCnst(r, positioner.GetSourcePosition(sL, sC, lineNum, colNum - 1)));
                            token = string.Empty;
                        }
                        else if (state == ParserState.Id)
                        {
                            if (token.Length < 2)
                            {
                                flags.Add(new Flag(
                                              SeverityKind.Error,
                                              positioner.GetSourcePosition(lineNum, colNum, lineNum, colNum),
                                              Constants.QuotationError.ToString("Bad id"),
                                              Constants.QuotationError.Code));
                                return(false);
                            }

                            revList.AddFirst(Factory.Instance.MkId(token, positioner.GetSourcePosition(sL, sC, lineNum, colNum - 1)));
                            token = string.Empty;
                        }

                        state = ParserState.None;
                    }
                    else
                    {
                        flags.Add(new Flag(
                                      SeverityKind.Error,
                                      positioner.GetSourcePosition(lineNum, colNum, lineNum, colNum),
                                      Constants.QuotationError.ToString("Unexpected character " + c),
                                      Constants.QuotationError.Code));
                        return(false);
                    }
                }

                if (state == ParserState.Nat)
                {
                    Contract.Assert(token.Length > 0);
                    Rational r;
                    Rational.TryParseFraction(token, out r);
                    revList.AddFirst(Factory.Instance.MkCnst(r, positioner.GetSourcePosition(sL, sC, lineNum, colNum)));
                }
                else if (state == ParserState.Id)
                {
                    if (token.Length < 2)
                    {
                        flags.Add(new Flag(
                                      SeverityKind.Error,
                                      positioner.GetSourcePosition(lineNum, colNum, lineNum, colNum),
                                      Constants.QuotationError.ToString("Bad id"),
                                      Constants.QuotationError.Code));
                        return(false);
                    }

                    revList.AddFirst(Factory.Instance.MkId(token, positioner.GetSourcePosition(sL, sC, lineNum, colNum)));
                }

                token = string.Empty;
                state = ParserState.None;
                ++lineNum;
            }

            if (revList.Count == 0)
            {
                results = Factory.Instance.MkId(nilName, positioner.GetSourcePosition(0, 0, 0, 0));
            }
            else
            {
                foreach (var item in revList)
                {
                    if (results == null)
                    {
                        results = item;
                    }
                    else
                    {
                        results = Factory.Instance.AddArg(
                            Factory.Instance.MkFuncTerm(Factory.Instance.MkId(consName, item.Node.Span), item.Node.Span),
                            results);
                        results = Factory.Instance.AddArg((AST <FuncTerm>)results, item, false);
                    }
                }
            }

            return(true);
        }