separated importable package from executable

master
Jordan Orelli 10 years ago
parent 486c8a8e1d
commit 4ef77d7b6c

@ -4,9 +4,9 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"github.com/jordanorelli/moon/lib"
"io" "io"
"os" "os"
"strings"
) )
func input(n int) io.ReadCloser { func input(n int) io.ReadCloser {
@ -25,53 +25,12 @@ func check() {
r := input(1) r := input(1)
defer r.Close() defer r.Close()
_, err := parse(r) _, err := moon.Read(r)
if err != nil { if err != nil {
bail(1, "parse error: %s", err) bail(1, "parse error: %s", err)
} }
} }
func lexx() {
r := input(1)
defer r.Close()
c := lex(r)
for t := range c {
fmt.Println(t)
}
}
func parsse() {
r := input(1)
defer r.Close()
n, err := parse(r)
if err != nil {
bail(1, "parse error: %s", err)
}
if err := n.pretty(os.Stdout, ""); err != nil {
bail(1, "output error: %s", err)
}
}
func eval(r io.Reader) (map[string]interface{}, error) {
n, err := parse(r)
if err != nil {
return nil, fmt.Errorf("parse error: %s\n", err)
}
ctx := make(map[string]interface{})
if _, err := n.eval(ctx); err != nil {
return nil, fmt.Errorf("eval error: %s\n", err)
}
for name, _ := range ctx {
if strings.HasPrefix(name, ".") {
delete(ctx, name)
}
}
return ctx, nil
}
func to() { func to() {
switch flag.Arg(1) { switch flag.Arg(1) {
case "json": case "json":
@ -84,11 +43,11 @@ func to() {
} }
func to_json(n int) { func to_json(n int) {
v, err := eval(input(n)) conf, err := moon.Read(input(n))
if err != nil { if err != nil {
bail(1, "input error: %s", err) bail(1, "input error: %s", err)
} }
b, err := json.MarshalIndent(v, "", " ") b, err := json.MarshalIndent(conf, "", " ")
if err != nil { if err != nil {
bail(1, "encode error: %s", err) bail(1, "encode error: %s", err)
} }
@ -111,10 +70,6 @@ func main() {
switch flag.Arg(0) { switch flag.Arg(0) {
case "check": case "check":
check() check()
case "lex":
lexx()
case "parse":
parsse()
case "to": case "to":
to() to()
case "": case "":

@ -1,6 +1,7 @@
package main package moon
import ( import (
"encoding/json"
"fmt" "fmt"
) )
@ -49,3 +50,7 @@ func (c *Config) GetString(key string) string {
} }
return "" return ""
} }
func (c *Config) MarshalJSON() ([]byte, error) {
return json.Marshal(c.items)
}

@ -1,4 +1,4 @@
package main package moon
import ( import (
"bufio" "bufio"

@ -1,4 +1,4 @@
package main package moon
import ( import (
"bytes" "bytes"

@ -1,4 +1,4 @@
package main package moon
import ( import (
"bufio" "bufio"

@ -1,12 +1,30 @@
package main package moon
import ( import (
"fmt" "fmt"
"io" "io"
"strings"
) )
const () const ()
func Read(r io.Reader) (*Config, error) {
tree, err := parse(r)
if err != nil {
return nil, err
}
ctx := make(map[string]interface{})
if _, err := tree.eval(ctx); err != nil {
return nil, fmt.Errorf("eval error: %s\n", err)
}
for name, _ := range ctx {
if strings.HasPrefix(name, ".") {
delete(ctx, name)
}
}
return &Config{items: ctx}, nil
}
func parse(r io.Reader) (node, error) { func parse(r io.Reader) (node, error) {
p := &parser{ p := &parser{
root: newRootNode(), root: newRootNode(),

@ -1,4 +1,4 @@
package main package moon
import ( import (
"bytes" "bytes"
Loading…
Cancel
Save