OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @constructor | |
7 * @param {string} content | |
8 */ | |
9 FormatterWorker.AcornTokenizer = function(content) | |
10 { | |
11 this._content = content; | |
12 this._comments = []; | |
13 this._tokenizer = acorn.tokenizer(this._content, { ecmaVersion: 6, onComment
: this._comments }); | |
14 this._lineEndings = this._content.lineEndings(); | |
15 this._lineNumber = 0; | |
16 this._tokenLineStart = 0; | |
17 this._tokenLineEnd = 0; | |
18 this._nextTokenInternal(); | |
19 } | |
20 | |
21 /** | |
22 * @param {!Acorn.TokenOrComment} token | |
23 * @param {string=} values | |
24 * @return {boolean} | |
25 */ | |
26 FormatterWorker.AcornTokenizer.punctuator = function(token, values) | |
27 { | |
28 return token.type !== acorn.tokTypes.num && | |
29 token.type !== acorn.tokTypes.regexp && | |
30 token.type !== acorn.tokTypes.string && | |
31 token.type !== acorn.tokTypes.name && | |
32 (!values || (token.type.label.length === 1 && values.indexOf(token.type.
label) !== -1)); | |
33 } | |
34 | |
35 /** | |
36 * @param {!Acorn.TokenOrComment} token | |
37 * @param {string=} keyword | |
38 * @return {boolean} | |
39 */ | |
40 FormatterWorker.AcornTokenizer.keyword = function(token, keyword) | |
41 { | |
42 return !!token.type.keyword && token.type !== acorn.tokTypes._true && token.
type !== acorn.tokTypes._false && | |
43 (!keyword || token.type.keyword === keyword); | |
44 } | |
45 | |
46 /** | |
47 * @param {!Acorn.TokenOrComment} token | |
48 * @param {string=} identifier | |
49 * @return {boolean} | |
50 */ | |
51 FormatterWorker.AcornTokenizer.identifier = function(token, identifier) | |
52 { | |
53 return token.type === acorn.tokTypes.name && (!identifier || token.value ===
identifier); | |
54 } | |
55 | |
56 /** | |
57 * @param {!Acorn.TokenOrComment} token | |
58 * @return {boolean} | |
59 */ | |
60 FormatterWorker.AcornTokenizer.lineComment = function(token) | |
61 { | |
62 return token.type === "Line"; | |
63 } | |
64 | |
65 /** | |
66 * @param {!Acorn.TokenOrComment} token | |
67 * @return {boolean} | |
68 */ | |
69 FormatterWorker.AcornTokenizer.blockComment = function(token) | |
70 { | |
71 return token.type === "Block"; | |
72 } | |
73 | |
74 FormatterWorker.AcornTokenizer.prototype = { | |
75 /** | |
76 * @return {!Acorn.TokenOrComment} | |
77 */ | |
78 _nextTokenInternal: function() | |
79 { | |
80 if (this._comments.length) | |
81 return this._comments.shift(); | |
82 var token = this._bufferedToken; | |
83 | |
84 this._bufferedToken = this._tokenizer.getToken(); | |
85 return token; | |
86 }, | |
87 | |
88 /** | |
89 * @param {number} position | |
90 * @return {number} | |
91 */ | |
92 _rollLineNumberToPosition: function(position) | |
93 { | |
94 while (this._lineNumber + 1 < this._lineEndings.length && position > thi
s._lineEndings[this._lineNumber]) | |
95 ++this._lineNumber; | |
96 return this._lineNumber; | |
97 }, | |
98 | |
99 /** | |
100 * @return {?Acorn.TokenOrComment} | |
101 */ | |
102 nextToken: function() | |
103 { | |
104 var token = this._nextTokenInternal(); | |
105 if (token.type === acorn.tokTypes.eof) | |
106 return null; | |
107 | |
108 this._tokenLineStart = this._rollLineNumberToPosition(token.start); | |
109 this._tokenLineEnd = this._rollLineNumberToPosition(token.end); | |
110 this._tokenColumnStart = this._tokenLineStart > 0 ? token.start - this._
lineEndings[this._tokenLineStart - 1] - 1 : token.start; | |
111 return token; | |
112 }, | |
113 | |
114 /** | |
115 * @return {?Acorn.TokenOrComment} | |
116 */ | |
117 peekToken: function() | |
118 { | |
119 if (this._comments.length) | |
120 return this._comments[0]; | |
121 return this._bufferedToken.type !== acorn.tokTypes.eof ? this._bufferedT
oken : null; | |
122 }, | |
123 | |
124 /** | |
125 * @return {number} | |
126 */ | |
127 tokenLineStart: function() | |
128 { | |
129 return this._tokenLineStart; | |
130 }, | |
131 | |
132 /** | |
133 * @return {number} | |
134 */ | |
135 tokenLineEnd: function() | |
136 { | |
137 return this._tokenLineEnd; | |
138 }, | |
139 | |
140 /** | |
141 * @return {number} | |
142 */ | |
143 tokenColumnStart: function() | |
144 { | |
145 return this._tokenColumnStart; | |
146 } | |
147 } | |
148 | |
149 // A dummy javascript mode which is used only by htmlmixed mode to advance | |
150 // stream until a </script> is found. | |
151 CodeMirror.defineMode("javascript", function(config, parserConfig) { | |
152 return { | |
153 token: function(stream, state) | |
154 { | |
155 return stream.next(); | |
156 } | |
157 } | |
158 }); | |
OLD | NEW |