Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / google-gax / build / src / parserExtras.js
1 "use strict";
2 /*
3  *
4  * Copyright 2016, Google Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
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
16  * distribution.
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.
20  *
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.
32  *
33  */
34 Object.defineProperty(exports, "__esModule", { value: true });
35 const util = require("util");
36 /* constants used in the pegjs parser */
37 exports.BINDING = 1;
38 exports.END_BINDING = 2;
39 exports.TERMINAL = 3;
40 /**
41  * Checks that segments only has one terminal segment that is a path wildcard.
42  *
43  * @private
44  *
45  * @param {Segments[]} segments the parsed segments
46  * @throws {TypeError} if there are too many
47  */
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 !== '**') {
53             continue;
54         }
55         if (hasPathWildcard) {
56             const tooManyWildcards = 'cannot contain more than one path wildcard';
57             throw new TypeError(tooManyWildcards);
58         }
59         hasPathWildcard = true;
60     }
61 }
62 /**
63  * Counts the number of terminal segments.
64  *
65  * @private
66  *
67  * @param {Segments[]} segments the parsed segments
68  * @return {number} the number of terminal segments in the template
69  */
70 function countTerminals(segments) {
71     return segments.filter(x => x.kind === exports.TERMINAL).length;
72 }
73 /**
74  * Updates missing literals of each of the binding segments.
75  *
76  * @private
77  *
78  * @param {Segments[]} segments the parsed segments
79  */
80 function updateBindingLiterals(segments) {
81     let bindingIndex = 0;
82     segments.forEach(s => {
83         if (s.kind === exports.BINDING && !s.literal) {
84             s.literal = util.format('$%d', bindingIndex);
85             bindingIndex += 1;
86         }
87     });
88 }
89 /**
90  * Completes the parsing of the segments
91  *
92  * Validates them, and transforms them into the object used by the
93  * PathTemplate class.
94  *
95  * @private
96  *
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
101  */
102 function finishParse(segments) {
103     allowOnePathWildcard(segments);
104     updateBindingLiterals(segments);
105     return {
106         segments,
107         size: countTerminals(segments),
108     };
109 }
110 exports.finishParse = finishParse;
111 //# sourceMappingURL=parserExtras.js.map