3 * A mini C-like language scanner.
8 #define IDENT_BUFLEN 256
13 # Function to buffer a character.
15 if ( identLen < IDENT_BUFLEN ) {
16 identBuf[identLen] = fc;
21 # Function to clear the buffer.
26 # Functions to dump tokens as they are matched.
28 identBuf[identLen] = 0;
29 printf("ident(%i): %s\n", curLine, identBuf);
32 identBuf[identLen] = 0;
33 printf("literal(%i): %s\n", curLine, identBuf);
36 identBuf[identLen] = 0;
37 printf("float(%i): %s\n", curLine, identBuf);
40 identBuf[identLen] = 0;
41 printf("int(%i): %s\n", curLine, identBuf);
44 identBuf[identLen] = 0;
45 printf("hex(%i): 0x%s\n", curLine, identBuf);
48 identBuf[identLen] = 0;
49 printf("symbol(%i): %s\n", curLine, identBuf);
52 # Alpha numberic characters or underscore.
55 # Alpha charactres or underscore.
58 # Symbols. Upon entering clear the buffer. On all transitions
59 # buffer a character. Upon leaving dump the symbol.
60 symbol = ( punct - [_'"] ) >clearBuf $bufChar %symbol;
62 # Identifier. Upon entering clear the buffer. On all transitions
63 # buffer a character. Upon leaving, dump the identifier.
64 ident = (alphau . alnumu*) >clearBuf $bufChar %ident;
66 # Match single characters inside literal strings. Or match
67 # an escape sequence. Buffers the charater matched.
69 ( extend - ['\\] ) @bufChar |
70 ( '\\' . extend @bufChar );
72 ( extend - ["\\] ) @bufChar |
73 ( '\\' . extend @bufChar );
75 # Single quote and double quota literals. At the start clear
76 # the buffer. Upon leaving dump the literal.
77 sliteral = ('\'' @clearBuf . sliteralChar* . '\'' ) %literal;
78 dliteral = ('"' @clearBuf . dliteralChar* . '"' ) %literal;
79 literal = sliteral | dliteral;
81 # Whitespace is standard ws, newlines and control codes.
82 whitespace = any - 0x21..0x7e;
84 # Describe both c style comments and c++ style comments. The
85 # priority bump on tne terminator of the comments brings us
86 # out of the extend* which matches everything.
87 ccComment = '//' . extend* $0 . '\n' @1;
88 cComment = '/*' . extend* $0 . '*/' @1;
90 # Match an integer. We don't bother clearing the buf or filling it.
91 # The float machine overlaps with int and it will do it.
94 # Match a float. Upon entering the machine clear the buf, buffer
95 # characters on every trans and dump the float upon leaving.
96 float = ( digit+ . '.' . digit+ ) >clearBuf $bufChar %float;
98 # Match a hex. Upon entering the hex part, clear the buf, buffer characters
99 # on every trans and dump the hex on leaving transitions.
100 hex = '0x' . xdigit+ >clearBuf $bufChar %hex;
102 # Or together all the lanuage elements.
113 # Star the language elements. It is critical in this type of application
114 # that we decrease the priority of out transitions before doing so. This
115 # is so that when we see 'aa' we stay in the fin machine to match an ident
116 # of length two and not wrap around to the front to match two idents of
118 clang_main = ( fin $1 %0 )*;
120 # This machine matches everything, taking note of newlines.
121 newline = ( any | '\n' @{ curLine += 1; } )*;
123 # The final fsm is the lexer intersected with the newline machine which
124 # will count lines for us. Since the newline machine accepts everything,
125 # the strings accepted is goverened by the clang_main machine, onto which
126 # the newline machine overlays line counting.
127 main := clang_main & newline;
132 %% write data noerror;
143 "#define _AAPL_RESIZE_H\n"
145 "#include <assert.h>\n"
147 "#ifdef AAPL_NAMESPACE\n"
150 "#define LIN_DEFAULT_STEP 256\n"
151 "#define EXPN_UP( existing, needed ) \\\n"
152 " need > eng ? (ned<<1) : eing\n"
157 "#ifdef AAPL_NAMESPACE\n"
158 "#endif /* _AAPL_RESIZE_H */\n";
160 void test( char *buf )
162 int len = strlen( buf );
163 char *p = buf, *pe = buf + len;
165 char identBuf[IDENT_BUFLEN+1];
176 if ( cs >= clang_first_final )
185 "999 0xaAFF99 99.99 /*\n"
192 "\"0x00aba foobardd.ddsf 0x0.9\n" );
194 "wordwithnum00asdf\n"
195 "000wordfollowsnum,makes new symbol\n"
197 "finishing early /* unfinished ...\n" );
202 #ifdef _____OUTPUT_____
221 ident(1): wordwithnum00asdf
223 ident(2): wordfollowsnum
233 ident(8): _AAPL_RESIZE_H
243 ident(12): AAPL_NAMESPACE
251 ident(15): LIN_DEFAULT_STEP
279 ident(22): AAPL_NAMESPACE