aboutsummaryrefslogtreecommitdiffstats
path: root/tools/node_modules/expresso/deps/jscoverage/highlight.c
blob: 7dddf3be2f380f40bfc472ef9f5103042cd9a972 (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
    highlight.c - JavaScript syntax highlighting
    Copyright (C) 2008 siliconforks.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <config.h>

#include "highlight.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include <jslock.h>
#include <jsscan.h>

#include "util.h"

enum Class {
  CLASS_NONE,
  CLASS_COMMENT,
  CLASS_REGEXP,
  CLASS_NUMBER,
  CLASS_STRING,
  CLASS_SPECIALCHAR,
  CLASS_KEYWORD,
  CLASS_TYPE,
  CLASS_SYMBOL,
  CLASS_CBRACKET
};

static const char * get_class_name(enum Class class) {
  switch (class) {
  case CLASS_NONE:
    abort();
    break;
  case CLASS_COMMENT:
    return "c";
    break;
  case CLASS_REGEXP:
    return "s";
    break;
  case CLASS_NUMBER:
    return "s";
    break;
  case CLASS_STRING:
    return "s";
    break;
  case CLASS_SPECIALCHAR:
    return "t";
    break;
  case CLASS_KEYWORD:
    return "k";
    break;
  case CLASS_TYPE:
    return "k";
    break;
  case CLASS_SYMBOL:
    return "k";
    break;
  case CLASS_CBRACKET:
    return "k";
    break;
  default:
    abort();
    break;
  }
}

static const char * g_id;
static const jschar * g_characters;
static size_t g_num_characters;
static Stream * g_output;
static size_t character_offset;
static uint16_t line_num;
static uint16_t column_num;
static enum Class current_class;

static void output_character(jschar c, enum Class class) {
  if (c == '\r' || c == '\n' || c == 0x2028 || c == 0x2029) {
    class = CLASS_NONE;
  }

  if (class != current_class) {
    /* output the end tag */
    if (current_class != CLASS_NONE) {
      Stream_write_string(g_output, "</span>");
    }

    current_class = class;

    /* output the start tag */
    if (current_class != CLASS_NONE) {
      Stream_printf(g_output, "<span class=\"%s\">", get_class_name(class));
    }
  }

  if (column_num == UINT16_MAX) {
    fatal("%s: script contains a line with more than 65,535 columns", g_id);
  }
  column_num++;
  switch (c) {
  case '&':
    Stream_write_string(g_output, "&amp;");
    break;
  case '<':
    Stream_write_string(g_output, "&lt;");
    break;
  case '>':
    Stream_write_string(g_output, "&gt;");
    break;
  case '\t':
    Stream_write_char(g_output, c);
    break;
  case '\r':
  case '\n':
  case 0x2028:
  case 0x2029:
    if (c == '\r' && character_offset + 1 < g_num_characters && g_characters[character_offset + 1] == '\n') {
      break;
    }
    Stream_write_char(g_output, '\n');
    column_num = 0;
    if (line_num == UINT16_MAX) {
      fatal("%s: script contains more than 65,535 lines", g_id);
    }
    line_num++;
    break;
  default:
    if (32 <= c && c <= 126) {
      Stream_write_char(g_output, c);
    }
    else {
      Stream_printf(g_output, "&#%d;", c);
    }
    break;
  }
  character_offset++;
}

static void mark_nontoken_chars(uint16_t end_line, uint16_t end_column) {
  enum State {
    STATE_NORMAL,
    STATE_LINE_COMMENT,
    STATE_MULTILINE_COMMENT
  };

  enum State state = STATE_NORMAL;
  while (character_offset < g_num_characters) {
    if (end_line != 0 && line_num > end_line) {
      break;
    }
    else if (line_num == end_line && column_num >= end_column) {
      break;
    }

    jschar c = g_characters[character_offset];
    if (c == '\0') {
      fatal("%s: script contains NULL character", g_id);
    }

    switch (state) {
    case STATE_NORMAL:
      if (c == '/' && character_offset + 1 < g_num_characters && g_characters[character_offset + 1] == '/') {
        state = STATE_LINE_COMMENT;
      }
      else if (c == '/' && character_offset + 1 < g_num_characters && g_characters[character_offset + 1] == '*') {
        state = STATE_MULTILINE_COMMENT;
        output_character('/', CLASS_COMMENT);
        output_character('*', CLASS_COMMENT);
        continue;
      }
      break;
    case STATE_LINE_COMMENT:
      if (c == '\r' || c == '\n' || c == 0x2028 || c == 0x2029) {
        state = STATE_NORMAL;
      }
      break;
    case STATE_MULTILINE_COMMENT:
      if (c == '*' && character_offset + 1 < g_num_characters && g_characters[character_offset + 1] == '/') {
        output_character('*', CLASS_COMMENT);
        output_character('/', CLASS_COMMENT);
        state = STATE_NORMAL;
        continue;
      }
      break;
    }

    if (state == STATE_NORMAL) {
      output_character(c, CLASS_NONE);
    }
    else {
      output_character(c, CLASS_COMMENT);
    }
  }
}

void jscoverage_highlight_js(JSContext * context, const char * id, const jschar * characters, size_t num_characters, Stream * output) {
  g_id = id;
  g_characters = characters;
  g_num_characters = num_characters;
  g_output = output;

  character_offset = 0;
  line_num = 1;
  column_num = 0;
  current_class = CLASS_NONE;

  /* tokenize the JavaScript */
  JSTokenStream token_stream;
  if (! js_InitTokenStream(context, &token_stream, characters, num_characters, NULL, NULL, 1)) {
    fatal("cannot create token stream from JavaScript file %s", id);
  }

  for (;;) {
    JSTokenType tt = js_GetToken(context, &token_stream);

    if (tt == TOK_ERROR) {
      fatal("JavaScript parse error: %s: line = %d, col = %d\n", id, line_num, column_num);
    }

    if (tt == TOK_EOF) {
      mark_nontoken_chars(0, 0);
      break;
    }

    /* mark the chars before the token */
    JSToken t = CURRENT_TOKEN(&token_stream);
    mark_nontoken_chars(t.pos.begin.lineno, t.pos.begin.index);

    /* mark the token */
    enum Class class;
    switch (tt) {
    case TOK_ERROR:
    case TOK_EOF:
      abort();
    case TOK_EOL:
      class = CLASS_NONE;
      token_stream.flags |= TSF_OPERAND;
      break;
    case TOK_SEMI:
    case TOK_COMMA:
    case TOK_ASSIGN:
    case TOK_HOOK:
    case TOK_COLON:
    case TOK_OR:
    case TOK_AND:
    case TOK_BITOR:
    case TOK_BITXOR:
    case TOK_BITAND:
    case TOK_EQOP:
    case TOK_RELOP:
    case TOK_SHOP:
    case TOK_PLUS:
    case TOK_MINUS:
    case TOK_STAR:
    case TOK_DIVOP:
      class = CLASS_SYMBOL;
      token_stream.flags |= TSF_OPERAND;
      break;
    case TOK_UNARYOP:
      switch (t.t_op) {
      case JSOP_NEG:
      case JSOP_POS:
      case JSOP_NOT:
      case JSOP_BITNOT:
        class = CLASS_SYMBOL;
        token_stream.flags |= TSF_OPERAND;
        break;
      case JSOP_TYPEOF:
        class = CLASS_KEYWORD;
        token_stream.flags |= TSF_OPERAND;
        break;
      case JSOP_VOID:
        class = CLASS_TYPE;
        token_stream.flags |= TSF_OPERAND;
        break;
      default:
        abort();
      }
      break;
    case TOK_INC:
    case TOK_DEC:
      class = CLASS_SYMBOL;
      /* token_stream.flags does not change w.r.t. TSF_OPERAND */
      break;
    case TOK_DOT:
    case TOK_LB:
      class = CLASS_SYMBOL;
      token_stream.flags |= TSF_OPERAND;
      break;
    case TOK_RB:
      class = CLASS_SYMBOL;
      token_stream.flags &= ~TSF_OPERAND;
      break;
    case TOK_LC:
      class = CLASS_CBRACKET;
      token_stream.flags |= TSF_OPERAND;
      break;
    case TOK_RC:
      class = CLASS_CBRACKET;
      token_stream.flags &= ~TSF_OPERAND;
      break;
    case TOK_LP:
      class = CLASS_SYMBOL;
      token_stream.flags |= TSF_OPERAND;
      break;
    case TOK_RP:
      class = CLASS_SYMBOL;
      token_stream.flags &= ~TSF_OPERAND;
      break;
    case TOK_NAME:
      class = CLASS_NONE;
      token_stream.flags &= ~TSF_OPERAND;
      if (js_PeekToken(context, &token_stream) == TOK_LP) {
        /* function */
        class = CLASS_NONE;
      }
      break;
    case TOK_NUMBER:
      class = CLASS_NUMBER;
      token_stream.flags &= ~TSF_OPERAND;
      break;
    case TOK_STRING:
      class = CLASS_STRING;
      token_stream.flags &= ~TSF_OPERAND;
      break;
    case TOK_REGEXP:
      class = CLASS_REGEXP;
      token_stream.flags &= ~TSF_OPERAND;
      break;
    case TOK_PRIMARY:
      switch (t.t_op) {
      case JSOP_TRUE:
      case JSOP_FALSE:
      case JSOP_NULL:
      case JSOP_THIS:
        class = CLASS_KEYWORD;
        token_stream.flags &= ~TSF_OPERAND;
        break;
      default:
        abort();
      }
      break;
    case TOK_FUNCTION:
      class = CLASS_KEYWORD;
      token_stream.flags |= TSF_OPERAND;
      break;
    case TOK_IF:
    case TOK_ELSE:
    case TOK_SWITCH:
    case TOK_CASE:
    case TOK_DEFAULT:
    case TOK_WHILE:
    case TOK_DO:
    case TOK_FOR:
    case TOK_BREAK:
    case TOK_CONTINUE:
    case TOK_IN:
    case TOK_VAR:
    case TOK_WITH:
    case TOK_RETURN:
    case TOK_NEW:
    case TOK_DELETE:
      token_stream.flags |= TSF_OPERAND;
      class = CLASS_KEYWORD;
      break;
    case TOK_DEFSHARP:
    case TOK_USESHARP:
      abort();
      break;
    case TOK_TRY:
    case TOK_CATCH:
    case TOK_FINALLY:
    case TOK_THROW:
    case TOK_INSTANCEOF:
    case TOK_DEBUGGER:
      token_stream.flags |= TSF_OPERAND;
      class = CLASS_KEYWORD;
      break;
    case TOK_XMLSTAGO:
    case TOK_XMLETAGO:
    case TOK_XMLPTAGC:
    case TOK_XMLTAGC:
    case TOK_XMLNAME:
    case TOK_XMLATTR:
    case TOK_XMLSPACE:
    case TOK_XMLTEXT:
    case TOK_XMLCOMMENT:
    case TOK_XMLCDATA:
    case TOK_XMLPI:
    case TOK_AT:
    case TOK_DBLCOLON:
    case TOK_ANYNAME:
    case TOK_DBLDOT:
    case TOK_FILTER:
    case TOK_XMLELEM:
    case TOK_XMLLIST:
      abort();
      break;
    case TOK_YIELD:
      token_stream.flags |= TSF_OPERAND;
      class = CLASS_KEYWORD;
      break;
    case TOK_ARRAYCOMP:
    case TOK_ARRAYPUSH:
    case TOK_LEXICALSCOPE:
      abort();
      break;
    case TOK_LET:
      token_stream.flags |= TSF_OPERAND;
      class = CLASS_KEYWORD;
      break;
    case TOK_SEQ:
    case TOK_FORHEAD:
    case TOK_RESERVED:
    case TOK_LIMIT:
      abort();
      break;
    default:
      abort();
      break;
    }

    uint16_t start_line = t.pos.begin.lineno;
    uint16_t end_line = t.pos.end.lineno;
    uint16_t start_column = t.pos.begin.index;
    uint16_t end_column = t.pos.end.index;
    assert(line_num == start_line);
    assert(column_num == start_column);
    if (start_line == end_line && start_column >= end_column) {
      fatal("%s: script contains line with more than 65,535 characters", id);
    }
    for (;;) {
      assert(character_offset < num_characters);
      jschar c = characters[character_offset];
      if (tt == TOK_STRING && c == '\\') {
        output_character(c, CLASS_SPECIALCHAR);
        assert(character_offset < num_characters);
        c = characters[character_offset];
        output_character(c, CLASS_SPECIALCHAR);
      }
      else {
        output_character(c, class);
      }

      if (line_num > end_line) {
        break;
      }
      else if (line_num == end_line && column_num >= end_column) {
        break;
      }
    }

    assert(line_num == end_line);
    assert(column_num = end_column);
  }

  if (current_class != CLASS_NONE) {
    output_character('\n', CLASS_NONE);
  }

  js_CloseTokenStream(context, &token_stream);
}