Changeset View
Changeset View
Standalone View
Standalone View
swh/search/parser/grammar.js
- This file was added.
module.exports = grammar({ | |||||||||||
name: 'swh_search_query_language', | |||||||||||
rules: { | |||||||||||
query: $ => repeat( | |||||||||||
choice( | |||||||||||
$.patternFilter, | |||||||||||
$.booleanFilter, | |||||||||||
$.numericFilter, | |||||||||||
$.unboundedListFilter, | |||||||||||
$.boundedListFilter, | |||||||||||
$.dateFilter, | |||||||||||
) | |||||||||||
), | |||||||||||
patternFilter: $ => seq($.patternField, $.patternOp, $.patternVal), | |||||||||||
patternField: $ => token(choice('url', 'metadata')), | |||||||||||
patternOp: $ => choice(' : ', ' = '), | |||||||||||
patternVal: $ => $.string, // should it be RegEx instead ? | |||||||||||
vlorentz: no, we shouldn't allow arbitrary regexps | |||||||||||
Done Inline Actionsokay. KShivendu: okay. | |||||||||||
booleanFilter: $ => seq($.booleanField, $.booleanOp, $.booleanVal), | |||||||||||
booleanField: $ => choice('with_visit'), | |||||||||||
booleanOp: $ => choice(' = ', ' : '), | |||||||||||
booleanVal: $ => choice($.True, $.False), | |||||||||||
numericFilter: $ => seq($.numericField, $.numericOp, $.numberVal), | |||||||||||
numericField: $ => choice('nb_visits', 'limit'), | |||||||||||
numericOp: $ => $.rangeOp, | |||||||||||
numberVal: $ => $.number, | |||||||||||
boundedListFilter: $ => choice($.visitTypeFilter, $.sortByFilter), | |||||||||||
visitTypeFilter: $ => seq($.visitTypeField, $.visitTypeOp, $.visitTypeVal), | |||||||||||
visitTypeField: $ => 'visit_types', | |||||||||||
visitTypeOp: $ => choice(':'), | |||||||||||
Not Done Inline Actionswhy no = or spaces here? vlorentz: why no `=` or spaces here? | |||||||||||
Done Inline ActionsFixed all such inconsistencies. Btw, I've replaced all = with : except for range operators (where filter value is of type date or number) KShivendu: Fixed all such inconsistencies.
Btw, I've replaced all `=` with `:` except for range operators… | |||||||||||
visitTypeVal: $ => seq('[', commaSepStr($.visitTypeOptions), ']'), | |||||||||||
visitTypeOptions: $ => choice( | |||||||||||
"any", "cran", "deb", "deposit", "ftp", "hg", "git", "nixguix", "npm", "pypi", "svn", "tar" | |||||||||||
), | |||||||||||
sortByFilter: $ => seq($.sortByField, $.sortByOp, $.sortByVal), | |||||||||||
sortByField: $ => 'sort_by', | |||||||||||
sortByOp: $ => choice(':'), | |||||||||||
sortByVal: $ => seq('[', commaSepStr($.sortByOptions), ']'), | |||||||||||
sortByOptions: $ => choice( | |||||||||||
'nb_visits', | |||||||||||
'last_visit_date', | |||||||||||
'last_eventful_visit_date', | |||||||||||
'last_revision_date', | |||||||||||
'last_release_date', | |||||||||||
'date_created', | |||||||||||
'date_modified', | |||||||||||
'date_published' | |||||||||||
), | |||||||||||
Not Done Inline Actionswhy no spaces here either? vlorentz: why no spaces here either? | |||||||||||
Done Inline Actionsfixed. KShivendu: fixed. | |||||||||||
unboundedListFilter: $ => seq($.listField, $.listOp, $.listVal), | |||||||||||
listField: $ => choice('programming_languages', 'licenses', 'keywords'), | |||||||||||
listOp: $ => token(choice('in', 'not in')), | |||||||||||
listVal: $ => $.array, | |||||||||||
dateFilter: $ => seq($.dateField, $.dateOp, $.dateVal), | |||||||||||
dateField: $ => choice( | |||||||||||
'last_visit_date', | |||||||||||
'last_eventful_visit_date', | |||||||||||
'last_revision_date', | |||||||||||
'last_release_date', | |||||||||||
'date_created', | |||||||||||
'date_modified', | |||||||||||
'date_published' | |||||||||||
), | |||||||||||
dateOp: $ => $.rangeOp, | |||||||||||
dateVal: $ => $.dateWithOptionalTime, | |||||||||||
rangeOp: $ => choice('<', '<=', '=', '!=', '>=', '>'), | |||||||||||
array: $ => seq( | |||||||||||
"[", commaSepStr($.string_content), "]" | |||||||||||
), | |||||||||||
dateWithOptionalTime: $ => /\d{4}[-]\d{2}[-]\d{2} (\d{2}:\d{2}(:\d{2})*)*/, | |||||||||||
// ^\d{2}\/\d{2}\/\d{4}\s*(?:\d{2}:\d{2}(?::\d{2})?)?$ | |||||||||||
// Reference : https://stackoverflow.com/questions/23786905/regex-for-validating-dd-mm-yyyy-with-optional-time | |||||||||||
// Matches : | |||||||||||
// 21/05/2014 | |||||||||||
// 21/05/2014 15:54 | |||||||||||
// 21/05/2014 15:54:12 | |||||||||||
string: $ => wrapWithInvertedComma($.string_content), | |||||||||||
number: $ => /\d+/, | |||||||||||
True: $ => "true", | |||||||||||
False: $ => "false", | |||||||||||
Not Done Inline Actions
to be closer to ISO8601 And why are you using * instead of ? for optionals? vlorentz: to be closer to ISO8601
And why are you using `*` instead of `?` for optionals? | |||||||||||
Done Inline Actions
Oops. So stupid of me *facepalm*
Done. KShivendu: > And why are you using * instead of ? for optionals?
Oops. So stupid of me
*facepalm*
> to… | |||||||||||
string_content: $ => repeat1(choice( | |||||||||||
token.immediate(/[^\\"\n]+/), | |||||||||||
$.escape_sequence | |||||||||||
)), | |||||||||||
escape_sequence: $ => token.immediate(seq( | |||||||||||
'\\', | |||||||||||
Not Done Inline Actionsthe comment is misleading as the regexp expects the year first vlorentz: the comment is misleading as the regexp expects the year first | |||||||||||
Done Inline Actionsremoved. KShivendu: removed. | |||||||||||
/(\"|\\|\/|b|n|r|t|u)/ | |||||||||||
)), | |||||||||||
} | |||||||||||
}); | |||||||||||
function commaSep1(rule) { | |||||||||||
return seq(rule, repeat(seq(",", rule))) | |||||||||||
} | |||||||||||
function commaSep(rule) { | |||||||||||
return optional(commaSep1(rule)) | |||||||||||
} | |||||||||||
function commaSepStr(rule) { | |||||||||||
return commaSep(wrapWithInvertedComma(rule)) | |||||||||||
} | |||||||||||
function wrapWithInvertedComma(rule) { | |||||||||||
return choice( | |||||||||||
Not Done Inline Actionscan you allow trailing commas too? vlorentz: can you allow trailing commas too? | |||||||||||
Done Inline Actionsdone KShivendu: done | |||||||||||
seq("'", rule, "'"), | |||||||||||
seq('"', rule, '"'), | |||||||||||
rule, | |||||||||||
) | |||||||||||
} | |||||||||||
No newline at end of file |
no, we shouldn't allow arbitrary regexps