post queries: raise error on invalid searches.

Raise an error if the search is invalid for one of the following reasons:

* It contains multiple conflicting order: metatags (e.g. `order:score order:favcount` or `ordfav:a ordfav:b`).
* It contains a metatag that can't be used more than once: (e.g. `limit:5 limit:10`, `random:5 random:10`).
* It contains a metatag that can't be negated (e.g. `-order:score`, `-limit:20`, or `-random:20`).
* It contains a metatag that can't be used in an OR clause (e.g. ` touhou or order:score`, `touhou or limit:20`, `touhou or random:20`).
This commit is contained in:
evazion
2022-04-09 01:36:44 -05:00
parent c45d1d42c2
commit eca0ab04f7
6 changed files with 123 additions and 29 deletions

View File

@@ -41,7 +41,8 @@ class PostQuery
include Comparable
include Enumerable
attr_reader :type, :args
attr_reader :type, :args, :parent
protected attr_writer :parent
delegate :all?, :none?, :and?, :or?, :not?, :opt?, :tag?, :metatag?, :wildcard?, to: :inquirer
# Create an AST node.
@@ -51,7 +52,14 @@ class PostQuery
# AND/OR/NOT/OPT nodes, or the name and/or value for tag, metatag, or wildcard nodes).
def initialize(type, args)
@type = type
@args = args
@parent = nil
if term?
@args = args
else
@args = args.deep_dup
@args.each { _1.parent = self }
end
end
concerning :ConstructorMethods do
@@ -198,6 +206,10 @@ class PostQuery
end
concerning :OutputMethods do
def to_s
to_infix
end
def inspect
to_sexp
end
@@ -339,6 +351,19 @@ class PostQuery
tags.map(&:name)
end
# @return [Array<AST>] The list of all parent nodes of this node.
def parents
parents = []
node = self
until node.parent.nil?
parents << node.parent
node = node.parent
end
parents
end
# True if the AST is a simple node, that is a leaf node with no child nodes.
def term?
type.in?(%i[tag metatag wildcard all none])
@@ -390,6 +415,6 @@ class PostQuery
end
end
memoize :to_cnf, :simplify, :simplify_once, :rewrite_opts, :trim, :trim_once, :sort, :inquirer, :deconstruct, :inspect, :to_sexp, :to_infix, :to_tree, :nodes, :tags, :metatags, :tag_names
memoize :to_cnf, :simplify, :simplify_once, :rewrite_opts, :trim, :trim_once, :sort, :inquirer, :deconstruct, :inspect, :to_sexp, :to_infix, :to_tree, :nodes, :tags, :metatags, :tag_names, :parents
end
end