Created
March 25, 2011 10:31
-
-
Save knieriem/886662 to your computer and use it in GitHub Desktop.
fix compilation of peg.go Compilation failed with error(s): <label> defined and not used
due to a recent change to the Go compilers: http://code.google.com/p/go/source/detail?r=570c3ce8 This temporary fix introduces bookkeeping about labels being
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/peg.go b/peg.go | |
--- a/peg.go | |
+++ b/peg.go | |
@@ -1135,7 +1135,7 @@ | |
} | |
var printRule func(node Node) | |
- var compile func(expression Node, ko uint) | |
+ var compile func(expression Node, ko uint) (usedko bool) | |
var label uint | |
printRule = func(node Node) { | |
switch node.GetType() { | |
@@ -1214,15 +1214,16 @@ | |
fmt.Fprintf(os.Stderr, "illegal node type: %v\n", node.GetType()) | |
} | |
} | |
- compileExpression := func(rule *rule, ko uint) { | |
+ compileExpression := func(rule *rule, ko uint) (usedko bool) { | |
nvar := len(rule.variables) | |
if nvar > 0 { | |
w.lnPrint("doarg(yyPush, %d)", nvar) | |
} | |
- compile(rule.GetExpression(), ko) | |
+ usedko = compile(rule.GetExpression(), ko) | |
if nvar > 0 { | |
w.lnPrint("doarg(yyPop, %d)", nvar) | |
} | |
+ return | |
} | |
canCompilePeek := func(node Node, jumpIfTrue bool, label uint) bool { | |
switch node.GetType() { | |
@@ -1237,35 +1238,42 @@ | |
} | |
return true | |
} | |
- compile = func(node Node, ko uint) { | |
+ compile = func(node Node, ko uint) (usedko bool) { | |
switch node.GetType() { | |
case TypeRule: | |
fmt.Fprintf(os.Stderr, "internal error #1 (%v)\n", node) | |
case TypeDot: | |
w.cJump(false, ko, "matchDot()") | |
+ usedko = true | |
case TypeName: | |
varp := node.(*name).varp | |
name := node.String() | |
rule := t.rules[name] | |
if t.inline && t.rulesCount[name] == 1 { | |
- compileExpression(rule, ko) | |
+ usedko = compileExpression(rule, ko) | |
} else { | |
w.cJump(false, ko, "p.rules[rule%s]()", rule.String()) | |
+ usedko = true | |
} | |
if varp != nil { | |
w.lnPrint("doarg(yySet, %d)", varp.offset) | |
} | |
case TypeCharacter: | |
+ usedko = true | |
w.cJump(false, ko, "matchChar('%v')", node) | |
case TypeString: | |
+ usedko = true | |
w.cJump(false, ko, "matchString(\"%v\")", node) | |
case TypeClass: | |
+ usedko = true | |
w.cJump(false, ko, "matchClass(%d)", classes[node.String()]) | |
case TypePredicate: | |
+ usedko = true | |
w.cJump(false, ko, "(%v)", node) | |
case TypeAction: | |
w.lnPrint("do(%d)", node.(Action).GetId()) | |
case TypeCommit: | |
+ usedko = true | |
w.cJump(false, ko, "(commit(thunkPosition0))") | |
case TypeBegin: | |
if hasActions { | |
@@ -1287,27 +1295,32 @@ | |
for element.Next() != nil { | |
next := label | |
label++ | |
- compile(element.Value.(Node), next) | |
+ usednext := compile(element.Value.(Node), next) | |
w.jump(ok) | |
- w.label(next) | |
- w.restore(ok) | |
+ if usednext { | |
+ w.label(next) | |
+ } | |
+ w.restore(ok) | |
element = element.Next() | |
} | |
if list.GetLastIsEmpty() { | |
done := label | |
label++ | |
- compile(element.Value.(Node), done) | |
+ useddone := compile(element.Value.(Node), done) | |
w.jump(ok) | |
- w.label(done) | |
- w.restore(ok) | |
+ if useddone { | |
+ w.label(done) | |
+ } | |
+ w.restore(ok) | |
} else { | |
- compile(element.Value.(Node), ko) | |
+ usedko = compile(element.Value.(Node), ko) | |
} | |
w.end() | |
w.label(ok) | |
case TypeUnorderedAlternate: | |
list := node.(List) | |
done, ok := ko, label | |
+ useddone := false | |
label++ | |
w.begin() | |
if list.GetLastIsEmpty() { | |
@@ -1356,49 +1369,68 @@ | |
} | |
print(":") | |
w.indent++ | |
- compile(sequence.Value.(Node), done) | |
+ if compile(sequence.Value.(Node), done) { | |
+ useddone = true | |
+ } | |
w.indent-- | |
} | |
w.lnPrint("default:") | |
w.indent++ | |
- compile(element.Value.(List).Front().Next().Value.(Node), done) | |
+ if compile(element.Value.(List).Front().Next().Value.(Node), done) { | |
+ useddone = true | |
+ } | |
w.indent-- | |
w.lnPrint("}") | |
+ usedok := false | |
if list.GetLastIsEmpty() { | |
w.jump(ok) | |
- w.label(done) | |
- w.restore(ok) | |
+ usedok = true | |
+ if useddone { | |
+ w.label(done) | |
+ } | |
+ w.restore(ok) | |
+ } else if useddone { | |
+ usedko = true | |
} | |
w.end() | |
- w.label(ok) | |
+ if usedok { | |
+ w.label(ok) | |
+ } | |
case TypeSequence: | |
for element := node.(List).Front(); element != nil; element = element.Next() { | |
- compile(element.Value.(Node), ko) | |
+ if u := compile(element.Value.(Node), ko); u { | |
+ usedko = true | |
+ } | |
} | |
case TypePeekFor: | |
sub := node.(Fix).GetNode() | |
if canCompilePeek(sub, false, ko) { | |
+ usedko = true | |
return | |
} | |
ok := label | |
label++ | |
w.begin() | |
w.save(ok) | |
- compile(sub, ko) | |
+ usedko = compile(sub, ko) | |
w.restore(ok) | |
w.end() | |
case TypePeekNot: | |
sub := node.(Fix).GetNode() | |
if canCompilePeek(sub, true, ko) { | |
+ usedko = true | |
return | |
} | |
ok := label | |
label++ | |
w.begin() | |
w.save(ok) | |
- compile(sub, ok) | |
+ usedok := compile(sub, ok) | |
w.jump(ko) | |
- w.label(ok) | |
+ usedko = true | |
+ if usedok { | |
+ w.label(ok) | |
+ } | |
w.restore(ok) | |
w.end() | |
case TypeQuery: | |
@@ -1408,10 +1440,12 @@ | |
label++ | |
w.begin() | |
w.save(qko) | |
- compile(node.(Fix).GetNode(), qko) | |
+ usedqko := compile(node.(Fix).GetNode(), qko) | |
w.jump(qok) | |
- w.label(qko) | |
- w.restore(qko) | |
+ if usedqko { | |
+ w.label(qko) | |
+ } | |
+ w.restore(qko) | |
w.end() | |
w.label(qok) | |
case TypeStar: | |
@@ -1422,29 +1456,34 @@ | |
w.label(again) | |
w.begin() | |
w.save(out) | |
- compile(node.(Fix).GetNode(), out) | |
+ usedout := compile(node.(Fix).GetNode(), out) | |
w.jump(again) | |
- w.label(out) | |
- w.restore(out) | |
+ if usedout { | |
+ w.label(out) | |
+ } | |
+ w.restore(out) | |
w.end() | |
case TypePlus: | |
again := label | |
label++ | |
out := label | |
label++ | |
- compile(node.(Fix).GetNode(), ko) | |
+ usedko = compile(node.(Fix).GetNode(), ko) | |
w.label(again) | |
w.begin() | |
w.save(out) | |
- compile(node.(Fix).GetNode(), out) | |
+ usedout := compile(node.(Fix).GetNode(), out) | |
w.jump(again) | |
- w.label(out) | |
- w.restore(out) | |
+ if usedout { | |
+ w.label(out) | |
+ } | |
+ w.restore(out) | |
w.end() | |
case TypeNil: | |
default: | |
fmt.Fprintf(os.Stderr, "illegal node type: %v\n", node.GetType()) | |
} | |
+ return | |
} | |
print("\n\tp.rules = [...]func() bool{") | |
@@ -1476,10 +1515,12 @@ | |
if !expression.GetType().IsSafe() { | |
w.save(0) | |
} | |
- compileExpression(rule, ko) | |
+ usedko := compileExpression(rule, ko) | |
w.lnPrint("return true") | |
if !expression.GetType().IsSafe() { | |
- w.label(ko) | |
+ if usedko { | |
+ w.label(ko) | |
+ } | |
w.restore(0) | |
w.lnPrint("return false") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment