-
Notifications
You must be signed in to change notification settings - Fork 4
/
token.go
93 lines (85 loc) · 1.92 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
88
89
90
91
92
93
package token
import "fmt"
// Type is the type of the given token as a string.
type Type string
// Token contains the lexeme read by the scanner.
type Token struct {
Type Type // Token type
Lexeme string // String representation of literal value
Literal interface{} // Native value in Go
Line int // Line of occurance
Column int // Column of occurance on line
File string // File of occurance
}
func (token *Token) String() string {
return fmt.Sprintf("%s \"%s\" %v on line %d", token.Type, token.Lexeme, token.Literal, token.Line)
}
const (
// single-character tokens
COLON = ":"
COMMA = ","
LEFTBRACE = "{"
LEFTBRACKET = "["
LEFTPAREN = "("
MINUS = "-"
PLUS = "+"
QUESTION = "?"
RIGHTBRACE = "}"
RIGHTBRACKET = "]"
RIGHTPAREN = ")"
SEMICOLON = ";"
SLASH = "/"
STAR = "*"
PERCENT = "%"
// one or two character tokens
BANG = "!"
BANGEQUAL = "!="
DOT = "."
DOTDOT = ".."
EQUAL = "="
EQUALEQUAL = "=="
GREATER = ">"
GREATEREQUAL = ">="
LESS = "<"
LESSEQUAL = "<="
PLUSEQUAL = "+="
PLUSPLUS = "++"
MINUSEQUAL = "-="
MINUSMINUS = "--"
STAREQUAL = "*="
SLASHEQUAL = "/="
// literals
IDENTIFIER = "IDENTIFIER"
STRING = "STRING"
NUMBER = "NUMBER"
// keywords
AND = "and"
AS = "as"
BREAK = "break"
CASE = "case"
CLASS = "class"
CONTINUE = "continue"
DEFAULT = "default"
ELSE = "else"
EXTENDS = "extends"
FALSE = "false"
FOR = "for"
FROM = "from"
FUNCTION = "function"
IF = "if"
IMPORT = "import"
IN = "in"
NULL = "null"
OR = "or"
PRINT = "print"
RETURN = "return"
SUPER = "super"
SWITCH = "switch"
THIS = "this"
TRAIT = "trait"
TRUE = "true"
USE = "use"
WHILE = "while"
EOF = "eof"
INVALID = "__INVALID__"
)