Got rid out dyaml.util . Was never a good idea, the functions

were not trivial to understand. For now, we just use canFind
with strings in scanner and emitter.
This commit is contained in:
Ferdinand Majerech 2011-10-13 22:04:37 +02:00
parent 8360da733d
commit 21001b36b9
3 changed files with 48 additions and 78 deletions

View file

@ -32,7 +32,6 @@ import dyaml.flags;
import dyaml.linebreak; import dyaml.linebreak;
import dyaml.tag; import dyaml.tag;
import dyaml.token; import dyaml.token;
import dyaml.util;
package: package:
@ -678,9 +677,11 @@ struct Emitter
uint length = 0; uint length = 0;
const id = event_.id; const id = event_.id;
const scalar = id == EventID.Scalar; const scalar = id == EventID.Scalar;
const collectionStart = [EventID.MappingStart, EventID.SequenceStart].canFind(id); const collectionStart = [EventID.MappingStart,
EventID.SequenceStart].canFind(id);
if((id == EventID.Alias || scalar || collectionStart) && !event_.anchor.isNull()) if((id == EventID.Alias || scalar || collectionStart)
&& !event_.anchor.isNull())
{ {
if(preparedAnchor_ is null) if(preparedAnchor_ is null)
{ {
@ -1002,7 +1003,7 @@ struct Emitter
//Last character or followed by a whitespace. //Last character or followed by a whitespace.
bool followedByWhitespace = scalar.length == 1 || bool followedByWhitespace = scalar.length == 1 ||
or!(isBreakOrZero, isSpace)(scalar[1]); " \t\0\n\r\u0085\u2028\u2029".canFind(scalar[1]);
//The previous character is a space/break (false by default). //The previous character is a space/break (false by default).
bool previousSpace, previousBreak; bool previousSpace, previousBreak;
@ -1074,10 +1075,9 @@ struct Emitter
} }
//Prepare for the next character. //Prepare for the next character.
preceededByWhitespace = isBreakOrZero(c) || isSpace(c); preceededByWhitespace = "\0\n\r\u0085\u2028\u2029 \t".canFind(c);
followedByWhitespace = index + 2 >= scalar.length || followedByWhitespace = index + 2 >= scalar.length ||
isBreakOrZero(scalar[index + 2]) || "\0\n\r\u0085\u2028\u2029 \t".canFind(scalar[index + 2]);
isSpace(scalar[index + 2]);
} }
with(analysis.flags) with(analysis.flags)

View file

