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
9 changes: 6 additions & 3 deletions g4/SecLangLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1384,13 +1384,16 @@ OPERATOR_QUOTED_STRING
: (('\\"') | ~([" @!])) (('\\"')|~('"'))* -> pushMode(DEFAULT_MODE)
;


mode COMMENT_MODE;

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

NEWLINE_COMMENT
HASH_COMMENT_BLOCK
: '\r'? '\n' -> skip,popMode
;

BLOCK_COMMENT_END
: '\r'? '\n' '\r'? '\n' -> popMode
;
30 changes: 17 additions & 13 deletions g4/SecLangParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ configuration
;

stmt:
comment* engine_config_rule_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_block? engine_config_rule_directive variables operator actions?
| comment_block? rule_script_directive file_path actions?
| comment_block? rule_script_directive QUOTE file_path QUOTE actions?
| comment_block? remove_rule_by_id remove_rule_by_id_values+
| comment_block? string_remove_rules string_remove_rules_values
| comment_block? string_remove_rules QUOTE string_remove_rules_values QUOTE
| comment_block? update_target_rules update_target_rules_values update_variables
| comment_block? update_target_rules QUOTE update_target_rules_values QUOTE update_variables
| comment_block? update_target_rules update_target_rules_values update_variables PIPE new_target
| comment_block? update_target_rules QUOTE update_target_rules_values QUOTE update_variables PIPE new_target
| comment_block? update_action_rule id actions
| comment_block? engine_config_directive
| comment_block+;

comment_block:
comment+ (BLOCK_COMMENT_END | EOF)?
;

comment:
HASH COMMENT?
Expand Down
5 changes: 5 additions & 0 deletions listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

type ParserResult struct {
Comments []string `yaml:"comments"`
CommentsBlocks int `yaml:"comment_blocks"`
Variables []string `yaml:"variables"`
NegatedVarCount int `yaml:"negated_var_count"`
CollectionLengthCount int `yaml:"collection_length_count"`
Expand Down Expand Up @@ -197,3 +198,7 @@ func (l *TreeShapeListener) EnterComment(ctx *parser.CommentContext) {
func (l *TreeShapeListener) EnterTransformation_action_value(ctx *parser.Transformation_action_valueContext) {
l.results.DirectiveValues = append(l.results.DirectiveValues, ctx.GetText())
}

func (l *TreeShapeListener) EnterComment_block(ctx *parser.Comment_blockContext) {
l.results.CommentsBlocks++
}
4,999 changes: 2,502 additions & 2,497 deletions parser/seclang_lexer.go

Large diffs are not rendered by default.

1,958 changes: 1,031 additions & 927 deletions parser/seclang_parser.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions parser/seclangparser_base_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions parser/seclangparser_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4,359 changes: 2,182 additions & 2,177 deletions src/seclang_parser/SecLangLexer.py

Large diffs are not rendered by default.

1,722 changes: 885 additions & 837 deletions src/seclang_parser/SecLangParser.py

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/seclang_parser/SecLangParserListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def exitStmt(self, ctx:SecLangParser.StmtContext):
pass


# Enter a parse tree produced by SecLangParser#comment_block.
def enterComment_block(self, ctx:SecLangParser.Comment_blockContext):
pass

# Exit a parse tree produced by SecLangParser#comment_block.
def exitComment_block(self, ctx:SecLangParser.Comment_blockContext):
pass


# Enter a parse tree produced by SecLangParser#comment.
def enterComment(self, ctx:SecLangParser.CommentContext):
pass
Expand Down
7 changes: 6 additions & 1 deletion src/seclang_parser/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ParserResult:
"""Stores results from parsing for test validation."""

comments: list[str] = field(default_factory=list)
comment_blocks: int = 0
variables: list[str] = field(default_factory=list)
negated_var_count: int = 0
collection_length_count: int = 0
Expand Down Expand Up @@ -133,6 +134,10 @@ def enterComment(self, ctx: SecLangParser.CommentContext):
self.results.comments.append(ctx.COMMENT().getText())
else:
self.results.comments.append("")
# print(ctx.stop.getLine())

def enterTransformation_action_value(self, ctx: SecLangParser.Transformation_action_valueContext):
self.results.directive_values.append(ctx.getText())
self.results.directive_values.append(ctx.getText())

def enterComment_block(self, ctx: SecLangParser.Comment_blockContext):
self.results.comment_blocks += 1
7 changes: 7 additions & 0 deletions testdata/test_44_comments.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# This is a comment
#
# This is a multiline \
# comment
#
# This is a multiline \
16 changes: 16 additions & 0 deletions testdata/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ check_output_tests:
expected_result:
comments:
- " Test comment"
comment_blocks: 1
variables:
- REQUEST_FILENAME
- REQUEST_URI
Expand Down Expand Up @@ -273,6 +274,7 @@ check_output_tests:
- " The variable is a numerical representation of the CRS version number."
- " E.g., v3.0.0 is represented as 300."
- ""
comment_blocks: 1
variables:
- REQUEST_URI
negated_var_count: 2
Expand Down Expand Up @@ -343,6 +345,7 @@ check_output_tests:
comments:
- " Force body variable"
- " Force body processor URLENCODED"
comment_blocks: 2
variables:
- REQBODY_PROCESSOR
- REQBODY_PROCESSOR
Expand Down Expand Up @@ -420,3 +423,16 @@ check_output_tests:
- "1"
- "/%{tx.0}/"
- "%{tx.critical_anomaly_score}"
testdata/test_44_comments.conf:
error_count: 0
comment: ""
expected_result:
comments:
- ""
- " This is a comment"
- ""
- " This is a multiline \\"
- " comment"
- ""
- " This is a multiline \\"
comment_blocks: 1