Skip to content

Commit 174fa2d

Browse files
Merge pull request #63 from coreruleset/fix/comments
fix: divide comment block and remove #
2 parents c17b2dd + f45e09e commit 174fa2d

9 files changed

Lines changed: 7872 additions & 7581 deletions

File tree

g4/SecLangLexer.g4

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@ tokens {
2323
}
2424

2525
WS
26-
: ([ \t\r\n]+ | '\\' '\n' | '\\') -> skip
26+
: ([ \t\r\n\\]+) -> skip
2727
;
2828

29-
COMMENT
30-
: ('#' .*? '\r'? '\n')+ '\n'?
29+
HASH
30+
: '#' -> pushMode(COMMENT_MODE)
3131
;
3232

33-
SPACE
34-
: ' '
35-
;
36-
3733
PIPE_DEFAULT
3834
: '|' -> type(PIPE)
3935
;
@@ -1387,3 +1383,14 @@ AT
13871383
OPERATOR_QUOTED_STRING
13881384
: (('\\"') | ~([" @!])) (('\\"')|~('"'))* -> pushMode(DEFAULT_MODE)
13891385
;
1386+
1387+
1388+
mode COMMENT_MODE;
1389+
1390+
COMMENT
1391+
: (~[\r\n] | '\\' '\r'? '\n')+
1392+
;
1393+
1394+
NEWLINE_COMMENT
1395+
: '\r'? '\n' -> skip,popMode
1396+
;

g4/SecLangParser.g4

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ configuration
2525
;
2626

2727
stmt:
28-
comment? rules_directive variables operator actions?
29-
| comment? rule_script_directive file_path actions?
30-
| comment? rule_script_directive QUOTE file_path QUOTE actions?
31-
| comment? remove_rule_by_id remove_rule_by_id_values+
32-
| comment? string_remove_rules string_remove_rules_values
33-
| comment? string_remove_rules QUOTE string_remove_rules_values QUOTE
34-
| comment? update_target_rules update_target_rules_values update_variables
35-
| comment? update_target_rules QUOTE update_target_rules_values QUOTE update_variables
36-
| comment? update_target_rules update_target_rules_values update_variables PIPE new_target
37-
| comment? update_target_rules QUOTE update_target_rules_values QUOTE update_variables PIPE new_target
38-
| comment? update_action_rule id actions
39-
| comment? engine_config_directive
40-
| comment;
28+
comment* rules_directive variables operator actions?
29+
| comment* rule_script_directive file_path actions?
30+
| comment* rule_script_directive QUOTE file_path QUOTE actions?
31+
| comment* remove_rule_by_id remove_rule_by_id_values+
32+
| comment* string_remove_rules string_remove_rules_values
33+
| comment* string_remove_rules QUOTE string_remove_rules_values QUOTE
34+
| comment* update_target_rules update_target_rules_values update_variables
35+
| comment* update_target_rules QUOTE update_target_rules_values QUOTE update_variables
36+
| comment* update_target_rules update_target_rules_values update_variables PIPE new_target
37+
| comment* update_target_rules QUOTE update_target_rules_values QUOTE update_variables PIPE new_target
38+
| comment* update_action_rule id actions
39+
| comment* engine_config_directive
40+
| comment+;
4141

4242
comment:
43-
COMMENT
43+
HASH COMMENT?
4444
;
4545

4646
rules_directive:

listener.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@ import (
1313
)
1414

1515
type ParserResult struct {
16-
Variables []string `yaml:"variables"`
17-
NegatedVarCount int `yaml:"negated_var_count"`
18-
CollectionLengthCount int `yaml:"collection_length_count"`
19-
Collections []string `yaml:"collections"`
20-
CollectionArgs []string `yaml:"collection_args"`
21-
OperatorList []string `yaml:"operator_list"`
22-
OperatorValueList []string `yaml:"operator_value_list"`
23-
NegatedOperatorCount int `yaml:"negated_operator_count"`
24-
DirectiveList []string `yaml:"directive_list"`
25-
DirectiveValues []string `yaml:"directive_values"`
26-
RangeEvents []string `yaml:"range_events"`
27-
RangeStartEvents []int `yaml:"range_start_events"`
28-
RangeEndEvents []int `yaml:"range_end_events"`
29-
SetvarCollections []string `yaml:"setvar_collections"`
30-
SetvarNames []string `yaml:"setvar_names"`
31-
SetvarOperations []string `yaml:"setvar_operations"`
16+
Comments []string `yaml:"comments"`
17+
Variables []string `yaml:"variables"`
18+
NegatedVarCount int `yaml:"negated_var_count"`
19+
CollectionLengthCount int `yaml:"collection_length_count"`
20+
Collections []string `yaml:"collections"`
21+
CollectionArgs []string `yaml:"collection_args"`
22+
OperatorList []string `yaml:"operator_list"`
23+
OperatorValueList []string `yaml:"operator_value_list"`
24+
NegatedOperatorCount int `yaml:"negated_operator_count"`
25+
DirectiveList []string `yaml:"directive_list"`
26+
DirectiveValues []string `yaml:"directive_values"`
27+
RangeEvents []string `yaml:"range_events"`
28+
RangeStartEvents []int `yaml:"range_start_events"`
29+
RangeEndEvents []int `yaml:"range_end_events"`
30+
SetvarCollections []string `yaml:"setvar_collections"`
31+
SetvarNames []string `yaml:"setvar_names"`
32+
SetvarOperations []string `yaml:"setvar_operations"`
3233
}
3334

3435
type TreeShapeListener struct {
@@ -183,3 +184,12 @@ func (l *TreeShapeListener) EnterCtl_action(ctx *parser.Ctl_actionContext) {
183184
func (l *TreeShapeListener) EnterCtl_id(ctx *parser.Ctl_idContext) {
184185
l.results.DirectiveValues = append(l.results.DirectiveValues, ctx.GetText())
185186
}
187+
188+
func (l *TreeShapeListener) EnterComment(ctx *parser.CommentContext) {
189+
// ctx.COMMENT() can be nil if there is only a HASH without comment text
190+
if ctx.COMMENT() != nil {
191+
l.results.Comments = append(l.results.Comments, ctx.COMMENT().GetText())
192+
} else {
193+
l.results.Comments = append(l.results.Comments, "")
194+
}
195+
}

0 commit comments

Comments
 (0)