dyaml/source/dyaml/parser.d

959 lines
35 KiB
D
Raw Normal View History

2011-08-16 12:53:13 +00:00
// Copyright Ferdinand Majerech 2011-2014.
2011-08-16 12:53:13 +00:00
// 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)
/**
* YAML parser.
* Code based on PyYAML: http://www.pyyaml.org
*/
module dyaml.parser;
import std.algorithm;
2011-08-16 12:53:13 +00:00
import std.array;
import std.conv;
import std.exception;
import std.typecons;
2011-08-16 12:53:13 +00:00
import dyaml.event;
import dyaml.exception;
2011-08-16 12:53:13 +00:00
import dyaml.scanner;
import dyaml.style;
2011-08-16 12:53:13 +00:00
import dyaml.token;
import dyaml.tagdirective;
2011-08-16 12:53:13 +00:00
package:
/**
* The following YAML grammar is LL(1) and is parsed by a recursive descent
* parser.
2014-07-26 14:43:02 +00:00
*
2011-08-16 12:53:13 +00:00
* stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
* implicit_document ::= block_node DOCUMENT-END*
* explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
* block_node_or_indentless_sequence ::=
* ALIAS
* | properties (block_content | indentless_block_sequence)?
* | block_content
* | indentless_block_sequence
* block_node ::= ALIAS
* | properties block_content?
* | block_content
* flow_node ::= ALIAS
* | properties flow_content?
* | flow_content
* properties ::= TAG ANCHOR? | ANCHOR TAG?
* block_content ::= block_collection | flow_collection | SCALAR
* flow_content ::= flow_collection | SCALAR
* block_collection ::= block_sequence | block_mapping
* flow_collection ::= flow_sequence | flow_mapping
* block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
* indentless_sequence ::= (BLOCK-ENTRY block_node?)+
* block_mapping ::= BLOCK-MAPPING_START
* ((KEY block_node_or_indentless_sequence?)?
* (VALUE block_node_or_indentless_sequence?)?)*
* BLOCK-END
* flow_sequence ::= FLOW-SEQUENCE-START
* (flow_sequence_entry FLOW-ENTRY)*
* flow_sequence_entry?
* FLOW-SEQUENCE-END
* flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
* flow_mapping ::= FLOW-MAPPING-START
* (flow_mapping_entry FLOW-ENTRY)*
* flow_mapping_entry?
* FLOW-MAPPING-END
* flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
2014-07-26 14:43:02 +00:00
*
2011-08-16 12:53:13 +00:00
* FIRST sets:
2014-07-26 14:43:02 +00:00
*
2011-08-16 12:53:13 +00:00
* stream: { STREAM-START }
* explicit_document: { DIRECTIVE DOCUMENT-START }
* implicit_document: FIRST(block_node)
* block_node: { ALIAS TAG ANCHOR SCALAR BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START }
* flow_node: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START }
* block_content: { BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START SCALAR }
* flow_content: { FLOW-SEQUENCE-START FLOW-MAPPING-START SCALAR }
* block_collection: { BLOCK-SEQUENCE-START BLOCK-MAPPING-START }
* flow_collection: { FLOW-SEQUENCE-START FLOW-MAPPING-START }
* block_sequence: { BLOCK-SEQUENCE-START }
* block_mapping: { BLOCK-MAPPING-START }
* block_node_or_indentless_sequence: { ALIAS ANCHOR TAG SCALAR BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START BLOCK-ENTRY }
* indentless_sequence: { ENTRY }
* flow_collection: { FLOW-SEQUENCE-START FLOW-MAPPING-START }
* flow_sequence: { FLOW-SEQUENCE-START }
* flow_mapping: { FLOW-MAPPING-START }
* flow_sequence_entry: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START KEY }
* flow_mapping_entry: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START KEY }
2014-07-26 14:43:02 +00:00
*/
2011-08-16 12:53:13 +00:00
/**
* Marked exception thrown at parser errors.
*
* See_Also: MarkedYAMLException
*/
class ParserException : MarkedYAMLException
{
mixin MarkedExceptionCtors;
2011-08-16 12:53:13 +00:00
}
/// Generates events from tokens provided by a Scanner.
///
2018-04-10 01:02:30 +00:00
/// While Parser receives tokens with non-const character slices, the events it
/// produces are immutable strings, which are usually the same slices, cast to string.
/// Parser is the last layer of D:YAML that may possibly do any modifications to these
/// slices.
2014-07-26 14:43:02 +00:00
final class Parser
2011-08-16 12:53:13 +00:00
{
private:
///Default tag handle shortcuts and replacements.
2018-04-10 01:02:30 +00:00
static TagDirective[] defaultTagDirectives_ =
[TagDirective("!", "!"), TagDirective("!!", "tag:yaml.org,2002:")];
2011-08-16 12:53:13 +00:00
///Scanner providing YAML tokens.
Scanner scanner_;
///Event produced by the most recent state.
Event currentEvent_;
2011-08-16 12:53:13 +00:00
///YAML version string.
string YAMLVersion_ = null;
///Tag handle shortcuts and replacements.
TagDirective[] tagDirectives_;
2011-08-16 12:53:13 +00:00
///Stack of states.
2018-06-16 05:00:40 +00:00
Appender!(Event delegate() @safe[]) states_;
2011-08-16 12:53:13 +00:00
///Stack of marks used to keep track of extents of e.g. YAML collections.
2018-06-16 05:00:40 +00:00
Appender!(Mark[]) marks_;
2011-08-16 12:53:13 +00:00
///Current state.
2018-03-23 21:35:16 +00:00
Event delegate() @safe state_;
2011-08-16 12:53:13 +00:00
public:
///Construct a Parser using specified Scanner.
2018-06-16 05:00:40 +00:00
this(Scanner scanner) @safe
2011-08-16 12:53:13 +00:00
{
state_ = &parseStreamStart;
scanner_ = scanner;
states_.reserve(32);
marks_.reserve(32);
2011-08-16 12:53:13 +00:00
}
/**
* Check if any events are left. May have side effects in some cases.
2011-08-16 12:53:13 +00:00
*/
bool empty() @safe
2011-08-16 12:53:13 +00:00
{
ensureState();
return currentEvent_.isNull;
2011-08-16 12:53:13 +00:00
}
/**
* Return the current event.
2011-08-16 12:53:13 +00:00
*
* Must not be called if there are no events left.
*/
Event front() @safe
2011-08-16 12:53:13 +00:00
{
ensureState();
assert(!currentEvent_.isNull, "No event left to peek");
return currentEvent_;
2011-08-16 12:53:13 +00:00
}
/**
* Skip to the next event.
2011-08-16 12:53:13 +00:00
*
* Must not be called if there are no events left.
*/
void popFront() @safe
{
2018-08-27 00:49:14 +00:00
currentEvent_.id = EventID.invalid;
ensureState();
}
private:
/// If current event is invalid, load the next valid one if possible.
void ensureState() @safe
2011-08-16 12:53:13 +00:00
{
if(currentEvent_.isNull && state_ !is null)
2011-08-16 12:53:13 +00:00
{
currentEvent_ = state_();
2011-08-16 12:53:13 +00:00
}
}
///Pop and return the newest state in states_.
2018-06-16 05:00:40 +00:00
Event delegate() @safe popState() @safe
2011-08-16 12:53:13 +00:00
{
2018-06-16 05:00:40 +00:00
enforce(states_.data.length > 0,
new YAMLException("Parser: Need to pop state but no states left to pop"));
2018-06-16 05:00:40 +00:00
const result = states_.data.back;
states_.shrinkTo(states_.data.length - 1);
2011-08-16 12:53:13 +00:00
return result;
}
///Pop and return the newest mark in marks_.
2018-06-16 05:00:40 +00:00
Mark popMark() @safe
2011-08-16 12:53:13 +00:00
{
2018-06-16 05:00:40 +00:00
enforce(marks_.data.length > 0,
new YAMLException("Parser: Need to pop mark but no marks left to pop"));
2018-06-16 05:00:40 +00:00
const result = marks_.data.back;
marks_.shrinkTo(marks_.data.length - 1);
2011-08-16 12:53:13 +00:00
return result;
}
/// Push a state on the stack
2018-06-16 05:00:40 +00:00
void pushState(Event delegate() @safe state) @safe
{
states_ ~= state;
}
/// Push a mark on the stack
2018-06-16 05:00:40 +00:00
void pushMark(Mark mark) @safe
{
marks_ ~= mark;
}
2011-08-16 12:53:13 +00:00
/**
* stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
* implicit_document ::= block_node DOCUMENT-END*
* explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
*/
///Parse stream start.
Event parseStreamStart() @safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2011-08-16 12:53:13 +00:00
state_ = &parseImplicitDocumentStart;
return streamStartEvent(token.startMark, token.endMark);
2011-08-16 12:53:13 +00:00
}
2014-07-26 14:43:02 +00:00
/// Parse implicit document start, unless explicit detected: if so, parse explicit.
Event parseImplicitDocumentStart() @safe
2011-08-16 12:53:13 +00:00
{
2014-07-26 14:43:02 +00:00
// Parse an implicit document.
2019-01-24 06:53:40 +00:00
if(!scanner_.front.id.among!(TokenID.directive, TokenID.documentStart,
2018-08-27 00:49:14 +00:00
TokenID.streamEnd))
2011-08-16 12:53:13 +00:00
{
2014-07-26 14:43:02 +00:00
tagDirectives_ = defaultTagDirectives_;
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
2011-08-16 12:53:13 +00:00
pushState(&parseDocumentEnd);
2011-08-16 12:53:13 +00:00
state_ = &parseBlockNode;
2014-07-26 14:43:02 +00:00
return documentStartEvent(token.startMark, token.endMark, false, null, null);
2011-08-16 12:53:13 +00:00
}
return parseDocumentStart();
}
///Parse explicit document start.
Event parseDocumentStart() @safe
2011-08-16 12:53:13 +00:00
{
//Parse any extra document end indicators.
2019-01-24 06:53:40 +00:00
while(scanner_.front.id == TokenID.documentEnd)
{
scanner_.popFront();
}
2011-08-16 12:53:13 +00:00
//Parse an explicit document.
2019-01-24 06:53:40 +00:00
if(scanner_.front.id != TokenID.streamEnd)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const startMark = scanner_.front.startMark;
2011-08-16 12:53:13 +00:00
auto tagDirectives = processDirectives();
2019-01-24 06:53:40 +00:00
enforce(scanner_.front.id == TokenID.documentStart,
new ParserException("Expected document start but found " ~
2019-01-24 06:53:40 +00:00
scanner_.front.idString,
scanner_.front.startMark));
2011-08-16 12:53:13 +00:00
2019-01-24 06:53:40 +00:00
const endMark = scanner_.front.endMark;
scanner_.popFront();
pushState(&parseDocumentEnd);
2011-08-16 12:53:13 +00:00
state_ = &parseDocumentContent;
return documentStartEvent(startMark, endMark, true, YAMLVersion_, tagDirectives);
2011-08-16 12:53:13 +00:00
}
else
{
//Parse the end of the stream.
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2018-06-16 05:00:40 +00:00
assert(states_.data.length == 0);
assert(marks_.data.length == 0);
2011-08-16 12:53:13 +00:00
state_ = null;
return streamEndEvent(token.startMark, token.endMark);
}
}
///Parse document end (explicit or implicit).
Event parseDocumentEnd() @safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
Mark startMark = scanner_.front.startMark;
const bool explicit = scanner_.front.id == TokenID.documentEnd;
Mark endMark = startMark;
if (explicit)
{
endMark = scanner_.front.endMark;
scanner_.popFront();
}
2011-08-16 12:53:13 +00:00
state_ = &parseDocumentStart;
return documentEndEvent(startMark, endMark, explicit);
}
///Parse document content.
Event parseDocumentContent() @safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
if(scanner_.front.id.among!(TokenID.directive, TokenID.documentStart,
2018-08-27 00:49:14 +00:00
TokenID.documentEnd, TokenID.streamEnd))
2011-08-16 12:53:13 +00:00
{
state_ = popState();
2019-01-24 06:53:40 +00:00
return processEmptyScalar(scanner_.front.startMark);
2011-08-16 12:53:13 +00:00
}
return parseBlockNode();
}
2014-07-26 14:43:02 +00:00
/// Process directives at the beginning of a document.
TagDirective[] processDirectives() @safe
2011-08-16 12:53:13 +00:00
{
2014-07-26 14:43:02 +00:00
// Destroy version and tag handles from previous document.
2011-08-16 12:53:13 +00:00
YAMLVersion_ = null;
tagDirectives_.length = 0;
2011-08-16 12:53:13 +00:00
2014-07-26 14:43:02 +00:00
// Process directives.
2019-01-24 06:53:40 +00:00
while(scanner_.front.id == TokenID.directive)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2018-03-23 21:35:16 +00:00
string value = token.value.idup;
2018-08-27 00:49:14 +00:00
if(token.directive == DirectiveType.yaml)
2011-08-16 12:53:13 +00:00
{
enforce(YAMLVersion_ is null,
new ParserException("Duplicate YAML directive", token.startMark));
const minor = value.split(".")[0];
enforce(minor == "1",
new ParserException("Incompatible document (version 1.x is required)",
token.startMark));
2018-03-23 21:35:16 +00:00
YAMLVersion_ = value;
2011-08-16 12:53:13 +00:00
}
2018-08-27 00:49:14 +00:00
else if(token.directive == DirectiveType.tag)
2011-08-16 12:53:13 +00:00
{
2018-03-23 21:35:16 +00:00
auto handle = value[0 .. token.valueDivider];
2011-08-16 12:53:13 +00:00
foreach(ref pair; tagDirectives_)
2011-08-16 12:53:13 +00:00
{
2014-07-26 14:43:02 +00:00
// handle
const h = pair.handle;
enforce(h != handle, new ParserException("Duplicate tag handle: " ~ handle,
token.startMark));
2011-08-16 12:53:13 +00:00
}
2018-03-23 21:35:16 +00:00
tagDirectives_ ~=
TagDirective(handle, value[token.valueDivider .. $]);
2011-08-16 12:53:13 +00:00
}
// Any other directive type is ignored (only YAML and TAG are in YAML
// 1.1/1.2, any other directives are "reserved")
2011-08-16 12:53:13 +00:00
}
TagDirective[] value = tagDirectives_;
2011-08-16 12:53:13 +00:00
//Add any default tag handles that haven't been overridden.
foreach(ref defaultPair; defaultTagDirectives_)
2011-08-16 12:53:13 +00:00
{
bool found;
foreach(ref pair; tagDirectives_) if(defaultPair.handle == pair.handle)
{
found = true;
break;
}
2014-07-26 14:43:02 +00:00
if(!found) {tagDirectives_ ~= defaultPair; }
2011-08-16 12:53:13 +00:00
}
return value;
2011-08-16 12:53:13 +00:00
}
/**
* block_node_or_indentless_sequence ::= ALIAS
* | properties (block_content | indentless_block_sequence)?
* | block_content
* | indentless_block_sequence
* block_node ::= ALIAS
* | properties block_content?
* | block_content
* flow_node ::= ALIAS
* | properties flow_content?
* | flow_content
* properties ::= TAG ANCHOR? | ANCHOR TAG?
* block_content ::= block_collection | flow_collection | SCALAR
* flow_content ::= flow_collection | SCALAR
* block_collection ::= block_sequence | block_mapping
* flow_collection ::= flow_sequence | flow_mapping
*/
///Parse a node.
Event parseNode(const Flag!"block" block,
const Flag!"indentlessSequence" indentlessSequence = No.indentlessSequence)
@trusted
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.alias_)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2011-08-16 12:53:13 +00:00
state_ = popState();
return aliasEvent(token.startMark, token.endMark,
cast(string)token.value);
2011-08-16 12:53:13 +00:00
}
string anchor;
string tag;
2011-08-16 12:53:13 +00:00
Mark startMark, endMark, tagMark;
bool invalidMarks = true;
// The index in the tag string where tag handle ends and tag suffix starts.
uint tagHandleEnd;
2011-08-16 12:53:13 +00:00
//Get anchor/tag if detected. Return false otherwise.
bool get(const TokenID id, const Flag!"first" first, ref string target) @safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
if(scanner_.front.id != id){return false;}
2011-08-16 12:53:13 +00:00
invalidMarks = false;
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
if(first){startMark = token.startMark;}
2018-08-27 00:49:14 +00:00
if(id == TokenID.tag)
{
tagMark = token.startMark;
tagHandleEnd = token.valueDivider;
}
2014-07-26 14:43:02 +00:00
endMark = token.endMark;
target = token.value.idup;
2011-08-16 12:53:13 +00:00
return true;
}
//Anchor and/or tag can be in any order.
2018-08-27 00:49:14 +00:00
if(get(TokenID.anchor, Yes.first, anchor)){get(TokenID.tag, No.first, tag);}
else if(get(TokenID.tag, Yes.first, tag)) {get(TokenID.anchor, No.first, anchor);}
2011-08-16 12:53:13 +00:00
if(tag !is null){tag = processTag(tag, tagHandleEnd, startMark, tagMark);}
2011-08-16 12:53:13 +00:00
if(invalidMarks)
{
2019-01-24 06:53:40 +00:00
startMark = endMark = scanner_.front.startMark;
2011-08-16 12:53:13 +00:00
}
bool implicit = (tag is null || tag == "!");
2019-01-24 06:53:40 +00:00
if(indentlessSequence && scanner_.front.id == TokenID.blockEntry)
2011-08-16 12:53:13 +00:00
{
state_ = &parseIndentlessSequenceEntry;
return sequenceStartEvent
2019-01-24 06:53:40 +00:00
(startMark, scanner_.front.endMark, anchor,
2018-08-27 00:49:14 +00:00
tag, implicit, CollectionStyle.block);
2011-08-16 12:53:13 +00:00
}
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.scalar)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
auto token = scanner_.front;
scanner_.popFront();
2018-08-27 00:49:14 +00:00
auto value = token.style == ScalarStyle.doubleQuoted
? handleDoubleQuotedScalarEscapes(token.value)
: cast(string)token.value;
2011-08-16 12:53:13 +00:00
2018-08-27 00:49:14 +00:00
implicit = (token.style == ScalarStyle.plain && tag is null) || tag == "!";
2011-08-16 12:53:13 +00:00
state_ = popState();
return scalarEvent(startMark, token.endMark, anchor, tag,
implicit, value, token.style);
2011-08-16 12:53:13 +00:00
}
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.flowSequenceStart)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
endMark = scanner_.front.endMark;
state_ = &parseFlowSequenceEntry!(Yes.first);
return sequenceStartEvent(startMark, endMark, anchor, tag,
2018-08-27 00:49:14 +00:00
implicit, CollectionStyle.flow);
2011-08-16 12:53:13 +00:00
}
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.flowMappingStart)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
endMark = scanner_.front.endMark;
state_ = &parseFlowMappingKey!(Yes.first);
return mappingStartEvent(startMark, endMark, anchor, tag,
2018-08-27 00:49:14 +00:00
implicit, CollectionStyle.flow);
2011-08-16 12:53:13 +00:00
}
2019-01-24 06:53:40 +00:00
if(block && scanner_.front.id == TokenID.blockSequenceStart)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
endMark = scanner_.front.endMark;
state_ = &parseBlockSequenceEntry!(Yes.first);
return sequenceStartEvent(startMark, endMark, anchor, tag,
2018-08-27 00:49:14 +00:00
implicit, CollectionStyle.block);
2011-08-16 12:53:13 +00:00
}
2019-01-24 06:53:40 +00:00
if(block && scanner_.front.id == TokenID.blockMappingStart)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
endMark = scanner_.front.endMark;
state_ = &parseBlockMappingKey!(Yes.first);
return mappingStartEvent(startMark, endMark, anchor, tag,
2018-08-27 00:49:14 +00:00
implicit, CollectionStyle.block);
2011-08-16 12:53:13 +00:00
}
if(anchor !is null || tag !is null)
2011-08-16 12:53:13 +00:00
{
state_ = popState();
2014-07-26 14:43:02 +00:00
//PyYAML uses a tuple(implicit, false) for the second last arg here,
2011-08-16 12:53:13 +00:00
//but the second bool is never used after that - so we don't use it.
//Empty scalars are allowed even if a tag or an anchor is specified.
return scalarEvent(startMark, endMark, anchor, tag,
implicit , "");
2011-08-16 12:53:13 +00:00
}
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
throw new ParserException("While parsing a " ~ (block ? "block" : "flow") ~ " node",
2014-07-26 14:43:02 +00:00
startMark, "expected node content, but found: "
~ token.idString, token.startMark);
2011-08-16 12:53:13 +00:00
}
/// Handle escape sequences in a double quoted scalar.
///
/// Moved here from scanner as it can't always be done in-place with slices.
string handleDoubleQuotedScalarEscapes(const(char)[] tokenValue) const @safe
{
string notInPlace;
bool inEscape;
auto appender = appender!(string)();
for(const(char)[] oldValue = tokenValue; !oldValue.empty();)
{
const dchar c = oldValue.front();
oldValue.popFront();
if(!inEscape)
{
if(c != '\\')
{
if(notInPlace is null) { appender.put(c); }
else { notInPlace ~= c; }
continue;
}
// Escape sequence starts with a '\'
inEscape = true;
continue;
}
import dyaml.escapes;
scope(exit) { inEscape = false; }
// 'Normal' escape sequence.
if(c.among!(escapes))
{
if(notInPlace is null)
{
// \L and \C can't be handled in place as the expand into
// many-byte unicode chars
if(c != 'L' && c != 'P')
{
appender.put(dyaml.escapes.fromEscape(c));
continue;
}
// Need to duplicate as we won't fit into
// token.value - which is what appender uses
notInPlace = appender.data.dup;
notInPlace ~= dyaml.escapes.fromEscape(c);
continue;
}
notInPlace ~= dyaml.escapes.fromEscape(c);
continue;
}
// Unicode char written in hexadecimal in an escape sequence.
if(c.among!(escapeHexCodeList))
{
// Scanner has already checked that the hex string is valid.
const hexLength = dyaml.escapes.escapeHexLength(c);
// Any hex digits are 1-byte so this works.
const(char)[] hex = oldValue[0 .. hexLength];
oldValue = oldValue[hexLength .. $];
2018-01-18 00:54:33 +00:00
import std.ascii : isHexDigit;
assert(!hex.canFind!(d => !d.isHexDigit),
"Scanner must ensure the hex string is valid");
const decoded = cast(dchar)parse!int(hex, 16u);
if(notInPlace is null) { appender.put(decoded); }
else { notInPlace ~= decoded; }
continue;
}
assert(false, "Scanner must handle unsupported escapes");
}
return notInPlace is null ? appender.data : notInPlace;
}
2011-08-16 12:53:13 +00:00
/**
* Process a tag string retrieved from a tag token.
*
* Params: tag = Tag before processing.
* handleEnd = Index in tag where tag handle ends and tag suffix
* starts.
2011-08-16 12:53:13 +00:00
* startMark = Position of the node the tag belongs to.
* tagMark = Position of the tag.
2014-07-26 14:43:02 +00:00
*/
string processTag(const string tag, const uint handleEnd,
const Mark startMark, const Mark tagMark)
2018-03-23 21:35:16 +00:00
const @safe
2011-08-16 12:53:13 +00:00
{
const handle = tag[0 .. handleEnd];
const suffix = tag[handleEnd .. $];
2011-08-16 12:53:13 +00:00
if(handle.length > 0)
{
string replacement;
foreach(ref pair; tagDirectives_)
{
if(pair.handle == handle)
{
replacement = pair.prefix;
break;
}
}
//handle must be in tagDirectives_
enforce(replacement !is null,
new ParserException("While parsing a node", startMark,
"found undefined tag handle: " ~ handle, tagMark));
return replacement ~ suffix;
2011-08-16 12:53:13 +00:00
}
return suffix;
}
///Wrappers to parse nodes.
Event parseBlockNode() @safe {return parseNode(Yes.block);}
Event parseFlowNode() @safe {return parseNode(No.block);}
Event parseBlockNodeOrIndentlessSequence() @safe {return parseNode(Yes.block, Yes.indentlessSequence);}
2011-08-16 12:53:13 +00:00
///block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
///Parse an entry of a block sequence. If first is true, this is the first entry.
Event parseBlockSequenceEntry(Flag!"first" first)() @safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
static if(first)
{
pushMark(scanner_.front.startMark);
scanner_.popFront();
}
2011-08-16 12:53:13 +00:00
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.blockEntry)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
if(!scanner_.front.id.among!(TokenID.blockEntry, TokenID.blockEnd))
2011-08-16 12:53:13 +00:00
{
pushState(&parseBlockSequenceEntry!(No.first));
2011-08-16 12:53:13 +00:00
return parseBlockNode();
}
state_ = &parseBlockSequenceEntry!(No.first);
2011-08-16 12:53:13 +00:00
return processEmptyScalar(token.endMark);
}
2019-01-24 06:53:40 +00:00
if(scanner_.front.id != TokenID.blockEnd)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
2018-06-16 05:00:40 +00:00
throw new ParserException("While parsing a block collection", marks_.data.back,
2014-07-26 14:43:02 +00:00
"expected block end, but found " ~ token.idString,
token.startMark);
2011-08-16 12:53:13 +00:00
}
state_ = popState();
popMark();
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2011-08-16 12:53:13 +00:00
return sequenceEndEvent(token.startMark, token.endMark);
}
///indentless_sequence ::= (BLOCK-ENTRY block_node?)+
///Parse an entry of an indentless sequence.
Event parseIndentlessSequenceEntry() @safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.blockEntry)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2011-08-16 12:53:13 +00:00
2019-01-24 06:53:40 +00:00
if(!scanner_.front.id.among!(TokenID.blockEntry, TokenID.key,
2018-08-27 00:49:14 +00:00
TokenID.value, TokenID.blockEnd))
2014-07-26 14:43:02 +00:00
{
pushState(&parseIndentlessSequenceEntry);
2011-08-16 12:53:13 +00:00
return parseBlockNode();
}
state_ = &parseIndentlessSequenceEntry;
return processEmptyScalar(token.endMark);
}
state_ = popState();
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
2011-08-16 12:53:13 +00:00
return sequenceEndEvent(token.startMark, token.endMark);
}
/**
* block_mapping ::= BLOCK-MAPPING_START
* ((KEY block_node_or_indentless_sequence?)?
* (VALUE block_node_or_indentless_sequence?)?)*
* BLOCK-END
*/
///Parse a key in a block mapping. If first is true, this is the first key.
Event parseBlockMappingKey(Flag!"first" first)() @safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
static if(first)
{
pushMark(scanner_.front.startMark);
scanner_.popFront();
}
2011-08-16 12:53:13 +00:00
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.key)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2011-08-16 12:53:13 +00:00
2019-01-24 06:53:40 +00:00
if(!scanner_.front.id.among!(TokenID.key, TokenID.value, TokenID.blockEnd))
2011-08-16 12:53:13 +00:00
{
pushState(&parseBlockMappingValue);
2011-08-16 12:53:13 +00:00
return parseBlockNodeOrIndentlessSequence();
}
state_ = &parseBlockMappingValue;
return processEmptyScalar(token.endMark);
}
2019-01-24 06:53:40 +00:00
if(scanner_.front.id != TokenID.blockEnd)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
2018-06-16 05:00:40 +00:00
throw new ParserException("While parsing a block mapping", marks_.data.back,
2014-07-26 14:43:02 +00:00
"expected block end, but found: " ~ token.idString,
token.startMark);
2011-08-16 12:53:13 +00:00
}
state_ = popState();
popMark();
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2011-08-16 12:53:13 +00:00
return mappingEndEvent(token.startMark, token.endMark);
}
///Parse a value in a block mapping.
Event parseBlockMappingValue() @safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.value)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2011-08-16 12:53:13 +00:00
2019-01-24 06:53:40 +00:00
if(!scanner_.front.id.among!(TokenID.key, TokenID.value, TokenID.blockEnd))
2011-08-16 12:53:13 +00:00
{
pushState(&parseBlockMappingKey!(No.first));
2011-08-16 12:53:13 +00:00
return parseBlockNodeOrIndentlessSequence();
}
state_ = &parseBlockMappingKey!(No.first);
2011-08-16 12:53:13 +00:00
return processEmptyScalar(token.endMark);
}
state_= &parseBlockMappingKey!(No.first);
2019-01-24 06:53:40 +00:00
return processEmptyScalar(scanner_.front.startMark);
2011-08-16 12:53:13 +00:00
}
/**
* flow_sequence ::= FLOW-SEQUENCE-START
* (flow_sequence_entry FLOW-ENTRY)*
* flow_sequence_entry?
* FLOW-SEQUENCE-END
* flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
2014-07-26 14:43:02 +00:00
*
2011-08-16 12:53:13 +00:00
* Note that while production rules for both flow_sequence_entry and
* flow_mapping_entry are equal, their interpretations are different.
* For `flow_sequence_entry`, the part `KEY flow_node? (VALUE flow_node?)?`
* generate an inline mapping (set syntax).
*/
///Parse an entry in a flow sequence. If first is true, this is the first entry.
Event parseFlowSequenceEntry(Flag!"first" first)() @safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
static if(first)
{
pushMark(scanner_.front.startMark);
scanner_.popFront();
}
2011-08-16 12:53:13 +00:00
2019-01-24 06:53:40 +00:00
if(scanner_.front.id != TokenID.flowSequenceEnd)
2011-08-16 12:53:13 +00:00
{
static if(!first)
{
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.flowEntry)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
scanner_.popFront();
2011-08-16 12:53:13 +00:00
}
else
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
2018-06-16 05:00:40 +00:00
throw new ParserException("While parsing a flow sequence", marks_.data.back,
"expected ',' or ']', but got: " ~
token.idString, token.startMark);
2011-08-16 12:53:13 +00:00
}
}
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.key)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
2011-08-16 12:53:13 +00:00
state_ = &parseFlowSequenceEntryMappingKey;
2014-07-26 14:43:02 +00:00
return mappingStartEvent(token.startMark, token.endMark,
2018-08-27 00:49:14 +00:00
null, null, true, CollectionStyle.flow);
2011-08-16 12:53:13 +00:00
}
2019-01-24 06:53:40 +00:00
else if(scanner_.front.id != TokenID.flowSequenceEnd)
2011-08-16 12:53:13 +00:00
{
pushState(&parseFlowSequenceEntry!(No.first));
2011-08-16 12:53:13 +00:00
return parseFlowNode();
}
}
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2011-08-16 12:53:13 +00:00
state_ = popState();
popMark();
return sequenceEndEvent(token.startMark, token.endMark);
}
///Parse a key in flow context.
Event parseFlowKey(Event delegate() @safe nextState) @safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2011-08-16 12:53:13 +00:00
2019-01-24 06:53:40 +00:00
if(!scanner_.front.id.among!(TokenID.value, TokenID.flowEntry,
2018-08-27 00:49:14 +00:00
TokenID.flowSequenceEnd))
2011-08-16 12:53:13 +00:00
{
pushState(nextState);
2011-08-16 12:53:13 +00:00
return parseFlowNode();
}
state_ = nextState;
return processEmptyScalar(token.endMark);
}
///Parse a mapping key in an entry in a flow sequence.
Event parseFlowSequenceEntryMappingKey() @safe
2011-08-16 12:53:13 +00:00
{
return parseFlowKey(&parseFlowSequenceEntryMappingValue);
}
///Parse a mapping value in a flow context.
Event parseFlowValue(TokenID checkId, Event delegate() @safe nextState)
@safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.value)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
if(!scanner_.front.id.among(TokenID.flowEntry, checkId))
2011-08-16 12:53:13 +00:00
{
pushState(nextState);
2011-08-16 12:53:13 +00:00
return parseFlowNode();
}
2014-07-26 14:43:02 +00:00
2011-08-16 12:53:13 +00:00
state_ = nextState;
return processEmptyScalar(token.endMark);
}
state_ = nextState;
2019-01-24 06:53:40 +00:00
return processEmptyScalar(scanner_.front.startMark);
2011-08-16 12:53:13 +00:00
}
///Parse a mapping value in an entry in a flow sequence.
Event parseFlowSequenceEntryMappingValue() @safe
2011-08-16 12:53:13 +00:00
{
2018-08-27 00:49:14 +00:00
return parseFlowValue(TokenID.flowSequenceEnd,
2011-08-16 12:53:13 +00:00
&parseFlowSequenceEntryMappingEnd);
}
///Parse end of a mapping in a flow sequence entry.
Event parseFlowSequenceEntryMappingEnd() @safe
2011-08-16 12:53:13 +00:00
{
state_ = &parseFlowSequenceEntry!(No.first);
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
2011-08-16 12:53:13 +00:00
return mappingEndEvent(token.startMark, token.startMark);
}
/**
* flow_mapping ::= FLOW-MAPPING-START
* (flow_mapping_entry FLOW-ENTRY)*
* flow_mapping_entry?
* FLOW-MAPPING-END
* flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
*/
///Parse a key in a flow mapping.
Event parseFlowMappingKey(Flag!"first" first)() @safe
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
static if(first)
{
pushMark(scanner_.front.startMark);
scanner_.popFront();
}
2011-08-16 12:53:13 +00:00
2019-01-24 06:53:40 +00:00
if(scanner_.front.id != TokenID.flowMappingEnd)
2011-08-16 12:53:13 +00:00
{
static if(!first)
{
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.flowEntry)
2011-08-16 12:53:13 +00:00
{
2019-01-24 06:53:40 +00:00
scanner_.popFront();
2011-08-16 12:53:13 +00:00
}
else
{
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
2018-06-16 05:00:40 +00:00
throw new ParserException("While parsing a flow mapping", marks_.data.back,
"expected ',' or '}', but got: " ~
token.idString, token.startMark);
2011-08-16 12:53:13 +00:00
}
}
2019-01-24 06:53:40 +00:00
if(scanner_.front.id == TokenID.key)
2011-08-16 12:53:13 +00:00
{
return parseFlowKey(&parseFlowMappingValue);
}
2019-01-24 06:53:40 +00:00
if(scanner_.front.id != TokenID.flowMappingEnd)
2011-08-16 12:53:13 +00:00
{
pushState(&parseFlowMappingEmptyValue);
2011-08-16 12:53:13 +00:00
return parseFlowNode();
}
}
2019-01-24 06:53:40 +00:00
const token = scanner_.front;
scanner_.popFront();
2011-08-16 12:53:13 +00:00
state_ = popState();
popMark();
return mappingEndEvent(token.startMark, token.endMark);
}
///Parse a value in a flow mapping.
Event parseFlowMappingValue() @safe
2011-08-16 12:53:13 +00:00
{
2018-08-27 00:49:14 +00:00
return parseFlowValue(TokenID.flowMappingEnd, &parseFlowMappingKey!(No.first));
2011-08-16 12:53:13 +00:00
}
///Parse an empty value in a flow mapping.
Event parseFlowMappingEmptyValue() @safe
2011-08-16 12:53:13 +00:00
{
state_ = &parseFlowMappingKey!(No.first);
2019-01-24 06:53:40 +00:00
return processEmptyScalar(scanner_.front.startMark);
2011-08-16 12:53:13 +00:00
}
///Return an empty scalar.
2014-07-30 23:56:36 +00:00
Event processEmptyScalar(const Mark mark) @safe pure nothrow const @nogc
2011-08-16 12:53:13 +00:00
{
return scalarEvent(mark, mark, null, null, true, "");
2011-08-16 12:53:13 +00:00
}
}