public override void ExitFormat_function(YarnSpinnerV1Parser.Format_functionContext context)
            {
                // V1: [select {$gender} male="male" female="female" other="other"]
                //  function_name: "select" variable: "$gender" key_value_pair="male="male"..."
                //
                // V2: [select value={$gender} male="male" female="female" other="other"/]
                var formatFunctionType = context.function_name?.Text;
                var variableContext    = context.variable();

                if (formatFunctionType == null || variableContext == null)
                {
                    // Not actually a format function, but the parser may
                    // have misinterpreted it? Do nothing here.
                    return;
                }

                var variableName = variableContext.GetText();

                StringBuilder sb = new StringBuilder();

                sb.Append($"{formatFunctionType} value={{{variableName}}}");

                foreach (var kvp in context.key_value_pair())
                {
                    sb.Append($" {kvp.GetText()}");
                }

                sb.Append(" /");

                // '[' and ']' are tokens that wrap this format_function,
                // so we're just replacing its innards
                var originalLength = context.Stop.StopIndex + 1 - context.Start.StartIndex;
                var originalStart  = context.Start.StartIndex;
                var originalText   = this.contents.Substring(originalStart, originalLength);

                var replacement = new TextReplacement()
                {
                    Start           = context.Start.StartIndex,
                    StartLine       = context.Start.Line,
                    OriginalText    = originalText,
                    ReplacementText = sb.ToString(),
                    Comment         = "Format functions have been replaced with markup.",
                };

                // Deliver the replacement!
                this.replacementCallback(replacement);
            }
Beispiel #2
0
 /// <summary>
 /// Exit a parse tree produced by <see cref="YarnSpinnerV1Parser.format_function"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitFormat_function([NotNull] YarnSpinnerV1Parser.Format_functionContext context)
 {
 }
 /// <summary>
 /// Visit a parse tree produced by <see cref="YarnSpinnerV1Parser.format_function"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitFormat_function([NotNull] YarnSpinnerV1Parser.Format_functionContext context)
 {
     return(VisitChildren(context));
 }