Skip to content

Commit

Permalink
DRY for sql.pegjs-head.js
Browse files Browse the repository at this point in the history
  • Loading branch information
steveyen committed Apr 23, 2011
1 parent 1970d2d commit 98aa352
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 64 deletions.
32 changes: 32 additions & 0 deletions public/js/misc.js → public/js/sql.pegjs-head.js
@@ -1,3 +1,5 @@
// Header/utility functions for sql.pegjs grammar match bodies.
//
function append(arr, x) {
arr[arr.length] = x;
return arr;
Expand Down Expand Up @@ -33,3 +35,33 @@ function flatstr(x, rejectSpace, joinChar) {
return flatten(x, rejectSpace, []).join(joinChar || '');
}

function filter(arr, x) {
var acc = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] != x) {
acc[length] = arr[i];
}
}
return acc;
}

function nonempty(x) { // Ex: nonempty("") == null;
if (x == null || x.length > 0) { // Ex: nonempty(null) == null;
return x;
}
return null;
}

function put_if_not_null(m, key, val) {
if (val) {
m[key] = val;
}
return m;
}
function merge(src, dst) {
for (var k in src) {
dst[k] = src[k];
}
return dst;
}

4 changes: 2 additions & 2 deletions public/test/test-misc.html → public/test/test-head.html
Expand Up @@ -3,7 +3,7 @@
<head>
<script type="text/javascript" src="/js/peg-0.6.1.min.js"></script>
<script type="text/javascript" src="/js/qunit/qunit.js"></script>
<script type="text/javascript" src="/js/misc.js"></script>
<script type="text/javascript" src="/js/sql.pegjs-head.js"></script>
<link rel="stylesheet" type="text/css" href="/js/qunit/qunit.css" media="screen">
</head>
<body>
Expand All @@ -28,7 +28,7 @@
</script>

<div>
<h1 id="qunit-header">Misc Test Suite</h1>
<h1 id="qunit-header">sql.pegjs-head.js Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
Expand Down
62 changes: 0 additions & 62 deletions sql.pegjs
Expand Up @@ -7,68 +7,6 @@
//
// Rules with indentation or with comments have manual edits.
//
{
function append(arr, x) {
arr[arr.length] = x;
return arr;
}
function flatten(x, rejectSpace, acc) {
acc = acc || [];
if (x == null || x == undefined) {
if (!rejectSpace) {
return append(acc, x);
}
return acc;
}
if (x.length == undefined) { // Just an object, not a string or array.
return append(acc, x);
}
if (rejectSpace &&
((x.length == 0) ||
(typeof(x) == "string" &&
x.match(/^\s*$/)))) {
return acc;
}
if (typeof(x) == "string") {
return append(acc, x);
}
for (var i = 0; i < x.length; i++) {
flatten(x[i], rejectSpace, acc);
}
return acc;
}
function flatstr(x, rejectSpace, joinChar) {
return flatten(x, rejectSpace, []).join(joinChar || '');
}
function filter(arr, x) {
var acc = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] != x) {
acc[length] = arr[i];
}
}
return acc;
}
function nonempty(x) { // Ex: nonempty("") == null;
if (x == null || x.length > 0) { // Ex: nonempty(null) == null;
return x;
}
return null;
}
function put_if_not_null(m, key, val) {
if (val) {
m[key] = val;
}
return m;
}
function merge(src, dst) {
for (var k in src) {
dst[k] = src[k];
}
return dst;
}
}

start = sql_stmt_list

sql_stmt_list =
Expand Down
4 changes: 4 additions & 0 deletions views/index.erb
Expand Up @@ -7,6 +7,10 @@
</head>
<body>
<script type="text/pegjs" id="sql_pegjs">
{
<%= File.open("public/js/sql.pegjs-head.js", "r") {|f| f.read } %>
}

<%= File.open("sql.pegjs", "r") {|f| f.read } %>
</script>
<script>
Expand Down

0 comments on commit 98aa352

Please sign in to comment.