Merge pull request #107 from Herringway/event-checks

allow @safe access to anchor/tag/tagDirectives of events
merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
This commit is contained in:
The Dlang Bot 2018-04-20 10:42:24 +02:00 committed by GitHub
commit 15988fec57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 15 deletions

View file

@ -174,7 +174,7 @@ final class Composer
/// ///
/// Params: pairAppenderLevel = Current level of the pair appender stack. /// Params: pairAppenderLevel = Current level of the pair appender stack.
/// nodeAppenderLevel = Current level of the node appender stack. /// nodeAppenderLevel = Current level of the node appender stack.
Node composeNode(const uint pairAppenderLevel, const uint nodeAppenderLevel) @system Node composeNode(const uint pairAppenderLevel, const uint nodeAppenderLevel) @safe
{ {
if(parser_.checkEvent(EventID.Alias)) if(parser_.checkEvent(EventID.Alias))
{ {
@ -249,7 +249,7 @@ final class Composer
/// Params: pairAppenderLevel = Current level of the pair appender stack. /// Params: pairAppenderLevel = Current level of the pair appender stack.
/// nodeAppenderLevel = Current level of the node appender stack. /// nodeAppenderLevel = Current level of the node appender stack.
Node composeSequenceNode(const uint pairAppenderLevel, const uint nodeAppenderLevel) Node composeSequenceNode(const uint pairAppenderLevel, const uint nodeAppenderLevel)
@system @safe
{ {
ensureAppendersExist(pairAppenderLevel, nodeAppenderLevel); ensureAppendersExist(pairAppenderLevel, nodeAppenderLevel);
auto nodeAppender = &(nodeAppenders_[nodeAppenderLevel]); auto nodeAppender = &(nodeAppenders_[nodeAppenderLevel]);
@ -346,7 +346,7 @@ final class Composer
/// Params: pairAppenderLevel = Current level of the pair appender stack. /// Params: pairAppenderLevel = Current level of the pair appender stack.
/// nodeAppenderLevel = Current level of the node appender stack. /// nodeAppenderLevel = Current level of the node appender stack.
Node composeMappingNode(const uint pairAppenderLevel, const uint nodeAppenderLevel) Node composeMappingNode(const uint pairAppenderLevel, const uint nodeAppenderLevel)
@system @safe
{ {
ensureAppendersExist(pairAppenderLevel, nodeAppenderLevel); ensureAppendersExist(pairAppenderLevel, nodeAppenderLevel);
immutable startEvent = parser_.getEvent(); immutable startEvent = parser_.getEvent();

View file

@ -480,7 +480,7 @@ struct Emitter
} }
} }
///Handle an alias. ///Handle an alias.
void expectAlias() @trusted void expectAlias() @safe
{ {
enforce(event_.anchor !is null, new EmitterException("Anchor is not specified for alias")); enforce(event_.anchor !is null, new EmitterException("Anchor is not specified for alias"));
processAnchor("*"); processAnchor("*");
@ -681,7 +681,7 @@ struct Emitter
} }
///Check if an empty document is next. ///Check if an empty document is next.
bool checkEmptyDocument() const @trusted pure nothrow bool checkEmptyDocument() const @safe pure nothrow
{ {
if(event_.id != EventID.DocumentStart || events_.length == 0) if(event_.id != EventID.DocumentStart || events_.length == 0)
{ {
@ -695,7 +695,7 @@ struct Emitter
} }
///Check if a simple key is next. ///Check if a simple key is next.
bool checkSimpleKey() @trusted bool checkSimpleKey() @safe
{ {
uint length = 0; uint length = 0;
const id = event_.id; const id = event_.id;
@ -764,7 +764,7 @@ struct Emitter
} }
///Process and write an anchor/alias. ///Process and write an anchor/alias.
void processAnchor(const string indicator) @trusted void processAnchor(const string indicator) @safe
{ {
if(event_.anchor is null) if(event_.anchor is null)
{ {
@ -784,7 +784,7 @@ struct Emitter
} }
///Process and write a tag. ///Process and write a tag.
void processTag() @trusted void processTag() @safe
{ {
string tag = event_.tag; string tag = event_.tag;

View file

@ -58,13 +58,13 @@ struct Event
struct struct
{ {
///Anchor of the event, if any. ///Anchor of the event, if any.
string anchor; string _anchor;
///Tag of the event, if any. ///Tag of the event, if any.
string tag; string _tag;
} }
///Tag directives, if this is a DocumentStart. ///Tag directives, if this is a DocumentStart.
//TagDirectives tagDirectives; //TagDirectives tagDirectives;
TagDirective[] tagDirectives; TagDirective[] _tagDirectives;
} }
///Event type. ///Event type.
EventID id = EventID.Invalid; EventID id = EventID.Invalid;
@ -94,6 +94,21 @@ struct Event
///Get string representation of the token ID. ///Get string representation of the token ID.
@property string idString() const @safe {return to!string(id);} @property string idString() const @safe {return to!string(id);}
auto ref anchor() inout @trusted pure {
assert(id != EventID.DocumentStart, "DocumentStart events cannot have anchors.");
return _anchor;
}
auto ref tag() inout @trusted pure {
assert(id != EventID.DocumentStart, "DocumentStart events cannot have tags.");
return _tag;
}
auto ref tagDirectives() inout @trusted pure {
assert(id == EventID.DocumentStart, "Only DocumentStart events have tag directives.");
return _tagDirectives;
}
static assert(Event.sizeof <= 64, "Event struct larger than expected"); static assert(Event.sizeof <= 64, "Event struct larger than expected");
} }
@ -105,7 +120,7 @@ struct Event
* anchor = Anchor, if this is an alias event. * anchor = Anchor, if this is an alias event.
*/ */
Event event(EventID id)(const Mark start, const Mark end, const string anchor = null) Event event(EventID id)(const Mark start, const Mark end, const string anchor = null)
@trusted @safe
{ {
Event result; Event result;
result.startMark = start; result.startMark = start;
@ -127,7 +142,7 @@ Event event(EventID id)(const Mark start, const Mark end, const string anchor =
*/ */
Event collectionStartEvent(EventID id) Event collectionStartEvent(EventID id)
(const Mark start, const Mark end, const string anchor, const string tag, (const Mark start, const Mark end, const string anchor, const string tag,
const bool implicit, const CollectionStyle style) pure @trusted nothrow const bool implicit, const CollectionStyle style) pure @safe nothrow
{ {
static assert(id == EventID.SequenceStart || id == EventID.SequenceEnd || static assert(id == EventID.SequenceStart || id == EventID.SequenceEnd ||
id == EventID.MappingStart || id == EventID.MappingEnd); id == EventID.MappingStart || id == EventID.MappingEnd);
@ -180,7 +195,7 @@ alias collectionStartEvent!(EventID.MappingStart) mappingStartEvent;
* tagDirectives = Tag directives of the document. * tagDirectives = Tag directives of the document.
*/ */
Event documentStartEvent(const Mark start, const Mark end, const bool explicit, string YAMLVersion, Event documentStartEvent(const Mark start, const Mark end, const bool explicit, string YAMLVersion,
TagDirective[] tagDirectives) pure @trusted nothrow TagDirective[] tagDirectives) pure @safe nothrow
{ {
Event result; Event result;
result.value = YAMLVersion; result.value = YAMLVersion;
@ -220,7 +235,7 @@ Event documentEndEvent(const Mark start, const Mark end, const bool explicit) pu
/// style = Scalar style. /// style = Scalar style.
Event scalarEvent(const Mark start, const Mark end, const string anchor, const string tag, Event scalarEvent(const Mark start, const Mark end, const string anchor, const string tag,
const Tuple!(bool, bool) implicit, const string value, const Tuple!(bool, bool) implicit, const string value,
const ScalarStyle style = ScalarStyle.Invalid) @trusted pure nothrow @nogc const ScalarStyle style = ScalarStyle.Invalid) @safe pure nothrow @nogc
{ {
Event result; Event result;
result.value = value; result.value = value;