-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.go
184 lines (153 loc) · 4.48 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package Grammar
import (
"fmt"
"strings"
ds "github.com/PlayerR9/MyGoLib/ListLike/DoubleLL"
)
const (
// LeftToRight is the direction of a production from left to right.
LeftToRight string = "->"
// StartSymbolID is the identifier of the start symbol in the grammar.
StartSymbolID string = "source"
// EndSymbolID is the identifier of the end symbol in the grammar.
EndSymbolID string = "EOF"
// EpsilonSymbolID is the identifier of the epsilon symbol in the grammar.
EpsilonSymbolID string = "ε"
)
// Grammar represents a context-free grammar.
type Grammar struct {
// Productions is a slice of Productions in the grammar.
Productions []Productioner
// LhsToSkip is a slice of productions to skip.
LhsToSkip []string
// Symbols is a slice of Symbols in the grammar.
Symbols []string
}
// NewGrammar is a constructor of an empty Grammar.
//
// A context-free grammar is a set of productions, each of which
// consists of a non-terminal symbol and a sequence of symbols.
//
// The non-terminal symbol is the left-hand side of the production,
// and the sequence of symbols is the right-hand side of the production.
//
// The grammar also contains a set of symbols, which are the
// non-terminal and terminal symbols in the grammar.
//
// Returns:
// - *Grammar: A new empty Grammar.
func NewGrammar() *Grammar {
return &Grammar{
Productions: make([]Productioner, 0),
LhsToSkip: make([]string, 0),
Symbols: make([]string, 0),
}
}
// String is a method of Grammar that returns a string representation
// of a Grammar.
//
// It should only be used for debugging and logging purposes.
//
// Returns:
// - string: A string representation of a Grammar.
func (g *Grammar) String() string {
if g == nil {
return "Grammar[nil]"
}
values := make([]string, 0, len(g.Productions))
for _, production := range g.Productions {
values = append(values, production.String())
}
return fmt.Sprintf(
"Grammar[productions=[%s], symbols=[%s], skipProductions=[%s]]",
strings.Join(values, ", "),
strings.Join(g.Symbols, ", "),
strings.Join(g.LhsToSkip, ", "),
)
}
// RegexMatch returns a slice of MatchedResult that match the input token.
//
// Parameters:
// - at: The position in the input string.
// - b: The input stream to match. Refers to Productioner.Match.
//
// Returns:
// - []MatchedResult: A slice of MatchedResult that match the input token.
func (g *Grammar) RegexMatch(at int, b []byte) []*MatchedResult[*LeafToken] {
matches := make([]*MatchedResult[*LeafToken], 0)
for i, p := range g.Productions {
val, ok := p.(*RegProduction)
if !ok {
continue
}
matched := val.Match(at, b)
if matched != nil {
matches = append(matches, NewMatchResult(matched, i))
}
}
return matches
}
// ProductionMatch returns a slice of MatchedResult that match the input token.
//
// Parameters:
// - at: The position in the input string.
// - stack: The input stream to match. Refers to Productioner.Match.
//
// Returns:
// - []MatchedResult: A slice of MatchedResult that match the input token.
func (g *Grammar) ProductionMatch(at int, stack *ds.DoubleStack[Tokener]) []*MatchedResult[*NonLeafToken] {
matches := make([]*MatchedResult[*NonLeafToken], 0)
for i, p := range g.Productions {
val, ok := p.(*Production)
if !ok {
continue
}
matched, err := val.Match(at, stack)
if err != nil {
matches = append(matches, NewMatchResult(matched, i))
}
}
return matches
}
// GetRegProductions returns a slice of RegProduction in the grammar.
//
// Returns:
// - []*RegProduction: A slice of RegProduction in the grammar.
func (g *Grammar) GetRegProductions() []*RegProduction {
regProds := make([]*RegProduction, 0, len(g.Productions))
for _, p := range g.Productions {
if val, ok := p.(*RegProduction); ok {
regProds = append(regProds, val)
}
}
return regProds
}
// GetProductions returns a slice of Production in the grammar.
//
// Returns:
// - []*Production: A slice of Production in the grammar.
func (g *Grammar) GetProductions() []*Production {
prods := make([]*Production, 0, len(g.Productions))
for _, p := range g.Productions {
if val, ok := p.(*Production); ok {
prods = append(prods, val)
}
}
return prods
}
// Compile compiles the grammar.
//
// It should be called before using the grammar.
//
// Returns:
// - error: An error if the grammar could not be compiled.
func (g *Grammar) compile() error {
regProds := g.GetRegProductions()
for _, p := range regProds {
err := p.Compile()
if err != nil {
return err
}
}
return nil
}