Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / node_modules / protobufjs / src / ProtoBuf / DotProto / Tokenizer.js
1 /*?\r
2  // --- Scope -----------------\r
3  // Lang : Language expressions\r
4 */\r
5 /**\r
6  * Constructs a new Tokenizer.\r
7  * @exports ProtoBuf.DotProto.Tokenizer\r
8  * @class prototype tokenizer\r
9  * @param {string} proto Proto to tokenize\r
10  * @constructor\r
11  */\r
12 var Tokenizer = function(proto) {\r
13 \r
14     /**\r
15      * Source to parse.\r
16      * @type {string}\r
17      * @expose\r
18      */\r
19     this.source = proto+"";\r
20 \r
21     /**\r
22      * Current index.\r
23      * @type {number}\r
24      * @expose\r
25      */\r
26     this.index = 0;\r
27 \r
28     /**\r
29      * Current line.\r
30      * @type {number}\r
31      * @expose\r
32      */\r
33     this.line = 1;\r
34 \r
35     /**\r
36      * Token stack.\r
37      * @type {!Array.<string>}\r
38      * @expose\r
39      */\r
40     this.stack = [];\r
41 \r
42     /**\r
43      * Opening character of the current string read, if any.\r
44      * @type {?string}\r
45      * @private\r
46      */\r
47     this._stringOpen = null;\r
48 };\r
49 \r
50 /**\r
51  * @alias ProtoBuf.DotProto.Tokenizer.prototype\r
52  * @inner\r
53  */\r
54 var TokenizerPrototype = Tokenizer.prototype;\r
55 \r
56 /**\r
57  * Reads a string beginning at the current index.\r
58  * @return {string}\r
59  * @private\r
60  */\r
61 TokenizerPrototype._readString = function() {\r
62     var re = this._stringOpen === '"'\r
63         ? Lang.STRING_DQ\r
64         : Lang.STRING_SQ;\r
65     re.lastIndex = this.index - 1; // Include the open quote\r
66     var match = re.exec(this.source);\r
67     if (!match)\r
68         throw Error("unterminated string");\r
69     this.index = re.lastIndex;\r
70     this.stack.push(this._stringOpen);\r
71     this._stringOpen = null;\r
72     return match[1];\r
73 };\r
74 \r
75 /**\r
76  * Gets the next token and advances by one.\r
77  * @return {?string} Token or `null` on EOF\r
78  * @expose\r
79  */\r
80 TokenizerPrototype.next = function() {\r
81     if (this.stack.length > 0)\r
82         return this.stack.shift();\r
83     if (this.index >= this.source.length)\r
84         return null;\r
85     if (this._stringOpen !== null)\r
86         return this._readString();\r
87 \r
88     var repeat,\r
89         prev,\r
90         next;\r
91     do {\r
92         repeat = false;\r
93 \r
94         // Strip white spaces\r
95         while (Lang.WHITESPACE.test(next = this.source.charAt(this.index))) {\r
96             if (next === '\n')\r
97                 ++this.line;\r
98             if (++this.index === this.source.length)\r
99                 return null;\r
100         }\r
101 \r
102         // Strip comments\r
103         if (this.source.charAt(this.index) === '/') {\r
104             ++this.index;\r
105             if (this.source.charAt(this.index) === '/') { // Line\r
106                 while (this.source.charAt(++this.index) !== '\n')\r
107                     if (this.index == this.source.length)\r
108                         return null;\r
109                 ++this.index;\r
110                 ++this.line;\r
111                 repeat = true;\r
112             } else if ((next = this.source.charAt(this.index)) === '*') { /* Block */\r
113                 do {\r
114                     if (next === '\n')\r
115                         ++this.line;\r
116                     if (++this.index === this.source.length)\r
117                         return null;\r
118                     prev = next;\r
119                     next = this.source.charAt(this.index);\r
120                 } while (prev !== '*' || next !== '/');\r
121                 ++this.index;\r
122                 repeat = true;\r
123             } else\r
124                 return '/';\r
125         }\r
126     } while (repeat);\r
127 \r
128     if (this.index === this.source.length)\r
129         return null;\r
130 \r
131     // Read the next token\r
132     var end = this.index;\r
133     Lang.DELIM.lastIndex = 0;\r
134     var delim = Lang.DELIM.test(this.source.charAt(end++));\r
135     if (!delim)\r
136         while(end < this.source.length && !Lang.DELIM.test(this.source.charAt(end)))\r
137             ++end;\r
138     var token = this.source.substring(this.index, this.index = end);\r
139     if (token === '"' || token === "'")\r
140         this._stringOpen = token;\r
141     return token;\r
142 };\r
143 \r
144 /**\r
145  * Peeks for the next token.\r
146  * @return {?string} Token or `null` on EOF\r
147  * @expose\r
148  */\r
149 TokenizerPrototype.peek = function() {\r
150     if (this.stack.length === 0) {\r
151         var token = this.next();\r
152         if (token === null)\r
153             return null;\r
154         this.stack.push(token);\r
155     }\r
156     return this.stack[0];\r
157 };\r
158 \r
159 /**\r
160  * Skips a specific token and throws if it differs.\r
161  * @param {string} expected Expected token\r
162  * @throws {Error} If the actual token differs\r
163  */\r
164 TokenizerPrototype.skip = function(expected) {\r
165     var actual = this.next();\r
166     if (actual !== expected)\r
167         throw Error("illegal '"+actual+"', '"+expected+"' expected");\r
168 };\r
169 \r
170 /**\r
171  * Omits an optional token.\r
172  * @param {string} expected Expected optional token\r
173  * @returns {boolean} `true` if the token exists\r
174  */\r
175 TokenizerPrototype.omit = function(expected) {\r
176     if (this.peek() === expected) {\r
177         this.next();\r
178         return true;\r
179     }\r
180     return false;\r
181 };\r
182 \r
183 /**\r
184  * Returns a string representation of this object.\r
185  * @return {string} String representation as of "Tokenizer(index/length)"\r
186  * @expose\r
187  */\r
188 TokenizerPrototype.toString = function() {\r
189     return "Tokenizer ("+this.index+"/"+this.source.length+" at line "+this.line+")";\r
190 };\r