@ -25,7 +25,6 @@ import std.utf;
import dyaml.exception; import dyaml.exception;
import dyaml.reader; import dyaml.reader;
import dyaml.token; import dyaml.token;
import dyaml.util;
package: package:
@ -691,7 +690,7 @@ final class Scanner
return reader_.column == 0 && return reader_.column == 0 &&
reader_.peek() == '-' && reader_.peek() == '-' &&
reader_.prefix(3) == "---" && reader_.prefix(3) == "---" &&
or!(isBreakOrZero, isSpace)(reader_.peek(3)); " \t\0\n\r\u0085\u2028\u2029".canFind(reader_.peek(3));
} }
///Check if the next token is DOCUMENT-END: ^ '...' (' '|'\n') ///Check if the next token is DOCUMENT-END: ^ '...' (' '|'\n')
@ -701,13 +700,14 @@ final class Scanner
return reader_.column == 0 && return reader_.column == 0 &&
reader_.peek() == '.' && reader_.peek() == '.' &&
reader_.prefix(3) == "..." && reader_.prefix(3) == "..." &&
or!(isBreakOrZero, isSpace)(reader_.peek(3)); " \t\0\n\r\u0085\u2028\u2029".canFind(reader_.peek(3));
} }
///Check if the next token is BLOCK-ENTRY: '-' (' '|'\n') ///Check if the next token is BLOCK-ENTRY: '-' (' '|'\n')
bool checkBlockEntry() bool checkBlockEntry()
{ {
return reader_.peek() == '-' && or!(isBreakOrZero, isSpace)(reader_.peek(1)); return reader_.peek() == '-' &&
" \t\0\n\r\u0085\u2028\u2029".canFind(reader_.peek(1));
} }
/** /**
@ -718,7 +718,8 @@ final class Scanner
bool checkKey() bool checkKey()
{ {
return reader_.peek() == '?' && return reader_.peek() == '?' &&
(flowLevel_ > 0 || or!(isBreakOrZero, isSpace)(reader_.peek(1))); (flowLevel_ > 0 ||
" \t\0\n\r\u0085\u2028\u2029".canFind(reader_.peek(1)));
} }
/** /**
@ -729,7 +730,8 @@ final class Scanner
bool checkValue() bool checkValue()
{ {
return reader_.peek() == ':' && return reader_.peek() == ':' &&
(flowLevel_ > 0 || or!(isBreakOrZero, isSpace)(reader_.peek(1))); (flowLevel_ > 0 ||
" \t\0\n\r\u0085\u2028\u2029".canFind(reader_.peek(1)));
} }
/** /**
@ -751,8 +753,8 @@ final class Scanner
bool checkPlain() bool checkPlain()
{ {
const c = reader_.peek(); const c = reader_.peek();
return !(or!(isBreakOrZero, isSpace)(c) || "-?:,[]{}#&*!|>\'\"%@`".canFind(c)) || return !("-?:,[]{}#&*!|>\'\"%@` \t\0\n\r\u0085\u2028\u2029".canFind(c)) ||
(!or!(isBreakOrZero, isSpace)(reader_.peek(1)) && (!" \t\0\n\r\u0085\u2028\u2029".canFind(reader_.peek(1)) &&
(c == '-' || (flowLevel_ == 0 && "?:".canFind(c)))); (c == '-' || (flowLevel_ == 0 && "?:".canFind(c))));
} }
@ -786,7 +788,7 @@ final class Scanner
dstring scanToNextBreak() dstring scanToNextBreak()
{ {
uint length = 0; uint length = 0;
while(!isBreakOrZero(reader_.peek(length))){++length;} while(!"\0\n\r\u0085\u2028\u2029".canFind(reader_.peek(length))){++length;}
return reader_.get(length); return reader_.get(length);
} }
@ -853,7 +855,7 @@ final class Scanner
//Scan directive name. //Scan directive name.
const name = scanAlphaNumeric!"a directive"(startMark); const name = scanAlphaNumeric!"a directive"(startMark);
enforce(or!(isChar!' ', isBreakOrZero)(reader_.peek()), enforce(" \0\n\r\u0085\u2028\u2029".canFind(reader_.peek()),
new ScannerException("While scanning a directive", startMark, new ScannerException("While scanning a directive", startMark,
"expected alphanumeric, - or _, but found " "expected alphanumeric, - or _, but found "
~ to!string(reader_.peek()), reader_.mark)); ~ to!string(reader_.peek()), reader_.mark));
@ -874,7 +876,7 @@ final class Scanner
reader_.forward(); reader_.forward();
result ~= '.' ~ scanYAMLDirectiveNumber(startMark); result ~= '.' ~ scanYAMLDirectiveNumber(startMark);
enforce(or!(isChar!' ', isBreakOrZero)(reader_.peek()), enforce(" \0\n\r\u0085\u2028\u2029".canFind(reader_.peek()),
new ScannerException("While scanning a directive", startMark, new ScannerException("While scanning a directive", startMark,
"expected a digit or '.', but found: " "expected a digit or '.', but found: "
~ to!string(reader_.peek()), reader_.mark)); ~ to!string(reader_.peek()), reader_.mark));
@ -920,7 +922,7 @@ final class Scanner
dstring scanTagDirectivePrefix(in Mark startMark) dstring scanTagDirectivePrefix(in Mark startMark)
{ {
const value = scanTagURI("directive", startMark); const value = scanTagURI("directive", startMark);
enforce(or!(isChar!' ', isBreakOrZero)(reader_.peek()), enforce(" \0\n\r\u0085\u2028\u2029".canFind(reader_.peek()),
new ScannerException("While scanning a directive prefix", startMark, new ScannerException("While scanning a directive prefix", startMark,
"expected ' ', but found" ~ to!string(reader_.peek()), "expected ' ', but found" ~ to!string(reader_.peek()),
reader_.mark)); reader_.mark));
@ -933,7 +935,7 @@ final class Scanner
{ {
findNextNonSpace(); findNextNonSpace();
if(reader_.peek() == '#'){scanToNextBreak();} if(reader_.peek() == '#'){scanToNextBreak();}
enforce(isBreakOrZero(reader_.peek()), enforce("\0\n\r\u0085\u2028\u2029".canFind(reader_.peek()),
new ScannerException("While scanning a directive", startMark, new ScannerException("While scanning a directive", startMark,
"expected comment or a line break, but found" "expected comment or a line break, but found"
~ to!string(reader_.peek()), reader_.mark)); ~ to!string(reader_.peek()), reader_.mark));
@ -962,7 +964,7 @@ final class Scanner
dstring value = i == '*' ? scanAlphaNumeric!("an alias")(startMark) dstring value = i == '*' ? scanAlphaNumeric!("an alias")(startMark)
: scanAlphaNumeric!("an anchor")(startMark); : scanAlphaNumeric!("an anchor")(startMark);
enforce((or!(isSpace, isBreakOrZero)(reader_.peek()) || enforce((" \t\0\n\r\u0085\u2028\u2029".canFind(reader_.peek()) ||
("?:,]}%@").canFind(reader_.peek())), ("?:,]}%@").canFind(reader_.peek())),
new ScannerException("While scanning an " ~ (i == '*') ? "alias" : "anchor", new ScannerException("While scanning an " ~ (i == '*') ? "alias" : "anchor",
startMark, "expected alphanumeric, - or _, but found "~ startMark, "expected alphanumeric, - or _, but found "~
@ -997,7 +999,7 @@ final class Scanner
to!string(reader_.peek()), reader_.mark)); to!string(reader_.peek()), reader_.mark));
reader_.forward(); reader_.forward();
} }
else if(or!(isSpace, isBreakOrZero)(c)) else if(" \t\0\n\r\u0085\u2028\u2029".canFind(c))
{ {
suffix = "!"; suffix = "!";
reader_.forward(); reader_.forward();
@ -1007,7 +1009,7 @@ final class Scanner
uint length = 1; uint length = 1;
bool useHandle = false; bool useHandle = false;
while(!or!(isChar!' ', isBreakOrZero)(c)) while(!" \0\n\r\u0085\u2028\u2029".canFind(c))
{ {
if(c == '!') if(c == '!')
{ {
@ -1028,7 +1030,7 @@ final class Scanner
suffix = scanTagURI("tag", startMark); suffix = scanTagURI("tag", startMark);
} }
enforce(or!(isChar!' ', isBreakOrZero)(reader_.peek()), enforce(" \0\n\r\u0085\u2028\u2029".canFind(reader_.peek()),
new ScannerException("While scanning a tag", startMark, new ScannerException("While scanning a tag", startMark,
"expected ' ' but found" ~ "expected ' ' but found" ~
to!string(reader_.peek()), reader_.mark)); to!string(reader_.peek()), reader_.mark));
@ -1076,7 +1078,7 @@ final class Scanner
while(reader_.column == indent && reader_.peek() != '\0') while(reader_.column == indent && reader_.peek() != '\0')
{ {
appender.put(breaks); appender.put(breaks);
const bool leadingNonSpace = !isSpace(reader_.peek()); const bool leadingNonSpace = !" \t".canFind(reader_.peek());
appender.put(scanToNextBreak()); appender.put(scanToNextBreak());
lineBreak = ""d ~ scanLineBreak(); lineBreak = ""d ~ scanLineBreak();
@ -1090,7 +1092,7 @@ final class Scanner
//This is the folding according to the specification: //This is the folding according to the specification:
if(style == ScalarStyle.Folded && lineBreak == "\n" && if(style == ScalarStyle.Folded && lineBreak == "\n" &&
leadingNonSpace && !isSpace(reader_.peek())) leadingNonSpace && !" \t".canFind(reader_.peek()))
{ {
if(breaks.length == 0){appender.put(' ');} if(breaks.length == 0){appender.put(' ');}
} }
@ -1151,7 +1153,7 @@ final class Scanner
if(getChomping()) {getIncrement();} if(getChomping()) {getIncrement();}
else if(getIncrement()){getChomping();} else if(getIncrement()){getChomping();}
enforce(or!(isBreakOrZero, isChar!' ')(c), enforce(" \0\n\r\u0085\u2028\u2029".canFind(c),
new ScannerException("While scanning a block scalar", startMark, new ScannerException("While scanning a block scalar", startMark,
"expected chomping or indentation indicator, " "expected chomping or indentation indicator, "
"but found " ~ to!string(c), reader_.mark)); "but found " ~ to!string(c), reader_.mark));
@ -1165,7 +1167,7 @@ final class Scanner
findNextNonSpace(); findNextNonSpace();
if(reader_.peek == '#'){scanToNextBreak();} if(reader_.peek == '#'){scanToNextBreak();}
enforce(isBreakOrZero(reader_.peek()), enforce("\0\n\r\u0085\u2028\u2029".canFind(reader_.peek()),
new ScannerException("While scanning a block scalar", startMark, new ScannerException("While scanning a block scalar", startMark,
"expected a comment or a line break, but found " "expected a comment or a line break, but found "
~ to!string(reader_.peek()), reader_.mark)); ~ to!string(reader_.peek()), reader_.mark));
@ -1179,7 +1181,7 @@ final class Scanner
uint maxIndent; uint maxIndent;
Mark endMark = reader_.mark; Mark endMark = reader_.mark;
while(or!(isBreak, isChar!' ')(reader_.peek())) while(" \n\r\u0085\u2028\u2029".canFind(reader_.peek()))
{ {
if(reader_.peek() != ' ') if(reader_.peek() != ' ')
{ {
@ -1203,7 +1205,7 @@ final class Scanner
for(;;) for(;;)
{ {
while(reader_.column < indent && reader_.peek() == ' '){reader_.forward();} while(reader_.column < indent && reader_.peek() == ' '){reader_.forward();}
if(!isBreak(reader_.peek())){break;} if(!"\n\r\u0085\u2028\u2029".canFind(reader_.peek())){break;}
chunks ~= scanLineBreak(); chunks ~= scanLineBreak();
endMark = reader_.mark; endMark = reader_.mark;
} }
@ -1260,7 +1262,7 @@ final class Scanner
{ {
dchar c = reader_.peek(); dchar c = reader_.peek();
uint length = 0; uint length = 0;
while(!(or!(isBreakOrZero, isSpace)(c) || "\'\"\\".canFind(c))) while(!(" \t\0\n\r\u0085\u2028\u2029".canFind(c) || "\'\"\\".canFind(c)))
{ {
++length; ++length;
c = reader_.peek(length); c = reader_.peek(length);
@ -1308,7 +1310,7 @@ final class Scanner
dstring hex = reader_.get(length); dstring hex = reader_.get(length);
result ~= cast(dchar)parse!int(hex, 16); result ~= cast(dchar)parse!int(hex, 16);
} }
else if(isBreak(c)) else if("\n\r\u0085\u2028\u2029".canFind(c))
{ {
scanLineBreak(); scanLineBreak();
result ~= scanFlowScalarBreaks(startMark); result ~= scanFlowScalarBreaks(startMark);
@ -1329,7 +1331,7 @@ final class Scanner
dstring scanFlowScalarSpaces(in Mark startMark) dstring scanFlowScalarSpaces(in Mark startMark)
{ {
uint length = 0; uint length = 0;
while(isSpace(reader_.peek(length))){++length;} while(" \t".canFind(reader_.peek(length))){++length;}
const whitespaces = reader_.get(length); const whitespaces = reader_.get(length);
dchar c = reader_.peek(); dchar c = reader_.peek();
@ -1338,7 +1340,7 @@ final class Scanner
"found unexpected end of stream", reader_.mark)); "found unexpected end of stream", reader_.mark));
auto appender = appender!dstring(); auto appender = appender!dstring();
if(isBreak(c)) if("\n\r\u0085\u2028\u2029".canFind(c))
{ {
const lineBreak = scanLineBreak(); const lineBreak = scanLineBreak();
const breaks = scanFlowScalarBreaks(startMark); const breaks = scanFlowScalarBreaks(startMark);
@ -1360,16 +1362,19 @@ final class Scanner
//Instead of checking indentation, we check for document separators. //Instead of checking indentation, we check for document separators.
const prefix = reader_.prefix(3); const prefix = reader_.prefix(3);
if((prefix == "---" || prefix == "...") && if((prefix == "---" || prefix == "...") &&
or!(isBreakOrZero, isSpace)(reader_.peek(3))) " \t\0\n\r\u0085\u2028\u2029".canFind(reader_.peek(3)))
{ {
throw new ScannerException("While scanning a quoted scalar", startMark, throw new ScannerException("While scanning a quoted scalar", startMark,
"found unexpected document separator", "found unexpected document separator",
reader_.mark); reader_.mark);
} }
while(isSpace(reader_.peek())){reader_.forward();} while(" \t".canFind(reader_.peek())){reader_.forward();}
if(isBreak(reader_.peek())){appender.put(scanLineBreak());} if("\n\r\u0085\u2028\u2029".canFind(reader_.peek()))
{
appender.put(scanLineBreak());
}
else{return appender.data;} else{return appender.data;}
} }
} }
@ -1398,9 +1403,9 @@ final class Scanner
for(;;) for(;;)
{ {
c = reader_.peek(length); c = reader_.peek(length);
bool done = or!(isBreakOrZero, isSpace)(c) || bool done = " \t\0\n\r\u0085\u2028\u2029".canFind(c) ||
(flowLevel_ == 0 && c == ':' && (flowLevel_ == 0 && c == ':' &&
or!(isBreakOrZero, isSpace)(reader_.peek(length + 1))) || " \t\0\n\r\u0085\u2028\u2029".canFind(reader_.peek(length + 1))) ||
(flowLevel_ > 0 && ",:?[]{}".canFind(c)); (flowLevel_ > 0 && ",:?[]{}".canFind(c));
if(done){break;} if(done){break;}
++length; ++length;
@ -1408,7 +1413,7 @@ final class Scanner
//It's not clear what we should do with ':' in the flow context. //It's not clear what we should do with ':' in the flow context.
if(flowLevel_ > 0 && c == ':' && if(flowLevel_ > 0 && c == ':' &&
!or!(isBreakOrZero, isSpace)(reader_.peek(length + 1)) && !" \t\0\n\r\u0085\u2028\u2029".canFind(reader_.peek(length + 1)) &&
!",[]{}".canFind(reader_.peek(length + 1))) !",[]{}".canFind(reader_.peek(length + 1)))
{ {
reader_.forward(length); reader_.forward(length);
@ -1448,7 +1453,7 @@ final class Scanner
dstring whitespaces = reader_.get(length); dstring whitespaces = reader_.get(length);
dchar c = reader_.peek(); dchar c = reader_.peek();
if(isBreak(c)) if("\n\r\u0085\u2028\u2029".canFind(c))
{ {
const lineBreak = scanLineBreak(); const lineBreak = scanLineBreak();
allowSimpleKey_ = true; allowSimpleKey_ = true;
@ -1456,13 +1461,13 @@ final class Scanner
bool end() bool end()
{ {
return ["---"d, "..."d].canFind(reader_.prefix(3)) && return ["---"d, "..."d].canFind(reader_.prefix(3)) &&
or!(isBreakOrZero, isSpace)(reader_.peek(3)); " \t\0\n\r\u0085\u2028\u2029".canFind(reader_.peek(3));
} }
if(end()){return "";} if(end()){return "";}
dstring breaks; dstring breaks;
while(or!(isBreak, isChar!' ')(reader_.peek())) while(" \n\r\u0085\u2028\u2029".canFind(reader_.peek()))
{ {
if(reader_.peek() == ' '){reader_.forward();} if(reader_.peek() == ' '){reader_.forward();}
else else

View file

@ -1,35 +0,0 @@
// Copyright Ferdinand Majerech 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
module dyaml.util;
package:
///Is given character YAML whitespace (space or tab)?
bool isSpace(in dchar c){return c == ' ' || c == '\t';}
///Is given character YAML line break?
bool isBreak(in dchar c)
{
return c == '\n' || c == '\r' || c == '\u0085' || c == '\u2028' || c == '\u2029';
}
///Is c the checked character?
bool isChar(dchar checked)(in dchar c){return checked == c;}
///Function that or's specified functions with a character input.
bool or(F ...)(in dchar input)
{
foreach(f; F)
{
if(f(input)){return true;}
}
return false;
}
///Convenience aliases.
alias isChar!'\0' isZero;
alias or!(isZero, isBreak) isBreakOrZero;