summaryrefslogtreecommitdiff
path: root/support/splint/examples/ld/ldnump.w
blob: 813628c68c521899af0cfb3a13fa3d54ea25ed46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
@q Copyright 2012-2020, Alexander Shibakov@>
@q This file is part of SPLinT@>

@q SPLinT is free software: you can redistribute it and/or modify@>
@q it under the terms of the GNU General Public License as published by@>
@q the Free Software Foundation, either version 3 of the License, or@>
@q (at your option) any later version.@>

@q SPLinT is distributed in the hope that it will be useful,@>
@q but WITHOUT ANY WARRANTY; without even the implied warranty of@>
@q MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the@>
@q GNU General Public License for more details.@>

@q You should have received a copy of the GNU General Public License@>
@q along with SPLinT.  If not, see <http://www.gnu.org/licenses/>.@>

@** The name parser for \ld\ term names. 
We take a lazy approach to the typeseting of term names for the \ld\
grammar by creating a dedicated parser for name processing. This way
any pattern we notice can be quickly incorporated into our typesetting scheme.
% We include the macros here since this file is intended to be
% included by the documentation `aggregator' so putting bare \TeX\
% at the beginning of the file runs the risk of producing an error
% of having \TeX\ material inside a \Cee\ section.
\genericprettytokens namespace: ldsmall, tokens: {}, correction: lstokenset.sty, host: ldsmall;
\iffalse
\let\currentparsernamespace\parsernamespace
    \let\parsernamespace\ldsmallnamespace
    \let\hostparsernamespace\ldsmallnamespace
    \input lstokenset.sty
\let\parsernamespace\currentparsernamespace
\fi
@(ld_num_parser.yy@>=
@G Switch to generic mode.
%{@> @<Name parser \Cee\ preamble@> @=%}
 @> @<Bison options@> @= 
%union {@> @<Union of parser types@> @=}
%{@> @<Name parser \Cee\ postamble@> @=%}
 @> @<Token and types ...@> @= 
%%
 @> @<Parser productions@> @= 
%%
@g

@ To put the new name parser to work, we need to initialize it. The
initialization is done by the macros below. After the initialization
has been completed, the switch command is replaced by the one that
activates the new name parser.
@<Modified name parser for \ld\ grammar@>=
@G
\genericparser name: ldnum, 
               ptables: ld_num_tab.tex,
               ltables: ld_num_dfa.tex,
               tokens: {},
               asetup: {}, 
               dsetup: {}, 
               rsetup: {},
               optimization: {};%
@g

@ @<Bison options@>=
@G
%token-table
%debug
%start hex_number
@g

@ @<Token and types declarations@>=
@G
%token DIGIT NYBBLE
@g

@*1 The name parser productions. These macros do a bit more than we
need to typeset the term names. Their core is designed to treat
suffixes and prefixes of a certain form in a special way. In addition,
some productions were left in place from the original name parser in
order to be able to refer to, say, \flex\ options in text. The inline
action in one of the rules for \prodstyle{identifier\_string} was
added to adjust the number and the position of the terms so that the
appropriate action can be reused later for
\prodstyle{qualified\_identifier\_string}. 
@<Parser productions@>=
@G
hex_number:
   DIGIT                                 {}
 | DIGIT DIGIT                           {}
 | DIGIT DIGIT DIGIT                     {}
 | hex_number nybble                     {}
 ; 

nybble:
   DIGIT DIGIT DIGIT DIGIT               {}
 ;
@g

@ @<Compose the full name@>=
  @[TeX_( "/yy0{/the/yy(1)/the/yy(2)}/namechars/yyval" );@]@;

@ @<Compose a qualified name@>=
  @[TeX_( "/yy0{/the/yy(3)/the/yy(4)/nx/dotsp/nx/qual/the/yy(1)}/namechars/yyval" );@]@;

@ @<Attach option name@>=
  @[TeX_( "/getfirst{/yy(1)}/to/toksa" );@]@;
  @[TeX_( "/getsecond{/yy(1)}/to/toksb" );@]@;
  @[TeX_( "/yy0{/nx/optstr{/the/toksa}{/the/toksb}}" );@]@;

@ @<Start with an identifier@>=
  @[TeX_( "/getfirst{/yy(1)}/to/toksa" );@]@;
  @[TeX_( "/getsecond{/yy(1)}/to/toksb" );@]@;
  @[TeX_( "/yy0{/nx/idstr{/the/toksa}{/the/toksb}}" );@]@;

@ @<Start with a tag@>=
  @[TeX_( "/getfirst{/yy(2)}/to/toksa" );@]@;
  @[TeX_( "/getsecond{/yy(2)}/to/toksb" );@]@;
  @[TeX_( "/yy0{/nx/idstr{</the/toksa>}{</the/toksb>}}" );@]@;

@ @<Start with a quoted string@>=
  @[TeX_( "/getfirst{/yy(2)}/to/toksa" );@]@;
  @[TeX_( "/getsecond{/yy(2)}/to/toksb" );@]@;
  @[TeX_( "/yy0{/nx/chstr{/the/toksa}{/the/toksb}}" );@]@;

@ @<Turn a qualifier into an identifier@>=
  @<Start with an identifier@>@;

@ @<Attach an identifier@>=
  @[TeX_( "/getsecond{/yy(1)}/to/toksa" );@]@;
  @[TeX_( "/appendr/toksa{/noexpand/_}" );@]@;  
  @[TeX_( "/getfirst{/yy(3)}/to/toksb" );@]@;
  @[TeX_( "/concat/toksa/toksb" );@]@;
  @[TeX_( "/getthird{/yy(1)}/to/toksb" );@]@;
  @[TeX_( "/appendr/toksb{/uscoreletter}" );@]@;  
  @[TeX_( "/getsecond{/yy(3)}/to/toksc" );@]@;
  @[TeX_( "/concat/toksb/toksc" );@]@;
  @[TeX_( "/yy0{/nx/idstr{/the/toksa}{/the/toksb}}" );@]@;

@ @<Attach qualifier to a name@>=


@ @<Attach an integer@>=
  @[TeX_( "/yy0{/nx/dotsp/nx/sfxi/the/yy(1)}" );@]@;
                               
@ @<Attach a subscripted integer@>=
  @[TeX_( "/yy0{/nx/dotsp/nx/sfxi/the/yy(2)}" );@]@;
                               
@ @<Attach a subscripted qualifier@>=
  @[TeX_( "/yy0{/nx/dotsp/nx/qual/the/yy(2)}" );@]@;
                               
@ @<Attach suffixes@>=
  @[TeX_( "/yy0{/nx/dotsp/the/yy(2)}" );@]@;

@ @<Attach qualified suffixes@>=
  @<Attach suffixes@>@;

@ @<Start with a named suffix@>=
  @[TeX_( "/yy0{/nx/sfxn/the/yy(1)}" );@]@;

@ @<Start with a numeric suffix@>=
  @[TeX_( "/yy0{/nx/sfxi/the/yy(1)}" );@]@;

@ @<Add a dot separator@>=
  @[TeX_( "/yy0{/the/yy(1)/nx/dotsp}" );@]@;

@ @<Attach integer suffix@>=
  @[TeX_( "/yy0{/the/yy(1)/nx/sfxi/the/yy(2)}" );@]@;

@ @<Attach a named suffix@>=
  @[TeX_( "/yy0{/the/yy(1)/nx/sfxn/the/yy(2)}" );@]@;

@ @<Attach a qualifier@>=
  @[TeX_( "/yy0{/the/yy(1)/nx/qual/the/yy(2)}" );@]@;

@ @<Start suffixes with a qualifier@>=
  @[TeX_( "/yy0{/nx/qual/the/yy(1)}" );@]@;

@ \Cee\ preamble. In this case, there are no `real' actions that our
grammar performs, only \TeX\ output, so this section is empty.

