Permalink
Browse files

Update Parser

  • Loading branch information...
1 parent f4ee1bf commit 24e67c4df888fdf6d095e4edae4451d37a2daaa3 @ixti ixti committed Aug 13, 2012
Showing with 97 additions and 66 deletions.
  1. +61 −30 lib/babelfish/parser.js
  2. +10 −10 src/parser.pegjs
  3. +7 −7 test/babelfish-test.js
  4. +19 −19 test/parser-test.js
View
91 lib/babelfish/parser.js
@@ -133,25 +133,25 @@ module.exports = (function(){
pos0 = pos;
pos1 = pos;
- if (input.substr(pos, 2) === "{{") {
- result0 = "{{";
+ if (input.substr(pos, 2) === "((") {
+ result0 = "((";
pos += 2;
} else {
result0 = null;
if (reportFailures === 0) {
- matchFailed("\"{{\"");
+ matchFailed("\"((\"");
}
}
if (result0 !== null) {
result1 = parse_plural_forms();
if (result1 !== null) {
- if (input.substr(pos, 2) === "}}") {
- result2 = "}}";
+ if (input.substr(pos, 2) === "))") {
+ result2 = "))";
pos += 2;
} else {
result2 = null;
if (reportFailures === 0) {
- matchFailed("\"}}\"");
+ matchFailed("\"))\"");
}
}
if (result2 !== null) {
@@ -278,25 +278,17 @@ module.exports = (function(){
pos0 = pos;
pos1 = pos;
- if (input.charCodeAt(pos) === 92) {
- result0 = "\\";
+ if (input.charCodeAt(pos) === 32) {
+ result0 = " ";
pos++;
} else {
result0 = null;
if (reportFailures === 0) {
- matchFailed("\"\\\\\"");
+ matchFailed("\" \"");
}
}
if (result0 !== null) {
- if (/^[\\|}{]/.test(input.charAt(pos))) {
- result1 = input.charAt(pos);
- pos++;
- } else {
- result1 = null;
- if (reportFailures === 0) {
- matchFailed("[\\\\|}{]");
- }
- }
+ result1 = parse_plural_char();
if (result1 !== null) {
result0 = [result0, result1];
} else {
@@ -308,19 +300,58 @@ module.exports = (function(){
pos = pos1;
}
if (result0 !== null) {
- result0 = (function(offset, char) { return String(char); })(pos0, result0[1]);
+ result0 = (function(offset, char) { return " " + char; })(pos0, result0[1]);
}
if (result0 === null) {
pos = pos0;
}
if (result0 === null) {
- if (/^[^\\|}]/.test(input.charAt(pos))) {
- result0 = input.charAt(pos);
+ pos0 = pos;
+ pos1 = pos;
+ if (input.charCodeAt(pos) === 92) {
+ result0 = "\\";
pos++;
} else {
result0 = null;
if (reportFailures === 0) {
- matchFailed("[^\\\\|}]");
+ matchFailed("\"\\\\\"");
+ }
+ }
+ if (result0 !== null) {
+ if (/^[\\|)(]/.test(input.charAt(pos))) {
+ result1 = input.charAt(pos);
+ pos++;
+ } else {
+ result1 = null;
+ if (reportFailures === 0) {
+ matchFailed("[\\\\|)(]");
+ }
+ }
+ if (result1 !== null) {
+ result0 = [result0, result1];
+ } else {
+ result0 = null;
+ pos = pos1;
+ }
+ } else {
+ result0 = null;
+ pos = pos1;
+ }
+ if (result0 !== null) {
+ result0 = (function(offset, char) { return String(char); })(pos0, result0[1]);
+ }
+ if (result0 === null) {
+ pos = pos0;
+ }
+ if (result0 === null) {
+ if (/^[^ \\|)]/.test(input.charAt(pos))) {
+ result0 = input.charAt(pos);
+ pos++;
+ } else {
+ result0 = null;
+ if (reportFailures === 0) {
+ matchFailed("[^ \\\\|)]");
+ }
}
}
}
@@ -371,25 +402,25 @@ module.exports = (function(){
pos0 = pos;
pos1 = pos;
- if (input.substr(pos, 2) === "#(") {
- result0 = "#(";
+ if (input.substr(pos, 2) === "#{") {
+ result0 = "#{";
pos += 2;
} else {
result0 = null;
if (reportFailures === 0) {
- matchFailed("\"#(\"");
+ matchFailed("\"#{\"");
}
}
if (result0 !== null) {
result1 = parse_identifier();
if (result1 !== null) {
- if (input.charCodeAt(pos) === 41) {
- result2 = ")";
+ if (input.charCodeAt(pos) === 125) {
+ result2 = "}";
pos++;
} else {
result2 = null;
if (reportFailures === 0) {
- matchFailed("\")\"");
+ matchFailed("\"}\"");
}
}
if (result2 !== null) {
@@ -571,13 +602,13 @@ module.exports = (function(){
}
}
if (result0 !== null) {
- if (/^[\\{#]/.test(input.charAt(pos))) {
+ if (/^[\\(#]/.test(input.charAt(pos))) {
result1 = input.charAt(pos);
pos++;
} else {
result1 = null;
if (reportFailures === 0) {
- matchFailed("[\\\\{#]");
+ matchFailed("[\\\\(#]");
}
}
if (result1 !== null) {
View
20 src/parser.pegjs
@@ -3,10 +3,10 @@ start
// Plurals macros
-// - `{{girl|girls}}`
-// - `{{girl|girls}}:chicks_count`
+// - `((girl|girls))`
+// - `((girl|girls)):chicks_count`
plural
- = "{{" forms:plural_forms "}}" anchor:plural_anchor? {
+ = "((" forms:plural_forms "))" anchor:plural_anchor? {
return {
type: 'plural',
forms: forms,
@@ -37,8 +37,9 @@ plural_part
// Single char of the plural form (returns simple char or unescapes `\|`)
plural_char
- = "\\" char:[\\|}{] { return String(char); }
- / [^\\|}]
+ = " " char:plural_char { return " " + char; }
+ / "\\" char:[\\|)(] { return String(char); }
+ / [^ \\|)]
// Name of a variable containing count for plurals
@@ -49,10 +50,10 @@ plural_anchor
// Interpolation variable, e.g.:
-// - `#(count)`
-// - `#(user.name)`
+// - `#{count}`
+// - `#{user.name}`
variable
- = "#(" anchor:identifier ")" {
+ = "#{" anchor:identifier "}" {
return {
type: 'variable',
anchor: anchor
@@ -91,8 +92,7 @@ literal
// Any non-special character
literal_char
- // any Unicode character except { or \ or control character
- = "\\" char:[\\{#] {
+ = "\\" char:[\\(#] {
return String(char);
}
/ .
View
14 test/babelfish-test.js
@@ -169,9 +169,9 @@ require('vows').describe('BabelFish').addBatch({
var i18n = BabelFish.create('en');
i18n.addPhrase('en', 'test.simple_string', 'test');
- i18n.addPhrase('en', 'test.complex.variable', '-#(count)-');
- i18n.addPhrase('en', 'test.complex.plurals', '-{{foo|bar}}:count-');
- i18n.addPhrase('ru', 'test.complex.plurals', '-{{ruu|bar}}:count-');
+ i18n.addPhrase('en', 'test.complex.variable', '-#{count}-');
+ i18n.addPhrase('en', 'test.complex.plurals', '-((foo|bar)):count-');
+ i18n.addPhrase('ru', 'test.complex.plurals', '-((ruu|bar)):count-');
return i18n;
},
@@ -224,10 +224,10 @@ require('vows').describe('BabelFish').addBatch({
i18n.addPhrase('en', 'a', 'a (en)');
i18n.addPhrase('en', 'b', 'b (en)');
- i18n.addPhrase('en', 'c', 'c (en) {{one|other}}:count');
- i18n.addPhrase('fr', 'd', 'd (fr) {{une|autre}}:count');
- i18n.addPhrase('ru', 'b', 'b (ru) #(foo)');
- i18n.addPhrase('es', 'b', 'b (es) #(f.o)');
+ i18n.addPhrase('en', 'c', 'c (en) ((one|other)):count');
+ i18n.addPhrase('fr', 'd', 'd (fr) ((une|autre)):count');
+ i18n.addPhrase('ru', 'b', 'b (ru) #{foo}');
+ i18n.addPhrase('es', 'b', 'b (es) #{f.o}');
return i18n;
},
View
38 test/parser-test.js
@@ -85,74 +85,74 @@ require('vows').describe('BabelFish.Parser').addBatch({
new LiteralNode('Simple string }{ with \b brackets and \t special chars')
],
- 'Quirky #() #(1) #( ) foo bar. #(. (.) . (.).) bazzz.{{}}$_ mess': [
- new LiteralNode('Quirky #() #(1) #( ) foo bar. #(. (.) . (.).) bazzz.{{}}$_ mess')
+ 'Quirky #{} #{1} #{ } (()):foo ((|)) (( )):bar mess': [
+ new LiteralNode('Quirky #{} #{1} #{ } (()):foo ((|)) (( )):bar mess')
],
- 'String with simple #(variable)...': [
+ 'String with simple #{variable}...': [
new LiteralNode('String with simple '),
new VariableNode('variable'),
new LiteralNode('...')
],
- 'String with complex #(foo.bar.baz) variable': [
+ 'String with complex #{foo.bar.baz} variable': [
new LiteralNode('String with complex '),
new VariableNode('foo.bar.baz'),
new LiteralNode(' variable')
],
- 'String with plurals {{a|b}}:c': [
+ 'String with plurals ((a|b)):c': [
new LiteralNode('String with plurals '),
new PluralNode('c', ['a', 'b'])
],
- 'Plurals with {{a\\}b\\|c\\{d|e}}:myvar, escaping': [
+ 'Plurals with ((a\\)b\\|c\\(d|e)):myvar, escaping': [
new LiteralNode('Plurals with '),
- new PluralNode('myvar', ['a}b|c{d', 'e']),
+ new PluralNode('myvar', ['a)b|c(d', 'e']),
new LiteralNode(', escaping')
],
- 'Plurals with {{a|b}}:_compl3x.$variable.': [
+ 'Plurals with ((a|b)):_compl3x.$variable.': [
new LiteralNode('Plurals with '),
new PluralNode('_compl3x.$variable', ['a', 'b']),
new LiteralNode('.')
],
- 'Plurals with empty {{}}:myvar forms': [
- new LiteralNode('Plurals with empty {{}}:myvar forms')
+ 'Plurals with empty (()):myvar forms': [
+ new LiteralNode('Plurals with empty (()):myvar forms')
],
- 'Plurals with single {{abc}}:$myvar forms': [
+ 'Plurals with single ((abc)):$myvar forms': [
new LiteralNode('Plurals with single '),
new PluralNode('$myvar', ['abc']),
new LiteralNode(' forms')
],
- 'Plurals with lots of forms {{b|c|d|e|f|g|h}}:a': [
+ 'Plurals with lots of forms ((b|c|d|e|f|g|h)):a': [
new LiteralNode('Plurals with lots of forms '),
new PluralNode('a', ['b', 'c', 'd', 'e', 'f', 'g', 'h'])
],
- 'Escape \\{{a|b|}}:plurals and \\#(variables)': [
- new LiteralNode('Escape {{a|b|}}:plurals and #(variables)')
+ 'Escape \\((a|b)):plurals and \\#{variables}': [
+ new LiteralNode('Escape ((a|b)):plurals and #{variables}')
],
- 'Invalid variable #(n..e)': [
- new LiteralNode('Invalid variable #(n..e)')
+ 'Invalid variable #{n..e}': [
+ new LiteralNode('Invalid variable #{n..e}')
],
- 'Escape backslash {{a\\\\|b}}:c': [
+ 'Escape backslash ((a\\\\|b)):c': [
new LiteralNode('Escape backslash '),
new PluralNode('c', ['a\\', 'b'])
],
- 'Automagically set {{anchor|to|count}} when plural have no anchor': [
+ 'Automagically set ((anchor|to|count)) when plural have no anchor': [
new LiteralNode('Automagically set '),
new PluralNode('count', ['anchor', 'to', 'count']),
new LiteralNode(' when plural have no anchor')
],
- 'Treat {{trailing|semicolumn}}: literally and use automagic anchor': [
+ 'Treat ((trailing|semicolumn)): literally and use automagic anchor': [
new LiteralNode('Treat '),
new PluralNode('count', ['trailing', 'semicolumn']),
new LiteralNode(': literally and use automagic anchor')

0 comments on commit 24e67c4

Please sign in to comment.