UNPKG

6.14 kBMarkdownView Raw
1Overview [![Build Status](https://travis-ci.org/lydell/js-tokens.png?branch=master)](https://travis-ci.org/lydell/js-tokens)
2========
3
4A regex that tokenizes JavaScript.
5
6```js
7var jsTokens = require("js-tokens")
8
9var jsString = "var foo=opts.foo;\n..."
10
11jsString.match(jsTokens)
12// ["var", " ", "foo", "=", "opts", ".", "foo", ";", "\n", ...]
13```
14
15
16Installation
17============
18
19- `npm install js-tokens`
20
21```js
22var jsTokens = require("js-tokens")
23```
24
25
26Usage
27=====
28
29### `jsTokens` ###
30
31A regex with the `g` flag that matches JavaScript tokens.
32
33The regex _always_ matches, even invalid JavaScript and the empty string.
34
35The next match is always directly after the previous.
36
37### `var token = jsTokens.matchToToken(match)` ###
38
39Takes a `match` returned by `jsTokens.exec(string)`, and returns a `{type:
40String, value: String}` object. The following types are available:
41
42- string
43- comment
44- regex
45- number
46- name
47- punctuator
48- whitespace
49- invalid
50
51Multi-line comments and strings also have a `closed` property indicating if the
52token was closed or not (see below).
53
54Comments and strings both come in several flavors. To distinguish them, check if
55the token starts with `//`, `/*`, `'`, `"` or `` ` ``.
56
57Names are ECMAScript IdentifierNames, that is, including both identifiers and
58keywords. You may use [is-keyword-js] to tell them apart.
59
60Whitespace includes both line terminators and other whitespace.
61
62For example usage, please see this [gist].
63
64[is-keyword-js]: https://github.com/crissdev/is-keyword-js
65[gist]: https://gist.github.com/lydell/be49dbf80c382c473004
66
67
68Invalid code handling
69=====================
70
71Unterminated strings are still matched as strings. JavaScript strings cannot
72contain (unescaped) newlines, so unterminated strings simply end at the end of
73the line. Unterminated template strings can contain unescaped newlines, though,
74so they go on to the end of input.
75
76Unterminated multi-line comments are also still matched as comments. They
77simply go on to the end of the input.
78
79Unterminated regex literals are likely matched as division and whatever is
80inside the regex.
81
82Invalid ASCII characters have their own capturing group.
83
84Invalid non-ASCII characters are treated as names, to simplify the matching of
85names (except unicode spaces which are treated as whitespace).
86
87Regex literals may contain invalid regex syntax. They are still matched as
88regex literals. They may also contain repeated regex flags, to keep the regex
89simple.
90
91Strings may contain invalid escape sequences.
92
93
94Limitations
95===========
96
97Tokenizing JavaScript using regexes—in fact, _one single regex_—won’t be
98perfect. But that’s not the point either.
99
100You may compare jsTokens with [esprima] by using `esprima-compare.js`.
101See `npm run esprima-compare`!
102
103[esprima]: http://esprima.org/
104
105### Template string interpolation ###
106
107Template strings are matched as single tokens, from the starting `` ` `` to the
108ending `` ` ``, including interpolations (whose tokens are not matched
109individually).
110
111Matching template string interpolations requires recursive balancing of `{` and
112`}`—something that JavaScript regexes cannot do. Only one level of nesting is
113supported.
114
115### Division and regex literals collision ###
116
117Consider this example:
118
119```js
120var g = 9.82
121var number = bar / 2/g
122
123var regex = / 2/g
124```
125
126A human can easily understand that in the `number` line we’re dealing with
127division, and in the `regex` line we’re dealing with a regex literal. How come?
128Because humans can look at the whole code to put the `/` characters in context.
129A JavaScript regex cannot. It only sees forwards.
130
131When the `jsTokens` regex scans throught the above, it will see the following
132at the end of both the `number` and `regex` rows:
133
134```js
135/ 2/g
136```
137
138It is then impossible to know if that is a regex literal, or part of an
139expression dealing with division.
140
141Here is a similar case:
142
143```js
144foo /= 2/g
145foo(/= 2/g)
146```
147
148The first line divides the `foo` variable with `2/g`. The second line calls the
149`foo` function with the regex literal `/= 2/g`. Again, since `jsTokens` only
150sees forwards, it cannot tell the two cases apart.
151
152There are some cases where we _can_ tell division and regex literals apart,
153though.
154
155First off, we have the simple cases where there’s only one slash in the line:
156
157```js
158var foo = 2/g
159foo /= 2
160```
161
162Regex literals cannot contain newlines, so the above cases are correctly
163identified as division. Things are only problematic when there are more than
164one non-comment slash in a single line.
165
166Secondly, not every character is a valid regex flag.
167
168```js
169var number = bar / 2/e
170```
171
172The above example is also correctly identified as division, because `e` is not a
173valid regex flag. I initially wanted to future-proof by allowing `[a-zA-Z]*`
174(any letter) as flags, but it is not worth it since it increases the amount of
175ambigous cases. So only the standard `g`, `m`, `i`, `y` and `u` flags are
176allowed. This means that the above example will be identified as division as
177long as you don’t rename the `e` variable to some permutation of `gmiyu` 1 to 5
178characters long.
179
180Lastly, we can look _forward_ for information.
181
182- If the token following what looks like a regex literal is not valid after a
183 regex literal, but is valid in a division expression, then the regex literal
184 is treated as division instead. For example, a flagless regex cannot be
185 followed by a string, number or name, but all of those three can be the
186 denominator of a division.
187- Generally, if what looks like a regex literal is followed by an operator, the
188 regex literal is treated as division instead. This is because regexes are
189 seldomly used with operators (such as `+`, `*`, `&&` and `==`), but division
190 could likely be part of such an expression.
191
192Please consult the regex source and the test cases for precise information on
193when regex or division is matched (should you need to know). In short, you
194could sum it up as:
195
196If the end of a statement looks like a regex literal (even if it isn’t), it
197will be treated as one. Otherwise it should work as expected (if you write sane
198code).
199
200
201License
202=======
203
204[The X11 (“MIT”) License](LICENSE).