-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathJavaBackgroundParser.cs
More file actions
64 lines (52 loc) · 2.61 KB
/
Copy pathJavaBackgroundParser.cs
File metadata and controls
64 lines (52 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace Tvl.VisualStudio.Language.Java
{
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Antlr.Runtime;
using Microsoft.VisualStudio.Text;
using Tvl.VisualStudio.Language.Parsing;
using Tvl.VisualStudio.OutputWindow.Interfaces;
using Stopwatch = System.Diagnostics.Stopwatch;
public class JavaBackgroundParser : BackgroundParser
{
private readonly JavaBackgroundParserProvider _provider;
public JavaBackgroundParser(ITextBuffer textBuffer, JavaBackgroundParserProvider provider)
: base(textBuffer, provider.BackgroundIntelliSenseTaskScheduler, provider.TextDocumentFactoryService, provider.OutputWindowService)
{
Contract.Requires<ArgumentNullException>(provider != null, "provider");
_provider = provider;
}
protected override void ReParseImpl()
{
var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense);
Stopwatch stopwatch = Stopwatch.StartNew();
string filename = "<Unknown File>";
ITextDocument textDocument = TextDocument;
if (textDocument != null)
filename = textDocument.FilePath;
var snapshot = TextBuffer.CurrentSnapshot;
ANTLRStringStream input = new ANTLRStringStream(snapshot.GetText());
Java2Lexer lexer = new Java2Lexer(new JavaUnicodeStream(input));
CommonTokenStream tokens = new CommonTokenStream(lexer);
Java2Parser parser = new Java2Parser(tokens);
List<ParseErrorEventArgs> errors = new List<ParseErrorEventArgs>();
parser.ParseError += (sender, e) =>
{
errors.Add(e);
string message = e.Message;
if (message.Length > 100)
message = message.Substring(0, 100) + " ...";
ITextSnapshotLine startLine = snapshot.GetLineFromPosition(e.Span.Start);
int line = startLine.LineNumber;
int column = e.Span.Start - startLine.Start;
if (outputWindow != null)
outputWindow.WriteLine(string.Format("{0}({1},{2}): {3}", filename, line + 1, column + 1, message));
if (errors.Count > 100)
throw new OperationCanceledException();
};
var result = parser.compilationUnit();
OnParseComplete(new AntlrParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), result));
}
}
}