Permalink
Browse files

Add the atom %a sigil

  • Loading branch information...
1 parent c6ff84e commit ba4adec493ba117a6a996e01931f0b0295e820b8 @devinus committed Mar 27, 2013
Showing with 53 additions and 10 deletions.
  1. +42 −9 lib/elixir/lib/kernel.ex
  2. +11 −1 lib/elixir/test/elixir/kernel/sigils_test.exs
View
@@ -3112,22 +3112,36 @@ defmodule Kernel do
end
@doc """
- Handles the sigil %r. It returns a Regex pattern.
+ Handles the sigil %A. It simply returns an atom without unescaping
+ characters and without interpolations.
## Examples
- Regex.match? %r(foo), "foo" #=> true
+ %A(foo) #=> :foo
+ %A(f\#{o}o) #=> :"f\\\#{o}o"
"""
- defmacro __r__({ :<<>>, _line, [string] }, options) when is_binary(string) do
- binary = Macro.unescape_binary(string, Regex.unescape_map(&1))
- regex = Regex.compile!(binary, options)
- Macro.escape(regex)
+ defmacro __A__({ :<<>>, _line, [string] }, []) when is_binary(string) do
+ binary_to_atom(string)
end
- defmacro __r__({ :<<>>, line, pieces }, options) do
- binary = { :<<>>, line, Macro.unescape_tokens(pieces, Regex.unescape_map(&1)) }
- quote do: Regex.compile!(unquote(binary), unquote(options))
+ @doc """
+ Handles the sigil %a. It returns an atom, unescaping characters
+ and replacing interpolations.
+
+ ## Examples
+
+ %a(foo) #=> :foo
+ %a(f\#{:o}o) #=> :foo
+
+ """
+ defmacro __a__({ :<<>>, _line, [string] }, []) when is_binary(string) do
+ binary_to_atom(string)
+ end
+
+ defmacro __a__({ :<<>>, line, pieces }, []) do
+ binary = { :<<>>, line, Macro.unescape_tokens(pieces) }
+ quote do: binary_to_atom(unquote(binary))
end
@doc """
@@ -3144,6 +3158,25 @@ defmodule Kernel do
Macro.escape(regex)
end
+ @doc """
+ Handles the sigil %r. It returns a Regex pattern.
+
+ ## Examples
+
+ Regex.match? %r(foo), "foo" #=> true
+
+ """
+ defmacro __r__({ :<<>>, _line, [string] }, options) when is_binary(string) do
+ binary = Macro.unescape_binary(string, Regex.unescape_map(&1))
+ regex = Regex.compile!(binary, options)
+ Macro.escape(regex)
+ end
+
+ defmacro __r__({ :<<>>, line, pieces }, options) do
+ binary = { :<<>>, line, Macro.unescape_tokens(pieces, Regex.unescape_map(&1)) }
+ quote do: Regex.compile!(unquote(binary), unquote(options))
+ end
+
## Private functions
# Extracts concatenations in order to optimize many
@@ -48,4 +48,14 @@ defmodule Kernel.SigilsTest do
assert %C(f#{o}o) == 'f\#{o}o'
assert %C(f\no) == 'f\\no'
end
-end
+
+ test :__a__ do
+ assert %a(foo) == :foo
+ assert %a(f#{:o}o) == :foo
+ end
+
+ test :__A__ do
+ assert %A(foo) == :foo
+ assert %A(f#{o}o) == :"f\#{o}o"
+ end
+end

0 comments on commit ba4adec

Please sign in to comment.