Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
make regex-literals single line by default
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Mar 4, 2013
1 parent 52394f6 commit d8c7a78
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
26 changes: 19 additions & 7 deletions js-modules/prettify.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,26 +497,38 @@ var prettyPrint;
fallthroughStylePatterns.push(
[PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]);
}
if (options['regexLiterals']) {
var regexLiterals = options['regexLiterals'];
if (regexLiterals) {
/**
* @const
*/
var regexExcls = regexLiterals > 1
? '' // Multiline regex literals
: '\n\r';
/**
* @const
*/
var regexAny = regexExcls ? '.' : '[\\S\\s]';
/**
* @const
*/
var REGEX_LITERAL = (
// A regular expression literal starts with a slash that is
// not followed by * or / so that it is not confused with
// comments.
'/(?=[^/*])'
'/(?=[^/*' + regexExcls + '])'
// and then contains any number of raw characters,
+ '(?:[^/\\x5B\\x5C]'
+ '(?:[^/\\x5B\\x5C' + regexExcls + ']'
// escape sequences (\x5C),
+ '|\\x5C[\\s\\S]'
+ '|\\x5C' + regexAny
// or non-nesting character sets (\x5B\x5D);
+ '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+'
+ '|\\x5B(?:[^\\x5C\\x5D' + regexExcls + ']'
+ '|\\x5C' + regexAny + ')*(?:\\x5D|$))+'
// finally closed by a /.
+ '/');
fallthroughStylePatterns.push(
['lang-regex',
new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
]);
}

Expand Down Expand Up @@ -728,7 +740,7 @@ var prettyPrint;
'keywords': PERL_KEYWORDS,
'hashComments': true,
'multiLineStrings': true,
'regexLiterals': true
'regexLiterals': 2 // multiline regex literals
}), ['perl', 'pl', 'pm']);
registerLangHandler(sourceDecorator({
'keywords': RUBY_KEYWORDS,
Expand Down
26 changes: 19 additions & 7 deletions src/prettify.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,26 +852,38 @@ var prettyPrint;
fallthroughStylePatterns.push(
[PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]);
}
if (options['regexLiterals']) {
var regexLiterals = options['regexLiterals'];
if (regexLiterals) {
/**
* @const
*/
var regexExcls = regexLiterals > 1
? '' // Multiline regex literals
: '\n\r';
/**
* @const
*/
var regexAny = regexExcls ? '.' : '[\\S\\s]';
/**
* @const
*/
var REGEX_LITERAL = (
// A regular expression literal starts with a slash that is
// not followed by * or / so that it is not confused with
// comments.
'/(?=[^/*])'
'/(?=[^/*' + regexExcls + '])'
// and then contains any number of raw characters,
+ '(?:[^/\\x5B\\x5C]'
+ '(?:[^/\\x5B\\x5C' + regexExcls + ']'
// escape sequences (\x5C),
+ '|\\x5C[\\s\\S]'
+ '|\\x5C' + regexAny
// or non-nesting character sets (\x5B\x5D);
+ '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+'
+ '|\\x5B(?:[^\\x5C\\x5D' + regexExcls + ']'
+ '|\\x5C' + regexAny + ')*(?:\\x5D|$))+'
// finally closed by a /.
+ '/');
fallthroughStylePatterns.push(
['lang-regex',
new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
]);
}

Expand Down Expand Up @@ -1336,7 +1348,7 @@ var prettyPrint;
'keywords': PERL_KEYWORDS,
'hashComments': true,
'multiLineStrings': true,
'regexLiterals': true
'regexLiterals': 2 // multiline regex literals
}), ['perl', 'pl', 'pm']);
registerLangHandler(sourceDecorator({
'keywords': RUBY_KEYWORDS,
Expand Down
26 changes: 19 additions & 7 deletions src/run_prettify.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,26 +1074,38 @@ var IN_GLOBAL_SCOPE = false;
fallthroughStylePatterns.push(
[PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]);
}
if (options['regexLiterals']) {
var regexLiterals = options['regexLiterals'];
if (regexLiterals) {
/**
* @const
*/
var regexExcls = regexLiterals > 1
? '' // Multiline regex literals
: '\n\r';
/**
* @const
*/
var regexAny = regexExcls ? '.' : '[\\S\\s]';
/**
* @const
*/
var REGEX_LITERAL = (
// A regular expression literal starts with a slash that is
// not followed by * or / so that it is not confused with
// comments.
'/(?=[^/*])'
'/(?=[^/*' + regexExcls + '])'
// and then contains any number of raw characters,
+ '(?:[^/\\x5B\\x5C]'
+ '(?:[^/\\x5B\\x5C' + regexExcls + ']'
// escape sequences (\x5C),
+ '|\\x5C[\\s\\S]'
+ '|\\x5C' + regexAny
// or non-nesting character sets (\x5B\x5D);
+ '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+'
+ '|\\x5B(?:[^\\x5C\\x5D' + regexExcls + ']'
+ '|\\x5C' + regexAny + ')*(?:\\x5D|$))+'
// finally closed by a /.
+ '/');
fallthroughStylePatterns.push(
['lang-regex',
new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
]);
}

Expand Down Expand Up @@ -1558,7 +1570,7 @@ var IN_GLOBAL_SCOPE = false;
'keywords': PERL_KEYWORDS,
'hashComments': true,
'multiLineStrings': true,
'regexLiterals': true
'regexLiterals': 2 // multiline regex literals
}), ['perl', 'pl', 'pm']);
registerLangHandler(sourceDecorator({
'keywords': RUBY_KEYWORDS,
Expand Down

0 comments on commit d8c7a78

Please sign in to comment.