From 36e31e28ac118f7e451a749a0dc47d50e2b055f9 Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Tue, 12 Jul 2022 16:22:29 -0700 Subject: [PATCH] Add /s modifier to parser for patterns --- src/parse.y | 14 ++++++++++++-- src/scan.l | 21 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/parse.y b/src/parse.y index 25743e9599..637e40c318 100644 --- a/src/parse.y +++ b/src/parse.y @@ -54,7 +54,7 @@ %left '$' '[' ']' '(' ')' TOK_HAS_FIELD TOK_HAS_ATTR %nonassoc TOK_AS TOK_IS -%type opt_no_test opt_no_test_block TOK_PATTERN_END opt_deep when_flavor +%type opt_no_test opt_no_test_block opt_deep when_flavor %type TOK_ID TOK_PATTERN_TEXT %type local_id global_id def_global_id event_id global_or_event_id resolve_id begin_lambda case_type %type local_id_list case_type_list @@ -77,6 +77,7 @@ %type capture %type capture_list opt_captures when_captures %type when_head when_start when_clause +%type TOK_PATTERN_END %{ #include @@ -324,6 +325,11 @@ static StmtPtr build_local(ID* id, Type* t, InitClass ic, Expr* e, zeek::FuncType::Capture* capture; zeek::FuncType::CaptureList* captures; zeek::detail::WhenInfo* when_clause; + struct + { + bool ignore_case; + bool single_line; + } re_modes; } %% @@ -912,9 +918,13 @@ expr: auto* re = new RE_Matcher($3); delete [] $3; - if ( $4 ) + if ( $4.ignore_case ) re->MakeCaseInsensitive(); + if ( $4.single_line ) + { + } + re->Compile(); $$ = new ConstExpr(make_intrusive(re)); } diff --git a/src/scan.l b/src/scan.l index 17c07c9749..508c21cbfe 100644 --- a/src/scan.l +++ b/src/scan.l @@ -562,13 +562,28 @@ F RET_CONST(zeek::val_mgr->False()->Ref()) "/" { BEGIN(INITIAL); - yylval.b = false; + yylval.re_modes.ignore_case = false; + yylval.re_modes.single_line = false; return TOK_PATTERN_END; } -"/i" { +(\/[is]{0,2}) { BEGIN(INITIAL); - yylval.b = true; + + if (strlen(yytext) == 2) + { + yylval.re_modes.ignore_case = (yytext[1] == 'i'); + yylval.re_modes.single_line = (yytext[1] == 's'); + } + else + { + if ( yytext[1] == yytext[2] ) + zeek::reporter->Error("pattern has duplicate mode %c", yytext[1]); + + yylval.re_modes.ignore_case = (yytext[1] == 'i' || yytext[2] == 'i'); + yylval.re_modes.single_line = (yytext[1] == 's' || yytext[2] == 's'); + } + return TOK_PATTERN_END; }