node package manager
Loosely couple your services. Use Orgs to version and reuse your code. Create a free org »

kison

kison

kison For Nodejs NPM version NPM downloads Build Status

A LALR parser generator for javascript originated in KISSY

example

grammar and lexer definition

cal-grammar.kison:

(function () {
    return {
        productions: [
            {
                symbol: 'expressions',
                rhs: ['e']
            },
 
            {
                symbol: 'e',
                rhs: ['e', '-', 'e'],
                action: function () {
                    return this.$1 - this.$3;
                }
            },
            {
                symbol: 'e',
                rhs: ['e', '+', 'e'],
                action: function () {
                    return this.$1 + this.$3;
                }
            },
            {
                symbol: 'e',
                rhs: ['NUMBER'],
                action: function () {
                    return Number(this.$1);
                }
            }
        ],
 
        lexer: {
            rules: [
                {
                    regexp: /^\s+/
                },
                {
                    regexp: /^[0-9]+(\.[0-9]+)?\b/,
                    token: 'NUMBER'
                },
                {
                    regexp: /^\+/,
                    token: '+'
                },
                {
                    regexp: /^-/,
                    token: '-'
                },
                {
                    // force to match one for error message 
                    regexp: /^./,
                    token: 'ERROR_LA'
                }
            ]
        }
    };
 
})();

run command

kison -g cal-grammar.kison

generated parser

cal.js:

(function(){
    var parser = {
        parse: function(){
            // ... 
        }
    }
    return parser;
})();

run parser

    parser.parse('1+2') // => 3 
    parser.parse('2-1') // => 1 
    parser.parse('2a') // => syntax error at line 1: 2^a expect NUMBER 

changelog

0.3.0

  • optimize error debug info