Skip to content

Commit

Permalink
Add support for array of tables
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
Brandon Tilley committed Feb 23, 2014
1 parent 6b4dd00 commit 0cef6e2
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ function compile(nodes) {
genError("Cannot redefine existing table '" + path + "'.", line, column);
}
assignedPaths.push(path);
createContext(data, path, {}, true);
context = deepRef(data, path, {});
currentPath = path;
}

function addTableArray(node) {
Expand All @@ -71,7 +72,8 @@ function compile(nodes) {
var column = node.column;

if (assignedPaths.indexOf(path) === -1) assignedPaths.push(path);
createContext(data, path, [], true);
context = deepRef(data, path, []);
currentPath = path;

if (context instanceof Array) {
var newObj = {};
Expand All @@ -81,9 +83,10 @@ function compile(nodes) {
}

// Given a path 'a.b.c', create (as necessary) `start.a`,
// `start.a.b`, and `start.a.b.c`, assigning `value` to `start.a.b.c`
// If `setPath` is true, sets `context` to `value`.
function createContext(start, path, value, setContext) {
// `start.a.b`, and `start.a.b.c`, assigning `value` to `start.a.b.c`.
// If `a` or `b` are arrays and have items in them, the last item in the
// array is used as the context for the next sub-path.
function deepRef(start, path, value) {
var key;
var keys = path.split('.');
var ctx = start;
Expand All @@ -99,12 +102,11 @@ function compile(nodes) {
}

ctx = ctx[key];
if (ctx instanceof Array && ctx.length && i < keys.length - 1)
ctx = ctx[ctx.length - 1];
}

if (setContext) {
context = ctx;
currentPath = path;
}
return ctx;
}

function reduceArrayWithTypeChecking(array) {
Expand Down

0 comments on commit 0cef6e2

Please sign in to comment.