Skip to content

Instantly share code, notes, and snippets.

@johnno1962
Created March 25, 2018 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnno1962/43fe6507abef88127e97a0342165976c to your computer and use it in GitHub Desktop.
Save johnno1962/43fe6507abef88127e97a0342165976c to your computer and use it in GitHub Desktop.
UNICODE char literal
case '"':
return lexStringLiteral(Token::StringModifiers(0));
case '\'':
return lexChar();
case '`':
return lexEscapedIdentifier();
}
}
void Lexer::lexChar() {
const char *TokStart = CurPtr;
uint32_t c = 0;
if (*CurPtr == '\\') {
switch (*++CurPtr) {
case 'r': c = '\r'; break;
case 'n': c = '\n'; break;
case '\'': c = '\''; ++CurPtr; break;
case '\\': c = '\\'; break;
default:
diagnose(CurPtr, diag::lex_character_invalid_escape);
}
}
else {
c = swift::validateUTF8CharacterAndAdvance(CurPtr, BufferEnd);
if (*CurPtr != '\'' || c == ~0U) {
diagnose(TokStart, diag::lex_character_not_codepoint);
c = 0;
}
}
// nobody is suggesting for a minute this is how this should be done
// but it does have all the right semantics.
static char *charLiteralBuffer, *nextAvailable;
static const ptrdiff_t buffsize = 1000;
if (!charLiteralBuffer || nextAvailable - charLiteralBuffer > buffsize - 20) {
auto mem = llvm::MemoryBuffer::getNewMemBuffer(buffsize);
charLiteralBuffer = nextAvailable = (char *)mem->getBufferStart();
((SourceManager *)&SourceMgr)->addNewSourceBuffer(std::move(mem));
}
char *newLiteral = nextAvailable;
nextAvailable += sprintf(nextAvailable, "%u", c);
NextToken.setToken(tok::integer_literal, StringRef(newLiteral));
while (*CurPtr++ != '\'');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment