public IEnumerable <ITagSpan <ITag> > DoWork(ITextSnapshot snapshot, object input, System.Threading.CancellationToken cancelToken) { List <ITagSpan <ITag> > results = new List <ITagSpan <ITag> >(); var source = snapshot.GetText(); var inputStream = new AntlrInputStream(new StringReader(source)); var txMarkLexer = new TxMarkLexer(inputStream); var tokens = new CommonTokenStream(txMarkLexer); var txMarkParser = new TxMarkParser(tokens); txMarkParser.BuildParseTree = true; var parseTree = txMarkParser.document(); var txMarkSyntaxHighlighterParseTreeWalker = new TxMarkSyntaxHighlighterParseTreeWalker(this, snapshot); ParseTreeWalker.Default.Walk(txMarkSyntaxHighlighterParseTreeWalker, parseTree); return(txMarkSyntaxHighlighterParseTreeWalker.Tags); }
private static CompileContext Parse <TModel>(TextReader reader, string templatePath, Options options, Result result) { DateTime startTime = DateTime.Now; TxMarkParser.DocumentContext parseTree; var inputStream = new AntlrInputStream(reader); var txMarkLexer = new TxMarkLexer(inputStream); var tokens = new CommonTokenStream(txMarkLexer); var txMarkParser = new TxMarkParser(tokens); txMarkParser.RemoveErrorListeners(); txMarkParser.AddErrorListener(new ParserErrorListener(result)); txMarkParser.BuildParseTree = true; parseTree = txMarkParser.document(); DateTime parseTime = DateTime.Now; result.ParserTime = parseTime - startTime; List <Diagnostic> diagnostics = new List <Diagnostic>(); var compileContext = new CompileContext( options.TemplateBaseClass ?? typeof(CompiledTemplateBase <TModel>), typeof(TModel), templateName: MakeTemplateName(options, templatePath), templatePath: templatePath, logHandler: (level, message, line, column) => { //Debug.WriteLine($"{level}: {message} at or near line {line}, column {column}"); switch (level) { case LogLevel.Warning: diagnostics.Add( new Diagnostic(DiagnosticSeverity.Warning, Result.Sources.Parser, null, message, line, column) ); break; case LogLevel.Error: diagnostics.Add( new Diagnostic(DiagnosticSeverity.Error, Result.Sources.Parser, null, message, line, column) ); break; } }); var txMarkListener = new TxMarkParseTreeListener(compileContext); try { ParseTreeWalker.Default.Walk(txMarkListener, parseTree); result.Success = true; } catch (Exception e) { diagnostics.Add( new Diagnostic(DiagnosticSeverity.Error, Result.Sources.Parser, "parse tree walking", e.Message) ); result.Success = false; } foreach (var diagnostic in diagnostics) { if (diagnostic.Severity == DiagnosticSeverity.Error) { result.Success = false; } result.AddDiagnostic(diagnostic); } if (options.OutputParser) { var formatStartTime = DateTime.Now; try { result.ParserStream = compileContext.ToStream(options.Encoding); } catch (Exception e) { diagnostics.Add( new Diagnostic(DiagnosticSeverity.Error, Result.Sources.Parser, "formatting parser output", e.Message) ); result.Success = false; } var formatEndTime = DateTime.Now; result.FormatTime = formatEndTime - formatStartTime; } return(compileContext); }