Skip to content

Instantly share code, notes, and snippets.

@hungtcs
Last active December 13, 2023 06:06
Show Gist options
  • Save hungtcs/a48bea4fb9b7d8fa354f7904ada6c76c to your computer and use it in GitHub Desktop.
Save hungtcs/a48bea4fb9b7d8fa354f7904ada6c76c to your computer and use it in GitHub Desktop.
搜索过滤表达式匹配

可以匹配类似 Github 的搜索表达式,如

author:hungtcs keyword

image

grammar Expr;
fragment ESC: '\\' .;
fragment DIGIT: [0-9];
WS: [ \t\r\n]+ -> skip;
COMMA: ',';
EXCLUDE: '-';
KEYWORLD: ~[ \t\r\n:,-]+;
QUOTED_STRING: '"' (ESC | ~["\\])* '"';
GT: ':>';
LT: ':<';
EQ: ':';
NE: ':!=';
GE: ':>=';
LE: ':<=';
DATE: DIGIT DIGIT DIGIT DIGIT '-' DIGIT DIGIT '-' DIGIT DIGIT;
filters: (tagsFilter | userFilter | authorFilter | createdFilter | keywordFilter)+ EOF;
date: DATE;
keyword: KEYWORLD | QUOTED_STRING;
keywordFilter: keyword (COMMA keyword)*;
tag: KEYWORLD | QUOTED_STRING;
tags: tag (COMMA tag)*;
tagsFilter: 'tags' EQ tags;
userFilter: 'user' EQ keyword;
authorFilter: 'author' EQ keyword;
createdFilter: 'created' createdOperation date;
createdOperation: (GT | LT | EQ | NE | GE | LE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment