2 /**********************************************************************
8 **********************************************************************/
10 /* The includes/config.h one */
15 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
17 /* An ANSI string.h and pre-ANSI memory.h might conflict. */
18 #if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
20 #endif /* not STDC_HEADERS and HAVE_MEMORY_H */
22 #define rindex strrchr
23 #define bcopy(s, d, n) memcpy ((d), (s), (n))
24 #define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
25 #define bzero(s, n) memset ((s), 0, (n))
26 #else /* not STDC_HEADERS and not HAVE_STRING_H */
28 /* memory.h and strings.h conflict on some systems. */
29 #endif /* not STDC_HEADERS and not HAVE_STRING_H */
32 #include "hsparser.tab.h"
33 #include "constants.h"
36 /* Our substitute for <ctype.h> */
45 static unsigned char CharTable[NCHARS] = {
46 /* nul */ 0, 0, 0, 0, 0, 0, 0, 0,
47 /* bs */ 0, _S, _S, _S, _S, 0, 0, 0,
48 /* dle */ 0, 0, 0, 0, 0, 0, 0, 0,
49 /* can */ 0, 0, 0, 0, 0, 0, 0, 0,
50 /* sp */ _S, 0, 0, 0, 0, 0, 0, 0,
51 /* '(' */ _C, 0, 0, 0, 0, 0, 0, 0,
52 /* '0' */ _D|_H|_O,_D|_H|_O,_D|_H|_O,_D|_H|_O,_D|_H|_O,_D|_H|_O,_D|_H|_O,_D|_H|_O,
53 /* '8' */ _D|_H, _D|_H, _C, 0, 0, 0, 0, 0,
54 /* '@' */ 0, _H|_C, _H|_C, _H|_C, _H|_C, _H|_C, _H|_C, _C,
55 /* 'H' */ _C, _C, _C, _C, _C, _C, _C, _C,
56 /* 'P' */ _C, _C, _C, _C, _C, _C, _C, _C,
57 /* 'X' */ _C, _C, _C, _C, 0, 0, 0, 0,
58 /* '`' */ 0, _H, _H, _H, _H, _H, _H, 0,
59 /* 'h' */ 0, 0, 0, 0, 0, 0, 0, 0,
60 /* 'p' */ 0, 0, 0, 0, 0, 0, 0, 0,
61 /* 'x' */ 0, 0, 0, 0, 0, 0, 0, 0,
63 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
64 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
65 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
66 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
67 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
68 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
69 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
70 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
71 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
72 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
73 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
74 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
75 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
76 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
77 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
78 /* */ 0, 0, 0, 0, 0, 0, 0, 0,
84 return(CharTable[*s]&(_C));
87 /**********************************************************************
93 **********************************************************************/
95 char *input_filename = NULL; /* Always points to a dynamically allocated string */
98 * For my own sanity, things that are not part of the flex skeleton
99 * have been renamed as hsXXXXX rather than yyXXXXX. --JSM
102 static int hslineno = 0; /* Line number at end of token */
103 int hsplineno = 0; /* Line number at end of previous token */
105 static int hscolno = 0; /* Column number at end of token */
106 int hspcolno = 0; /* Column number at end of previous token */
107 static int hsmlcolno = 0; /* Column number for multiple-rule lexemes */
109 int modulelineno = -1; /* The line number where the module starts */
110 int startlineno = 0; /* The line number where something starts */
111 int endlineno = 0; /* The line number where something ends */
113 static BOOLEAN noGap = TRUE; /* For checking string gaps */
114 static BOOLEAN forgetindent = FALSE; /* Don't bother applying indentation rules */
116 static int nested_comments; /* For counting comment nesting depth */
118 /* OLD: Hacky definition of yywrap: see flex doc.
120 If we don't do this, then we'll have to get the default
121 yywrap from the flex library, which is often something
122 we are not good at locating. This avoids that difficulty.
123 (Besides which, this is the way old flexes (pre 2.4.x) did it.)
128 /* Essential forward declarations */
130 static void hsnewid PROTO((char *, int));
131 static void layout_input PROTO((char *, int));
132 static void cleartext (NO_ARGS);
133 static void addtext PROTO((char *, unsigned));
134 static void addchar PROTO((char));
135 static char *fetchtext PROTO((unsigned *));
136 static void new_filename PROTO((char *));
137 static int Return PROTO((int));
138 static void hsentercontext PROTO((int));
140 /* Special file handling for IMPORTS */
141 /* Note: imports only ever go *one deep* (hence no need for a stack) WDP 94/09 */
143 static YY_BUFFER_STATE hsbuf_save = NULL; /* Saved input buffer */
144 static char *filename_save; /* File Name */
145 static int hslineno_save = 0, /* Line Number */
146 hsplineno_save = 0, /* Line Number of Prev. token */
147 hscolno_save = 0, /* Indentation */
148 hspcolno_save = 0; /* Left Indentation */
149 static short icontexts_save = 0; /* Indent Context Level */
151 static BOOLEAN etags_save; /* saved: whether doing etags stuff or not */
152 extern BOOLEAN etags; /* that which is saved */
154 extern BOOLEAN nonstandardFlag; /* Glasgow extensions allowed */
156 static int hssttok = -1; /* Stacked Token: -1 -- no token; -ve -- ";"
157 * inserted before token +ve -- "}" inserted before
160 short icontexts = 0; /* Which context we're in */
163 Table of indentations: right bit indicates whether to use
164 indentation rules (1 = use rules; 0 = ignore)
167 push one of these "contexts" at every "case" or "where"; the right bit says
168 whether user supplied braces, etc., or not. pop appropriately (hsendindent).
170 ALSO, a push/pop when enter/exit a new file (e.g., on importing). A -1 is
171 pushed (the "column" for "module", "interface" and EOF). The -1 from the initial
172 push is shown just below.
177 static short indenttab[MAX_CONTEXTS] = {-1};
179 #define INDENTPT (indenttab[icontexts]>>1)
180 #define INDENTON (indenttab[icontexts]&1)
182 #define RETURN(tok) return(Return(tok))
185 #define YY_DECL int yylex1()
187 /* We should not peek at yy_act, but flex calls us even for the internal action
188 triggered on 'end-of-buffer' (This is not true of flex 2.4.4 and up, but
189 to support older versions of flex, we'll continue to peek for now.
191 #define YY_USER_ACTION \
192 if (yy_act != YY_END_OF_BUFFER) layout_input(yytext, yyleng);
196 #define YY_BREAK if (etags) fprintf(stderr,"%d %d / %d %d / %d\n",hsplineno,hspcolno,hslineno,hscolno,startlineno); break;
199 /* Each time we enter a new start state, we push it onto the state stack.
201 #define PUSH_STATE(n) yy_push_state(n)
202 #define POP_STATE yy_pop_state()
207 noyywrap (do not call yywrap on end of file; avoid use of -lfl)
208 never-interactive (to go a bit faster)
209 stack (use a start-condition stack)
213 %option never-interactive
216 /* The start states are:
217 Code -- normal Haskell code (principal lexer)
218 GlaExt -- Haskell code with Glasgow extensions
219 Comment -- Nested comment processing
220 String -- Inside a string literal with backslashes
221 StringEsc -- Immediately following a backslash in a string literal
222 Char -- Inside a character literal with backslashes
223 CharEsc -- Immediately following a backslash in a character literal
225 Note that the INITIAL state is unused. Also note that these states
226 are _exclusive_. All rules should be prefixed with an appropriate
227 list of start states.
230 %x Char CharEsc Code Comment GlaExt UserPragma String StringEsc
232 isoS [\xa1-\xbf\xd7\xf7]
233 isoL [\xc0-\xd6\xd8-\xde]
234 isol [\xdf-\xf6\xf8-\xff]
241 F {N}"."{N}(("e"|"E")("+"|"-")?{N})?
242 S [!#$%&*+./<=>?@\\^|\-~:\xa1-\xbf\xd7\xf7]
244 L [A-Z\xc0-\xd6\xd8-\xde]
245 l [a-z\xdf-\xf6\xf8-\xff]
250 CHAR [ !#$%&()*+,\-./0-9:;<=>?@A-Z\[\]^_`a-z{|}~\xa1-\xff]
259 * Simple comments and whitespace. Normally, we would just ignore these, but
260 * in case we're processing a string escape, we need to note that we've seen
263 * Note that we cater for a comment line that *doesn't* end in a newline.
264 * This is incorrect, strictly speaking, but seems like the right thing
265 * to do. Reported by Rajiv Mirani. (WDP 95/08)
267 * Hackily moved up here so that --<<EOF>> will match -- SOF 5/97
271 <Code,GlaExt,StringEsc>"--"[^\n\r]*{NL}?{WS}* |
272 <Code,GlaExt,UserPragma,StringEsc>{WS}+ { noGap = FALSE; }
276 * Special GHC pragma rules. Do we need a start state for interface files,
277 * so these won't be matched in source files? --JSM
283 /* I believe the next rule is not ever matched.
285 The '#line ' rule is un-cool, recognising a cpp directive inside hs source.
286 Driver has now been modified to output `standard' {-# LINE ..-} pragmas
287 where possible, so the lexer should now never see cpp directives
288 like '# ' and '#line'.
292 <Code,GlaExt>^"# ".*{NL} {
293 char tempf[FILENAME_SIZE];
294 sscanf(yytext+1, "%d \"%[^\"]", &hslineno, tempf);
296 hsplineno = hslineno; hscolno = 0; hspcolno = 0;
299 <Code,GlaExt>^"#line ".*{NL} {
300 char tempf[FILENAME_SIZE];
301 sscanf(yytext+5, "%d \"%[^\"]", &hslineno, tempf);
303 hsplineno = hslineno; hscolno = 0; hspcolno = 0;
308 <Code,GlaExt>"{-# LINE ".*"-}"{NL} {
309 /* partain: pragma-style line directive */
310 char tempf[FILENAME_SIZE];
311 sscanf(yytext+9, "%d \"%[^\"]", &hslineno, tempf);
313 hsplineno = hslineno; hscolno = 0; hspcolno = 0;
316 <Code,GlaExt>"{-#"{WS}*"INTERFACE" {
317 PUSH_STATE(UserPragma);
318 RETURN(INTERFACE_UPRAGMA);
320 <Code,GlaExt>"{-#"{WS}*"SPECIALI"[SZ]E {
321 PUSH_STATE(UserPragma);
322 RETURN(SPECIALISE_UPRAGMA);
324 <Code,GlaExt>"{-#"{WS}*"INLINE" {
325 PUSH_STATE(UserPragma);
326 RETURN(INLINE_UPRAGMA);
328 <Code,GlaExt>"{-#"{WS}*"NOINLINE" {
329 PUSH_STATE(UserPragma);
330 RETURN(NOINLINE_UPRAGMA);
332 <Code,GlaExt>"{-#"{WS}*"MAGIC_UNFOLDING" {
333 PUSH_STATE(UserPragma);
334 RETURN(MAGIC_UNFOLDING_UPRAGMA);
336 <Code,GlaExt>"{-#"{WS}*"GENERATE_SPECS" {
337 /* these are handled by hscpp */
341 <Code,GlaExt>"{-#"{WS}*"OPTIONS" {
342 /* these are for the driver! */
346 <Code,GlaExt>"{-#"{WS}*"SOURCE"{WS}*"#"?"-}" {
347 /* these are used by `make depend' and the
348 compiler to indicate that a module should
349 be imported from source */
351 RETURN(SOURCE_UPRAGMA);
354 <Code,GlaExt>"{-#"{WS}*[A-Z_]+ {
355 fprintf(stderr, "%s:%d: Warning: Unrecognised pragma '",
356 input_filename, hsplineno);
357 format_string(stderr, (unsigned char *) yytext, yyleng);
358 fputs("'\n", stderr);
362 <UserPragma>"#-}" { POP_STATE; RETURN(END_UPRAGMA); }
366 * Haskell keywords. `scc' is actually a Glasgow extension, but it is
367 * intentionally accepted as a keyword even for normal <Code>.
371 <Code,GlaExt>"case" { RETURN(CASE); }
372 <Code,GlaExt>"class" { RETURN(CLASS); }
373 <Code,GlaExt,UserPragma>"data" { RETURN(DATA); }
374 <Code,GlaExt>"default" { RETURN(DEFAULT); }
375 <Code,GlaExt>"deriving" { RETURN(DERIVING); }
376 <Code,GlaExt>"do" { RETURN(DO); }
377 <Code,GlaExt>"else" { RETURN(ELSE); }
378 <Code,GlaExt>"if" { RETURN(IF); }
379 <Code,GlaExt>"import" { RETURN(IMPORT); }
380 <Code,GlaExt>"in" { RETURN(IN); }
381 <Code,GlaExt>"infix" { RETURN(INFIX); }
382 <Code,GlaExt>"infixl" { RETURN(INFIXL); }
383 <Code,GlaExt>"infixr" { RETURN(INFIXR); }
384 <Code,GlaExt,UserPragma>"instance" { RETURN(INSTANCE); }
385 <Code,GlaExt>"let" { RETURN(LET); }
386 <Code,GlaExt>"module" { RETURN(MODULE); }
387 <Code,GlaExt>"newtype" { RETURN(NEWTYPE); }
388 <Code,GlaExt>"of" { RETURN(OF); }
389 <Code,GlaExt>"then" { RETURN(THEN); }
390 <Code,GlaExt>"type" { RETURN(TYPE); }
391 <Code,GlaExt>"where" { RETURN(WHERE); }
393 <Code,GlaExt>"as" { RETURN(AS); }
394 <Code,GlaExt>"hiding" { RETURN(HIDING); }
395 <Code,GlaExt>"qualified" { RETURN(QUALIFIED); }
397 <Code,GlaExt>"_scc_" { RETURN(SCC); }
398 <GlaExt>"_ccall_" { RETURN(CCALL); }
399 <GlaExt>"_ccall_GC_" { RETURN(CCALL_GC); }
400 <GlaExt>"_casm_" { RETURN(CASM); }
401 <GlaExt>"_casm_GC_" { RETURN(CASM_GC); }
405 * Haskell operators: special, reservedops and useful varsyms
409 <Code,GlaExt,UserPragma>"(" { RETURN(OPAREN); }
410 <Code,GlaExt,UserPragma>")" { RETURN(CPAREN); }
411 <Code,GlaExt,UserPragma>"[" { RETURN(OBRACK); }
412 <Code,GlaExt,UserPragma>"]" { RETURN(CBRACK); }
413 <Code,GlaExt>"{" { RETURN(OCURLY); }
414 <Code,GlaExt>"}" { RETURN(CCURLY); }
415 <Code,GlaExt,UserPragma>"," { RETURN(COMMA); }
416 <Code,GlaExt>";" { RETURN(SEMI); }
417 <Code,GlaExt>"`" { RETURN(BQUOTE); }
418 <Code,GlaExt>"_" { RETURN(WILDCARD); }
420 <Code,GlaExt>".." { RETURN(DOTDOT); }
421 <Code,GlaExt,UserPragma>"::" { RETURN(DCOLON); }
422 <Code,GlaExt,UserPragma>"=" { RETURN(EQUAL); }
423 <Code,GlaExt>"\\" { RETURN(LAMBDA); }
424 <Code,GlaExt>"|" { RETURN(VBAR); }
425 <Code,GlaExt>"<-" { RETURN(LARROW); }
426 <Code,GlaExt,UserPragma>"->" { RETURN(RARROW); }
427 <Code,GlaExt>"-" { RETURN(MINUS); }
428 <Code,GlaExt>"+" { RETURN(PLUS); }
430 <Code,GlaExt,UserPragma>"=>" { RETURN(DARROW); }
431 <Code,GlaExt>"@" { RETURN(AT); }
432 <Code,GlaExt>"!" { RETURN(BANG); }
433 <Code,GlaExt>"~" { RETURN(LAZY); }
437 * Integers and (for Glasgow extensions) primitive integers. Note that
438 * we pass all of the text on to the parser, because flex/C can't handle
439 * arbitrary precision numbers.
443 <GlaExt>("-")?"0"[Oo]{O}+"#" { /* octal */
444 yylval.uid = xstrndup(yytext, yyleng - 1);
447 <Code,GlaExt>"0"[Oo]{O}+ { /* octal */
448 yylval.uid = xstrndup(yytext, yyleng);
451 <GlaExt>("-")?"0"[Xx]{H}+"#" { /* hexadecimal */
452 yylval.uid = xstrndup(yytext, yyleng - 1);
455 <Code,GlaExt>"0"[Xx]{H}+ { /* hexadecimal */
456 yylval.uid = xstrndup(yytext, yyleng);
459 <GlaExt>("-")?{N}"#" {
460 yylval.uid = xstrndup(yytext, yyleng - 1);
463 <Code,GlaExt,UserPragma>{N} {
464 yylval.uid = xstrndup(yytext, yyleng);
470 * Floats and (for Glasgow extensions) primitive floats/doubles.
474 <GlaExt>("-")?{F}"##" {
475 yylval.uid = xstrndup(yytext, yyleng - 2);
478 <GlaExt>("-")?{F}"#" {
479 yylval.uid = xstrndup(yytext, yyleng - 1);
483 yylval.uid = xstrndup(yytext, yyleng);
489 * Funky ``foo'' style C literals for Glasgow extensions
493 <GlaExt>"``"[^']+"''" {
494 hsnewid(yytext + 2, yyleng - 4);
500 * Identifiers, both variables and operators. The trailing hash is allowed
501 * for Glasgow extensions.
507 /* These SHOULDNAE work in "Code" (sigh) */
509 <GlaExt,UserPragma>{Id}"#" {
510 if (! nonstandardFlag) {
511 char errbuf[ERR_BUF_SIZE];
512 sprintf(errbuf, "Non-standard identifier (trailing `#'): %s\n", yytext);
515 hsnewid(yytext, yyleng);
516 RETURN(isconstr(yytext) ? CONID : VARID);
518 <Code,GlaExt,UserPragma>{Id} {
519 hsnewid(yytext, yyleng);
520 RETURN(isconstr(yytext) ? CONID : VARID);
522 <Code,GlaExt,UserPragma>{SId} {
523 hsnewid(yytext, yyleng);
524 RETURN(isconstr(yytext) ? CONSYM : VARSYM);
526 <Code,GlaExt,UserPragma>{Mod}"."{Id}"#" {
528 if (! nonstandardFlag) {
529 char errbuf[ERR_BUF_SIZE];
530 sprintf(errbuf, "Non-standard identifier (trailing `#'): %s\n", yytext);
533 is_constr = hsnewqid(yytext, yyleng);
534 RETURN(is_constr ? QCONID : QVARID);
536 <Code,GlaExt,UserPragma>{Mod}"."{Id} {
537 BOOLEAN is_constr = hsnewqid(yytext, yyleng);
538 RETURN(is_constr ? QCONID : QVARID);
540 <Code,GlaExt,UserPragma>{Mod}"."{SId} {
541 BOOLEAN is_constr = hsnewqid(yytext, yyleng);
542 RETURN(is_constr ? QCONSYM : QVARSYM);
546 /* Why is `{Id}#` matched this way, and `{Id}` lexed as three tokens? --JSM */
548 /* Because we can make the former well-behaved (we defined them).
550 Sadly, the latter is defined by Haskell, which allows such
551 la-la land constructs as `{-a 900-line comment-} foo`. (WDP 94/12)
555 <GlaExt,UserPragma>"`"{Id}"#`" {
556 hsnewid(yytext + 1, yyleng - 2);
557 RETURN(isconstr(yytext+1) ? CONSYM : VARSYM);
562 * Character literals. The first form is the quick form, for character
563 * literals that don't contain backslashes. Literals with backslashes are
564 * lexed through multiple rules. First, we match the open ' and as many
565 * normal characters as possible. This puts us into the <Char> state, where
566 * a backslash is legal. Then, we match the backslash and move into the
567 * <CharEsc> state. When we drop out of <CharEsc>, we collect more normal
568 * characters and the close '. We may end up with too many characters, but
569 * this allows us to easily share the lex rules with strings. Excess characters
570 * are ignored with a warning.
574 <GlaExt>'({CHAR}|"\"")"'#" {
575 yylval.uhstring = installHstring(1, yytext+1);
578 <Code,GlaExt>'({CHAR}|"\"")' {
579 yylval.uhstring = installHstring(1, yytext+1);
582 <Code,GlaExt>'' {char errbuf[ERR_BUF_SIZE];
583 sprintf(errbuf, "'' is not a valid character (or string) literal\n");
586 <Code,GlaExt>'({CHAR}|"\"")* {
587 hsmlcolno = hspcolno;
589 addtext(yytext+1, yyleng-1);
592 <Char>({CHAR}|"\"")*'# {
596 addtext(yytext, yyleng - 2);
597 text = fetchtext(&length);
599 if (! nonstandardFlag) {
600 char errbuf[ERR_BUF_SIZE];
601 sprintf(errbuf, "`Char-hash' literals are non-standard: %s\n", text);
606 fprintf(stderr, "%s:%d:%d: Unboxed character literal '",
607 input_filename, hsplineno, hspcolno + 1);
608 format_string(stderr, (unsigned char *) text, length);
609 fputs("' too long\n", stderr);
612 yylval.uhstring = installHstring(1, text);
613 hspcolno = hsmlcolno;
617 <Char>({CHAR}|"\"")*' {
621 addtext(yytext, yyleng - 1);
622 text = fetchtext(&length);
625 fprintf(stderr, "%s:%d:%d: Character literal '",
626 input_filename, hsplineno, hspcolno + 1);
627 format_string(stderr, (unsigned char *) text, length);
628 fputs("' too long\n", stderr);
631 yylval.uhstring = installHstring(1, text);
632 hspcolno = hsmlcolno;
636 <Char>({CHAR}|"\"")+ { addtext(yytext, yyleng); }
641 * String literals. The first form is the quick form, for string literals
642 * that don't contain backslashes. Literals with backslashes are lexed
643 * through multiple rules. First, we match the open " and as many normal
644 * characters as possible. This puts us into the <String> state, where
645 * a backslash is legal. Then, we match the backslash and move into the
646 * <StringEsc> state. When we drop out of <StringEsc>, we collect more normal
647 * characters, moving back and forth between <String> and <StringEsc> as more
648 * backslashes are encountered. (We may even digress into <Comment> mode if we
649 * find a comment in a gap between backslashes.) Finally, we read the last chunk
650 * of normal characters and the close ".
654 <GlaExt>"\""({CHAR}|"'")*"\""# {
655 yylval.uhstring = installHstring(yyleng-3, yytext+1);
656 /* the -3 accounts for the " on front, "# on the end */
659 <Code,GlaExt>"\""({CHAR}|"'")*"\"" {
660 yylval.uhstring = installHstring(yyleng-2, yytext+1);
663 <Code,GlaExt>"\""({CHAR}|"'")* {
664 hsmlcolno = hspcolno;
666 addtext(yytext+1, yyleng-1);
669 <String>({CHAR}|"'")*"\"#" {
673 addtext(yytext, yyleng-2);
674 text = fetchtext(&length);
676 if (! nonstandardFlag) {
677 char errbuf[ERR_BUF_SIZE];
678 sprintf(errbuf, "`String-hash' literals are non-standard: %s\n", text);
682 yylval.uhstring = installHstring(length, text);
683 hspcolno = hsmlcolno;
687 <String>({CHAR}|"'")*"\"" {
691 addtext(yytext, yyleng-1);
692 text = fetchtext(&length);
694 yylval.uhstring = installHstring(length, text);
695 hspcolno = hsmlcolno;
699 <String>({CHAR}|"'")+ { addtext(yytext, yyleng); }
703 * Character and string escapes are roughly the same, but strings have the
704 * extra `\&' sequence which is not allowed for characters. Also, comments
705 * are allowed in the <StringEsc> state. (See the comment section much
708 * NB: Backslashes and tabs are stored in strings as themselves.
709 * But if we print them (in printtree.c), they must go out as
710 * "\\\\" and "\\t" respectively. (This is because of the bogus
711 * intermediate format that the parser produces. It uses '\t' fpr end of
712 * string, so it needs to be able to escape tabs, which means that it
713 * also needs to be able to escape the escape character ('\\'). Sigh.
717 <Char>\\ { PUSH_STATE(CharEsc); }
718 <String>\\& /* Ignore */ ;
719 <String>\\ { PUSH_STATE(StringEsc); noGap = TRUE; }
721 <CharEsc>\\ { addchar(*yytext); POP_STATE; }
722 <StringEsc>\\ { if (noGap) { addchar(*yytext); } POP_STATE; }
724 <CharEsc,StringEsc>["'] { addchar(*yytext); POP_STATE; }
725 <CharEsc,StringEsc>NUL { addchar('\000'); POP_STATE; }
726 <CharEsc,StringEsc>SOH { addchar('\001'); POP_STATE; }
727 <CharEsc,StringEsc>STX { addchar('\002'); POP_STATE; }
728 <CharEsc,StringEsc>ETX { addchar('\003'); POP_STATE; }
729 <CharEsc,StringEsc>EOT { addchar('\004'); POP_STATE; }
730 <CharEsc,StringEsc>ENQ { addchar('\005'); POP_STATE; }
731 <CharEsc,StringEsc>ACK { addchar('\006'); POP_STATE; }
732 <CharEsc,StringEsc>BEL |
733 <CharEsc,StringEsc>a { addchar('\007'); POP_STATE; }
734 <CharEsc,StringEsc>BS |
735 <CharEsc,StringEsc>b { addchar('\010'); POP_STATE; }
736 <CharEsc,StringEsc>HT |
737 <CharEsc,StringEsc>t { addchar('\011'); POP_STATE; }
738 <CharEsc,StringEsc>LF |
739 <CharEsc,StringEsc>n { addchar('\012'); POP_STATE; }
740 <CharEsc,StringEsc>VT |
741 <CharEsc,StringEsc>v { addchar('\013'); POP_STATE; }
742 <CharEsc,StringEsc>FF |
743 <CharEsc,StringEsc>f { addchar('\014'); POP_STATE; }
744 <CharEsc,StringEsc>CR |
745 <CharEsc,StringEsc>r { addchar('\015'); POP_STATE; }
746 <CharEsc,StringEsc>SO { addchar('\016'); POP_STATE; }
747 <CharEsc,StringEsc>SI { addchar('\017'); POP_STATE; }
748 <CharEsc,StringEsc>DLE { addchar('\020'); POP_STATE; }
749 <CharEsc,StringEsc>DC1 { addchar('\021'); POP_STATE; }
750 <CharEsc,StringEsc>DC2 { addchar('\022'); POP_STATE; }
751 <CharEsc,StringEsc>DC3 { addchar('\023'); POP_STATE; }
752 <CharEsc,StringEsc>DC4 { addchar('\024'); POP_STATE; }
753 <CharEsc,StringEsc>NAK { addchar('\025'); POP_STATE; }
754 <CharEsc,StringEsc>SYN { addchar('\026'); POP_STATE; }
755 <CharEsc,StringEsc>ETB { addchar('\027'); POP_STATE; }
756 <CharEsc,StringEsc>CAN { addchar('\030'); POP_STATE; }
757 <CharEsc,StringEsc>EM { addchar('\031'); POP_STATE; }
758 <CharEsc,StringEsc>SUB { addchar('\032'); POP_STATE; }
759 <CharEsc,StringEsc>ESC { addchar('\033'); POP_STATE; }
760 <CharEsc,StringEsc>FS { addchar('\034'); POP_STATE; }
761 <CharEsc,StringEsc>GS { addchar('\035'); POP_STATE; }
762 <CharEsc,StringEsc>RS { addchar('\036'); POP_STATE; }
763 <CharEsc,StringEsc>US { addchar('\037'); POP_STATE; }
764 <CharEsc,StringEsc>SP { addchar('\040'); POP_STATE; }
765 <CharEsc,StringEsc>DEL { addchar('\177'); POP_STATE; }
766 <CharEsc,StringEsc>"^"{CNTRL} { char c = yytext[1] - '@'; addchar(c); POP_STATE; }
767 <CharEsc,StringEsc>{D}+ {
768 int i = strtol(yytext, NULL, 10);
772 char errbuf[ERR_BUF_SIZE];
773 sprintf(errbuf, "Numeric escape \"\\%s\" out of range\n",
779 <CharEsc,StringEsc>o{O}+ {
780 int i = strtol(yytext + 1, NULL, 8);
784 char errbuf[ERR_BUF_SIZE];
785 sprintf(errbuf, "Numeric escape \"\\%s\" out of range\n",
791 <CharEsc,StringEsc>x{H}+ {
792 int i = strtol(yytext + 1, NULL, 16);
796 char errbuf[ERR_BUF_SIZE];
797 sprintf(errbuf, "Numeric escape \"\\%s\" out of range\n",
807 * Nested comments. The major complication here is in trying to match the
808 * longest lexemes possible, for better performance. (See the flex document.)
809 * That's why the rules look so bizarre.
813 <Code,GlaExt,UserPragma,StringEsc>"{-" {
814 noGap = FALSE; nested_comments = 1; PUSH_STATE(Comment);
818 <Comment>"-"+[^-{}]+ |
819 <Comment>"{"+[^-{}]+ ;
820 <Comment>"{-" { nested_comments++; }
821 <Comment>"-}" { if (--nested_comments == 0) POP_STATE; }
826 * Illegal characters. This used to be a single rule, but we might as well
827 * pass on as much information as we have, so now we indicate our state in
832 <INITIAL,Code,GlaExt,UserPragma>(.|\n) {
833 fprintf(stderr, "%s:%d:%d: Illegal character: `",
834 input_filename, hsplineno, hspcolno + 1);
835 format_string(stderr, (unsigned char *) yytext, 1);
836 fputs("'\n", stderr);
840 fprintf(stderr, "%s:%d:%d: Illegal character: `",
841 input_filename, hsplineno, hspcolno + 1);
842 format_string(stderr, (unsigned char *) yytext, 1);
843 fputs("' in a character literal\n", stderr);
847 fprintf(stderr, "%s:%d:%d: Illegal character escape: `\\",
848 input_filename, hsplineno, hspcolno + 1);
849 format_string(stderr, (unsigned char *) yytext, 1);
850 fputs("'\n", stderr);
853 <String>(.|\n) { if (nonstandardFlag) {
854 addtext(yytext, yyleng);
856 fprintf(stderr, "%s:%d:%d: Illegal character: `",
857 input_filename, hsplineno, hspcolno + 1);
858 format_string(stderr, (unsigned char *) yytext, 1);
859 fputs("' in a string literal\n", stderr);
865 fprintf(stderr, "%s:%d:%d: Illegal string escape: `\\",
866 input_filename, hsplineno, hspcolno + 1);
867 format_string(stderr, (unsigned char *) yytext, 1);
868 fputs("'\n", stderr);
871 fprintf(stderr, "%s:%d:%d: Illegal character: `",
872 input_filename, hsplineno, hspcolno + 1);
873 format_string(stderr, (unsigned char *) yytext, 1);
874 fputs("' in a string gap\n", stderr);
881 * End of file. In any sub-state, this is an error. However, for the primary
882 * <Code> and <GlaExt> states, this is perfectly normal. We just return an EOF
883 * and let the yylex() wrapper deal with whatever has to be done next (e.g.
884 * adding virtual close curlies, or closing an interface and returning to the
885 * primary source file.
887 * Note that flex does not call YY_USER_ACTION for <<EOF>> rules. Hence the
888 * line/column advancement has to be done by hand.
892 <Char,CharEsc><<EOF>> {
893 hsplineno = hslineno; hspcolno = hscolno;
894 hsperror("unterminated character literal");
897 hsplineno = hslineno; hspcolno = hscolno;
898 hsperror("unterminated comment");
900 <String,StringEsc><<EOF>> {
901 hsplineno = hslineno; hspcolno = hscolno;
902 hsperror("unterminated string literal");
904 <UserPragma><<EOF>> {
905 hsplineno = hslineno; hspcolno = hscolno;
906 hsperror("unterminated user-specified pragma");
908 <Code,GlaExt><<EOF>> { hsplineno = hslineno; hspcolno = hscolno; return(EOF); }
912 /**********************************************************************
915 * YACC/LEX Initialisation etc. *
918 **********************************************************************/
921 We initialise input_filename to "<stdin>".
922 This allows unnamed sources to be piped into the parser.
928 input_filename = xstrdup("<stdin>");
930 /* We must initialize the input buffer _now_, because we call
931 setyyin _before_ calling yylex for the first time! */
932 yy_switch_to_buffer(yy_create_buffer(stdin, YY_BUF_SIZE));
941 new_filename(char *f) /* This looks pretty dodgy to me (WDP) */
943 if (input_filename != NULL)
944 free(input_filename);
945 input_filename = xstrdup(f);
948 /**********************************************************************
951 * Layout Processing *
954 **********************************************************************/
957 The following section deals with Haskell Layout conventions
958 forcing insertion of ; or } as appropriate
964 return (!forgetindent && INDENTON);
968 /* Enter new context and set new indentation level */
973 fprintf(stderr, "hssetindent:hscolno=%d,hspcolno=%d,INDENTPT[%d]=%d\n", hscolno, hspcolno, icontexts, INDENTPT);
977 * partain: first chk that new indent won't be less than current one; this code
978 * doesn't make sense to me; hscolno tells the position of the _end_ of the
979 * current token; what that has to do with indenting, I don't know.
983 if (hscolno - 1 <= INDENTPT) {
985 return; /* Empty input OK for Haskell 1.1 */
987 char errbuf[ERR_BUF_SIZE];
989 sprintf(errbuf, "Layout error -- indentation should be > %d cols", INDENTPT);
993 hsentercontext((hspcolno << 1) | 1);
997 /* Enter a new context without changing the indentation level */
1002 fprintf(stderr, "hsincindent:hscolno=%d,hspcolno=%d,INDENTPT[%d]=%d\n", hscolno, hspcolno, icontexts, INDENTPT);
1004 hsentercontext(indenttab[icontexts] & ~1);
1008 /* Turn off indentation processing, usually because an explicit "{" has been seen */
1012 forgetindent = TRUE;
1016 /* Enter a new layout context. */
1018 hsentercontext(int indent)
1020 /* Enter new context and set indentation as specified */
1021 if (++icontexts >= MAX_CONTEXTS) {
1022 char errbuf[ERR_BUF_SIZE];
1024 sprintf(errbuf, "`wheres' and `cases' nested too deeply (>%d)", MAX_CONTEXTS - 1);
1027 forgetindent = FALSE;
1028 indenttab[icontexts] = indent;
1030 fprintf(stderr, "hsentercontext:indent=%d,hscolno=%d,hspcolno=%d,INDENTPT[%d]=%d\n", indent, hscolno, hspcolno, icontexts, INDENTPT);
1035 /* Exit a layout context */
1041 fprintf(stderr, "hsendindent:hscolno=%d,hspcolno=%d,INDENTPT[%d]=%d\n", hscolno, hspcolno, icontexts, INDENTPT);
1046 * Return checks the indentation level and returns ;, } or the specified token.
1056 if (hsshouldindent()) {
1057 if (hspcolno < INDENTPT) {
1059 fprintf(stderr, "inserted '}' before %d (%d:%d:%d:%d)\n", tok, hspcolno, hscolno, yyleng, INDENTPT);
1063 } else if (hspcolno == INDENTPT) {
1065 fprintf(stderr, "inserted ';' before %d (%d:%d)\n", tok, hspcolno, INDENTPT);
1073 fprintf(stderr, "returning %d (%d:%d)\n", tok, hspcolno, INDENTPT);
1080 * Redefine yylex to check for stacked tokens, yylex1() is the original yylex()
1086 static BOOLEAN eof = FALSE;
1089 if (hssttok != -1) {
1097 endlineno = hslineno;
1098 if ((tok = yylex1()) != EOF)
1104 if (icontexts > icontexts_save) {
1107 indenttab[icontexts] = 0;
1110 hsperror("missing '}' at end of file");
1111 } else if (hsbuf_save != NULL) {
1113 yy_delete_buffer(YY_CURRENT_BUFFER);
1114 yy_switch_to_buffer(hsbuf_save);
1116 new_filename(filename_save);
1117 free(filename_save);
1118 hslineno = hslineno_save;
1119 hsplineno = hsplineno_save;
1120 hscolno = hscolno_save;
1121 hspcolno = hspcolno_save;
1123 icontexts = icontexts_save - 1;
1126 fprintf(stderr, "finished reading interface (%d:%d:%d)\n", hscolno, hspcolno, INDENTPT);
1131 hsperror("No longer using yacc to parse interface files");
1136 abort(); /* should never get here! */
1140 /**********************************************************************
1143 * Input Processing for Interfaces -- Not currently used !!! *
1146 **********************************************************************/
1148 /* setyyin(file) open file as new lex input buffer */
1154 hsbuf_save = YY_CURRENT_BUFFER;
1155 if ((yyin = fopen(file, "r")) == NULL) {
1156 char errbuf[ERR_BUF_SIZE];
1158 sprintf(errbuf, "can't read \"%-.50s\"", file);
1161 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
1163 hslineno_save = hslineno;
1164 hsplineno_save = hsplineno;
1165 hslineno = hsplineno = 1;
1167 filename_save = input_filename;
1168 input_filename = NULL;
1170 hscolno_save = hscolno;
1171 hspcolno_save = hspcolno;
1172 hscolno = hspcolno = 0;
1173 etags_save = etags; /* do not do "etags" stuff in interfaces */
1174 etags = 0; /* We remember whether we are doing it in
1175 the module, so we can restore it later [WDP 94/09] */
1176 hsentercontext(-1); /* partain: changed this from 0 */
1177 icontexts_save = icontexts;
1179 fprintf(stderr, "reading %s (%d:%d:%d)\n", input_filename, hscolno_save, hspcolno_save, INDENTPT);
1184 layout_input(char *text, int len)
1187 fprintf(stderr, "Scanning \"%s\"\n", text);
1190 hsplineno = hslineno;
1202 hscolno += 8 - (hscolno % 8); /* Tabs stops are 8 columns apart */
1214 setstartlineno(void)
1216 startlineno = hsplineno;
1218 if (modulelineno == 0) {
1219 modulelineno = startlineno;
1225 fprintf(stderr,"%u\tsetstartlineno (col %u)\n",startlineno,hscolno);
1229 /**********************************************************************
1235 **********************************************************************/
1237 #define CACHE_SIZE YY_BUF_SIZE
1243 } textcache = { 0, 0, NULL };
1248 /* fprintf(stderr, "cleartext\n"); */
1250 if (textcache.allocated == 0) {
1251 textcache.allocated = CACHE_SIZE;
1252 textcache.text = xmalloc(CACHE_SIZE);
1257 addtext(char *text, unsigned length)
1259 /* fprintf(stderr, "addtext: %d %s\n", length, text); */
1264 if (textcache.next + length + 1 >= textcache.allocated) {
1265 textcache.allocated += length + CACHE_SIZE;
1266 textcache.text = xrealloc(textcache.text, textcache.allocated);
1268 bcopy(text, textcache.text + textcache.next, length);
1269 textcache.next += length;
1275 /* fprintf(stderr, "addchar: %c\n", c); */
1277 if (textcache.next + 2 >= textcache.allocated) {
1278 textcache.allocated += CACHE_SIZE;
1279 textcache.text = xrealloc(textcache.text, textcache.allocated);
1281 textcache.text[textcache.next++] = c;
1285 fetchtext(unsigned *length)
1287 /* fprintf(stderr, "fetchtext: %d\n", textcache.next); */
1289 *length = textcache.next;
1290 textcache.text[textcache.next] = '\0';
1291 return textcache.text;
1294 /**********************************************************************
1297 * Identifier Processing *
1300 **********************************************************************/
1303 hsnewid Enters an id of length n into the symbol table.
1307 hsnewid(char *name, int length)
1309 char save = name[length];
1311 name[length] = '\0';
1312 yylval.uid = installid(name);
1313 name[length] = save;
1317 hsnewqid(char *name, int length)
1320 char save = name[length];
1321 name[length] = '\0';
1323 dot = strchr(name, '.');
1325 yylval.uqid = mkaqual(installid(name),installid(dot+1));
1327 name[length] = save;
1329 return isconstr(dot+1);