summaryrefslogtreecommitdiff
path: root/support/splint/examples/ld/ldnp.w
blob: 9a7d86f097aad494f684a10ed1a99a18011af953 (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
@q Copyright 2012-2022, 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;
@(ld_small_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(t)
\genericparser 
    name: ldsmall, 
    ptables: ld_small_tab.tex,
    ltables: ld_small_dfa.tex,
    tokens: {},
    asetup: {}, 
    dsetup: {}, 
    rsetup: \let\returnexplicitspace\ignoreexplicitspace, % ignore spaces in names
    optimization: {};%
\let\otosmallparser\tosmallparser % /* save the old name parser */
\let\tosmallparser\toldsmallparser
\expandafter\let\csname to\stripbrackets\cwebclinknamespace parser\endcsname\tosmallparser % 
/* make the name parser handle the typesetting of \Cee\ variables */
@g

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

@ @<Token and types declarations@>=
@G
%token PERCENT_IDENTIFIER
%token IDENTIFIER
%token OPTIONAL
%token K_SUFFIX
%token INTEGER
%token EXTENDED
%token WILDCARD
%token META_IDENTIFIER
@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}.
%\tracebadnamestrue 
%\tracenamestrue
%\traceparserstatestrue
%\tracestackstrue
%\tracerulestrue
%\traceactionstrue
%\tracelookaheadtrue
%\traceparseresultstrue
%\tracebadcharstrue
%\yyflexdebugtrue
@<Parser productions@>=
@G
full_name:
  identifier_string suffixes.opt                {@> @<Compose the full name@> @=}
| qualifier '_' identifier_string suffixes.opt  {@> @<Compose a qualified name@> @=}
| META_IDENTIFIER  {@> @<Turn a \prodstylens{META\_IDENTIFIER}{\ldsmallnamespace} into a full name@> @=}
| '\''                                          {@> @<Make \prodstyle{'} into a name@> @=}
;

identifier_string:
   PERCENT_IDENTIFIER                           {@> @<Attach option name@> @=}  
|  IDENTIFIER                                   {@> @<Start with an identifier@> @=}
|  '\'' WILDCARD '\''                           {@> @<Start with a quoted string@> @=}
|  '\'' '_' '\''                                {@> @<Start with a \prodstyle{'\_'} string@> @=}
|  '\'' '.' '\''                                {@> @<Start with a \prodstyle{'.'} string@> @=}
@t}\vb{\flatten}{@>
|  incomplete_identifier_string {} IDENTIFIER   {@> @<Attach an identifier@> @=}
;
@t}\vb{\resetf}{@>
incomplete_identifier_string:
   '_'                                          {@> TeX_( "/yy0{/nx/idstr{}{}}" );@=}
|  identifier_string '_'                        {@> TeX_( "/yy0{/the/yy(1)}" ); @=}
|  qualified_identifier_string '_'              {@> TeX_( "/yy0{/the/yy(1)}" ); @=}
;

qualified_identifier_string:
   identifier_string '_' qualifier              {@> @<Attach qualifier to a name@> @=}
|  qualified_identifier_string '_' qualifier    {@> @<Attach qualifier to a name@> @=}
;

suffixes.opt:
                                  {@> TeX_( "/yy0{}" ); @=}
|  '.'                            {@> TeX_( "/yy0{/nx/dotsp/nx/sfxnone}" ); @=}
|  '.' suffixes                   {@> @<Attach suffixes@> @=}
|  '.' qualified_suffixes         {@> @<Attach qualified suffixes@> @=}
|  INTEGER                        {@> @<Attach an integer@> @=}
|  '_' INTEGER                    {@> @<Attach a subscripted integer@> @=}
|  '_' qualifier                  {@> @<Attach a subscripted qualifier@> @=}
;

suffixes:
   IDENTIFIER                     {@> @<Start with a named suffix@> @=}
|  INTEGER                        {@> @<Start with a numeric suffix@> @=}
|  suffixes '.'                   {@> @<Add a dot separator@> @=}
|  suffixes IDENTIFIER            {@> @<Attach a named suffix@> @=}
|  suffixes INTEGER               {@> @<Attach integer suffix@> @=}
|  qualifier '.'                  {@> TeX_( "/yy0{/nx/sfxn/the/yy(1)/nx/dotsp}" ); @=}
|  suffixes qualifier '.'         {@> TeX_( "/yy0{/the/yy(1)/nx/sfxn/the/yy(2)/nx/dotsp}" ); @=}
;

qualified_suffixes:
  suffixes qualifier              {@> @<Attach a qualifier@> @=}
| qualifier                       {@> @<Start suffixes with a qualifier@> @=}
;

qualifier:
  OPTIONAL                        {@> TeX_( "/yy0{/the/yy(1)}" ); @=}
| K_SUFFIX                        {@> TeX_( "/yy0{/the/yy(1)}" ); @=}
| EXTENDED                        {@> TeX_( "/yy0{/the/yy(1)}" ); @=}
;
@g

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

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

@ @<Turn a \prodstylens{META\_IDENTIFIER}{\ldsmallnamespace} into a full name@>=
  @[TeX_( "/getfirst{/yy(1)}/to/toksa" );@]@;
  @[TeX_( "/getsecond{/yy(1)}/to/toksb" );@]@;
  @[TeX_( "/yy0{/nx/idstr{/the/toksa}{/the/toksb}}/namechars/yyval" );@]@;

@ @<Make \prodstyle{'} into a name@>=
  @[TeX_( "/yy0{/nx/chstr{'}{'}}/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 quoted string@>=
  @[TeX_( "/getfirst{/yy(2)}/to/toksa" );@]@;
  @[TeX_( "/getsecond{/yy(2)}/to/toksb" );@]@;
  @[TeX_( "/yy0{/nx/visflag{/nx/termvstring}{}/nx/chstr{/the/toksa}{/the/toksb}}" );@]@;

@ @<Start with a \prodstyle{'\_'} string@>=
  @[TeX_( "/yy0{/nx/visflag{/nx/termvstring}{}/nx/chstr{/nx/_}{_}}" );@]@;

@ @<Start with a \prodstyle{'.'} string@>=
  @[TeX_( "/yy0{/nx/visflag{/nx/termvstring}{}/nx/chstr{.}{.}}" );@]@;

@ @<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_( "/getfirst{/yy(2)}/to/toksa/getsecond{/yy(2)}/to/toksb" );@]@;
  @[TeX_( "/yy0{/nx/dotsp/nx/sfxi{/nx/_/the/toksa}{/uscoreletter/the/toksb}}" );@]@;
                               
@ @<Attach a subscripted qualifier@>=
  @[TeX_( "/getfirst{/yy(2)}/to/toksa/getsecond{/yy(2)}/to/toksb" );@]@;
  @[TeX_( "/yy0{/nx/dotsp/nx/qual{/nx/_/the/toksa}{/uscoreletter/the/toksb}}" );@]@;
                               
@ @<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@>=

@ Union of types.

@<Union of parser types@>=

@*1 The name scanner. The scanner for \ld\ name parser is essentially identical to that for the
\bison\ and \flex\ name parser\footnote{And is just as much of an overkill. This should serve as a
cautionary tale of how suboptimal but convenient choices tend to take root.}. The only exception
is the scanning of suffixes.
%\checktabletrue
@(ld_small_lexer.ll@>=
@G
 @> @<Lexer definitions@> @= 
%{@> @<Lexer \Cee\ preamble@> @=%}
 @> @<Lexer options@> @= 
%%
 @> @<Regular expressions for the name parser@> @= 
%%
@O
void define_all_states( void ) {
    @<Collect all state definitions@>@;
}
@o
@g
@t}\ifbootstrapmode\yyskipparsetrue\fi{@>

@ @<Lexer definitions@>=
  @<Lexer states@>@;
@G(fs1)
aletter   [a-zA-Z]
letter    (_|{aletter})
wc        ([^\\\'\"]{-}[_a-zA-Z0-9]|\\.)
id        ({aletter}|{aletter}({aletter}|[0-9])*{aletter})
id_strict {letter}(({letter}|[-0-9])*{letter})?
meta_id   "*"{id_strict}"*"?
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(fs1)
%x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
@g

@ @<Lexer \Cee\ preamble@>=
#include <stdint.h>
#include <stdbool.h>@t}\yyskipparsefalse{@>

@ @<Lexer options@>=
@G(fs1)
%option bison-bridge
%option noyywrap nounput noinput reentrant 
%option noyy_top_state
%option debug
%option stack
%option outfile="ld_small_lexer.c"
@g

@ @<Regular expressions for the name parser@>=
  @<Scan white space@>@;
  @<Scan identifiers@>@;

@ White space is skipped in names\footnote{How it finds its way {\em into\/} a name deserves
some investigation for which we do not have the space here.}.
Note that the input routine produced by \splint\ springs into action
{\em after\/} the input passes through the scanning mechanism of \TeX\ which makes it very unlikely
that any of the characters below in the definition of `whitespace' could make it to the scanner.
They are left in, however, in case a custom input routine is used that produces such
characters\footnote{Although, in this case, some special processing by the scanner
would likely be required.}.
\traceparserstatestrue
\tracestackstrue
\tracerulestrue
\traceactionstrue
\tracelookaheadtrue
\traceparseresultstrue
\tracebadcharstrue
\yyflexdebugtrue
%
%\prodstyle{abc123}
%
\traceparserstatesfalse
\tracestacksfalse
\tracerulesfalse
\traceactionsfalse
\tracelookaheadfalse
\traceparseresultsfalse
\tracebadcharsfalse
\yyflexdebugfalse
%
@<Scan white space@>=
@G(fs2)
[ \f\n\t\v]                        {@> @[TeX_( "/yylexnext" );@]@=}
@g

@ Suffixes specific to \ld\ name scanner are defined here, along with some other
lexical elements. Most of these definitions mimic the ones for the \bison\ name
scanner.
@<Scan identifiers@>=
@G(fs2)
"%"({aletter}|[0-9]|[-_]|"%"|[<>])+ {@> @[TeX_( "/yylexreturnval{PERCENT_IDENTIFIER}" );@]@=}

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

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

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

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

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

@ @<Prepare to process a meta-identifier@>=
 @[TeX_( "/yylexreturnval{META_IDENTIFIER}" );@]@;

@ \let\hostparsernamespace\ldnamespace\yyskipparsefalse A simple routine to detect
trivial scanning problems.
@<React to a bad character@>=
 @[TeX_( "/iftracebadchars" );@]@;
 @[TeX_( "    /yycomplain{invalid character(s): /the/yytext}" );@]@;
 @[TeX_( "/fi" );@]@;
 @[TeX_( "/yyerrterminate" );@]@;