-
I asked this question on SO, and although it received positive response in terms of upvotes and stars, there are no comments or answers. So perhaps it's outside the scope of expertise of users who track the babeljs tag. I would like to add custom operators, keywords, and syntax to Javascript. Someone asked for help with this many years ago and got some great instructions: Implemented here: This method does not work with the most current version of Is this possible with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Great question @abalter! Short answer with Babel at the moment is you need to fork the parser itself + use the
There was a good, older article about this, where that day we removed the ability to do so unfortunately (by bundling the parser, we disallowed modifying any internals). @tanhauhau has a good article on a current way to do this: https://lihautan.com/creating-custom-javascript-syntax-with-babel. If the new syntax is a TC39 proposal, then someone (our the proposal champion) could implement it on their own and send a PR to Babel. Example (record and tuple syntax): #10865. You can also look through old PRs that were modifying the parser and were a new feature import customParser from './custom-parser';
export default function ourBabelPlugin() {
return {
parserOverride(code, opts) {
return customParser.parse(code, opts);
},
};
} |
Beta Was this translation helpful? Give feedback.
-
Thanks @hzoo. I'm getting clearer that what I'm really after is a language that is very close to Javascript, and easily compiles to Javascript, but has some syntactic elements more in line with data-centric languages such as Python, R, Matlab, ... Examples:
Hopefully only a small number of other things. Maybe the module you load could be a forked version of babel. Right now I know almost nothing about ASTs, parsers, and compilers. So much to learn... |
Beta Was this translation helpful? Give feedback.
Great question @abalter! Short answer with Babel at the moment is you need to fork the parser itself + use the
parserOverride
option in a plugin to parse it differently. (we made it rather difficult to extend on purpose, although I would like people to experiment at least to learn about parsers/ASTs/languages). And you can definitely do this with other parsers (esprima/acorn) as well.There was a good, older article about this, where that day we removed the abilit…