4 * Copyright 2016, Google Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 Object.defineProperty(exports, "__esModule", { value: true });
35 const util = require("util");
36 /* constants used in the pegjs parser */
38 exports.END_BINDING = 2;
41 * Checks that segments only has one terminal segment that is a path wildcard.
45 * @param {Segments[]} segments the parsed segments
46 * @throws {TypeError} if there are too many
48 function allowOnePathWildcard(segments) {
49 let hasPathWildcard = false;
50 for (let i = 0; i < segments.length; i++) {
51 const s = segments[i];
52 if (s.kind !== exports.TERMINAL || s.literal !== '**') {
55 if (hasPathWildcard) {
56 const tooManyWildcards = 'cannot contain more than one path wildcard';
57 throw new TypeError(tooManyWildcards);
59 hasPathWildcard = true;
63 * Counts the number of terminal segments.
67 * @param {Segments[]} segments the parsed segments
68 * @return {number} the number of terminal segments in the template
70 function countTerminals(segments) {
71 return segments.filter(x => x.kind === exports.TERMINAL).length;
74 * Updates missing literals of each of the binding segments.
78 * @param {Segments[]} segments the parsed segments
80 function updateBindingLiterals(segments) {
82 segments.forEach(s => {
83 if (s.kind === exports.BINDING && !s.literal) {
84 s.literal = util.format('$%d', bindingIndex);
90 * Completes the parsing of the segments
92 * Validates them, and transforms them into the object used by the
97 * @param {Segments[]} segments the parsed segments
98 * @param {Object} initializes the attributes of a PathTemplate
99 * @return {Object} Returns segments and size
100 * @throws {TypeError} if multiple path wildcards exist
102 function finishParse(segments) {
103 allowOnePathWildcard(segments);
104 updateBindingLiterals(segments);
107 size: countTerminals(segments),
110 exports.finishParse = finishParse;
111 //# sourceMappingURL=parserExtras.js.map