I have a project where a user needs to define a set of instructions for a ui that is completely written in javascript. I need to have the ability to parse a string of instructions and then translate them into instructions. Is there any libraries out there for parsing that are 100% javascript? Or a generator that will generate in javascript? Thanks!
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Correctly parse PDF paragraphs with Python
- Keeping track of variable instances
For simple parsing tasks I'm quite fond of using a variant of Pratt's Top Down Operator Precedence parser. While Pratt wrote the original paper using an old Lisp dialect, the same concepts can easily be used in most any language. In fact, Douglas Crockford wrote an excellent article on Top Down Operator Precedence parsing in JavaScript, which might be just what you need.
Something like http://jscc.phorward-software.com/, maybe?
if you're really looking for just a lexer, try prettify.
Depending on the design of the 'set of instructions', you may be able to use Javascript's built-in
eval
function, which parses Javascript source; you may be able to write a simple translator to convert the instructions to Javascript code.By the way, be very careful about XSS holes.
I was looking for something similar that wouldn't have any security holes and I came across two resources. They don't parse the script, but actually run it in a "safe" environment - something you can't guarantee when using the
eval
function. So, I don't know if it's exactly what you are looking for but take a look:If you want a lexer and nothing but a lexer then take a look at this: https://github.com/aaditmshah/lexer
It's a pure JavaScript lexer with lots of powerful features written in just a few lines of code.