Created
July 28, 2014 20:37
-
-
Save akrennmair/c8342505c96f7e76eb96 to your computer and use it in GitHub Desktop.
gopp test how to parse ( foo, bar, baz ) lists.
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
package main | |
import ( | |
"fmt" | |
"github.com/davecgh/go-spew/spew" | |
"github.com/skelterjohn/gopp" | |
"os" | |
"strings" | |
) | |
const grammar = ` | |
ignore: /^(?:[ \t\n])+/ | |
Program => {type=Program} {field=List} <<List>> | |
List => {type=List} '(' [ {field=Field} <<Identifier>> {field=.} ( ',' {field=Field} <<Identifier>> )* ] ')' | |
Identifier => <identifier> | |
identifier = /([a-zA-Z]+)/ | |
` | |
type Program struct { | |
List List | |
} | |
type List struct { | |
Field []string | |
} | |
const example = ` | |
( foobar, quux, blabla ) | |
` | |
func main() { | |
df, err := gopp.NewDecoderFactory(grammar, "Program") | |
if err != nil { | |
fmt.Printf("Creating parser failed: %v\n", err) | |
os.Exit(1) | |
} | |
var program Program | |
dec := df.NewDecoder(strings.NewReader(example)) | |
if err := dec.Decode(&program); err != nil { | |
fmt.Printf("Decoding failed: %v\n", err) | |
os.Exit(1) | |
} | |
fmt.Printf("Object: %s\n", spew.Sdump(program)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment