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
|
//===-- Lexer.cpp ---------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "llbuild/Ninja/Lexer.h"
#include "llbuild/Basic/LLVM.h"
#include <cstring>
#include <string>
#include <iostream>
#include <iomanip>
using namespace llbuild;
using namespace llbuild::ninja;
///
const char* Token::getKindName() const {
#define CASE(name) case Kind::name: return #name
switch (tokenKind) {
CASE(Colon);
CASE(Comment);
CASE(EndOfFile);
CASE(Equals);
CASE(Identifier);
CASE(Indentation);
CASE(KWBuild);
CASE(KWDefault);
CASE(KWInclude);
CASE(KWPool);
CASE(KWRule);
CASE(KWSubninja);
CASE(Newline);
CASE(Pipe);
CASE(PipePipe);
CASE(String);
CASE(Unknown);
}
#undef CASE
return "<invalid token kind>";
}
#ifndef NDEBUG
void Token::dump() {
std::cerr << "(Token \"" << getKindName() << "\" "
<< (const void*) start << " " << length << " "
<< line << " " << column << ")\n";
}
#endif
///
Lexer::Lexer(StringRef buffer)
: buffer(buffer), bufferPos(buffer.data()), lineNumber(1), columnNumber(0),
mode(LexingMode::None)
{
}
Lexer::~Lexer() {
}
int Lexer::peekNextChar() {
if (bufferPos == buffer.end())
return -1;
return *bufferPos;
}
int Lexer::getNextChar() {
if (bufferPos == buffer.end())
return -1;
// Handle DOS/Mac newlines here, by stripping duplicates and by returning '\n'
// for both.
char result = *bufferPos++;
if (result == '\n' || result == '\r') {
if (bufferPos != buffer.end() && *bufferPos == ('\n' + '\r' - result))
++bufferPos;
result = '\n';
}
if (result == '\n') {
++lineNumber;
columnNumber = 0;
} else {
++columnNumber;
}
return result;
}
Token& Lexer::setTokenKind(Token& result, Token::Kind kind) const {
result.tokenKind = kind;
result.length = bufferPos - result.start;
return result;
}
void Lexer::skipToEndOfLine() {
// Skip to the end of the line, but not past the actual newline character
// (which we want to generate a Newline token).
for (;;) {
int c = peekNextChar();
if (c == -1 || c == '\n' || c == '\r')
break;
getNextChar();
}
}
Token& Lexer::setIdentifierTokenKind(Token& result) const {
unsigned length = bufferPos - result.start;
switch (length) {
case 4:
if (memcmp("rule", result.start, 4) == 0)
return setTokenKind(result, Token::Kind::KWRule);
if (memcmp("pool", result.start, 4) == 0)
return setTokenKind(result, Token::Kind::KWPool);
break;
case 5:
if (memcmp("build", result.start, 5) == 0)
return setTokenKind(result, Token::Kind::KWBuild);
break;
case 7:
if (memcmp("default", result.start, 7) == 0)
return setTokenKind(result, Token::Kind::KWDefault);
if (memcmp("include", result.start, 7) == 0)
return setTokenKind(result, Token::Kind::KWInclude);
break;
case 8:
if (memcmp("subninja", result.start, 7) == 0)
return setTokenKind(result, Token::Kind::KWSubninja);
break;
}
return setTokenKind(result, Token::Kind::Identifier);
}
Token& Lexer::lexIdentifier(Token& result) {
// Consume characters as long as we are in an identifier.
while (Lexer::isIdentifierChar(peekNextChar())) {
getNextChar();
}
// If we are in identifier specific mode, ignore keywords.
if (mode == Lexer::LexingMode::IdentifierSpecific)
return setTokenKind(result, Token::Kind::Identifier);
// Recognize keywords specially.
return setIdentifierTokenKind(result);
}
static bool isNonNewlineSpace(int c) {
return isspace(c) && c != '\n' && c != '\r';
}
Token &Lexer::lexPathString(Token &result) {
// String tokens in path contexts consume until a space, ':', or '|'
// character.
while (true) {
int c = peekNextChar();
// If this is an escape character, skip the next character.
if (c == '$') {
getNextChar(); // Consume the actual '$'.
// Consume the next character.
c = getNextChar();
// If the character was a newline, consume any leading spaces.
if (c == '\n') {
while (isNonNewlineSpace(peekNextChar()))
getNextChar();
}
continue;
}
// Otherwise, continue only if this is not the EOL or EOF.
if (isspace(c) || c == ':' || c == '|' || c == -1)
break;
getNextChar();
}
return setTokenKind(result, Token::Kind::String);
}
Token& Lexer::lexVariableString(Token& result) {
// String tokens in variable assignments consume until the end of the line.
while (true) {
int c = peekNextChar();
// If this is an escape character, skip the next character.
if (c == '$') {
getNextChar(); // Consume the actual '$'.
getNextChar(); // Consume the next character.
continue;
}
// Otherwise, continue only if this is not the EOL or EOF.
if (c == '\n' || c == -1 || c == '\r')
break;
getNextChar();
}
return setTokenKind(result, Token::Kind::String);
}
Token& Lexer::lex(Token& result) {
// Check if we need to emit an indentation token.
int c = peekNextChar();
if (isNonNewlineSpace(c) && columnNumber == 0) {
// If we are at the start of a line, then any leading whitespace should be
// parsed as an indentation token.
//
// We do not need to handle "$\n" sequences here because they will be
// consumed next, and the exact length of the indentation token is never
// used.
if (columnNumber == 0) {
result.start = bufferPos;
result.line = lineNumber;
result.column = columnNumber;
do {
getNextChar();
} while (isNonNewlineSpace(peekNextChar()));
return setTokenKind(result, Token::Kind::Indentation);
}
}
// Otherwise, consume any leading whitespace or "$\n" escape sequences (except
// at the start of lines, which Ninja does not recognize).
while (true) {
// Check for escape sequences.
if (c == '$' && columnNumber != 0) {
// If this is a newline escape, consume it.
if ((bufferPos + 1 != buffer.end() && bufferPos[1] == '\n') ||
(bufferPos + 2 != buffer.end() && bufferPos[1] == '\r' &&
bufferPos[2] == '\n')) {
getNextChar();
getNextChar();
} else {
// Otherwise, break out and lex normally.
break;
}
} else if (isNonNewlineSpace(c)) {
getNextChar();
} else {
break;
}
c = peekNextChar();
}
// Initialize the token position.
result.start = bufferPos;
result.line = lineNumber;
result.column = columnNumber;
// Check if we are at a string mode independent token.
if (c == '\n' || c == '\r') {
getNextChar();
return setTokenKind(result, Token::Kind::Newline);
}
if (c == -1)
return setTokenKind(result, Token::Kind::EndOfFile);
// If we are in string lexing mode, delegate immediately if appropriate.
if (mode == LexingMode::VariableString)
return lexVariableString(result);
if (mode == LexingMode::PathString) {
// Only delegate for characters not special to path lexing.
if (c != ':' && c != '|')
return lexPathString(result);
}
// Otherwise, consume the character and lex from the regular token set.
getNextChar();
switch (c) {
case ':': return setTokenKind(result, Token::Kind::Colon);
case '=': return setTokenKind(result, Token::Kind::Equals);
case '#': {
skipToEndOfLine();
return setTokenKind(result, Token::Kind::Comment);
}
case '|': {
if (peekNextChar() == '|') {
(void) getNextChar();
return setTokenKind(result, Token::Kind::PipePipe);
}
return setTokenKind(result, Token::Kind::Pipe);
}
default:
if (Lexer::isIdentifierChar(c))
return lexIdentifier(result);
return setTokenKind(result, Token::Kind::Unknown);
}
}
|