Tokenization of JavaScript programming language source text. Returns 1 when a next valid token is recognized. Then the token is made avaliable in the NUL-terminated string `token`, and its token class is indicated via `type` and its start location in the source via `line` and `col`. Returns 0 upon EOF or error. */
| 79 | Returns 0 upon EOF or error. |
| 80 | */ |
| 81 | static int tokenize(char *token, const char **type, |
| 82 | unsigned *line, unsigned *col) |
| 83 | { |
| 84 | unsigned len; |
| 85 | int cc; |
| 86 | *type = "unknown"; |
| 87 | |
| 88 | do { // infinite loop; after token recognized breaks out. |
| 89 | len = 0; |
| 90 | cc = get(); |
| 91 | |
| 92 | restart: |
| 93 | // cc already read. |
| 94 | |
| 95 | /*** WHITE-SPACE ***/ |
| 96 | |
| 97 | while (strchr(" \t\n\r\f\v", cc)) |
| 98 | cc = get(); |
| 99 | // cc !in [ \t\n\r\f\v], maybe EOF |
| 100 | if (cc == EOF) |
| 101 | return 0; |
| 102 | |
| 103 | /*** LINE COMMENT AND BLOCK COMMENT ***/ |
| 104 | |
| 105 | if (cc == '/') { |
| 106 | cc = get(); |
| 107 | if (cc == '/') { |
| 108 | // Skip till end-of-line (\n exclusive): |
| 109 | while ((cc = get()) != EOF && cc != '\n' && cc != '\r') |
| 110 | ; |
| 111 | // cc == '\n' || cc == '\r' || cc == EOF |
| 112 | if (cc == '\r') { |
| 113 | if (!nowarn) |
| 114 | fprintf(stderr, |
| 115 | "(W): Unexpected continuation in line comment.\n"); |
| 116 | // Effectively ignore any \ and terminate logical line: |
| 117 | cc == '\n'; |
| 118 | } |
| 119 | goto restart; |
| 120 | } |
| 121 | |
| 122 | if (cc == '*') { |
| 123 | // Remember start position: |
| 124 | unsigned lin = linenr; |
| 125 | |
| 126 | // Skip till */ inclusive: |
| 127 | int nc = get(); // if EOF next get will be EOF too |
| 128 | do { |
| 129 | cc = nc; |
| 130 | nc = get(); |
| 131 | if (nc == EOF) { // Error! |
| 132 | fprintf(stderr, |
| 133 | "(E): [%s:%u] Unexpected end-of-file in /* comment.\n", |
| 134 | filename, lin); |
| 135 | unexpect_eof++; |
| 136 | return 0; |
| 137 | } |
| 138 | } while (cc != '*' || nc != '/'); |
no test coverage detected