user5858
user5858

Reputation: 1221

Parsing Javascript code using Flex parser

My motive is to mangle variable and function names and also encrypt strings in a javascript file.

For this I only need to separate strings, comments, and variable/function names.

I've tried UglifyJs2 but I need more control on myself so I tried to write a lexer myself using Flex.

I'm able to take care of comments and quoted strings.

However I'm stuck in regular expression format for example /"/ -- a regular expression containing quotes causing correct parsing to fail.

Looks like to correctly identify a regular expression i'd need Bison parser using grammar rules otherwise comments, strings and regular expression get mixed up. I don't want to get that far and use Bison.

One way is to move all regular expression code to another file in functions.

Is there any other alternative so that I can handle this in Flex itself?

Upvotes: 0

Views: 276

Answers (1)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

If you can run JavaScript, you can use Esprima, a JavaScript parser coded in JavaScript. It can even run in your browser or any runtime like NodeJS.

It can output just tokens or abstract syntax trees. I believe that this should enough for you.

Upvotes: 1

Related Questions