Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

kison

0.3.6 • Public • Published

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

Keywords

none

install

npm i kison

Downloadsweekly downloads

51

version

0.3.6

license

none

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar