Skip to content

Commit 26a08a5

Browse files
authored
(chore) grammar audits: convert strings -> regex (#2863)
* chore(audit) abnf and accesslog * chore(audit) actionscript * chore(audit) apache * chore(audit) applescript (simple) * chore(audit) applescript (rewrite complex patterns) * chore(audit) aspectj * chore(audit) zephir * chore(audit) xquery * chore(audit) xml
1 parent 2b833dd commit 26a08a5

File tree

9 files changed

+217
-137
lines changed

9 files changed

+217
-137
lines changed

src/languages/abnf.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
Language: Augmented Backus-Naur Form
33
Author: Alex McKibben <alex@nullscope.net>
44
Website: https://tools.ietf.org/html/rfc5234
5+
Audit: 2020
56
*/
67

8+
import * as regex from '../lib/regex.js';
9+
710
/** @type LanguageFn */
811
export default function(hljs) {
912
const regexes = {
10-
ruleDeclaration: "^[a-zA-Z][a-zA-Z0-9-]*",
11-
unexpectedChars: "[!@#$^&',?+~`|:]"
13+
ruleDeclaration: /^[a-zA-Z][a-zA-Z0-9-]*/,
14+
unexpectedChars: /[!@#$^&',?+~`|:]/
1215
};
1316

1417
const keywords = [
@@ -30,7 +33,7 @@ export default function(hljs) {
3033
"WSP"
3134
];
3235

33-
const commentMode = hljs.COMMENT(";", "$");
36+
const commentMode = hljs.COMMENT(/;/, /$/);
3437

3538
const terminalBinaryMode = {
3639
className: "symbol",
@@ -54,7 +57,7 @@ export default function(hljs) {
5457

5558
const ruleDeclarationMode = {
5659
className: "attribute",
57-
begin: regexes.ruleDeclaration + '(?=\\s*=)'
60+
begin: regex.concat(regexes.ruleDeclaration, /(?=\s*=)/)
5861
};
5962

6063
return {

src/languages/accesslog.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,54 @@
33
Author: Oleg Efimov <efimovov@gmail.com>
44
Description: Apache/Nginx Access Logs
55
Website: https://httpd.apache.org/docs/2.4/logs.html#accesslog
6+
Audit: 2020
67
*/
78

9+
import * as regex from '../lib/regex.js';
10+
811
/** @type LanguageFn */
9-
export default function(hljs) {
12+
export default function(_hljs) {
1013
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
1114
const HTTP_VERBS = [
12-
"GET", "POST", "HEAD", "PUT", "DELETE", "CONNECT", "OPTIONS", "PATCH", "TRACE"
15+
"GET",
16+
"POST",
17+
"HEAD",
18+
"PUT",
19+
"DELETE",
20+
"CONNECT",
21+
"OPTIONS",
22+
"PATCH",
23+
"TRACE"
1324
];
1425
return {
1526
name: 'Apache Access Log',
1627
contains: [
1728
// IP
1829
{
1930
className: 'number',
20-
begin: '^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}(:\\d{1,5})?\\b',
31+
begin: /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d{1,5})?\b/,
2132
relevance: 5
2233
},
2334
// Other numbers
2435
{
2536
className: 'number',
26-
begin: '\\b\\d+\\b',
37+
begin: /\b\d+\b/,
2738
relevance: 0
2839
},
2940
// Requests
3041
{
3142
className: 'string',
32-
begin: '"(' + HTTP_VERBS.join("|") + ')',
33-
end: '"',
43+
begin: regex.concat(/"/, regex.either(...HTTP_VERBS)),
44+
end: /"/,
3445
keywords: HTTP_VERBS.join(" "),
35-
illegal: '\\n',
46+
illegal: /\n/,
3647
relevance: 5,
37-
contains: [{
38-
begin: 'HTTP/[12]\\.\\d',
39-
relevance: 5
40-
}]
48+
contains: [
49+
{
50+
begin: /HTTP\/[12]\.\d'/,
51+
relevance: 5
52+
}
53+
]
4154
},
4255
// Dates
4356
{
@@ -46,30 +59,30 @@ export default function(hljs) {
4659
// simple array accesses a[123] and [] and other common patterns
4760
// found in other languages
4861
begin: /\[\d[^\]\n]{8,}\]/,
49-
illegal: '\\n',
62+
illegal: /\n/,
5063
relevance: 1
5164
},
5265
{
5366
className: 'string',
5467
begin: /\[/,
5568
end: /\]/,
56-
illegal: '\\n',
69+
illegal: /\n/,
5770
relevance: 0
5871
},
5972
// User agent / relevance boost
6073
{
6174
className: 'string',
62-
begin: '"Mozilla/\\d\\.\\d \\(',
63-
end: '"',
64-
illegal: '\\n',
75+
begin: /"Mozilla\/\d\.\d \(/,
76+
end: /"/,
77+
illegal: /\n/,
6578
relevance: 3
6679
},
6780
// Strings
6881
{
6982
className: 'string',
70-
begin: '"',
71-
end: '"',
72-
illegal: '\\n',
83+
begin: /"/,
84+
end: /"/,
85+
illegal: /\n/,
7386
relevance: 0
7487
}
7588
]

src/languages/actionscript.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22
Language: ActionScript
33
Author: Alexander Myadzel <myadzel@gmail.com>
44
Category: scripting
5+
Audit: 2020
56
*/
67

8+
import * as regex from '../lib/regex.js';
9+
710
/** @type LanguageFn */
811
export default function(hljs) {
9-
const IDENT_RE = '[a-zA-Z_$][a-zA-Z0-9_$]*';
10-
const IDENT_FUNC_RETURN_TYPE_RE = '([*]|[a-zA-Z_$][a-zA-Z0-9_$]*)';
12+
const IDENT_RE = /[a-zA-Z_$][a-zA-Z0-9_$]*/;
13+
const IDENT_FUNC_RETURN_TYPE_RE = /([*]|[a-zA-Z_$][a-zA-Z0-9_$]*)/;
1114

1215
const AS3_REST_ARG_MODE = {
1316
className: 'rest_arg',
14-
begin: '[.]{3}',
17+
begin: /[.]{3}/,
1518
end: IDENT_RE,
1619
relevance: 10
1720
};
1821

1922
return {
2023
name: 'ActionScript',
21-
aliases: ['as'],
24+
aliases: [ 'as' ],
2225
keywords: {
2326
keyword: 'as break case catch class const continue default delete do dynamic each ' +
2427
'else extends final finally for function get if implements import in include ' +
@@ -37,7 +40,7 @@ export default function(hljs) {
3740
className: 'class',
3841
beginKeywords: 'package',
3942
end: /\{/,
40-
contains: [hljs.TITLE_MODE]
43+
contains: [ hljs.TITLE_MODE ]
4144
},
4245
{
4346
className: 'class',
@@ -52,21 +55,21 @@ export default function(hljs) {
5255
{
5356
className: 'meta',
5457
beginKeywords: 'import include',
55-
end: ';',
58+
end: /;/,
5659
keywords: { 'meta-keyword': 'import include' }
5760
},
5861
{
5962
className: 'function',
6063
beginKeywords: 'function',
61-
end: '[{;]',
64+
end: /[{;]/,
6265
excludeEnd: true,
63-
illegal: '\\S',
66+
illegal: /\S/,
6467
contains: [
6568
hljs.TITLE_MODE,
6669
{
6770
className: 'params',
68-
begin: '\\(',
69-
end: '\\)',
71+
begin: /\(/,
72+
end: /\)/,
7073
contains: [
7174
hljs.APOS_STRING_MODE,
7275
hljs.QUOTE_STRING_MODE,
@@ -75,7 +78,7 @@ export default function(hljs) {
7578
AS3_REST_ARG_MODE
7679
]
7780
},
78-
{ begin: ':\\s*' + IDENT_FUNC_RETURN_TYPE_RE }
81+
{ begin: regex.concat(/:\s*/, IDENT_FUNC_RETURN_TYPE_RE) }
7982
]
8083
},
8184
hljs.METHOD_GUARD

src/languages/apache.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,37 @@ Contributors: Ivan Sagalaev <maniac@softwaremaniacs.org>
55
Website: https://httpd.apache.org
66
Description: language definition for Apache configuration files (httpd.conf & .htaccess)
77
Category: common, config
8+
Audit: 2020
89
*/
910

1011
/** @type LanguageFn */
1112
export default function(hljs) {
1213
const NUMBER_REF = {
1314
className: 'number',
14-
begin: '[\\$%]\\d+'
15+
begin: /[$%]\d+/
1516
};
1617
const NUMBER = {
1718
className: 'number',
18-
begin: '\\d+'
19+
begin: /\d+/
1920
};
2021
const IP_ADDRESS = {
2122
className: "number",
22-
begin: '\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}(:\\d{1,5})?'
23+
begin: /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d{1,5})?/
2324
};
2425
const PORT_NUMBER = {
2526
className: "number",
26-
begin: ":\\d{1,5}"
27+
begin: /:\d{1,5}/
2728
};
2829
return {
2930
name: 'Apache config',
30-
aliases: ['apacheconf'],
31+
aliases: [ 'apacheconf' ],
3132
case_insensitive: true,
3233
contains: [
3334
hljs.HASH_COMMENT_MODE,
3435
{
3536
className: 'section',
36-
begin: '</?',
37-
end: '>',
37+
begin: /<\/?/,
38+
end: />/,
3839
contains: [
3940
IP_ADDRESS,
4041
PORT_NUMBER,
@@ -49,26 +50,30 @@ export default function(hljs) {
4950
relevance: 0,
5051
// keywords aren’t needed for highlighting per se, they only boost relevance
5152
// for a very generally defined mode (starts with a word, ends with line-end
52-
keywords: { nomarkup:
53+
keywords: {
54+
nomarkup:
5355
'order deny allow setenv rewriterule rewriteengine rewritecond documentroot ' +
5456
'sethandler errordocument loadmodule options header listen serverroot ' +
55-
'servername' },
57+
'servername'
58+
},
5659
starts: {
5760
end: /$/,
5861
relevance: 0,
5962
keywords: { literal: 'on off all deny allow' },
6063
contains: [
6164
{
6265
className: 'meta',
63-
begin: '\\s\\[',
64-
end: '\\]$'
66+
begin: /\s\[/,
67+
end: /\]$/
6568
},
6669
{
6770
className: 'variable',
68-
begin: '[\\$%]\\{',
69-
end: '\\}',
70-
contains: ['self',
71-
NUMBER_REF]
71+
begin: /[\$%]\{/,
72+
end: /\}/,
73+
contains: [
74+
'self',
75+
NUMBER_REF
76+
]
7277
},
7378
IP_ADDRESS,
7479
NUMBER,

0 commit comments

Comments
 (0)