summaryrefslogtreecommitdiff
path: root/support/splint/tex/yyparse.sty
blob: 35996923462cced7d497a5e88b53db26f0f1e553 (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
% Copyright 2012-2024, Alexander Shibakov
% This file is part of SPLinT
%
% SPLinT is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% SPLinT is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with SPLinT.  If not, see <http://www.gnu.org/licenses/>.

% global variables: current token, state, variable to store various table indices

\def\yyparse{%
  \yylen=\z@ %
  \yyinitstack\yyssa % state stack
  \yyinitstack\yyvsa % data stack
  \yystate=\z@ %
  \yychar=\YYEMPTY\relax
  \yylval{}% bogus value
  \yynewstate
}

\newif\iftracestacks
\newif\iftraceparserstates
\newif\iftracerules

\def\yynewstate{%
  \ifnum\yystate=\YYFINAL\relax
      \yybreak\yypaccept
  \else
      \yypushr\yystate\on\yyssa
      {%
          \iftraceparserstates
              \ferrmessage{^^Jstate \the\yystate\iftracestacks, \fi}%
          \fi
          \iftracestacks
              \iftraceparserstates
              \else
                  \ferrmessage{^^J}%
              \fi
              \showstack\yyssa\ferrmessage{state stack: \stackcs^^J}%
          \fi
      }%
      \yybreak\yybackup
  \yycontinue
}

\def\yybackup{%
  \yyn=\fgetelemof{yypact}\at\yystate\relax
                                    % yyn = yypact[yystate]
  \ifnum\yyn=\YYPACTNINF\relax      % if (yyn == YYPACT_NINF) (if(yypact_value_is_default(yyn)))
      \yybreak\yydefault            %     goto yydefault
  \else                             % else
      \ifnum\yychar=\YYEMPTY\relax  %     if (yychar == YYEMPTY)
                                    % ... ... if desired, this is where `hidden context' may be inserted
                                    % ... ... for example:
                                    % ... ... \gethidden\yychar
                                    % ... ... \ifnum\yychar=\YYEMPTY\relax
                                    % ... ...     \yybreak@@\yylex
                                    % ... ... \else
                                    % ... ...     \yybreak@@\yyparsetail
                                    % ... ... \fi
                                    % ... ... here \gethidden#1 is a hidden context-extracting macro that
                                    % ... ... takes a token value from a special stream and puts it in #1
                                    % ... ... filling this special stream should be done by the lexer, 
                                    % ... ... respecting any state changes required.
          \yybreak@\yylex           %         yychar = YYLEX
      \else
          \yybreak@\yyparsetail
      \fi
  \yycontinue
}

\newif\iftracelookahead

\def\yyparsetail{% this is where the lexer returns
  \ifnum\yychar>\z@\relax           % if (yychar > YYEOF)
      \yytoken=\fgetelemof{yytranslate}\at\yychar\relax
      \iftracelookahead
          \derrmessage{lookahead: \tokname, value: \the\yylval^^J}%
      \fi
                                    %     yytoken = yytranslate[yychar]
  \else                             % else
      \yychar=\z@\relax             %     yychar = yytoken = YYEOF
      \yytoken=\YYSYMBOLxYYEOF\relax
  \fi% valid lookahead token
  \advance\yyn by\yytoken           % yyn = yypact[yystate] + yytoken
  \ifnum\yyn<\z@
      \yybreak\yydefault
  \else 
      \ifnum\yyn>\YYLAST\relax
          \yybreak@\yydefault
      \else
          \ifnum\fgetelemof{yycheck}\at\yyn=\yytoken      
                                    % if (yyn >= 0 && yyn <= YYLAST &&
                                    %           yytoken == yycheck[yyn])
              \yyn=\fgetelemof{yytable}\at\yyn\relax
                                    %     yyn = yytable[ yypact[yystate] + yytoken ]
              \ifnum\yyn>\z@        %     if (yyn > 0) ...shift
                  \iftraceactions
                      \derrmessage{shifting \tokname^^J}%
                  \fi
                  \yystate=\yyn     %         yystate = yytable[ yypact[yystate] + yytoken ]
                  \yychar=\YYEMPTY\relax  
                                    %         yychar = YYEMPTY ...drop
                                    %                          ...the
                                    %                          ...shifted token 
                  \yypushr\yylval\on\yyvsa
                                    %         ...shift the value
                                    %         *++yyvsa = yylval
                  \yybreak@@@\yynewstate % new state will be shifted
              \else                 % ...if (yyn < 0)
                  \iffalse          % if (yytable_value_is_error(yyn))
                                    % /* #define yytable_value_is_error(Yytable_value) YYID (0) */
                      \yybreak@@@@\yyerr
                  \else             
                      \yyn=-\yyn
                      \yybreak@@@@\yyreduce
                  \fi
              \fi
          \else
              \yybreak@@\yydefault% goto yydefault
          \fi
      \fi
  \yycontinue
}

\newif\iftraceactions

\def\yydefault{%
  \yyn=\fgetelemof{yydefact}\at\yystate\relax
                                      % yyn = yydefact[yystate]
  \iftraceactions
      \ferrmessage{default action^^J}%
  \fi
  \ifnum\yyn=\z@
      \xskiptofi\yyerr
  \else
      \xskiptofi\yyreduce             % if (yyn != 0) goto yyreduce
  \fi
}

\def\yyreduce{%
  \yylen=\fgetelemof{yyrtwo}\at\yyn\relax
  \preparevaluestack
  \yyaction % value
  %
  \yypopstack\yyvsa\by\yylen
  \yypopstack\yyssa\by\yylen           % YYPOPSTACK(yylen)
  \yypushr\yyval\on\yyvsa 
                                       % ...push the computed
                                       % ...value on the value stack
                                       %
  \yyn=\fgetelemof{yyrone}\at\yyn\relax
                                       % yyn = yyr1[yyn]
                                       %
  \yylen\yyn                           % 
  \advance\yylen-\YYNTOKENS\relax      % ...reuse yylen
  \yystate=\fgetelemof{yypgoto}\at\yylen\relax
                                       % yystate = yypgoto[ yyn - YYNTOKENS ]
  \yyreadstackr\yyssa\at\z@\to\yyilen  % 
                                       % ...reuse yyilen
  \advance\yystate\yyilen              % yystate = yypgoto[ yyn - YYNTOKENS ] + yyssa[0]
                                       %
  \ifnum\yystate<\z@                   % if (yystate < 0)
      \yystate=\fgetelemof{yydefgoto}\at\yylen\relax 
                                       %     yystate = yydefgoto[ yyn - YYNTOKENS ]
  \else                                % else
      \ifnum\yystate>\YYLAST\relax     %     ...
          \yystate=\fgetelemof{yydefgoto}\at\yylen\relax
      \else
          \ifnum\fgetelemof{yycheck}\at\yystate=\yyilen            
                                       % if (yystate >= 0 && yystate <= YYLAST && yyssa[0] == yycheck[yystate])
                                       %
              \yystate=\fgetelemof{yytable}\at\yystate\relax
                                       %     yystate = yytable[yystate]
          \else
              \yystate=\fgetelemof{yydefgoto}\at\yylen\relax
          \fi
      \fi
  \fi
  \yylen=\z@
  \yynewstate
}

\def\preparevaluestack{%
  \iftracerules
      \ferrmessage{^^Jreducing (rule \the\yyn)}\printrule{\yyn}%
  \fi
  {%
       \iftracestacks
           \iftracerules
           \else
               \ferrmessage{^^J}%
           \fi
           \showstack\yyssa
           \ferrmessage{stack: \stackcs^^Jpopping \the\yylen^^J}%
       \fi
  }%
  \yyilen=\fgetelemof{yyrthree}\at\yyn\relax
  \ifnum\yyilen>\z@ % this is an inline rule
      \ifnum\yylen>\z@
          \errmessage{an inline rule with a nontrivial left hand side: rule \the\yyn}%
      \fi
  \else
      \yyilen\yylen
  \fi
  \ifnum\yyilen>\z@
      \yypeekvstack\yyvsa\upto\yyilen % see yymisc.sty for the definition
      \yyval\csname $$'1\endcsname % $$ = $1
  \else
      \yypeekustack\yyvsa 
  \fi
}

\newif\iftraceparseresults

\def\yypaccept{%
    \iftraceparseresults
        \ferrmessage{accepted^^J}%
    \fi
    \finishparse
}

% most of the error processing mechanisms supplied by the `genuine' \bison\ parsers
% have been omitted for simplicity

\def\yyerr{%
    \iftraceparseresults
        \ferrmessage{(parse) error^^J}%
    \fi
    \cleanupparse
}

% no symbolic name support by default

\let\yysymswitch\eatone
\let\yysymcleanup\eatone

% parser specific output message: defines the current token name to output

\def\derrmessage#1{{%
  \newlinechar=`\^^J%
  \edef\tokname{\fgetelemof{yytname}\at\yytoken}%
  \ferrmessage{#1}%
}}

% these macros reuse dedicated counters ... locally

\def\printrule#1{{% some values are computed several times to save on register assignments
                  % this has more to do with not polluting the namespace than speed
    \edef\ruleline{\space\space\space\fgetelemof{yytname}\at{\fgetelemof{yyrone}\at{#1}}:}%
    \yyn=\fgetelemof{yyprhs}\at{#1}
    \ifnum\fgetelemof{yyrhs}\at\yyn>\m@ne
        \loop
            \edef\ruleline{\ruleline\space\fgetelemof{yytname}\at{\fgetelemof{yyrhs}\at\yyn}}%
            \advance\yyn\@ne
        \ifnum\fgetelemof{yyrhs}\at\yyn>\m@ne
        \repeat
    \else
        \edef\ruleline{\ruleline\space<empty>}%
    \fi
    \ferrmessage{ -->\ruleline}%
}}

% print the rule without using yyprhs and yyrhs (necessary if using
% bison version 3.0 or above)

\def\printrule#1{{%
    \edef\ruleline{\space\space\space\fgetelemof{yytname}\at{\fgetelemof{yyrone}\at{#1}}:}%
    \ifnum\yylen>\z@
        \yypeeksstack\yyssa\upto\yylen\withprefix{\appendtoruleline}%
    \else
        \edef\ruleline{\ruleline\space<empty>}%
    \fi
    \ferrmessage{ -->\ruleline}%
}}

\def\appendtoruleline#1{%
    \edef\ruleline{\ruleline\space\fgetelemof{yytname}\at{\fgetelemof{yystos}\at{#1}}}%
}

% the two control sequences below are replaced in yyfaststack.sty 
% to match the appropriate stack implementation 

\def\showstacksym#1{%
  \edef\stackcs{\stackcs^^J \space\space\fgetelemof{yytname}\at{\fgetelemof{yystos}\at{#1}}}%
}

\def\showstack#1{%
    \def\stackcs{}%
    \let\sts\showstacksym
    #1%
}

\newif\iftracediscardedinput

\long\def\cleanupparse#1\endparse{%
    \iftracediscardedinput
        {\toksa{#1}\immediate\write16{discarding the rest of the input: \the\toksa}}%
    \fi
    \yyerror
}

\newif\ifyyparsefail

\def\yyerror{\yyparsefailtrue}

\long\def\finishparse#1\endparseinput{%
    \yystringempty{#1}{}{\yylval{#1}\immediate\write16{skipping: \the\yylval}}%
}
\let\endparse\relax
\let\endparseinput\relax

% action code

\def\yyaction{%
    \yysymswitch{\yyn}% setting symbol names
    % the big switch, set \yyval
    \yybigswitch{\yyn}%
    %
    \yysymcleanup{\yyn}% removing symbol names from the namespace
}

% common parser initializations; note that some initializations (such as resetting the data
% and state stacks) are done at parser startup (see the definition of \yyparse)

\def\basicparserinit{%
    \yytext{}%
    \yysubtext{}%
    \yytext@{}%
    \yytextpure{}%
    \yytextseenpure{}%
    \yytextseen{}%
    \yytext@seen{}%
    \yybyte{}%
    \initlist\yystash
    \appendtolist\yystash{}% increment the list counter
    \initlist\yyformat
    \appendtolist\yyformat{}% increment the list counter
    \yyfutureyytext{}%
    \yyinitstack\yystatestack
    \yyfmark=\z@
    \yysmark=\z@
    \yyfmarklast=\z@
    \yyfmark@accept=\z@
    \formatmarker=\z@
    \yysmarklast=\z@
    \yysmark@accept=\z@
    \stashmarker=\z@
    \yytextbackupfalse
    \yyg@yyinit=\z@
    \yyg@yystart=\z@
    \YYATBOL=\@ne
    \yyparsefailfalse
    \YYEOBLASTMATCHtrue
}