-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.go
44 lines (40 loc) · 1.46 KB
/
grammar.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
// Copyright 2021-2022 Patrick Smith
// Use of this source code is subject to the MIT-style license in the LICENSE file.
package glean
// A RuleAdder can have grammar rules added to it.
//
// Grammar symbols are represented by arbitrary strings. Each rule
// has a name, which should be unique.
//
// If the intent is to write a parser for the grammar, then the
// rule names and symbol strings should be valid Go identifiers.
type RuleAdder interface {
// AddRule adds one rule to the grammar.
//
// Callers should ensure the same name is never used in two calls
// to AddRule.
AddRule(name, target string, items []string) error
}
// A ParserWriter can write a parser (in Go) for a grammar.
type ParserWriter interface {
// ParserWriter writes a grammar parser in Go.
//
// Typically, the caller will write the result into a .go file,
// with a comment marking the file as automatically generated.
//
// The goal argument is the goal symbol that will be the result
// of the parse. The parse function will have the signature
//
// func ([]interface{}) (goal, error)
//
// The packname argument is copied to the package statment in the
// generated code. The prefix is prepended to the names of all
// file-level identifiers; in particular the name of the main
// parse function will be prefix + "Parse".
WriteParser(goal, packname, prefix string) (string, error)
}
// A Grammar can accumulate rules and write a parser.
type Grammar interface {
RuleAdder
ParserWriter
}