Skip to content

Commit

Permalink
fix panic when state code block used with the -optimize-grammar option (
Browse files Browse the repository at this point in the history
#49)

fix panic when state code block used with the -optimize-grammar option
clone state code expression node
  • Loading branch information
mna authored and breml committed Oct 22, 2017
1 parent b5651e1 commit e2a867d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 150 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -125,7 +125,7 @@ $(TEST_DIR)/alternate_entrypoint/altentry.go: $(TEST_DIR)/alternate_entrypoint/a
$(BINDIR)/pigeon -optimize-grammar -alternate-entrypoints Entry2,Entry3,C $< > $@

$(TEST_DIR)/state/state.go: $(TEST_DIR)/state/state.peg $(BINDIR)/pigeon
$(BINDIR)/pigeon $< > $@
$(BINDIR)/pigeon -optimize-grammar $< > $@

$(TEST_DIR)/stateclone/stateclone.go: $(TEST_DIR)/stateclone/stateclone.peg $(BINDIR)/pigeon
$(BINDIR)/pigeon $< > $@
Expand Down
6 changes: 6 additions & 0 deletions ast/ast_optimize.go
Expand Up @@ -320,6 +320,12 @@ func cloneExpr(expr Expression) Expression {
Exprs: exprs,
p: expr.p,
}
case *StateCodeExpr:
return &StateCodeExpr{
p: expr.p,
Code: expr.Code,
FuncIx: expr.FuncIx,
}
case *ZeroOrMoreExpr:
return &ZeroOrMoreExpr{
Expr: cloneExpr(expr.Expr),
Expand Down
2 changes: 2 additions & 0 deletions ast/ast_walk.go
Expand Up @@ -58,6 +58,8 @@ func Walk(v Visitor, expr Expression) {
for _, e := range expr.Exprs {
Walk(v, e)
}
case *StateCodeExpr:
// Nothing to do
case *ZeroOrMoreExpr:
Walk(v, expr.Expr)
case *ZeroOrOneExpr:
Expand Down
222 changes: 73 additions & 149 deletions test/state/state.go
Expand Up @@ -53,25 +53,68 @@ var g = &grammar{
&choiceExpr{
pos: position{line: 25, col: 5, offset: 344},
alternatives: []interface{}{
&ruleRefExpr{
pos: position{line: 25, col: 5, offset: 344},
name: "x",
&seqExpr{
pos: position{line: 29, col: 5, offset: 400},
exprs: []interface{}{
&litMatcher{
pos: position{line: 29, col: 5, offset: 400},
val: "abc",
ignoreCase: false,
},
&stateCodeExpr{
pos: position{line: 33, col: 9, offset: 512},
run: (*parser).callonstart9,
},
&litMatcher{
pos: position{line: 29, col: 12, offset: 407},
val: "d",
ignoreCase: false,
},
},
},
&ruleRefExpr{
pos: position{line: 25, col: 7, offset: 346},
name: "y",
&seqExpr{
pos: position{line: 30, col: 5, offset: 415},
exprs: []interface{}{
&litMatcher{
pos: position{line: 30, col: 5, offset: 415},
val: "abc",
ignoreCase: false,
},
&stateCodeExpr{
pos: position{line: 34, col: 11, offset: 589},
run: (*parser).callonstart13,
},
&litMatcher{
pos: position{line: 30, col: 12, offset: 422},
val: "e",
ignoreCase: false,
},
},
},
&ruleRefExpr{
pos: position{line: 25, col: 9, offset: 348},
name: "z",
&seqExpr{
pos: position{line: 31, col: 5, offset: 430},
exprs: []interface{}{
&litMatcher{
pos: position{line: 31, col: 5, offset: 430},
val: "abcf",
ignoreCase: false,
},
&stateCodeExpr{
pos: position{line: 31, col: 12, offset: 437},
run: (*parser).callonstart17,
},
},
},
},
},
&zeroOrMoreExpr{
pos: position{line: 25, col: 12, offset: 351},
expr: &ruleRefExpr{
pos: position{line: 25, col: 12, offset: 351},
name: "ws",
expr: &charClassMatcher{
pos: position{line: 36, col: 6, offset: 661},
val: "[ \\n]",
chars: []rune{' ', '\n'},
ignoreCase: false,
inverted: false,
},
},
},
Expand All @@ -81,125 +124,6 @@ var g = &grammar{
},
},
},
{
name: "x",
pos: position{line: 29, col: 1, offset: 396},
expr: &seqExpr{
pos: position{line: 29, col: 5, offset: 400},
exprs: []interface{}{
&litMatcher{
pos: position{line: 29, col: 5, offset: 400},
val: "ab",
ignoreCase: false,
},
&ruleRefExpr{
pos: position{line: 29, col: 10, offset: 405},
name: "c",
},
&litMatcher{
pos: position{line: 29, col: 12, offset: 407},
val: "d",
ignoreCase: false,
},
},
},
},
{
name: "y",
pos: position{line: 30, col: 1, offset: 411},
expr: &seqExpr{
pos: position{line: 30, col: 5, offset: 415},
exprs: []interface{}{
&litMatcher{
pos: position{line: 30, col: 5, offset: 415},
val: "a",
ignoreCase: false,
},
&ruleRefExpr{
pos: position{line: 30, col: 9, offset: 419},
name: "bc",
},
&litMatcher{
pos: position{line: 30, col: 12, offset: 422},
val: "e",
ignoreCase: false,
},
},
},
},
{
name: "z",
pos: position{line: 31, col: 1, offset: 426},
expr: &seqExpr{
pos: position{line: 31, col: 5, offset: 430},
exprs: []interface{}{
&litMatcher{
pos: position{line: 31, col: 5, offset: 430},
val: "abcf",
ignoreCase: false,
},
&stateCodeExpr{
pos: position{line: 31, col: 12, offset: 437},
run: (*parser).callonz3,
},
},
},
},
{
name: "c",
pos: position{line: 33, col: 1, offset: 504},
expr: &seqExpr{
pos: position{line: 33, col: 5, offset: 508},
exprs: []interface{}{
&litMatcher{
pos: position{line: 33, col: 5, offset: 508},
val: "c",
ignoreCase: false,
},
&stateCodeExpr{
pos: position{line: 33, col: 9, offset: 512},
run: (*parser).callonc3,
},
},
},
},
{
name: "bc",
pos: position{line: 34, col: 1, offset: 579},
expr: &seqExpr{
pos: position{line: 34, col: 6, offset: 584},
exprs: []interface{}{
&litMatcher{
pos: position{line: 34, col: 6, offset: 584},
val: "bc",
ignoreCase: false,
},
&stateCodeExpr{
pos: position{line: 34, col: 11, offset: 589},
run: (*parser).callonbc3,
},
},
},
},
{
name: "ws",
pos: position{line: 36, col: 1, offset: 656},
expr: &choiceExpr{
pos: position{line: 36, col: 6, offset: 661},
alternatives: []interface{}{
&litMatcher{
pos: position{line: 36, col: 6, offset: 661},
val: " ",
ignoreCase: false,
},
&litMatcher{
pos: position{line: 36, col: 12, offset: 667},
val: "\n",
ignoreCase: false,
},
},
},
},
},
}

Expand All @@ -217,48 +141,48 @@ func (p *parser) callonstart3() error {
return p.cur.onstart3()
}

func (c *current) onstart1() (interface{}, error) {

return c.state["countCs"], nil
func (c *current) onstart9() error {
c.state["countCs"] = c.state["countCs"].(int) + 3
return nil
}

func (p *parser) callonstart1() (interface{}, error) {
func (p *parser) callonstart9() error {
stack := p.vstack[len(p.vstack)-1]
_ = stack
return p.cur.onstart1()
return p.cur.onstart9()
}

func (c *current) onz3() error {
c.state["countCs"] = c.state["countCs"].(int) + 5
func (c *current) onstart13() error {
c.state["countCs"] = c.state["countCs"].(int) + 1
return nil
}

func (p *parser) callonz3() error {
func (p *parser) callonstart13() error {
stack := p.vstack[len(p.vstack)-1]
_ = stack
return p.cur.onz3()
return p.cur.onstart13()
}

func (c *current) onc3() error {
c.state["countCs"] = c.state["countCs"].(int) + 3
func (c *current) onstart17() error {
c.state["countCs"] = c.state["countCs"].(int) + 5
return nil
}

func (p *parser) callonc3() error {
func (p *parser) callonstart17() error {
stack := p.vstack[len(p.vstack)-1]
_ = stack
return p.cur.onc3()
return p.cur.onstart17()
}

func (c *current) onbc3() error {
c.state["countCs"] = c.state["countCs"].(int) + 1
return nil
func (c *current) onstart1() (interface{}, error) {

return c.state["countCs"], nil
}

func (p *parser) callonbc3() error {
func (p *parser) callonstart1() (interface{}, error) {
stack := p.vstack[len(p.vstack)-1]
_ = stack
return p.cur.onbc3()
return p.cur.onstart1()
}

var (
Expand Down

0 comments on commit e2a867d

Please sign in to comment.