-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbibtex.y.go
645 lines (576 loc) · 14 KB
/
bibtex.y.go
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
// Code generated by goyacc -p bibtex -o bibtex.y.go bibtex.y. DO NOT EDIT.
//line bibtex.y:2
package bibtex
import __yyfmt__ "fmt"
//line bibtex.y:2
import (
"io"
)
type bibTag struct {
key string
val BibString
}
var bib *BibTex // Only for holding current bib
//line bibtex.y:16
type bibtexSymType struct {
yys int
bibtex *BibTex
strval string
bibentry *BibEntry
bibtag *bibTag
bibtags []*bibTag
strings BibString
}
const tCOMMENT = 57346
const tSTRING = 57347
const tPREAMBLE = 57348
const tATSIGN = 57349
const tCOLON = 57350
const tEQUAL = 57351
const tCOMMA = 57352
const tPOUND = 57353
const tLBRACE = 57354
const tRBRACE = 57355
const tDQUOTE = 57356
const tLPAREN = 57357
const tRPAREN = 57358
const tBAREIDENT = 57359
const tIDENT = 57360
const tCOMMENTBODY = 57361
var bibtexToknames = [...]string{
"$end",
"error",
"$unk",
"tCOMMENT",
"tSTRING",
"tPREAMBLE",
"tATSIGN",
"tCOLON",
"tEQUAL",
"tCOMMA",
"tPOUND",
"tLBRACE",
"tRBRACE",
"tDQUOTE",
"tLPAREN",
"tRPAREN",
"tBAREIDENT",
"tIDENT",
"tCOMMENTBODY",
}
var bibtexStatenames = [...]string{}
const bibtexEofCode = 1
const bibtexErrCode = 2
const bibtexInitialStackSize = 16
//line bibtex.y:75
// Parse is the entry point to the bibtex parser.
func Parse(r io.Reader) (*BibTex, error) {
l := newLexer(r)
bibtexParse(l)
switch {
case len(l.Errors) > 0: // Non-yacc errors
return nil, l.Errors[0]
case len(l.ParseErrors) > 0:
return nil, l.ParseErrors[0]
default:
return bib, nil
}
}
//line yacctab:1
var bibtexExca = [...]int{
-1, 1,
1, -1,
-2, 0,
}
const bibtexPrivate = 57344
const bibtexLast = 54
var bibtexAct = [...]int{
23, 14, 35, 34, 9, 10, 11, 25, 24, 41,
40, 36, 43, 22, 21, 32, 20, 8, 45, 26,
33, 17, 19, 15, 18, 12, 16, 32, 13, 47,
38, 39, 37, 32, 43, 46, 32, 42, 31, 32,
28, 27, 44, 30, 29, 49, 48, 7, 4, 1,
6, 5, 3, 2,
}
var bibtexPact = [...]int{
-1000, -1000, 40, -1000, -1000, -1000, -1000, 0, 13, -18,
11, 9, 5, -1, -1000, -3, -4, -10, -10, 31,
30, 35, 34, 25, -1000, -1000, 4, -6, -6, -10,
-10, -1000, -8, -1000, 24, -1000, 33, 2, 22, 16,
-1000, -1000, -1000, -6, -10, -1000, -1000, -1000, -1000, 28,
}
var bibtexPgo = [...]int{
0, 53, 52, 2, 51, 3, 0, 50, 49, 48,
}
var bibtexR1 = [...]int{
0, 8, 1, 1, 1, 1, 1, 2, 2, 9,
4, 4, 7, 7, 6, 6, 6, 6, 3, 3,
5, 5,
}
var bibtexR2 = [...]int{
0, 1, 0, 2, 2, 2, 2, 7, 7, 3,
7, 7, 5, 5, 1, 1, 3, 3, 0, 3,
1, 3,
}
var bibtexChk = [...]int{
-1000, -8, -1, -2, -9, -4, -7, 7, 17, 4,
5, 6, 12, 15, 19, 12, 15, 12, 15, 17,
17, 17, 17, -6, 18, 17, -6, 10, 10, 9,
9, 13, 11, 16, -5, -3, 17, -5, -6, -6,
18, 17, 13, 10, 9, 16, 13, 13, -3, -6,
}
var bibtexDef = [...]int{
2, -2, 1, 3, 4, 5, 6, 0, 0, 0,
0, 0, 0, 0, 9, 0, 0, 0, 0, 0,
0, 0, 0, 0, 14, 15, 0, 18, 18, 0,
0, 12, 0, 13, 0, 20, 0, 0, 0, 0,
16, 17, 7, 18, 0, 8, 10, 11, 21, 19,
}
var bibtexTok1 = [...]int{
1,
}
var bibtexTok2 = [...]int{
2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19,
}
var bibtexTok3 = [...]int{
0,
}
var bibtexErrorMessages = [...]struct {
state int
token int
msg string
}{}
//line yaccpar:1
/* parser for yacc output */
var (
bibtexDebug = 0
bibtexErrorVerbose = false
)
type bibtexLexer interface {
Lex(lval *bibtexSymType) int
Error(s string)
}
type bibtexParser interface {
Parse(bibtexLexer) int
Lookahead() int
}
type bibtexParserImpl struct {
lval bibtexSymType
stack [bibtexInitialStackSize]bibtexSymType
char int
}
func (p *bibtexParserImpl) Lookahead() int {
return p.char
}
func bibtexNewParser() bibtexParser {
return &bibtexParserImpl{}
}
const bibtexFlag = -1000
func bibtexTokname(c int) string {
if c >= 1 && c-1 < len(bibtexToknames) {
if bibtexToknames[c-1] != "" {
return bibtexToknames[c-1]
}
}
return __yyfmt__.Sprintf("tok-%v", c)
}
func bibtexStatname(s int) string {
if s >= 0 && s < len(bibtexStatenames) {
if bibtexStatenames[s] != "" {
return bibtexStatenames[s]
}
}
return __yyfmt__.Sprintf("state-%v", s)
}
func bibtexErrorMessage(state, lookAhead int) string {
const TOKSTART = 4
if !bibtexErrorVerbose {
return "syntax error"
}
for _, e := range bibtexErrorMessages {
if e.state == state && e.token == lookAhead {
return "syntax error: " + e.msg
}
}
res := "syntax error: unexpected " + bibtexTokname(lookAhead)
// To match Bison, suggest at most four expected tokens.
expected := make([]int, 0, 4)
// Look for shiftable tokens.
base := bibtexPact[state]
for tok := TOKSTART; tok-1 < len(bibtexToknames); tok++ {
if n := base + tok; n >= 0 && n < bibtexLast && bibtexChk[bibtexAct[n]] == tok {
if len(expected) == cap(expected) {
return res
}
expected = append(expected, tok)
}
}
if bibtexDef[state] == -2 {
i := 0
for bibtexExca[i] != -1 || bibtexExca[i+1] != state {
i += 2
}
// Look for tokens that we accept or reduce.
for i += 2; bibtexExca[i] >= 0; i += 2 {
tok := bibtexExca[i]
if tok < TOKSTART || bibtexExca[i+1] == 0 {
continue
}
if len(expected) == cap(expected) {
return res
}
expected = append(expected, tok)
}
// If the default action is to accept or reduce, give up.
if bibtexExca[i+1] != 0 {
return res
}
}
for i, tok := range expected {
if i == 0 {
res += ", expecting "
} else {
res += " or "
}
res += bibtexTokname(tok)
}
return res
}
func bibtexlex1(lex bibtexLexer, lval *bibtexSymType) (char, token int) {
token = 0
char = lex.Lex(lval)
if char <= 0 {
token = bibtexTok1[0]
goto out
}
if char < len(bibtexTok1) {
token = bibtexTok1[char]
goto out
}
if char >= bibtexPrivate {
if char < bibtexPrivate+len(bibtexTok2) {
token = bibtexTok2[char-bibtexPrivate]
goto out
}
}
for i := 0; i < len(bibtexTok3); i += 2 {
token = bibtexTok3[i+0]
if token == char {
token = bibtexTok3[i+1]
goto out
}
}
out:
if token == 0 {
token = bibtexTok2[1] /* unknown char */
}
if bibtexDebug >= 3 {
__yyfmt__.Printf("lex %s(%d)\n", bibtexTokname(token), uint(char))
}
return char, token
}
func bibtexParse(bibtexlex bibtexLexer) int {
return bibtexNewParser().Parse(bibtexlex)
}
func (bibtexrcvr *bibtexParserImpl) Parse(bibtexlex bibtexLexer) int {
var bibtexn int
var bibtexVAL bibtexSymType
var bibtexDollar []bibtexSymType
_ = bibtexDollar // silence set and not used
bibtexS := bibtexrcvr.stack[:]
Nerrs := 0 /* number of errors */
Errflag := 0 /* error recovery flag */
bibtexstate := 0
bibtexrcvr.char = -1
bibtextoken := -1 // bibtexrcvr.char translated into internal numbering
defer func() {
// Make sure we report no lookahead when not parsing.
bibtexstate = -1
bibtexrcvr.char = -1
bibtextoken = -1
}()
bibtexp := -1
goto bibtexstack
ret0:
return 0
ret1:
return 1
bibtexstack:
/* put a state and value onto the stack */
if bibtexDebug >= 4 {
__yyfmt__.Printf("char %v in %v\n", bibtexTokname(bibtextoken), bibtexStatname(bibtexstate))
}
bibtexp++
if bibtexp >= len(bibtexS) {
nyys := make([]bibtexSymType, len(bibtexS)*2)
copy(nyys, bibtexS)
bibtexS = nyys
}
bibtexS[bibtexp] = bibtexVAL
bibtexS[bibtexp].yys = bibtexstate
bibtexnewstate:
bibtexn = bibtexPact[bibtexstate]
if bibtexn <= bibtexFlag {
goto bibtexdefault /* simple state */
}
if bibtexrcvr.char < 0 {
bibtexrcvr.char, bibtextoken = bibtexlex1(bibtexlex, &bibtexrcvr.lval)
}
bibtexn += bibtextoken
if bibtexn < 0 || bibtexn >= bibtexLast {
goto bibtexdefault
}
bibtexn = bibtexAct[bibtexn]
if bibtexChk[bibtexn] == bibtextoken { /* valid shift */
bibtexrcvr.char = -1
bibtextoken = -1
bibtexVAL = bibtexrcvr.lval
bibtexstate = bibtexn
if Errflag > 0 {
Errflag--
}
goto bibtexstack
}
bibtexdefault:
/* default state action */
bibtexn = bibtexDef[bibtexstate]
if bibtexn == -2 {
if bibtexrcvr.char < 0 {
bibtexrcvr.char, bibtextoken = bibtexlex1(bibtexlex, &bibtexrcvr.lval)
}
/* look through exception table */
xi := 0
for {
if bibtexExca[xi+0] == -1 && bibtexExca[xi+1] == bibtexstate {
break
}
xi += 2
}
for xi += 2; ; xi += 2 {
bibtexn = bibtexExca[xi+0]
if bibtexn < 0 || bibtexn == bibtextoken {
break
}
}
bibtexn = bibtexExca[xi+1]
if bibtexn < 0 {
goto ret0
}
}
if bibtexn == 0 {
/* error ... attempt to resume parsing */
switch Errflag {
case 0: /* brand new error */
bibtexlex.Error(bibtexErrorMessage(bibtexstate, bibtextoken))
Nerrs++
if bibtexDebug >= 1 {
__yyfmt__.Printf("%s", bibtexStatname(bibtexstate))
__yyfmt__.Printf(" saw %s\n", bibtexTokname(bibtextoken))
}
fallthrough
case 1, 2: /* incompletely recovered error ... try again */
Errflag = 3
/* find a state where "error" is a legal shift action */
for bibtexp >= 0 {
bibtexn = bibtexPact[bibtexS[bibtexp].yys] + bibtexErrCode
if bibtexn >= 0 && bibtexn < bibtexLast {
bibtexstate = bibtexAct[bibtexn] /* simulate a shift of "error" */
if bibtexChk[bibtexstate] == bibtexErrCode {
goto bibtexstack
}
}
/* the current p has no shift on "error", pop stack */
if bibtexDebug >= 2 {
__yyfmt__.Printf("error recovery pops state %d\n", bibtexS[bibtexp].yys)
}
bibtexp--
}
/* there is no state on the stack with an error shift ... abort */
goto ret1
case 3: /* no shift yet; clobber input char */
if bibtexDebug >= 2 {
__yyfmt__.Printf("error recovery discards %s\n", bibtexTokname(bibtextoken))
}
if bibtextoken == bibtexEofCode {
goto ret1
}
bibtexrcvr.char = -1
bibtextoken = -1
goto bibtexnewstate /* try again in the same state */
}
}
/* reduction by production bibtexn */
if bibtexDebug >= 2 {
__yyfmt__.Printf("reduce %v in:\n\t%v\n", bibtexn, bibtexStatname(bibtexstate))
}
bibtexnt := bibtexn
bibtexpt := bibtexp
_ = bibtexpt // guard against "declared and not used"
bibtexp -= bibtexR2[bibtexn]
// bibtexp is now the index of $0. Perform the default action. Iff the
// reduced production is ε, $1 is possibly out of range.
if bibtexp+1 >= len(bibtexS) {
nyys := make([]bibtexSymType, len(bibtexS)*2)
copy(nyys, bibtexS)
bibtexS = nyys
}
bibtexVAL = bibtexS[bibtexp+1]
/* consult goto table to find next state */
bibtexn = bibtexR1[bibtexn]
bibtexg := bibtexPgo[bibtexn]
bibtexj := bibtexg + bibtexS[bibtexp].yys + 1
if bibtexj >= bibtexLast {
bibtexstate = bibtexAct[bibtexg]
} else {
bibtexstate = bibtexAct[bibtexj]
if bibtexChk[bibtexstate] != -bibtexn {
bibtexstate = bibtexAct[bibtexg]
}
}
// dummy call; replaced with literal code
switch bibtexnt {
case 1:
bibtexDollar = bibtexS[bibtexpt-1 : bibtexpt+1]
//line bibtex.y:36
{
}
case 2:
bibtexDollar = bibtexS[bibtexpt-0 : bibtexpt+1]
//line bibtex.y:39
{
bibtexVAL.bibtex = NewBibTex()
bib = bibtexVAL.bibtex
}
case 3:
bibtexDollar = bibtexS[bibtexpt-2 : bibtexpt+1]
//line bibtex.y:40
{
bibtexVAL.bibtex = bibtexDollar[1].bibtex
bibtexVAL.bibtex.AddEntry(bibtexDollar[2].bibentry)
}
case 4:
bibtexDollar = bibtexS[bibtexpt-2 : bibtexpt+1]
//line bibtex.y:41
{
bibtexVAL.bibtex = bibtexDollar[1].bibtex
}
case 5:
bibtexDollar = bibtexS[bibtexpt-2 : bibtexpt+1]
//line bibtex.y:42
{
bibtexVAL.bibtex = bibtexDollar[1].bibtex
bibtexVAL.bibtex.AddStringVar(bibtexDollar[2].bibtag.key, bibtexDollar[2].bibtag.val)
}
case 6:
bibtexDollar = bibtexS[bibtexpt-2 : bibtexpt+1]
//line bibtex.y:43
{
bibtexVAL.bibtex = bibtexDollar[1].bibtex
bibtexVAL.bibtex.AddPreamble(bibtexDollar[2].strings)
}
case 7:
bibtexDollar = bibtexS[bibtexpt-7 : bibtexpt+1]
//line bibtex.y:46
{
bibtexVAL.bibentry = NewBibEntry(bibtexDollar[2].strval, bibtexDollar[4].strval)
for _, t := range bibtexDollar[6].bibtags {
bibtexVAL.bibentry.AddField(t.key, t.val)
}
}
case 8:
bibtexDollar = bibtexS[bibtexpt-7 : bibtexpt+1]
//line bibtex.y:47
{
bibtexVAL.bibentry = NewBibEntry(bibtexDollar[2].strval, bibtexDollar[4].strval)
for _, t := range bibtexDollar[6].bibtags {
bibtexVAL.bibentry.AddField(t.key, t.val)
}
}
case 9:
bibtexDollar = bibtexS[bibtexpt-3 : bibtexpt+1]
//line bibtex.y:50
{
}
case 10:
bibtexDollar = bibtexS[bibtexpt-7 : bibtexpt+1]
//line bibtex.y:53
{
bibtexVAL.bibtag = &bibTag{key: bibtexDollar[4].strval, val: bibtexDollar[6].strings}
}
case 11:
bibtexDollar = bibtexS[bibtexpt-7 : bibtexpt+1]
//line bibtex.y:54
{
bibtexVAL.bibtag = &bibTag{key: bibtexDollar[4].strval, val: bibtexDollar[6].strings}
}
case 12:
bibtexDollar = bibtexS[bibtexpt-5 : bibtexpt+1]
//line bibtex.y:57
{
bibtexVAL.strings = bibtexDollar[4].strings
}
case 13:
bibtexDollar = bibtexS[bibtexpt-5 : bibtexpt+1]
//line bibtex.y:58
{
bibtexVAL.strings = bibtexDollar[4].strings
}
case 14:
bibtexDollar = bibtexS[bibtexpt-1 : bibtexpt+1]
//line bibtex.y:61
{
bibtexVAL.strings = NewBibConst(bibtexDollar[1].strval)
}
case 15:
bibtexDollar = bibtexS[bibtexpt-1 : bibtexpt+1]
//line bibtex.y:62
{
bibtexVAL.strings = bib.GetStringVar(bibtexDollar[1].strval)
}
case 16:
bibtexDollar = bibtexS[bibtexpt-3 : bibtexpt+1]
//line bibtex.y:63
{
bibtexVAL.strings = NewBibComposite(bibtexDollar[1].strings)
bibtexVAL.strings.(*BibComposite).Append(NewBibConst(bibtexDollar[3].strval))
}
case 17:
bibtexDollar = bibtexS[bibtexpt-3 : bibtexpt+1]
//line bibtex.y:64
{
bibtexVAL.strings = NewBibComposite(bibtexDollar[1].strings)
bibtexVAL.strings.(*BibComposite).Append(bib.GetStringVar(bibtexDollar[3].strval))
}
case 18:
bibtexDollar = bibtexS[bibtexpt-0 : bibtexpt+1]
//line bibtex.y:67
{
}
case 19:
bibtexDollar = bibtexS[bibtexpt-3 : bibtexpt+1]
//line bibtex.y:68
{
bibtexVAL.bibtag = &bibTag{key: bibtexDollar[1].strval, val: bibtexDollar[3].strings}
}
case 20:
bibtexDollar = bibtexS[bibtexpt-1 : bibtexpt+1]
//line bibtex.y:71
{
if bibtexDollar[1].bibtag != nil {
bibtexVAL.bibtags = []*bibTag{bibtexDollar[1].bibtag}
}
}
case 21:
bibtexDollar = bibtexS[bibtexpt-3 : bibtexpt+1]
//line bibtex.y:72
{
if bibtexDollar[3].bibtag == nil {
bibtexVAL.bibtags = bibtexDollar[1].bibtags
} else {
bibtexVAL.bibtags = append(bibtexDollar[1].bibtags, bibtexDollar[3].bibtag)
}
}
}
goto bibtexstack /* stack new state and value */
}