Skip to content

goyacc: reduce dependence on globals #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/goyacc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ Error is equivalent to yyerror in the original yacc.
Code inside the grammar actions may refer to the variable yylex,
which holds the yyLexer passed to yyParse.

Code inside the grammar actions may refer to yyrcvr,
which holds the yyParser.

Clients that need to understand more about the parser state can
create the parser separately from invoking it. The function yyNewParser
returns a yyParser conforming to the following interface:

type yyParser interface {
Parse(yyLex) int
Lookahead() int
Debug() int
ErrorVerbose() bool
SetDebug(level int)
SetErrorVerbose(verbose bool)
}

Parse runs the parser; the top-level call yyParse(yylex) is equivalent
Expand Down
69 changes: 53 additions & 16 deletions cmd/goyacc/yacc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3277,20 +3277,53 @@ type $$Lexer interface {
type $$Parser interface {
Parse($$Lexer) int
Lookahead() int
Debug() int
ErrorVerbose() bool
SetDebug(level int)
SetErrorVerbose(verbose bool)
}

type $$ParserImpl struct {
lval $$SymType
stack [$$InitialStackSize]$$SymType
char int
lval $$SymType
stack [$$InitialStackSize]$$SymType
char int
debug int
errorVerbose bool
}

func (p *$$ParserImpl) Lookahead() int {
return p.char
}

func (p *$$ParserImpl) Debug() int {
return p.debug
}

func (p *$$ParserImpl) SetDebug(level int) {
p.debug = level
}

func (p *$$ParserImpl) ErrorVerbose() bool {
return p.errorVerbose
}

func (p *$$ParserImpl) SetErrorVerbose(verbose bool) {
p.errorVerbose = verbose
}


func $$NewParser() $$Parser {
return &$$ParserImpl{}
return &$$ParserImpl{
debug: $$Debug,
errorVerbose: $$ErrorVerbose,
}
}

func $$NewParserExt(debug int, errorVerbose bool) $$Parser {
return &$$ParserImpl{
debug: debug,
errorVerbose: errorVerbose,
}
}

const $$Flag = -1000
Expand All @@ -3313,10 +3346,10 @@ func $$Statname(s int) string {
return __yyfmt__.Sprintf("state-%v", s)
}

func $$ErrorMessage(state, lookAhead int) string {
func ($$rcvr *$$ParserImpl) $$ErrorMessage(state, lookAhead int) string {
const TOKSTART = 4

if !$$ErrorVerbose {
if !$$rcvr.errorVerbose {
return "syntax error"
}

Expand Down Expand Up @@ -3377,7 +3410,7 @@ func $$ErrorMessage(state, lookAhead int) string {
return res
}

func $$lex1(lex $$Lexer, lval *$$SymType) (char, token int) {
func ($$rcvr *$$ParserImpl) $$lex1(lex $$Lexer, lval *$$SymType) (char, token int) {
token = 0
char = lex.Lex(lval)
if char <= 0 {
Expand Down Expand Up @@ -3406,7 +3439,7 @@ out:
if token == 0 {
token = $$Tok2[1] /* unknown char */
}
if $$Debug >= 3 {
if $$rcvr.debug >= 3 {
__yyfmt__.Printf("lex %s(%d)\n", $$Tokname(token), uint(char))
}
return char, token
Expand All @@ -3416,6 +3449,10 @@ func $$Parse($$lex $$Lexer) int {
return $$NewParser().Parse($$lex)
}

func $$ParseExt(debug int, errorVerbose bool, $$lex $$Lexer) int {
return $$NewParserExt(debug, errorVerbose).Parse($$lex)
}

func ($$rcvr *$$ParserImpl) Parse($$lex $$Lexer) int {
var $$n int
var $$VAL $$SymType
Expand Down Expand Up @@ -3445,7 +3482,7 @@ ret1:

$$stack:
/* put a state and value onto the stack */
if $$Debug >= 4 {
if $$rcvr.debug >= 4 {
__yyfmt__.Printf("char %v in %v\n", $$Tokname($$token), $$Statname($$state))
}

Expand All @@ -3464,7 +3501,7 @@ $$newstate:
goto $$default /* simple state */
}
if $$rcvr.char < 0 {
$$rcvr.char, $$token = $$lex1($$lex, &$$rcvr.lval)
$$rcvr.char, $$token = $$rcvr.$$lex1($$lex, &$$rcvr.lval)
}
$$n += $$token
if $$n < 0 || $$n >= $$Last {
Expand All @@ -3487,7 +3524,7 @@ $$default:
$$n = $$Def[$$state]
if $$n == -2 {
if $$rcvr.char < 0 {
$$rcvr.char, $$token = $$lex1($$lex, &$$rcvr.lval)
$$rcvr.char, $$token = $$rcvr.$$lex1($$lex, &$$rcvr.lval)
}

/* look through exception table */
Expand All @@ -3513,9 +3550,9 @@ $$default:
/* error ... attempt to resume parsing */
switch Errflag {
case 0: /* brand new error */
$$lex.Error($$ErrorMessage($$state, $$token))
$$lex.Error($$rcvr.$$ErrorMessage($$state, $$token))
Nerrs++
if $$Debug >= 1 {
if $$rcvr.debug >= 1 {
__yyfmt__.Printf("%s", $$Statname($$state))
__yyfmt__.Printf(" saw %s\n", $$Tokname($$token))
}
Expand All @@ -3535,7 +3572,7 @@ $$default:
}

/* the current p has no shift on "error", pop stack */
if $$Debug >= 2 {
if $$rcvr.debug >= 2 {
__yyfmt__.Printf("error recovery pops state %d\n", $$S[$$p].yys)
}
$$p--
Expand All @@ -3544,7 +3581,7 @@ $$default:
goto ret1

case 3: /* no shift yet; clobber input char */
if $$Debug >= 2 {
if $$rcvr.debug >= 2 {
__yyfmt__.Printf("error recovery discards %s\n", $$Tokname($$token))
}
if $$token == $$EofCode {
Expand All @@ -3557,7 +3594,7 @@ $$default:
}

/* reduction by production $$n */
if $$Debug >= 2 {
if $$rcvr.debug >= 2 {
__yyfmt__.Printf("reduce %v in:\n\t%v\n", $$n, $$Statname($$state))
}

Expand Down