Commit bcdcf395 authored by Roger Peppe's avatar Roger Peppe Committed by Ken Thompson

Add Error member to yyLexer type (yyError

has no access to yylex)

R=ken2, ken3
CC=golang-dev
https://golang.org/cl/813047
parent dbb62327
...@@ -24,10 +24,12 @@ argument that conforms to the following interface: ...@@ -24,10 +24,12 @@ argument that conforms to the following interface:
type yyLexer interface { type yyLexer interface {
Lex(lval *yySymType) int Lex(lval *yySymType) int
Error(e string)
} }
Lex should return the token identifier, and place other token Lex should return the token identifier, and place other token
information in lval (which replaces the usual yylval). information in lval (which replaces the usual yylval).
Error is equivalent to yyerror in the original yacc.
Code inside the parser may refer to the variable yylex Code inside the parser may refer to the variable yylex
which holds the yyLexer passed to Parse. which holds the yyLexer passed to Parse.
......
...@@ -3080,6 +3080,7 @@ var yyDebug = 0 ...@@ -3080,6 +3080,7 @@ var yyDebug = 0
type yyLexer interface { type yyLexer interface {
Lex(lval *yySymType) int Lex(lval *yySymType) int
Error(s string)
} }
const yyFlag = -1000 const yyFlag = -1000
...@@ -3162,7 +3163,7 @@ ret1: ...@@ -3162,7 +3163,7 @@ ret1:
yystack: yystack:
/* put a state and value onto the stack */ /* put a state and value onto the stack */
if yyDebug >= 4 { if yyDebug >= 4 {
fmt.Printf("char %v in %v", yyTokname(yychar), yyStatname(yystate)) fmt.Printf("char %v in %v\n", yyTokname(yychar), yyStatname(yystate))
} }
yyp++ yyp++
...@@ -3228,7 +3229,7 @@ yydefault: ...@@ -3228,7 +3229,7 @@ yydefault:
/* error ... attempt to resume parsing */ /* error ... attempt to resume parsing */
switch Errflag { switch Errflag {
case 0: /* brand new error */ case 0: /* brand new error */
yyError("syntax error") yylex.Error("syntax error")
Nerrs++ Nerrs++
if yyDebug >= 1 { if yyDebug >= 1 {
fmt.Printf("%s", yyStatname(yystate)) fmt.Printf("%s", yyStatname(yystate))
...@@ -3273,7 +3274,7 @@ yydefault: ...@@ -3273,7 +3274,7 @@ yydefault:
/* reduction by production yyn */ /* reduction by production yyn */
if yyDebug >= 2 { if yyDebug >= 2 {
fmt.Printf("reduce %v in:\n\t%v", yyn, yyStatname(yystate)) fmt.Printf("reduce %v in:\n\t%v\n", yyn, yyStatname(yystate))
} }
yynt := yyn yynt := yyn
......
...@@ -215,7 +215,7 @@ expr0: ...@@ -215,7 +215,7 @@ expr0:
type UnitsLex int type UnitsLex int
func (l UnitsLex) Lex(yylval *yySymType) int { func (_ UnitsLex) Lex(yylval *yySymType) int {
var c, i int var c, i int
c = peekrune c = peekrune
...@@ -280,6 +280,10 @@ numb: ...@@ -280,6 +280,10 @@ numb:
return VAL return VAL
} }
func (_ UnitsLex) Error(s string) {
Error("syntax error, last name: %v", sym)
}
func main() { func main() {
var file string var file string
...@@ -384,10 +388,6 @@ func rdigit(c int) bool { ...@@ -384,10 +388,6 @@ func rdigit(c int) bool {
return false return false
} }
func yyError(s string) {
Error("syntax error, last name: %v", sym)
}
func Error(s string, v ...interface{}) { func Error(s string, v ...interface{}) {
fmt.Printf("%v: %v\n\t", lineno, line) fmt.Printf("%v: %v\n\t", lineno, line)
fmt.Printf(s, v) fmt.Printf(s, v)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment