Friday, July 23, 2010

RegLex.js

0 comments
I've released a Regular Expressions based Lexer for JavaScript at github.  The script is extremely easy to use and requires absolutely no dependencies.  To give it a spin one must simply download the code.

To get started define your tokens:
var tokens = {
    Name: {
        name: "NAME",
        expression: /[A-Za-z][A-Za-z0-9]*/
    },
    String: {
        name: "STRING",
        expression: /\"[^\"]*\"/
    },
    Number: {
        name: "NUMBER",
        expression: /0|[1-9][0-9]*/
    },
    Equals: {
        name: "EQUALS",
        expression: /=/
    },
    WhiteSpace: {
        name: "WHITESPACE",
        expression: /\s/
    }
};
Create the grammar:
var grammar = $g(tokens);
Perform the lexical analysis on a string (this will throw an error if the string violates the lexicon):
var lts = $lex('test = 0', $g(tokens)); //NAME WS EQUALS WS NUMBER
Confirm your suspicions:
for (var i = 0; i < lts.length; i++) {
    alert('Token: ' + lts[i].token + ', Value: ' + lts[i].value);
}

Lexical analysis is your oyster! Go forth, and please make sure to mention any issues you might find at github.

Thursday, July 22, 2010

Wow.js

0 comments
I recently completed a preliminary JavaScript version of a World of Warcraft Armory client. The project is currently being hosted at github and is therefore completely open source.  To get started one needs only to download the client script, and its dependency jQuery. It really is simple...

To create a client object:
var client = new Wow.Armory.Client();
To retrieve the character sheet:
client.getCharacterSheet({
  character: 'Wetall',
  realm: 'Dunemaul',
  success: function(character, s, xhr) {
    /* do something with the JSON version of the character sheet here */
  },
  failure: function(xhr, s, e) {
    /* we failed somewhere along the line with the request */
  }
});
To retrieve item information:
client.getItemInfo({
  itemId: 22630,
  success: function(item, s, xhr) {
    /* do something with the JSON version of the item here */
  },
  failure: function(xhr, s, e) {
    /* we failed somewhere along the line with the request */
  }
});
To do a general search query:
client.search({
  searchType: Wow.Armory.SearchType.Characters, /* as well as All, Items, and Guilds */
  searchQuery: 'Calox', /* this parameter is optional! */
  success: function(results, s, xhr) {
    /* do something with the JSON results here */
  },
  failure: function(xhr, s, e) {
    /* we failed somewhere along the line with the request */
  }
});
This is merely scratching the surface! With serious usage the mileage may vary, but should any serious problems arise please report them at github so that I can do my best to rectify the situation.