Skip to content

Commit

Permalink
v1.2.0 repeated lexems parsing rework (how its values will be stored …
Browse files Browse the repository at this point in the history
…in result object)
  • Loading branch information
Holixus committed May 2, 2017
1 parent 31679dc commit 041ed9c
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 15 deletions.
50 changes: 36 additions & 14 deletions lib/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,29 +261,43 @@ function SeqParser(name, text, lib) {
return set();
}

function mark_lists(ast, rep) {
function mark_lists(ast, rep, lists) {
switch (ast[0]) {
case SET:
if (!rep) {
var touches = newObject();
for (var i = 1, n = ast.length; i < n; ++i) {
var sublists = newObject();
mark_lists(ast[i], 0, sublists);
Object.keys(sublists).forEach(function (id) {
if (id in lists)
lists[id] = 1;
else
if (sublists[id])
lists[id] = 1;
else
touches[id] = 0;
});
}
Object.keys(touches).forEach(function (id) {
lists[id] = 0;
});
break;
}
case SEQ:
for (var i = 1, n = ast.length; i < n; ++i)
mark_lists(ast[i], rep);
mark_lists(ast[i], rep, lists);
break;
case OPT:
return mark_lists(ast[1], 0);
return mark_lists(ast[1], rep, lists);
case PLUS:
case STAR:
return mark_lists(ast[1], 1);
return mark_lists(ast[1], 1, lists);
case LEX:
var id = ast[1];
if (id in lists)
++lists[id];
else
lists[id] = rep;
break;
case DEF:
var id = ast[1];
if (id in lists)
++lists[id];
lists[id] = 1;
else
lists[id] = rep;
break;
Expand All @@ -294,7 +308,7 @@ function SeqParser(name, text, lib) {
var ret = this.ast = tokens.length ? expr() : [];
if (pos < tokens.length)
throw SyntaxError('invalid token '+pos);
mark_lists(ret, 0);
mark_lists(ret, 0, lists);
this.lists = lists;
this.str = undefined;
this.fn = undefined;
Expand Down Expand Up @@ -335,10 +349,18 @@ SeqParser.prototype = {
return function (text, start, ctx) {
var pos = start;
for (var i = 0, n = set.length; i < n; ++i) {
var next = set[i](text, pos, ctx);
var sub_ctx = newObject(),
next = set[i](text, pos, sub_ctx);
//console.log('set-%d "%s" %s', i, text.slice(pos, next >= pos ? next : pos), next);
if (next >= 0)
if (next >= 0) {
Object.keys(sub_ctx).forEach(function (id) {
if (id in lists && id in ctx)
Array.prototype.push.apply(ctx[id], sub_ctx[id]);
else
ctx[id] = sub_ctx[id];
});
return next;
}
}
//console.log('SET: "%s"', text.slice(start, pos >= 0 ? pos : pos));
return -pos-1; // nothing fit
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nano-ui-scheme",
"version": "1.1.6",
"version": "1.2.0",
"description": "UI-files scheme compiler and validator",
"main": "index.js",
"scripts": {
Expand Down
94 changes: 94 additions & 0 deletions test/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,100 @@ suite('compile to parser 12 optionals', function () {
});


suite('compile to parser 13 subsequences', function () {
var src = 'ID COMMA VALUE|id DD SLASH',
tester = newTester(src);

massive('good expressions ['+src+']', tester, [
'AAA,55', { length:6, ID:'AAA', VALUE: '55' },
'BBB../', { length:6, id:'BBB' }
]);

massive_fails('bad expressions ['+src+']', tester, [
'SOME-ID (', SyntaxError,
'SOME-ID (5', SyntaxError,
'SOME-ID (aass', SyntaxError,
'SOME-ID ()', SyntaxError,
'SOME-ID )', SyntaxError
]);
});


suite('compile to parser 13 lists', function () {
var src = 'ID|ID',
tester = newTester(src);

massive('good expressions ['+src+']', tester, [
'AAA', { length:3, ID:'AAA' }
]);

massive_fails('bad expressions ['+src+']', tester, [
'5', SyntaxError
]);
});

suite('compile to parser 14 lists', function () {
var src = 'ID SP (ID|VALUE (SP VALUE)*)',
tester = newTester(src);

massive('good expressions ['+src+']', tester, [
'AAA BBB', { length:7, ID: [ 'AAA', 'BBB' ] },
'AAA 45', { length:6, ID: [ 'AAA' ], VALUE: [ '45' ] },
'AAA 45 22', { length:9, ID: [ 'AAA' ], VALUE: [ '45', '22' ] }
]);

massive_fails('bad expressions ['+src+']', tester, [
'5', SyntaxError,
'AA', SyntaxError,
'AA ', SyntaxError,
'AA BB CC', SyntaxError,
'AA 44 DD', SyntaxError
]);
});


suite('compile to parser 15 lists', function () {
var src = 'id SP (ID|VALUE (SP VALUE)*)',
tester = newTester(src);

massive('good expressions ['+src+']', tester, [
'AAA BBB', { length:7, id: 'AAA', ID: 'BBB' },
'AAA 45', { length:6, id: 'AAA', VALUE: [ '45' ] },
'AAA 45 22', { length:9, id: 'AAA', VALUE: [ '45', '22' ] }
]);

massive_fails('bad expressions ['+src+']', tester, [
'5', SyntaxError,
'AA', SyntaxError,
'AA ', SyntaxError,
'AA BB CC', SyntaxError,
'AA 44 DD', SyntaxError
]);
});

suite('compile to parser 16 lists', function () {
var src = 'id (SP (ID|VALUE))+',
tester = newTester(src);

massive('good expressions ['+src+']', tester, [
'AAA BBB', { length:7, id: 'AAA', ID: [ 'BBB' ] },
'AAA 45', { length:6, id: 'AAA', VALUE: [ '45' ] },
'AAA 45 22', { length:9, id: 'AAA', VALUE: [ '45', '22' ] },
'AAA BBB CC', { length:10, id: 'AAA', ID: [ 'BBB', 'CC' ] },
'AAA 45 CC', { length:9, id: 'AAA', VALUE: [ '45' ], ID: [ 'CC' ] },
'AAA 45 DD 22', { length:12, id: 'AAA', VALUE: [ '45', '22' ], ID: [ 'DD' ] }
]);

massive_fails('bad expressions ['+src+']', tester, [
'5', SyntaxError,
'AA', SyntaxError,
'AA ', SyntaxError,
'AA BB. CC', SyntaxError,
'AA 44 DD,', SyntaxError
]);
});



function newDefSeqTester(name, dseq, seq) {
var defs = newDefsLib(),
Expand Down

0 comments on commit 041ed9c

Please sign in to comment.