-
Notifications
You must be signed in to change notification settings - Fork 260
/
token.go
192 lines (175 loc) · 4.58 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"strings"
)
// TokenType represents a token's type.
type TokenType string
// Token represents a lexer token.
type Token struct {
Type TokenType
Literal string
Line int
Column int
}
// Tokens for the VHS language
const (
AT = "@"
EQUAL = "="
PLUS = "+"
PERCENT = "%"
SLASH = "/"
DOT = "."
DASH = "-"
EM = "EM"
MILLISECONDS = "MILLISECONDS"
MINUTES = "MINUTES"
PX = "PX"
SECONDS = "SECONDS"
EOF = "EOF"
ILLEGAL = "ILLEGAL"
ALT = "ALT"
BACKSPACE = "BACKSPACE"
CTRL = "CTRL"
DELETE = "DELETE"
END = "END"
ENTER = "ENTER"
ESCAPE = "ESCAPE"
HOME = "HOME"
INSERT = "INSERT"
PAGEDOWN = "PAGEDOWN"
PAGEUP = "PAGEUP"
SLEEP = "SLEEP"
SPACE = "SPACE"
TAB = "TAB"
SHIFT = "SHIFT"
COMMENT = "COMMENT"
NUMBER = "NUMBER"
STRING = "STRING"
JSON = "JSON"
BOOLEAN = "BOOLEAN"
DOWN = "DOWN"
LEFT = "LEFT"
RIGHT = "RIGHT"
UP = "UP"
HIDE = "HIDE"
OUTPUT = "OUTPUT"
REQUIRE = "REQUIRE"
SET = "SET"
SHOW = "SHOW"
SOURCE = "SOURCE"
TYPE = "TYPE"
SHELL = "SHELL"
FONT_FAMILY = "FONT_FAMILY" //nolint:revive
FONT_SIZE = "FONT_SIZE" //nolint:revive
FRAMERATE = "FRAMERATE"
PLAYBACK_SPEED = "PLAYBACK_SPEED" //nolint:revive
HEIGHT = "HEIGHT"
WIDTH = "WIDTH"
LETTER_SPACING = "LETTER_SPACING" //nolint:revive
LINE_HEIGHT = "LINE_HEIGHT" //nolint:revive
TYPING_SPEED = "TYPING_SPEED" //nolint:revive
PADDING = "PADDING"
THEME = "THEME"
LOOP_OFFSET = "LOOP_OFFSET" //nolint:revive
MARGIN_FILL = "MARGIN_FILL" //nolint:revive
MARGIN = "MARGIN" //nolint:revive
WINDOW_BAR = "WINDOW_BAR" //nolint:revive
WINDOW_BAR_SIZE = "WINDOW_BAR_SIZE" //nolint:revive
BORDER_RADIUS = "CORNER_RADIUS" //nolint:revive
CURSOR_BLINK = "CURSOR_BLINK" //nolint:revive
)
var keywords = map[string]TokenType{
"em": EM,
"px": PX,
"ms": MILLISECONDS,
"s": SECONDS,
"m": MINUTES,
"Set": SET,
"Sleep": SLEEP,
"Type": TYPE,
"Enter": ENTER,
"Space": SPACE,
"Backspace": BACKSPACE,
"Ctrl": CTRL,
"Alt": ALT,
"Shift": SHIFT,
"Down": DOWN,
"Left": LEFT,
"Right": RIGHT,
"Up": UP,
"PageUp": PAGEUP,
"PageDown": PAGEDOWN,
"Tab": TAB,
"Escape": ESCAPE,
"End": END,
"Hide": HIDE,
"Require": REQUIRE,
"Show": SHOW,
"Output": OUTPUT,
"Shell": SHELL,
"FontFamily": FONT_FAMILY,
"MarginFill": MARGIN_FILL,
"Margin": MARGIN,
"WindowBar": WINDOW_BAR,
"WindowBarSize": WINDOW_BAR_SIZE,
"BorderRadius": BORDER_RADIUS,
"FontSize": FONT_SIZE,
"Framerate": FRAMERATE,
"Height": HEIGHT,
"LetterSpacing": LETTER_SPACING,
"LineHeight": LINE_HEIGHT,
"PlaybackSpeed": PLAYBACK_SPEED,
"TypingSpeed": TYPING_SPEED,
"Padding": PADDING,
"Theme": THEME,
"Width": WIDTH,
"LoopOffset": LOOP_OFFSET,
"Source": SOURCE,
"CursorBlink": CURSOR_BLINK,
"true": BOOLEAN,
"false": BOOLEAN,
}
// IsSetting returns whether a token is a setting.
func IsSetting(t TokenType) bool {
switch t {
case SHELL, FONT_FAMILY, FONT_SIZE, LETTER_SPACING, LINE_HEIGHT,
FRAMERATE, TYPING_SPEED, THEME, PLAYBACK_SPEED, HEIGHT, WIDTH,
PADDING, LOOP_OFFSET, MARGIN_FILL, MARGIN, WINDOW_BAR,
WINDOW_BAR_SIZE, BORDER_RADIUS, CURSOR_BLINK:
return true
default:
return false
}
}
// IsCommand returns whether the string is a command.
func IsCommand(t TokenType) bool {
switch t {
case TYPE, SLEEP,
UP, DOWN, RIGHT, LEFT, PAGEUP, PAGEDOWN,
ENTER, BACKSPACE, DELETE, TAB,
ESCAPE, HOME, INSERT, END, CTRL, SOURCE:
return true
default:
return false
}
}
// IsModifier returns whether the token is a modifier.
func IsModifier(t TokenType) bool {
return t == ALT || t == SHIFT
}
// String converts a token to it's human readable string format.
func (t TokenType) String() string {
if IsCommand(t) || IsSetting(t) {
return strings.ToTitle(string(t[0])) + strings.ToLower(string(t[1:]))
}
return string(t)
}
// LookupIdentifier returns whether the identifier is a keyword.
// In `vhs`, there are no _actual_ identifiers, i.e. there are no variables.
// Instead, identifiers are simply strings (i.e. bare words).
func LookupIdentifier(ident string) TokenType {
if t, ok := keywords[ident]; ok {
return t
}
return STRING
}