Ejemplo n.º 1
0
        public static IMaybe <(Block, int)> OneLineStatement(string source, int index)
        {
            var oldTabs = Tabs;

            Tabs = "/s*";
            var whenUnlessParser = new WhenUnlessParser();

            try
            {
                var parser = new StatementParser();
                if (parser.Scan(source, index))
                {
                    var verb = ReturnExpression.Convert(parser.Verb);
                    whenUnlessParser.OtherVerb = verb;
                    if (whenUnlessParser.Scan(source, parser.Position))
                    {
                        return((Inline(whenUnlessParser.Verb), whenUnlessParser.Position).Some());
                    }
                    else
                    {
                        return((Inline(verb), parser.Position).Some());
                    }
                }

                return(none <(Block, int)>());
            }
            finally
            {
                Tabs = oldTabs;
            }
        }
Ejemplo n.º 2
0
        public override Verb CreateVerb(string[] tokens)
        {
            Color(position, tokens[1].Length, KeyWords);

            AdvanceTabs();

            var statementParser = new StatementParser();
            var block           = new Block();

            var index = NextPosition;

            while (index < source.Length)
            {
                if (statementParser.Scan(source, index))
                {
                    index = statementParser.Position;
                    var verb = statementParser.Verb;
                    if (verb != null && !(verb is NullOp))
                    {
                        block.Add(verb);
                    }
                }
                else
                {
                    break;
                }
            }

            RegressTabs();

            overridePosition = index;
            Block            = block;
            return(block.PushedVerb);
        }
Ejemplo n.º 3
0
        public static IMaybe <(Block, int)> GetBlock(string source, int index, bool advanceTabs, InclusionType inclusion = InclusionType.Classes,
                                                     bool compileAll = false)
        {
            if (advanceTabs)
            {
                AdvanceTabs();
            }

            var whenUnlessParser = new WhenUnlessParser();
            var statementParser  = new StatementParser {
                Inclusions = inclusion
            };
            var block      = new Block();
            var continuing = true;

            while (continuing && index < source.Length)
            {
                continuing = false;
                foreach (var parserResult in statementParser.Parsers
                         .Where(parser => parser.Scan(source, index))
                         .Select(parser => parser.Result))
                {
                    whenUnlessParser.OtherVerb = parserResult.Verb;
                    if (whenUnlessParser.Scan(source, parserResult.Position))
                    {
                        block.Add(whenUnlessParser.Verb);
                        index = whenUnlessParser.Position;
                    }
                    else
                    {
                        block.Add(parserResult.Verb);
                        index = parserResult.Position;
                    }

                    continuing = true;
                    break;
                }

                if (continuing && index < source.Length)
                {
                    var endParser = new EndParser();
                    if (endParser.Scan(source, index))
                    {
                        index = endParser.Position;
                    }
                }
            }

            if (compileAll)
            {
                index.Must().BeGreaterThanOrEqual(source.Length).OrThrow("Compilation", () => $"Didn't understand {source.Drop(index)}");
            }

            if (advanceTabs)
            {
                RegressTabs();
            }

            return(maybe(block.Count > 0, () => (block, index)));
        }