2007-11-18 02:57:27 +00:00
|
|
|
//===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:37:13 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-11-18 02:57:27 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implement the Lexer for TableGen.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-11-18 05:25:45 +00:00
|
|
|
#include "TGLexer.h"
|
2010-12-08 20:02:49 +00:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Config/config.h" // for strtoull()/strtoll() define
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/TableGen/Error.h"
|
2007-11-18 02:57:27 +00:00
|
|
|
#include <cctype>
|
2012-12-03 16:50:05 +00:00
|
|
|
#include <cerrno>
|
2008-10-08 07:23:46 +00:00
|
|
|
#include <cstdio>
|
2008-02-20 11:08:44 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2011-12-22 23:08:39 +00:00
|
|
|
|
2007-11-18 02:57:27 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-06-21 03:41:50 +00:00
|
|
|
TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) {
|
2009-03-13 07:05:43 +00:00
|
|
|
CurBuffer = 0;
|
|
|
|
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
|
2007-11-18 02:57:27 +00:00
|
|
|
CurPtr = CurBuf->getBufferStart();
|
2007-11-19 07:43:52 +00:00
|
|
|
TokStart = 0;
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 03:39:35 +00:00
|
|
|
SMLoc TGLexer::getLoc() const {
|
|
|
|
return SMLoc::getFromPointer(TokStart);
|
2009-03-13 16:01:53 +00:00
|
|
|
}
|
|
|
|
|
2007-11-19 07:38:58 +00:00
|
|
|
/// ReturnError - Set the error to the specified string at the specified
|
2007-11-22 20:49:04 +00:00
|
|
|
/// location. This is defined to always return tgtok::Error.
|
2010-09-27 17:42:11 +00:00
|
|
|
tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) {
|
2007-11-19 07:38:58 +00:00
|
|
|
PrintError(Loc, Msg);
|
2007-11-22 20:49:04 +00:00
|
|
|
return tgtok::Error;
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int TGLexer::getNextChar() {
|
|
|
|
char CurChar = *CurPtr++;
|
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
2007-11-18 05:48:46 +00:00
|
|
|
return (unsigned char)CurChar;
|
2009-03-13 07:05:43 +00:00
|
|
|
case 0: {
|
2007-11-18 02:57:27 +00:00
|
|
|
// A nul character in the stream is either the end of the current buffer or
|
|
|
|
// a random nul in the file. Disambiguate that here.
|
|
|
|
if (CurPtr-1 != CurBuf->getBufferEnd())
|
|
|
|
return 0; // Just whitespace.
|
|
|
|
|
|
|
|
// If this is the end of an included file, pop the parent file off the
|
|
|
|
// include stack.
|
2009-06-21 03:39:35 +00:00
|
|
|
SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
|
|
|
|
if (ParentIncludeLoc != SMLoc()) {
|
2009-03-13 07:05:43 +00:00
|
|
|
CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
|
|
|
|
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
|
2009-03-13 16:01:53 +00:00
|
|
|
CurPtr = ParentIncludeLoc.getPointer();
|
2007-11-18 02:57:27 +00:00
|
|
|
return getNextChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, return end of file.
|
|
|
|
--CurPtr; // Another call to lex will return EOF again.
|
|
|
|
return EOF;
|
2009-03-13 07:05:43 +00:00
|
|
|
}
|
2007-11-18 02:57:27 +00:00
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
// Handle the newline character by ignoring it and incrementing the line
|
|
|
|
// count. However, be careful about 'dos style' files with \n\r in them.
|
|
|
|
// Only treat a \n\r or \r\n as a single line.
|
|
|
|
if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
|
|
|
|
*CurPtr != CurChar)
|
2007-11-18 05:48:46 +00:00
|
|
|
++CurPtr; // Eat the two char newline sequence.
|
2007-11-18 02:57:27 +00:00
|
|
|
return '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-19 13:03:35 +00:00
|
|
|
int TGLexer::peekNextChar(int Index) {
|
|
|
|
return *(CurPtr + Index);
|
|
|
|
}
|
|
|
|
|
2007-11-22 20:49:04 +00:00
|
|
|
tgtok::TokKind TGLexer::LexToken() {
|
2007-11-19 07:43:52 +00:00
|
|
|
TokStart = CurPtr;
|
2007-11-18 02:57:27 +00:00
|
|
|
// This always consumes at least one character.
|
|
|
|
int CurChar = getNextChar();
|
|
|
|
|
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
2011-10-19 13:04:43 +00:00
|
|
|
// Handle letters: [a-zA-Z_]
|
|
|
|
if (isalpha(CurChar) || CurChar == '_')
|
2007-11-18 02:57:27 +00:00
|
|
|
return LexIdentifier();
|
2011-10-19 13:04:43 +00:00
|
|
|
|
2007-11-22 20:49:04 +00:00
|
|
|
// Unknown character, emit an error.
|
|
|
|
return ReturnError(TokStart, "Unexpected character");
|
|
|
|
case EOF: return tgtok::Eof;
|
|
|
|
case ':': return tgtok::colon;
|
|
|
|
case ';': return tgtok::semi;
|
|
|
|
case '.': return tgtok::period;
|
|
|
|
case ',': return tgtok::comma;
|
|
|
|
case '<': return tgtok::less;
|
|
|
|
case '>': return tgtok::greater;
|
|
|
|
case ']': return tgtok::r_square;
|
|
|
|
case '{': return tgtok::l_brace;
|
|
|
|
case '}': return tgtok::r_brace;
|
|
|
|
case '(': return tgtok::l_paren;
|
|
|
|
case ')': return tgtok::r_paren;
|
|
|
|
case '=': return tgtok::equal;
|
|
|
|
case '?': return tgtok::question;
|
2011-10-19 13:04:43 +00:00
|
|
|
case '#': return tgtok::paste;
|
2007-11-22 20:49:04 +00:00
|
|
|
|
2007-11-18 02:57:27 +00:00
|
|
|
case 0:
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
// Ignore whitespace.
|
|
|
|
return LexToken();
|
|
|
|
case '/':
|
|
|
|
// If this is the start of a // comment, skip until the end of the line or
|
|
|
|
// the end of the buffer.
|
|
|
|
if (*CurPtr == '/')
|
|
|
|
SkipBCPLComment();
|
|
|
|
else if (*CurPtr == '*') {
|
|
|
|
if (SkipCComment())
|
2007-11-22 20:49:04 +00:00
|
|
|
return tgtok::Error;
|
|
|
|
} else // Otherwise, this is an error.
|
|
|
|
return ReturnError(TokStart, "Unexpected character");
|
2007-11-18 02:57:27 +00:00
|
|
|
return LexToken();
|
|
|
|
case '-': case '+':
|
|
|
|
case '0': case '1': case '2': case '3': case '4': case '5': case '6':
|
2011-10-19 13:03:39 +00:00
|
|
|
case '7': case '8': case '9': {
|
|
|
|
int NextChar = 0;
|
|
|
|
if (isdigit(CurChar)) {
|
|
|
|
// Allow identifiers to start with a number if it is followed by
|
|
|
|
// an identifier. This can happen with paste operations like
|
|
|
|
// foo#8i.
|
|
|
|
int i = 0;
|
|
|
|
do {
|
|
|
|
NextChar = peekNextChar(i++);
|
|
|
|
} while (isdigit(NextChar));
|
|
|
|
|
|
|
|
if (NextChar == 'x' || NextChar == 'b') {
|
|
|
|
// If this is [0-9]b[01] or [0-9]x[0-9A-fa-f] this is most
|
|
|
|
// likely a number.
|
|
|
|
int NextNextChar = peekNextChar(i);
|
|
|
|
switch (NextNextChar) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case '0': case '1':
|
|
|
|
if (NextChar == 'b')
|
|
|
|
return LexNumber();
|
|
|
|
// Fallthrough
|
|
|
|
case '2': case '3': case '4': case '5':
|
|
|
|
case '6': case '7': case '8': case '9':
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
|
|
|
if (NextChar == 'x')
|
|
|
|
return LexNumber();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isalpha(NextChar) || NextChar == '_')
|
|
|
|
return LexIdentifier();
|
|
|
|
|
2007-11-18 02:57:27 +00:00
|
|
|
return LexNumber();
|
2011-10-19 13:03:39 +00:00
|
|
|
}
|
2007-11-18 02:57:27 +00:00
|
|
|
case '"': return LexString();
|
|
|
|
case '$': return LexVarName();
|
|
|
|
case '[': return LexBracket();
|
|
|
|
case '!': return LexExclaim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LexString - Lex "[^"]*"
|
2007-11-22 20:49:04 +00:00
|
|
|
tgtok::TokKind TGLexer::LexString() {
|
2007-11-18 02:57:27 +00:00
|
|
|
const char *StrStart = CurPtr;
|
|
|
|
|
2009-03-13 21:03:27 +00:00
|
|
|
CurStrVal = "";
|
|
|
|
|
2007-11-18 02:57:27 +00:00
|
|
|
while (*CurPtr != '"') {
|
|
|
|
// If we hit the end of the buffer, report an error.
|
2007-11-19 07:38:58 +00:00
|
|
|
if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
|
|
|
|
return ReturnError(StrStart, "End of file in string literal");
|
|
|
|
|
|
|
|
if (*CurPtr == '\n' || *CurPtr == '\r')
|
|
|
|
return ReturnError(StrStart, "End of line in string literal");
|
2007-11-18 02:57:27 +00:00
|
|
|
|
2009-03-13 21:03:27 +00:00
|
|
|
if (*CurPtr != '\\') {
|
|
|
|
CurStrVal += *CurPtr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-11-18 02:57:27 +00:00
|
|
|
++CurPtr;
|
2009-03-13 21:03:27 +00:00
|
|
|
|
|
|
|
switch (*CurPtr) {
|
|
|
|
case '\\': case '\'': case '"':
|
|
|
|
// These turn into their literal character.
|
|
|
|
CurStrVal += *CurPtr++;
|
|
|
|
break;
|
2009-03-13 21:23:43 +00:00
|
|
|
case 't':
|
2009-03-13 21:33:17 +00:00
|
|
|
CurStrVal += '\t';
|
2009-03-13 21:23:43 +00:00
|
|
|
++CurPtr;
|
|
|
|
break;
|
|
|
|
case 'n':
|
2009-03-13 21:33:17 +00:00
|
|
|
CurStrVal += '\n';
|
2009-03-13 21:23:43 +00:00
|
|
|
++CurPtr;
|
|
|
|
break;
|
|
|
|
|
2009-03-13 21:03:27 +00:00
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
|
|
|
|
|
|
|
|
// If we hit the end of the buffer, report an error.
|
|
|
|
case '\0':
|
|
|
|
if (CurPtr == CurBuf->getBufferEnd())
|
|
|
|
return ReturnError(StrStart, "End of file in string literal");
|
|
|
|
// FALL THROUGH
|
|
|
|
default:
|
|
|
|
return ReturnError(CurPtr, "invalid escape in string literal");
|
|
|
|
}
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++CurPtr;
|
2007-11-22 20:49:04 +00:00
|
|
|
return tgtok::StrVal;
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
|
2007-11-22 20:49:04 +00:00
|
|
|
tgtok::TokKind TGLexer::LexVarName() {
|
2007-11-18 02:57:27 +00:00
|
|
|
if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
|
2007-11-22 20:49:04 +00:00
|
|
|
return ReturnError(TokStart, "Invalid variable name");
|
2007-11-18 02:57:27 +00:00
|
|
|
|
|
|
|
// Otherwise, we're ok, consume the rest of the characters.
|
|
|
|
const char *VarNameStart = CurPtr++;
|
|
|
|
|
|
|
|
while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
|
|
|
|
++CurPtr;
|
|
|
|
|
2007-11-22 20:49:04 +00:00
|
|
|
CurStrVal.assign(VarNameStart, CurPtr);
|
|
|
|
return tgtok::VarName;
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-22 20:49:04 +00:00
|
|
|
tgtok::TokKind TGLexer::LexIdentifier() {
|
2010-10-05 22:59:29 +00:00
|
|
|
// The first letter is [a-zA-Z_#].
|
2007-11-22 20:49:04 +00:00
|
|
|
const char *IdentStart = TokStart;
|
2011-10-06 18:23:56 +00:00
|
|
|
|
2010-10-05 22:59:29 +00:00
|
|
|
// Match the rest of the identifier regex: [0-9a-zA-Z_#]*
|
2011-10-19 13:04:43 +00:00
|
|
|
while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
|
2010-10-05 22:59:29 +00:00
|
|
|
++CurPtr;
|
2011-10-06 18:23:56 +00:00
|
|
|
|
2007-11-18 02:57:27 +00:00
|
|
|
// Check to see if this identifier is a keyword.
|
2011-10-06 18:23:56 +00:00
|
|
|
StringRef Str(IdentStart, CurPtr-IdentStart);
|
|
|
|
|
|
|
|
if (Str == "include") {
|
2007-11-22 20:49:04 +00:00
|
|
|
if (LexInclude()) return tgtok::Error;
|
|
|
|
return Lex();
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
2011-10-06 18:23:56 +00:00
|
|
|
|
2011-10-06 18:53:43 +00:00
|
|
|
tgtok::TokKind Kind = StringSwitch<tgtok::TokKind>(Str)
|
|
|
|
.Case("int", tgtok::Int)
|
|
|
|
.Case("bit", tgtok::Bit)
|
|
|
|
.Case("bits", tgtok::Bits)
|
|
|
|
.Case("string", tgtok::String)
|
|
|
|
.Case("list", tgtok::List)
|
|
|
|
.Case("code", tgtok::Code)
|
|
|
|
.Case("dag", tgtok::Dag)
|
|
|
|
.Case("class", tgtok::Class)
|
|
|
|
.Case("def", tgtok::Def)
|
2012-02-22 16:09:41 +00:00
|
|
|
.Case("foreach", tgtok::Foreach)
|
2011-10-06 18:53:43 +00:00
|
|
|
.Case("defm", tgtok::Defm)
|
|
|
|
.Case("multiclass", tgtok::MultiClass)
|
|
|
|
.Case("field", tgtok::Field)
|
|
|
|
.Case("let", tgtok::Let)
|
|
|
|
.Case("in", tgtok::In)
|
|
|
|
.Default(tgtok::Id);
|
|
|
|
|
|
|
|
if (Kind == tgtok::Id)
|
|
|
|
CurStrVal.assign(Str.begin(), Str.end());
|
|
|
|
return Kind;
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexInclude - We just read the "include" token. Get the string token that
|
|
|
|
/// comes next and enter the include.
|
|
|
|
bool TGLexer::LexInclude() {
|
|
|
|
// The token after the include must be a string.
|
2007-11-22 20:49:04 +00:00
|
|
|
tgtok::TokKind Tok = LexToken();
|
|
|
|
if (Tok == tgtok::Error) return true;
|
|
|
|
if (Tok != tgtok::StrVal) {
|
|
|
|
PrintError(getLoc(), "Expected filename after include");
|
2007-11-18 02:57:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the string.
|
2007-11-22 20:49:04 +00:00
|
|
|
std::string Filename = CurStrVal;
|
2011-06-01 13:10:15 +00:00
|
|
|
std::string IncludedFile;
|
2007-11-18 02:57:27 +00:00
|
|
|
|
2009-06-21 05:06:04 +00:00
|
|
|
|
2011-06-01 13:10:15 +00:00
|
|
|
CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
|
|
|
|
IncludedFile);
|
2009-06-21 05:33:06 +00:00
|
|
|
if (CurBuffer == -1) {
|
2007-11-22 20:49:04 +00:00
|
|
|
PrintError(getLoc(), "Could not find include file '" + Filename + "'");
|
2007-11-18 02:57:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-07 04:30:39 +00:00
|
|
|
DependenciesMapTy::const_iterator Found = Dependencies.find(IncludedFile);
|
|
|
|
if (Found != Dependencies.end()) {
|
|
|
|
PrintError(getLoc(),
|
|
|
|
"File '" + IncludedFile + "' has already been included.");
|
|
|
|
SrcMgr.PrintMessage(Found->second, SourceMgr::DK_Note,
|
|
|
|
"previously included here");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Dependencies.insert(std::make_pair(IncludedFile, getLoc()));
|
2007-11-18 02:57:27 +00:00
|
|
|
// Save the line number and lex buffer of the includer.
|
2009-06-21 05:06:04 +00:00
|
|
|
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
|
2007-11-18 02:57:27 +00:00
|
|
|
CurPtr = CurBuf->getBufferStart();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TGLexer::SkipBCPLComment() {
|
|
|
|
++CurPtr; // skip the second slash.
|
|
|
|
while (1) {
|
|
|
|
switch (*CurPtr) {
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
return; // Newline is end of comment.
|
|
|
|
case 0:
|
|
|
|
// If this is the end of the buffer, end the comment.
|
|
|
|
if (CurPtr == CurBuf->getBufferEnd())
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Otherwise, skip the character.
|
|
|
|
++CurPtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SkipCComment - This skips C-style /**/ comments. The only difference from C
|
|
|
|
/// is that we allow nesting.
|
|
|
|
bool TGLexer::SkipCComment() {
|
|
|
|
++CurPtr; // skip the star.
|
|
|
|
unsigned CommentDepth = 1;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int CurChar = getNextChar();
|
|
|
|
switch (CurChar) {
|
|
|
|
case EOF:
|
2007-11-22 20:49:04 +00:00
|
|
|
PrintError(TokStart, "Unterminated comment!");
|
2007-11-18 02:57:27 +00:00
|
|
|
return true;
|
|
|
|
case '*':
|
|
|
|
// End of the comment?
|
|
|
|
if (CurPtr[0] != '/') break;
|
|
|
|
|
|
|
|
++CurPtr; // End the */.
|
|
|
|
if (--CommentDepth == 0)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
// Start of a nested comment?
|
|
|
|
if (CurPtr[0] != '*') break;
|
|
|
|
++CurPtr;
|
|
|
|
++CommentDepth;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LexNumber - Lex:
|
|
|
|
/// [-+]?[0-9]+
|
|
|
|
/// 0x[0-9a-fA-F]+
|
|
|
|
/// 0b[01]+
|
2007-11-22 20:49:04 +00:00
|
|
|
tgtok::TokKind TGLexer::LexNumber() {
|
2007-11-18 02:57:27 +00:00
|
|
|
if (CurPtr[-1] == '0') {
|
|
|
|
if (CurPtr[0] == 'x') {
|
|
|
|
++CurPtr;
|
2007-11-22 20:49:04 +00:00
|
|
|
const char *NumStart = CurPtr;
|
2007-11-18 02:57:27 +00:00
|
|
|
while (isxdigit(CurPtr[0]))
|
|
|
|
++CurPtr;
|
|
|
|
|
2007-11-19 07:38:58 +00:00
|
|
|
// Requires at least one hex digit.
|
|
|
|
if (CurPtr == NumStart)
|
2009-06-21 19:22:49 +00:00
|
|
|
return ReturnError(TokStart, "Invalid hexadecimal number");
|
2007-11-19 07:38:58 +00:00
|
|
|
|
2008-10-17 01:33:43 +00:00
|
|
|
errno = 0;
|
2007-11-22 20:49:04 +00:00
|
|
|
CurIntVal = strtoll(NumStart, 0, 16);
|
2008-10-17 01:33:43 +00:00
|
|
|
if (errno == EINVAL)
|
2009-06-21 19:22:49 +00:00
|
|
|
return ReturnError(TokStart, "Invalid hexadecimal number");
|
2008-10-17 01:33:43 +00:00
|
|
|
if (errno == ERANGE) {
|
|
|
|
errno = 0;
|
|
|
|
CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
|
|
|
|
if (errno == EINVAL)
|
2009-06-21 19:22:49 +00:00
|
|
|
return ReturnError(TokStart, "Invalid hexadecimal number");
|
2008-10-17 01:33:43 +00:00
|
|
|
if (errno == ERANGE)
|
2009-06-21 19:22:49 +00:00
|
|
|
return ReturnError(TokStart, "Hexadecimal number out of range");
|
2008-10-17 01:33:43 +00:00
|
|
|
}
|
2007-11-22 20:49:04 +00:00
|
|
|
return tgtok::IntVal;
|
2007-11-18 02:57:27 +00:00
|
|
|
} else if (CurPtr[0] == 'b') {
|
|
|
|
++CurPtr;
|
2007-11-22 20:49:04 +00:00
|
|
|
const char *NumStart = CurPtr;
|
2007-11-18 02:57:27 +00:00
|
|
|
while (CurPtr[0] == '0' || CurPtr[0] == '1')
|
|
|
|
++CurPtr;
|
2007-11-19 07:38:58 +00:00
|
|
|
|
|
|
|
// Requires at least one binary digit.
|
|
|
|
if (CurPtr == NumStart)
|
|
|
|
return ReturnError(CurPtr-2, "Invalid binary number");
|
2007-11-22 20:49:04 +00:00
|
|
|
CurIntVal = strtoll(NumStart, 0, 2);
|
|
|
|
return tgtok::IntVal;
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a sign without a digit.
|
2007-11-22 20:49:04 +00:00
|
|
|
if (!isdigit(CurPtr[0])) {
|
|
|
|
if (CurPtr[-1] == '-')
|
|
|
|
return tgtok::minus;
|
|
|
|
else if (CurPtr[-1] == '+')
|
|
|
|
return tgtok::plus;
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (isdigit(CurPtr[0]))
|
|
|
|
++CurPtr;
|
2007-11-22 20:49:04 +00:00
|
|
|
CurIntVal = strtoll(TokStart, 0, 10);
|
|
|
|
return tgtok::IntVal;
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexBracket - We just read '['. If this is a code block, return it,
|
|
|
|
/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
|
2007-11-22 20:49:04 +00:00
|
|
|
tgtok::TokKind TGLexer::LexBracket() {
|
2007-11-18 02:57:27 +00:00
|
|
|
if (CurPtr[0] != '{')
|
2007-11-22 20:49:04 +00:00
|
|
|
return tgtok::l_square;
|
2007-11-18 02:57:27 +00:00
|
|
|
++CurPtr;
|
|
|
|
const char *CodeStart = CurPtr;
|
|
|
|
while (1) {
|
|
|
|
int Char = getNextChar();
|
|
|
|
if (Char == EOF) break;
|
|
|
|
|
|
|
|
if (Char != '}') continue;
|
|
|
|
|
|
|
|
Char = getNextChar();
|
|
|
|
if (Char == EOF) break;
|
|
|
|
if (Char == ']') {
|
2007-11-22 20:49:04 +00:00
|
|
|
CurStrVal.assign(CodeStart, CurPtr-2);
|
|
|
|
return tgtok::CodeFragment;
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-19 07:38:58 +00:00
|
|
|
return ReturnError(CodeStart-2, "Unterminated Code Block");
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
|
2007-11-22 20:49:04 +00:00
|
|
|
tgtok::TokKind TGLexer::LexExclaim() {
|
2007-11-18 02:57:27 +00:00
|
|
|
if (!isalpha(*CurPtr))
|
2010-12-08 13:03:15 +00:00
|
|
|
return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
|
2007-11-18 02:57:27 +00:00
|
|
|
|
|
|
|
const char *Start = CurPtr++;
|
|
|
|
while (isalpha(*CurPtr))
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
// Check to see which operator this is.
|
2010-12-08 20:02:49 +00:00
|
|
|
tgtok::TokKind Kind =
|
|
|
|
StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
|
|
|
|
.Case("eq", tgtok::XEq)
|
|
|
|
.Case("if", tgtok::XIf)
|
2011-01-07 17:05:37 +00:00
|
|
|
.Case("head", tgtok::XHead)
|
|
|
|
.Case("tail", tgtok::XTail)
|
2010-12-08 20:02:49 +00:00
|
|
|
.Case("con", tgtok::XConcat)
|
2013-01-25 14:49:08 +00:00
|
|
|
.Case("add", tgtok::XADD)
|
2010-12-08 20:02:49 +00:00
|
|
|
.Case("shl", tgtok::XSHL)
|
|
|
|
.Case("sra", tgtok::XSRA)
|
|
|
|
.Case("srl", tgtok::XSRL)
|
|
|
|
.Case("cast", tgtok::XCast)
|
2011-01-07 17:05:37 +00:00
|
|
|
.Case("empty", tgtok::XEmpty)
|
2010-12-08 20:02:49 +00:00
|
|
|
.Case("subst", tgtok::XSubst)
|
|
|
|
.Case("foreach", tgtok::XForEach)
|
|
|
|
.Case("strconcat", tgtok::XStrConcat)
|
|
|
|
.Default(tgtok::Error);
|
|
|
|
|
|
|
|
return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");
|
2007-11-18 02:57:27 +00:00
|
|
|
}
|
|
|
|
|