Added Location struct
Its purpose is to carry error reporting information with it outside of files.
This commit is contained in:
parent
64a8a2445a
commit
c3a5b15049
21
file/file.go
21
file/file.go
@ -7,10 +7,11 @@ var logger = log.New(os.Stderr, "", 0)
|
||||
|
||||
// File represents a read only file that can print out formatted errors.
|
||||
type File struct {
|
||||
path string
|
||||
file *os.File
|
||||
currentLine int
|
||||
lines [][]byte
|
||||
path string
|
||||
file *os.File
|
||||
currentLine int
|
||||
currentColumn int
|
||||
lines [][]byte
|
||||
}
|
||||
|
||||
// Open opens the file specified by path and returns a new File struct.
|
||||
@ -44,10 +45,12 @@ func (file *File) Read (bytes []byte) (amountRead int, err error) {
|
||||
if char == '\n' {
|
||||
file.lines = append(file.lines, []byte { })
|
||||
file.currentLine ++
|
||||
file.currentColumn = 0
|
||||
} else {
|
||||
file.lines[file.currentLine] = append (
|
||||
file.lines[file.currentLine],
|
||||
bytes...)
|
||||
file.currentColumn ++
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,3 +103,13 @@ func (file *File) Error (column, row, width int, message string) {
|
||||
func (file *File) Warn (column, row, width int, message string) {
|
||||
file.mistake(column, row, width, message, "!!!")
|
||||
}
|
||||
|
||||
// Location returns a location struct describing the current position inside of
|
||||
// the file. This can be stored and used to print errors.
|
||||
func (file *File) Location () (location Location) {
|
||||
return Location {
|
||||
file: file,
|
||||
row: file.currentLine,
|
||||
column: file.currentColumn,
|
||||
}
|
||||
}
|
||||
|
19
file/location.go
Normal file
19
file/location.go
Normal file
@ -0,0 +1,19 @@
|
||||
package file
|
||||
|
||||
// Location represents a specific point in a file. It is used for error
|
||||
// reporting.
|
||||
type Location struct {
|
||||
file *File
|
||||
row int
|
||||
column int
|
||||
}
|
||||
|
||||
// Error prints an error at this location.
|
||||
func (location Location) Error (width int, message string) {
|
||||
location.file.Error(location.column, location.row, width, message)
|
||||
}
|
||||
|
||||
// Warn prints a warning at this location.
|
||||
func (location Location) Warn (width int, message string) {
|
||||
location.file.Warn(location.column, location.row, width, message)
|
||||
}
|
Reference in New Issue
Block a user