notes on javascript, linux, and more

4.13.2009

A simple general-purpose Lexer written in JavaScript

If you need to tokenize a string, this code might come in handy. Just modify the regular expressions in 'definitions' to suit your needs.

/*
Copyright (c) 2009 Elijah Rutschman <elijahr@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

var definitions = {
identifier : { regexp : /^[a-zA-Z]+[a-zA-Z0-9]*$/, skip : false },
string : { regexp : /^\"[^\"]*\"|\'[^\']*\'$/, skip : false },
decimal_integer : { regexp : /^[1-9]*\d+$/, skip : false },
left_parenthesis : { regexp : /^\($/, skip : false },
right_parenthesis : { regexp : /^\)$/, skip : false },
comma : { regexp : /^,$/, skip : false },
whitespace : { regexp : /^[\t \n]$/, skip : true }
}

var Lexer = function ( string ) {
var index = 0;
var length = 1;
var arrayPosition = -1;
var _Lexer = this;
_Lexer.tokens = [];
_Lexer.lexemes = [];
_Lexer.token = null;
_Lexer.lexeme = null;
_Lexer.bof = true;
_Lexer.eof = false;
_Lexer.line_number = 0;
_Lexer.char_position = 0;

// fill tokens and lexemes
while ( index + length <= string.length ) {
var small = string.substr( index, length ) ;
var big = string.substr( index, length + 1 ) ;
for ( var def in definitions ) {
var smallmatch = small.match( definitions[def].regexp ) !== null;
var bigmatch = big.match( definitions[def].regexp ) !== null;
if ( smallmatch &&( ! bigmatch || small == big ) ) {
// found a token
index += length;
length = 0;
_Lexer.tokens.push(def);
_Lexer.lexemes.push(small);
break;
}
}
length++;
}

_Lexer.next = function (count) {
if (count == undefined) count = 1;
while (count-- > 0) {
arrayPosition++;
if ( arrayPosition < _Lexer.tokens.length) {
_Lexer.token = _Lexer.tokens[arrayPosition];
_Lexer.lexeme = _Lexer.lexemes[arrayPosition];
_Lexer.bof = false;
_Lexer.char_position += _Lexer.lexeme.length;
if (definitions[_Lexer.token].skip) {
if (_Lexer.token == 'newline') {
_Lexer.line_number++;
_Lexer.char_position = 0;
}
_Lexer.next();
}
} else {
_Lexer.token = 'EOF';
_Lexer.lexeme = null;
_Lexer.eof = true;
}
}
}

_Lexer.prev = function (count) {
if (count == undefined) count = 1;
while (count-- > 0) {
arrayPosition--;
if ( arrayPosition-- > 0) {
_Lexer.token = _Lexer.tokens[arrayPosition];
_Lexer.lexeme = _Lexer.lexemes[arrayPosition];
_Lexer.eof = false;
_Lexer.char_position -= _Lexer.lexeme.length;
if (definitions[_Lexer.token].skip) {
if (_Lexer.token == 'newline') {
_Lexer.line_number--;
_Lexer.char_position = 0;
}
_Lexer.prev();
}
} else {
_Lexer.token = 'BOF';
_Lexer.lexeme = null;
_Lexer.bof = true;
break;
}
}
}
}

var lexer = new Lexer("hello('world', 2)");
while (! lexer.eof ) {
lexer.next()
document.write( lexer.token + ' ' );
}

This will output "identifier left_parenthesis string comma decimal_integer right_parenthesis EOF" to the page.

2 comments:

LohithRaj P.L. said...

how to run this code

LohithRaj P.L. said...

how to run this java script code

About Me

My photo
chicago, il, United States
I'm a software engineer by profession.

Labels