Something like http://jscc.phorward-software.com/, maybe?
The platform-independent software 21 unions both: A regular expression-based lexical analyzer generator matching individual tokens 20 from the input character stream and a LALR(1) parser generator, computing 19 the parse tables for a given context-free grammar specification 18 and building a stand-alone, working parser. The 17 context-free grammar fed to JS/CC is defined 16 in a Backus-Naur-Form-based meta language, and allows the 15 insertion of individual semantic code to 14 be evaluated on a rule's reduction.
A MetaII Compiler 18 tutorial walks you through building a completely 17 self-contained compiler system that can 16 translate itself and other languages:
This 15 is all based on an amazing little 10-page 14 technical paper by Val Schorre: META II: A 13 Syntax-Oriented Compiler Writing Language 12 from honest-to-god 1964. The MetaII compiler 11 complete self-description is about 30 lines! I 10 learned how to build compilers from this 9 back in 1970. There's a mind-blowing moment 8 when you finally grok how the compiler can 7 regenerate itself....
I 2 know the website author from my college 1 days, but have nothing to do with the website.
If 2 you want just a light weight lexer (~100 1 sloc) you can take a look at Lexed.js: https://github.com/tantaman/lexed.js
This example even includes 7 support for multiple natural languages (English 6 & German) using grammar inheritance.
- Lookahead function creation (deciding which alternative to take)
- Automatic Error Recovery.
- Left recursion detection
- Ambiguity Detection.
- Position information.
as Chevrotain handles 1 that automatically.
By the way, be very careful about XSS 1 holes.
If you want a lexer and nothing but a lexer 3 then take a look at this: https://github.com/aaditmshah/lexer
if you're really looking for just a lexer, try 1 prettify.
I was looking for something similar that 7 wouldn't have any security holes and I came 6 across two resources. They don't parse the 5 script, but actually run it in a "safe" environment 4 - something you can't guarantee when using 3 the
eval function. So, I don't know if it's 2 exactly what you are looking for but take 1 a look:
- Google Caja - virtual iframe.
More Related questions