-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser-utils.js
115 lines (96 loc) · 2.49 KB
/
parser-utils.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class Traverser {
inorder(tree, processNodeCb, ctx) {
let cursor = tree.rootNode.walk();
let finished = false;
let skipDescendants = false;
while(!finished) {
do {
do {
skipDescendants = processNodeCb(cursor.currentNode, ctx);
}
while(!skipDescendants && cursor.gotoFirstChild());
}
while(cursor.gotoNextSibling());
let toParentResult = cursor.gotoParent();
while(toParentResult) {
if(cursor.gotoNextSibling()) {
break;
}
toParentResult = cursor.gotoParent();
}
if(!toParentResult) {
finished = true;
}
}
}
}
function getChildByFieldName(node, name, defval = null) {
let ch = node.childrenForFieldName(name);
return ch.length > 0 ? ch[0] : defval;
}
function getStringByFieldName(node, name, defval = null) {
let ch = node.childrenForFieldName(name);
return ch.length > 0 ? ch[0].text : defval;
}
function getFloatByFieldName(node, name, defval = null) {
let ch = node.childrenForFieldName(name);
if(ch.length == 0) {
return defval;
}
let floatVal = parseFloat(ch[0].text);
if(isNaN(floatVal)) {
throw new Error(`Cannot parse float value '${ch[0].text}'`);
}
return floatVal;
}
function acceptNumber(n) {
let result = parseFloat(n.text);
return isNaN(result) ? null : result;
}
function acceptNumberZeroToOne(n) {
let result = parseFloat(n.text);
return (isNaN(result) || result > 1 || result < 0) ? null : result;
}
function acceptPositiveNumberOrZero(n) {
let result = parseFloat(n.text);
return (isNaN(result) || result < 0) ? null : result;
}
function acceptAllStrings(n) {
return n.text;
}
function acceptRGBString(node) {
const channels = node.childrenForFieldName("channel");
if(channels.length !== 3) {
return null;
}
let result = [];
for (let c = 0; c < 3; c++) {
const channelStr = channels[c].text;
let channelVal = parseFloat(channelStr);
if(isNaN(channelVal) || channelVal < 0 || channelVal > 255) {
return null;
}
else {
result.push(channelVal);
}
}
return result;
}
function acceptRGBDiffString(node) {
const channels = node.childrenForFieldName("channel");
if(channels.length !== 3) {
return null;
}
let result = [];
for (let c = 0; c < 3; c++) {
const channelStr = channels[c].text;
let channelVal = parseFloat(channelStr);
if(isNaN(channelVal)) {
return null;
}
else {
result.push(channelVal);
}
}
return result;
}