es5-lexer - это JS-лексер, который использует очень точную эвристику для отличия регулярных выражений в коде JS от выражений деления, а также предоставляет преобразование уровня токена, которое можно использовать для проверки программа будет интерпретироваться полным JS-парсером так же, как и лексером.
Бит, определяющий, начинается ли регулярное выражение /
, находится в guess_is_regexp.js
, а тесты начинаются с scanner_test.js
строка 401
var REGEXP_PRECEDER_TOKEN_RE = new RegExp(
"^(?:" // Match the whole tokens below
+ "break"
+ "|case"
+ "|continue"
+ "|delete"
+ "|do"
+ "|else"
+ "|finally"
+ "|in"
+ "|instanceof"
+ "|return"
+ "|throw"
+ "|try"
+ "|typeof"
+ "|void"
// Binary operators which cannot be followed by a division operator.
+ "|[+]" // Match + but not ++. += is handled below.
+ "|-" // Match - but not --. -= is handled below.
+ "|[.]" // Match . but not a number with a trailing decimal.
+ "|[/]" // Match /, but not a regexp. /= is handled below.
+ "|," // Second binary operand cannot start a division.
+ "|[*]" // Ditto binary operand.
+ ")$"
// Or match a token that ends with one of the characters below to match
// a variety of punctuation tokens.
// Some of the single char tokens could go above, but putting them below
// allows closure-compiler's regex optimizer to do a better job.
// The right column explains why the terminal character to the left can only
// precede a regexp.
+ "|["
+ "!" // ! prefix operator operand cannot start with a division
+ "%" // % second binary operand cannot start with a division
+ "&" // &, && ditto binary operand
+ "(" // ( expression cannot start with a division
+ ":" // : property value, labelled statement, and operand of ?:
// cannot start with a division
+ ";" // ; statement & for condition cannot start with division
+ "<" // <, <<, << ditto binary operand
// !=, !==, %=, &&=, &=, *=, +=, -=, /=, <<=, <=, =, ==, ===, >=, >>=, >>>=,
// ^=, |=, ||=
// All are binary operands (assignment ops or comparisons) whose right
// operand cannot start with a division operator
+ "="
+ ">" // >, >>, >>> ditto binary operand
+ "?" // ? expression in ?: cannot start with a division operator
+ "[" // [ first array value & key expression cannot start with
// a division
+ "^" // ^ ditto binary operand
+ "{" // { statement in block and object property key cannot start
// with a division
+ "|" // |, || ditto binary operand
+ "}" // } PROBLEMATIC: could be an object literal divided or
// a block. More likely to be start of a statement after
// a block which cannot start with a /.
+ "~" // ~ ditto binary operand
+ "]$"
// The exclusion of ++ and -- from the above is also problematic.
// Both are prefix and postfix operators.
// Given that there is rarely a good reason to increment a regular expression
// and good reason to have a post-increment operator as the left operand of
// a division (x++ / y) this pattern treats ++ and -- as division preceders.
);