Beispiel #1
0
 static void Main(string[] args)
 {
     try
     {
         OsloCodeGeneratorCompiler<ArrayList> compiler = new OsloCodeGeneratorCompiler<ArrayList>(@"..\..\test.mcg");
         OsloErrorReporter oer = new OsloErrorReporter();
         compiler.Parse(oer);
         Console.WriteLine(compiler.GetTemporaryCSharpCode());
         Generator<ArrayList, GeneratorContext> generator = compiler.CreateGenerator(new ArrayList());
         Console.WriteLine("There are {0} errors.", oer.Errors.Count);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
     }
 }
 public void Process_Template(dynamic template)
 {
     if (template == null) return;
     try
     {
         this.PrintLineNumber(template);
         this.CodePrinter.Write("public List<string> Generated_{0}(", template.Name);
         if (template.Parameters != null)
         {
             string delim = "";
             foreach (dynamic param in template.Parameters)
             {
                 this.CodePrinter.Write(delim);
                 this.Process(param);
                 delim = ", ";
             }
         }
         this.CodePrinter.WriteLine(")");
         this.CodePrinter.WriteLine("{");
         if (template.Content != null)
         {
             this.BeginScope();
             if (template.Parameters != null)
             {
                 foreach (dynamic param in template.Parameters)
                 {
                     this.CurrentScope.Add(param.Name);
                 }
             }
             this.CodePrinter.AppendIndent("    ");
             this.CodePrinter.WriteLine("List<string> __result = new List<string>();");
             this.CodePrinter.WriteLine("using(TemplatePrinter __printer = new TemplatePrinter(__result))");
             this.CodePrinter.WriteLine("{");
             this.CodePrinter.AppendIndent("    ");
             int lineNumber = template.GetSourceSpan().Start.Line;
             foreach (dynamic line in template.Content)
             {
                 ++lineNumber;
                 this.baseLineNumber = lineNumber - 1;
                 string text = line.ToString();
                 OsloErrorReporter templateLineErrorReporter = new OsloErrorReporter();
                 dynamic program = this.rootCodeGenerator.TemplateParser.Parse(new StringReader(text), templateLineErrorReporter);
                 foreach (var error in templateLineErrorReporter.Errors)
                 {
                     this.rootCodeGenerator.ErrorReporter.Report(
                         new SourceLocation(
                             new SourceSpan(
                                 new SourcePoint(lineNumber, error.Location.Span.Start.Column),
                                 new SourcePoint(lineNumber, error.Location.Span.End.Column)),
                             error.Location.FileName),
                         error);
                 }
                 if (program != null)
                 {
                     bool hasStatement = false;
                     foreach (dynamic content in program.Content)
                     {
                         if (content.GetBrand() == "TemplateControl" && content.Statement != null)
                         {
                             hasStatement = true;
                         }
                         this.Process(content);
                     }
                     if (hasStatement)
                     {
                         this.CodePrinter.WriteLine("__printer.TrimLine();");
                     }
                     this.CodePrinter.WriteLine("__printer.WriteLine();");
                 }
                 else if (templateLineErrorReporter.ErrorCount == 0)
                 {
                     this.rootCodeGenerator.ErrorReporter.Error(
                         new SourceLocation(
                             new SourceSpan(
                                 new SourcePoint(lineNumber, 0),
                                 new SourcePoint(lineNumber, text.Length)),
                             this.codeGenerator.FileName),
                         "Could not process line in template {1}.", template.Name);
                 }
             }
             this.CodePrinter.ResetIndent();
             this.CodePrinter.WriteLine("}");
             this.CodePrinter.WriteLine("return __result;");
             this.CodePrinter.ResetIndent();
             this.EndScope();
         }
         this.CodePrinter.WriteLine("}");
         this.CodePrinter.ForcedWriteLine();
         ++this.functionCounter;
         this.rootCodeGenerator.Progress(this.functionCounter, this.rootCodeGenerator.FunctionCount);
     }
     finally
     {
         this.baseLineNumber = 0;
     }
 }
 public override AuthoringScope ParseSource(ParseRequest req)
 {
     this.logger.Log(EventLogEntryType.Information, "Entering: ParseSource()");
     OsloCodeGeneratorSource source = (OsloCodeGeneratorSource)this.GetSource(req.FileName);
     switch (req.Reason)
     {
         case ParseReason.Check:
             // This is where you perform your syntax highlighting.
             // Parse entire source as given in req.Text.
             // Store results in the AuthoringScope object.
             OsloErrorReporter errorReporter = new OsloErrorReporter();
             dynamic program = this.codeParser.Parse(new StringReader(req.Text), errorReporter);
             source.ParseResult = program;
             foreach (var error in errorReporter.Errors)
             {
                 TextSpan span = new TextSpan();
                 span.iStartLine = error.LineNumber - 1;
                 span.iEndLine = error.EndLineNumber - 1;
                 span.iStartIndex = error.ColumnNumber - 1;
                 span.iEndIndex = error.EndColumnNumber - 1;
                 Severity severity = Severity.Error;
                 switch (error.Level)
                 {
                     case ErrorLevel.DeprecationWarning:
                         severity = Severity.Warning;
                         break;
                     case ErrorLevel.Error:
                         severity = Severity.Error;
                         break;
                     case ErrorLevel.Message:
                         severity = Severity.Hint;
                         break;
                     case ErrorLevel.Warning:
                         severity = Severity.Warning;
                         break;
                 }
                 req.Sink.AddError(req.FileName, error.ToString(), span, severity);
             }
             break;
     }
     return new OsloCodeGeneratorAuthoringScope();
 }