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
|
#include "stdafx.h"
#include "Tokenizer.h"
#include "Compiler/Exception.h"
#include "Core/StrBuf.h"
#include "Decl.h"
namespace storm {
namespace syntax {
Token::Token(Str *str, Nat len, const SrcPos &pos) : pos(pos), str(str), len(len) {}
bool Token::empty() const {
return len == 0;
}
const wchar *Token::start() const {
return str->c_str() + pos.start;
}
bool Token::operator ==(const wchar *t) const {
const wchar *s = start();
for (nat i = 0; i < len; i++) {
if (s[i] == 0)
return false;
if (s[i] != t[i])
return false;
}
return true;
}
bool Token::isStrLiteral() const {
const wchar *s = start();
if (*s != '"')
return false;
return s[len-1] == '"';
}
Str *Token::strLiteral() const {
if (!isStrLiteral())
return new (str) Str();
Str *r = new (str) Str(start() + 1, start() + len - 1);
// This is the same function as is used by the "real" grammar.
return unescapeStr(r);
}
Str *Token::toS() const {
const wchar *start = this->start();
const wchar *end = start + len;
return new (str) Str(start, end);
}
wostream &operator <<(wostream &to, const Token &t) {
return to << t.toS()->c_str();
}
static const wchar *operators = S("-?*=><.+");
static const wchar *specials = S("(){}[],;:@#");
static bool isOperator(wchar c) {
for (const wchar *p = operators; *p; p++)
if (c == *p)
return true;
return false;
}
static bool isSpecial(wchar c) {
for (const wchar *p = specials; *p; p++)
if (c == *p)
return true;
return false;
}
static bool isWhitespace(wchar c) {
switch (c) {
case ' ':
case '\n':
case '\r':
case '\t':
return true;
}
return false;
}
Tokenizer::Tokenizer(Url *path, Str *src, Nat start)
: src(src),
file(path),
pos(start),
commentStart(invalid),
lookahead(src, 0, SrcPos(path, 0, 0)) {
lookahead = findNext();
}
Token Tokenizer::next() {
Token t = peek();
skip();
return t;
}
Token Tokenizer::peek() const {
if (!more())
throw new (src) SyntaxError(position(), S("Unexpected end of file."));
return lookahead;
}
void Tokenizer::skip() {
lookahead = findNext();
}
bool Tokenizer::skipIf(const wchar *token) {
if (!more())
throw new (src) SyntaxError(position(), S("Unexpected end of file."));
if (lookahead == token) {
skip();
return true;
} else {
return false;
}
}
void Tokenizer::expect(const wchar *token) {
if (!more())
throw new (src) SyntaxError(position(), S("Unexpected end of file."));
if (lookahead != token)
throw new (src) SyntaxError(lookahead.pos,
TO_S(src, S("Expected ") << token << S(" but got ") << lookahead.toS()));
skip();
}
bool Tokenizer::more() const {
return !lookahead.empty();
}
SrcPos Tokenizer::position() const {
return lookahead.pos;
}
Nat Tokenizer::invalid = Nat(-1);
SrcPos Tokenizer::comment() const {
if (commentStart == invalid)
return SrcPos();
else
return SrcPos(file, commentStart, commentStart);
}
void Tokenizer::clearComment() {
commentStart = invalid;
}
Token Tokenizer::findNext() {
State state = sStart;
nat start = pos;
bool firstComment = true;
while (state != sDone)
processChar(start, state, firstComment);
return Token(src, pos - start, SrcPos(file, start, pos));
}
void Tokenizer::processChar(Nat &start, State &state, bool &firstComment) {
const wchar *str = src->c_str();
wchar ch = str[pos];
if (ch == 0) {
state = sDone;
return;
}
if (ch == '/' && str[pos+1] == '/') {
switch (state) {
case sStart:
state = sComment;
if (firstComment) {
commentStart = pos;
firstComment = false;
}
break;
case sString:
case sComment:
break;
default:
state = sDone;
return;
}
}
switch (state) {
case sStart:
pos++;
if (isWhitespace(ch)) {
start = pos;
// If there is a space inside the block, they are considered two blocks and we
// are only interested in the last one.
if (ch == '\n')
firstComment = true;
} else if (isSpecial(ch)) {
state = sDone;
} else if (isOperator(ch)) {
state = sOperator;
} else if (ch == '"') {
state = sString;
} else {
state = sText;
}
break;
case sText:
if (isOperator(ch) || isWhitespace(ch) || isSpecial(ch) || ch == '"') {
state = sDone;
} else {
pos++;
}
break;
case sOperator:
if (!isOperator(ch)) {
state = sDone;
} else {
pos++;
}
break;
case sString:
pos++;
if (ch == '"') {
state = sDone;
} else if (ch == '\\') {
pos++;
}
break;
case sComment:
start = ++pos;
if (ch == '\n')
state = sStart;
break;
case sDone:
if (pos > 0)
pos--;
break;
}
}
}
}
|