@@ -85,10 +85,10 @@ func RelaxedReSyntax(yes bool) CompileOption {
85
85
// ErrorOnSlowPattern is an option for [NewCompiler] and [Compile] that
86
86
// tells the compiler to treat slow patterns as errors instead of warnings.
87
87
func ErrorOnSlowPattern (yes bool ) CompileOption {
88
- return func (c * Compiler ) error {
89
- c .errorOnSlowPattern = yes
90
- return nil
91
- }
88
+ return func (c * Compiler ) error {
89
+ c .errorOnSlowPattern = yes
90
+ return nil
91
+ }
92
92
}
93
93
94
94
// A structure that contains the options passed to [Compiler.AddSource].
@@ -107,64 +107,63 @@ type SourceOption func(opt *sourceOptions) error
107
107
// source's origin. This origin appears in error reports, for instance, if
108
108
// if origin is "some_file.yar", error reports will look like:
109
109
//
110
- // error: syntax error
111
- // --> some_file.yar:4:17
112
- // |
113
- // 4 | ... more details
114
- //
110
+ // error: syntax error
111
+ // --> some_file.yar:4:17
112
+ // |
113
+ // 4 | ... more details
115
114
//
116
115
// Example:
117
116
//
118
- // c := NewCompiler()
119
- // c.AddSource("rule some_rule { condition: true }", WithOrigin("some_file.yar"))
117
+ // c := NewCompiler()
118
+ // c.AddSource("rule some_rule { condition: true }", WithOrigin("some_file.yar"))
120
119
func WithOrigin (origin string ) SourceOption {
121
- return func (opts * sourceOptions ) error {
122
- opts .origin = origin
123
- return nil
124
- }
120
+ return func (opts * sourceOptions ) error {
121
+ opts .origin = origin
122
+ return nil
123
+ }
125
124
}
126
125
127
126
// CompileError represents each of the errors returned by [Compiler.Errors].
128
127
type CompileError struct {
129
128
// Error code (e.g: "E001").
130
- Code string
129
+ Code string `json:"code"`
131
130
// Error title (e.g: "unknown identifier `foo`").
132
- Title string
131
+ Title string `json:"title"`
133
132
// Each of the labels in the error report.
134
- Labels []Label
133
+ Labels []Label `json:"labels"`
135
134
// The error's full report, as shown by the command-line tool.
136
- Text string
135
+ Text string `json:"text"`
137
136
}
138
137
139
138
// Warning represents each of the warnings returned by [Compiler.Warnings].
140
139
type Warning struct {
141
140
// Error code (e.g: "slow_pattern").
142
- Code string
141
+ Code string `json:"code"`
143
142
// Error title (e.g: "slow pattern").
144
- Title string
143
+ Title string `json:"title"`
145
144
// Each of the labels in the error report.
146
- Labels []Label
145
+ Labels []Label `json:"labels"`
147
146
// The error's full report, as shown by the command-line tool.
148
- Text string
147
+ Text string `json:"text"`
149
148
}
150
149
151
150
// Label represents a label in a [CompileError].
152
151
type Label struct {
153
152
// Label's level (e.g: "error", "warning", "info", "note", "help").
154
- Level string
153
+ Level string `json:"level"`
155
154
// Origin of the code where the error occurred.
156
- CodeOrigin string
155
+ CodeOrigin string `json:"code_origin"`
157
156
// The code span highlighted by this label.
158
- Span Span
157
+ Span Span `json:"span"`
159
158
// Text associated to the label.
160
- Text string
159
+ Text string `json:"text"`
161
160
}
162
161
163
162
// Span represents the starting and ending point of some piece of source
164
163
// code.
165
164
type Span struct {
166
- Start int
167
- End int
165
+ Start int `json:"start"`
166
+ End int `json:"end"`
168
167
}
169
168
170
169
// Error returns the error's full report.
@@ -174,16 +173,16 @@ func (c CompileError) Error() string {
174
173
175
174
// Compiler represent a YARA compiler.
176
175
type Compiler struct {
177
- cCompiler * C.YRX_COMPILER
178
- relaxedReSyntax bool
176
+ cCompiler * C.YRX_COMPILER
177
+ relaxedReSyntax bool
179
178
errorOnSlowPattern bool
180
179
ignoredModules map [string ]bool
181
180
vars map [string ]interface {}
182
181
features []string
183
182
}
184
183
185
184
// NewCompiler creates a new compiler.
186
- func NewCompiler (opts ... CompileOption ) (* Compiler , error ) {
185
+ func NewCompiler (opts ... CompileOption ) (* Compiler , error ) {
187
186
c := & Compiler {
188
187
ignoredModules : make (map [string ]bool ),
189
188
vars : make (map [string ]interface {}),
@@ -250,11 +249,11 @@ func (c *Compiler) initialize() error {
250
249
//
251
250
// Examples:
252
251
//
253
- // c := NewCompiler()
254
- // c.AddSource("rule foo { condition: true }")
255
- // c.AddSource("rule bar { condition: true }")
256
- // c.AddSource("rule baz { condition: true }", WithOrigin("baz.yar"))
257
- func (c * Compiler ) AddSource (src string , opts ... SourceOption ) error {
252
+ // c := NewCompiler()
253
+ // c.AddSource("rule foo { condition: true }")
254
+ // c.AddSource("rule bar { condition: true }")
255
+ // c.AddSource("rule baz { condition: true }", WithOrigin("baz.yar"))
256
+ func (c * Compiler ) AddSource (src string , opts ... SourceOption ) error {
258
257
options := & sourceOptions {}
259
258
for _ , opt := range opts {
260
259
opt (options )
@@ -264,10 +263,10 @@ func (c *Compiler) AddSource(src string, opts... SourceOption) error {
264
263
defer C .free (unsafe .Pointer (cSrc ))
265
264
266
265
var cOrigin * C.char
267
- if options .origin != "" {
268
- cOrigin = C .CString (options .origin )
269
- defer C .free (unsafe .Pointer (cOrigin ))
270
- }
266
+ if options .origin != "" {
267
+ cOrigin = C .CString (options .origin )
268
+ defer C .free (unsafe .Pointer (cOrigin ))
269
+ }
271
270
272
271
// The call to runtime.LockOSThread() is necessary to make sure that
273
272
// yrx_compiler_add_source and yrx_last_error are called from the same OS
@@ -322,16 +321,16 @@ func (c *Compiler) ignoreModule(module string) {
322
321
//
323
322
// Examples:
324
323
//
325
- // c := NewCompiler()
326
- // // Add some rule named "foo" under the default namespace
327
- // c.AddSource("rule foo { condition: true }")
324
+ // c := NewCompiler()
325
+ // // Add some rule named "foo" under the default namespace
326
+ // c.AddSource("rule foo { condition: true }")
328
327
//
329
- // // Create a new namespace named "bar"
330
- // c.NewNamespace("bar")
328
+ // // Create a new namespace named "bar"
329
+ // c.NewNamespace("bar")
331
330
//
332
- // // It's ok to add another rule named "foo", as it is in a different
333
- // // namespace than the previous one.
334
- // c.AddSource("rule foo { condition: true }")
331
+ // // It's ok to add another rule named "foo", as it is in a different
332
+ // // namespace than the previous one.
333
+ // c.AddSource("rule foo { condition: true }")
335
334
func (c * Compiler ) NewNamespace (namespace string ) {
336
335
cNamespace := C .CString (namespace )
337
336
defer C .free (unsafe .Pointer (cNamespace ))
0 commit comments