Skip to content

Commit

Permalink
cleanup global state, fix .gitattributes
Browse files Browse the repository at this point in the history
  • Loading branch information
RedSkotina authored and breml committed Oct 14, 2017
1 parent 6ea1fb1 commit 7e45eee
Show file tree
Hide file tree
Showing 27 changed files with 109 additions and 131 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -1 +1,2 @@
*.go text eol=lf
*.peg text eol=lf
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -80,7 +80,7 @@ $(EXAMPLES_DIR)/calculator/calculator.go: $(EXAMPLES_DIR)/calculator/calculator.
$(BINDIR)/pigeon $< > $@

$(EXAMPLES_DIR)/indentation/indentation.go: $(EXAMPLES_DIR)/indentation/indentation.peg $(BINDIR)/pigeon
$(BINDIR)/pigeon $< | goimports > $@
$(BINDIR)/pigeon $< > $@

$(TEST_DIR)/andnot/andnot.go: $(TEST_DIR)/andnot/andnot.peg $(BINDIR)/pigeon
$(BINDIR)/pigeon $< > $@
Expand Down Expand Up @@ -122,7 +122,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 $< | goimports > $@
$(BINDIR)/pigeon $< > $@

lint:
golint ./...
Expand Down
9 changes: 4 additions & 5 deletions bootstrap/cmd/bootstrap-pigeon/bootstrap_pigeon.go
Expand Up @@ -2788,14 +2788,12 @@ func (p *parser) cloneState() (state statedict) {
}

// restore parser current state to the state statedict.
// every restoreState should applied only one time for every cloned state
func (p *parser) restoreState(state statedict) {
if p.debug {
defer p.out(p.in("restoreState"))
}
p.cur.state = make(statedict)
for k, v := range state {
p.cur.state[k] = v
}
p.cur.state = state
}

// get the slice of bytes from the savepoint start to the current position.
Expand Down Expand Up @@ -3151,11 +3149,12 @@ func (p *parser) parseChoiceExpr(ch *choiceExpr) (interface{}, bool) {
defer p.out(p.in("parseChoiceExpr"))
}

