blob: 980fbf68ffee6d8134aae3494c0df17d94d453be [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import jsyaml from 'js-yaml'
/**
* @requires nodeca/js-yaml
* @constructor
*/
export function JsYamlParser() {
};
/** returns a YamlParseNode containing { doc, parent, children, start, end, result } */
/**
*
* @param {!string} input
* @returns {YamlParseNode}
*/
JsYamlParser.prototype.parse = function (input) {
/**
* @type {YamlParseNode}
*/
var rootNode, node;
var l = function (event, state) {
if (event === 'open') {
node = new YamlParseNode(input, node, state);
} else if (event == 'close') {
node.close(state);
if (node.parent) {
node.parent.children.push(node);
node = node.parent;
} else {
if (rootNode != null) throw 'doc should have only one root node';
rootNode = node;
node = null;
}
}
};
jsyaml.safeLoad(input, {listener: l});
return rootNode;
};
/**
* @typedef YamlParseNode
* @type Object
* @property {string} doc The original YAML document
* @property {?YamlParseNode} parent This parse nodes parent
*/
/**
*
* @param {string} doc The original YAML document
* @param {?YamlParseNode} parent
* @param {object} state
* @constructor
*/
export function YamlParseNode(doc, parent, state) {
this.doc = doc;
this.parent = parent;
this.children = [];
this.start = state.position;
this.end = null;
this.result = null;
this.kind = null;
}
/**
* @param {object} state
*/
YamlParseNode.prototype.close = function (state) {
this.end = state.position;
this.result = state.result;
this.kind = state.kind;
};