how to build your own camxes.js: Difference between revisions

From Lojban
Jump to navigation Jump to search
No edit summary
 
m (Gleki moved page jbocre: how to build your own camxes.js to how to build your own camxes.js without leaving a redirect: Text replace - "jbocre: h" to "h")
 
(One intermediate revision by the same user not shown)

Latest revision as of 11:49, 23 March 2014

la .ilmen.:

Hello;

I've been trying to build a Lojban parser from the PEG grammar by myself, in order to be able to create experimental versions of camxes if necessary.

I finally succeeded to build my own version of camxes.js, and I'll explain below how to do the same thing yourself.

0) The only complete Lojban PEG grammar that builds successfully with PEG.js that I found is the one written by Masato Hagiwara (many thanks to him ^^): http://masatohagiwara.net/camxes.js/ At the bottom of the page you can see a demo of the generated camxes.js parser. Here, our goal is building the same thing by ourself locally. :)

1) Fist thing needed: getting Hagiwara's version of the PEG grammar, located here:

https://raw.github.com/mhagiwara/camxes.js/master/camxes.js.peg

2) Now you'll need to download and install the javascript plateform node.js; here it is:

http://nodejs.org/ (click on the install button)

3) Now go to the page http://pegjs.majda.cz/documentation#installation-node-js; open the Node.js command prompt, and enter the two commands below, which are necessary for installing PEG.js:

$ npm install -g pegjs
$ npm install pegjs

(Of course, without the $ symbol.)

4) Now create a new javascript file (let's call it "camxes-builder.js"), and put it in the same directory as the PEG grammar file (under Windows, I advise to put both files in your main drive (usually "c:"), and write the below code in it:

// // load peg.js and the file system module
var fs = require("fs")
var PEG = require("pegjs")
// // read peg and build a parser
var camxes_peg = fs.readFileSync("\camxes.js.peg").toString();
var camxes = PEG.buildParser(camxes_peg, {cache: true});
// // test it
camxes.parse("ko'a broda");
// [ 'text',
//   [ 'text_1',
//     [ 'paragraphs', [Object] ] ] ]
// // write to a file
fs.writeFileSync("\camxes.js", camxes.toSource());

(Well, it's basically a copy-past from an example shown in the PEG file, with some little changes.)

5) Now run your camxes-builder.js with the Node.js command prompt:

$ cd [my_working_directory]
$ node camxes-builder.js

Then you should see appear a file called "camxes.js" in the same directory as the PEG grammar.

6) Edit camxes.js with a text editor, and check that it does begin with the following line:

var camxes = (function(){

It'll probably lack the "var camxes =" part, so please add it (the first line must look like the above one).

7) Now everything is ready, you just need a HTML interface for your newly-built parser. Download this one: https://www.dropbox.com/s/kbp4fitxe9taldf/camxes-html-interface.zip

Unzip it; the archives contains an example of camxes.js + an HTML interface; just replace the camxes.js with yours. The HTML file must be in the same directory as camxes.js and jquery-1.9.1.min.js.

Now you just have to open your newly created HTML interface, and type some Lojban text in the textarea. ;)

Phew, it was longer than I thought, I hope that nobody fell asleep while reading this post. :roll: