-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtoken.go
87 lines (79 loc) · 1.36 KB
/
token.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
package denada
import "fmt"
type TokenType int
const (
T_IDENTIFIER TokenType = iota
T_RBRACE
T_LBRACE
T_LPAREN
T_RPAREN
T_QUOTE
T_EQUALS
T_SEMI
T_COMMA
T_EOF
T_WHITE
T_SLCOMMENT
T_MLCOMMENT
T_UNKNOWN
)
func (tt TokenType) String() string {
switch tt {
case T_IDENTIFIER:
return "<identifier>"
case T_RBRACE:
return "}"
case T_LBRACE:
return "{"
case T_LPAREN:
return "("
case T_RPAREN:
return ")"
case T_QUOTE:
return "\""
case T_EQUALS:
return "="
case T_SEMI:
return ";"
case T_COMMA:
return ","
case T_WHITE:
return "<whitespace>"
case T_SLCOMMENT:
return "<slcomment>"
case T_MLCOMMENT:
return "<mlcomment>"
case T_EOF:
return "EOF"
case T_UNKNOWN:
fallthrough
default:
return "<???>"
}
}
type UnexpectedToken struct {
Found Token
Expected string
}
func (u UnexpectedToken) Error() string {
if u.Found.File != "" {
return fmt.Sprintf("Expecting %s, found '%v' @ (%d, %d) in %s", u.Expected, u.Found.Type,
u.Found.Line, u.Found.Column, u.Found.File)
} else {
return fmt.Sprintf("Expecting %s, found '%v' @ (%d, %d)", u.Expected, u.Found.Type,
u.Found.Line, u.Found.Column)
}
}
type Token struct {
Type TokenType
String string
Line int
Column int
File string
}
func (t Token) Expected(expected string) UnexpectedToken {
return UnexpectedToken{
Found: t,
Expected: expected,
}
}