Ejemplo n.º 1
0
        public static Tok MoveIfOrThrow(this TokFlow flow, TokType tokType)
        {
            var cur = flow.Current;

            if (cur == null)
            {
                var prev = flow.Previous;
                if (prev == null)
                {
                    throw FunParseException.ErrorStubToDo($"\"{tokType}\" is missing at end of stream");
                }
                else
                {
                    throw FunParseException.ErrorStubToDo($"\"{tokType}\" is missing at end of stream");
                }
            }

            if (!cur.Is(tokType))
            {
                throw FunParseException.ErrorStubToDo(
                          $"\"{tokType}\" is missing but was \"{cur}\"");
            }

            flow.MoveNext();
            return(cur);
        }
Ejemplo n.º 2
0
        public static FunnyType ReadType(this TokFlow flow)
        {
            var cur      = flow.Current;
            var readType = ToFunnyType(cur);

            flow.MoveNext();
            var lastPosition = cur.Finish;

            while (flow.IsCurrent(TokType.ArrOBr))
            {
                if (flow.Current.Start != lastPosition)
                {
                    throw FunParseException.ErrorStubToDo("unexpected space before []");
                }

                flow.MoveNext();
                lastPosition = flow.Current.Finish;
                if (!flow.MoveIf(TokType.ArrCBr))
                {
                    throw ErrorFactory.ArrTypeCbrMissed(new Interval(cur.Start, flow.Current.Start));
                }
                readType = FunnyType.ArrayOf(readType);
            }

            return(readType);
        }
Ejemplo n.º 3
0
 public static bool MoveIf(this TokFlow flow, TokType tokType)
 {
     if (flow.IsCurrent(tokType))
     {
         flow.MoveNext();
         return(true);
     }
     return(false);
 }
Ejemplo n.º 4
0
        public static bool MoveIf(this TokFlow flow, TokType tokType, out Tok tok)
        {
            if (flow.IsCurrent(tokType))
            {
                tok = flow.Current;
                flow.MoveNext();
                return(true);
            }

            tok = null;
            return(false);
        }
Ejemplo n.º 5
0
        private static VarAttribute ReadAttributeOrThrow(this TokFlow flow)
        {
            var start = flow.Current.Start;

            flow.MoveNext();
            if (!flow.MoveIf(TokType.Id, out var id))
            {
                throw ErrorFactory.ItIsNotAnAttribute(start, flow.Current);
            }
            object val = null;

            if (flow.MoveIf(TokType.Obr))
            {
                var next = flow.Current;
                switch (next.Type)
                {
                case TokType.False:
                    val = false;
                    break;

                case TokType.True:
                    val = true;
                    break;

                case TokType.RealNumber:
                case TokType.HexOrBinaryNumber:
                case TokType.IntNumber:
                    val = TokenHelper.ToConstant(next.Value).Item1;
                    break;

                case TokType.Text:
                    val = next.Value;
                    break;

                default:
                    throw ErrorFactory.ItIsNotCorrectAttributeValue(next);
                }
                flow.MoveNext();
                if (!flow.MoveIf(TokType.Cbr))
                {
                    throw ErrorFactory.AttributeCbrMissed(start, flow);
                }
            }
            if (!flow.MoveIf(TokType.NewLine))
            {
                throw ErrorFactory.NowNewLineAfterAttribute(start, flow);
            }

            return(new VarAttribute(id.Value, val));
        }
Ejemplo n.º 6
0
        public static VarAttribute[] ReadAttributes(this TokFlow flow)
        {
            var attributes = new VarAttribute[0];

            if (!flow.IsCurrent(TokType.MetaInfo))
            {
                return(attributes);
            }

            bool newLine = flow.IsStart || flow.Previous.Is(TokType.NewLine);
            var  ans     = new List <VarAttribute>();

            while (flow.IsCurrent(TokType.MetaInfo))
            {
                if (!newLine)
                {
                    throw ErrorFactory.NowNewLineBeforeAttribute(flow);
                }

                ans.Add(ReadAttributeOrThrow(flow));
                flow.SkipNewLines();
            }
            return(ans.ToArray());
        }
Ejemplo n.º 7
0
 public static bool IsDoneOrEof(this TokFlow flow)
 => flow.IsDone || flow.IsCurrent(TokType.Eof);
Ejemplo n.º 8
0
 public static bool IsStartOfTheLine(this TokFlow flow)
 => flow.IsStart || flow.IsPrevious(TokType.NewLine);