aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/syntax/syntax.go
blob: 4df2eb5fe2d522ca8776bf8e46d1577f4da8804e (plain)
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
64
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package syntax

import (
	"fmt"
	"io"
	"os"
)

type Mode uint

// TODO(gri) These need a lot more work.

func ReadFile(filename string, mode Mode) (*File, error) {
	src, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer src.Close()
	return Read(src, mode)
}

type bytesReader struct {
	data []byte
}

func (r *bytesReader) Read(p []byte) (int, error) {
	if len(r.data) > 0 {
		n := copy(p, r.data)
		r.data = r.data[n:]
		return n, nil
	}
	return 0, io.EOF
}

func ReadBytes(src []byte, mode Mode) (*File, error) {
	return Read(&bytesReader{src}, mode)
}

func Read(src io.Reader, mode Mode) (*File, error) {
	var p parser
	p.init(src)

	// skip initial BOM if present
	if p.getr() != '\ufeff' {
		p.ungetr()
	}

	p.next()
	ast := p.file()

	if nerrors > 0 {
		return nil, fmt.Errorf("%d syntax errors", nerrors)
	}

	return ast, nil
}

func Write(w io.Writer, n *File) error {
	panic("unimplemented")
}