Esempio n. 1
0
        // #if ...
        //  ^
        public static ReadOnlyMemory <char> ParseCodeScriptBlock(this ReadOnlyMemory <char> literal, ScriptContext context,
                                                                 out PageBlockFragment blockFragment)
        {
            literal = literal.ParseVarName(out var blockNameSpan);
            var endArgumentPos = literal.IndexOf('\n');
            var argument       = literal.Slice(0, endArgumentPos).Trim();

            literal = literal.Slice(endArgumentPos + 1);

            var blockName = blockNameSpan.ToString();

            var language = context.ParseAsLanguage.TryGetValue(blockName, out var lang)
                ? lang
                : ScriptCode.Language;

            if (language.Name == ScriptVerbatim.Language.Name)
            {
                literal = literal.ParseCodeBody(blockNameSpan, out var body);
                body    = body.ChopNewLine();

                blockFragment = language.ParseVerbatimBlock(blockName, argument, body);
                return(literal);
            }

            literal = literal.ParseCodeBody(blockNameSpan, out var bodyText);
            var bodyFragments = language.Parse(context, bodyText);

            var elseBlocks = new List <PageElseBlock>();

            literal = literal.AdvancePastWhitespace();
            while (literal.StartsWith("else"))
            {
                literal = literal.ParseCodeElseBlock(blockNameSpan, out var elseArgument, out var elseBody);

                var elseBlock = new PageElseBlock(elseArgument, language.Parse(context, elseBody));
                elseBlocks.Add(elseBlock);

                literal = literal.AdvancePastWhitespace();
            }

            blockFragment = new PageBlockFragment(blockName, argument, bodyFragments, elseBlocks);

            return(literal);
        }