Permalink
Browse files

DRY for sql.pegjs-head.js

  • Loading branch information...
1 parent 1970d2d commit 98aa3525fe464e0c3cc60b446de6a372ff581ad1 @steveyen committed Apr 23, 2011
Showing with 38 additions and 64 deletions.
  1. +32 −0 public/js/{misc.js → sql.pegjs-head.js}
  2. +2 −2 public/test/{test-misc.html → test-head.html}
  3. +0 −62 sql.pegjs
  4. +4 −0 views/index.erb
@@ -1,3 +1,5 @@
+// Header/utility functions for sql.pegjs grammar match bodies.
+//
function append(arr, x) {
arr[arr.length] = x;
return arr;
@@ -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;
+}
+
@@ -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>
@@ -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>
View
@@ -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 =
View
@@ -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>

0 comments on commit 98aa352

Please sign in to comment.