|
|
1.1 root 1: /*
2: * Simple ad-hoc lexer.
3: */
4:
5: /*
6: *-IMPORTS:
7: * <sys/compat.h>
8: * USE_PROTO
9: * ARGS ()
10: * <string.h>
11: * strchr ()
12: */
13:
14: #include <sys/compat.h>
15: #include <string.h>
16:
17: #include "ehand.h"
18:
19: #include "lex.h"
20:
21:
22: CONST char LEX_WILD [1]; /* special wildcard value */
23:
24: lex_t WHITESPACE [] = {
25: { NULL, STD_FLUSH, STD_SEP, LEX_WILD, NULL }
26: };
27:
28:
29: /*
30: * Here the code that uses the "lex_t" structure to classify a character has
31: * been factored out.
32: */
33:
34: #if USE_PROTO
35: int (classify) (lex_t * lexp, int ch, int flush)
36: #else
37: int
38: classify ARGS ((lexp, ch, flush))
39: lex_t * lexp;
40: int ch;
41: int flush;
42: #endif
43: {
44: int valid = 0;
45: int invalid = 0;
46: CONST lex_t * scan = lexp;
47:
48: if (ch < 0)
49: throw_error ("Bad character passed to classify ()");
50:
51: do {
52: /*
53: * Classify the new character.
54: */
55:
56: if (flush && scan->l_flush != NULL &&
57: strchr (scan->l_flush, ch) != 0)
58: return CLASS_FLUSH;
59:
60: if (scan->l_sep != NULL && strchr (scan->l_sep, ch) != 0)
61: return CLASS_SEP;
62:
63: if (scan->l_valid == LEX_WILD ||
64: (scan->l_valid != NULL &&
65: strchr (scan->l_valid, ch) != NULL))
66: valid = 1;
67:
68: if (scan->l_error == LEX_WILD)
69: invalid = 1;
70: else if (! valid && scan->l_error != NULL &&
71: strchr (scan->l_error, ch) != NULL) {
72:
73: invalid = 1;
74: break;
75: }
76: } while ((scan = scan->l_prev) != NULL);
77:
78:
79: if (! valid) {
80:
81: if (invalid)
82: throw_error ("invalid character value 0x%x\n", ch);
83:
84: throw_error ("unexpected character value 0x%x\n", ch);
85: }
86:
87: return CLASS_VALID;
88: }
89:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.