Skip to content

Commit

Permalink
refactor: parser (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Apr 4, 2019
1 parent b91c6e9 commit b5e20dc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
20 changes: 10 additions & 10 deletions src/lib/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function isValueType(type) {
case 'VminValue':
case 'VmaxValue':
case 'PercentageValue':
case 'Value':
case 'Number':
return true;
}
return false;
Expand Down Expand Up @@ -236,7 +236,7 @@ function reduceDivisionExpression(node) {
return node;
}

if (node.right.type !== 'Value') {
if (node.right.type !== 'Number') {
throw new Error(`Cannot divide by "${node.right.unit}", number expected`);
}

Expand All @@ -253,8 +253,8 @@ function reduceDivisionExpression(node) {
}

function reduceMultiplicationExpression(node) {
// (expr) * value
if (node.left.type === 'MathExpression' && node.right.type === 'Value') {
// (expr) * number
if (node.left.type === 'MathExpression' && node.right.type === 'Number') {
if (
isValueType(node.left.left.type) &&
isValueType(node.left.right.type)
Expand All @@ -264,13 +264,13 @@ function reduceMultiplicationExpression(node) {
return node.left;
}
}
// something * value
else if (isValueType(node.left.type) && node.right.type === 'Value') {
// something * number
else if (isValueType(node.left.type) && node.right.type === 'Number') {
node.left.value *= node.right.value;
return node.left;
}
// value * (expr)
else if (node.left.type === 'Value' && node.right.type === 'MathExpression') {
// number * (expr)
else if (node.left.type === 'Number' && node.right.type === 'MathExpression') {
if (
isValueType(node.right.left.type) &&
isValueType(node.right.right.type)
Expand All @@ -280,8 +280,8 @@ function reduceMultiplicationExpression(node) {
return node.right;
}
}
// value * something
else if (node.left.type === 'Value' && isValueType(node.right.type)) {
// number * something
else if (node.left.type === 'Number' && isValueType(node.right.type)) {
node.right.value *= node.left.value;
return node.right;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function stringify(node, prec) {

return str;
}
case "Value":
case 'Number':
return round(node.value, prec);
case 'Function':
return node.value;
Expand Down
18 changes: 9 additions & 9 deletions src/parser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ expression
| math_expression DIV math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
| LPAREN math_expression RPAREN { $$ = $2; }
| function { $$ = $1; }
| css_value { $$ = $1; }
| value { $$ = $1; }
| dimension { $$ = $1; }
| number { $$ = $1; }
;

function
: FUNCTION { $$ = { type: 'Function', value: $1 }; }
;

css_value
dimension
: LENGTH { $$ = { type: 'LengthValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
| ANGLE { $$ = { type: 'AngleValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
| TIME { $$ = { type: 'TimeValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
Expand All @@ -100,12 +100,12 @@ expression
| VMINS { $$ = { type: 'VminValue', value: parseFloat($1), unit: 'vmin' }; }
| VMAXS { $$ = { type: 'VmaxValue', value: parseFloat($1), unit: 'vmax' }; }
| PERCENTAGE { $$ = { type: 'PercentageValue', value: parseFloat($1), unit: '%' }; }
| ADD css_value { var prev = $2; $$ = prev; }
| SUB css_value { var prev = $2; prev.value *= -1; $$ = prev; }
| ADD dimension { var prev = $2; $$ = prev; }
| SUB dimension { var prev = $2; prev.value *= -1; $$ = prev; }
;

value
: NUMBER { $$ = { type: 'Value', value: parseFloat($1) }; }
| ADD NUMBER { $$ = { type: 'Value', value: parseFloat($2) }; }
| SUB NUMBER { $$ = { type: 'Value', value: parseFloat($2) * -1 }; }
number
: NUMBER { $$ = { type: 'Number', value: parseFloat($1) }; }
| ADD NUMBER { $$ = { type: 'Number', value: parseFloat($2) }; }
| SUB NUMBER { $$ = { type: 'Number', value: parseFloat($2) * -1 }; }
;

0 comments on commit b5e20dc

Please sign in to comment.