-
-
Notifications
You must be signed in to change notification settings - Fork 255
Add a parseExpression method #210
Comments
Try this: Example 1: import * as babylon from "babylon";
import traverse from "babel-traverse";
const ast = babylon.parse('expr='+exprfoo);
traverse(ast, {
// will called each time when "BinaryExpression" is found
BinaryExpression(path){
let res = path.get('right') // log: exprfoo
},
}); see handbook, very useful: https://github.com/thejameskyle/babel-handbook/blob/master/translations/de/plugin-handbook.md babel-types: https://github.com/babel/babel/blob/master/packages/babel-types/README.md |
Personally I'd wrap it in parens and go with that, e.g.
I'm somewhat hesitant to add an API for this since it's easy enough to do with the existing one. |
@SerkanSipahi thanks for the snippet. Nevertheless, I am trying to use a subset of what babylon does, trying to see if there can be an entry point inside babylon that would improve the performance of building the AST of something we know should be an expression (and throw if not) @loganfsmyth your solution with "(..)" is maybe more robust than mine with "expr=.." I'll try that. I would have to revise my javascript 101 to make sure which pattern would always be parsed as an expression. do you think I could get faster parsing with access to internals ? (skip the |
And, another disadvantage with such a solution is that inputs like |
@TimothyGu indeed ! "=a" leads to an expression with "expr=.." so I guess none of these solutions are foolproof except if we really find a pattern that would force the expression parsing (your example made me realize that may not exist). |
Seeing the code on https://github.com/pugjs/is-expression-babylon/blob/master/src/index.js and on https://github.com/babel/babylon/blob/master/src/parser/index.js, do you think that babylon could export the Parser or the Tokenizer so that we can sub-class it again ? or would you agree with a PR that would allow for parsing specific sub-parts of js like
It would be great if we could have that again ! Feel free to comment on this and tell me what kind of PR has the best chance of beeing accepted (exporting the parser/tokenizer or a new assertExpression method) Thanks ! |
Done in #213 |
Hello,
I would like to use babylon as a fast expression parser to build an AST based code generator for the pug templating language. I was pointed to
is-expression-babylon
(https://github.com/pugjs/is-expression-babylon) that has a getExpression method.But this module is now broken (I believe it broke after the migration to rollup).
Currently I use the code
it works but I suspect that this is not the most efficient way to simply parse an expression.
Would it be possible for someone with good knowledge of babylon internals to export an efficient
parseExpression
method ?I could try an write a PR but would need directions. It is hard for me to know if what
is-expression-babylon
did is the most efficient use of babylon's internals.Thanks for your help.
The text was updated successfully, but these errors were encountered: