-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredicates.js
More file actions
175 lines (145 loc) · 4.33 KB
/
predicates.js
File metadata and controls
175 lines (145 loc) · 4.33 KB
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
170
171
172
173
174
175
// predicates.js
var tester = Object.prototype.toString;
function has_matching_paren(text){
// accepts a string and determines if there are equal # of
// matching parens
var i; // counter
var tokens = tokenize(text);
var left_paren = 0; // counters for paren
var right_paren = 0; // counters for paren
for (i=0; i < tokens.length; i++) {
if (is_lparen(tokens[i])) {
left_paren += 1;
} else if (is_rparen(tokens[i])) {
right_paren += 1;
}
}
return (left_paren.length === right_paren.length);
}
function is_dotted_pair(AST) {
return((AST.length == 3) && (AST[1] === "."));
}
function is_backquote(item) {
return item === '`';
}
function is_forwardquote(item) {
return item === "'";
}
function is_comma(item) {
return item === ',';
}
function is_lparen(item) {
return item === '(';
}
function is_rparen(item) {
return item === ')';
}
function is_forward_quoted(node) { // inside ast
return (node[0] === 'QUOTE');
}
function is_backquoted(node) {
return (node[0] === 'BACKQUOTE');
}
function is_comma_escaped(node) {
return (node[0] === 'COMMA');
}
function is_ast_meta_elem(node) {
return is_comma_escaped(node) || is_backquoted(node) || is_forward_quoted(node);
}
function is_quoted(node) {
return ((is_forward_quoted(node)) || (is_backquoted(node)));
}
function is_object(test) {
return tester.call(test) === '[object Object]';
}
function is_null(test) {
return tester.call(test) === '[object Null]';
}
function is_array(test) {
return tester.call(test) === '[object Array]';
}
function is_string(test) {
return tester.call(test) === '[object String]';
}
function is_number(test) {
return tester.call(test) === '[object Number]';
}
function is_function(test) {
return tester.call(test) === '[object Function]';
}
function is_boolean(test) {
return tester.call(test) === '[object Boolean]';
}
function is_quoted_twice(string) {
// check for two sets of quotes at the beginning and ending of a string
var beginning = false;
var ending = false;
var beginning_group;
var ending_group;
var quote_test = /(\'\")|(\"\')|(\"\")|(\'\')/g;
string.replace(quote_test,
function () {
if (arguments[arguments.length-2] == 0) {
// acquire beginning group
beginning = true;
}
if (arguments[arguments.length - 1].length - 2 == arguments[arguments.length - 2]){
// acquire end group
ending = true;
}
});
if ((beginning == true) && (ending == true)) { // check that groups match
return true;
} else {
return false;
}
}
function is_quoted_once(string) {
// check for a single quote( either ' or " ) at the beginning and end of a string
var quote_test = /(\')|(\")/g;
var beginning = false;
var ending = false;
string.replace(quote_test,
function () {
if (arguments[arguments.length-2] == 0) {
beginning = true;
}
if (arguments[arguments.length - 1].length - 2 == arguments[arguments.length -1]){
ending = true;
}
});
if ((beginning == true) && (ending == true)) {
return true;
} else {
return false;
}
}
exports.is_dotted_pair = is_dotted_pair;
exports.is_backquote = is_backquote;
exports.is_forwardquote = is_forwardquote;
exports.is_quoted_twice = is_quoted_twice;
exports.is_quoted_once = is_quoted_once;
exports.is_comma = is_comma;
exports.is_lparen = is_lparen;
exports.is_rparen = is_rparen;
exports.is_object = is_object;
exports.is_null = is_null;
exports.is_array = is_array;
exports.is_string = is_string;
exports.is_number = is_number;
exports.is_function = is_function;
exports.is_quoted = is_quoted;
exports.is_forward_quoted = is_forward_quoted;
exports.is_backquoted = is_backquoted;
exports.is_comma_escaped = is_comma_escaped;
exports.is_ast_meta_elem = is_ast_meta_elem;
// console.log("given: ", '\""hammer"\"');
// console.log(double_to_single_quoted('"hammer"'));
// console.log("given: ", '"\"hammer"\"');
// console.log(double_to_single_quoted('"hammer"'));
// console.log("given: ", '"\"hammer\""');
// console.log(double_to_single_quoted('"hammer"'));
// console.log("given: ", '"hammer"');
// console.log(single_to_none('"hammer"'));
// console.log("given: ", "'hammer'");
// console.log(single_to_none("'hammer'"));