Skip to content

Commit

Permalink
initial refactoring of lexer.match
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jun 14, 2017
1 parent ab837be commit 01ec036
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 353 deletions.
26 changes: 10 additions & 16 deletions lib/lexer/Lexer.js
Expand Up @@ -149,23 +149,17 @@ Lexer.prototype = {
};

if (typeof syntax === 'function') {
descriptor.match = function(node) {
if (node && syntax(node)) {
return {
badNode: null,
lastNode: null,
next: node.next,
match: {
type: 'ASTNode',
node: node.data
}
};
}

return null;
syntax = {
type: 'ASTNode',
match: syntax
};

descriptor.match = function(item) {
return match(self, syntax, item);
};
} else {
if (typeof syntax === 'string') {
// lazy parsing on first access
Object.defineProperty(descriptor, 'syntax', {
get: function() {
Object.defineProperty(descriptor, 'syntax', {
Expand All @@ -179,8 +173,8 @@ Lexer.prototype = {
descriptor.syntax = syntax;
}

descriptor.match = function(ast) {
return match(self, descriptor.syntax, ast);
descriptor.match = function(item) {
return match(self, descriptor.syntax, item);
};
}

Expand Down
10 changes: 5 additions & 5 deletions lib/lexer/grammar/parse.js
Expand Up @@ -256,20 +256,20 @@ function readType(tokenizer) {
}

function readKeywordOrFunction(tokenizer) {
var sequence = null;
var children = null;
var name;

name = scanWord(tokenizer);

if (tokenizer.charCode() === LEFTPARENTHESIS) {
tokenizer.pos++;
sequence = readImplicitGroup(tokenizer);
children = readImplicitGroup(tokenizer);
tokenizer.eat(RIGHTPARENTHESIS);

return maybeMultiplied(tokenizer, {
type: 'Function',
name: name,
sequence: sequence
children: children
});
}

Expand Down Expand Up @@ -450,12 +450,12 @@ function peek(tokenizer) {

case LEFTPARENTHESIS:
tokenizer.pos++;
var sequence = readImplicitGroup(tokenizer);
var children = readImplicitGroup(tokenizer);
tokenizer.eat(RIGHTPARENTHESIS);

return {
type: 'Parentheses',
sequence: sequence
children: children
};

case APOSTROPHE:
Expand Down
28 changes: 14 additions & 14 deletions lib/lexer/grammar/translate.js
Expand Up @@ -5,22 +5,22 @@ function isNodeType(node, type) {
}

function serializeMultiplier(multiplier) {
if (multiplier.min === 0 && multiplier.max === 1) {
return '?';
}

if (multiplier.min === 1 && multiplier.max === 1) {
return '';
}

if (multiplier.min === 0 && multiplier.max === 0) {
return '*';
}

if (multiplier.min === 0 && multiplier.max === 1) {
return '?';
}

if (multiplier.min === 1 && multiplier.max === 0) {
return multiplier.comma ? '#' : '+';
}

if (multiplier.min === 1 && multiplier.max === 1) {
return '';
}

return (
(multiplier.comma ? '#' : '') +
'{' + multiplier.min + (multiplier.min !== multiplier.max ? ',' + (multiplier.max !== 0 ? multiplier.max : '') : '') + '}'
Expand All @@ -45,12 +45,12 @@ function translateSequence(node, forceBraces, decorate) {
return result;
}

function translateParentheses(sequence, forceBraces, decorate) {
if (!sequence.terms.length) {
function translateParentheses(group, forceBraces, decorate) {
if (!group.terms.length) {
return '()';
}

return '( ' + translateSequence(sequence, forceBraces, decorate) + ' )';
return '( ' + translateSequence(group, forceBraces, decorate) + ' )';
}

function translate(node, forceBraces, decorate) {
Expand All @@ -75,11 +75,11 @@ function translate(node, forceBraces, decorate) {
break;

case 'Function':
result = node.name + translateParentheses(node.sequence, forceBraces, decorate);
result = node.name + translateParentheses(node.children, forceBraces, decorate);
break;

case 'Parentheses': // replace for seq('(' seq(...node.sequence) ')')
result = translateParentheses(node.sequence, forceBraces, decorate);
case 'Parentheses': // replace for seq('(' seq(...node.children) ')')
result = translateParentheses(node.children, forceBraces, decorate);
break;

case 'Type':
Expand Down
2 changes: 1 addition & 1 deletion lib/lexer/grammar/walk.js
Expand Up @@ -10,7 +10,7 @@ module.exports = function walk(node, fn, context) {

case 'Function':
case 'Parentheses':
walk(node.sequence, fn, context);
walk(node.children, fn, context);
break;

case 'Keyword':
Expand Down

0 comments on commit 01ec036

Please sign in to comment.