@<Name parser \Cee\ preamble@>=

@ \Cee\ postamble. It is tricky to insert function definitions that use \bison's internal types,
as they have to be inserted in a place that is aware of the internal definitions but before said 
definitions are used.

@<Name parser \Cee\ postamble@>=
#define YYPRINT(file, type, value)   @[yyprint (file, type, value)@]
  static void yyprint (FILE *file, int type, YYSTYPE value){}

@ Union of types.

@<Union of parser types@>=

@*1 The name scanner.
%\checktabletrue
@(ld_num_lexer.ll@>=
@G
 @> @<Lexer definitions@> @= 
%{@> @<Lexer \Cee\ preamble@> @=%}
 @> @<Lexer options@> @= 
%%
 @> @<Regular expressions@> @= 
%%
@O
void define_all_states( void ) {
    @<Collect all state definitions@>@;
}
@o
@g

@ \ifbotstrapmode\yyskipparsetrue\fi@<Lexer definitions@>=
  @<Lexer states@>@;
@G
aletter   [a-zA-Z]
wc        ([^\\\'\"-]{-}[_a-zA-Z0-9]|\\.)
id        ({aletter}|{aletter}({aletter}|[0-9])*{aletter})
int       [0-9]+
@g

@ @<Collect all state definitions@>=
#define _register_name( name ) @[Define_State( #name, name )@]
/* nothing for now */
#undef _register_name

@ Strings and characters in directives/rules.
@<Lexer states@>=
@G
%x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
@g

@ @<Lexer \Cee\ preamble@>=

#include <stdint.h>
#include <stdbool.h>

@ \yyskipparsefalse@<Lexer options@>=
@G
%option bison-bridge
%option noyywrap nounput noinput reentrant 
%option noyy_top_state
%option debug
%option stack
%option outfile="ld_num_lexer.c"
@g

@ @<Regular expressions@>=
  @<Scan white space@>@;
  @<Scan identifiers@>@;

@ White space skipping. 
\traceparserstatestrue
\tracestackstrue
\tracerulestrue
\traceactionstrue
\tracelookaheadtrue
\traceparseresultstrue
\tracebadcharstrue
\yyflexdebugtrue
%
%\prodstyle{abc123}
%
\traceparserstatesfalse
\tracestacksfalse
\tracerulesfalse
\traceactionsfalse
\tracelookaheadfalse
\traceparseresultsfalse
\tracebadcharsfalse
\yyflexdebugfalse
%
%\yyskipparsetrue
@<Scan white space@>=
@G
[ \f\n\t\v]                        {@> @[TeX_( "/yylexnext" );@]@=}
@g

@ This collection of regular expressions might seem redundant, and in
its present state, it certainly is. However, if later on the
typesetting style for some of the keywords would need to be adjusted,
such changes would be easy to implement, since the template is already
here.
%\yyskipparsefalse % this is not necessary
@<Scan identifiers@>=
@G
"%"({aletter}|[0-9]|[-_]|"%"|[<>])+ {@> @[TeX_( "/yylexreturnval{PERCENT_IDENTIFIER}" );@]@=}

"opt"                              {@> @[TeX_( "/yylexreturnval{OPTIONAL}" );@]@=}
"na"                               {@> @[TeX_( "/yylexreturnval{NO_ATTR}" );@]@=}
"ext"                              {@> @[TeX_( "/yylexreturnval{EXTENDED}" );@]@=}

[<>._\']                           {@> @[TeX_( "/yylexreturnchar" );@]@=}
{wc}                               {@> @[TeX_( "/yylexreturnval{WILDCARD}" );@]@=}

{id}                               {@> @[@<Prepare to process an identifier@>@]@=}
{int}                              {@> @[TeX_( "/yylexreturnval{INTEGER}" );@]@=}

"\""                               {@> @[TeX_( "/yylexnext" );@]@=}
.                                  {@> @[@<React to a bad character@>@]@=}
@g

@ @<Prepare to process an identifier@>=
 @[TeX_( "/yylexreturnval{IDENTIFIER}" );@]@;

@ \let\hostparsernamespace\ldnamespace @<React to a bad character@>=
 @[TeX_( "/iftracebadchars" );@]@;
 @[TeX_( "    /yycomplain{invalid character(s): /the/yytext}" );@]@;
 @[TeX_( "/fi" );@]@;
 @[TeX_( "/yylexreturn{$undefined}" );@]@;