goccmack / gocc

Parser / Scanner Generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

internal: remove commented-out debugging block from production generated code

kortschak opened this issue · comments

Is anybody working on this or should I take it?
a simple grep -r "// fmt.Print" gave me the following 71 lines:

example/nolexer/parser/parser.go:		// fmt.Printf("S%d %s %s\n", this.stack.top(), token.TokMap.TokenString(this.nextToken), action.String())
example/sr/lexer/lexer.go:	// fmt.Printf("Lexer.Scan() pos=%d\n", this.pos)
example/sr/lexer/lexer.go:		// fmt.Printf("\tpos=%d, line=%d, col=%d, state=%d\n", this.pos, this.line, this.column, state)
example/sr/lexer/lexer.go:		// fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
example/sr/lexer/lexer.go:		// fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", this.pos, size, start, end)
example/sr/lexer/lexer.go:				// fmt.Printf("\t Accept(%s), %s(%d)\n", string(act), token.TokMap.Id(tok), tok)
example/sr/lexer/lexer.go:				// fmt.Printf("\t Ignore(%s)\n", string(act))
example/sr/lexer/lexer.go:	// fmt.Printf("Token at %s: %s \"%s\"\n", tok.String(), token.TokMap.Id(tok.Type), tok.Lit)
example/sr/parser/parser.go:		// fmt.Printf("S%d %s %s\n", this.stack.top(), token.TokMap.TokenString(this.nextToken), action.String())
example/errorrecovery/parser/parser.go:		// fmt.Printf("S%d %s %s\n", this.stack.top(), token.TokMap.TokenString(this.nextToken), action.String())
example/calc/lexer/lexer.go:	// fmt.Printf("Lexer.Scan() pos=%d\n", this.pos)
example/calc/lexer/lexer.go:		// fmt.Printf("\tpos=%d, line=%d, col=%d, state=%d\n", this.pos, this.line, this.column, state)
example/calc/lexer/lexer.go:		// fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
example/calc/lexer/lexer.go:		// fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", this.pos, size, start, end)
example/calc/lexer/lexer.go:				// fmt.Printf("\t Accept(%s), %s(%d)\n", string(act), token.TokMap.Id(tok), tok)
example/calc/lexer/lexer.go:				// fmt.Printf("\t Ignore(%s)\n", string(act))
example/calc/lexer/lexer.go:	// fmt.Printf("Token at %s: %s \"%s\"\n", tok.String(), token.TokMap.Id(tok.Type), tok.Lit)
example/calc/parser/parser.go:		// fmt.Printf("S%d %s %s\n", this.stack.top(), token.TokMap.TokenString(this.nextToken), action.String())
example/astx/lexer/lexer.go:	// fmt.Printf("Lexer.Scan() pos=%d\n", this.pos)
example/astx/lexer/lexer.go:		// fmt.Printf("\tpos=%d, line=%d, col=%d, state=%d\n", this.pos, this.line, this.column, state)
example/astx/lexer/lexer.go:		// fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
example/astx/lexer/lexer.go:		// fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", this.pos, size, start, end)
example/astx/lexer/lexer.go:				// fmt.Printf("\t Accept(%s), %s(%d)\n", string(act), token.TokMap.Id(tok), tok)
example/astx/lexer/lexer.go:				// fmt.Printf("\t Ignore(%s)\n", string(act))
example/astx/lexer/lexer.go:	// fmt.Printf("Token at %s: %s \"%s\"\n", tok.String(), token.TokMap.Id(tok.Type), tok.Lit)
example/astx/parser/parser.go:		// fmt.Printf("S%d %s %s\n", this.stack.top(), token.TokMap.TokenString(this.nextToken), action.String())
example/bools/lexer/lexer.go:	// fmt.Printf("Lexer.Scan() pos=%d\n", this.pos)
example/bools/lexer/lexer.go:		// fmt.Printf("\tpos=%d, line=%d, col=%d, state=%d\n", this.pos, this.line, this.column, state)
example/bools/lexer/lexer.go:		// fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
example/bools/lexer/lexer.go:		// fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", this.pos, size, start, end)
example/bools/lexer/lexer.go:				// fmt.Printf("\t Accept(%s), %s(%d)\n", string(act), token.TokMap.Id(tok), tok)
example/bools/lexer/lexer.go:				// fmt.Printf("\t Ignore(%s)\n", string(act))
example/bools/lexer/lexer.go:	// fmt.Printf("Token at %s: %s \"%s\"\n", tok.String(), token.TokMap.Id(tok.Type), tok.Lit)
example/bools/parser/parser.go:		// fmt.Printf("S%d %s %s\n", this.stack.top(), token.TokMap.TokenString(this.nextToken), action.String())
example/rr/lexer/lexer.go:	// fmt.Printf("Lexer.Scan() pos=%d\n", this.pos)
example/rr/lexer/lexer.go:		// fmt.Printf("\tpos=%d, line=%d, col=%d, state=%d\n", this.pos, this.line, this.column, state)
example/rr/lexer/lexer.go:		// fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
example/rr/lexer/lexer.go:		// fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", this.pos, size, start, end)
example/rr/lexer/lexer.go:				// fmt.Printf("\t Accept(%s), %s(%d)\n", string(act), token.TokMap.Id(tok), tok)
example/rr/lexer/lexer.go:				// fmt.Printf("\t Ignore(%s)\n", string(act))
example/rr/lexer/lexer.go:	// fmt.Printf("Token at %s: %s \"%s\"\n", tok.String(), token.TokMap.Id(tok.Type), tok.Lit)
example/rr/parser/parser.go:		// fmt.Printf("S%d %s %s\n", this.stack.top(), token.TokMap.TokenString(this.nextToken), action.String())
example/mail/lexer/lexer.go:	// fmt.Printf("Lexer.Scan() pos=%d\n", this.pos)
example/mail/lexer/lexer.go:		// fmt.Printf("\tpos=%d, line=%d, col=%d, state=%d\n", this.pos, this.line, this.column, state)
example/mail/lexer/lexer.go:		// fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
example/mail/lexer/lexer.go:		// fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", this.pos, size, start, end)
example/mail/lexer/lexer.go:				// fmt.Printf("\t Accept(%s), %s(%d)\n", string(act), token.TokMap.Id(tok), tok)
example/mail/lexer/lexer.go:				// fmt.Printf("\t Ignore(%s)\n", string(act))
example/mail/lexer/lexer.go:	// fmt.Printf("Token at %s: %s \"%s\"\n", tok.String(), token.TokMap.Id(tok.Type), tok.Lit)
internal/frontend/parser/parser.go:		// fmt.Printf("S%d %s %s\n", this.stack.Top(), this.nextToken, action)
internal/frontend/parser/parser_ut.go:		// fmt.Printf("S%d %s %s\n", this.stack.Top(), this.nextToken, action)
internal/lexer/items/item_test.go:			// fmt.Printf("%s", item.pos)
internal/lexer/items/itemset.go:	// fmt.Printf("S%d%s\n", this.setNo, this)
internal/lexer/items/itemset.go:	// fmt.Printf("dependentsClosure S%d, %s\n", this.setNo, items)
internal/lexer/items/itemset.go:					// fmt.Printf("\t%s\n", thisItem)
internal/lexer/gen/golang/lexer.go:	// fmt.Printf("Lexer.Scan() pos=%d\n", this.pos)
internal/lexer/gen/golang/lexer.go:		// fmt.Printf("\tpos=%d, line=%d, col=%d, state=%d\n", this.pos, this.line, this.column, state)
internal/lexer/gen/golang/lexer.go:		// fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
internal/lexer/gen/golang/lexer.go:		// fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", this.pos, size, start, end)
internal/lexer/gen/golang/lexer.go:				// fmt.Printf("\t Accept(%s), %s(%d)\n", string(act), token.TokMap.Id(tok), tok)
internal/lexer/gen/golang/lexer.go:				// fmt.Printf("\t Ignore(%s)\n", string(act))
internal/lexer/gen/golang/lexer.go:	// fmt.Printf("Token at %s: %s \"%s\"\n", tok.String(), token.TokMap.Id(tok.Type), tok.Lit)
internal/parser/gen/golang/parser.go:		// fmt.Printf("S%d %s %s\n", this.stack.top(), token.TokMap.TokenString(this.nextToken), action.String())
internal/test/t1/lexer/lexer.go:	// fmt.Printf("Lexer.Scan() pos=%d\n", this.pos)
internal/test/t1/lexer/lexer.go:		// fmt.Printf("\tpos=%d, line=%d, col=%d, state=%d\n", this.pos, this.line, this.column, state)
internal/test/t1/lexer/lexer.go:		// fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
internal/test/t1/lexer/lexer.go:		// fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", this.pos, size, start, end)
internal/test/t1/lexer/lexer.go:				// fmt.Printf("\t Accept(%s), %s(%d)\n", string(act), token.TokMap.Id(tok), tok)
internal/test/t1/lexer/lexer.go:				// fmt.Printf("\t Ignore(%s)\n", string(act))
internal/test/t1/lexer/lexer.go:	// fmt.Printf("Token at %s: %s \"%s\"\n", tok.String(), token.TokMap.Id(tok.Type), tok.Lit)
internal/test/t1/parser/parser.go:		// fmt.Printf("S%d %s %s\n", this.stack.top(), token.TokMap.TokenString(this.nextToken), action.String())

