post queries: refactor AST #simplify method.

Refactor the `PostQuery::AST#simplify` method to split it into three
methods: `#trim` to eliminate redundant AND and OR clauses, `#simplify`
to expand deeply nested subexpressions, and `#sort` to sort the query
into alphabetical order.

This is so we can normalize queries written by users by parsing and
rewriting them, but without expanding out nested subexpressions, which
can substantially alter the way the query is written.
This commit is contained in:
evazion
2022-04-04 00:48:40 -05:00
parent 8055c4f172
commit bf7c721815
2 changed files with 137 additions and 92 deletions

View File

@@ -2,7 +2,7 @@ require 'test_helper'
class PostQueryParserTest < ActiveSupport::TestCase
def assert_parse_equals(expected, input)
assert_equal(expected, PostQuery::Parser.parse(input).simplify.to_sexp)
assert_equal(expected, PostQuery::Parser.parse(input).to_cnf.to_sexp)
end
def to_infix(string)