Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Cherow for parsing instead of Acorn.js #81

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

--next

-D cherow_parser
-lib hxnodejs
-cp tool/src
HxSplit
Expand Down
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"@elsassph/fast-source-map": "^0.3.0",
"acorn": "^4.0.3",
"cherow": "^1.6.6",
"graphlib": "^2.1.1",
"react-deep-force-update": "^2.0.1",
"react-proxy": "^2.0.8",
Expand Down
27 changes: 16 additions & 11 deletions tool/bin/split.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tool/src/HxSplit.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HxSplit
astHooks:Array<Graph->String->Array<String>>)
{
// parse input
var src = Fs.readFileSync(input).toString();
var src = Fs.readFileSync(input, "utf8");
var parser = new Parser(src, debugMode, commonjs);
var sourceMap = debugMode ? new SourceMap(input, src) : null;

Expand Down
23 changes: 18 additions & 5 deletions tool/src/Parser.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import acorn.Acorn;
import acorn.Cherow;
import graphlib.Graph;
import haxe.DynamicAccess;
import acorn.Acorn;

class Parser
{
Expand All @@ -26,7 +27,11 @@ class Parser
public function new(src:String, withLocation:Bool, commonjs:Bool)
{
var t0 = Date.now().getTime();
processInput(src, withLocation);
#if cherow_parser
processInputAcorn(src, withLocation);
#else
processInputCherow(src, withLocation);
#end
var t1 = Date.now().getTime();
trace('Parsed in: ${t1 - t0}ms');

Expand All @@ -35,32 +40,40 @@ class Parser
trace('AST processed in: ${t2 - t1}ms');
}

function processInput(src:String, withLocation:Bool)
function processInputAcorn(src:String, withLocation:Bool)
{
var options:AcornOptions = { ecmaVersion: 5, allowReserved: true };
if (withLocation) options.locations = true;
var program = Acorn.parse(src, options);
walkProgram(program);
}

function processInputCherow(src:String, withLocation:Bool)
{
var program = Cherow.parse(src, { ranges: true, loc: withLocation });
walkProgram(program);
}

function buildGraph(commonjs:Bool)
{
var g = new Graph({ directed: true, compound:true });
var cpt = 0;
var refs = 0;
for (t in types.keys()) {
var keys = types.keys();
for (t in keys) {
cpt++;
g.setNode(t, t);
}

if (!commonjs) {
// require stub is generated in web entry point
types.set('require', []);
keys.push('require');
g.setNode('require', 'require');
g.setEdge(mainModule, 'require');
}

for (t in types.keys())
for (t in keys)
refs += walk(g, t, types.get(t));

trace('Stats: $cpt types, $refs references');
Expand Down
26 changes: 26 additions & 0 deletions tool/src/acorn/Cherow.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package acorn;

import acorn.Acorn;

typedef CherowOptions = {
?module: Bool, // Enable module syntax
?loc: Bool, // Attach line/column location information to each node
?ranges: Bool, // Append start and end offsets to each node
?impliedStrict: Bool, // Enable strict mode initial enforcement
?next: Bool, // Enable stage 3 support (ESNext)
?tolerant: Bool, // Create a top-level error array containing all "skipped" errors
?source: Bool, // Set to true to record the source file in every node's loc object when the loc option is set.
?raw: Bool, // Attach raw property to each literal node
?rawIdentifier: Bool, // Attach raw property to each identifier node
}

@:jsRequire('cherow')
extern class Cherow {

/**
* is used to parse a JavaScript program. The input parameter is a string, options can
* be undefined or an object setting some of the options listed below. The return value
* will be an abstract syntax tree object as specified by the ESTree spec.
*/
static public function parse(input:String, options:CherowOptions):AstNode;
}
7 changes: 3 additions & 4 deletions tool/test/test-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ function execTest(className, name, params, isNode, callback) {
const folder = `tool/test/bin/${name}`;
if (useLib[className]) params += ' -D uselib';
var cmd = `haxe tool/test/test-common.hxml -main ${className} -js ${folder}/index.js ${params}`;
//console.log(cmd);
console.log(cmd);
exec(cmd, (err, stdout, stderr) => {
console.log(stdout);
if (err) {
hasFailedCase = 1;
console.log(stderr);
callback(err);
} else {
console.log(stdout);
runValidation(name, isNode, callback);
}
});
Expand All @@ -132,12 +132,11 @@ function runValidation(name, isNode, callback) {
const result = `tool/test/bin/${name}/index.js.json`;
const valid = `tool/test/expect/${name}.json`;
exec(`node tool/test/validate.js ${result} ${valid}`, (err, stdout, stderr) => {
console.log(stdout);
if (err) {
hasFailedCase = 2;
console.log(stdout);
callback(err);
} else {
console.log(stdout);
if (isNode) runOutput(name, callback);
else callback();
}
Expand Down