2007-11-18 03:57:27 +01:00
|
|
|
//===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:37:13 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-11-18 03:57:27 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implement the Lexer for TableGen.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-11-18 06:25:45 +01:00
|
|
|
#include "TGLexer.h"
|
2009-06-21 05:36:54 +02:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2007-11-18 03:57:27 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2007-11-21 20:36:25 +01:00
|
|
|
#include "llvm/Config/config.h"
|
2007-11-18 03:57:27 +01:00
|
|
|
#include <cctype>
|
2008-10-08 09:23:46 +02:00
|
|
|
#include <cstdio>
|
2008-02-20 12:08:44 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2008-10-17 03:33:43 +02:00
|
|
|
#include <cerrno>
|
2007-11-18 03:57:27 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-06-21 05:41:50 +02:00
|
|
|
TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) {
|
2009-03-13 08:05:43 +01:00
|
|
|
CurBuffer = 0;
|
|
|
|
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
|
2007-11-18 03:57:27 +01:00
|
|
|
CurPtr = CurBuf->getBufferStart();
|
2007-11-19 08:43:52 +01:00
|
|
|
TokStart = 0;
|
2007-11-18 03:57:27 +01:00
|
|
|
}
|
|
|
|
|
2009-06-21 05:39:35 +02:00
|
|
|
SMLoc TGLexer::getLoc() const {
|
|
|
|
return SMLoc::getFromPointer(TokStart);
|
2009-03-13 17:01:53 +01:00
|
|
|
}
|
|
|
|
|
2007-11-18 03:57:27 +01:00
|
|
|
|
2007-11-19 08:38:58 +01:00
|
|
|
/// ReturnError - Set the error to the specified string at the specified
|
2007-11-22 21:49:04 +01:00
|
|
|
/// location. This is defined to always return tgtok::Error.
|
|
|
|
tgtok::TokKind TGLexer::ReturnError(const char *Loc, const std::string &Msg) {
|
2007-11-19 08:38:58 +01:00
|
|
|
PrintError(Loc, Msg);
|
2007-11-22 21:49:04 +01:00
|
|
|
return tgtok::Error;
|
2007-11-18 03:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-13 17:01:53 +01:00
|
|
|
void TGLexer::PrintError(const char *Loc, const std::string &Msg) const {
|
2009-06-30 02:49:23 +02:00
|
|
|
SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg, "error");
|
2009-03-13 17:01:53 +01:00
|
|
|
}
|
|
|
|
|
2009-06-21 05:39:35 +02:00
|
|
|
void TGLexer::PrintError(SMLoc Loc, const std::string &Msg) const {
|
2009-06-30 02:49:23 +02:00
|
|
|
SrcMgr.PrintMessage(Loc, Msg, "error");
|
2007-11-19 08:38:58 +01:00
|
|
|
}
|
|
|
|
|
2009-03-13 17:01:53 +01:00
|
|
|
|
2007-11-18 03:57:27 +01:00
|
|
|
int TGLexer::getNextChar() {
|
|
|
|
char CurChar = *CurPtr++;
|
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
2007-11-18 06:48:46 +01:00
|
|
|
return (unsigned char)CurChar;
|
2009-03-13 08:05:43 +01:00
|
|
|
case 0: {
|
2007-11-18 03:57:27 +01: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 05:39:35 +02:00
|
|
|
SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
|
|
|
|
if (ParentIncludeLoc != SMLoc()) {
|
2009-03-13 08:05:43 +01:00
|
|
|
CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
|
|
|
|
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
|
2009-03-13 17:01:53 +01:00
|
|
|
CurPtr = ParentIncludeLoc.getPointer();
|
2007-11-18 03:57:27 +01:00
|
|
|
return getNextChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, return end of file.
|
|
|
|
--CurPtr; // Another call to lex will return EOF again.
|
|
|
|
return EOF;
|
2009-03-13 08:05:43 +01:00
|
|
|
}
|
2007-11-18 03:57:27 +01: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 06:48:46 +01:00
|
|
|
++CurPtr; // Eat the two char newline sequence.
|
2007-11-18 03:57:27 +01:00
|
|
|
return '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-22 21:49:04 +01:00
|
|
|
tgtok::TokKind TGLexer::LexToken() {
|
2007-11-19 08:43:52 +01:00
|
|
|
TokStart = CurPtr;
|
2007-11-18 03:57:27 +01:00
|
|
|
// This always consumes at least one character.
|
|
|
|
int CurChar = getNextChar();
|
|
|
|
|
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
|
|
|
// Handle letters: [a-zA-Z_]
|
2009-05-05 18:28:25 +02:00
|
|
|
if (isalpha(CurChar) || CurChar == '_' || CurChar == '#')
|
2007-11-18 03:57:27 +01:00
|
|
|
return LexIdentifier();
|
|
|
|
|
2007-11-22 21:49:04 +01: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;
|
|
|
|
|
2007-11-18 03:57:27 +01: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 21:49:04 +01:00
|
|
|
return tgtok::Error;
|
|
|
|
} else // Otherwise, this is an error.
|
|
|
|
return ReturnError(TokStart, "Unexpected character");
|
2007-11-18 03:57:27 +01:00
|
|
|
return LexToken();
|
|
|
|
case '-': case '+':
|
|
|
|
case '0': case '1': case '2': case '3': case '4': case '5': case '6':
|
|
|
|
case '7': case '8': case '9':
|
|
|
|
return LexNumber();
|
|
|
|
case '"': return LexString();
|
|
|
|
case '$': return LexVarName();
|
|
|
|
case '[': return LexBracket();
|
|
|
|
case '!': return LexExclaim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LexString - Lex "[^"]*"
|
2007-11-22 21:49:04 +01:00
|
|
|
tgtok::TokKind TGLexer::LexString() {
|
2007-11-18 03:57:27 +01:00
|
|
|
const char *StrStart = CurPtr;
|
|
|
|
|
2009-03-13 22:03:27 +01:00
|
|
|
CurStrVal = "";
|
|
|
|
|
2007-11-18 03:57:27 +01:00
|
|
|
while (*CurPtr != '"') {
|
|
|
|
// If we hit the end of the buffer, report an error.
|
2007-11-19 08:38:58 +01: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 03:57:27 +01:00
|
|
|
|
2009-03-13 22:03:27 +01:00
|
|
|
if (*CurPtr != '\\') {
|
|
|
|
CurStrVal += *CurPtr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-11-18 03:57:27 +01:00
|
|
|
++CurPtr;
|
2009-03-13 22:03:27 +01:00
|
|
|
|
|
|
|
switch (*CurPtr) {
|
|
|
|
case '\\': case '\'': case '"':
|
|
|
|
// These turn into their literal character.
|
|
|
|
CurStrVal += *CurPtr++;
|
|
|
|
break;
|
2009-03-13 22:23:43 +01:00
|
|
|
case 't':
|
2009-03-13 22:33:17 +01:00
|
|
|
CurStrVal += '\t';
|
2009-03-13 22:23:43 +01:00
|
|
|
++CurPtr;
|
|
|
|
break;
|
|
|
|
case 'n':
|
2009-03-13 22:33:17 +01:00
|
|
|
CurStrVal += '\n';
|
2009-03-13 22:23:43 +01:00
|
|
|
++CurPtr;
|
|
|
|
break;
|
|
|
|
|
2009-03-13 22:03:27 +01: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 03:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
++CurPtr;
|
2007-11-22 21:49:04 +01:00
|
|
|
return tgtok::StrVal;
|
2007-11-18 03:57:27 +01:00
|
|
|
}
|
|
|
|
|
2007-11-22 21:49:04 +01:00
|
|
|
tgtok::TokKind TGLexer::LexVarName() {
|
2007-11-18 03:57:27 +01:00
|
|
|
if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
|
2007-11-22 21:49:04 +01:00
|
|
|
return ReturnError(TokStart, "Invalid variable name");
|
2007-11-18 03:57:27 +01: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 21:49:04 +01:00
|
|
|
CurStrVal.assign(VarNameStart, CurPtr);
|
|
|
|
return tgtok::VarName;
|
2007-11-18 03:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-22 21:49:04 +01:00
|
|
|
tgtok::TokKind TGLexer::LexIdentifier() {
|
2007-11-18 03:57:27 +01:00
|
|
|
// The first letter is [a-zA-Z_].
|
2007-11-22 21:49:04 +01:00
|
|
|
const char *IdentStart = TokStart;
|
2007-11-18 03:57:27 +01:00
|
|
|
|
|
|
|
// Match the rest of the identifier regex: [0-9a-zA-Z_]*
|
2009-05-05 18:28:25 +02:00
|
|
|
while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_'
|
|
|
|
|| *CurPtr == '#') {
|
|
|
|
// If this contains a '#', make sure it's value
|
|
|
|
if (*CurPtr == '#') {
|
|
|
|
if (strncmp(CurPtr, "#NAME#", 6) != 0) {
|
|
|
|
return tgtok::Error;
|
|
|
|
}
|
|
|
|
CurPtr += 6;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++CurPtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-18 03:57:27 +01:00
|
|
|
|
|
|
|
// Check to see if this identifier is a keyword.
|
|
|
|
unsigned Len = CurPtr-IdentStart;
|
|
|
|
|
2007-11-22 21:49:04 +01:00
|
|
|
if (Len == 3 && !memcmp(IdentStart, "int", 3)) return tgtok::Int;
|
|
|
|
if (Len == 3 && !memcmp(IdentStart, "bit", 3)) return tgtok::Bit;
|
|
|
|
if (Len == 4 && !memcmp(IdentStart, "bits", 4)) return tgtok::Bits;
|
|
|
|
if (Len == 6 && !memcmp(IdentStart, "string", 6)) return tgtok::String;
|
|
|
|
if (Len == 4 && !memcmp(IdentStart, "list", 4)) return tgtok::List;
|
|
|
|
if (Len == 4 && !memcmp(IdentStart, "code", 4)) return tgtok::Code;
|
|
|
|
if (Len == 3 && !memcmp(IdentStart, "dag", 3)) return tgtok::Dag;
|
2007-11-18 03:57:27 +01:00
|
|
|
|
2007-11-22 21:49:04 +01:00
|
|
|
if (Len == 5 && !memcmp(IdentStart, "class", 5)) return tgtok::Class;
|
|
|
|
if (Len == 3 && !memcmp(IdentStart, "def", 3)) return tgtok::Def;
|
|
|
|
if (Len == 4 && !memcmp(IdentStart, "defm", 4)) return tgtok::Defm;
|
|
|
|
if (Len == 10 && !memcmp(IdentStart, "multiclass", 10))
|
|
|
|
return tgtok::MultiClass;
|
|
|
|
if (Len == 5 && !memcmp(IdentStart, "field", 5)) return tgtok::Field;
|
|
|
|
if (Len == 3 && !memcmp(IdentStart, "let", 3)) return tgtok::Let;
|
|
|
|
if (Len == 2 && !memcmp(IdentStart, "in", 2)) return tgtok::In;
|
2007-11-18 03:57:27 +01:00
|
|
|
|
|
|
|
if (Len == 7 && !memcmp(IdentStart, "include", 7)) {
|
2007-11-22 21:49:04 +01:00
|
|
|
if (LexInclude()) return tgtok::Error;
|
|
|
|
return Lex();
|
2007-11-18 03:57:27 +01:00
|
|
|
}
|
|
|
|
|
2007-11-22 21:49:04 +01:00
|
|
|
CurStrVal.assign(IdentStart, CurPtr);
|
|
|
|
return tgtok::Id;
|
2007-11-18 03:57:27 +01: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 21:49:04 +01:00
|
|
|
tgtok::TokKind Tok = LexToken();
|
|
|
|
if (Tok == tgtok::Error) return true;
|
|
|
|
if (Tok != tgtok::StrVal) {
|
|
|
|
PrintError(getLoc(), "Expected filename after include");
|
2007-11-18 03:57:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the string.
|
2007-11-22 21:49:04 +01:00
|
|
|
std::string Filename = CurStrVal;
|
2007-11-18 03:57:27 +01:00
|
|
|
|
2009-06-21 07:06:04 +02:00
|
|
|
|
|
|
|
CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr));
|
2009-06-21 07:33:06 +02:00
|
|
|
if (CurBuffer == -1) {
|
2007-11-22 21:49:04 +01:00
|
|
|
PrintError(getLoc(), "Could not find include file '" + Filename + "'");
|
2007-11-18 03:57:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the line number and lex buffer of the includer.
|
2009-06-21 07:06:04 +02:00
|
|
|
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
|
2007-11-18 03:57:27 +01: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 21:49:04 +01:00
|
|
|
PrintError(TokStart, "Unterminated comment!");
|
2007-11-18 03:57:27 +01: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 21:49:04 +01:00
|
|
|
tgtok::TokKind TGLexer::LexNumber() {
|
2007-11-18 03:57:27 +01:00
|
|
|
if (CurPtr[-1] == '0') {
|
|
|
|
if (CurPtr[0] == 'x') {
|
|
|
|
++CurPtr;
|
2007-11-22 21:49:04 +01:00
|
|
|
const char *NumStart = CurPtr;
|
2007-11-18 03:57:27 +01:00
|
|
|
while (isxdigit(CurPtr[0]))
|
|
|
|
++CurPtr;
|
|
|
|
|
2007-11-19 08:38:58 +01:00
|
|
|
// Requires at least one hex digit.
|
|
|
|
if (CurPtr == NumStart)
|
2009-06-21 21:22:49 +02:00
|
|
|
return ReturnError(TokStart, "Invalid hexadecimal number");
|
2007-11-19 08:38:58 +01:00
|
|
|
|
2008-10-17 03:33:43 +02:00
|
|
|
errno = 0;
|
2007-11-22 21:49:04 +01:00
|
|
|
CurIntVal = strtoll(NumStart, 0, 16);
|
2008-10-17 03:33:43 +02:00
|
|
|
if (errno == EINVAL)
|
2009-06-21 21:22:49 +02:00
|
|
|
return ReturnError(TokStart, "Invalid hexadecimal number");
|
2008-10-17 03:33:43 +02:00
|
|
|
if (errno == ERANGE) {
|
|
|
|
errno = 0;
|
|
|
|
CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
|
|
|
|
if (errno == EINVAL)
|
2009-06-21 21:22:49 +02:00
|
|
|
return ReturnError(TokStart, "Invalid hexadecimal number");
|
2008-10-17 03:33:43 +02:00
|
|
|
if (errno == ERANGE)
|
2009-06-21 21:22:49 +02:00
|
|
|
return ReturnError(TokStart, "Hexadecimal number out of range");
|
2008-10-17 03:33:43 +02:00
|
|
|
}
|
2007-11-22 21:49:04 +01:00
|
|
|
return tgtok::IntVal;
|
2007-11-18 03:57:27 +01:00
|
|
|
} else if (CurPtr[0] == 'b') {
|
|
|
|
++CurPtr;
|
2007-11-22 21:49:04 +01:00
|
|
|
const char *NumStart = CurPtr;
|
2007-11-18 03:57:27 +01:00
|
|
|
while (CurPtr[0] == '0' || CurPtr[0] == '1')
|
|
|
|
++CurPtr;
|
2007-11-19 08:38:58 +01:00
|
|
|
|
|
|
|
// Requires at least one binary digit.
|
|
|
|
if (CurPtr == NumStart)
|
|
|
|
return ReturnError(CurPtr-2, "Invalid binary number");
|
2007-11-22 21:49:04 +01:00
|
|
|
CurIntVal = strtoll(NumStart, 0, 2);
|
|
|
|
return tgtok::IntVal;
|
2007-11-18 03:57:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a sign without a digit.
|
2007-11-22 21:49:04 +01:00
|
|
|
if (!isdigit(CurPtr[0])) {
|
|
|
|
if (CurPtr[-1] == '-')
|
|
|
|
return tgtok::minus;
|
|
|
|
else if (CurPtr[-1] == '+')
|
|
|
|
return tgtok::plus;
|
2007-11-18 03:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (isdigit(CurPtr[0]))
|
|
|
|
++CurPtr;
|
2007-11-22 21:49:04 +01:00
|
|
|
CurIntVal = strtoll(TokStart, 0, 10);
|
|
|
|
return tgtok::IntVal;
|
2007-11-18 03:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexBracket - We just read '['. If this is a code block, return it,
|
|
|
|
/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
|
2007-11-22 21:49:04 +01:00
|
|
|
tgtok::TokKind TGLexer::LexBracket() {
|
2007-11-18 03:57:27 +01:00
|
|
|
if (CurPtr[0] != '{')
|
2007-11-22 21:49:04 +01:00
|
|
|
return tgtok::l_square;
|
2007-11-18 03:57:27 +01: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 21:49:04 +01:00
|
|
|
CurStrVal.assign(CodeStart, CurPtr-2);
|
|
|
|
return tgtok::CodeFragment;
|
2007-11-18 03:57:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-19 08:38:58 +01:00
|
|
|
return ReturnError(CodeStart-2, "Unterminated Code Block");
|
2007-11-18 03:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
|
2007-11-22 21:49:04 +01:00
|
|
|
tgtok::TokKind TGLexer::LexExclaim() {
|
2007-11-18 03:57:27 +01:00
|
|
|
if (!isalpha(*CurPtr))
|
2007-11-22 21:49:04 +01:00
|
|
|
return ReturnError(CurPtr-1, "Invalid \"!operator\"");
|
2007-11-18 03:57:27 +01:00
|
|
|
|
|
|
|
const char *Start = CurPtr++;
|
|
|
|
while (isalpha(*CurPtr))
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
// Check to see which operator this is.
|
|
|
|
unsigned Len = CurPtr-Start;
|
|
|
|
|
2009-04-22 22:18:10 +02:00
|
|
|
if (Len == 3 && !memcmp(Start, "con", 3)) return tgtok::XConcat;
|
|
|
|
if (Len == 3 && !memcmp(Start, "sra", 3)) return tgtok::XSRA;
|
|
|
|
if (Len == 3 && !memcmp(Start, "srl", 3)) return tgtok::XSRL;
|
|
|
|
if (Len == 3 && !memcmp(Start, "shl", 3)) return tgtok::XSHL;
|
2010-01-05 20:11:42 +01:00
|
|
|
if (Len == 2 && !memcmp(Start, "eq", 2)) return tgtok::XEq;
|
2009-04-22 22:18:10 +02:00
|
|
|
if (Len == 9 && !memcmp(Start, "strconcat", 9)) return tgtok::XStrConcat;
|
|
|
|
if (Len == 10 && !memcmp(Start, "nameconcat", 10)) return tgtok::XNameConcat;
|
2009-05-14 23:54:42 +02:00
|
|
|
if (Len == 5 && !memcmp(Start, "subst", 5)) return tgtok::XSubst;
|
2009-05-15 00:23:47 +02:00
|
|
|
if (Len == 7 && !memcmp(Start, "foreach", 7)) return tgtok::XForEach;
|
2009-05-14 23:22:49 +02:00
|
|
|
if (Len == 4 && !memcmp(Start, "cast", 4)) return tgtok::XCast;
|
2009-05-15 00:38:31 +02:00
|
|
|
if (Len == 3 && !memcmp(Start, "car", 3)) return tgtok::XCar;
|
|
|
|
if (Len == 3 && !memcmp(Start, "cdr", 3)) return tgtok::XCdr;
|
|
|
|
if (Len == 4 && !memcmp(Start, "null", 4)) return tgtok::XNull;
|
2009-05-15 01:26:46 +02:00
|
|
|
if (Len == 2 && !memcmp(Start, "if", 2)) return tgtok::XIf;
|
2009-05-14 22:54:48 +02:00
|
|
|
|
2007-11-19 08:38:58 +01:00
|
|
|
return ReturnError(Start-1, "Unknown operator");
|
2007-11-18 03:57:27 +01:00
|
|
|
}
|
|
|
|
|