state := p.cloneState()
for altI, alt := range ch.alternatives {
// dummy assignment to prevent compile error if optimized
_ = altI

state := p.cloneState()

p.pushV()
val, ok := p.parseExpr(alt)
p.popV()
Expand Down
9 changes: 4 additions & 5 deletions builder/generated_static_code.go
Expand Up @@ -636,16 +636,14 @@ func (p *parser) cloneState() (state statedict) {
}
// restore parser current state to the state statedict.
// every restoreState should applied only one time for every cloned state
func (p *parser) restoreState(state statedict) {
// ==template== {{ if not .Optimize }}
if p.debug {
defer p.out(p.in("restoreState"))
}
// {{ end }} ==template==
p.cur.state = make(statedict)
for k, v := range state {
p.cur.state[k] = v
}
p.cur.state = state
}
// get the slice of bytes from the savepoint start to the current position.
Expand Down Expand Up @@ -1045,11 +1043,12 @@ func (p *parser) parseChoiceExpr(ch *choiceExpr) (interface{}, bool) {
}
// {{ end }} ==template==
state := p.cloneState()
for altI, alt := range ch.alternatives {
// dummy assignment to prevent compile error if optimized
_ = altI
state := p.cloneState()
p.pushV()
val, ok := p.parseExpr(alt)
p.popV()
Expand Down
9 changes: 4 additions & 5 deletions builder/static_code.go
Expand Up @@ -652,16 +652,14 @@ func (p *parser) cloneState() (state statedict) {
}

// restore parser current state to the state statedict.
// every restoreState should applied only one time for every cloned state
func (p *parser) restoreState(state statedict) {
// ==template== {{ if not .Optimize }}
if p.debug {
defer p.out(p.in("restoreState"))
}
// {{ end }} ==template==
p.cur.state = make(statedict)
for k, v := range state {
p.cur.state[k] = v
}
p.cur.state = state
}

// get the slice of bytes from the savepoint start to the current position.
Expand Down Expand Up @@ -1061,11 +1059,12 @@ func (p *parser) parseChoiceExpr(ch *choiceExpr) (interface{}, bool) {
}

// {{ end }} ==template==
state := p.cloneState()
for altI, alt := range ch.alternatives {
// dummy assignment to prevent compile error if optimized
_ = altI

state := p.cloneState()

p.pushV()
val, ok := p.parseExpr(alt)
p.popV()
Expand Down
5 changes: 2 additions & 3 deletions doc.go
Expand Up @@ -309,7 +309,7 @@ parser will return. E.g.:
}
The current type is a struct that provides four useful fields that can be
accessed in action and predicate code blocks: "pos", "text", "store" and "globalStore".
accessed in action and predicate code blocks: "pos", "text", "state" and "globalStore".
The "pos" field indicates the current position of the parser in the source
input. It is itself a struct with three fields: "line", "col" and "offset".
Expand All @@ -319,13 +319,12 @@ runes from the start of the line, and offset is a 0-based byte offset.
The "text" field is the slice of bytes of the current match. It is empty
in a predicate code block.
The "store" field is a global store with backtrack support of type "map[string]interface{}",
The "state" field is a global store with backtrack support of type "map[string]interface{}",
which allows to store arbitrary values, which are available in action and
predicate code blocks for read as well as write access.
It is important to notice, that the global store is dependent from
the backtrack mechanism of PEG and and it is set back to its old state
during backtrack.
Changes of "store" field in predicate expressions is not keeped.
The "globalStore" field is a global store of type "map[string]interface{}",
which allows to store arbitrary values, which are available in action and
Expand Down
9 changes: 4 additions & 5 deletions examples/calculator/calculator.go
Expand Up @@ -1066,14 +1066,12 @@ func (p *parser) cloneState() (state statedict) {
}

// restore parser current state to the state statedict.
// every restoreState should applied only one time for every cloned state
func (p *parser) restoreState(state statedict) {
if p.debug {
defer p.out(p.in("restoreState"))
}
p.cur.state = make(statedict)
for k, v := range state {
p.cur.state[k] = v
}
p.cur.state = state
}

// get the slice of bytes from the savepoint start to the current position.
Expand Down Expand Up @@ -1429,11 +1427,12 @@ func (p *parser) parseChoiceExpr(ch *choiceExpr) (interface{}, bool) {
defer p.out(p.in("parseChoiceExpr"))
}

state := p.cloneState()
for altI, alt := range ch.alternatives {
// dummy assignment to prevent compile error if optimized
_ = altI

state := p.cloneState()

p.pushV()
val, ok := p.parseExpr(alt)
p.popV()
Expand Down
9 changes: 4 additions & 5 deletions examples/indentation/indentation.go
Expand Up @@ -1347,14 +1347,12 @@ func (p *parser) cloneState() (state statedict) {
}

// restore parser current state to the state statedict.
// every restoreState should applied only one time for every cloned state
func (p *parser) restoreState(state statedict) {
if p.debug {
defer p.out(p.in("restoreState"))
}
p.cur.state = make(statedict)
for k, v := range state {
p.cur.state[k] = v
}
p.cur.state = state
}

// get the slice of bytes from the savepoint start to the current position.
Expand Down Expand Up @@ -1705,11 +1703,12 @@ func (p *parser) parseChoiceExpr(ch *choiceExpr) (interface{}, bool) {
defer p.out(p.in("parseChoiceExpr"))
}

state := p.cloneState()
for altI, alt := range ch.alternatives {
// dummy assignment to prevent compile error if optimized
_ = altI

state := p.cloneState()

p.pushV()
val, ok := p.parseExpr(alt)
p.popV()
Expand Down
30 changes: 15 additions & 15 deletions examples/indentation/indentation_ast.go
Expand Up @@ -31,12 +31,12 @@ func main() {

var lvalues = make(map[string]int)

// element of statements
type Executor interface {
// Statement is the smallest standalone element
type Statement interface {
exec() error
}

// ProgramNode
// ProgramNode is a root node
type ProgramNode struct {
statements StatementsNode
ret ReturnNode
Expand All @@ -53,17 +53,17 @@ func (n ProgramNode) exec() (int, error) {
return n.ret.exec()
}

// StatementsNode
// StatementsNode is a list of statement
type StatementsNode struct {
statements []Executor
statements []Statement
}

func newStatementsNode(stmts interface{}) (StatementsNode, error) {

st := toIfaceSlice(stmts)
ex := make([]Executor, len(st))
ex := make([]Statement, len(st))
for i, v := range st {
ex[i] = v.(Executor)
ex[i] = v.(Statement)
}
return StatementsNode{ex}, nil
}
Expand All @@ -77,7 +77,7 @@ func (n StatementsNode) exec() error {
return nil
}

// ReturnNode
// ReturnNode return value to the caller.
type ReturnNode struct {
arg IdentifierNode
}
Expand All @@ -90,7 +90,7 @@ func (n ReturnNode) exec() (int, error) {
return v, err
}

// IfNode
// IfNode controls conditional branching.
type IfNode struct {
arg LogicalExpressionNode
statements StatementsNode
Expand All @@ -111,7 +111,7 @@ func (n IfNode) exec() error {
return nil
}

// AssignmentNode
// AssignmentNode gives a value to a variable
type AssignmentNode struct {
lvalue string
rvalue AdditiveExpressionNode
Expand All @@ -129,7 +129,7 @@ func (n AssignmentNode) exec() error {
return nil
}

// LogicalExpressionNode
// LogicalExpressionNode is a logical expression
type LogicalExpressionNode struct {
expr PrimaryExpressionNode
}
Expand All @@ -143,7 +143,7 @@ func (n LogicalExpressionNode) exec() (bool, error) {
return b, err
}

// AdditiveExpressionNode
// AdditiveExpressionNode is a additive expression
type AdditiveExpressionNode struct {
arg1 interface{}
arg2 PrimaryExpressionNode
Expand Down Expand Up @@ -195,7 +195,7 @@ func (n AdditiveExpressionNode) exec() (int, error) {
return v, err
}

// PrimaryExpressionNode
// PrimaryExpressionNode is a basic element
type PrimaryExpressionNode struct {
arg interface{}
}
Expand All @@ -217,7 +217,7 @@ func (n PrimaryExpressionNode) exec() (int, error) {
return v, err
}

// IntegerNode
// IntegerNode is a integer number
type IntegerNode struct {
val int
}
Expand All @@ -230,7 +230,7 @@ func (n IntegerNode) exec() (int, error) {
return n.val, nil
}

// IdentifierNode
// IdentifierNode is a reference to variable
type IdentifierNode struct {
val string
}
Expand Down
9 changes: 4 additions & 5 deletions examples/json/json.go
Expand Up @@ -1360,14 +1360,12 @@ func (p *parser) cloneState() (state statedict) {
}

// restore parser current state to the state statedict.
// every restoreState should applied only one time for every cloned state
func (p *parser) restoreState(state statedict) {
if p.debug {
defer p.out(p.in("restoreState"))
}
p.cur.state = make(statedict)
for k, v := range state {
p.cur.state[k] = v
}
p.cur.state = state
}

// get the slice of bytes from the savepoint start to the current position.
Expand Down Expand Up @@ -1723,11 +1721,12 @@ func (p *parser) parseChoiceExpr(ch *choiceExpr) (interface{}, bool) {
defer p.out(p.in("parseChoiceExpr"))
}

state := p.cloneState()
for altI, alt := range ch.alternatives {
// dummy assignment to prevent compile error if optimized
_ = altI

state := p.cloneState()

p.pushV()
val, ok := p.parseExpr(alt)
p.popV()
Expand Down
9 changes: 4 additions & 5 deletions examples/json/optimized-grammar/json.go
Expand Up @@ -1531,14 +1531,12 @@ func (p *parser) cloneState() (state statedict) {
}

// restore parser current state to the state statedict.
// every restoreState should applied only one time for every cloned state
func (p *parser) restoreState(state statedict) {
if p.debug {
defer p.out(p.in("restoreState"))
}
p.cur.state = make(statedict)
for k, v := range state {
p.cur.state[k] = v
}
p.cur.state = state
}

// get the slice of bytes from the savepoint start to the current position.
Expand Down Expand Up @@ -1894,11 +1892,12 @@ func (p *parser) parseChoiceExpr(ch *choiceExpr) (interface{}, bool) {
defer p.out(p.in("parseChoiceExpr"))
}

state := p.cloneState()
for altI, alt := range ch.alternatives {
// dummy assignment to prevent compile error if optimized
_ = altI

state := p.cloneState()

p.pushV()
val, ok := p.parseExpr(alt)
p.popV()
Expand Down

0 comments on commit 7e45eee

Please sign in to comment.