MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

improve highlighing for operators

+122 -20
+122 -20
src/highlight/iter.c
··· 139 139 return (i + 1 < input_len && input[i] == '=' && input[i + 1] == '>'); 140 140 } 141 141 142 + static bool find_matching_close_paren(const char *input, size_t input_len, size_t open_paren, size_t *close_paren) { 143 + size_t depth = 0; 144 + for (size_t i = open_paren + 1; i < input_len; i++) { 145 + unsigned char ch = (unsigned char)input[i]; 146 + if (ch == '(') { 147 + depth++; 148 + continue; 149 + } 150 + if (ch == ')') { 151 + if (depth == 0) { 152 + *close_paren = i; 153 + return true; 154 + } 155 + depth--; 156 + } 157 + } 158 + return false; 159 + } 160 + 161 + static bool has_declaration_keyword_before(const char *input, size_t ident_start) { 162 + size_t ws, wstart, wlen; 163 + ws = skip_inline_ws_backward(input, ident_start); 164 + if (ws == 0) return false; 165 + 166 + size_t wend = ws; 167 + size_t i = ws; 168 + while (i > 0 && is_ident_continue((unsigned char)input[i - 1])) i--; 169 + wstart = i; 170 + wlen = wend - i; 171 + 172 + return (wlen == 5 && memcmp(input + wstart, "const", 5) == 0) || 173 + (wlen == 3 && memcmp(input + wstart, "let", 3) == 0) || 174 + (wlen == 3 && memcmp(input + wstart, "var", 3) == 0); 175 + } 176 + 177 + static bool is_assigned_arrow_function(const char *input, size_t input_len, size_t ident_start, size_t ident_end) { 178 + if (!has_declaration_keyword_before(input, ident_start)) return false; 179 + 180 + size_t i = skip_inline_ws_forward(input, input_len, ident_end); 181 + if (i >= input_len || input[i] != '=') return false; 182 + if (i + 1 < input_len && input[i + 1] == '>') return false; 183 + i++; 184 + 185 + i = skip_inline_ws_forward(input, input_len, i); 186 + if (i >= input_len) return false; 187 + 188 + if (is_arrow_after(input, input_len, i)) return false; 189 + 190 + if (is_ident_begin((unsigned char)input[i])) { 191 + size_t j = i + 1; 192 + while (j < input_len && is_ident_continue((unsigned char)input[j])) j++; 193 + if (is_arrow_after(input, input_len, j)) return true; 194 + } 195 + 196 + if (input[i] == '(') { 197 + size_t close = 0; 198 + if (find_matching_close_paren(input, input_len, i, &close)) 199 + if (is_arrow_after(input, input_len, close + 1)) return true; 200 + } 201 + 202 + if (is_ident_begin((unsigned char)input[i]) || input[i] == '(') { 203 + size_t j = i; 204 + if (input[j] == '(') { 205 + size_t close = 0; 206 + if (find_matching_close_paren(input, input_len, j, &close)) j = close + 1; 207 + else return false; 208 + } else { 209 + j++; 210 + while (j < input_len && is_ident_continue((unsigned char)input[j])) j++; 211 + } 212 + 213 + j = skip_inline_ws_forward(input, input_len, j); 214 + if (j < input_len && input[j] == ':') { 215 + j++; 216 + size_t depth = 0; 217 + while (j < input_len) { 218 + unsigned char ch = (unsigned char)input[j]; 219 + if (ch == '(' || ch == '<') { depth++; j++; continue; } 220 + if (ch == ')' || ch == '>') { 221 + if (depth > 0) depth--; 222 + j++; 223 + continue; 224 + } 225 + if (depth == 0 && (ch == '=' || ch == ',')) break; 226 + j++; 227 + } 228 + if (is_arrow_after(input, input_len, j)) return true; 229 + } 230 + } 231 + 232 + if (input[i] == 'a') { 233 + if (i + 5 <= input_len && memcmp(input + i, "async", 5) == 0 && 234 + (i + 5 >= input_len || !is_ident_continue((unsigned char)input[i + 5]))) { 235 + size_t j = skip_inline_ws_forward(input, input_len, i + 5); 236 + if (j < input_len && input[j] == '(') { 237 + size_t close = 0; 238 + if (find_matching_close_paren(input, input_len, j, &close)) 239 + if (is_arrow_after(input, input_len, close + 1)) return true; 240 + } 241 + if (j < input_len && is_ident_begin((unsigned char)input[j])) { 242 + size_t k = j + 1; 243 + while (k < input_len && is_ident_continue((unsigned char)input[k])) k++; 244 + if (is_arrow_after(input, input_len, k)) return true; 245 + } 246 + } 247 + } 248 + 249 + if (input[i] == 'f') { 250 + if (i + 8 <= input_len && memcmp(input + i, "function", 8) == 0 && 251 + (i + 8 >= input_len || !is_ident_continue((unsigned char)input[i + 8]))) 252 + return true; 253 + } 254 + 255 + return false; 256 + } 257 + 142 258 static bool has_function_keyword_before_paren(const char *input, size_t open_paren) { 143 259 size_t word_start = 0; 144 260 size_t word_len = 0; ··· 188 304 if (ch == '(') { 189 305 if (depth == 0) { 190 306 *open_paren = i; 191 - return true; 192 - } 193 - depth--; 194 - } 195 - } 196 - return false; 197 - } 198 - 199 - static bool find_matching_close_paren(const char *input, size_t input_len, size_t open_paren, size_t *close_paren) { 200 - size_t depth = 0; 201 - for (size_t i = open_paren + 1; i < input_len; i++) { 202 - unsigned char ch = (unsigned char)input[i]; 203 - if (ch == '(') { 204 - depth++; 205 - continue; 206 - } 207 - if (ch == ')') { 208 - if (depth == 0) { 209 - *close_paren = i; 210 307 return true; 211 308 } 212 309 depth--; ··· 469 566 cls = HL_FUNCTION; 470 567 } else if (is_member_access) { 471 568 cls = HL_PROPERTY; 569 + } else if (is_assigned_arrow_function(input, input_len, start, i)) { 570 + cls = HL_FUNCTION_NAME; 472 571 } else if (it->ctx == HL_CTX_AFTER_FUNCTION) { 473 572 cls = HL_FUNCTION_NAME; 474 573 it->ctx = HL_CTX_NONE; ··· 518 617 return true; 519 618 } 520 619 521 - if (c == '<' || c == '>' || c == '=') { 620 + if (c == '<' || c == '>' || c == '=' || 621 + c == '+' || c == '-' || c == '*' || c == '/' || 622 + c == '%' || c == '&' || c == '|' || c == '^' || 623 + c == '~' || c == '!' || c == '?') { 522 624 it->ctx = HL_CTX_NONE; 523 625 *out = (hl_span){ i, 1, HL_OPERATOR }; 524 626 it->pos = i + 1;