Ejemplo n.º 1
0
        public override Verb Parse()
        {
            var block = new Block();
            var index = position;

            prefixOperatorParser  = new PrefixOperatorParser();
            valueParser           = new ValueParser();
            postfixOperatorParser = new PostfixOperatorParser(asStatement);
            infixOperatorParser   = new InfixOperatorParser();
            andOrParser           = new AndOrParser(Stop.PassAlong(stop, Structures));
            ignoreReturnsParser   = new IgnoreReturnsParser();
            ifExpressionParser    = new IfExpressionParser();

            IMaybe <int> newIndex;

            if (index < source.Length)
            {
                newIndex = isStopping(index);
                if (newIndex.IsSome)
                {
                    return(returnBlock(block, newIndex.Value));
                }

                newIndex = getTerm(block, index);
                if (newIndex.IsNone || newIndex.Value == index)
                {
                    return(null);
                }

                index = newIndex.Value;
            }

            while (index < source.Length)
            {
                newIndex = isStopping(index);
                if (newIndex.IsSome)
                {
                    return(returnBlock(block, newIndex.Value));
                }

                if (infixOperatorParser.Scan(source, index))
                {
                    block.Add(infixOperatorParser.Verb);
                    index = infixOperatorParser.Position;
                }
                else
                {
                    break;
                }

                newIndex = getTerm(block, index);
                if (newIndex.IsNone || newIndex.Value == index)
                {
                    return(null);
                }

                index = newIndex.Value;
            }

            if (index < source.Length)
            {
                newIndex = isStopping(index);
                if (newIndex.IsSome)
                {
                    return(returnBlock(block, newIndex.Value));
                }

                if (andOrParser.Scan(source, index))
                {
                    block.Add(andOrParser.Verb);
                    index = andOrParser.Position;
                }
            }

            return(returnBlock(block, index));
        }
Ejemplo n.º 2
0
 public static IMaybe <(Block, Block, int)> GetExpressionThenBlock(string source, int index)
 {
     return(GetExpression(source, index, Stop.ExpressionThenBlock())
            .Map(t => GetOneOrMultipleBlock(source, t.Item2).Map(t2 => (t.Item1, t2.Item1, t2.Item2))));
 }
Ejemplo n.º 3
0
        public static IMaybe <(Block, Block, int)> GetComparisand(string source, int index, Stop stop)
        {
            if (ExpressionParser.GetExpression(source, index, stop).If(out var expression, out var i))
            {
                (var comparisand, var condition) = GetComparisand(expression);
                return((comparisand, condition, i).Some());
            }

            return(MaybeFunctions.none <(Block, Block, int)>());
        }
Ejemplo n.º 4
0
        public static IMaybe <(Block, int)> GetExpression(string source, int index, Stop stop, bool asStatement = false)
        {
            var parser = new ExpressionParser(stop, asStatement);

            return(when(parser.Scan(source, index), () => (parser.Block, parser.Position)));
        }
Ejemplo n.º 5
0
 public static Stop PassAlong(Stop stop, EntityType color) => new Stop(stop.Pattern, stop.Consume, color);