<commit_before>using Alphaleonis.Horseshoe.Grammars;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Alphaleonis.Horseshoe.Compiler
{
   class HorseshoeTranslationListener : HorseshoeParserBaseListener
   {
      private const string VAR_TemplateResult = "$templateResult";
      private const string VAR_DataContext = "$dataContext";

      private StringWriter m_result = new StringWriter();
      private IndentedTextWriter m_writer;
      private readonly SymbolTable m_symbols = new SymbolTable();
      private readonly StringBuilder m_buffer = new StringBuilder();
      private bool m_inModule;
      private bool m_trimLeadingWhitespaceFromBody;
      private bool m_invertTrim;

      public string Result
      {
         get
         {
            return m_result.ToString();
         }
      }

      public HorseshoeTranslationListener()
      {
         m_symbols.AddSymbol(new Symbol(VAR_DataContext, null));
         m_symbols.AddSymbol(new Symbol(VAR_TemplateResult, null));
         m_writer = new IndentedTextWriter(m_result, "   ");
      }

      public override void EnterModule(HorseshoeParser.ModuleContext context)
      {
         m_writer.WriteLine("module {0} {{", context.name.GetText());
         PushIndent();
         m_symbols.PushScope(context);
         m_inModule = true;
      }

      public override void ExitModule(HorseshoeParser.ModuleContext context)
      {
         PopIndent();
         m_writer.WriteLine("}");
         m_symbols.PopScope();
         m_inModule = false;
      }

      public override void EnterTemplateDecl(HorseshoeParser.TemplateDeclContext context)
      {
         if (m_inModule)
            m_writer.Write("export ");

         if (context.name == null)
            throw new Exception("No name specified for template.");

         m_writer.WriteLine("module {0} {{", context.name.GetText());
         PushIndent();
         m_writer.WriteLine("function render({0} : {1}) : string {{", VAR_DataContext, context.contextType.GetText());
         PushIndent();
         m_writer.WriteLine("var {0} : string = '';", VAR_TemplateResult);
         m_symbols.PushScope(context);
         m_invertTrim = context.invertTrim != null;
         m_trimLeadingWhitespaceFromBody = context.openTrimEnd != null;

      }

      public override void ExitTemplateDecl(HorseshoeParser.TemplateDeclContext context)
      {
         FlushBuffer(context.closeTrimStart != null);
         m_writer.WriteLine("return {0}", VAR_TemplateResult);
         PopIndent();
         m_writer.WriteLine("}");
         PopIndent();
         m_writer.WriteLine("}");

         m_symbols.PopScope();
      }

      public override void EnterBody(HorseshoeParser.BodyContext context)
      {
         string text = context.GetText();
         if (m_trimLeadingWhitespaceFromBody ^ m_invertTrim)
         {<commit_msg>Fixed some incorrect code generation.<commit_after>using Alphaleonis.Horseshoe.Grammars;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Alphaleonis.Horseshoe.Compiler
{
   class HorseshoeTranslationListener : HorseshoeParserBaseListener
   {
      private const string VAR_TemplateResult = "$templateResult";
      private const string VAR_DataContext = "$dataContext";

      private StringWriter m_result = new StringWriter();
      private IndentedTextWriter m_writer;
      private readonly SymbolTable m_symbols = new SymbolTable();
      private readonly StringBuilder m_buffer = new StringBuilder();
      private bool m_inModule;
      private bool m_trimLeadingWhitespaceFromBody;
      private bool m_invertTrim;

      public string Result
      {
         get
         {
            return m_result.ToString();
         }
      }

      public HorseshoeTranslationListener()
      {
         m_symbols.AddSymbol(new Symbol(VAR_DataContext, null));
         m_symbols.AddSymbol(new Symbol(VAR_TemplateResult, null));
         m_writer = new IndentedTextWriter(m_result, "   ");
      }

      public override void EnterModule(HorseshoeParser.ModuleContext context)
      {
         m_writer.WriteLine("module {0} {{", context.name.GetText());
         PushIndent();
         m_symbols.PushScope(context);
         m_inModule = true;
      }

      public override void ExitModule(HorseshoeParser.ModuleContext context)
      {
         PopIndent();
         m_writer.WriteLine("}");
         m_symbols.PopScope();
         m_inModule = false;
      }

      public override void EnterTemplateDecl(HorseshoeParser.TemplateDeclContext context)
      {
         if (m_inModule)
            m_writer.Write("export ");

         if (context.name == null)
            throw new Exception("No name specified for template.");

         m_writer.WriteLine("module {0} {{", context.name.GetText());
         PushIndent();
         m_writer.WriteLine("export function render({0} : {1}) : string {{", VAR_DataContext, context.contextType.GetText());
         PushIndent();
         m_writer.WriteLine("var {0} : string = '';", VAR_TemplateResult);
         m_symbols.PushScope(context);
         m_invertTrim = context.invertTrim != null;
         m_trimLeadingWhitespaceFromBody = context.openTrimEnd != null;

      }

      public override void ExitTemplateDecl(HorseshoeParser.TemplateDeclContext context)
      {
         FlushBuffer(context.closeTrimStart != null);
         m_writer.WriteLine("return {0};", VAR_TemplateResult);
         PopIndent();
         m_writer.WriteLine("}");
         PopIndent();
         m_writer.WriteLine("}");

         m_symbols.PopScope();
      }

      public override void EnterBody(HorseshoeParser.BodyContext context)
      {
         string text = context.GetText();
         if (m_trimLeadingWhitespaceFromBody ^ m_invertTrim)
         {
{'fraction_non_alphanumeric': 0.09658344283837056, 'fraction_numerical': 0.002628120893561104, 'mean_word_length': 2.789047915370255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6716184', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1658, 'n_words': 343}