PostQuery::AST: fix #to_infix to not add unnecessary parentheses.

* Fix the `#to_infix` method to not add unnecessary parentheses around subexpressions.
* Fix metatags to add quotes around values when necessary.
This commit is contained in:
evazion
2022-03-30 00:43:00 -05:00
parent fb0a7851bf
commit 8c9e045a9c
3 changed files with 74 additions and 18 deletions

View File

@@ -168,7 +168,7 @@ class PostQuery
in [:tag, name]
name
in [:metatag, name, value]
"#{name}:#{value}"
"#{name}:#{quoted_value}"
in [:wildcard, name]
"(wildcard #{name})"
in [type, *args]
@@ -180,7 +180,7 @@ class PostQuery
def to_infix
case self
in [:all]
"all"
""
in [:none]
"none"
in [:wildcard, name]
@@ -188,19 +188,15 @@ class PostQuery
in [:tag, name]
name
in [:metatag, name, value]
"#{name}:#{value}"
in [:not, a]
"-#{a.to_infix}"
in [:opt, a]
"~#{a.to_infix}"
in [:and, a]
a.to_infix
in [:or, a]
a.to_infix
in [:and, *a]
"(#{a.map(&:to_infix).join(" ")})"
in [:or, *a]
"(#{a.map(&:to_infix).join(" or ")})"
"#{name}:#{quoted_value}"
in :not, child
child.term? ? "-#{child.to_infix}" : "-(#{child.to_infix})"
in :opt, child
child.term? ? "~#{child.to_infix}" : "~(#{child.to_infix})"
in :and, *children
children.map { _1.children.many? ? "(#{_1.to_infix})" : _1.to_infix }.join(" ")
in :or, *children
children.map { _1.children.many? ? "(#{_1.to_infix})" : _1.to_infix }.join(" or ")
end
end
@@ -266,6 +262,17 @@ class PostQuery
args.second if metatag?
end
# @return [String, nil] The value of the metatag as a quoted string, if a metatag node.
def quoted_value
return nil unless metatag?
if value.include?(" ") || value.starts_with?('"') || value.empty?
%Q{"#{value.gsub(/"/, '\\"')}"}
else
value
end
end
# @return [Array<AST>] The child nodes, if the node has children.
def children
term? ? [] : args

View File

@@ -33,6 +33,8 @@ require "strscan"
class PostQuery
class Parser
extend Memoist
class Error < StandardError; end
METATAG_NAME_REGEX = /(#{PostQueryBuilder::METATAGS.join("|")}):/i
@@ -265,5 +267,7 @@ class PostQuery
AST.new(type, args)
end
end
memoize :parse, :parse!
end
end