Skip to content

Commit

Permalink
[error] Improve "Duplicate symbol" location.
Browse files Browse the repository at this point in the history
Fix #161
  • Loading branch information
pfusik committed May 9, 2024
1 parent 5b07f4f commit 3aecf51
Showing 9 changed files with 47 additions and 46 deletions.
9 changes: 9 additions & 0 deletions Lexer.fu
Original file line number Diff line number Diff line change
@@ -21,7 +21,16 @@
public abstract class FuParserHost
{
internal FuProgram! Program;

public abstract void ReportError!(string filename, int line, int startUtf16Column, int endUtf16Column, string message);

internal void ReportStatementError!(FuStatement statement, string message)
{
int line = this.Program.GetLine(statement.Loc);
int column = statement.Loc - this.Program.LineLocs[line];
FuSourceFile file = this.Program.GetSourceFile(line);
ReportError(file.Filename, line - file.Line, column, column + statement.GetLocLength(), message);
}
}

public enum FuToken
2 changes: 1 addition & 1 deletion Parser.fu
Original file line number Diff line number Diff line change
@@ -523,7 +523,7 @@ public class FuParser : FuLexer
void AddSymbol!(FuScope! scope, FuSymbol# symbol)
{
if (scope.Contains(symbol))
ReportError("Duplicate symbol");
this.Host.ReportStatementError(symbol, "Duplicate symbol");
else
scope.Add(symbol);
}
8 changes: 0 additions & 8 deletions Sema.fu
Original file line number Diff line number Diff line change
@@ -21,14 +21,6 @@
public abstract class FuSemaHost : FuParserHost
{
internal virtual int GetResourceLength!(string name, FuPrefixExpr expr) => 0;

internal void ReportStatementError!(FuStatement statement, string message)
{
int line = this.Program.GetLine(statement.Loc);
int column = statement.Loc - this.Program.LineLocs[line];
FuSourceFile file = this.Program.GetSourceFile(line);
ReportError(file.Filename, line - file.Line, column, column + statement.GetLocLength(), message);
}
}

public class FuSema
18 changes: 9 additions & 9 deletions libfut.cpp
Original file line number Diff line number Diff line change
@@ -31,6 +31,14 @@ static std::string FuString_Replace(std::string_view s, std::string_view oldValu
}
}

void FuParserHost::reportStatementError(const FuStatement * statement, std::string_view message)
{
int line = this->program->getLine(statement->loc);
int column = statement->loc - this->program->lineLocs[line];
const FuSourceFile * file = this->program->getSourceFile(line);
reportError(file->filename, line - file->line, column, column + statement->getLocLength(), message);
}

void FuLexer::setHost(FuParserHost * host)
{
this->host = host;
@@ -3668,7 +3676,7 @@ std::shared_ptr<FuExpr> FuParser::parseInitializer()
void FuParser::addSymbol(FuScope * scope, std::shared_ptr<FuSymbol> symbol)
{
if (scope->contains(symbol.get()))
reportError("Duplicate symbol");
this->host->reportStatementError(symbol.get(), "Duplicate symbol");
else
scope->add(symbol);
}
@@ -4365,14 +4373,6 @@ int FuSemaHost::getResourceLength(std::string_view name, const FuPrefixExpr * ex
{
return 0;
}

void FuSemaHost::reportStatementError(const FuStatement * statement, std::string_view message)
{
int line = this->program->getLine(statement->loc);
int column = statement->loc - this->program->lineLocs[line];
const FuSourceFile * file = this->program->getSourceFile(line);
reportError(file->filename, line - file->line, column, column + statement->getLocLength(), message);
}
FuSema::FuSema()
{
this->poison->name = "poison";
18 changes: 9 additions & 9 deletions libfut.cs
Original file line number Diff line number Diff line change
@@ -14,6 +14,14 @@ public abstract class FuParserHost
internal FuProgram Program;

public abstract void ReportError(string filename, int line, int startUtf16Column, int endUtf16Column, string message);

internal void ReportStatementError(FuStatement statement, string message)
{
int line = this.Program.GetLine(statement.Loc);
int column = statement.Loc - this.Program.LineLocs[line];
FuSourceFile file = this.Program.GetSourceFile(line);
ReportError(file.Filename, line - file.Line, column, column + statement.GetLocLength(), message);
}
}

public enum FuToken
@@ -4000,7 +4008,7 @@ FuExpr ParseInitializer()
void AddSymbol(FuScope scope, FuSymbol symbol)
{
if (scope.Contains(symbol))
ReportError("Duplicate symbol");
this.Host.ReportStatementError(symbol, "Duplicate symbol");
else
scope.Add(symbol);
}
@@ -4628,14 +4636,6 @@ public abstract class FuSemaHost : FuParserHost
{

internal virtual int GetResourceLength(string name, FuPrefixExpr expr) => 0;

internal void ReportStatementError(FuStatement statement, string message)
{
int line = this.Program.GetLine(statement.Loc);
int column = statement.Loc - this.Program.LineLocs[line];
FuSourceFile file = this.Program.GetSourceFile(line);
ReportError(file.Filename, line - file.Line, column, column + statement.GetLocLength(), message);
}
}

public class FuSema
2 changes: 1 addition & 1 deletion libfut.hpp
Original file line number Diff line number Diff line change
@@ -479,6 +479,7 @@ class FuParserHost
FuParserHost() = default;
public:
FuProgram * program;
void reportStatementError(const FuStatement * statement, std::string_view message);
};

class FuLexer
@@ -1658,7 +1659,6 @@ class FuSemaHost : public FuParserHost
FuSemaHost() = default;
public:
virtual int getResourceLength(std::string_view name, const FuPrefixExpr * expr);
void reportStatementError(const FuStatement * statement, std::string_view message);
};

class GenHost : public FuSemaHost
18 changes: 9 additions & 9 deletions libfut.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,14 @@ export const RegexOptions = {
export class FuParserHost
{
program;

reportStatementError(statement, message)
{
let line = this.program.getLine(statement.loc);
let column = statement.loc - this.program.lineLocs[line];
let file = this.program.getSourceFile(line);
this.reportError(file.filename, line - file.line, column, column + statement.getLocLength(), message);
}
}

export const FuToken = {
@@ -4202,7 +4210,7 @@ export class FuParser extends FuLexer
#addSymbol(scope, symbol)
{
if (scope.contains(symbol))
this.reportError("Duplicate symbol");
this.host.reportStatementError(symbol, "Duplicate symbol");
else
scope.add(symbol);
}
@@ -4829,14 +4837,6 @@ export class FuSemaHost extends FuParserHost
{
return 0;
}

reportStatementError(statement, message)
{
let line = this.program.getLine(statement.loc);
let column = statement.loc - this.program.lineLocs[line];
let file = this.program.getSourceFile(line);
this.reportError(file.filename, line - file.line, column, column + statement.getLocLength(), message);
}
}

export class GenHost extends FuSemaHost
9 changes: 0 additions & 9 deletions test/error/ClassDuplicate.fu

This file was deleted.

9 changes: 9 additions & 0 deletions test/error/SymbolDuplicate.fu
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Foo
{
public const int A = 0;
public const int A = 0; //ERROR: Duplicate symbol
}

public class Foo //ERROR: Duplicate symbol
{
}

0 comments on commit 3aecf51

Please sign in to comment.