Merge branch 'master' into recipe_handler_refactoring_part1

This commit is contained in:
Martin Robertz authored and GitHub committed 2025-10-04 13:52:59 +02:00
commit caeb72a723
5 files changed
+39 -27

No files matched your search

+6 -4
View File
@@ -18,20 +18,22 @@ public SearchExpressionLexer(CharStream input, SearchTokenParser searchParser) {
// Lexer rules
REGEX_LEFT : 'r'? '/' -> pushMode(REGEX) ;
QUOTE_LEFT : '"' -> pushMode(QUOTED) ;
DASH : '-' ;
RECIPE_INGREDIENTS : '<&' | '<' ;
RECIPE_RESULT : '>&' | '>' ;
RECIPE_OTHERS : '^&' | '^' ;
PREFIX : CLEAN_SYMBOL {searchParser.hasRedefinedPrefix(getText().charAt(0))}? ;
OR : '|' ;
LEFT_BRACKET : '{' ;
RIGHT_BRACKET : '}' ;
PLAIN_TEXT : (CLEAN_SYMBOL | ESCAPED_SYMBOL)+ {!searchParser.hasRedefinedPrefix(getText().charAt(0))}? ;
NEWLINE_OR_TAB : [\t\r\n] -> skip ;
SPACE : ' ' ;
ESCAPED_SYMBOL : '\\' . ;
fragment ESCAPED_SYMBOL : '\\' . ;
fragment CLEAN_SYMBOL : ~[-<>^{}|/\\ "] ;
PREFIX : CLEAN_SYMBOL {searchParser.hasRedefinedPrefix(getText().charAt(0))}? ;
CLEAN_SYMBOL : ~[-<>^{}|/\\ "] ;
mode REGEX;
+4 -1
View File
@@ -56,7 +56,7 @@ prefixedExpression
token[Character prefix]
: regex[prefix]
| quoted[prefix]
| PLAIN_TEXT
| plainText
;
regex[Character prefix]
@@ -67,4 +67,7 @@ quoted[Character prefix]
: QUOTE_LEFT QUOTED_CONTENT QUOTE_RIGHT?
;
plainText
: (CLEAN_SYMBOL | ESCAPED_SYMBOL)+
;
@@ -20,7 +20,7 @@ import codechicken.nei.api.ItemFilter;
public class ItemFilterVisitor extends AbstractSearchExpressionVisitor<ItemFilter> {
private static final Pattern REGEX_ESCAPED_SPACE_PATTERN = Pattern.compile("([^\\\\](?:\\\\\\\\)+)?\\\\ ");
private static final Pattern PLAIN_TEXT_ESCAPED_PATTERN = Pattern.compile("\\\\(.)");
private static final Pattern ESCAPED_SYMBOL_PATTERN = Pattern.compile("\\\\(.)");
private static final Pattern ESCAPED_QUOTE_PATTERN = Pattern.compile("\\\\\"");
private static final Pattern ESCAPED_SPACE_PATTERN = Pattern.compile("\\\\ ");
@@ -69,21 +69,21 @@ public class ItemFilterVisitor extends AbstractSearchExpressionVisitor<ItemFilte
@Override
public ItemFilter visitPrefixedExpression(SearchExpressionParser.PrefixedExpressionContext ctx) {
if (ctx.token() != null) {
final String cleanText = getTokenCleanText(ctx.token());
final String cleanPattern = getCleanPattern(ctx.token());
final SearchTokenParser.ISearchParserProvider provider = searchParser.getProvider(ctx.token().prefix);
if (cleanText == null || provider == null) {
if (cleanPattern == null || provider == null) {
return defaultResult();
}
return provider.getFilter(cleanText);
return provider.getFilter(cleanPattern);
}
return defaultResult();
}
@Override
public ItemFilter visitToken(SearchExpressionParser.TokenContext ctx) {
final String cleanText = getTokenCleanText(ctx);
if (cleanText != null) {
return getAlwaysProvidersFilter(cleanText);
final String cleanPattern = getCleanPattern(ctx);
if (cleanPattern != null) {
return getAlwaysProvidersFilter(cleanPattern);
}
return defaultResult();
}
@@ -118,36 +118,36 @@ public class ItemFilterVisitor extends AbstractSearchExpressionVisitor<ItemFilte
return constructFilter(filters, AllMultiItemFilter::new);
}
private String getTokenCleanText(SearchExpressionParser.TokenContext ctx) {
String cleanText = null;
if (ctx.PLAIN_TEXT() != null) {
cleanText = Pattern.quote(
PLAIN_TEXT_ESCAPED_PATTERN.matcher(ctx.PLAIN_TEXT().getSymbol().getText())
private String getCleanPattern(SearchExpressionParser.TokenContext ctx) {
String cleanPattern = null;
if (ctx.plainText() != null) {
cleanPattern = Pattern.quote(
ESCAPED_SYMBOL_PATTERN.matcher(ctx.plainText().getText())
// Unescape everything in search expression
.replaceAll("$1"));
} else if (ctx.regex() != null) {
if (ctx.regex().REGEX_CONTENT() != null) {
cleanText = ctx.regex().REGEX_CONTENT().getSymbol().getText();
cleanPattern = ctx.regex().REGEX_CONTENT().getSymbol().getText();
final int spaceModeEnabled = NEIClientConfig.getIntSetting("inventory.search.spaceMode");
// Unescape spaces
if (spaceModeEnabled == 1) {
cleanText = REGEX_ESCAPED_SPACE_PATTERN.matcher(cleanText).replaceAll("$1 ");
cleanPattern = REGEX_ESCAPED_SPACE_PATTERN.matcher(cleanPattern).replaceAll("$1 ");
}
}
} else if (ctx.quoted() != null) {
if (ctx.quoted().QUOTED_CONTENT() != null) {
cleanText = ctx.quoted().QUOTED_CONTENT().getSymbol().getText();
cleanPattern = ctx.quoted().QUOTED_CONTENT().getSymbol().getText();
// Unescape quotes
cleanText = ESCAPED_QUOTE_PATTERN.matcher(cleanText).replaceAll("\"");
cleanText = Pattern.quote(cleanText);
cleanPattern = ESCAPED_QUOTE_PATTERN.matcher(cleanPattern).replaceAll("\"");
cleanPattern = Pattern.quote(cleanPattern);
final int spaceModeEnabled = NEIClientConfig.getIntSetting("inventory.search.spaceMode");
// Unescape spaces
if (spaceModeEnabled == 1) {
cleanText = ESCAPED_SPACE_PATTERN.matcher(cleanText).replaceAll(" ");
cleanPattern = ESCAPED_SPACE_PATTERN.matcher(cleanPattern).replaceAll(" ");
}
}
}
return cleanText;
return cleanPattern;
}
private ItemFilter visitChildren(ParserRuleContext node, Function<List<ItemFilter>, ItemFilter> filterConstructor) {
@@ -62,8 +62,8 @@ public class SearchExpressionFormatVisitor extends AbstractSearchExpressionVisit
private String getTokenCleanText(SearchExpressionParser.TokenContext ctx, Character prefix) {
String cleanText = null;
final int spaceModeEnabled = NEIClientConfig.getIntSetting("inventory.search.spaceMode");
if (ctx.PLAIN_TEXT() != null) {
cleanText = ctx.PLAIN_TEXT().getSymbol().getText();
if (ctx.plainText() != null) {
cleanText = ctx.plainText().getText();
// Unescape spaces
if (spaceModeEnabled == 1) {
cleanText = ESCAPED_SPACE_PATTERN.matcher(cleanText).replaceAll(" ");
@@ -88,6 +88,11 @@ public class SearchTokenParserTest {
protected String getIdentifier(ItemStack stack) {
return getStringIdentifier(stack);
}
@Override
public String toString() {
return "PREFIX(" + pattern.toString() + ")";
}
};
private static MockedStatic<NEIClientConfig> config;
@@ -284,12 +289,14 @@ public class SearchTokenParserTest {
searchParser.addProvider(defaultParserProvider);
searchParser
.addProvider(parserProvider('?', "custom", SearchTokenParser.SearchMode.PREFIX, customCreateFilter));
searchParser
.addProvider(parserProvider('*', "custom2", SearchTokenParser.SearchMode.PREFIX, customCreateFilter));
Set<ItemStack> matchedItems = testFilterAgainstAllItems(
spaceMode,
patternMode,
searchParser,
"\"" + itemA.getDisplayName() + "\"?A");
"\"" + itemA.getDisplayName() + "\"?A*A");
assertEquals(1, matchedItems.size());
assertTrue(matchedItems.contains(itemA));