<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="311344" data-post-id="311344">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="akash-akya" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/akash-akya/120/18688_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  akash-akya
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><strong>EDIT:</strong>   Ehh, I missed that you also want to consider words separated by space, <code>Foo Bar AND Baz</code>. That won’t work here too I guess.</p>
<p>Just realized its similarity with Erlang expression, and I couldn’t resist abusing it.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">expr = ~S|("Modesty Blase" AND "X") Or not "Willie Garvin and" and Foo|
</code></pre>

<p>out =&gt;</p>
<pre data-code-wrap="quote"><code class="lang-quote">"(\"Modesty Blase\" AND \"X\") Or not \"Willie Garvin and\" and Foo"
</code></pre>
<hr>
<pre data-code-wrap="elixir"><code class="lang-elixir">{:ok, tokens, _EndLine} = :erl_scan.string(to_charlist(expr))
tokens
</code></pre>

<p>out =&gt;</p>
<pre data-code-wrap="quote"><code class="lang-quote">[
  {:"(", 1},
  {:string, 1, ~c"Modesty Blase"},
  {:var, 1, :AND},
  {:string, 1, ~c"X"},
  {:")", 1},
  {:var, 1, :Or},
  {:not, 1},
  {:string, 1, ~c"Willie Garvin and"},
  {:and, 1},
  {:var, 1, :Foo}
]
</code></pre>
<hr>
<pre data-code-wrap="elixir"><code class="lang-elixir">tokens =
  Enum.map(tokens, fn
    {:var, anno, var} -&gt;
      term = String.downcase(to_string(var))

      if term in ~w(and or not) do
        {String.to_existing_atom(term), anno}
      else
        {:string, anno, to_string(var)}
      end

    {op, anno} when op in ~w|( )|a -&gt;
      {op, anno}

    {type, anno, value} when type in ~w(string atom integer)a -&gt;
      {:string, anno, to_string(value)}

    {boolean_op, _anno} = token when boolean_op in ~w(and or not)a -&gt;
      token

    {op, anno} -&gt;
      {:string, anno, to_string(op)}
  end)
</code></pre>

<p>out =&gt;</p>
<pre data-code-wrap="quote"><code class="lang-quote">[
  {:"(", 1},
  {:string, 1, "Modesty Blase"},
  {:and, 1},
  {:string, 1, "X"},
  {:")", 1},
  {:or, 1},
  {:not, 1},
  {:string, 1, "Willie Garvin and"},
  {:and, 1},
  {:string, 1, "Foo"}
]
</code></pre>
<hr>
<pre data-code-wrap="elixir"><code class="lang-elixir">IO.puts("Expression: " &lt;&gt; expr)

{:ok, abs_form} = :erl_parse.parse_exprs(tokens ++ [{:dot, 1}])
abs_form
</code></pre>

<p>out =&gt;</p>
<pre data-code-wrap="quote"><code class="lang-quote">Expression: ("Modesty Blase" AND "X") Or not "Willie Garvin and" and Foo

[
  {:op, 1, :or, {:op, 1, :and, {:string, 1, "Modesty Blase"}, {:string, 1, "X"}},
   {:op, 1, :and, {:op, 1, :not, {:string, 1, "Willie Garvin and"}}, {:string, 1, "Foo"}}}
]
</code></pre>
<p>You can then use the parsed <code>abs_form</code> term for further analysis.</p>
<p><img src="https://forum.elixirforum.com/images/emoji/apple/warning.png?v=15" title=":warning:" class="emoji" alt=":warning:" loading="lazy" width="20" height="20"> Although the hack seems to work, but there is one major issue, <strong>it seems  <code>erl_scan</code> dynamically creates atoms</strong></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="311344" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/recommendation-for-building-search-parser/59807/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-311344" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="311344"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>