| |
| |
| |
| |
| |
| |
|
|
| #include "libtoken.h" |
|
|
| |
| const char *filename = "stdin"; |
| unsigned linenr = 1; |
| unsigned column = 0; |
| unsigned char_count = 0; |
| unsigned utf8_count = 0; |
|
|
| int buffer[MAX_BUF]; |
| unsigned buffered = 0; |
| unsigned saved_col = 0; |
|
|
| |
| int debug = 0; |
| int verbose = 0; |
| int nowarn = 0; |
|
|
| unsigned illegals = 0; |
| unsigned unexpect_eof = 0; |
| int hash_as_comment = 0; |
| int newline_token = 0; |
| int comment_token = 0; |
| int whitespace_token = 0; |
| int continuation_token = 0; |
|
|
| static int logical_lines = 0; |
|
|
| |
| const char *token_class[] = { |
| "identifier", |
| "keyword", |
| "string", |
| "character", |
| "integer", |
| "floating", |
| "operator", |
| "preprocessor", |
| "line_comment", |
| "block_comment", |
| "whitespace", |
| "newline", |
| "continuation", |
| "filename", |
| "endoffile" |
| }; |
|
|
| |
|
|
| |
| |
| |
| static const char *C_keywords[] = { |
| "_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex", |
| "_Generic", "_Imaginary", "_Noreturn", "_Static_assert", |
| "_Thread_local", |
|
|
| "auto", "break", "case", "char", "const", |
| "continue", "default", "do", "double", "else", |
| "enum", "extern", "float", "for", "goto", |
| "if", "inline", "int", "long", "register", |
| "restrict", "return", "short", "signed", "sizeof", |
| "static", "struct", "switch", "typedef", "union", |
| "unsigned", "void", "volatile", "while" |
| }; |
|
|
| #if 0 |
| |
| static const char *CPP_keywords[] = { |
| "alignas", "alignof", "and", "and_eq", "asm", |
| "auto", "bitand", "bitor", "bool", "break", |
| "case", "catch", "char", "char16_t", "char32_t", |
| "class", "compl", "const", "const_cast", "constexpr", |
| "continue", "decltype", "default", "delete", "do", |
| "double", "dynamic_cast", "else", "enum", "explicit", |
| "export", "extern", "false", "float", "for", |
| "friend", "goto", "if", "inline", "int", |
| "long", "mutable", "namespace", "new", "noexcept", |
| "not", "not_eq", "nullptr", "operator", "or", |
| "or_eq", "private", "protected", "public", "register", |
| "reinterpret_cast", "return", "short", "signed", "sizeof", |
| "static", "static_assert", "static_cast", "struct", "switch", |
| "template", "this", "thread_local", "throw", "true", |
| "try", "typedef", "typeid", "typename", "union", |
| "unsigned", "using", "virtual", "void", "volatile", |
| "wchar_t", "while", "xor", "xor_eq" |
| }; |
| #endif |
|
|
| |
| static const char *CPP_keywords[] = { |
| "alignas", "alignof", "and", "and_eq", "asm", |
| "auto", "bitand", "bitor", "bool", "break", |
| "case", "catch", "char", "char16_t", "char32_t", |
| "char8_t", "class", "co_await", "co_return", "co_yield", |
| "compl", "concept", "const", "const_cast", "consteval", |
| "constexpr", "constinit", "continue", "decltype", "default", |
| "delete", "do", "double", "dynamic_cast", "else", |
| "enum", "explicit", "export", "extern", "false", |
| "float", "for", "friend", "goto", "if", |
| "inline", "int", "long", "mutable", "namespace", |
| "new", "noexcept", "not", "not_eq", "nullptr", |
| "operator", "or", "or_eq", "private", "protected", |
| "public", "register", "reinterpret_cast", "requires","return", |
| "short", "signed", "sizeof", "static", "static_assert", |
| "static_cast", "struct", "switch", "template", "this", |
| "thread_local", "throw", "true", "try", "typedef", |
| "typeid", "typename", "union", "unsigned", "using", |
| "virtual", "void", "volatile", "wchar_t", "while", |
| "xor", "xor_eq" |
| }; |
|
|
| |
| |
| static const char *Java_keywords[] = { |
| "abstract", "assert", "boolean", "break", "byte", "case", |
| "catch", "char", "class", "const", "continue", "default", |
| "do", "double", "else", "enum", "extends", "final", |
| "finally", "float", "for", "goto", "if", "implements", |
| "import", "instanceof", "int", "interface", "long", "native", |
| "new", "package", "private", "protected", "public", "return", |
| "short", "static", "strictfp","super", "switch", "synchronized", |
| "this", "throw", "throws", "transient", "try", "void", |
| "volatile", "while" |
| }; |
|
|
| static const char *Python_keywords[] = { |
| "False", "None", "True", "and", "as", "assert", "async", |
| "await", "break", "class", "continue", "def", "del", "elif", |
| "else", "except", "finally", "for", "from", "global", "if", |
| "import", "in", "is", "lambda", "nonlocal", "not", "or", |
| "pass", "raise", "return", "try", "while", "with", "yield" |
| }; |
|
|
| |
| |
| |
| |
| static const char *JavaScript_keywords[] = { |
| "abstract", "await", "boolean", "break", "byte", |
| "case", "catch", "char", "class", "const", |
| "continue", "debugger", "default", "delete", "do", |
| "double", "else", "enum", "export", "extends", |
| "false", "final", "finally", "float", "for", |
| "function", "goto", "if", "implements", "import", |
| "in", "instanceof", "int", "interface", "let", |
| "long", "native", "new", "null", "package", |
| "private", "protected", "public", "return", "short", |
| "static", "super", "switch", "synchronized", "this", |
| "throw", "throws", "transient", "true", "try", |
| "typeof", "var", "void", "volatile", "while", |
| "with", "yield" |
| }; |
|
|
| #define num_keywords(lang) sizeof(lang##_keywords)/sizeof(lang##_keywords[0]); |
|
|
| |
| |
| |
| |
| |
| #define lang_is_keyword(lang) \ |
| static const char *lang##_is_keyword(const char *word) \ |
| { \ |
| int i = 0, j = num_keywords(lang); \ |
| while (i < j) { \ |
| int k = (i + j) >> 1 ; \ |
| const char *kw = lang##_keywords[k]; \ |
| int cmp = strcmp(word, kw); \ |
| if (!cmp) \ |
| return kw; \ |
| if (cmp < 0) j = k; else i = k + 1; \ |
| } \ |
| return 0; \ |
| } |
|
|
| |
| |
| lang_is_keyword(C) |
| |
| lang_is_keyword(CPP) |
| |
| lang_is_keyword(Java) |
| |
| lang_is_keyword(Python) |
| |
| lang_is_keyword(JavaScript) |
|
|
| const char *(*is_keyword)(const char *) = C_is_keyword; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static const struct { |
| const char *ext; |
| Language lang; |
| const char *name; |
| } |
| langs[] = { |
| { ".c", C, "C" }, |
| { ".cpp", CPP, "C++" }, |
| { ".java", JAVA, "Java" }, |
| { ".js", JAVASCRIPT, "JavaScript" }, |
| { ".py", PYTHON, "Python" }, |
|
|
| |
| { ".h", C, "" }, |
| { ".C", CPP, "" }, |
| { ".cc", CPP, "" }, |
| { ".hh", CPP, "" }, |
| }; |
|
|
| const char *lang_name(Language lang) |
| { |
| return langs[lang].name; |
| } |
|
|
| static const struct { |
| |
| const char *(*is_keyword)(const char *); |
| } |
| lang_configs[] = { |
| { C_is_keyword, }, |
| { CPP_is_keyword, }, |
| { Java_is_keyword, }, |
| { JavaScript_is_keyword, }, |
| { Python_is_keyword, }, |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static void remove_BOM(void) |
| { |
| int c1 = getchar(); |
| if (c1 == 0xEF) { |
| int c2 = getchar(); |
| if (c2 == 0xBB) { |
| int c3 = getchar(); |
| if (c3 == 0xBF) { |
| return; |
| } |
| if (c3 != EOF) buffer[buffered++] = c3; |
| } |
| if (c2 != EOF) buffer[buffered++] = c2; |
| } |
| if (c1 != EOF) buffer[buffered++] = c1; |
| } |
|
|
| int open_as_stdin(const char *file) |
| { |
| filename = file; |
| if (!freopen(filename, "r", stdin)) { |
| if (!nowarn) |
| fprintf(stderr, "(W): Cannot read file %s.\n", filename); |
| return -1; |
| } |
| return set_or_detect_lang(0); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| static int normalize_newline(void) |
| { |
| |
| int cc = getchar(); |
|
|
| if (cc == '\r') { |
| |
| int nc = getchar(); |
| if (nc == '\n') { |
| char_count++; |
| utf8_count++; |
| |
| return nc; |
| } |
| |
| ungetc(nc, stdin); |
| |
| cc = '\n'; |
| } |
| return cc; |
| } |
|
|
| |
| |
| |
| |
| int get(void) |
| { |
| int cc; |
|
|
| restart: |
| |
| if (buffered) { |
| cc = buffer[--buffered]; |
| char_count++; |
| |
| if (cc == '\n' || cc == '\r') { |
| linenr++; |
| saved_col = column; |
| column = 0; |
| return cc; |
| } |
| column++; |
| return cc; |
| } |
|
|
| |
| cc = normalize_newline(); |
| if (cc == EOF) return EOF; |
| char_count++; |
| if (utf8_start(cc)) utf8_count++; |
|
|
| if (cc == '\n') { |
| linenr++; |
| saved_col = column; |
| column = 0; |
| return cc; |
| } |
|
|
| |
| if (cc == '\\') { |
| |
| int nc = normalize_newline(); |
| if (nc == '\n') { |
| char_count++; |
| utf8_count++; |
| linenr++; |
| saved_col = column+1; |
| column = 0; |
|
|
| if (logical_lines) |
| |
| |
| goto restart; |
|
|
| |
| return '\r'; |
| } |
| |
| ungetc(nc, stdin); |
| |
| } |
| column++; |
| return cc; |
| } |
|
|
| |
| |
| |
| |
| |
| void unget(int cc) |
| { |
| if (cc == EOF) return; |
| if (buffered < MAX_BUF) { |
| if (cc == '\n' || cc == '\r') { |
| linenr--; |
| |
| |
| column = saved_col; |
| } |
| else |
| column--; |
| char_count--; |
| buffer[buffered++] = cc; |
| } |
| else { |
| fprintf(stderr, "(F): Lookahead buffer overflow (MAX=%u).\n", MAX_BUF); |
| exit(2); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| Language set_or_detect_lang(const char *source) |
| { |
| int i; |
| Language lang = C; |
|
|
| if (source) { |
| |
| for (i = 0; i < sizeof(langs)/sizeof(langs[0]); i++) |
| if (!strcmp(source, langs[i].name)) { |
| lang = langs[i].lang; |
| goto done; |
| } |
| fprintf(stderr, "(E): No support for language `%s'.\n", source); |
| } |
|
|
| char *p; |
| if (p = strrchr(filename, '.')) { |
| for (i = 0; i < sizeof(langs)/sizeof(langs[0]); i++) |
| if (!strcmp(p, langs[i].ext)) { |
| lang = langs[i].lang; |
| goto done; |
| } |
| fprintf(stderr, "(E): Unknown filename extension `%s'.\n", p); |
| } |
| if (!nowarn) |
| fprintf(stderr, "(W): Assuming default language C.\n"); |
|
|
| done: |
| is_keyword = lang_configs[lang].is_keyword; |
| return lang; |
| } |
|
|
| |
| static char *token_buf = 0; |
| static unsigned token_alloc = 0; |
| static unsigned token_len = 0; |
|
|
| |
| static void token_buf_room(void) |
| { |
| if (token_len == token_alloc) { |
| if (!token_alloc) { |
| token_alloc = 65536; |
| if (!(token_buf = malloc(token_alloc))) { |
| fprintf(stderr, "(F): Allocation of token buffer failed.\n"); |
| exit(4); |
| } |
| token_buf[0] = '\0'; |
| return; |
| } |
|
|
| token_alloc <<= 1; |
| if (!(token_buf = realloc(token_buf, token_alloc))) { |
| fprintf(stderr, "(F): Reallocation of token buffer failed.\n"); |
| exit(4); |
| } |
| |
| } |
| } |
|
|
| |
| static void token_buf_push(int cc) |
| { |
| token_buf_room(); |
| |
| token_buf[token_len++] = cc; |
| } |
|
|
| |
| static int token_buf_pop(void) |
| { |
| return token_len ? token_buf[--token_len] : 0; |
| } |
|
|
| |
| static void token_buf_close(void) |
| { |
| token_buf_room(); |
| token_buf[token_len] = '\0'; |
| } |
|
|
| |
| static void token_buf_reset(void) |
| { |
| token_len = 0; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| unsigned C_tokenize_int(const char **token, enum TokenClass *type, |
| unsigned *line, unsigned *col, unsigned *pos) |
| { |
| int cc; |
| *type = ENDOFFILE; |
|
|
| do { |
| |
| token_buf_reset(); |
| *line = linenr; |
| *col = column; |
| *pos = char_count; |
| |
| logical_lines = 0; |
| cc = get(); |
|
|
| restart: |
| |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| if (cc == '\n' && newline_token) { |
| |
| token_buf_push(cc); |
| *type = NEWLINE; |
| break; |
| } |
|
|
| if (cc == '\r' && continuation_token) { |
| |
| token_buf_push('\\'); |
| token_buf_push('\n'); |
| *type = CONTINUATION; |
| break; |
| } |
|
|
| |
| |
| while (isspace(cc)) { |
| |
| if (whitespace_token) |
| if (cc == '\r') { |
| |
| token_buf_push('\\'); |
| token_buf_push('\n'); |
| } |
| else |
| token_buf_push(cc); |
| |
|
|
| |
|
|
| cc = get(); |
| if (cc == '\n' && newline_token || |
| cc == '\r' && continuation_token) { |
| |
| if (whitespace_token) { |
| |
| unget(cc); |
| *type = WHITESPACE; |
| token_buf_close(); |
| *token = token_buf; |
| return token_len; |
| } |
| |
| goto restart; |
| } |
| } |
| |
|
|
| if (whitespace_token && token_len) { |
| |
| unget(cc); |
| *type = WHITESPACE; |
| break; |
| } |
|
|
| if (cc == EOF) { |
| token_buf_reset(); |
| break; |
| } |
|
|
| |
| logical_lines = 1; |
|
|
| |
| *line = linenr; |
| *col = column-1; |
| *pos = char_count-1; |
|
|
| |
| |
|
|
| |
|
|
| if (cc == '#' && hash_as_comment) { |
| if (comment_token) |
| token_buf_push(cc); |
| |
| while ((cc = get()) != '\n' && cc != EOF) |
| if (comment_token) |
| token_buf_push(cc); |
| |
| |
| if (comment_token) { |
| |
| unget(cc); |
| *type = LINE_COMMENT; |
| break; |
| } |
| *line = linenr-1; |
| *col = saved_col; |
| *pos = char_count; |
| goto restart; |
| } |
|
|
| |
|
|
| if (cc == '/') { |
| cc = get(); |
| if (cc == '/') { |
| if (comment_token) { |
| token_buf_push(cc); |
| token_buf_push(cc); |
| } |
| |
| while ((cc = get()) != '\n' && cc != EOF) |
| if (comment_token) |
| token_buf_push(cc); |
| |
| |
| if (comment_token) { |
| |
| unget(cc); |
| *type = LINE_COMMENT; |
| break; |
| } |
| *line = linenr-1; |
| *col = saved_col; |
| *pos = char_count; |
| goto restart; |
| } |
|
|
| if (cc == '*') { |
| if (comment_token) { |
| token_buf_push('/'); |
| token_buf_push(cc); |
| } |
| |
| int nc = get(); |
| if (comment_token && nc != EOF) |
| token_buf_push(nc); |
| do { |
| cc = nc; |
| nc = get(); |
| if (nc == EOF) { |
| fprintf(stderr, |
| "(E): [%s:%u] Unexpected end-of-file in /* comment.\n", |
| filename, *line); |
| unexpect_eof++; |
| if (comment_token) |
| |
| *type = BLOCK_COMMENT; |
| else |
| token_buf_reset(); |
| token_buf_close(); |
| *token = token_buf; |
| return token_len; |
| } |
| if (comment_token) |
| token_buf_push(nc); |
| } while (cc != '*' || nc != '/'); |
| |
| |
| if (comment_token) { |
| *type = BLOCK_COMMENT; |
| break; |
| } |
| *line = linenr; |
| *col = column; |
| *pos = char_count; |
| cc = get(); |
| goto restart; |
| } |
| |
| unget(cc); |
| cc = '/'; |
| } |
|
|
| |
| *line = linenr; |
| *col = column-1; |
| *pos = char_count-1; |
|
|
| |
|
|
| |
| |
| if (cc == 'L' || cc == 'u' || cc == 'U') { |
| token_buf_push(cc); |
| cc = get(); |
| if (cc == '"') |
| goto string_token; |
| if (cc == '\'') |
| goto char_token; |
| |
| unget(cc); |
| cc = token_buf_pop(); |
| } |
|
|
| |
| |
| |
| |
|
|
| |
| |
| if (isalpha(cc) || cc == '_' || cc == '$' || (cc & 0x80)) { |
| token_buf_push(cc); |
| while (isalnum(cc = get()) || cc == '_' || cc == '$' || |
| cc != EOF && (cc & 0x80)) |
| token_buf_push(cc); |
| unget(cc); |
| token_buf_close(); |
| *type = is_keyword(token_buf) ? KEYWORD : IDENTIFIER; |
| break; |
| } |
|
|
| |
| |
| |
| |
|
|
| #if 0 |
| |
| int bin_num = 0B010101u; |
| int oct_num = 01234567L; |
| int hex_num = 0x123ABCLL; |
| int dec_num = 12345678; |
|
|
| float flt_num1 = 077.; |
| float flt_num2 = 077.987; |
| float flt_num3 = 77.; |
| float flt_num4 = .77; |
| #endif |
|
|
| |
| if (cc == '.') { |
| |
| int nc; |
| if (isdigit(nc = get())) { |
| unget(nc); |
| goto start_fraction; |
| } |
| unget(nc); |
| |
| } |
|
|
| if (isdigit(cc)) { |
| |
| enum { |
| BIN, OCT, DEC, HEX |
| } int_lit = cc == '0' ? OCT : DEC; |
|
|
| |
| int nc = get(); |
| if (int_lit == OCT && (nc == 'x' || nc == 'X')) { |
| int_lit = HEX; |
| token_buf_push(cc); |
| cc = nc; |
| } |
| else |
| if (int_lit == OCT && (nc == 'b' || nc == 'B')) { |
| int_lit = BIN; |
| token_buf_push(cc); |
| cc = nc; |
| } |
| else |
| unget(nc); |
|
|
| do { |
| token_buf_push(cc); |
| cc = get(); |
|
|
| |
| if (cc == '\'') { |
| |
| token_buf_push(cc); |
| int nc = get(); |
| if (isdigit(nc) || int_lit == HEX && isxdigit(nc)) |
| cc = nc; |
| else { |
| fprintf(stderr, |
| "(E): [%s:%u] C++14 only allows ' between digits.\n", |
| filename, linenr); |
| |
| } |
| } |
| } while (isdigit(cc) || int_lit == HEX && isxdigit(cc)); |
| |
|
|
| |
| if (int_lit == OCT || int_lit == DEC) { |
| int floating = 0; |
| |
| if (cc == '.') { |
| start_fraction: |
| floating = 1; |
| token_buf_push(cc); |
| |
| while (isdigit(cc = get())) |
| token_buf_push(cc); |
| |
| } |
| |
| if (cc == 'e' || cc == 'E') { |
| floating = 1; |
| token_buf_push(cc); |
| if ((cc = get()) == '-' || cc == '+') { |
| token_buf_push(cc); |
| cc = get(); |
| } |
| |
| |
| while (isdigit(cc)) { |
| token_buf_push(cc); |
| cc = get(); |
| } |
| |
| } |
| if (floating) { |
| if (cc == 'f' || cc == 'F' || cc == 'l' || cc == 'L') |
| token_buf_push(cc); |
| else |
| unget(cc); |
| *type = FLOATING; |
| break; |
| } |
| } |
|
|
| |
| if (cc == 'l' || cc == 'L') { |
| token_buf_push(cc); |
| |
| cc = get(); |
| if (cc == 'l' || cc == 'L') { |
| token_buf_push(cc); |
| |
| cc = get(); |
| } |
| |
| if (cc == 'u' || cc == 'U') |
| |
| token_buf_push(cc); |
| else |
| unget(cc); |
| } |
| else if (cc == 'u' || cc == 'U') { |
| token_buf_push(cc); |
| |
| cc = get(); |
| if (cc == 'l' || cc == 'L') { |
| token_buf_push(cc); |
| |
| cc = get(); |
| } |
| |
| if (cc == 'l' || cc == 'L') |
| |
| token_buf_push(cc); |
| else |
| unget(cc); |
| } |
| else |
| unget(cc); |
| *type = INTEGER; |
| break; |
| } |
|
|
| |
|
|
| if (cc == '"') { |
| string_token: |
| token_buf_push(cc); |
| |
| cc = get(); |
| while (cc != '"') { |
| if (cc == EOF) { |
| fprintf(stderr, |
| "(E): [%s:%u] Unexpected end-of-file in string literal.\n", |
| filename, *line); |
| unexpect_eof++; |
| |
| *type = STRING; |
| token_buf_close(); |
| *token = token_buf; |
| return token_len; |
| } |
| token_buf_push(cc); |
| int nc = get(); |
|
|
| if (cc == '\\') { |
| |
| |
| token_buf_push(nc); |
| cc = get(); |
| } |
| else |
| cc = nc; |
| } |
| |
| token_buf_push(cc); |
| *type = STRING; |
| break; |
| } |
|
|
| |
|
|
| if (cc == '\'') { |
| char_token: |
| token_buf_push(cc); |
| |
| cc = get(); |
| |
| if (cc == '\'') { |
| fprintf(stderr, |
| "(E): [%s:%u] Cannot have an empty character literal.\n", |
| filename, linenr); |
| |
| token_buf_push(cc); |
| *type = CHARACTER; |
| illegals++; |
| break; |
| } |
|
|
| |
| while (cc != '\'') { |
| if (cc == EOF) { |
| fprintf(stderr, |
| "(E): [%s:%u] Unexpected end-of-file in character literal.\n", |
| filename, linenr); |
| unexpect_eof++; |
| |
| *type = CHARACTER; |
| token_buf_close(); |
| *token = token_buf; |
| return token_len; |
| } |
| if (cc == '\n') { |
| fprintf(stderr, |
| "(E): [%s:%u] Cannot have end-of-line in character literal.\n", |
| filename, linenr); |
| illegals++; |
| |
| |
| break; |
| } |
| token_buf_push(cc); |
| int nc = get(); |
| if (cc == '\\') { |
| token_buf_push(nc); |
| cc = get(); |
| |
| |
| } |
| else { |
| cc = nc; |
| |
| if (token_len == 2) { |
| if (nc != '\'') { |
| fprintf(stderr, |
| "(E): [%s:%u] Cannot have multi-character literal.\n", |
| filename, linenr); |
| illegals++; |
| |
| |
| break; |
| } |
| } |
| } |
| } |
| if (cc == '\'') |
| token_buf_push(cc); |
| else |
| unget(cc); |
| *type = CHARACTER; |
| break; |
| } |
|
|
| |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| token_buf_push(cc); |
| token_buf_close(); |
| |
|
|
| if (strstr("{}[]();?~,@", token_buf)) { |
| |
| *type = OPERATOR; |
| break; |
| } |
|
|
| if (strstr("<:.-+*/%^&|=!>", token_buf)) { |
| |
| int c2 = get(); |
| if (c2 != EOF) { |
| token_buf_push(c2); |
| |
|
|
| |
| int c3 = get(); |
| if (c3 != EOF) { |
| token_buf_push(c3); |
| token_buf_close(); |
| |
| if (!strcmp(">>>", token_buf)) { |
| |
| |
| int c4 = get(); |
| if (c4 == '=') |
| token_buf_push(c4); |
| |
| else |
| unget(c4); |
| |
| *type = OPERATOR; |
| break; |
| } |
| |
|
|
| if (!strcmp("...", token_buf) || |
| !strcmp("<=>", token_buf) || |
| !strcmp("->*", token_buf) || |
| !strcmp("<<=", token_buf) || |
| !strcmp(">>=", token_buf)) { |
| |
| *type = OPERATOR; |
| break; |
| } |
|
|
| |
| token_buf_pop(); |
| token_buf_close(); |
| |
| } |
| else |
| token_buf_close(); |
| |
| unget(c3); |
|
|
| |
| static const char * const ops2[] = { |
| "<:", "<%", "<=", "<<", ":>", |
| "::", ".*", "->", "-=", "--", |
| "+=", "++", "*=", "/=", "%>", |
| "%=", "^=", "&=", "&&", "|=", |
| "||", "==", "!=", ">=", ">>" |
| }; |
| unsigned size = sizeof(ops2) / sizeof(ops2[0]); |
| unsigned i; |
| for (i = 0; i < size; i++) |
| if (!strcmp(ops2[i], token_buf)) |
| break; |
| if (i < size) { |
| *type = OPERATOR; |
| break; |
| } |
| |
|
|
| |
| token_buf_pop(); |
| token_buf_close(); |
| |
| } |
| |
|
|
| |
| unget(c2); |
| *type = OPERATOR; |
| break; |
| } |
| |
|
|
| |
|
|
| if (cc == '#') { |
| int nc = get(); |
| if (nc != '#') |
| unget(nc); |
| else |
| token_buf_push(nc); |
| *type = PREPROCESSOR; |
| break; |
| } |
|
|
| |
| if (!nowarn) |
| |
| fprintf(stderr, |
| "(W): [%s:%u] Illegal character `%s%c` (0x%02x) skipped.\n", |
| filename, linenr, cc<32?"CTRL-":"", cc<32?cc+64:cc, cc); |
| |
| illegals++; |
|
|
| } while (1); |
| token_buf_close(); |
| *token = token_buf; |
| return token_len; |
| } |
|
|
| unsigned C_tokenize(const char **token, const char **type, |
| unsigned *line, unsigned *col, unsigned *pos) |
| { |
| enum TokenClass typeid; |
| unsigned result = C_tokenize_int(token, &typeid, line, col, pos); |
| *type = token_class[typeid]; |
| return result; |
| } |
|
|
| |
| void RAW_escape(FILE *out, const char *token) |
| { |
| const char *p; |
| for (p = token; *p; p++) { |
| if (*p == '\n') { |
| fputs("\\n", out); |
| continue; |
| } |
| fputc(*p, out); |
| } |
| } |
|
|
| |
| void CSV_escape(FILE *out, const char *token) |
| { |
| const char *p; |
| |
| fputc('"', out); |
| for (p = token; *p; p++) { |
| if (*p == '\n') { |
| fputs("\\n", out); |
| continue; |
| } |
| if (*p == '"') |
| fputc('"', out); |
| fputc(*p, out); |
| } |
| |
| fputc('"', out); |
| } |
|
|
| |
| void JSON_escape(FILE *out, const char *token) |
| { |
| |
| |
| const char *p; |
| for (p = token; *p; p++) { |
| if (*p == '\n') { |
| fputs("\\n", out); |
| continue; |
| } |
| if (*p == '\t') { |
| fputs("\\t", out); |
| continue; |
| } |
| |
| if (*p == '\\' || *p == '"') |
| fputc('\\', out); |
| fputc(*p, out); |
| } |
| } |
|
|
| |
| void XML_escape(FILE *out, const char *token) |
| { |
| #if 1 |
| |
| const char *p; |
| for (p = token; *p; p++) { |
| if (*p == '<') |
| fputs("<", out); |
| else |
| if (*p == '>') |
| fputs(">", out); |
| else |
| if (*p == '&') |
| fputs("&", out); |
| else |
| fputc(*p, out); |
| } |
| #else |
| |
| |
| |
| const char *p; |
| const char *q = token; |
| |
| |
| while ((p = strstr(q, "]]>"))) { |
| int len = p - q; |
| fputs("<![CDATA[", out); |
| fwrite(q, 1, len, out); |
| fputs("]]]]>", out); |
| q = p+2; |
| } |
| if (q < token+strlen(token)) |
| fprintf(out, "<![CDATA[%s]]>", q); |
| #endif |
| } |
|
|