#Erlang File Directive for Elixir
9 messages · Page 1 of 1 (latest)
I wrote a code for generating an elixir module from another file. If there is a problem elixir compiler should be pointed to the my file instead of generated elixir file.
I do not know if it is possible within such file, but if that would be just macro, then you can do it in quote options.
Actually, I rewrote the leex for erlang to the elixir. And it is compiling a xex file (like xrl) to elixir code. So that is why I need this attribute.
An instance of xex file.
@Definitions
INT = [0-9]+
ATOM = :[a-z_]+
WHITESPACE = [\s\t\n\r]
@Rules
{INT} do: {:token, {:int, token_line, String.to_integer(token_val)}}
{ATOM} do: {:token, {:atom, token_line, to_atom(token_val)}}
\[ do: {:token, {:"[", token_line}}
\] do: {:token, {:"]", token_line}}
, do: {:token, {:",", token_line}}
{WHITESPACE}+ do: :skip_token
@Code
def to_atom(<<?:,string::binary>>) do
String.to_atom(string)
end
I do not think that you can set line, but for sure you can set file name https://hexdocs.pm/elixir/Module.html#module-file
Though probably "the Elixir way" would be to do it like that:
defmodule MyParser do
use Xex.Compiler, definition_file: "my_file.xex"
def to_atom(":" <> string), do: String.to_atom(string)
end
And then Xex.Compiler.__using__/1 is doing the magic spitting out the quoted value that describes the compiler. There each quote block can have line and file defined separately.
Alternatively you can do magic like:
unquote(quote [file: file_name, line: line] do
# your code
end)
I think that this would also work