Skip to content
Permalink
Browse files

New `grunt grammar` task for automatically building customized Gramma…

…r.js and Grammar.min.js.
  • Loading branch information
ibc committed Feb 18, 2013
1 parent fe15d69 commit 50d7bf14705701676412bab74e62cc209e9bfcdd
Showing with 246 additions and 186 deletions.
  1. +8 −0 BUILDING.md
  2. +45 −7 grunt.js
  3. +4 −3 package.json
  4. +25 −19 src/Grammar/README.md
  5. +9 −2 src/Grammar/dist/Grammar.js
  6. +155 −155 src/Grammar/dist/Grammar.min.js
@@ -56,3 +56,11 @@ Running "qunit:noWebRTC" (qunit) task
Testing testNoWebRTC.html.........OK
>> 206 assertions passed (213ms)
```

## Changes in JsSIP grammar

If you modify `src/Grammar/src/Grammar.pegjs` then you need to recompile JsSIP grammar files. For that run the following task:
```
$ grunt grammar
```
And then build JsSIP again by running `grunt`.
@@ -1,4 +1,5 @@
/*global module:false*/

module.exports = function(grunt) {

// Project configuration.
@@ -8,8 +9,7 @@ module.exports = function(grunt) {
banner: '/*! jsSIP v@<%= pkg.version %> jssip.net | jssip.net/license */'
},
lint: {
dist: 'dist/<%= pkg.name %>-<%= pkg.version %>.js',
grunt: 'grunt.js'
dist: 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
},
concat: {
dist: {
@@ -92,10 +92,48 @@ module.exports = function(grunt) {
});

// Default task.
grunt.registerTask('default', 'concat:dist lint min concat:post concat:post_min');
grunt.registerTask('default', ['concat:dist', 'lint', 'min', 'concat:post', 'concat:post_min']);

// Test tasks.
grunt.registerTask('testNoWebRTC', 'qunit:noWebRTC');
grunt.registerTask('testWebRTC', 'qunit:WebRTC');
grunt.registerTask('test', 'testNoWebRTC');
};
grunt.registerTask('testNoWebRTC', ['qunit:noWebRTC']);
grunt.registerTask('test', ['testNoWebRTC']);

// Task for building JsSIP grammar.
grunt.registerTask('grammar', function(){
// First compile JsSIP grammar with PEGjs.
console.log("grammar task: compiling JsSIP PEGjs grammar into Grammar.js ...");
var done = this.async(); // This is an async task.
var sys = require('sys');
var exec = require('child_process').exec;
var child;

child = exec('pegjs -e JsSIP.Grammar src/Grammar/src/Grammar.pegjs src/Grammar/dist/Grammar.js', function(error, stdout, stderr) {
if (error == null) {
// Then modify the generated Grammar.js file with custom changes.
console.log("OK");
console.log("grammar task: applying custom changes to Grammar.js ...");
var fs = require('fs');
var grammar = fs.readFileSync('src/Grammar/dist/Grammar.js').toString();
var modified_grammar = grammar.replace(/throw new this\.SyntaxError\(([\s\S]*?)\);([\s\S]*?)}([\s\S]*?)return result;/, 'new this.SyntaxError($1);\n return -1;$2}$3return data;');
fs.writeFileSync('src/Grammar/dist/Grammar.js', modified_grammar);
console.log("OK");

// Then minify Grammar.js.
console.log("grammar task: minifying Grammar.js ...");
child = exec('cd src/Grammar/ && node minify.js', function(error, stdout, stderr) {
if (error == null) {
console.log("OK");
done(); // Tell grunt that async task has succeeded.
} else {
sys.print('ERROR: ' + stderr);
done(false); // Tell grunt that async task has failed.
}
});
} else {
sys.print('ERROR: ' + stderr);
done(false); // Tell grunt that async task has failed.
}
});
});

};
@@ -20,8 +20,9 @@
"webrtc",
"library"
],
"devDependencies": {
"grunt": "0.3.17"
},
"devDependencies": {
"grunt": "0.3.17",
"pegjs": "0.7.0"
},
"license": "MIT"
}
@@ -1,25 +1,32 @@
JsSIP Parser Grammar
======================
## JsSIP Parser Grammar

JsSIP uses [PEG.js](https://github.com/dmajda/pegjs) to build its parser grammar, a PEG based parser generator for JavaScript.

The grammar source is defined in PEG format in `src/Grammar.pegjs` file. It must be converted to JavaScript by using PEG.js


PEG.js Installation
------------------
### PEG.js Installation


In order to use the `pegjs` node command, install PEG.js globally:
```
$ npm install -g pegjs
```

$ npm install -g pegjs
### Compiling JsSIP Grammar

There are two ways for achieving this task:

Generating the Grammar parser from the Grammar source
-----------------------------------------------------
* Automatically by running `grunt grammar` in the JsSIP root directory.
* Manually by following steps below one by one:

The following command converts the PEG grammar into a JsSIP module named `Grammar`. The output file is created in `dist/Grammar.js`.

$ pegjs -e JsSIP.Grammar src/Grammar.pegjs dist/Grammar.js
#### Generating the Grammar parser from the Grammar source

The following command converts the PEG grammar into a JsSIP module named `Grammar`. The output file is created in `dist/Grammar.js`.
```
$ pegjs -e JsSIP.Grammar src/Grammar.pegjs dist/Grammar.js
```

In case there is an error in the grammar, the command will throw a descriptive error.

@@ -56,17 +63,16 @@ The changes to be done in `dist/Grammar.js` file are located at the end of the `
toSource: function() { return this._source; }
```

Minifying the Grammar parser
-----------------------------
#### Minifying the Grammar parser

[node-minify](https://github.com/srod/node-minify) is used in order to minify the generated grammar.

Install node-minify

$ npm install node-minify

Run the `minify.js` script with node command to minimize the grammar.

$ node minify.js

Install node-minify:
```
$ npm install node-minify
```
Run the `minify.js` script with node command to minimize the grammar:
```
$ node minify.js
```
This will generate the `dist/Grammar.min.js` file.

Some generated files are not rendered by default. Learn more.

2 comments on commit 50d7bf1

@jmillan

This comment has been minimized.

Copy link
Member

jmillan replied Feb 18, 2013

Automate everything!! ;-)

@saghul

This comment has been minimized.

Copy link
Contributor

saghul replied Feb 19, 2013

Awesome! 🍰

Please sign in to comment.
You can’t perform that action at this time.