Debian Codegraph @

src/lib/star.l

raw
1 /*******************************************************************************
2 This code borrows some patterns from star.l,
3 which was written and designed by
4 Andrew Gene HALL of I.N. Services Pty. Ltd., Scarborough WA, Australia,
5 for the University of Western Australia, Crawley WA, Australia.
6
7 Present code by JRH to work with PyCIFRW v 3.1
8 Match for EOL at EOF contributed by Boris Dusek
9 *******************************************************************************/
10 /* Lex Definitions for a STAR File */
11
12 %{
13
14 /* Global Definitions */
15 #include <ctype.h>
16 #include <stdio.h>
17
18 #include "star_scanner.h"
19
20 #define YY_DECL int star_scanner()
21
22 /* redefine YY_INPUT to come from our global string */
23 #define YY_INPUT(buf,result,max_size)\
24 {\
25 if(string_pos == in_string_len) result = YY_NULL;\
26 if(string_pos + max_size <= in_string_len) {\
27 strncpy(buf,input_string+string_pos,max_size);\
28 string_pos +=max_size;\
29 result =max_size;\
30 } else {\
31 strncpy(buf,input_string+string_pos,in_string_len - string_pos);\
32 result = in_string_len - string_pos;\
33 string_pos = in_string_len;\
34 }\
35 }
36
37 %}
38
39 /* These have been modified from starbase, as they allow all sorts of
40 forbidden characters */
41 a [aA]
42 b [bB]
43 d [dD]
44 e [eE]
45 g [gG]
46 l [lL]
47 o [oO]
48 p [pP]
49 s [sS]
50 t [tT]
51 v [vV]
52 Dollar \$
53 Data_ {d}{a}{t}{a}_
54 Loop_ {l}{o}{o}{p}_
55 Global_ {g}{l}{o}{b}{a}{l}_
56 Stop_ {s}{t}{o}{p}_
57 Save_ {s}{a}{v}{e}_
58 Char [^\n\r\0]
59 TrueChar [][!%&\(\)*+,./:<=>?@0-9A-Za-z\\^`{}|~"#$';_ \t-]
60 Blank [\n \t\r\0]
61 NonBlank [^\n \t\r]
62 TrueNonBlank [][!%&\(\)*+,./:<=>?@0-9A-Za-z\\^`{}|~"#$';_-]
63 NonDQuote [^\n\r"\0]
64 NonSQuote [^\n\r'\0]
65 NotUnderscore [^\n \r\t_\0]
66 Space [\t ]
67 Spaces [\t ]*
68 Comment #[^\n\r]*
69 EndOfLine {Spaces}({Comment})?(\n|\r|\r\n)
70
71 %a 4000
72 %o 6000
73 %x Alltext
74 %option noyywrap
75 %option yylineno
76
77 /* Lex Rules fo a STAR File */
78 %%
79 {Loop_} {return(DLBLOCK);}
80 {Global_} {return(DGLOBAL);}
81 {Stop_} {return(DSTOP);}
82 {Save_}{TrueNonBlank}+ {return(DSAVE_HEADING);}
83 {Save_} {return(DSAVE_END);}
84 {Data_}{TrueNonBlank}+ {return(DDATA_HEADING);}
85 _{TrueNonBlank}+ {return(DDATA_NAME);}
86 {Spaces} {/* do nothing */}
87 {Comment} {/* do nothing */}
88 {EndOfLine}/[^;] {/* do nothing */}
89
90 (\n|\r\n);({TrueChar})*(\r\n|\r|\n)+ {BEGIN(Alltext);return(DSTART_SC_LINE);}
91 <Alltext>[^;\r\n]({TrueChar})*(\r\n|\r|\n)+ {return(DSC_LINE_OF_TEXT);}
92 <Alltext>; {BEGIN(INITIAL);
93 return(DEND_SC_LINE);}
94
95 ([^\n\r\t \"#$\'_\[\]\0]{TrueNonBlank}*) {return(DDATA_VALUE_1);}
96
97 \'{TrueChar}*\' {/* this code borrowed from star.l in
98 starbase. We can't write a rule in
99 flex to accept a quote if the following
100 character is not blank, and accepting
101 the following non-blank may swallow the
102 closing quote! */
103
104 char *p;
105 p = yytext;
106 /* step over first quote */
107 p++;
108 /* scan to "?
109 * (? == null) or (? == space) => end of string, stop
110 */
111 while ( p = strchr( p, '\''))
112 if (isspace( *(++p))) {
113 /* (? == space) => push back rest of the input,
114 * S_Q_T_S */
115 /* this is to avoid side effects in the macro */
116 int i = yyleng - strlen(p);
117 yyless( i );
118 }
119 return(DDATA_VALUE_1);
120 }
121
122 \"{TrueChar}*\" {/* this code borrowed from star.l in
123 starbase. We can't write a rule in
124 flex to accept a quote only if the following
125 character is not blank, and writing a
126 quote-nonblank rule may swallow the
127 closing quote. */
128
129 char *p;
130 p = yytext;
131 /* step over first quote */
132 p++;
133 /* scan to '?
134 * (? == null) or (? == space) => end of string, stop
135 */
136 while ( p = strchr( p, '\"'))
137 if (isspace( *(++p))) {
138 /* (? == space) => push back rest of the input,
139 * S_Q_T_S */
140 /* this is to avoid side effects in the macro */
141 int i = yyleng - strlen(p);
142 yyless( i );
143 }
144 return(DDATA_VALUE_1);
145 }
146
147 \0 {YY_FLUSH_BUFFER; return(DEND);}
148 [\n] {/*printf("Stray carriage return...\n");*/}
149 . {
150 /* printf("Unexpected Character\n");*/
151 /* printf("<%d>\n",yytext[0]);*/
152 YY_FLUSH_BUFFER;
153 return(DERROR);
154 }
155 %%
156 /* This routine is called during initialisation to avoid any problems
157 which might arise due to a syntax error not re-initialising the
158 scanner. Each time the scanner is initialised, the buffer is
159 flushed */
160 void star_clear(void) {
161 yy_flush_buffer(YY_CURRENT_BUFFER);
162 yylineno = 0;
163 BEGIN(INITIAL);
164 }