remove awkward error 'optimization' from Scanner

This commit is contained in:
Cameron Ross 2019-03-17 18:41:08 -03:00
parent 11020e46ed
commit c7b8ff8a1b
No known key found for this signature in database
GPG key ID: 777897D98DC91C54

View file

@ -150,25 +150,6 @@ struct Scanner
/// Possible simple keys indexed by flow levels. /// Possible simple keys indexed by flow levels.
SimpleKey[] possibleSimpleKeys_; SimpleKey[] possibleSimpleKeys_;
/// Set on error by nothrow/@nogc inner functions along with errorData_.
///
/// Non-nothrow/GC-using caller functions can then throw an exception using
/// data stored in errorData_.
bool error_;
/// Data for the exception to throw if error_ is true.
MarkedYAMLExceptionData errorData_;
/// Error messages can be built in this buffer without using the GC.
///
/// ScannerException (MarkedYAMLException) copies string data passed to its
/// constructor so it's safe to use slices of this buffer as parameters for
/// exceptions that may outlive the Scanner. The GC allocation when creating the
/// error message is removed, but the allocation when creating an exception is
/// not.
char[256] msgBuffer_;
public: public:
/// Construct a Scanner using specified Reader. /// Construct a Scanner using specified Reader.
this(Reader reader) @safe nothrow this(Reader reader) @safe nothrow
@ -203,45 +184,11 @@ struct Scanner
} }
private: private:
/// Build an error message in msgBuffer_ and return it as a string.
string buildMsg(S ...)(S args)
{
try {
return text(args);
}
catch (Exception)
{
return "";
}
}
/// Most scanning error messages have the same format; so build them with this /// Most scanning error messages have the same format; so build them with this
/// function. /// function.
string expected(T)(string expected, T found) string expected(T)(string expected, T found)
{ {
return buildMsg("expected ", expected, ", but found ", found); return text("expected ", expected, ", but found ", found);
}
/// If error_ is true, throws a ScannerException constructed from errorData_ and
/// sets error_ to false.
void throwIfError() @safe pure
{
if(!error_) { return; }
error_ = false;
throw new ScannerException(errorData_);
}
/// Called by internal nothrow/@nogc methods to set an error to be thrown by
/// their callers.
///
/// See_Also: dyaml.exception.MarkedYamlException
void error(string context, const Mark contextMark, string problem,
const Mark problemMark) @safe pure nothrow @nogc
{
assert(error_ == false,
"Setting an error when there already is a not yet thrown error");
error_ = true;
errorData_ = MarkedYAMLExceptionData(context, contextMark, problem, problemMark);
} }
/// Determine whether or not we need to fetch more tokens before peeking/getting a token. /// Determine whether or not we need to fetch more tokens before peeking/getting a token.
@ -462,7 +409,6 @@ struct Scanner
allowSimpleKey_ = false; allowSimpleKey_ = false;
auto directive = scanDirective(); auto directive = scanDirective();
throwIfError();
tokens_.push(directive); tokens_.push(directive);
} }
@ -646,7 +592,6 @@ struct Scanner
allowSimpleKey_ = false; allowSimpleKey_ = false;
auto anchor = scanAnchor(id); auto anchor = scanAnchor(id);
throwIfError();
tokens_.push(anchor); tokens_.push(anchor);
} }
@ -663,7 +608,6 @@ struct Scanner
allowSimpleKey_ = false; allowSimpleKey_ = false;
tokens_.push(scanTag()); tokens_.push(scanTag());
throwIfError();
} }
/// Add block SCALAR token. /// Add block SCALAR token.
@ -676,7 +620,6 @@ struct Scanner
allowSimpleKey_ = true; allowSimpleKey_ = true;
auto blockScalar = scanBlockScalar(style); auto blockScalar = scanBlockScalar(style);
throwIfError();
tokens_.push(blockScalar); tokens_.push(blockScalar);
} }
@ -694,7 +637,6 @@ struct Scanner
// Scan and add SCALAR. // Scan and add SCALAR.
auto scalar = scanFlowScalar(quotes); auto scalar = scanFlowScalar(quotes);
throwIfError();
tokens_.push(scalar); tokens_.push(scalar);
} }
@ -711,7 +653,6 @@ struct Scanner
// change this flag if the scan is finished at the beginning of the line. // change this flag if the scan is finished at the beginning of the line.
allowSimpleKey_ = false; allowSimpleKey_ = false;
auto plain = scanPlain(); auto plain = scanPlain();
throwIfError();
// Scan and add SCALAR. May change allowSimpleKey_ // Scan and add SCALAR. May change allowSimpleKey_
tokens_.push(plain); tokens_.push(plain);
@ -802,8 +743,6 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// characters into that slice. /// characters into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanAlphaNumericToSlice(string name)(const Mark startMark) void scanAlphaNumericToSlice(string name)(const Mark startMark)
{ {
size_t length; size_t length;
@ -813,9 +752,8 @@ struct Scanner
if(length == 0) if(length == 0)
{ {
enum contextMsg = "While scanning " ~ name; enum contextMsg = "While scanning " ~ name;
error(contextMsg, startMark, expected("alphanumeric, '-' or '_'", c), throw new ScannerException(contextMsg, startMark, expected("alphanumeric, '-' or '_'", c),
reader_.mark); reader_.mark);
return;
} }
reader_.sliceBuilder.write(reader_.get(length)); reader_.sliceBuilder.write(reader_.get(length));
@ -890,7 +828,6 @@ struct Scanner
// Scan directive name // Scan directive name
reader_.sliceBuilder.begin(); reader_.sliceBuilder.begin();
scanDirectiveNameToSlice(startMark); scanDirectiveNameToSlice(startMark);
if(error_) { return Token.init; }
const name = reader_.sliceBuilder.finish(); const name = reader_.sliceBuilder.finish();
reader_.sliceBuilder.begin(); reader_.sliceBuilder.begin();
@ -899,7 +836,6 @@ struct Scanner
uint tagHandleEnd = uint.max; uint tagHandleEnd = uint.max;
if(name == "YAML") { scanYAMLDirectiveValueToSlice(startMark); } if(name == "YAML") { scanYAMLDirectiveValueToSlice(startMark); }
else if(name == "TAG") { tagHandleEnd = scanTagDirectiveValueToSlice(startMark); } else if(name == "TAG") { tagHandleEnd = scanTagDirectiveValueToSlice(startMark); }
if(error_) { return Token.init; }
char[] value = reader_.sliceBuilder.finish(); char[] value = reader_.sliceBuilder.finish();
Mark endMark = reader_.mark; Mark endMark = reader_.mark;
@ -914,7 +850,6 @@ struct Scanner
} }
scanDirectiveIgnoredLine(startMark); scanDirectiveIgnoredLine(startMark);
if(error_) { return Token.init; }
return directiveToken(startMark, endMark, value, directive, tagHandleEnd); return directiveToken(startMark, endMark, value, directive, tagHandleEnd);
} }
@ -923,16 +858,13 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// characters into that slice. /// characters into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanDirectiveNameToSlice(const Mark startMark) @safe void scanDirectiveNameToSlice(const Mark startMark) @safe
{ {
// Scan directive name. // Scan directive name.
scanAlphaNumericToSlice!"a directive"(startMark); scanAlphaNumericToSlice!"a directive"(startMark);
if(error_) { return; }
if(reader_.peek().among!(' ', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029')) { return; } if(reader_.peek().among!(' ', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029')) { return; }
error("While scanning a directive", startMark, throw new ScannerException("While scanning a directive", startMark,
expected("alphanumeric, '-' or '_'", reader_.peek()), reader_.mark); expected("alphanumeric, '-' or '_'", reader_.peek()), reader_.mark);
} }
@ -940,31 +872,26 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// characters into that slice. /// characters into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanYAMLDirectiveValueToSlice(const Mark startMark) @safe void scanYAMLDirectiveValueToSlice(const Mark startMark) @safe
{ {
findNextNonSpace(); findNextNonSpace();
scanYAMLDirectiveNumberToSlice(startMark); scanYAMLDirectiveNumberToSlice(startMark);
if(error_) { return; }
if(reader_.peekByte() != '.') if(reader_.peekByte() != '.')
{ {
error("While scanning a directive", startMark, throw new ScannerException("While scanning a directive", startMark,
expected("digit or '.'", reader_.peek()), reader_.mark); expected("digit or '.'", reader_.peek()), reader_.mark);
return;
} }
// Skip the '.'. // Skip the '.'.
reader_.forward(); reader_.forward();
reader_.sliceBuilder.write('.'); reader_.sliceBuilder.write('.');
scanYAMLDirectiveNumberToSlice(startMark); scanYAMLDirectiveNumberToSlice(startMark);
if(error_) { return; }
if(!reader_.peek().among!(' ', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029')) if(!reader_.peek().among!(' ', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029'))
{ {
error("While scanning a directive", startMark, throw new ScannerException("While scanning a directive", startMark,
expected("digit or '.'", reader_.peek()), reader_.mark); expected("digit or '.'", reader_.peek()), reader_.mark);
} }
} }
@ -973,15 +900,12 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// characters into that slice. /// characters into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanYAMLDirectiveNumberToSlice(const Mark startMark) @safe void scanYAMLDirectiveNumberToSlice(const Mark startMark) @safe
{ {
if(!isDigit(reader_.peek())) if(!isDigit(reader_.peek()))
{ {
error("While scanning a directive", startMark, throw new ScannerException("While scanning a directive", startMark,
expected("digit", reader_.peek()), reader_.mark); expected("digit", reader_.peek()), reader_.mark);
return;
} }
// Already found the first digit in the enforce(), so set length to 1. // Already found the first digit in the enforce(), so set length to 1.
@ -997,14 +921,11 @@ struct Scanner
/// characters into that slice. /// characters into that slice.
/// ///
/// Returns: Length of tag handle (which is before tag prefix) in scanned data /// Returns: Length of tag handle (which is before tag prefix) in scanned data
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
uint scanTagDirectiveValueToSlice(const Mark startMark) @safe uint scanTagDirectiveValueToSlice(const Mark startMark) @safe
{ {
findNextNonSpace(); findNextNonSpace();
const startLength = reader_.sliceBuilder.length; const startLength = reader_.sliceBuilder.length;
scanTagDirectiveHandleToSlice(startMark); scanTagDirectiveHandleToSlice(startMark);
if(error_) { return uint.max; }
const handleLength = cast(uint)(reader_.sliceBuilder.length - startLength); const handleLength = cast(uint)(reader_.sliceBuilder.length - startLength);
findNextNonSpace(); findNextNonSpace();
scanTagDirectivePrefixToSlice(startMark); scanTagDirectivePrefixToSlice(startMark);
@ -1016,14 +937,11 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// characters into that slice. /// characters into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanTagDirectiveHandleToSlice(const Mark startMark) @safe void scanTagDirectiveHandleToSlice(const Mark startMark) @safe
{ {
scanTagHandleToSlice!"directive"(startMark); scanTagHandleToSlice!"directive"(startMark);
if(error_) { return; }
if(reader_.peekByte() == ' ') { return; } if(reader_.peekByte() == ' ') { return; }
error("While scanning a directive handle", startMark, throw new ScannerException("While scanning a directive handle", startMark,
expected("' '", reader_.peek()), reader_.mark); expected("' '", reader_.peek()), reader_.mark);
} }
@ -1031,19 +949,15 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// characters into that slice. /// characters into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanTagDirectivePrefixToSlice(const Mark startMark) @safe void scanTagDirectivePrefixToSlice(const Mark startMark) @safe
{ {
scanTagURIToSlice!"directive"(startMark); scanTagURIToSlice!"directive"(startMark);
if(reader_.peek().among!(' ', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029')) { return; } if(reader_.peek().among!(' ', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029')) { return; }
error("While scanning a directive prefix", startMark, throw new ScannerException("While scanning a directive prefix", startMark,
expected("' '", reader_.peek()), reader_.mark); expected("' '", reader_.peek()), reader_.mark);
} }
/// Scan (and ignore) ignored line after a directive. /// Scan (and ignore) ignored line after a directive.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanDirectiveIgnoredLine(const Mark startMark) @safe void scanDirectiveIgnoredLine(const Mark startMark) @safe
{ {
findNextNonSpace(); findNextNonSpace();
@ -1053,7 +967,7 @@ struct Scanner
scanLineBreak(); scanLineBreak();
return; return;
} }
error("While scanning a directive", startMark, throw new ScannerException("While scanning a directive", startMark,
expected("comment or a line break", reader_.peek()), reader_.mark); expected("comment or a line break", reader_.peek()), reader_.mark);
} }
@ -1068,8 +982,6 @@ struct Scanner
/// and /// and
/// [ *alias , "value" ] /// [ *alias , "value" ]
/// Therefore we restrict aliases to ASCII alphanumeric characters. /// Therefore we restrict aliases to ASCII alphanumeric characters.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
Token scanAnchor(const TokenID id) @safe Token scanAnchor(const TokenID id) @safe
{ {
const startMark = reader_.mark; const startMark = reader_.mark;
@ -1080,16 +992,14 @@ struct Scanner
else { scanAlphaNumericToSlice!"an anchor"(startMark); } else { scanAlphaNumericToSlice!"an anchor"(startMark); }
// On error, value is discarded as we return immediately // On error, value is discarded as we return immediately
char[] value = reader_.sliceBuilder.finish(); char[] value = reader_.sliceBuilder.finish();
if(error_) { return Token.init; }
if(!reader_.peek().isWhiteSpace && if(!reader_.peek().isWhiteSpace &&
!reader_.peekByte().among!('?', ':', ',', ']', '}', '%', '@')) !reader_.peekByte().among!('?', ':', ',', ']', '}', '%', '@'))
{ {
enum anchorCtx = "While scanning an anchor"; enum anchorCtx = "While scanning an anchor";
enum aliasCtx = "While scanning an alias"; enum aliasCtx = "While scanning an alias";
error(i == '*' ? aliasCtx : anchorCtx, startMark, throw new ScannerException(i == '*' ? aliasCtx : anchorCtx, startMark,
expected("alphanumeric, '-' or '_'", reader_.peek()), reader_.mark); expected("alphanumeric, '-' or '_'", reader_.peek()), reader_.mark);
return Token.init;
} }
if(id == TokenID.alias_) if(id == TokenID.alias_)
@ -1104,8 +1014,6 @@ struct Scanner
} }
/// Scan a tag token. /// Scan a tag token.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
Token scanTag() @safe Token scanTag() @safe
{ {
const startMark = reader_.mark; const startMark = reader_.mark;
@ -1123,12 +1031,10 @@ struct Scanner
handleEnd = 0; handleEnd = 0;
scanTagURIToSlice!"tag"(startMark); scanTagURIToSlice!"tag"(startMark);
if(error_) { return Token.init; }
if(reader_.peekByte() != '>') if(reader_.peekByte() != '>')
{ {
error("While scanning a tag", startMark, throw new ScannerException("While scanning a tag", startMark,
expected("'>'", reader_.peek()), reader_.mark); expected("'>'", reader_.peek()), reader_.mark);
return Token.init;
} }
reader_.forward(); reader_.forward();
} }
@ -1158,7 +1064,6 @@ struct Scanner
{ {
scanTagHandleToSlice!"tag"(startMark); scanTagHandleToSlice!"tag"(startMark);
handleEnd = cast(uint)reader_.sliceBuilder.length; handleEnd = cast(uint)reader_.sliceBuilder.length;
if(error_) { return Token.init; }
} }
else else
{ {
@ -1168,7 +1073,6 @@ struct Scanner
} }
scanTagURIToSlice!"tag"(startMark); scanTagURIToSlice!"tag"(startMark);
if(error_) { return Token.init; }
} }
if(reader_.peek().isBreakOrSpace) if(reader_.peek().isBreakOrSpace)
@ -1177,14 +1081,11 @@ struct Scanner
return tagToken(startMark, reader_.mark, slice, handleEnd); return tagToken(startMark, reader_.mark, slice, handleEnd);
} }
error("While scanning a tag", startMark, expected("' '", reader_.peek()), throw new ScannerException("While scanning a tag", startMark, expected("' '", reader_.peek()),
reader_.mark); reader_.mark);
return Token.init;
} }
/// Scan a block scalar token with specified style. /// Scan a block scalar token with specified style.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
Token scanBlockScalar(const ScalarStyle style) @safe Token scanBlockScalar(const ScalarStyle style) @safe
{ {
const startMark = reader_.mark; const startMark = reader_.mark;
@ -1193,12 +1094,10 @@ struct Scanner
reader_.forward(); reader_.forward();
const indicators = scanBlockScalarIndicators(startMark); const indicators = scanBlockScalarIndicators(startMark);
if(error_) { return Token.init; }
const chomping = indicators[0]; const chomping = indicators[0];
const increment = indicators[1]; const increment = indicators[1];
scanBlockScalarIgnoredLine(startMark); scanBlockScalarIgnoredLine(startMark);
if(error_) { return Token.init; }
// Determine the indentation level and go to the first non-empty line. // Determine the indentation level and go to the first non-empty line.
Mark endMark; Mark endMark;
@ -1325,8 +1224,6 @@ struct Scanner
} }
/// Scan chomping and indentation indicators of a scalar token. /// Scan chomping and indentation indicators of a scalar token.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
Tuple!(Chomping, int) scanBlockScalarIndicators(const Mark startMark) @safe Tuple!(Chomping, int) scanBlockScalarIndicators(const Mark startMark) @safe
{ {
auto chomping = Chomping.clip; auto chomping = Chomping.clip;
@ -1337,12 +1234,10 @@ struct Scanner
if(getChomping(c, chomping)) if(getChomping(c, chomping))
{ {
getIncrement(c, increment, startMark); getIncrement(c, increment, startMark);
if(error_) { return tuple(Chomping.init, int.max); }
} }
else else
{ {
const gotIncrement = getIncrement(c, increment, startMark); const gotIncrement = getIncrement(c, increment, startMark);
if(error_) { return tuple(Chomping.init, int.max); }
if(gotIncrement) { getChomping(c, chomping); } if(gotIncrement) { getChomping(c, chomping); }
} }
@ -1350,9 +1245,8 @@ struct Scanner
{ {
return tuple(chomping, increment); return tuple(chomping, increment);
} }
error("While scanning a block scalar", startMark, throw new ScannerException("While scanning a block scalar", startMark,
expected("chomping or indentation indicator", c), reader_.mark); expected("chomping or indentation indicator", c), reader_.mark);
return tuple(Chomping.init, int.max);
} }
/// Get chomping indicator, if detected. Return false otherwise. /// Get chomping indicator, if detected. Return false otherwise.
@ -1383,8 +1277,6 @@ struct Scanner
/// the next character in the Reader. /// the next character in the Reader.
/// increment = Write the increment value here, if detected. /// increment = Write the increment value here, if detected.
/// startMark = Mark for error messages. /// startMark = Mark for error messages.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
bool getIncrement(ref dchar c, ref int increment, const Mark startMark) @safe bool getIncrement(ref dchar c, ref int increment, const Mark startMark) @safe
{ {
if(!c.isDigit) { return false; } if(!c.isDigit) { return false; }
@ -1397,14 +1289,11 @@ struct Scanner
c = reader_.peek(); c = reader_.peek();
return true; return true;
} }
error("While scanning a block scalar", startMark, throw new ScannerException("While scanning a block scalar", startMark,
expected("indentation indicator in range 1-9", "0"), reader_.mark); expected("indentation indicator in range 1-9", "0"), reader_.mark);
return false;
} }
/// Scan (and ignore) ignored line in a block scalar. /// Scan (and ignore) ignored line in a block scalar.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanBlockScalarIgnoredLine(const Mark startMark) @safe void scanBlockScalarIgnoredLine(const Mark startMark) @safe
{ {
findNextNonSpace(); findNextNonSpace();
@ -1415,7 +1304,7 @@ struct Scanner
scanLineBreak(); scanLineBreak();
return; return;
} }
error("While scanning a block scalar", startMark, throw new ScannerException("While scanning a block scalar", startMark,
expected("comment or line break", reader_.peek()), reader_.mark); expected("comment or line break", reader_.peek()), reader_.mark);
} }
@ -1463,25 +1352,19 @@ struct Scanner
} }
/// Scan a qouted flow scalar token with specified quotes. /// Scan a qouted flow scalar token with specified quotes.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
Token scanFlowScalar(const ScalarStyle quotes) @safe Token scanFlowScalar(const ScalarStyle quotes) @safe
{ {
const startMark = reader_.mark; const startMark = reader_.mark;
const quote = reader_.get(); const quote = reader_.get();
reader_.sliceBuilder.begin(); reader_.sliceBuilder.begin();
scope(exit) if(error_) { reader_.sliceBuilder.finish(); }
scanFlowScalarNonSpacesToSlice(quotes, startMark); scanFlowScalarNonSpacesToSlice(quotes, startMark);
if(error_) { return Token.init; }
while(reader_.peek() != quote) while(reader_.peek() != quote)
{ {
scanFlowScalarSpacesToSlice(startMark); scanFlowScalarSpacesToSlice(startMark);
if(error_) { return Token.init; }
scanFlowScalarNonSpacesToSlice(quotes, startMark); scanFlowScalarNonSpacesToSlice(quotes, startMark);
if(error_) { return Token.init; }
} }
reader_.forward(); reader_.forward();
@ -1493,8 +1376,6 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// characters into that slice. /// characters into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanFlowScalarNonSpacesToSlice(const ScalarStyle quotes, const Mark startMark) void scanFlowScalarNonSpacesToSlice(const ScalarStyle quotes, const Mark startMark)
@safe @safe
{ {
@ -1512,9 +1393,8 @@ struct Scanner
const char[] slice = reader_.slice(numCodePoints + 32); const char[] slice = reader_.slice(numCodePoints + 32);
if(slice.length == oldSliceLength) if(slice.length == oldSliceLength)
{ {
error("While reading a flow scalar", startMark, throw new ScannerException("While reading a flow scalar", startMark,
"reached end of file", reader_.mark); "reached end of file", reader_.mark);
return;
} }
for(size_t i = oldSliceLength; i < slice.length;) for(size_t i = oldSliceLength; i < slice.length;)
{ {
@ -1558,12 +1438,13 @@ struct Scanner
const hexLength = dyaml.escapes.escapeHexLength(c); const hexLength = dyaml.escapes.escapeHexLength(c);
reader_.forward(); reader_.forward();
foreach(i; 0 .. hexLength) if(!reader_.peek(i).isHexDigit) foreach(i; 0 .. hexLength) {
if(!reader_.peek(i).isHexDigit)
{ {
error("While scanning a double quoted scalar", startMark, throw new ScannerException("While scanning a double quoted scalar", startMark,
expected("escape sequence of hexadecimal numbers", expected("escape sequence of hexadecimal numbers",
reader_.peek(i)), reader_.mark); reader_.peek(i)), reader_.mark);
return; }
} }
char[] hex = reader_.get(hexLength); char[] hex = reader_.get(hexLength);
char[2] escapeStart = ['\\', cast(char) c]; char[2] escapeStart = ['\\', cast(char) c];
@ -1579,24 +1460,21 @@ struct Scanner
} }
catch (Exception) catch (Exception)
{ {
error("While scanning a double quoted scalar", startMark, throw new ScannerException("While scanning a double quoted scalar", startMark,
"overflow when parsing an escape sequence of " ~ "overflow when parsing an escape sequence of " ~
"hexadecimal numbers.", reader_.mark); "hexadecimal numbers.", reader_.mark);
return;
} }
} }
else if(c.among!('\n', '\r', '\u0085', '\u2028', '\u2029')) else if(c.among!('\n', '\r', '\u0085', '\u2028', '\u2029'))
{ {
scanLineBreak(); scanLineBreak();
scanFlowScalarBreaksToSlice(startMark); scanFlowScalarBreaksToSlice(startMark);
if(error_) { return; }
} }
else else
{ {
error("While scanning a double quoted scalar", startMark, throw new ScannerException("While scanning a double quoted scalar", startMark,
buildMsg("found unsupported escape character", c), text("found unsupported escape character", c),
reader_.mark); reader_.mark);
return;
} }
} }
else { return; } else { return; }
@ -1607,8 +1485,6 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// spaces into that slice. /// spaces into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanFlowScalarSpacesToSlice(const Mark startMark) @safe void scanFlowScalarSpacesToSlice(const Mark startMark) @safe
{ {
// Increase length as long as we see whitespace. // Increase length as long as we see whitespace.
@ -1620,9 +1496,8 @@ struct Scanner
const c = reader_.peek(length); const c = reader_.peek(length);
if(c == '\0') if(c == '\0')
{ {
error("While scanning a quoted scalar", startMark, throw new ScannerException("While scanning a quoted scalar", startMark,
"found unexpected end of buffer", reader_.mark); "found unexpected end of buffer", reader_.mark);
return;
} }
// Spaces not followed by a line break. // Spaces not followed by a line break.
@ -1642,7 +1517,6 @@ struct Scanner
// If we have extra line breaks after the first, scan them into the // If we have extra line breaks after the first, scan them into the
// slice. // slice.
const bool extraBreaks = scanFlowScalarBreaksToSlice(startMark); const bool extraBreaks = scanFlowScalarBreaksToSlice(startMark);
if(error_) { return; }
// No extra breaks, one normal line break. Replace it with a space. // No extra breaks, one normal line break. Replace it with a space.
if(lineBreak == '\n' && !extraBreaks) { reader_.sliceBuilder.write(' '); } if(lineBreak == '\n' && !extraBreaks) { reader_.sliceBuilder.write(' '); }
@ -1652,8 +1526,6 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// line breaks into that slice. /// line breaks into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
bool scanFlowScalarBreaksToSlice(const Mark startMark) @safe bool scanFlowScalarBreaksToSlice(const Mark startMark) @safe
{ {
// True if at least one line break was found. // True if at least one line break was found.
@ -1665,9 +1537,8 @@ struct Scanner
if((prefix == "---" || prefix == "...") && if((prefix == "---" || prefix == "...") &&
reader_.peek(3).isWhiteSpace) reader_.peek(3).isWhiteSpace)
{ {
error("While scanning a quoted scalar", startMark, throw new ScannerException("While scanning a quoted scalar", startMark,
"found unexpected document separator", reader_.mark); "found unexpected document separator", reader_.mark);
return false;
} }
// Skip any whitespaces. // Skip any whitespaces.
@ -1684,8 +1555,6 @@ struct Scanner
} }
/// Scan plain scalar token (no block, no quotes). /// Scan plain scalar token (no block, no quotes).
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
Token scanPlain() @safe Token scanPlain() @safe
{ {
// We keep track of the allowSimpleKey_ flag here. // We keep track of the allowSimpleKey_ flag here.
@ -1746,11 +1615,10 @@ struct Scanner
spacesTransaction.commit(); spacesTransaction.commit();
reader_.sliceBuilder.finish(); reader_.sliceBuilder.finish();
reader_.forward(length); reader_.forward(length);
error("While scanning a plain scalar", startMark, throw new ScannerException("While scanning a plain scalar", startMark,
"found unexpected ':' . Please check " ~ "found unexpected ':' . Please check " ~
"http://pyyaml.org/wiki/YAMLColonInFlowContext for details.", "http://pyyaml.org/wiki/YAMLColonInFlowContext for details.",
reader_.mark); reader_.mark);
return Token.init;
} }
if(length == 0) { break; } if(length == 0) { break; }
@ -1842,16 +1710,13 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// characters into that slice. /// characters into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanTagHandleToSlice(string name)(const Mark startMark) void scanTagHandleToSlice(string name)(const Mark startMark)
{ {
dchar c = reader_.peek(); dchar c = reader_.peek();
enum contextMsg = "While scanning a " ~ name; enum contextMsg = "While scanning a " ~ name;
if(c != '!') if(c != '!')
{ {
error(contextMsg, startMark, expected("'!'", c), reader_.mark); throw new ScannerException(contextMsg, startMark, expected("'!'", c), reader_.mark);
return;
} }
uint length = 1; uint length = 1;
@ -1866,8 +1731,7 @@ struct Scanner
if(c != '!') if(c != '!')
{ {
reader_.forward(length); reader_.forward(length);
error(contextMsg, startMark, expected("'!'", c), reader_.mark); throw new ScannerException(contextMsg, startMark, expected("'!'", c), reader_.mark);
return;
} }
++length; ++length;
} }
@ -1879,8 +1743,6 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// characters into that slice. /// characters into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanTagURIToSlice(string name)(const Mark startMark) void scanTagURIToSlice(string name)(const Mark startMark)
{ {
// Note: we do not check if URI is well-formed. // Note: we do not check if URI is well-formed.
@ -1896,7 +1758,6 @@ struct Scanner
reader_.sliceBuilder.write(chars); reader_.sliceBuilder.write(chars);
length = 0; length = 0;
scanURIEscapesToSlice!name(startMark); scanURIEscapesToSlice!name(startMark);
if(error_) { return; }
} }
else { ++length; } else { ++length; }
c = reader_.peek(length); c = reader_.peek(length);
@ -1912,7 +1773,7 @@ struct Scanner
if(reader_.sliceBuilder.length > startLen) { return; } if(reader_.sliceBuilder.length > startLen) { return; }
enum contextMsg = "While parsing a " ~ name; enum contextMsg = "While parsing a " ~ name;
error(contextMsg, startMark, expected("URI", c), reader_.mark); throw new ScannerException(contextMsg, startMark, expected("URI", c), reader_.mark);
} }
// Not @nogc yet because std.utf.decode is not @nogc // Not @nogc yet because std.utf.decode is not @nogc
@ -1920,8 +1781,6 @@ struct Scanner
/// ///
/// Assumes that the caller is building a slice in Reader, and puts the scanned /// Assumes that the caller is building a slice in Reader, and puts the scanned
/// characters into that slice. /// characters into that slice.
///
/// In case of an error, error_ is set. Use throwIfError() to handle this.
void scanURIEscapesToSlice(string name)(const Mark startMark) void scanURIEscapesToSlice(string name)(const Mark startMark)
{ {
import core.exception : UnicodeException; import core.exception : UnicodeException;
@ -1939,8 +1798,7 @@ struct Scanner
{ {
auto msg = expected("URI escape sequence of 2 hexadecimal " ~ auto msg = expected("URI escape sequence of 2 hexadecimal " ~
"numbers", nextByte); "numbers", nextByte);
error(contextMsg, startMark, msg, reader_.mark); throw new ScannerException(contextMsg, startMark, msg, reader_.mark);
return;
} }
buffer ~= nextByte[].to!ubyte(16); buffer ~= nextByte[].to!ubyte(16);
@ -1955,10 +1813,9 @@ struct Scanner
} }
catch (UnicodeException) catch (UnicodeException)
{ {
error(contextMsg, startMark, throw new ScannerException(contextMsg, startMark,
"Invalid UTF-8 data encoded in URI escape sequence", "Invalid UTF-8 data encoded in URI escape sequence",
reader_.mark); reader_.mark);
return;
} }
} }