telling us that each lexer generated gives us 7 commented out prints in lexer.go, and each parser generated gives 1 commented out print in parser.go. Should we only deal with these and leave the rest of the commented out prints in the backend code alone for now? Or should there be a complete overhaul of debug-prints even in backend?

commented

@sangisos Go for it!

I'm guessing the whole argument for keeping debug prints commented out in production is because of the part of the lexer being done differently depending on debug mode:

{{- if .Debug}}
		// Production start
		// if rune1 != -1 {
		// 	state = TransTab[state](rune1)
		// } else {
		// 	state = -1
		// }
		// Production end

		// Debug start
		nextState := -1
		if rune1 != -1 {
			nextState = TransTab[state](rune1)
		}
		fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
		fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", l.pos, size, start, end)
		if nextState != -1 {
			fmt.Printf("\t\taction:%s\n", ActTab[nextState].String())
		}
		state = nextState
		// Debug end
	{{- else}}
		// Production start
		if rune1 != -1 {
			state = TransTab[state](rune1)
		} else {
			state = -1
		}
		// Production end

		// Debug start
		// nextState := -1
		// if rune1 != -1 {
		// 	nextState = TransTab[state](rune1)
		// }
		// fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
		// fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", l.pos, size, start, end)
		// if nextState != -1 {
		// 	fmt.Printf("\t\taction:%s\n", ActTab[nextState].String())
		// }
		// state = nextState
		// Debug end
	{{- end}}

Removing all commented out parts, not mentioning the difference, feels prone to create hisenbugs. Any tips?

I believe in the power of the go compiler to optimize this, and would go with the debug version at all times (minus the print outs) if it does not hit performance. @mewmew could you bench it after i push to sangisos/gocc branch remcom?

commented

Removing all commented out parts, not mentioning the difference, feels prone to create hisenbugs. Any tips?

Indeed, the difference between the debug and production code should be next to none; basically only include debug messages when debugging is enabled, but the lexing (and parsing) code should be identical. Otherwise, the debugger will be debugging something other than the production code.

I believe in the power of the go compiler to optimize this, and would go with the debug version at all times (minus the print outs) if it does not hit performance. @mewmew could you bench it after i push to sangisos/gocc branch remcom?

Ping me when you wish to try the benchmark. llir/llvm is large enough to be useful for such a benchmark :)

Sounds good to me :)