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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
package grammar
import (
"strconv"
"strings"
)
type Terminal struct {
Number int `json:"number"`
Name string `json:"name"`
Pattern string `json:"pattern"`
Precedence int `json:"prec"`
Associativity string `json:"assoc"`
}
type NonTerminal struct {
Number int `json:"number"`
Name string `json:"name"`
}
type Production struct {
Number int `json:"number"`
LHS int `json:"lhs"`
RHS []int `json:"rhs"`
Precedence int `json:"prec"`
Associativity string `json:"assoc"`
}
type Item struct {
Production int `json:"production"`
Dot int `json:"dot"`
}
type Transition struct {
Symbol int `json:"symbol"`
State int `json:"state"`
}
type Reduce struct {
LookAhead []int `json:"look_ahead"`
Production int `json:"production"`
}
type SRConflict struct {
Symbol int `json:"symbol"`
State int `json:"state"`
Production int `json:"production"`
AdoptedState *int `json:"adopted_state"`
AdoptedProduction *int `json:"adopted_production"`
ResolvedBy int `json:"resolved_by"`
}
type RRConflict struct {
Symbol int `json:"symbol"`
Production1 int `json:"production_1"`
Production2 int `json:"production_2"`
AdoptedProduction int `json:"adopted_production"`
ResolvedBy int `json:"resolved_by"`
}
type State struct {
Number int `json:"number"`
Kernel []*Item `json:"kernel"`
Shift []*Transition `json:"shift"`
Reduce []*Reduce `json:"reduce"`
GoTo []*Transition `json:"goto"`
SRConflict []*SRConflict `json:"sr_conflict"`
RRConflict []*RRConflict `json:"rr_conflict"`
}
type Report struct {
Terminals []*Terminal `json:"terminals"`
NonTerminals []*NonTerminal `json:"non_terminals"`
Productions []*Production `json:"productions"`
States []*State `json:"states"`
}
type CompiledGrammar struct {
Name string `json:"name"`
Lexical *LexicalSpec `json:"lexical"`
Syntactic *SyntacticSpec `json:"syntactic"`
ASTAction *ASTAction `json:"ast_action"`
}
// StateID represents an ID of a state of a transition table.
type StateID int
const (
// StateIDNil represents an empty entry of a transition table.
// When the driver reads this value, it raises an error meaning lexical analysis failed.
StateIDNil = StateID(0)
// StateIDMin is the minimum value of the state ID. All valid state IDs are represented as
// sequential numbers starting from this value.
StateIDMin = StateID(1)
)
func (id StateID) Int() int {
return int(id)
}
// LexModeID represents an ID of a lex mode.
type LexModeID int
const (
LexModeIDNil = LexModeID(0)
LexModeIDDefault = LexModeID(1)
)
func (n LexModeID) String() string {
return strconv.Itoa(int(n))
}
func (n LexModeID) Int() int {
return int(n)
}
func (n LexModeID) IsNil() bool {
return n == LexModeIDNil
}
// LexModeName represents a name of a lex mode.
type LexModeName string
const (
LexModeNameNil = LexModeName("")
LexModeNameDefault = LexModeName("default")
)
func (m LexModeName) String() string {
return string(m)
}
// LexKindID represents an ID of a lexical kind and is unique across all modes.
type LexKindID int
const (
LexKindIDNil = LexKindID(0)
LexKindIDMin = LexKindID(1)
)
func (id LexKindID) Int() int {
return int(id)
}
// LexModeKindID represents an ID of a lexical kind and is unique within a mode.
// Use LexKindID to identify a kind across all modes uniquely.
type LexModeKindID int
const (
LexModeKindIDNil = LexModeKindID(0)
LexModeKindIDMin = LexModeKindID(1)
)
func (id LexModeKindID) Int() int {
return int(id)
}
// LexKindName represents a name of a lexical kind.
type LexKindName string
const LexKindNameNil = LexKindName("")
func (k LexKindName) String() string {
return string(k)
}
type RowDisplacementTable struct {
OriginalRowCount int `json:"original_row_count"`
OriginalColCount int `json:"original_col_count"`
EmptyValue StateID `json:"empty_value"`
Entries []StateID `json:"entries"`
Bounds []int `json:"bounds"`
RowDisplacement []int `json:"row_displacement"`
}
type UniqueEntriesTable struct {
UniqueEntries *RowDisplacementTable `json:"unique_entries,omitempty"`
UncompressedUniqueEntries []StateID `json:"uncompressed_unique_entries,omitempty"`
RowNums []int `json:"row_nums"`
OriginalRowCount int `json:"original_row_count"`
OriginalColCount int `json:"original_col_count"`
EmptyValue int `json:"empty_value"`
}
type TransitionTable struct {
InitialStateID StateID `json:"initial_state_id"`
AcceptingStates []LexModeKindID `json:"accepting_states"`
RowCount int `json:"row_count"`
ColCount int `json:"col_count"`
Transition *UniqueEntriesTable `json:"transition,omitempty"`
UncompressedTransition []StateID `json:"uncompressed_transition,omitempty"`
}
type CompiledLexModeSpec struct {
KindNames []LexKindName `json:"kind_names"`
Push []LexModeID `json:"push"`
Pop []int `json:"pop"`
DFA *TransitionTable `json:"dfa"`
}
type LexicalSpec struct {
InitialModeID LexModeID `json:"initial_mode_id"`
ModeNames []LexModeName `json:"mode_names"`
KindNames []LexKindName `json:"kind_names"`
KindIDs [][]LexKindID `json:"kind_ids"`
CompressionLevel int `json:"compression_level"`
Specs []*CompiledLexModeSpec `json:"specs"`
}
type SyntacticSpec struct {
Action []int `json:"action"`
GoTo []int `json:"goto"`
StateCount int `json:"state_count"`
InitialState int `json:"initial_state"`
StartProduction int `json:"start_production"`
LHSSymbols []int `json:"lhs_symbols"`
AlternativeSymbolCounts []int `json:"alternative_symbol_counts"`
Terminals []string `json:"terminals"`
TerminalCount int `json:"terminal_count"`
TerminalSkip []int `json:"terminal_skip"`
KindToTerminal []int `json:"kind_to_terminal"`
NonTerminals []string `json:"non_terminals"`
NonTerminalCount int `json:"non_terminal_count"`
EOFSymbol int `json:"eof_symbol"`
ErrorSymbol int `json:"error_symbol"`
ErrorTrapperStates []int `json:"error_trapper_states"`
RecoverProductions []int `json:"recover_productions"`
}
type ASTAction struct {
Entries [][]int `json:"entries"`
}
var rep = strings.NewReplacer(
`.`, `\.`,
`*`, `\*`,
`+`, `\+`,
`?`, `\?`,
`|`, `\|`,
`(`, `\(`,
`)`, `\)`,
`[`, `\[`,
`\`, `\\`,
)
// EscapePattern escapes the special characters.
// For example, EscapePattern(`+`) returns `\+`.
func EscapePattern(s string) string {
return rep.Replace(s)
}
|