Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 25 additions & 2 deletions src/core/search/lexer.lex
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@

Parser::symbol_type make_StringLit(string_view src, const Parser::location_type& loc);
Parser::symbol_type make_TagVal(string_view src, const Parser::location_type& loc);
Parser::symbol_type make_TagPrefix(string_view src, const Parser::location_type& loc);
%}

blank [ \t\r]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule is useless.
There is no place where it is used.

dq \"
sq \'
esc_chars ['"\?\\abfnrtv]
esc_seq \\{esc_chars}
term_char [_]|\w
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because \w has [ _ ] as part of its rule.
It duplicates it. It's useless, at least.

term_char \w
tag_val_char {term_char}|\\[,.<>{}\[\]\\\"\':;!@#$%^&*()\-+=~\/ ]


Expand Down Expand Up @@ -77,6 +77,7 @@ tag_val_char {term_char}|\\[,.<>{}\[\]\\\"\':;!@#$%^&*()\-+=~\/ ]
{term_char}+"*" return Parser::make_PREFIX(str(), loc());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have now two times + "*"
consider add a variable for *. And may be join this to methods (if possible)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


{term_char}+ return Parser::make_TERM(str(), loc());
{tag_val_char}+"*" return make_TagPrefix(str(), loc());
{tag_val_char}+ return make_TagVal(str(), loc());

<<EOF>> return Parser::make_YYEOF(loc());
Expand Down Expand Up @@ -108,3 +109,25 @@ Parser::symbol_type make_TagVal(string_view src, const Parser::location_type& lo

return Parser::make_TAG_VAL(res, loc);
}

Parser::symbol_type make_TagPrefix(string_view src, const Parser::location_type& loc) {
string res;
res.reserve(src.size());

bool escaped = false;
size_t len = src.size() - 1; // Exclude the '*' at the end
for (size_t i = 0; i < len; ++i) {
if (escaped) {
escaped = false;
} else if (src[i] == '\\') {
escaped = true;
continue;
}
res.push_back(src[i]);
}

// Add '*' back to make it a prefix
res.push_back('*');

return Parser::make_PREFIX(res, loc);
}
32 changes: 32 additions & 0 deletions src/core/search/search_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,36 @@ TEST_F(SearchParserTest, Scanner) {
NEXT_EQ(TOK_DOUBLE, string, "33.3");
}

TEST_F(SearchParserTest, EscapedTagPrefixes) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need also add tests for that to the search_family_test
You can just copy snippet from the user's issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

SetInput("@name:{escape\\-err*}");
NEXT_EQ(TOK_FIELD, string, "@name");
NEXT_TOK(TOK_COLON);
NEXT_TOK(TOK_LCURLBR);
NEXT_EQ(TOK_PREFIX, string, "escape-err*");
NEXT_TOK(TOK_RCURLBR);

SetInput("@name:{escape\\+pre*}");
NEXT_EQ(TOK_FIELD, string, "@name");
NEXT_TOK(TOK_COLON);
NEXT_TOK(TOK_LCURLBR);
NEXT_EQ(TOK_PREFIX, string, "escape+pre*");
NEXT_TOK(TOK_RCURLBR);

SetInput("@name:{escape\\.pre*}");
NEXT_EQ(TOK_FIELD, string, "@name");
NEXT_TOK(TOK_COLON);
NEXT_TOK(TOK_LCURLBR);
NEXT_EQ(TOK_PREFIX, string, "escape.pre*");
NEXT_TOK(TOK_RCURLBR);

SetInput("@name:{complex\\-escape\\+with\\.many\\*chars*}");
NEXT_EQ(TOK_FIELD, string, "@name");
NEXT_TOK(TOK_COLON);
NEXT_TOK(TOK_LCURLBR);
NEXT_EQ(TOK_PREFIX, string, "complex-escape+with.many*chars*");
NEXT_TOK(TOK_RCURLBR);
}

TEST_F(SearchParserTest, Parse) {
EXPECT_EQ(0, Parse(" foo bar (baz) "));
EXPECT_EQ(0, Parse(" -(foo) @foo:bar @ss:[1 2]"));
Expand All @@ -200,6 +230,8 @@ TEST_F(SearchParserTest, Parse) {
EXPECT_EQ(0, Parse("@foo:{1|2.0|4|3.0}"));
EXPECT_EQ(0, Parse("@foo:{1|hello|3.0|world|4}"));

EXPECT_EQ(0, Parse("@name:{escape\\-err*}"));

EXPECT_EQ(1, Parse(" -(foo "));
EXPECT_EQ(1, Parse(" foo:bar "));
EXPECT_EQ(1, Parse(" @foo:@bar "));
Expand Down
Loading