Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Jan 5, 2024
1 parent af64782 commit f9647a3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
9 changes: 8 additions & 1 deletion src/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ enum CompileStageType : uint8_t {
FINISHED
};

enum CompileStageIOType { IO_CODE, IO_TOKENS, IO_CST, IO_AST, IO_IR, IO_OBJECT_FILE };
enum CompileStageIOType : uint8_t {
IO_CODE,
IO_TOKENS,
IO_CST,
IO_AST,
IO_IR,
IO_OBJECT_FILE,
};

struct SourceFileAntlrCtx {
// Create error handlers for lexer and parser
Expand Down
12 changes: 6 additions & 6 deletions src/ast/ASTBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace spice::compiler {

ASTBuilder::ASTBuilder(GlobalResourceManager &resourceManager, SourceFile *sourceFile, antlr4::ANTLRInputStream *inputStream)
: CompilerPass(resourceManager, sourceFile), filePath(sourceFile->filePath), inputStream(inputStream) {}
: CompilerPass(resourceManager, sourceFile), inputStream(inputStream) {}

std::any ASTBuilder::visitEntry(SpiceParser::EntryContext *ctx) {
auto entryNode = createNode<EntryNode>(ctx);
Expand Down Expand Up @@ -1386,11 +1386,11 @@ int8_t ASTBuilder::parseChar(TerminalNode *terminal) {
case '0':
return '\0';
default:
const CodeLoc codeLoc(terminal->getSymbol(), filePath);
const CodeLoc codeLoc(terminal->getSymbol(), sourceFile);
throw ParserError(codeLoc, INVALID_CHAR_LITERAL, "Invalid escape sequence " + input);
}
} else {
const CodeLoc codeLoc(terminal->getSymbol(), filePath);
const CodeLoc codeLoc(terminal->getSymbol(), sourceFile);
throw ParserError(codeLoc, INVALID_CHAR_LITERAL, "Invalid char literal " + input);
}
}
Expand Down Expand Up @@ -1437,10 +1437,10 @@ T ASTBuilder::parseNumeric(ConstantNode *constantNode, TerminalNode *terminal, s
}
return cb(input, 10);
} catch (std::out_of_range &e) {
const CodeLoc codeLoc(terminal->getSymbol(), filePath);
const CodeLoc codeLoc(terminal->getSymbol(), sourceFile);
throw ParserError(codeLoc, NUMBER_OUT_OF_RANGE, "The provided number is out of range");
} catch (std::invalid_argument &e) {
const CodeLoc codeLoc(terminal->getSymbol(), filePath);
const CodeLoc codeLoc(terminal->getSymbol(), sourceFile);
throw ParserError(codeLoc, NUMBER_OUT_OF_RANGE, "You tried to parse '" + input + "' as an integer, but it was no integer");
}
}
Expand All @@ -1467,7 +1467,7 @@ std::string ASTBuilder::getIdentifier(TerminalNode *terminal) {
isReserved |= std::find(std::begin(RESERVED_KEYWORDS), std::end(RESERVED_KEYWORDS), identifier) != std::end(RESERVED_KEYWORDS);
// Print error message
if (isReserved) {
const CodeLoc codeLoc(terminal->getSymbol(), filePath);
const CodeLoc codeLoc(terminal->getSymbol(), sourceFile);
throw ParserError(codeLoc, RESERVED_KEYWORD, "'" + identifier + "' is a reserved keyword. Please use another name instead");
}

Expand Down
5 changes: 3 additions & 2 deletions src/ast/ASTBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ class ASTBuilder : private CompilerPass, public SpiceVisitor {

private:
// Members
const std::filesystem::path &filePath;
antlr4::ANTLRInputStream *inputStream;
std::stack<ASTNode *> parentStack;

// Private methods
template <typename T> T *createNode(const ParserRuleContext *ctx);
template <typename T> T *concludeNode(T *node);
ALWAYS_INLINE CodeLoc getCodeLoc(const ParserRuleContext *ctx) { return CodeLoc(ctx->start, filePath); }
ALWAYS_INLINE CodeLoc getCodeLoc(const ParserRuleContext *ctx) {
return {ctx->start, ctx->start->getStartIndex(), ctx->stop->getStopIndex(), sourceFile};
}
int32_t parseInt(ConstantNode *constantNode, TerminalNode *terminal);
int16_t parseShort(ConstantNode *constantNode, TerminalNode *terminal);
int64_t parseLong(ConstantNode *constantNode, TerminalNode *terminal);
Expand Down
8 changes: 4 additions & 4 deletions src/ast/ASTNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ namespace spice::compiler {
static constexpr size_t ERROR_MESSAGE_CONTEXT = 20;

std::string ASTNode::getErrorMessage() const {
antlr4::CharStream *inputStream = codeLoc.token->getInputStream();
const antlr4::misc::Interval sourceInterval(codeLoc.token->getStartIndex(), codeLoc.token->getStopIndex());
antlr4::misc::Interval extSourceInterval(sourceInterval);
antlr4::CharStream *inputStream = codeLoc.sourceFile->antlrCtx.inputStream.get();
antlr4::misc::Interval extSourceInterval(codeLoc.sourceInterval);

// If we have a multi-line interval, only use the first line
if (size_t offset = inputStream->getText(extSourceInterval).find('\n'); offset != std::string::npos)
Expand Down Expand Up @@ -53,7 +52,8 @@ std::string ASTNode::getErrorMessage() const {
// Build error message
std::stringstream ss;
ss << lineNumberStr << " " << inputStream->getText(extSourceInterval) << "\n";
ss << std::string(markerIndentation, ' ') << std::string(std::min(sourceInterval.length(), extSourceInterval.length()), '^');
ss << std::string(markerIndentation, ' ');
ss << std::string(std::min(codeLoc.sourceInterval.length(), extSourceInterval.length()), '^');
return ss.str();
}

Expand Down
6 changes: 6 additions & 0 deletions src/util/CodeLoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "CodeLoc.h"

#include "SourceFile.h"
#include <util/CommonUtil.h>

#include <utility>
Expand All @@ -21,6 +22,7 @@ std::string CodeLoc::toString() const { return "L" + std::to_string(line) + "C"
* @return Pretty code location
*/
std::string CodeLoc::toPrettyString() const {
const std::filesystem::path &sourceFilePath = sourceFile->filePath;
const std::string prefix = sourceFilePath.empty() ? "" : sourceFilePath.generic_string() + ":";
return prefix + std::to_string(line) + ":" + std::to_string(col);
}
Expand All @@ -39,4 +41,8 @@ std::string CodeLoc::toPrettyLine() const { return "L" + std::to_string(line); }
*/
std::string CodeLoc::toPrettyLineAndColumn() const { return toString(); }

bool operator==(const CodeLoc &a, const CodeLoc &b) {
return a.sourceFile->filePath == b.sourceFile->filePath && a.line == b.line && a.col == b.col;
}

} // namespace spice::compiler
23 changes: 13 additions & 10 deletions src/util/CodeLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@

namespace spice::compiler {

// Forward declarations
class SourceFile;

struct CodeLoc {
public:
// Constructors
CodeLoc(size_t line, size_t col, std::filesystem::path sourceFilePath = "")
: line(line), col(col), sourceFilePath(std::move(sourceFilePath)) {}
explicit CodeLoc(const antlr4::Token *token, std::filesystem::path sourceFilePath = "")
: token(token), line(token->getLine()), col(token->getCharPositionInLine() + 1),
sourceFilePath(std::move(sourceFilePath)){};
explicit CodeLoc(const antlr4::Token *token, SourceFile *sourceFile = nullptr)
: sourceInterval(token->getStartIndex(), token->getStopIndex()), line(token->getLine()),
col(token->getCharPositionInLine() + 1), sourceFile(sourceFile){};
CodeLoc(const antlr4::Token *token, size_t startIdx, size_t stopIdx, SourceFile *sourceFile = nullptr)
: sourceInterval(startIdx, stopIdx), line(token->getLine()), col(token->getCharPositionInLine() + 1),
sourceFile(sourceFile){};
CodeLoc(size_t line, size_t col, SourceFile *sourceFile = nullptr) : line(line), col(col), sourceFile(sourceFile) {}

// Public members
const antlr4::Token *token = nullptr;
std::filesystem::path sourceFilePath;
SourceFile *sourceFile = nullptr;
const antlr4::misc::Interval sourceInterval;
size_t line;
size_t col;

Expand All @@ -35,9 +40,7 @@ struct CodeLoc {
[[nodiscard]] std::string toPrettyLineAndColumn() const;

// Operators
ALWAYS_INLINE friend bool operator==(const CodeLoc &a, const CodeLoc &b) {
return a.sourceFilePath == b.sourceFilePath && a.line == b.line && a.col == b.col;
}
friend bool operator==(const CodeLoc &a, const CodeLoc &b);
ALWAYS_INLINE friend bool operator<(const CodeLoc &a, const CodeLoc &b) {
return a.line == b.line ? a.col < b.col : a.line < b.line;
}
Expand Down

0 comments on commit f9647a3

Please sign in to comment.