📄 token.c
字号:
tp = 0; while(tp < TOKSIZE-1 && tok_char != '"' && tok_char != EOF) { token_string[tp++] = tok_char; read_src_char(); } if(tok_char != '\"' && !ifskip_mode) error(0, "String not terminated"); token_string[tp] = '\0'; read_src_char(); token_type = TOK_STRCONST; return; }/* * integer number */ if(isdigit(tok_char)) { token_type = TOK_INTCONST; token_string[0] = tok_char; tp = 1; read_src_char(); if(token_string[0] == '0') { if(tok_char == 'x' || tok_char == 'X') { /* hex number */ token_string[tp++] = tok_char; read_src_char(); while(tp < TOKSIZE-1 && isxdigit(tok_char)) { token_string[tp++] = tok_char; read_src_char(); } token_string[tp] = '\0'; token_int_val = strtoul(&token_string[2], NULL, 16); /* should put range check here */ return; } } while(tp < TOKSIZE-2 && isxdigit(tok_char)) { token_string[tp++] = tok_char; read_src_char(); } base = 10; switch(tok_char) { case 'H': /* hex */ case 'h': base = 16; /* hex */ token_string[tp++] = tok_char; read_src_char(); break; case 'O': /* octal */ case 'o': base = 8; /* octal */ token_string[tp++] = tok_char; read_src_char(); break; default: if(token_string[0] == '0' && (token_string[1] == 'b' || token_string[1] == 'B')) { token_string[tp] = '\0'; token_int_val = strtoul(&token_string[2], &cp, 2); if(cp != &token_string[tp] && !ifskip_mode) error(0, "Invalid digit in a number"); /* should put range check here */ return; } else if(token_string[tp-1] == 'B' || token_string[tp-1] == 'b') { base = 2; } else { if(token_string[tp-1] != 'D' && token_string[tp-1] != 'd') token_string[tp++] = '\0'; } break; } token_string[tp] = '\0'; token_int_val = strtoul(token_string, &cp, base); if(cp != &token_string[tp-1] && !ifskip_mode) error(0, "Invalid digit in a number"); /* should put range check here */ return; }/* * Handle B'10010100' binary etc. */ if((tok_char == 'b' || tok_char == 'B' || tok_char == 'd' || tok_char == 'D' || tok_char == 'h' || tok_char == 'H' || tok_char == 'o' || tok_char == 'O') && line_buf_ptr != NULL && *line_buf_ptr == '\'') { token_string[0] = tok_char; read_src_char(); token_string[1] = tok_char; read_src_char(); tp = 2; while(tp < TOKSIZE-1 && isxdigit(tok_char)) { token_string[tp++] = tok_char; read_src_char(); } if(tok_char != '\'') goto invalid_token; token_string[tp++] = tok_char; read_src_char(); token_string[tp] = '\0'; switch(token_string[0]) { case 'b': case 'B': base = 2; break; case 'o': case 'O': base = 8; break; case 'h': case 'H': base = 16; break; case 'd': case 'D': default: base = 10; break; } token_int_val = strtoul(&token_string[2], &cp, base); if(cp != &token_string[tp-1] && !ifskip_mode) error(0, "Invalid digit in a number"); /* should put range check here */ token_type = TOK_INTCONST; return; }/* * keyword or identifier */ if(tok_char == '_' || tok_char == '.' || isalpha(tok_char)) { line_buf_off = (line_buf_ptr - &line_buffer[1]); token_string[0] = tok_char; tp = 1; read_src_char(); if(token_string[0] == '.' && tok_char != '_' && !isalnum(tok_char)) { token_string[1] = '\0'; token_type = TOK_PERIOD; return; } while(tp < TOKSIZE-1 && (tok_char == '_' || tok_char == '.' || isalnum(tok_char))) { token_string[tp++] = tok_char; read_src_char(); } token_string[tp] = '\0'; token_type = FIRST_KW; cp = Keyword_Table; while(*cp) { if(strcasecmp(token_string, cp) == 0) return; while(*cp++) ; token_type++; } token_type = TOK_IDENTIFIER; return; }/* * non-numeric & non-alpha tokens */ switch(tok_char) { case '\n': case '\0': token_type = TOK_NEWLINE; strcpy(token_string, "\\n"); skip_eol(); return; case '<': token_string[0] = tok_char; read_src_char(); if(tok_char == '<') { token_string[1] = tok_char; token_string[2] = '\0'; token_type = TOK_LSHIFT; read_src_char(); return; } if(tok_char == '=') { token_string[1] = tok_char; token_string[2] = '\0'; token_type = TOK_LESS_EQ; read_src_char(); return; } if(tok_char == '>') { token_string[1] = tok_char; token_string[2] = '\0'; token_type = TOK_NOT_EQ; read_src_char(); return; } token_type = TOK_LESS; token_string[1] = '\0'; return; case '>': token_string[0] = tok_char; read_src_char(); if(tok_char == '>') { token_string[1] = tok_char; token_string[2] = '\0'; token_type = TOK_RSHIFT; read_src_char(); return; } if(tok_char == '=') { token_string[1] = tok_char; token_string[2] = '\0'; token_type = TOK_GT_EQ; read_src_char(); return; } token_string[1] = '\0'; token_type = TOK_GREATER; return; case '!': token_string[0] = tok_char; read_src_char(); if(tok_char != '=') goto invalid_token; token_string[1] = tok_char; token_string[2] = '\0'; read_src_char(); token_type = TOK_NOT_EQ; return; case '=': token_string[0] = tok_char; read_src_char(); if(tok_char == '=') { token_string[1] = tok_char; read_src_char(); token_string[2] = '\0'; token_type = TOK_EQ; return; } if(tok_char == '<') { token_string[1] = tok_char; read_src_char(); token_string[2] = '\0'; token_type = TOK_LESS_EQ; return; } if(tok_char == '>') { token_string[1] = tok_char; read_src_char(); token_string[2] = '\0'; token_type = TOK_GT_EQ; return; } if(tok_char == '_' || tok_char == '.' || isalpha(tok_char)) { /* local symbol */ line_buf_off = (line_buf_ptr - &line_buffer[2]); token_string[0] = tok_char; tp = 1; read_src_char(); while(tp < TOKSIZE-1 && (tok_char == '_' || tok_char == '.' || isalnum(tok_char))) { token_string[tp++] = tok_char; read_src_char(); } token_string[tp] = '\0'; token_type = TOK_LOCAL_ID; return; } token_string[1] = '\0'; token_type = TOK_EQUAL; return; case '$': read_src_char(); if(!isxdigit(tok_char)) { token_string[0] = '$'; token_string[1] = '\0'; token_type = TOK_DOLLAR; return; } tp = 0; do { token_string[tp++] = tok_char; read_src_char(); } while(tp < TOKSIZE-1 && isxdigit(tok_char)); token_string[tp] = '\0'; token_int_val = strtoul(&token_string[1], NULL, 16); token_type = TOK_INTCONST; /* should put range check here */ return; case '\\': token_type = TOK_BACKSLASH; break; case ',': token_type = TOK_COMMA; break; case '(': token_type = TOK_LEFTPAR; break; case ')': token_type = TOK_RIGHTPAR; break; case '+': token_type = TOK_PLUS; break; case '-': token_type = TOK_MINUS; break; case '&': token_type = TOK_BITAND; break; case '|': token_type = TOK_BITOR; break; case '^': token_type = TOK_BITXOR; break; case '~': token_type = TOK_BITNOT; break; case '*': token_type = TOK_ASTERISK; break; case '/': token_type = TOK_SLASH; break; case '%': token_type = TOK_PERCENT; break; case ':': token_type = TOK_COLON; break; case '[': token_type = TOK_LEFTBRAK; break; case ']': token_type = TOK_RIGHTBRAK; break; default: goto invalid_token; } token_string[0] = tok_char; token_string[1] = '\0'; read_src_char(); return;invalid_token: if(!ifskip_mode) error(0, "Invalid token"); token_string[0] = '\0'; token_type = TOK_INVALID;}/* skip to the next line */voidskip_eol(void){ line_buf_ptr = NULL; tok_char = ' ';}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -