Current section
Files
Jump to
Current section
Files
lib/cliex_map/pipeline_functions.ex
defmodule CliexMap.PipelineFunctions do
use CliexMap.Types
alias CliexMap.Fn
@moduledoc ~S"""
Implements all functions the compiler compiles to.
It is like a runtime, but as we will always use them in the Mapper's pipeline the naming seems
appropriate
"""
@doc ~S"""
Function that represents fields like `%`, `%2` or `%-3`
"""
@spec field_function(integer()) :: Fn.t
def field_function(field_number)
def field_function(0) do
Fn.new(fn {_lnb, text}, _context -> text end, "field %0")
end
def field_function(nb) do
Fn.new(&_field_function(&1, &2, nb), "field %#{nb}")
end
def _field_function({_lnb, text}, _context, nb) do
parts = String.split(text)
if nb > 0 do
Enum.at(parts, nb - 1)
else
Enum.at(parts, nb)
end || ""
end
@doc ~S"""
Fucnction that represents literal strings in the pattern, e.g. `%%` or `Hello World`
"""
@spec literal_function(binary()) :: Fn.t
def literal_function(literal) do
Fn.new(fn _, _ -> literal end, "literal: #{literal}")
end
end
# SPDX-License-Identifier: Apache-2.0