#Erlang File Directive for Elixir

9 messages · Page 1 of 1 (latest)

timid yew
solemn vector
#

You mean in macro?

#

Or you generate file by external means?

timid yew
#

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.

solemn vector
#

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.

timid yew
#

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
solemn vector
#

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