Sunday, March 29, 2009

Javascript Closures - A very basic explanation



This article has been moved to http://geektechjl.blogspot.com.

Thursday, May 15, 2008

A text parser

I have written a javascript text parser. To understand the code the reader will need a basic understanding of object oriented programming using javascript and regular expressions. To view source code click here. Below this posting, on my javascript blog, are some text areas and a button to demonstrate the parser's functionality. Click here to move to the section on my blog that demonstrates the parser. The parser object contains the following functions and helper functions-


function regexpindexAt(txt,value,startindex)


regexpindexAt() returns the position in a string of a specified value or regular expression, starting at a certain position in the string. First parameter is the text to search, second is what to search for. A couple valid examples are regular expressions such as /\d/ or a string such as "header". The last parameter is the position in the text to start the search. If the value specified is not found in the text a -1 will be returned.


function parser(data,delimeter)


the objects constructor or initializer is listed above. The 2 parameters are the text to parse and the delimeter to use. The delimeter can be a string or a regular expression. For example if "," was used as the second parameter, then each call to the parser object's nextword() function would return text that was to the left of each comma, in the text specified in parameter 1. Here is an example below


var theparser = new parser("field1,field2,field3",",");


alert(theparser.nextword()); //shows 'field1' in alert box
alert(theparser.nextword()); //shows 'field2' in alert box
alert(theparser.nextword()); //shows 'field3' in alert box


That being said the next object function explained is...


parser.prototype.nextword = function()


The nextword() member function returns each word according to the delimeter set in the object constructor as listed above. When the end of text is hit nextword() returns null. nextword() also stores the word, within the parser object, in the curword member.


parser.prototype.rewind = function()


The rewind() member function resets the parser object so calling nextword() will return the first word in the text. Typically this would be called if needing to read the text again, after nextword() returns null, indicating the end of the text to be parsed.


parser.prototype.setdelimeter = function(data)


setdelimeter() can be used to change the parser delimeter after the objects contructor or initiliazer is called.


parser.prototype.setvaliddata = function(data)


setvaliddata() is basically a way of using the parser in a kind of 'inverse' way. By calling setvaliddata() the parser will parse based on the data that is needed rather than what needs to be ignored and discarded. It makes the most sense if a regular expression is used. To make more sense of this here is an example...


var theparser = new parser("field1 = 123, field2 = 234,field3 = 345" , ",");


alert(theparser.nextword()); //shows 'field1 = 123' in alert box
alert(theparser.nextword()); //shows 'field2 = 234' in alert box
alert(theparser.nextword()); //shows 'field3 = 345' in alert box


theparser.rewind();


theparser.setvaliddata(/\d/); //parse by valid data using numeric fields


alert(theparser.nextword()); //shows '123' in alert box
alert(theparser.nextword()); //shows '234' in alert box
alert(theparser.nextword()); //shows '345' in alert box


Hope that example clarifies the setvaliddata() member function a bit.


function getinverseregexp(theregexp)


getinverseregexp is a helper function and is called by the member functions setvaliddata() and setdelimeter(). Internally the parser parses by looking at both valid data and the delimeters. If the reader is interested in how this works he/she can take a look at the source code. A link to click on to view the source code is at the top of this post.


function procinputtext()


procinputtext() is simply the function that is called when the 'Proc input' button is clicked on the
Parser demo section
of my javascript blog.


First in the demo section there are 2 small text fields with the header "reg exp fields," above them. To the left of these fields are the words "Valid Data:" and "Delimeter:". When the 'Proc input' button is clicked, the text in the text area with the header 'input text' above it is parsed in 2 passes. On the first pass it is parsed by delimeter using the delimeter specified in the 'Delimeter:' textarea, and the parsing results are placed in the text area with the header 'output text - delimeter' above it. Then the parser's rewind() function is called and the text is parsed via valid data specified in the 'Valid Data:' textarea. You can edit and change the text in the input box,valid data box, and delimeter box to see how the parser works.

 
My Zimbio
Top Stories