zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

helper.ex (2241B)


      1 defmodule Makeup.Lexers.ElixirLexer.Helper do
      2   @moduledoc false
      3   import NimbleParsec
      4   alias Makeup.Lexer.Combinators
      5 
      6   def with_optional_separator(combinator, separator) when is_binary(separator) do
      7     combinator |> repeat(string(separator) |> concat(combinator))
      8   end
      9 
     10   # Allows escaping of the first character of a right delimiter.
     11   # This is used in sigils that don't support interpolation or character escapes but
     12   # must support escaping of the right delimiter.
     13   def escape_delim(rdelim) do
     14     rdelim_first_char = String.slice(rdelim, 0..0)
     15     string("\\" <> rdelim_first_char)
     16   end
     17 
     18   def sigil(ldelim, rdelim, ranges, middle) do
     19     left = string("~") |> utf8_char(ranges) |> string(ldelim)
     20     right = string(rdelim)
     21 
     22     choices = middle ++ [utf8_char([])]
     23 
     24     left
     25     |> repeat(lookahead_not(right) |> choice(choices))
     26     |> concat(right)
     27     |> optional(utf8_string([?a..?z, ?A..?Z, ?0..?9], min: 1))
     28     |> post_traverse({__MODULE__, :build_sigil, []})
     29   end
     30 
     31   def build_sigil(rest, acc, context, line, offset) do
     32     type =
     33       case Enum.at(acc, -2) do
     34         sigil when sigil in 'sScC' -> :string
     35         sigil when sigil in 'rR' -> :string_regex
     36         sigil when sigil in 'TDNU' -> :literal_date
     37         _ -> :string_sigil
     38       end
     39 
     40     Combinators.collect_raw_chars_and_binaries(rest, acc, context, line, offset, type, %{})
     41   end
     42 
     43   def escaped(literal) when is_binary(literal) do
     44     string("\\" <> literal)
     45   end
     46 
     47   def keyword_matcher(kind, fun_name, words) do
     48     heads =
     49       for {ttype, words} <- words do
     50         for word <- words do
     51           case kind do
     52             :defp ->
     53               quote do
     54                 defp unquote(fun_name)([{:name, attrs, unquote(ttype)} | tokens]) do
     55                   [{unquote(ttype), attrs, unquote(word)} | unquote(fun_name)(tokens)]
     56                 end
     57               end
     58               |> IO.inspect()
     59 
     60             :def ->
     61               quote do
     62                 def unquote(fun_name)([{:name, attrs, unquote(ttype)} | tokens]) do
     63                   [{unquote(ttype), attrs, unquote(word)} | unquote(fun_name)(tokens)]
     64                 end
     65               end
     66           end
     67         end
     68       end
     69 
     70     quote do
     71       (unquote_splicing(heads))
     72     end
     73   end
     74 end