📄 shell_slex_c
字号:
}/********************************************************************************* getString - interpret scanned string as quoted string** RETURNS: STRING*/LOCAL int getString ( char *string, int nChars, VALUE *pValue ) { pValue->side = RHS; pValue->type = T_INT; string [nChars - 1] = EOS; pValue->value.rv = (int)addTempString (&string[1]); return (STRING); }/********************************************************************************* getChar - interpret scanned string as quoted character** RETURNS: CHAR*/LOCAL int getChar ( char *string, int nChars, VALUE *pValue ) { char ch; int n = strToChar (&string [1], &ch); if (nChars != (n + 2)) { lexError (string, "invalid char"); return (LEX_ERROR); } pValue->side = RHS; pValue->type = T_BYTE; pValue->value.byte = ch; return (CHAR); }/********************************************************************************* getId - interpret scanned string as identifier or keyword** RETURNS: TYPECAST, {T,D,U}_SYMBOL*/LOCAL int getId ( char *string, FAST VALUE *pValue ) { char tempString [MAX_SHELL_LINE + 1]; SYM_TYPE type; char *value; int t = typeCast (string); if (t != ERROR) { pValue->type = (TYPE)t; return (TYPECAST); } tempString[0] = '_'; strncpy (&tempString[1], string, MAX_SHELL_LINE); tempString [MAX_SHELL_LINE] = EOS; if ((symFindByName (sysSymTbl, &tempString[1], &value, &type) == OK) || (symFindByName (sysSymTbl, &tempString[0], &value, &type) == OK) || (type = N_TEXT, value = (char *) taskNameToId (&tempString[1]), (int) value != ERROR)) { pValue->value.lv = (int *) value; pValue->type = T_INT; pValue->side = LHS; if ((type & 0xe) == N_TEXT) /* only need to check three bits of type*/ return (T_SYMBOL); else return (D_SYMBOL); } if (sysAdaEnable) { /* check for ada names of the form "_A_<symbolname>.<anything>" * a match occurs only if the result is unambiguous. */ switch (numAdaIdMatches (string, &type, (int *) &pValue->value.lv)) { case 0: break; case 1: pValue->type = T_INT; pValue->side = LHS; if (type & N_TEXT) return (T_SYMBOL); else return (D_SYMBOL); default: printf ("%s: ambiguous Ada identifier - Possibilities include:\n", string); symEach (sysSymTbl, (FUNCPTR)printAdaIdMatch, (int)string); } } if (sysCplusEnable) { /* check for mangled C++ names; return unique match or nothing */ if (cplusMatchMangled(sysSymTbl, string, &type, (int *) &pValue->value.lv)) { pValue->type = T_INT; pValue->side = LHS; if ((type & N_TYPE) == N_TEXT) return T_SYMBOL; else return D_SYMBOL; } } /* identifier not found */ pValue->side = RHS; pValue->type = T_UNKNOWN; pValue->value.rv = (int)addTempString (string); return (U_SYMBOL); }/* HIDDEN */typedef struct { char *string; int numOccurrences; SYM_TYPE type; int value; } ADA_MATCH;/* END HIDDEN *//********************************************************************************* numAdaIdMatches - attempt to match id with bizarre ada names** RETURNS: number of potential matches*/LOCAL int numAdaIdMatches ( char *string, SYM_TYPE *pType, int *pValue ) { ADA_MATCH adaMatch; adaMatch.string = string; adaMatch.numOccurrences = 0; symEach (sysSymTbl, (FUNCPTR)countAdaIdMatch, (int)&adaMatch); *pType = adaMatch.type; *pValue = adaMatch.value; return (adaMatch.numOccurrences); }/********************************************************************************* countAdaIdMatch - count number of symbols that match id as Ada symbol** count ada names of the form "_A_<id>.<anything>"* called via symEach.** RETURNS: TRUE*/LOCAL BOOL countAdaIdMatch ( char *symbol, /* symbol from symbol table */ int val, SYM_TYPE type, int arg ) { FAST ADA_MATCH *pAdaMatch = (ADA_MATCH *) arg; if (adaIdMatch (symbol, pAdaMatch->string)) { pAdaMatch->numOccurrences++; pAdaMatch->type = type; pAdaMatch->value = val; } return (TRUE); }/********************************************************************************* printAdaIdMatch - print a symbol if it is a potential ada name match** called via symEach** RETURNS: TRUE** ARGSUSED2*/LOCAL BOOL printAdaIdMatch ( char *symbol, /* symbol from symbol table */ int val, SYM_TYPE type, int arg /* id being sought */ ) { if (adaIdMatch (symbol, (char *) arg)) printf ("%s\n", symbol); return (TRUE); }/********************************************************************************* adaIdMatch - determine if a symbol is an Ada match for an id.** RETURNS: TRUE if symbol is of form "_A_<id>" or "_A_<id>.<anything>".*/LOCAL BOOL adaIdMatch ( FAST char *symbol, FAST char *id ) { if ((symbol [0] != '_') || (symbol [1] != 'A') || (symbol [2] != '_')) return (FALSE); symbol += 3; /* skip "_A_" */ while ((*id != EOS) && (*symbol != EOS) && (*id == *symbol)) { ++id; ++symbol; } return ((*id == EOS) && ((*symbol == EOS) || (*symbol == '.'))); }/********************************************************************************* typeCast - determine if string is a keyword type cast** RETURNS: T_{BYTE,WORD,INT,FLOAT,DOUBLE}, or ERROR*/LOCAL int typeCast ( FAST char *string ) { static char *typen [] =#ifndef _WRS_NO_TGT_SHELL_FP {"char", "short", "int", "long", "float", "double"};#else /* _WRS_NO_TGT_SHELL_FP */ {"char", "short", "int", "long"};#endif /* _WRS_NO_TGT_SHELL_FP */ static TYPE typet [] =#ifndef _WRS_NO_TGT_SHELL_FP {T_BYTE, T_WORD, T_INT, T_INT, T_FLOAT, T_DOUBLE};#else /* _WRS_NO_TGT_SHELL_FP */ {T_BYTE, T_WORD, T_INT, T_INT};#endif /* _WRS_NO_TGT_SHELL_FP */ FAST int ix; for (ix = 0; ix < NELEMENTS (typet); ix++) { if (strcmp (string, typen [ix]) == 0) return ((int)typet [ix]); } return (ERROR); }/********************************************************************************* strToChar - get a possibly escaped character from a string** RETURNS: number of characters parsed and character in <pChar>.*/LOCAL int strToChar ( FAST char *string, char *pChar ) { FAST int nchars = 1; int num; FAST char ch; if (*string != '\\') { *pChar = *string; return (nchars); } string++; if ((*string >= '0') && (*string <= '7')) { sscanf (string, "%o", &num); ch = num % 0400; while ((*string >= '0') && (*string <= '7')) { ++string; ++nchars; } } else { nchars++; switch (*string) { case 'n': ch = '\n'; break; case 't': ch = '\t'; break; case 'b': ch = '\b'; break; case 'r': ch = '\r'; break; case 'f': ch = '\f'; break; case '\\': ch = '\\'; break; case '\'': ch = '\''; break; case '"': ch = '"'; break; case 'a': ch = (char)0x07; break; case 'v': ch = (char)0x0b; break; default: ch = *string; break; } } *pChar = ch; return (nchars); }/* lexeme scan routines */#define EMPTY -2LOCAL int retractChar;LOCAL int lastChar;/********************************************************************************* lexInit - initialize lex scan routines** RETURNS: N/A*/LOCAL void lexInit (void) { retractChar = EMPTY; }/********************************************************************************* lexScan - scan input for next lexeme** RETURNS: next lexeme.*/LOCAL int lexScan (void) { FAST int ch; FAST int state; int nChars; int code; BOOL scanContinue; char string [MAX_SHELL_LINE + 1]; do { /* get first character; use any retracted character first */ if (retractChar != EMPTY) { ch = retractChar; retractChar = EMPTY; } else ch = *(nextChar++); /* consume characters until final state reached */ state = 0; for (nChars = 0; nChars < MAX_SHELL_LINE; nChars++) { /* consume character and make state transition */ string [nChars] = ch; state = lexStateTable [state * lexNclasses + lexClass [ch + 1]]; /* if final state reached, quit; otherwise get next character */ if (state < 0) { nChars++; break; } ch = *(nextChar++); } /* final state reached */ state = -state; string [nChars] = EOS; lastChar = ch; code = lexActions (state, string, nChars, &scanContinue); } while (scanContinue); return (code); }/********************************************************************************* lexRetract - retract last character consumed** RETURNS: N/A*/LOCAL void lexRetract (void) { retractChar = lastChar; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -