/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/151617var docblockRe = /^\s*(\/\*\*(.|\r?\n)*?\*\/)/;1819var ltrimRe = /^\s*/;20/**21* @param {String} contents22* @return {String}23*/24function extract(contents) {25var match = contents.match(docblockRe);26if (match) {27return match[0].replace(ltrimRe, '') || '';28}29return '';30}313233var commentStartRe = /^\/\*\*?/;34var commentEndRe = /\*\/$/;35var wsRe = /[\t ]+/g;36var stringStartRe = /(\r?\n|^) *\*/g;37var multilineRe = /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *([^@\r\n\s][^@\r\n]+?) *\r?\n/g;38var propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g;3940/**41* @param {String} contents42* @return {Array}43*/44function parse(docblock) {45docblock = docblock46.replace(commentStartRe, '')47.replace(commentEndRe, '')48.replace(wsRe, ' ')49.replace(stringStartRe, '$1');5051// Normalize multi-line directives52var prev = '';53while (prev != docblock) {54prev = docblock;55docblock = docblock.replace(multilineRe, "\n$1 $2\n");56}57docblock = docblock.trim();5859var result = [];60var match;61while (match = propertyRe.exec(docblock)) {62result.push([match[1], match[2]]);63}6465return result;66}6768/**69* Same as parse but returns an object of prop: value instead of array of paris70* If a property appers more than once the last one will be returned71*72* @param {String} contents73* @return {Object}74*/75function parseAsObject(docblock) {76var pairs = parse(docblock);77var result = {};78for (var i = 0; i < pairs.length; i++) {79result[pairs[i][0]] = pairs[i][1];80}81return result;82}838485exports.extract = extract;86exports.parse = parse;87exports.parseAsObject = parseAsObject;888990