Compare commits

...
333 Commits
Author SHA1 Message Date
José Valim 243433d9d3 Release v0.7.2 2012-12-04 19:58:32 +01:00
Yurii Rashkovskii 7a69a0335c Fix return value of String.at/2 with negative position out of bounds 2012-12-04 01:43:16 -08:00
Yurii Rashkovskii 16da928df8 Fix String.at/1 documentation example 2012-12-04 01:36:32 -08:00
José Valim 4583162eda Improve docs for mix run 2012-12-03 22:47:27 +01:00
José Valim b20fea7c4f Ensure update_* fields work with record rewriter 2012-12-03 20:51:29 +01:00
José Valim d9029c7f98 Add a test for conflicting record inference 2012-12-03 20:43:39 +01:00
José Valim 362ff69e65 Move more behaviour to Enum 2012-12-03 20:23:22 +01:00
José Valim ba43183f1b Ensure RecordRewriter is no-op for non record fields 2012-12-03 14:48:11 +01:00
Yurii Rashkovskii af959582d7 Decapitalize Erlang-style named variables in type specifications' annotations when converting to AST 2012-12-03 04:11:42 -08:00
Yurii Rashkovskii 46b73173b5 Minor CHANGELOG improvements 2012-12-03 03:27:44 -08:00
José Valim 8b66976c96 Ensure Macro.to_binary work with unary not 2012-12-03 12:24:26 +01:00
José Valim 36e6261164 Update CHANGELOG 2012-12-03 12:08:57 +01:00
José Valim a629fdac0d Document the record access rewrite feature 2012-12-03 11:35:34 +01:00
José Valim 3c7cc9ab84 Merge branch 'jv-tuple-opt' 2012-12-03 11:23:02 +01:00
José Valim c7f67bc52a Rewrite records accessors when possible 2012-12-03 11:19:51 +01:00
Yurii Rashkovskii dffd8c59ad Improve documentation for the /> operator 2012-12-03 01:24:32 -08:00
José Valim fa9a78f0dc Merge pull request #683 from devinus/unicode-strip
Add String.Unicode.{lstrip,rstrip}
2012-12-03 00:06:22 -08:00
José Valim 82cf622e9b Enable record rewriter inference 2012-12-03 00:47:34 +01:00
Devin Torres 5c29d76bca Add String.Unicode.{lstrip,rstrip} 2012-12-02 17:42:32 -06:00
José Valim 088ef2fbe8 Support try/receive in record rewriter 2012-12-03 00:24:09 +01:00
José Valim 498ac849fb Record rewriting works with case clauses 2012-12-02 23:37:53 +01:00
José Valim d4c2436521 Extend reach of record rewriting 2012-12-02 22:33:12 +01:00
Yurii Rashkovskii 0f9b717c03 Improve IEX's s helper implementation 2012-12-02 12:14:47 -08:00
Yurii Rashkovskii 295d275d13 Fix IEX's s helper to correctly handle MF and MFA requests 2012-12-02 12:06:40 -08:00
Yurii Rashkovskii 4c1f4451e2 Type specification to AST conversion should support paren_type 2012-12-02 11:58:58 -08:00
José Valim 0f1584c0fa Initial work on RecordRewriter 2012-12-02 20:58:57 +01:00
Yurii Rashkovskii 0a42d7aa3c IEX's s helper should include callbacks in its output 2012-12-02 11:41:17 -08:00
Yurii Rashkovskii 31dcbfb8de mix compile.app should allow custom keys in .app 2012-12-02 11:01:50 -08:00
José Valim 49fa59d3a9 Improve Typespec stacktraces 2012-12-02 16:13:29 +01:00
José Valim c06a44c2fd Add Macro.extract_args, closes #677 2012-12-02 16:10:46 +01:00
José Valim 4e8fb9915b Validate all app properties and do not accept nil values 2012-12-02 15:18:21 +01:00
Yurii Rashkovskii cf5f57f07a Implement Mix.Project.application values validation
Closes #685

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2012-12-02 14:33:16 +01:00
José Valim fc03c8b43d Get rid of unused compilation mode 2012-12-02 14:32:36 +01:00
José Valim b32f6ef51f Keep variables in a single dictionary
This ensures we give the same treatment to quoted
and non-quoted variables.
2012-12-02 12:22:10 +01:00
José Valim 9b3d851a9c Deprecate record default-based generated functions 2012-12-01 11:14:45 +01:00
José Valim 8df803471c IEx.preload is gone 2012-11-30 17:20:31 +01:00
José Valim d55afa44c6 Ensure new IEx works on Windows 2012-11-30 17:07:12 +01:00
José Valim a061528731 Ensure IEx runs only after Elixir's CLI process its options 2012-11-30 17:07:12 +01:00
Yurii Rashkovskii 9e030a6102 Improve System module type specifications coverage 2012-11-30 05:54:05 -08:00
Yurii Rashkovskii 7c5ce0ce74 Fix System.halt/1 and System.halt/0 type specifications
Dialyzer no longer complains about them.
2012-11-30 05:29:44 -08:00
José Valim 73340a466d Consider zero-arity on Macro.to_binary, closes #682 2012-11-30 14:15:02 +01:00
Yurii Rashkovskii 94cc204e4f Add yrashk to .travis.yml notification recipients 2012-11-30 05:01:37 -08:00
José Valim 2ae2b88ea8 Fix mix executable 2012-11-30 13:56:18 +01:00
José Valim b3d203712a Fix mix iex 2012-11-30 13:49:18 +01:00
Yurii Rashkovskii 486cb79fab Store @specs in the order they were specified in the source code 2012-11-30 04:47:43 -08:00
José Valim fe13173aaf Update lib/iex/lib/iex/helpers.ex 2012-11-30 13:38:14 +01:00
Yurii Rashkovskii d8a070d145 Fix typespec to AST conversion for functions with variable arity (...) 2012-11-30 04:34:05 -08:00
Yurii Rashkovskii 8a6fddd477 Add a note about string(), char_list() and String.t() types 2012-11-30 04:03:00 -08:00
Yurii Rashkovskii 57a09f25eb Fix a bug that prevented from inspecting immediate values in typespecs (such as ...) 2012-11-30 03:59:24 -08:00
Yurii Rashkovskii 3e320fd10c Add IEx helper l/1 2012-11-30 03:48:11 -08:00
Yurii Rashkovskii 97bc7a3727 Mention r/1 helper in IEx help 2012-11-30 03:44:29 -08:00
José Valim 252886c2dc Simplify API exposed in IEx.Helpers as h/1, s/1 and t/1 are more flexible now 2012-11-30 11:29:27 +01:00
Yurii Rashkovskii 909db1170c Add IEx helper flush/0 2012-11-30 02:25:35 -08:00
José Valim 6a68bef35a Improve shelling out on mix, closes #492 2012-11-30 11:22:48 +01:00
Yurii Rashkovskii b04351f625 Add support for "local" function syntax in IEx's s helper
Example:

  s(atom_to_list)
  s(atom_to_list/1)
2012-11-30 02:17:28 -08:00
Yurii Rashkovskii 624f781c07 Catch invalid IEx's h helper invocations 2012-11-30 02:10:48 -08:00
Yurii Rashkovskii 5cd8de0493 Fix indentation 2012-11-30 02:08:03 -08:00
Yurii Rashkovskii 8742673da2 Make h/0 IEx helper work again 2012-11-30 02:04:03 -08:00
Yurii Rashkovskii 2fcbf077d0 Minor IEx helper improvement 2012-11-30 02:03:08 -08:00
Yurii Rashkovskii 2aee6bdf2d Add support for h(function_name) helper in IEx
Allows printing documentation for IEx helpers and Kernel's functions/macros without specifying arity.
2012-11-30 01:53:05 -08:00
Yurii Rashkovskii 51f2530f79 Add support for h(Module.function) helper in IEx
Allows printing documentation for functions/macros without specifying arity
2012-11-30 01:39:58 -08:00
Yurii Rashkovskii 1e2f170e95 Initial piece of documentation on type specification syntax
Closes #681
2012-11-30 01:25:29 -08:00
Yurii Rashkovskii 9ed5936f9a Fix a bug in macro_exported?/3
Previously, it would fail if a macro with multiple arities was defined but the first occurence of it won't match the requested arity.
2012-11-30 00:56:58 -08:00
Yurii Rashkovskii bf5431a105 Minor documentation fix in IEx.Helpers 2012-11-30 00:53:39 -08:00
Yurii Rashkovskii ab8da88aad Improve h/1 IEx helper to handle some cases that were not handled before
For example, this didn't work:

    iex(1)> h function_exported?/3
    ** (UndefinedFunctionError) undefined function: IEx.Helpers.function_exported?/0
2012-11-30 00:49:09 -08:00
Yurii Rashkovskii 670822ce63 IEx's h/1 helper should check IEx.Helper's macros before handing off to Kernel 2012-11-30 00:43:46 -08:00
Yurii Rashkovskii d731e8ca4a Add macro_exported?/3 helper 2012-11-30 00:41:21 -08:00
José Valim a18dbf8704 Update CHANGELOG 2012-11-30 09:35:41 +01:00
José Valim bc53005f98 Add new typespec syntax to defcallback too 2012-11-30 09:35:41 +01:00
José Valim d538bbbd22 Ensure proper stacktrace is shown in macros, closes #676 2012-11-30 09:35:41 +01:00
Yurii Rashkovskii 82716f12cb Add support for s/2 and h/2 helpers
These helpers will print all matching specs and helpers for specified module:name with any arity

Example:

    t(Enum.t)
    s(Enum.all?)
2012-11-30 00:21:10 -08:00
Yurii Rashkovskii f84800a9d3 AST representation for the :elixir.char_list() type should be char_list() as it is possible to use this type directly without specifying :elixir module name 2012-11-30 00:00:22 -08:00
José Valim 3eb509fdd2 Support -> in Macro.to_binary and improve fn printing too 2012-11-30 08:38:25 +01:00
José Valim 71de5f0524 Merge branch 'jv-new-spec' 2012-11-30 08:13:35 +01:00
José Valim c2076c79df Merge pull request #678 from yrashk/jv-new-spec
Change fun(... :: signature) specification format to (... -> signature)
2012-11-29 23:13:01 -08:00
Yurii Rashkovskii f0ab87fc1e Add a more explicit syntax for ((...) -> ...): (fun(...) -> ...) 2012-11-29 20:44:06 -08:00
Yurii Rashkovskii b1cb6801c7 Change fun(... :: signature) specification format to (... -> signature) 2012-11-29 15:53:37 -08:00
José Valim f249eab01e Ensure parenthesis are allowed in call args 2012-11-29 23:58:25 +01:00
José Valim ced5a4386b Support -> inside parenthesis
This is consistent with the fact that do...end
is a direct translation to do: (...).
2012-11-29 23:47:53 +01:00
José Valim e11285007e More work on improved typespec syntax 2012-11-29 14:13:26 +01:00
Yurii Rashkovskii 0db1f7279b Change function type specification syntax (WIP)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2012-11-29 13:34:06 +01:00
José Valim f1b81fd726 Merge pull request #675 from yrashk/code-server-terminate
elixir_code_server should not trap exits and should not log on terminate
2012-11-29 00:18:15 -08:00
Yurii Rashkovskii b8d16dae6f elixir_code_server should not trap exits and should not log on terminate 2012-11-28 18:51:42 -08:00
José Valim b25d5f27d7 Ensure IEx boots fast and with Elixir available, closes #673 2012-11-28 23:19:00 +01:00
José Valim b9e5e490cd Do not add lines to __info__ function 2012-11-28 11:57:57 +01:00
José Valim 956773ee7e Make ExUnit exit happen in two steps.
This allows developers to hook custom mechanisms
after the tests are run but before the system halts.
2012-11-28 11:57:38 +01:00
José Valim 401ad781c3 Ensure debug_info is actually set to true 2012-11-28 09:48:04 +01:00
José Valim 4227d94603 Support \xXX and \x{HEX} escape sequences in strings and char lists
It implements the same behaviour as seen in regexes. Closes #661.
2012-11-27 15:50:45 +01:00
José Valim bf745b352e Make CHANGELOG more consistent 2012-11-27 15:09:12 +01:00
José Valim 3de677607c Deprecated assert left in right in favor of assert left inlist right, closes #662 2012-11-27 15:05:03 +01:00
José Valim f178d999b3 Update CHANGELOG 2012-11-27 14:52:19 +01:00
José Valim bce07f3c27 --debug-info is now true by default 2012-11-27 14:51:10 +01:00
José Valim c7f83ccda3 Merge pull request #665 from yrashk/function-reference-syntax
Add function(Module.fun/arity) and function(fun/arity) syntax
2012-11-27 01:37:29 -08:00
Yurii Rashkovskii e174cbfbfe Add function(Module.fun/arity) and function(fun/arity) syntax
These are equivalent to function(Module, :fun, arity) and function(:fun, arity) constructs, respectively
2012-11-27 01:35:06 -08:00
José Valim 703a7d833f Revert "/u is not required for Elixir regexes"
unicode option is actually required in order to
match \p escape groups.

This reverts commit 0f75b92b25.
2012-11-26 08:36:42 +01:00
José Valim 29f76a106c Merge pull request #660 from carlosmn/mix-dep-fmt
Fix mix dependency format error string
2012-11-25 23:09:12 -08:00
José Valim ba70744f14 Merge pull request #658 from kimshrier/kernel_special_forms_typo
fix some doc typos
2012-11-25 23:07:38 -08:00
Carlos Martín Nieto 54d72121c3 Fix mix dependency format error string
The app name is an atom, but the error message says it should be a
binary, which causes confusion.

Fix that and also add a version without the "requirement" field, which
is optional.
2012-11-26 02:42:56 +01:00
Kim Shrier 1b19c9a7bd fix some doc typos 2012-11-25 15:35:39 -06:00
José Valim 13abbeb1b2 Update lib/elixir/lib/record.ex
Use record field default value as the value
still can be a function/port/etc
2012-11-25 21:22:00 +01:00
José Valim f97b3cea18 Update lib/elixir/lib/record.ex
Use record field value in the exception message
2012-11-25 20:15:32 +01:00
José Valim 9a7bdc158a Speed up native compilation by skipping it in the bootstrap process 2012-11-25 19:09:55 +01:00
José Valim ed0eff4d1d Fix failing erlang tests 2012-11-25 18:25:03 +01:00
José Valim b8f05b2b37 Raise an exception if invalid record field is given, closes #554 2012-11-25 18:13:53 +01:00
José Valim b9600fb6c4 More improvements to typespecs, closes #617 2012-11-25 17:43:56 +01:00
José Valim 5e26cd01b6 Improve error message when invalid token is followed by new line 2012-11-25 17:26:47 +01:00
Yurii Rashkovskii 775803be1a WIP on bounded_fun support
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2012-11-25 17:06:30 +01:00
José Valim 9f16178898 Raise an error if clauses with different arities are defined in the same function, closes #657 2012-11-25 17:01:54 +01:00
José Valim 7ddab53707 Also allow multiclause functions on fn
The function macro is still necessary for function
retrieval and it will be extended when R16 is out
to supported named functions
2012-11-25 00:06:39 +01:00
José Valim 05e694f7e2 Improvements to Regex and String 2012-11-24 21:56:58 +01:00
José Valim 768ede8a1c Merge pull request #656 from l4u/string_strip
String.strip should strip other whitespace characters
2012-11-24 12:13:16 -08:00
Leo Lou 2984771883 Add String.strip/1 2012-11-24 22:59:51 +08:00
Leo Lou 875d1007a9 Strip whitespace characters instead of space in String.rstrip 2012-11-24 22:44:37 +08:00
Leo Lou 1c8993695d Strip whitespace characters instead of space in String.lstrip 2012-11-24 21:07:18 +08:00
José Valim 71837bfbf0 Show warning if a rescue clause can never match, closes #641 2012-11-24 12:55:28 +01:00
José Valim df8a33b16a Show better error message when rebar is not available, closes #647 2012-11-24 12:04:08 +01:00
José Valim 600c8022ba Deprecated Enum.qsort and List.sort in favor of Enum.sort 2012-11-23 21:51:11 +01:00
José Valim 5c967ace04 Add Enum.sort which uses merge sort and performs considerably faster than Enum.qsort 2012-11-23 20:10:26 +01:00
José Valim 87ab504e06 Perfomance improvements to Enum.drop/take 2012-11-23 18:51:10 +01:00
José Valim 36ceb48e21 Do not exit after each run on ExUnit 2012-11-22 10:21:56 +01:00
José Valim 678fafe7c1 Improve IEx by using the -user option (ht @yrashk) 2012-11-22 10:01:57 +01:00
José Valim b5eb2dbd3e Updated rebar with more env variable fixes 2012-11-20 15:44:39 +01:00
José Valim 44bcc00fce Remove tests since utf-8 names on env variables are poorly supported depending on the OS and ERTS 2012-11-20 11:30:53 +01:00
José Valim 2016b4b236 Remove previously deprecated code 2012-11-20 11:17:13 +01:00
José Valim bd67df4d8c CI doesn't like utf-8 chars as env, let's try iso 2012-11-20 11:16:05 +01:00
José Valim 69ba46ec3e Update version to 0.7.2.dev 2012-11-20 11:15:28 +01:00
José Valim 3cd1a7d1ce Ensure we are inside a git repository before retrieve git info, closes #649 2012-11-20 11:10:25 +01:00
José Valim 224f5e47fe Ensure System env functions properly handle utf-8 data 2012-11-20 11:09:53 +01:00
José Valim 0f75b92b25 /u is not required for Elixir regexes 2012-11-20 10:52:57 +01:00
José Valim 7b7b08c49e Update rebar executable with utf-8 env fixes 2012-11-20 10:31:42 +01:00
José Valim 46a293d6f5 Merge pull request #648 from devinus/install-task
Add `make install` task
2012-11-20 01:09:51 -08:00
José Valim 166217a105 Merge pull request #650 from tim/fix-uri-encode
Fix percent encoding algorithm used by URI.encode
2012-11-19 23:51:27 -08:00
Tim Fletcher bddb614927 Fix percent encoding algorithm used by URI.encode 2012-11-20 00:09:59 +00:00
José Valim 80866e3544 Improve docs and style on unicode.ex 2012-11-20 00:15:00 +01:00
Devin Torres 131db13763 Add make install task
To change the install path, simply override the `INSTALL_PATH`:

    $ make INSTALL_PATH=/opt/local install
2012-11-19 11:28:19 -06:00
José Valim 8cede65b0f Release 0.7.1 2012-11-18 20:51:37 +01:00
José Valim bb2e1d5d7c Merge pull request #646 from burningTyger/master
enum doc typo
2012-11-17 02:48:25 -08:00
burningTyger e4f5f9d9ed fix typo in enum docs
RuPy 2012 in talk bug hunt
2012-11-17 11:16:27 +01:00
José Valim e801cbd380 Convert integer to a list in order to generate the module name 2012-11-16 09:50:13 +01:00
José Valim 7894ba0fc1 Add IO.binread, IO.binwrite and friends, closes #645 2012-11-16 09:42:35 +01:00
José Valim ad37d5f67a No need for lists:concat 2012-11-15 16:56:29 +01:00
José Valim 40efeb48d7 Reduce atom footprint when compiling code
Previously, we were generating atom names based on
the file or the module name. This commit replaces
it by a pool of atom names. After a module name
is used and purged with success, it is added back
to the pool so it can be reused many times.
2012-11-15 16:44:27 +01:00
José Valim d03d647560 Bring the same __ENV__ semantics to Module.create 2012-11-15 16:31:26 +01:00
José Valim c3b58eef16 file needs to be a binary 2012-11-15 16:30:55 +01:00
José Valim 32764b3370 Copy all __ENV__ information on eval 2012-11-15 10:01:18 +01:00
José Valim 2104b292c1 Ensure rstrip and strip work on emoty strings, closes #643 2012-11-14 10:34:43 +01:00
José Valim e0bbd47d7b Support R15B 2012-11-13 09:50:28 +01:00
José Valim 743213d8c1 halt was moved to System module
`halt` is a function that is less frequently used than the other
functions in Kernel, not allowed in guard clauses and its outcome
is to shutdown the whole runtime, being a great fit to the System
module.
2012-11-13 09:23:21 +01:00
José Valim 2db5b9c76c Make update no-op if no args are given 2012-11-13 09:23:21 +01:00
José Valim ba6a23a1a4 Merge pull request #640 from clofresh/master
default extracted record vals to :undefined instead of :nil
2012-11-11 23:44:20 -08:00
Carlo Cabanilla b9692fc2fe default record vals to :undefined instead of :nil
When translating erlang records into elixir records, use :undefined
as the default record value since that's what erlang uses, and
presumably you're going to be using these records with erlang apis.
2012-11-12 01:05:17 -05:00
José Valim 8a5f3f3a53 Fix documentation for defrecordp 2012-11-10 21:08:24 +01:00
José Valim c0a42c95e0 Do not print warnings for injected private macros 2012-11-10 12:30:24 +01:00
José Valim 9cad7eb1e2 Use new defrecordp in Regex module 2012-11-10 12:17:52 +01:00
José Valim fcb1a610de Add support for defrecordp 2012-11-10 11:51:13 +01:00
José Valim 04d96f7f18 Update CHANGELOG 2012-11-08 21:20:47 +01:00
José Valim d117dbf04c Filter out compilable files from stale ones in Mix 2012-11-08 21:19:40 +01:00
José Valim ce489eae2b Only allow () under special circunstances 2012-11-08 16:00:16 +01:00
José Valim 02e63e4849 Ensure parenthesis can be used in macros and fn shortcut, closes #634 2012-11-08 15:52:52 +01:00
José Valim cae328112c Fix unecessary double file read 2012-11-08 13:42:25 +01:00
José Valim b2931135d0 Use File.read instead of File.exists? + File.read! 2012-11-08 13:34:47 +01:00
José Valim bddaed1284 Merge pull request #636 from yrashk/exunit-user-config
Add support for ExUnit user configs
2012-11-08 04:30:51 -08:00
José Valim f78433c892 Clean up ExUnit.Formatter 2012-11-08 13:29:45 +01:00
Yurii Rashkovskii f1bbd3f37e Improve mix test helpers with respect to user configs 2012-11-08 03:23:35 -08:00
José Valim b159b1c6bf Merge pull request #637 from elixir-lang/ex_unit_formatter
Flexible formatters for ExUnit
2012-11-08 03:15:49 -08:00
José Valim e6c75bbe98 Merge pull request #632 from yrashk/is_keyword
Add keyword?/1 function
2012-11-08 03:08:58 -08:00
Yurii Rashkovskii c6a012602c Add Keyword.keyword?/1 function 2012-11-08 03:04:55 -08:00
José Valim cc89cf942f Flexible formatters for ExUnit 2012-11-08 12:01:07 +01:00
Yurii Rashkovskii 73d7de7bc9 Add support for ExUnit user configs 2012-11-08 02:49:59 -08:00
José Valim 1ba159ddd7 Merge pull request #635 from yrashk/deps_test-fix
Bring one more check back in mix's deps.git_test
2012-11-07 15:05:49 -08:00
Yurii Rashkovskii 8c3d8fc676 Bring one more check back in mix's deps.git_test 2012-11-07 15:00:51 -08:00
José Valim 8ac3844955 Merge pull request #633 from yrashk/test-kernel-warn-suppress
Suppress an unnecessary (optimization-related) warning in one of the ker...
2012-11-07 07:55:41 -08:00
Yurii Rashkovskii 1b15972d07 Suppress an unnecessary (optimization-related) warning in one of the kernel tests 2012-11-07 06:14:33 -08:00
José Valim 4a2d773e34 Merge pull request #631 from yrashk/defrecord-doc-improvement
Improve defrecord documentation
2012-11-07 05:59:56 -08:00
Yurii Rashkovskii 8185ff7333 Improve defrecord documentation 2012-11-07 05:57:39 -08:00
José Valim 2263d44ccf Merge pull request #630 from yrashk/t-helper-doc-improvement
Improve t helper documentation
2012-11-07 05:05:53 -08:00
Yurii Rashkovskii 46f3696799 Improve t helper documentation 2012-11-07 04:52:30 -08:00
José Valim 9b276f52d9 Update lib/iex/lib/iex/helpers.ex
Fix grammar and improve docs
2012-11-07 13:46:00 +01:00
José Valim 175a2dc1eb Merge pull request #628 from yrashk/t3-helper
Resurrect t/3 iex helper
2012-11-07 04:42:43 -08:00
Yurii Rashkovskii aba27a27e3 Resurrect t/3 iex helper
Closes #627
2012-11-07 04:38:11 -08:00
José Valim 1f56b673bd Support -q when running tests to compile only changed files 2012-11-07 13:04:51 +01:00
José Valim 41dbbbf714 Merge pull request #626 from yrashk/typespec-inspect-improvements-record
Add support for printing record typespecs
2012-11-07 04:01:48 -08:00
Yurii Rashkovskii 1331f23a27 Add support for printing record typespecs 2012-11-07 02:40:45 -08:00
José Valim 52a5f5c9c9 Generate a proper clause var name 2012-11-06 10:19:23 +01:00
José Valim 1ae30a56e2 Do not name quoted variables 2012-11-06 09:54:17 +01:00
José Valim 30bfd4736d Remove Calendar from the upcoming release 2012-11-05 15:19:10 +01:00
José Valim e9334fb7dc Tidy up Typespec API, improve docs 2012-11-05 14:51:51 +01:00
José Valim bd8648d490 Only generate needed clauses in Protocol 2012-11-05 11:20:29 +01:00
José Valim ec68b7dd8a Compile specs and callbacks to @spec and @callback so they are easier to access 2012-11-05 10:26:02 +01:00
José Valim a5539beb37 Use $(RELEASE_FLAG) in Makefile 2012-11-05 09:26:02 +01:00
José Valim 47ce7e3fd8 Actually define types for Macro.Env 2012-11-05 09:21:31 +01:00
José Valim 1113d3cfc9 Add support to record_type in defrecord
Closes #613
Closes #452
2012-11-05 09:14:07 +01:00
José Valim f160f3ee6f Merge remote-tracking branch 'yrashk/record-typespec' into record-typespec 2012-11-05 07:53:36 +01:00
José Valim 1bf06bbc1a Speed up protocols by concatenating modules at compilation time 2012-11-03 10:52:22 +01:00
José Valim 8b981efecd Improve shadowed variable warning 2012-11-03 09:15:46 +01:00
José Valim f95e4512b5 Update CHANGELOG 2012-11-02 10:19:18 +01:00
José Valim 12d14edab5 Deprecate Enum.times, closes #619 2012-11-02 10:17:39 +01:00
José Valim caa50368a7 Check if the first aliases argument is indeed an alias atom 2012-11-02 10:15:19 +01:00
José Valim a144d27886 Warn that function definitions should start with lowercase characters or underscore, closes #620 2012-11-02 10:05:17 +01:00
José Valim 413fcfdd65 Reorganize keyword handling clauses 2012-11-02 10:05:17 +01:00
José Valim 78c6c7201f Keywords are allowed after . identifier, closes #621 2012-11-02 10:05:17 +01:00
José Valim 76cf3e5bba Merge pull request #618 from yrashk/typespec-to_binary-ann_type
Support annotated types in Kernel.Typespec.to_binary
2012-11-01 07:07:51 -07:00
Yurii Rashkovskii 5d2336d23e Support annotated types in Kernel.Typespec.to_binary 2012-11-01 07:05:54 -07:00
José Valim 8280de5fa9 Merge pull request #615 from yrashk/specs-retrieval-fixes
Kernel.Typespec.types/1,3 should recognize typeps and opaques
2012-11-01 06:48:16 -07:00
Yurii Rashkovskii 8cadba5ea4 Kernel.Typespec.types/1,3 should recognize typeps and opaques
Also improves code reuse
2012-11-01 06:37:59 -07:00
José Valim 3750bcc8d1 Remove unused variables 2012-11-01 14:11:10 +01:00
José Valim 3fac73ea17 Merge pull request #609 from yrashk/iex-t
Implement a simple version of the t helper for the interactive shell
2012-11-01 05:23:03 -07:00
José Valim 01f9518899 Keep user's given name when rebinding variables
The following code:

    counter = 1
    counter = 2

Is now rewritten internally to:

    counter   = 1
    counter@1 = 2

Which gives us a couple things:

1. Better debugging
2. We can now show warnings for rebound variables not used

Elixir also keeps `_@` prefix to store internal variables
(for example, `_@1`, `_@MODULE`, etc).

Closes #607.
2012-11-01 12:37:08 +01:00
José Valim 11eb2d6d3c Do not print elixir modules as keywords, closes #603 2012-11-01 10:04:05 +01:00
Yurii Rashkovskii c00381780f Implement a simple version of the t helper for the interactive shell (prints out type/spec information)
Also implements corresponding helper functions in Kernel.Typespec
2012-10-31 22:26:59 -07:00
Yurii Rashkovskii b325e29d23 Record field types definition support 2012-10-31 16:07:13 -07:00
José Valim e2e1747acb Merge pull request #610 from yrashk/fix-typespec-to_binary_tuple_any
Fix Kernel.Typespec.to_binary for tuple() type (with no element type spe...
2012-10-31 08:42:58 -07:00
Yurii Rashkovskii 84501090ce Fix Kernel.Typespec.to_binary for tuple() type (with no element type specified) 2012-10-31 07:26:30 -07:00
José Valim c9dead1fc1 Merge pull request #608 from yrashk/typespec-misc
Typespec misc
2012-10-31 06:33:54 -07:00
Yurii Rashkovskii 4906fac3cf Remove dialyzer warnings from elixir_errors 2012-10-31 06:05:57 -07:00
Yurii Rashkovskii 1c827af937 Remove dialyzer warnings from ExUnit.Assertions 2012-10-31 05:56:38 -07:00
José Valim 06d9b26f94 Merge pull request #606 from yrashk/typespec-dict
Add typespecs for Dict
2012-10-31 05:42:47 -07:00
José Valim aecb1b3716 Merge pull request #605 from yrashk/typespec-enum
Add typespecs for Enum
2012-10-31 05:42:24 -07:00
Yurii Rashkovskii a322635971 Add typespecs for Dict 2012-10-31 05:40:55 -07:00
Yurii Rashkovskii 88fdb20eb0 Add typespecs for Enum
Also fixes some documentation mistakes
2012-10-31 05:29:33 -07:00
José Valim 6d5c2e1d22 Merge pull request #602 from yrashk/typespec-string
Add typespecs for String
2012-10-31 04:59:17 -07:00
José Valim 437ef403c4 Merge pull request #601 from yrashk/typespec-keyword
Add typespecs for Keyword
2012-10-31 04:44:28 -07:00
José Valim d5b6340977 Support (parallel) requires on mix run, closes #495 2012-10-31 12:43:24 +01:00
Yurii Rashkovskii 99befc6715 Add typespecs for Keyword 2012-10-31 04:41:59 -07:00
Yurii Rashkovskii e97e2b310e Add typespecs for String 2012-10-31 04:40:52 -07:00
José Valim d305c96762 Keep arguments order on OptionParser 2012-10-31 12:40:03 +01:00
José Valim e99f623b03 Purge loaded modules on tests 2012-10-31 12:37:18 +01:00
José Valim 562e7e391e Changing deps information on mix.exs forces users to fetch new dependencies
Closes #557
2012-10-31 11:22:09 +01:00
José Valim 69bccec7ae Make SCM formatting more flexible 2012-10-31 10:20:13 +01:00
José Valim e65b18b203 Wrap mix escriptize in Elixir's default CLI setup, closes #560 2012-10-31 10:10:38 +01:00
José Valim 3b33478cc0 Make typespecs compilation happen in two steps, closes #598 2012-10-31 09:44:16 +01:00
José Valim 4d427f9f50 Merge pull request #600 from yrashk/typespecs
Use Behaviour in Dict
2012-10-30 12:59:31 -07:00
Yurii Rashkovskii ea3775ecf0 Use Behaviour in Dict 2012-10-30 07:13:43 -07:00
José Valim 7607051224 Merge Typespec and Kernel.Typespec 2012-10-30 11:47:57 +01:00
José Valim 4fdec2c780 Only define types in protocols and records if one was not defined yet 2012-10-30 11:42:07 +01:00
José Valim 335ba59712 Merge pull request #599 from yrashk/typespec-to_binary
Add Typespec.to_binary/1
2012-10-30 03:41:59 -07:00
Yurii Rashkovskii 84e04b6a8b Add Typespec.to_binary/1 2012-10-30 03:37:44 -07:00
José Valim 06d1b47cc2 Support @opaque 2012-10-30 10:56:53 +01:00
José Valim c21c00d42f Add Kernel.Typespec.defines_type? 2012-10-30 10:48:33 +01:00
José Valim 097a427cb7 Ensure defoverridable functions can be referred in many clauses, closes #595 2012-10-30 10:37:10 +01:00
José Valim 8c317e3d43 Do not check clauses just after non quoted ones, closes #591 2012-10-30 10:13:32 +01:00
José Valim f7490bd5af Provide faster, with docs and debug info first compilation 2012-10-30 10:01:09 +01:00
José Valim 6aec85fae5 Avoid possible race conditions on test suite 2012-10-30 09:17:16 +01:00
José Valim cc22787c65 Do actual matching on = assertions 2012-10-30 09:17:16 +01:00
José Valim c547d70c5f Merge pull request #596 from devinus/dict-force-get
Add get! method to dict behaviour
2012-10-30 01:16:49 -07:00
José Valim a35b3f8ac1 Merge pull request #597 from yrashk/typespec-access
Add support for access macro in typespecs
2012-10-30 01:16:18 -07:00
Yurii Rashkovskii 856cd70d86 Add support for access macro in typespecs 2012-10-29 17:42:14 -07:00
Devin Torres beac96cd55 Add get! method to dict behaviour 2012-10-29 17:17:10 -05:00
José Valim edf34edf2a Merge pull request #593 from yrashk/dialyze-release
Run make dialyze only after .release
2012-10-29 02:06:13 -07:00
José Valim f4daf0a39d Merge pull request #592 from yrashk/char_list_string_t-types
Add String.t() and char_list() types, discourage use of string() because...
2012-10-29 02:04:46 -07:00
Yurii Rashkovskii a67df523ce Run make dialyze only after .release 2012-10-29 02:01:38 -07:00
Yurii Rashkovskii 154e73b0d4 Add String.t() and char_list() types, discourage use of string() because it might lead to confusion.
Note, however, that dialyzer will often talk about string() anyway.

Addresses #588
2012-10-29 01:55:30 -07:00
José Valim 18fd04010e Merge pull request #590 from yrashk/typespec-build-fix
Typespec build fix
2012-10-29 00:40:38 -07:00
Yurii Rashkovskii a6d86d9f44 Protocol compilation happens during the internal phase, therefore @callback and @type are not yet available 2012-10-28 20:01:42 -07:00
Yurii Rashkovskii a2b79e4331 Correct dialyzer option use when adding Elixir to PLT 2012-10-28 19:42:19 -07:00
Yurii Rashkovskii 6a9a3b3efe Reorder core modules so that elixir will compile 2012-10-28 19:31:31 -07:00
José Valim 591b95bfb9 Protocols should define a @callback implementation 2012-10-29 01:38:37 +01:00
José Valim 833a153b33 Avoid dispatching to locals from compiled structures 2012-10-29 00:45:32 +01:00
José Valim fdc2657af8 Deprecate Behaviour.defcallback/1 in favor of the typespecs wrapper, closes #520 2012-10-29 00:04:43 +01:00
José Valim f2a284e268 Use new callbacks on Mix.SCM 2012-10-28 23:08:07 +01:00
José Valim d75fb11dc4 Disable internal as soon as core is fully compiled
This allows us to compile callbacks for the following
modules, which would trigger missing behaviour warnings
otherwise.
2012-10-28 22:33:13 +01:00
José Valim 6edab89372 Add Module.to_binary 2012-10-28 21:56:50 +01:00
José Valim 078da93886 Use new callbacks in URI.Parser 2012-10-28 21:48:53 +01:00
José Valim ee380fe79c Add support to new style behaviours 2012-10-28 21:41:53 +01:00
José Valim ce26312803 Define a type t for records and protocols 2012-10-28 21:29:47 +01:00
José Valim 7c78f22a95 Allow to define a callback from a spec 2012-10-28 21:20:11 +01:00
José Valim 925ab81ca2 Define __info__(:module) instead of __info__(:self) 2012-10-28 21:20:11 +01:00
José Valim 4301d14555 Merge pull request #586 from yrashk/dialyzer-makefile
Add make dialyze target
2012-10-28 02:26:08 -07:00
Yurii Rashkovskii 523170228a Add make dialyze target 2012-10-27 17:02:38 -07:00
José Valim 2b9d67dd9d More dialyzer fixes 2012-10-28 01:36:38 +02:00
José Valim 7eae5a5e8e Avoid negation in unless, simply invert the clauses 2012-10-28 01:15:54 +02:00
José Valim 87391a5832 Implement more returns boolean checks 2012-10-28 01:06:00 +02:00
José Valim b9dbd56f3a Do not take :__exception__ for granted as a record 2012-10-28 00:26:21 +02:00
José Valim 215a28ceed Avoid generating extra clauses for protocols 2012-10-27 23:52:40 +02:00
José Valim f95529b7b3 Many dialyzer fixes 2012-10-27 22:20:15 +02:00
José Valim 21d2fec7aa Filter no auto import list 2012-10-27 21:45:18 +02:00
José Valim 6afb2684c6 Merge pull request #584 from yrashk/kernel-typespec-fix
Typespec fix in Kernel
2012-10-27 12:44:26 -07:00
Yurii Rashkovskii 93f426fcff Typespec fix in Kernel 2012-10-27 12:31:49 -07:00
José Valim 94b4831709 Revert "Work around a dialyzer bug"
The dialyzer bug was caused because orelse clauses was generated
from inside out. For example, the following clause:

    var in [:a, :b, :c]

Was generating:

    ((var == :a orelse var == :b) orelse var == :c)

Instead of:

    var == :a orelse var == :b orelse var == :c

While it isn't clear why the first one caused the dialyzer to
hang, the `in` operator was fixed to output the second in the
previous commit.

This reverts commit 3c15bcc36f.
2012-10-27 21:01:50 +02:00
José Valim b497632540 Invert the order arguments are handled on in 2012-10-27 20:55:51 +02:00
José Valim d1cbefdbe0 No need to special case the compiler_clause anymore 2012-10-27 20:54:37 +02:00
José Valim 2704ba1a07 Convert file to char list, closes #583 2012-10-27 19:57:04 +02:00
José Valim 3c15bcc36f Work around a dialyzer bug 2012-10-27 12:34:33 +02:00
José Valim 40cda34a23 Direct to Binary.Inspect 2012-10-27 12:18:44 +02:00
José Valim 95bea1a4c7 Fix delegations in Process.ex 2012-10-27 11:19:07 +02:00
José Valim 85fce7aaef Fix delegations in Node.ex (ht @yrashk) 2012-10-27 11:15:56 +02:00
José Valim fc76de175c Also add debug_info to Erlang source 2012-10-27 11:04:15 +02:00
José Valim bae135c0ef Use inspect to convert atoms 2012-10-27 10:53:03 +02:00
José Valim e653dece8d Merge pull request #579 from yrashk/macro-to-binary-call-atom
Macro.to_binary should correctly process expressions involving regular a...
2012-10-27 01:47:57 -07:00
José Valim 71368b6590 Compile with --debug-info by default, closes #580 2012-10-27 10:43:23 +02:00
Yurii Rashkovskii 50960e0e88 Macro.to_binary should correctly process expressions involving regular atom module names (e.g. :lists.seq(1,100)) 2012-10-26 15:15:06 -07:00
José Valim 03bb0831e5 Support do blocks on __scope__ special form 2012-10-26 20:37:25 +02:00
José Valim 194647ef5a Only unquote() if unquote is permitted 2012-10-26 18:54:51 +02:00
José Valim 1514ffbabc Refactor the Makefile and recompile kernel by default 2012-10-26 18:32:46 +02:00
José Valim c3da51f32c Use String.printable? instead of Enum.all? on tests
When we first run the tests, docs are not compiled
and therefore Enum.all?/1 and Enum.all?/2 are shown
in the autocompletion results.

We chose String.printable? because it also ends
with a question mark but has only one arity.

Closes #578
2012-10-26 18:12:24 +02:00
José Valim 91b43f244c Also autocomplete when / was already typed in 2012-10-25 14:32:37 +02:00
José Valim fa10442606 Show available arities when function name is complete in autocomplete 2012-10-25 14:08:38 +02:00
José Valim 35fd0bf896 Show docs for default generated functions 2012-10-25 13:36:08 +02:00
José Valim 7a8fa34020 Only show documented functions on autocomplete
Autocompletion is used for discovery by other developers
and we should avoid exposing non documented functions.
2012-10-25 13:36:08 +02:00
José Valim 9a949702f1 Merge pull request #575 from yrashk/patch-3
Minor documentation fix for defdelegate
2012-10-24 14:32:58 -07:00
Yurii Rashkovskii e8ea3e834c Minor documentation fix for defdelegate 2012-10-24 14:15:37 -07:00
José Valim 2f3289aaef Try a different and simpler approach to clauses warnings, closes #567 2012-10-24 16:57:23 +02:00
José Valim 2de1102544 Merge pull request #573 from yrashk/string-at-fix
Out-of-bounds String.at should return nil
2012-10-24 03:59:41 -07:00
Yurii Rashkovskii 445398b7bc Out-of-bounds String.at should return nil 2012-10-24 03:53:42 -07:00
José Valim e2e6e5d0d4 Store overridable funtions defined in before_compile callbacks, closes #572 2012-10-24 11:55:12 +02:00
José Valim d33eff2aec Improve docs for __aliases__ 2012-10-24 11:35:45 +02:00
José Valim 9a2ae1bac7 Rename String.Unicode.sequences to graphemes 2012-10-24 11:20:45 +02:00
José Valim 31e1965ea2 Merge pull request #566 from yrashk/unicode-sequences
Add support for named sequences and make String module aware of it so it...
2012-10-24 01:57:22 -07:00
José Valim dc169dc783 Fix an issue where variables inside clauses remained unassigned 2012-10-23 11:48:34 +02:00
José Valim a617628848 Atoms true and false are also boolean returns 2012-10-23 11:48:34 +02:00
José Valim 431c8739cc Merge pull request #569 from yrashk/patch-2
README: List all applications provided
2012-10-22 01:43:54 -07:00
Yurii Rashkovskii 96c84370e3 README: List all applications provided 2012-10-22 01:32:36 -07:00
Yurii Rashkovskii 4717c9a10b Add support for named sequences and make String module aware of it so it does correct job at calculating length, picking characters, etc. 2012-10-22 01:10:49 -07:00
José Valim 78230df154 Update Makefile 2012-10-22 01:41:07 +03:00
José Valim b1e130cace Merge pull request #563 from yrashk/unicode-makefile
Minor improvements to the unicode compilation step
2012-10-21 15:40:16 -07:00
Yurii Rashkovskii 354be2383d Minor improvements to the unicode compilation step 2012-10-21 15:17:47 -07:00
José Valim 3b63ce2ab1 Merge pull request #562 from yrashk/unicode-version
Add String.Unicode.version/0
2012-10-21 14:55:27 -07:00
José Valim d220618f12 Compare all known compilation types 2012-10-21 23:53:28 +02:00
Yurii Rashkovskii 5c4a5db828 Add String.Unicode.version/0 2012-10-21 14:52:15 -07:00
José Valim 146dddf0b5 Use bin/elixirc (fix the build) 2012-10-21 23:37:43 +02:00
José Valim b2c8669c4c Support String.downcase and String.upcase according to Unicode 6.2.0 2012-10-21 23:35:03 +02:00
José Valim 318454c092 Optimize range match when values are known at compile time 2012-10-21 20:56:47 +02:00
José + Yehuda 0aa4c7227e Rename Mix.SCM behaviour functions 2012-10-21 20:56:17 +02:00
José Valim a17140ea50 Remove unused function 2012-10-21 19:44:13 +02:00
José + Yehuda e03bf4d7d8 Initial work on Calendar.format (based on ICU) 2012-10-21 19:43:11 +02:00
José + Yehuda 85a9d70544 Improvements to Calendar module 2012-10-21 19:00:16 +02:00
José Valim 1cfdb00c4d Improvements to SCM behaviour specs 2012-10-21 18:08:18 +02:00
José Valim b94def32d2 Add package.exs, update RELEASE file 2012-10-20 22:25:19 +02:00
José Valim f438630e82 Remove deprecated branches 2012-10-20 22:06:03 +02:00
José Valim 293044c168 Give more context to h() 2012-10-20 20:24:15 +02:00
José Valim e975336d5d Bump to 0.7.1.dev 2012-10-20 20:18:19 +02:00
José Valim 43b8a26b36 Revert "Remove Calendar from the upcoming release"
This reverts commit 411f1ce55d.
2012-10-20 20:10:59 +02:00
José Valim 26d35eead1 Use Keyword and check for atom key 2012-10-20 20:09:42 +02:00
176 changed files with 31347 additions and 2885 deletions
+4 -2
View File
@@ -1,5 +1,5 @@
/.eunit/*
/.full
/.release
/lib/*/ebin/*
/lib/*/tmp
/lib/*/test/tmp
@@ -10,4 +10,6 @@
/deps/*
/ebin
/rel/elixir
erl_crash.dump
erl_crash.dump
.dialyzer_plt
.dialyzer.base_plt
+2 -1
View File
@@ -1,9 +1,10 @@
language: erlang
script: "make compile && make .full test"
script: "make compile && make .release test"
notifications:
irc: "irc.freenode.org#elixir-lang"
recipients:
- jose.valim@plataformatec.com.br
- yrashk@gmail.com
otp_release:
- R15B02
- R15B01
+59 -1
View File
@@ -1,3 +1,61 @@
# v0.7.2 (2012-12-04)
* enhancements
* [CLI] `--debug-info` is now true by default
* [ExUnit] Make ExUnit exit happen in two steps allowing developers to add custom `at_exit` hooks
* [IEx] Many improvements to helpers functions `h/1`, `s/1` and others
* [Kernel] Functions defined with `fn` can now handle many clauses
* [Kernel] Raise an error if clauses with different arities are defined in the same function
* [Kernel] `function` macro now accepts arguments in `M.f/a` and `f/a` formats
* [Macro] Improvements to `Macro.to_binary`
* [Mix] Mix now echoes the output as it comes when executing external commands such as git or rebar
* [Mix] Mix now validates `application` callback's values
* [Record] Record accessors are now optimized and can be up to 6x faster in some cases
* [String] Support `\xXX` and `\x{HEX}` escape sequences in strings, char lists and regexes
* bug fix
* [Bootstrap] Compiling Elixir source no longer fails if environment variables contain utf-8 entries
* [IEx] IEx will now wait for all command line options to be processed before starting
* [Kernel] Ensure proper stacktraces when showing deprecations
* deprecations
* [Enum] `Enum.qsort` is deprecated in favor of `Enum.sort`
* [ExUnit] `assert left in right` is deprecated in favor of `assert left inlist right`
* [List] `List.sort` and `List.uniq` have been deprecated in favor of their `Enum` counterparts
* [Record] Default-based generated functions are deprecated
* [Typespec] Enhancements and deprecations to the `@spec/@callback` and the fun type syntax
# v0.7.1 (2012-11-18)
* enhancements
* [IEx] Only show documented functions and also show docs for default generated functions
* [IO] Add `IO.binread`, `IO.binwrite` and `IO.binreadline` to handle raw binary file operations
* [ExUnit] Add support for user configuration at `HOME/.ex_unit.exs`
* [ExUnit] Add support for custom formatters via a well-defined behaviour
* [Kernel] Add support for `defrecordp`
* [Kernel] Improved dialyzer support
* [Kernel] Improved error messages when creating functions with aliases names
* [Mix] Improve SCM behaviour to allow more robust integration
* [Mix] Changing deps information on `mix.exs` forces users to fetch new dependencies
* [Mix] Support (parallel) requires on mix run
* [Mix] Support `-q` when running tests to compile only changed files
* [String] Support `String.downcase` and `String.upcase` according to Unicode 6.2.0
* [String] Add support for graphemes in `String.length`, `String.at` and others
* [Typespec] Support `@opaque` as attribute
* [Typespec] Define a default type `t` for protocols and records
* [Typespec] Add support for the access protocol in typespecs
* bug fix
* [Kernel] Fix an issue where variables inside clauses remained unassigned
* [Kernel] Ensure `defoverridable` functions can be referred in many clauses
* [Kernel] Allow keywords as function names when following a dot (useful when integrating with erlang libraries)
* [File] File is opened by default on binary mode instead of utf-8
* deprecations
* [Behaviour] `defcallback/1` is deprecated in favor of `defcallback/2` which matches erlang `@callbacks`
* [Enum] `Enum.times` is deprecated in favor of using ranges
* [System] `halt` moved to `System` module
# v0.7.0 (2012-10-20)
* enhancements
@@ -58,7 +116,7 @@
* deprecations
* [Access] The semantics of the access protocol were reduced from a broad query API to simple data structure key-based access
* [ExUnit] Some assertions are deprecated in favor of simply using `assert()`
* [ExUnit] Some assertions are deprecated in favor of simply using `assert()`
* [File] `File.read_info` is deprecated in favor of `File.stat`
* [IO] `IO.print` is deprecated in favor of `IO.write`
* [Kernel] Deprecate `__LINE__` and `__FUNCTION__` in favor of `__ENV__.line` and `__ENV__.function`
+68 -32
View File
@@ -1,21 +1,23 @@
REBAR:=$(shell echo `pwd`/rebar)
ELIXIRC:=bin/elixirc --ignore-module-conflict $(ELIXIRC_OPTS)
ERLC:=erlc -I lib/elixir/include
ERL:=erl -I lib/elixir/include -noshell -env ERL_LIBS $ERL_LIBS:lib
FULLFLAG:=.full
VERSION:=0.7.0
REBAR := $(shell echo `pwd`/rebar)
ELIXIRC := bin/elixirc --ignore-module-conflict $(ELIXIRC_OPTS)
ERLC := erlc -I lib/elixir/include
ERL := erl -I lib/elixir/include -noshell -env ERL_LIBS $ERL_LIBS:lib
VERSION := 0.7.2
RELEASE_FLAG := .release
INSTALL_PATH := /usr/local
.PHONY: 1
.NOTPARALLEL: compile
#==> Templates
define TASK_TEMPLATE
define APP_TEMPLATE
$(1): lib/$(1)/ebin/Elixir-$(2).beam lib/$(1)/ebin/$(1).app
lib/$(1)/ebin/$(1).app:
@ cd lib/$(1) && ../../bin/elixir ../../bin/mix compile.app
@ cd lib/$(1) && ../../bin/mix compile.app
lib/$(1)/ebin/Elixir-$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1)/lib/*/*.ex) $(wildcard lib/$(1)/lib/*/*/*.ex) $$(FORCE)
lib/$(1)/ebin/Elixir-$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1)/lib/*/*.ex) $(wildcard lib/$(1)/lib/*/*/*.ex)
@ echo "==> $(1) (compile)"
@ $$(ELIXIRC) "lib/$(1)/lib/**/*.ex" -o lib/$(1)/ebin
@@ -25,7 +27,11 @@ test_$(1): $(1)
endef
#==> Compilation tasks
KERNEL:=lib/elixir/ebin/Elixir-Kernel.beam
UNICODE:=lib/elixir/ebin/Elixir-String-Unicode.beam
default: compile
compile: lib/elixir/src/elixir.app.src erlang elixir
@@ -36,30 +42,47 @@ lib/elixir/src/elixir.app.src: src/elixir.app.src
erlang:
@ cd lib/elixir && $(REBAR) compile
# We need to compile only EEx (without the app)
# file so we can compile Mix
elixir: kernel lib/eex/ebin/Elixir-EEx.beam mix ex_unit eex iex
# Since Mix depends on EEx and EEx depends on
# Mix, we first compile EEx without the .app
# file, then mix and then compile eex fully
elixir: kernel unicode lib/eex/ebin/Elixir-EEx.beam mix ex_unit eex iex
kernel: $(KERNEL)
$(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex $(FORCE)
@ if [ -f $(KERNEL) ]; then \
echo "==> kernel (compile)"; \
$(ELIXIRC) "lib/elixir/lib/**/*.ex" -o lib/elixir/ebin; \
else \
echo "==> bootstrap (compile)"; \
$(ERL) -s elixir_compiler core -s erlang halt; \
$(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex
@ if [ ! -f $(KERNEL) ]; then \
echo "==> bootstrap (compile)"; \
$(ERL) -s elixir_compiler core -s erlang halt; \
fi
@ echo "==> kernel (compile)";
@ $(ELIXIRC) "lib/elixir/lib/**/*.ex" -o lib/elixir/ebin;
@ rm -rf lib/elixir/ebin/elixir.app
@ cd lib/elixir && $(REBAR) compile
$(eval $(call TASK_TEMPLATE,ex_unit,ExUnit))
$(eval $(call TASK_TEMPLATE,eex,EEx))
$(eval $(call TASK_TEMPLATE,mix,Mix))
$(eval $(call TASK_TEMPLATE,iex,IEx))
unicode: $(UNICODE)
$(UNICODE): lib/elixir/priv/unicode.ex lib/elixir/priv/UnicodeData.txt lib/elixir/priv/NamedSequences.txt
@ echo "==> unicode (compile)";
@ echo "This step can take up to a minute to compile in order to embed the Unicode database"
@ $(ELIXIRC) lib/elixir/priv/unicode.ex -o lib/elixir/ebin;
$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
$(eval $(call APP_TEMPLATE,eex,EEx))
$(eval $(call APP_TEMPLATE,mix,Mix))
$(eval $(call APP_TEMPLATE,iex,IEx))
install: compile
@ echo "==> elixir (install)"
for dir in lib/*; do \
install -m755 -d $(INSTALL_PATH)/lib/elixir/$$dir/ebin; \
install -m644 $$dir/ebin/* $(INSTALL_PATH)/lib/elixir/$$dir/ebin; \
done
install -m755 -d $(INSTALL_PATH)/lib/elixir/bin
install -m755 $(filter-out %.bat, $(wildcard bin/*)) $(INSTALL_PATH)/lib/elixir/bin
install -m755 -d $(INSTALL_PATH)/bin
ln -sf $(INSTALL_PATH)/lib/elixir/bin/* $(INSTALL_PATH)/bin
clean:
@ cd lib/elixir && $(REBAR) clean
rm -rf .full
rm -rf $(RELEASE_FLAG)
rm -rf ebin
rm -rf lib/*/ebin
rm -rf lib/*/test/tmp
@@ -67,31 +90,32 @@ clean:
rm -rf lib/mix/tmp
#==> Release tasks
$(FULLFLAG): $(wildcard lib/*/ebin/*)
make ELIXIRC_OPTS="--debug-info" FORCE=1
touch $(FULLFLAG)
zip: $(FULLFLAG)
rm -rf v$(VERSION).zip
zip -9 -r v$(VERSION).zip bin CHANGELOG.md LEGAL lib/*/ebin LICENSE README.md rel
$(RELEASE_FLAG): compile
touch $(RELEASE_FLAG)
docs: $(FULLFLAG)
docs: $(RELEASE_FLAG)
mkdir -p ebin
rm -rf docs
cp -R -f lib/*/ebin/*.beam ./ebin
bin/elixir ../exdoc/bin/exdoc
rm -rf ebin
release_zip: $(RELEASE_FLAG)
rm -rf v$(VERSION).zip
zip -9 -r v$(VERSION).zip bin CHANGELOG.md LEGAL lib/*/ebin LICENSE README.md rel
release_docs: docs
cd ../elixir-lang.github.com && git checkout master
rm -rf ../elixir-lang.github.com/docs/master
mv output ../elixir-lang.github.com/docs/master
release_erl: $(FULLFLAG)
release_erl: $(RELEASE_FLAG)
@ rm -rf rel/elixir
@ cd rel && ../rebar generate
#==> Tests tasks
test: test_erlang test_elixir
test_erlang: compile
@@ -106,3 +130,15 @@ test_elixir: test_kernel test_mix test_ex_unit test_eex test_iex
test_kernel: compile
@ echo "==> kernel (exunit)"
@ cd lib/elixir && time ../../bin/elixir -r "test/elixir/test_helper.exs" -pr "test/elixir/**/*_test.exs";
.dialyzer.base_plt:
@ echo "==> Adding Erlang/OTP basic applications to a new base PLT"
@ dialyzer --output_plt .dialyzer.base_plt --build_plt --apps erts kernel stdlib compiler syntax_tools inets crypto ssl
dialyze: $(RELEASE_FLAG) .dialyzer.base_plt
@ rm -f .dialyzer_plt
@ cp .dialyzer.base_plt .dialyzer_plt
@ echo "==> Adding Elixir to PLT..."
@ dialyzer --plt .dialyzer_plt --add_to_plt -r lib/elixir/ebin lib/ex_unit/ebin lib/mix/ebin lib/iex/ebin lib/eex/ebin
@ echo "==> Dialyzing Elixir..."
@ dialyzer --plt .dialyzer_plt -r lib/elixir/ebin lib/ex_unit/ebin lib/mix/ebin lib/iex/ebin lib/eex/ebin
+4
View File
@@ -30,6 +30,10 @@ If you want to contribute, Elixir code is divided in applications inside the `li
* `ex_unit` - Simple test framework that ships with Elixir;
* `iex` — IEx, Elixir's interactive shell
* `mix` — Elixir's build tool
We usually keep a list of features and bugs [in the issue tracker][2].
# Important links
+8 -4
View File
@@ -4,24 +4,28 @@ This document simply outlines the release process:
1) Remove .dev extension from current versions
2) Run `make clean test` to ensure all tests pass from scratch
2) Run `make clean test` to ensure all tests pass from scratch and the CI is green
3) Ensure CHANGELOG is updated and tag release version with timestamp in it
4) Commit changes above and update stable branch
5) Create tag from stable branch
5) Create tag from master branch
6) Release new docs, update elixir-lang.org
7) Push new zip to Elixir's downloads page
8) After release, bump versions and add .dev back
8) Push package to expm
9) After release, bump versions and add .dev back
## Places where version is mentioned
* src/elixir.app.src
* lib/elixir/src/elixir.app.src
* lib/elixir/lib/system.ex
* rel/reltool.config
* Makefile
* CHANGELOG
* CHANGELOG
* package.exs
+3 -3
View File
@@ -2,9 +2,9 @@
if [ $# -eq 0 ]; then
echo "Usage: `basename $0` [elixir switches] [compiler switches] [.ex files]
-o The directory to output compiled files
--no-docs Do not attach documentation with compiled code
--debug-info Attach debug info to compiled modules
-o The directory to output compiled files
--no-docs Do not attach documentation to compiled modules
--no-debug-info Do not attach debug info to compiled modules
--ignore-module-conflict
** Options given after -- are passed down to the executed code
+4 -7
View File
@@ -5,14 +5,11 @@ if "%*" == "" (
goto run
)
:documentation
echo Usage: %~nx0 [switches] [.ex files]
echo Usage: %~nx0 [elixir switches] [compiler switches] [.ex files]
echo.
echo -v Prints version and exit
echo -o The directory to output compiled files
echo -pa path Prepend the given path to Erlang code path (*)
echo -pz path Append the given path to Erlang code path (*)
echo --no-docs Do not attach documentation with compiled code
echo --debug-info Attach debug info to compiled modules
echo -o The directory to output compiled files
echo --no-docs Do not attach documentation to compiled modules
echo --no-debug-info Do not attach debug info to compiled modules
echo --ignore-module-conflict
echo.
echo ** Options marked with (*) can be given more than once
+1 -1
View File
@@ -11,4 +11,4 @@ readlink_f () {
SELF=$(readlink_f "$0")
SCRIPT_PATH=$(dirname "$SELF")
exec "$SCRIPT_PATH"/elixir --no-halt -e "IEx.cli" "$@"
exec "$SCRIPT_PATH"/elixir --no-halt --erl "-user Elixir-IEx-CLI" "$@"
+1 -1
View File
@@ -1,2 +1,2 @@
@echo off
call "%~dp0\elixir.bat" --no-halt -e "IEx.cli" %*
call "%~dp0\elixir.bat" --no-halt -e "IEx.start" %*
+17 -3
View File
@@ -1,3 +1,17 @@
#!/usr/bin/env elixir
Mix.start
Mix.CLI.run
#!/bin/sh
readlink_f () {
cd "$(dirname "$1")" > /dev/null
local filename="$(basename "$1")"
if [ -h "$filename" ]; then
readlink_f "$(readlink "$filename")"
else
echo "`pwd -P`/$filename"
fi
}
SELF=$(readlink_f "$0")
SCRIPT_PATH=$(dirname "$SELF")
EXECUTABLE="elixir"
if [ "$1" = "iex" ]; then EXECUTABLE="iex"; fi
exec "$SCRIPT_PATH"/$EXECUTABLE -e "Mix.start; Mix.CLI.run" -- "$@"
+9 -1
View File
@@ -1,2 +1,10 @@
@echo off
call "%~dp0\elixir.bat" "%~dp0\mix" %*
if "%1" == "iex" (
goto iex
) else (
goto elixir
)
:iex
call "%~dp0\iex.bat" -e "Mix.start" -e "Mix.CLI.run" -- %*
:elixir
call "%~dp0\elixir.bat" -e "Mix.start" -e "Mix.CLI.run" -- %*
+1 -1
View File
@@ -68,7 +68,7 @@ defmodule EEx.Compiler do
else
key = length(state.dict)
placeholder = '__EEX__(' ++ integer_to_list(key) ++ ');'
{ current ++ placeholder ++ new_lines ++ chars, state.prepend_dict([{key, buffer}]) }
{ current ++ placeholder ++ new_lines ++ chars, state.update_dict([{key, buffer}|&1]) }
end
end
+8 -11
View File
@@ -14,23 +14,20 @@
super=false, %% when true, it means super was invoked
caller=false, %% when true, it means caller was invoked
name_args=false, %% when true, it means arguments should be named
macro=[], %% a stack with macros nesting
module=nil, %% the current module
function=nil, %% the current function
recur=nil, %% the current loop function to be recurred
vars=[], %% a dict of defined variables and their alias
temp_vars=[], %% a dict of all variables defined in a particular assign
clause_vars=[], %% a dict of all variables defined in a particular clause
quote_vars=[], %% a dict of all quoted variables
clause_vars=nil, %% a dict of all variables defined in a particular clause
extra_guards=nil, %% extra guards from args expansion
counter=0, %% a counter for the variables defined
file=(<<"nofile">>), %% the current scope filename
counter=[], %% a counter for the variables defined
local=nil, %% the scope to evaluate local functions against
aliases=[], %% an orddict with aliases by new -> old names
requires=elixir_dispatch:default_requires(), %% a set with modules required
macros=elixir_dispatch:default_macros(), %% a list with macros imported by module
functions=elixir_dispatch:default_functions(), %% a list with functions imported by module
scheduled=[]}). %% scheduled modules to be loaded
scheduled=[], %% scheduled modules to be loaded
file, %% the current scope filename
aliases, %% an orddict with aliases by new -> old names
requires, %% a set with modules required
macros, %% a list with macros imported by module
functions}). %% a list with functions imported by module
-record(elixir_quote, {
line=0,
+3 -4
View File
@@ -5,11 +5,10 @@ defprotocol Access do
The Access protocol is the underlying protocol invoked
when the brackets syntax is used. For instance, `foo[bar]`
is translated to `access foo, bar` which, by default,
invokes `Access.access` protocol.
invokes the `Access.access` protocol.
This protocol is limited and is implemented only for the
following built-in types: keywords, records, atoms and
functions.
following built-in types: keywords, records and functions.
"""
@only [List, Function, Record, Atom]
@@ -17,7 +16,7 @@ defprotocol Access do
@doc """
Receives the element being accessed and the access item.
"""
def access(element, qualifier)
def access(container, key)
end
defimpl Access, for: List do
+61 -29
View File
@@ -12,13 +12,13 @@ defmodule Behaviour do
use Behaviour
@doc "Parses the given URL"
defcallback parse(arg)
defcallback parse(uri_info :: URI.Info.t) :: URI.Info.t
@doc "Defines a default port"
defcallback default_port()
defcallback default_port() :: integer
end
And then a specific protocol may use it as:
And then a specific module may use it as:
defmodule URI.HTTP do
@behaviour URI.Parser
@@ -32,46 +32,74 @@ defmodule Behaviour do
## Implementation
Internally, Erlang call `behaviour_info(:callbacks)`
to obtain all functions that a behaviour should
implemented. Therefore, all this module does is
to define `behaviour_info(:callbacks)` with the
`defcallback` definitions.
Behaviours since Erlang R15 must be defined via
`@callback` attributes. `defcallback` is a simple
mechanism that defines the `@callback` attribute
according to the type specification and also allows
docs and defines a custom function signature.
The callbacks and their documentation can be retrieved
via the `__behaviour__` callback function.
"""
@doc """
Annotates the given function is a callback. `defcallback` is
slightly different than simple using `def` because, even if
`defcallback` contains default values, a default function
won't be generated, which would happen with `def`.
Defines a callback according to the given type specification.
"""
defmacro defcallback(fun, do: return) do
IO.write "[WARNING] defcallback(fun, do: return) is deprecated, please use defcallback(fun :: return) instead\n#{Exception.env_stacktrace(__CALLER__)}"
do_defcallback(fun, return, __CALLER__)
end
defmacro defcallback({ :::, _, [fun, return] }) do
do_defcallback(fun, return, __CALLER__)
end
defmacro defcallback(fun) do
{ name, args } = :elixir_clauses.extract_args(fun)
length = length(args)
do_defcallback(fun, quote(do: term), __CALLER__)
end
{ args, defaults } = Enum.map_reduce(args, 0, function do
{ ://, _, [expr, _] }, acc ->
{ expr, acc + 1 }
expr, acc ->
{ expr, acc }
end)
defp do_defcallback(fun, return, caller) do
case Macro.extract_args(fun) do
{ name, args } -> :ok
:error ->
raise ArgumentError, message: "invalid syntax in defcallback #{Macro.to_binary(fun)}"
end
attributes = Enum.map (length - defaults)..length, fn(i) ->
quote do
@__behaviour_callbacks { unquote(name), unquote(i) }
end
arity = length(args)
Enum.each args, fn
{ :::, _, [left, right] } ->
ensure_not_default(left)
ensure_not_default(right)
left
other ->
ensure_not_default(other)
other
end
quote do
__block__ unquote(attributes)
def unquote(name)(unquote_splicing(args))
@callback unquote(name)(unquote_splicing(args)) :: unquote(return)
Behaviour.store_docs __MODULE__, unquote(caller.line), unquote(name), unquote(arity)
end
end
defp ensure_not_default({ ://, _, [_, _] }) do
raise ArgumentError, message: "default arguments // not supported in defcallback"
end
defp ensure_not_default(_), do: :ok
@doc false
def store_docs(module, line, name, arity) do
doc = Module.get_attribute module, :doc
Module.delete_attribute module, :doc
Module.put_attribute module, :__behaviour_docs, { { name, arity }, line, doc }
end
@doc false
defmacro __using__(_) do
quote do
Module.register_attribute(__MODULE__, :__behaviour_callbacks, accumulate: true)
Module.register_attribute(__MODULE__, :__behaviour_docs, accumulate: true)
@before_compile unquote(__MODULE__)
import unquote(__MODULE__)
end
@@ -81,8 +109,12 @@ defmodule Behaviour do
defmacro __before_compile__(_) do
quote do
@doc false
def behaviour_info(:callbacks) do
@__behaviour_callbacks
def __behaviour__(:callbacks) do
__MODULE__.behaviour_info(:callbacks)
end
def __behaviour__(:docs) do
@__behaviour_docs
end
end
end
+7
View File
@@ -38,6 +38,13 @@ defmodule Binary.Dict do
end
end
def get!(dict(data), key) do
case :orddict.find(to_binary(key), data) do
{:ok, value} -> value
:error -> raise(KeyError, key: key)
end
end
def put(dict(data), key, value) do
dict(:orddict.store to_binary(key), value, data)
end
+14 -26
View File
@@ -128,8 +128,7 @@ defimpl Binary.Inspect, for: Atom do
valid_atom_identifier?(binary) ->
":" <> binary
valid_ref_identifier?(binary) ->
"Elixir-" <> rest = binary
bc <<r>> inbits rest, do: <<to_dot(r)>>
Module.to_binary(atom)
atom in Macro.binary_ops or atom in Macro.unary_ops ->
":" <> binary
true ->
@@ -139,9 +138,6 @@ defimpl Binary.Inspect, for: Atom do
# Detect if atom is an atom alias (Elixir-Foo-Bar-Baz)
defp to_dot(?-), do: ?.
defp to_dot(l), do: l
defp valid_ref_identifier?("Elixir" <> rest) do
valid_ref_piece?(rest)
end
@@ -265,20 +261,13 @@ defimpl Binary.Inspect, for: List do
cond do
printable?(thing) ->
escape(list_to_binary(thing), ?')
keywords?(thing) ->
Keyword.keyword?(thing) ->
"[" <> join_keywords(thing, opts) <> "]"
true ->
container_join(thing, "[", "]", opts)
end
end
## keywords?
defp keywords?([]), do: true
defp keywords?([{ key, _value } | rest]) when is_atom(key) do
keywords?(rest)
end
defp keywords?(_other), do: false
defp join_keywords(thing, opts) do
Enum.join(lc {key, value} inlist thing do
key_to_binary(key, opts) <> ": " <> Binary.Inspect.inspect(value, opts)
@@ -334,9 +323,7 @@ defimpl Binary.Inspect, for: Tuple do
name = elem(tuple, 0)
if is_atom(name) and match?("Elixir-" <> _, atom_to_binary(name)) do
if name in [BitString, List, Tuple, Atom, Number, Any] do
record_inspect(tuple, opts)
else
unless name in [BitString, List, Tuple, Atom, Number, Any] do
try do
target = Module.concat(Binary.Inspect, name)
target.inspect(tuple, opts)
@@ -348,19 +335,16 @@ defimpl Binary.Inspect, for: Tuple do
end
end
defp record_inspect(exception, opts) when is_exception(exception) do
list = tuple_to_list(exception)
[name,_|tail] = list
[_|fields] = name.__record__(:fields)
record_join(name, fields, tail, opts)
end
defp record_inspect(record, opts) do
list = tuple_to_list(record)
[name|tail] = list
if (fields = record_fields(name)) && (length(fields) == size(record) - 1) do
record_join(name, fields, tail, opts)
if Enum.first(tail) == :__exception__ do
record_join(name, tl(fields), tl(tail), opts)
else
record_join(name, fields, tail, opts)
end
end
end
@@ -422,8 +406,12 @@ defimpl Binary.Inspect, for: Regex do
"""
def inspect(thing, _) do
"%r" <> Binary.Inspect.inspect(Regex.source(thing), []) <> Regex.opts(thing)
def inspect(regex, _opts) when size(regex) == 5 do
"%r" <> Binary.Inspect.inspect(Regex.source(regex), []) <> Regex.opts(regex)
end
def inspect(other, opts) do
Binary.Inspect.inspect other, Keyword.put(opts, :raw, true)
end
end
+28 -28
View File
@@ -52,28 +52,41 @@ defmodule Code do
end
@doc """
Evalutes the contents given by string. The second argument is the binding
(which should be a Keyword) followed by a keyword list of options. The
options can be:
Evalutes the contents given by string. The second argument is the
binding (which should be a keyword) followed by a keyword list of
environment options. Those options can be:
* `:file` - the file to be considered in the evaluation
* `:line` - the line the script starts
* `:aliases` - a list of tuples with the alias and its target
* `:requires` - a list of modules required
* `:functions` - a list of tuples where the first element is a module
and the second a list of imported function names and arity
* `:macros` - a list of tuples where the first element is a module
and the second a list of imported macro names and arity
* `:delegate_locals_to` - delegate local calls to the given module,
otherwise functions are evaluated inside Erlang's default scope.
the default is to not delegate
## Examples
Code.eval "a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line
#=> { 3, [ {:a, 1}, {:b, 2} ] }
When passing the __ENV__'s file and line, we could simply get
the location which already returns both fields as a keyword list:
For convenience, you can my pass `__ENV__` as argument and
all imports, requires and aliases will be automatically carried
over:
Code.eval "a + b", [a: 1, b: 2], __ENV__.location
Code.eval "a + b", [a: 1, b: 2], __ENV__
#=> { 3, [ {:a, 1}, {:b, 2} ] }
"""
def eval(string, binding // [], opts // []) do
def eval(string, binding // [], opts // [])
def eval(string, binding, Macro.Env[] = env) do
eval(string, binding, env.to_keywords)
end
def eval(string, binding, opts) do
{ value, binding, _scope } =
:elixir.eval :unicode.characters_to_list(string), binding, opts
{ value, binding }
@@ -82,15 +95,8 @@ defmodule Code do
@doc """
Evalutes the quoted contents.
## Options
This function accepts a list of options. The supported
options are:
* `:file` - The filename to be used in stacktraces
and the file reported in the __ENV__ variable.
* `:line` - The line reported in the __ENV__ variable.
This function accepts a list of environment options.
Check `Code.eval` for more information.
## Examples
@@ -99,8 +105,8 @@ defmodule Code do
Code.eval_quoted contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line
#=> { 3, [ {:a, 1}, {:b, 2} ] }
For convenience, you can also pass the current __ENV__ and
the proper information will be extracted:
For convenience, you can my pass `__ENV__` as argument and
all options will be automatically extracted from the environment:
Code.eval_quoted contents, [a: 1, b: 2], __ENV__
#=> { 3, [ {:a, 1}, {:b, 2} ] }
@@ -109,7 +115,7 @@ defmodule Code do
def eval_quoted(quoted, binding // [], opts // [])
def eval_quoted(quoted, binding, Macro.Env[] = env) do
eval_quoted(quoted, binding, env.location)
eval_quoted(quoted, binding, env.to_keywords)
end
def eval_quoted(quoted, binding, opts) do
@@ -319,7 +325,7 @@ defmodule Code do
:undefined -> error
_ ->
try do
module.__info__(:self)
module.__info__(:module)
{ :module, module }
rescue
UndefinedFunctionError -> error
@@ -353,13 +359,7 @@ defmodule Code do
if File.regular?(file) do
file
else
prefix = "#{file}.exs"
if File.regular?(prefix) do
IO.write "[WARNING] Passing a file without .exs extension to Code.load_file or Code.require_file is deprecated, please pass the full name instead\n#{Exception.formatted_stacktrace}"
prefix
else
raise ArgumentError, message: "could not load #{file}"
end
raise ArgumentError, message: "could not load #{file}"
end
end
+47 -6
View File
@@ -26,12 +26,23 @@ defmodule Dict do
"""
@doc false
def behaviour_info(:callbacks) do
[ delete: 2, empty: 1, get: 3, has_key?: 2,
keys: 1, merge: 3, put: 3, size: 1, to_list: 1,
update: 3, update: 4, values: 1 ]
end
use Behaviour
@type key :: any
@type value :: any
@type t :: tuple
defcallback delete(t, key) :: t
defcallback empty(t) :: t
defcallback get(t, key, value) :: value
defcallback has_key?(t, key) :: boolean
defcallback keys(t) :: list(key)
defcallback merge(t, t, (key, value, value -> value)) :: t
defcallback put(t, key, value) :: t
defcallback size(t) :: non_neg_integer()
defcallback to_list(t) :: list()
defcallback update(t, key, (value -> value)) :: t
defcallback values(t) :: list(value)
@doc """
Returns a list containing all dict's keys.
@@ -44,6 +55,7 @@ defmodule Dict do
Dict.keys d #=> [:a,:b]
"""
@spec keys(t) :: [key]
def keys(dict) do
elem(dict, 0).keys(dict)
end
@@ -57,6 +69,7 @@ defmodule Dict do
Dict.values d #=> [1,2]
"""
@spec values(t) :: [value]
def values(dict) do
elem(dict, 0).values(dict)
end
@@ -70,6 +83,7 @@ defmodule Dict do
Dict.size d #=> 2
"""
@spec size(t) :: non_neg_integer
def size(dict) do
elem(dict, 0).size(dict)
end
@@ -84,6 +98,7 @@ defmodule Dict do
Dict.has_key?(d, :b) #=> false
"""
@spec has_key?(t, key) :: boolean
def has_key?(dict, key) do
elem(dict, 0).has_key?(dict, key)
end
@@ -100,10 +115,28 @@ defmodule Dict do
Dict.get d, :b, 3 #=> 3
"""
@spec get(t, key) :: value | nil
@spec get(t, key, value) :: value
def get(dict, key, default // nil) do
elem(dict, 0).get(dict, key, default)
end
@doc """
Returns the value associated with `key` in `dict`. If `dict` does not
contain `key`, it raises `KeyError`.
## Examples
d = new [a: 1]
Dict.get d, :a #=> 1
Dict.get d, :b #=> raises KeyError[key: :b]
"""
@spec get!(t, key) :: value | no_return
def get!(dict, key) do
elem(dict, 0).get!(dict, key)
end
@doc """
Stores the given `value` under `key` in `dict`.
If `dict` already has `key`, the stored value is replaced by the new one.
@@ -115,6 +148,7 @@ defmodule Dict do
#=> [a: 3, b: 2]
"""
@spec put(t, key, value) :: t
def put(dict, key, val) do
elem(dict, 0).put(dict, key, val)
end
@@ -132,6 +166,7 @@ defmodule Dict do
Dict.delete d, :a #=> [b: 2]
"""
@spec delete(t, key) :: t
def delete(dict, key) do
elem(dict, 0).delete(dict, key)
end
@@ -150,6 +185,7 @@ defmodule Dict do
#=> [a: 3, b: 2, d: 4]
"""
@spec merge(t, t) :: t
def merge(dict1, dict2) do
merge(dict1, dict2, fn(_k, _v1, v2) -> v2 end)
end
@@ -168,6 +204,7 @@ defmodule Dict do
#=> [a: 4, b: 2, d: 4]
"""
@spec merge(t, t, (key, value, value -> value)) :: t
def merge(dict1, dict2, fun) do
elem(dict1, 0).merge(dict1, dict2, fun)
end
@@ -183,6 +220,7 @@ defmodule Dict do
#=> [a: -1, b: 2]
"""
@spec update(t, key, (value -> value)) :: t
def update(dict, key, fun) do
elem(dict, 0).update(dict, key, fun)
end
@@ -199,6 +237,7 @@ defmodule Dict do
#=> [a: 1, b: 2, c: 3]
"""
@spec update(t, key, value, (value -> value)) :: t
def update(dict, key, initial, fun) do
elem(dict, 0).update(dict, key, initial, fun)
end
@@ -206,6 +245,7 @@ defmodule Dict do
@doc """
Returns an empty dict of the same type as `dict`.
"""
@spec empty(t) :: t
def empty(dict) do
elem(dict, 0).empty(dict)
end
@@ -214,6 +254,7 @@ defmodule Dict do
Returns a list of key-value pairs stored in `dict`.
No particular order is enforced.
"""
@spec to_list(t) :: list
def to_list(dict) do
elem(dict, 0).to_list(dict)
end
+427 -156
View File
@@ -68,9 +68,12 @@ defmodule Enum do
`{ key, value }` tuple.
"""
@type t :: Enum.Iterator.t
@type element :: any
@doc """
Invokes the given `fun` for each item in the `collection` and returns true if
each invocation returns true as well, otherwise it shirt-circuits and returns
each invocation returns true as well, otherwise it short-circuits and returns
false.
## Examples
@@ -88,6 +91,9 @@ defmodule Enum do
Enum.all? [1,nil,3] #=> false
"""
@spec all?(t) :: boolean
@spec all?(t, (element -> boolean)) :: boolean
def all?(collection, fun // fn(x) -> x end)
def all?(collection, fun) when is_list(collection) do
@@ -122,6 +128,9 @@ defmodule Enum do
Enum.any? [false,true,false] #=> true
"""
@spec any?(t) :: boolean
@spec any?(t, (element -> boolean)) :: boolean
def any?(collection, fun // fn(x) -> x end)
def any?(collection, fun) when is_list(collection) do
@@ -151,6 +160,7 @@ defmodule Enum do
Enum.at! [2,4,6], 4 #=> raises Enum.OutOfBoundsError
"""
@spec at!(t, non_neg_integer) :: element | no_return
def at!(collection, n) when is_list(collection) and n >= 0 do
do_at!(collection, n)
end
@@ -172,6 +182,7 @@ defmodule Enum do
Enum.count [1,2,3] #=> 3
"""
@spec count(t) :: non_neg_integer
def count(collection) do
I.count(collection)
end
@@ -179,6 +190,7 @@ defmodule Enum do
@doc """
Counts for how many items the function returns true.
"""
@spec count(t, (element -> boolean)) :: non_neg_integer
def count(collection, fun) when is_list(collection) do
do_count(collection, fun)
end
@@ -193,8 +205,8 @@ defmodule Enum do
end
@doc """
Drops the first `count` items from the collection. Expects an ordered
collection.
Drops the first `count` items from the collection.
Expects an ordered collection.
## Examples
@@ -203,8 +215,23 @@ defmodule Enum do
Enum.drop [1,2,3], 0 #=> [1,2,3]
"""
def drop(collection, count) do
elem split(collection, count), 1
@spec drop(t, integer) :: list
def drop(collection, count) when is_list(collection) and count >= 0 do
do_drop(collection, count)
end
def drop(collection, count) when count >= 0 do
case I.iterator(collection) do
{ iterator, pointer } ->
do_drop(pointer, iterator, count)
list when is_list(list) ->
do_drop(list, count)
end
end
def drop(collection, count) when count < 0 do
{ list, count } = iterate_and_count(collection, count)
drop(list, count)
end
@doc """
@@ -216,6 +243,7 @@ defmodule Enum do
Enum.drop_while [1,2,3,4,5], fn(x) -> x < 3 end
#=> [3,4,5]
"""
@spec drop_while(t, (element -> boolean)) :: list
def drop_while(collection, fun) when is_list(collection) do
do_drop_while(collection, fun)
end
@@ -238,6 +266,7 @@ defmodule Enum do
Enum.each ['some', 'example'], fn(x) -> IO.puts x end
"""
@spec each(t, (element -> any)) :: :ok
def each(collection, fun) when is_list(collection) do
:lists.foreach(fun, collection)
:ok
@@ -262,6 +291,7 @@ defmodule Enum do
Enum.empty? [1,2,3] #=> false
"""
@spec empty?(t) :: boolean
def empty?(collection) when is_list(collection) do
collection == []
end
@@ -283,6 +313,7 @@ defmodule Enum do
#=> [2]
"""
@spec filter(t, (element -> boolean)) :: list
def filter(collection, fun) when is_list(collection) do
lc item inlist collection, fun.(item), do: item
end
@@ -305,6 +336,7 @@ defmodule Enum do
#=> [4]
"""
@spec filter_map(t, (element -> boolean), (element -> element)) :: list
def filter_map(collection, filter, mapper) when is_list(collection) do
lc item inlist collection, filter.(item), do: mapper.(item)
end
@@ -334,6 +366,9 @@ defmodule Enum do
#=> 3
"""
@spec find(t, (element -> any)) :: element | :nil
@spec find(t, any, (element -> any)) :: element | :nil
def find(collection, ifnone // nil, fun)
def find(collection, ifnone, fun) when is_list(collection) do
@@ -362,6 +397,9 @@ defmodule Enum do
#=> true
"""
@spec find_value(t, (element -> any)) :: any | :nil
@spec find_value(t, any, (element -> any)) :: any | :nil
def find_value(collection, ifnone // nil, fun)
def find_value(collection, ifnone, fun) when is_list(collection) do
@@ -392,6 +430,7 @@ defmodule Enum do
#=> 2
"""
@spec find_index(t, (element -> any)) :: non_neg_integer | :nil
def find_index(collection, fun) when is_list(collection) do
do_find_index(collection, 0, fun)
end
@@ -414,6 +453,7 @@ defmodule Enum do
Enum.first [1,2,3] #=> 1
"""
@spec first(t) :: :nil | element
def first([]), do: nil
def first([h|_]), do: h
@@ -442,6 +482,8 @@ defmodule Enum do
Enum.join([1,2,3], ' = ') #=> '1 = 2 = 3'
"""
@spec join(t) :: String.t
@spec join(t, String.t | char_list) :: String.t | char_list
def join(collection, joiner // "")
def join(collection, joiner) when is_list(joiner) do
@@ -475,6 +517,7 @@ defmodule Enum do
#=> [a: -1, b: -2]
"""
@spec map(t, (element -> any)) :: list
def map(collection, fun) when is_list(collection) do
lc item inlist collection, do: fun.(item)
end
@@ -501,10 +544,12 @@ defmodule Enum do
## Examples
Enum.map_join([1,2,3], &1 * 2) #=> "246"
Enum.map_join([1,2,3], &1 * 2, " = ") #=> "2 = 4 = 6"
Enum.map_join([1,2,3], &1 * 2, ' = ') #=> '2 = 4 = 6'
Enum.map_join([1,2,3], " = ", &1 * 2) #=> "2 = 4 = 6"
Enum.map_join([1,2,3], ' = ', &1 * 2) #=> '2 = 4 = 6'
"""
@spec map_join(t, (element -> any)) :: String.t
@spec map_join(t, String.t | char_list, (element -> any)) :: String.t | char_list
def map_join(collection, joiner // "", mapper)
def map_join(collection, joiner, mapper) when is_list(joiner) do
@@ -539,6 +584,7 @@ defmodule Enum do
#=> { [2, 4, 6], 6 }
"""
@spec map_reduce(t, any, (element, any -> any)) :: any
def map_reduce(collection, acc, f) when is_list(collection) do
:lists.mapfoldl(f, acc, collection)
end
@@ -563,6 +609,7 @@ defmodule Enum do
#=> { [2], [1,3] }
"""
@spec partition(t, (element -> any)) :: {list, list}
def partition(collection, fun) when is_list(collection) do
do_partition(collection, fun, [], [])
end
@@ -587,6 +634,7 @@ defmodule Enum do
#=> 6
"""
@spec reduce(t, any, (element, any -> any)) :: any
def reduce(collection, acc, fun) when is_list(collection) do
:lists.foldl(fun, acc, collection)
end
@@ -603,14 +651,13 @@ defmodule Enum do
@doc """
Reverses the collection.
Expects an ordered collection.
## Examples
Enum.reverse [1, 2, 3]
#=> [3, 2, 1]
"""
@spec reverse(t) :: list
def reverse(collection) when is_list(collection) do
:lists.reverse(collection)
end
@@ -622,21 +669,17 @@ defmodule Enum do
end
end
@doc """
Sorts the collection according to the quick sort algorithm.
## Examples
Enum.qsort [3,2,1] #=> [1,2,3]
"""
@doc false
@spec qsort(t) :: list
def qsort(collection) when is_list(collection) do
IO.write "[WARNING] Enum.qsort is deprecated, please use Enum.sort instead\n#{Exception.formatted_stacktrace}"
do_list_qsort(collection, [])
end
def qsort(collection) do
case I.iterator(collection) do
{ iterator, pointer } ->
IO.write "[WARNING] Enum.qsort is deprecated, please use Enum.sort instead\n#{Exception.formatted_stacktrace}"
do_qsort(pointer, iterator, [])
list when is_list(list) ->
qsort(list)
@@ -644,11 +687,58 @@ defmodule Enum do
end
@doc """
Splits the enumerable into two collections, leaving `count` elements in the
first one. If `count` is a negative number, it starts couting from the back
to the beginning of the collection. Be aware that a negative `count`
implies in an iteration through the whole collection.
Expects an ordered collection.
Sorts the collection using the merge sort algorithm.
## Examples
Enum.sort [3,2,1] #=> [1,2,3]
"""
@spec sort(t) :: list
def sort(collection) when is_list(collection) do
:lists.sort(collection)
end
def sort(collection) do
case I.iterator(collection) do
{ iterator, pointer } ->
do_sort(pointer, iterator, &1 <= &2)
list when is_list(list) ->
sort(list)
end
end
@doc """
Sorts the collection using the merge sort algorithm.
## Examples
Enum.sort [3,2,1], &1 > &2 #=> [1,2,3]
"""
@spec sort(t, (element, element -> boolean)) :: list
def sort(collection, fun) when is_list(collection) do
:lists.sort(fun, collection)
end
def sort(collection, fun) do
case I.iterator(collection) do
{ iterator, pointer } ->
do_sort(pointer, iterator, fun)
list when is_list(list) ->
sort(list, fun)
end
end
@doc """
Splits the enumerable into two collections, leaving `count`
elements in the first one. If `count` is a negative number,
it starts couting from the back to the beginning of the
collection.
Be aware that a negative `count` implies the collection
will be iterate twice. One to calculate the position and
another one to do the actual splitting.
## Examples
@@ -659,6 +749,7 @@ defmodule Enum do
Enum.split [1,2,3], -5 #=> { [], [1,2,3] }
"""
@spec split(t, integer) :: {list, list}
def split(collection, count) when is_list(collection) and count >= 0 do
do_split(collection, count, [])
end
@@ -673,16 +764,8 @@ defmodule Enum do
end
def split(collection, count) when count < 0 do
reducer = fn(x, acc) -> {x, acc + 1} end
{ list , total_items } = Enum.map_reduce(collection, 0, reducer)
real_count = max(0, total_items - abs(count))
split(list, real_count)
end
@doc false
def split_with(collection, fun) do
IO.write "[WARNING] Enum.split_with is deprecated, please use split_while instead\n#{Exception.formatted_stacktrace}"
split_while(collection, fun)
{ list, count } = iterate_and_count(collection, count)
split(list, count)
end
@doc """
@@ -694,6 +777,7 @@ defmodule Enum do
Enum.split_while [1,2,3,4], fn x -> x == 2 end
#=> { [1], [2, 3, 4] }
"""
@spec split_while(t, (element -> boolean)) :: {list, list}
def split_while(collection, fun) when is_list(collection) do
do_split_while(collection, fun, [])
end
@@ -718,8 +802,23 @@ defmodule Enum do
Enum.take [1,2,3], 0 #=> []
"""
def take(collection, count) do
elem split(collection, count), 0
@spec take(t, integer) :: list
def take(collection, count) when is_list(collection) and count >= 0 do
do_take(collection, count)
end
def take(collection, count) when count >= 0 do
case I.iterator(collection) do
{ iterator, pointer } ->
do_take(pointer, iterator, count)
list when is_list(list) ->
do_take(list, count)
end
end
def take(collection, count) when count < 0 do
{ list, count } = iterate_and_count(collection, count)
take(list, count)
end
@doc """
@@ -732,6 +831,7 @@ defmodule Enum do
#=> [1, 2]
"""
@spec take_while(t, (element -> boolean)) :: list
def take_while(collection, fun) when is_list(collection) do
do_take_while(collection, fun)
end
@@ -746,40 +846,26 @@ defmodule Enum do
end
@doc """
Iterates the given function n times, passing values
from 0 to n - 1.
Iterates the enumerable removing all duplicated items.
## Examples
Enum.times 3, fn(x) -> IO.inspect x end
0
1
2
Enum.uniq [1,2,3,2,1]
#=> [1, 2, 3]
"""
def times(times, function) when times >= 0 do
case is_function(function, 0) do
true ->
do_times_0(times, 1, function)
_ ->
do_times_1(times, 1, function)
end
times
@spec uniq(t) :: list
def uniq(collection) when is_list(collection) do
do_uniq(collection, [])
end
@doc """
Iterates the given function n times, passing values from 1
to n. Also has an accumulator similar to reduce to store the
value between computations.
## Examples
Enum.times 5, 0, fn(x, acc) -> acc + x end
#=> 15
"""
def times(times, acc, function) when times >= 0 do
do_times_2(times, 1, function, acc)
def uniq(collection) do
case I.iterator(collection) do
{ iterator, pointer } ->
do_uniq(pointer, iterator, [])
list when is_list(list) ->
do_uniq(list, [])
end
end
@doc """
@@ -788,6 +874,7 @@ defmodule Enum do
dictated by the first enum. In case the second list is shorter,
values are filled with nil.
"""
@spec zip(t, t) :: [{any, any}]
def zip(coll1, coll2) when is_list(coll1) do
do_zip(coll1, iterator(coll2))
end
@@ -814,6 +901,25 @@ defmodule Enum do
[]
end
defp iterate_and_count(collection, count) do
{ list, total_items } = do_iterate_and_count(collection)
{ list, max(0, total_items - abs(count)) }
end
defp do_iterate_and_count(collection) when is_list(collection) do
{ collection, length(collection) }
end
defp do_iterate_and_count(collection) do
case I.iterator(collection) do
{ iterator, pointer } ->
reducer = fn(x, acc) -> { x, acc + 1 } end
do_map_reduce(pointer, iterator, [], 0, reducer)
list when is_list(list) ->
do_iterate_and_count(list)
end
end
## Implementations
## all?
@@ -904,6 +1010,32 @@ defmodule Enum do
0
end
## drop
defp do_drop([_|t], counter) when counter > 0 do
do_drop(t, counter - 1)
end
defp do_drop(list, 0) do
list
end
defp do_drop([], _) do
[]
end
defp do_drop({ _, next }, iterator, counter) when counter > 0 do
do_drop(iterator.(next), iterator, counter - 1)
end
defp do_drop(extra, iterator, 0) do
to_list(extra, iterator)
end
defp do_drop(:stop, _, _) do
[]
end
## drop_while
defp do_drop_while([h|t], fun) do
@@ -956,24 +1088,6 @@ defmodule Enum do
ifnone
end
## find_value
defp do_find_value([h|t], ifnone, fun) do
fun.(h) || do_find_value(t, ifnone, fun)
end
defp do_find_value([], ifnone, _) do
ifnone
end
defp do_find_value({ h, next }, iterator, ifnone, fun) do
fun.(h) || do_find_value(iterator.(next), iterator, ifnone, fun)
end
defp do_find_value(:stop, _, ifnone, _) do
ifnone
end
## find_index
defp do_find_index([h|t], counter, fun) do
@@ -1000,6 +1114,24 @@ defmodule Enum do
nil
end
## find_value
defp do_find_value([h|t], ifnone, fun) do
fun.(h) || do_find_value(t, ifnone, fun)
end
defp do_find_value([], ifnone, _) do
ifnone
end
defp do_find_value({ h, next }, iterator, ifnone, fun) do
fun.(h) || do_find_value(iterator.(next), iterator, ifnone, fun)
end
defp do_find_value(:stop, _, ifnone, _) do
ifnone
end
## each
defp do_each({ h, next }, iterator, fun) do
@@ -1039,42 +1171,6 @@ defmodule Enum do
[]
end
## reduce
defp do_reduce({ h, next }, iterator, acc, fun) do
do_reduce(iterator.(next), iterator, fun.(h, acc), fun)
end
defp do_reduce(:stop, _, acc, _) do
acc
end
## split_while
defp do_split_while([h|t], fun, acc) do
if fun.(h) do
do_split_while(t, fun, [h|acc])
else
{ :lists.reverse(acc), [h|t] }
end
end
defp do_split_while([], _, acc) do
{ :lists.reverse(acc), [] }
end
defp do_split_while({ h, next } = extra, iterator, fun, acc) do
if fun.(h) do
do_split_while(iterator.(next), iterator, fun, [h|acc])
else
{ :lists.reverse(acc), to_list(extra, iterator) }
end
end
defp do_split_while(:stop, _, _, acc) do
{ :lists.reverse(acc), [] }
end
## join
defp do_join([h|t], joiner, nil) do
@@ -1103,6 +1199,16 @@ defmodule Enum do
acc || ""
end
## map
defp do_map({ h, next }, iterator, fun) do
[fun.(h)|do_map(iterator.(next), iterator, fun)]
end
defp do_map(:stop, _, _) do
[]
end
## map join
defp do_map_join([h|t], mapper, joiner, nil) do
@@ -1131,16 +1237,6 @@ defmodule Enum do
acc || ""
end
## map
defp do_map({ h, next }, iterator, fun) do
[fun.(h)|do_map(iterator.(next), iterator, fun)]
end
defp do_map(:stop, _, _) do
[]
end
## map_reduce
defp do_map_reduce({ h, next }, iterator, list_acc, acc, f) do
@@ -1178,6 +1274,16 @@ defmodule Enum do
{ :lists.reverse(acc1), :lists.reverse(acc2) }
end
## reduce
defp do_reduce({ h, next }, iterator, acc, fun) do
do_reduce(iterator.(next), iterator, fun.(h, acc), fun)
end
defp do_reduce(:stop, _, acc, _) do
acc
end
## reverse
defp do_reverse({ h, next }, iterator, acc) do
@@ -1188,6 +1294,174 @@ defmodule Enum do
acc
end
## sort
defp do_sort(extra, iterator, fun) do
case sort_take(extra, iterator, 2, []) do
{ [y, x], next } -> sort_split(y, x, next, iterator, fun, [], [], fun.(x, y))
{ other, _ } -> other
end
end
defp sort_take({ h, next }, iterator, counter, acc) when counter > 0 do
sort_take(iterator.(next), iterator, counter - 1, [h|acc])
end
defp sort_take(extra, _iterator, 0, acc) do
{ acc, extra }
end
defp sort_take(:stop, _, _, acc) do
{ acc, :stop }
end
defp sort_split(y, x, { z, next }, iterator, fun, r, rs, bool) do
cond do
fun.(y, z) == bool ->
sort_split(z, y, iterator.(next), iterator, fun, [x | r], rs, bool)
fun.(x, z) == bool ->
sort_split(y, z, iterator.(next), iterator, fun, [x | r], rs, bool)
r == [] ->
sort_split(y, x, iterator.(next), iterator, fun, [z], rs, bool)
true ->
sort_split_pivot(y, x, iterator.(next), iterator, fun, r, rs, z, bool)
end
end
defp sort_split(y, x, :stop, _iterator, fun, r, rs, bool) do
sort_merge([[y, x | r] | rs], fun, bool)
end
defp sort_split_pivot(y, x, { z, next }, iterator, fun, r, rs, s, bool) do
cond do
fun.(y, z) == bool ->
sort_split_pivot(z, y, iterator.(next), iterator, fun, [x | r], rs, s, bool)
fun.(x, z) == bool ->
sort_split_pivot(y, z, iterator.(next), iterator, fun, [x | r], rs, s, bool)
fun.(s, z) == bool ->
sort_split(z, s, iterator.(next), iterator, fun, [], [[y, x | r] | rs], bool)
true ->
sort_split(s, z, iterator.(next), iterator, fun, [], [[y, x | r] | rs], bool)
end
end
defp sort_split_pivot(y, x, :stop, _iterator, fun, r, rs, s, bool) do
sort_merge([[s], [[y, x | r] | rs]], fun, bool)
end
defp sort_merge(list, fun, true), do:
reverse_sort_merge(list, [], fun, true)
defp sort_merge(list, fun, false), do:
sort_merge(list, [], fun, false)
defp sort_merge([t1, [h2 | t2] | l], acc, fun, true), do:
sort_merge(l, [sort_merge_1(t1, h2, t2, [], fun, false) | acc], fun, true)
defp sort_merge([[h2 | t2], t1 | l], acc, fun, false), do:
sort_merge(l, [sort_merge_1(t1, h2, t2, [], fun, false) | acc], fun, false)
defp sort_merge([l], [], _fun, _bool), do: l
defp sort_merge([l], acc, fun, bool), do:
reverse_sort_merge([:lists.reverse(l, []) | acc], [], fun, bool)
defp sort_merge([], acc, fun, bool), do:
reverse_sort_merge(acc, [], fun, bool)
defp reverse_sort_merge([[h2 | t2], t1 | l], acc, fun, true), do:
reverse_sort_merge(l, [sort_merge_1(t1, h2, t2, [], fun, true) | acc], fun, true)
defp reverse_sort_merge([t1, [h2 | t2] | l], acc, fun, false), do:
reverse_sort_merge(l, [sort_merge_1(t1, h2, t2, [], fun, true) | acc], fun, false)
defp reverse_sort_merge([l], acc, fun, bool), do:
sort_merge([:lists.reverse(l, []) | acc], [], fun, bool)
defp reverse_sort_merge([], acc, fun, bool), do:
sort_merge(acc, [], fun, bool)
defp sort_merge_1([h1 | t1], h2, t2, m, fun, bool) do
if fun.(h1, h2) == bool do
sort_merge_2(h1, t1, t2, [h2 | m], fun, bool)
else
sort_merge_1(t1, h2, t2, [h1 | m], fun, bool)
end
end
defp sort_merge_1([], h2, t2, m, _fun, _bool), do:
:lists.reverse(t2, [h2 | m])
defp sort_merge_2(h1, t1, [h2 | t2], m, fun, bool) do
if fun.(h1, h2) == bool do
sort_merge_2(h1, t1, t2, [h2 | m], fun, bool)
else
sort_merge_1(t1, h2, t2, [h1 | m], fun, bool)
end
end
defp sort_merge_2(h1, t1, [], m, _fun, _bool), do:
:lists.reverse(t1, [h1 | m])
## split
defp do_split([h|t], counter, acc) when counter > 0 do
do_split(t, counter - 1, [h|acc])
end
defp do_split(list, 0, acc) do
{ :lists.reverse(acc), list }
end
defp do_split([], _, acc) do
{ :lists.reverse(acc), [] }
end
defp do_split({ h, next }, iterator, counter, acc) when counter > 0 do
do_split(iterator.(next), iterator, counter - 1, [h|acc])
end
defp do_split(extra, iterator, 0, acc) do
{ :lists.reverse(acc), to_list(extra, iterator) }
end
defp do_split(:stop, _, _, acc) do
{ :lists.reverse(acc), [] }
end
## split_while
defp do_split_while([h|t], fun, acc) do
if fun.(h) do
do_split_while(t, fun, [h|acc])
else
{ :lists.reverse(acc), [h|t] }
end
end
defp do_split_while([], _, acc) do
{ :lists.reverse(acc), [] }
end
defp do_split_while({ h, next } = extra, iterator, fun, acc) do
if fun.(h) do
do_split_while(iterator.(next), iterator, fun, [h|acc])
else
{ :lists.reverse(acc), to_list(extra, iterator) }
end
end
defp do_split_while(:stop, _, _, acc) do
{ :lists.reverse(acc), [] }
end
## qsort (lists)
defp do_list_qsort([], acc) do
@@ -1238,30 +1512,30 @@ defmodule Enum do
end
end
## split
## take
defp do_split([h|t], counter, acc) when counter > 0 do
do_split(t, counter - 1, [h|acc])
defp do_take([h|t], counter) when counter > 0 do
[h|do_take(t, counter - 1)]
end
defp do_split(list, 0, acc) do
{ :lists.reverse(acc), list }
defp do_take(_list, 0) do
[]
end
defp do_split([], _, acc) do
{ :lists.reverse(acc), [] }
defp do_take([], _) do
[]
end
defp do_split({ h, next }, iterator, counter, acc) when counter > 0 do
do_split(iterator.(next), iterator, counter - 1, [h|acc])
defp do_take({ h, next }, iterator, counter) when counter > 0 do
[h|do_take(iterator.(next), iterator, counter - 1)]
end
defp do_split(extra, iterator, 0, acc) do
{ :lists.reverse(acc), to_list(extra, iterator) }
defp do_take(_extra, _iterator, 0) do
[]
end
defp do_split(:stop, _, _, acc) do
{ :lists.reverse(acc), [] }
defp do_take(:stop, _, _) do
[]
end
## take_while
@@ -1290,31 +1564,28 @@ defmodule Enum do
[]
end
## times
## uniq
defp do_times_0(limit, counter, _function) when counter >= limit do
defp do_uniq([h|t], acc) do
case :lists.member(h, acc) do
true -> do_uniq(t, acc)
false -> [h|do_uniq(t, [h|acc])]
end
end
defp do_times_0(limit, counter, function) do
function.()
do_times_0(limit, 1 + counter, function)
defp do_uniq([], _acc) do
[]
end
defp do_times_1(limit, counter, _function) when counter >= limit do
defp do_uniq({ h, next }, iterator, acc) do
case :lists.member(h, acc) do
true -> do_uniq(iterator.(next), iterator, acc)
false -> [h|do_uniq(iterator.(next), iterator, [h|acc])]
end
end
defp do_times_1(limit, counter, function) do
function.(counter)
do_times_1(limit, 1 + counter, function)
end
defp do_times_2(limit, counter, _function, acc) when counter >= limit do
acc
end
defp do_times_2(limit, counter, function, acc) do
new_acc = function.(counter, acc)
do_times_2(limit, 1 + counter, function, new_acc)
defp do_uniq(:stop, _, _acc) do
[]
end
## zip
+22 -3
View File
@@ -56,7 +56,7 @@ defmodule Exception do
ErlangError.new original: other
end
# Check the given module is a valid record.
# Check the given module is a valid exception record.
@doc false
def check!(module) do
unless :erlang.function_exported(module, :message, 1) do
@@ -86,13 +86,32 @@ defmodule Exception do
@doc """
Returns the stacktrace as a binary formatted as per `format_stacktrace/1`.
"""
def formatted_stacktrace(trace // System.stacktrace) do
def formatted_stacktrace(trace // nil) do
trace = trace || try do
throw(:stacktrace)
catch
:stacktrace -> Enum.drop(System.stacktrace, 1)
end
case trace do
[] -> ""
s -> " " <> Enum.map_join(s, "\n ", format_stacktrace(&1)) <> "\n"
end
end
@doc """
Returns a formatted stacktrace from the environment.
"""
def env_stacktrace(env) do
rest =
case env.function do
{ name, arity } -> format_module_fun_arity(env.module, name, arity)
nil -> "#{inspect env.module} (body)"
end
" #{format_file_line(env.location)}#{rest}\n"
end
@doc """
Formats each line in the stacktrace.
"""
@@ -217,7 +236,7 @@ defexception ErlangError, [original: nil] do
end
end
defexception Keyword.KeyError, key: nil do
defexception KeyError, key: nil do
def message(exception) do
"key not found: #{inspect exception.key}"
end
+80 -28
View File
@@ -59,8 +59,16 @@ defmodule File do
that interact with the filesystem have their naming
based on its UNIX variants. For example, deleting a
file is done with `File.rm`. Getting its stats with
`File.stat`. If you want to read or write to a file
in chunks, check the IO module.
`File.stat`.
In order to write and read files, one must use the
functions in the IO module. By default, a file is
opened on binary mode which requires the functions
`IO.binread`, `IO.binwrite` and `IO.binreadline` to
interact with the file. A developer may pass `:utf8`
as an option when opening the file and then all other
functions from IO are available, since they work directly
with Unicode data.
Most of the functions in this module return `:ok`
or `{ :ok, result }` in case of success, `{ :error, reason }`
@@ -974,6 +982,12 @@ defmodule File do
Note that the file size obtained with `stat/1` will most probably not
match the number of bytes that can be read from a compressed file.
* `:utf8` - This option denotes how data is actually stored in the disk file and
makes the file perform automatic translation of characters to and from utf-8.
If data is sent to a file in a format that cannot be converted to the utf-8
or if data is read by a function that returns data in a format that cannot cope
with the character range of the data, an error occurs and the file will be closed.
If a function is given to modes (instead of a list), it dispatches to `open/3`.
Check `http://www.erlang.org/doc/man/file.html#open-2` for more information about
@@ -1000,7 +1014,7 @@ defmodule File do
def open(path, modes // [])
def open(path, modes) when is_list(modes) do
F.open(path, open_defaults(modes, true, true))
F.open(path, open_defaults(modes, true))
end
def open(path, function) when is_function(function) do
@@ -1138,10 +1152,12 @@ defmodule File do
end
@doc """
Convert the file device into an iterator that can be
Converts the file device into an iterator that can be
passed into `Enum`. The device is iterated line
by line lazily, at the end of iteration the file is
closed.
by line, at the end of iteration the file is closed.
This reads the file as utf-8. CHeck out `File.biniterator`
to handle the file as a raw binary.
## Examples
@@ -1166,28 +1182,20 @@ defmodule File do
def iterator(device) do
fn ->
function = fn(_) ->
case :io.get_line(device, "") do
case :io.get_line(device, '') do
:eof ->
close(device)
:stop
{ :error, reason } ->
raise File.IteratorError, reason: reason
data ->
{ strip_ending(data), :ok }
{ data, :ok }
end
end
{ function, function.(:start) }
end
end
defp strip_ending("\r\n"), do: <<>>
defp strip_ending("\n"), do: <<>>
defp strip_ending(""), do: <<>>
defp strip_ending(<< h, t :: binary >>) do
<< h, strip_ending(t) :: binary >>
end
@doc """
Opens the given `file` with the given `mode` and
returns its iterator. Fails for the same reasons
@@ -1208,6 +1216,54 @@ defmodule File do
open!(file, mode) /> iterator
end
@doc """
Converts the file device into an iterator that can
be passed into `Enum` to iterate line by line as a
binary. Check `iterator/1` for more information.
"""
def biniterator(device)
def biniterator(file) when is_binary(file) or is_list(file) do
biniterator(file, [])
end
def biniterator(device) do
fn ->
function = fn(_) ->
case :file.read_line(device) do
:eof ->
close(device)
:stop
{ :error, reason } ->
raise File.IteratorError, reason: reason
{ :ok, data } ->
{ data, :ok }
end
end
{ function, function.(:start) }
end
end
@doc """
Opens the given `file` with the given `mode` and
returns its biniterator. Fails for the same reasons
as `File.open`.
"""
def biniterator(file, mode) do
case open(file, mode) do
{ :ok, device } -> { :ok, biniterator(device) }
error -> error
end
end
@doc """
Same as `biniterator/2` but raises if the file
cannot be opened.
"""
def biniterator!(file, mode // []) do
open!(file, mode) /> biniterator
end
## Helpers
# Normalize the given path by removing "..".
@@ -1229,22 +1285,18 @@ defmodule File do
join Enum.reverse(acc)
end
defp open_defaults([:charlist|t], add_encoding, _add_binary) do
open_defaults(t, add_encoding, false)
defp open_defaults([:charlist|t], _add_binary) do
open_defaults(t, false)
end
defp open_defaults([{:encoding, _} = h|t], _add_encoding, add_binary) do
[h|open_defaults(t, false, add_binary)]
defp open_defaults([:utf8|t], add_binary) do
open_defaults([{ :encoding, :utf8 }|t], add_binary)
end
defp open_defaults([h|t], add_encoding, add_binary) do
[h|open_defaults(t, add_encoding, add_binary)]
defp open_defaults([h|t], add_binary) do
[h|open_defaults(t, add_binary)]
end
defp open_defaults([], add_encoding, add_binary) do
options = []
if add_encoding, do: options = [{:encoding, :unicode}|options]
if add_binary, do: options = [:binary|options]
options
end
defp open_defaults([], true), do: [:binary]
defp open_defaults([], false), do: []
end
-11
View File
@@ -1,14 +1,3 @@
defmodule GenServer.Behavior do
@moduledoc false
defmacro __using__(_) do
IO.puts "Using GenServer.Behavior is deprecated, use GenServer.Behaviour instead"
quote do
use GenServer.Behaviour
end
end
end
defmodule GenServer.Behaviour do
@moduledoc """
By using this module, you get default GenServer callbacks
+8
View File
@@ -46,6 +46,14 @@ defmodule HashDict do
end
end
@doc false
def get!(dict(data), key) do
case :dict.find(key, data) do
{:ok, value} -> value
:error -> raise(KeyError, key: key)
end
end
@doc false
def put(dict(data), key, value) do
dict(:dict.store key, value, data)
+38 -2
View File
@@ -3,7 +3,7 @@ defmodule IO do
Module responsible for doing IO. Many functions in this
module expects an IO device and an io data encoded in UTF-8.
An IO device must be a pid is an atom representing a process.
An IO device must be a pid or an atom representing a process.
For convenience, Elixir provides `:stdio` and `:stderr` as
shortcut to Erlang's `:standard_io` and `:standard_error`.
@@ -39,7 +39,20 @@ defmodule IO do
end
@doc """
Read a line from the IO device. It returns:
Reads `count` bytes from the IO device as binary,
no unicode conversion happens.
Check `read/2` for more information.
"""
def binread(device // :stdio, count) do
case :file.read(map_dev(device), count) do
{ :ok, data } -> data
other -> other
end
end
@doc """
Reads a line from the IO device. It returns:
* `data` - The input characters.
@@ -56,6 +69,19 @@ defmodule IO do
:io.get_line(map_dev(device), "")
end
@doc """
Reads a line from the IO device as binary,
no unicode conversion happens.
Check `readline/1` for more information.
"""
def binreadline(device // :stdio) do
case :file.read_line(map_dev(device)) do
{ :ok, data } -> data
other -> other
end
end
@doc """
Writes the given argument to the given device.
By default the device is the standard output.
@@ -77,6 +103,16 @@ defmodule IO do
:io.put_chars map_dev(device), to_iodata(item)
end
@doc """
Writes the given argument to the given device
as a binary, no unicode conversion happens.
Check `write/2` for more information.
"""
def binwrite(device // :stdio, item) do
:file.write map_dev(device), to_iodata(item)
end
@doc """
Writes the argument to the device, similarly to write
but adds a new line at the end. The argument is expected
+302 -204
View File
File diff suppressed because it is too large Load Diff
+73 -39
View File
@@ -1,24 +1,52 @@
defrecord Kernel.CLI.Config, commands: [], output: ".",
compile: [], halt: true, compiler_options: []
defmodule Kernel.CLI do
@moduledoc false
@moduledoc """
Module responsible for controlling Elixir's CLI
"""
# Invoked directly from erlang boot process. It parses all argv
# options and execute them in the order they are specified.
def process_argv(options) do
{ config, argv } = process_options(options, Kernel.CLI.Config.new)
defrecord Config, commands: [], output: ".",
compile: [], halt: true, compiler_options: []
# This is the API invoked by Elixir boot process.
@doc false
def main(options) do
{ config, argv } = process_argv(options, Kernel.CLI.Config.new)
argv = lc arg inlist argv, do: list_to_binary(arg)
:gen_server.call(:elixir_code_server, { :argv, argv })
all_commands = Enum.reverse(config.commands)
run fn ->
Enum.map Enum.reverse(config.commands), process_command(&1, config)
:gen_server.cast(:elixir_code_server, :finished)
end, config.halt
end
@doc """
Wait until the CLI finishes procesing options.
"""
def wait_until_finished do
case :gen_server.call(:elixir_code_server, { :wait_until_finished, self }) do
:wait ->
receive do
{ :elixir_code_server, :finished } -> :ok
end
:ok -> :ok
end
end
@doc """
Runs the given function by catching any failure
and printing them to stdout. `at_exit` hooks are
also invoked before exiting.
This function is used by Elixir's CLI and also
by escripts generated by Elixir.
"""
def run(fun, halt // true) do
try do
Enum.map all_commands, process_command(&1, config)
if config.halt do
fun.()
if halt do
at_exit(0)
halt(0)
System.halt(0)
end
rescue
exception ->
@@ -26,27 +54,28 @@ defmodule Kernel.CLI do
trace = System.stacktrace
IO.puts :stderr, "** (#{inspect exception.__record__(:name)}) #{exception.message}"
IO.puts Exception.formatted_stacktrace(trace)
halt(1)
System.halt(1)
catch
:exit, reason when is_integer(reason) ->
at_exit(reason)
halt(reason)
System.halt(reason)
:exit, :normal ->
at_exit(0)
halt(0)
System.halt(0)
kind, reason ->
at_exit(1)
trace = System.stacktrace
IO.puts :stderr, "** (#{kind}) #{inspect(reason)}"
IO.puts Exception.formatted_stacktrace(trace)
halt(1)
System.halt(1)
end
end
## Private
defp at_exit(status) do
hooks = :gen_server.call(:elixir_code_server, :at_exit)
hooks = :gen_server.call(:elixir_code_server, :flush_at_exit)
lc hook inlist hooks do
try do
hook.(status)
@@ -62,11 +91,15 @@ defmodule Kernel.CLI do
IO.puts Exception.formatted_stacktrace(trace)
end
end
# If an at_exit callback adds a
# new hook we need to invoke it.
unless hooks == [], do: at_exit(status)
end
defp invalid_option(option) do
IO.puts(:stderr, "Unknown option #{list_to_binary(option)}")
halt(1)
System.halt(1)
end
defp shared_option?(list, config, callback) do
@@ -90,7 +123,7 @@ defmodule Kernel.CLI do
end
defp process_shared(['-e',h|t], config) do
process_shared t, config.prepend_commands [eval: h]
process_shared t, config.update_commands [{:eval,h}|&1]
end
defp process_shared(['-pa',h|t], config) do
@@ -106,14 +139,14 @@ defmodule Kernel.CLI do
defp process_shared(['-r',h|t], config) do
h = list_to_binary(h)
config = Enum.reduce File.wildcard(h), config, fn path, config ->
config.prepend_commands [require: path]
config.update_commands [{:require,path}|&1]
end
process_shared t, config
end
defp process_shared(['-pr',h|t], config) do
h = list_to_binary(h)
process_shared t, config.prepend_commands [parallel_require: h]
process_shared t, config.update_commands [{:parallel_require,h}|&1]
end
defp process_shared([erl,_|t], config) when erl in ['--erl', '--sname', '--remsh', '--name'] do
@@ -126,35 +159,36 @@ defmodule Kernel.CLI do
# Process init options
def process_options(['--'|t], config) do
defp process_argv(['--'|t], config) do
{ config, t }
end
def process_options(['--compile'|t], config) do
defp process_argv(['--compile'|t], config) do
process_compiler t, config
end
def process_options(['-S',h|t], config) do
defp process_argv(['-S',h|t], config) do
exec = System.find_executable(h)
if exec do
{ config.prepend_commands([require: list_to_binary(exec)]), t }
bin = list_to_binary(exec)
{ config.update_commands([{:require,bin}|&1]), t }
else
IO.puts(:stderr, "Could not find executable #{h}")
halt(1)
System.halt(1)
end
end
def process_options([h|t] = list, config) do
defp process_argv([h|t] = list, config) do
case h do
'-' ++ _ ->
shared_option? list, config, process_options(&1, &2)
shared_option? list, config, process_argv(&1, &2)
_ ->
h = list_to_binary(h)
{ config.prepend_commands([require: h]), t }
bin = list_to_binary(h)
{ config.update_commands([{:require,bin}|&1]), t }
end
end
def process_options([], config) do
defp process_argv([], config) do
{ config, [] }
end
@@ -169,15 +203,15 @@ defmodule Kernel.CLI do
end
defp process_compiler(['--no-docs'|t], config) do
process_compiler t, config.merge_compiler_options(docs: false)
process_compiler t, config.update_compiler_options([{:docs,false}|&1])
end
defp process_compiler(['--debug-info'|t], config) do
process_compiler t, config.merge_compiler_options(debug_info: true)
defp process_compiler(['--no-debug-info'|t], config) do
process_compiler t, config.update_compiler_options([{:debug_info,false}|&1])
end
defp process_compiler(['--ignore-module-conflict'|t], config) do
process_compiler t, config.merge_compiler_options(ignore_module_conflict: true)
process_compiler t, config.update_compiler_options([{:ignore_module_conflict,true}|&1])
end
defp process_compiler([h|t] = list, config) do
@@ -187,12 +221,12 @@ defmodule Kernel.CLI do
_ ->
h = list_to_binary(h)
pattern = if File.dir?(h), do: "#{h}/**/*.ex", else: h
process_compiler t, config.prepend_compile [pattern]
process_compiler t, config.update_compile [pattern|&1]
end
end
defp process_compiler([], config) do
{ config.prepend_commands([compile: config.compile]), [] }
{ config.update_commands([{:compile,config.compile}|&1]), [] }
end
# Process commands
@@ -207,7 +241,7 @@ defmodule Kernel.CLI do
defp process_command({:parallel_require, pattern}, _config) when is_binary(pattern) do
files = File.wildcard(pattern)
files = List.uniq(files)
files = Enum.uniq(files)
files = Enum.filter files, File.regular?(&1)
Kernel.ParallelRequire.files(files)
end
@@ -216,7 +250,7 @@ defmodule Kernel.CLI do
File.mkdir_p(config.output)
files = Enum.map patterns, File.wildcard(&1)
files = List.uniq(List.concat(files))
files = Enum.uniq(List.concat(files))
files = Enum.filter files, File.regular?(&1)
Code.compiler_options(config.compiler_options)
+8 -4
View File
@@ -19,7 +19,7 @@ defmodule Kernel.ParallelCompiler do
with its name can be optionally given as argument.
"""
def files(files, callback // default_callback) do
files_to_path(files, nil, callback)
spawn_compilers(files, nil, callback)
end
@doc """
@@ -27,6 +27,10 @@ defmodule Kernel.ParallelCompiler do
Read files/2 for more information.
"""
def files_to_path(files, path, callback // default_callback) when is_binary(path) do
spawn_compilers(files, path, callback)
end
defp spawn_compilers(files, path, callback) do
Code.ensure_loaded(Kernel.ErrorHandler)
spawn_compilers(files, path, callback, [], [], [])
end
@@ -42,9 +46,9 @@ defmodule Kernel.ParallelCompiler do
parent = self()
child = spawn_link fn ->
Process.put(:elixir_compiler_pid, parent)
Process.put(:elixir_ensure_compiled, true)
Process.flag(:error_handler, Kernel.ErrorHandler)
:erlang.put(:elixir_compiler_pid, parent)
:erlang.put(:elixir_ensure_compiled, true)
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
try do
if output do
+4 -5
View File
@@ -27,10 +27,9 @@ defmodule Kernel.ParallelRequire do
child = spawn_link fn ->
try do
new = Code.require_file(h)
result = (new || []) ++ result
new = Code.require_file(h)
callback.(h)
parent <- { :required, self }
parent <- { :required, self, new }
catch
kind, reason ->
parent <- { :failure, self, kind, reason, System.stacktrace }
@@ -42,8 +41,8 @@ defmodule Kernel.ParallelRequire do
defp wait_for_messages(files, waiting, callback, result) do
receive do
{ :required, child } ->
spawn_requires(files, List.delete(waiting, child), callback, result)
{ :required, child, new } ->
spawn_requires(files, List.delete(waiting, child), callback, (new || []) ++ result)
{ :failure, _child, kind, reason, stacktrace } ->
:erlang.raise(kind, reason, stacktrace)
end
+374
View File
@@ -0,0 +1,374 @@
# This is an optimization Elixir runs on function clauses.
# Whenever a variables matches against a record (a tagged
# tuple), this information is stored in order to optimize
# record calls.
defmodule Kernel.RecordRewriter do
@moduledoc false
def optimize_clause(clause) do
optimize_clause(clause, :orddict.new)
end
## Clause
defp optimize_clause({ :clause, line, args, guards, body }, dict) do
{ args, dict } = optimize_args(args, dict)
{ body, dict, res } = optimize_body(body, dict, [])
{ { :clause, line, args, guards, body }, dict, res }
end
defp optimize_args(args, dict) do
Enum.map_reduce args, dict, fn(arg, acc) ->
{ new_arg, new_acc, _res } = optimize_expr(arg, acc)
{ new_arg, new_acc }
end
end
defp optimize_body([], dict, _acc) do
{ [], dict, nil }
end
defp optimize_body([h], dict, acc) do
{ new_expr, new_dict, new_res } = optimize_expr(h, dict)
{ Enum.reverse([new_expr|acc]), new_dict, new_res }
end
defp optimize_body([h|t], dict, acc) do
{ new_expr, new_dict, _ } = optimize_expr(h, dict)
optimize_body(t, new_dict, [new_expr|acc])
end
## Record helpers
defp record_fields(record) do
if Code.ensure_loaded?(record) && function_exported?(record, :__record__, 1) do
try do
fields = lc { k, _ } inlist record.__record__(:fields), do: k
optimizable = record.__record__(:optimizable)
{ fields, optimizable }
rescue
[UndefinedFunctionError, FunctionClauseError] -> nil
end
end
end
defp record_field_info(function) do
case atom_to_list(function) do
'update_' ++ field -> { :update, list_to_atom(field) }
_ -> { :accessor, function }
end
end
defp optimize_call(line, { record, _ } = res, left, { :atom, _, function }, args) do
case record_fields(record) do
{ fields, optimizable } ->
if List.member?(optimizable, { function, length(args) + 1 }) do
{ kind, field } = record_field_info(function)
if index = Enum.find_index(fields, field == &1) do
optimize_call(line, res, kind, field, index, left, args)
end
end
nil -> nil
end
end
defp optimize_call(_line, _res, _left, _right, _args) do
nil
end
defp optimize_call(line, _res, :accessor, _field, index, left, []) do
call = { :call, line,
{ :remote, line, { :atom, 0, :erlang }, { :atom, 0, :element } },
[{ :integer, 0, index + 2 }, left]
}
{ call, nil }
end
defp optimize_call(line, res, :accessor, _field, index, left, [arg]) do
call = { :call, line,
{ :remote, line, { :atom, 0, :erlang }, { :atom, 0, :setelement } },
[{ :integer, 0, index + 2 }, left, arg]
}
{ call, res }
end
defp optimize_call(line, res, :update, field, _index, left, args) do
call = { :call, line,
{ :remote, line, left, { :atom, 0, :"update_#{field}" } },
args
}
{ call, res }
end
## Expr
defp optimize_expr({ :call, call_line, { :remote, line, left, right }, args }, dict) do
{ left, dict, res } = optimize_expr(left, dict)
{ right, dict, _ } = optimize_expr(right, dict)
{ args, dict } = optimize_args(args, dict)
case optimize_call(call_line, res, left, right, args) do
{ call, call_res } ->
{ call, dict, call_res }
nil ->
{ { :call, call_line, { :remote, line, left, right }, args }, dict, nil }
end
end
defp optimize_expr({ :call, line, expr, args }, dict) do
{ expr, dict, _ } = optimize_expr(expr, dict)
{ args, dict } = optimize_args(args, dict)
{ { :call, line, expr, args }, dict, nil }
end
defp optimize_expr({ :match, line, left, right }, dict) do
{ left, dict, left_res } = optimize_expr(left, dict)
{ right, dict, right_res } = optimize_expr(right, dict)
match = { :match, line, left, right }
if left_res do
dict = assign_vars(extract_vars(right, []), dict, left_res)
end
if right_res do
dict = assign_vars(extract_vars(left, []), dict, right_res)
end
{ match, dict, right_res || left_res }
end
defp optimize_expr({ :op, line, op, left, right }, dict) do
{ left, dict, _ } = optimize_expr(left, dict)
{ right, dict, _ } = optimize_expr(right, dict)
{ { :op, line, op, left, right }, dict, nil }
end
defp optimize_expr({ :op, line, op, expr }, dict) do
{ expr, dict, _ } = optimize_expr(expr, dict)
{ { :op, line, op, expr }, dict, nil }
end
defp optimize_expr({ :bin, line, elements }, dict) do
{ elements, dict } = optimize_args(elements, dict)
{ { :bin, line, elements }, dict, nil }
end
defp optimize_expr({ :bin_element, line, expr, type1, type2 }, dict) do
{ expr, dict, _ } = optimize_expr(expr, dict)
{ { :bin_element, line, expr, type1, type2 }, dict, nil }
end
defp optimize_expr({ :cons, line, left, right }, dict) do
{ left, dict, _ } = optimize_expr(left, dict)
{ right, dict, _ } = optimize_expr(right, dict)
{ { :cons, line, left, right }, dict, nil }
end
defp optimize_expr({ :block, line, args }, dict) do
{ args, dict, res } = optimize_body(args, dict, [])
{ { :block, line, args }, dict, res }
end
defp optimize_expr({ :tuple, line, args }, dict) do
{ args, dict, args_res } = optimize_tuple_args(args, dict)
args_res = if Enum.any?(args_res), do: args_res, else: nil
res =
case args do
[{ :atom, _, atom }|t] -> atom
_ -> nil
end
{ { :tuple, line, args }, dict, { res, args_res } }
end
defp optimize_expr({ :var, _, name } = var, dict) do
case :orddict.find(name, dict) do
{ :ok, res } -> { var, dict, res }
:error -> { var, dict, nil }
end
end
defp optimize_expr({ :case, line, expr, clauses }, dict) do
{ expr, dict, _ } = optimize_expr(expr, dict)
tuples = lc clause inlist clauses, do: optimize_clause(clause, dict)
clauses = lc { clause, _, _ } inlist tuples, do: clause
dict = join_dict(tuples)
res = join_result(tuples)
{ { :case, line, expr, clauses }, dict, res }
end
defp optimize_expr({ :receive, line, clauses }, dict) do
tuples = lc clause inlist clauses, do: optimize_clause(clause, dict)
clauses = lc { clause, _, _ } inlist tuples, do: clause
dict = join_dict(tuples)
res = join_result(tuples)
{ { :receive, line, clauses }, dict, res }
end
defp optimize_expr({ :receive, line, clauses, after_key, after_value }, dict) do
tuples = lc clause inlist clauses, do: optimize_clause(clause, dict)
clauses = lc { clause, _, _ } inlist tuples, do: clause
{ after_key, dict, _ } = optimize_expr(after_key, dict)
{ after_value, dict, res } = optimize_body(after_value, dict, [])
dict = join_dict(tuples, dict)
res = join_result(tuples, res)
{ { :receive, line, clauses, after_key, after_value }, dict, res }
end
defp optimize_expr({ :try, line, body, [], clauses, try_after }, dict) do
tuples = lc clause inlist clauses, do: optimize_clause(clause, dict)
clauses = lc { clause, _, _ } inlist tuples, do: clause
{ body, _, res } = optimize_body(body, dict, [])
res = join_result(tuples, res)
{ try_after, _, _ } = optimize_body(try_after, dict, [])
{ { :try, line, body, [], clauses, try_after }, dict, res }
end
defp optimize_expr({ :fun, line, { :function, module, name, arity } }, dict) do
{ module, dict, _ } = optimize_expr(module, dict)
{ name, dict, _ } = optimize_expr(name, dict)
{ arity, dict, _ } = optimize_expr(arity, dict)
{ { :fun, line, { :function, module, name, arity } }, dict, nil }
end
defp optimize_expr({ :fun, line, { :clauses, clauses } }, dict) do
clauses = lc clause inlist clauses do
{ clause, _, _ } = optimize_clause(clause, dict)
clause
end
{ { :fun, line, { :clauses, clauses } }, dict, nil }
end
defp optimize_expr({ comprehension, line, expr, args }, dict) when comprehension in [:lc, :bc] do
{ args, new_dict } = optimize_args(args, dict)
{ expr, _, _ } = optimize_expr(expr, new_dict)
{ { comprehension, line, expr, args }, dict, nil }
end
defp optimize_expr({ generate, line, left, right }, dict) when generate in [:generate, :b_generate] do
{ left, dict, _ } = optimize_expr(left, dict)
{ right, dict, _ } = optimize_expr(right, dict)
{ { generate, line, left, right }, dict, nil }
end
defp optimize_expr(other, dict) when elem(other, 0) in [:string, :atom, :integer, :float, :nil, :fun] do
{ other, dict, nil }
end
## Helpers
defp optimize_tuple_args(args, dict) do
{ final_args, { final_dict, final_acc } } =
Enum.map_reduce args, { dict, [] }, fn(arg, { acc_dict, acc_res }) ->
{ new_arg, new_acc, res } = optimize_expr(arg, acc_dict)
{ new_arg, { new_acc, [res|acc_res] } }
end
{ final_args, final_dict, Enum.reverse(final_acc) }
end
defp assign_vars([key|t], dict, { _, value } = res) when is_list(key) and is_list(value) and length(key) == length(value) do
assign_vars t, assign_nested_vars(key, dict, value), res
end
defp assign_vars([key|t], dict, { value, _ } = res) when is_atom(key) and value != nil do
dict =
case :orddict.find(key, dict) do
{ :ok, ^res } ->
dict
{ :ok, { ^value, _ } } ->
:orddict.store(key, { value, nil }, dict)
{ :ok, _ } ->
# We are overriding a type of an existing variable,
# which means the source code is invalid.
:orddict.store(key, nil, dict)
:error ->
:orddict.store(key, res, dict)
end
assign_vars t, dict, res
end
defp assign_vars([_|t], dict, res) do
assign_vars t, dict, res
end
defp assign_vars([], dict, _res) do
dict
end
defp assign_nested_vars([vars|vt], dict, [res|rt]) do
assign_nested_vars(vt, assign_vars(vars, dict, res), rt)
end
defp assign_nested_vars([], dict, []) do
dict
end
defp extract_vars({ :match, _, left, right }, vars) do
vars = extract_vars(right, vars)
extract_vars(left, vars)
end
defp extract_vars({ :var, _, name }, vars) do
[name|vars]
end
defp extract_vars({ :tuple, _, args }, vars) do
[Enum.map(args, extract_vars(&1, []))|vars]
end
defp extract_vars(_, vars) do
vars
end
defp join_dict([{ _, dict, _ }|t]) do
join_dict(t, dict)
end
defp join_dict([{ _, dict, _ }|t], other) do
other = Enum.reduce other, other, fn
{ key, { value, _ } = res }, acc ->
case :orddict.find(key, dict) do
{ :ok, ^res } -> acc
{ :ok, { ^value, _ } } -> :orddict.store(key, { value, nil }, acc)
{ :ok, _ } -> :orddict.store(key, nil, acc)
:error -> :orddict.erase(key, acc)
end
end
join_dict(t, other)
end
defp join_dict([], other) do
other
end
defp join_result([{ _, _, res }|t]) do
join_result(t, res)
end
defp join_result([{ _, _, res }|t], res) do
join_result(t, res)
end
defp join_result([{ _, _, { res, _ } }|t], { res, _ }) do
join_result(t, { res, nil })
end
defp join_result([{ _, _, _ }|_], _res) do
nil
end
defp join_result([], res) do
res
end
end
+34 -6
View File
@@ -56,7 +56,7 @@ defmodule Kernel.SpecialForms do
alias MyKeyword, as: Keyword
end
In the example above, we have set up `MyOrdict` to be alias
In the example above, we have set up `MyKeyword` to be alias
as `Keyword`. So now, any reference to `Keyword` will be
automatically replaced by `MyKeyword`.
@@ -139,8 +139,8 @@ defmodule Kernel.SpecialForms do
import :all, List
It can also be customized to import only functions or only
macros:
It can also be customized to import only all functions or
all macros:
import :functions, List
import :macros, List
@@ -276,7 +276,7 @@ defmodule Kernel.SpecialForms do
a = 10
NoHygiene.interference
a #=> 11
a #=> 1
Notice that aliases are not hygienic in Elixir, ambiguity
must be solved by prepending Elixir:
@@ -440,7 +440,7 @@ defmodule Kernel.SpecialForms do
it belonged to another file.
quote location: :keep, do: 1
#=> { :__scope__, 1,[[file: "iex"], 1] }
#=> { :__scope__, 1,[[file: "iex"],[do: 1]] }
Check `quote/1` for more information.
"""
@@ -448,11 +448,39 @@ defmodule Kernel.SpecialForms do
@doc """
This is the special form used to hold aliases information.
At compilation time, it is usually compiled to an atom:
It is usually compiled to an atom:
quote do: Foo.Bar
{ :__aliases__, 0, [:Foo,:Bar] }
Elixir represents `Foo.Bar` as `__aliases__` so calls can be
unambiguously identified by the operator `:.`. For example:
quote do: Foo.bar
{{:.,0,[{:__aliases__,0,[:Foo]},:bar]},0,[]}
Whenever an expression iterator sees a `:.` as the tuple key,
it can be sure that it represents a call and the second element
of the arguments list is an atom.
On the other hand, aliases holds some properties:
1) The head element of aliases can be any term;
2) The tail elements of aliases are guaranteed to always be atoms;
3) When the head element of aliases is the atom :Elixir, no expansion happen;
4) When the head element of aliases is not an atom, it is expanded at runtime:
quote do: some_var.Foo
{:__aliases__,0,[{:some_var,0,:quoted},:Bar]}
Since `some_var` is not available at compilation time, the compiler
expands such expression to:
Module.concat [some_var, Foo]
"""
defmacro __aliases__(args)
end
+540 -103
View File
@@ -1,54 +1,518 @@
defmodule Kernel.Typespec do
@moduledoc """
This is the module that converts Elixir typespecs
to Erlang typespecs syntax. Everytime @spec, @type
and @typep are used they proxy to the functions
in this module.
Holds macros and functions for working with typespecs.
The attributes `@type`, `@opaque`, `@typep`, `@spec` and
`@callback` available in modules are handled by the equivalent
macros defined by this module.
## Defining a type
@type type_name :: type
@typep type_name :: type
@opaque type_name :: type
For more details, see documentation for deftype, deftypep and defopaque in
Kernel.Typespec
## Defining a specification
@spec function_name(type, type) :: type
@callback function_name(type, type) :: type
For more details, see documentation for defspec and defcallback in
Kernel.Typespec
## Types
The type syntax provided by Elixir is fairly similar to the one
in Erlang.
Most of the built-in types provided in Erlang (for example, `pid()`)
are expressed the same way: `pid()` or simply `pid`. Parametrized types
are also supported: `list(integer())` and so are remote types: `Enum.t`.
Certain data type shortcuts ([...], <<>> and {...}) are supported as well.
Main differences lie in how bit strings and functions are defined:
### Bit Strings
Bit string with a base size of 3:
<<_ :: 3>>
Bit string with a unit size of 8:
<<_ :: _ * 8>>
### Functions
Any function:
(fun(...) -> any)
or
((...) -> any)
or
(... -> any)
Function with arity of zero:
(fun() -> type)
or
(() -> type)
Function with some arity:
(fun(type, type) -> type)
or
((type, type) -> type)
or
(type, type -> type)
## Notes
Elixir discourages the use of type `string()` as it might be confused
with binaries which are referred to as "strings" in Elixir (as opposed to
character lists). In order to use the type that is called `string()` in Erlang,
one has to use the `char_list()` type which is a synonym to `string()`. If yu
use `string()`, you'll get a warning from the compiler.
If you want to refer to the "string" type (the one operated by functions in the
String module), use `String.t()` type instead.
See http://www.erlang.org/doc/reference_manual/typespec.html
for more information.
"""
defmacro deftype(name, options // []) do
_deftype(name, true, __CALLER__, options)
end
@doc """
Defines a type.
This macro is the one responsible to handle the attribute @type.
defmacro deftypep(name) do
_deftype(name, false, __CALLER__, [])
end
## Examples
defmacro defspec(spec, block) do
_defspec(:spec, __CALLER__, spec, block)
end
@type my_type :: atom
defmacro defcallback(spec, block) do
_defspec(:callback, __CALLER__, spec, block)
"""
defmacro deftype(type) do
quote do
Kernel.Typespec.deftype(:type, (quote line: :keep, do: unquote(type)), __ENV__)
end
end
@doc """
Get the types defined for the given module. This function
is only available for modules being compiled. If the module
was already compiled, you need to loop its attributes
to get such information.
Defines an opaque type.
This macro is the one responsible to handle the attribute @opaque.
## Examples
@opaque my_type :: atom
"""
def get_types(module) do
Module.get_attribute(module, :type) ++ Module.get_attribute(module, :opaque)
defmacro defopaque(type) do
quote do
Kernel.Typespec.deftype(:opaque, (quote line: :keep, do: unquote(type)), __ENV__)
end
end
@doc """
Get the specs defined for the given module. This function
is only available for modules being compiled. If the module
was already compiled, you need to loop its attributes
to get such information.
Defines a private type.
This macro is the one responsible to handle the attribute @typep.
## Examples
@typep my_type :: atom
"""
def get_specs(module) do
specs = :ets.tab2list(spec_table_for(module))
keys = :lists.ukeysort(1, specs)
lc { k, _ } inlist keys, do: { k, :proplists.append_values(k, specs) }
defmacro deftypep(type) do
quote do
Kernel.Typespec.deftype(:typep, (quote line: :keep, do: unquote(type)), __ENV__)
end
end
## Typespec conversion
@doc false
defmacro defspec(spec, [do: block]) do
IO.write "[WARNING] @spec f(...), do: type is deprecated, use @spec f(...) :: type\n#{Exception.env_stacktrace(__CALLER__)}"
quote do
Kernel.Typespec.defspec(:spec, (quote line: :keep, do: unquote(spec) :: unquote(block)), __ENV__)
end
end
@doc """
Defines a spec.
This macro is the one responsible to handle the attribute @spec.
## Examples
@spec add(number, number) :: number
"""
defmacro defspec(spec) do
quote do
Kernel.Typespec.defspec(:spec, (quote line: :keep, do: unquote spec), __ENV__)
end
end
@doc false
defmacro defcallback(spec, [do: block]) do
IO.write "[WARNING] @callback f(...), do: type is deprecated, use @callback f(...) :: type\n#{Exception.env_stacktrace(__CALLER__)}"
quote do
Kernel.Typespec.defspec(:callback, (quote line: :keep, do: unquote(spec) :: unquote(block)), __ENV__)
end
end
@doc """
Defines a callback.
This macro is the one responsible to handle the attribute @callback.
## Examples
@callback add(number, number) :: number
"""
defmacro defcallback(spec) do
quote do
Kernel.Typespec.defspec(:callback, (quote line: :keep, do: unquote(spec)), __ENV__)
end
end
## Helpers
@doc """
Defines a `type`, `typep` or `opaque` by receiving Erlang's typespec.
"""
def define_type(module, kind, { name, _, vars } = type) when kind in [:type, :typep, :opaque] do
{ kind, export } =
case kind do
:type -> { :type, true }
:typep -> { :type, false }
:opaque -> { :opaque, true }
end
Module.compile_typespec module, kind, type
if export, do:
Module.compile_typespec module, :export_type, [{ name, length(vars) }]
type
end
@doc """
Defines a `spec` by receiving Erlang's typespec.
"""
def define_spec(module, tuple, definition) do
Module.compile_typespec module, :spec, { tuple, definition }
end
@doc """
Defines a `callback` by receiving Erlang's typespec.
"""
def define_callback(module, tuple, definition) do
Module.compile_typespec module, :callback, { tuple, definition }
end
@doc """
Returns true if the current module defines a given type
(private, opaque or not). This function is only available
for modules being compiled.
"""
def defines_type?(module, name, arity) do
finder = match?({ ^name, _, vars } when length(vars) == arity, &1)
:lists.any(finder, Module.get_attribute(module, :type)) or
:lists.any(finder, Module.get_attribute(module, :opaque))
end
@doc """
Returns true if the current module defines a given spec.
This function is only available for modules being compiled.
"""
def defines_spec?(module, name, arity) do
tuple = { name, arity }
:lists.any(match?(^tuple, &1), Module.get_attribute(module, :spec))
end
@doc """
Returns true if the current module defines a callback.
This function is only available for modules being compiled.
"""
def defines_callback?(module, name, arity) do
tuple = { name, arity }
:lists.any(match?(^tuple, &1), Module.get_attribute(module, :callback))
end
@doc """
Converts a spec clause back to Elixir AST.
"""
def spec_to_ast(name, { :type, line, :fun, [{:type, _, :product, args},result] }) do
args = lc arg inlist args, do: typespec_to_ast(arg)
{ :::, line, [{ name, line, args }, typespec_to_ast(result)] }
end
def spec_to_ast(name, { :type, line, :fun, [] }) do
{ :::, line, [{ name, line, [] }, quote(do: term)] }
end
def spec_to_ast(name, { :type, line, :bounded_fun, [{ :type, _, :fun, [{ :type, _, :product, args }, result] }, constraints] }) do
[h|t] =
lc {:type, line, :constraint, [{:atom, _, :is_subtype}, [var, type]]} inlist constraints do
{ :is_subtype, line, [typespec_to_ast(var), typespec_to_ast(type)] }
end
args = lc arg inlist args, do: typespec_to_ast(arg)
guards = Enum.reduce t, h, fn(x, acc) -> { :and, line, [acc, x] } end
{ :::, line, [{ :when, line, [{ name, line, args }, guards] }, typespec_to_ast(result)] }
end
@doc """
Converts a type clause back to Elixir AST.
"""
def type_to_ast({ { :record, record }, fields, args }) when is_atom(record) do
fields = lc field inlist fields, do: typespec_to_ast(field)
args = lc arg inlist args, do: typespec_to_ast(arg)
type = { :{}, 0, [record|fields] }
quote do: unquote(record)(unquote_splicing(args)) :: unquote(type)
end
def type_to_ast({ name, type, args }) do
args = lc arg inlist args, do: typespec_to_ast(arg)
quote do: unquote(name)(unquote_splicing(args)) :: unquote(typespec_to_ast(type))
end
@doc """
Returns all types available from the beam.
It is returned as a list of tuples where the first
element is the type (`:typep`, `:type` and `:opaque`).
The module has to have a corresponding beam file
on the file system.
"""
def beam_types(module) do
case abstract_code(module) do
{ :ok, abstract_code } ->
exported_types = lc { :attribute, _, :export_type, types } inlist abstract_code, do: types
exported_types = List.flatten(exported_types)
lc { :attribute, _, kind, { name, _, args } = type } inlist abstract_code, kind in [:opaque, :type] do
cond do
kind == :opaque -> { :opaque, type }
List.member?(exported_types, { name, length(args) }) -> { :type, type }
true -> { :typep, type }
end
end
_ ->
[]
end
end
@doc """
Returns all specs available from the beam.
It is returned as a list of tuples where the first
element is spec name and arity and the second is the spec.
The module has to have a corresponding beam file
on the file system.
"""
def beam_specs(module) do
from_abstract_code(module, :spec)
end
@doc """
Returns all callbacks available from the beam.
It is returned as a list of tuples where the first
element is spec name and arity and the second is the spec.
The module has to have a corresponding beam file
on the file system.
"""
def beam_callbacks(module) do
from_abstract_code(module, :callback)
end
defp from_abstract_code(module, kind) do
case abstract_code(module) do
{ :ok, abstract_code } ->
lc { :attribute, _, abs_kind, value } inlist abstract_code, kind == abs_kind, do: value
_ ->
[]
end
end
defp abstract_code(module) do
case :beam_lib.chunks(abstract_code_beam(module), [:abstract_code]) do
{:ok, { _, [{ :abstract_code, { raw_abstract_v1, abstract_code } }] } } ->
{ :ok, abstract_code }
_ ->
[]
end
end
defp abstract_code_beam(module) when is_atom(module) do
case :code.which(module) do
:non_existing -> module
file -> file
end
end
defp abstract_code_beam(binary) when is_binary(binary) do
binary
end
## Macro callbacks
@doc false
def deftype(kind, { :::, _, [type, definition] }, caller) do
do_deftype(kind, type, definition, caller)
end
def deftype(kind, type, caller) do
do_deftype(kind, type, { :term, caller.line, nil }, caller)
end
defp do_deftype(kind, { name, _, args }, definition, caller) do
args =
if is_atom(args) do
[]
else
lc(arg inlist args, do: variable(arg))
end
vars = lc { :var, _, var } inlist args, do: var
spec = typespec(definition, vars, caller)
vars = lc { :var, _, _ } = var inlist args, do: var
type = { name, spec, vars }
define_type(caller.module, kind, type)
end
@doc false
def defspec(type, {:::, _, [{ :when, _, [{ name, line, args }, constraints_guard] }, return] }, caller) do
if is_atom(args), do: args = []
constraints = guard_to_constraints(constraints_guard, caller)
spec = { :type, line, :fun, fn_args(line, args, return, Keyword.keys(constraints), caller) }
spec = { :type, line, :bounded_fun, [spec, Keyword.values(constraints)] }
code = { { name, Kernel.length(args) }, spec }
Module.compile_typespec(caller.module, type, code)
code
end
def defspec(type, {:::, _, [{ name, line, args }, return]}, caller) do
if is_atom(args), do: args = []
spec = { :type, line, :fun, fn_args(line, args, return, [], caller) }
code = { { name, Kernel.length(args) }, spec }
Module.compile_typespec(caller.module, type, code)
code
end
defp guard_to_constraints({ :is_subtype, line, [{ name, _, _ }, type] }, caller) do
contraints = [{ :atom, line, :is_subtype }, [{:var, line, name}, typespec(type, [], caller)]]
[{ name, { :type, line, :constraint, contraints } }]
end
defp guard_to_constraints({ :and, _, [left, right] }, caller) do
guard_to_constraints(left, caller) ++ guard_to_constraints(right, caller)
end
## To AST conversion
defp typespec_to_ast({ :type, line, :tuple, :any }) do
typespec_to_ast({:type, line, :tuple, []})
end
defp typespec_to_ast({ :type, line, :tuple, args }) do
args = lc arg inlist args, do: typespec_to_ast(arg)
{ :{}, line, args }
end
defp typespec_to_ast({ :type, line, :binary, [arg1, arg2] }) do
[arg1, arg2] = lc arg inlist [arg1, arg2], do: typespec_to_ast(arg)
cond do
arg2 == 0 ->
quote line: line, do: <<_ :: unquote(arg1)>>
arg1 == 0 ->
quote line: line, do: <<_ :: _ * unquote(arg2)>>
true ->
quote line: line, do: <<_ :: unquote(arg1) * unquote(arg2)>>
end
end
defp typespec_to_ast({ :type, line, :union, args }) do
args = lc arg inlist args, do: typespec_to_ast(arg)
Enum.reduce tl(args), hd(args),
fn(arg, expr) -> { :|, line, [expr, arg] } end
end
defp typespec_to_ast({ :type, line, :fun, [{:type, _, :product, args},result] }) do
args = lc arg inlist args, do: typespec_to_ast(arg)
{ :"->", line, [{args, typespec_to_ast(result)}] }
end
defp typespec_to_ast({ :type, line, :fun, [args, result] }) do
{ :"->", line, [{[typespec_to_ast(args)], typespec_to_ast(result)}] }
end
defp typespec_to_ast({ :type, line, :fun, [] }) do
typespec_to_ast({ :type, line, :fun, [{:type, line, :any}, {:type,line,:any, []} ] })
end
defp typespec_to_ast({ :type, line, name, args }) do
args = lc arg inlist args, do: typespec_to_ast(arg)
{ name, line, args }
end
defp typespec_to_ast({ :var, line, var }) do
var =
case atom_to_binary(var) do
<<"_", c :: [binary, size(1)], rest :: binary>> ->
binary_to_atom("_#{String.downcase(c)}#{rest}")
<<c :: [binary, size(1)], rest :: binary>> ->
binary_to_atom("#{String.downcase(c)}#{rest}")
end
{ var, line, nil }
end
# special shortcut(s)
defp typespec_to_ast({ :remote_type, line, [{:atom, _, :elixir}, {:atom, _, :char_list}, []] }) do
typespec_to_ast({:type, line, :char_list, []})
end
defp typespec_to_ast({ :remote_type, line, [mod, name, args] }) do
args = lc arg inlist args, do: typespec_to_ast(arg)
dot = { :., line, [typespec_to_ast(mod), typespec_to_ast(name)] }
{ dot, line, args }
end
defp typespec_to_ast({ :ann_type, line, [var, type] }) do
{ :::, line, [typespec_to_ast(var), typespec_to_ast(type)] }
end
defp typespec_to_ast({ :typed_record_field,
{ :record_field, line, { :atom, line1, name }},
type }) do
typespec_to_ast({ :ann_type, line, [{ :var, line1, name }, type] })
end
defp typespec_to_ast({:type, _, :any}) do
quote do: ...
end
defp typespec_to_ast({:paren_type, _, [type]}) do
typespec_to_ast(type)
end
defp typespec_to_ast({ t, _line, atom }) when is_atom(t) do
atom
end
defp typespec_to_ast(other), do: other
## From AST conversion
# Handle unions
defp typespec({ :|, line, [_,_] } = exprs, vars, caller) do
exprs = Enum.reverse(collect_union(exprs))
exprs = :lists.reverse(collect_union(exprs))
union = lc e inlist exprs, do: typespec(e, vars, caller)
{ :type, line, :union, union }
end
@@ -58,11 +522,11 @@ defmodule Kernel.Typespec do
{:type, line, :binary, [{:integer, line, 0}, {:integer, line, 0}]}
end
defp typespec({:<<>>, line, [{:|, _, [{:_, line1, atom}, {:*, _, [{:_, line2, atom}, unit]}]}]}, _, _) when is_atom(atom) do
defp typespec({:<<>>, line, [{:::, _, [{:_, line1, atom}, {:*, _, [{:_, line2, atom}, unit]}]}]}, _, _) when is_atom(atom) do
{:type, line, :binary, [{:integer, line1, 0}, {:integer, line2, unit}]}
end
defp typespec({:<<>>, line, [{:|, line1, [{:_, line2, atom}, base]}]}, _, _) when is_atom(atom) do
defp typespec({:<<>>, line, [{:::, line1, [{:_, line2, atom}, base]}]}, _, _) when is_atom(atom) do
{:type, line, :binary, [{:integer, line1, base}, {:integer, line2, 0}]}
end
@@ -71,12 +535,29 @@ defmodule Kernel.Typespec do
typespec({:range, line, args}, vars, caller)
end
# Handle aliases
# Handle special forms
defp typespec({:__MODULE__, _, atom}, vars, caller) when is_atom(atom) do
typespec(caller.module, vars, caller)
end
defp typespec({:__aliases__, _, _} = alias, vars, caller) do
atom = Macro.expand alias, caller
typespec(atom, vars, caller)
end
# Handle funs
defp typespec({:fun, line, args}, vars, caller) when is_list(args) do
IO.write "[WARNING] fun() type is deprecated, use (... -> type) instead\n#{Exception.env_stacktrace(caller)}"
typespec({:"->", line, [{args, quote do: any}]}, vars, caller)
end
defp typespec({:"->", line, [{[{:fun, _, arguments}], return}]}, vars, caller) when is_list(arguments) do
typespec({:"->", line, [{arguments, return}]}, vars, caller)
end
defp typespec({:"->", line, [{arguments, return}]}, vars, caller) when is_list(arguments) do
args = fn_args(line, arguments, return, vars, caller)
{ :type, line, :fun, args }
end
# Handle type operator
defp typespec({:"::", line, [var, expr] }, vars, caller) do
left = typespec(var, [elem(var, 0)|vars], caller)
@@ -89,10 +570,17 @@ defmodule Kernel.Typespec do
{ :op, line, op, {:integer, line, integer} }
end
# Handle access macro
defp typespec({{:., line, [Kernel, :access]}, line1, [target, args]}, vars, caller) do
access = {{:., line, [Kernel, :access]}, line1,
[target, args ++ [_: (quote hygiene: false, do: any)]]}
typespec(Macro.expand(access, caller), vars, caller)
end
# Handle remote calls
defp typespec({{:., line, [remote, name]}, _, args}, vars, caller) do
remote = Macro.expand remote, caller
unless is_atom(remote), do: raise(ArgumentError, message: "Invalid remote in typespec")
unless is_atom(remote), do: raise ArgumentError, message: "invalid remote in typespec"
remote_type({typespec(remote, vars, caller), line, typespec(name, vars, caller), args}, vars, caller)
end
@@ -110,28 +598,27 @@ defmodule Kernel.Typespec do
{ :type, line, :tuple, args }
end
# Handle funs
defp typespec({:fun, line, arguments}, vars, caller) when is_list(arguments) do
args =
case Enum.reverse(arguments) do
[[{:do,h}]|t] -> fn_args(line, Enum.reverse(t), h, vars, caller)
[] -> []
_ -> [fn_args(line, arguments, vars, caller)]
end
{ :type, line, :fun, args }
end
# Handle variables or local calls
defp typespec({name, line, atom}, vars, caller) when is_atom(atom) do
if List.member?(vars, name) do
{ :var, line, name }
{ :var, line, name }
else
typespec({name, line, []}, vars, caller)
end
end
# Handle local calls
defp typespec({:string, line, arguments}, vars, caller) do
IO.write "warning: string() type use is discouraged. For character lists, use " <>
"char_list() type, for strings, String.t()\n#{Exception.env_stacktrace(caller)}"
arguments = lc arg inlist arguments, do: typespec(arg, vars, caller)
{ :type, line, :string, arguments }
end
defp typespec({:char_list, _line, arguments}, vars, caller) do
typespec((quote do: :elixir.char_list(unquote_splicing(arguments))), vars, caller)
end
defp typespec({name, line, arguments}, vars, caller) do
arguments = lc arg inlist arguments, do: typespec(arg, vars, caller)
{ :type, line, name, arguments }
@@ -155,7 +642,7 @@ defmodule Kernel.Typespec do
end
defp typespec(l, _, _) when is_list(l) do
raise(ArgumentError, message: "Unexpected list #{inspect l}")
raise ArgumentError, message: "Unexpected list #{inspect l}"
end
defp typespec(t, vars, caller) when is_tuple(t) do
@@ -174,7 +661,10 @@ defmodule Kernel.Typespec do
defp collect_union(v), do: [v]
defp fn_args(line, args, return, vars, caller) do
[fn_args(line, args, vars, caller), typespec(return, vars, caller)]
case [fn_args(line, args, vars, caller), typespec(return, vars, caller)] do
[{:type,_,:any},{:type,_,:any,[]}] -> []
x -> x
end
end
defp fn_args(line, [{:"...", _, _}], _vars, _caller) do
@@ -186,59 +676,6 @@ defmodule Kernel.Typespec do
{ :type, line, :product, args }
end
def _deftype({:"::", _, [name, definition]}, export, caller, opts) do
_deftype(name, definition, export, caller, opts)
end
def _deftype(name, export, caller, opts) do
_deftype(name, { :term, caller.line, nil }, export, caller, opts)
end
defp _deftype({name, _, args}, definition, export, caller, options) do
args = if is_atom(args), do: [], else: lc(arg inlist args, do: variable(arg))
vars = lc {:var, _, var} inlist args, do: var
spec = typespec(definition, vars, caller)
vars = lc ({:var, _, _} = var) inlist args, do: var
attr = if options[:opaque], do: :opaque, else: :type
export = if export do
quote do: Module.compile_type(__MODULE__, :export_type, [{name, length(vars)}])
else
nil
end
quote do
name = unquote(name)
spec = unquote(Macro.escape(spec))
vars = unquote(Macro.escape(vars))
type = { name, spec, vars }
Module.compile_type __MODULE__, unquote(attr), type
unquote(export)
{ unquote(attr), type }
end
end
defp _defspec(type, caller, {name, line, args},[{:do,return}]) do
if is_atom(args), do: args = []
spec = { :type, line, :fun, fn_args(line, args, return, [], caller) }
code = Macro.escape { {type, { name, length(args) }}, [spec] }
table = spec_table_for(caller.module)
quote do
code = unquote(code)
:ets.insert(unquote(table), code)
code
end
end
defp spec_table_for(module) do
table = list_to_atom :lists.concat([:s, module])
unless table == :ets.info(table, :name), do:
raise(ArgumentError, message: "cannot manage specs for #{inspect module} because it was already compiled")
table
end
defp variable({name, line, _}) do
{:var, line, name}
end
+47 -17
View File
@@ -13,11 +13,16 @@ defmodule Keyword do
entries for a given key are removed when invoked.
"""
@type key :: atom
@type value :: any
@type t :: [{key, value}]
@doc """
Creates a Keyword from enum. Differently from `Keyword.new`
which behaves as a dict, `Keyword.from_enum` do not remove
duplicated entries.
"""
@spec from_enum(Enum.t) :: t
def from_enum(enum) when is_list(enum) do
enum
end
@@ -26,9 +31,24 @@ defmodule Keyword do
Enum.map(enum, fn(x) -> x end)
end
@doc """
Checks if the given argument is a keywords list or not
"""
@spec keyword?(term) :: boolean
def keyword?([{ key, _value } | rest]) when is_atom(key) do
case atom_to_list(key) do
'Elixir-' ++ _ -> false
_ -> keyword?(rest)
end
end
def keyword?([]), do: true
def keyword?(_other), do: false
@doc """
Returns an empty keyword list, i.e. an empty list.
"""
@spec new :: t
def new do
[]
end
@@ -43,6 +63,7 @@ defmodule Keyword do
#=> [a: 2, b: 1]
"""
@spec new(Enum.t) :: t
def new(pairs) do
Enum.reduce pairs, [], fn { k, v }, keywords ->
put(keywords, k, v)
@@ -60,6 +81,7 @@ defmodule Keyword do
#=> [a: :a, b: :b]
"""
@spec new(Enum.t, ({key, value} -> {key, value})) :: t
def new(pairs, transform) do
Enum.reduce pairs, [], fn i, keywords ->
{ k, v } = transform.(i)
@@ -83,7 +105,9 @@ defmodule Keyword do
Keyword.get [a: 1], :b, 3 #=> 3
"""
def get(keywords, key, default // nil) do
@spec get(t, key) :: value
@spec get(t, key, value) :: value
def get(keywords, key, default // nil) when is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
{ ^key, value } -> value
false -> default
@@ -100,10 +124,11 @@ defmodule Keyword do
Keyword.get! [a: 1], :b #=> raises KeyError[key: :b]
"""
def get!(keywords, key) do
@spec get!(t, key) :: value | no_return
def get!(keywords, key) when is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
{ ^key, value } -> value
false -> raise(Keyword.KeyError, key: key)
false -> raise(KeyError, key: key)
end
end
@@ -116,7 +141,8 @@ defmodule Keyword do
#=> [1,2]
"""
def get_values(keywords, key) do
@spec get_values(t, key) :: [value]
def get_values(keywords, key) when is_atom(key) do
lc { k, v } inlist keywords, key == k, do: v
end
@@ -129,6 +155,7 @@ defmodule Keyword do
Keyword.keys [a: 1, b: 2] #=> [:a,:b]
"""
@spec keys(t) :: [key]
def keys(keywords) do
lc { key, _ } inlist keywords, do: key
end
@@ -141,6 +168,7 @@ defmodule Keyword do
Keyword.values [a: 1, b: 2] #=> [1,2]
"""
@spec values(t) :: [value]
def values(keywords) do
lc { _, value } inlist keywords, do: value
end
@@ -157,7 +185,8 @@ defmodule Keyword do
Keyword.delete [b: 2], :a #=> [b: 2]
"""
def delete(keywords, key) do
@spec delete(t, key) :: t
def delete(keywords, key) when is_atom(key) do
lc { k, _ } = tuple inlist keywords, key != k, do: tuple
end
@@ -173,8 +202,9 @@ defmodule Keyword do
#=> [a: 3, b: 2]
"""
def put(list, key, value) do
[{key, value}|delete(list, key)]
@spec put(t, key, value) :: t
def put(keywords, key, value) when is_atom(key) do
[{key, value}|delete(keywords, key)]
end
@doc """
@@ -187,6 +217,7 @@ defmodule Keyword do
#=> true
"""
@spec equal?(t, t) :: boolean
def equal?(left, right) do
:lists.sort(left) == :lists.sort(right)
end
@@ -201,6 +232,7 @@ defmodule Keyword do
#=> [a:3, b:2, d: 4]
"""
@spec merge(t, t) :: t
def merge(d1, d2) do
d2 ++ lc({ k, _ } = tuple inlist d1, not has_key?(d2, k), do: tuple)
end
@@ -217,6 +249,7 @@ defmodule Keyword do
#=> [a:4, b:2, d: 4]
"""
@spec merge(t, t, (key, value, value -> value)) :: t
def merge(d1, d2, fun) do
do_merge(d2, d1, fun)
end
@@ -229,12 +262,6 @@ defmodule Keyword do
acc
end
@doc false
def key?(list, key) do
IO.write "[WARNING] Keyword.key? is deprecated, please use Keyword.has_key? instead\n#{Exception.formatted_stacktrace}"
has_key?(list, key)
end
@doc """
Returns whether a given key exists in the given keywords.
@@ -246,22 +273,24 @@ defmodule Keyword do
#=> false
"""
def has_key?(keywords, key) do
@spec has_key?(t, key) :: boolean
def has_key?(keywords, key) when is_atom(key) do
:lists.keymember(key, 1, keywords)
end
@doc """
Updates the key with the given function. If the key does
not exist, raises `Keyword.KeyError`.
not exist, raises `KeyError`.
## Examples
Keyword.update([a: 1], :a, &1 * 2)
#=> [a: 2]
Keyword.update([a: 1], :b, &1 * 2)
#=> Keyword.KeyError
#=> KeyError
"""
@spec update(t, key, (value -> value)) :: t | no_return
def update([{key, value}|keywords], key, fun) do
[{key, fun.(value)}|delete(keywords, key)]
end
@@ -271,7 +300,7 @@ defmodule Keyword do
end
def update([], key, _fun) when is_atom(key) do
raise(Keyword.KeyError, key: key)
raise(KeyError, key: key)
end
@doc """
@@ -286,6 +315,7 @@ defmodule Keyword do
#=> [a: 1, b: 11]
"""
@spec update(t, key, value, (value -> value)) :: t
def update([{key, value}|keywords], key, _initial, fun) do
[{key, fun.(value)}|delete(keywords, key)]
end
+15 -75
View File
@@ -123,12 +123,6 @@ defmodule List do
:lists.foldr(function, acc, list)
end
@doc false
def reverse(list) do
IO.write "[WARNING] List.reverse/1 is deprecated, please use Enum.reverse/1 instead\n#{Exception.formatted_stacktrace}"
:lists.reverse(list)
end
@doc """
Returns the last element in `list` or nil if the `list` is empty.
@@ -258,26 +252,12 @@ defmodule List do
:lists.keydelete(key, position + 1, list)
end
@doc """
Returns a list of integers in the given range (both ends included when
possible). An optional step can be provided as well (defaults to 1).
If first > last and no step is provided, the numbers will be in descending
order.
## Examples
List.range 1, 3 #=> [1,2,3]
List.range 1, 8, 2 #=> [1,3,5,7]
List.range 1, 0 #=> []
List.range 3, 1 #=> [3,2,1]
List.range 5, 1, -2 #=> [5, 3, 1]
"""
@doc false
def range(first, last, step // nil)
def range(first, last, step) when is_integer(first) and is_integer(last) and first <= last do
step = case step do
IO.write "[WARNING] List.range is deprecated, please use ranges instead\n#{Exception.formatted_stacktrace}"
case step do
nil ->
:lists.seq(first, last, 1)
x when x < 0 ->
@@ -288,7 +268,8 @@ defmodule List do
end
def range(first, last, step) when is_integer(first) and is_integer(last) and first > last do
step = case step do
IO.write "[WARNING] List.range is deprecated, please use ranges instead\n#{Exception.formatted_stacktrace}"
case step do
nil ->
:lists.seq(first, last, -1)
x when x > 0 ->
@@ -298,44 +279,21 @@ defmodule List do
end
end
@doc """
Sorts the list by comparing each term. For an alternative
sorting algorithm, check `Enum.qsort`.
## Examples
List.sort [3, 4, 2, 1, 7]
#=> [1, 2, 3, 4, 7]
"""
@doc false
def sort(list) do
IO.write "[WARNING] List.sort is deprecated, please use Enum.sort instead\n#{Exception.formatted_stacktrace}"
:lists.sort list
end
@doc """
Sorts the list according to an ordering function. fun(a, b) should
return true if `a` compares less than or equal to `b`, `false` otherwise.
## Examples
List.sort [3, 4, 2, 1, 7], fn a, b -> b <= a end
#=> [7, 4, 3, 2, 1]
"""
@doc false
def sort(list, fun) do
IO.write "[WARNING] List.sort is deprecated, please use Enum.sort instead\n#{Exception.formatted_stacktrace}"
:lists.sort fun, list
end
@doc """
Returns a list without duplicated items.
## Examples
List.uniq [1,2,3,2,1]
#=> [1,2,3]
"""
@doc false
def uniq(list) when is_list(list) do
IO.write "[WARNING] List.uniq is deprecated, please use Enum.uniq instead\n#{Exception.formatted_stacktrace}"
do_uniq(list, [])
end
@@ -361,12 +319,6 @@ defmodule List do
[other]
end
@doc false
def zip(item1, item2) do
IO.write "[WARNING] List.zip/2 is deprecated, please use Enum.zip/2 instead\n#{Exception.formatted_stacktrace}"
do_zip(to_list(item1), to_list(item2), [])
end
@doc """
Zips corresponding elements from each list in `list_of_lists`.
@@ -407,10 +359,8 @@ defmodule List do
defp do_uniq([h|t], acc) do
case :lists.member(h, acc) do
true ->
do_uniq(t, acc)
false ->
[h|do_uniq(t, [h|acc])]
true -> do_uniq(t, acc)
false -> [h|do_uniq(t, [h|acc])]
end
end
@@ -420,23 +370,13 @@ defmodule List do
# zip
defp do_zip([h1|t1], [h2|t2], acc) do
do_zip t1, t2, [{h1, h2}|acc]
end
defp do_zip(_, _, acc) do
:lists.reverse acc
end
defp do_zip(list, acc) do
converter = fn x, acc -> do_zip_each(to_list(x), acc) end
{mlist, heads} = :lists.mapfoldl converter, [], list
case heads do
nil ->
:lists.reverse acc
_ ->
do_zip mlist, [list_to_tuple(:lists.reverse(heads))|acc]
nil -> :lists.reverse acc
_ -> do_zip mlist, [list_to_tuple(:lists.reverse(heads))|acc]
end
end
+71 -7
View File
@@ -29,6 +29,28 @@ defmodule Macro do
[:!, :@, :^, :not, :+, :-]
end
@doc """
Receives an expresion representing a possible definition
and extracts its arguments. It returns a tuple with the
function name and the arguments list or `:error` if not
a valid call syntax.
This is useful for macros that want to provide the same
arguments syntax available in def/defp/defmacro and friends.
## Examples
extract_args(quote do: foo) == { :foo, [] }
extract_args(quote do: foo()) == { :foo, [] }
extract_args(quote do: :foo.()) == { :foo, [] }
extract_args(quote do: foo(1,2,3)) == { :foo, [1,2,3] }
extract_args(quote do: 1.(1,2,3)) == :error
"""
def extract_args(expr) do
:elixir_clauses.extract_args(expr)
end
@doc """
Recursively escapes the given value so it can be inserted
into a syntax tree. Structures that are valid syntax nodes
@@ -111,6 +133,11 @@ defmodule Macro do
Octals will by default be escaped unless the map function
returns false for ?0.
## Hex
Octals will by default be escaped unless the map function
returns false for ?x.
## Examples
Using the unescape_map defined above is easy:
@@ -126,7 +153,7 @@ defmodule Macro do
Unescape the given tokens according to the default map.
Check `unescape/1` and `unescape/2` for more information
about unescaping. Only tokens that are binaries are
unescaped, all others are ignored. This method is useful
unescaped, all others are ignored. This function is useful
when implementing your own sigils. Check the implementation
of `Kernel.__b__` for examples.
"""
@@ -189,21 +216,40 @@ defmodule Macro do
end
# Fn keyword
def to_binary({ :fn, _, [[do: block]] }) do
def to_binary({ :fn, _, [[do: { :->, _, [{_,tuple}] } = arrow]] })
when not is_tuple(tuple) or elem(tuple, 0) != :__block__ do
"fn " <> arrow_to_binary(arrow) <> " end"
end
def to_binary({ :fn, _, [[do: { :->, _, [_] } = block]] }) do
"fn " <> block_to_binary(block) <> "\nend"
end
def to_binary({ :fn, _, [[do: block]] }) do
block = adjust_new_lines block_to_binary(block), "\n "
"fn\n " <> block <> "\nend"
end
# Partial call
def to_binary({ :&, _, [num] }) do
"&#{num}"
end
# left -> right
def to_binary({ :->, _, _ } = arrow) do
"(" <> arrow_to_binary(arrow, true) <> ")"
end
# Binary ops
def to_binary({ op, _, [left, right] }) when op in binary_ops do
op_to_binary(left) <> " #{op} " <> op_to_binary(right)
end
# Unary ops
def to_binary({ :not, _, [arg] }) do
"not " <> to_binary(arg)
end
def to_binary({ op, _, [arg] }) when op in unary_ops do
atom_to_binary(op, :utf8) <> to_binary(arg)
end
@@ -236,11 +282,14 @@ defmodule Macro do
defp is_kw_blocks?([_|_] = kw) do
Enum.all?(kw, match?({x, _} when x in kw_keywords, &1))
end
defp is_kw_blocks?(_), do: false
defp is_kw_blocks?(_), do: false
defp module_to_binary(atom) when is_atom(atom), do: Binary.Inspect.inspect(atom, raw: true)
defp module_to_binary(other), do: call_to_binary(other)
defp call_to_binary(atom) when is_atom(atom), do: atom_to_binary(atom, :utf8)
defp call_to_binary({ :., _, [arg] }), do: call_to_binary(arg) <> "."
defp call_to_binary({ :., _, [left, right] }), do: call_to_binary(left) <> "." <> call_to_binary(right)
defp call_to_binary({ :., _, [arg] }), do: module_to_binary(arg) <> "."
defp call_to_binary({ :., _, [left, right] }), do: module_to_binary(left) <> "." <> call_to_binary(right)
defp call_to_binary(other), do: to_binary(other)
defp call_to_binary_with_args(target, args) do
@@ -264,8 +313,8 @@ defmodule Macro do
defp block_to_binary({ :->, _, exprs }) do
Enum.map_join(exprs, "\n", fn({ left, right }) ->
left = Enum.map_join(left, ", ", to_binary(&1))
left <> " ->\n " <> adjust_new_lines block_to_binary(right), "\n "
left = comma_join_or_empty_paren(left, false)
left <> "->\n " <> adjust_new_lines block_to_binary(right), "\n "
end)
end
@@ -281,6 +330,20 @@ defmodule Macro do
defp op_to_binary(expr), do: to_binary(expr)
defp arrow_to_binary({ :->, _, pairs }, paren // false) do
Enum.map_join(pairs, "; ", fn({ left, right }) ->
left = comma_join_or_empty_paren(left, paren)
left <> "-> " <> to_binary(right)
end)
end
defp comma_join_or_empty_paren([], true), do: "() "
defp comma_join_or_empty_paren([], false), do: ""
defp comma_join_or_empty_paren(left, _) do
Enum.map_join(left, ", ", to_binary(&1)) <> " "
end
defp adjust_new_lines(block, replacement) do
bc <<x>> inbits block do
<< case x == ?\n do
@@ -297,6 +360,7 @@ defmodule Macro do
* Macros (local or remote);
* Aliases are expanded (if possible) and return atoms;
* All pseudo-variables (__FILE__, __MODULE__, etc);
* Module attributes reader (@foo);
In case the expression cannot be expanded, it returns the expression itself.
+23 -2
View File
@@ -1,6 +1,22 @@
defmodule Macro.Env do
Record.deffunctions([:module, :file, :line, :function,
:aliases, :context, :requires, :macros], __MODULE__)
@type name_arity :: { atom, non_neg_integer }
@type file :: binary
@type line :: non_neg_integer
@type aliases :: [{ module, module }]
@type context :: :assign | :guard | nil
@type requires :: [module]
@type functions :: [{ module, [name_arity] }]
@type macros :: [{ module, [name_arity] }]
fields = [:module, :file, :line, :function,
:aliases, :context, :requires, :functions, :macros]
types = quote do: [module: module, file: file, line: line,
function: name_arity, aliases: aliases, requires: requires,
functions: functions, macros: macros]
Record.deffunctions(fields, __MODULE__)
Record.deftypes(fields, types, __MODULE__)
@moduledoc """
A record that contains compile time environment information,
@@ -46,6 +62,11 @@ defmodule Macro.Env do
"""
def requires(record)
@doc """
Returns a list of functions imported from each module.
"""
def functions(record)
@doc """
Returns a list of macros imported from each module.
"""
+49 -54
View File
@@ -1,6 +1,12 @@
defmodule Module do
require :ets, as: ETS
defmacrop is_env(env) do
quote do
is_tuple(unquote(env)) and size(unquote(env)) > 1 and elem(unquote(env), 0) == Macro.Env
end
end
@moduledoc """
This module provides many functions to deal with modules during
compilation time. It allows a developer to dynamically attach
@@ -23,43 +29,40 @@ defmodule Module do
@doc """
Evalutes the quotes contents in the given module context.
A list of environment options can also be given as argument.
Check `Code.eval` for more information.
Raises an error if the module was already compiled.
## Options
This function accepts a list of options. The supported
options are:
* `:file` - The filename to be used in stacktraces
and the file reported in the __ENV__ variable.
* `:line` - The line reported in the __ENV__ variable.
## Examples
defmodule Foo do
contents = quote do: (def sum(a, b), do: a + b)
Module.eval_quoted __MODULE__, contents, []
Module.eval_quoted __MODULE__, contents
end
Foo.sum(1, 2) #=> 3
This function also accepts a `Macro.Env` as first argument. This
is useful to evalute the quoted contents inside an existing
environment (considering the environemnt module, line and file):
For convenience, you can my pass `__ENV__` as argument and
all options will be automatically extracted from the environment:
defmodule Foo do
contents = quote do: (def sum(a, b), do: a + b)
Module.eval_quoted __ENV__, contents, []
Module.eval_quoted __MODULE__, contents, [], __ENV__
end
Foo.sum(1, 2) #=> 3
"""
def eval_quoted(env, quoted, binding // [], opts // [])
def eval_quoted(module, quoted, binding // [], opts // [])
def eval_quoted(Macro.Env[module: module] = env, quoted, binding, opts) do
eval_quoted(module, quoted, binding, Keyword.merge(env.location, opts))
def eval_quoted(env, quoted, binding, opts) when is_env(env) do
eval_quoted(env.module, quoted, binding, Keyword.merge(env.to_keywords, opts))
end
def eval_quoted(module, quoted, binding, env) when is_env(env) do
eval_quoted(module, quoted, binding, env.to_keywords)
end
def eval_quoted(module, quoted, binding, opts) do
@@ -79,7 +82,7 @@ defmodule Module do
def world, do: true
end
Module.create(Hello, contents, __ENV__)
Module.create(Hello, contents, __ENV__.location)
Hello.world #=> true
@@ -91,20 +94,19 @@ defmodule Module do
function is preferred when the module body is given
by a quoted expression.
Another important distinction is that `defmodule`
blends into the scope it is invoked, allowing you
to access all variables, imports and requires from
the module. `Module.create`, on the other hand, creates
a new scope so imports, requires, etc are not inherited.
Another important distinction is that `Module.create`
allows you to control the environment variables used
when defining the module, while `defmodule` automatically
shares the same environment.
"""
def create(module, quoted, opts // [])
def create(module, quoted, Macro.Env[] = env) do
create(module, quoted, env.location)
def create(module, quoted, env) when is_env(env) do
create(module, quoted, env.to_keywords)
end
def create(module, quoted, opts) when is_atom(module) do
line = opts[:line] || 1
line = Keyword.get(opts, :line, 1)
:elixir_module.compile(line, module, quoted, [], :elixir.scope_for_eval(opts))
end
@@ -196,7 +198,7 @@ defmodule Module do
end
def add_doc(module, line, kind, tuple, signature, doc) when
kind in [:def, :defmacro, :defcallback] and (is_binary(doc) or is_boolean(doc) or doc == nil) do
kind in [:def, :defmacro] and (is_binary(doc) or is_boolean(doc) or doc == nil) do
assert_not_compiled!(:add_doc, module)
table = docs_table_for(module)
@@ -212,7 +214,7 @@ defmodule Module do
ETS.insert(table, {
tuple,
line,
merge_kind(old_kind, kind),
kind,
merge_signatures(old_sign, signature, 1),
doc || old_doc
})
@@ -249,10 +251,6 @@ defmodule Module do
# Merge
defp merge_kind(:defcallback, new), do: new
defp merge_kind(old, :defcallback), do: old
defp merge_kind(_old, new), do: new
defp merge_signatures([h1|t1], [h2|t2], i) do
[merge_signature(h1, h2, i)|merge_signatures(t1, t2, i + 1)]
end
@@ -371,8 +369,8 @@ defmodule Module do
ETS.delete(table, tuple)
old = get_attribute(module, :__overridable)
new = [ { tuple, { 1, [clause] } } ]
merged = :orddict.merge(fn(_k, { count, v1 }, _v2) -> { count + 1, [clause|v1] } end, old, new)
new = [ { tuple, { 1, clause, false } } ]
merged = :orddict.merge(fn(_k, { count, _, _ }, _v2) -> { count + 1, clause, false } end, old, new)
put_attribute(module, :__overridable, merged)
_ ->
@@ -386,8 +384,7 @@ defmodule Module do
Returns true if the given tuple in module is marked as overridable.
"""
def overridable?(module, tuple) do
key = List.keyfind(get_attribute(module, :__overridable), tuple, 0)
match? { _, { _, [_|_] } }, key
!! List.keyfind(get_attribute(module, :__overridable), tuple, 0)
end
@doc """
@@ -421,12 +418,6 @@ defmodule Module do
ETS.insert(table, { key, new })
end
@doc false
def add_attribute(module, key, value) when is_atom(key) do
IO.write "[WARNING] Module.add_attribute is deprecated, please use Module.put_attribute instead\n#{Exception.formatted_stacktrace}"
put_attribute(module, key, value)
end
@doc """
Gets the given attribute from a module. If the attribute
was marked as accumulate with `Module.register_attribute`,
@@ -456,12 +447,6 @@ defmodule Module do
end
end
@doc false
def read_attribute(module, key) when is_atom(key) do
IO.write "[WARNING] Module.read_attribute is deprecated, please use Module.get_attribute instead\n#{Exception.formatted_stacktrace}"
get_attribute(module, key)
end
@doc """
Deletes all attributes that matches the given key.
@@ -536,19 +521,29 @@ defmodule Module do
"""
def split(module) do
tl(String.split(to_binary(module), "-"))
tl(String.split(Binary.Chars.to_binary(module), "-"))
end
@doc """
Convert a module name to binary without the Elixir prefix.
"""
def to_binary(module) do
"Elixir-" <> rest = Binary.Chars.to_binary(module)
bc <<r>> inbits rest, do: <<to_dot(r)>>
end
defp to_dot(?-), do: ?.
defp to_dot(l), do: l
@doc false
# Used internally to compile documentation. This function
# is private and must be used only internally.
def compile_doc(env, kind, name, args, _guards, body) do
def compile_doc(env, kind, name, args, _guards, _body) do
module = env.module
line = env.line
arity = length(args)
pair = { name, arity }
doc = get_attribute(module, :doc)
kind = if kind == :def and body == nil and module != Kernel, do: :defcallback, else: kind
case add_doc(module, line, kind, pair, args, doc) do
:ok ->
@@ -565,7 +560,7 @@ defmodule Module do
@doc false
# Used internally to compile types. This function
# is private and must be used only internally.
def compile_type(module, key, value) when is_atom(key) do
def compile_typespec(module, key, value) when is_atom(key) do
assert_not_compiled!(:put_attribute, module)
table = data_table_for(module)
@@ -589,7 +584,7 @@ defmodule Module do
atom
end
defp normalize_attribute(:file, Macro.Env[file: file, line: line]), do: { binary_to_list(file), line}
defp normalize_attribute(:file, env) when is_env(env), do: { binary_to_list(env.file), env.line}
defp normalize_attribute(:file, { binary, line }) when is_binary(binary), do: { binary_to_list(binary), line }
defp normalize_attribute(:file, other) when not is_tuple(other), do: normalize_attribute(:file, { other, 1 })
+30 -7
View File
@@ -73,23 +73,46 @@ defmodule Node do
Returns the pid of a new process started by the application of `fun`
on `node`. If `node` does not exist, a useless pid is returned.
Check http://www.erlang.org/doc/man/erlang.html#spawn_opt-4 for
Check http://www.erlang.org/doc/man/erlang.html#spawn-2 for
the list of available options.
"""
def spawn(node, fun, opts // []) do
def spawn(node, fun) do
:erlang.spawn(node, fun)
end
@doc """
Returns the pid of a new process started by the application of `fun`
on `node`. If `node` does not exist, a useless pid is returned.
Check http://www.erlang.org/doc/man/erlang.html#spawn_opt-3 for
the list of available options.
"""
def spawn(node, fun, opts) do
:erlang.spawn_opt(node, fun, opts)
end
@doc """
Returns the pid of a new process started by the application of
`module.function(args)` on `node`. If `node` does not exists, a useless
pid is returned.
`module.function(args)` on `node`. If `node` does not exists,
a useless pid is returned.
Check http://www.erlang.org/doc/man/erlang.html#spawn_opt-4 for
Check http://www.erlang.org/doc/man/erlang.html#spawn-4 for
the list of available options.
"""
def spawn(node, module, fun, args, opts // []) do
:erlang.spawn(node, module, fun, args, opts)
def spawn(node, module, fun, args) do
:erlang.spawn(node, module, fun, args)
end
@doc """
Returns the pid of a new process started by the application of
`module.function(args)` on `node`. If `node` does not exists,
a useless pid is returned.
Check http://www.erlang.org/doc/man/erlang.html#spawn_opt-5 for
the list of available options.
"""
def spawn(node, module, fun, args, opts) do
:erlang.spawn_opt(node, module, fun, args, opts)
end
@doc """
+16 -9
View File
@@ -44,8 +44,7 @@ defmodule OptionParser do
def parse(argv, opts // []) when is_list(argv) and is_list(opts) do
aliases = opts[:aliases] || []
flags = opts[:flags] || []
dict = Keyword.new(flags, fn(k) -> { k, false } end)
parse(argv, aliases, flags, dict, [], true)
parse(argv, aliases, flags, true)
end
@doc """
@@ -63,12 +62,15 @@ defmodule OptionParser do
def parse_head(argv, opts // []) when is_list(argv) and is_list(opts) do
aliases = opts[:aliases] || []
flags = opts[:flags] || []
dict = Keyword.new(flags, fn(k) -> { k, false } end)
parse(argv, aliases, flags, dict, [], false)
parse(argv, aliases, flags, false)
end
## Helpers
defp parse(argv, aliases, flags, all) do
parse(argv, aliases, flags, [], [], all)
end
defp parse(["-" <> option|t], aliases, flags, dict, args, all) do
{ option, value } = normalize_option(option, aliases)
@@ -84,16 +86,16 @@ defmodule OptionParser do
parse(t, aliases, flags, dict, args, all)
end
defp parse([], _, _, dict, args, true) do
{ dict, Enum.reverse(args) }
defp parse([], _, flags, dict, args, true) do
{ reverse_dict(dict, flags), Enum.reverse(args) }
end
defp parse([h|t], aliases, flags, dict, args, true) do
parse(t, aliases, flags, dict, [h|args], true)
end
defp parse(value, _, _, dict, _args, false) do
{ dict, value }
defp parse(value, _, flags, dict, _args, false) do
{ reverse_dict(dict, flags), value }
end
defp flag_from_tail([h|t]) when h in ["false", "true"], do: { h, t }
@@ -108,7 +110,12 @@ defmodule OptionParser do
end
defp store_option(dict, option, value) do
Keyword.put dict, option, value
[{ option, value }|dict]
end
defp reverse_dict(dict, flags) do
flags = lc k inlist flags, not Keyword.has_key?(dict, k), do: { k, false }
Enum.reverse flags ++ dict
end
defp normalize_option(<<?-, option :: binary>>, aliases) do
+8
View File
@@ -44,6 +44,14 @@ defmodule OrdDict do
end
end
@doc false
def get!(dict(data), key) do
case :orddict.find(key, data) do
{:ok, value} -> value
:error -> raise(KeyError, key: key)
end
end
@doc false
def put(dict(data), key, value) do
dict(:orddict.store key, value, data)
+25 -8
View File
@@ -103,12 +103,19 @@ defmodule Process do
@doc """
Returns the pid of a new process started by the application of `fun`.
It behaves exactly the same as the `Kernel.spawn/1` function except
it also accepts extra options, for the list of available options
check http://www.erlang.org/doc/man/erlang.html#spawn_opt-4
It behaves exactly the same as `Kernel.spawn/1`.
"""
def spawn(fun, opts // []) do
def spawn(fun) do
:erlang.spawn(fun)
end
@doc """
Returns the pid of a new process started by the application of `fun`.
It also accepts extra options, for the list of available options
check http://www.erlang.org/doc/man/erlang.html#spawn_opt-2
"""
def spawn(fun, opts) do
:erlang.spawn_opt(fun, opts)
end
@@ -117,12 +124,22 @@ defmodule Process do
`module.function(args)`. The new process created will be placed in the system
scheduler queue and be run some time later.
It behaves exactly the same as the `Kernel.spawn/3` function except
it also accepts extra options, for the list of available options
It behaves exactly the same as the `Kernel.spawn/3` function.
"""
def spawn(mod, fun, args) do
:erlang.spawn(mod, fun, args)
end
@doc """
Returns the pid of a new process started by the application of
`module.function(args)`. The new process created will be placed in the system
scheduler queue and be run some time later.
It also accepts extra options, for the list of available options
check http://www.erlang.org/doc/man/erlang.html#spawn_opt-4
"""
def spawn(mod, fun, args, opts // []) do
def spawn(mod, fun, args, opts) do
:erlang.spawn_opt(mod, fun, args, opts)
end
+207 -129
View File
@@ -23,13 +23,14 @@ defmodule Protocol do
defmodule unquote(name) do
# We don't allow function definition inside protocols
import Kernel, except: [
defmacrop: 1, defmacrop: 2, defmacrop: 4,
defmacro: 1, defmacro: 2, defmacro: 4,
defp: 1, defp: 2, defp: 4,
def: 1, def: 2, def: 4
]
# Import the new dsl that holds the new def
import Protocol.DSL
import :macros, Protocol.DSL
# Set up a clear slate to store defined functions
@functions []
@@ -38,9 +39,9 @@ defmodule Protocol do
unquote(block)
# Define callbacks and meta information
{ conversions, fallback } = Protocol.conversions_for(__MODULE__, @only, @except)
Protocol.impl_for(conversions, fallback, __ENV__)
Protocol.meta(@functions, fallback, __ENV__)
{ conversions, fallback, returns_nil } = Protocol.conversions_for(__MODULE__, @only, @except)
Protocol.impl_for(conversions, fallback, returns_nil, __ENV__)
Protocol.meta(@functions, conversions, fallback, returns_nil, __ENV__)
end
end
end
@@ -74,10 +75,9 @@ defmodule Protocol do
end
end
@doc """
Check if the given module is a protocol. Raises an error
if not loaded or not a protocol.
"""
# Check if the given module is a protocol. Raises an error
# if not loaded or not a protocol.
@doc false
def assert_protocol(module) do
case Code.ensure_compiled(module) do
{ :module, ^module } -> nil
@@ -92,60 +92,16 @@ defmodule Protocol do
end
end
@doc """
Defines meta information about the protocol and internal callbacks.
"""
def meta(functions, fallback, env) do
contents = quote do
def __protocol__(:name), do: __MODULE__
def __protocol__(:functions), do: unquote(:lists.sort(functions))
@doc false
def behaviour_info(:callbacks), do: [{ :__impl__, 0 }|__protocol__(:functions)]
def __impl_for__(arg) do
case __raw_impl__(arg) do
__MODULE__.Record ->
target = Module.concat(__MODULE__, :erlang.element(1, arg))
try do
target.__impl__
target
catch
:error, :undef, [[{ ^target, :__impl__, [], _ }|_]|_] ->
__fallback__
end
other ->
other
end
end
def __impl_for__!(arg) do
if module = __impl_for__(arg) do
module
else
raise Protocol.UndefinedError, protocol: __MODULE__, structure: arg
end
end
defp __fallback__, do: unquote(fallback)
end
Module.eval_quoted env, contents
end
@doc """
Implements the function that detects the protocol and returns
the module to dispatch to. Returns module.Record for records
which should be properly handled by the dispatching function.
"""
def impl_for(conversions, fallback, env) do
# Implements the function that detects the protocol and returns
# the module to dispatch to. Returns module.Record for records
# which should be properly handled by the dispatching function.
@doc false
def impl_for(conversions, fallback, returns_nil, env) do
contents = lc kind inlist conversions do
each_impl_for(kind, if fallback, do: conversions)
each_impl_for(kind, conversions, fallback)
end
# If we don't implement all protocols and any is not in the
# list, we need to add a final clause that returns nil.
if !L.keyfind(Any, 1, conversions) && length(conversions) < 10 do
if returns_nil do
contents = contents ++ [quote do
defp __raw_impl__(_) do
nil
@@ -153,13 +109,65 @@ defmodule Protocol do
end]
end
Module.eval_quoted env, contents
Module.eval_quoted env.module, contents, [], env.location
end
@doc """
Returns the default conversions according to the given
only/except options.
"""
# Defines meta information about the protocol and internal callbacks.
@doc false
def meta(functions, conversions, fallback, returns_nil, env) do
any = L.keyfind(Any, 1, conversions) != false
meta = quote do
unless Kernel.Typespec.defines_type?(__MODULE__, :t, 0) do
@type t :: unquote(generate_type(conversions, any))
end
def __protocol__(:name), do: __MODULE__
def __protocol__(:functions), do: unquote(:lists.sort(functions))
end
impl_for = if L.keyfind(Record, 1, conversions) do
quote do
def __impl_for__(arg) do
case __raw_impl__(arg) do
__MODULE__.Record ->
target = Module.concat(__MODULE__, :erlang.element(1, arg))
try do
target.__impl__
target
catch
:error, :undef, [[{ ^target, :__impl__, [], _ }|_]|_] ->
unquote(fallback)
end
other ->
other
end
end
end
else
quote do
def __impl_for__(arg), do: __raw_impl__(arg)
end
end
impl_bang = if returns_nil do
quote do
def __impl_for__!(arg) do
__impl_for__(arg) || raise(Protocol.UndefinedError, protocol: __MODULE__, structure: arg)
end
end
else
quote do
def __impl_for__!(arg), do: __impl_for__(arg)
end
end
Module.eval_quoted env.module, [meta, impl_for, impl_bang], [], env.location
end
# Returns the default conversions according to the given
# only/except options.
@doc false
def conversions_for(module, only, except) do
kinds = all_types
@@ -180,65 +188,63 @@ defmodule Protocol do
nil
end
{ conversions, fallback }
# If any is not in the list and we don't implement all
# protocols, we need to handle nil cases.
returns_nil = L.keyfind(Any, 1, conversions) == false and length(conversions) < 10
{ conversions, fallback, returns_nil }
end
## Helpers
defp generate_type(_conversions, true) do
quote(do: any)
end
defp generate_type(conversions, false) do
or_function = fn({ _, _, x }, acc) -> { :|, 0, [acc, x] } end
{ _, _, first } = hd(conversions)
:lists.foldl(or_function, first, tl(conversions))
end
defp all_types do
[
{ Record, :is_record },
{ Tuple, :is_tuple },
{ Atom, :is_atom },
{ List, :is_list },
{ BitString, :is_bitstring },
{ Number, :is_number },
{ Function, :is_function },
{ PID, :is_pid },
{ Port, :is_port },
{ Reference, :is_reference },
{ Any, :is_any }
]
[ { Record, :is_record, quote do: tuple },
{ Tuple, :is_tuple, quote do: tuple },
{ Atom, :is_atom, quote do: atom },
{ List, :is_list, quote do: list },
{ BitString, :is_bitstring, quote do: <<>> },
{ Number, :is_number, quote do: number },
{ Function, :is_function, quote do: (... -> any) },
{ PID, :is_pid, quote do: pid },
{ Port, :is_port, quote do: port },
{ Reference, :is_reference, quote do: reference },
{ Any, :is_any, quote do: any } ]
end
# Returns a quoted expression that allows to check
# if the first item in the tuple is a built-in or not.
defp is_builtin?([{h,_}]) do
defp is_builtin?([{h,_,_}]) do
quote do
first == unquote(h)
end
end
defp is_builtin?([{h,_}|t]) do
defp is_builtin?([{h,_,_}|t]) do
quote do
first == unquote(h) or unquote(is_builtin?(t))
end
end
# Handle records when we don't have fallbacks.
# It simply gets the first element of the tuple.
# This case assumes that, whenever a tuple is given
# it is meant to be a record, so we don't need extra
# checks.
defp each_impl_for({ _, :is_record }, nil) do
quote do
defp __raw_impl__(arg) when is_tuple(arg) and is_atom(:erlang.element(1, arg)) do
__MODULE__.Record
end
end
end
# Specially handle records in the case we have fallbacks.
defp each_impl_for({ _, :is_record }, conversions) do
defp each_impl_for({ _, :is_record, _ }, conversions, fallback) do
quote do
defp __raw_impl__(arg) when is_tuple(arg) and is_atom(:erlang.element(1, arg)) do
first = :erlang.element(1, arg)
case unquote(is_builtin?(conversions)) do
true -> __fallback__
true -> unquote(fallback)
false ->
case atom_to_list(first) do
'Elixir-' ++ _ -> __MODULE__.Record
_ -> __fallback__
_ -> unquote(fallback)
end
end
end
@@ -246,7 +252,7 @@ defmodule Protocol do
end
# Special case any as we don't need to generate a guard.
defp each_impl_for({ _, :is_any }, _) do
defp each_impl_for({ _, :is_any, _ }, _, _) do
quote do
defp __raw_impl__(_) do
__MODULE__.Any
@@ -255,10 +261,10 @@ defmodule Protocol do
end
# Generate all others protocols.
defp each_impl_for({ kind, fun }, _) do
defp each_impl_for({ kind, fun, _ }, _, _) do
quote do
defp __raw_impl__(arg) when unquote(fun).(arg) do
Module.concat __MODULE__, unquote(kind)
__MODULE__.unquote(kind)
end
end
end
@@ -267,6 +273,95 @@ end
defmodule Protocol.DSL do
@moduledoc false
# We need to use :lists because Enum is not available yet
require :lists, as: L
@doc false
def args_and_body(module, name, arity) do
# Generate arguments according the arity. The arguments
# are named xa, xb and so forth. We cannot use string
# interpolation to generate the arguments because of compile
# dependencies, so we use the <<>> instead.
args = lc i inlist :lists.seq(1, arity) do
{ binary_to_atom(<<?x, i + 64>>), 0, :quoted }
end
{ conversions, fallback, returns_nil } = conversions_for(module)
clauses = [default_clause(name, args)]
if returns_nil do
clauses = [nil_clause()|clauses]
end
if L.keyfind(Record, 1, conversions) do
clauses = [record_clause(name, args, fallback)|clauses]
end
body =
quote do
case __raw_impl__(xA), do: unquote({ :->, 0, clauses })
end
{ args, body }
end
@doc false
def callback_from_spec(module, name, arity) do
tuple = { name, arity }
specs = Module.get_attribute(module, :spec)
found = lc { k, v } inlist specs, k == tuple do
Kernel.Typespec.define_callback(module, tuple, v)
true
end
found != []
end
defp conversions_for(module) do
only = Module.get_attribute(module, :only)
except = Module.get_attribute(module, :except)
Protocol.conversions_for(module, only, except)
end
defp default_clause(name, args) do
quote do: { [other], apply(other, unquote(name), [unquote_splicing(args)]) }
end
defp nil_clause() do
quote do
{ [nil], raise(Protocol.UndefinedError, protocol: __MODULE__, structure: xA) }
end
end
defp record_clause(name, args, fallback) do
arity = length(args)
quote do
{ [__MODULE__.Record],
(target = Module.concat(__MODULE__, :erlang.element(1, xA))
try do
target.unquote(name)(unquote_splicing(args))
catch
:error, :undef, [[{ ^target, name, args, _ }|_]|_] when
name == unquote(name) and length(args) == unquote(arity) ->
unquote(catch_clause(args, fallback))
end) }
end
end
defp catch_clause(args, fallback) do
if fallback do
quote do
apply unquote(fallback), name, [unquote_splicing(args)]
end
else
quote do
raise Protocol.UndefinedError, protocol: __MODULE__, structure: xA
end
end
end
defmacro def(expression) do
case expression do
{ _, _, args } when args == [] or is_atom(args) ->
@@ -279,43 +374,26 @@ defmodule Protocol.DSL do
arity = length(args)
# Generate arguments according the arity. The arguments
# are named xa, xb and so forth. We cannot use string
# interpolation to generate the arguments because of compile
# dependencies, so we use the <<>> instead.
generated = lc i inlist :lists.seq(1, arity) do
{ binary_to_atom(<<?x, i + 64>>), 0, :quoted }
end
type_args = lc _ inlist :lists.seq(2, arity), do: quote(do: term)
type_args = [quote(do: t) | type_args]
quote do
# Append new function to the list
@functions [unquote({name, arity})|@functions]
name = unquote(name)
arity = unquote(arity)
@functions [{name, arity}|@functions]
# Generate a fake definition with the user
# signature that will be used by docs
Kernel.def unquote(name).(unquote_splicing(args))
Kernel.def unquote(name).(unquote_splicing(generated)) do
case __raw_impl__(xA) do
__MODULE__.Record ->
target = Module.concat(__MODULE__, :erlang.element(1, xA))
try do
target.unquote(name)(unquote_splicing(generated))
catch
:error, :undef, [[{ ^target, unquote(name), args, _ }|_]|_] when length(args) == unquote(arity) ->
case __fallback__ do
nil ->
raise Protocol.UndefinedError, protocol: __MODULE__, structure: xA
other ->
apply other, unquote(name), [unquote_splicing(generated)]
end
end
nil ->
raise Protocol.UndefinedError, protocol: __MODULE__, structure: xA
other ->
apply other, unquote(name), [unquote_splicing(generated)]
end
end
{ args, body } = Protocol.DSL.args_and_body(__MODULE__, name, arity)
Kernel.def unquote(name), args, [], do: body
# Convert the spec to callback if possible,
# otherwise generate a dummy callback
Protocol.DSL.callback_from_spec(__MODULE__, name, arity) ||
@callback unquote(name)(unquote_splicing(type_args)) :: term
end
end
end
+255 -210
View File
@@ -18,7 +18,7 @@ defmodule Record do
@doc """
Main entry point for records definition. It defines a module
with the given `name` and the fields specified in `modules`.
with the given `name` and the fields specified in `values`.
This is invoked directly by `Kernel.defrecord`, so check it
for more information and documentation.
"""
@@ -29,125 +29,165 @@ defmodule Record do
quote do
defmodule unquote(name) do
@moduledoc false
Record.deffunctions(unquote(values), unquote(opts), __ENV__)
import Record.DSL
values = unquote(values)
opts = unquote(opts)
Record.deffunctions(values, opts, __ENV__)
unquote(block)
Record.deftypes(values, @record_type, opts, __ENV__)
end
end
end
@doc """
Main entry point for private records definition. It defines
a set of macros with the given `name` and the fields specified
in `values`. This is invoked directly by `Kernel.defrecordp`,
so check it for more information and documentation.
"""
def defrecordp(name, fields) do
quote do
Record.defmacros(unquote(name), unquote(fields), __ENV__)
end
end
@doc """
Defines record functions skipping the module definition.
This is called directly by `defrecord`. It expects the
module environment, the module values and a keyword list
of options.
This is called directly by `defrecord`. It expects the record
values, a set of options and the module environment.
## Examples
defmodule CustomRecord do
Record.deffunctions [:name, :age], __ENV__
Record.deftypes [:name, :age], __ENV__
end
"""
def deffunctions(values, opts // [], env) do
values = lc value inlist values, do: convert_value(value)
escaped = Macro.escape(values)
extensions = Keyword.get(opts, :extensions, Record.Extensions)
def deffunctions(values, _opts // [], env) do
values = lc value inlist values, do: convert_value(value)
escaped = Macro.escape(values)
contents = [
reflection(escaped),
initializer(escaped),
indexes(escaped),
readers(values, 1, []),
conversions(values),
writers(values, 1, []),
updater(values),
extensions(values, 1, [], extensions)
extensions(values, 1, [], Record.Extensions),
accessors(values),
]
contents = [quote(do: @__record__ unquote(escaped))|contents]
# Special case for bootstraping purposes
if env == Macro.Env do
:elixir_module.eval_quoted(env, contents, [], [])
Module.eval_quoted(env, contents, [], [])
else
contents = [quote(do: @__record__ unquote(escaped))|contents]
Module.eval_quoted(env, contents)
Module.eval_quoted(env.module, contents, [], env.location)
end
end
@doc """
Defines three macros for reading and writing records values.
These macros are private to the current module and are
basically a simple mechanism for manipulating tuples when
there isn't an interest in exposing the record as a whole.
In some ways, it is similar to Erlang records, since it is
only available at compilation time.
Defines types and specs for the record.
"""
def deftypes(values, types, _opts // [], env) do
types = types || []
values = lc value inlist values do
{ name, default } = convert_value(value)
{ name, default, find_spec(types, name) }
end
contents = [
core_specs(values),
accessor_specs(values, 1, [])
]
if env == Macro.Env do
Module.eval_quoted(env, contents, [], [])
else
Module.eval_quoted(env.module, contents, [], env.location)
end
end
@doc """
Defines macros for manipulating records. This is called
directly by `defrecordp`. It expects the macro name, the
record values and the environment.
## Examples
defmodule CustomModule do
Record.defmacros :_user, [:name, :age], __ENV__
def new(name, age) do
_user(name: name, age: age)
end
def name(user, name) do
_user(user, name: name)
end
def age(user) do
_user(user, :age)
end
def to_keywords(user) do
_user(user)
end
def name_and_age(user) do
_user(user, [:name, :age])
end
def age_and_name(user) do
_user(user, [:age, :name])
end
defmodule CustomRecord do
Record.defmacros :user, [:name, :age], __ENV__
end
"""
def defmacros(name, values, env) do
escaped = lc value inlist values, do: Macro.escape(convert_value(value))
escaped = lc value inlist values do
{ key, value } = convert_value(value)
{ key, Macro.escape(value) }
end
contents = quote do
defmacrop unquote(name).() do
Record.access(__MODULE__, unquote(escaped), [], __CALLER__)
end
defmacrop unquote(name).(record) when is_tuple(record) do
Record.to_keywords(__CALLER__, __MODULE__, unquote(escaped), record)
Record.to_keywords(__MODULE__, unquote(escaped), record)
end
defmacrop unquote(name).(args) do
Record.access(__CALLER__, __MODULE__, unquote(escaped), args)
Record.access(__MODULE__, unquote(escaped), args, __CALLER__)
end
defmacrop unquote(name).(record, key) when is_atom(key) do
Record.get(__CALLER__, __MODULE__, unquote(escaped), record, key)
Record.get(__MODULE__, unquote(escaped), record, key)
end
defmacrop unquote(name).(record, args) do
Record.dispatch(__CALLER__, __MODULE__, unquote(escaped), record, args)
Record.dispatch(__MODULE__, unquote(escaped), record, args, __CALLER__)
end
end
Module.eval_quoted(env, contents)
Module.eval_quoted(env.module, contents, [], env.location)
end
defp convert_value(atom) when is_atom(atom) do
{ atom, nil }
## Callbacks
# Store all optimizable fields in the record as well
@doc false
defmacro __before_compile__(_) do
quote do
def __record__(:optimizable), do: @record_optimizable
end
end
defp convert_value(other), do: other
# Store fields that can be optimized and that cannot be
# optimized as they are overriden
@doc false
def __on_definition__(env, kind, name, args, _guards, _body) do
tuple = { name, length(args) }
module = env.module
functions = Module.get_attribute(module, :record_optimizable)
functions =
if kind in [:def] and Module.get_attribute(module, :record_optimized) do
[tuple|functions]
else
List.delete(functions, tuple)
end
Module.put_attribute(module, :record_optimizable, functions)
end
# Implements the access macro used by records.
# It returns a quoted expression that defines
# a record or a match in case the record is
# inside a match.
@doc false
def access(caller, atom, fields, keyword) do
def access(atom, fields, keyword, caller) do
unless is_keyword(keyword) do
raise "expected contents inside brackets to be a Keyword"
end
@@ -184,11 +224,11 @@ defmodule Record do
# Dispatch the call to either update or to_list depending on the args given.
@doc false
def dispatch(caller, atom, fields, record, args) do
def dispatch(atom, fields, record, args, caller) do
if is_keyword(args) do
update(caller, atom, fields, record, args)
update(atom, fields, record, args, caller)
else
to_list(caller, atom, fields, record, args)
to_list(atom, fields, record, args)
end
end
@@ -196,7 +236,7 @@ defmodule Record do
# It returns a quoted expression that represents
# the access given by the keywords.
@doc false
defp update(caller, atom, fields, var, keyword) do
defp update(atom, fields, var, keyword, caller) do
unless is_keyword(keyword) do
raise "expected contents inside brackets to be a Keyword"
end
@@ -221,7 +261,7 @@ defmodule Record do
# It returns a quoted expression that represents
# getting the value of a given field.
@doc false
def get(_caller, atom, fields, var, key) do
def get(atom, fields, var, key) do
index = find_index(fields, key, 0)
if index do
quote do
@@ -236,12 +276,12 @@ defmodule Record do
# It returns a quoted expression that represents
# converting record to keywords list.
@doc false
def to_keywords(_caller, _atom, fields, record) do
Enum.map Keyword.from_enum(fields),
fn({key, _default}) ->
def to_keywords(_atom, fields, record) do
Enum.map fields,
fn { key, _default } ->
index = find_index(fields, key, 0)
quote do
{unquote(key), :erlang.element(unquote(index + 2), unquote(record))}
{ unquote(key), :erlang.element(unquote(index + 2), unquote(record)) }
end
end
end
@@ -250,7 +290,7 @@ defmodule Record do
# It returns a quoted expression that represents
# extracting given fields from record.
@doc false
defp to_list(_caller, atom, fields, record, keys) do
defp to_list(atom, fields, record, keys) do
Enum.map keys,
fn(key) ->
index = find_index(fields, key, 0)
@@ -262,11 +302,7 @@ defmodule Record do
end
end
defp is_keyword(list) when is_list(list), do: :lists.all(is_keyword_tuple(&1), list)
defp is_keyword(_), do: false
defp is_keyword_tuple({ x, _ }) when is_atom(x), do: true
defp is_keyword_tuple(_), do: false
## Function generation
# Define __record__/1 and __record__/2 as reflection functions
# that returns the record names and fields.
@@ -378,11 +414,7 @@ defmodule Record do
end
end
defp find_index([{ k, _ }|_], k, i), do: i
defp find_index([{ _, _ }|t], k, i), do: find_index(t, k, i + 1)
defp find_index([], _k, _i), do: nil
# Implement readers. For a declaration like:
# Implement accessors. For a declaration like:
#
# defrecord FileInfo, atime: nil, mtime: nil
#
@@ -396,24 +428,6 @@ defmodule Record do
# elem(record, 2)
# end
#
defp readers([{ key, _default }|t], i, acc) do
contents = quote do
def unquote(key).(record) do
:erlang.element(unquote(i + 1), record)
end
end
readers(t, i + 1, [contents | acc])
end
defp readers([], _i, acc), do: acc
# Implement writers. For a declaration like:
#
# defrecord FileInfo, atime: nil, mtime: nil
#
# It will define four methods:
#
# def :atime.(value, record) do
# setelem(record, 1, value)
# end
@@ -422,30 +436,70 @@ defmodule Record do
# setelem(record, 2, value)
# end
#
defp writers([{ key, _default }|t], i, acc) do
# def :atime.(callback, record) do
# setelem(record, 1, callback.(elem(record, 1)))
# end
#
# def :mtime.(callback, record) do
# setelem(record, 2, callback.(elem(record, 2)))
# end
#
defp accessors(values) do
[ quote do
@record_optimized true
@record_optimizable []
@before_compile { unquote(__MODULE__), :__before_compile__ }
@on_definition { unquote(__MODULE__), :__on_definition__ }
end | accessors(values, 1) ]
end
defp accessors([{ :__exception__, _ }|t], 1) do
accessors(t, 2)
end
defp accessors([{ key, _default }|t], i) do
update = binary_to_atom "update_" <> atom_to_binary(key)
contents = quote do
def unquote(key).(record) do
:erlang.element(unquote(i + 1), record)
end
def unquote(key).(value, record) do
:erlang.setelement(unquote(i + 1), record, value)
end
def unquote(update).(function, record) do
:erlang.setelement(unquote(i + 1), record,
function.(:erlang.element(unquote(i + 1), record)))
end
end
writers(t, i + 1, [contents | acc])
[contents|accessors(t, i + 1)]
end
defp writers([], _i, acc), do: acc
defp accessors([], _i) do
[quote do: @record_optimized false]
end
# Defines update/2
# Define an updater method that receives a
# keyword list and updates the record.
defp updater(values) do
fields =
lc {key, _default} inlist values do
index = find_index(values, key, 1)
quote do
Keyword.get(keywords,
unquote(key),
elem(record, unquote(find_index(values, key, 1))))
Keyword.get(keywords, unquote(key), elem(record, unquote(index)))
end
end
contents = {:{}, 0, [(quote do: __MODULE__)|fields]}
contents = { :{}, 0, [(quote do: __MODULE__)|fields] }
quote do
def update([], record) do
record
end
def update(keywords, record) do
unquote(contents)
end
@@ -459,157 +513,148 @@ defmodule Record do
end
defp extensions([], _i, acc, _), do: acc
## Types/specs generation
defp core_specs(values) do
types = lc { _, _, spec } inlist values, do: spec
quote do
unless Kernel.Typespec.defines_type?(__MODULE__, :t, 0) do
@type t :: { __MODULE__, unquote_splicing(types) }
end
@spec new :: t
@spec new(Keyword.t | tuple) :: t
@spec to_keywords(t) :: Keyword.t
@spec update(Keyword.t, t) :: t
@spec __index__(atom) :: non_neg_integer | nil
end
end
defp accessor_specs([{ :__exception__, _, _ }|t], 1, acc) do
accessor_specs(t, 2, acc)
end
defp accessor_specs([{ key, _default, spec }|t], i, acc) do
update = binary_to_atom "update_" <> atom_to_binary(key)
contents = quote do
@spec unquote(key)(t) :: unquote(spec)
@spec unquote(key)(unquote(spec), t) :: t
@spec unquote(update)((unquote(spec) -> unquote(spec)), t) :: t
end
accessor_specs(t, i + 1, [contents | acc])
end
defp accessor_specs([], _i, acc), do: acc
## Helpers
defp is_keyword(list) when is_list(list), do: :lists.all(is_keyword_tuple(&1), list)
defp is_keyword(_), do: false
defp is_keyword_tuple({ x, _ }) when is_atom(x), do: true
defp is_keyword_tuple(_), do: false
defp convert_value(atom) when is_atom(atom), do: { atom, nil }
defp convert_value({ atom, other }) when is_atom(atom) and is_function(other), do:
raise ArgumentError, message: "record field default value #{inspect atom} cannot be a function"
defp convert_value({ atom, other }) when is_atom(atom) and (is_reference(other) or is_pid(other) or is_port(other)), do:
raise ArgumentError, message: "record field default value #{inspect atom} cannot be a reference, pid or port"
defp convert_value({ atom, _ } = tuple) when is_atom(atom), do: tuple
defp convert_value({ field, _ }), do:
raise ArgumentError, message: "record field name has to be an atom, got #{inspect field}"
defp find_index([{ k, _ }|_], k, i), do: i
defp find_index([{ _, _ }|t], k, i), do: find_index(t, k, i + 1)
defp find_index([], _k, _i), do: nil
defp find_spec(types, name) do
matches = lc { k, v } inlist types, name == k, do: v
case matches do
[h|_] -> h
_ -> quote do: term
end
end
end
defmodule Record.Extractor do
defmodule Record.DSL do
@moduledoc false
# Retrieve a record definition from an Erlang file using
# the same lookup as the *include* attribute from Erlang modules.
def retrieve(name, from: string) do
file = to_char_list(string)
case :code.where_is_file(file) do
:non_existing -> realfile = file
realfile -> nil
@doc """
Defines the type for each field in the record.
Expects a keyword list.
"""
defmacro record_type(opts) when is_list(opts) do
quote do
@record_type quote do: unquote(opts)
end
retrieve_record(name, realfile)
end
# Retrieve a record definition from an Erlang file using
# the same lookup as the *include_lib* attribute from Erlang modules.
def retrieve(name, from_lib: file) do
[app|path] = :filename.split(to_char_list(file))
case :code.lib_dir(to_char_list(app)) do
{ :error, _ } ->
raise ArgumentError, "Lib file #{to_binary(file)} could not be found"
libpath ->
retrieve_record name, :filename.join([libpath|path])
end
end
# Retrieve the record with the given name from the given file
defp retrieve_record(name, file) do
records = retrieve_from_file(file)
if record = List.keyfind(records, name, 0) do
parse_record(record)
else
raise ArgumentError, "No record #{name} found at #{to_binary(file)}"
end
end
# Parse the given file and retrieve all existent records.
defp retrieve_from_file(file) do
lc { :attribute, _, :record, record } inlist read_file(file), do: record
end
# Read a file and return its abstract syntax form that also
# includes record and other preprocessor modules. This is done
# by using Erlang's epp_dodger.
defp read_file(file) do
case :epp_dodger.quick_parse_file(file) do
{ :ok, form } ->
form
other ->
raise "Error parsing file #{to_binary(file)}, got: #{inspect(other)}"
end
end
# Parse a tuple with name and fields and returns a
# list of second order tuples where the first element
# is the field and the second is its default value.
defp parse_record({ _name, fields }) do
cons = List.foldr fields, { nil, 0 }, fn f, acc ->
{ :cons, 0, parse_field(f), acc }
end
{ :value, list, _ } = :erl_eval.expr(cons, [])
list
end
defp parse_field({ :typed_record_field, record_field, _type }) do
parse_field(record_field)
end
defp parse_field({ :record_field, _, key }) do
{ :tuple, 0, [key, {:atom, 0, :nil}] }
end
defp parse_field({ :record_field, _, key, value }) do
{ :tuple, 0, [key, value] }
end
end
defmodule Record.Extensions do
@moduledoc false
# Main entry point. It defines both default functions
# via `default_for` and extensions via `extension_for`.
# Function definition
def functions_for(key, default, i) do
[ default_for(key, default, i),
extension_for(key, default, i) ]
extension_for(key, default, i)
end
# Skip the __exception__ for defexception.
def default_for(:__exception__, _default, _i) do
nil
end
# Define the default functions for each field.
def default_for(key, _default, i) do
bin_update = "update_" <> atom_to_binary(key)
update = binary_to_atom(bin_update)
quote do
def unquote(update).(function, record) do
current = :erlang.element(unquote(i + 1), record)
:erlang.setelement(unquote(i + 1), record, function.(current))
end
end
end
# Define extensions based on the default type.
def extension_for(key, default, i) when is_list(default) do
bin_key = atom_to_binary(key)
prepend = :"prepend_#{bin_key}"
merge = :"merge_#{bin_key}"
defp extension_for(key, default, i) when is_list(default) do
prepend = prefix("prepend_", key)
merge = prefix("merge_", key)
quote do
def unquote(prepend).(value, record) do
IO.write "[WARNING] record default-based generated function #{unquote(prepend)} is deprecated\n#{Exception.formatted_stacktrace}"
current = :erlang.element(unquote(i + 1), record)
:erlang.setelement(unquote(i + 1), record, value ++ current)
end
def unquote(merge).(value, record) do
IO.write "[WARNING] record default-based generated function #{unquote(merge)} is deprecated\n#{Exception.formatted_stacktrace}"
current = :erlang.element(unquote(i + 1), record)
:erlang.setelement(unquote(i + 1), record, Keyword.merge(current, value))
end
end
end
def extension_for(key, default, i) when is_number(default) do
bin_key = atom_to_binary(key)
increment = :"increment_#{bin_key}"
defp extension_for(key, default, i) when is_number(default) do
increment = prefix("increment_", key)
quote do
def unquote(increment).(value // 1, record) do
IO.write "[WARNING] record default-based generated function #{unquote(increment)} is deprecated\n#{Exception.formatted_stacktrace}"
current = :erlang.element(unquote(i + 1), record)
:erlang.setelement(unquote(i + 1), record, current + value)
end
end
end
def extension_for(key, default, i) when is_boolean(default) do
bin_key = atom_to_binary(key)
toggle = :"toggle_#{bin_key}"
defp extension_for(key, default, i) when is_boolean(default) do
toggle = prefix("toggle_", key)
quote do
def unquote(toggle).(record) do
IO.write "[WARNING] record default-based generated function #{unquote(toggle)} is deprecated\n#{Exception.formatted_stacktrace}"
current = :erlang.element(unquote(i + 1), record)
:erlang.setelement(unquote(i + 1), record, not current)
end
end
end
def extension_for(_, _, _), do: nil
defp extension_for(_, _, _), do: nil
# Helpers
defp prefix(prefix, key) do
binary_to_atom prefix <> atom_to_binary(key)
end
end
+79
View File
@@ -0,0 +1,79 @@
defmodule Record.Extractor do
@moduledoc false
# Retrieve a record definition from an Erlang file using
# the same lookup as the *include* attribute from Erlang modules.
def retrieve(name, from: string) do
file = to_char_list(string)
case :code.where_is_file(file) do
:non_existing -> realfile = file
realfile -> nil
end
retrieve_record(name, realfile)
end
# Retrieve a record definition from an Erlang file using
# the same lookup as the *include_lib* attribute from Erlang modules.
def retrieve(name, from_lib: file) do
[app|path] = :filename.split(to_char_list(file))
case :code.lib_dir(to_char_list(app)) do
{ :error, _ } ->
raise ArgumentError, "lib file #{to_binary(file)} could not be found"
libpath ->
retrieve_record name, :filename.join([libpath|path])
end
end
# Retrieve the record with the given name from the given file
defp retrieve_record(name, file) do
records = retrieve_from_file(file)
if record = List.keyfind(records, name, 0) do
parse_record(record)
else
raise ArgumentError, "no record #{name} found at #{to_binary(file)}"
end
end
# Parse the given file and retrieve all existent records.
defp retrieve_from_file(file) do
lc { :attribute, _, :record, record } inlist read_file(file), do: record
end
# Read a file and return its abstract syntax form that also
# includes record and other preprocessor modules. This is done
# by using Erlang's epp_dodger.
defp read_file(file) do
case :epp_dodger.quick_parse_file(file) do
{ :ok, form } ->
form
other ->
raise "error parsing file #{to_binary(file)}, got: #{inspect(other)}"
end
end
# Parse a tuple with name and fields and returns a
# list of tuples where the first element is the field
# and the second is its default value.
defp parse_record({ _name, fields }) do
cons = List.foldr fields, { nil, 0 }, fn f, acc ->
{ :cons, 0, parse_field(f), acc }
end
{ :value, list, _ } = :erl_eval.expr(cons, [])
list
end
defp parse_field({ :typed_record_field, record_field, _type }) do
parse_field(record_field)
end
defp parse_field({ :record_field, _, key }) do
{ :tuple, 0, [key, {:atom, 0, :undefined}] }
end
defp parse_field({ :record_field, _, key, value }) do
{ :tuple, 0, [key, value] }
end
end
+31 -56
View File
@@ -16,7 +16,7 @@ defmodule Regex do
The re module provides several options, the one available in Elixir, followed by
their shortcut in parenthesis, are:
* unicode (u) - used when you want to match against specific unicode characters
* unicode (u) - enable unicode specific patterns like \p
* caseless (i) - add case insensitivity
* dotall (s) - causes dot to match newlines and also set newline to anycrlf.
The new line setting can be overwritten by setting `(*CR)` or `(*LF)` or
@@ -43,6 +43,9 @@ defmodule Regex do
a binary, a char list will return a char list).
"""
defrecordp :regex, [:re_pattern, :source, :options, :groups]
@type t :: { Regex, term, term, term, term }
defexception CompileError, message: "regex could not be compiled"
@doc """
@@ -57,9 +60,10 @@ defmodule Regex do
opts = translate_options(options)
re_opts = opts -- [:groups]
groups = if opts != re_opts, do: parse_groups(source)
case :re.compile(source, re_opts) do
{ :ok, compiled } ->
{ :ok, { Regex, compiled, source, options, groups } }
{ :ok, re_pattern } ->
{ :ok, regex(re_pattern: re_pattern, source: source, options: options, groups: groups) }
error ->
error
end
@@ -87,7 +91,7 @@ defmodule Regex do
Regex.index %r/e/, "abcd" #=> nil
"""
def index({ Regex, compiled, _, _, _ }, string) do
def index(regex(re_pattern: compiled), string) do
case :re.run(string, compiled, [{ :capture, :first, :index }]) do
:nomatch -> nil
{ :match, [{index,_}] } -> index
@@ -103,7 +107,7 @@ defmodule Regex do
Regex.match? %r/foo/, "bar" #=> false
"""
def match?({ Regex, compiled, _, _, _ }, string) do
def match?(regex(re_pattern: compiled), string) do
:re.run(string, compiled, [{ :capture, :none }]) == :match
end
@@ -118,12 +122,13 @@ defmodule Regex do
"""
def run(regex, string, options // [])
def run({ Regex, compiled, _, _, groups }, string, options) do
def run(regex(re_pattern: compiled, groups: groups), string, options) do
return = Keyword.get(options, :return, return_for(string))
captures =
case Keyword.get(options, :capture, :all) do
:groups -> groups || raise "Regex was not compiled with g"
:groups -> groups || raise "regex was not compiled with g"
others -> others
end
@@ -141,10 +146,10 @@ defmodule Regex do
Regex.captures %r/c(?<foo>d)/g, "abcd" #=> [{:foo, ["d"]}]
"""
def captures({ Regex, _, _, _, groups } = regex, string, options // []) do
def captures(regex(groups: groups) = regex, string, options // []) do
unless captures = Keyword.get(options, :capture) do
captures = if groups do
List.sort(groups)
Enum.sort(groups)
else
raise "Regex was not compiled with g"
end
@@ -154,30 +159,10 @@ defmodule Regex do
if results, do: Enum.zip captures, results
end
@doc """
Returns a list with the match indexes in the given string.
The matches are tuples where the first element is the index
(zero indexed) the match happened and the second is the length
of the match.
## Examples
Regex.indexes %r/c(d)/, "abcd" #=> [{2,2},{3,1}]
Regex.indexes %r/e/, "abcd" #=> nil
"""
def indexes({ Regex, compiled, _, _, _ }, string) do
IO.write "[WARNING] Regex.indexes is deprecated, please use Regex.run with return: :index as option instead\n#{Exception.formatted_stacktrace}"
case :re.run(string, compiled, [{ :capture, :all, :index }]) do
:nomatch -> nil
{ :match, results } -> results
end
end
@doc """
Returns the underlying re_pattern in the regular expression.
"""
def re_pattern({ Regex, compiled, _, _, _ }) do
def re_pattern(regex(re_pattern: compiled)) do
compiled
end
@@ -189,7 +174,7 @@ defmodule Regex do
Regex.source %r(foo) #=> "foo"
"""
def source({ Regex, _, source, _, _ }) do
def source(regex(source: source)) do
source
end
@@ -201,8 +186,8 @@ defmodule Regex do
Regex.opts %r(foo)m #=> 'm'
"""
def opts({ Regex, _, _, opts, _ }) do
opts
def opts(regex(options: options)) do
options
end
@doc """
@@ -213,7 +198,7 @@ defmodule Regex do
Regex.groups %r/(?<foo>foo)/g #=> ["foo"]
"""
def groups({ Regex, _, _, _, groups }) do
def groups(regex(groups: groups)) do
groups
end
@@ -231,8 +216,9 @@ defmodule Regex do
"""
def scan(regex, string, options // [])
def scan({ Regex, compiled, _, _, _ }, string, options) do
return = options[:return] || return_for(string)
def scan(regex(re_pattern: compiled), string, options) do
return = Keyword.get(options, :return, return_for(string))
options = [{ :capture, :all, return }, :global]
case :re.run(string, compiled, options) do
:nomatch -> []
@@ -247,21 +233,16 @@ defmodule Regex do
def split(regex, string, options // [])
def split(regex, string, options) when is_integer(options) or is_atom(options) do
IO.write "[WARNING] Passing an integer or atom to Regex.split/3 is deprecated, pass a :parts option instead\n#{Exception.formatted_stacktrace}"
split(regex, string, parts: options)
end
def split({ Regex, compiled, _, _, _ }, string, options) do
def split(regex(re_pattern: compiled), string, options) do
parts =
cond do
options[:global] == false -> 2
p = options[:parts] -> p
true -> :infinity
Keyword.get(options, :global) == false -> 2
p = Keyword.get(options, :parts) -> p
true -> :infinity
end
return = options[:return] || return_for(string)
opts = [{ :return, return }, { :parts, parts }]
return = Keyword.get(options, :return, return_for(string))
opts = [return: return, parts: parts]
:re.split(string, compiled, opts)
end
@@ -283,19 +264,13 @@ defmodule Regex do
Regex.replace(%r/(b)/, "abc", "[\\1]") #=> "a[b]c"
"""
def replace({ Regex, compiled, _, _, _ }, string, replacement, options // []) do
opts = if options[:global] != false, do: [:global], else: []
return = options[:return] || return_for(string)
def replace(regex(re_pattern: compiled), string, replacement, options // []) do
opts = if Keyword.get(options, :global) != false, do: [:global], else: []
return = Keyword.get(options, :return, return_for(string))
opts = [{ :return, return }|opts]
:re.replace(string, compiled, replacement, opts)
end
@doc false
def replace_all({ Regex, compiled, _, _, _ }, string, replacement) do
IO.write "[WARNING] Regex.replace_all is deprecated, simply use Regex.replace instead\n#{Exception.formatted_stacktrace}"
:re.replace(string, compiled, replacement, [{ :return, return_for(string) }, :global])
end
# Helpers
@doc false
+153 -99
View File
@@ -1,13 +1,23 @@
defmodule String do
@moduledoc """
A string in Elixir is a utf-8 binary. This module
contains function to work with utf-8 data and its
codepoints.
contains function to work with utf-8 data, its
codepoints and graphemes.
Notice that graphemes is a superset of UTF-8 codepoints
which also contains named sequences as defined per
http://www.unicode.org/reports/tr34/. In short, graphemes
also contain multiple characters that are "perceived as
a single character" by readers.
For working with raw binaries, use Erlang's :binary
module.
"""
@type t :: binary
@type codepoint :: t
@type grapheme :: t
@doc """
Checks if a string is printable considering it is encoded
as UTF-8. Returns true if so, false otherwise.
@@ -17,7 +27,7 @@ defmodule String do
String.printable?("abc") #=> true
"""
@spec printable?(t) :: boolean
# Allow basic ascii chars
def printable?(<<c, t :: binary>>) when c in ?\s..?~ do
printable?(t)
@@ -90,7 +100,6 @@ defmodule String do
def printable?(<<>>), do: true
def printable?(_), do: false
@doc """
Divides a string into sub string based on a pattern,
returning a list of these sub string. The pattern can
@@ -116,6 +125,9 @@ defmodule String do
String.split("a,b", %r{\.}) #=> ["a,b"]
"""
@spec split(t) :: [t]
@spec split(t, t | [t] | Regex.t) :: [t]
@spec split(t, t | [t] | Regex.t, Keyword.t) :: [t]
def split(binary, pattern // " ", options // [])
def split(binary, pattern, options) when is_regex(pattern) do
@@ -128,7 +140,11 @@ defmodule String do
end
@doc """
Convert all characters on the given string to upper case.
Convert all characters on the given string to upcase.
This function relies on the simple uppercase mapping
available in Unicode 6.2.0, check http://unicode.org/reports/tr44/
for more information.
## Examples
@@ -137,22 +153,15 @@ defmodule String do
String.upcase("josé") #=> "JOSÉ"
"""
def upcase(<<>>), do: <<>>
def upcase(<<195, c, t :: binary>>) when c in 160..191 do
<<195, c - 32, upcase(t) :: binary>>
end
def upcase(<<c, t :: binary>>) when c in ?a..?z do
<<c - 32, upcase(t) :: binary>>
end
def upcase(<<c, t :: binary>>) do
<<c , upcase(t) :: binary>>
end
@spec upcase(t) :: t
defdelegate upcase(binary), to: String.Unicode
@doc """
Convert all characters on the given string to down case.
Convert all characters on the given string to downcase.
This function relies on the simple lowercase mapping
available in Unicode 6.2.0, check http://unicode.org/reports/tr44/
for more information.
## Examples
@@ -161,66 +170,77 @@ defmodule String do
String.downcase("JOSÉ") #=> "josé"
"""
def downcase(<<>>), do: <<>>
def downcase(<<195, c, t :: binary>>) when c in 128..159 do
<<195, c + 32, downcase(t) :: binary>>
end
def downcase(<<c, t :: binary>>) when c in ?A..?Z do
<<c + 32, downcase(t) :: binary>>
end
def downcase(<<c, t :: binary>>) do
<<c , downcase(t) :: binary>>
end
@spec downcase(t) :: t
defdelegate downcase(binary), to: String.Unicode
@doc """
Returns a string where trailing char have been
removed. If no `char` is passed `space`is used.
Returns a string where trailing whitespace characters
and new line have been removed.
## Examples
String.rstrip(" abc ") #=> " abc"
"""
@spec rstrip(t) :: t
defdelegate rstrip(binary), to: String.Unicode
@doc """
Returns a string where trailing `char` have been removed.
## Examples
String.rstrip(" abc _", ?_) #=> " abc "
"""
def rstrip(string, char // ?\s)
@spec rstrip(t, char) :: t
def rstrip("", _char), do: ""
# Do a quick check before we traverse the whole
# binary. :binary.last is a fast operation (it
# does not traverse the whole binary).
def rstrip(string, char) do
if :binary.last(string) == char do
rstrip(string, "", char)
do_rstrip(string, "", char)
else
string
end
end
defp rstrip(<<char, string :: binary>>, buffer, char) do
rstrip(string, <<char, buffer :: binary>>, char)
defp do_rstrip(<<char, string :: binary>>, buffer, char) do
do_rstrip(string, <<char, buffer :: binary>>, char)
end
defp rstrip(<<char, string :: binary>>, buffer, another_char) do
<<buffer :: binary, char, rstrip(string, "", another_char) :: binary>>
defp do_rstrip(<<char, string :: binary>>, buffer, another_char) do
<<buffer :: binary, char, do_rstrip(string, "", another_char) :: binary>>
end
defp rstrip(<<>>, _, _) do
defp do_rstrip(<<>>, _, _) do
<<>>
end
@doc """
Returns a string where leading char have been
removed. If no `char` is passed `space`is used.
Returns a string where leading whitespace characters
have been removed.
## Examples
String.lstrip(" abc ") #=> "abc "
"""
defdelegate lstrip(binary), to: String.Unicode
@doc """
Returns a string where leading `char` have been removed.
## Examples
String.lstrip("_ abc _", ?_) #=> " abc _"
"""
def lstrip(string, char // ?\s)
@spec lstrip(t, char) :: t
def lstrip(<<char, rest :: binary>>, char) do
<<lstrip(rest, char) :: binary>>
@@ -231,16 +251,32 @@ defmodule String do
end
@doc """
Returns a string where leading/trailing char have been
removed. If no `char` is passed `space`is used.
Returns a string where leading/trailing whitespace
and new line characters have been removed.
## Examples
String.strip(" abc ") #=> "abc"
"""
@spec strip(t) :: t
def strip(string) do
rstrip(lstrip(string))
end
@doc """
Returns a string where leading/trailing `char` have been
removed.
## Examples
String.strip("a abc a", ?a) #=> " abc "
"""
def strip(string, char // ?\s) do
@spec strip(t, char) :: t
def strip(string, char) do
rstrip(lstrip(string, char), char)
end
@@ -263,6 +299,9 @@ defmodule String do
String.replace("a,b,c", ",", "[]", insert_replaced: [1,1]) #=> "a[,,]b[,,]c"
"""
@spec replace(t, t, t) :: t
@spec replace(t, t, t, Keyword.t) :: t
def replace(subject, pattern, replacement, options // []) do
opts = translate_replace_options(options)
:binary.replace(subject, pattern, replacement, opts)
@@ -287,6 +326,7 @@ defmodule String do
String.duplicate("abc", 2) #=> "abcabc"
"""
@spec duplicate(t, pos_integer) :: t
def duplicate(subject, n) when is_integer(n) and n > 0 do
:binary.copy(subject, n)
end
@@ -301,18 +341,51 @@ defmodule String do
String.codepoints("ἅἪῼ") #=> ["ἅ","Ἢ","ῼ"]
"""
def codepoints(string) do
do_codepoints(codepoint(string))
end
defp do_codepoints({char, rest}) do
[char|do_codepoints(codepoint(rest))]
end
defp do_codepoints(:no_codepoint), do: []
@spec codepoints(t) :: [codepoint]
defdelegate codepoints(string), to: String.Unicode
@doc """
Returns the first codepoint from an utf8 string.
Returns the next codepoint in a String.
The result is a tuple with the codepoint and the
remaining of the string or `:no_codepoint` in case
the String reached its end.
## Examples
String.next_codepoint("josé") #=> { "j", "osé" }
"""
@spec next_codepoint(t) :: codepoint | :no_codepoint
defdelegate next_codepoint(string), to: String.Unicode
@doc """
Returns unicode graphemes in the string
## Examples
String.graphemes("Ā̀stute") # => ["Ā̀","s","t","u","t","e"]
"""
@spec graphemes(t) :: [grapheme]
defdelegate graphemes(string), to: String.Unicode
@doc """
Returns the next grapheme in a String.
The result is a tuple with the grapheme and the
remaining of the string or `:no_grapheme` in case
the String reached its end.
## Examples
String.next_grapheme("josé") #=> { "j", "osé" }
"""
@spec next_grapheme(t) :: grapheme | :no_grapheme
defdelegate next_grapheme(string), to: String.Unicode
@doc """
Returns the first grapheme from an utf8 string.
## Examples
@@ -320,15 +393,16 @@ defmodule String do
String.first("եոգլի") #=> "ե"
"""
@spec first(t) :: grapheme
def first(string) do
case codepoint(string) do
case next_grapheme(string) do
{ char, _ } -> char
:no_codepoint -> ""
:no_grapheme -> ""
end
end
@doc """
Returns the last codepoint from an utf8 string.
Returns the last grapheme from an utf8 string.
## Examples
@@ -336,18 +410,19 @@ defmodule String do
String.last("եոգլի") #=> "ի"
"""
@spec last(t) :: grapheme
def last(string) do
do_last(codepoint(string), "")
do_last(next_grapheme(string), "")
end
defp do_last({char, rest}, _) do
do_last(codepoint(rest), char)
do_last(next_grapheme(rest), char)
end
defp do_last(:no_codepoint, last_char), do: last_char
defp do_last(:no_grapheme, last_char), do: last_char
@doc """
Returns the number of codepoint in an utf8 string.
Returns the number of unicode graphemes in an utf8 string.
## Examples
@@ -355,71 +430,50 @@ defmodule String do
String.length("եոգլի") #=> 5
"""
@spec length(t) :: non_neg_integer
def length(string) do
do_length(codepoint(string))
do_length(next_grapheme(string))
end
defp do_length({_, rest}) do
1 + do_length(codepoint(rest))
1 + do_length(next_grapheme(rest))
end
defp do_length(:no_codepoint), do: 0
defp do_length(:no_grapheme), do: 0
@doc """
Returns the codepoint in the `position` of the given utf8 `string`.
Returns the grapheme in the `position` of the given utf8 `string`.
If `position` is greater than `string` length, than it returns `nil`.
## Examples
String.at("elixir", 0) #=> "1"
String.at("elixir", 0) #=> "e"
String.at("elixir", 1) #=> "l"
String.at("elixir", 10) #=> nil
String.at("elixir", -1) #=> "r"
String.at("elixir", -10) #=> "nil"
String.at("elixir", -10) #=> nil
"""
@spec at(t, integer) :: grapheme | nil
def at(string, position) when position >= 0 do
do_at(codepoint(string), position, 0)
do_at(next_grapheme(string), position, 0)
end
def at(string, position) when position < 0 do
real_pos = do_length(codepoint(string)) - abs(position)
real_pos = do_length(next_grapheme(string)) - abs(position)
case real_pos >= 0 do
true -> do_at(codepoint(string), real_pos, 0)
false -> ""
true -> do_at(next_grapheme(string), real_pos, 0)
false -> nil
end
end
defp do_at({_ , rest}, desired_pos, current_pos) when desired_pos > current_pos do
do_at(codepoint(rest), desired_pos, current_pos + 1)
do_at(next_grapheme(rest), desired_pos, current_pos + 1)
end
defp do_at({char, _}, desired_pos, current_pos) when desired_pos == current_pos do
char
end
defp do_at(:no_codepoint, _, _), do: ""
# Private implementation which returns the first codepoint
# of any given utf8 string and the rest of it
# If an empty string is given, :no_codepoint is returned.
defp codepoint(<<194, char, rest :: binary>>)
when char in 161..191,
do: { <<194, char>>, rest }
defp codepoint(<<first, char, rest :: binary>>)
when first in 195..223 and char in 128..191,
do: { <<first, char>>, rest }
defp codepoint(<<first, second, char, rest :: binary>>)
when first == 224 and second in 160..191 and char in 128..191,
do: { <<first, second, char>>, rest }
defp codepoint(<<first, second, char, rest :: binary>>)
when first in 225..239 and second in 128..191 and char in 128..191,
do: { <<first, second, char>>, rest }
defp codepoint(<<other, rest :: binary>>), do: { <<other>>, rest }
defp codepoint(<<>>), do: :no_codepoint
defp do_at(:no_grapheme, _, _), do: nil
end
+71 -6
View File
@@ -8,7 +8,8 @@ defmodule System do
# Tries to run `git describe --always --tags`. In case of success
# returns the most recent tag, otherwise returns an empty string.
defmacrop get_describe do
if :os.find_executable('git') do
dotgit = File.join(File.cwd!, ".git")
if :os.find_executable('git') && File.exists?(dotgit) do
data = :os.cmd('git describe --always --tags')
Regex.replace %r/\n/, to_binary(data), ""
else
@@ -24,11 +25,13 @@ defmodule System do
@doc """
Returns Elixir's version as binary.
"""
def version, do: "0.7.0"
@spec version() :: String.t
def version, do: "0.7.2"
@doc """
Returns a keywords list with version, git tag info and date.
"""
@spec build_info() :: Keyword.t
def build_info do
[version: version, tag: get_describe, date: get_date]
end
@@ -36,6 +39,7 @@ defmodule System do
@doc """
Returns the list of command-line arguments passed to the program.
"""
@spec argv() :: [String.t]
def argv do
:gen_server.call(:elixir_code_server, :argv)
end
@@ -60,6 +64,8 @@ defmodule System do
If `command` is a char list, a char list is returned.
Returns a binary otherwise.
"""
@spec cmd(char_list) :: char_list
@spec cmd(String.t) :: String.t
def cmd(command) when is_list(command) do
:os.cmd(command)
end
@@ -76,6 +82,8 @@ defmodule System do
If `command` is a char list, a char list is returned.
Returns a binary otherwise.
"""
@spec find_executable(char_list) :: char_list | nil
@spec find_executable(String.t) :: String.t | nil
def find_executable(command) when is_list(command) do
:os.find_executable(command) || nil
end
@@ -92,8 +100,9 @@ defmodule System do
given as a single string of the format "VarName=Value", where VarName is the
name of the variable and Value its value.
"""
@spec get_env() :: [{String.t, String.t}]
def get_env do
Enum.map :os.getenv, list_to_binary &1
Enum.map :os.getenv, :unicode.characters_to_binary &1
end
@doc """
@@ -101,10 +110,11 @@ defmodule System do
`varname` as a binary, or nil if the environment
variable is undefined.
"""
@spec get_env(String.t) :: String.t | nil
def get_env(varname) do
case :os.getenv(to_char_list(varname)) do
false -> nil
other -> list_to_binary(other)
other -> :unicode.characters_to_binary(other)
end
end
@@ -114,19 +124,23 @@ defmodule System do
See http://www.erlang.org/doc/man/os.html#getpid-0 for more info.
"""
@spec get_pid() :: String.t
def get_pid, do: list_to_binary(:os.getpid)
@doc """
Sets a new `value` for the environment variable `varname`.
"""
def put_env(varname, value) do
:os.putenv to_char_list(varname), to_char_list(value)
@spec put_env(String.t, String.t | char_list) :: :ok
def put_env(varname, value) when is_binary(value) or is_list(value) do
:os.putenv to_char_list(varname), :unicode.characters_to_list(value)
:ok
end
@doc """
Sets a new value for each environment variable corresponding
to each key in `dict`.
"""
@spec put_env(Dict.t) :: :ok
def put_env(dict) do
Enum.each dict, fn {key, val} -> put_env key, val end
end
@@ -138,6 +152,57 @@ defmodule System do
filter_stacktrace :erlang.get_stacktrace
end
@doc """
Halts the Erlang runtime system where the first argument status must be a
non-negative integer, the atom `:abort` or any type that can be converted
to a char list.
* If an integer, the runtime system exits with the integer value which
is returned to the Operating System;
* If `:abort`, the runtime system aborts producing a core dump, if that is
enabled in the operating system;
* If a char list, an erlang crash dump is produced with status as slogan,
and then the runtime system exits with status code 1;
Note that on many platforms, only the status codes 0-255 are supported
by the operating system.
For integer status, Erlang runtime system closes all ports and allows async
threads to finish their operations before exiting. To exit without such
flushing, pass options [flush: false] instead.
For more information, check: http://www.erlang.org/doc/man/erlang.html#halt-2
## Examples
System.halt(0)
System.halt(1, flush: false)
System.halt(:abort)
"""
@spec halt() :: no_return
@spec halt(non_neg_integer | List.Chars.t | :abort) :: no_return
@spec halt(non_neg_integer | List.Chars.t | :abort, [] | [flush: false]) :: no_return
def halt(status // 0, options // [])
def halt(status, options) when is_integer(status) or status == :abort do
do_halt(status, options)
end
def halt(status, options) do
do_halt(to_char_list(status), options)
end
# Support R15B
if List.member?(:erlang.module_info(:exports), { :halt, 2 }) do
defp do_halt(status, options), do: :erlang.halt(status, options)
else
IO.puts "Using limited halt support. Upgrade to R15B01 or later is recommended."
defp do_halt(status, _options), do: :erlang.halt(status)
end
## Helpers
# Filter stacktrace by removing internal BOOTSTRAP calls.
+1 -6
View File
@@ -81,14 +81,9 @@ defmodule URI do
<<c>>
end
defp percent(c), do: escape_byte(c)
defp escape_byte(c), do: "%" <> hex(c)
defp percent(c), do: "%" <> hex(bsr(c, 4)) <> hex(band(c, 15))
defp hex(n) when n <= 9, do: <<n + ?0>>
defp hex(n) when n > 15 do
hex(bsr(n, 4)) <> hex(band(n, 15))
end
defp hex(n), do: <<n + ?A - 10>>
@doc """
+2 -2
View File
@@ -9,10 +9,10 @@ defmodule URI.Parser do
@doc """
Responsible for parsing extra URL information.
"""
defcallback parse(uri_info)
defcallback parse(uri_info :: URI.Info.t) :: URI.Info.t
@doc """
Responsible for returning the default port.
"""
defcallback default_port()
defcallback default_port() :: integer
end
+420
View File
@@ -0,0 +1,420 @@
LATIN CAPITAL LETTER A WITH MACRON AND GRAVE;0100 0300
LATIN SMALL LETTER A WITH MACRON AND GRAVE;0101 0300
LATIN CAPITAL LETTER E WITH VERTICAL LINE BELOW;0045 0329
LATIN SMALL LETTER E WITH VERTICAL LINE BELOW;0065 0329
LATIN CAPITAL LETTER E WITH VERTICAL LINE BELOW AND GRAVE;00C8 0329
LATIN SMALL LETTER E WITH VERTICAL LINE BELOW AND GRAVE;00E8 0329
LATIN CAPITAL LETTER E WITH VERTICAL LINE BELOW AND ACUTE;00C9 0329
LATIN SMALL LETTER E WITH VERTICAL LINE BELOW AND ACUTE;00E9 0329
LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND MACRON;00CA 0304
LATIN SMALL LETTER E WITH CIRCUMFLEX AND MACRON;00EA 0304
LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND CARON;00CA 030C
LATIN SMALL LETTER E WITH CIRCUMFLEX AND CARON;00EA 030C
LATIN CAPITAL LETTER I WITH MACRON AND GRAVE;012A 0300
LATIN SMALL LETTER I WITH MACRON AND GRAVE;012B 0300
LATIN SMALL LETTER I WITH DOT ABOVE AND ACUTE;0069 0307 0301
LATIN SMALL LETTER NG WITH TILDE ABOVE;006E 0360 0067
LATIN CAPITAL LETTER O WITH VERTICAL LINE BELOW;004F 0329
LATIN SMALL LETTER O WITH VERTICAL LINE BELOW;006F 0329
LATIN CAPITAL LETTER O WITH VERTICAL LINE BELOW AND GRAVE;00D2 0329
LATIN SMALL LETTER O WITH VERTICAL LINE BELOW AND GRAVE;00F2 0329
LATIN CAPITAL LETTER O WITH VERTICAL LINE BELOW AND ACUTE;00D3 0329
LATIN SMALL LETTER O WITH VERTICAL LINE BELOW AND ACUTE;00F3 0329
LATIN CAPITAL LETTER S WITH VERTICAL LINE BELOW;0053 0329
LATIN SMALL LETTER S WITH VERTICAL LINE BELOW;0073 0329
LATIN CAPITAL LETTER U WITH MACRON AND GRAVE;016A 0300
LATIN SMALL LETTER U WITH MACRON AND GRAVE;016B 0300
LATIN CAPITAL LETTER A WITH OGONEK AND ACUTE;0104 0301
LATIN SMALL LETTER A WITH OGONEK AND ACUTE;0105 0301
LATIN CAPITAL LETTER A WITH OGONEK AND TILDE;0104 0303
LATIN SMALL LETTER A WITH OGONEK AND TILDE;0105 0303
LATIN CAPITAL LETTER E WITH OGONEK AND ACUTE;0118 0301
LATIN SMALL LETTER E WITH OGONEK AND ACUTE;0119 0301
LATIN CAPITAL LETTER E WITH OGONEK AND TILDE;0118 0303
LATIN SMALL LETTER E WITH OGONEK AND TILDE;0119 0303
LATIN CAPITAL LETTER E WITH DOT ABOVE AND ACUTE;0116 0301
LATIN SMALL LETTER E WITH DOT ABOVE AND ACUTE;0117 0301
LATIN CAPITAL LETTER E WITH DOT ABOVE AND TILDE;0116 0303
LATIN SMALL LETTER E WITH DOT ABOVE AND TILDE;0117 0303
LATIN SMALL LETTER I WITH DOT ABOVE AND GRAVE;0069 0307 0300
LATIN SMALL LETTER I WITH DOT ABOVE AND TILDE;0069 0307 0303
LATIN CAPITAL LETTER I WITH OGONEK AND ACUTE;012E 0301
LATIN SMALL LETTER I WITH OGONEK AND DOT ABOVE AND ACUTE;012F 0307 0301
LATIN CAPITAL LETTER I WITH OGONEK AND TILDE;012E 0303
LATIN SMALL LETTER I WITH OGONEK AND DOT ABOVE AND TILDE;012F 0307 0303
LATIN CAPITAL LETTER J WITH TILDE;004A 0303
LATIN SMALL LETTER J WITH DOT ABOVE AND TILDE;006A 0307 0303
LATIN CAPITAL LETTER L WITH TILDE;004C 0303
LATIN SMALL LETTER L WITH TILDE;006C 0303
LATIN CAPITAL LETTER M WITH TILDE;004D 0303
LATIN SMALL LETTER M WITH TILDE;006D 0303
LATIN CAPITAL LETTER R WITH TILDE;0052 0303
LATIN SMALL LETTER R WITH TILDE;0072 0303
LATIN CAPITAL LETTER U WITH OGONEK AND ACUTE;0172 0301
LATIN SMALL LETTER U WITH OGONEK AND ACUTE;0173 0301
LATIN CAPITAL LETTER U WITH OGONEK AND TILDE;0172 0303
LATIN SMALL LETTER U WITH OGONEK AND TILDE;0173 0303
LATIN CAPITAL LETTER U WITH MACRON AND ACUTE;016A 0301
LATIN SMALL LETTER U WITH MACRON AND ACUTE;016B 0301
LATIN CAPITAL LETTER U WITH MACRON AND TILDE;016A 0303
LATIN SMALL LETTER U WITH MACRON AND TILDE;016B 0303
LATIN SMALL LETTER AE WITH GRAVE;00E6 0300
LATIN SMALL LETTER OPEN O WITH GRAVE;0254 0300
LATIN SMALL LETTER OPEN O WITH ACUTE;0254 0301
LATIN SMALL LETTER TURNED V WITH GRAVE;028C 0300
LATIN SMALL LETTER TURNED V WITH ACUTE;028C 0301
LATIN SMALL LETTER SCHWA WITH GRAVE;0259 0300
LATIN SMALL LETTER SCHWA WITH ACUTE;0259 0301
LATIN SMALL LETTER HOOKED SCHWA WITH GRAVE;025A 0300
LATIN SMALL LETTER HOOKED SCHWA WITH ACUTE;025A 0301
BENGALI LETTER KHINYA;0995 09CD 09B7
TAMIL CONSONANT K; 0B95 0BCD
TAMIL CONSONANT NG; 0B99 0BCD
TAMIL CONSONANT C; 0B9A 0BCD
TAMIL CONSONANT NY; 0B9E 0BCD
TAMIL CONSONANT TT; 0B9F 0BCD
TAMIL CONSONANT NN; 0BA3 0BCD
TAMIL CONSONANT T; 0BA4 0BCD
TAMIL CONSONANT N; 0BA8 0BCD
TAMIL CONSONANT P; 0BAA 0BCD
TAMIL CONSONANT M; 0BAE 0BCD
TAMIL CONSONANT Y; 0BAF 0BCD
TAMIL CONSONANT R; 0BB0 0BCD
TAMIL CONSONANT L; 0BB2 0BCD
TAMIL CONSONANT V; 0BB5 0BCD
TAMIL CONSONANT LLL;0BB4 0BCD
TAMIL CONSONANT LL; 0BB3 0BCD
TAMIL CONSONANT RR; 0BB1 0BCD
TAMIL CONSONANT NNN;0BA9 0BCD
TAMIL CONSONANT J; 0B9C 0BCD
TAMIL CONSONANT SH; 0BB6 0BCD
TAMIL CONSONANT SS; 0BB7 0BCD
TAMIL CONSONANT S; 0BB8 0BCD
TAMIL CONSONANT H; 0BB9 0BCD
TAMIL CONSONANT KSS;0B95 0BCD 0BB7 0BCD
TAMIL SYLLABLE KAA; 0B95 0BBE
TAMIL SYLLABLE KI; 0B95 0BBF
TAMIL SYLLABLE KII; 0B95 0BC0
TAMIL SYLLABLE KU; 0B95 0BC1
TAMIL SYLLABLE KUU; 0B95 0BC2
TAMIL SYLLABLE KE; 0B95 0BC6
TAMIL SYLLABLE KEE; 0B95 0BC7
TAMIL SYLLABLE KAI; 0B95 0BC8
TAMIL SYLLABLE KO; 0B95 0BCA
TAMIL SYLLABLE KOO; 0B95 0BCB
TAMIL SYLLABLE KAU; 0B95 0BCC
TAMIL SYLLABLE NGAA; 0B99 0BBE
TAMIL SYLLABLE NGI; 0B99 0BBF
TAMIL SYLLABLE NGII; 0B99 0BC0
TAMIL SYLLABLE NGU; 0B99 0BC1
TAMIL SYLLABLE NGUU; 0B99 0BC2
TAMIL SYLLABLE NGE; 0B99 0BC6
TAMIL SYLLABLE NGEE; 0B99 0BC7
TAMIL SYLLABLE NGAI; 0B99 0BC8
TAMIL SYLLABLE NGO; 0B99 0BCA
TAMIL SYLLABLE NGOO; 0B99 0BCB
TAMIL SYLLABLE NGAU; 0B99 0BCC
TAMIL SYLLABLE CI; 0B9A 0BBF
TAMIL SYLLABLE CII; 0B9A 0BC0
TAMIL SYLLABLE CU; 0B9A 0BC1
TAMIL SYLLABLE CUU; 0B9A 0BC2
TAMIL SYLLABLE CE; 0B9A 0BC6
TAMIL SYLLABLE CEE; 0B9A 0BC7
TAMIL SYLLABLE CAI; 0B9A 0BC8
TAMIL SYLLABLE CO; 0B9A 0BCA
TAMIL SYLLABLE COO; 0B9A 0BCB
TAMIL SYLLABLE CAU; 0B9A 0BCC
TAMIL SYLLABLE NYAA; 0B9E 0BBE
TAMIL SYLLABLE NYI; 0B9E 0BBF
TAMIL SYLLABLE NYII; 0B9E 0BC0
TAMIL SYLLABLE NYU; 0B9E 0BC1
TAMIL SYLLABLE NYUU; 0B9E 0BC2
TAMIL SYLLABLE NYE; 0B9E 0BC6
TAMIL SYLLABLE NYEE; 0B9E 0BC7
TAMIL SYLLABLE NYAI; 0B9E 0BC8
TAMIL SYLLABLE NYO; 0B9E 0BCA
TAMIL SYLLABLE NYOO; 0B9E 0BCB
TAMIL SYLLABLE NYAU; 0B9E 0BCC
TAMIL SYLLABLE TTAA; 0B9F 0BBE
TAMIL SYLLABLE TTI; 0B9F 0BBF
TAMIL SYLLABLE TTII; 0B9F 0BC0
TAMIL SYLLABLE TTU; 0B9F 0BC1
TAMIL SYLLABLE TTUU; 0B9F 0BC2
TAMIL SYLLABLE TTE; 0B9F 0BC6
TAMIL SYLLABLE TTEE; 0B9F 0BC7
TAMIL SYLLABLE TTAI; 0B9F 0BC8
TAMIL SYLLABLE TTO; 0B9F 0BCA
TAMIL SYLLABLE TTOO; 0B9F 0BCB
TAMIL SYLLABLE TTAU; 0B9F 0BCC
TAMIL SYLLABLE NNAA; 0BA3 0BBE
TAMIL SYLLABLE NNI; 0BA3 0BBF
TAMIL SYLLABLE NNII; 0BA3 0BC0
TAMIL SYLLABLE NNU; 0BA3 0BC1
TAMIL SYLLABLE NNUU; 0BA3 0BC2
TAMIL SYLLABLE NNE; 0BA3 0BC6
TAMIL SYLLABLE NNEE; 0BA3 0BC7
TAMIL SYLLABLE NNAI; 0BA3 0BC8
TAMIL SYLLABLE NNO; 0BA3 0BCA
TAMIL SYLLABLE NNOO; 0BA3 0BCB
TAMIL SYLLABLE NNAU; 0BA3 0BCC
TAMIL SYLLABLE TAA; 0BA4 0BBE
TAMIL SYLLABLE TI; 0BA4 0BBF
TAMIL SYLLABLE TII; 0BA4 0BC0
TAMIL SYLLABLE TU; 0BA4 0BC1
TAMIL SYLLABLE TUU; 0BA4 0BC2
TAMIL SYLLABLE TE; 0BA4 0BC6
TAMIL SYLLABLE TEE; 0BA4 0BC7
TAMIL SYLLABLE TAI; 0BA4 0BC8
TAMIL SYLLABLE TO; 0BA4 0BCA
TAMIL SYLLABLE TOO; 0BA4 0BCB
TAMIL SYLLABLE TAU; 0BA4 0BCC
TAMIL SYLLABLE NAA; 0BA8 0BBE
TAMIL SYLLABLE NI; 0BA8 0BBF
TAMIL SYLLABLE NII; 0BA8 0BC0
TAMIL SYLLABLE NU; 0BA8 0BC1
TAMIL SYLLABLE NUU; 0BA8 0BC2
TAMIL SYLLABLE NE; 0BA8 0BC6
TAMIL SYLLABLE NEE; 0BA8 0BC7
TAMIL SYLLABLE NAI; 0BA8 0BC8
TAMIL SYLLABLE NO; 0BA8 0BCA
TAMIL SYLLABLE NOO; 0BA8 0BCB
TAMIL SYLLABLE NAU; 0BA8 0BCC
TAMIL SYLLABLE PAA; 0BAA 0BBE
TAMIL SYLLABLE PI; 0BAA 0BBF
TAMIL SYLLABLE PII; 0BAA 0BC0
TAMIL SYLLABLE PU; 0BAA 0BC1
TAMIL SYLLABLE PUU; 0BAA 0BC2
TAMIL SYLLABLE PE; 0BAA 0BC6
TAMIL SYLLABLE PEE; 0BAA 0BC7
TAMIL SYLLABLE PAI; 0BAA 0BC8
TAMIL SYLLABLE PO; 0BAA 0BCA
TAMIL SYLLABLE POO; 0BAA 0BCB
TAMIL SYLLABLE PAU; 0BAA 0BCC
TAMIL SYLLABLE MAA; 0BAE 0BBE
TAMIL SYLLABLE MI; 0BAE 0BBF
TAMIL SYLLABLE MII; 0BAE 0BC0
TAMIL SYLLABLE MU; 0BAE 0BC1
TAMIL SYLLABLE MUU; 0BAE 0BC2
TAMIL SYLLABLE ME; 0BAE 0BC6
TAMIL SYLLABLE MEE; 0BAE 0BC7
TAMIL SYLLABLE MAI; 0BAE 0BC8
TAMIL SYLLABLE MO; 0BAE 0BCA
TAMIL SYLLABLE MOO; 0BAE 0BCB
TAMIL SYLLABLE MAU; 0BAE 0BCC
TAMIL SYLLABLE YAA; 0BAF 0BBE
TAMIL SYLLABLE YI; 0BAF 0BBF
TAMIL SYLLABLE YII; 0BAF 0BC0
TAMIL SYLLABLE YU; 0BAF 0BC1
TAMIL SYLLABLE YUU; 0BAF 0BC2
TAMIL SYLLABLE YE; 0BAF 0BC6
TAMIL SYLLABLE YEE; 0BAF 0BC7
TAMIL SYLLABLE YAI; 0BAF 0BC8
TAMIL SYLLABLE YO; 0BAF 0BCA
TAMIL SYLLABLE YOO; 0BAF 0BCB
TAMIL SYLLABLE YAU; 0BAF 0BCC
TAMIL SYLLABLE RAA; 0BB0 0BBE
TAMIL SYLLABLE RI; 0BB0 0BBF
TAMIL SYLLABLE RII; 0BB0 0BC0
TAMIL SYLLABLE RU; 0BB0 0BC1
TAMIL SYLLABLE RUU; 0BB0 0BC2
TAMIL SYLLABLE RE; 0BB0 0BC6
TAMIL SYLLABLE REE; 0BB0 0BC7
TAMIL SYLLABLE RAI; 0BB0 0BC8
TAMIL SYLLABLE RO; 0BB0 0BCA
TAMIL SYLLABLE ROO; 0BB0 0BCB
TAMIL SYLLABLE RAU; 0BB0 0BCC
TAMIL SYLLABLE LAA; 0BB2 0BBE
TAMIL SYLLABLE LI; 0BB2 0BBF
TAMIL SYLLABLE LII; 0BB2 0BC0
TAMIL SYLLABLE LU; 0BB2 0BC1
TAMIL SYLLABLE LUU; 0BB2 0BC2
TAMIL SYLLABLE LE; 0BB2 0BC6
TAMIL SYLLABLE LEE; 0BB2 0BC7
TAMIL SYLLABLE LAI; 0BB2 0BC8
TAMIL SYLLABLE LO; 0BB2 0BCA
TAMIL SYLLABLE LOO; 0BB2 0BCB
TAMIL SYLLABLE LAU; 0BB2 0BCC
TAMIL SYLLABLE VAA; 0BB5 0BBE
TAMIL SYLLABLE VI; 0BB5 0BBF
TAMIL SYLLABLE VII; 0BB5 0BC0
TAMIL SYLLABLE VU; 0BB5 0BC1
TAMIL SYLLABLE VUU; 0BB5 0BC2
TAMIL SYLLABLE VE; 0BB5 0BC6
TAMIL SYLLABLE VEE; 0BB5 0BC7
TAMIL SYLLABLE VAI; 0BB5 0BC8
TAMIL SYLLABLE VO; 0BB5 0BCA
TAMIL SYLLABLE VOO; 0BB5 0BCB
TAMIL SYLLABLE VAU; 0BB5 0BCC
TAMIL SYLLABLE LLLAA; 0BB4 0BBE
TAMIL SYLLABLE LLLI; 0BB4 0BBF
TAMIL SYLLABLE LLLII; 0BB4 0BC0
TAMIL SYLLABLE LLLU; 0BB4 0BC1
TAMIL SYLLABLE LLLUU; 0BB4 0BC2
TAMIL SYLLABLE LLLE; 0BB4 0BC6
TAMIL SYLLABLE LLLEE; 0BB4 0BC7
TAMIL SYLLABLE LLLAI; 0BB4 0BC8
TAMIL SYLLABLE LLLO; 0BB4 0BCA
TAMIL SYLLABLE LLLOO; 0BB4 0BCB
TAMIL SYLLABLE LLLAU; 0BB4 0BCC
TAMIL SYLLABLE LLAA; 0BB3 0BBE
TAMIL SYLLABLE LLI; 0BB3 0BBF
TAMIL SYLLABLE LLII; 0BB3 0BC0
TAMIL SYLLABLE LLU; 0BB3 0BC1
TAMIL SYLLABLE LLUU; 0BB3 0BC2
TAMIL SYLLABLE LLE; 0BB3 0BC6
TAMIL SYLLABLE LLEE; 0BB3 0BC7
TAMIL SYLLABLE LLAI; 0BB3 0BC8
TAMIL SYLLABLE LLO; 0BB3 0BCA
TAMIL SYLLABLE LLOO; 0BB3 0BCB
TAMIL SYLLABLE LLAU; 0BB3 0BCC
TAMIL SYLLABLE RRAA; 0BB1 0BBE
TAMIL SYLLABLE RRI; 0BB1 0BBF
TAMIL SYLLABLE RRII; 0BB1 0BC0
TAMIL SYLLABLE RRU; 0BB1 0BC1
TAMIL SYLLABLE RRUU; 0BB1 0BC2
TAMIL SYLLABLE RRE; 0BB1 0BC6
TAMIL SYLLABLE RREE; 0BB1 0BC7
TAMIL SYLLABLE RRAI; 0BB1 0BC8
TAMIL SYLLABLE RRO; 0BB1 0BCA
TAMIL SYLLABLE RROO; 0BB1 0BCB
TAMIL SYLLABLE RRAU; 0BB1 0BCC
TAMIL SYLLABLE NNNAA; 0BA9 0BBE
TAMIL SYLLABLE NNNI; 0BA9 0BBF
TAMIL SYLLABLE NNNII; 0BA9 0BC0
TAMIL SYLLABLE NNNU; 0BA9 0BC1
TAMIL SYLLABLE NNNUU; 0BA9 0BC2
TAMIL SYLLABLE NNNE; 0BA9 0BC6
TAMIL SYLLABLE NNNEE; 0BA9 0BC7
TAMIL SYLLABLE NNNAI; 0BA9 0BC8
TAMIL SYLLABLE NNNO; 0BA9 0BCA
TAMIL SYLLABLE NNNOO; 0BA9 0BCB
TAMIL SYLLABLE NNNAU; 0BA9 0BCC
TAMIL SYLLABLE JAA; 0B9C 0BBE
TAMIL SYLLABLE JI; 0B9C 0BBF
TAMIL SYLLABLE JII; 0B9C 0BC0
TAMIL SYLLABLE JU; 0B9C 0BC1
TAMIL SYLLABLE JUU; 0B9C 0BC2
TAMIL SYLLABLE JE; 0B9C 0BC6
TAMIL SYLLABLE JEE; 0B9C 0BC7
TAMIL SYLLABLE JAI; 0B9C 0BC8
TAMIL SYLLABLE JO; 0B9C 0BCA
TAMIL SYLLABLE JOO; 0B9C 0BCB
TAMIL SYLLABLE JAU; 0B9C 0BCC
TAMIL SYLLABLE SHAA; 0BB6 0BBE
TAMIL SYLLABLE SHI; 0BB6 0BBF
TAMIL SYLLABLE SHII; 0BB6 0BC0
TAMIL SYLLABLE SHU; 0BB6 0BC1
TAMIL SYLLABLE SHUU; 0BB6 0BC2
TAMIL SYLLABLE SHE; 0BB6 0BC6
TAMIL SYLLABLE SHEE; 0BB6 0BC7
TAMIL SYLLABLE SHAI; 0BB6 0BC8
TAMIL SYLLABLE SHO; 0BB6 0BCA
TAMIL SYLLABLE SHOO; 0BB6 0BCB
TAMIL SYLLABLE SHAU; 0BB6 0BCC
TAMIL SYLLABLE SSAA; 0BB7 0BBE
TAMIL SYLLABLE SSI; 0BB7 0BBF
TAMIL SYLLABLE SSII; 0BB7 0BC0
TAMIL SYLLABLE SSU; 0BB7 0BC1
TAMIL SYLLABLE SSUU; 0BB7 0BC2
TAMIL SYLLABLE SSE; 0BB7 0BC6
TAMIL SYLLABLE SSEE; 0BB7 0BC7
TAMIL SYLLABLE SSAI; 0BB7 0BC8
TAMIL SYLLABLE SSO; 0BB7 0BCA
TAMIL SYLLABLE SSOO; 0BB7 0BCB
TAMIL SYLLABLE SSAU; 0BB7 0BCC
TAMIL SYLLABLE SAA; 0BB8 0BBE
TAMIL SYLLABLE SI; 0BB8 0BBF
TAMIL SYLLABLE SII; 0BB8 0BC0
TAMIL SYLLABLE SU; 0BB8 0BC1
TAMIL SYLLABLE SUU; 0BB8 0BC2
TAMIL SYLLABLE SE; 0BB8 0BC6
TAMIL SYLLABLE SEE; 0BB8 0BC7
TAMIL SYLLABLE SAI; 0BB8 0BC8
TAMIL SYLLABLE SO; 0BB8 0BCA
TAMIL SYLLABLE SOO; 0BB8 0BCB
TAMIL SYLLABLE SAU; 0BB8 0BCC
TAMIL SYLLABLE HAA; 0BB9 0BBE
TAMIL SYLLABLE HI; 0BB9 0BBF
TAMIL SYLLABLE HII; 0BB9 0BC0
TAMIL SYLLABLE HU; 0BB9 0BC1
TAMIL SYLLABLE HUU; 0BB9 0BC2
TAMIL SYLLABLE HE; 0BB9 0BC6
TAMIL SYLLABLE HEE; 0BB9 0BC7
TAMIL SYLLABLE HAI; 0BB9 0BC8
TAMIL SYLLABLE HO; 0BB9 0BCA
TAMIL SYLLABLE HOO; 0BB9 0BCB
TAMIL SYLLABLE HAU; 0BB9 0BCC
TAMIL SYLLABLE KSSA; 0B95 0BCD 0BB7
TAMIL SYLLABLE KSSAA; 0B95 0BCD 0BB7 0BBE
TAMIL SYLLABLE KSSI; 0B95 0BCD 0BB7 0BBF
TAMIL SYLLABLE KSSII; 0B95 0BCD 0BB7 0BC0
TAMIL SYLLABLE KSSU; 0B95 0BCD 0BB7 0BC1
TAMIL SYLLABLE KSSUU; 0B95 0BCD 0BB7 0BC2
TAMIL SYLLABLE KSSE; 0B95 0BCD 0BB7 0BC6
TAMIL SYLLABLE KSSEE; 0B95 0BCD 0BB7 0BC7
TAMIL SYLLABLE KSSAI; 0B95 0BCD 0BB7 0BC8
TAMIL SYLLABLE KSSO; 0B95 0BCD 0BB7 0BCA
TAMIL SYLLABLE KSSOO; 0B95 0BCD 0BB7 0BCB
TAMIL SYLLABLE KSSAU; 0B95 0BCD 0BB7 0BCC
TAMIL SYLLABLE SHRII; 0BB6 0BCD 0BB0 0BC0
SINHALA CONSONANT SIGN YANSAYA;0DCA 200D 0DBA
SINHALA CONSONANT SIGN RAKAARAANSAYA;0DCA 200D 0DBB
SINHALA CONSONANT SIGN REPAYA;0DBB 0DCA 200D
GEORGIAN LETTER U-BRJGU;10E3 0302
KHMER CONSONANT SIGN COENG KA;17D2 1780
KHMER CONSONANT SIGN COENG KHA;17D2 1781
KHMER CONSONANT SIGN COENG KO;17D2 1782
KHMER CONSONANT SIGN COENG KHO;17D2 1783
KHMER CONSONANT SIGN COENG NGO;17D2 1784
KHMER CONSONANT SIGN COENG CA;17D2 1785
KHMER CONSONANT SIGN COENG CHA;17D2 1786
KHMER CONSONANT SIGN COENG CO;17D2 1787
KHMER CONSONANT SIGN COENG CHO;17D2 1788
KHMER CONSONANT SIGN COENG NYO;17D2 1789
KHMER CONSONANT SIGN COENG DA;17D2 178A
KHMER CONSONANT SIGN COENG TTHA;17D2 178B
KHMER CONSONANT SIGN COENG DO;17D2 178C
KHMER CONSONANT SIGN COENG TTHO;17D2 178D
KHMER CONSONANT SIGN COENG NA;17D2 178E
KHMER CONSONANT SIGN COENG TA;17D2 178F
KHMER CONSONANT SIGN COENG THA;17D2 1790
KHMER CONSONANT SIGN COENG TO;17D2 1791
KHMER CONSONANT SIGN COENG THO;17D2 1792
KHMER CONSONANT SIGN COENG NO;17D2 1793
KHMER CONSONANT SIGN COENG BA;17D2 1794
KHMER CONSONANT SIGN COENG PHA;17D2 1795
KHMER CONSONANT SIGN COENG PO;17D2 1796
KHMER CONSONANT SIGN COENG PHO;17D2 1797
KHMER CONSONANT SIGN COENG MO;17D2 1798
KHMER CONSONANT SIGN COENG YO;17D2 1799
KHMER CONSONANT SIGN COENG RO;17D2 179A
KHMER CONSONANT SIGN COENG LO;17D2 179B
KHMER CONSONANT SIGN COENG VO;17D2 179C
KHMER CONSONANT SIGN COENG SHA;17D2 179D
KHMER CONSONANT SIGN COENG SSA;17D2 179E
KHMER CONSONANT SIGN COENG SA;17D2 179F
KHMER CONSONANT SIGN COENG HA;17D2 17A0
KHMER CONSONANT SIGN COENG LA;17D2 17A1
KHMER VOWEL SIGN COENG QA;17D2 17A2
KHMER INDEPENDENT VOWEL SIGN COENG QU;17D2 17A7
KHMER INDEPENDENT VOWEL SIGN COENG RY;17D2 17AB
KHMER INDEPENDENT VOWEL SIGN COENG RYY;17D2 17AC
KHMER INDEPENDENT VOWEL SIGN COENG QE;17D2 17AF
KHMER VOWEL SIGN OM;17BB 17C6
KHMER VOWEL SIGN AAM;17B6 17C6
HIRAGANA LETTER BIDAKUON NGA;304B 309A
HIRAGANA LETTER BIDAKUON NGI;304D 309A
HIRAGANA LETTER BIDAKUON NGU;304F 309A
HIRAGANA LETTER BIDAKUON NGE;3051 309A
HIRAGANA LETTER BIDAKUON NGO;3053 309A
KATAKANA LETTER BIDAKUON NGA;30AB 309A
KATAKANA LETTER BIDAKUON NGI;30AD 309A
KATAKANA LETTER BIDAKUON NGU;30AF 309A
KATAKANA LETTER BIDAKUON NGE;30B1 309A
KATAKANA LETTER BIDAKUON NGO;30B3 309A
KATAKANA LETTER AINU CE;30BB 309A
KATAKANA LETTER AINU TU;30C4 309A
KATAKANA LETTER AINU TO;30C8 309A
KATAKANA LETTER AINU P;31F7 309A
MODIFIER LETTER EXTRA-HIGH EXTRA-LOW CONTOUR TONE BAR;02E5 02E9
MODIFIER LETTER EXTRA-LOW EXTRA-HIGH CONTOUR TONE BAR;02E9 02E5
File diff suppressed because it is too large Load Diff
+170
View File
@@ -0,0 +1,170 @@
# This file has its own compilation step because
# it needs to parse String.Unicode data and
# compile a digested module.
defmodule String.Unicode do
@moduledoc false
def version, do: {6,2,0}
to_binary = fn(codepoint) ->
:unicode.characters_to_binary([binary_to_integer(codepoint, 16)])
end
data_path = File.expand_path("../UnicodeData.txt", __FILE__)
{ codes, whitespace } = Enum.reduce File.iterator!(data_path), { [], [] }, fn(line, { cacc, wacc }) ->
[ codepoint, _name, _category,
_class, bidi, _decomposition,
_numeric_1, _numeric_2, _numeric_3,
_bidi_mirror, _unicode_1, _iso,
upper, lower, _title ] = :binary.split(line, ";", [:global])
cond do
upper != "" or lower != "" ->
{ [{ to_binary.(codepoint), upper, lower } | cacc], wacc }
bidi in ["B", "S", "WS"] ->
{ cacc, [to_binary.(codepoint) | wacc] }
true ->
{ cacc, wacc }
end
end
seqs_path = File.expand_path("../NamedSequences.txt", __FILE__)
seqs = Enum.map File.iterator!(seqs_path), fn(line) ->
[ _name, codepoints ] = :binary.split(line, ";", [:global])
codepoints = :binary.split(codepoints, " ", [:global])
codepoints = Enum.map codepoints, Regex.replace(%r/\s+/, &1, "")
codepoints = Enum.filter codepoints, fn(x) -> size(x) > 0 end
Enum.map codepoints, to_binary.(&1)
end
# Downcase
lc { codepoint, _upper, lower } inlist codes, lower != "" do
lower = to_binary.(lower)
args = quote do: [unquote(codepoint) <> t]
code = quote do: unquote(lower) <> downcase(t)
def :downcase, args, [], do: code
end
def downcase(<< h, t :: binary >>) do
<< h >> <> downcase(t)
end
def downcase(<< >>) do
<< >>
end
# Upcase
lc { codepoint, upper, _lower } inlist codes, upper != "" do
upper = to_binary.(upper)
args = quote do: [unquote(codepoint) <> t]
code = quote do: unquote(upper) <> upcase(t)
def :upcase, args, [], do: code
end
def upcase(<< h, t :: binary >>) do
<< h >> <> upcase(t)
end
def upcase(<< >>) do
<< >>
end
# Strip
def lstrip(""), do: ""
lc char inlist whitespace do
args = quote do: [unquote(char) <> rest]
exprs = quote do: lstrip(rest)
def :lstrip, args, [], do: exprs
end
def lstrip(other), do: other
def rstrip(""), do: ""
def rstrip(string) do
do_rstrip(string, "")
end
lc char inlist whitespace do
args = quote do: [unquote(char) <> rest, buffer]
exprs = quote do: do_rstrip(rest, unquote(char) <> buffer)
defp :do_rstrip, args, [], do: exprs
end
defp do_rstrip(<< char, string :: binary >>, buffer) do
<< buffer :: binary, char, do_rstrip(string, "") :: binary >>
end
defp do_rstrip(<<>>, _), do: <<>>
# Graphemes
lc codepoints inlist seqs do
seq_args = quote do: [<< unquote_splicing(codepoints), t :: binary >>]
seq_code = quote do: {<< unquote_splicing(codepoints) >>, t}
def :next_grapheme, seq_args, [], do: seq_code
end
def next_grapheme(<<>>) do
:no_grapheme
end
def next_grapheme(binary) when is_binary(binary) do
case next_codepoint(binary) do
:no_codepoint -> :no_grapheme
other -> other
end
end
def graphemes(binary) when is_binary(binary) do
do_graphemes(next_grapheme(binary))
end
defp do_graphemes({ c, rest }) do
[c|do_graphemes(next_grapheme(rest))]
end
defp do_graphemes(:no_grapheme) do
[]
end
# Codepoints
def next_codepoint(<<194, char, rest :: binary>>)
when char in 161..191,
do: { <<194, char>>, rest }
def next_codepoint(<<first, char, rest :: binary>>)
when first in 195..223 and char in 128..191,
do: { <<first, char>>, rest }
def next_codepoint(<<first, second, char, rest :: binary>>)
when first == 224 and second in 160..191 and char in 128..191,
do: { <<first, second, char>>, rest }
def next_codepoint(<<first, second, char, rest :: binary>>)
when first in 225..239 and second in 128..191 and char in 128..191,
do: { <<first, second, char>>, rest }
def next_codepoint(<<other, rest :: binary>>), do: { <<other>>, rest }
def next_codepoint(<<>>), do: :no_codepoint
def codepoints(binary) when is_binary(binary) do
do_codepoints(next_codepoint(binary))
end
defp do_codepoints({ c, rest }) do
[c|do_codepoints(next_codepoint(rest))]
end
defp do_codepoints(:no_codepoint) do
[]
end
end
+1 -1
View File
@@ -16,7 +16,7 @@
%% warn_missing_spec,
%% warn_untyped_record,
%% warnings_as_errors,
no_debug_info
debug_info
]}.
{yrl_opts, [
+53 -34
View File
@@ -1,21 +1,27 @@
-module(elixir).
-behaviour(application).
-export([main/1, start_cli/0, start_app/0,
-export([main/1, start_cli/0,
scope_for_eval/1, eval/2, eval/3, eval/4,
eval_quoted/2, eval_quoted/3, eval_quoted/4,
eval_forms/3]).
-include("elixir.hrl").
-compile({parse_transform, elixir_transform}).
%% Top level types
-export_type([char_list/0]).
-type char_list() :: string().
% OTP APPLICATION API
-export([start/2, stop/1, config_change/3]).
start(_Type, _Args) ->
%% Set the shell to unicode so printing inside files work
io:setopts(standard_io, [{encoding,unicode}]),
io:setopts(standard_error, [{encoding,unicode}]),
%% Set the shell to unicode so printing inside scripts work
%% Those can take a while, so let's do it in a new process
spawn(fun() ->
io:setopts(standard_io, [{encoding,unicode}]),
io:setopts(standard_error, [{encoding,unicode}])
end),
elixir_sup:start_link([]).
stop(_S) ->
@@ -27,40 +33,52 @@ config_change(_Changed, _New, _Remove) ->
%% escript entry point
main(Args) ->
start_app(),
'Elixir.Kernel.CLI':process_argv(Args).
%% ELIXIR ENTRY POINTS
% Start the Elixir app. This is the proper way to boot Elixir from
% inside an Erlang process.
start_app() ->
case lists:keyfind(?MODULE, 1, application:loaded_applications()) of
false -> application:start(?MODULE);
_ -> ok
end.
application:start(?MODULE),
'Elixir.Kernel.CLI':main(Args).
% Boot and process given options. Invoked by Elixir's script.
start_cli() ->
start_app(),
'Elixir.Kernel.CLI':process_argv(init:get_plain_arguments()).
application:start(?MODULE),
'Elixir.Kernel.CLI':main(init:get_plain_arguments()).
%% EVAL HOOKS
scope_for_eval(Opts) ->
File = case lists:keyfind(file, 1, Opts) of
{ file, F } -> to_binary(F);
false -> <<"nofile">>
case lists:keyfind(file, 1, Opts) of
{ file, RawFile } -> File = to_binary(RawFile);
false -> File = <<"nofile">>
end,
Local = case lists:keyfind(delegate_locals_to, 1, Opts) of
{ delegate_locals_to, L } -> L;
false -> nil
case lists:keyfind(delegate_locals_to, 1, Opts) of
{ delegate_locals_to, Local } -> Local;
false -> Local = nil
end,
#elixir_scope{file=File,local=Local}.
case lists:keyfind(aliases, 1, Opts) of
{ aliases, Aliases } -> Aliases;
false -> Aliases = []
end,
case lists:keyfind(requires, 1, Opts) of
{ requires, Requires } -> Requires;
false -> Requires = elixir_dispatch:default_requires()
end,
case lists:keyfind(functions, 1, Opts) of
{ functions, Functions } -> Functions;
false -> Functions = elixir_dispatch:default_functions()
end,
case lists:keyfind(macros, 1, Opts) of
{ macros, Macros } -> Macros;
false -> Macros = elixir_dispatch:default_macros()
end,
#elixir_scope{
file=File, local=Local,
macros=Macros, functions=Functions,
requires=Requires, aliases=Aliases }.
%% String evaluation
@@ -99,9 +117,8 @@ eval_forms(Tree, Binding, RawScope) ->
Scope = RawScope#elixir_scope{
vars=binding_dict(Binding),
temp_vars=[],
quote_vars=[],
clause_vars=[],
counter=0
clause_vars=nil,
counter=[]
},
{ ParseTree, NewScope } = elixir_translator:translate(Tree, Scope),
case ParseTree of
@@ -117,15 +134,17 @@ to_binary(Bin) when is_binary(Bin) -> Bin;
to_binary(List) when is_list(List) -> list_to_binary(List).
binding_dict(List) -> binding_dict(List, orddict:new()).
binding_dict([{H,_}|T], Dict) -> binding_dict(T, orddict:store(H, H, Dict));
binding_dict([{{H,Kind},_}|T], Dict) -> binding_dict(T, orddict:store({ H, Kind }, H, Dict));
binding_dict([{H,_}|T], Dict) -> binding_dict(T, orddict:store({ H, nil }, H, Dict));
binding_dict([], Dict) -> Dict.
final_binding(Binding, Vars) -> final_binding(Binding, [], Binding, Vars).
final_binding([{Var,_}|T], Acc, Binding, Vars) ->
case atom_to_list(Var) of
"_@" ++ _ -> final_binding(T, Acc, Binding, Vars);
_ ->
RealName = orddict:fetch(Var, Vars),
case lists:member($@, atom_to_list(Var)) of
true ->
final_binding(T, Acc, Binding, Vars);
false ->
RealName = orddict:fetch({ Var, nil }, Vars),
RealValue = proplists:get_value(RealName, Binding, nil),
final_binding(T, [{Var, RealValue}|Acc], Binding, Vars)
end;
+32 -20
View File
@@ -75,7 +75,8 @@ extract_or_clauses(Term, Acc) -> [Term|Acc].
extract_args({ { '.', _, [Name] }, _, Args }) when is_atom(Name), is_list(Args) -> { Name, Args };
extract_args({ Name, _, Args }) when is_atom(Name), is_atom(Args) -> { Name, [] };
extract_args({ Name, _, Args }) when is_atom(Name), is_list(Args) -> { Name, Args }.
extract_args({ Name, _, Args }) when is_atom(Name), is_list(Args) -> { Name, Args };
extract_args(_) -> error.
% Extract guards when it is in the last element of the args
@@ -87,9 +88,12 @@ extract_last_guards(Args) ->
% Function for translating macros with match style like case and receive.
match(Line, DecoupledClauses, RawS) ->
S = RawS#elixir_scope{clause_vars=orddict:new()},
match(Line, Clauses, #elixir_scope{clause_vars=C1} = S) ->
{ TC, TS } = do_match(Line, Clauses, S#elixir_scope{clause_vars=orddict:new()}),
C2 = TS#elixir_scope.clause_vars,
{ TC, TS#elixir_scope{clause_vars=elixir_scope:merge_clause_vars(C1, C2)} }.
do_match(Line, DecoupledClauses, S) ->
case DecoupledClauses of
[DecoupledClause] ->
{ TDecoupledClause, TS } = each_clause(Line, DecoupledClause, S),
@@ -106,9 +110,11 @@ match(Line, DecoupledClauses, RawS) ->
% Now get all the variables defined inside each clause
CV = lists:reverse(RawCV),
NewVars = lists:umerge([lists:sort(orddict:fetch_keys(X)) || X <- CV]),
case NewVars of
AllVars = lists:umerge([orddict:fetch_keys(X) || X <- CV]),
SharedVars = ordsets:intersection(CV),
case AllVars of
[] -> { TClauses, TS };
_ ->
% Create a new scope that contains a list of all variables
@@ -116,7 +122,7 @@ match(Line, DecoupledClauses, RawS) ->
% a list of tuples where the first element is the variable name,
% the second one is the new pointer to the variable and the third
% is the old pointer.
{ FinalVars, FS } = lists:mapfoldl(fun normalize_vars/2, TS, NewVars),
{ FinalVars, FS } = lists:mapfoldl(fun(X, Acc) -> normalize_vars(X, SharedVars, Acc) end, TS, AllVars),
% Defines a tuple that will be used as left side of the match operator
LeftVars = [{var, Line, NewValue} || { _, _, NewValue, _ } <- FinalVars],
@@ -128,7 +134,8 @@ match(Line, DecoupledClauses, RawS) ->
end.
expand_clauses(Line, [Clause|T], [ClauseVars|V], LeftVars, FinalVars, Acc, S) ->
RightVars = [normalize_clause_var(Var, Kind, OldValue, ClauseVars) || { Var, Kind, _, OldValue } <- FinalVars],
RightVars = [normalize_clause_var(Var, Kind, OldValue, ClauseVars) ||
{ Var, Kind, _, OldValue } <- FinalVars],
AssignExpr = generate_match(Line, LeftVars, RightVars),
ClauseExprs = element(5, Clause),
@@ -199,25 +206,30 @@ has_match_tuple(_) -> false.
% Normalize the given var checking its existence in the scope var dictionary.
normalize_vars({ Var, quoted }, S) ->
normalize_vars(Var, quoted, #elixir_scope.quote_vars, S);
normalize_vars({ Var, Kind } = Key, Shared, #elixir_scope{vars=Vars,clause_vars=ClauseVars} = S) ->
{ NewValue, S1 } =
case orddict:find(Key, Shared) of
{ ok, SharedValue } ->
{ SharedValue, S };
error ->
{ { _, _, ErlValue }, ErlS } = case (Kind == quoted) or (S#elixir_scope.noname) of
true -> elixir_scope:build_erl_var(0, S);
false -> elixir_scope:build_erl_var(0, Var, "_@" ++ atom_to_list(Var), S)
end,
{ ErlValue, ErlS }
end,
normalize_vars({ Var, Kind }, S) ->
normalize_vars(Var, Kind, #elixir_scope.vars, S).
S2 = S1#elixir_scope{
vars=orddict:store(Key, NewValue, Vars),
clause_vars=orddict:store(Key, NewValue, ClauseVars)
},
normalize_vars(Var, Kind, Index, #elixir_scope{clause_vars=ClauseVars} = S) ->
Vars = element(Index, S),
{ { _, _, NewValue }, S1 } = elixir_scope:build_erl_var(0, S),
S2 = setelement(Index, S1, orddict:store(Var, NewValue, Vars)),
S3 = S2#elixir_scope{clause_vars=orddict:store({ Var, Kind }, NewValue, ClauseVars)},
Expr = case orddict:find(Var, Vars) of
Expr = case orddict:find(Key, Vars) of
{ ok, OldValue } -> { var, 0, OldValue };
error -> { atom, 0, nil }
end,
{ { Var, Kind, NewValue, Expr }, S3 }.
{ { Var, Kind, NewValue, Expr }, S2 }.
% Normalize a var by checking if it was defined in the clause.
% If so, use it, otherwise use from main scope.
+35 -6
View File
@@ -6,16 +6,25 @@
argv=[],
loaded=[],
at_exit=[],
compiler_options=[{docs,true}]
pool=[],
counter=0,
compiler_options=[{docs,true},{debug_info,true}],
waiting=[]
}).
start_link() ->
{ ok, _ } = gen_server:start_link({local, elixir_code_server}, ?MODULE, [], []).
init(_args) ->
process_flag(trap_exit, true),
{ ok, #elixir_code_server{} }.
handle_call({ wait_until_finished, Pid }, _, Config) ->
Waiting = Config#elixir_code_server.waiting,
case is_list(Waiting) of
true -> { reply, wait, Config#elixir_code_server{waiting=[Pid|Waiting]} };
false -> { reply, ok, Config }
end;
handle_call({ acquire, Path }, From, Config) ->
Current = Config#elixir_code_server.loaded,
case orddict:find(Path, Current) of
@@ -45,15 +54,32 @@ handle_call(loaded, _From, Config) ->
handle_call(at_exit, _From, Config) ->
{ reply, Config#elixir_code_server.at_exit, Config };
handle_call(flush_at_exit, _From, Config) ->
{ reply, Config#elixir_code_server.at_exit, Config#elixir_code_server{at_exit=[]} };
handle_call(argv, _From, Config) ->
{ reply, Config#elixir_code_server.argv, Config };
handle_call(compiler_options, _From, Config) ->
{ reply, Config#elixir_code_server.compiler_options, Config };
handle_call(retrieve_module_name, _From, Config) ->
case Config#elixir_code_server.pool of
[H|T] ->
{ reply, module_tuple(H), Config#elixir_code_server{pool=T} };
[] ->
Counter = Config#elixir_code_server.counter,
{ reply, module_tuple(Counter), Config#elixir_code_server{counter=Counter+1} }
end;
handle_call(_Request, _From, Config) ->
{ reply, undef, Config }.
handle_cast(finished, Config) ->
Waiting = Config#elixir_code_server.waiting,
[Pid ! { elixir_code_server, finished } || Pid <- lists:reverse(Waiting)],
{ noreply, Config#elixir_code_server{waiting=done} };
handle_cast({ loaded, Path }, Config) ->
Current = Config#elixir_code_server.loaded,
case orddict:find(Path, Current) of
@@ -73,16 +99,19 @@ handle_cast({ unload_files, Files }, Config) ->
Unloaded = lists:foldl(fun(File, Acc) -> orddict:erase(File, Acc) end, Current, Files),
{ noreply, Config#elixir_code_server{loaded=Unloaded} };
handle_cast({ return_module_name, I }, #elixir_code_server{pool=Pool} = Config) ->
{ noreply, Config#elixir_code_server{pool=[I|Pool]} };
handle_cast(_Request, Config) ->
{ noreply, Config }.
handle_info(_Request, Config) ->
{ noreply, Config }.
terminate(Reason, Config) ->
io:format("[FATAL] ~p crashed:\n~p~n", [?MODULE, Reason]),
io:format("[FATAL] ~p snapshot:\n~p~n", [?MODULE, Config]),
terminate(_Reason, _Config) ->
ok.
code_change(_Old, Config, _Extra) ->
{ ok, Config }.
{ ok, Config }.
module_tuple(I) -> { list_to_atom("elixir_compiler_" ++ integer_to_list(I)), I }.
+36 -73
View File
@@ -1,6 +1,6 @@
-module(elixir_compiler).
-export([get_opts/0, get_opt/1, get_opt/2, string/2, file/1, file_to_path/2]).
-export([core/0, module/3, eval_forms/5]).
-export([core/0, module/3, eval_forms/4]).
-include("elixir.hrl").
-compile({parse_transform, elixir_transform}).
@@ -27,7 +27,7 @@ string(Contents, File) when is_list(Contents), is_binary(File) ->
try
put(elixir_compiled, []),
Forms = elixir_translator:'forms!'(Contents, 1, File, []),
eval_forms(Forms, 1, File, [], #elixir_scope{file=File}),
eval_forms(Forms, 1, [], elixir:scope_for_eval([{file,File}])),
lists:reverse(get(elixir_compiled))
after
put(elixir_compiled, Previous)
@@ -49,34 +49,22 @@ file_to_path(File, Path) when is_binary(File), is_binary(Path) ->
%% Evaluates the contents/forms by compiling them to an Erlang module.
eval_forms(Forms, Line, Module, Vars, #elixir_scope{module=nil} = S) ->
eval_forms(Forms, Line, Module, nil, Vars, S);
eval_forms(Forms, Line, Module, Vars, #elixir_scope{module=Value} = S) ->
eval_forms(Forms, Line, Module, Value, Vars, S).
eval_forms(Forms, Line, RawModule, Value, Vars, S) ->
case (Value == nil) andalso allows_fast_compilation(Forms) of
true -> eval_compilation(Forms, Vars, S);
false -> code_loading_compilation(Forms, Line, RawModule, Value, Vars, S)
end.
eval_compilation(Forms, Vars, S) ->
Binding = [{ Var, Value } || { _, Var, Value } <- Vars],
{ Result, _Binding, FS } = elixir:eval_forms(Forms, [{'_@MODULE',nil}|Binding], S),
{ Result, FS }.
code_loading_compilation(Forms, Line, RawModule, Value, Vars, S) ->
Module = escape_module(RawModule),
eval_forms(Forms, Line, Vars, S) ->
{ Module, I } = retrieve_module_name(),
{ Exprs, FS } = elixir_translator:translate(Forms, S),
ModuleForm = module_form(Exprs, Line, S#elixir_scope.file, Module, Vars),
Args = [X || { _, _, X } <- Vars],
Args = [X || { _, _, _, X } <- Vars],
{ module(ModuleForm, S#elixir_scope.file, [], true, fun(Mod, _) ->
Res = Mod:'BOOTSTRAP'(Value, Args),
code:purge(Module),
%% Pass { native, false } to speed up bootstrap
%% process when native is set to true
{ module(ModuleForm, S#elixir_scope.file, [{native,false}], true, fun(_, _) ->
Res = Module:'BOOTSTRAP'(S#elixir_scope.module, Args),
code:delete(Module),
case code:soft_purge(Module) of
true -> return_module_name(I);
false -> ok
end,
Res
end), FS }.
@@ -109,23 +97,20 @@ module(Forms, File, Options, Bootstrap, Callback) when
%% Invoked from the Makefile.
core() ->
elixir:start_app(),
application:start(elixir),
gen_server:call(elixir_code_server, { compiler_options, [{docs,false},{internal,true}] }),
[core_file(File) || File <- core_main()],
AllLists = [filelib:wildcard(Wildcard) || Wildcard <- core_list()],
Files = lists:append(AllLists) -- core_main(),
[core_file(File) || File <- 'Elixir.List':uniq(Files)].
[core_file(File) || File <- core_main()].
%% HELPERS
no_auto_import() ->
{ attribute, 0, compile, {
no_auto_import, erlang:module_info(exports) } }.
Bifs = [{ Name, Arity } || { Name, Arity } <- erlang:module_info(exports), erl_internal:bif(Name, Arity)],
{ attribute, 0, compile, { no_auto_import, Bifs } }.
module_form(Exprs, Line, File, Module, Vars) when
is_binary(File), is_list(Exprs), is_integer(Line), is_atom(Module) ->
Cons = lists:foldr(fun({ _, Var, _ }, Acc) ->
Cons = lists:foldr(fun({ _, _, Var, _ }, Acc) ->
{ cons, Line, { var, Line, Var }, Acc }
end, { nil, Line }, Vars),
@@ -140,31 +125,13 @@ module_form(Exprs, Line, File, Module, Vars) when
] }
].
%% Fast compilation is available?
%% Generate module names from code server.
allows_fast_compilation([{defmodule,_,_}|T]) -> allows_fast_compilation(T);
allows_fast_compilation([]) -> true;
allows_fast_compilation(_) -> false.
retrieve_module_name() ->
gen_server:call(elixir_code_server, retrieve_module_name).
%% Escape the module name, removing slashes, dots,
%% so it can be loaded by Erlang.
escape_module(Module) when is_atom(Module) ->
escape_module(atom_to_list(Module));
escape_module(Module) when is_binary(Module) ->
escape_module(binary_to_list(Module));
escape_module(Module) when is_list(Module) ->
list_to_atom(escape_each(Module)).
escape_each([H|T]) when H >= $A, H =< $Z; H >= $a, H =< $z; H >= $0, H =< $9; H == $- ->
[H|escape_each(T)];
escape_each([_|T]) ->
[$_|escape_each(T)];
escape_each([]) -> [].
return_module_name(I) ->
gen_server:cast(elixir_code_server, { return_module_name, I }).
%% Receives a module Binary and outputs it in the given path.
@@ -186,29 +153,17 @@ core_file(File) ->
exit(1)
end.
core_list() ->
[
"lib/elixir/lib/range.ex",
"lib/elixir/lib/behaviour.ex",
"lib/elixir/lib/uri/parser.ex",
"lib/elixir/lib/elixir/formatter.ex",
"lib/elixir/lib/dict.ex",
"lib/elixir/lib/dict/common.ex",
"lib/elixir/lib/access.ex",
"lib/elixir/lib/range.ex",
"lib/elixir/lib/*/*.ex",
"lib/elixir/lib/*.ex"
].
core_main() ->
[
"lib/elixir/lib/kernel.ex",
"lib/elixir/lib/keyword.ex",
"lib/elixir/lib/list.ex",
"lib/elixir/lib/kernel/typespec.ex",
"lib/elixir/lib/module.ex",
"lib/elixir/lib/record.ex",
"lib/elixir/lib/record/extractor.ex",
"lib/elixir/lib/macro.ex",
"lib/elixir/lib/macro/env.ex",
"lib/elixir/lib/module.ex",
"lib/elixir/lib/list.ex",
"lib/elixir/lib/code.ex",
"lib/elixir/lib/protocol.ex",
"lib/elixir/lib/enum.ex",
@@ -216,7 +171,15 @@ core_main() ->
"lib/elixir/lib/binary/inspect.ex",
"lib/elixir/lib/binary/chars.ex",
"lib/elixir/lib/list/chars.ex",
"lib/elixir/lib/gen_server/behaviour.ex"
"lib/elixir/lib/io.ex",
"lib/elixir/lib/file.ex",
"lib/elixir/lib/access.ex",
"lib/elixir/lib/regex.ex",
"lib/elixir/lib/system.ex",
"lib/elixir/lib/kernel/cli.ex",
"lib/elixir/lib/kernel/error_handler.ex",
"lib/elixir/lib/kernel/parallel_compiler.ex",
"lib/elixir/lib/kernel/record_rewriter.ex"
].
%% ERROR HANDLING
+16 -16
View File
@@ -6,7 +6,7 @@
reset_last/1,
wrap_definition/7,
store_definition/8,
store_each/8,
store_each/7,
unwrap_stored_definitions/1,
format_error/1]).
-include("elixir.hrl").
@@ -19,7 +19,7 @@ table(Module) -> ?ELIXIR_ATOM_CONCAT([f, Module]).
build_table(Module) ->
FunctionTable = table(Module),
ets:new(FunctionTable, [set, named_table, public]),
ets:insert(FunctionTable, { last, [] }),
reset_last(Module),
FunctionTable.
delete_table(Module) ->
@@ -81,7 +81,6 @@ store_definition(Kind, Line, Module, Name, Args, Guards, Body, RawS) ->
File = TS#elixir_scope.file,
Table = table(Module),
Stack = TS#elixir_scope.macro,
%% Store function
if
@@ -90,11 +89,11 @@ store_definition(Kind, Line, Module, Name, Args, Guards, Body, RawS) ->
compile_super(Module, TS),
CheckClauses = S#elixir_scope.check_clauses,
store_each(CheckClauses, Kind, File, Location,
Stack, Table, length(Defaults), Function)
Table, length(Defaults), Function)
end,
[store_each(false, Kind, File, Location, Stack, Table, 0,
function_for_default(Kind, Name, Default)) || Default <- Defaults],
[store_each(false, Kind, File, Location, Table, 0,
default_function_for(Kind, Name, Default)) || Default <- Defaults],
{ Name, Arity }.
@@ -215,9 +214,10 @@ unwrap_stored_definition([Fun|T], Exports, Private, Def, Defmacro, Defmacrop, Fu
);
unwrap_stored_definition([Fun|T], Exports, Private, Def, Defmacro, Defmacrop, Functions) when element(2, Fun) == defmacrop ->
Tuple = element(1, Fun),
unwrap_stored_definition(
T, Exports, [element(1, Fun)|Private], Def, Defmacro,
[{ element(1, Fun), element(3, Fun) }|Defmacrop], Functions
T, Exports, [Tuple|Private], Def, Defmacro,
[{ Tuple, element(3, Fun), element(5, Fun) }|Defmacrop], Functions
);
unwrap_stored_definition([], Exports, Private, Def, Defmacro, Defmacrop, {Functions,Tail}) ->
@@ -226,13 +226,13 @@ unwrap_stored_definition([], Exports, Private, Def, Defmacro, Defmacrop, {Functi
%% Helpers
function_for_stored_definition({{Name, Arity}, _, Line, _, [], _, _, Clauses}, {Functions,Tail}) ->
function_for_stored_definition({{Name, Arity}, _, Line, _, _, [], _, Clauses}, {Functions,Tail}) ->
{
[{ function, Line, Name, Arity, lists:reverse(Clauses) }|Functions],
Tail
};
function_for_stored_definition({{Name, Arity}, _, Line, _, Location, _, _, Clauses}, {Functions,Tail}) ->
function_for_stored_definition({{Name, Arity}, _, Line, _, _, Location, _, Clauses}, {Functions,Tail}) ->
{
Functions,
[
@@ -241,33 +241,33 @@ function_for_stored_definition({{Name, Arity}, _, Line, _, Location, _, _, Claus
]
}.
function_for_default(Kind, Name, { clause, Line, Args, _Guards, _Exprs } = Clause)
default_function_for(Kind, Name, { clause, Line, Args, _Guards, _Exprs } = Clause)
when Kind == defmacro; Kind == defmacrop ->
{ function, Line, Name, length(Args) - 1, [Clause] };
function_for_default(_, Name, { clause, Line, Args, _Guards, _Exprs } = Clause) ->
default_function_for(_, Name, { clause, Line, Args, _Guards, _Exprs } = Clause) ->
{ function, Line, Name, length(Args), [Clause] }.
%% Store each definition in the table.
%% This function also checks and emit warnings in case
%% the kind, of the visibility of the function changes.
store_each(Check, Kind, File, Location, Stack, Table, Defaults, {function, Line, Name, Arity, Clauses}) ->
store_each(Check, Kind, File, Location, Table, Defaults, {function, Line, Name, Arity, Clauses}) ->
case ets:lookup(Table, {Name, Arity}) of
[{{Name, Arity}, StoredKind, _, _, StoredLocation, StoredStack, StoredDefaults, StoredClauses}] ->
[{{Name, Arity}, StoredKind, _, _, StoredCheck, StoredLocation, StoredDefaults, StoredClauses}] ->
FinalLocation = StoredLocation,
FinalDefaults = Defaults + StoredDefaults,
FinalClauses = Clauses ++ StoredClauses,
check_valid_kind(Line, File, Name, Arity, Kind, StoredKind),
check_valid_defaults(Line, File, Name, Arity, FinalDefaults),
Check andalso (Stack == StoredStack) andalso check_valid_clause(Line, File, Name, Arity, Table);
(Check and StoredCheck) andalso check_valid_clause(Line, File, Name, Arity, Table);
[] ->
FinalLocation = Location,
FinalDefaults = Defaults,
FinalClauses = Clauses,
Check andalso ets:insert(Table, { last, { Name, Arity } })
end,
ets:insert(Table, {{Name, Arity}, Kind, Line, File, FinalLocation, Stack, FinalDefaults, FinalClauses}).
ets:insert(Table, {{Name, Arity}, Kind, Line, File, Check, FinalLocation, FinalDefaults, FinalClauses}).
%% Validations
+3 -4
View File
@@ -15,8 +15,7 @@ unpack_each(Kind, Name, [{'//', Line, [Expr, _]}|T] = List, Acc, Clauses, S) ->
Base = build_match(Acc, Line, []),
{ Args, Invoke } = extract_defaults(List, Line, length(Base), [], []),
SM = S#elixir_scope{counter=length(Base)},
{ DefArgs, SA } = elixir_clauses:assigns(fun elixir_translator:translate/2, Base ++ Args, SM),
{ DefArgs, SA } = elixir_clauses:assigns(fun elixir_translator:translate/2, Base ++ Args, S),
{ InvokeArgs, _ } = elixir_translator:translate_args(Base ++ Invoke, SA),
Call = { call, Line,
@@ -39,7 +38,7 @@ extract_defaults([{'//', _, [_Expr, Default]}|T], Line, Counter, NewArgs, NewInv
extract_defaults(T, Line, Counter, NewArgs, [Default|NewInvoke]);
extract_defaults([_|T], Line, Counter, NewArgs, NewInvoke) ->
H = { ?ELIXIR_ATOM_CONCAT(["_@", Counter]), Line, nil },
H = { ?ELIXIR_ATOM_CONCAT(["_@D", Counter]), Line, nil },
extract_defaults(T, Line, Counter + 1, [H|NewArgs], [H|NewInvoke]);
extract_defaults([], _Line, _Counter, NewArgs, NewInvoke) ->
@@ -50,7 +49,7 @@ extract_defaults([], _Line, _Counter, NewArgs, NewInvoke) ->
build_match([], _Line, Acc) -> Acc;
build_match([_|T], Line, Acc) ->
Var = { ?ELIXIR_ATOM_CONCAT(["_@", length(T)]), Line, nil },
Var = { ?ELIXIR_ATOM_CONCAT(["_@D", length(T)]), Line, nil },
build_match(T, Line, [Var|Acc]).
% Given the invoked function name based on the kind
+3 -2
View File
@@ -69,9 +69,10 @@ rewrite_clause(Else, _) -> Else.
%% Error handling
check_unused_local_macros(File, Recorded, PMacros) ->
check_unused_local_macros(File, Recorded, Defmacrop) ->
[elixir_errors:handle_file_warning(File,
{ Line, ?MODULE, { unused_macro, Fun } }) || { Fun, Line } <- PMacros, not lists:member(Fun, Recorded)].
{ Line, ?MODULE, { unused_macro, Fun } }) || { Fun, Line, Check } <- Defmacrop,
Check, not lists:member(Fun, Recorded)].
format_error({unused_macro,{Name, Arity}}) ->
io_lib:format("macro ~s/~B is unused", [Name, Arity]).
+17 -15
View File
@@ -16,7 +16,7 @@ overridable(Module, Value) ->
is_defined(Module, Tuple) ->
Overridable = overridable(Module),
case orddict:find(Tuple, Overridable) of
{ ok, { _, [_|_] } } -> true;
{ ok, { _, _, _ } } -> true;
_ -> false
end.
@@ -53,37 +53,39 @@ name(Module, Function) ->
name(Module, Function, overridable(Module)).
name(_Module, { Name, _ } = Function, Overridable) ->
{ Count, _ } = orddict:fetch(Function, Overridable),
{ Count, _, _ } = orddict:fetch(Function, Overridable),
?ELIXIR_ATOM_CONCAT([Name, " (overridable ", Count, ")"]).
%% Store
store(Module, Function, GenerateName) ->
Overridable = overridable(Module),
{ Count, [H|T] } = orddict:fetch(Function, Overridable),
overridable(Module, orddict:store(Function, { Count, T }, Overridable)),
case orddict:fetch(Function, Overridable) of
{ _Count, _Clause, true } -> ok;
{ Count, Clause, false } ->
overridable(Module, orddict:store(Function, { Count, Clause, true }, Overridable)),
{ { Name, Arity }, Kind, Line, File, _Check, Location, Defaults, Clauses } = Clause,
{ { Name, Arity }, Kind, Line, File, Location, Stack, Defaults, Clauses } = H,
{ FinalKind, FinalName } = case GenerateName of
true -> { defp, name(Module, Function, Overridable) };
false -> { Kind, Name }
end,
{ FinalKind, FinalName } = case GenerateName of
true -> { defp, name(Module, Function, Overridable) };
false -> { Kind, Name }
end,
Def = { function, Line, FinalName, Arity, Clauses },
elixir_def:store_each(false, FinalKind, File, Location,
Stack, elixir_def:table(Module), Defaults, Def).
Def = { function, Line, FinalName, Arity, Clauses },
elixir_def:store_each(false, FinalKind, File, Location,
elixir_def:table(Module), Defaults, Def)
end.
%% Store pending declarations that were not manually made concrete.
store_pending(Module) ->
[store(Module, X, false) || { X, { _, [_|_] } } <- overridable(Module),
[store(Module, X, false) || { X, { _, _, false } } <- overridable(Module),
not 'Elixir.Module':'defines?'(Module, X)].
%% Error handling
format_error({ no_super, Module, { Name, Arity } }) ->
Bins = [ format_fa(X) || { X, { _, [_|_] } } <- overridable(Module)],
Bins = [ format_fa(X) || { X, { _, _, _ } } <- overridable(Module)],
Joined = 'Elixir.Enum':join(Bins, <<", ">>),
io_lib:format("no super defined for ~s/~B in module ~s. Overridable functions available are: ~s",
[Name, Arity, elixir_errors:inspect(Module), Joined]).
+9 -10
View File
@@ -59,8 +59,8 @@ dispatch_import(Line, Name, Args, S, Callback) ->
{ error, internal } ->
elixir_import:record(import, Tuple, ?BUILTIN, Module),
elixir_macros:translate({ Name, Line, Args }, S);
{ ok, Receiver, Tree } ->
translate_expansion(Line, Tree, Receiver, Name, Arity, S)
{ ok, _Receiver, Tree } ->
translate_expansion(Line, Tree, S)
end;
Receiver ->
elixir_import:record(import, Tuple, Receiver, Module),
@@ -87,7 +87,7 @@ dispatch_require(Line, Receiver, Name, Args, S, Callback) ->
{ error, internal } ->
elixir_macros:translate({ Name, Line, Args }, S);
{ ok, Tree } ->
translate_expansion(Line, Tree, Receiver, Name, Arity, S)
translate_expansion(Line, Tree, S)
end
end.
@@ -167,10 +167,12 @@ expand_macro_named(Line, Receiver, Name, Arity, Args, Module, Requires, SEnv) ->
Fun = fun Receiver:ProperName/ProperArity,
expand_macro_fun(Line, Fun, Receiver, Name, Args, Module, Requires, SEnv).
translate_expansion(Line, Tree, Receiver, Name, Arity, S) ->
NewS = S#elixir_scope{macro=[{Line,Receiver,Name,Arity}|S#elixir_scope.macro]},
{ TTree, TS } = elixir_translator:translate_each(elixir_quote:linify(Line, Tree), NewS),
{ TTree, TS#elixir_scope{macro=S#elixir_scope.macro} }.
translate_expansion(Line, Tree, S) ->
{ TR, TS } = elixir_translator:translate_each(
elixir_quote:linify(Line, Tree),
S#elixir_scope{check_clauses=false}
),
{ TR, TS#elixir_scope{check_clauses=S#elixir_scope.check_clauses} }.
%% Helpers
@@ -268,9 +270,6 @@ in_erlang_functions() ->
{ exit, 1 },
{ float, 1 },
{ float_to_list, 1 },
{ halt, 0 },
{ halt, 1 },
{ halt, 2 },
{ hd, 1 },
{ integer_to_list, 1 },
{ integer_to_list, 2 },
+23 -5
View File
@@ -26,6 +26,9 @@ to_dot(L) -> L.
%% Raised during macros translation.
-spec syntax_error(non_neg_integer(), file:filename(), binary() | string()) -> no_return().
-spec syntax_error(non_neg_integer(), file:filename(), binary() | string(), list()) -> no_return().
syntax_error(Line, File, Message) when is_list(Message) ->
syntax_error(Line, File, iolist_to_binary(Message));
@@ -38,6 +41,8 @@ syntax_error(Line, File, Format, Args) ->
%% Raised on tokenizing/parsing
-spec parse_error(non_neg_integer(), file:filename(), iolist() | atom(), [] | iolist()) -> no_return().
parse_error(Line, File, Error, []) ->
Message = case Error of
"syntax error before: " -> <<"syntax error: expression is incomplete">>;
@@ -64,6 +69,8 @@ parse_error(Line, File, Error, Token) ->
%% Raised during compilation
-spec form_error(non_neg_integer(), file:filename(), module(), any()) -> no_return().
form_error(Line, File, Module, Desc) ->
Message = iolist_to_binary(format_error(Module, Desc)),
raise(Line, File, 'Elixir.CompileError', Message).
@@ -95,13 +102,19 @@ handle_file_warning(_, File, {Line,erl_lint,{undefined_behaviour_func,{Fun,Arity
io:format(file_format(Line, File, Message));
handle_file_warning(_, File, {Line,erl_lint,{undefined_behaviour,Module}}) ->
Raw = io_lib:format("behaviour ~s undefined", [inspect(Module)]),
case elixir_compiler:get_opt(internal) of
true -> [];
false ->
Message = io_lib:format("behaviour ~s undefined", [inspect(Module)]),
io:format(file_format(Line, File, Message))
end;
Message = case erlang:function_exported(Module, behavior_info, 1) of
true -> Raw ++ " (maybe you meant behaviour_info instead of behavior_info?)";
false -> Raw
end,
handle_file_warning(_, File, {Line,erl_lint,{unused_var,Var}}) ->
Message = format_error(erl_lint, { unused_var, format_var(Var) }),
io:format(file_format(Line, File, Message));
handle_file_warning(_, File, {Line,erl_lint,{shadowed_var,Var,Where}}) ->
Message = format_error(erl_lint, { shadowed_var, format_var(Var), Where }),
io:format(file_format(Line, File, Message));
%% Default behavior
@@ -112,6 +125,8 @@ handle_file_warning(_, File, {Line,Module,Desc}) ->
handle_file_warning(File, Desc) ->
handle_file_warning(false, File, Desc).
-spec handle_file_error(file:filename(), {non_neg_integer(), module(), any()}) -> no_return().
handle_file_error(File, {Line,Module,Desc}) ->
form_error(Line, File, Module, Desc).
@@ -151,6 +166,9 @@ raise(Line, File, Kind, Message) ->
file_format(Line, File, Message) ->
io_lib:format("~ts:~w: ~ts~n", [File, Line, Message]).
format_var(Var) ->
list_to_atom(lists:takewhile(fun(X) -> X /= $@ end, atom_to_list(Var))).
format_error([], Desc) ->
io_lib:format("~p", [Desc]);
+2 -2
View File
@@ -236,10 +236,10 @@ format_error({import_conflict,{Receiver, Name, Arity}}) ->
[elixir_errors:inspect(Receiver), Name, Arity]);
format_error({local_conflict,{_, Name, Arity}}) ->
io_lib:format("cannot define local ~s/~B because it conflicts with Elixir internal macros", [Name, Arity]);
io_lib:format("cannot define local ~s/~B because it conflicts with Elixir special forms", [Name, Arity]);
format_error({internal_conflict,{Receiver, Name, Arity}}) ->
io_lib:format("cannot import ~s.~s/~B because it conflicts with Elixir internal macros",
io_lib:format("cannot import ~s.~s/~B because it conflicts with Elixir special forms",
[elixir_errors:inspect(Receiver), Name, Arity]);
format_error({ no_macros, Module }) ->
+41 -23
View File
@@ -2,7 +2,6 @@
-module(elixir_interpolation).
-export([extract/5, unescape_chars/1, unescape_chars/2,
unescape_tokens/1, unescape_tokens/2, unescape_map/1]).
-define(is_octal(S), S >= $0 andalso S =< $7).
-include("elixir.hrl").
-compile({parse_transform, elixir_transform}).
@@ -75,42 +74,61 @@ unescape_token(Other, _Map) -> Other.
% Unescape chars. For instance, "\" "n" (two chars) needs to be converted to "\n" (one char).
-define(to_octal(List),
).
-define(is_octal(S), S >= $0 andalso S =< $7).
-define(is_hex(S), (S >= $0 andalso S =< $9) orelse (S >= $A andalso S =< $F)).
unescape_chars(String) ->
unescape_chars(String, fun unescape_map/1).
unescape_chars(String, Map) ->
Octals = case Map($0) of
false -> false;
_ -> true
end,
unescape_chars(String, Map, Octals, <<>>).
Octals = Map($0) /= false,
Hex = Map($x) /= false,
unescape_chars(String, Map, Octals, Hex, <<>>).
unescape_chars(<<$\\,A,B,C,Rest/binary>>, Map, true, Acc) when ?is_octal(A), ?is_octal(B), ?is_octal(C) ->
to_octal(Rest, Map, [A,B,C], Acc);
unescape_chars(<<$\\,A,B,C,Rest/binary>>, Map, true, Hex, Acc) when ?is_octal(A), ?is_octal(B), ?is_octal(C) ->
append_escaped(Rest, Map, [A,B,C], true, Hex, Acc, 8);
unescape_chars(<<$\\,A,B,Rest/binary>>, Map, true, Acc) when ?is_octal(A), ?is_octal(B) ->
to_octal(Rest, Map, [A,B], Acc);
unescape_chars(<<$\\,A,B,Rest/binary>>, Map, true, Hex, Acc) when ?is_octal(A), ?is_octal(B) ->
append_escaped(Rest, Map, [A,B], true, Hex, Acc, 8);
unescape_chars(<<$\\,A,Rest/binary>>, Map, true, Acc) when ?is_octal(A) ->
to_octal(Rest, Map, [A], Acc);
unescape_chars(<<$\\,A,Rest/binary>>, Map, true, Hex, Acc) when ?is_octal(A) ->
append_escaped(Rest, Map, [A], true, Hex, Acc, 8);
unescape_chars(<<$\\,Escaped,Rest/binary>>, Map, Octals, Acc) ->
unescape_chars(<<$\\,$x,A,B,Rest/binary>>, Map, Octal, true, Acc) when ?is_hex(A), ?is_hex(B) ->
append_escaped(Rest, Map, [A,B], Octal, true, Acc, 16);
unescape_chars(<<$\\,$x,${,A,$},Rest/binary>>, Map, Octal, true, Acc) when ?is_hex(A) ->
append_escaped(Rest, Map, [A], Octal, true, Acc, 16);
unescape_chars(<<$\\,$x,${,A,B,$},Rest/binary>>, Map, Octal, true, Acc) when ?is_hex(A), ?is_hex(B) ->
append_escaped(Rest, Map, [A,B], Octal, true, Acc, 16);
unescape_chars(<<$\\,$x,${,A,B,C,$},Rest/binary>>, Map, Octal, true, Acc) when ?is_hex(A), ?is_hex(B), ?is_hex(C) ->
append_escaped(Rest, Map, [A,B,C], Octal, true, Acc, 16);
unescape_chars(<<$\\,$x,${,A,B,C,D,$},Rest/binary>>, Map, Octal, true, Acc) when ?is_hex(A), ?is_hex(B), ?is_hex(C), ?is_hex(D) ->
append_escaped(Rest, Map, [A,B,C,D], Octal, true, Acc, 16);
unescape_chars(<<$\\,$x,${,A,B,C,D,E,$},Rest/binary>>, Map, Octal, true, Acc) when ?is_hex(A), ?is_hex(B), ?is_hex(C), ?is_hex(D), ?is_hex(E) ->
append_escaped(Rest, Map, [A,B,C,D,E], Octal, true, Acc, 16);
unescape_chars(<<$\\,$x,${,A,B,C,D,E,F,$},Rest/binary>>, Map, Octal, true, Acc) when ?is_hex(A), ?is_hex(B), ?is_hex(C), ?is_hex(D), ?is_hex(E), ?is_hex(F) ->
append_escaped(Rest, Map, [A,B,C,D,E,F], Octal, true, Acc, 16);
unescape_chars(<<$\\,Escaped,Rest/binary>>, Map, Octals, Hex, Acc) ->
case Map(Escaped) of
false -> unescape_chars(Rest, Map, Octals, <<Acc/binary, $\\, Escaped>>);
Other -> unescape_chars(Rest, Map, Octals, <<Acc/binary, Other>>)
false -> unescape_chars(Rest, Map, Octals, Hex, <<Acc/binary, $\\, Escaped>>);
Other -> unescape_chars(Rest, Map, Octals, Hex, <<Acc/binary, Other>>)
end;
unescape_chars(<<Char, Rest/binary>>, Map, Octals, Acc) ->
unescape_chars(Rest, Map, Octals, <<Acc/binary, Char>>);
unescape_chars(<<Char, Rest/binary>>, Map, Octals, Hex, Acc) ->
unescape_chars(Rest, Map, Octals, Hex, <<Acc/binary, Char>>);
unescape_chars(<<>>, _Map, _Octals, Acc) -> Acc.
unescape_chars(<<>>, _Map, _Octals, _Hex, Acc) -> Acc.
to_octal(Rest, Map, Octal, Acc) ->
unescape_chars(Rest, Map, true, <<Acc/binary, (list_to_integer(Octal, 8))/integer>>).
append_escaped(Rest, Map, List, Octal, Hex, Acc, Base) ->
Binary = unicode:characters_to_binary([list_to_integer(List, Base)]),
unescape_chars(Rest, Map, Octal, Hex, <<Acc/binary, Binary/binary>>).
% Unescape Helpers
-44
View File
@@ -114,28 +114,6 @@ build_bitstr_each(Fun, [{'::',_,[H,V]}|T], Line, S, Acc) ->
{ Size, Types } = extract_bit_values(Line, V, ES),
build_bitstr_each(Fun, T, Line, NS, [{ bin_element, Line, Expr, Size, Types }|Acc]);
%% Deprecated syntax
build_bitstr_each(Fun, [{'|',_,[H,V]}|T], Line, S, Acc) ->
{ Expr, NS } = Fun(H, S),
%% Just variables defined outside the binary can be accounted on subparts
case NS of
{ ES, _ } -> [];
ES -> []
end,
elixir_errors:deprecation(Line, ES#elixir_scope.file, "Old bitstring syntax with | is deprecated, use the new one with :: instead"),
%% Assigns can be made in subparts
{ Int, Types } = extract_bin_values(Line, V, default, [], ES#elixir_scope{context=nil}),
Final = case Types of
[] -> default;
_ -> lists:reverse(Types)
end,
build_bitstr_each(Fun, T, Line, NS, [{ bin_element, Line, Expr, Int, Final }|Acc]);
build_bitstr_each(Fun, [H|T], Line, S, Acc) ->
{ Expr, NS } = Fun(H, S),
build_bitstr_each(Fun, T, Line, NS, [{ bin_element, Line, Expr, default, default }|Acc]).
@@ -221,25 +199,3 @@ handle_unknown_specifier(Line, [H|T], Size, Types, S) ->
join_expansion({ '__block__', _, [Expanded] }, Tail) -> join_expansion(Expanded, Tail);
join_expansion(Expanded, Tail) when is_list(Expanded) -> Expanded ++ Tail;
join_expansion(Expanded, Tail) -> [Expanded|Tail].
%% Deprecated specifiers
extract_bin_values(Line, { '-', _Line, [Left, Right] }, Int, Types, S) ->
{ LInt, LTypes } = extract_bin_values(Line, Left, Int, Types, S),
extract_bin_values(Line, Right, LInt, LTypes, S);
extract_bin_values(Line, Value, default, Types, _S) when is_integer(Value) ->
{ { integer, Line, Value }, Types };
extract_bin_values(_Line, { Value, _, Atom } = Expr, default, Types, S) when is_atom(Value), is_atom(Atom) ->
Translated = element(1, elixir_translator:translate_each(Expr, S)),
{ Translated, Types };
extract_bin_values(Line, Value, _Int, _Types, S) when is_integer(Value) ->
elixir_errors:syntax_error(Line, S#elixir_scope.file, "duplicated size specifier ~p in <<>>", [Value]);
extract_bin_values(_Line, Value, Int, Types, _S) when is_atom(Value); is_tuple(Value), tuple_size(Value) == 2 ->
{ Int, [Value|Types] };
extract_bin_values(Line, _Value, _Int, _Types, S) ->
elixir_errors:syntax_error(Line, S#elixir_scope.file, "invalid specifier for <<>>").
+64 -23
View File
@@ -8,7 +8,9 @@
assert_no_function_scope/3, assert_module_scope/3, assert_no_assign_or_guard_scope/3]).
-include("elixir.hrl").
-define(FUNS(), Kind == def; Kind == defp; Kind == defmacro; Kind == defmacrop).
-define(FUNS(Kind), Kind == def; Kind == defp; Kind == defmacro; Kind == defmacrop).
-define(IN_TYPES(Kind), Kind == atom orelse Kind == integer orelse Kind == float).
-compile({parse_transform, elixir_transform}).
%% Operators
@@ -34,11 +36,11 @@ translate({ Op, Line, Exprs }, S) when is_list(Exprs),
translate({ '!', Line, [{ '!', _, [Expr] }] }, S) ->
{ TExpr, SE } = translate_each(Expr, S),
{ elixir_tree_helpers:convert_to_boolean(Line, TExpr, true, S#elixir_scope.context == guard), SE };
elixir_tree_helpers:convert_to_boolean(Line, TExpr, true, S#elixir_scope.context == guard, SE);
translate({ '!', Line, [Expr] }, S) ->
{ TExpr, SE } = translate_each(Expr, S),
{ elixir_tree_helpers:convert_to_boolean(Line, TExpr, false, S#elixir_scope.context == guard), SE };
elixir_tree_helpers:convert_to_boolean(Line, TExpr, false, S#elixir_scope.context == guard, SE);
translate({ in, Line, [Left, Right] }, #elixir_scope{extra_guards=nil} = S) ->
{ _, TExpr, TS } = translate_in(Line, Left, Right, S),
@@ -54,6 +56,12 @@ translate({ function, Line, [[{do,{ '->',_,Pairs}}]] }, S) ->
assert_no_assign_or_guard_scope(Line, 'function', S),
elixir_translator:translate_fn(Line, Pairs, S);
translate({ function, Line, [{ '/', _, [{{ '.', _ ,[M, F] }, _ , [] }, A]}] }, S) ->
translate({ function, Line, [M, F, A] }, S);
translate({ function, Line, [{ '/', _, [{F, _, Q}, A]}] }, S) when is_atom(Q) ->
translate({ function, Line, [F, A] }, S);
translate({ function, Line, [_] }, S) ->
assert_no_assign_or_guard_scope(Line, 'function', S),
syntax_error(Line, S#elixir_scope.file, "invalid args for function");
@@ -78,7 +86,7 @@ translate({ function, Line, [_,_,_] = Args }, S) when is_list(Args) ->
%% @
translate({'@', Line, [{ Name, _, Args }]}, S) when Name == typep; Name == type; Name == spec; Name == callback ->
translate({'@', Line, [{ Name, _, Args }]}, S) when is_list(Args) andalso (Name == typep orelse Name == type orelse Name == spec orelse Name == callback orelse Name == opaque) ->
case elixir_compiler:get_opt(internal) of
true -> { { nil, Line }, S };
false ->
@@ -205,23 +213,32 @@ translate({defmodule, Line, [Ref, KV]}, S) ->
{ TRef, S }
end,
{ elixir_module:translate(Line, FRef, Block, S), FS };
{ elixir_module:translate(Line, FRef, Block, S#elixir_scope{check_clauses=true}), FS };
translate({Kind, Line, [Call]}, S) when ?FUNS() ->
translate({Kind, Line, [Call]}, S) when ?FUNS(Kind) ->
translate({Kind, Line, [Call, nil]}, S);
translate({Kind, Line, [Call, Expr]}, S) when ?FUNS() ->
translate({Kind, Line, [Call, Expr]}, S) when ?FUNS(Kind) ->
assert_module_scope(Line, Kind, S),
assert_no_function_scope(Line, Kind, S),
{ TCall, Guards } = elixir_clauses:extract_guards(Call),
{ Name, Args } = elixir_clauses:extract_args(TCall),
{ Name, Args } = case elixir_clauses:extract_args(TCall) of
error -> syntax_error(Line, S#elixir_scope.file,
"invalid syntax in ~s ~s", [Kind, 'Elixir.Macro':to_binary(TCall)]);
Tuple -> Tuple
end,
assert_no_aliases_name(Line, Name, Args, S),
TName = elixir_tree_helpers:abstract_syntax(Name),
TArgs = elixir_tree_helpers:abstract_syntax(Args),
TGuards = elixir_tree_helpers:abstract_syntax(Guards),
TExpr = elixir_tree_helpers:abstract_syntax(Expr),
{ elixir_def:wrap_definition(Kind, Line, TName, TArgs, TGuards, TExpr, S), S };
translate({Kind, Line, [Name, Args, Guards, Expr]}, S) when ?FUNS() ->
translate({Kind, Line, [Name, Args, Guards, Expr]}, S) when ?FUNS(Kind) ->
assert_module_scope(Line, Kind, S),
assert_no_function_scope(Line, Kind, S),
{ TName, NS } = translate_each(Name, S),
@@ -271,22 +288,28 @@ translate_in(Line, Left, Right, S) ->
Expr = case TRight of
{ cons, _, _, _ } ->
[H|T] = elixir_tree_helpers:cons_to_list(TRight),
lists:foldl(fun(X, Acc) ->
{ op, Line, 'orelse', Acc, { op, Line, '==', Var, X } }
lists:foldr(fun(X, Acc) ->
{ op, Line, 'orelse', { op, Line, '==', Var, X }, Acc }
end, { op, Line, '==', Var, H }, T);
{ string, _, [H|T] } ->
lists:foldl(fun(X, Acc) ->
{ op, Line, 'orelse', { op, Line, '==', Var, { integer, Line, X } }, Acc }
end, { op, Line, '==', Var, { integer, Line, H } }, T);
{ tuple, _, [{ atom, _, 'Elixir.Range' }, Start, End] } ->
{ op, Line, 'orelse',
{ op, Line, 'andalso',
{ op, Line, '=<', Start, End},
{ op, Line, 'andalso',
{ op, Line, '>=', Var, Start },
{ op, Line, '=<', Var, End } } },
{ op, Line, 'andalso',
{ op, Line, '<', End, Start},
{ op, Line, 'andalso',
{ op, Line, '=<', Var, Start },
{ op, Line, '>=', Var, End } } }
};
case { Start, End } of
{ { K1, _, StartInt }, { K2, _, EndInt } } when ?IN_TYPES(K1), ?IN_TYPES(K2), StartInt =< EndInt ->
increasing_compare(Line, Var, Start, End);
{ { K1, _, _ }, { K2, _, _ } } when ?IN_TYPES(K1), ?IN_TYPES(K2) ->
decreasing_compare(Line, Var, Start, End);
_ ->
{ op, Line, 'orelse',
{ op, Line, 'andalso',
{ op, Line, '=<', Start, End},
increasing_compare(Line, Var, Start, End) },
{ op, Line, 'andalso',
{ op, Line, '<', End, Start},
decreasing_compare(Line, Var, Start, End) } }
end;
_ ->
syntax_error(Line, S#elixir_scope.file, "invalid args for operator in")
end,
@@ -296,6 +319,16 @@ translate_in(Line, Left, Right, S) ->
false -> { Var, Expr, SV }
end.
increasing_compare(Line, Var, Start, End) ->
{ op, Line, 'andalso',
{ op, Line, '>=', Var, Start },
{ op, Line, '=<', Var, End } }.
decreasing_compare(Line, Var, Start, End) ->
{ op, Line, 'andalso',
{ op, Line, '=<', Var, Start },
{ op, Line, '>=', Var, End } }.
rewrite_case_clauses([{do,[{in,_,[{'_',_,_},[false,nil]]}],False},{do,[{'_',_,_}],True}]) ->
[{do,[false],False},{do,[true],True}];
@@ -317,9 +350,17 @@ is_reserved_data(_) -> false.
spec_to_macro(type) -> deftype;
spec_to_macro(typep) -> deftypep;
spec_to_macro(opaque) -> defopaque;
spec_to_macro(spec) -> defspec;
spec_to_macro(callback) -> defcallback.
% Unpack a list of expressions from a block.
unpack([{ '__block__', _, Exprs }]) -> Exprs;
unpack(Exprs) -> Exprs.
assert_no_aliases_name(Line, '__aliases__', [Atom], #elixir_scope{file=File}) when is_atom(Atom) ->
Message = "function names should start with lowercase characters or underscore, invalid name ~s",
syntax_error(Line, File, Message, [atom_to_binary(Atom, utf8)]);
assert_no_aliases_name(_Line, _Aliases, _Args, _S) ->
ok.
+75 -75
View File
@@ -14,7 +14,7 @@ eval_quoted(Module, Quoted, RawBinding, Opts) ->
false -> Line = 1
end,
{ Value, FinalBinding, _Scope } = elixir:eval_quoted([Quoted], Binding, Line, Scope),
{ Value, FinalBinding, _Scope } = elixir:eval_quoted([Quoted], Binding, Line, Scope#elixir_scope{check_clauses=false}),
{ Value, FinalBinding }.
scope_for_eval(Module, #elixir_scope{} = S) ->
@@ -25,7 +25,6 @@ scope_for_eval(Module, Opts) ->
binding_for_eval(Module, Binding) -> [{'_@MODULE',Module}|Binding].
%% TABLE METHODS
data_table(Module) ->
@@ -34,9 +33,6 @@ data_table(Module) ->
docs_table(Module) ->
?ELIXIR_ATOM_CONCAT([o, Module]).
spec_table(Module) ->
?ELIXIR_ATOM_CONCAT([s, Module]).
%% TRANSFORMATION FUNCTIONS
%% Transformation of args and scope into a compiled erlang call.
@@ -44,18 +40,10 @@ spec_table(Module) ->
%% will be passed to the invoked function.
translate(Line, Ref, Block, S) ->
MetaBlock = elixir_tree_helpers:abstract_syntax(Block),
MetaS = elixir_scope:serialize(S),
MetaBlock = elixir_tree_helpers:abstract_syntax(Block),
{ MetaS, Vars } = elixir_scope:serialize_with_vars(Line, S),
Vars = orddict:fold(fun(Key, Value, { Acc, Counter }) ->
{ { cons, Line, { tuple, Line, [
{ atom, Line, Key },
{ atom, Line, ?ELIXIR_ATOM_CONCAT(["_@", Counter]) },
{ var, Line, Value }
] }, Acc }, Counter + 1 }
end, { { nil, Line }, 0 }, S#elixir_scope.vars),
Args = [{integer, Line, Line}, Ref, MetaBlock, element(1, Vars), MetaS],
Args = [{integer, Line, Line}, Ref, MetaBlock, Vars, MetaS],
?ELIXIR_WRAP_CALL(Line, ?MODULE, compile, Args).
%% The compilation hook.
@@ -72,15 +60,15 @@ compile(Line, Module, Block, Vars, #elixir_scope{} = S) when is_atom(Module) ->
{ Export, Private, Def, Defmacro, Defmacrop, Functions } = elixir_def:unwrap_stored_definitions(Module),
{ All, Forms0 } = functions_form(Line, File, Module, Export, Private, Def, Defmacro, Defmacrop, Functions, C),
Forms1 = attributes_form(Line, File, Module, Forms0),
Forms2 = specs_form(Line, Module, Defmacro, Defmacrop, Forms1),
Forms1 = specs_form(Line, Module, Defmacro, Defmacrop, Forms0, C),
Forms2 = attributes_form(Line, File, Module, Forms1),
elixir_import:ensure_no_local_conflict(Line, File, Module, All),
elixir_import:ensure_no_import_conflict(Line, File, Module, All),
Final = [
{attribute, Line, file, {File,Line}},
{attribute, Line, module, Module} | Forms2
{ attribute, Line, file, { binary_to_list(File), Line } },
{ attribute, Line, module, Module } | Forms2
],
Binary = load_form(Line, Final, S),
@@ -88,7 +76,6 @@ compile(Line, Module, Block, Vars, #elixir_scope{} = S) when is_atom(Module) ->
after
ets:delete(data_table(Module)),
ets:delete(docs_table(Module)),
ets:delete(spec_table(Module)),
elixir_def:delete_table(Module),
elixir_import:delete_table(Module)
end;
@@ -97,8 +84,8 @@ compile(Line, Other, _Block, _Vars, #elixir_scope{file=File}) ->
elixir_errors:form_error(Line, File, ?MODULE, { invalid_module, Other });
compile(Line, Module, Block, Vars, RawS) ->
Dict = [{ X, Y } || { X, Y, _ } <- Vars],
S = elixir_scope:deserialize(RawS, Dict),
Dict = [{ { Name, Kind }, Value } || { Name, Kind, Value, _ } <- Vars],
S = elixir_scope:deserialize_with_vars(RawS, Dict),
compile(Line, Module, Block, Vars, S).
%% Hook that builds both attribute and functions and set up common hooks.
@@ -131,10 +118,6 @@ build(Line, File, Module) ->
DocsTable = docs_table(Module),
ets:new(DocsTable, [ordered_set, named_table, public]),
%% Holds specs in a format it is easier to query.
SpecTable = spec_table(Module),
ets:new(SpecTable, [duplicate_bag, named_table, public]),
%% We keep a separated table for function definitions
%% and another one for imports. We keep them in different
%% tables for organization and speed purpose (since the
@@ -145,15 +128,20 @@ build(Line, File, Module) ->
%% Receives the module representation and evaluates it.
eval_form(Line, Module, Block, Vars, RawS) ->
Temp = ?ELIXIR_ATOM_CONCAT(["COMPILE-",Module]),
S = scope_for_eval(Module, RawS),
{ Value, NewS } = elixir_compiler:eval_forms([Block], Line, Temp, Vars, S),
{ Value, NewS } = elixir_compiler:eval_forms([Block], Line, Vars, S),
elixir_def_overridable:store_pending(Module),
eval_callbacks(Line, Module, before_compile, [Module], NewS),
elixir_def_overridable:store_pending(Module),
Value.
%% Return the form with exports and function declarations.
functions_form(Line, File, Module, Export, Private, Def, Defmacro, Defmacrop, Functions, C) ->
functions_form(Line, File, Module, Export, Private, Def, Defmacro, Defmacrop, RawFunctions, C) ->
Functions = case elixir_compiler:get_opt(internal, C) of
true -> RawFunctions;
false -> record_rewrite_functions(RawFunctions)
end,
{ FinalExport, FinalFunctions } =
add_info_function(Line, File, Module, Export, Functions, Def, Defmacro, C),
@@ -164,6 +152,17 @@ functions_form(Line, File, Module, Export, Private, Def, Defmacro, Defmacrop, Fu
{attribute, Line, export, lists:sort(FinalExport)} | FinalFunctions
] }.
record_rewrite_functions(Functions) ->
lists:map(fun
({ function, Line, Name, Arity, Clauses }) ->
Rewriten = [begin
{ C, _, _ } = 'Elixir.Kernel.RecordRewriter':optimize_clause(Clause),
C
end || Clause <- Clauses],
{ function, Line, Name, Arity, Rewriten };
(Other) -> Other
end, Functions).
%% Add attributes handling to the form
attributes_form(Line, _File, Module, Current) ->
@@ -188,36 +187,43 @@ attributes_form(Line, _File, Module, Current) ->
%% Specs
specs_form(Line, Module, Defmacro, Defmacrop, Forms) ->
{ Specs, Callbacks } = ets:foldl(fun(X, Acc) ->
translate_spec(X, Defmacro, Defmacrop, Acc)
end, { [], [] }, spec_table(Module)),
specs_form(Line, Module, Defmacro, DefmacropWithLine, Forms, C) ->
Defmacrop = [Tuple || { Tuple, _, _ } <- DefmacropWithLine],
case elixir_compiler:get_opt(internal, C) of
true -> Forms;
_ ->
Callbacks = 'Elixir.Module':get_attribute(Module, callback),
Specs = [translate_spec(Spec, Defmacro, Defmacrop) ||
Spec <- 'Elixir.Module':get_attribute(Module, spec)],
Temp = specs_attributes(Line, spec, Forms, Specs),
specs_attributes(Line, callback, Temp, Callbacks).
'Elixir.Module':delete_attribute(Module, spec),
'Elixir.Module':delete_attribute(Module, callback),
Temp = specs_attributes(Line, spec, Forms, Specs),
specs_attributes(Line, callback, Temp, Callbacks)
end.
specs_attributes(Line, Type, Forms, Specs) ->
Keys = lists:ukeysort(1, Specs),
Keys = lists:foldl(fun({ Tuple, Value }, Acc) ->
lists:keystore(Tuple, 1, Acc, { Tuple, Value } )
end, [], Specs),
lists:foldl(fun({ Tuple, _ }, Acc) ->
[{ attribute, Line, Type, { Tuple, proplists:append_values(Tuple, Specs) } }|Acc]
Values = [V || { K, V } <- Specs, K == Tuple],
[{ attribute, Line, Type, { Tuple, Values } }|Acc]
end, Forms, Keys).
translate_spec({ { spec, Spec }, Rest }, Defmacro, Defmacrop, { Specs, Callbacks } = Acc) ->
translate_spec({ Spec, Rest }, Defmacro, Defmacrop) ->
case ordsets:is_element(Spec, Defmacrop) of
true -> Acc;
true -> { Spec, Rest };
false ->
case ordsets:is_element(Spec, Defmacro) of
true ->
{ Name, Arity } = Spec,
New = { { ?ELIXIR_MACRO(Name), Arity + 1 }, [spec_for_macro(X) || X <- Rest] },
{ [New|Specs], Callbacks };
{ { ?ELIXIR_MACRO(Name), Arity + 1 }, spec_for_macro(Rest) };
false ->
{ [{ Spec, Rest }|Specs], Callbacks }
{ Spec, Rest }
end
end;
translate_spec({ { callback, Spec }, Rest }, _, _, { Specs, Callbacks }) ->
{ Specs, [{ Spec, Rest }|Callbacks] }.
end.
spec_for_macro({ type, Line, 'fun', [{ type, _, product, Args }|T] }) ->
NewArgs = [{type,Line,term,[]}|Args],
@@ -266,56 +272,50 @@ add_info_function(Line, File, Module, Export, Functions, Def, Defmacro, C) ->
true -> elixir_errors:form_error(Line, File, ?MODULE, {internal_function_overridden, Pair});
false ->
Docs = elixir_compiler:get_opt(docs, C),
Contents = { function, Line, '__info__', 1, [
functions_clause(Line, Def),
macros_clause(Line, Module, Def, Defmacro),
docs_clause(Line, Module, Docs),
Contents = { function, 0, '__info__', 1, [
functions_clause(Def),
macros_clause(Module, Def, Defmacro),
docs_clause(Module, Docs),
moduledoc_clause(Line, Module, Docs),
compile_clause(Line),
self_clause(Line, Module),
else_clause(Line)
module_clause(Module),
else_clause()
] },
{ [Pair|Export], [Contents|Functions] }
end.
functions_clause(Line, Def) ->
functions_clause(Def) ->
All = ordsets:add_element({'__info__',1}, Def),
{ clause, Line, [{ atom, Line, functions }], [], [elixir_tree_helpers:abstract_syntax(All)] }.
{ clause, 0, [{ atom, 0, functions }], [], [elixir_tree_helpers:abstract_syntax(All)] }.
macros_clause(Line, Module, Def, Defmacro) ->
macros_clause(Module, Def, Defmacro) ->
All = handle_builtin_macros(Module, Def, Defmacro),
{ clause, Line, [{ atom, Line, macros }], [], [elixir_tree_helpers:abstract_syntax(All)] }.
{ clause, 0, [{ atom, 0, macros }], [], [elixir_tree_helpers:abstract_syntax(All)] }.
handle_builtin_macros('Elixir.Kernel', Def, Defmacro) ->
ordsets:subtract(ordsets:union(Defmacro,
elixir_dispatch:in_erlang_macros()), Def);
handle_builtin_macros(_, _Def, Defmacro) -> Defmacro.
self_clause(Line, Module) ->
{ clause, Line, [{ atom, Line, self }], [], [{ atom, Line, Module }] }.
module_clause(Module) ->
{ clause, 0, [{ atom, 0, module }], [], [{ atom, 0, Module }] }.
docs_clause(Line, Module, true) ->
docs_clause(Module, true) ->
Docs = ordsets:from_list(ets:tab2list(docs_table(Module))),
{ clause, Line, [{ atom, Line, docs }], [], [elixir_tree_helpers:abstract_syntax(Docs)] };
{ clause, 0, [{ atom, 0, docs }], [], [elixir_tree_helpers:abstract_syntax(Docs)] };
docs_clause(Line, _Module, _) ->
{ clause, Line, [{ atom, Line, docs }], [], [{ atom, Line, nil }] }.
docs_clause(_Module, _) ->
{ clause, 0, [{ atom, 0, docs }], [], [{ atom, 0, nil }] }.
moduledoc_clause(Line, Module, true) ->
Docs = 'Elixir.Module':get_attribute(Module, moduledoc),
{ clause, Line, [{ atom, Line, moduledoc }], [], [elixir_tree_helpers:abstract_syntax({ Line, Docs })] };
{ clause, 0, [{ atom, 0, moduledoc }], [], [elixir_tree_helpers:abstract_syntax({ Line, Docs })] };
moduledoc_clause(Line, _Module, _) ->
{ clause, Line, [{ atom, Line, moduledoc }], [], [{ atom, Line, nil }] }.
moduledoc_clause(_Line, _Module, _) ->
{ clause, 0, [{ atom, 0, moduledoc }], [], [{ atom, 0, nil }] }.
compile_clause(Line) ->
Info = { call, Line, { atom, Line, module_info }, [{ atom, Line, compile }] },
WrappedInfo = ?ELIXIR_WRAP_CALL(Line, 'Elixir.Keyword', 'from_enum', [Info]),
{ clause, Line, [{ atom, Line, compile }], [], [WrappedInfo] }.
else_clause(Line) ->
Info = { call, Line, { atom, Line, module_info }, [{ var, Line, atom }] },
{ clause, Line, [{ var, Line, atom }], [], [Info] }.
else_clause() ->
Info = { call, 0, { atom, 0, module_info }, [{ var, 0, atom }] },
{ clause, 0, [{ var, 0, atom }], [], [Info] }.
% HELPERS
+49 -43
View File
@@ -3,30 +3,30 @@
Nonterminals
grammar expr_list
expr block_expr fn_expr bracket_expr call_expr bracket_at_expr max_expr
expr paren_expr block_expr fn_expr bracket_expr call_expr bracket_at_expr max_expr
base_expr matched_expr matched_op_expr unmatched_expr op_expr
add_op mult_op unary_op two_op pipeline_op bin_concat_op
match_op send_op default_op when_op pipe_op in_op inc_op stab_op range_op
andand_op oror_op and_op or_op comp_expr_op colon_colon_op three_op at_op
open_paren close_paren
open_paren close_paren empty_paren
open_bracket close_bracket
open_curly close_curly
open_bit close_bit
base_comma_expr comma_expr optional_comma_expr matched_comma_expr
call_args call_args_parens call_args_no_parens parens_call
call_args call_args_parens call_args_parens_not_one call_args_no_parens parens_call
stab_expr stab_expr_list
kw_eol kw_expr kw_comma kw_base
matched_kw_expr matched_kw_comma matched_kw_base
dot_op dot_ref dot_identifier dot_op_identifier dot_do_identifier
dot_paren_identifier dot_punctuated_identifier dot_bracket_identifier
var list bracket_access bit_string tuple
fn_block do_block do_eol end_eol block_eol block_item block_list
do_block fn_eol do_eol end_eol block_eol block_item block_list
.
Terminals
identifier kw_identifier punctuated_identifier
bracket_identifier paren_identifier do_identifier block_identifier
fn fn_paren 'end' '__aliases__'
fn 'end' '__aliases__'
number signed_number atom bin_string list_string sigil
dot_call_op comp_op op_identifier
'not' 'and' 'or' 'xor' 'when' 'in' 'inlist' 'inbits' 'do'
@@ -43,8 +43,8 @@ Rootsymbol grammar.
Left 5 do.
Right 10 '->'.
Left 20 ','. % Solve nested call_args conflicts
Right 30 when_op.
Right 40 colon_colon_op.
Right 30 colon_colon_op.
Right 40 when_op.
Right 50 default_op.
Left 60 pipe_op.
Left 70 inc_op.
@@ -80,8 +80,12 @@ grammar -> '$empty' : [nil].
% Note expressions are on reverse order
expr_list -> expr : ['$1'].
expr_list -> empty_paren : [nil].
expr_list -> expr_list eol expr : ['$3'|'$1'].
paren_expr -> empty_paren : nil.
paren_expr -> expr : '$1'.
expr -> matched_expr : '$1'.
expr -> unmatched_expr : '$1'.
@@ -145,9 +149,8 @@ block_expr -> dot_punctuated_identifier call_args_no_parens do_block : build_ide
block_expr -> dot_do_identifier do_block : build_identifier('$1', '$2').
block_expr -> dot_identifier call_args_no_parens do_block : build_identifier('$1', '$2' ++ '$3').
fn_expr -> fn_paren call_args_parens fn_block : build_fn('$1', '$2', '$3').
fn_expr -> fn call_args_no_parens fn_block : build_fn('$1', '$2', '$3').
fn_expr -> fn fn_block : build_fn('$1', [], '$2').
fn_expr -> fn_eol stab_expr_list end_eol : build_fn('$1', build_stab(lists:reverse('$2'))).
fn_expr -> fn_eol '->' grammar 'end' : build_fn('$1', { '->', ?line('$2'), [{ [], build_block('$3') }] }).
fn_expr -> call_expr : '$1'.
call_expr -> dot_punctuated_identifier call_args_no_parens : build_identifier('$1', '$2').
@@ -163,8 +166,7 @@ max_expr -> parens_call call_args_parens : build_identifier('$1', '$2').
max_expr -> parens_call call_args_parens call_args_parens : { build_identifier('$1', '$2'), ?line('$1'), '$3' }.
max_expr -> dot_ref : '$1'.
max_expr -> base_expr : '$1'.
max_expr -> open_paren ')' : build_block([]).
max_expr -> open_paren expr_list close_paren : build_block('$2').
max_expr -> open_paren stab_expr_list close_paren : build_stab(lists:reverse('$2')).
bracket_expr -> dot_bracket_identifier bracket_access : build_access(build_identifier('$1', nil), '$2').
bracket_expr -> max_expr bracket_access : build_access('$1', '$2').
@@ -191,13 +193,14 @@ base_expr -> sigil : build_sigil('$1').
%% Blocks
fn_block -> '->' grammar 'end' : build_block('$2', false).
do_block -> do_eol 'end' : [[{do,nil}]].
do_block -> do_eol stab_expr_list end_eol : [[{ do, build_stab(lists:reverse('$2')) }]].
do_block -> do_eol block_list 'end' : [[{ do, nil }|'$2']].
do_block -> do_eol stab_expr_list eol block_list 'end' : [[{ do, build_stab(lists:reverse('$2')) }|'$4']].
fn_eol -> 'fn' : '$1'.
fn_eol -> 'fn' eol : '$1'.
do_eol -> 'do' : '$1'.
do_eol -> 'do' eol : '$1'.
@@ -211,7 +214,8 @@ stab_expr_list -> stab_expr : ['$1'].
stab_expr_list -> stab_expr_list eol stab_expr : ['$3'|'$1'].
stab_expr -> expr : '$1'.
stab_expr -> comma_expr stab_op expr : build_op('$2', lists:reverse('$1'), '$3').
stab_expr -> call_args_no_parens stab_op expr : build_op('$2', '$1', '$3').
stab_expr -> call_args_parens_not_one stab_op expr : build_op('$2', '$1', '$3').
block_item -> block_eol stab_expr_list eol : { ?exprs('$1'), build_stab(lists:reverse('$2')) }.
block_item -> block_eol : { ?exprs('$1'), nil }.
@@ -228,6 +232,8 @@ open_paren -> '(' eol : '$1'.
close_paren -> ')' : '$1'.
close_paren -> eol ')' : '$2'.
empty_paren -> open_paren ')' : nil.
open_bracket -> '[' : '$1'.
open_bracket -> '[' eol : '$1'.
close_bracket -> ']' : '$1'.
@@ -383,17 +389,21 @@ call_args_no_parens -> matched_comma_expr : lists:reverse('$1').
call_args_no_parens -> matched_kw_base : ['$1'].
call_args_no_parens -> matched_comma_expr ',' matched_kw_base : lists:reverse(['$3'|'$1']).
base_comma_expr -> expr ',' : ['$1'].
base_comma_expr -> base_comma_expr expr ',' : ['$2'|'$1'].
call_args_parens_not_one -> open_paren ')' : [].
call_args_parens_not_one -> open_paren matched_kw_base close_paren : ['$2'].
call_args_parens_not_one -> open_paren matched_expr ',' call_args_no_parens close_paren : ['$2'|'$4'].
comma_expr -> expr : ['$1'].
base_comma_expr -> paren_expr ',' : ['$1'].
base_comma_expr -> base_comma_expr paren_expr ',' : ['$2'|'$1'].
comma_expr -> paren_expr : ['$1'].
comma_expr -> kw_base : ['$1'].
comma_expr -> base_comma_expr : '$1'.
comma_expr -> base_comma_expr expr : ['$2'|'$1'].
comma_expr -> base_comma_expr paren_expr : ['$2'|'$1'].
comma_expr -> base_comma_expr kw_base : ['$2'|'$1'].
optional_comma_expr -> expr : '$1'.
optional_comma_expr -> expr ',' : '$1'.
optional_comma_expr -> paren_expr : '$1'.
optional_comma_expr -> paren_expr ',' : '$1'.
call_args -> comma_expr : lists:reverse('$1').
@@ -404,7 +414,7 @@ call_args_parens -> open_paren call_args close_paren : '$2'.
kw_eol -> kw_identifier : '$1'.
kw_eol -> kw_identifier eol : '$1'.
kw_expr -> kw_eol expr : { ?exprs('$1'),'$2' }.
kw_expr -> kw_eol paren_expr : { ?exprs('$1'),'$2' }.
kw_comma -> kw_expr ',' : ['$1'].
kw_comma -> kw_comma kw_expr ',' : ['$2'|'$1'].
@@ -414,6 +424,7 @@ kw_base -> kw_comma : lists:reverse('$1').
kw_base -> kw_comma kw_expr : lists:reverse(['$2'|'$1']).
matched_kw_expr -> kw_eol matched_expr : {?exprs('$1'),'$2'}.
matched_kw_expr -> kw_eol empty_paren : {?exprs('$1'),nil}.
matched_kw_comma -> matched_kw_expr : ['$1'].
matched_kw_comma -> matched_kw_expr ',' matched_kw_comma : ['$1'|'$3'].
matched_kw_base -> matched_kw_comma : '$1'.
@@ -427,13 +438,13 @@ bracket_access -> open_bracket kw_base close_bracket : { '$2', ?line('$1') }.
list -> open_bracket ']' : [].
list -> open_bracket kw_base close_bracket : '$2'.
list -> open_bracket optional_comma_expr close_bracket : ['$2'].
list -> open_bracket expr ',' call_args close_bracket : ['$2'|'$4'].
list -> open_bracket paren_expr ',' call_args close_bracket : ['$2'|'$4'].
% Tuple
tuple -> open_curly '}' : build_tuple('$1', []).
tuple -> open_curly optional_comma_expr close_curly : build_tuple('$1', ['$2']).
tuple -> open_curly expr ',' call_args close_curly : build_tuple('$1', ['$2'|'$4']).
tuple -> open_curly paren_expr ',' call_args close_curly : build_tuple('$1', ['$2'|'$4']).
% Bitstrings
@@ -475,14 +486,10 @@ build_tuple(Marker, Args) ->
%% Blocks
build_block(Exprs) -> build_block(Exprs, true).
build_block([], _) -> nil;
build_block([nil], _) -> { '__block__', 0, [nil] };
build_block([{Op,_,[_]}]=Exprs, _) when ?rearrange_uop(Op) -> { '__block__', 0, Exprs };
build_block([Expr], _) when not is_list(Expr) -> Expr;
build_block(Exprs, true) -> { '__block__', 0, lists:reverse(Exprs) };
build_block(Exprs, false) -> { '__block__', 0, Exprs }.
build_block([nil]) -> { '__block__', 0, [nil] };
build_block([{Op,_,[_]}]=Exprs) when ?rearrange_uop(Op) -> { '__block__', 0, Exprs };
build_block([Expr]) when not is_list(Expr) -> Expr;
build_block(Exprs) -> { '__block__', 0, Exprs }.
%% Dots
@@ -504,7 +511,7 @@ build_identifier({ '.', Line, _ } = Dot, Args) ->
end,
{ Dot, Line, FArgs };
build_identifier({ Keyword, Line }, Args) when Keyword == fn; Keyword == fn_paren ->
build_identifier({ Keyword, Line }, Args) when Keyword == fn ->
{ fn, Line, Args };
build_identifier({ op_identifier, Line, Identifier }, Args) ->
@@ -522,15 +529,14 @@ extract_identifier(Other) -> Other.
%% Fn
build_fn(Op, Args, Expr) ->
Line = ?line(Op),
Stab = [{do, { '->', Line, [{ Args, Expr }] }}],
{ fn, Line, [Stab] }.
build_fn(Op, Stab) ->
{ fn, ?line(Op), [[{ do, Stab }]] }.
%% Access
build_access(Expr, Access) ->
{ access, ?line(Access), [ Expr, ?op(Access) ] }.
Line = ?line(Access),
{ { '.', Line, ['Elixir-Kernel', access] }, ?line(Access), [ Expr, ?op(Access) ] }.
%% Interpolation aware
@@ -541,10 +547,10 @@ build_bin_string({ bin_string, _Line, [H] }) when is_binary(H) -> H;
build_bin_string({ bin_string, Line, Args }) -> { '<<>>', Line, Args }.
build_list_string({ list_string, _Line, [H] }) when is_binary(H) -> binary_to_list(H);
build_list_string({ list_string, Line, Args }) -> { binary_to_list, Line, [{ '<<>>', Line, Args}] }.
build_list_string({ list_string, Line, Args }) -> { { '.', Line, [erlang, binary_to_list] }, Line, [{ '<<>>', Line, Args}] }.
build_atom({ atom, _Line, Atom }) when is_atom(Atom) -> Atom;
build_atom({ atom, Line, Args }) -> { binary_to_atom, Line, [{ '<<>>', Line, Args }, utf8] }.
build_atom({ atom, Line, Args }) -> { { '.', Line, [erlang, binary_to_atom] }, Line, [{ '<<>>', Line, Args }, utf8] }.
%% Keywords
@@ -552,15 +558,15 @@ build_stab([{ '->', Line, [Left, Right] }|T]) ->
{ '->', Line, build_stab(T, Left, [Right], []) };
build_stab(Else) ->
build_block(Else, false).
build_block(Else).
build_stab([{ '->', _, [Left, Right] }|T], Marker, Temp, Acc) ->
H = { Marker, build_block(Temp) },
H = { Marker, build_block(lists:reverse(Temp)) },
build_stab(T, Left, [Right], [H|Acc]);
build_stab([H|T], Marker, Temp, Acc) ->
build_stab(T, Marker, [H|Temp], Acc);
build_stab([], Marker, Temp, Acc) ->
H = { Marker, build_block(Temp) },
H = { Marker, build_block(lists:reverse(Temp)) },
lists:reverse([H|Acc]).
+29 -8
View File
@@ -1,5 +1,5 @@
-module(elixir_quote).
-export([quote/3, linify/2]).
-export([quote/3, linify/2, join_quoted/5]).
-include("elixir.hrl").
%% Apply the line from site call on quoted contents.
@@ -15,9 +15,28 @@ linify(Line, List) when is_list(List) ->
linify(_, Else) -> Else.
%% Join quoted checks for quote arguments at runtime
%% in order to properly insert them into the tree
join_quoted(Line, Left, { '__aliases__', _, Args }, nil, _File) ->
{ '__aliases__', Line, [Left|Args] };
join_quoted(Line, Left, Right, nil, _File) when is_atom(Right) ->
case atom_to_list(Right) of
"Elixir-" ++ _ ->
{ '__aliases__', Line, [Left, Right] };
_ ->
{ { '.', Line, [Left, Right] }, Line, [] }
end;
join_quoted(Line, Left, Right, Args, _File) when is_atom(Right) ->
{ { '.', Line, [Left, Right] }, Line, Args };
join_quoted(Line, _Left, _Right, _Args, File) ->
elixir_errors:syntax_error(Line, File, "expected unquote after dot to return an atom or an alias").
%% Translation
quote({ 'unquote_splicing', Line, _ } = Expr, Q, S) ->
quote({ 'unquote_splicing', Line, _ } = Expr, #elixir_quote{unquote=true} = Q, S) ->
do_quote({ '__block__', Line, [Expr] }, Q, S);
quote(Else, Q, S) ->
@@ -26,13 +45,15 @@ quote(Else, Q, S) ->
do_quote({ unquote, _Line, [Expr] }, #elixir_quote{unquote=true}, S) ->
elixir_translator:translate_each(Expr, S);
do_quote({ { { '.', Line, [Left, unquote] }, _Line, [Expr] }, CallLine, Args }, Q, S) ->
Rewritten = { { '.', Line, [Left, { unquote, Line, [Expr] }] }, CallLine, Args },
do_quote(Rewritten, Q, S);
do_quote({ { { '.', Line, [Left, unquote] }, _, [Expr] }, _, Args }, #elixir_quote{unquote=true} = Q, S) ->
All = [Left, { unquote, Line, [Expr] }, Args, S#elixir_scope.file],
{ TAll, TS } = lists:mapfoldl(fun(X, Acc) -> do_quote(X, Q, Acc) end, S, All),
{ ?ELIXIR_WRAP_CALL(Line, elixir_quote, join_quoted, [line(Line, Q)|TAll]), TS };
do_quote({ { '.', Line, [Left, unquote] }, _Line, [Expr] }, Q, S) ->
Rewritten = { { '.', Line, [Left, { unquote, Line, [Expr] }] }, Line, [] },
do_quote(Rewritten, Q, S);
do_quote({ { '.', Line, [Left, unquote] }, _, [Expr] }, #elixir_quote{unquote=true} = Q, S) ->
All = [Left, { unquote, Line, [Expr] }, nil, S#elixir_scope.file],
{ TAll, TS } = lists:mapfoldl(fun(X, Acc) -> do_quote(X, Q, Acc) end, S, All),
{ ?ELIXIR_WRAP_CALL(Line, elixir_quote, join_quoted, [line(Line, Q)|TAll]), TS };
do_quote({ Left, Line, nil }, Q, S) when is_atom(Left) ->
Tuple = { tuple, Line, [
+70 -39
View File
@@ -3,9 +3,12 @@
-module(elixir_scope).
-export([translate_var/4,
build_erl_var/2, build_ex_var/2,
serialize/1, deserialize/1, deserialize/2,
build_erl_var/3, build_ex_var/3,
build_erl_var/4, build_ex_var/4,
serialize/1, deserialize/1,
serialize_with_vars/2, deserialize_with_vars/2,
to_erl_env/1, to_ex_env/1, filename/1,
umergev/2, umergec/2
umergev/2, umergec/2, merge_clause_vars/2
]).
-include("elixir.hrl").
-compile({parse_transform, elixir_transform}).
@@ -14,45 +17,61 @@ translate_var(Line, Name, Kind, S) ->
Vars = S#elixir_scope.vars,
case Name of
'_' -> { {var, Line, Name}, S };
'_' ->
{ { var, Line, Name }, S };
_ ->
case S#elixir_scope.context of
assign ->
TempVars = S#elixir_scope.temp_vars,
case { orddict:is_key(Name, Vars), orddict:find(Name, TempVars) } of
case { orddict:is_key({ Name, Kind }, Vars), orddict:find(Name, TempVars) } of
{ true, { ok, Kind } } ->
{ {var, Line, orddict:fetch(Name, Vars) }, S };
{ { var, Line, orddict:fetch({ Name, Kind }, Vars) }, S };
{ Else, _ } ->
{ NewVar, NS } = case Else or S#elixir_scope.noname of
true -> build_erl_var(Line, S);
false -> { {var, Line, Name}, S }
{ NewVar, NS } = if
Kind == quoted -> build_erl_var(Line, S);
Else -> build_erl_var(Line, Name, S);
S#elixir_scope.noname -> build_erl_var(Line, Name, S);
true -> { { var, Line, Name }, S }
end,
RealName = element(3, NewVar),
ClauseVars = S#elixir_scope.clause_vars,
{ NewVar, NS#elixir_scope{
vars=orddict:store(Name, RealName, Vars),
vars=orddict:store({ Name, Kind }, RealName, Vars),
temp_vars=orddict:store(Name, Kind, TempVars),
clause_vars=orddict:store({ Name, Kind }, RealName, ClauseVars)
clause_vars=if
ClauseVars == nil -> nil;
true -> orddict:store({ Name, Kind }, RealName, ClauseVars)
end
} }
end;
_ ->
case orddict:is_key(Name, Vars) of
false -> elixir_translator:translate_each({Name, Line, []}, S);
true -> { {var, Line, orddict:fetch(Name, Vars) }, S }
case orddict:find({ Name, Kind }, Vars) of
{ ok, VarName } -> { { var, Line, VarName }, S };
error -> elixir_translator:translate_each({ Name, Line, [] }, S)
end
end
end.
% Handle variables translation
build_erl_var(Line, #elixir_scope{counter=Counter} = S) ->
NS = S#elixir_scope{counter=Counter+1},
Var = { var, Line, ?ELIXIR_ATOM_CONCAT(["_@", Counter]) },
build_ex_var(Line, S) -> build_ex_var(Line, '', "_", S).
build_erl_var(Line, S) -> build_erl_var(Line, '', "_", S).
build_ex_var(Line, Key, S) -> build_ex_var(Line, Key, Key, S).
build_erl_var(Line, Key, S) -> build_erl_var(Line, Key, Key, S).
build_var_counter(Key, #elixir_scope{counter=Counter} = S) ->
New = orddict:update_counter(Key, 1, Counter),
{ orddict:fetch(Key, New), S#elixir_scope{counter=New} }.
build_erl_var(Line, Key, Name, S) ->
{ Counter, NS } = build_var_counter(Key, S),
Var = { var, Line, ?ELIXIR_ATOM_CONCAT([Name, "@", Counter]) },
{ Var, NS }.
build_ex_var(Line, #elixir_scope{counter=Counter} = S) ->
NS = S#elixir_scope{counter=Counter+1},
Var = { ?ELIXIR_ATOM_CONCAT(["_@", Counter]), Line, nil },
build_ex_var(Line, Key, Name, S) ->
{ Counter, NS } = build_var_counter(Key, S),
Var = { ?ELIXIR_ATOM_CONCAT([Name, "@", Counter]), Line, quoted },
{ Var, NS }.
% Handle Macro.Env conversion
@@ -65,8 +84,8 @@ to_ex_env({ Line, Tuple }) when element(1, Tuple) == 'Elixir.Macro.Env' ->
to_ex_env({ Line, #elixir_scope{module=Module,file=File,
function=Function,aliases=Aliases,context=Context,
requires=Requires,macros=Macros} }) ->
{ 'Elixir.Macro.Env', Module, File, Line, Function, Aliases, Context, Requires, Macros }.
requires=Requires,macros=Macros,functions=Functions} }) ->
{ 'Elixir.Macro.Env', Module, File, Line, Function, Aliases, Context, Requires, Functions, Macros }.
filename(#elixir_scope{file=File}) -> File;
filename(Other) -> element(3, Other).
@@ -75,26 +94,36 @@ filename(Other) -> element(3, Other).
serialize(S) ->
elixir_tree_helpers:abstract_syntax(
{ S#elixir_scope.file, S#elixir_scope.functions, S#elixir_scope.check_clauses, S#elixir_scope.macro,
{ S#elixir_scope.file, S#elixir_scope.functions, S#elixir_scope.check_clauses,
S#elixir_scope.requires, S#elixir_scope.macros, S#elixir_scope.aliases, S#elixir_scope.scheduled }
).
serialize_with_vars(Line, S) ->
{ Vars, _ } = orddict:fold(fun({ Key, Kind }, Value, { Acc, Counter }) ->
{ { cons, Line, { tuple, Line, [
{ atom, Line, Key },
{ atom, Line, Kind },
{ atom, Line, ?ELIXIR_ATOM_CONCAT(["_@", Counter]) },
{ var, Line, Value }
] }, Acc }, Counter + 1 }
end, { { nil, Line }, 0 }, S#elixir_scope.vars),
{ serialize(S), Vars }.
% Fill in the scope with the variables serialization set in serialize_scope.
deserialize(Tuple) -> deserialize(Tuple, []).
deserialize(Tuple) -> deserialize_with_vars(Tuple, []).
deserialize({ File, Functions, CheckClauses, Macro, Requires, Macros, Aliases, Scheduled }, Vars) ->
deserialize_with_vars({ File, Functions, CheckClauses, Requires, Macros, Aliases, Scheduled }, Vars) ->
#elixir_scope{
file=File,
functions=Functions,
check_clauses=CheckClauses,
macro=Macro,
requires=Requires,
macros=Macros,
aliases=Aliases,
scheduled=Scheduled,
vars=orddict:from_list(Vars),
counter=length(Vars)
counter=[{'',length(Vars)}]
}.
% Receives two scopes and return a new scope based on the second
@@ -103,14 +132,11 @@ deserialize({ File, Functions, CheckClauses, Macro, Requires, Macros, Aliases, S
umergev(S1, S2) ->
V1 = S1#elixir_scope.vars,
V2 = S2#elixir_scope.vars,
Q1 = S1#elixir_scope.quote_vars,
Q2 = S2#elixir_scope.quote_vars,
C1 = S1#elixir_scope.clause_vars,
C2 = S2#elixir_scope.clause_vars,
S2#elixir_scope{
vars=orddict:merge(fun var_merger/3, V1, V2),
quote_vars=orddict:merge(fun var_merger/3, Q1, Q2),
clause_vars=orddict:merge(fun clause_var_merger/3, C1, C2)
clause_vars=merge_clause_vars(C1, C2)
}.
% Receives two scopes and return a new scope based on the first
@@ -127,16 +153,21 @@ umergec(S1, S2) ->
% Merge variables trying to find the most recently created.
clause_var_merger({ Var, _ }, K1, K2) ->
var_merger(Var, K1, K2).
merge_clause_vars(nil, _C2) -> nil;
merge_clause_vars(_C1, nil) -> nil;
merge_clause_vars(C1, C2) ->
orddict:merge(fun var_merger/3, C1, C2).
var_merger(Var, Var, K2) -> K2;
var_merger(Var, K1, Var) -> K1;
var_merger({ Var, _ }, Var, K2) -> K2;
var_merger({ Var, _ }, K1, Var) -> K1;
var_merger(_Var, K1, K2) ->
V1 = var_number(atom_to_list(K1)),
V2 = var_number(atom_to_list(K2)),
if V1 > V2 -> K1;
true -> K2
V1 = var_number(atom_to_list(K1), []),
V2 = var_number(atom_to_list(K2), []),
case V1 > V2 of
true -> K1;
false -> K2
end.
var_number([_,_|T]) -> list_to_integer(T).
var_number([$@|T], _Acc) -> var_number(T, []);
var_number([H|T], Acc) -> var_number(T, [H|Acc]);
var_number([], Acc) -> list_to_integer(lists:reverse(Acc)).
+11 -15
View File
@@ -269,10 +269,10 @@ tokenize([$:,T|Rest], Line, Scope, Tokens) when ?comp1(T); ?op1(T); T == $&; T =
% End of line
tokenize(";" ++ Rest, Line, Scope, []) ->
tokenize(Rest, Line, Scope, eol(Line, $;, []));
tokenize(Rest, Line, Scope, eol(Line, ';', []));
tokenize(";" ++ Rest, Line, Scope, [Top|Tokens]) when element(1, Top) /= eol ->
tokenize(Rest, Line, Scope, eol(Line, $;, [Top|Tokens]));
tokenize(Rest, Line, Scope, eol(Line, ';', [Top|Tokens]));
tokenize("\\\n" ++ Rest, Line, Scope, Tokens) ->
tokenize(Rest, Line + 1, Scope, Tokens);
@@ -281,10 +281,10 @@ tokenize("\\\r\n" ++ Rest, Line, Scope, Tokens) ->
tokenize(Rest, Line + 1, Scope, Tokens);
tokenize("\n" ++ Rest, Line, Scope, Tokens) ->
tokenize(Rest, Line + 1, Scope, eol(Line, $\n, Tokens));
tokenize(Rest, Line + 1, Scope, eol(Line, newline, Tokens));
tokenize("\r\n" ++ Rest, Line, Scope, Tokens) ->
tokenize(Rest, Line + 1, Scope, eol(Line, $\n, Tokens));
tokenize(Rest, Line + 1, Scope, eol(Line, newline, Tokens));
% Stand-alone tokens
@@ -702,7 +702,7 @@ next_is_block([$d,$o|_]) ->
next_is_block(_) ->
false.
add_token_with_nl(Left, [{eol,_,$\n}|T]) -> [Left|T];
add_token_with_nl(Left, [{eol,_,newline}|T]) -> [Left|T];
add_token_with_nl(Left, T) -> [Left|T].
% Error handling
@@ -729,7 +729,7 @@ handle_terminator(Token, #scope{terminators=Terminators} = Scope) ->
New -> Scope#scope{terminators=New}
end.
check_terminator({ S, Line }, Terminators) when S == 'fn'; S == 'fn_paren' ->
check_terminator({ S, Line }, Terminators) when S == 'fn' ->
[{ fn, Line }|Terminators];
check_terminator({ S, _ } = New, Terminators) when
@@ -770,16 +770,12 @@ terminator('{') -> '}';
terminator('<<') -> '>>'.
% Keywords check
handle_keyword(Line, paren_identifier, fn, Tokens) ->
[{ 'fn_paren', Line }|Tokens];
handle_keyword(Line, Identifier, Atom, [{ '.', _ }|_] = Tokens) ->
[{ Identifier, Line, Atom }|Tokens];
handle_keyword(Line, Identifier, Atom, Tokens) when Identifier == bracket_identifier; Identifier == paren_identifier ->
case keyword(Atom) of
true -> [{ Atom, Line }|Tokens];
_ -> [{ Identifier, Line, Atom }|Tokens]
end;
handle_keyword(Line, Identifier, Atom, Tokens) when Identifier == identifier; Identifier == do_identifier ->
handle_keyword(Line, Identifier, Atom, Tokens) when
Identifier == identifier; Identifier == do_identifier;
Identifier == bracket_identifier; Identifier == paren_identifier ->
case keyword(Atom) of
true -> [{ Atom, Line }|Tokens];
op -> add_token_with_nl({ Atom, Line }, Tokens);
+35 -60
View File
@@ -61,7 +61,7 @@ translate_each({ '__block__', Line, Args }, S) when is_list(Args) ->
{ TArgs, NS } = translate(Args, S),
{ { block, Line, TArgs }, NS };
translate_each({ '__scope__', _Line, [[{file,File}],Expr] }, S) ->
translate_each({ '__scope__', _Line, [[{file,File}],[{do,Expr}]] }, S) ->
Old = S#elixir_scope.file,
{ TExpr, TS } = translate_each(Expr, S#elixir_scope{file=File}),
{ TExpr, TS#elixir_scope{file=Old} };
@@ -77,9 +77,9 @@ translate_each({ '__op__', Line, [Op, Left, Right] }, S) when is_atom(Op) ->
{ { op, Line, convert_op(Op), TLeft, TRight }, NS };
translate_each({ '__ambiguousop__', Line, [Var, H|T] }, S) ->
{ Name, _, _ } = Var,
{ Name, _, Kind } = Var,
case orddict:find(Name, S#elixir_scope.vars) of
case orddict:find({ Name, Kind }, S#elixir_scope.vars) of
error -> translate_each({ Name, Line, [H|T] }, S);
_ ->
case T of
@@ -103,7 +103,7 @@ translate_each({ alias, Line, [Ref, KV] }, S) ->
{ atom, _, Old } ->
{ New, SF } = case lists:keyfind(as, 1, KV) of
Opt when Opt == { as, true }; Opt == false ->
{ elixir_aliases:last(Old), SR };
{ elixir_aliases:last(Old), SR };
{ as, false } ->
{ Old, SR };
{ as, Other } ->
@@ -259,7 +259,7 @@ translate_each({ quote, GivenLine, [T] }, S) when is_list(T) ->
{ DefaultLine, WrappedExprs } = case lists:keyfind(location, 1, T) of
{ location, keep } ->
Scoped = { '__scope__', GivenLine, [[{file,S#elixir_scope.file}],Exprs] },
Scoped = { '__scope__', GivenLine, [[{file,S#elixir_scope.file}],[{do,Exprs}]] },
{ keep, Scoped };
_ ->
{ 0, Exprs}
@@ -284,7 +284,7 @@ translate_each({ quote, GivenLine, [T] }, S) when is_list(T) ->
translate_each({ quote, GivenLine, [_] }, S) ->
syntax_error(GivenLine, S#elixir_scope.file, "invalid args for quote");
%% Functions
translate_each({ fn, Line, [[{do, { '->', _, Pairs }}]] }, S) ->
@@ -330,41 +330,23 @@ translate_each({ 'super?', Line, [] }, S) ->
%% Variables
translate_each({ '^', Line, [ { Name, _, Args } ] }, S) ->
Dict = case Args of
nil -> S#elixir_scope.vars;
quoted -> S#elixir_scope.quote_vars;
_ ->
syntax_error(Line, S#elixir_scope.file, "cannot use ^ with expression at ^~s, ^ must be used only with variables", [Name])
end,
translate_each({ '^', Line, [ { Name, _, Kind } ] }, S) when is_list(Kind) ->
syntax_error(Line, S#elixir_scope.file, "cannot use ^ with expression at ^~s, ^ must be used only with variables", [Name]);
Result = case S#elixir_scope.context of
assign ->
case orddict:find(Name, Dict) of
error -> "unbound variable ^~s";
{ ok, Value } -> { {var, Line, Value}, S }
end;
_ -> "cannot access variable ^~s outside of assignment"
end,
case is_list(Result) of
true ->
syntax_error(Line, S#elixir_scope.file, Result, [Name]);
false ->
Result
translate_each({ '^', Line, [ { Name, _, Kind } ] }, #elixir_scope{context=assign} = S) when is_atom(Kind) ->
case orddict:find({ Name, Kind }, S#elixir_scope.vars) of
{ ok, Value } ->
{ { var, Line, Value }, S };
error ->
syntax_error(Line, S#elixir_scope.file, "unbound variable ^~s", [Name])
end;
translate_each({ Name, Line, quoted }, S) when is_atom(Name) ->
NewS = S#elixir_scope{vars=S#elixir_scope.quote_vars,noname=true},
{ TVar, VS } = elixir_scope:translate_var(Line, Name, quoted, NewS),
{ TVar, VS#elixir_scope{
quote_vars=VS#elixir_scope.vars,
noname=S#elixir_scope.noname,
vars=S#elixir_scope.vars
} };
translate_each({ '^', Line, [ { Name, _, Kind } ] }, S) when is_atom(Kind) ->
syntax_error(Line, S#elixir_scope.file,
"cannot access variable ^~s outside of assignment", [Name]);
translate_each({ Name, Line, nil }, S) when is_atom(Name) ->
elixir_scope:translate_var(Line, Name, nil, S);
translate_each({ Name, Line, Kind }, S) when is_atom(Name), (Kind == nil orelse Kind == quoted) ->
elixir_scope:translate_var(Line, Name, Kind, S);
%% Local calls
@@ -408,14 +390,6 @@ translate_each({ { '.', _, [Left, Right] }, Line, Args } = Original, S) when is_
Callback = fun() -> translate_apply(Line, TLeft, TRight, Args, S, SL, SR) end,
case TLeft of
{ atom, _, 'Elixir.Erlang' } ->
elixir_errors:deprecation(Line, S#elixir_scope.file, "Erlang. syntax is deprecated"),
case Args of
[] -> { { atom, Line, Right }, S };
_ ->
Message = "invalid args for Erlang.~s expression",
syntax_error(Line, S#elixir_scope.file, Message, [Right])
end;
{ atom, _, Receiver } ->
elixir_dispatch:dispatch_require(Line, Receiver, Right, Args, umergev(SL, SR), Callback);
_ ->
@@ -470,7 +444,14 @@ translate_fn(Line, Clauses, S) ->
end,
{ TClauses, NS } = lists:mapfoldl(Transformer, S, Clauses),
{ { 'fun', Line, {clauses, TClauses} }, umergec(S, NS) }.
Arities = [length(Args) || { clause, _Line, Args, _Guards, _Exprs } <- TClauses],
case length(lists:usort(Arities)) of
1 ->
{ { 'fun', Line, { clauses, TClauses } }, umergec(S, NS) };
_ ->
syntax_error(Line, S#elixir_scope.file, "cannot mix clauses with different arities in function definition")
end.
translate_local(Line, Name, Args, #elixir_scope{local=nil} = S) ->
{ TArgs, NS } = translate_args(Args, S),
@@ -519,11 +500,9 @@ translate_apply(Line, TLeft, TRight, Args, S, SL, SR) ->
Optimize = case (Args == []) orelse lists:last(Args) of
{ '|', _, _ } -> false;
_ ->
case { TLeft, TRight } of
{ { Kind, _, _ }, { atom, _, _ } } when Kind == var; Kind == tuple; Kind == atom ->
true;
_ ->
false
case TRight of
{ atom, _, _ } -> true;
_ -> false
end
end,
@@ -565,7 +544,7 @@ rellocate_ambiguous_op({ Call, Line, [H|T] }, Var) ->
translate_comprehension(Line, Kind, Args, S) ->
case elixir_tree_helpers:split_last(Args) of
{ Cases, [{do,Expr}] } ->
{ TCases, SC } = lists:mapfoldl(fun translate_comprehension_clause/2, S, Cases),
{ TCases, SC } = lists:mapfoldl(fun(C, Acc) -> translate_comprehension_clause(Line, C, Acc) end, S, Cases),
{ TExpr, SE } = translate_comprehension_do(Line, Kind, Expr, SC),
{ { Kind, Line, TExpr, TCases }, umergec(S, SE) };
_ ->
@@ -581,20 +560,16 @@ translate_comprehension_do(Line, bc, _Expr, S) ->
translate_comprehension_do(_Line, _Kind, Expr, S) ->
translate_each(Expr, S).
translate_comprehension_clause({inbits, Line, [Left, Right]}, S) ->
translate_comprehension_clause(_Line, {inbits, Line, [Left, Right]}, S) ->
{ TRight, SR } = translate_each(Right, S),
{ TLeft, SL } = elixir_clauses:assigns(fun elixir_translator:translate_each/2, Left, SR),
{ { b_generate, Line, TLeft, TRight }, SL };
translate_comprehension_clause({inlist, Line, [Left, Right]}, S) ->
translate_comprehension_clause(_Line, {inlist, Line, [Left, Right]}, S) ->
{ TRight, SR } = translate_each(Right, S),
{ TLeft, SL } = elixir_clauses:assigns(fun elixir_translator:translate_each/2, Left, SR),
{ { generate, Line, TLeft, TRight }, SL };
translate_comprehension_clause(X, S) ->
translate_comprehension_clause(Line, X, S) ->
{ TX, TS } = translate_each(X, S),
Line = case X of
{ _, L, _ } -> L;
_ -> 0
end,
{ elixir_tree_helpers:convert_to_boolean(Line, TX, true, false), TS }.
elixir_tree_helpers:convert_to_boolean(Line, TX, true, false, TS).
+33 -20
View File
@@ -5,7 +5,7 @@
-module(elixir_tree_helpers).
-compile({parse_transform, elixir_transform}).
-export([abstract_syntax/1, split_last/1, cons_to_list/1,
convert_to_boolean/4, returns_boolean/1,
convert_to_boolean/5, returns_boolean/1,
build_list/4, build_list/5, build_simple_list/2,
build_reverse_list/4, build_reverse_list/5, build_simple_reverse_list/2]).
-include("elixir.hrl").
@@ -84,34 +84,47 @@ returns_boolean({ call, _, { remote, _, { atom, _, erlang }, { atom, _, Fun } },
returns_boolean({ call, _, { remote, _, { atom, _, erlang }, { atom, _, Fun } }, [_,_] }) when
Fun == is_function -> true;
returns_boolean({ call, _, { remote, _, { atom, _, erlang }, { atom, _, Fun } }, [_,_,_] }) when
Fun == function_exported -> true;
returns_boolean({ atom, _, Bool }) when Bool == true; Bool == false -> true;
returns_boolean({ 'case', _, _, Clauses }) ->
lists:all(fun
({clause,_,_,_,[Expr]}) -> returns_boolean(Expr);
(_) -> false
end, Clauses);
returns_boolean(_) -> false.
convert_to_boolean(Line, Expr, Bool, InGuard) ->
convert_to_boolean(Line, Expr, Bool, InGuard, S) ->
case { returns_boolean(Expr), Bool } of
{ true, true } -> Expr;
{ true, false } -> { op, Line, 'not', Expr };
_ -> do_convert_to_boolean(Line, Expr, Bool, InGuard)
{ true, true } -> { Expr, S };
{ true, false } -> { { op, Line, 'not', Expr }, S };
_ -> do_convert_to_boolean(Line, Expr, Bool, InGuard, S)
end.
do_convert_to_boolean(Line, Expr, Bool, false) ->
Any = [{var, Line, '_'}],
False = [{atom,Line,false}],
Nil = [{atom,Line,nil}],
%% Notice we use a temporary var and bundle nil
%% and false checks in the same clause since
%% it makes dialyzer happy.
do_convert_to_boolean(Line, Expr, Bool, false, S) ->
Any = { var, Line, '_' },
{ Var, TS } = elixir_scope:build_erl_var(Line, S),
OrElse = do_guarded_convert_to_boolean(Line, Var, 'orelse', '=='),
FalseResult = [{atom,Line,not Bool}],
TrueResult = [{atom,Line,Bool}],
FalseResult = { atom,Line,not Bool },
TrueResult = { atom,Line,Bool },
{ 'case', Line, Expr, [
{ clause, Line, False, [], FalseResult },
{ clause, Line, Nil, [], FalseResult },
{ clause, Line, Any, [], TrueResult }
] };
{ { 'case', Line, Expr, [
{ clause, Line, [Var], [[OrElse]], [FalseResult] },
{ clause, Line, [Any], [], [TrueResult] }
] }, TS };
do_convert_to_boolean(Line, Expr, true, true) ->
do_guarded_convert_to_boolean(Line, Expr, 'andalso', '/=');
do_convert_to_boolean(Line, Expr, true, true, S) ->
{ do_guarded_convert_to_boolean(Line, Expr, 'andalso', '/='), S };
do_convert_to_boolean(Line, Expr, false, true) ->
do_guarded_convert_to_boolean(Line, Expr, 'orelse', '==').
do_convert_to_boolean(Line, Expr, false, true, S) ->
{ do_guarded_convert_to_boolean(Line, Expr, 'orelse', '=='), S }.
do_guarded_convert_to_boolean(Line, Expr, Op, Comp) ->
Left = { op, Line, Comp, Expr, { atom, Line, false } },
+18 -3
View File
@@ -1,5 +1,5 @@
-module(elixir_try).
-export([clauses/3]).
-export([clauses/3, format_error/1]).
-import(elixir_scope, [umergec/2]).
-include("elixir.hrl").
-compile({parse_transform, elixir_transform}).
@@ -86,6 +86,16 @@ normalize_rescue(_, { in, Line, [Left, Right] }, S) ->
end
end;
normalize_rescue(_, { '=', Line, [{ '__aliases__', _, _ } = Alias, { Name, _, Atom } = Var] }, S)
when is_atom(Name) and is_atom(Atom) ->
elixir_errors:handle_file_warning(S#elixir_scope.file, { Line, ?MODULE, { rescue_no_match, Var, Alias } }),
false;
normalize_rescue(_, { '=', Line, [{ Name, _, Atom } = Var, { '__aliases__', _, _ } = Alias] }, S)
when is_atom(Name) and is_atom(Atom) ->
elixir_errors:handle_file_warning(S#elixir_scope.file, { Line, ?MODULE, { rescue_no_match, Var, Alias } }),
false;
normalize_rescue(Line, Condition, S) ->
case elixir_translator:translate_each(Condition, S#elixir_scope{context=assign}) of
{ { atom, _, Atom }, _ } ->
@@ -94,7 +104,6 @@ normalize_rescue(Line, Condition, S) ->
false
end.
%% Convert rescue clauses into guards.
rescue_guards(_, Var, nil, _) -> { Var, false };
@@ -240,7 +249,7 @@ validate_rescue_access(Line, { '=', _, [Left, Right] }, S) ->
validate_rescue_access(Line, Left, S),
validate_rescue_access(Line, Right, S);
validate_rescue_access(Line, { 'access', _, [Element, _] }, S) ->
validate_rescue_access(Line, { { '.', _, ['Elixir.Kernel', 'access'] }, _, [Element, _] }, S) ->
case elixir_translator:translate_each(Element, S) of
{ { atom, _, Atom }, _ } ->
case lists:member(Atom, erlang_rescues()) of
@@ -254,6 +263,12 @@ validate_rescue_access(_, _, _) -> [].
%% Helpers
format_error({ rescue_no_match, Var, Alias }) ->
VarBinary = 'Elixir.Macro':to_binary(Var),
AliasBinary = 'Elixir.Macro':to_binary(Alias),
Message = "rescue clause (~s = ~s) can never match, maybe you meant to write: ~s in [~s] ?",
io_lib:format(Message, [AliasBinary, VarBinary, VarBinary, AliasBinary]).
is_var({ Name, _, Atom }) when is_atom(Name), is_atom(Atom) -> true;
is_var(_) -> false.
+32 -6
View File
@@ -6,16 +6,42 @@ defmodule BehaviourTest do
defmodule Sample do
use Behaviour
@doc "Doc"
defcallback foo(foo, bar // [], bat // [])
@doc "Foo"
defcallback foo(atom, binary) :: binary
@doc "Bar"
defcallback bar(External.hello, my_var :: binary) :: binary
end
test :docs_are_defined do
assert [_, _] = Sample.__info__(:docs)
test :docs do
docs = Enum.sort(Sample.__behaviour__(:docs))
assert docs == [
{{:bar,2},13,"Bar"},
{{:foo,2},10,"Foo"}
]
end
test :callbacks do
assert [{:foo,3},{:foo,2},{:foo,1}] = Sample.behaviour_info(:callbacks)
assert Sample.__behaviour__(:callbacks) == [foo: 2, bar: 2]
end
test :specs do
assert length(Keyword.get_values(Sample.module_info[:attributes], :callback)) == 2
end
test :default_is_not_supported do
assert_raise ArgumentError, fn ->
defmodule WithDefault do
use Behaviour
defcallback hello(num // 0 :: integer) :: integer
end
end
assert_raise ArgumentError, fn ->
defmodule WithDefault do
use Behaviour
defcallback hello(num :: integer // 0) :: integer
end
end
end
end
@@ -110,6 +110,10 @@ defmodule Binary.Inspect.TupleTest do
assert inspect({ List, 1 }) == "{List,1}"
end
test :with_record_like_pseudo_exception do
assert inspect({ Other, :__exception__, 1 }) == "{Other,:__exception__,1}"
end
defrecord Config, a: 1, b: []
test :with_record do
@@ -155,6 +159,10 @@ defmodule Binary.Inspect.ListTest do
assert inspect(["123": 1]) == %b(["123": 1])
end
test :non_keyword do
assert inspect([{ Regex, 1 }]) == "[{Regex,1}]"
end
test :non_printable do
assert inspect([{:b, 1},{:a,1}]) == "[b: 1, a: 1]"
end
+9 -1
View File
@@ -52,6 +52,14 @@ bar
assert "\18" == <<1,?8>>
end
test :hex do
assert "\xE9" == "é"
assert "\xFF" == "ÿ"
assert "\x{A}"== "\n"
assert "\x{E9}"== "é"
assert "\x{10FFFF}" == <<244,143,191,191>>
end
test :match do
assert is_match?("ab", ?a)
assert not is_match?("cd", ?a)
@@ -59,7 +67,7 @@ bar
test :pattern_match do
s = 16
assert <<a, b :: size(s)>> = "foo"
assert <<_a, _b :: size(s)>> = "foo"
end
test :partial_application do
+9 -4
View File
@@ -28,23 +28,28 @@ defmodule CodeTest do
assert CodeTest.Sample.eval_quoted_info() == { CodeTest.Sample, "sample.ex", 13 }
end
test :eval_quoted_with_env do
alias :lists, as: MyList
assert Code.eval_quoted(quote(do: MyList.flatten [[1,2,3]]), [], __ENV__) == { [1,2,3],[] }
end
test :require do
Code.require_file fixture_path("code_sample.exs")
assert fixture_path("code_sample.exs") in Code.loaded_files
assert fixture_path("code_sample.exs") inlist Code.loaded_files
assert Code.require_file(fixture_path("code_sample.exs")) == nil
Code.unload_files [fixture_path("code_sample.exs")]
refute fixture_path("code_sample.exs") in Code.loaded_files
refute fixture_path("code_sample.exs") inlist Code.loaded_files
assert Code.require_file(fixture_path("code_sample.exs")) != nil
end
test :path_manipulation do
path = File.expand_path("../binary", __FILE__)
Code.prepend_path path
assert binary_to_list(path) in :code.get_path
assert binary_to_list(path) inlist :code.get_path
Code.delete_path path
refute binary_to_list(path) in :code.get_path
refute binary_to_list(path) inlist :code.get_path
end
test :file do
+14 -6
View File
@@ -15,16 +15,16 @@ defmodule DictTest.Common do
dict = new_dict [{"first_key", 1}, {"second_key", 2}]
assert 2 == Dict.size dict
assert ["first_key", "second_key"] == List.sort Dict.keys dict
assert [1, 2] == List.sort Dict.values dict
assert ["first_key", "second_key"] == Enum.sort Dict.keys dict
assert [1, 2] == Enum.sort Dict.values dict
end
test :new_pairs_with_transform do
dict = new_dict [{1}, {2}, {3}], fn {x} -> { <<x + 64>>, x } end
assert 3 == Dict.size dict
assert ["A", "B", "C"] == List.sort Dict.keys dict
assert [1, 2, 3] == List.sort Dict.values dict
assert ["A", "B", "C"] == Enum.sort Dict.keys dict
assert [1, 2, 3] == Enum.sort Dict.values dict
end
test :get do
@@ -34,6 +34,14 @@ defmodule DictTest.Common do
assert "default" == Dict.get(empty_dict, "first_key", "default")
end
test :get! do
assert 1 == Dict.get!(new_dict, "first_key")
assert 2 == Dict.get!(new_dict, "second_key")
assert_raise KeyError, fn ->
Dict.get!(new_dict, "other_key")
end
end
test :put do
dict = Dict.put(new_dict, "first_key", {1})
assert {1} == Dict.get dict, "first_key"
@@ -41,12 +49,12 @@ defmodule DictTest.Common do
end
test :keys do
assert ["first_key", "second_key"] == List.sort Dict.keys new_dict
assert ["first_key", "second_key"] == Enum.sort Dict.keys new_dict
assert [] == Dict.keys empty_dict
end
test :values do
assert [1, 2] ==List.sort Dict.values(new_dict)
assert [1, 2] ==Enum.sort Dict.values(new_dict)
assert [] == Dict.values empty_dict
end
+26 -32
View File
@@ -3,31 +3,6 @@ Code.require_file "../test_helper.exs", __FILE__
defmodule EnumTest do
use ExUnit.Case, async: true
test :times do
try do
Process.put(:times_with_arity, nil)
assert Enum.times(0, fn -> Process.put(:times_with_arity, :ok) end) == 0
assert Process.get(:times_with_arity) == nil
assert Enum.times(3, fn -> Process.put(:times_with_arity, :ok) end) == 3
assert Process.get(:times_with_arity) == :ok
after
Process.delete(:times_with_arity)
end
end
test :times_with_iterator do
try do
assert Enum.times(5, fn x -> Process.put(:times_with_arity, x) end) == 5
assert Process.get(:times_with_arity) == 4
after
Process.delete(:times_with_arity)
end
end
test :times_with_iterator_and_acumulator do
assert Enum.times(5, 0, fn acc, x -> acc + x end) == 10
end
test :reverse do
assert Enum.reverse(URI.query_decoder("foo=bar&baz=bat")) ==
[{ "baz", "bat" }, { "foo", "bar" }]
@@ -85,6 +60,7 @@ defmodule EnumTest.List do
assert Enum.drop([1,2,3], 2) == [3]
assert Enum.drop([1,2,3], 3) == []
assert Enum.drop([1,2,3], 4) == []
assert Enum.drop([1,2,3], -1) == [3]
assert Enum.drop([], 3) == []
end
@@ -198,8 +174,9 @@ defmodule EnumTest.List do
assert Enum.reverse([1,2,3]) == [3,2,1]
end
test :qsort do
assert Enum.qsort([5,3,2,4,1]) == [1,2,3,4,5]
test :sort do
assert Enum.sort([5,3,2,4,1]) == [1,2,3,4,5]
assert Enum.sort([5,3,2,4,1], &1 > &2) == [5,4,3,2,1]
end
test :split do
@@ -230,6 +207,7 @@ defmodule EnumTest.List do
assert Enum.take([1,2,3], 2) == [1,2]
assert Enum.take([1,2,3], 3) == [1,2,3]
assert Enum.take([1,2,3], 4) == [1,2,3]
assert Enum.take([1,2,3], -1) == [1,2]
assert Enum.take([], 3) == []
end
@@ -240,6 +218,10 @@ defmodule EnumTest.List do
assert Enum.take_while([], fn(_) -> true end) == []
end
test :uniq do
assert Enum.uniq([1,2,3,2,1]) == [1,2,3]
end
test :zip do
assert Enum.zip([:a, :b], [1, 2]) == [{:a, 1}, {:b, 2}]
assert Enum.zip([:a, :b], [1, 2, 3, 4]) == [{:a, 1}, {:b, 2}]
@@ -384,9 +366,9 @@ defmodule EnumTest.Dict.Common do
assert { all, [] } == Enum.partition(dict, fn({_k, v}) -> v < 10 end)
end
test :qsort do
test :sort do
dict = unquote(module).new [{:b,2},{:c,3},{:a,1},{:d,4}]
assert Enum.qsort(dict) == [a: 1, b: 2, c: 3, d: 4]
assert Enum.sort(dict) == [a: 1, b: 2, c: 3, d: 4]
end
end
end
@@ -483,10 +465,10 @@ defmodule EnumTest.Range do
assert_raise Enum.OutOfBoundsError, fn ->
assert Enum.at!(2..6, 8)
end
assert_raise Enum.OutOfBoundsError, fn ->
assert Enum.at!(-2..-6, 8)
end
end
test :count do
@@ -667,6 +649,16 @@ defmodule EnumTest.Range do
assert Enum.partition(range, fn(x) -> rem(x, 2) == 0 end) == { [2], [1,3] }
end
test :sort do
assert Enum.sort(Range.new(first: 3, last: 1)) == [1,2,3]
assert Enum.sort(Range.new(first: 2, last: 1)) == [1,2]
assert Enum.sort(Range.new(first: 1, last: 1)) == [1]
assert Enum.sort(Range.new(first: 3, last: 1), &1 > &2) == [3,2,1]
assert Enum.sort(Range.new(first: 2, last: 1), &1 > &2) == [2,1]
assert Enum.sort(Range.new(first: 1, last: 1), &1 > &2) == [1]
end
test :split do
range = Range.new(first: 1, last: 3)
assert Enum.split(range, 0) == { [], [1,2,3] }
@@ -712,11 +704,13 @@ defmodule EnumTest.Range do
assert Enum.take_while(range, fn(x) -> x > 3 end) == []
assert Enum.take_while(range, fn(x) -> x <= 1 end) == [1]
assert Enum.take_while(range, fn(x) -> x <= 3 end) == [1,2,3]
range = Range.new(first: 1, last: 0)
assert Enum.take_while([], fn(_) -> true end) == []
end
test :uniq do
assert Enum.uniq(1..3) == [1,2,3]
end
test :zip do
assert Enum.zip([:a, :b], 1..2) == [{:a, 1}, {:b, 2}]
assert Enum.zip([:a, :b], 1..4) == [{:a, 1}, {:b, 2}]
@@ -8,6 +8,13 @@ defmodule Kernel.ExceptionTest do
refute is_exception({ :foo, :bar })
end
test :format_environment do
env = __ENV__.file("foo").line(12)
assert Exception.env_stacktrace(env) == " foo:12: Kernel.ExceptionTest.test_format_environment/0\n"
env = env.function(nil)
assert Exception.env_stacktrace(env) == " foo:12: Kernel.ExceptionTest (body)\n"
end
test :format_stacktrace_with_no_file_or_line do
assert Exception.format_stacktrace({Foo, :bar, [1, 2, 3], []}) == "Foo.bar(1, 2, 3)"
assert Exception.format_stacktrace({Foo, :bar, [], []}) == "Foo.bar()"
+77 -14
View File
@@ -189,9 +189,9 @@ defmodule FileTest do
{ :ok, files } = File.cp_r(src, dest)
assert length(files) == 8
assert tmp_path("tmp/cp_r/a") in files
assert tmp_path("tmp/cp_r/c") in files
assert tmp_path("tmp/cp_r/a/1.txt") in files
assert tmp_path("tmp/cp_r/a") inlist files
assert tmp_path("tmp/cp_r/c") inlist files
assert tmp_path("tmp/cp_r/a/1.txt") inlist files
assert { :ok, 'certainly/invalid' } = :file.read_link(tmp_path("tmp/cp_r/c"))
@@ -589,7 +589,7 @@ defmodule FileTest do
end
test :open_utf8_by_default do
{ :ok, file } = File.open(fixture_path("utf8.txt"))
{ :ok, file } = File.open(fixture_path("utf8.txt"), [:utf8])
assert IO.gets(file, "") == "Русский\n"
assert File.close(file) == :ok
end
@@ -612,8 +612,20 @@ defmodule FileTest do
end
end
test :open_with_binwrite_permission do
fixture = tmp_path("tmp_text.txt")
try do
{ :ok, file } = File.open(fixture, [:write])
assert IO.binwrite(file, "Русский") == :ok
assert File.close(file) == :ok
assert File.read(fixture) == { :ok, "Русский" }
after
File.rm(fixture)
end
end
test :open_utf8_and_charlist do
{ :ok, file } = File.open(fixture_path("utf8.txt"), [:charlist])
{ :ok, file } = File.open(fixture_path("utf8.txt"), [:charlist, :utf8])
assert IO.gets(file, "") == [1056,1091,1089,1089,1082,1080,1081,10]
assert File.close(file) == :ok
end
@@ -866,8 +878,8 @@ defmodule FileTest do
{ :ok, files } = File.rm_rf(fixture)
assert length(files) == 8
assert fixture in files
assert tmp_path("tmp/a/1.txt") in files
assert fixture inlist files
assert tmp_path("tmp/a/1.txt") inlist files
refute File.exists?(tmp_path("tmp/a/1.txt"))
refute File.exists?(tmp_path("tmp/a/a/2.txt"))
@@ -900,8 +912,8 @@ defmodule FileTest do
{ :ok, files } = File.rm_rf(fixture)
assert length(files) == 8
assert fixture in files
assert (tmp_path("tmp/a/1.txt") /> to_char_list) in files
assert fixture inlist files
assert (tmp_path("tmp/a/1.txt") /> to_char_list) inlist files
refute File.exists?(tmp_path("tmp/a/1.txt"))
refute File.exists?(tmp_path("tmp/a/a/2.txt"))
@@ -936,8 +948,8 @@ defmodule FileTest do
files = File.rm_rf!(fixture)
assert length(files) == 8
assert fixture in files
assert tmp_path("tmp/a/1.txt") in files
assert fixture inlist files
assert tmp_path("tmp/a/1.txt") inlist files
refute File.exists?(tmp_path("tmp/a/1.txt"))
refute File.exists?(tmp_path("tmp/a/a/2.txt"))
@@ -980,7 +992,7 @@ defmodule FileTest do
iterator = File.iterator(src)
File.open dest, [:write], fn(target) ->
Enum.each iterator, fn(line) ->
IO.puts target, Regex.replace(%r/"/, line, "'")
IO.write target, Regex.replace(%r/"/, line, "'")
end
end
assert File.read(dest) == { :ok, "FOO\n" }
@@ -997,7 +1009,7 @@ defmodule FileTest do
{ :ok, iterator } = File.iterator(src)
File.open dest, [:write], fn(target) ->
Enum.each iterator, fn(line) ->
IO.puts target, Regex.replace(%r/"/, line, "'")
IO.write target, Regex.replace(%r/"/, line, "'")
end
end
assert File.read(dest) == { :ok, "FOO\n" }
@@ -1014,7 +1026,58 @@ defmodule FileTest do
iterator = File.iterator!(src)
File.open dest, [:write], fn(target) ->
Enum.each iterator, fn(line) ->
IO.puts target, Regex.replace(%r/"/, line, "'")
IO.write target, Regex.replace(%r/"/, line, "'")
end
end
assert File.read(dest) == { :ok, "FOO\n" }
after
File.rm(dest)
end
end
test :biniterator do
src = File.open! fixture_path("foo.txt")
dest = tmp_path("tmp_test.txt")
try do
iterator = File.biniterator(src)
File.open dest, [:write], fn(target) ->
Enum.each iterator, fn(line) ->
IO.write target, Regex.replace(%r/"/, line, "'")
end
end
assert File.read(dest) == { :ok, "FOO\n" }
after
File.rm(dest)
end
end
test :biniterator_with_path do
src = fixture_path("foo.txt")
dest = tmp_path("tmp_test.txt")
try do
{ :ok, iterator } = File.biniterator(src)
File.open dest, [:write], fn(target) ->
Enum.each iterator, fn(line) ->
IO.write target, Regex.replace(%r/"/, line, "'")
end
end
assert File.read(dest) == { :ok, "FOO\n" }
after
File.rm(dest)
end
end
test :biniterator! do
src = fixture_path("foo.txt")
dest = tmp_path("tmp_test.txt")
try do
iterator = File.biniterator!(src)
File.open dest, [:write], fn(target) ->
Enum.each iterator, fn(line) ->
IO.write target, Regex.replace(%r/"/, line, "'")
end
end
assert File.read(dest) == { :ok, "FOO\n" }
+17 -4
View File
@@ -10,11 +10,17 @@ defmodule IOTest do
end
test :read_with_utf8_and_binary do
{ :ok, file } = File.open(File.expand_path('../fixtures/utf8.txt', __FILE__))
{ :ok, file } = File.open(File.expand_path('../fixtures/utf8.txt', __FILE__), [:utf8])
assert "Русский" == IO.read(file, 7)
assert File.close(file) == :ok
end
test :binread do
{ :ok, file } = File.open(File.expand_path('../fixtures/utf8.txt', __FILE__))
assert "Русский" == IO.binread(file, 14)
assert File.close(file) == :ok
end
test :getb do
{ :ok, file } = File.open(File.expand_path('../fixtures/foo.txt', __FILE__))
assert "F" == IO.getb(file, "")
@@ -32,7 +38,7 @@ defmodule IOTest do
end
test :getb_with_utf8_and_binary do
{ :ok, file } = File.open(File.expand_path('../fixtures/utf8.txt', __FILE__))
{ :ok, file } = File.open(File.expand_path('../fixtures/utf8.txt', __FILE__), [:utf8])
assert "Русский" == IO.getb(file, "", 7)
assert File.close(file) == :ok
end
@@ -45,7 +51,7 @@ defmodule IOTest do
end
test :gets_with_utf8_and_binary do
{ :ok, file } = File.open(File.expand_path('../fixtures/utf8.txt', __FILE__))
{ :ok, file } = File.open(File.expand_path('../fixtures/utf8.txt', __FILE__), [:utf8])
assert "Русский\n" == IO.gets(file, "")
assert "日\n" == IO.gets(file, "")
assert File.close(file) == :ok
@@ -59,9 +65,16 @@ defmodule IOTest do
end
test :readline_with_utf8_and_binary do
{ :ok, file } = File.open(File.expand_path('../fixtures/utf8.txt', __FILE__))
{ :ok, file } = File.open(File.expand_path('../fixtures/utf8.txt', __FILE__), [:utf8])
assert "Русский\n" == IO.readline(file)
assert "日\n" == IO.readline(file)
assert File.close(file) == :ok
end
test :binreadline do
{ :ok, file } = File.open(File.expand_path('../fixtures/utf8.txt', __FILE__))
assert "Русский\n" == IO.binreadline(file)
assert "日\n" == IO.binreadline(file)
assert File.close(file) == :ok
end
end
+6 -2
View File
@@ -3,6 +3,10 @@ Code.require_file "../../test_helper.exs", __FILE__
defmodule Kernel.CaseTest do
use ExUnit.Case, async: true
test :inline_case do
assert (case 1, do: (1 -> :ok; 2 -> :wrong)) == :ok
end
test :nested_case do
assert get_case == 2
end
@@ -31,8 +35,8 @@ defmodule Kernel.CaseTest do
test :in_operator_outside_case do
x = 1
y = 4
assert x in [1,2,3]
assert not y in [1, 2, 3]
assert x in [1,2,3], "in assertion"
assert not y in [1, 2, 3], "not in assertion"
end
test :in_operator_in_function_definition do
+11 -11
View File
@@ -22,18 +22,12 @@ defmodule Kernel.CLI.OptionParsingTest do
{ path, _ } = Code.eval list, []
# pa
assert File.expand_path('ebin', root) in path
assert File.expand_path('lib', root) in path
assert File.expand_path('src', root) in path
assert File.expand_path('ebin', root) inlist path
assert File.expand_path('lib', root) inlist path
assert File.expand_path('src', root) inlist path
# pz
assert File.expand_path('lib/list', root) in path
end
test :require do
options = ['-r', fixture_path('../../../lib/list/*') /> to_char_list, '-r', '/never/gonna/*/up']
{ config, _argv } = Kernel.CLI.process_options(options, Kernel.CLI.Config.new)
assert {:require, fixture_path "../../../lib/list/chars.ex"} in config.commands
assert File.expand_path('lib/list', root) inlist path
end
end
@@ -77,7 +71,7 @@ defmodule Kernel.CLI.CompileTest do
import PathHelpers
test :compile_code do
fixture = fixture_path "compile_sample.exs"
fixture = fixture_path "compile_sample.ex"
assert elixirc('#{fixture} -o #{tmp_path}') ==
'Compiled #{fixture}\n'
assert File.regular?(tmp_path "Elixir-CompileSample.beam")
@@ -88,6 +82,12 @@ defmodule Kernel.CLI.ParallelCompilerTest do
use ExUnit.Case, async: true
import PathHelpers
test :files do
fixtures = [fixture_path("compile_sample.ex")]
assert [{ CompileSample, binary }] = Kernel.ParallelCompiler.files fixtures
assert is_binary(binary)
end
test :compile_code do
output = elixirc('#{fixture_path("parallel_compiler")} -o #{tmp_path}')
assert :string.str(output, 'message_from_foo') > 0,
+2 -2
View File
@@ -17,8 +17,8 @@ defmodule Kernel.DocTest do
assert CompiledWithDocs.__info__(:docs) == expected
assert CompiledWithDocs.__info__(:moduledoc) == { 1, "moduledoc" }
after
:code.purge CompiledWithDocs
:code.delete CompiledWithDocs
:code.purge CompiledWithDocs
end
end
@@ -33,8 +33,8 @@ defmodule Kernel.DocTest do
assert CompiledWithDocs.__info__(:moduledoc) == nil
after
Code.compiler_options(docs: true)
:code.purge CompiledWithDocs
:code.delete CompiledWithDocs
:code.purge CompiledWithDocs
end
end
end
+22 -2
View File
@@ -103,6 +103,11 @@ defmodule Kernel.ErrorsTest do
format_rescue 'defmodule Foo do\ndefmacrop foo, do: 1\ndefmacro bar, do: __MODULE__.foo\ndefmacro baz, do: bar\nend'
end
test :function_definition_with_alias do
assert "nofile:1: function names should start with lowercase characters or underscore, invalid name Bar" ==
format_rescue 'defmodule Foo do def Bar do :baz end\nend\n'
end
test :erlang_function_conflict do
assert "nofile:1: function exit/1 already imported from Kernel" ==
format_rescue 'defmodule Foo do import Kernel.ErrorsTest.UnproperMacro, only: [exit: 1]\nend'
@@ -153,7 +158,7 @@ defmodule Kernel.ErrorsTest do
end
test :cant_define_local_due_to_in_erlang_macros_conflict do
assert "nofile:1: cannot define local quote/1 because it conflicts with Elixir internal macros" ==
assert "nofile:1: cannot define local quote/1 because it conflicts with Elixir special forms" ==
format_rescue 'defmodule Foo do\ndef quote(x), do: x\ndef bar(x), do: quote(do: x)\nend'
end
@@ -162,6 +167,11 @@ defmodule Kernel.ErrorsTest do
format_rescue 'defmodule Foo, do: (defmodule Elixir.Foo, do: true)'
end
test :invalid_definition do
assert "nofile:1: invalid syntax in def 1.(hello)" ==
format_rescue 'defmodule Foo, do: (def 1.(hello), do: true)'
end
test :duplicated_bitstring_size do
assert "nofile:1: duplicated size definition for bitstring" == format_rescue '<<1 :: [size(12), size(13)]>>'
end
@@ -229,6 +239,16 @@ defmodule Kernel.ErrorsTest do
format_rescue 'cond do _ -> true end'
end
test :fun_different_arities do
assert "nofile:1: cannot mix clauses with different arities in function definition" ==
format_rescue 'fn x -> x; x, y -> x + y end'
end
test :new_line_error do
assert "nofile:3: syntax error before: newline" ==
format_rescue 'if true do\n foo = [],\n baz\nend'
end
test :macros_error_stacktrace do
assert [{:erlang,:+,[1,:foo],_},{Foo,:sample,1,_}|_] =
rescue_stacktrace("defmodule Foo do\ndefmacro sample(num), do: num + :foo\ndef other, do: sample(1)\nend")
@@ -273,7 +293,7 @@ defmodule Kernel.ErrorsTest do
:elixir.eval(to_char_list(expr), [])
nil
rescue
error -> System.stacktrace
_ -> System.stacktrace
end
result || raise(ExUnit.AssertionError, message: "Expected function given to format_rescue to fail")
@@ -27,7 +27,7 @@ defmodule Kernel.Overridable do
{ super, super?, 1 }
end
false = Module.overridable? __MODULE__, { :explicit_nested_super, 0 }
true = Module.overridable? __MODULE__, { :explicit_nested_super, 0 }
defoverridable [explicit_nested_super: 0]
@@ -94,6 +94,10 @@ defmodule Kernel.Overridable do
17
end
def many_clauses(3) do
super(0) + super(1)
end
def many_clauses(_) do
super
end
@@ -139,10 +143,11 @@ defmodule Kernel.OverridableTest do
assert Overridable.many_clauses(0) == 11
assert Overridable.many_clauses(1) == 13
assert Overridable.many_clauses(2) == 17
assert Overridable.many_clauses(3) == 24
end
test "overridable definitions are private" do
refute {:"with_super (overridable 0)",0} in Overridable.__info__(:exports)
refute {:"with_super (overridable 0)",0} inlist Overridable.__info__(:exports)
end
test "invalid super call" do
+8 -1
View File
@@ -69,7 +69,7 @@ defmodule Kernel.QuoteTest do
67,
[
[file: __FILE__],
{ :bar, 67, [1,2,3] }
[do: { :bar, 67, [1,2,3] }]
]
}
end
@@ -87,6 +87,13 @@ defmodule Kernel.QuoteTest do
assert quote(do: foo.unquote(:bar)) == quote(do: foo.bar)
assert quote(do: foo.unquote(:bar)(1)) == quote(do: foo.bar(1))
assert quote(do: foo.unquote(:bar)(1) do 2 + 3 end) == quote(do: foo.bar(1) do 2 + 3 end)
assert Code.eval_quoted(quote(do: Foo.unquote(Bar))) == { Foo.Bar, [] }
assert Code.eval_quoted(quote(do: Foo.unquote(quote do: Bar))) == { Foo.Bar, [] }
assert_raise SyntaxError, fn ->
quote(do: foo.unquote(1))
end
end
test :splice_on_root do
@@ -0,0 +1,257 @@
Code.require_file "../../test_helper.exs", __FILE__
defrecord BadRange, first: 0, last: 0 do
defoverridable [first: 1]
def first(_) do
:not_optimizable
end
end
defmodule Kernel.RecordRewriterTest do
use ExUnit.Case, async: true
## Helpers
defmacrop clause(expr) do
quote do: extract_clause(unquote(Macro.escape(expr)))
end
defp extract_clause(fun) do
{ { :fun, _, { :clauses, clauses } }, _ } =
:elixir_translator.translate_each(fun, :elixir.scope_for_eval([]))
hd(clauses)
end
defp optimize_clause(clause) do
{ clause, dict, res } = Kernel.RecordRewriter.optimize_clause(clause)
dict = Enum.map dict, fn { k, { v, _ } } -> { k, v }; other -> other; end
{ clause, dict, res }
end
## Inference tests
test "simple atom" do
clause = clause(fn -> :foo end)
assert optimize_clause(clause) == { clause, [], nil }
end
test "with left-side arg match" do
clause = clause(fn(x = Macro.Env[]) -> :foo end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], nil }
end
test "with right-side arg match" do
clause = clause(fn(Macro.Env[] = x) -> :foo end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], nil }
end
test "with nested left-side arg match" do
clause = clause(fn(x = y = Macro.Env[]) -> :foo end)
assert optimize_clause(clause) == { clause, [x: Macro.Env, y: Macro.Env], nil }
end
test "with nested right-side arg match" do
clause = clause(fn(Macro.Env[] = x = y) -> :foo end)
assert optimize_clause(clause) == { clause, [x: Macro.Env, y: Macro.Env], nil }
end
test "with deep left-side arg match" do
clause = clause(fn({ x, y = Macro.Env[] }) -> :foo end)
assert optimize_clause(clause) == { clause, [y: Macro.Env], nil }
end
test "with deep right-side arg match" do
clause = clause(fn({ x, Macro.Env[] = y }) -> :foo end)
assert optimize_clause(clause) == { clause, [y: Macro.Env], nil }
end
test "with tuple match" do
clause = clause(fn({ x, y } = { Macro.Env[], Range[] }) -> :foo end)
assert optimize_clause(clause) == { clause, [x: Macro.Env, y: Range], nil }
clause = clause(fn({ Macro.Env[], y } = { x, Range[] }) -> :foo end)
assert optimize_clause(clause) == { clause, [x: Macro.Env, y: Range], nil }
end
test "inside body" do
clause = clause(fn(x = Macro.Env[]) -> y = x end)
assert optimize_clause(clause) == { clause, [x: Macro.Env, y: Macro.Env], { Macro.Env, nil } }
end
test "inside body with variable overridden" do
clause = clause(fn(x = Macro.Env[], y = Range[]) -> y = x end)
assert optimize_clause(clause) == { clause, [x: Macro.Env, y: Range, "y@1": Macro.Env], { Macro.Env, nil } }
end
test "inside body with nested tuple" do
clause = clause(fn(x = Range[]) -> ^x = Range[first: { :hello, 2 }] end)
assert optimize_clause(clause) == { clause, [x: Range], { Range, [nil, { :hello, nil }, nil] } }
end
test "conflicting definition" do
clause = clause(fn(x = Macro.Env[]) -> ^x = Range[]; :foo end)
assert optimize_clause(clause) == { clause, [x: nil], nil }
end
test "inside list" do
clause = clause(fn -> [x = Macro.Env[]]; :foo end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], nil }
end
test "inside tuple" do
clause = clause(fn -> { x = Macro.Env[] }; :foo end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], nil }
end
test "inside bin" do
clause = clause(fn -> << x = Macro.Env[] >>; :foo end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], nil }
end
test "inside operator" do
clause = clause(fn -> 1 + (x = Macro.Env[]); :foo end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], nil }
end
test "inside block" do
clause = clause(fn -> :foo; (x = Macro.Env[]; x) end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], { Macro.Env, nil } }
end
test "inside local call" do
clause = clause(fn -> (x = Macro.Env[]).(y = Range[]) end)
assert optimize_clause(clause) == { clause, [x: Macro.Env, y: Range], nil }
end
test "inside remote call" do
clause = clause(fn -> (x = Macro.Env[]).call(y = Range[]) end)
assert optimize_clause(clause) == { clause, [x: Macro.Env, y: Range], nil }
end
test "inside list comprehension" do
clause = clause(fn -> lc x = Macro.Env[] inlist sample, do: x end)
assert optimize_clause(clause) == { clause, [], nil }
end
test "inside bit comprehension" do
clause = clause(fn -> bc x = Macro.Env[] inbits sample, do: <<x>> end)
assert optimize_clause(clause) == { clause, [], nil }
end
test "inside function retrieval" do
clause = clause(fn -> function(x = Macro.Env[], y, z) end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], nil }
end
test "inside anonymous function" do
clause = clause(fn -> fn (x = Macro.Env[]) -> x end end)
assert optimize_clause(clause) == { clause, [], nil }
end
test "inside case" do
clause = clause(fn -> case x = Macro.Env[] do _ -> x end end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], { Macro.Env, nil } }
clause = clause(fn -> case something do x = Macro.Env[] -> x; Macro.Env[] = x -> x end end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], { Macro.Env, nil } }
clause = clause(fn -> case something do x = Macro.Env[] -> x; Macro.Env[] = y -> y end end)
assert optimize_clause(clause) == { clause, [], { Macro.Env, nil } }
clause = clause(fn -> case something do 1 -> x = Macro.Env[]; 2 -> x = Macro.Env[] end end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], { Macro.Env, nil } }
clause = clause(fn -> case something do 1 -> x = Macro.Env[]; 2 -> x = Range[] end end)
assert optimize_clause(clause) == { clause, [x: nil], nil }
end
test "inside case with nested tuple" do
clause = clause(fn -> case something do x = Range[first: { :foo, 2 }] -> x; Range[] = x -> x end end)
assert optimize_clause(clause) == { clause, [x: Range], { Range, nil } }
clause = clause(fn -> case something do x = Range[first: { :foo, 2 }] -> x; Range[first: { :foo, 2 }] = x -> x end end)
assert optimize_clause(clause) == { clause, [x: Range], { Range, [nil, { :foo, nil }, nil] } }
end
test "inside receive" do
clause = clause(fn -> receive do x = Macro.Env[] -> x; Macro.Env[] = x -> x end end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], { Macro.Env, nil } }
clause = clause(fn -> receive do x = Macro.Env[] -> x; Macro.Env[] = y -> y end end)
assert optimize_clause(clause) == { clause, [], { Macro.Env, nil } }
clause = clause(fn -> receive do 1 -> x = Macro.Env[]; 2 -> x = Macro.Env[] end end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], { Macro.Env, nil } }
clause = clause(fn -> receive do 1 -> x = Macro.Env[]; 2 -> x = Range[] end end)
assert optimize_clause(clause) == { clause, [x: nil], nil }
end
test "inside receive with after" do
clause = clause(fn -> receive do 1 -> x = Macro.Env[]; after 2 -> x = Macro.Env[]; end end)
assert optimize_clause(clause) == { clause, [x: Macro.Env], { Macro.Env, nil } }
clause = clause(fn -> receive do 1 -> x = Macro.Env[]; after 2 -> x = Range[]; end end)
assert optimize_clause(clause) == { clause, [x: nil], nil }
end
test "inside try" do
clause = clause(fn -> try do x = Macro.Env[]; x end end)
assert optimize_clause(clause) == { clause, [], { Macro.Env, nil } }
clause = clause(fn -> try do x = Macro.Env[]; x; catch :oops -> x = Macro.Env[]; end end)
assert optimize_clause(clause) == { clause, [], { Macro.Env, nil } }
clause = clause(fn -> try do x = Macro.Env[]; x; after x = Macro.Env[]; end end)
assert optimize_clause(clause) == { clause, [], { Macro.Env, nil } }
end
## Rewrite tests
test "getter call is rewriten" do
{ clause, rewriten } =
{ clause(fn(x = Range[]) -> x.first end), clause(fn(x = Range[]) -> :erlang.element(2, x) end) }
assert optimize_clause(clause) == { rewriten, [x: Range], nil }
end
test "setter call is rewriten" do
{ clause, rewriten } =
{ clause(fn(x = Range[]) -> x.first(:first) end), clause(fn(x = Range[]) -> :erlang.setelement(2, x, :first) end) }
assert optimize_clause(clause) == { rewriten, [x: Range], { Range, nil } }
end
test "nested setter call is rewriten" do
{ clause, rewriten } =
{ clause(fn(x = Range[]) -> x.first(:first).last(:last) end), clause(fn(x = Range[]) -> :erlang.setelement(3, :erlang.setelement(2, x, :first), :last) end) }
assert optimize_clause(clause) == { rewriten, [x: Range], { Range, nil } }
end
test "updater call is no-op (for now)" do
clause = clause(fn(x = Range[]) -> x.update_first(&1 + 1) end)
assert optimize_clause(clause) == { clause, [x: Range], { Range, nil } }
end
test "noop for unknown fields" do
clause = clause(fn(x = Range[]) -> x.unknown end)
assert optimize_clause(clause) == { clause, [x: Range], nil }
end
test "noop for not records fields" do
clause = clause(fn(x = { :not_a_record, _ }) -> x.unknown end)
assert optimize_clause(clause) == { clause, [x: :not_a_record], nil }
end
test "noop for rewriten fields" do
clause = clause(fn(x = BadRange[]) -> x.first end)
assert optimize_clause(clause) == { clause, [x: BadRange], nil }
end
test "noop for conflicting inference" do
clause = clause(fn(x = Macro.Env[]) -> ^x = Range[]; x.first end)
assert optimize_clause(clause) == { clause, [x: nil], nil }
end
end
@@ -31,7 +31,7 @@ defmodule Kernel.RescueTest do
rescue
RuntimeError -> true
catch
:error, value -> false
:error, _ -> false
end
assert result
+79 -1
View File
@@ -20,8 +20,28 @@ defmodule KernelTest do
refute x([])
end
test :paren do
assert nil?(())
assert [ 1, (), 3 ] == [1, nil, 3 ]
assert [do: ()] == [do: nil]
assert { 1, (), 3 } == { 1, nil, 3 }
end
test :__info__ do
assert { :in, 2 } in Kernel.__info__(:macros)
assert { :in, 2 } inlist Kernel.__info__(:macros)
end
test :macro_exported? do
assert macro_exported?(Kernel, :in, 2) == true
assert macro_exported?(Kernel, :def, 1) == true
assert macro_exported?(Kernel, :def, 2) == true
assert macro_exported?(Kernel, :def, 3) == false
assert macro_exported?(Kernel, :def, 4) == true
assert macro_exported?(Kernel, :no_such_macro, 2) == false
end
test :debug_info do
assert :debug_info inlist Kernel.__info__(:compile)[:options]
end
defp x(value) when value in [1,2,3], do: true
@@ -96,12 +116,22 @@ defmodule KernelTest do
assert function(:erlang, :atom_to_list, 1).(:a) == 'a'
end
test :remote_syntax_function do
assert function(:erlang.atom_to_list/1) ==
function(:erlang, :atom_to_list, 1)
assert function(Enum.map/2) == function(Enum, :map, 2)
end
test :retrieve_local_function do
assert is_function(function(:atl, 1))
assert :erlang.fun_info(function(:atl, 1), :arity) == {:arity, 1}
assert function(:atl, 1).(:a) == 'a'
end
test :local_syntax_function do
assert function(atl/1).(:a) == 'a'
end
test :retrieve_imported_function do
assert is_function(function(:atom_to_list, 1))
assert :erlang.fun_info(function(:atom_to_list, 1), :arity) == {:arity, 1}
@@ -204,6 +234,54 @@ defmodule KernelTest do
defp bang(_), do: :truthy
end
defmodule IfScope do
use ExUnit.Case, async: true
test :variables_on_nested_if do
if true do
a = 1
if true do
b = 2
end
end
assert a == 1
assert b == 2
end
test :variables_on_siblings_if do
if true do
a = 1
if true do
b = 2
end
if true do
c = 3
end
end
assert a == 1
assert b == 2
assert c == 3
end
test :variables_counter_on_nested_if do
r = (fn() -> 3 end).() # supresses warning at (if r < 0...)
r = r - 1
r = r - 1
r = r - 1
if true do
r = r - 1
if r < 0, do: r = 0
end
assert r == 0
end
end
defmodule Destructure do
use ExUnit.Case, async: true
+9 -2
View File
@@ -28,6 +28,13 @@ defmodule KeywordTest do
assert Keyword.from_enum(dict) == [a: 1, b: 2, c: 3]
end
test :keyword? do
assert Keyword.keyword?([])
assert Keyword.keyword?([a: 1])
refute Keyword.keyword?([{}])
refute Keyword.keyword?(<<>>)
end
test :new do
assert Keyword.new == []
end
@@ -50,7 +57,7 @@ defmodule KeywordTest do
test :get! do
assert Keyword.get!(create_keywords, :first_key) == 1
error = assert_raise Keyword.KeyError, fn ->
error = assert_raise KeyError, fn ->
Keyword.get!(create_keywords, :unknown)
end
@@ -99,7 +106,7 @@ defmodule KeywordTest do
test :update do
assert Keyword.update([a: 1], :a, &1 * 2) == [a: 2]
assert_raise Keyword.KeyError, fn ->
assert_raise KeyError, fn ->
assert Keyword.update([a: 1], :b, &1 * 2)
end
end
+1 -29
View File
@@ -53,36 +53,12 @@ defmodule ListTest do
assert List.foldr([1,2,3,4], 0, fn x,y -> x - y end) == -2
end
def test_member? do
test :member? do
assert List.member? [1,2,3], 1
refute List.member? [1,2,3], 0
refute List.member? [], 0
end
test :range do
assert List.range(1,3) == [1,2,3]
assert List.range(1, 1) == [1]
assert List.range(5, 0) == [5,4,3,2,1,0]
assert List.range(1, 0, -1) == [1,0]
assert List.range(1,8,2) == [1,3,5,7]
assert List.range(7,-1,-3) == [7,4,1]
assert List.range(2,1,1) == []
assert List.range(8,1,1) == []
assert List.range(1,8,-1) == []
assert List.range(1,1,-1) == []
end
test :sort do
assert List.sort([3, 5, 1, 2, 4]) == [1,2,3,4,5]
assert List.sort([3, 5, 1, 2, 4], &2 <= &1) == [5,4,3,2,1]
assert List.sort(['2', '3', '0', '11', '10']) == ['0', '10', '11', '2', '3']
assert ['0', '2', '3', '10', '11'] == List.sort ['2', '3', '0', '11', '10'], fn a, b ->
{na, _} = :string.to_integer a
{nb, _} = :string.to_integer b
na <= nb
end
end
test :concat_1 do
assert List.concat([[1,[2],3], [4], [5,6]]) == [1,[2],3,4,5,6]
end
@@ -95,10 +71,6 @@ defmodule ListTest do
assert Enum.reverse([1,2,3]) == [3,2,1]
end
test :uniq do
assert List.uniq([1,2,3,2,1]) == [1,2,3]
end
test :duplicate do
assert List.duplicate(1, 3) == [1,1,1]
assert List.duplicate([1], 1) == [[1]]
+45 -2
View File
@@ -128,16 +128,32 @@ defmodule MacroTest do
assert Macro.to_binary(quote do: foo.bar([1, 2, 3])) == "foo.bar([1, 2, 3])"
end
test :low_atom_remote_call_to_binary do
assert Macro.to_binary(quote do: :foo.bar(1, 2, 3)) == ":foo.bar(1, 2, 3)"
end
test :big_atom_remote_call_to_binary do
assert Macro.to_binary(quote do: Foo.Bar.bar(1, 2, 3)) == "Foo.Bar.bar(1, 2, 3)"
end
test :remote_and_fun_call_to_binary do
assert Macro.to_binary(quote do: foo.bar.(1, 2, 3)) == "foo.bar().(1, 2, 3)"
assert Macro.to_binary(quote do: foo.bar.([1, 2, 3])) == "foo.bar().([1, 2, 3])"
end
test :atom_call_to_binary do
assert Macro.to_binary(quote do: :foo.(1, 2, 3)) == ":foo.(1, 2, 3)"
end
test :aliases_call_to_binary do
assert Macro.to_binary(quote do: Foo.Bar.baz(1, 2, 3)) == "Foo.Bar.baz(1, 2, 3)"
assert Macro.to_binary(quote do: Foo.Bar.baz([1, 2, 3])) == "Foo.Bar.baz([1, 2, 3])"
end
test :arrow_to_binary do
assert Macro.to_binary(quote do: foo(1, (2 -> 3))) == "foo(1, (2 -> 3))"
end
test :blocks_to_binary do
assert Macro.to_binary(quote do: (1; 2; (:foo; :bar); 3)) <> "\n" == """
(
@@ -175,9 +191,24 @@ defmodule MacroTest do
end
test :fn_to_binary do
assert Macro.to_binary(quote do: (fn(x) -> x + 1 end)) <> "\n" == """
assert Macro.to_binary(quote do: (() -> x)) == "(() -> x)"
assert Macro.to_binary(quote do: (fn -> 1 + 2 end)) == "fn -> 1 + 2 end"
assert Macro.to_binary(quote do: (fn(x) -> x + 1 end)) == "fn x -> x + 1 end"
assert Macro.to_binary(quote do: (fn(x) -> y = x + 1; y end)) <> "\n" == """
fn x ->
x + 1
y = (x + 1)
y
end
"""
assert Macro.to_binary(quote do: (fn(x) -> y = x + 1; y; (z) -> z end)) <> "\n" == """
fn
x ->
y = (x + 1)
y
z ->
z
end
"""
end
@@ -214,6 +245,8 @@ defmodule MacroTest do
end
test :unary_ops_to_binary do
assert Macro.to_binary(quote do: not 1) == "not 1"
assert Macro.to_binary(quote do: not foo) == "not foo"
assert Macro.to_binary(quote do: -1) == "-1"
assert Macro.to_binary(quote do: @foo(bar)) == "@foo(bar)"
end
@@ -240,4 +273,14 @@ defmodule MacroTest do
assert Macro.safe_term(quote do: [1+1]) == { :unsafe, quote do: 1 + 1 }
assert Macro.safe_term(quote do: {1+1}) == { :unsafe, quote do: 1 + 1 }
end
## extract_args
test :extract_args do
assert Macro.extract_args(quote do: foo) == { :foo, [] }
assert Macro.extract_args(quote do: foo()) == { :foo, [] }
assert Macro.extract_args(quote do: :foo.()) == { :foo, [] }
assert Macro.extract_args(quote do: foo(1,2,3)) == { :foo, [1,2,3] }
assert Macro.extract_args(quote do: 1.(1,2,3)) == :error
end
end
+28 -10
View File
@@ -170,6 +170,16 @@ defmodule ModuleTest do
end)
end
def __on_definition__(env, kind, name, args, guards, expr) do
Process.put(env.module, :called)
assert env.module == ModuleTest.OnDefinition
assert kind == :def
assert name == :hello
assert [{ :foo, _, _ }, { :bar, _ ,_ }] = args
assert [] = guards
assert [do: { :+, _, [{ :foo, _, _ }, { :bar, _, _ }] }] = expr
end
test :on_definition do
defmodule OnDefinition do
@on_definition ModuleTest
@@ -182,16 +192,6 @@ defmodule ModuleTest do
assert Process.get(ModuleTest.OnDefinition) == :called
end
def __on_definition__(env, kind, name, args, guards, expr) do
Process.put(env.module, :called)
assert env.module == ModuleTest.OnDefinition
assert kind == :def
assert name == :hello
assert [{ :foo, _, _ }, { :bar, _ ,_ }] = args
assert [] = guards
assert [do: { :+, _, [{ :foo, _, _ }, { :bar, _, _ }] }] = expr
end
test :create do
contents =
quote do
@@ -201,4 +201,22 @@ defmodule ModuleTest do
Module.create(ModuleCreateSample, contents, __ENV__)
assert ModuleCreateSample.world
end
defmacro __before_compile__(_) do
quote do
def constant, do: 1
defoverridable constant: 0
end
end
test :overridable_inside_before_compile do
defmodule OverridableWithBeforeCompile do
@before_compile ModuleTest
end
assert OverridableWithBeforeCompile.constant == 1
end
test :to_binary do
assert Module.to_binary(Hello.World) == "Hello.World"
end
end
+11 -13
View File
@@ -16,11 +16,11 @@ defmodule OptionParserTest do
end
test "parses more than one boolean option" do
assert OptionParser.parse(["--docs", "--compile"]) == { [compile: true, docs: true], [] }
assert OptionParser.parse(["--docs", "--compile"]) == { [docs: true, compile: true], [] }
end
test "parses more than one boolean options as the alias" do
assert OptionParser.parse(["--d", "--compile"], aliases: [d: :docs]) == { [compile: true, docs: true], [] }
assert OptionParser.parse(["--d", "--compile"], aliases: [d: :docs]) == { [docs: true, compile: true], [] }
end
test "parses key/value option" do
@@ -57,7 +57,7 @@ defmodule OptionParserTest do
assert OptionParser.parse(["--docs", "foo"], flags: [:docs]) == { [docs: true], ["foo"] }
end
test "flags default to false even when not given" do
test "not parsed flags defaults to false" do
assert OptionParser.parse(["foo"], flags: [:docs]) == { [docs: false], ["foo"] }
end
@@ -71,27 +71,25 @@ defmodule OptionParserTest do
end
test "parses more than one key/value options" do
options = OptionParser.parse(["--source", "from_docs/", "--docs", "false"])
assert_equal options, { [docs: false, source: "from_docs/"], [] }
assert OptionParser.parse(["--source", "from_docs/", "--docs", "false"]) == { [source: "from_docs/", docs: false], [] }
end
test "accumulates options accumulators" do
assert OptionParser.parse(["--require", "foo", "--require", "bar", "baz"]) == { [require: "foo", require: "bar"], ["baz"] }
end
test "parses mixed options" do
options = OptionParser.parse(["--source", "from_docs/", "--docs", "false", "--compile", "-x"])
assert_equal options, { :orddict.from_list([docs: false, source: "from_docs/", compile: true, x: true]), [] }
assert options == { [source: "from_docs/", docs: false, compile: true, x: true], [] }
end
test "stops on first non option arguments" do
options = OptionParser.parse_head(["--source", "from_docs/", "test/enum_test.exs", "--verbose"])
assert_equal options, { [source: "from_docs/"], ["test/enum_test.exs", "--verbose"] }
assert options == { [source: "from_docs/"], ["test/enum_test.exs", "--verbose"] }
end
test "goes beyond the first non option arguments" do
options = OptionParser.parse(["--source", "from_docs/", "test/enum_test.exs", "--verbose"])
assert_equal options, { [source: "from_docs/", verbose: true], ["test/enum_test.exs"] }
end
defp assert_equal({ opts1, tail1 }, { opts2, tail2 }) do
assert Keyword.equal?(opts1, opts2)
assert tail1 == tail2
assert options == { [source: "from_docs/", verbose: true], ["test/enum_test.exs"] }
end
end

Some files were not shown because too many files have changed in this diff Show More