@bablr/boot 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/lib/index.js +38 -0
- package/lib/languages/cstml.js +131 -0
- package/lib/languages/instruction.js +135 -0
- package/lib/languages/regex.js +319 -0
- package/lib/languages/spamex.js +150 -0
- package/lib/languages/string.js +92 -0
- package/lib/match.js +84 -0
- package/lib/miniparser.js +434 -0
- package/lib/path.js +47 -0
- package/lib/symbols.js +4 -0
- package/lib/utils.js +119 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Conrad Buck
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const instruction = require('./languages/instruction.js');
|
|
2
|
+
const regex = require('./languages/regex.js');
|
|
3
|
+
const spamex = require('./languages/spamex.js');
|
|
4
|
+
const string = require('./languages/string.js');
|
|
5
|
+
const cstml = require('./languages/cstml.js');
|
|
6
|
+
const { TemplateParser } = require('./miniparser.js');
|
|
7
|
+
|
|
8
|
+
const buildTag = (language, defaultType) => {
|
|
9
|
+
const defaultTag = (quasis, ...exprs) => {
|
|
10
|
+
return new TemplateParser(language, quasis.raw, exprs).eval({
|
|
11
|
+
language: language.name,
|
|
12
|
+
type: defaultType,
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
return new Proxy(defaultTag, {
|
|
17
|
+
apply(defaultTag, receiver, argsList) {
|
|
18
|
+
return defaultTag.apply(receiver, argsList);
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
get(_, type) {
|
|
22
|
+
return (quasis, ...exprs) => {
|
|
23
|
+
return new TemplateParser(language, quasis.raw, exprs).eval({
|
|
24
|
+
language: language.name,
|
|
25
|
+
type,
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const i = buildTag(instruction, 'Call');
|
|
33
|
+
const spam = buildTag(spamex, 'Matcher');
|
|
34
|
+
const re = buildTag(regex, 'Pattern');
|
|
35
|
+
const str = buildTag(string, 'String');
|
|
36
|
+
const cst = buildTag(cstml, 'Node');
|
|
37
|
+
|
|
38
|
+
module.exports = { re, spam, str, i, cst };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
const Regex = require('./regex.js');
|
|
2
|
+
const StringLanguage = require('./string.js');
|
|
3
|
+
const { buildCovers } = require('../utils.js');
|
|
4
|
+
const sym = require('../symbols.js');
|
|
5
|
+
|
|
6
|
+
const _ = /\s+/y;
|
|
7
|
+
const PN = 'Punctuator';
|
|
8
|
+
const ID = 'Identifier';
|
|
9
|
+
|
|
10
|
+
const name = 'CSTML';
|
|
11
|
+
|
|
12
|
+
const dependencies = { Regex, String: StringLanguage };
|
|
13
|
+
|
|
14
|
+
const covers = buildCovers({
|
|
15
|
+
[sym.node]: ['Attribute', 'Property', 'TagType', 'Node', 'OpenNodeTag', 'CloseNodeTag'],
|
|
16
|
+
[sym.fragment]: ['Attributes'],
|
|
17
|
+
Attribute: ['StringAttribute', 'BooleanAttribute'],
|
|
18
|
+
TagType: ['Identifier', 'GlobalIdentifier'],
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const grammar = class CSTMLMiniparserGrammar {
|
|
22
|
+
Fragment(p) {
|
|
23
|
+
p.eatMatchTrivia(_);
|
|
24
|
+
while (p.match(/<[^/]/y) || p.atExpression) {
|
|
25
|
+
p.eatProduction('Property', { path: 'properties' });
|
|
26
|
+
p.eatMatchTrivia(_);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// @Node
|
|
31
|
+
Property(p) {
|
|
32
|
+
p.eat(/\w+/y, ID, { path: 'key' });
|
|
33
|
+
p.eatMatchTrivia(_);
|
|
34
|
+
p.eat(':', PN, { path: 'mapOperator' });
|
|
35
|
+
p.eatMatchTrivia(_);
|
|
36
|
+
p.eatProduction('Node', { path: 'value' });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// @Node
|
|
40
|
+
Node(p) {
|
|
41
|
+
p.eatProduction('OpenNodeTag', { path: 'open' });
|
|
42
|
+
p.eatProduction('Fragment');
|
|
43
|
+
p.eatProduction('CloseNodeTag', { path: 'close' });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// @Node
|
|
47
|
+
OpenNodeTag(p) {
|
|
48
|
+
p.eat('<', PN, { path: 'open', startSpan: 'Tag', balanced: '>' });
|
|
49
|
+
p.eatProduction('TagType', { path: 'type' });
|
|
50
|
+
|
|
51
|
+
let sp = p.eatMatchTrivia(_);
|
|
52
|
+
|
|
53
|
+
if ((sp && p.match(/\w+/y)) || p.atExpression) {
|
|
54
|
+
p.eatProduction('Attributes');
|
|
55
|
+
sp = p.eatMatchTrivia(_);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
p.eatMatchTrivia(_);
|
|
59
|
+
p.eat('>', PN, { path: 'close', endSpan: 'Tag', balancer: true });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// @Node
|
|
63
|
+
CloseNodeTag(p) {
|
|
64
|
+
p.eat('</', PN, { path: 'open', startSpan: 'Tag', balanced: '>' });
|
|
65
|
+
|
|
66
|
+
p.eat('>', PN, { path: 'close', endSpan: 'Tag', balancer: true });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// @Fragment
|
|
70
|
+
Attributes(p) {
|
|
71
|
+
let sp = true;
|
|
72
|
+
while (sp && (p.match(/\w+/y) || p.atExpression)) {
|
|
73
|
+
if (p.atExpression) {
|
|
74
|
+
p.eatProduction('Attributes'); // ??
|
|
75
|
+
} else {
|
|
76
|
+
p.eatProduction('Attribute', { path: '[attributes]' });
|
|
77
|
+
}
|
|
78
|
+
if (p.match(/\s+\w/y) || (p.match(/\s+$/y) && !p.quasisDone)) {
|
|
79
|
+
sp = p.eatMatchTrivia(_);
|
|
80
|
+
} else {
|
|
81
|
+
sp = false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// @Cover
|
|
87
|
+
Attribute(p) {
|
|
88
|
+
if (p.match(/\w+\s*=/y)) {
|
|
89
|
+
p.eatProduction('StringAttribute');
|
|
90
|
+
} else {
|
|
91
|
+
p.eatProduction('BooleanAttribute');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// @Node
|
|
96
|
+
BooleanAttribute(p) {
|
|
97
|
+
p.eat(/\w+/y, ID, { path: 'key' });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// @Node
|
|
101
|
+
StringAttribute(p) {
|
|
102
|
+
p.eat(/\w+/y, ID, { path: 'key' });
|
|
103
|
+
p.eatMatchTrivia(_);
|
|
104
|
+
p.eat('=', PN, { path: 'mapOperator' });
|
|
105
|
+
p.eatMatchTrivia(_);
|
|
106
|
+
p.eatProduction('String:String', { path: 'value' });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// @Cover
|
|
110
|
+
TagType(p) {
|
|
111
|
+
if (p.match(/\w+:/y)) {
|
|
112
|
+
p.eatProduction('GlobalIdentifier');
|
|
113
|
+
} else {
|
|
114
|
+
p.eat(/\w+/y, ID, { path: 'type' });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// @Node
|
|
119
|
+
GlobalIdentifier(p) {
|
|
120
|
+
p.eat(/\w+/y, ID, { path: 'language' });
|
|
121
|
+
p.eat(':', PN, { path: 'namespaceOperator' });
|
|
122
|
+
p.eat(/\w+/y, ID, { path: 'type' });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// @Node
|
|
126
|
+
Identifier(p) {
|
|
127
|
+
p.eatLiteral(/\w+/y);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
module.exports = { name, dependencies, covers, grammar };
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
const Spamex = require('./spamex.js');
|
|
2
|
+
const Regex = require('./regex.js');
|
|
3
|
+
const StringLanguage = require('./string.js');
|
|
4
|
+
const { node } = require('../symbols.js');
|
|
5
|
+
const { buildCovers } = require('../utils.js');
|
|
6
|
+
|
|
7
|
+
const _ = /\s+/y;
|
|
8
|
+
const PN = 'Punctuator';
|
|
9
|
+
const ID = 'Identifier';
|
|
10
|
+
const KW = 'Keyword';
|
|
11
|
+
const LIT = 'Literal';
|
|
12
|
+
|
|
13
|
+
const name = 'Instruction';
|
|
14
|
+
|
|
15
|
+
const dependencies = { Spamex, Regex, String: StringLanguage };
|
|
16
|
+
|
|
17
|
+
const covers = buildCovers({
|
|
18
|
+
[node]: ['Call', 'Punctuator', 'Property', 'Expression'],
|
|
19
|
+
Expression: [
|
|
20
|
+
'Object',
|
|
21
|
+
'Array',
|
|
22
|
+
'Tuple',
|
|
23
|
+
'Identifier',
|
|
24
|
+
'String:String',
|
|
25
|
+
'Regex:Pattern',
|
|
26
|
+
'Boolean',
|
|
27
|
+
'Null',
|
|
28
|
+
'Spamex:Matcher',
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const grammar = class InstructionMiniparserGrammar {
|
|
33
|
+
// @Node
|
|
34
|
+
Call(p) {
|
|
35
|
+
p.eat(/\w+/y, ID, { path: 'verb' });
|
|
36
|
+
p.eatMatchTrivia(_);
|
|
37
|
+
p.eatProduction('Tuple', { path: 'arguments' });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// @Cover
|
|
41
|
+
Expression(p) {
|
|
42
|
+
if (p.match('[')) {
|
|
43
|
+
p.eatProduction('Array');
|
|
44
|
+
} else if (p.match('{')) {
|
|
45
|
+
p.eatProduction('Object');
|
|
46
|
+
} else if (p.match('(')) {
|
|
47
|
+
p.eatProduction('Tuple');
|
|
48
|
+
} else if (p.match(/['"]/y)) {
|
|
49
|
+
p.eatProduction('String:String');
|
|
50
|
+
} else if (p.match('/')) {
|
|
51
|
+
p.eatProduction('Regex:Pattern');
|
|
52
|
+
} else if (p.match(/true|false/y)) {
|
|
53
|
+
p.eatProduction('Boolean');
|
|
54
|
+
} else if (p.match('null')) {
|
|
55
|
+
p.eatProduction('Null');
|
|
56
|
+
} else if (p.match(/\w/y)) {
|
|
57
|
+
p.eat(/\w+/y, ID, p.m.attributes);
|
|
58
|
+
} else if (p.match('<')) {
|
|
59
|
+
p.eatProduction('Spamex:Matcher');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// @Node
|
|
64
|
+
Object(p) {
|
|
65
|
+
p.eat('{', PN, { path: 'open', balanced: '}' });
|
|
66
|
+
|
|
67
|
+
p.eatMatchTrivia(_);
|
|
68
|
+
|
|
69
|
+
let first = true;
|
|
70
|
+
let sep;
|
|
71
|
+
while (first || (sep && (p.match(/./y) || p.atExpression))) {
|
|
72
|
+
p.eatProduction('Property', { path: '[properties]' });
|
|
73
|
+
sep = p.eatMatchTrivia(_);
|
|
74
|
+
first = false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
p.eatMatchTrivia(_);
|
|
78
|
+
|
|
79
|
+
p.eat('}', PN, { path: 'close', balancer: true });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// @Node
|
|
83
|
+
Property(p) {
|
|
84
|
+
p.eat(/\w+/y, LIT, { path: 'key' });
|
|
85
|
+
p.eatMatchTrivia(_);
|
|
86
|
+
p.eat(':', PN, { path: 'mapOperator' });
|
|
87
|
+
p.eatMatchTrivia(_);
|
|
88
|
+
p.eatProduction('Expression', { path: 'value' });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// @Node
|
|
92
|
+
Array(p) {
|
|
93
|
+
p.eat('[', PN, { path: 'open', balanced: ']' });
|
|
94
|
+
|
|
95
|
+
p.eatMatchTrivia(_);
|
|
96
|
+
|
|
97
|
+
let first = true;
|
|
98
|
+
let sep;
|
|
99
|
+
while (first || (sep && (p.match(/./y) || p.atExpression))) {
|
|
100
|
+
p.eatProduction('Expression', { path: '[elements]' });
|
|
101
|
+
sep = p.eatMatchTrivia(_);
|
|
102
|
+
first = false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
p.eat(']', PN, { path: 'close', balancer: true });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// @Node
|
|
109
|
+
Tuple(p) {
|
|
110
|
+
p.eat('(', PN, { path: 'open', balanced: ')' });
|
|
111
|
+
|
|
112
|
+
let sep = p.eatMatchTrivia(_);
|
|
113
|
+
|
|
114
|
+
let i = 0;
|
|
115
|
+
while (i === 0 || (sep && (p.match(/./y) || p.atExpression))) {
|
|
116
|
+
p.eatProduction('Expression', { path: '[values]' });
|
|
117
|
+
sep = p.eatMatchTrivia(_);
|
|
118
|
+
i++;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
p.eat(')', PN, { path: 'close', balancer: true });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// @Node
|
|
125
|
+
Boolean(p) {
|
|
126
|
+
p.eat(/true|false/y, KW, { path: 'value' });
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// @Node
|
|
130
|
+
Null(p) {
|
|
131
|
+
p.eat(/null/y, KW, { path: 'value' });
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
module.exports = { name, dependencies, covers, grammar };
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
const when = require('iter-tools-es/methods/when');
|
|
2
|
+
const { escapables } = require('./string.js');
|
|
3
|
+
const { buildCovers } = require('../utils.js');
|
|
4
|
+
const { node } = require('../symbols.js');
|
|
5
|
+
|
|
6
|
+
const name = 'Regex';
|
|
7
|
+
|
|
8
|
+
const dependencies = {};
|
|
9
|
+
|
|
10
|
+
const covers = buildCovers({
|
|
11
|
+
[node]: [
|
|
12
|
+
'RegExpLiteral',
|
|
13
|
+
'Flag',
|
|
14
|
+
'Pattern',
|
|
15
|
+
'Alternative',
|
|
16
|
+
'Group',
|
|
17
|
+
'CapturingGroup',
|
|
18
|
+
'Assertion',
|
|
19
|
+
'Character',
|
|
20
|
+
'CharacterClass',
|
|
21
|
+
'CharacterClassRange',
|
|
22
|
+
'CharacterSet',
|
|
23
|
+
'Quantifier',
|
|
24
|
+
'Punctuator',
|
|
25
|
+
'Keyword',
|
|
26
|
+
'Escape',
|
|
27
|
+
'Number',
|
|
28
|
+
],
|
|
29
|
+
CharacterClassElement: ['CharacterClassRange', 'Character'],
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const flags = {
|
|
33
|
+
global: 'g',
|
|
34
|
+
ignoreCase: 'i',
|
|
35
|
+
multiline: 'm',
|
|
36
|
+
dotAll: 's',
|
|
37
|
+
unicode: 'u',
|
|
38
|
+
sticky: 'y',
|
|
39
|
+
};
|
|
40
|
+
const flagsReverse = Object.fromEntries(Object.entries(flags).map(([key, value]) => [value, key]));
|
|
41
|
+
|
|
42
|
+
const PN = 'Punctuator';
|
|
43
|
+
const KW = 'Keyword';
|
|
44
|
+
const ESC = 'Escape';
|
|
45
|
+
|
|
46
|
+
const unique = (flags) => flags.length === new Set(flags).size;
|
|
47
|
+
|
|
48
|
+
const getSpecialPattern = (span) => {
|
|
49
|
+
const { type } = span;
|
|
50
|
+
if (type === 'Bare') {
|
|
51
|
+
return /[*+{}\[\]()\.^$|\\\n\/]/y;
|
|
52
|
+
} else if (type === 'CharacterClass') {
|
|
53
|
+
return /[\]\\\.]/y;
|
|
54
|
+
} else if (type === 'CharacterClass:First') {
|
|
55
|
+
return /[\]^\\\.]/y;
|
|
56
|
+
} else if (type === 'Quantifier') {
|
|
57
|
+
return /[{}]/;
|
|
58
|
+
} else {
|
|
59
|
+
throw new Error();
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const cookEscape = (escape, span) => {
|
|
64
|
+
let hexMatch;
|
|
65
|
+
|
|
66
|
+
if (!escape.startsWith('\\')) {
|
|
67
|
+
throw new Error('regex escape must start with \\');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if ((hexMatch = /\\x([0-9a-f]{2})/iy.exec(escape))) {
|
|
71
|
+
//continue
|
|
72
|
+
} else if ((hexMatch = /\\u([0-9a-f]{4})/iy.exec(escape))) {
|
|
73
|
+
//continue
|
|
74
|
+
} else if ((hexMatch = /\\u{([0-9a-f]+)}/iy.exec(escape))) {
|
|
75
|
+
//continue
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (hexMatch) {
|
|
79
|
+
return parseInt(hexMatch[1], 16);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let litMatch = /\\([nrt0])/y.exec(escape);
|
|
83
|
+
|
|
84
|
+
if (litMatch) {
|
|
85
|
+
return escapables.get(litMatch[1]);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let specialMatch = getSpecialPattern(span).exec(escape.slice(1));
|
|
89
|
+
|
|
90
|
+
if (specialMatch) {
|
|
91
|
+
return specialMatch[0];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
throw new Error('unable to cook regex escape');
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const grammar = class RegexMiniparserGrammar {
|
|
98
|
+
// @Node
|
|
99
|
+
Pattern(p) {
|
|
100
|
+
p.eat('/', PN, { path: 'open', balanced: '/' });
|
|
101
|
+
p.eatProduction('Alternatives', { path: '[alternatives]' });
|
|
102
|
+
p.eat('/', PN, { path: 'close', balancer: true });
|
|
103
|
+
p.eatProduction('Flags', { path: '[flags]' });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
Flags(p) {
|
|
107
|
+
const flags = p.match(/[gimsuy]+/y) || '';
|
|
108
|
+
|
|
109
|
+
if (!unique(flags)) throw new Error('flags must be unique');
|
|
110
|
+
|
|
111
|
+
for (const _ of flags) {
|
|
112
|
+
p.eatProduction('Flag');
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// @Node
|
|
117
|
+
Flag(p) {
|
|
118
|
+
const flag = p.eatMatch(/[gimsuy]/y, KW, { path: 'value' });
|
|
119
|
+
|
|
120
|
+
return { attrs: { kind: flagsReverse[flag] } };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
Alternatives(p) {
|
|
124
|
+
do {
|
|
125
|
+
p.eatProduction('Alternative');
|
|
126
|
+
} while (p.eatMatch('|', PN, { path: '[separators]' }));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// @Node
|
|
130
|
+
Alternative(p) {
|
|
131
|
+
p.eatProduction('Elements', { path: '[elements]' });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
Elements(p) {
|
|
135
|
+
while (p.match(/[^|]/y)) {
|
|
136
|
+
p.eatProduction('Element');
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// @Cover
|
|
141
|
+
Element(p) {
|
|
142
|
+
if (p.match('[')) {
|
|
143
|
+
p.eatProduction('CharacterClass');
|
|
144
|
+
} else if (p.match('(?:')) {
|
|
145
|
+
p.eatProduction('Group');
|
|
146
|
+
} else if (p.match(/\(\?<?[=!]/y)) {
|
|
147
|
+
throw new Error('Lookahead and lookbehind are not supported');
|
|
148
|
+
} else if (p.match('(')) {
|
|
149
|
+
p.eatProduction('CapturingGroup');
|
|
150
|
+
} else if (p.match(/[$^]|\\b|/iy)) {
|
|
151
|
+
p.eatProduction('Assertion');
|
|
152
|
+
} else if (p.match(/\.|\\[dswp]/iy)) {
|
|
153
|
+
p.eatProduction('CharacterSet');
|
|
154
|
+
} else {
|
|
155
|
+
p.eatProduction('Character');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (p.match(/[*+?]|{/y)) {
|
|
159
|
+
p.shiftProduction('Quantifier');
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// @Node
|
|
164
|
+
Group(p) {
|
|
165
|
+
p.eat('(?:', PN, { path: 'open', balanced: ')' });
|
|
166
|
+
p.eatProduction('Alternatives', { path: '[alternatives]' });
|
|
167
|
+
p.eat(')', PN, { path: 'close', balancer: true });
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// @Node
|
|
171
|
+
CapturingGroup(p) {
|
|
172
|
+
p.eat('(', PN, { path: 'open', balanced: ')' });
|
|
173
|
+
p.eatProduction('Alternatives', { path: '[alternatives]' });
|
|
174
|
+
p.eat(')', PN, { path: 'close', balancer: true });
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// @Node
|
|
178
|
+
Assertion(p) {
|
|
179
|
+
let attrs = {};
|
|
180
|
+
if (p.eatMatch('^', PN, { path: 'value' })) {
|
|
181
|
+
attrs = { kind: 'start' };
|
|
182
|
+
} else if (p.eatMatch('$', KW, { path: 'value' })) {
|
|
183
|
+
attrs = { kind: 'end' };
|
|
184
|
+
} else {
|
|
185
|
+
if (p.eatMatch('\\', ESC, { path: 'escape' })) {
|
|
186
|
+
const m = p.eat(/b/iy, KW, { path: 'value' });
|
|
187
|
+
attrs = { kind: 'word', negate: m === 'B' };
|
|
188
|
+
} else {
|
|
189
|
+
throw new Error('invalid boundary');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return { attrs };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// @Node
|
|
196
|
+
Character(p) {
|
|
197
|
+
const specialPattern = getSpecialPattern(p.span);
|
|
198
|
+
|
|
199
|
+
if (p.match('\\')) {
|
|
200
|
+
if (
|
|
201
|
+
p.eatMatchEscape(
|
|
202
|
+
new RegExp(
|
|
203
|
+
String.raw`\\(u(\{\d{1,6}\}|\d{4})|x[0-9a-fA-F]{2}|[nrt0]|${specialPattern.source})`,
|
|
204
|
+
'y',
|
|
205
|
+
),
|
|
206
|
+
)
|
|
207
|
+
) {
|
|
208
|
+
// done
|
|
209
|
+
} else {
|
|
210
|
+
throw new Error('escape required');
|
|
211
|
+
}
|
|
212
|
+
} else {
|
|
213
|
+
if (p.match(new RegExp(specialPattern, 'y'))) {
|
|
214
|
+
throw new Error('escape required');
|
|
215
|
+
} else {
|
|
216
|
+
p.eatLiteral(/./sy);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// @Node
|
|
222
|
+
CharacterClass(p) {
|
|
223
|
+
p.eat('[', PN, { path: 'open', balanced: ']', startSpan: 'CharacterClass' });
|
|
224
|
+
|
|
225
|
+
const negate = !!p.eatMatch('^', KW, { path: 'negate', boolean: true });
|
|
226
|
+
|
|
227
|
+
let first = !negate;
|
|
228
|
+
while (p.match(/./sy)) {
|
|
229
|
+
p.eatProduction('CharacterClassElement', { path: '[elements]' }, { first });
|
|
230
|
+
first = false;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
p.eat(']', PN, { path: 'close', balancer: true, endSpan: 'CharacterClass' });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// @Cover
|
|
237
|
+
CharacterClassElement(p, { first }) {
|
|
238
|
+
if (p.match(/.-[^\]\n]/y)) {
|
|
239
|
+
p.eatProduction('CharacterClassRange', undefined, { first });
|
|
240
|
+
} else if (p.match(/\.|\\[dswp]/iy)) {
|
|
241
|
+
p.eatProduction('CharacterSet');
|
|
242
|
+
} else {
|
|
243
|
+
p.eatProduction('Character', when(first, { span: 'CharacterClass:First' }));
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// @Node
|
|
248
|
+
CharacterClassRange(p, { first }) {
|
|
249
|
+
p.eatProduction('Character', {
|
|
250
|
+
path: 'min',
|
|
251
|
+
...when(first, { span: 'CharacterClass:First' }),
|
|
252
|
+
});
|
|
253
|
+
p.eat('-', PN, { path: 'rangeOperator' });
|
|
254
|
+
p.eatProduction('Character', { path: 'max' });
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// @Node
|
|
258
|
+
CharacterSet(p) {
|
|
259
|
+
if (p.eatMatch('.', KW, { path: 'value' })) {
|
|
260
|
+
return { attrs: { kind: 'any' } };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
p.eat('\\', PN, { path: 'escape' });
|
|
264
|
+
|
|
265
|
+
let attrs;
|
|
266
|
+
|
|
267
|
+
if (p.eatMatch('d', KW, { path: 'value' })) {
|
|
268
|
+
attrs = { kind: 'digit' };
|
|
269
|
+
} else if (p.eatMatch('D', KW, { path: 'value' })) {
|
|
270
|
+
attrs = { kind: 'digit', negate: true };
|
|
271
|
+
} else if (p.eatMatch('s', KW, { path: 'value' })) {
|
|
272
|
+
attrs = { kind: 'space' };
|
|
273
|
+
} else if (p.eatMatch('S', KW, { path: 'value' })) {
|
|
274
|
+
attrs = { kind: 'space', negate: true };
|
|
275
|
+
} else if (p.eatMatch('w', KW, { path: 'value' })) {
|
|
276
|
+
attrs = { kind: 'word' };
|
|
277
|
+
} else if (p.eatMatch('W', KW, { path: 'value' })) {
|
|
278
|
+
attrs = { kind: 'word', negate: true };
|
|
279
|
+
} else if (p.match(/p/iy)) {
|
|
280
|
+
throw new Error('unicode property character sets are not supported yet');
|
|
281
|
+
} else {
|
|
282
|
+
throw new Error('unknown character set kind');
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return { attrs };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// @Node
|
|
289
|
+
Quantifier(p) {
|
|
290
|
+
p.eatHeldProduction('Element', { path: 'element' });
|
|
291
|
+
|
|
292
|
+
let attrs;
|
|
293
|
+
|
|
294
|
+
if (p.eatMatch('*', KW, { path: 'value' })) {
|
|
295
|
+
attrs = { min: 0, max: Infinity };
|
|
296
|
+
} else if (p.eatMatch('+', KW, { path: 'value' })) {
|
|
297
|
+
attrs = { min: 1, max: Infinity };
|
|
298
|
+
} else if (p.eatMatch('?', KW, { path: 'value' })) {
|
|
299
|
+
attrs = { min: 0, max: 1 };
|
|
300
|
+
} else if (p.match('{')) {
|
|
301
|
+
p.eat('{', PN, { path: 'open', balanced: '}' });
|
|
302
|
+
|
|
303
|
+
let max;
|
|
304
|
+
let min = p.eat(/\d+/y, 'Number', { path: 'min' });
|
|
305
|
+
|
|
306
|
+
if (p.eatMatch(',', PN, { path: 'separator' })) {
|
|
307
|
+
max = p.eatMatch(/\d+/y, 'Number', { path: 'max' });
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
attrs = { min, max };
|
|
311
|
+
|
|
312
|
+
p.eat('}', PN, { path: 'close', balancer: true });
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return { attrs };
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
module.exports = { name, dependencies, covers, grammar, cookEscape };
|