Skip to content

Instantly share code, notes, and snippets.

@nfisher
Last active January 30, 2018 18:34
Show Gist options
  • Save nfisher/649ca816f82bb3ccd7164331ac2324ac to your computer and use it in GitHub Desktop.
Save nfisher/649ca816f82bb3ccd7164331ac2324ac to your computer and use it in GitHub Desktop.
bracket matching with ragel
package main
// generate with ragel -G1 -Z main.rl no compile error with -G2 the following;
// main.rl:13[/Users/nathanfisher/workspace/go/src/github.com/nfisher/gir/command/runner/main.go:119:2]: syntax error: unexpected goto at end of statement
// main.go:59[/Users/nathanfisher/workspace/go/src/github.com/nfisher/gir/command/runner/main.go:59:1]: label _again defined and not used
import "fmt"
%%{
machine graphql_collections;
ObjectValues := |*
'}' => { fret; };
*|;
main := (
'{' @{ fcall ObjectValues; }
)*;
}%%
%%{
prepush {
fmt.Println("push")
c := cap(stack)
l := len(stack)
if stack == nil || l == c {
// c+1 avoids c==0
tmp := make([]int, l, (c+1) * 2)
copy(tmp, stack)
stack = tmp
}
stack = append(stack, 0)
}
postpop {
fmt.Println("pop")
stack = stack[0:len(stack)-1]
}
}%%
%% write data nofinal;
func main() {
data := []byte(`{{}}`)
var stack []int
cs, p, pe, eof := 0, 0, len(data), len(data)
ts, te, act, top := 0, 0, 0, 0
_ = ts
_ = te
_ = eof
_ = act
_ = stack
_ = top
%% write init;
%% write exec;
if ( cs < %%{ write first_final; }%% ) {
fmt.Println("parser error")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment