コード例 #1
0
        public ParserResult Parse(ProgressiveList <Token> tokens)
        {
            var result = new ParserResult();

            var values = new ParserValues();

            BeginValues(values);
begin:
            foreach (var p in parsers)
            {
                var parser = (MainParser)p.Value;
                int state  = tokens.GetState();
                if (parser.Match(tokens))
                {
                    parser.Work(tokens, result);
                    result.Include(parser.GetResult());
                    goto begin;
                }
                else
                {
                    tokens.RestoreState(state);
                }
                values.Clear();
            }
            EndValues();

            return(result);
        }
コード例 #2
0
        public static ProgressiveList <T> Get(bool andReturn = false, int defSize = 1)
        {
            ProgressiveList <T> list;

            if (pool.Count == 0)
            {
                for (int i = 0; i < defSize; i++)
                {
                    pool.Enqueue(new ProgressiveList <T>(32));
                }
                list = new ProgressiveList <T>(32);
            }
            else
            {
                list = pool.Dequeue();
            }

            if (andReturn)
            {
                pool.Enqueue(list);
            }

            list.Clear();
            return(list);
        }
コード例 #3
0
 protected void DefWork(ProgressiveList <Token> tokens, ParserResult result)
 {
     if (child?.Match(tokens) ?? false)
     {
         child?.Work(tokens, result);
     }
 }
コード例 #4
0
        public ProgressiveList <T> SplitUntil(Predicate <T> predicate,
                                              bool includeTruth = true, bool removeSelf = true,
                                              int startingIndex = 0)
        {
            int count = items.Count;
            var pl    = new ProgressiveList <T>();

            for (var i = startingIndex; i < count; i++)
            {
                var  item  = items[i];
                bool truth = predicate(item);
                if (!truth || truth && includeTruth)
                {
                    pl.Add(item);
                }
                if (truth)
                {
                    break;
                }
            }
            if (removeSelf)
            {
                items.RemoveAll(i => pl.items.Contains(i));
            }
            return(pl);
        }
コード例 #5
0
 public override void Work(ProgressiveList <Token> tokens, ParserResult result)
 {
     if (Child.Match(tokens))
     {
         Child.Work(tokens, result);
     }
     else
     {
         tokens.This().Or(tokens.Look_Back()).Error();
     }
 }
コード例 #6
0
 public override void Work(ProgressiveList <Token> tokens, ParserResult result)
 {
     if (Conditional.Match(tokens))
     {
         var work = onWork();
         if (work != null)
         {
             if (work.Match(tokens))
             {
                 work.Work(tokens, result);
             }
         }
     }
     Child?.Work(tokens, result);
 }
コード例 #7
0
        public bool VerifyNexts(ProgressiveList <T> nexts)
        {
            T    def   = default(T);
            bool ret   = true;
            int  state = GetState();
            T    item;

            while (!Equals(item = nexts.Next(), def))
            {
                var nitem = nexts.Next();
                if (Equals(nitem, def) || !Equals(item, nitem))
                {
                    ret = false;
                    break;
                }
            }
            RestoreState(state);
            return(ret);
        }
コード例 #8
0
        public override bool Match(ProgressiveList <Token> tokens)
        {
            Token token;
            bool  result;

            switch (condType)
            {
            case TokenConditional.Text:
                return(tokens.Next().text == text);

            case TokenConditional.Type:
                token  = tokens.Next();
                result = token.Type() == type;
                if (result && storeOn != null)
                {
                    Set(storeOn, token.value);
                }
                return(result);

            case TokenConditional.Value:
                token  = tokens.Next();
                result = Tokenizer.isValue(token.Type());
                if (result && storeOn != null)
                {
                    Set(storeOn, token.value);
                }
                return(result);

            case TokenConditional.Any:
                token  = tokens.Next();
                result = true;
                if (storeOn != null)
                {
                    Set(storeOn, token.value);
                }
                return(result);
            }
            return(false);
        }
コード例 #9
0
 public bool VerifyNexts(ProgressiveList <Token> nexts)
 {
     return(tokens.VerifyNexts(nexts));
 }
コード例 #10
0
        public bool VerifyNexts(ProgressiveList <string> nexts, bool dontWorryAboutOrder = false,
                                Func <Token, Tokens, bool> checker = null)
        {
            bool ret   = true;
            int  state = GetState();
            bool begun = true;

            if (dontWorryAboutOrder)
            {
                begun = false;
            }
            Token token;

            while ((token = tokens.Next()) != null)
            {
                var ntoken = nexts.Next();
                if (ntoken == null)
                {
                    if (!begun)
                    {
                        return(false);
                    }
                    return(ret);
                }
                if (token.text != ntoken)
                {
                    if (!begun)
                    {
                        nexts.Back();
                        continue;
                    }
                    ret = false;
                    break;
                }
                else
                {
                    if (!begun)
                    {
                        if (checker == null)
                        {
                            begun = true;
                        }
                        else if (checker != null)
                        {
                            int prev = GetState();
                            if (!checker(token, this))
                            {
                                nexts.Back();
                            }
                            else
                            {
                                begun = true;
                                return(true);
                            }
                            RestoreState(prev);
                        }
                    }
                }
            }
            RestoreState(state);
            if (!begun)
            {
                ret = false;
            }
            return(ret);
        }
コード例 #11
0
 public Tokens(List <Token> tokens)
 {
     this.tokens = new ProgressiveList <Token>(tokens);
 }
コード例 #12
0
 public Tokens(ProgressiveList <Token> tokens)
 {
     this.tokens = tokens;
 }
コード例 #13
0
 public static void Return(ProgressiveList <T> list)
 {
     list.Clear();
     pool.Enqueue(list);
 }
コード例 #14
0
 public static void Return <T>(this ProgressiveList <T> array) =>
 ProgressiveLists <T> .Return(array);
コード例 #15
0
 public override bool Match(ProgressiveList <Token> tokens)
 {
     return(true);
 }
コード例 #16
0
 public override bool Match(ProgressiveList <Token> tokens)
 {
     return(Conditional.Match(tokens));
 }
コード例 #17
0
 public virtual bool Match(ProgressiveList <Token> tokens)
 {
     return(false);
 }
コード例 #18
0
 public override void Work(ProgressiveList <Token> tokens, ParserResult result)
 {
     DefWork(tokens, result);
 }
コード例 #19
0
 public virtual void Work(ProgressiveList <Token> tokens, ParserResult result)
 {
 }