Some random messing about as well as some changes to make the code compile on git HEAD dmd

This commit is contained in:
Hackerpilot 2013-12-16 01:23:23 +00:00
parent 629c93eca7
commit 84005dc200
3 changed files with 161 additions and 135 deletions

View File

@ -421,7 +421,7 @@ public struct DLexer(R)
Token lexWhitespace(LR)(ref LR range) Token lexWhitespace(LR)(ref LR range)
{ {
range.mark(); range.mark();
loop: while (!range.empty) loop: do
{ {
switch (range.front) switch (range.front)
{ {
@ -456,7 +456,7 @@ public struct DLexer(R)
default: default:
break loop; break loop;
} }
} } while (!range.empty);
return Token(tok!"whitespace", cast(string) range.getMarked(), range.line, return Token(tok!"whitespace", cast(string) range.getMarked(), range.line,
range.column, range.index); range.column, range.index);
} }
@ -861,7 +861,10 @@ public struct DLexer(R)
while (true) while (true)
{ {
if (range.empty) if (range.empty)
goto error; {
writeln("Error: unterminated string literal");
return Token(tok!"");
}
else if (range.front == '`') else if (range.front == '`')
{ {
range.popFront(); range.popFront();
@ -875,12 +878,18 @@ public struct DLexer(R)
{ {
range.popFront(); range.popFront();
if (range.empty) if (range.empty)
goto error; {
writeln("Error: unterminated string literal");
return Token(tok!"");
}
range.popFront(); range.popFront();
while (true) while (true)
{ {
if (range.empty) if (range.empty)
goto error; {
writeln("Error: unterminated string literal");
return Token(tok!"");
}
else if (range.front == '"') else if (range.front == '"')
{ {
range.popFront(); range.popFront();
@ -893,9 +902,6 @@ public struct DLexer(R)
lexStringSuffix(range, type); lexStringSuffix(range, type);
return Token(type, cast(string) range.getMarked(), range.line, range.column, return Token(type, cast(string) range.getMarked(), range.line, range.column,
range.index); range.index);
error:
writeln("Error: unterminated string literal");
return Token();
} }
static void lexStringSuffix(R)(ref R range, ref IdType type) static void lexStringSuffix(R)(ref R range, ref IdType type)
@ -1232,7 +1238,7 @@ public struct DLexer(R)
range.column, range.index); range.column, range.index);
} }
static bool isSeparating(C)(C c) static bool isSeparating(C)(C c) nothrow pure
{ {
if (c <= 0x2f) return true; if (c <= 0x2f) return true;
if (c >= ':' && c <= '@') return true; if (c >= ':' && c <= '@') return true;

View File

@ -80,7 +80,7 @@ import std.string : format;
* Returns: the parsed module * Returns: the parsed module
*/ */
Module parseModule(const(Token)[] tokens, string fileName, Module parseModule(const(Token)[] tokens, string fileName,
void function(string, int, int, string) messageFunction = null) void function(string, size_t, size_t, string) messageFunction = null)
{ {
auto parser = new Parser(); auto parser = new Parser();
parser.fileName = fileName; parser.fileName = fileName;
@ -4117,7 +4117,8 @@ q{(int a, ...)
if (currentIs(tok!"=>")) if (currentIs(tok!"=>"))
{ {
goToBookmark(b); goToBookmark(b);
goto lambda; node.lambdaExpression = parseLambdaExpression();
break;
} }
else else
goToBookmark(b); goToBookmark(b);
@ -4152,7 +4153,6 @@ q{(int a, ...)
if (currentIs(tok!"=>")) if (currentIs(tok!"=>"))
{ {
goToBookmark(b); goToBookmark(b);
lambda:
node.lambdaExpression = parseLambdaExpression(); node.lambdaExpression = parseLambdaExpression();
} }
else if (currentIs(tok!"{")) else if (currentIs(tok!"{"))
@ -5549,7 +5549,10 @@ q{(int a, ...)
node.array = true; node.array = true;
advance(); advance();
if (currentIs(tok!"]")) if (currentIs(tok!"]"))
goto end; {
if (expect(tok!"]") is null) return null;
return node;
}
auto bookmark = setBookmark(); auto bookmark = setBookmark();
auto type = parseType(); auto type = parseType();
if (type !is null && currentIs(tok!"]")) if (type !is null && currentIs(tok!"]"))
@ -5570,7 +5573,6 @@ q{(int a, ...)
if (node.high is null) return null; if (node.high is null) return null;
} }
} }
end:
if (expect(tok!"]") is null) return null; if (expect(tok!"]") is null) return null;
return node; return node;
case tok!"delegate": case tok!"delegate":
@ -6023,7 +6025,7 @@ q{doStuff(5)}c;
* The parameters are the file name, line number, column number, * The parameters are the file name, line number, column number,
* and the error or warning message. * and the error or warning message.
*/ */
void function(string, int, int, string) messageFunction; void function(string, size_t, size_t, string) messageFunction;
bool isSliceExpression() bool isSliceExpression()
{ {

View File

@ -90,11 +90,25 @@ struct TokenStructure(IDType)
return this.type == type; return this.type == type;
} }
IDType type; this(IDType type)
{
this.type = type;
}
this(IDType type, string text, size_t line, size_t column, size_t index)
{
this.text = text;
this.line = line;
this.column = column;
this.type = type;
this.index = index;
}
string text; string text;
size_t line; size_t line;
size_t column; size_t column;
size_t index; size_t index;
IDType type;
} }
mixin template Lexer(R, IDType, Token, alias isSeparating, alias defaultTokenFunction, mixin template Lexer(R, IDType, Token, alias isSeparating, alias defaultTokenFunction,
@ -159,6 +173,9 @@ mixin template Lexer(R, IDType, Token, alias isSeparating, alias defaultTokenFun
string code; string code;
if (staticTokens.countUntil(token) >= 0) if (staticTokens.countUntil(token) >= 0)
{ {
if (token.length == 1)
code ~= indent ~ "range.popFront();\n";
else
code ~= indent ~ "range.popFrontN(" ~ text(token.length) ~ ");\n"; code ~= indent ~ "range.popFrontN(" ~ text(token.length) ~ ");\n";
code ~= indent ~ "return Token(tok!\"" ~ escape(token) ~"\", null, range.line, range.column, range.index);\n"; code ~= indent ~ "return Token(tok!\"" ~ escape(token) ~"\", null, range.line, range.column, range.index);\n";
} }
@ -168,6 +185,9 @@ mixin template Lexer(R, IDType, Token, alias isSeparating, alias defaultTokenFun
{ {
code ~= indent ~ "if (!range.canPeek(" ~ text(token.length) ~ ") || isSeparating(range.peek(" ~ text(token.length) ~ ")))\n"; code ~= indent ~ "if (!range.canPeek(" ~ text(token.length) ~ ") || isSeparating(range.peek(" ~ text(token.length) ~ ")))\n";
code ~= indent ~ "{\n"; code ~= indent ~ "{\n";
if (token.length == 1)
code ~= indent ~ " range.popFront();\n";
else
code ~= indent ~ " range.popFrontN(" ~ text(token.length) ~ ");\n"; code ~= indent ~ " range.popFrontN(" ~ text(token.length) ~ ");\n";
code ~= indent ~ " return Token(tok!\"" ~ escape(token) ~"\", null, range.line, range.column, range.index);\n"; code ~= indent ~ " return Token(tok!\"" ~ escape(token) ~"\", null, range.line, range.column, range.index);\n";
code ~= indent ~ "}\n"; code ~= indent ~ "}\n";
@ -233,7 +253,7 @@ mixin template Lexer(R, IDType, Token, alias isSeparating, alias defaultTokenFun
lexerLoop: switch (range.front) lexerLoop: switch (range.front)
{ {
mixin(generateCaseStatements(stupidToArray(sort(staticTokens ~ pseudoTokens ~ possibleDefaultTokens)))); mixin(generateCaseStatements(stupidToArray(sort(staticTokens ~ pseudoTokens ~ possibleDefaultTokens))));
//pragma(msg, generateCaseStatements(stupidToArray(sort(staticTokens ~ pseudoTokens ~ possibleDefaultTokens)))); pragma(msg, generateCaseStatements(stupidToArray(sort(staticTokens ~ pseudoTokens ~ possibleDefaultTokens))));
outer_default: outer_default:
default: default:
range = r; range = r;
@ -309,9 +329,9 @@ public:
return range[_index + offset]; return range[_index + offset];
} }
bool canPeek(int offset = 1) pure nothrow const bool canPeek(size_t offset = 1) pure nothrow const
{ {
return _index + offset >= 0 && _index + offset < range.length; return _index + offset < range.length;
} }
typeof(this) save() @property typeof(this) save() @property
@ -352,117 +372,115 @@ private:
R range; R range;
} }
struct PeekRange(R, size_t peekSupported = 1) //struct PeekRange(R, size_t peekSupported = 1)
if (!isRandomAccessRange!R && isForwardRange!R) // if (!isRandomAccessRange!R && isForwardRange!R)
{ //{
public: //public:
//
this(R range) // this(R range)
{ // {
this.range = range; // this.range = range;
for (size_t i = 0; !this.range.empty && i < peekSupported; i++) // for (size_t i = 0; !this.range.empty && i < peekSupported; i++)
{ // {
rangeSizeCount++; // rangeSizeCount++;
buffer[i] = this.range.front; // buffer[i] = this.range.front;
range.popFront(); // range.popFront();
} // }
} // }
//
ElementType!R front() const @property // ElementType!R front() const @property
in // in
{ // {
assert (!empty); // assert (!empty);
} // }
body // body
{ // {
return buffer[bufferIndex]; // return buffer[bufferIndex];
} // }
//
void popFront() // void popFront()
in // in
{ // {
assert (!empty); // assert (!empty);
} // }
body // body
{ // {
index++; // index++;
column++; // column++;
count++; // count++;
bufferIndex = bufferIndex + 1 > buffer.length ? 0 : bufferIndex + 1; // bufferIndex = bufferIndex + 1 > buffer.length ? 0 : bufferIndex + 1;
if (marking) // if (marking)
markBuffer.put(buffer[bufferIndex]); // markBuffer.put(buffer[bufferIndex]);
if (!range.empty) // if (!range.empty)
{ // {
buffer[bufferIndex + peekSupported % buffer.length] = range.front(); // buffer[bufferIndex + peekSupported % buffer.length] = range.front();
range.popFront(); // range.popFront();
rangeSizeCount++; // rangeSizeCount++;
} // }
} // }
//
bool empty() const nothrow pure @property // bool empty() const nothrow pure @property
{ // {
return rangeSizeCount == count; // return rangeSizeCount == count;
} // }
//
ElementType!R peek(int offset = 1) pure nothrow const // ElementType!R peek(int offset = 1) pure nothrow const
in // in
{ // {
assert (canPeek(offset)); // assert (canPeek(offset));
} // }
body // body
{ // {
return buffer[(bufferIndex + offset) % buffer.length]; // return buffer[(bufferIndex + offset) % buffer.length];
} // }
//
bool canPeek(int offset = 1) pure nothrow const // bool canPeek(size_t int offset = 1) pure nothrow const
{ // {
return offset >= 0 // return offset <= peekSupported && count + offset <= rangeSizeCount;
? offset <= peekSupported && count + offset <= rangeSizeCount // }
: abs(offset) <= peekSupported && (count - abs(offset)) >= 0; //
} // typeof(this) save() @property
// {
typeof(this) save() @property // typeof(this) newRange;
{ // newRange.count = count;
typeof(this) newRange; // newRange.rangeSizeCount = count;
newRange.count = count; // newRange.buffer = buffer.dup;
newRange.rangeSizeCount = count; // newRange.bufferIndex = bufferIndex;
newRange.buffer = buffer.dup; // newRange.range = range.save;
newRange.bufferIndex = bufferIndex; // return newRange;
newRange.range = range.save; // }
return newRange; //
} // void mark()
// {
void mark() // marking = true;
{ // markBuffer.clear();
marking = true; // }
markBuffer.clear(); //
} // ElementEncodingType!R[] getMarked()
// {
ElementEncodingType!R[] getMarked() // marking = false;
{ // return markBuffer.data;
marking = false; // }
return markBuffer.data; //
} // void incrementLine() pure nothrow
// {
void incrementLine() pure nothrow // _column = 1;
{ // _line++;
_column = 1; // }
_line++; //
} // size_t line() pure nothrow const @property { return _line; }
// size_t column() pure nothrow const @property { return _column; }
size_t line() pure nothrow const @property { return _line; } // size_t index() pure nothrow const @property { return _index; }
size_t column() pure nothrow const @property { return _column; } //
size_t index() pure nothrow const @property { return _index; } //private:
// auto markBuffer = appender!(ElementType!R[])();
private: // bool marking;
auto markBuffer = appender!(ElementType!R[])(); // size_t count;
bool marking; // size_t rangeSizeCount;
size_t count; // ElementType!(R)[peekSupported + 1] buffer;
size_t rangeSizeCount; // size_t bufferIndex;
ElementType!(R)[(peekSupported * 2) + 1] buffer; // size_t _column = 1;
size_t bufferIndex; // size_t _line = 1;
size_t _column = 1; // size_t _index = 0;
size_t _line = 1; // R range;
size_t _index = 0; //}
R range;
}