ref: ccaaf5e5e5e2660778cbb9390f8cb81a6b245a41 fastpeg/internal/bootstrap/parser/parser.go -rw-r--r-- 1.2 KiB View raw
                                                                                
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
// Package parser implements a parser for the fastpeg language.
// Input may be provided in a variety of forms, the output is
// an abstract syntax tree (AST) representing the parsed grammar.
// The parser is invoked through one of the Parse* functions.
package parser

import (
	"errors"
	"io/ioutil"

	"git.sr.ht/~mna/fastpeg/internal/ast"
	"git.sr.ht/~mna/fastpeg/internal/bootstrap/token"
)

// ParseFiles parses the content of the provided filenames in a single
// Grammar AST node.
func ParseFiles(filenames ...string) (*ast.Grammar, error) {
	inputs := make(map[string][]byte, len(filenames))
	for _, filename := range filenames {
		b, err := ioutil.ReadFile(filename)
		if err != nil {
			return nil, err
		}
		inputs[filename] = b
	}
	return ParseInputs(inputs)
}

// ParseInputs parses the provided inputs in a single Grammar AST node.
// The key of the map is the corresponding filename used for error
// reporting.
func ParseInputs(inputs map[string][]byte) (*ast.Grammar, error) {
	fs := token.NewFileSet()
	for file, input := range inputs {
		f := fs.AddFile(file, -1, len(input))
		_ = f
		// TODO: parse f...
	}
	return nil, errors.New("not implemented")
}

type parser struct {
}