Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit.

  • Loading branch information...
commit 34fbcad52b59c7a18e4f8e4c6b06767e1723ef1c 0 parents
@cweider authored
Showing with 59 additions and 0 deletions.
  1. +59 −0 README.md
59 README.md
@@ -0,0 +1,59 @@
+# JS-Lex #
+
+## What Is It? ##
+A [lexer](http://en.wikipedia.org/wiki/Lexical_analysis) generator written in JavaScript that uses the language’s regular expression engine to perform its analysis.
+
+## Usage ##
+For anyone familiar with lex, flex, and friends the syntax is familiar enough. The following will create a lexer that will tokenize a JavaScript-like text. A full example can be found in the repository.
+
+ var rules = [
+ [/(?:"((?:\\.|[^"])*)"|'((?:\\.|[^'])*)')/, 'STRING'
+ , function (token) {
+ token.value = (token.match[1] || token.match[2] || '')
+ .replace(new RegExp("\\\\\"", 'g'), "\"")
+ .replace(new RegExp("\\\\\'", 'g'), "\'")
+ .replace(new RegExp("\\\\\\\\", 'g'), "\\");
+ }]
+ , [/([_a-zA-Z][_a-zA-Z0-9]*)\(/, 'FUNC'
+ , function (token) {
+ token.value = token.match[1];
+ }]
+ , [/[+-]?(?:((?:[1-9][0-9]*|0?)\.[0-9]+|NaN|Infinity)|(0x[0-9a-fA-F]+|[0-9]+))/, 'NUM'
+ , function (token) {
+ if (token.match[1]) {
+ token.value = parseFloat(token.match[0]);
+ } else {
+ token.value = parseInt(token.match[0]);
+ }
+ }]
+ , [/\)/, 'RPAREN']
+ , [/;/, 'SEMI']
+ , [/,/, 'COMMA']
+ , [/\s+/, null]
+ ];
+
+ lexer = new Lexer(rules);
+
+## Notes ##
+At the moment, `Lexer` does not support macros (the definitions section for users of lex). There are also no guards on the regular expressions preventing the use of non-regular constructs (e.g. back references) that are exposed by the language; the generated lexer will not work properly of those are used.
+
+## License ##
+Released under zlib
+
+ Copyright (C) 2010 Chad Weider
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
Please sign in to comment.
Something went wrong with that request. Please try again.