Skip to content

Commit

Permalink
Merge pull request #19 from stasm/ontheflycompiler
Browse files Browse the repository at this point in the history
Add the on-the-fly compiler
  • Loading branch information
zbraniecki committed Aug 10, 2012
2 parents 21e4dce + d2267ac commit f10ec3d
Show file tree
Hide file tree
Showing 12 changed files with 837 additions and 8 deletions.
443 changes: 443 additions & 0 deletions js/compiler.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions js/l20n.js
Expand Up @@ -58,12 +58,10 @@ L20n.Context = function() {
// we should have getValue for value, getAttributes for attributes and get
// for both
get: function(id, args) {
return mObjects['resources'][id]();
var curObj = this._get(id, args);
return mObjects['system'].getent(curObj, mObjects['system'], id);
},
getAttributes: function(id, args) {
return {}; // skip the attributes for now
var curObj = this._get(id, args);
return mObjects['system'].getattrs(curObj, mObjects['system'], id);
},
Expand Down
4 changes: 3 additions & 1 deletion tests/html5/clientside/basic/index.html
@@ -1,6 +1,8 @@
<html>
<head>
<script type="text/javascript;version=1.8" src="/l20n/js/l20n.js"></script>
<script type="text/javascript;version=1.8" src="/l20n/js/parser.js"></script>
<script type="text/javascript;version=1.8" src="/l20n/js/compiler.js"></script>
<script type="text/javascript;version=1.8" src="/l20n/js/l20n-xml.js"></script>

<link rel="stylesheet" type="text/css" href="../lib/jasmine/jasmine.css">
Expand All @@ -11,7 +13,7 @@
<script type="text/javascript" src="spec.js"></script>
<script type="text/javascript" src="../lib/test.js"></script>

<link href="./index.j20n" type="intl/l20n" />
<link href="./index.lol" type="intl/l20n" />
</head>
<body>
<p class="intro">
Expand Down
5 changes: 0 additions & 5 deletions tests/html5/clientside/basic/index.j20n

This file was deleted.

103 changes: 103 additions & 0 deletions tests/js/compiler/basic.js
@@ -0,0 +1,103 @@
var fs = require('fs');
var should = require('should');
var Compiler = require('../../../js/compiler.js');

function read(filename) {
return JSON.parse(fs.readFileSync(filename)).body;
}

describe('Basic entities', function(){
var ast;
var obj;

beforeEach(function() {
obj = {};
Compiler.compile(ast, obj);
});

describe('Simple value', function(){
var filename = './lol/basic1.json';
before(function() {
ast = read(filename);
});

describe('Simple string value', function(){
it('is "Simple"', function(){
var value = obj['basic1'].get();
value.should.equal("Basic 1");
});
});
});

describe('Simple array', function(){
var filename = './lol/basic2.json';
before(function() {
ast = read(filename);
});

describe('an array without an index', function(){
it('is "One" when called without an index', function(){
var value = obj['basic21'].get(obj);
value.should.equal('One');
});
it('is "Two" when called with [1]', function(){
var value = obj['basic21'].get(obj, {}, [1]);
value.should.equal('Two');
});
});
describe('an array with an index of [1]', function(){
it('is "Two" when called without an index', function(){
var value = obj['basic22'].get(obj);
value.should.equal('Two');
});
it('is "One" when called with [0]', function(){
var value = obj['basic22'].get(obj, {}, [0]);
value.should.equal('One');
});
});
});

describe('Simple hash', function(){
var filename = './lol/basic3.json';
before(function() {
ast = read(filename);
});

describe('a hash with no index and no default value', function(){
it('is "Firefox"', function(){
var value = obj['brandName1'].get(obj);
value.should.equal('Firefox');
});
it('is "Aurora when called with an index of ["feminine"] "', function(){
var value = obj['brandName1'].get(obj, {}, ['feminine']);
value.should.equal('Aurora');
});
});
describe('a hash with no index and with a default value', function(){
it('is "Aurora"', function(){
var value = obj['brandName2'].get(obj);
value.should.equal('Aurora');
});
it('is "Firefox" when called with an index of ["masculine"] ', function(){
var value = obj['brandName2'].get(obj, {}, ['masculine']);
value.should.equal('Firefox');
});
});
describe('a hash with an index and no default value', function(){
it('is "Aurora"', function(){
var value = obj['brandName3'].get(obj);
value.should.equal('Aurora');
});
it('is "Firefox" when called with an index of ["masculine"] ', function(){
var value = obj['brandName3'].get(obj, {}, ['masculine']);
value.should.equal('Firefox');
});
});
describe('a hash with too many index keys and no default value', function(){
it('is "Aurora"', function(){
var value = obj['brandName3'].get(obj);
value.should.equal('Aurora');
});
});
});
});
16 changes: 16 additions & 0 deletions tests/js/compiler/generate_json.sh
@@ -0,0 +1,16 @@
#!/bin/bash

HERE=$(cd `dirname $0`; pwd)
LOLS=$HERE/lol
L20N=$HOME/moz/code/l20n

source $L20N/@l20n/bin/activate

DUMP=$L20N/l20n/python/tools/dump_lol.py
OPTS="-t json"

for LOL in $(ls -1 $LOLS/*.lol);
do
NAME=$(basename $LOL .lol);
python $DUMP $OPTS $LOL > $LOLS/${NAME}.json
done
19 changes: 19 additions & 0 deletions tests/js/compiler/lol/basic1.json
@@ -0,0 +1,19 @@
{
"type": "LOL",
"body": [
{
"type": "Entity",
"id": {
"type": "Identifier",
"name": "basic1"
},
"index": [],
"value": {
"type": "String",
"content": "Basic 1"
},
"attrs": {},
"local": false
}
]
}
1 change: 1 addition & 0 deletions tests/js/compiler/lol/basic1.lol
@@ -0,0 +1 @@
<basic1 "Basic 1">
56 changes: 56 additions & 0 deletions tests/js/compiler/lol/basic2.json
@@ -0,0 +1,56 @@
{
"type": "LOL",
"body": [
{
"type": "Entity",
"id": {
"type": "Identifier",
"name": "basic21"
},
"index": [],
"value": {
"type": "Array",
"content": [
{
"type": "String",
"content": "One"
},
{
"type": "String",
"content": "Two"
}
]
},
"attrs": {},
"local": false
},
{
"type": "Entity",
"id": {
"type": "Identifier",
"name": "basic22"
},
"index": [
{
"type": "Literal",
"value": 1
}
],
"value": {
"type": "Array",
"content": [
{
"type": "String",
"content": "One"
},
{
"type": "String",
"content": "Two"
}
]
},
"attrs": {},
"local": false
}
]
}
2 changes: 2 additions & 0 deletions tests/js/compiler/lol/basic2.lol
@@ -0,0 +1,2 @@
<basic21 ['One', 'Two']>
<basic22[1] ['One', 'Two']>

0 comments on commit f10ec3d

Please sign in to comment.