Skip to content

Commit

Permalink
small optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Oct 27, 2016
1 parent a73797c commit c7142b6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/syntax/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function match(syntax, syntaxNode, node) {
var result = [];
var multiplier = syntaxNode.multiplier || MULTIPLIER_DEFAULT;
var min = multiplier.min;
var max = multiplier.max === null ? Infinity : multiplier.max;
var max = multiplier.max === 0 ? Infinity : multiplier.max;
var lastCommaTermCount;
var lastComma;
var matchCount = 0;
Expand Down
27 changes: 14 additions & 13 deletions lib/syntax/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,27 @@ var COMBINATOR_PRECEDENCE = {
var MULTIPLIER_DEFAULT = {
comma: false,
min: 1,
max: 1,
value: ''
max: 1
};
var MULTIPLIER_ZERO_OR_MORE = {
comma: false,
min: 0,
max: null,
value: '*'
max: 0
};
var MULTIPLIER_ONE_OR_MORE = {
comma: false,
min: 1,
max: null,
value: '+'
max: 0
};
var MULTIPLIER_ONE_OR_MORE_COMMA_SEPARATED = {
comma: true,
min: 1,
max: null,
value: '#'
max: 0
};
var MULTIPLIER_ZERO_OR_ONE = {
comma: false,
min: 0,
max: 1,
value: '?'
max: 1
};
var NAME_CHAR = (function() {
var array = typeof Uint32Array === 'function' ? new Uint32Array(128) : new Array(128);
Expand Down Expand Up @@ -171,7 +166,7 @@ function readMultiplierRange(scanner, comma) {
return {
comma: comma,
min: Number(min),
max: max ? Number(max) : null
max: max ? Number(max) : 0
};
}

Expand Down Expand Up @@ -254,12 +249,18 @@ function readKeywordOrFunction(scanner) {
scanner.pos++;
sequence = readSequence(scanner);
scanner.eat(RIGHTPARENTHESIS);

return {
type: 'Function',
name: name,
sequence: sequence,
multiplier: readMultiplier(scanner)
};
}

return {
type: sequence ? 'Function' : 'Keyword',
type: 'Keyword',
name: name,
sequence: sequence,
multiplier: readMultiplier(scanner)
};
}
Expand Down
6 changes: 3 additions & 3 deletions lib/syntax/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ function serializeMultiplier(token) {
return '';
}

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

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

return (
(token.comma ? '#' : '') +
'{' + token.min + (token.min !== token.max ? ',' + (token.max !== null ? token.max : '') : '') + '}'
'{' + token.min + (token.min !== token.max ? ',' + (token.max !== 0 ? token.max : '') : '') + '}'
);
}

Expand Down

0 comments on commit c7142b6

Please sign in to comment.