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

Add cst parser #38

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/dist
/node_modules/*
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"babylon": "^5.8.22",
"classnames": "^2.1.3",
"codemirror": "^5.1.0",
"cst": "0.0.11",
"escodegen": "^1.4.1",
"espree": "^2.2.4",
"esprima": "^2.5",
Expand Down
1 change: 1 addition & 0 deletions partition.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"dist/app.js": ["./src/app"],
"dist/parsers/acorn.js": ["acorn"],
"dist/parsers/babel.js": ["babylon", "babel-core", "acorn-to-esprima"],
"dist/parsers/cst.js": ["cst"],
"dist/parsers/esprima.js": ["esprima"],
"dist/parsers/espree.js": ["espree"],
"dist/parsers/recast.js": ["recast"],
Expand Down
1 change: 1 addition & 0 deletions src/getFocusPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function isInRange(range, pos) {
}

export default function getFocusPath(node, pos, parser, path) {
if (parser.id === 'cst') { return []; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove this if there's no infinite recursion issue now.

path = path || [];
let range = parser.nodeToRange(node);
if (range) {
Expand Down
62 changes: 62 additions & 0 deletions src/parsers/cst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';
import pkg from 'cst/package.json';
import loadAndExecute from './utils/loadAndExecute';
import * as LocalStorage from '../LocalStorage';
import SettingsRenderer from './utils/SettingsRenderer';

const ID = 'cst';

const options = {
strictMode: true
};

export default {
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage,

parse(code) {
return loadAndExecute(
['cst'],
cst => {
let parser = new cst.Parser();
if (!options.strictMode) {
parser.disableStrictMode();
}
return parser.parse(code);
}
);
},

nodeToRange(node) {
return node.range;
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do return node.range; I guess we need to do something different for whitespace highlighting since this will be the only one that will do that


renderSettings() {
return Settings();
},
};

let parserSettings = [
'strictMode',
];

function changeOption(name, {target}) {
if (name === 'strictMode') {
options.strictMode = target.value;
}
LocalStorage.setParserSettings(ID, options);
}

function Settings() {
return (
<div>
{SettingsRenderer({
settings: parserSettings,
values: options,
onChange: changeOption,
})}
</div>
);
}
2 changes: 2 additions & 0 deletions src/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import esprima from './esprima';
import espree from './espree';
import acorn from './acorn';
import babylon from './babylon';
import cst from './cst';
import recast from './recast';
import shift from './shift';
import babelEslint from './babel-eslint';
Expand All @@ -10,6 +11,7 @@ export var parsers = [
acorn,
babelEslint,
babylon,
cst,
espree,
esprima,
recast,
Expand Down