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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
// Lexer tokens
package lexer
// Token is the type of a single token.
type Token int
const (
ILLEGAL Token = iota
EOF
NEWLINE
CONCAT // Not really a token, but used as an operator
// Symbols
ADD
ADD_ASSIGN
AND
APPEND
ASSIGN
AT
COLON
COMMA
DECR
DIV
DIV_ASSIGN
DOLLAR
EQUALS
GTE
GREATER
INCR
LBRACE
LBRACKET
LESS
LPAREN
LTE
MATCH
MOD
MOD_ASSIGN
MUL
MUL_ASSIGN
NOT_MATCH
NOT
NOT_EQUALS
OR
PIPE
POW
POW_ASSIGN
QUESTION
RBRACE
RBRACKET
RPAREN
SEMICOLON
SUB
SUB_ASSIGN
// Keywords
BEGIN
BREAK
CONTINUE
DELETE
DO
ELSE
END
EXIT
FOR
FUNCTION
GETLINE
IF
IN
NEXT
NEXTFILE
PRINT
PRINTF
RETURN
WHILE
// Built-in functions
F_ATAN2
F_CLOSE
F_COS
F_EXP
F_FFLUSH
F_GSUB
F_INDEX
F_INT
F_LENGTH
F_LOG
F_MATCH
F_RAND
F_SIN
F_SPLIT
F_SPRINTF
F_SQRT
F_SRAND
F_SUB
F_SUBSTR
F_SYSTEM
F_TOLOWER
F_TOUPPER
// Literals and names (variables and arrays)
NAME
NUMBER
STRING
REGEX
LAST = REGEX
FIRST_FUNC = F_ATAN2
LAST_FUNC = F_TOUPPER
)
var keywordTokens = map[string]Token{
"BEGIN": BEGIN,
"break": BREAK,
"continue": CONTINUE,
"delete": DELETE,
"do": DO,
"else": ELSE,
"END": END,
"exit": EXIT,
"for": FOR,
"function": FUNCTION,
"getline": GETLINE,
"if": IF,
"in": IN,
"next": NEXT,
"nextfile": NEXTFILE,
"print": PRINT,
"printf": PRINTF,
"return": RETURN,
"while": WHILE,
"atan2": F_ATAN2,
"close": F_CLOSE,
"cos": F_COS,
"exp": F_EXP,
"fflush": F_FFLUSH,
"gsub": F_GSUB,
"index": F_INDEX,
"int": F_INT,
"length": F_LENGTH,
"log": F_LOG,
"match": F_MATCH,
"rand": F_RAND,
"sin": F_SIN,
"split": F_SPLIT,
"sprintf": F_SPRINTF,
"sqrt": F_SQRT,
"srand": F_SRAND,
"sub": F_SUB,
"substr": F_SUBSTR,
"system": F_SYSTEM,
"tolower": F_TOLOWER,
"toupper": F_TOUPPER,
}
// KeywordToken returns the token associated with the given keyword
// string, or ILLEGAL if given name is not a keyword.
func KeywordToken(name string) Token {
return keywordTokens[name]
}
var tokenNames = map[Token]string{
ILLEGAL: "<illegal>",
EOF: "EOF",
NEWLINE: "<newline>",
CONCAT: "<concat>",
ADD: "+",
ADD_ASSIGN: "+=",
AND: "&&",
APPEND: ">>",
ASSIGN: "=",
AT: "@",
COLON: ":",
COMMA: ",",
DECR: "--",
DIV: "/",
DIV_ASSIGN: "/=",
DOLLAR: "$",
EQUALS: "==",
GTE: ">=",
GREATER: ">",
INCR: "++",
LBRACE: "{",
LBRACKET: "[",
LESS: "<",
LPAREN: "(",
LTE: "<=",
MATCH: "~",
MOD: "%",
MOD_ASSIGN: "%=",
MUL: "*",
MUL_ASSIGN: "*=",
NOT_MATCH: "!~",
NOT: "!",
NOT_EQUALS: "!=",
OR: "||",
PIPE: "|",
POW: "^",
POW_ASSIGN: "^=",
QUESTION: "?",
RBRACE: "}",
RBRACKET: "]",
RPAREN: ")",
SEMICOLON: ";",
SUB: "-",
SUB_ASSIGN: "-=",
BEGIN: "BEGIN",
BREAK: "break",
CONTINUE: "continue",
DELETE: "delete",
DO: "do",
ELSE: "else",
END: "END",
EXIT: "exit",
FOR: "for",
FUNCTION: "function",
GETLINE: "getline",
IF: "if",
IN: "in",
NEXT: "next",
NEXTFILE: "nextfile",
PRINT: "print",
PRINTF: "printf",
RETURN: "return",
WHILE: "while",
F_ATAN2: "atan2",
F_CLOSE: "close",
F_COS: "cos",
F_EXP: "exp",
F_FFLUSH: "fflush",
F_GSUB: "gsub",
F_INDEX: "index",
F_INT: "int",
F_LENGTH: "length",
F_LOG: "log",
F_MATCH: "match",
F_RAND: "rand",
F_SIN: "sin",
F_SPLIT: "split",
F_SPRINTF: "sprintf",
F_SQRT: "sqrt",
F_SRAND: "srand",
F_SUB: "sub",
F_SUBSTR: "substr",
F_SYSTEM: "system",
F_TOLOWER: "tolower",
F_TOUPPER: "toupper",
NAME: "name",
NUMBER: "number",
STRING: "string",
REGEX: "regex",
}
// String returns the string name of this token.
func (t Token) String() string {
return tokenNames[t]
}
|