Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("wtf dude give some files: <output> <file.lah...>");
            }
            var outname = args[0];
            var sources = new string[] {
                "stdlib/mem.lah",
                "stdlib/collections.lah",
                "stdlib/math.lah"
            }.Concat(args.Skip(1));

            foreach (var file in args.Skip(1))
            {
                Console.WriteLine($"Compiling {file}");
            }
            var multiSource = new MultiCodeSource(sources.Select(x => CodeSource.FromFile(x)).ToArray());

            Console.WriteLine(multiSource.Content);
            var parser  = new LahdaParser(new LahdaLexer(new CompilationConfiguration(multiSource)));
            var output  = new StringBuilderOutput();
            var codeGen = new CodeGenerator(output, parser.Root());

            codeGen.Build();
            if (File.Exists(outname))
            {
                File.Delete(outname);
            }
            File.WriteAllText(outname, output.ToString());
        }
Exemplo n.º 2
0
        public string TestScriban()
        {
            // We could use the following simpler version, but we demonstrate the use of PushGlobal/PopGlobal object context
            // for a slightly higher efficiency and the reuse of a TemplateContext on the same thread
            //return _scribanTemplate.Render(new { products = _dotLiquidProducts });
            _templateContext.BuiltinObject.SetValue("products", _scribanProducts, false);
            _templateContext.PushOutput(StringBuilderOutput.GetThreadInstance());
            var result = _scribanTemplate.Render(_templateContext);

            _templateContext.PopOutput();
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Renders this template using the specified context. See remarks.
        /// </summary>
        /// <param name="context">The template context.</param>
        /// <exception cref="ArgumentNullException">If context is null</exception>
        /// <exception cref="InvalidOperationException">If the template <see cref="HasErrors"/>. Check the <see cref="Messages"/> property for more details</exception>
        /// <remarks>
        /// When using this method, the result of rendering this page is output to <see cref="TemplateContext.Output"/>
        /// </remarks>
        public string Render(TemplateContext context)
        {
            EvaluateAndRender(context, true);
            string result = context.Output.ToString();
            StringBuilderOutput output = context.Output as StringBuilderOutput;

            if (output != null)
            {
                output.Builder.Length = 0;
            }

            return(result);
        }
Exemplo n.º 4
0
        public sealed override string ToString()
        {
            var strOutput = new StringBuilderOutput();
            var printer   = new ScriptPrinter(strOutput, new ScriptPrinterOptions()
            {
                Mode = ScriptMode.ScriptOnly
            });

            printer.Write(this);
            var result = strOutput.ToString();

            strOutput.Builder.Length = 0;
            return(result);
        }
        public void CodeGenerator_should_generate_godlike_code(string content, int i)
        {
            var codeSource = CodeSource.FromMemory(content);
            var parser     = new LahdaParser(new LahdaLexer(new CompilationConfiguration(codeSource)));
            var output     = new StringBuilderOutput();
            var codeGen    = new CodeGenerator(output, parser.Root());

            codeGen.Build();
            var path = $"test_{i}.s";

            if (File.Exists(path))
            {
                File.Delete(path);
            }
            File.WriteAllText(path, output.ToString());
        }
Exemplo n.º 6
0
        /// <summary>
        /// The evaluates a string as a scriban template or evaluate the passed function or return the passed value.
        /// </summary>
        /// <param name="context">The template context</param>
        /// <param name="span">The source span</param>
        /// <param name="value">The input value, either a scriban template in a string, or an alias function or directly a value.</param>
        /// <returns>The evaluation of the input value.</returns>
        /// <remarks>
        /// ```scriban-html
        /// {{ "This is a template text {{ 1 + 2 }}" | object.eval_template }}
        /// ```
        /// ```html
        /// This is a template text 3
        /// ```
        /// </remarks>
        public static object EvalTemplate(TemplateContext context, SourceSpan span, object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (value is string templateStr)
            {
                try
                {
                    var template = Template.Parse(templateStr, lexerOptions: new LexerOptions()
                    {
                        Lang = context.Language, Mode = ScriptMode.Default
                    });
                    var output = new StringBuilderOutput();
                    context.PushOutput(output);
                    try
                    {
                        context.Evaluate(template.Page);
                    }
                    finally
                    {
                        context.PopOutput();
                    }
                    return(output.ToString());
                }
                catch (Exception ex)
                {
                    throw new ArgumentException(ex.Message, nameof(value));
                }
            }

            if (value is IScriptCustomFunction function)
            {
                return(ScriptFunctionCall.Call(context, context.CurrentNode, function, false, null));
            }

            return(value);
        }
Exemplo n.º 7
0
 protected void outputCallback(string obj)
 {
     StringBuilderOutput.AppendLine(obj);
 }