@@ -75,10 +75,10 @@ func RelaxedReSyntax(yes bool) CompileOption {
75
75
// ErrorOnSlowPattern is an option for [NewCompiler] and [Compile] that
76
76
// tells the compiler to treat slow patterns as errors instead of warnings.
77
77
func ErrorOnSlowPattern (yes bool ) CompileOption {
78
- return func (c * Compiler ) error {
79
- c .errorOnSlowPattern = yes
80
- return nil
81
- }
78
+ return func (c * Compiler ) error {
79
+ c .errorOnSlowPattern = yes
80
+ return nil
81
+ }
82
82
}
83
83
84
84
// A structure that contains the options passed to [Compiler.AddSource].
@@ -97,64 +97,63 @@ type SourceOption func(opt *sourceOptions) error
97
97
// source's origin. This origin appears in error reports, for instance, if
98
98
// if origin is "some_file.yar", error reports will look like:
99
99
//
100
- // error: syntax error
101
- // --> some_file.yar:4:17
102
- // |
103
- // 4 | ... more details
104
- //
100
+ // error: syntax error
101
+ // --> some_file.yar:4:17
102
+ // |
103
+ // 4 | ... more details
105
104
//
106
105
// Example:
107
106
//
108
- // c := NewCompiler()
109
- // c.AddSource("rule some_rule { condition: true }", WithOrigin("some_file.yar"))
107
+ // c := NewCompiler()
108
+ // c.AddSource("rule some_rule { condition: true }", WithOrigin("some_file.yar"))
110
109
func WithOrigin (origin string ) SourceOption {
111
- return func (opts * sourceOptions ) error {
112
- opts .origin = origin
113
- return nil
114
- }
110
+ return func (opts * sourceOptions ) error {
111
+ opts .origin = origin
112
+ return nil
113
+ }
115
114
}
116
115
117
116
// CompileError represents each of the errors returned by [Compiler.Errors].
118
117
type CompileError struct {
119
118
// Error code (e.g: "E001").
120
- Code string
119
+ Code string `json:"code"`
121
120
// Error title (e.g: "unknown identifier `foo`").
122
- Title string
121
+ Title string `json:"title"`
123
122
// Each of the labels in the error report.
124
- Labels []Label
123
+ Labels []Label `json:"labels"`
125
124
// The error's full report, as shown by the command-line tool.
126
- Text string
125
+ Text string `json:"text"`
127
126
}
128
127
129
128
// Warning represents each of the warnings returned by [Compiler.Warnings].
130
129
type Warning struct {
131
130
// Error code (e.g: "slow_pattern").
132
- Code string
131
+ Code string `json:"code"`
133
132
// Error title (e.g: "slow pattern").
134
- Title string
133
+ Title string `json:"title"`
135
134
// Each of the labels in the error report.
136
- Labels []Label
135
+ Labels []Label `json:"labels"`
137
136
// The error's full report, as shown by the command-line tool.
138
- Text string
137
+ Text string `json:"text"`
139
138
}
140
139
141
140
// Label represents a label in a [CompileError].
142
141
type Label struct {
143
142
// Label's level (e.g: "error", "warning", "info", "note", "help").
144
- Level string
143
+ Level string `json:"level"`
145
144
// Origin of the code where the error occurred.
146
- CodeOrigin string
145
+ CodeOrigin string `json:"code_origin"`
147
146
// The code span highlighted by this label.
148
- Span Span
147
+ Span Span `json:"span"`
149
148
// Text associated to the label.
150
- Text string
149
+ Text string `json:"text"`
151
150
}
152
151
153
152
// Span represents the starting and ending point of some piece of source
154
153
// code.
155
154
type Span struct {
156
- Start int
157
- End int
155
+ Start int `json:"start"`
156
+ End int `json:"end"`
158
157
}
159
158
160
159
// Error returns the error's full report.
@@ -164,18 +163,18 @@ func (c CompileError) Error() string {
164
163
165
164
// Compiler represent a YARA compiler.
166
165
type Compiler struct {
167
- cCompiler * C.YRX_COMPILER
168
- relaxedReSyntax bool
166
+ cCompiler * C.YRX_COMPILER
167
+ relaxedReSyntax bool
169
168
errorOnSlowPattern bool
170
- ignoredModules map [string ]bool
171
- vars map [string ]interface {}
169
+ ignoredModules map [string ]bool
170
+ vars map [string ]interface {}
172
171
}
173
172
174
173
// NewCompiler creates a new compiler.
175
- func NewCompiler (opts ... CompileOption ) (* Compiler , error ) {
174
+ func NewCompiler (opts ... CompileOption ) (* Compiler , error ) {
176
175
c := & Compiler {
177
176
ignoredModules : make (map [string ]bool ),
178
- vars : make (map [string ]interface {}),
177
+ vars : make (map [string ]interface {}),
179
178
}
180
179
181
180
for _ , opt := range opts {
@@ -235,11 +234,11 @@ func (c *Compiler) initialize() error {
235
234
//
236
235
// Examples:
237
236
//
238
- // c := NewCompiler()
239
- // c.AddSource("rule foo { condition: true }")
240
- // c.AddSource("rule bar { condition: true }")
241
- // c.AddSource("rule baz { condition: true }", WithOrigin("baz.yar"))
242
- func (c * Compiler ) AddSource (src string , opts ... SourceOption ) error {
237
+ // c := NewCompiler()
238
+ // c.AddSource("rule foo { condition: true }")
239
+ // c.AddSource("rule bar { condition: true }")
240
+ // c.AddSource("rule baz { condition: true }", WithOrigin("baz.yar"))
241
+ func (c * Compiler ) AddSource (src string , opts ... SourceOption ) error {
243
242
options := & sourceOptions {}
244
243
for _ , opt := range opts {
245
244
opt (options )
@@ -249,10 +248,10 @@ func (c *Compiler) AddSource(src string, opts... SourceOption) error {
249
248
defer C .free (unsafe .Pointer (cSrc ))
250
249
251
250
var cOrigin * C.char
252
- if options .origin != "" {
253
- cOrigin = C .CString (options .origin )
254
- defer C .free (unsafe .Pointer (cOrigin ))
255
- }
251
+ if options .origin != "" {
252
+ cOrigin = C .CString (options .origin )
253
+ defer C .free (unsafe .Pointer (cOrigin ))
254
+ }
256
255
257
256
// The call to runtime.LockOSThread() is necessary to make sure that
258
257
// yrx_compiler_add_source and yrx_last_error are called from the same OS
@@ -295,16 +294,16 @@ func (c *Compiler) ignoreModule(module string) {
295
294
//
296
295
// Examples:
297
296
//
298
- // c := NewCompiler()
299
- // // Add some rule named "foo" under the default namespace
300
- // c.AddSource("rule foo { condition: true }")
297
+ // c := NewCompiler()
298
+ // // Add some rule named "foo" under the default namespace
299
+ // c.AddSource("rule foo { condition: true }")
301
300
//
302
- // // Create a new namespace named "bar"
303
- // c.NewNamespace("bar")
301
+ // // Create a new namespace named "bar"
302
+ // c.NewNamespace("bar")
304
303
//
305
- // // It's ok to add another rule named "foo", as it is in a different
306
- // // namespace than the previous one.
307
- // c.AddSource("rule foo { condition: true }")
304
+ // // It's ok to add another rule named "foo", as it is in a different
305
+ // // namespace than the previous one.
306
+ // c.AddSource("rule foo { condition: true }")
308
307
func (c * Compiler ) NewNamespace (namespace string ) {
309
308
cNamespace := C .CString (namespace )
310
309
defer C .free (unsafe .Pointer (cNamespace ))
0 commit comments