-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy patharraynl.js
55 lines (42 loc) · 1.78 KB
/
arraynl.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
const BaseNode = require("../basenode.js");
const constants = require("../../constants.js");
class ArrayNl extends BaseNode {
getNode (arrayNameToken) {
return (!arrayNameToken) ? ArrayNl.getParsedArrayLiteral(this)
: ArrayNl.getParsedArrayElement(this, arrayNameToken);
}
static getParsedArrayLiteral (context) {
const node = {};
node.operation = constants.ARRAY;
node.body = context.parseDelimited(
constants.SYM.L_SQ_BRACKET, constants.SYM.R_SQ_BRACKET, constants.SYM.COMMA,
context.parseExpression.bind(context), null
);
return node;
}
static getParsedArrayElement (context, arrayNameToken) {
const node = {};
node.operation = constants.ARRAY_ELEM;
node.name = arrayNameToken.value;
node.indexNodes = ArrayNl.getArrayElementIndexNodes(context);
return node;
}
static getArrayElementIndexNodes (context) {
const indexNodes = [ ArrayNl.getArrayElementIndexNode(context), ];
while (context.isNextTokenPunctuation(constants.SYM.L_SQ_BRACKET)) { // handles multi-dimensional array element
indexNodes.push(ArrayNl.getArrayElementIndexNode(context));
}
return indexNodes;
}
static getArrayElementIndexNode (context) {
let indexNode = { operation: null, right: null, left: null, value: "", };
context.skipPunctuation(constants.SYM.L_SQ_BRACKET);
if (ArrayNl.isNotEmptyArrayIndex(context)) indexNode = context.parseExpression();
context.skipPunctuation(constants.SYM.R_SQ_BRACKET);
return indexNode;
}
static isNotEmptyArrayIndex (context) {
return context.lexer().peek().value !== constants.SYM.R_SQ_BRACKET;
}
}
module.exports = new ArrayNl();