📄 lexi.c
字号:
/* Copyright (c) 1992, Free Software Foundation, Inc. All rights reserved. Copyright (c) 1985 Sun Microsystems, Inc. Copyright (c) 1980 The Regents of the University of California. Copyright (c) 1976 Board of Trustees of the University of Illinois. All rights reserved. Redistribution and use in source and binary forms are permitted provided that the above copyright notice and this paragraph are duplicated in all such forms and that any documentation, advertising materials, and other materials related to such distribution and use acknowledge that the software was developed by the University of California, Berkeley, the University of Illinois, Urbana, and Sun Microsystems, Inc. The name of either University or Sun Microsystems may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. *//* Here we have the token scanner for indent. It scans off one token and puts it in the global variable "token". It returns a code, indicating the type of token scanned. */#include "sys.h"#include "indent.h"#include <ctype.h>/* Stuff that needs to be shared with the rest of indent. Documented in indent.h. */char *token;char *token_end;#define alphanum 1#define opchar 3struct templ{ char *rwd; enum rwcodes rwcode;};/* Pointer to a vector of keywords specified by the user. */static struct templ *user_specials = 0;/* Allocated size of user_specials. */static unsigned int user_specials_max;/* Index in user_specials of the first unused entry. */static unsigned int user_specials_idx;char chartype[128] ={ /* this is used to facilitate the decision of what type (alphanumeric, operator) each character is */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 1, 3, 3, 0, 0, 0, 3, 3, 0, 3, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 3, 3, 3, 3, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 3, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 3, 0, 3, 0};/* C code produced by gperf version 2.0 (K&R C version) Command-line: gperf -c -p -t -T -g -j1 -o -K rwd -N is_reserved indent.gperf */#define MIN_WORD_LENGTH 2#define MAX_WORD_LENGTH 8#define MIN_HASH_VALUE 4#define MAX_HASH_VALUE 42/* 31 keywords. 39 is the maximum key range */INLINEstatic inthash (str, len) register char *str; register unsigned int len;{ static unsigned char hash_table[] = { 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 6, 9, 10, 0, 16, 5, 4, 24, 42, 0, 20, 4, 20, 0, 42, 42, 6, 0, 0, 10, 10, 2, 42, 42, 42, 42, 42, 42, 42, 42, }; return len + hash_table[str[len - 1]] + hash_table[str[0]];}INLINEstruct templ*is_reserved (str, len) register char *str; register unsigned int len;{ static struct templ wordlist[] = { {"",}, {"",}, {"",}, {"",}, {"else", rw_sp_nparen,}, {"short", rw_decl,}, {"struct", rw_struct_like,}, {"while", rw_sp_paren,}, {"enum", rw_struct_like,}, {"goto", rw_break,}, {"switch", rw_switch,}, {"break", rw_break,}, {"do", rw_sp_nparen,}, {"case", rw_case,}, {"const", rw_decl,}, {"static", rw_decl,}, {"double", rw_decl,}, {"default", rw_case,}, {"volatile", rw_decl,}, {"char", rw_decl,}, {"register", rw_decl,}, {"float", rw_decl,}, {"sizeof", rw_sizeof,}, {"typedef", rw_decl,}, {"void", rw_decl,}, {"for", rw_sp_paren,}, {"extern", rw_decl,}, {"int", rw_decl,}, {"unsigned", rw_decl,}, {"long", rw_decl,}, {"",}, {"global", rw_decl,}, {"return", rw_return,}, {"",}, {"",}, {"union", rw_struct_like,}, {"va_dcl", rw_decl,}, {"",}, {"",}, {"",}, {"",}, {"",}, {"if", rw_sp_paren,}, }; if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH) { register int key = hash (str, len); if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE) { register char *s = wordlist[key].rwd; if (*s == *str && !strncmp (str + 1, s + 1, len - 1)) return &wordlist[key]; } } return 0;}extern int squest;enum codeslexi (){ int unary_delim; /* this is set to 1 if the current token forces a following operator to be unary */ static enum codes last_code; /* the last token type returned */ static int l_struct; /* set to 1 if the last token was 'struct' */ enum codes code; /* internal code to be returned */ char qchar; /* the delimiter character for a string */ unary_delim = false; /* tell world that this token started in column 1 iff the last thing scanned was nl */ parser_state_tos->col_1 = parser_state_tos->last_nl; parser_state_tos->last_nl = false; while (*buf_ptr == ' ' || *buf_ptr == TAB) { /* get rid of blanks */ parser_state_tos->col_1 = false; /* leading blanks imply token is not in column 1 */ if (++buf_ptr >= buf_end) fill_buffer (); } token = buf_ptr; /* Scan an alphanumeric token */ if (chartype[*buf_ptr] == alphanum || (buf_ptr[0] == '.' && isdigit (buf_ptr[1]))) { /* we have a character or number */ register struct templ *p; if (isdigit (*buf_ptr) || (buf_ptr[0] == '.' && isdigit (buf_ptr[1]))) { int seendot = 0, seenexp = 0; if (*buf_ptr == '0' && (buf_ptr[1] == 'x' || buf_ptr[1] == 'X')) { buf_ptr += 2; while (isxdigit (*buf_ptr)) buf_ptr++; } else while (1) { if (*buf_ptr == '.') if (seendot) break; else seendot++; buf_ptr++; if (!isdigit (*buf_ptr) && *buf_ptr != '.') if ((*buf_ptr != 'E' && *buf_ptr != 'e') || seenexp) break; else { seenexp++; seendot++; buf_ptr++; if (*buf_ptr == '+' || *buf_ptr == '-') buf_ptr++; } } /* Accept unsigned, unsigned long, and float constants (U, UL, and F suffixes). I'm not sure if LU is ansii. 11Jun93 - rdh@key.amdahl.com tells me LL and ULL are also acceptable. */ if (*buf_ptr == 'F' || *buf_ptr == 'f') buf_ptr++; else { if (*buf_ptr == 'U' || *buf_ptr == 'u') buf_ptr++; if (*buf_ptr == 'L' || *buf_ptr == 'l') buf_ptr++; if (*buf_ptr == 'L' || *buf_ptr == 'l') buf_ptr++; } } else while (chartype[*buf_ptr] == alphanum) { /* copy it over */ buf_ptr++; if (buf_ptr >= buf_end) fill_buffer (); } token_end = buf_ptr; while (*buf_ptr == ' ' || *buf_ptr == TAB) { if (++buf_ptr >= buf_end) fill_buffer (); } parser_state_tos->its_a_keyword = false; parser_state_tos->sizeof_keyword = false; /* if last token was 'struct', then this token should be treated as a declaration */ if (l_struct) { l_struct = false; last_code = ident; parser_state_tos->last_u_d = true; return (decl); } /* Operator after indentifier is binary */ parser_state_tos->last_u_d = false; last_code = ident; /* Check whether the token is a reserved word. Use perfect hashing... */ p = is_reserved (token, token_end - token); if (!p && user_specials != 0) { for (p = &user_specials[0]; p < &user_specials[0] + user_specials_idx; p++) { char *q = token; char *r = p->rwd; /* This string compare is a little nonstandard because token ends at the character before token_end and p->rwd is null-terminated. */ while (1) { /* If we have come to the end of both the keyword in user_specials and the keyword in token they are equal. */ if (q >= token_end && !*r) goto found_keyword; /* If we have come to the end of just one, they are not equal. */ if (q >= token_end || !*r) break; /* If the characters in corresponding characters are not equal, the strings are not equal. */ if (*q++ != *r++) break; } } /* Didn't find anything in user_specials. */ p = 0; } if (p) { /* we have a keyword */ found_keyword: parser_state_tos->its_a_keyword = true; parser_state_tos->last_u_d = true; parser_state_tos->last_rw = p->rwcode; switch (p->rwcode) { case rw_switch: /* it is a switch */ return (swstmt); case rw_case: /* a case or default */ return (casestmt); case rw_struct_like: /* a "struct" */ if (parser_state_tos->p_l_follow && !(parser_state_tos->noncast_mask & 1 << parser_state_tos->p_l_follow)) /* inside parens: cast */ { parser_state_tos->cast_mask |= 1 << parser_state_tos->p_l_follow; break; } l_struct = true; /* Next time around, we will want to know that we have had a 'struct' */ case rw_decl: /* one of the declaration keywords */ if (parser_state_tos->p_l_follow && !(parser_state_tos->noncast_mask & 1 << parser_state_tos->p_l_follow)) /* inside parens: cast */ { parser_state_tos->cast_mask |= 1 << parser_state_tos->p_l_follow; break; } last_code = decl; return (decl); case rw_sp_paren: /* if, while, for */ return (sp_paren); case rw_sp_nparen: /* do, else */ return (sp_nparen); case rw_sizeof: parser_state_tos->sizeof_keyword = true; return (ident); case rw_return: case rw_break: default: /* all others are treated like any other identifier */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -