Exemplo 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;
            }
        }
Exemplo n.º 2
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)));
        }