Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
balazssimon committed Jul 16, 2019
1 parent e283602 commit 2349a90
Show file tree
Hide file tree
Showing 35 changed files with 1,630 additions and 422 deletions.
2 changes: 1 addition & 1 deletion src/Main/MetaDslx.BuildTasks/Antlr4CompilerTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private bool Compile(ITaskItem taskItem)
}
catch (Exception ex)
{
Log.LogError("{0}: FAILED to generate code for '{1}': {2}", this.Name, filePath, ex.Message);
Log.LogError("{0}: FAILED to generate code for '{1}': {2}", this.Name, filePath, ex.StackTrace);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using MetaDslx.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.PooledObjects;
using MetaDslx.CodeAnalysis.Syntax;

namespace MetaDslx.Languages.Antlr4Roslyn.Compilation
{
Expand Down Expand Up @@ -138,58 +139,6 @@ public void Generate()
protected abstract void DoCompile();
protected abstract void DoGenerate();

private TextSpan GetTextSpan(IToken token)
{
if (token == null) return default;
return new TextSpan(token.StartIndex, token.StopIndex - token.StartIndex + 1);
}

private LinePositionSpan GetLinePositionSpan(IToken token)
{
if (token == null) return default;
int startLine = token.Line;
int startPosition = token.Column;
int endLine;
int endPosition;
string text = token.Text;
if (!text.Contains('\n'))
{
endLine = startLine;
endPosition = startPosition + token.Text.Length;
}
else
{
endLine = token.Line + token.Text.Count(c => c == '\n');
int index = text.LastIndexOf('\n');
endPosition = text.Length - index;
}
if (startLine > 0 && endLine > 0)
{
--startLine;
--endLine;
}
return new LinePositionSpan(new LinePosition(startLine, startPosition), new LinePosition(endLine, endPosition));
}

private TextSpan GetTextSpan(ParserRuleContext rule)
{
return new TextSpan(rule.Start.StartIndex, rule.Stop.StopIndex - rule.Start.StartIndex + 1);
}

private LinePositionSpan GetLinePositionSpan(ParserRuleContext rule)
{
int startLine = rule.Start.Line;
int startPosition = rule.Start.Column;
int endLine = rule.Stop.Line;
int endPosition = rule.Stop.Column + rule.Stop.Text.Length;
if (startLine > 0 && endLine > 0)
{
--startLine;
--endLine;
}
return new LinePositionSpan(new LinePosition(startLine, startPosition), new LinePosition(endLine, endPosition));
}

private TextSpan GetTextSpan(int position)
{
return new TextSpan(position, 0);
Expand Down Expand Up @@ -224,25 +173,25 @@ internal void AddDiagnostic(Antlr4RoslynErrorCode code, params object[] args)

internal void AddDiagnostic(IToken token, Antlr4RoslynErrorCode code, params object[] args)
{
this.DiagnosticBag.Add(code, Location.Create(this.FileName, this.GetTextSpan(token), this.GetLinePositionSpan(token)), args);
this.DiagnosticBag.Add(code, Location.Create(this.FileName, token.GetTextSpan(), token.GetLinePositionSpan()), args);
}

internal void AddDiagnostic(ITerminalNode token, Antlr4RoslynErrorCode code, params object[] args)
{
this.DiagnosticBag.Add(code, Location.Create(this.FileName, this.GetTextSpan(token.Symbol), this.GetLinePositionSpan(token.Symbol)), args);
this.DiagnosticBag.Add(code, Location.Create(this.FileName, token.Symbol.GetTextSpan(), token.Symbol.GetLinePositionSpan()), args);
}

internal void AddDiagnostic(ParserRuleContext rule, Antlr4RoslynErrorCode code, params object[] args)
{
this.DiagnosticBag.Add(code, Location.Create(this.FileName, this.GetTextSpan(rule), this.GetLinePositionSpan(rule)), args);
this.DiagnosticBag.Add(code, Location.Create(this.FileName, rule.GetTextSpan(), rule.GetLinePositionSpan()), args);
}

public void SyntaxError([NotNull] IRecognizer recognizer, [Nullable] int offendingSymbol, int line, int charPositionInLine, [NotNull] string msg, [Nullable] RecognitionException e)
{
IToken token = e.OffendingToken;
if (token != null)
{
this.DiagnosticBag.Add(Antlr4RoslynErrorCode.ERR_SyntaxError, Location.Create(this.FileName, this.GetTextSpan(token), this.GetLinePositionSpan(token)), msg);
this.DiagnosticBag.Add(Antlr4RoslynErrorCode.ERR_SyntaxError, Location.Create(this.FileName, token.GetTextSpan(), token.GetLinePositionSpan()), msg);
}
else
{
Expand All @@ -254,7 +203,7 @@ public void SyntaxError([NotNull] IRecognizer recognizer, [Nullable] IToken offe
{
if (offendingSymbol != null)
{
this.DiagnosticBag.Add(Antlr4RoslynErrorCode.ERR_SyntaxError, Location.Create(this.FileName, this.GetTextSpan(offendingSymbol), this.GetLinePositionSpan(offendingSymbol)), msg);
this.DiagnosticBag.Add(Antlr4RoslynErrorCode.ERR_SyntaxError, Location.Create(this.FileName, offendingSymbol.GetTextSpan(), offendingSymbol.GetLinePositionSpan()), msg);
}
else
{
Expand Down
Loading

0 comments on commit 2349a90

Please sign in to comment.