Skip to content

Commit

Permalink
fixed csscolor example
Browse files Browse the repository at this point in the history
  • Loading branch information
kach committed Jun 13, 2016
1 parent 3e4fe5f commit 2900f1b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/csscolor.ne
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ csscolor -> "#" hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit {%
| "hsla" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ "," _ decimal _ ")" {% $({"h": 4, "s": 8, "l": 12, "a": 16}) %}

hexdigit -> [a-fA-F0-9]
colnum -> posint {% id %} | percentage {%
colnum -> unsigned_int {% id %} | percentage {%
function(d) {return Math.floor(d[0]*255); }
%}
25 changes: 20 additions & 5 deletions examples/js/csscolor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ var grammar = {
{"name": "__$ebnf$1", "symbols": ["wschar", "__$ebnf$1"], "postprocess": function arrconcat(d) {return [d[0]].concat(d[1]);}},
{"name": "__", "symbols": ["__$ebnf$1"], "postprocess": function(d) {return null;}},
{"name": "wschar", "symbols": [/[ \t\n\v\f]/], "postprocess": id},
{"name": "posint$ebnf$1", "symbols": [/[0-9]/]},
{"name": "posint$ebnf$1", "symbols": [/[0-9]/, "posint$ebnf$1"], "postprocess": function arrconcat(d) {return [d[0]].concat(d[1]);}},
{"name": "posint", "symbols": ["posint$ebnf$1"], "postprocess":
{"name": "unsigned_int$ebnf$1", "symbols": [/[0-9]/]},
{"name": "unsigned_int$ebnf$1", "symbols": [/[0-9]/, "unsigned_int$ebnf$1"], "postprocess": function arrconcat(d) {return [d[0]].concat(d[1]);}},
{"name": "unsigned_int", "symbols": ["unsigned_int$ebnf$1"], "postprocess":
function(d) {
return parseInt(d[0].join(""));
}
Expand All @@ -50,6 +50,21 @@ var grammar = {
}
}
},
{"name": "unsigned_decimal$ebnf$1", "symbols": [/[0-9]/]},
{"name": "unsigned_decimal$ebnf$1", "symbols": [/[0-9]/, "unsigned_decimal$ebnf$1"], "postprocess": function arrconcat(d) {return [d[0]].concat(d[1]);}},
{"name": "unsigned_decimal$ebnf$2$subexpression$1$ebnf$1", "symbols": [/[0-9]/]},
{"name": "unsigned_decimal$ebnf$2$subexpression$1$ebnf$1", "symbols": [/[0-9]/, "unsigned_decimal$ebnf$2$subexpression$1$ebnf$1"], "postprocess": function arrconcat(d) {return [d[0]].concat(d[1]);}},
{"name": "unsigned_decimal$ebnf$2$subexpression$1", "symbols": [{"literal":"."}, "unsigned_decimal$ebnf$2$subexpression$1$ebnf$1"]},
{"name": "unsigned_decimal$ebnf$2", "symbols": ["unsigned_decimal$ebnf$2$subexpression$1"], "postprocess": id},
{"name": "unsigned_decimal$ebnf$2", "symbols": [], "postprocess": function(d) {return null;}},
{"name": "unsigned_decimal", "symbols": ["unsigned_decimal$ebnf$1", "unsigned_decimal$ebnf$2"], "postprocess":
function(d) {
return parseFloat(
d[0].join("") +
(d[1] ? "."+d[1][1].join("") : "")
);
}
},
{"name": "decimal$ebnf$1", "symbols": [{"literal":"-"}], "postprocess": id},
{"name": "decimal$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}},
{"name": "decimal$ebnf$2", "symbols": [/[0-9]/]},
Expand Down Expand Up @@ -126,7 +141,7 @@ var grammar = {
{"name": "csscolor$string$4", "symbols": [{"literal":"h"}, {"literal":"s"}, {"literal":"l"}, {"literal":"a"}], "postprocess": function joiner(d) {return d.join('');}},
{"name": "csscolor", "symbols": ["csscolor$string$4", "_", {"literal":"("}, "_", "colnum", "_", {"literal":","}, "_", "colnum", "_", {"literal":","}, "_", "colnum", "_", {"literal":","}, "_", "decimal", "_", {"literal":")"}], "postprocess": $({"h": 4, "s": 8, "l": 12, "a": 16})},
{"name": "hexdigit", "symbols": [/[a-fA-F0-9]/]},
{"name": "colnum", "symbols": ["posint"], "postprocess": id},
{"name": "colnum", "symbols": ["unsigned_int"], "postprocess": id},
{"name": "colnum", "symbols": ["percentage"], "postprocess":
function(d) {return Math.floor(d[0]*255); }
}
Expand All @@ -136,6 +151,6 @@ var grammar = {
if (typeof module !== 'undefined'&& typeof module.exports !== 'undefined') {
module.exports = grammar;
} else {
window.csscolor = grammar;
window.grammar = grammar;
}
})();
5 changes: 5 additions & 0 deletions test/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,10 @@ describe("nearleyc", function() {
sh("node bin/nearleyc.js test/percent.ne");
});

it('tokens', function() {
var tokc = load(nearleyc("examples/token.ne"));
parse(tokc, [123, 456, " ", 789]).should.deep.equal([ [123, [ [ 456, " ", 789 ] ]] ]);
});

});

0 comments on commit 2900f1b

Please sign in to comment.