-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
148 lines (125 loc) · 4.08 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"strconv"
)
/*
Function which prints the 'Unexpected String Literal' error message
Parameters:
- line: the line of code which caused the error
- lineNumber: the line of the code which contains the error
- token: the token which caused the error
Returns: Nothing
Prints: The error to the console.
*/
func printParseError(line string, lineNumber int, token string) {
functionMessage := ""
functionMessage += "\n"
functionMessage += "💀 Unexpected String Literal\n"
functionMessage += `Unexpected String Literal '` + token + `' at line number ` + strconv.Itoa(lineNumber) + "\n"
functionMessage += "👉 " + line + "\n"
functionMessage += "\n"
panicMessage += functionMessage
panic("💀 ERROR 💀")
}
/*
Function which prints the 'Expected Token' error message
Parameters:
- line: the line of code which caused the error
- lineNumber: the line of the code which contains the error
- token: the expectedToken
Returns: Nothing
Prints: The error to the console.
*/
func printExpectedTokenError(line string, lineNumber int, expectedToken string) {
functionMessage := ""
functionMessage += "\n"
functionMessage += "💀 Token was Expected\n"
functionMessage += `Expected Token '` + expectedToken + `' at line number ` + strconv.Itoa(lineNumber) + "\n"
functionMessage += "👉 " + line + "\n"
functionMessage += "\n"
panicMessage += functionMessage
panic("💀 ERROR 💀")
}
/*
The function which prints the error to the console.
Parameters:
- line: The line which contains the error.
- lineNumber: The line number of the error.
- i: The index of the error at the given line
Returns: Nothing
Prints: The error to the console.
*/
func lexError(line string, lineNumber int, i int) {
functionMessage := ""
functionMessage += "\n"
functionMessage += "💀 Tokenization Error\n"
functionMessage += `Unknown token: "` + string(line[i]) + `" at line number ` + strconv.Itoa(lineNumber) + "\n"
functionMessage += line + "\n"
for j := 0; j < i; j++ {
functionMessage += " "
}
functionMessage += "👆\n"
panicMessage += functionMessage
panic("💀 ERROR 💀")
}
/*
A function which prints the runtime error when executing the program.
Parameters:
- line: The line which contains the error.
- lineNumber: The line number of the error.
- errorMessage: The error which was thrown.
Returns: Nothing
Prints: The error to the console.
*/
func runtimeError(line string, lineNumber int, errorMessage string) {
functionMessage := ""
functionMessage += "\n"
functionMessage += "💀 Runtime Error\n"
functionMessage += errorMessage + ` at line ` + strconv.Itoa(lineNumber) + "\n"
functionMessage += "👉 " + line + "\n"
functionMessage += "\n"
panicMessage += functionMessage
panic("💀 ERROR 💀")
}
/*
A Function which prints the file access error when executing the program.
It exits the program with a code of 1.
Parameters:
- errorMessage: The error message which should be printed.
- err: The error which was thrown.
*/
func printFileAccessError(errorMessage string, err string) {
functionMessage := ""
functionMessage += "\n"
functionMessage += errorMessage + "\n"
functionMessage += "Error: " + err
functionMessage += "\n"
panicMessage += functionMessage
panic("💀 ERROR 💀")
}
/*
A function which will throw an error if no command line arguments was passed
*/
func noArguments() {
functionMessage := ""
functionMessage += "\n💀 No file name provided. Please provide a file name."
functionMessage += "Usage: infant <filename>.infant"
functionMessage += "\n💀 Exiting..."
panicMessage += functionMessage
panic("💀 ERROR 💀")
}
/*
A function which will throw an error if the parser cannot build a grammar with the tokens
Parameters:
- line: string - The line which contains the error.
- lineNumber: int - The line number of the error.
*/
func parseError(line string, lineNumber int) {
functionMessage := ""
functionMessage += "\n💀 Parse Error\n"
functionMessage += `Malformed Statement at line number ` + strconv.Itoa(lineNumber) + "\n"
functionMessage += "👉 " + line + "\n"
functionMessage += "\n"
panicMessage += functionMessage
panic("💀 ERROR 💀")
}