/// <summary> /// Emits a mapping key. /// </summary> /// <param name="event">A <see cref="MappingKey"/> event.</param> private void EmitMappingKey(HclEvent @event) { var key = GetTypedEvent <MappingKey>(@event); if (EmitLifecycle.ContainsKey(this.currentResourceType) && EmitLifecycle[this.currentResourceType].Contains(key.Path)) { this.lifecycleKeys.Add(key.Path); } // Don't push path for repeating block key if (!key.IsBlockKey) { if (!this.isJson) { this.currentPath = key.Path; } this.WriteIndent(); } else { // ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault switch (key.InitialAnalysis) { case AttributeContent.BlockList: this.blockKeys.Push(key); this.currentBlockKey = key; this.PushState(this.state); this.state = EmitterState.BlockList; break; case AttributeContent.BlockObject: this.state = EmitterState.BlockObject; break; default: this.state = EmitterState.Mapping; break; } this.WriteBreak(); this.WriteIndent(); } this.EmitScalar(@event); if (this.state == EmitterState.Mapping) { this.WriteIndicator("=", true, false, false); } }
/// <summary> /// Emits a policy start. /// </summary> /// <param name="event">The event.</param> private void EmitJsonStart(HclEvent @event) { GetTypedEvent <JsonStart>(@event); this.isJson = true; this.PushState(this.state); this.state = EmitterState.Json; this.WriteIndicator("jsonencode(", true, false, true); this.IncreaseIndent(); this.WriteIndent(); }
/// <summary> /// Emits a policy end. /// </summary> /// <param name="event">The event.</param> private void EmitJsonEnd(HclEvent @event) { GetTypedEvent <JsonEnd>(@event); this.isJson = false; this.indent = this.indents.Pop(); this.state = this.PopState(); this.WriteIndent(); this.WriteIndicator(")", false, false, false); if (this.state == EmitterState.Sequence) { this.Write(','); } }
/// <summary> /// Emits a mapping end. /// </summary> /// <param name="event">The event.</param> private void EmitMappingEnd(HclEvent @event) { GetTypedEvent <MappingEnd>(@event); if (this.state != EmitterState.Resource) { this.state = this.PopState(); } this.indent = this.indents.Pop(); this.WriteIndent(); this.WriteIndicator("}", false, false, true); var mappingStart = this.events.Peek() as MappingStart; // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault switch (this.state) { case EmitterState.BlockList when mappingStart != null: // Next element in a block list this.WriteIndent(); this.indent = this.indents.Pop(); this.EmitMappingKey(this.currentBlockKey); this.IncreaseIndent(); return; case EmitterState.Sequence when mappingStart != null: this.Write(','); this.WriteIndent(); break; } }
/// <summary> /// Expect SEQUENCE-START. /// </summary> private void EmitSequenceStart(Event evt) { ProcessAnchor(); ProcessTag(); SequenceStart sequenceStart = (SequenceStart)evt; if (flowLevel != 0 || isCanonical || sequenceStart.Style == YamlStyle.Flow || CheckEmptySequence()) { state = EmitterState.YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE; } else { state = EmitterState.YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE; } }
/// <summary> /// Expect MAPPING-START. /// </summary> private void EmitMappingStart(Event evt) { ProcessAnchor(); ProcessTag(); MappingStart mappingStart = (MappingStart)evt; if (flowLevel != 0 || isCanonical || mappingStart.Style == YamlStyle.Flow || CheckEmptyMapping()) { state = EmitterState.YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE; } else { state = EmitterState.YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE; } }
/// <summary> /// Expect STREAM-START. /// </summary> private void EmitStreamStart(ParsingEvent evt) { if (!(evt is StreamStart)) { throw new ArgumentException("Expected STREAM-START.", "evt"); } indent = -1; column = 0; isWhitespace = true; isIndentation = true; state = EmitterState.FirstDocumentStart; }
/// <summary> /// Expect MAPPING-START. /// </summary> private void EmitMappingStart(ParsingEvent evt) { ProcessAnchor(); ProcessTag(); var mappingStart = (MappingStart) evt; if (flowLevel != 0 || isCanonical || mappingStart.Style == MappingStyle.Flow || CheckEmptyMapping()) { state = EmitterState.FlowMappingFirstKey; } else { state = EmitterState.BlockMappingFirstKey; } }
/// <summary> /// Expect DOCUMENT-START or STREAM-END. /// </summary> private void EmitDocumentStart(ParsingEvent evt, bool isFirst) { var documentStart = evt as DocumentStart; if (documentStart != null) { var isImplicit = documentStart.IsImplicit && isFirst && !isCanonical; var documentTagDirectives = NonDefaultTagsAmong(documentStart.Tags); if (!isFirst && !isDocumentEndWritten && (documentStart.Version != null || documentTagDirectives.Count > 0)) { isDocumentEndWritten = false; WriteIndicator("...", true, false, false); WriteIndent(); } if (documentStart.Version != null) { AnalyzeVersionDirective(documentStart.Version); isImplicit = false; WriteIndicator("%YAML", true, false, false); WriteIndicator(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", Constants.MajorVersion, Constants.MinorVersion), true, false, false); WriteIndent(); } foreach (var tagDirective in documentTagDirectives) { AppendTagDirectiveTo(tagDirective, false, tagDirectives); } foreach (var tagDirective in Constants.DefaultTagDirectives) { AppendTagDirectiveTo(tagDirective, true, tagDirectives); } if (documentTagDirectives.Count > 0) { isImplicit = false; foreach (var tagDirective in Constants.DefaultTagDirectives) { AppendTagDirectiveTo(tagDirective, true, documentTagDirectives); } foreach (var tagDirective in documentTagDirectives) { WriteIndicator("%TAG", true, false, false); WriteTagHandle(tagDirective.Handle); WriteTagContent(tagDirective.Prefix, true); WriteIndent(); } } if (CheckEmptyDocument()) { isImplicit = false; } if (!isImplicit) { WriteIndent(); WriteIndicator("---", true, false, false); if (isCanonical) { WriteIndent(); } } state = EmitterState.DocumentContent; } else if (evt is StreamEnd) { if (isOpenEnded) { WriteIndicator("...", true, false, false); WriteIndent(); } state = EmitterState.StreamEnd; } else { throw new YamlException("Expected DOCUMENT-START or STREAM-END"); } }
/// <summary> /// Expect a block item node. /// </summary> private void EmitBlockSequenceItem(ParsingEvent evt, bool isFirst) { if (isFirst) { IncreaseIndent(false, (isMappingContext && !isIndentation)); } if (evt is SequenceEnd) { indent = indents.Pop(); state = states.Pop(); return; } WriteIndent(); WriteIndicator("-", true, false, true); states.Push(EmitterState.BlockSequenceItem); EmitNode(evt, false, false, false); }
/// <summary> /// Expect DOCUMENT-START or STREAM-END. /// </summary> private void EmitDocumentStart(Event evt, bool isFirst) { DocumentStart documentStart = evt as DocumentStart; if (documentStart != null) { bool isImplicit = documentStart.IsImplicit && isFirst && !isCanonical; if (documentStart.Version != null && isOpenEnded) { WriteIndicator("...", true, false, false); WriteIndent(); } if (documentStart.Version != null) { AnalyzeVersionDirective(documentStart.Version); isImplicit = false; WriteIndicator("%YAML", true, false, false); WriteIndicator(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", Constants.MajorVersion, Constants.MinorVersion), true, false, false); WriteIndent(); } if (documentStart.Tags != null) { foreach (var tagDirective in documentStart.Tags) { AppendTagDirective(tagDirective, false); } } foreach (var tagDirective in Constants.DefaultTagDirectives) { AppendTagDirective(tagDirective, true); } if (documentStart.Tags != null && documentStart.Tags.Count != 0) { isImplicit = false; foreach (var tagDirective in documentStart.Tags) { WriteIndicator("%TAG", true, false, false); WriteTagHandle(tagDirective.Handle); WriteTagContent(tagDirective.Prefix, true); WriteIndent(); } } if (CheckEmptyDocument()) { isImplicit = false; } if (!isImplicit) { WriteIndent(); WriteIndicator("---", true, false, false); if (isCanonical) { WriteIndent(); } } state = EmitterState.YAML_EMIT_DOCUMENT_CONTENT_STATE; } else if (evt is IStreamEnd) { if (isOpenEnded) { WriteIndicator("...", true, false, false); WriteIndent(); } state = EmitterState.YAML_EMIT_END_STATE; } else { throw new YamlException("Expected DOCUMENT-START or STREAM-END"); } }
private void EmitAlias() { if (((NodeEvent)_event).Anchor == null) { throw new EmitterException("anchor is not specified for alias"); } ProcessAnchor("*"); _state = _states.Pop(); }
/// <summary> /// Push the current emitter state onto the state stack. /// </summary> /// <param name="currentState">The current state</param> private void PushState(EmitterState currentState) { this.states.Push(currentState); }
/// <summary> /// Pop the emitter state and store the previous state. /// </summary> /// <returns>The next emitter state.</returns> private EmitterState PopState() { this.previousState = this.state; return(this.states.Pop()); }
/// <summary> /// Emits a mapping end. /// </summary> /// <param name="event">The event.</param> private void EmitMappingEnd(HclEvent @event) { GetTypedEvent <MappingEnd>(@event); if (this.state != EmitterState.Resource) { this.state = this.PopState(); } if (this.state == EmitterState.Resource && this.lifecycleKeys.Any()) { /* * Needs to go ahead of ResourceEnd which should be next * * BlockKey (lifecycle) * SequenceStart * MappingStart * MappingKey (ignore_changes ) * SequenceStart * Scalar (unquoted) * ... * SequenceEnd * MappingEnd * SequenceEnd * MappingEnd (end of resource) * */ var resourceEnd = this.events.Dequeue(); this.events.Enqueue( new MappingKey( "lifecycle", new AttributePath("lifecycle"), new ValueSchema { Optional = true, ConfigMode = SchemaConfigMode.SchemaConfigModeBlock, Type = SchemaValueType.TypeList })); this.events.Enqueue(new SequenceStart()); this.events.Enqueue(new MappingStart()); this.events.Enqueue( new MappingKey( "ignore_changes", new AttributePath("lifecycle.0.ignore_changes"), new ValueSchema { Optional = true, ConfigMode = SchemaConfigMode.SchemaConfigModeAuto })); this.events.Enqueue(new SequenceStart()); foreach (var key in this.lifecycleKeys) { this.events.Enqueue(new ScalarValue(key, false)); } this.events.Enqueue(new SequenceEnd()); this.events.Enqueue(new MappingEnd()); this.events.Enqueue(new SequenceEnd()); this.events.Enqueue(new MappingEnd()); this.events.Enqueue(resourceEnd); this.lifecycleKeys.Clear(); return; } this.indent = this.indents.Pop(); this.WriteIndent(); this.WriteIndicator("}", false, false, true); var mappingStart = this.events.Peek() as MappingStart; // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault switch (this.state) { case EmitterState.BlockList when mappingStart != null: // Next element in a block list this.WriteIndent(); this.indent = this.indents.Pop(); this.EmitMappingKey(this.currentBlockKey); this.IncreaseIndent(); return; case EmitterState.Sequence when mappingStart != null: this.Write(','); this.WriteIndent(); break; } }
/// <summary> /// Expect ALIAS. /// </summary> private void EmitAlias() { ProcessAnchor(); state = states.Pop(); }
/// <summary> /// Expect a block item node. /// </summary> private void EmitBlockSequenceItem(Event evt, bool isFirst) { if (isFirst) { IncreaseIndent(false, (isMappingContext && !isIndentation)); } if (evt is ISequenceEnd) { indent = indents.Pop(); state = states.Pop(); return; } WriteIndent(); WriteIndicator("-", true, false, true); states.Push(EmitterState.YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE); EmitNode(evt, false, false, false); }
private void EmitBlockMapping() { increaseIndent(false, false); _state = EmitterState.FIRST_BLOCK_MAPPING_KEY; }
/// <summary> /// Initializes a new instance of the <see cref="HclEmitter"/> class. /// </summary> /// <param name="output">The output stream.</param> public HclEmitter(TextWriter output) { this.output = output; this.state = EmitterState.Resource; this.resourceTraits = AwsSchema.TraitsAll; }
private void EmitBlockSequence() { increaseIndent(false, _mappingContext && !_indentation); _state = EmitterState.FIRST_BLOCK_SEQUENCE_ITEM; }
/// <summary> /// Expect DOCUMENT-END. /// </summary> private void EmitDocumentEnd(ParsingEvent evt) { var documentEnd = evt as DocumentEnd; if (documentEnd != null) { WriteIndent(); if (!documentEnd.IsImplicit) { WriteIndicator("...", true, false, false); WriteIndent(); isDocumentEndWritten = true; } state = EmitterState.DocumentStart; tagDirectives.Clear(); } else { throw new YamlException("Expected DOCUMENT-END."); } }
private void EmitDocumentEnd() { if (_event is DocumentEndEvent) { WriteIndent(); if (((DocumentEndEvent)_event).Explicit) { WriteIndicator("...", true, false, false); WriteIndent(); } _writer.Flush(); _state = EmitterState.DOCUMENT_START; } else { throw new EmitterException("Emited DocumentEndEvent, but got " + _event); } }
/// <summary> /// Expect a flow key node. /// </summary> private void EmitFlowMappingKey(ParsingEvent evt, bool isFirst) { if (isFirst) { WriteIndicator("{", true, true, false); IncreaseIndent(true, false); ++flowLevel; } if (evt is MappingEnd) { --flowLevel; indent = indents.Pop(); if (isCanonical && !isFirst) { WriteIndicator(",", false, false, false); WriteIndent(); } WriteIndicator("}", false, false, false); state = states.Pop(); return; } if (!isFirst) { WriteIndicator(",", false, false, false); } if (isCanonical || column > bestWidth) { WriteIndent(); } if (!isCanonical && CheckSimpleKey()) { states.Push(EmitterState.FlowMappingSimpleValue); EmitNode(evt, false, true, true); } else { WriteIndicator("?", true, false, false); states.Push(EmitterState.FlowMappingValue); EmitNode(evt, false, true, false); } }
private void EmitDocumentStart(bool first) { if (_event is DocumentStartEvent) { DocumentStartEvent ev = (DocumentStartEvent)_event; if (first) { if (null != ev.Version) { WriteVersionDirective(prepareVersion(ev.Version)); } if ((null != ev.Version && ev.Version.Equals(new Version(1, 0))) || _options.Version.Equals(new Version(1, 0))) { _isVersion10 = true; _tagPrefixes.Clear(); _tagPrefixes.Add("tag:yaml.org,2002:", "!"); } else { _tagPrefixes.Clear(); _tagPrefixes.Add("!", "!"); _tagPrefixes.Add("tag:yaml.org,2002:", "!!"); } if (null != ev.Tags) { var entries = new List<KeyValuePair<string, string>>(ev.Tags); entries.Sort((x, y) => StringComparer.Ordinal.Compare(x.Key, y.Key)); foreach (KeyValuePair<string, string> tags in entries) { string handle = tags.Key; string prefix = tags.Value; _tagPrefixes.Add(prefix, handle); string handleText = prepareTagHandle(handle); string prefixText = prepareTagPrefix(prefix); WriteTagDirective(handleText, prefixText); } } } bool @implicit = first && !ev.Explicit && !_canonical && ev.Version == null && ev.Tags == null && !checkEmptyDocument(); if (!@implicit) { if (!first) { WriteIndent(); } WriteIndicator("--- ", !first, true, false); if (_canonical) { WriteIndent(); } } _state = EmitterState.DOCUMENT_ROOT; } else if (_event is StreamEndEvent) { WriteStreamEnd(); _state = EmitterState.NOTHING; } else { throw new EmitterException("Emited DocumentStartEvent, but got " + _event); } }
/// <summary> /// Expect SEQUENCE-START. /// </summary> private void EmitSequenceStart(ParsingEvent evt) { ProcessAnchor(); ProcessTag(); var sequenceStart = (SequenceStart) evt; if (flowLevel != 0 || isCanonical || sequenceStart.Style == SequenceStyle.Flow || CheckEmptySequence()) { state = EmitterState.FlowSequenceFirstItem; } else { state = EmitterState.BlockSequenceFirstItem; } }
private void EmitFlowMapping() { WriteIndicator("{", true, true, false); _flowLevel++; increaseIndent(true, false); _state = EmitterState.FIRST_FLOW_MAPPING_KEY; }
/// <summary> /// /// Expect a flow item node. /// </summary> private void EmitFlowSequenceItem(Event evt, bool isFirst) { if (isFirst) { WriteIndicator("[", true, true, false); IncreaseIndent(true, false); ++flowLevel; } if (evt is ISequenceEnd) { --flowLevel; indent = indents.Pop(); if (isCanonical && !isFirst) { WriteIndicator(",", false, false, false); WriteIndent(); } WriteIndicator("]", false, false, false); state = states.Pop(); return; } if (!isFirst) { WriteIndicator(",", false, false, false); } if (isCanonical || column > bestWidth) { WriteIndent(); } states.Push(EmitterState.YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE); EmitNode(evt, false, false, false); }
private void EmitFlowMappingKey(bool first) { if (_event is MappingEndEvent) { _indent = _indents.Pop(); _flowLevel--; if (_canonical && !first) { WriteIndicator(",", false, false, false); WriteIndent(); } WriteIndicator("}", false, false, false); _state = _states.Pop(); } else { if (!first) { WriteIndicator(",", false, false, false); } if (_canonical || _column > _bestWidth) { WriteIndent(); } if (!_canonical && CheckSimpleKey()) { _states.Push(EmitterState.FLOW_MAPPING_SIMPLE_VALUE); EmitNode(false, false, true, true); } else { WriteIndicator("?", true, false, false); _states.Push(EmitterState.FLOW_MAPPING_VALUE); EmitNode(false, false, true, false); } } }
/// <summary> /// Expect SCALAR. /// </summary> private void EmitScalar(Event evt) { SelectScalarStyle(evt); ProcessAnchor(); ProcessTag(); IncreaseIndent(true, false); ProcessScalar(); indent = indents.Pop(); state = states.Pop(); }
private void EmitFlowSequence() { WriteIndicator("[", true, true, false); _flowLevel++; increaseIndent(true, false); _state = EmitterState.FIRST_FLOW_SEQUENCE_ITEM; }
/// <summary> /// Expect STREAM-START. /// </summary> private void EmitStreamStart(Event evt) { if (!(evt is IStreamStart)) { throw new ArgumentException("Expected STREAM-START.", "evt"); } indent = -1; line = 0; column = 0; isWhitespace = true; isIndentation = true; state = EmitterState.YAML_EMIT_FIRST_DOCUMENT_START_STATE; }
private void EmitFlowSequenceItem(bool first) { if (_event is SequenceEndEvent) { _indent = _indents.Pop(); _flowLevel--; if (_canonical && !first) { WriteIndicator(",", false, false, false); WriteIndent(); } WriteIndicator("]", false, false, false); _state = _states.Pop(); } else { if (!first) { WriteIndicator(",", false, false, false); } if (_canonical || _column > _bestWidth) { WriteIndent(); } _states.Push(EmitterState.FLOW_SEQUENCE_ITEM); EmitNode(false, true, false, false); } }
/// <summary> /// Expect a block key node. /// </summary> private void EmitBlockMappingKey(Event evt, bool isFirst) { if (isFirst) { IncreaseIndent(false, false); } if (evt is IMappingEnd) { indent = indents.Pop(); state = states.Pop(); return; } WriteIndent(); if (CheckSimpleKey()) { states.Push(EmitterState.YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE); EmitNode(evt, false, true, true); } else { WriteIndicator("?", true, false, true); states.Push(EmitterState.YAML_EMIT_BLOCK_MAPPING_VALUE_STATE); EmitNode(evt, false, true, false); } }
private void EmitScalar(bool simpleKey) { ProcessScalar(simpleKey); _state = _states.Pop(); }
/// <summary> /// Expect DOCUMENT-END. /// </summary> private void EmitDocumentEnd(Event evt) { DocumentEnd documentEnd = evt as DocumentEnd; if (documentEnd != null) { WriteIndent(); if (!documentEnd.IsImplicit) { WriteIndicator("...", true, false, false); WriteIndent(); } state = EmitterState.YAML_EMIT_DOCUMENT_START_STATE; tagDirectives.Clear(); } else { throw new YamlException("Expected DOCUMENT-END."); } }
private void EmitStreamStart() { if (_event is StreamStartEvent) { WriteStreamStart(); _state = EmitterState.FIRST_DOCUMENT_START; } else { throw new EmitterException("Emited StreamStartEvent, but got " + _event); } }
/// <summary> /// Expect a flow key node. /// </summary> private void EmitFlowMappingKey(Event evt, bool isFirst) { if (isFirst) { WriteIndicator("{", true, true, false); IncreaseIndent(true, false); ++flowLevel; } if (evt is IMappingEnd) { --flowLevel; indent = indents.Pop(); if (isCanonical && !isFirst) { WriteIndicator(",", false, false, false); WriteIndent(); } WriteIndicator("}", false, false, false); state = states.Pop(); return; } if (!isFirst) { WriteIndicator(",", false, false, false); } if (isCanonical || column > bestWidth) { WriteIndent(); } if (!isCanonical && CheckSimpleKey()) { states.Push(EmitterState.YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE); EmitNode(evt, false, true, true); } else { WriteIndicator("?", true, false, false); states.Push(EmitterState.YAML_EMIT_FLOW_MAPPING_VALUE_STATE); EmitNode(evt, false, true, false); } }
/// <summary> /// Expect a block key node. /// </summary> private void EmitBlockMappingKey(ParsingEvent evt, bool isFirst) { if (isFirst) { IncreaseIndent(false, false); } if (evt is MappingEnd) { indent = indents.Pop(); state = states.Pop(); return; } WriteIndent(); if (CheckSimpleKey()) { states.Push(EmitterState.BlockMappingSimpleValue); EmitNode(evt, false, true, true); } else { WriteIndicator("?", true, false, true); states.Push(EmitterState.BlockMappingValue); EmitNode(evt, false, true, false); } }