-
Notifications
You must be signed in to change notification settings - Fork 2
/
syntaxFilters.js
169 lines (155 loc) · 4.73 KB
/
syntaxFilters.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
function x0Sisters(sTree, cat){
var x0SistersFound = false;
if(sTree.children && sTree.children.length){
var numX0 = 0;
for(var i=0; i<sTree.children.length; i++){
var child = sTree.children[i];
if(child.cat === cat){
numX0++;
}
if(numX0 > 1){
x0SistersFound = true;
break;
}
x0SistersFound = x0Sisters(child, cat);
if(x0SistersFound) break;
}
}
return x0SistersFound;
}
function ternaryNodes(sTree, maxBr){
var ternaryNodesFound = false;
if(sTree.children && sTree.children.length){
if(sTree.children.length > maxBr){
ternaryNodesFound = true;
return true;
}
for(var i=0; i<sTree.children.length; i++){
var child = sTree.children[i];
ternaryNodesFound = ternaryNodes(child, maxBr);
if(ternaryNodesFound) break;
}
}
return ternaryNodesFound;
}
function unaryNodes(sTree, minBr){
var unaryNodesFound = false;
if(sTree.children && sTree.children.length){
if(sTree.children.length < minBr){
unaryNodesFound = true;
return true;
}
for(var i=0; i<sTree.children.length; i++){
var child = sTree.children[i];
unaryNodesFound = unaryNodes(child, minBr);
if(unaryNodesFound) break;
}
}
return unaryNodesFound;
}
function headsOnWrongSide(sTree, side, strict){
var badHeadFound = false;
if(sTree.children && sTree.children.length){
var i = 0;
while(!badHeadFound && i<sTree.children.length){
var numChildren = sTree.children.length;
var child = sTree.children[i];
if(child.cat==='x0' && numChildren > 1){
if((side==='right' && i===0) || (side==='left' && i===numChildren-1)){
badHeadFound = true;
}
if(strict==='strict'){
if((side==='right' && i<sTree.children.length-1) || (side==='left' && i>0)){
badHeadFound = true;
}
}
}
else{
badHeadFound = headsOnWrongSide(child, side);
}
i++;
}
}
return badHeadFound;
}
// returns true if sTree is a mirror of an earlier tree in sTreeList
// returns false if sTree is not a mirror image of an earlier tree in sTreeList
function mirrorImages(sTree, sTreeList) {
var mirrorImageExists = false;
var index = sTreeList.indexOf(sTree);
//var reverseTree = JSON.parse(JSON.stringify(sTree));
for(var i = 0; i < index; i++) {
var currTree = sTreeList[i];
if(checkMirror(currTree, sTree)) {
mirrorImageExists = true;
return mirrorImageExists;
}
}
return mirrorImageExists;
}
// check for if trees are mirror images of eachother
function checkMirror(sTree, rTree) {
if(sTree.children && sTree.children.length){
if(rTree.children === undefined || sTree.children.length !== rTree.children.length) {
return false;
}
for(var i=0; i<sTree.children.length; i++){
var sChild = sTree.children[i];
var rChild = rTree.children[sTree.children.length-i-1];
if (sChild.cat !== rChild.cat) {
return false;
}
if(!checkMirror(sChild, rChild)){
return false;
//quit early if checkMirror evaluates to false for any child.
}
}
}
//if sTree has no children but rTree does
else if(rTree.children && rTree.children.length > 0){
return false;
}
return true;
}
// Return true if there is any node that has more than two children x such that x.cat === 'xp'.
// Two xp children is fine, but three (or more) is not fine.
function threeXPs(sTree) {
var threeXPsFound = false;
if(sTree.children && sTree.children.length){
// console.log(sTree.children)
var numXPs = 0;
for(var i=0; i<sTree.children.length; i++){
var child = sTree.children[i];
if(child.cat === 'xp') {
numXPs += 1;
}
if(numXPs > 2) {
threeXPsFound = true;
break;
}
threeXPsFound = threeXPs(child);
if(threeXPsFound) break;
}
}
return threeXPsFound;
}
// Return true if there is a node in it whose children are all xps, false if all nodes have an x0 child
function containsAdjunct(sTree) {
var adjunctFound = false;
if(sTree.children && sTree.children.length){
var numXPs = 0;
for(var i=0; i<sTree.children.length; i++){
var child = sTree.children[i];
if(child.cat === 'xp') {
numXPs += 1;
}
if(numXPs == sTree.children.length) {
adjunctFound = true;
break;
}
adjunctFound = containsAdjunct(child);
if(adjunctFound) break;
}
}
return adjunctFound;
}