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
13
file/file.go
13
file/file.go
@ -10,6 +10,7 @@ type File struct {
|
|||||||
path string
|
path string
|
||||||
file *os.File
|
file *os.File
|
||||||
currentLine int
|
currentLine int
|
||||||
|
currentColumn int
|
||||||
lines [][]byte
|
lines [][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,10 +45,12 @@ func (file *File) Read (bytes []byte) (amountRead int, err error) {
|
|||||||
if char == '\n' {
|
if char == '\n' {
|
||||||
file.lines = append(file.lines, []byte { })
|
file.lines = append(file.lines, []byte { })
|
||||||
file.currentLine ++
|
file.currentLine ++
|
||||||
|
file.currentColumn = 0
|
||||||
} else {
|
} else {
|
||||||
file.lines[file.currentLine] = append (
|
file.lines[file.currentLine] = append (
|
||||||
file.lines[file.currentLine],
|
file.lines[file.currentLine],
|
||||||
bytes...)
|
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) {
|
func (file *File) Warn (column, row, width int, message string) {
|
||||||
file.mistake(column, row, width, message, "!!!")
|
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