2022-11-23 09:29:42 +01:00
|
|
|
# Copyright 2022 Mattia Giambirtone & All Contributors
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
## Utilities to print formatted error messages to stderr
|
2022-12-15 11:48:36 +01:00
|
|
|
import frontend/compiler/compiler
|
|
|
|
import frontend/parsing/parser
|
|
|
|
import frontend/parsing/lexer
|
|
|
|
import errors
|
2022-11-23 09:29:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
import std/os
|
|
|
|
import std/terminal
|
|
|
|
import std/strutils
|
|
|
|
import std/strformat
|
|
|
|
|
|
|
|
|
2022-11-27 13:39:41 +01:00
|
|
|
proc printError(file, line: string, lineNo: int, pos: tuple[start, stop: int], fn: Declaration, msg: string) =
|
|
|
|
## Internal helper to print a formatted error message
|
|
|
|
## to stderr
|
|
|
|
stderr.styledWrite(fgRed, styleBright, "Error in ", fgYellow, &"{file}:{lineNo}:{pos.start}")
|
|
|
|
if not fn.isNil() and fn.kind == funDecl:
|
|
|
|
stderr.styledWrite(fgRed, styleBright, " in function ", fgYellow, FunDecl(fn).name.token.lexeme)
|
|
|
|
stderr.styledWriteLine(styleBright, fgDefault, ": ", msg)
|
2022-11-29 16:48:05 +01:00
|
|
|
if line.len() > 0:
|
|
|
|
stderr.styledWrite(fgRed, styleBright, "Source line: ", resetStyle, fgDefault, line[0..<pos.start])
|
2023-03-04 12:13:19 +01:00
|
|
|
if pos.stop == line.len():
|
|
|
|
stderr.styledWrite(fgRed, styleUnderscore, line[pos.start..<pos.stop])
|
|
|
|
stderr.styledWriteLine(fgDefault, line[pos.stop..^1])
|
|
|
|
else:
|
|
|
|
stderr.styledWrite(fgRed, styleUnderscore, line[pos.start..pos.stop])
|
|
|
|
stderr.styledWriteLine(fgDefault, line[pos.stop + 1..^1])
|
2022-11-27 13:39:41 +01:00
|
|
|
|
|
|
|
|
2022-11-23 09:29:42 +01:00
|
|
|
proc print*(exc: CompileError) =
|
|
|
|
## Prints a formatted error message
|
|
|
|
## for compilation errors to stderr
|
|
|
|
var file = exc.file
|
2022-12-04 17:02:32 +01:00
|
|
|
var contents: string
|
|
|
|
if file notin ["<string>", "", "stdin"]:
|
2022-11-23 09:29:42 +01:00
|
|
|
file = relativePath(exc.file, getCurrentDir())
|
2023-06-01 12:56:59 +02:00
|
|
|
contents = readFile(file).strip(chars={'\n'}).splitLines()[exc.line - 1]
|
2022-12-04 17:02:32 +01:00
|
|
|
else:
|
2023-06-01 12:56:59 +02:00
|
|
|
contents = exc.compiler.getSource().strip(chars={'\n'}).splitLines()[exc.line - 1]
|
2022-12-04 17:02:32 +01:00
|
|
|
printError(file, contents, exc.line, exc.node.getRelativeBoundaries(), exc.function,
|
2022-11-27 13:39:41 +01:00
|
|
|
exc.msg)
|
2022-11-23 09:29:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
proc print*(exc: ParseError) =
|
|
|
|
## Prints a formatted error message
|
|
|
|
## for parsing errors to stderr
|
|
|
|
var file = exc.file
|
|
|
|
if file notin ["<string>", ""]:
|
|
|
|
file = relativePath(exc.file, getCurrentDir())
|
2023-06-01 12:56:59 +02:00
|
|
|
printError(file, exc.parser.getSource().strip(chars={'\n'}).splitLines()[exc.line - 1],
|
2022-11-27 13:39:41 +01:00
|
|
|
exc.line, exc.token.relPos, exc.parser.getCurrentFunction(),
|
|
|
|
exc.msg)
|
2022-11-23 09:29:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
proc print*(exc: LexingError) =
|
|
|
|
## Prints a formatted error message
|
|
|
|
## for lexing errors to stderr
|
|
|
|
var file = exc.file
|
|
|
|
if file notin ["<string>", ""]:
|
|
|
|
file = relativePath(exc.file, getCurrentDir())
|
2023-06-01 12:56:59 +02:00
|
|
|
printError(file, exc.lexer.getSource().strip(chars={'\n'}).splitLines()[exc.line - 1],
|
2022-11-27 13:39:41 +01:00
|
|
|
exc.line, exc.pos, nil, exc.msg)
|
2022-11-23 09:29:42 +01:00
|
|
|
|