public override void ExitModule(HorseshoeParser.ModuleContext context)
 {
    PopIndent();
    m_writer.WriteLine("}");
    m_symbols.PopScope();
    m_inModule = false;
 }
 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 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();
         if (context.contextTypeName == null)
         {
            m_writer.WriteLine("export function render() : string {");
            m_hasDataContext = false;
         }
         else
         {
            m_writer.WriteLine("export function render({0} : {1}) : string {{", VAR_DataContext, context.contextTypeName.GetText());
            m_hasDataContext = true;
         }
         PushIndent();
         m_writer.WriteLine("var {0} : string = '';", VAR_TemplateResult);
         m_symbols.PushScope(context);
         m_invertTrim = context.invertTrim != null;
         m_trimLeadingWhitespaceFromBody = context.openTrimEnd != null;

      }
예제 #4
0
 public static string Compile(string input, BaseErrorListener errorListener)
 {         
    AntlrInputStream stream = new AntlrInputStream(input);
    HorseshoeLexer lexer = new HorseshoeLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    HorseshoeParser parser = new HorseshoeParser(tokenStream);
    if (errorListener != null)
    {
       parser.RemoveErrorListeners();
       parser.AddErrorListener(errorListener);
    }
    var context = parser.document();
    HorseshoeTranslationListener listener = new HorseshoeTranslationListener();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.Walk(listener, context);
    return listener.Result;
 }
예제 #5
0
        public static string Compile(string input, BaseErrorListener errorListener)
        {
            AntlrInputStream  stream      = new AntlrInputStream(input);
            HorseshoeLexer    lexer       = new HorseshoeLexer(stream);
            CommonTokenStream tokenStream = new CommonTokenStream(lexer);
            HorseshoeParser   parser      = new HorseshoeParser(tokenStream);

            if (errorListener != null)
            {
                parser.RemoveErrorListeners();
                parser.AddErrorListener(errorListener);
            }
            var context = parser.document();
            HorseshoeTranslationListener listener = new HorseshoeTranslationListener();
            ParseTreeWalker walker = new ParseTreeWalker();

            walker.Walk(listener, context);
            return(listener.Result);
        }
 public override void ExitConditional(HorseshoeParser.ConditionalContext context)
 {
    FlushBuffer(context.closeTrimStart != null);
    PopIndent();
    m_writer.WriteLine("}");
    m_symbols.PopScope();
    m_trimLeadingWhitespaceFromBody = context.closeTrimEnd != 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)
         {
            if (String.IsNullOrWhiteSpace(text))
               text = String.Empty;

            text = text.TrimStart();
         }

         m_trimLeadingWhitespaceFromBody = false;
         m_buffer.Append(text);
      }
 public override void ExitSubstitution(HorseshoeParser.SubstitutionContext context)
 {
    m_trimLeadingWhitespaceFromBody = (context.trimEnd != null) ^ m_invertTrim;
 }
      public override void EnterSubstitution(HorseshoeParser.SubstitutionContext context)
      {
         if (!m_hasDataContext)
            throw new Exception("Substitutions cannot be used without a data context.");

         FlushBuffer((context.trimStart != null) ^ m_invertTrim);
         string variableName = GetVariableName(context.id);
         m_writer.WriteLine("{0} += _.escape(String({1}));", VAR_TemplateResult, variableName);
      }
 public override void EnterInvoke(HorseshoeParser.InvokeContext context)
 {
    FlushBuffer(context.trimStart != null);
    m_writer.WriteLine("{0} += {1}({2});", VAR_TemplateResult, context.method.GetText(), GetVariableName(context.argument));
    m_trimLeadingWhitespaceFromBody = context.trimEnd != null;
 }
 public override void EnterUnescapedSubstitution(HorseshoeParser.UnescapedSubstitutionContext context)
 {
    FlushBuffer(context.trimStart != null);
    string variableName = GetVariableName(context.id);
    m_writer.WriteLine("{0} += {1};", VAR_TemplateResult, variableName);
 }
 public override void EnterElseClause(HorseshoeParser.ElseClauseContext context)
 {
    FlushBuffer(context.trimStart != null);
    PopIndent();
    m_writer.WriteLine("} else {");
    PushIndent();
    m_trimLeadingWhitespaceFromBody = context.trimEnd != null;
 }
      private string GetVariableName(HorseshoeParser.ScopeQualifiedIdentifierContext context)
      {
         string variableName = context.id.GetText();
         string scopeName = variableName;
         if (scopeName != null)
         {
            int index = scopeName.IndexOf('.');
            if (index != -1)
               scopeName = scopeName.Substring(0, index);
         }

         if (context.scope != null || !m_symbols.CurrentScope.ContainsSymbol(scopeName))
            variableName = VAR_DataContext + "." + variableName;
         return variableName;
      }
 public override void EnterConditional(HorseshoeParser.ConditionalContext context)
 {
    FlushBuffer(context.openTrimStart != null);
    m_writer.WriteLine("if ({0}{1}) {{", context.NOT() != null ? "!" : "", GetVariableName(context.id));
    PushIndent();
    m_symbols.PushScope(context);
    m_trimLeadingWhitespaceFromBody = context.openTrimEnd != null;
 }
 public override void EnterListIteration(HorseshoeParser.ListIterationContext context)
 {
    FlushBuffer(context.openTrimStart != null);
    m_symbols.PushScope(context);
    m_symbols.AddSymbol(new Symbol(context.variable.GetText(), context.variable));
    m_writer.WriteLine("_.each({0}, function ({1} : {2}) {{", GetVariableName(context.collection), context.variable.GetText(), context.type.GetText());
    PushIndent();
    m_trimLeadingWhitespaceFromBody = context.openTrimEnd != null;
 }
 public override void ExitUnescapedSubstitution(HorseshoeParser.UnescapedSubstitutionContext context)
 {
    m_trimLeadingWhitespaceFromBody = context.trimEnd != null;
 }