Optimized fetchToken()

This commit is contained in:
Ferdinand Majerech 2014-08-04 02:26:14 +02:00
parent 7360e85a3a
commit 44885cde4e

View file

@ -284,30 +284,33 @@ final class Scanner
unwindIndent(reader_.column); unwindIndent(reader_.column);
// Get the next character. // Get the next character.
const dchar c = reader_.peek(); const dchar c = reader_.peekByte();
// Fetch the token. // Fetch the token.
if(c == '\0') { return fetchStreamEnd(); } if(c == '\0') { return fetchStreamEnd(); }
if(checkDirective()) { return fetchDirective(); } if(checkDirective()) { return fetchDirective(); }
if(checkDocumentStart()) { return fetchDocumentStart(); } if(checkDocumentStart()) { return fetchDocumentStart(); }
if(checkDocumentEnd()) { return fetchDocumentEnd(); } if(checkDocumentEnd()) { return fetchDocumentEnd(); }
// Order of the following checks is NOT significant. // Order of the following checks is NOT significant.
if(c == '[') { return fetchFlowSequenceStart(); } switch(c)
if(c == '{') { return fetchFlowMappingStart(); } {
if(c == ']') { return fetchFlowSequenceEnd(); } case '[': return fetchFlowSequenceStart();
if(c == '}') { return fetchFlowMappingEnd(); } case '{': return fetchFlowMappingStart();
if(c == ',') { return fetchFlowEntry(); } case ']': return fetchFlowSequenceEnd();
if(checkBlockEntry()) { return fetchBlockEntry(); } case '}': return fetchFlowMappingEnd();
if(checkKey()) { return fetchKey(); } case ',': return fetchFlowEntry();
if(checkValue()) { return fetchValue(); } case '!': return fetchTag();
if(c == '*') { return fetchAlias(); } case '\'': return fetchSingle();
if(c == '&') { return fetchAnchor(); } case '\"': return fetchDouble();
if(c == '!') { return fetchTag(); } case '*': return fetchAlias();
if(c == '|' && flowLevel_ == 0) { return fetchLiteral(); } case '&': return fetchAnchor();
if(c == '>' && flowLevel_ == 0) { return fetchFolded(); } case '?': if(checkKey()) { return fetchKey(); } goto default;
if(c == '\'') { return fetchSingle(); } case ':': if(checkValue()) { return fetchValue(); } goto default;
if(c == '\"') { return fetchDouble(); } case '-': if(checkBlockEntry()) { return fetchBlockEntry(); } goto default;
if(checkPlain()) { return fetchPlain(); } case '|': if(flowLevel_ == 0) { return fetchLiteral(); } break;
case '>': if(flowLevel_ == 0) { return fetchFolded(); } break;
default: if(checkPlain()) { return fetchPlain(); }
}
throw new ScannerException("While scanning for the next token, found character " throw new ScannerException("While scanning for the next token, found character "
"\'%s\', index %s that cannot start any token" "\'%s\', index %s that cannot start any token"
@ -757,8 +760,7 @@ final class Scanner
/// Check if the next token is BLOCK-ENTRY: '-' (' '|'\n') /// Check if the next token is BLOCK-ENTRY: '-' (' '|'\n')
bool checkBlockEntry() @safe pure nothrow @nogc bool checkBlockEntry() @safe pure nothrow @nogc
{ {
return reader_.peek() == '-' && return " \t\0\n\r\u0085\u2028\u2029"d.canFind(reader_.peek(1));
" \t\0\n\r\u0085\u2028\u2029"d.canFind(reader_.peek(1));
} }
/// Check if the next token is KEY(flow context): '?' /// Check if the next token is KEY(flow context): '?'
@ -766,8 +768,7 @@ final class Scanner
/// or KEY(block context): '?' (' '|'\n') /// or KEY(block context): '?' (' '|'\n')
bool checkKey() @safe pure nothrow @nogc bool checkKey() @safe pure nothrow @nogc
{ {
return reader_.peek() == '?' && return (flowLevel_ > 0 ||
(flowLevel_ > 0 ||
" \t\0\n\r\u0085\u2028\u2029"d.canFind(reader_.peek(1))); " \t\0\n\r\u0085\u2028\u2029"d.canFind(reader_.peek(1)));
} }
@ -776,9 +777,8 @@ final class Scanner
/// or VALUE(block context): ':' (' '|'\n') /// or VALUE(block context): ':' (' '|'\n')
bool checkValue() @safe pure nothrow @nogc bool checkValue() @safe pure nothrow @nogc
{ {
return reader_.peek() == ':' && mixin FastCharSearch!" \t\0\n\r\u0085\u2028\u2029"d search;
(flowLevel_ > 0 || return flowLevel_ > 0 || search.canFind(reader_.peek(1));
" \t\0\n\r\u0085\u2028\u2029"d.canFind(reader_.peek(1)));
} }
/// Check if the next token is a plain scalar. /// Check if the next token is a plain scalar.