Skip to content

Commit 69b0ee2

Browse files
Support older Node versions
1 parent 6a3ce68 commit 69b0ee2

File tree

61 files changed

+9319
-874
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+9319
-874
lines changed

.babelrc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"presets": [
3+
[
4+
"env",
5+
{
6+
"targets": {
7+
"node": "0.12"
8+
}
9+
}
10+
],
11+
"flow"
12+
]
13+
}

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ You can also [try it online](https://astexplorer.net/#/gist/4ea2b52f0e546af6fb14
5252

5353
The _regexp-tree_ parser is implemented as an automatic LR parser using [Syntax](https://www.npmjs.com/package/syntax-cli) tool. The parser module is generated from the [regexp grammar](https://github.com/DmitrySoshnikov/regexp-tree/blob/master/src/parser/regexp.bnf), which is based on the regular expressions grammar used in ECMAScript.
5454

55-
For development from the github repository, run `build` command to generate the parser module:
55+
For development from the github repository, run `build` command to generate the parser module, and transpile JS code:
5656

5757
```
5858
git clone https://github.com/<your-github-account>/regexp-tree.git
@@ -63,8 +63,6 @@ npm run build
6363
./bin/regexp-tree --help
6464
```
6565

66-
> NOTE: You need to run `build` command every time you change the grammar file.
67-
6866
### Usage as a CLI
6967

7068
Check the options available from CLI:

bin/regexp-tree

+1-128
Original file line numberDiff line numberDiff line change
@@ -2,131 +2,4 @@
22

33
'use strict';
44

5-
const colors = require('colors');
6-
const os = require('os');
7-
const regexpTree = require('..');
8-
9-
function enforceUnique(v) {
10-
return Array.isArray(v) ? v[v.length - 1] : v;
11-
}
12-
13-
const options = require('yargs')
14-
.usage('Usage: $0 [options]')
15-
.options({
16-
expression: {
17-
alias: 'e',
18-
describe: 'A regular expression to be parsed',
19-
demandOption: true,
20-
requiresArg: true,
21-
coerce: enforceUnique,
22-
},
23-
loc: {
24-
alias: 'l',
25-
describe: 'Whether to capture AST node locations',
26-
},
27-
optimize: {
28-
alias: 'o',
29-
describe: 'Apply optimizer on the passed expression',
30-
},
31-
compat: {
32-
alias: 'c',
33-
describe: 'Apply compat-transpiler on the passed expression',
34-
},
35-
table: {
36-
alias: 't',
37-
describe: 'Print NFA/DFA transition tables (nfa/dfa/all)',
38-
nargs: 1,
39-
choices: ['nfa', 'dfa', 'all'],
40-
coerce: enforceUnique,
41-
}
42-
})
43-
.alias('help', 'h')
44-
.alias('version', 'v')
45-
.argv;
46-
47-
function shouldStripQuotes(expression) {
48-
return os.platform() === 'win32' && (
49-
(expression[0] === `'` && expression[expression.length - 1] === `'`) ||
50-
(expression[0] === '"' && expression[expression.length - 1] === '"')
51-
);
52-
}
53-
54-
function normalize(expression) {
55-
if (!shouldStripQuotes(expression)) {
56-
return expression;
57-
}
58-
59-
// For Windows strip ' at the beginning and end.
60-
return expression.slice(1, -1);
61-
}
62-
63-
function main() {
64-
const {
65-
compat,
66-
loc,
67-
optimize,
68-
table,
69-
} = options;
70-
71-
const expression = normalize(options.expression);
72-
73-
// ------------------------------------------------------
74-
// Optimizer.
75-
76-
if (optimize) {
77-
const optimized = regexpTree.optimize(expression);
78-
console.info('\n', colors.bold('Optimized:'), optimized.toString(), '\n');
79-
return;
80-
}
81-
82-
// ------------------------------------------------------
83-
// Compat-transpiler.
84-
85-
if (compat) {
86-
const compatTranspiled = regexpTree.compatTranspile(expression);
87-
console.info(
88-
'\n', colors.bold('Compat:'),
89-
compatTranspiled.toString(), '\n'
90-
);
91-
return;
92-
}
93-
94-
// ------------------------------------------------------
95-
// Transition table.
96-
if (table) {
97-
const {fa} = regexpTree;
98-
99-
const shouldPrintNFA = (table === 'nfa' || table === 'all');
100-
const shouldPrintDFA = (table === 'dfa' || table === 'all');
101-
102-
console.info(`\n${colors.bold(colors.yellow('>'))} - starting`);
103-
console.info(`${colors.bold(colors.green('✓'))} - accepting`);
104-
105-
if (shouldPrintNFA) {
106-
fa.toNFA(expression).printTransitionTable();
107-
}
108-
109-
if (shouldPrintDFA) {
110-
const dfa = fa.toDFA(expression);
111-
dfa.printTransitionTable();
112-
}
113-
114-
return;
115-
}
116-
117-
// ------------------------------------------------------
118-
// Parsing.
119-
120-
const parseOptions = {
121-
captureLocations: loc,
122-
};
123-
124-
const parsed = regexpTree.parse(expression, parseOptions);
125-
126-
console.info(JSON.stringify(parsed, null, 2));
127-
128-
}
129-
130-
if (require.main === module) {
131-
main();
132-
}
5+
require('../dist/bin/regexp-tree')();

dist/bin/regexp-tree.js

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* The MIT License (MIT)
3+
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
4+
*/
5+
6+
'use strict';
7+
8+
var colors = require('colors');
9+
var os = require('os');
10+
var regexpTree = require('../../index');
11+
12+
function enforceUnique(v) {
13+
return Array.isArray(v) ? v[v.length - 1] : v;
14+
}
15+
16+
var options = require('yargs').usage('Usage: $0 [options]').options({
17+
expression: {
18+
alias: 'e',
19+
describe: 'A regular expression to be parsed',
20+
demandOption: true,
21+
requiresArg: true,
22+
coerce: enforceUnique
23+
},
24+
loc: {
25+
alias: 'l',
26+
describe: 'Whether to capture AST node locations'
27+
},
28+
optimize: {
29+
alias: 'o',
30+
describe: 'Apply optimizer on the passed expression'
31+
},
32+
compat: {
33+
alias: 'c',
34+
describe: 'Apply compat-transpiler on the passed expression'
35+
},
36+
table: {
37+
alias: 't',
38+
describe: 'Print NFA/DFA transition tables (nfa/dfa/all)',
39+
nargs: 1,
40+
choices: ['nfa', 'dfa', 'all'],
41+
coerce: enforceUnique
42+
}
43+
}).alias('help', 'h').alias('version', 'v').argv;
44+
45+
function shouldStripQuotes(expression) {
46+
return os.platform() === 'win32' && (expression[0] === '\'' && expression[expression.length - 1] === '\'' || expression[0] === '"' && expression[expression.length - 1] === '"');
47+
}
48+
49+
function normalize(expression) {
50+
if (!shouldStripQuotes(expression)) {
51+
return expression;
52+
}
53+
54+
// For Windows strip ' at the beginning and end.
55+
return expression.slice(1, -1);
56+
}
57+
58+
function main() {
59+
var compat = options.compat,
60+
loc = options.loc,
61+
optimize = options.optimize,
62+
table = options.table;
63+
64+
65+
var expression = normalize(options.expression);
66+
67+
// ------------------------------------------------------
68+
// Optimizer.
69+
70+
if (optimize) {
71+
var optimized = regexpTree.optimize(expression);
72+
console.info('\n', colors.bold('Optimized:'), optimized.toString(), '\n');
73+
return;
74+
}
75+
76+
// ------------------------------------------------------
77+
// Compat-transpiler.
78+
79+
if (compat) {
80+
var compatTranspiled = regexpTree.compatTranspile(expression);
81+
console.info('\n', colors.bold('Compat:'), compatTranspiled.toString(), '\n');
82+
return;
83+
}
84+
85+
// ------------------------------------------------------
86+
// Transition table.
87+
if (table) {
88+
var fa = regexpTree.fa;
89+
90+
91+
var shouldPrintNFA = table === 'nfa' || table === 'all';
92+
var shouldPrintDFA = table === 'dfa' || table === 'all';
93+
94+
console.info('\n' + colors.bold(colors.yellow('>')) + ' - starting');
95+
console.info(colors.bold(colors.green('✓')) + ' - accepting');
96+
97+
if (shouldPrintNFA) {
98+
fa.toNFA(expression).printTransitionTable();
99+
}
100+
101+
if (shouldPrintDFA) {
102+
var dfa = fa.toDFA(expression);
103+
dfa.printTransitionTable();
104+
}
105+
106+
return;
107+
}
108+
109+
// ------------------------------------------------------
110+
// Parsing.
111+
112+
var parseOptions = {
113+
captureLocations: loc
114+
};
115+
116+
var parsed = regexpTree.parse(expression, parseOptions);
117+
118+
console.info(JSON.stringify(parsed, null, 2));
119+
}
120+
121+
module.exports = main;
122+
123+
if (require.main === module) {
124+
main();
125+
}

dist/compat-transpiler/index.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* The MIT License (MIT)
3+
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
4+
*/
5+
6+
'use strict';
7+
8+
var compatTransforms = require('./transforms');
9+
var _transform = require('../transform');
10+
11+
module.exports = {
12+
/**
13+
* Translates a regexp in new syntax to equivalent regexp in old syntax.
14+
*
15+
* @param string|RegExp|AST - regexp
16+
* @param Array transformsWhitelist - names of the transforms to apply
17+
*/
18+
transform: function transform(regexp) {
19+
var transformsWhitelist = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
20+
21+
var transformToApply = transformsWhitelist.length > 0 ? transformsWhitelist : Object.keys(compatTransforms);
22+
23+
var result = void 0;
24+
25+
// Collect extra data per transform.
26+
var extra = {};
27+
28+
transformToApply.forEach(function (transformName) {
29+
30+
if (!compatTransforms.hasOwnProperty(transformName)) {
31+
throw new Error('Unknown compat-transform: ' + transformName + '. ' + 'Available transforms are: ' + Object.keys(compatTransforms).join(', '));
32+
}
33+
34+
var handler = compatTransforms[transformName];
35+
36+
result = _transform.transform(regexp, handler);
37+
regexp = result.getAST();
38+
39+
// Collect `extra` transform result.
40+
if (typeof handler.getExtra === 'function') {
41+
extra[transformName] = handler.getExtra();
42+
}
43+
});
44+
45+
// Set the final extras for all transforms.
46+
result.setExtra(extra);
47+
48+
return result;
49+
}
50+
};

0 commit comments

Comments
 (0)