Created
December 1, 2010 15:49
-
-
Save knieriem/723666 to your computer and use it in GitHub Desktop.
avoid panic'ing in `used but not defined' cases
This file contains 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 | |
@@ -36,6 +36,7 @@ | |
TypeStar | |
TypePlus | |
TypePeg | |
+ TypeNil | |
TypeLast | |
) | |
@@ -75,6 +76,7 @@ | |
} | |
func (r *rule) GetExpression() Node { | |
+ if r.expression == nil {return nilNode} | |
return r.expression | |
} | |
@@ -86,7 +88,7 @@ | |
return r.name | |
} | |
-/* Used to represent TypeName, TypeDot, TypeCharacter, TypeString, TypeClass, and TypePredicate. */ | |
+/* Used to represent TypeName, TypeDot, TypeCharacter, TypeString, TypeClass, TypePredicate, and TypeNil. */ | |
type Token interface { | |
Node | |
GetClass() *characterClass | |
@@ -106,6 +108,8 @@ | |
return t.string | |
} | |
+var nilNode = &token{Type: TypeNil, string: "<nil>"} | |
+ | |
/* Used to represent TypeAction. */ | |
type Action interface { | |
Node | |
@@ -298,7 +302,10 @@ | |
t.PushBack(rule) | |
} | |
-func (t *Tree) AddName(text string) {t.push(&token{Type: TypeName, string: text})} | |
+func (t *Tree) AddName(text string) { | |
+ t.rules[text] = &rule{} | |
+ t.push(&token{Type: TypeName, string: text}) | |
+} | |
var dot *token = &token{Type: TypeDot, string: "."} | |
func (t *Tree) AddDot() {t.push(dot)} | |
func (t *Tree) AddString(text string) { | |
@@ -428,6 +435,14 @@ | |
t.rules[rule.String()] = rule | |
} | |
} | |
+ for name, r := range t.rules { | |
+ if r.name == "" { | |
+ r := &rule{name: name, id: t.ruleId} | |
+ t.ruleId++ | |
+ t.rules[name] = r | |
+ t.PushBack(r) | |
+ } | |
+ } | |
join([]func() { | |
func() { | |
@@ -833,7 +848,7 @@ | |
case TypeRule: | |
print("%v <- ", node) | |
expression := node.( Rule ).GetExpression() | |
- if expression != nil {printRule(expression)} | |
+ if expression != nilNode {printRule(expression)} | |
case TypeDot: print(".") | |
case TypeName: print("%v", node) | |
case TypeCharacter, | |
@@ -1042,6 +1057,7 @@ | |
printLabel(out) | |
printRestore(out) | |
printEnd() | |
+ case TypeNil: | |
default: | |
fmt.Fprintf(os.Stderr, "illegal node type: %v\n", node.GetType()) | |
} | |
@@ -1053,7 +1069,7 @@ | |
if node.GetType() != TypeRule {continue} | |
rule := node.( *rule ) | |
expression := rule.GetExpression() | |
- if expression == nil { | |
+ if expression == nilNode { | |
fmt.Fprintf(os.Stderr, "rule '%v' used but not defined\n", rule) | |
print("\n nil,") | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment