Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions g4/SecLangLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ tokens {
}

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

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

SPACE
: ' '
;

PIPE_DEFAULT
: '|' -> type(PIPE)
;
Expand Down Expand Up @@ -1387,3 +1383,14 @@ AT
OPERATOR_QUOTED_STRING
: (('\\"') | ~([" @!])) (('\\"')|~('"'))* -> pushMode(DEFAULT_MODE)
;


mode COMMENT_MODE;

COMMENT
: (~[\r\n] | '\\' '\r'? '\n')+
;

NEWLINE_COMMENT
: '\r'? '\n' -> skip,popMode
;
28 changes: 14 additions & 14 deletions g4/SecLangParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ configuration
;

stmt:
comment? rules_directive variables operator actions?
| comment? rule_script_directive file_path actions?
| comment? rule_script_directive QUOTE file_path QUOTE actions?
| comment? remove_rule_by_id remove_rule_by_id_values+
| comment? string_remove_rules string_remove_rules_values
| comment? string_remove_rules QUOTE string_remove_rules_values QUOTE
| comment? update_target_rules update_target_rules_values update_variables
| comment? update_target_rules QUOTE update_target_rules_values QUOTE update_variables
| comment? update_target_rules update_target_rules_values update_variables PIPE new_target
| comment? update_target_rules QUOTE update_target_rules_values QUOTE update_variables PIPE new_target
| comment? update_action_rule id actions
| comment? engine_config_directive
| comment;
comment* rules_directive variables operator actions?
| comment* rule_script_directive file_path actions?
| comment* rule_script_directive QUOTE file_path QUOTE actions?
| comment* remove_rule_by_id remove_rule_by_id_values+
| comment* string_remove_rules string_remove_rules_values
| comment* string_remove_rules QUOTE string_remove_rules_values QUOTE
| comment* update_target_rules update_target_rules_values update_variables
| comment* update_target_rules QUOTE update_target_rules_values QUOTE update_variables
| comment* update_target_rules update_target_rules_values update_variables PIPE new_target
| comment* update_target_rules QUOTE update_target_rules_values QUOTE update_variables PIPE new_target
| comment* update_action_rule id actions
| comment* engine_config_directive
| comment+;

comment:
COMMENT
HASH COMMENT?
;

rules_directive:
Expand Down
42 changes: 26 additions & 16 deletions listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ import (
)

type ParserResult struct {
Variables []string `yaml:"variables"`
NegatedVarCount int `yaml:"negated_var_count"`
CollectionLengthCount int `yaml:"collection_length_count"`
Collections []string `yaml:"collections"`
CollectionArgs []string `yaml:"collection_args"`
OperatorList []string `yaml:"operator_list"`
OperatorValueList []string `yaml:"operator_value_list"`
NegatedOperatorCount int `yaml:"negated_operator_count"`
DirectiveList []string `yaml:"directive_list"`
DirectiveValues []string `yaml:"directive_values"`
RangeEvents []string `yaml:"range_events"`
RangeStartEvents []int `yaml:"range_start_events"`
RangeEndEvents []int `yaml:"range_end_events"`
SetvarCollections []string `yaml:"setvar_collections"`
SetvarNames []string `yaml:"setvar_names"`
SetvarOperations []string `yaml:"setvar_operations"`
Comments []string `yaml:"comments"`
Variables []string `yaml:"variables"`
NegatedVarCount int `yaml:"negated_var_count"`
CollectionLengthCount int `yaml:"collection_length_count"`
Collections []string `yaml:"collections"`
CollectionArgs []string `yaml:"collection_args"`
OperatorList []string `yaml:"operator_list"`
OperatorValueList []string `yaml:"operator_value_list"`
NegatedOperatorCount int `yaml:"negated_operator_count"`
DirectiveList []string `yaml:"directive_list"`
DirectiveValues []string `yaml:"directive_values"`
RangeEvents []string `yaml:"range_events"`
RangeStartEvents []int `yaml:"range_start_events"`
RangeEndEvents []int `yaml:"range_end_events"`
SetvarCollections []string `yaml:"setvar_collections"`
SetvarNames []string `yaml:"setvar_names"`
SetvarOperations []string `yaml:"setvar_operations"`
}

type TreeShapeListener struct {
Expand Down Expand Up @@ -183,3 +184,12 @@ func (l *TreeShapeListener) EnterCtl_action(ctx *parser.Ctl_actionContext) {
func (l *TreeShapeListener) EnterCtl_id(ctx *parser.Ctl_idContext) {
l.results.DirectiveValues = append(l.results.DirectiveValues, ctx.GetText())
}

func (l *TreeShapeListener) EnterComment(ctx *parser.CommentContext) {
// ctx.COMMENT() can be nil if there is only a HASH without comment text
if ctx.COMMENT() != nil {
l.results.Comments = append(l.results.Comments, ctx.COMMENT().GetText())
} else {
l.results.Comments = append(l.results.Comments, "")
}
}
Loading