Skip to content

Commit

Permalink
add engine262 js and regex parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Nov 26, 2021
1 parent a477e48 commit 8eb9d6a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@engine262/engine262": "^0.0.1-87d618ac7a3df080f966a5862b80ea66a57c1ca4"
}
}
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@babel/eslint-parser": "^7.12.0",
"@babel/runtime": "^7.15.3",
"@creditkarma/thrift-parser": "^1.2.0",
"@engine262/engine262": "^0.0.1-87d618ac7a3df080f966a5862b80ea66a57c1ca4",
"@gengjiawen/monkey-wasm": "^0.7.0",
"@glimmer/compiler": "^0.73.1",
"@glimmer/syntax": "^0.73.1",
Expand Down
66 changes: 66 additions & 0 deletions website/src/parsers/js/engine262.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import defaultParserInterface from '../utils/defaultParserInterface';

const pkg = {
version: '0.0.1-x',
homepage: 'https://engine262.js.org',
};

const ID = 'engine262';

export default {
...defaultParserInterface,

id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage,
locationProps: new Set(['location']),

loadParser(callback) {
require(['@engine262/engine262'], callback);
},

parse(e262, source, options) {
e262.setSurroundingAgent(new e262.Agent());
const p = new e262.Parser({ source, json: false });
const parseMethod = options.sourceType === 'module' ?
'parseModule' :
'parseScript';
const ast = p[parseMethod]();
if (options.earlyErrors && p.earlyErrors.size > 0) {
throw [...p.earlyErrors][0];
}
return ast;
},

nodeToRange({ location }) {
if (location) {
return [location.startIndex, location.endIndex];
}
},

opensByDefault(node, key) {
return key !== 'location';
},

getDefaultOptions() {
return {
earlyErrors: false,
sourceType: 'module',
};
},

_getSettingsConfiguration() {
return {
fields: [
['sourceType', ['script', 'module']],
'earlyErrors',
],
};
},

_ignoredProperties: new Set([
'strict',
'arrowInfo',
]),
};
59 changes: 59 additions & 0 deletions website/src/parsers/regexp/engine262.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import defaultParserInterface from '../utils/defaultParserInterface';

const pkg = {
version: '0.0.1-x',
homepage: 'https://engine262.js.org',
};

const ID = 'engine262-re';

export default {
...defaultParserInterface,

id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage,
locationProps: new Set([]),

loadParser(callback) {
require(['@engine262/engine262'], callback);
},

parse(e262, source, options) {
e262.setSurroundingAgent(new e262.Agent());
const jsParser = new e262.Parser({ source, json: false });
const jsExp = jsParser.parseRegularExpressionLiteral();

const parse = (flags) => {
const p = new e262.RegExpParser(jsExp.RegularExpressionBody);
return p.scope(flags, () => p.parsePattern());
};

if (jsExp.RegularExpressionFlags.includes('u')) {
return parse({ U: true, N: true });
} else {
const pattern = parse({ U: false, N: false });
if (pattern.groupSpecifiers.size > 0) {
return parse({ U: false, N: true });
}
return pattern;
}
},

nodeToRange() {},

opensByDefault(node, key) {
return true;
},

getDefaultOptions() {
return {};
},

_getSettingsConfiguration() {
return {};
},

_ignoredProperties: new Set(['groupSpecifiers', 'capturingGroups', 'capturingParenthesesBefore']),
};
1 change: 1 addition & 0 deletions website/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ module.exports = Object.assign({
path.join(__dirname, 'node_modules', 'tslint'),
path.join(__dirname, 'node_modules', 'tslib'),
path.join(__dirname, 'node_modules', 'svelte'),
path.join(__dirname, 'node_modules', '@engine262/engine262'),
path.join(__dirname, 'src'),
],
loader: 'babel-loader',
Expand Down
5 changes: 5 additions & 0 deletions website/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,11 @@
resolved "https://registry.yarnpkg.com/@creditkarma/thrift-parser/-/thrift-parser-1.2.0.tgz#69b47dff3d2a5211e9417ff543ea65f681821906"
integrity sha512-mSRxjSXvU6sBfWhMBWXl7H/XEKWHQ7x+MUDLwyeFIwwX9UaZKPOc7TgGd0N78kcZtIMk82ArdxO6aFhqnBuNtg==

"@engine262/engine262@^0.0.1-87d618ac7a3df080f966a5862b80ea66a57c1ca4":
version "0.0.1-87d618ac7a3df080f966a5862b80ea66a57c1ca4"
resolved "https://registry.yarnpkg.com/@engine262/engine262/-/engine262-0.0.1-87d618ac7a3df080f966a5862b80ea66a57c1ca4.tgz#a9b6e18a46c264258ec7de6e0ac6cf13acd34c31"
integrity sha512-Rx88gj2s0WxDou2+qI6cBELHjpdsVo7heG8h7q9kZtfMYZU7it1dcEpONeptszM1KnrDQTrb4HjKi5nraXw/qQ==

"@formatjs/ecma402-abstract@1.5.0":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@formatjs/ecma402-abstract/-/ecma402-abstract-1.5.0.tgz#759c8f11ff45e96f8fb58741e7fbdb41096d5ddd"
Expand Down

0 comments on commit 8eb9d6a

Please sign in to comment.