Skip to content

Commit 6f50ce3

Browse files
Add BISON-style location tracking.
1 parent f37dae1 commit 6f50ce3

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

cmd/goyacc/yacc.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ var nerrors = 0 // number of errors
258258
// assigned token type values
259259

260260
var extval = 0
261+
var firsttok = 0
262+
var lasttok = 0
261263

262264
// grammar rule information
263265

@@ -698,6 +700,12 @@ outer:
698700
// non-literals
699701
if !tokset[i].noconst {
700702
fmt.Fprintf(ftable, "const %v = %v\n", tokset[i].name, tokset[i].value)
703+
if firsttok == 0 {
704+
firsttok = tokset[i].value
705+
}
706+
if lasttok == 0 && i == ntokens {
707+
lasttok = tokset[i].value
708+
}
701709
}
702710
}
703711

@@ -709,6 +717,11 @@ outer:
709717
}
710718
fmt.Fprintf(ftable, "}\n")
711719

720+
fmt.Fprintf(ftable, "\n")
721+
fmt.Fprintf(ftable, "var %sFirsttok = %d\n", prefix, firsttok)
722+
fmt.Fprintf(ftable, "var %sLasttok = %d\n", prefix, lasttok)
723+
fmt.Fprintf(ftable, "\n")
724+
712725
// put out names of states.
713726
// commented out to avoid a huge table just for debugging.
714727
// re-enable to have the names in the binary.
@@ -3296,9 +3309,14 @@ func $$NewParser() $$Parser {
32963309
const $$Flag = -1000
32973310
32983311
func $$Tokname(c int) string {
3299-
if c >= 1 && c-1 < len($$Toknames) {
3300-
if $$Toknames[c-1] != "" {
3301-
return $$Toknames[c-1]
3312+
offset := 1
3313+
if c >= $$Private {
3314+
offset = $$Private-1
3315+
}
3316+
3317+
if c >= offset && c-offset < len($$Toknames) {
3318+
if $$Toknames[c-offset] != "" {
3319+
return $$Toknames[c-offset]
33023320
}
33033321
}
33043322
return __yyfmt__.Sprintf("tok-%v", c)

0 commit comments

Comments
 (0)