forked from shellscape/postcss-values-parser
/
Operator.js
64 lines (48 loc) · 1.59 KB
/
Operator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Copyright © 2018 Andrew Powell
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of this Source Code Form.
*/
const { registerWalker } = require('../walker');
const Node = require('./Node');
const operators = ['+', '-', '/', '*', '%', '=', '<=', '>=', '<', '>'];
const operRegex = new RegExp(`([/|*}])`);
const compactRegex = /^[*/]\b/;
class Operator extends Node {
constructor(options) {
super(options);
this.type = 'operator';
}
static get chars() {
return operators;
}
static fromTokens(tokens, parser) {
parser.fromFirst(tokens, Operator);
}
static get regex() {
return operRegex;
}
static test(tokens, parser) {
const [first] = tokens;
const [, value] = first;
const { lastNode } = parser;
return lastNode && lastNode.type === 'func' && compactRegex.test(value);
}
static tokenize(tokens, parser) {
const [first, ...rest] = tokens;
const [, value, startLine, , endLine, endChar] = first;
const parts = value.split(operRegex).filter((t) => !!t);
let [, , , startChar] = first;
const newTokens = parts.map((part) => {
const newToken = ['word', part, startLine, startChar, endLine, endChar];
startChar += part.length;
return newToken;
});
parser.back(newTokens.concat(rest));
}
}
registerWalker(Operator);
module.exports = Operator;