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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
//===-- GoLexer.cpp ---------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <string.h>
#include "GoLexer.h"
using namespace lldb_private;
llvm::StringMap<GoLexer::TokenType> *GoLexer::m_keywords;
GoLexer::GoLexer(const char *src)
: m_src(src), m_end(src + strlen(src)), m_last_token(TOK_INVALID, "") {}
bool GoLexer::SkipWhitespace() {
bool saw_newline = false;
for (; m_src < m_end; ++m_src) {
if (*m_src == '\n')
saw_newline = true;
if (*m_src == '/' && !SkipComment())
return saw_newline;
else if (!IsWhitespace(*m_src))
return saw_newline;
}
return saw_newline;
}
bool GoLexer::SkipComment() {
if (m_src[0] == '/' && m_src[1] == '/') {
for (const char *c = m_src + 2; c < m_end; ++c) {
if (*c == '\n') {
m_src = c - 1;
return true;
}
}
return true;
} else if (m_src[0] == '/' && m_src[1] == '*') {
for (const char *c = m_src + 2; c < m_end; ++c) {
if (c[0] == '*' && c[1] == '/') {
m_src = c + 1;
return true;
}
}
}
return false;
}
const GoLexer::Token &GoLexer::Lex() {
bool newline = SkipWhitespace();
const char *start = m_src;
m_last_token.m_type = InternalLex(newline);
m_last_token.m_value = llvm::StringRef(start, m_src - start);
return m_last_token;
}
GoLexer::TokenType GoLexer::InternalLex(bool newline) {
if (m_src >= m_end) {
return TOK_EOF;
}
if (newline) {
switch (m_last_token.m_type) {
case TOK_IDENTIFIER:
case LIT_FLOAT:
case LIT_IMAGINARY:
case LIT_INTEGER:
case LIT_RUNE:
case LIT_STRING:
case KEYWORD_BREAK:
case KEYWORD_CONTINUE:
case KEYWORD_FALLTHROUGH:
case KEYWORD_RETURN:
case OP_PLUS_PLUS:
case OP_MINUS_MINUS:
case OP_RPAREN:
case OP_RBRACK:
case OP_RBRACE:
return OP_SEMICOLON;
default:
break;
}
}
char c = *m_src;
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return DoNumber();
case '+':
case '-':
case '*':
case '/':
case '%':
case '&':
case '|':
case '^':
case '<':
case '>':
case '!':
case ':':
case ';':
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
case ',':
case '=':
return DoOperator();
case '.':
if (IsDecimal(m_src[1]))
return DoNumber();
return DoOperator();
case '$':
// For lldb persistent vars.
return DoIdent();
case '"':
case '`':
return DoString();
case '\'':
return DoRune();
default:
break;
}
if (IsLetterOrDigit(c))
return DoIdent();
++m_src;
return TOK_INVALID;
}
GoLexer::TokenType GoLexer::DoOperator() {
TokenType t = TOK_INVALID;
if (m_end - m_src > 2) {
t = LookupKeyword(llvm::StringRef(m_src, 3));
if (t != TOK_INVALID)
m_src += 3;
}
if (t == TOK_INVALID && m_end - m_src > 1) {
t = LookupKeyword(llvm::StringRef(m_src, 2));
if (t != TOK_INVALID)
m_src += 2;
}
if (t == TOK_INVALID) {
t = LookupKeyword(llvm::StringRef(m_src, 1));
++m_src;
}
return t;
}
GoLexer::TokenType GoLexer::DoIdent() {
const char *start = m_src++;
while (m_src < m_end && IsLetterOrDigit(*m_src)) {
++m_src;
}
TokenType kw = LookupKeyword(llvm::StringRef(start, m_src - start));
if (kw != TOK_INVALID)
return kw;
return TOK_IDENTIFIER;
}
GoLexer::TokenType GoLexer::DoNumber() {
if (m_src[0] == '0' && (m_src[1] == 'x' || m_src[1] == 'X')) {
m_src += 2;
while (IsHexChar(*m_src))
++m_src;
return LIT_INTEGER;
}
bool dot_ok = true;
bool e_ok = true;
while (true) {
while (IsDecimal(*m_src))
++m_src;
switch (*m_src) {
case 'i':
++m_src;
return LIT_IMAGINARY;
case '.':
if (!dot_ok)
return LIT_FLOAT;
++m_src;
dot_ok = false;
break;
case 'e':
case 'E':
if (!e_ok)
return LIT_FLOAT;
dot_ok = e_ok = false;
++m_src;
if (*m_src == '+' || *m_src == '-')
++m_src;
break;
default:
if (dot_ok)
return LIT_INTEGER;
return LIT_FLOAT;
}
}
}
GoLexer::TokenType GoLexer::DoRune() {
while (++m_src < m_end) {
switch (*m_src) {
case '\'':
++m_src;
return LIT_RUNE;
case '\n':
return TOK_INVALID;
case '\\':
if (m_src[1] == '\n')
return TOK_INVALID;
++m_src;
}
}
return TOK_INVALID;
}
GoLexer::TokenType GoLexer::DoString() {
if (*m_src == '`') {
while (++m_src < m_end) {
if (*m_src == '`') {
++m_src;
return LIT_STRING;
}
}
return TOK_INVALID;
}
while (++m_src < m_end) {
switch (*m_src) {
case '"':
++m_src;
return LIT_STRING;
case '\n':
return TOK_INVALID;
case '\\':
if (m_src[1] == '\n')
return TOK_INVALID;
++m_src;
}
}
return TOK_INVALID;
}
GoLexer::TokenType GoLexer::LookupKeyword(llvm::StringRef id) {
if (m_keywords == nullptr)
m_keywords = InitKeywords();
const auto &it = m_keywords->find(id);
if (it == m_keywords->end())
return TOK_INVALID;
return it->second;
}
llvm::StringRef GoLexer::LookupToken(TokenType t) {
if (m_keywords == nullptr)
m_keywords = InitKeywords();
for (const auto &e : *m_keywords) {
if (e.getValue() == t)
return e.getKey();
}
return "";
}
llvm::StringMap<GoLexer::TokenType> *GoLexer::InitKeywords() {
auto &result = *new llvm::StringMap<TokenType>(128);
result["break"] = KEYWORD_BREAK;
result["default"] = KEYWORD_DEFAULT;
result["func"] = KEYWORD_FUNC;
result["interface"] = KEYWORD_INTERFACE;
result["select"] = KEYWORD_SELECT;
result["case"] = KEYWORD_CASE;
result["defer"] = KEYWORD_DEFER;
result["go"] = KEYWORD_GO;
result["map"] = KEYWORD_MAP;
result["struct"] = KEYWORD_STRUCT;
result["chan"] = KEYWORD_CHAN;
result["else"] = KEYWORD_ELSE;
result["goto"] = KEYWORD_GOTO;
result["package"] = KEYWORD_PACKAGE;
result["switch"] = KEYWORD_SWITCH;
result["const"] = KEYWORD_CONST;
result["fallthrough"] = KEYWORD_FALLTHROUGH;
result["if"] = KEYWORD_IF;
result["range"] = KEYWORD_RANGE;
result["type"] = KEYWORD_TYPE;
result["continue"] = KEYWORD_CONTINUE;
result["for"] = KEYWORD_FOR;
result["import"] = KEYWORD_IMPORT;
result["return"] = KEYWORD_RETURN;
result["var"] = KEYWORD_VAR;
result["+"] = OP_PLUS;
result["-"] = OP_MINUS;
result["*"] = OP_STAR;
result["/"] = OP_SLASH;
result["%"] = OP_PERCENT;
result["&"] = OP_AMP;
result["|"] = OP_PIPE;
result["^"] = OP_CARET;
result["<<"] = OP_LSHIFT;
result[">>"] = OP_RSHIFT;
result["&^"] = OP_AMP_CARET;
result["+="] = OP_PLUS_EQ;
result["-="] = OP_MINUS_EQ;
result["*="] = OP_STAR_EQ;
result["/="] = OP_SLASH_EQ;
result["%="] = OP_PERCENT_EQ;
result["&="] = OP_AMP_EQ;
result["|="] = OP_PIPE_EQ;
result["^="] = OP_CARET_EQ;
result["<<="] = OP_LSHIFT_EQ;
result[">>="] = OP_RSHIFT_EQ;
result["&^="] = OP_AMP_CARET_EQ;
result["&&"] = OP_AMP_AMP;
result["||"] = OP_PIPE_PIPE;
result["<-"] = OP_LT_MINUS;
result["++"] = OP_PLUS_PLUS;
result["--"] = OP_MINUS_MINUS;
result["=="] = OP_EQ_EQ;
result["<"] = OP_LT;
result[">"] = OP_GT;
result["="] = OP_EQ;
result["!"] = OP_BANG;
result["!="] = OP_BANG_EQ;
result["<="] = OP_LT_EQ;
result[">="] = OP_GT_EQ;
result[":="] = OP_COLON_EQ;
result["..."] = OP_DOTS;
result["("] = OP_LPAREN;
result["["] = OP_LBRACK;
result["{"] = OP_LBRACE;
result[","] = OP_COMMA;
result["."] = OP_DOT;
result[")"] = OP_RPAREN;
result["]"] = OP_RBRACK;
result["}"] = OP_RBRACE;
result[";"] = OP_SEMICOLON;
result[":"] = OP_COLON;
return &result;
}
|