-
Notifications
You must be signed in to change notification settings - Fork 14
/
parse.go
35 lines (28 loc) · 820 Bytes
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Package parse implements a parser for acc programs.
package parse
import (
"io"
"strings"
"github.com/mmcloughlin/addchain/acc/ast"
"github.com/mmcloughlin/addchain/acc/parse/internal/parser"
)
//go:generate pigeon -o internal/parser/zparser.go acc.peg
// File parses filename.
func File(filename string) (*ast.Chain, error) {
return cast(parser.ParseFile(filename))
}
// Reader parses the data from r using filename as information in
// error messages.
func Reader(filename string, r io.Reader) (*ast.Chain, error) {
return cast(parser.ParseReader(filename, r))
}
// String parses s.
func String(s string) (*ast.Chain, error) {
return Reader("string", strings.NewReader(s))
}
func cast(i interface{}, err error) (*ast.Chain, error) {
if err != nil {
return nil, err
}
return i.(*ast.Chain), nil
}