-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
63 lines (57 loc) · 1.35 KB
/
parser.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package syntax
import (
"fmt"
merror "github.com/hashicorp/go-multierror"
"github.com/lilac/fun-lang/pkg/ast"
"io"
"os"
"strings"
)
type Source struct {
io.Reader
Path string
}
func NewSourceFromFile(file string) (*Source, error) {
var reader *os.File
var err error
if file == "" {
reader = os.Stdin
} else {
reader, err = os.Open(file)
}
if err != nil {
return nil, err
} else {
return &Source{reader, reader.Name()}, nil
}
}
// NewDummySource make *Source with passed code. This is used for tests.
func NewDummySource(code string) *Source {
return &Source{strings.NewReader(code), "<dummy>"}
}
/*func Parse(source *locerr.Source) (*ast.Module, error) {
input := bytes.NewReader(source.Code)
src := &Source{input, source.Path}
return ParseReader(src)
}*/
func Parse(src *Source) (*ast.Module, error) {
var err error
lexer := NewLexer(src)
lexer.OnError = func(s string) {
e := fmt.Errorf("parse error at %v: %s", src.Path, s)
err = merror.Append(err, e)
}
parser := funNewParser()
status := parser.Parse(lexer)
//fmt.Printf("Parse %s: status = %d\n", src.Path, status)
module := parser.(*funParserImpl).lval.mod
if err != nil {
return module, err
} else if status == 0 {
return module, nil
} else {
pos := lexer.Current()
err := fmt.Errorf("parse error at line %d column %d", pos.Line, pos.Column)
return module, err
}
}