Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d543cd122 |
@@ -1 +0,0 @@
|
||||
lib/elixir/test/elixir/fixtures/*.txt text eol=lf
|
||||
+7
-11
@@ -1,17 +1,13 @@
|
||||
/doc
|
||||
/ebin
|
||||
/.eunit/*
|
||||
/.full
|
||||
/lib/*/ebin/*
|
||||
/lib/*/tmp
|
||||
/lib/*/test/tmp
|
||||
/lib/elixir/src/elixir.app.src
|
||||
/lib/elixir/src/*_lexer.erl
|
||||
/lib/elixir/src/*_parser.erl
|
||||
/lib/elixir/src/elixir.app.src
|
||||
/lib/elixir/test/ebin
|
||||
/man/elixir.1
|
||||
/man/iex.1
|
||||
/deps/*
|
||||
/ebin
|
||||
/rel/elixir
|
||||
/Docs-v*.zip
|
||||
/Precompiled-v*.zip
|
||||
/.eunit
|
||||
/.release
|
||||
.elixir.plt
|
||||
erl_crash.dump
|
||||
erl_crash.dump
|
||||
+4
-10
@@ -1,15 +1,9 @@
|
||||
language: erlang
|
||||
|
||||
otp_release:
|
||||
- 18.0
|
||||
|
||||
sudo: false
|
||||
|
||||
script: "make compile && rm -rf .git && make test"
|
||||
|
||||
script: "make compile && make .full test"
|
||||
notifications:
|
||||
irc: "irc.freenode.org#elixir-lang"
|
||||
recipients:
|
||||
- jose.valim@plataformatec.com.br
|
||||
- eric.meadows.jonsson@gmail.com
|
||||
|
||||
otp_release:
|
||||
- R15B01
|
||||
- R15B
|
||||
|
||||
+53
-257
@@ -1,257 +1,53 @@
|
||||
# Changelog for Elixir v1.2
|
||||
|
||||
v1.2 brings enhancements, bug fixes, performance improvements and more
|
||||
into Elixir. Elixir v1.2 relies on many features in Erlang 18, requiring
|
||||
at least Erlang 18+. Upgrading to Erlang 18 is therefore necessary before
|
||||
upgrading Elixir.
|
||||
|
||||
## Erlang 18 support
|
||||
|
||||
We have brought many features specific to Erlang 18. Here are the highlights:
|
||||
|
||||
* Maps can now scale from dozens to millions of keys. Therefore, usage of
|
||||
the modules `Dict` and `HashDict` is now discouraged and will be
|
||||
deprecated in future releases, instead use `Map`. Similarly, `Set` and
|
||||
`HashSet` will be deprecated in favor of `MapSet`
|
||||
* Compilation times are faster due to improvements in both the Elixir and
|
||||
Erlang compilers
|
||||
* Dialyzer now emits less false negative warnings thanks to new annotations
|
||||
available in the Erlang compiler
|
||||
|
||||
## Language improvements
|
||||
|
||||
This release includes four notable language improvements:
|
||||
|
||||
* The addition of multi aliases/imports/require:
|
||||
|
||||
alias MyApp.{Foo, Bar, Baz}
|
||||
|
||||
* Support for variables in map keys:
|
||||
|
||||
%{key => value}
|
||||
|
||||
* Support for the pin operator in map keys and function clauses:
|
||||
|
||||
%{^key => value} = %{key => value}
|
||||
fn ^key -> :ok end
|
||||
|
||||
* Addition of the `with` special form to match on multiple expressions:
|
||||
|
||||
with {:ok, contents} <- File.read("my_file.ex"),
|
||||
{res, binding} <- Code.eval_string(contents),
|
||||
do: {:ok, res}
|
||||
|
||||
These improvements aim to make the language more consistent and expressive.
|
||||
|
||||
## Getting started experience
|
||||
|
||||
While we were improving the language, we also improved both the parser and
|
||||
compiler to be even more aware of language constructs, emitting warnings
|
||||
on common pitfalls like when piping to expressions without parentheses or
|
||||
when defining unsafe variables.
|
||||
|
||||
We have also introduced the `i/1` helper in IEx, which allows developers
|
||||
to retrieve information about any data type. This will help newcomers
|
||||
explore the language values while providing experienced developers with
|
||||
crucial information about the value they are introspecting.
|
||||
|
||||
## Workflow improvements
|
||||
|
||||
Umbrella applications are now able to share both build and configuration files.
|
||||
This aims to drastically reduce compilation times in umbrella projects by
|
||||
adding the following configuration to each umbrella app's `mix.exs` file:
|
||||
|
||||
build_path: "../../_build",
|
||||
config_path: "../../config/config.exs",
|
||||
|
||||
Finally, Mix will now consolidate protocols by default as we are now able to
|
||||
consolidate in parallel and cache the consolidation results, providing the
|
||||
best performance across all environments without affecting compilation times.
|
||||
The only downside of this change is that, if you have been implementing
|
||||
protocols exclusively as part of your test suite, inside the `test` directory,
|
||||
those won't be picked up as it happens after compilation. For such cases,
|
||||
consolidation can be disabled by setting `consolidate_protocols: false` in
|
||||
the project config.
|
||||
|
||||
These are great additions on top of the faster compilation times we have
|
||||
achieved when migrating to Erlang 18.
|
||||
|
||||
## Rebar 3 support
|
||||
|
||||
With Rebar 3 gaining more adoption in the Erlang community, Mix is
|
||||
now able to fetch and compile Rebar 3 dependencies. This feature is currently
|
||||
experimental and therefore opt-in: if you have a Rebar 3 dependency, you can
|
||||
ask Mix to use Rebar 3 to compile it by passing the `manager: :rebar3` option.
|
||||
Once configured, Mix will prompt you to install Rebar 3 if it is not yet
|
||||
available.
|
||||
|
||||
## v1.2.4 (2016-04-01)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
* [Mix] Add `:archives` configuration to `def project` that allows projects to list archive dependencies. `--no-archives-check` (as well as `--no-deps-check`) will disable the archive check. The `:archives` option is not checked for dependencies.
|
||||
* [Mix] Add `deps.precompile` task as hook
|
||||
* [Mix] Support `--include-children` in `mix deps.compile` option
|
||||
* [String] Update version of the Unicode database to 8.0.0
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
* [Application] Ensure `spec/2` returns nil for unknown applications
|
||||
* [Integer] Fix a possible binary leak in `parse/1`
|
||||
* [Mix] Purge Erlang modules on recompilation
|
||||
* [String] Ensure `split/1` does not break on non-breakable whitespace
|
||||
* [String] Ensure NFC and NFD normalization pass all of Unicode 8.0.0 tests
|
||||
* [Version] Allow dots in build info for versions in `Version.parse/1`
|
||||
|
||||
## v1.2.3 (2016-02-21)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
* [Base] Add `:ignore` and `:padding` option to encoding/decoding functions
|
||||
* [Mix] Add `Mix.Projects.deps_paths` that returns the dependencies path as a map
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
* [ExUnit] Do not provide negative line numbers without generated annotation (for compatibility with Erlang 19)
|
||||
* [Mix] Reject non fullfilled optional dependencies later on in the convergence resolution for proper dependency sorting
|
||||
* [String] Fix incomplete data trimming on both `String.replace_trailing` and `String.rstrip`
|
||||
* [String] Attach debug_info back into Unicode modules for Dialyzer support
|
||||
|
||||
## v1.2.2 (2016-01-31)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
* [Kernel] Support `@compile {:autoload, false}` to disable automatic loading after compilation
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
* [ExUnit] Raise if trying to override reserved tag in `setup` blocks
|
||||
* [Mix] Ensure retrieve compile manifests do fail if some compilers are not yet available
|
||||
* [Mix] Automatically merge managers according to the mix > rebar3 > rebar > make order
|
||||
* [Mix] Force recompilation if dependency was recently fetched
|
||||
|
||||
## v1.2.1 (2016-01-14)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
* [IEx] Support remote pids/ports with IEx helper `i/1`
|
||||
* [Protocol] Warn when `defimpl` is called for a consolidated protocol
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
* [ExUnit] Ensure `assert` macros can be used from quoted code
|
||||
* [ExUnit] Do not warn in match assertion if variable is reused in pattern
|
||||
* [Macro] Fix a bug in `Macro.to_string/1` where a remote function could be accidentally interpreted as a sigil
|
||||
* [Mix] Ensure dependencies are properly skipped when `--only` option is given to `mix deps.get`
|
||||
|
||||
## v1.2.0 (2016-01-01)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Application] Add `spec/1` and `spec/2` to retrieve application specification
|
||||
* [Application] Add `get_application/1` to retrieve the application a given module belongs to
|
||||
* [Base] Optimize encode and decode operations about 10 times
|
||||
* [Enum] Use the faster and auto-seeding `:rand` instead of `:random` in `Enum.shuffle/1` and `Enum.random/1` and `Enum.take_random/2`
|
||||
* [Enum] Add `Enum.with_index/2`
|
||||
* [GenServer] Add `GenServer.stop/1` for shutting down servers reliably
|
||||
* [IO] Add `color` related functions to `IO.ANSI`
|
||||
* [Kernel] Support multiple aliases in `alias`, `import`, `require` and `use`. For example, `alias MyApp.{Foo, Bar, Baz}`
|
||||
* [Kernel] Add `struct!/2`. Similar to `struct/2` but raises on invalid keys
|
||||
* [Kernel] Warn if `@doc/@typedoc/@moduledoc` attributes are redefined
|
||||
* [Kernel] Warn if non-variables are used in `defdelegate/2` (as they have no effect)
|
||||
* [Kernel] Mark quoted expressions as generated, avoiding false positives on dialyzer
|
||||
* [Kernel] Allow variables as map keys on creation `%{key => value}` and on matches `%{^key => value}`
|
||||
* [Kernel] Allow the pin operator `^` in `fn` clauses and on the left side of `<-` in `for` comprehensions
|
||||
* [Kernel] Introduce `with` as a special form that allows matching on right side parameters
|
||||
* [Kernel] Warn when right hand side of `->` does not provide any expression
|
||||
* [Kernel] Warn if the Elixir was compiled with a different endianness than the one currently available at runtime
|
||||
* [Kernel] Warn if a variable is used after being defined exclusively in a nested context
|
||||
* [Kernel] Warn if piping into an expression without parentheses
|
||||
* [Macro] Add `Macro.traverse/4` that performs pre and post-walk at once
|
||||
* [Macro] Add `Macro.camelize/1` and `Macro.underscore/1`
|
||||
* [Process] Add `Process.get_keys/0`
|
||||
* [Stream] Add `Stream.with_index/2`
|
||||
* [String] Introduce `String.replace_{prefix,suffix,leading,trailing}/2`. The first two will replace only the first occurrence of the given match in string. The last two will replace all occurrences of the given match
|
||||
* [String] Support `String.normalize/2` and `String.equivalent?/2` that perform NFD and NFC normalization
|
||||
* [System] Add `System.time_offset`, `System.monotonic_time`, `System.system_time`, `System.convert_time_unit` and `System.unique_integer`
|
||||
* [System] Allow `System.cmd/3` to remove variables by specifying nil values
|
||||
* [Task] Add `Task.Supervisor.async_nolink/1/3` that spawns a supervised task without linking to the caller process
|
||||
* [Task] Introduce `Task.yield_many/2`
|
||||
* [Task] Raise an error when a task is queried from a non-owning process (instead of waiting forever)
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Allow one test to raise multiple errors. The goal is to enable tools in the ecosystem to emit multiple failure reports from the same test
|
||||
* [ExUnit] Support `@tag report: [:foo, :bar]` which will include the values for tags `:foo` and `:bar` whenever a test fails
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx] Allow `IEX_WITH_WERL` to be set on Windows to always run on WERL mode
|
||||
* [IEx] Display type docs for `t(Module.type)` and `t(Module.type/arity)`
|
||||
* [IEx] Add `i/1` helper that prints information about any data type
|
||||
* [IEx] Show source code snippet whenever there is a request to pry a given process
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Add file to logger metadata
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Cache and always consolidate protocols
|
||||
* [Mix] Add `warn_test_pattern` to `mix test` that will warn on potentially misconfigured test files
|
||||
* [Mix] Introduce `MIX_QUIET` environment variable that configures the underlying Mix task to output only error messages
|
||||
* [Mix] Introduce `MIX_DEBUG` environment variable that prints information about the task being run
|
||||
* [Mix] Validate git options and warn on conflicting ref, branch or tags
|
||||
* [Mix] New umbrella applications will now share configuration and build files
|
||||
* [Mix] Add experimental support for Rebar 3
|
||||
* [Mix] Do not warn when an optional dependency has a conflicting `:only` option with another dependency
|
||||
* [Mix] Raise readable error message when parsertools is not available
|
||||
* [Mix] Add `--build` flag to `mix deps.clean DEP` to only remove artifacts from `_build`
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Kernel
|
||||
|
||||
* [Access] Improve error messages when using Access on non-valid key-value structures
|
||||
* [Kernel] Raise when conflicting `:only` and `:except` are given to import
|
||||
* [Kernel] Change `__ENV__.file` if `@file` is set for the given function
|
||||
* [Kernel] Make `Kernel.ParallelRequire` aware of `:warning_as_errors`
|
||||
* [Kernel] Improve error message for invalid `do`/`do:`
|
||||
* [Macro] Ensure `Macro.to_string/2` respects operator precedence when using the access operator
|
||||
* [Path] Do not crash when expanding paths that go beyond the root, for example, `Path.expand("/../..")`
|
||||
* [String] Ensure `UnicodeConversionError` does not contain invalid string in its error message
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx] Do not start apps on `recompile` helper if `--no-start` was given
|
||||
* [IEx] Avoid copying of data when evaluating every expression in IEx
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Always run non-recursive tasks at the umbrella root
|
||||
* [Mix] Ensure rebar projects work on directory names that contain non-latin characters
|
||||
* [Mix] Ignore directories inside `apps` in umbrellas that do not have a `mix.exs` file
|
||||
* [Mix] Ensure Mix can be used with path dependencies where the app name is different than the path basename
|
||||
* [Mix] Ensure dependencies won't crash when updating from a git repository to a hex repository and the git version did not respect SemVer
|
||||
* [Mix] Do not run remote converger if dependencies have diverged
|
||||
* [Mix] Ensure umbrella dependencies across all environments are loaded on parent deps.get/deps.update
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Include file and line in all compilation errors for doctests
|
||||
|
||||
### 3. Soft deprecations (no warnings emitted)
|
||||
|
||||
#### Kernel
|
||||
|
||||
* [Dict] `Dict` and `HashDict` are soft deprecated in favor of `Map`
|
||||
* [Keyword] `Keyword.size/1` is deprecated in favor of `length/1`
|
||||
* [Map] `Map.size/1` is deprecated in favor of `map_size/1`
|
||||
* [Set] `Set` and `HashSet` are soft deprecated in favor of `MapSet`
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] `Mix.Utils.camelize/1` and `Mix.Utils.underscore/1` are soft deprecated in favor of `Macro.camelize/1` and `Macro.underscore/1`
|
||||
|
||||
# v0.6.0 (2012-08-01)
|
||||
|
||||
* incompatible changes
|
||||
* [Kernel] Compiled files now follow `Elixir-ModuleName` convention to solve issues with Erlang embedded mode. This removes the `__MAIN__` pseudo-variable as modules are now located inside `Elixir` namespace;
|
||||
* [Kernel] `__using__` callback triggered by `use` now receives just one argument. Caller information can be accessed via macros using `__CALLER__`;
|
||||
* [Module] Removed data functions in favor of unifying the attributes API;
|
||||
* [Kernel] Comprehensions syntax changed to be more compatible with Erlang behavior;
|
||||
* [Kernel] loop and recur were removed in favor of recursion with named functions;
|
||||
|
||||
* deprecations
|
||||
* [Access] The semantics of the access protocol were reduced from a broad query API to simple data structure key-based access;
|
||||
* [Module] `Module.add_compile_callback(module, target, callback)` was deprecated in favor of `Module.add_attribute(module, :before_compile, { target, callback })`;
|
||||
* [Module] `Module.function_defined?` was deprecated in favor of `Module.defines?`;
|
||||
* [Module] `Module.defined_functions` was deprecated in favor of `Module.definitions_in`;
|
||||
* [File] `File.read_info` was deprecated in favor of `File.stat`;
|
||||
* [IO] `IO.print` was deprecated in favor of `IO.write`;
|
||||
* [Kernel] Deprecated `__LINE__` and `__FUNCTION__` in favor of `__ENV__.line` and `__ENV__.function`;
|
||||
* [Kernel] Deprecated `in_guard` in favor of `__CALLER__.in_guard?`;
|
||||
* [Kernel] `refer` is deprecated in favor of `alias`;
|
||||
* [ExUnit] Some assertions were deprecated in favor of simply using `assert()`;
|
||||
|
||||
* enhancements
|
||||
* [OptionParser] Make OptionParser public, add support to flags and improved switch parsing;
|
||||
* [Kernel] Operator `!` is now allowed in guard clauses;
|
||||
* [IEx] IEx now provides autocomplete if the OS supports tty;
|
||||
* [IEx] IEx now supports remsh;
|
||||
* [Mix] First Mix public release;
|
||||
* [Regex] Back references are now properly supported;
|
||||
* [IEx] Elixir now defaults to compile with documentation and `d` can be used in IEx to print modules and functions documentation;
|
||||
* [ExUnit] Support setup and teardown callbacks;
|
||||
* [Kernel] Introduced operator `=~` for regular expression matches;
|
||||
* [Kernel] Compiled docs now include the function signature;
|
||||
* [Kernel] `defmodule` do not start a new variable scope, this improves meta-programming capabilities;
|
||||
* [Range] Added a Range module with support to `in` operator (`x in 1..3`) and iterators;
|
||||
* [Enum] Enhanced Enum protocol to support `Enum.count`;
|
||||
* [Module] Added support to `@before_compile` and `@after_compile` callbacks. The first receives the module name while the latter receives the module name and its object code;
|
||||
* [Kernel] quote special form now supports line and unquote as options;
|
||||
* [Record] Allow `Record[_: value]` to set a default value to all records fields, as in Erlang;
|
||||
* [IEx] Functions `c` and `m` are available in IEx to compile and print available module information. Functions `h` and `v` are available to show history and print previous commands values;
|
||||
* [Enum] Optimized functions when a list is given as collection;
|
||||
* [System] Added `System.find_executable`
|
||||
* [Kernel] Document the macro `@` and allow attributes to be read inside functions;
|
||||
* [IO/File] Many improvements to `File` and `IO` modules;
|
||||
* [Macro] Added `Macro.expand`, useful for debugging what a macro expands to;
|
||||
* [Enum] Added `find_index`;
|
||||
* [Record] Records now provide a `to_keywords` function;
|
||||
* [Kernel] Added support to the `%R` sigil. The same as `%r`, but without interpolation or escaping. Both implementations were also optimized to generate the regex at compilation time;
|
||||
* [Kernel] Added `__ENV__` which returns a `Macro.Env` record with information about the compilation environment;
|
||||
* [Kernel] Added `__CALLER__` inside macros which returns a `Macro.Env` record with information about the calling site;
|
||||
|
||||
# v0.5.0 (2012-05-24)
|
||||
|
||||
* First official release
|
||||
@@ -1,22 +0,0 @@
|
||||
# Contributor Code of Conduct
|
||||
|
||||
As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
||||
|
||||
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality.
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery
|
||||
* Personal attacks
|
||||
* Trolling or insulting/derogatory comments
|
||||
* Public or private harassment
|
||||
* Publishing other's private information, such as physical or electronic addresses, without explicit permission
|
||||
* Other unethical or unprofessional conduct.
|
||||
|
||||
The Elixir Core Team has the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, we commit ourselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team.
|
||||
|
||||
This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by sending an e-mail to elixir-lang-conduct@googlegroups.com.
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
|
||||
-275
@@ -1,275 +0,0 @@
|
||||
# Contributing to Elixir
|
||||
|
||||
Please take a moment to review this document in order to make the contribution
|
||||
process easy and effective for everyone involved!
|
||||
|
||||
Also make sure you read our [Code of Conduct](CODE_OF_CONDUCT.md) that
|
||||
outlines our commitment towards an open and welcoming environment.
|
||||
|
||||
## Using the issue tracker
|
||||
|
||||
Use the issues tracker for:
|
||||
|
||||
* [bug reports](#bug-reports)
|
||||
* [submitting pull requests](#pull-requests)
|
||||
|
||||
Please **do not** use the issue tracker for personal support requests nor feature requests. Support requests should be sent to:
|
||||
|
||||
* [the elixir-talk mailing list](https://groups.google.com/group/elixir-lang-talk)
|
||||
* [Stack Overflow](http://stackoverflow.com/questions/ask?tags=elixir)
|
||||
* **[#elixir-lang](irc://chat.freenode.net/elixir-lang)** IRC channel on [chat.freenode.net](http://www.freenode.net/)
|
||||
|
||||
Feature requests can be discussed on [the elixir-core mailing list](https://groups.google.com/group/elixir-lang-core).
|
||||
|
||||
We do our best to keep the issue tracker tidy and organized, making it useful
|
||||
for everyone. For example, we classify open issues per application and perceived
|
||||
difficulty of the issue, making it easier for developers to
|
||||
[contribute to Elixir](#contributing).
|
||||
|
||||
## Bug reports
|
||||
|
||||
A bug is a _demonstrable problem_ that is caused by the code in the repository.
|
||||
Good bug reports are extremely helpful - thank you!
|
||||
|
||||
Guidelines for bug reports:
|
||||
|
||||
1. **Use the GitHub issue search** — [check if the issue has already been
|
||||
reported](https://github.com/elixir-lang/elixir/search?type=Issues).
|
||||
|
||||
2. **Check if the issue has been fixed** — try to reproduce it using the
|
||||
`master` branch in the repository.
|
||||
|
||||
3. **Isolate and report the problem** — ideally create a reduced test
|
||||
case.
|
||||
|
||||
Please try to be as detailed as possible in your report. Include information about
|
||||
your Operating System, your Erlang and Elixir versions. Please provide steps to
|
||||
reproduce the issue as well as the outcome you were expecting! All these details
|
||||
will help developers to fix any potential bugs.
|
||||
|
||||
Example:
|
||||
|
||||
> Short and descriptive example bug report title
|
||||
>
|
||||
> A summary of the issue and the environment in which it occurs. If suitable,
|
||||
> include the steps required to reproduce the bug.
|
||||
>
|
||||
> 1. This is the first step
|
||||
> 2. This is the second step
|
||||
> 3. Further steps, etc.
|
||||
>
|
||||
> `<url>` - a link to the reduced test case (e.g. a GitHub Gist)
|
||||
>
|
||||
> Any other information you want to share that is relevant to the issue being
|
||||
> reported. This might include the lines of code that you have identified as
|
||||
> causing the bug, and potential solutions (and your opinions on their
|
||||
> merits).
|
||||
|
||||
## Feature requests
|
||||
|
||||
Feature requests are welcome and should be discussed on [the elixir-core mailing list](https://groups.google.com/group/elixir-lang-core). But take a moment to find
|
||||
out whether your idea fits with the scope and aims of the project. It's up to *you*
|
||||
to make a strong case to convince the community of the merits of this feature.
|
||||
Please provide as much detail and context as possible.
|
||||
|
||||
## Contributing
|
||||
|
||||
We incentivize everyone to contribute to Elixir and help us tackle
|
||||
existing issues! To do so, there are a few things you need to know
|
||||
about the code. First, Elixir code is divided in applications inside
|
||||
the `lib` folder:
|
||||
|
||||
* `elixir` - Contains Elixir's kernel and stdlib
|
||||
|
||||
* `eex` - Template engine that allows you to embed Elixir
|
||||
|
||||
* `ex_unit` - Simple test framework that ships with Elixir
|
||||
|
||||
* `iex` - IEx, Elixir's interactive shell
|
||||
|
||||
* `mix` - Elixir's build tool
|
||||
|
||||
You can run all tests in the root directory with `make test` and you can
|
||||
also run tests for a specific framework `make test_#{NAME}`, for example,
|
||||
`make test_ex_unit`.
|
||||
|
||||
In case you are changing a single file, you can compile and run tests only
|
||||
for that particular file for fast development cycles. For example, if you
|
||||
are changing the String module, you can compile it and run its tests as:
|
||||
|
||||
```sh
|
||||
bin/elixirc lib/elixir/lib/string.ex -o lib/elixir/ebin
|
||||
bin/elixir lib/elixir/test/elixir/string_test.exs
|
||||
```
|
||||
|
||||
After your changes are done, please remember to run the full suite with
|
||||
`make test`.
|
||||
|
||||
From time to time, your tests may fail in an existing Elixir checkout and
|
||||
may require a clean start by running `make clean compile`. You can always
|
||||
check [the official build status on Travis-CI](https://travis-ci.org/elixir-lang/elixir).
|
||||
|
||||
With tests running and passing, you are ready to contribute to Elixir and
|
||||
send your pull requests.
|
||||
|
||||
## Contributing Documentation
|
||||
|
||||
Code documentation (`@doc`, `@moduledoc`, `@typedoc`) has a special convention:
|
||||
the first paragraph is considered to be a short summary.
|
||||
|
||||
For functions, macros and callbacks say what it will do. For example write
|
||||
something like:
|
||||
|
||||
```elixir
|
||||
@doc """
|
||||
Returns only those elements for which `fun` is `true`.
|
||||
|
||||
...
|
||||
"""
|
||||
def filter(collection, fun) ...
|
||||
```
|
||||
|
||||
For modules, protocols and types say what it is. For example write
|
||||
something like:
|
||||
|
||||
```elixir
|
||||
defmodule File.Stat do
|
||||
@moduledoc """
|
||||
Information about a file.
|
||||
|
||||
...
|
||||
"""
|
||||
|
||||
defstruct [...]
|
||||
end
|
||||
```
|
||||
|
||||
Keep in mind that the first paragraph might show up in a summary somewhere, long
|
||||
texts in the first paragraph create very ugly summaries. As a rule of thumb
|
||||
anything longer than 80 characters is too long.
|
||||
|
||||
Try to keep unnecessary details out of the first paragraph, it's only there to
|
||||
give a user a quick idea of what the documented "thing" does/is. The rest of the
|
||||
documentation string can contain the details, for example when a value and when
|
||||
`nil` is returned.
|
||||
|
||||
If possible include examples, preferably in a form that works with doctests. For
|
||||
example:
|
||||
|
||||
```elixir
|
||||
@doc """
|
||||
Returns only those elements for which `fun` is `true`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Enum.filter([1, 2, 3], fn(x) -> rem(x, 2) == 0 end)
|
||||
[2]
|
||||
|
||||
"""
|
||||
def filter(collection, fun) ...
|
||||
```
|
||||
|
||||
This makes it easy to test the examples so that they don't go stale and examples
|
||||
are often a great help in explaining what a function does.
|
||||
|
||||
## Pull requests
|
||||
|
||||
Good pull requests - patches, improvements, new features - are a fantastic
|
||||
help. They should remain focused in scope and avoid containing unrelated
|
||||
commits.
|
||||
|
||||
**NOTE**: Do not send code style changes as pull requests like changing
|
||||
the indentation of some particular code snippet or how a function is called.
|
||||
Those will not be accepted as they pollute the repository history with non
|
||||
functional changes and are often based on personal preferences.
|
||||
|
||||
**IMPORTANT**: By submitting a patch, you agree that your work will be
|
||||
licensed under the license used by the project.
|
||||
|
||||
If you have any large pull request in mind (e.g. implementing features,
|
||||
refactoring code, etc), **please ask first** otherwise you risk spending
|
||||
a lot of time working on something that the project's developers might
|
||||
not want to merge into the project.
|
||||
|
||||
Please adhere to the coding conventions in the project (indentation,
|
||||
accurate comments, etc.) and don't forget to add your own tests and
|
||||
documentation. When working with Git, we recommend the following process
|
||||
in order to craft an excellent pull request:
|
||||
|
||||
1. [Fork](https://help.github.com/fork-a-repo/) the project, clone your fork,
|
||||
and configure the remotes:
|
||||
|
||||
```sh
|
||||
# Clone your fork of the repo into the current directory
|
||||
git clone https://github.com/<your-username>/elixir
|
||||
# Navigate to the newly cloned directory
|
||||
cd elixir
|
||||
# Assign the original repo to a remote called "upstream"
|
||||
git remote add upstream https://github.com/elixir-lang/elixir
|
||||
```
|
||||
|
||||
2. If you cloned a while ago, get the latest changes from upstream:
|
||||
|
||||
```sh
|
||||
git checkout master
|
||||
git pull upstream master
|
||||
```
|
||||
|
||||
3. Create a new topic branch (off of `master`) to contain your feature, change,
|
||||
or fix.
|
||||
|
||||
**IMPORTANT**: Making changes in `master` is discouraged. You should always
|
||||
keep your local `master` in sync with upstream `master` and make your
|
||||
changes in topic branches.
|
||||
|
||||
```sh
|
||||
git checkout -b <topic-branch-name>
|
||||
```
|
||||
|
||||
4. Commit your changes in logical chunks. Keep your commit messages organized,
|
||||
with a short description in the first line and more detailed information on
|
||||
the following lines. Feel free to use Git's
|
||||
[interactive rebase](https://help.github.com/articles/interactive-rebase)
|
||||
feature to tidy up your commits before making them public.
|
||||
|
||||
5. Make sure all the tests are still passing.
|
||||
|
||||
```sh
|
||||
make test
|
||||
```
|
||||
|
||||
This command will compile the code in your branch and use that
|
||||
version of Elixir to run the tests. This is needed to ensure your changes can
|
||||
pass all the tests.
|
||||
|
||||
6. Push your topic branch up to your fork:
|
||||
|
||||
```sh
|
||||
git push origin <topic-branch-name>
|
||||
```
|
||||
|
||||
7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/)
|
||||
with a clear title and description.
|
||||
|
||||
8. If you haven't updated your pull request for a while, you should consider
|
||||
rebasing on master and resolving any conflicts.
|
||||
|
||||
**IMPORTANT**: _Never ever_ merge upstream `master` into your branches. You
|
||||
should always `git rebase` on `master` to bring your changes up to date when
|
||||
necessary.
|
||||
|
||||
```sh
|
||||
git checkout master
|
||||
git pull upstream master
|
||||
git checkout <your-topic-branch>
|
||||
git rebase master
|
||||
```
|
||||
|
||||
We have saved some excellent pull requests we have received in the past in case
|
||||
you are looking for some examples:
|
||||
|
||||
* [Implement Enum.member? – Pull Request](https://github.com/elixir-lang/elixir/pull/992)
|
||||
* [Add String.valid? – Pull Request](https://github.com/elixir-lang/elixir/pull/1058)
|
||||
* [Implement capture_io for ExUnit – Pull Request](https://github.com/elixir-lang/elixir/pull/1059)
|
||||
|
||||
Thank you for your contributions!
|
||||
@@ -0,0 +1,10 @@
|
||||
LEGAL NOTICE INFORMATION
|
||||
------------------------
|
||||
|
||||
All the files in this distribution are covered under either Elixir's
|
||||
license (see the file LICENSE) except the files mentioned below that
|
||||
contains sections that are under Erlang's License (EPL):
|
||||
|
||||
lib/elixir/src/elixir_glob.erl
|
||||
lib/elixir/src/elixir_parser.erl (generated by build scripts)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Copyright 2012 Plataformatec
|
||||
Copyright 2012 Plataformatec.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,262 +1,103 @@
|
||||
REBAR ?= "$(CURDIR)/rebar"
|
||||
PREFIX ?= /usr/local
|
||||
DOCS := v1.2
|
||||
CANONICAL := stable
|
||||
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict
|
||||
ERLC := erlc -I lib/elixir/include
|
||||
ERL := erl -I lib/elixir/include -noshell -pa lib/elixir/ebin
|
||||
VERSION := $(strip $(shell cat VERSION))
|
||||
Q := @
|
||||
LIBDIR := lib
|
||||
INSTALL = install
|
||||
INSTALL_DIR = $(INSTALL) -m755 -d
|
||||
INSTALL_DATA = $(INSTALL) -m644
|
||||
INSTALL_PROGRAM = $(INSTALL) -m755
|
||||
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.6.0
|
||||
|
||||
.PHONY: install compile erlang elixir build_plt clean_plt dialyze test clean install_man clean_man docs Docs.zip Precompiled.zip publish_zips publish_docs publish_mix
|
||||
.PHONY: 1
|
||||
.NOTPARALLEL: compile
|
||||
|
||||
#==> Functions
|
||||
#==> Templates
|
||||
define TASK_TEMPLATE
|
||||
$(1): lib/$(1)/ebin/Elixir-$(2).beam lib/$(1)/ebin/$(1).app
|
||||
|
||||
define CHECK_ERLANG_RELEASE
|
||||
$(Q) erl -noshell -eval 'io:fwrite("~s", [erlang:system_info(otp_release) >= "18"])' -s erlang halt | grep -q '^true'; \
|
||||
if [ $$? != 0 ]; then \
|
||||
echo "At least Erlang 18.0 is required to build Elixir"; \
|
||||
exit 1; \
|
||||
fi;
|
||||
endef
|
||||
lib/$(1)/ebin/$(1).app:
|
||||
@ cd lib/$(1) && ../../bin/elixir ../../bin/mix compile.app
|
||||
|
||||
define APP_TEMPLATE
|
||||
$(1): lib/$(1)/ebin/Elixir.$(2).beam lib/$(1)/ebin/$(1).app
|
||||
|
||||
lib/$(1)/ebin/$(1).app: lib/$(1)/mix.exs
|
||||
$(Q) mkdir -p lib/$(1)/_build/shared/lib/$(1)
|
||||
$(Q) cp -R lib/$(1)/ebin lib/$(1)/_build/shared/lib/$(1)/
|
||||
$(Q) cd lib/$(1) && ../../bin/elixir -e 'Mix.start(:permanent, [])' -r mix.exs -e 'Mix.Task.run("compile.app")'
|
||||
$(Q) cp lib/$(1)/_build/shared/lib/$(1)/ebin/$(1).app lib/$(1)/ebin/$(1).app
|
||||
$(Q) rm -rf lib/$(1)/_build
|
||||
|
||||
lib/$(1)/ebin/Elixir.$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1)/lib/*/*.ex) $(wildcard lib/$(1)/lib/*/*/*.ex)
|
||||
lib/$(1)/ebin/Elixir-$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1)/lib/*/*.ex) $(wildcard lib/$(1)/lib/*/*/*.ex) $$(FORCE)
|
||||
@ echo "==> $(1) (compile)"
|
||||
@ rm -rf lib/$(1)/ebin
|
||||
$(Q) cd lib/$(1) && ../../$$(ELIXIRC) "lib/**/*.ex" -o ebin
|
||||
@ $$(ELIXIRC) "lib/$(1)/lib/**/*.ex" -o lib/$(1)/ebin
|
||||
|
||||
test_$(1): compile $(1)
|
||||
test_$(1): $(1)
|
||||
@ echo "==> $(1) (exunit)"
|
||||
$(Q) cd lib/$(1) && ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/*_test.exs";
|
||||
@ cd lib/$(1) && time ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/*_test.exs"
|
||||
endef
|
||||
|
||||
#==> Compilation tasks
|
||||
|
||||
KERNEL:=lib/elixir/ebin/Elixir.Kernel.beam
|
||||
UNICODE:=lib/elixir/ebin/Elixir.String.Unicode.beam
|
||||
|
||||
default: compile
|
||||
KERNEL:=lib/elixir/ebin/Elixir-Kernel.beam
|
||||
|
||||
compile: lib/elixir/src/elixir.app.src erlang elixir
|
||||
|
||||
lib/elixir/src/elixir.app.src: src/elixir.app.src
|
||||
$(Q) $(call CHECK_ERLANG_RELEASE)
|
||||
$(Q) rm -rf lib/elixir/src/elixir.app.src
|
||||
$(Q) echo "%% This file is automatically generated from <project_root>/src/elixir.app.src" \
|
||||
>lib/elixir/src/elixir.app.src
|
||||
$(Q) cat src/elixir.app.src >>lib/elixir/src/elixir.app.src
|
||||
@ rm -rf lib/elixir/src/elixir.app.src
|
||||
@ cp src/elixir.app.src lib/elixir/src/elixir.app.src
|
||||
|
||||
erlang:
|
||||
$(Q) cd lib/elixir && $(REBAR) compile
|
||||
@ cd lib/elixir && $(REBAR) compile
|
||||
|
||||
# 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: stdlib lib/eex/ebin/Elixir.EEx.beam mix ex_unit logger eex iex
|
||||
# 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
|
||||
|
||||
stdlib: $(KERNEL) VERSION
|
||||
$(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
|
||||
$(Q) if [ ! -f $(KERNEL) ]; then \
|
||||
echo "==> bootstrap (compile)"; \
|
||||
$(ERL) -s elixir_compiler core -s erlang halt; \
|
||||
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; \
|
||||
fi
|
||||
@ echo "==> elixir (compile)";
|
||||
$(Q) cd lib/elixir && ../../$(ELIXIRC) "lib/kernel.ex" -o ebin;
|
||||
$(Q) cd lib/elixir && ../../$(ELIXIRC) "lib/**/*.ex" -o ebin;
|
||||
$(Q) $(MAKE) unicode
|
||||
$(Q) rm -rf lib/elixir/ebin/elixir.app
|
||||
$(Q) cd lib/elixir && $(REBAR) compile
|
||||
@ rm -rf lib/elixir/ebin/elixir.app
|
||||
@ cd lib/elixir && $(REBAR) compile
|
||||
|
||||
unicode: $(UNICODE)
|
||||
$(UNICODE): lib/elixir/unicode/*
|
||||
@ echo "==> unicode (compile)";
|
||||
@ echo "Embedding the Unicode database... (this may take a while)"
|
||||
$(Q) cd lib/elixir && ../../$(ELIXIRC) unicode/unicode.ex -o ebin;
|
||||
|
||||
$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
|
||||
$(eval $(call APP_TEMPLATE,logger,Logger))
|
||||
$(eval $(call APP_TEMPLATE,eex,EEx))
|
||||
$(eval $(call APP_TEMPLATE,mix,Mix))
|
||||
$(eval $(call APP_TEMPLATE,iex,IEx))
|
||||
|
||||
install: compile
|
||||
@ echo "==> elixir (install)"
|
||||
$(Q) for dir in lib/*; do \
|
||||
rm -Rf $(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/$$dir/ebin; \
|
||||
$(INSTALL_DIR) "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/$$dir/ebin"; \
|
||||
$(INSTALL_DATA) $$dir/ebin/* "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/$$dir/ebin"; \
|
||||
done
|
||||
$(Q) $(INSTALL_DIR) "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/bin"
|
||||
$(Q) $(INSTALL_PROGRAM) $(filter-out %.ps1, $(filter-out %.bat, $(wildcard bin/*))) "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/bin"
|
||||
$(Q) $(INSTALL_DIR) "$(DESTDIR)$(PREFIX)/bin"
|
||||
$(Q) for file in "$(DESTDIR)$(PREFIX)"/$(LIBDIR)/elixir/bin/* ; do \
|
||||
ln -sf "../$(LIBDIR)/elixir/bin/$${file##*/}" "$(DESTDIR)$(PREFIX)/bin/" ; \
|
||||
done
|
||||
$(MAKE) install_man
|
||||
$(eval $(call TASK_TEMPLATE,ex_unit,ExUnit))
|
||||
$(eval $(call TASK_TEMPLATE,eex,EEx))
|
||||
$(eval $(call TASK_TEMPLATE,mix,Mix))
|
||||
|
||||
clean:
|
||||
cd lib/elixir && $(REBAR) clean
|
||||
@ rm -rf .full
|
||||
@ rm -rf lib/*/ebin
|
||||
@ cd lib/elixir && $(REBAR) clean
|
||||
|
||||
#==> 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
|
||||
|
||||
docs: $(FULLFLAG)
|
||||
mkdir -p ebin
|
||||
rm -rf docs
|
||||
cp -R -f lib/*/ebin/*.beam ./ebin
|
||||
bin/elixir ../exdoc/bin/exdoc
|
||||
rm -rf ebin
|
||||
rm -rf lib/*/ebin
|
||||
rm -rf lib/elixir/test/ebin
|
||||
rm -rf lib/*/tmp
|
||||
rm -rf lib/mix/test/fixtures/git_repo
|
||||
rm -rf lib/mix/test/fixtures/deps_on_git_repo
|
||||
rm -rf lib/mix/test/fixtures/git_rebar
|
||||
rm -rf lib/elixir/src/elixir.app.src
|
||||
$(MAKE) clean_man
|
||||
|
||||
clean_exbeam:
|
||||
$(Q) rm -f lib/*/ebin/Elixir.*.beam
|
||||
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
|
||||
|
||||
#==> Create Documentation
|
||||
|
||||
LOGO_PATH = $(shell test -f ../docs/logo.png && echo "--logo ../docs/logo.png")
|
||||
SOURCE_REF = $(shell head="$$(git rev-parse HEAD)" tag="$$(git tag --points-at $$head | tail -1)" ; echo "$${tag:-$$head}\c")
|
||||
COMPILE_DOCS = bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" -m "$(3)" -u "https://github.com/elixir-lang/elixir" --source-ref "v$(VERSION)" $(call LOGO_PATH) -o doc/$(2) -a http://elixir-lang.org/docs/$(CANONICAL)/$(2)/ -p http://elixir-lang.org/docs.html $(4)
|
||||
|
||||
docs: compile ../ex_doc/bin/ex_doc docs_elixir docs_eex docs_mix docs_iex docs_ex_unit docs_logger
|
||||
|
||||
docs_elixir: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (elixir)"
|
||||
$(Q) rm -rf doc/elixir
|
||||
$(call COMPILE_DOCS,Elixir,elixir,Kernel,-e "lib/elixir/pages/Typespecs.md" -e "lib/elixir/pages/Writing Documentation.md")
|
||||
|
||||
docs_eex: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (eex)"
|
||||
$(Q) rm -rf doc/eex
|
||||
$(call COMPILE_DOCS,EEx,eex,EEx)
|
||||
|
||||
docs_mix: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (mix)"
|
||||
$(Q) rm -rf doc/mix
|
||||
$(call COMPILE_DOCS,Mix,mix,Mix)
|
||||
|
||||
docs_iex: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (iex)"
|
||||
$(Q) rm -rf doc/iex
|
||||
$(call COMPILE_DOCS,IEx,iex,IEx)
|
||||
|
||||
docs_ex_unit: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (ex_unit)"
|
||||
$(Q) rm -rf doc/ex_unit
|
||||
$(call COMPILE_DOCS,ExUnit,ex_unit,ExUnit)
|
||||
|
||||
docs_logger: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (logger)"
|
||||
$(Q) rm -rf doc/logger
|
||||
$(call COMPILE_DOCS,Logger,logger,Logger)
|
||||
|
||||
../ex_doc/bin/ex_doc:
|
||||
@ echo "ex_doc is not found in ../ex_doc as expected. See README for more information."
|
||||
@ false
|
||||
|
||||
#==> Zips
|
||||
|
||||
Docs.zip: docs
|
||||
rm -rf Docs-v$(VERSION).zip
|
||||
zip -9 -r Docs-v$(VERSION).zip CHANGELOG.md doc NOTICE LICENSE README.md
|
||||
@ echo "Docs file created $(CURDIR)/Docs-v$(VERSION).zip"
|
||||
|
||||
Precompiled.zip: build_man compile
|
||||
rm -rf Precompiled-v$(VERSION).zip
|
||||
zip -9 -r Precompiled-v$(VERSION).zip bin CHANGELOG.md lib/*/ebin LICENSE man NOTICE README.md VERSION
|
||||
@ echo "Precompiled file created $(CURDIR)/Precompiled-v$(VERSION).zip"
|
||||
|
||||
#==> Publish
|
||||
|
||||
publish_zips: Precompiled.zip Docs.zip
|
||||
|
||||
publish_docs: docs
|
||||
rm -rf ../docs/$(DOCS)/*/
|
||||
cp -R doc/* ../docs/$(DOCS)
|
||||
release_erl: $(FULLFLAG)
|
||||
@ rm -rf rel/elixir
|
||||
@ cd rel && ../rebar generate
|
||||
|
||||
#==> Tests tasks
|
||||
|
||||
test: test_erlang test_elixir
|
||||
|
||||
TEST_ERL = lib/elixir/test/erlang
|
||||
TEST_EBIN = lib/elixir/test/ebin
|
||||
TEST_ERLS = $(addprefix $(TEST_EBIN)/, $(addsuffix .beam, $(basename $(notdir $(wildcard $(TEST_ERL)/*.erl)))))
|
||||
|
||||
test_erlang: compile $(TEST_ERLS)
|
||||
test_erlang: compile
|
||||
@ echo "==> elixir (eunit)"
|
||||
$(Q) $(ERL) -pa $(TEST_EBIN) -s test_helper test;
|
||||
@ echo ""
|
||||
@ mkdir -p lib/elixir/test/ebin
|
||||
@ $(ERLC) -pa lib/elixir/ebin -o lib/elixir/test/ebin lib/elixir/test/erlang/*.erl
|
||||
@ time $(ERL) -pa lib/elixir/test/ebin -s test_helper test -s erlang halt
|
||||
@ echo
|
||||
|
||||
$(TEST_EBIN)/%.beam: $(TEST_ERL)/%.erl
|
||||
$(Q) mkdir -p $(TEST_EBIN)
|
||||
$(Q) $(ERLC) -o $(TEST_EBIN) $<
|
||||
test_elixir: test_kernel test_mix test_ex_unit test_eex
|
||||
|
||||
test_elixir: test_stdlib test_ex_unit test_logger test_mix test_eex test_iex
|
||||
|
||||
test_stdlib: compile
|
||||
@ echo "==> elixir (exunit)"
|
||||
$(Q) exec epmd & exit
|
||||
$(Q) if [ "$(OS)" = "Windows_NT" ]; then \
|
||||
cd lib/elixir && cmd //C call ../../bin/elixir.bat -r "test/elixir/test_helper.exs" -pr "test/elixir/**/*_test.exs"; \
|
||||
else \
|
||||
cd lib/elixir && ../../bin/elixir -r "test/elixir/test_helper.exs" -pr "test/elixir/**/*_test.exs"; \
|
||||
fi
|
||||
|
||||
#==> Dialyzer tasks
|
||||
|
||||
DIALYZER_OPTS = --no_check_plt --fullpath -Werror_handling -Wunmatched_returns -Wunderspecs
|
||||
PLT = .elixir.plt
|
||||
|
||||
$(PLT):
|
||||
@ echo "==> Building PLT with Elixir's dependencies..."
|
||||
$(Q) dialyzer --output_plt $(PLT) --build_plt --apps erts kernel stdlib compiler syntax_tools parsetools tools ssl inets
|
||||
|
||||
clean_plt:
|
||||
$(Q) rm -f $(PLT)
|
||||
|
||||
build_plt: clean_plt $(PLT)
|
||||
|
||||
dialyze: compile $(PLT)
|
||||
@ echo "==> Dialyzing Elixir..."
|
||||
$(Q) dialyzer --plt $(PLT) $(DIALYZER_OPTS) lib/*/ebin
|
||||
|
||||
#==> Man page tasks
|
||||
|
||||
build_man: man/iex.1 man/elixir.1
|
||||
|
||||
man/iex.1:
|
||||
$(Q) cp man/iex.1.in man/iex.1
|
||||
$(Q) sed -i.bak "/{COMMON}/r common" man/iex.1
|
||||
$(Q) sed -i.bak "/{COMMON}/d" man/iex.1
|
||||
$(Q) rm man/iex.1.bak
|
||||
|
||||
man/elixir.1:
|
||||
$(Q) cp man/elixir.1.in man/elixir.1
|
||||
$(Q) sed -i.bak "/{COMMON}/r common" man/elixir.1
|
||||
$(Q) sed -i.bak "/{COMMON}/d" man/elixir.1
|
||||
$(Q) rm man/elixir.1.bak
|
||||
|
||||
clean_man:
|
||||
rm -f man/elixir.1
|
||||
rm -f man/iex.1
|
||||
|
||||
install_man: build_man
|
||||
$(Q) mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/elixir.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/elixirc.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/iex.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/mix.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(MAKE) clean_man
|
||||
test_kernel: compile
|
||||
@ echo "==> kernel (exunit)"
|
||||
@ cd lib/elixir && time ../../bin/elixir -r "test/elixir/test_helper.exs" -pr "test/elixir/**/*_test.exs"
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
LEGAL NOTICE INFORMATION
|
||||
------------------------
|
||||
|
||||
All the files in this distribution are copyright (c) 2012 Plataformatec
|
||||
covered under Elixir's license (see the file LICENSE) except the cases
|
||||
below.
|
||||
|
||||
== lib/elixir/src/elixir_parser.erl (generated by build scripts)
|
||||
|
||||
Copyright Ericsson AB 1996-2015
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -1,89 +1,52 @@
|
||||

|
||||
=========
|
||||
[](https://travis-ci.org/elixir-lang/elixir)
|
||||
[](http://travis-ci.org/elixir-lang/elixir)
|
||||
|
||||
For more about Elixir, installation and documentation,
|
||||
[check Elixir's website](http://elixir-lang.org/).
|
||||
For more about Elixir, installation and documentation, [check Elixir's website](http://elixir-lang.org/).
|
||||
|
||||
## Usage
|
||||
# Usage
|
||||
|
||||
If you want to contribute to Elixir or run it from source, clone this
|
||||
repository to your machine, compile and test it:
|
||||
If you want to contribute to Elixir or run it from source, clone this repository to your machine, compile and test it:
|
||||
|
||||
```sh
|
||||
git clone https://github.com/elixir-lang/elixir.git
|
||||
cd elixir
|
||||
make clean test
|
||||
```
|
||||
$ git clone https://github.com/elixir-lang/elixir.git
|
||||
$ cd elixir
|
||||
$ make test
|
||||
|
||||
> Note: if you are running on Windows,
|
||||
[this article includes important notes for compiling Elixir from source
|
||||
on Windows](https://github.com/elixir-lang/elixir/wiki/Windows).
|
||||
If tests pass, you are ready to move on to the [Getting Started guide][1] or to try Interactive Elixir by running: `bin/iex` in your terminal.
|
||||
|
||||
If Elixir fails to build (specifically when pulling in a new version via
|
||||
`git`), be sure to remove any previous build artifacts by running
|
||||
`make clean`, then `make test`.
|
||||
However, if tests fail, it is likely you have an outdated Erlang version (Elixir requires Erlang R15B or later). You can check your Erlang version by calling `erl` in the command line. You will see some information as follow:
|
||||
|
||||
If tests pass, you are ready to move on to the
|
||||
[Getting Started guide][1] or to try Interactive Elixir by running:
|
||||
`bin/iex` in your terminal.
|
||||
Erlang R15B (erts-5.8.4) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
|
||||
|
||||
However, if tests fail, it is likely you have an outdated Erlang version
|
||||
(Elixir requires Erlang 18.0 or later).
|
||||
You can check your Erlang version by calling `erl` in the command line.
|
||||
You will see some information as follows:
|
||||
If you have the correct version and tests still fail, feel free to [open an issue][2].
|
||||
|
||||
`Erlang/OTP 18 [erts-7.0] [source] [smp:2:2] [async-threads:10] [hipe]
|
||||
[kernel-poll:false]`
|
||||
# Contributing
|
||||
|
||||
If you have the correct version and tests still fail, feel free to
|
||||
[open an issue][2].
|
||||
If you want to contribute, Elixir code is divided in applications inside the `lib` folder:
|
||||
|
||||
## Building documentation
|
||||
* `elixir` - Contains Elixir's kernel and stdlib;
|
||||
|
||||
Building the documentation requires
|
||||
[ExDoc](https://github.com/elixir-lang/ex_doc) to be installed and built
|
||||
alongside Elixir.
|
||||
* `eex` - Template engine that allows you to embed Elixir;
|
||||
|
||||
```sh
|
||||
# After cloning and compiling Elixir, in its parent directory:
|
||||
git clone git://github.com/elixir-lang/ex_doc.git
|
||||
cd ex_doc && ../elixir/bin/mix do deps.get, compile
|
||||
cd ../elixir && make docs
|
||||
```
|
||||
* `ex_unit` - Simple test framework that ships with Elixir;
|
||||
|
||||
This will produce documentation sets for `elixir`, `mix`, etc., under the `doc` directory.
|
||||
|
||||
## Contributing
|
||||
|
||||
We appreciate any contribution to Elixir.
|
||||
Check our [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) and
|
||||
[CONTRIBUTING.md](CONTRIBUTING.md) guides for more information.
|
||||
We usually keep a list of features and bugs [in the issue tracker][2].
|
||||
|
||||
## Important links
|
||||
# Important links
|
||||
|
||||
* [Elixir Website][1]
|
||||
* [Elixir Documentation][7]
|
||||
* **[#elixir-lang][5]** on [Freenode][6] IRC
|
||||
* #elixir-lang on freenode IRC
|
||||
* [Website][1]
|
||||
* [Issue tracker][2]
|
||||
* [elixir-talk Mailing list (questions)][3]
|
||||
* [elixir-core Mailing list (development)][4]
|
||||
* [Mailing list][3]
|
||||
|
||||
[1]: http://elixir-lang.org
|
||||
[2]: https://github.com/elixir-lang/elixir/issues
|
||||
[3]: https://groups.google.com/group/elixir-lang-talk
|
||||
[4]: https://groups.google.com/group/elixir-lang-core
|
||||
[5]: https://webchat.freenode.net/?channels=#elixir-lang
|
||||
[6]: http://www.freenode.net
|
||||
[7]: http://elixir-lang.org/docs.html
|
||||
[3]: http://groups.google.com/group/elixir-lang-core
|
||||
|
||||
## License
|
||||
# License
|
||||
|
||||
"Elixir" and the Elixir logo are copyright (c) 2012 Plataformatec.
|
||||
|
||||
Elixir source code is released under Apache 2 License.
|
||||
Elixir source code is released under Apache 2 License with some parts under Erlang's license (EPL).
|
||||
|
||||
Check [NOTICE](NOTICE) and [LICENSE](LICENSE) files for more
|
||||
information.
|
||||
Check LEGAL and LICENSE files for more information.
|
||||
+13
-29
@@ -1,41 +1,25 @@
|
||||
# Release process
|
||||
|
||||
## All releases
|
||||
## Release process
|
||||
|
||||
This document simply outlines the release process:
|
||||
|
||||
1. Ensure you are running on the oldest supported Erlang version
|
||||
1) Remove .dev extension from current versions
|
||||
|
||||
2. Remove all `-dev` extension from versions (see below for all files)
|
||||
2) Run `make clean test` to ensure all tests pass from scratch
|
||||
|
||||
3. Ensure CHANGELOG is updated and add current date
|
||||
3) Ensure CHANGELOG is updated and tag release version with timestamp in it
|
||||
|
||||
4. Commit changes above with title "Release vVERSION" and generate new tag
|
||||
4) Commit changes above and tag new version on Git
|
||||
|
||||
5. Run `make clean test` to ensure all tests pass from scratch and the CI is green
|
||||
5) Release new docs, update elixir-lang.org
|
||||
|
||||
6. Recompile an existing project (for example, Ecto) to ensure manifests can be upgraded
|
||||
6) Push new zip to Elixir's downloads page
|
||||
|
||||
7. Push branch and the new tag
|
||||
|
||||
8. Publish new docs with `make publish_docs`, copy docs to `docs/stable` if appropriate, and push to GitHub Pages
|
||||
|
||||
9. Publish new zips with `make publish_zips`, upload `Precompiled.zip` and `Docs.zip` to GitHub Releases
|
||||
|
||||
10. Add the release to `elixir.csv` file in `elixir-lang/elixir-lang.github.com`
|
||||
|
||||
11. Build and push standalone Mix with `make publish_mix` (requires AWS credentials)
|
||||
|
||||
## New vMAJOR.MINOR releases
|
||||
|
||||
12. Create a new branch "vMAJOR.MINOR"
|
||||
|
||||
13. Move docs generation to `docs/vMAJOR.MINOR` in Makefile and copy them from `docs/stable` (change index.html accordingly)
|
||||
|
||||
14. In master, bump versions, start new CHANGELOG, add `-dev` back and commit "Start vVERSION+1"
|
||||
7) After release, bump versions and add .dev back
|
||||
|
||||
## Places where version is mentioned
|
||||
|
||||
* VERSION (make sure there is no newline in this file)
|
||||
* CHANGELOG.md
|
||||
* src/elixir.app.src (not lib/elixir/src/elixir.app.src)
|
||||
* src/elixir.app.src
|
||||
* lib/elixir/lib/system.ex
|
||||
* rel/reltool.config
|
||||
* Makefile
|
||||
* CHANGELOG
|
||||
+13
-51
@@ -1,33 +1,29 @@
|
||||
#!/bin/sh
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
-v Prints version and exits
|
||||
-v Prints version and exit
|
||||
-e \"command\" Evaluates the given command (*)
|
||||
-r \"file\" Requires the given files/patterns (*)
|
||||
-S \"script\" Finds and executes the given script
|
||||
-S \"script\" Finds and executes the given script (*)
|
||||
-pr \"file\" Requires the given files/patterns in parallel (*)
|
||||
-pa \"path\" Prepends the given path to Erlang code path (*)
|
||||
-pz \"path\" Appends the given path to Erlang code path (*)
|
||||
--app \"app\" Start the given app and its dependencies (*)
|
||||
--erl \"switches\" Switches to be passed down to Erlang (*)
|
||||
--erl \"switches\" Switches to be passed down to erlang
|
||||
--name \"name\" Makes and assigns a name to the distributed node
|
||||
--sname \"name\" Makes and assigns a short name to the distributed node
|
||||
--cookie \"cookie\" Sets a cookie for this distributed node
|
||||
--hidden Makes a hidden node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
--remsh \"name\" Connects to a node using a remote shell (with iex)
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
|
||||
** Options marked with (*) can be given more than once
|
||||
** Options given after the .exs file or -- are passed down to the executed code
|
||||
** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS or --erl" >&2
|
||||
** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTS or --erl" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
readlink_f () {
|
||||
cd "$(dirname "$1")" > /dev/null
|
||||
filename="$(basename "$1")"
|
||||
local filename="$(basename "$1")"
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
@@ -35,8 +31,6 @@ readlink_f () {
|
||||
fi
|
||||
}
|
||||
|
||||
MODE="elixir"
|
||||
ERL_EXEC="erl"
|
||||
ERL=""
|
||||
I=1
|
||||
|
||||
@@ -44,25 +38,11 @@ while [ $I -le $# ]; do
|
||||
S=1
|
||||
eval "PEEK=\${$I}"
|
||||
case "$PEEK" in
|
||||
+iex)
|
||||
MODE="iex"
|
||||
;;
|
||||
+elixirc)
|
||||
MODE="elixirc"
|
||||
;;
|
||||
-v|--compile|--no-halt)
|
||||
;;
|
||||
-e|-r|-pr|-pa|-pz|--remsh|--app)
|
||||
-e|-r|-pr|-pa|-pz|--remsh|-S)
|
||||
S=2
|
||||
;;
|
||||
--detached|--hidden)
|
||||
ERL="$ERL `echo $PEEK | cut -c 2-`"
|
||||
;;
|
||||
--cookie)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL -setcookie "$VAL""
|
||||
;;
|
||||
--sname|--name)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
@@ -73,9 +53,6 @@ while [ $I -le $# ]; do
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL "$VAL""
|
||||
;;
|
||||
--werl)
|
||||
USE_WERL=true
|
||||
;;
|
||||
*)
|
||||
break
|
||||
;;
|
||||
@@ -86,24 +63,9 @@ done
|
||||
SELF=$(readlink_f "$0")
|
||||
SCRIPT_PATH=$(dirname "$SELF")
|
||||
|
||||
if [ "$OSTYPE" = "cygwin" ]; then SCRIPT_PATH=$(cygpath -m "$SCRIPT_PATH"); fi
|
||||
if [ "$MODE" != "iex" ]; then ERL="-noshell -s elixir start_cli $ERL"; fi
|
||||
|
||||
# Check for terminal support
|
||||
if [ "$OS" != "Windows_NT" ]; then
|
||||
if test -t 1 -a -t 2; then ERL="-elixir ansi_enabled true $ERL"; fi
|
||||
if [ -f "$SCRIPT_PATH/../releases/RELEASES" ] && [ -f "$SCRIPT_PATH/erl" ]
|
||||
then
|
||||
"$SCRIPT_PATH"/erl -env ERL_LIBS $ERL_LIBS:"$SCRIPT_PATH/../lib" -boot elixir -noshell $ELIXIR_ERL_OPTS $ERL -s elixir start_cli -extra "$@"
|
||||
else
|
||||
erl -env ERL_LIBS $ERL_LIBS:"$SCRIPT_PATH/../lib" -noshell $ELIXIR_ERL_OPTS $ERL -s elixir start_cli -extra "$@"
|
||||
fi
|
||||
|
||||
if [ "$OS" = "Windows_NT" ] && [ $USE_WERL ]; then
|
||||
ERL_EXEC="werl"
|
||||
fi
|
||||
|
||||
if [ -z "$ERL_PATH" ]; then
|
||||
if [ -f "$SCRIPT_PATH/../releases/RELEASES" ] && [ -f "$SCRIPT_PATH/erl" ]; then
|
||||
ERL_PATH="$SCRIPT_PATH"/"$ERL_EXEC"
|
||||
else
|
||||
ERL_PATH="$ERL_EXEC"
|
||||
fi
|
||||
fi
|
||||
|
||||
exec "$ERL_PATH" -pa "$SCRIPT_PATH"/../lib/*/ebin $ELIXIR_ERL_OPTIONS $ERL -extra "$@"
|
||||
|
||||
+15
-99
@@ -1,106 +1,22 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
setlocal
|
||||
if ""%1""=="""" goto :documentation
|
||||
if ""%1""==""--help"" goto :documentation
|
||||
if ""%1""==""-h"" goto :documentation
|
||||
if ""%1""==""/h"" goto :documentation
|
||||
goto parseopts
|
||||
|
||||
@echo off
|
||||
if "%*" == "" (
|
||||
goto documentation
|
||||
) else (
|
||||
goto run
|
||||
)
|
||||
:documentation
|
||||
echo Usage: %~nx0 [options] [.exs file] [data]
|
||||
echo.
|
||||
echo -v Prints version and exits
|
||||
echo -e command Evaluates the given command (*)
|
||||
echo -r file Requires the given files/patterns (*)
|
||||
echo -S script Finds and executes the given script
|
||||
echo -pr file Requires the given files/patterns in parallel (*)
|
||||
echo -pa path Prepends the given path to Erlang code path (*)
|
||||
echo -pz path Appends the given path to Erlang code path (*)
|
||||
echo --app app Start the given app and its dependencies (*)
|
||||
echo --erl switches Switches to be passed down to erlang (*)
|
||||
echo --name name Makes and assigns a name to the distributed node
|
||||
echo --sname name Makes and assigns a short name to the distributed node
|
||||
echo --cookie cookie Sets a cookie for this distributed node
|
||||
echo --hidden Makes a hidden node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --werl Uses Erlang's Windows shell GUI
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo -v Prints version and exit
|
||||
echo -e command Evaluates the given command (*)
|
||||
echo -r command Requires the given file/pattern (*)
|
||||
echo -pr command Requires the given file/pattern in parallel (*)
|
||||
echo -pa path Prepend the given path to Erlang code path (*)
|
||||
echo -pz path Append the given path to Erlang code path (*)
|
||||
echo --no-halt Do not halt the Erlang VM after execution
|
||||
echo.
|
||||
echo ** Options marked with (*) can be given more than once
|
||||
echo ** Options given after the .exs file or -- are passed down to the executed code
|
||||
echo ** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS or --erl
|
||||
goto end
|
||||
|
||||
:parseopts
|
||||
|
||||
rem Parameters for Erlang
|
||||
set parsErlang=
|
||||
|
||||
rem Make sure we keep a copy of all parameters
|
||||
set allPars=%*
|
||||
|
||||
rem Get the original path name from the batch file
|
||||
set originPath=%~dp0
|
||||
|
||||
rem Optional parameters before the "-extra" parameter
|
||||
set beforeExtra=
|
||||
|
||||
rem Flag which determines whether or not to use werl vs erl
|
||||
set useWerl=0
|
||||
|
||||
rem Designates which mode / Elixir component to run as
|
||||
set runMode="elixir"
|
||||
|
||||
rem Recursive loop called for each parameter that parses the cmd line parameters
|
||||
:startloop
|
||||
set par="%1"
|
||||
shift
|
||||
if "%par%"=="" (
|
||||
rem if no parameters defined
|
||||
goto :expand_erl_libs
|
||||
)
|
||||
if "%par%"=="""" (
|
||||
rem if no parameters defined - special case for parameter that is already quoted
|
||||
goto :expand_erl_libs
|
||||
)
|
||||
rem ******* EXECUTION OPTIONS **********************
|
||||
IF "%par%"==""--werl"" (Set useWerl=1)
|
||||
IF "%par%"==""+iex"" (Set runMode="iex")
|
||||
rem ******* elixir parameters **********************
|
||||
rem Note: we don't have to do anything with options that don't take an argument
|
||||
IF """"=="%par:-e=%" (shift)
|
||||
IF """"=="%par:-r=%" (shift)
|
||||
IF """"=="%par:-pr=%" (shift)
|
||||
IF """"=="%par:-pa=%" (shift)
|
||||
IF """"=="%par:-pz=%" (shift)
|
||||
IF """"=="%par:--app=%" (shift)
|
||||
IF """"=="%par:--remsh=%" (shift)
|
||||
rem ******* ERLANG PARAMETERS **********************
|
||||
IF """"=="%par:--detached=%" (Set parsErlang=%parsErlang% -detached)
|
||||
IF """"=="%par:--hidden=%" (Set parsErlang=%parsErlang% -hidden)
|
||||
IF """"=="%par:--cookie=%" (Set parsErlang=%parsErlang% -setcookie %1 && shift)
|
||||
IF """"=="%par:--sname=%" (Set parsErlang=%parsErlang% -sname %1 && shift)
|
||||
IF """"=="%par:--name=%" (Set parsErlang=%parsErlang% -name %1 && shift)
|
||||
IF """"=="%par:--erl=%" (Set beforeExtra=%beforeExtra% %~1 && shift)
|
||||
goto:startloop
|
||||
|
||||
rem ******* assume all pre-params are parsed ********************
|
||||
:expand_erl_libs
|
||||
rem ******* expand all ebin paths as Windows does not support the ..\*\ebin wildcard ********************
|
||||
SETLOCAL enabledelayedexpansion
|
||||
set ext_libs=
|
||||
for /d %%d in ("%originPath%..\lib\*.") do (
|
||||
set ext_libs=!ext_libs! -pa "%%~fd\ebin"
|
||||
)
|
||||
SETLOCAL disabledelayedexpansion
|
||||
echo ** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTS
|
||||
:run
|
||||
IF NOT %runMode% == "iex" (
|
||||
set beforeExtra=-noshell -s elixir start_cli %beforeExtra%
|
||||
)
|
||||
IF %useWerl% EQU 1 (
|
||||
start werl.exe %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
) ELSE (
|
||||
erl.exe %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
)
|
||||
:end
|
||||
endlocal
|
||||
erl -env ERL_LIBS %ERL_LIBS%;"%~dp0\..\lib" -noshell %ELIXIR_ERL_OPTS% -s elixir start_cli -extra %*
|
||||
+7
-10
@@ -1,23 +1,20 @@
|
||||
#!/bin/sh
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
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 to compiled modules
|
||||
--no-debug-info Do not attach debug info to compiled modules
|
||||
-o The directory to output compiled files
|
||||
--no-docs Do not attach documentation with compiled code
|
||||
--debug-info Attach debug info to compiled modules
|
||||
--ignore-module-conflict
|
||||
--warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
--verbose Print informational messages.
|
||||
|
||||
** Options given after -- are passed down to the executed code
|
||||
** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
** Options can be passed to the erlang compiler using ERL_COMPILER_OPTIONS" >&2
|
||||
** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTS" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
readlink_f () {
|
||||
cd "$(dirname "$1")" > /dev/null
|
||||
filename="$(basename "$1")"
|
||||
local filename="$(basename "$1")"
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
@@ -27,4 +24,4 @@ readlink_f () {
|
||||
|
||||
SELF=$(readlink_f "$0")
|
||||
SCRIPT_PATH=$(dirname "$SELF")
|
||||
exec "$SCRIPT_PATH"/elixir +elixirc "$@"
|
||||
"$SCRIPT_PATH"/elixir --compile "$@"
|
||||
|
||||
+16
-20
@@ -1,26 +1,22 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
set argc=0
|
||||
for %%A in (%*) do (
|
||||
if "%%A"=="--help" goto documentation
|
||||
if "%%A"=="-h" goto documentation
|
||||
if "%%A"=="/h" goto documentation
|
||||
set /A argc+=1
|
||||
)
|
||||
if %argc%==0 goto documentation
|
||||
goto run
|
||||
|
||||
@echo off
|
||||
if "%*" == "" (
|
||||
goto documentation
|
||||
) else (
|
||||
goto run
|
||||
)
|
||||
:documentation
|
||||
echo Usage: %~nx0 [elixir switches] [compiler switches] [.ex files]
|
||||
echo Usage: %~nx0 [switches] [.ex files]
|
||||
echo.
|
||||
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 -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 --ignore-module-conflict
|
||||
echo --warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
echo --verbose Print informational messages.
|
||||
echo.
|
||||
echo ** Options marked with (*) can be given more than once
|
||||
echo ** Options given after -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
echo ** Options can be passed to the Erlang compiler using ERL_COMPILER_OPTIONS >&2
|
||||
echo ** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTS" >&2
|
||||
:run
|
||||
call "%~dp0\elixir.bat" +elixirc %*
|
||||
call "%~dp0\elixir.bat" --compile %*
|
||||
@@ -1,35 +1,7 @@
|
||||
#!/bin/sh
|
||||
if [ $# -gt 0 ] && ([ "$1" = "--help" ] || [ "$1" = "-h" ]); then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
-v Prints version
|
||||
-e \"command\" Evaluates the given command (*)
|
||||
-r \"file\" Requires the given files/patterns (*)
|
||||
-S \"script\" Finds and executes the given script
|
||||
-pr \"file\" Requires the given files/patterns in parallel (*)
|
||||
-pa \"path\" Prepends the given path to Erlang code path (*)
|
||||
-pz \"path\" Appends the given path to Erlang code path (*)
|
||||
--app \"app\" Start the given app and its dependencies (*)
|
||||
--erl \"switches\" Switches to be passed down to Erlang (*)
|
||||
--name \"name\" Makes and assigns a name to the distributed node
|
||||
--sname \"name\" Makes and assigns a short name to the distributed node
|
||||
--cookie \"cookie\" Sets a cookie for this distributed node
|
||||
--hidden Makes a hidden node
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--remsh \"name\" Connects to a node using a remote shell
|
||||
--dot-iex \"path\" Overrides default .iex.exs file and uses path instead;
|
||||
path can be empty, then no file will be loaded
|
||||
|
||||
** Options marked with (*) can be given more than once
|
||||
** Options given after the .exs file or -- are passed down to the executed code
|
||||
** Options can be passed to the VM using ELIXIR_ERL_OPTIONS or --erl" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
readlink_f () {
|
||||
cd "$(dirname "$1")" > /dev/null
|
||||
filename="$(basename "$1")"
|
||||
local filename="$(basename "$1")"
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
@@ -39,4 +11,4 @@ readlink_f () {
|
||||
|
||||
SELF=$(readlink_f "$0")
|
||||
SCRIPT_PATH=$(dirname "$SELF")
|
||||
exec "$SCRIPT_PATH"/elixir --no-halt --erl "-user Elixir.IEx.CLI" +iex "$@"
|
||||
"$SCRIPT_PATH"/elixir --no-halt -e "IEx.cli" "$@"
|
||||
+2
-4
@@ -1,4 +1,2 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
@if defined IEX_WITH_WERL (@set __ELIXIR_IEX_FLAGS=--werl) else (set __ELIXIR_IEX_FLAGS=)
|
||||
call "%~dp0\elixir.bat" +iex --erl "-user Elixir.IEx.CLI" --no-halt %__ELIXIR_IEX_FLAGS% %*
|
||||
@set __ELIXIR_IEX_FLAGS=
|
||||
@echo off
|
||||
call "%~dp0\elixir.bat" --no-halt -e "IEx.cli" %*
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
@echo off
|
||||
call "%~dp0\elixir.bat" "%~dp0\mix" %*
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
# Store path to mix.bat as a FileInfo object
|
||||
$mixBatPath = (Get-ChildItem (((Get-ChildItem $MyInvocation.MyCommand.Path).Directory.FullName) + '\mix.bat'))
|
||||
$newArgs = @()
|
||||
|
||||
for ($i = 0; $i -lt $args.length; $i++)
|
||||
{
|
||||
if ($args[$i] -is [array])
|
||||
{
|
||||
# Commas created the array so we need to reintroduce those commas
|
||||
for ($j = 0; $j -lt $args[$i].length - 1; $j++)
|
||||
{
|
||||
$newArgs += ($args[$i][$j] + ',')
|
||||
}
|
||||
$newArgs += $args[$i][-1]
|
||||
}
|
||||
else
|
||||
{
|
||||
$newArgs += $args[$i]
|
||||
}
|
||||
}
|
||||
|
||||
# Corrected arguments are ready to pass to batch file
|
||||
& $mixBatPath $newArgs
|
||||
+65
-83
@@ -1,47 +1,31 @@
|
||||
defmodule EEx.SyntaxError do
|
||||
defexception [:message, :file, :line]
|
||||
|
||||
def message(exception) do
|
||||
"#{exception.file}:#{exception.line}: #{exception.message}"
|
||||
end
|
||||
end
|
||||
defexception EEx.SyntaxError, message: nil
|
||||
|
||||
defmodule EEx do
|
||||
@moduledoc ~S"""
|
||||
@moduledoc %B"""
|
||||
EEx stands for Embedded Elixir. It allows you to embed
|
||||
Elixir code inside a string in a robust way.
|
||||
Elixir code inside a string in a robust way:
|
||||
|
||||
iex> EEx.eval_string "foo <%= bar %>", [bar: "baz"]
|
||||
"foo baz"
|
||||
EEx.eval_string "foo <%= bar %>", [bar: "baz"]
|
||||
#=> "foo baz"
|
||||
|
||||
## API
|
||||
|
||||
This module provides 3 main APIs for you to use:
|
||||
|
||||
1. Evaluate a string (`eval_string`) or a file (`eval_file`)
|
||||
directly. This is the simplest API to use but also the
|
||||
slowest, since the code is evaluated and not compiled before.
|
||||
1) Evaluate a string (`eval_string`) or a file (`eval_file`)
|
||||
directly. This is the simplest API to use but also the
|
||||
slowest, since the code is evaluated and not compiled before;
|
||||
|
||||
2. Define a function from a string (`function_from_string`)
|
||||
or a file (`function_from_file`). This allows you to embed
|
||||
the template as a function inside a module which will then
|
||||
be compiled. This is the preferred API if you have access
|
||||
to the template at compilation time.
|
||||
2) Define a function from a string (`function_from_string`)
|
||||
or a file (`function_from_file`). This allows you to embed
|
||||
the template as a function inside a module which will then
|
||||
be compiled. This is the preferred API if you have access
|
||||
to the template at compilation time;
|
||||
|
||||
3. Compile a string (`compile_string`) or a file (`compile_file`)
|
||||
into Elixir syntax tree. This is the API used by both functions
|
||||
above and is available to you if you want to provide your own
|
||||
ways of handling the compiled template.
|
||||
|
||||
## Options
|
||||
|
||||
All functions in this module accepts EEx-related options.
|
||||
They are:
|
||||
|
||||
* `:line` - the line to be used as the template start. Defaults to 1.
|
||||
* `:file` - the file to be used in the template. Defaults to the given
|
||||
file the template is read from or to "nofile" when compiling from a string.
|
||||
* `:engine` - the EEx engine to be used for compilation.
|
||||
3) Compile a string (`compile_string`) or a file (`compile_file`)
|
||||
into Elixir syntax tree. This is the API used by both functions
|
||||
above and is available to you if you want to provide your own
|
||||
ways of handling the compiled template.
|
||||
|
||||
## Engine
|
||||
|
||||
@@ -57,14 +41,12 @@ defmodule EEx do
|
||||
|
||||
<% Elixir expression - inline with output %>
|
||||
<%= Elixir expression - replace with result %>
|
||||
<%% EEx quotation - returns the contents inside %>
|
||||
<%# Comments - they are discarded from source %>
|
||||
|
||||
All expressions that output something to the template
|
||||
**must** use the equals sign (`=`). Since everything in
|
||||
Elixir is an expression, there are no exceptions for this rule.
|
||||
For example, while some template languages would special-case
|
||||
`if` clauses, they are treated the same in EEx and
|
||||
Elixir is a macro, there are no exceptions for this rule.
|
||||
For example, while some template languages would special-
|
||||
case `if` clauses, they are treated the same in EEx and
|
||||
also require `=` in order to have their result printed:
|
||||
|
||||
<%= if true do %>
|
||||
@@ -82,12 +64,12 @@ defmodule EEx do
|
||||
An example is the `@` macro which allows easy data access
|
||||
in a template:
|
||||
|
||||
iex> EEx.eval_string "<%= @foo %>", assigns: [foo: 1]
|
||||
"1"
|
||||
EEx.eval_string "<%= @foo %>", assigns: [foo: 1]
|
||||
#=> 1
|
||||
|
||||
In other words, `<%= @foo %>` translates to:
|
||||
In other words, <%= @foo %> is simply translated to:
|
||||
|
||||
<%= {:ok, v} = Access.fetch(assigns, :foo); v %>
|
||||
<%= Keyword.get assigns, :foo %>
|
||||
|
||||
The assigns extension is useful when the number of variables
|
||||
required by the template is not specified at compilation time.
|
||||
@@ -95,36 +77,30 @@ defmodule EEx do
|
||||
|
||||
@doc """
|
||||
Generates a function definition from the string.
|
||||
|
||||
The kind (`:def` or `:defp`) must be given, the
|
||||
function name, its arguments and the compilation options.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> defmodule Sample do
|
||||
...> require EEx
|
||||
...> EEx.function_from_string :def, :sample, "<%= a + b %>", [:a, :b]
|
||||
...> end
|
||||
iex> Sample.sample(1, 2)
|
||||
"3"
|
||||
defmodule Sample do
|
||||
require EEx
|
||||
EEx.function_from_string :def, :sample, "<%= a + b %>", [:a, :b]
|
||||
end
|
||||
|
||||
Sample.sample(1, 2) #=> "3"
|
||||
|
||||
"""
|
||||
defmacro function_from_string(kind, name, source, args \\ [], options \\ []) do
|
||||
quote bind_quoted: binding do
|
||||
info = Keyword.merge [file: __ENV__.file, line: __ENV__.line], options
|
||||
args = Enum.map args, fn arg -> {arg, [line: info[:line]], nil} end
|
||||
compiled = EEx.compile_string(source, info)
|
||||
|
||||
case kind do
|
||||
:def -> def(unquote(name)(unquote_splicing(args)), do: unquote(compiled))
|
||||
:defp -> defp(unquote(name)(unquote_splicing(args)), do: unquote(compiled))
|
||||
end
|
||||
defmacro function_from_string(kind, name, source, args // [], options // []) do
|
||||
info = [file: __CALLER__.file, line: __CALLER__.line + 1]
|
||||
quote do
|
||||
info = Keyword.merge unquote(info), unquote(options)
|
||||
EEx.function_from_quoted(__MODULE__, unquote(kind), unquote(name),
|
||||
unquote(args), EEx.compile_string(unquote(source), info), info)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Generates a function definition from the file contents.
|
||||
|
||||
The kind (`:def` or `:defp`) must be given, the
|
||||
function name, its arguments and the compilation options.
|
||||
|
||||
@@ -146,65 +122,62 @@ defmodule EEx do
|
||||
Sample.sample(1, 2) #=> "3"
|
||||
|
||||
"""
|
||||
defmacro function_from_file(kind, name, file, args \\ [], options \\ []) do
|
||||
quote bind_quoted: binding do
|
||||
info = Keyword.merge options, [file: file, line: 1]
|
||||
args = Enum.map args, fn arg -> {arg, [line: 1], nil} end
|
||||
compiled = EEx.compile_file(file, info)
|
||||
defmacro function_from_file(kind, name, filename, args // [], options // []) do
|
||||
quote do
|
||||
file = unquote(filename)
|
||||
info = Keyword.merge unquote(options), [file: file, line: 1]
|
||||
|
||||
@external_resource file
|
||||
@file file
|
||||
case kind do
|
||||
:def -> def(unquote(name)(unquote_splicing(args)), do: unquote(compiled))
|
||||
:defp -> defp(unquote(name)(unquote_splicing(args)), do: unquote(compiled))
|
||||
end
|
||||
EEx.function_from_quoted(__MODULE__, unquote(kind), unquote(name),
|
||||
unquote(args), EEx.compile_file(file, info), info)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a string `source` and generate a quoted expression
|
||||
Get a string `source` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
"""
|
||||
def compile_string(source, options \\ []) do
|
||||
def compile_string(source, options // []) do
|
||||
EEx.Compiler.compile(source, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a `filename` and generate a quoted expression
|
||||
Get a `filename` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
"""
|
||||
def compile_file(filename, options \\ []) do
|
||||
def compile_file(filename, options // []) do
|
||||
options = Keyword.merge options, [file: filename, line: 1]
|
||||
compile_string(File.read!(filename), options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a string `source` and evaluate the values using the `bindings`.
|
||||
Get a string `source` and evaluate the values using the `bindings`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> EEx.eval_string "foo <%= bar %>", [bar: "baz"]
|
||||
"foo baz"
|
||||
EEx.eval_string "foo <%= bar %>", [bar: "baz"]
|
||||
#=> "foo baz"
|
||||
|
||||
"""
|
||||
def eval_string(source, bindings \\ [], options \\ []) do
|
||||
def eval_string(source, bindings // [], options // []) do
|
||||
compiled = compile_string(source, options)
|
||||
do_eval(compiled, bindings, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a `filename` and evaluate the values using the `bindings`.
|
||||
Get a `filename` and evaluate the values using the `bindings`.
|
||||
|
||||
## Examples
|
||||
|
||||
# sample.eex
|
||||
# sample.ex
|
||||
foo <%= bar %>
|
||||
|
||||
# iex
|
||||
EEx.eval_file "sample.eex", [bar: "baz"] #=> "foo baz"
|
||||
EEx.eval_file "sample.ex", [bar: "baz"]
|
||||
#=> "foo baz"
|
||||
|
||||
"""
|
||||
def eval_file(filename, bindings \\ [], options \\ []) do
|
||||
def eval_file(filename, bindings // [], options // []) do
|
||||
options = Keyword.put options, :file, filename
|
||||
compiled = compile_file(filename, options)
|
||||
do_eval(compiled, bindings, options)
|
||||
@@ -212,8 +185,17 @@ defmodule EEx do
|
||||
|
||||
### Helpers
|
||||
|
||||
@doc false
|
||||
def function_from_quoted(module, kind, name, args, source, info) do
|
||||
args = Enum.map args, fn arg -> { arg, 0, nil } end
|
||||
quote = quote do
|
||||
unquote(kind).(unquote(name).(unquote_splicing(args)), do: unquote(source))
|
||||
end
|
||||
Module.eval_quoted module, quote, [], info
|
||||
end
|
||||
|
||||
defp do_eval(compiled, bindings, options) do
|
||||
{result, _} = Code.eval_quoted(compiled, bindings, options)
|
||||
{ result, _ } = Code.eval_quoted(compiled, bindings, options)
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
+60
-65
@@ -1,122 +1,117 @@
|
||||
defrecord EEx.State, engine: EEx.SmartEngine, dict: [], file: 'nofile', line: 1, start_line: 1
|
||||
|
||||
defmodule EEx.Compiler do
|
||||
@moduledoc false
|
||||
|
||||
# When changing this setting, don't forget to update the docs for EEx
|
||||
@default_engine EEx.SmartEngine
|
||||
|
||||
@doc """
|
||||
This is the compilation entry point. It glues the tokenizer
|
||||
and the engine together by handling the tokens and invoking
|
||||
the engine every time a full expression or text is received.
|
||||
"""
|
||||
def compile(source, opts) do
|
||||
file = opts[:file] || "nofile"
|
||||
line = opts[:line] || 1
|
||||
trim = opts[:trim] || false
|
||||
case EEx.Tokenizer.tokenize(source, line, trim: trim) do
|
||||
{:ok, tokens} ->
|
||||
state = %{engine: opts[:engine] || @default_engine,
|
||||
file: file, line: line, quoted: [], start_line: nil}
|
||||
generate_buffer(tokens, "", [], state)
|
||||
{:error, line, message} ->
|
||||
raise EEx.SyntaxError, line: line, file: file, message: message
|
||||
end
|
||||
def compile(source, options) do
|
||||
line = Keyword.get(options, :line, 1)
|
||||
tokens = EEx.Tokenizer.tokenize(source, line)
|
||||
state = EEx.State.new(options)
|
||||
generate_buffer(tokens, "", [], state)
|
||||
end
|
||||
|
||||
# Generates the buffers by handling each expression from the tokenizer
|
||||
|
||||
defp generate_buffer([{:text, chars}|t], buffer, scope, state) do
|
||||
buffer = state.engine.handle_text(buffer, IO.chardata_to_string(chars))
|
||||
defp generate_buffer([{ :text, _line, chars }|t], buffer, scope, state) do
|
||||
buffer = state.engine.handle_text(buffer, chars)
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:expr, line, mark, chars}|t], buffer, scope, state) do
|
||||
expr = Code.string_to_quoted!(chars, [line: line, file: state.file])
|
||||
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), expr)
|
||||
defp generate_buffer([{ :expr, line, mark, chars }|t], buffer, scope, state) do
|
||||
expr = maybe_block Erlang.elixir_translator.forms(chars, line, state.file)
|
||||
buffer = state.engine.handle_expr(buffer, mark, expr)
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:start_expr, start_line, mark, chars}|t], buffer, scope, state) do
|
||||
{contents, line, t} = look_ahead_text(t, start_line, chars)
|
||||
{contents, t} = generate_buffer(t, "", [contents|scope],
|
||||
%{state | quoted: [], line: line, start_line: start_line})
|
||||
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), contents)
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
defp generate_buffer([{ :start_expr, line, mark, chars }|t], buffer, scope, state) do
|
||||
{ contents, t } = generate_buffer(t, "", [chars|scope], state.dict([]).line(line).start_line(line))
|
||||
buffer = state.engine.handle_expr(buffer, mark, contents)
|
||||
generate_buffer(t, buffer, scope, state.dict([]))
|
||||
end
|
||||
|
||||
defp generate_buffer([{:middle_expr, line, _, chars}|t], buffer, [current|scope], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
generate_buffer(t, "", [wrapped|scope], %{state | line: line})
|
||||
defp generate_buffer([{ :middle_expr, line, _, chars }|t], buffer, [current|scope], state) do
|
||||
{ wrapped, state } = wrap_expr(current, line, buffer, chars, state)
|
||||
generate_buffer(t, "", [wrapped|scope], state.line(line))
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, _, chars}|t], buffer, [current|_], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
tuples = Code.string_to_quoted!(wrapped, [line: state.start_line, file: state.file])
|
||||
buffer = insert_quoted(tuples, state.quoted)
|
||||
{buffer, t}
|
||||
defp generate_buffer([{ :end_expr, line, _, chars }|t], buffer, [current|_], state) do
|
||||
{ wrapped, state } = wrap_expr(current, line, buffer, chars, state)
|
||||
tuples = maybe_block Erlang.elixir_translator.forms(wrapped, state.start_line, state.file)
|
||||
buffer = insert_quotes(tuples, state.dict)
|
||||
{ buffer, t }
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, _, chars}|_], _buffer, [], state) do
|
||||
raise EEx.SyntaxError, message: "unexpected token #{inspect chars}", file: state.file, line: line
|
||||
defp generate_buffer([{ :end_expr, line, _, chars }|_], _buffer, [], _state) do
|
||||
raise EEx.SyntaxError, message: "unexpected token: #{inspect chars} at line #{inspect line}"
|
||||
end
|
||||
|
||||
defp generate_buffer([], buffer, [], state) do
|
||||
state.engine.handle_body(buffer)
|
||||
defp generate_buffer([], buffer, [], _state) do
|
||||
buffer
|
||||
end
|
||||
|
||||
defp generate_buffer([], _buffer, _scope, state) do
|
||||
raise EEx.SyntaxError, message: "unexpected end of string, expected a closing '<% end %>'",
|
||||
file: state.file, line: state.line
|
||||
defp generate_buffer([], _buffer, _scope, _state) do
|
||||
raise EEx.SyntaxError, message: "unexpected end of string. expecting a closing <% end %>."
|
||||
end
|
||||
|
||||
# Creates a placeholder and wrap it inside the expression block
|
||||
|
||||
defp wrap_expr(current, line, buffer, chars, state) do
|
||||
new_lines = List.duplicate(?\n, line - state.line)
|
||||
key = length(state.quoted)
|
||||
placeholder = '__EEX__(' ++ Integer.to_char_list(key) ++ ');'
|
||||
{current ++ placeholder ++ new_lines ++ chars,
|
||||
%{state | quoted: [{key, buffer}|state.quoted]}}
|
||||
end
|
||||
|
||||
# Look text ahead on expressions
|
||||
|
||||
defp look_ahead_text([{:text, text}, {:middle_expr, line, _, chars}|t]=list, start, contents) do
|
||||
if only_spaces?(text) do
|
||||
{contents ++ text ++ chars, line, t}
|
||||
if state.dict == [] and is_empty?(buffer) do
|
||||
{ current ++ new_lines ++ chars, state }
|
||||
else
|
||||
{contents, start, list}
|
||||
key = length(state.dict)
|
||||
placeholder = '__EEX__(' ++ integer_to_list(key) ++ ');'
|
||||
{ current ++ placeholder ++ new_lines ++ chars, state.prepend_dict([{key, buffer}]) }
|
||||
end
|
||||
end
|
||||
|
||||
defp look_ahead_text(t, start, contents) do
|
||||
{contents, start, t}
|
||||
# Check if the syntax node represents an empty string
|
||||
|
||||
defp is_empty?(bin) when is_binary(bin) do
|
||||
bc(<<c>> inbits bin, not c in [?\s,?\t,?\r,?\n], do: <<c>>) == ""
|
||||
end
|
||||
|
||||
defp only_spaces?(chars) do
|
||||
Enum.all?(chars, &(&1 in [?\s, ?\t, ?\r, ?\n]))
|
||||
defp is_empty?({ :<>, _, [left, right] }) do
|
||||
is_empty?(left) and is_empty?(right)
|
||||
end
|
||||
|
||||
defp is_empty?(_) do
|
||||
false
|
||||
end
|
||||
|
||||
# Block wrapping
|
||||
|
||||
defp maybe_block([]), do: nil
|
||||
defp maybe_block([h]), do: h
|
||||
defp maybe_block(other), do: { :__block__, 0, other }
|
||||
|
||||
# Changes placeholder to real expression
|
||||
|
||||
defp insert_quoted({:__EEX__, _, [key]}, quoted) do
|
||||
{^key, value} = List.keyfind quoted, key, 0
|
||||
defp insert_quotes({ :__EEX__, _, [key] }, dict) do
|
||||
{ ^key, value } = List.keyfind dict, key, 1
|
||||
value
|
||||
end
|
||||
|
||||
defp insert_quoted({left, line, right}, quoted) do
|
||||
{insert_quoted(left, quoted), line, insert_quoted(right, quoted)}
|
||||
defp insert_quotes({ left, line, right }, dict) do
|
||||
{ insert_quotes(left, dict), line, insert_quotes(right, dict) }
|
||||
end
|
||||
|
||||
defp insert_quoted({left, right}, quoted) do
|
||||
{insert_quoted(left, quoted), insert_quoted(right, quoted)}
|
||||
defp insert_quotes({ left, right }, dict) do
|
||||
{ insert_quotes(left, dict), insert_quotes(right, dict) }
|
||||
end
|
||||
|
||||
defp insert_quoted(list, quoted) when is_list(list) do
|
||||
Enum.map list, &insert_quoted(&1, quoted)
|
||||
defp insert_quotes(list, dict) when is_list(list) do
|
||||
Enum.map list, insert_quotes(&1, dict)
|
||||
end
|
||||
|
||||
defp insert_quoted(other, _quoted) do
|
||||
defp insert_quotes(other, _dict) do
|
||||
other
|
||||
end
|
||||
end
|
||||
|
||||
+26
-98
@@ -1,101 +1,25 @@
|
||||
defmodule EEx.Engine do
|
||||
@moduledoc ~S"""
|
||||
Basic EEx engine that ships with Elixir.
|
||||
@moduledoc %B"""
|
||||
This is the basic EEx engine that ships with Elixir.
|
||||
An engine needs to implement two functions:
|
||||
|
||||
An engine needs to implement three functions:
|
||||
* `handle_text(buffer, text)` - it receives the buffer,
|
||||
the text and must return a new quoted expression;
|
||||
|
||||
* `handle_body(quoted)` - receives the final built quoted
|
||||
expression, should do final post-processing and return a
|
||||
quoted expression.
|
||||
* `handle_expr(buffer, marker, expr)` - it receives the buffer,
|
||||
the marker, the expr and must return a new quoted expression;
|
||||
|
||||
* `handle_text(buffer, text)` - it receives the buffer,
|
||||
the text and must return a new quoted expression.
|
||||
The marker is what follows exactly after `<%`. For example,
|
||||
`<% foo %>` has an empty marker, but `<%= foo %>` has `'='`
|
||||
as marker. The allowed markers so far are:
|
||||
|
||||
* `handle_expr(buffer, marker, expr)` - it receives the buffer,
|
||||
the marker, the expr and must return a new quoted expression.
|
||||
* `''`
|
||||
* `'='`
|
||||
|
||||
The marker is what follows exactly after `<%`. For example,
|
||||
`<% foo %>` has an empty marker, but `<%= foo %>` has `"="`
|
||||
as marker. The allowed markers so far are: `""` and `"="`.
|
||||
|
||||
Read `handle_expr/3` below for more information about the markers
|
||||
implemented by default by this engine.
|
||||
|
||||
`EEx.Engine` can be used directly if one desires to use the
|
||||
default implementations for the functions above.
|
||||
Read `handle_expr/3` below for more information about the markers
|
||||
implemented by default by this engine.
|
||||
"""
|
||||
|
||||
@callback handle_body(Macro.t) :: Macro.t
|
||||
@callback handle_text(Macro.t, String.t) :: Macro.t
|
||||
@callback handle_expr(Macro.t, String.t, Macro.t) :: Macro.t
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
@behaviour EEx.Engine
|
||||
|
||||
def handle_body(body) do
|
||||
EEx.Engine.handle_body(body)
|
||||
end
|
||||
|
||||
def handle_text(buffer, text) do
|
||||
EEx.Engine.handle_text(buffer, text)
|
||||
end
|
||||
|
||||
def handle_expr(buffer, marker, expr) do
|
||||
EEx.Engine.handle_expr(buffer, marker, expr)
|
||||
end
|
||||
|
||||
defoverridable [handle_body: 1, handle_expr: 3, handle_text: 2]
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Handles assigns in quoted expressions.
|
||||
|
||||
A warning will be printed on missing assigns.
|
||||
Future versions will raise.
|
||||
|
||||
This can be added to any custom engine by invoking
|
||||
`handle_assign/1` with `Macro.prewalk/2`:
|
||||
|
||||
def handle_expr(buffer, token, expr) do
|
||||
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
|
||||
EEx.Engine.handle_expr(buffer, token, expr)
|
||||
end
|
||||
|
||||
"""
|
||||
def handle_assign({:@, meta, [{name, _, atom}]}) when is_atom(name) and is_atom(atom) do
|
||||
line = meta[:line] || 0
|
||||
quote line: line, do: EEx.Engine.fetch_assign!(var!(assigns), unquote(name))
|
||||
end
|
||||
def handle_assign(arg) do
|
||||
arg
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: raise on 1.3 or 1.4
|
||||
def fetch_assign!(assigns, key) do
|
||||
case Access.fetch(assigns, key) do
|
||||
{:ok, val} ->
|
||||
val
|
||||
:error ->
|
||||
keys = Enum.map(assigns, &elem(&1, 0))
|
||||
IO.write :stderr, "warning: assign @#{key} not available in eex template. " <>
|
||||
"Please ensure all assigns are given as options. " <>
|
||||
"Available assigns: #{inspect keys}\n" <>
|
||||
Exception.format_stacktrace
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
The default implementation simply returns the given expression.
|
||||
"""
|
||||
def handle_body(quoted) do
|
||||
quoted
|
||||
end
|
||||
|
||||
@doc """
|
||||
The default implementation simply concatenates text to the buffer.
|
||||
"""
|
||||
@@ -111,19 +35,23 @@ defmodule EEx.Engine do
|
||||
|
||||
All other markers are not implemented by this engine.
|
||||
"""
|
||||
@spec handle_expr(Macro.t, String.t, Macro.t) :: Macro.t
|
||||
def handle_expr(buffer, "=", expr) do
|
||||
def handle_expr(buffer, '=', expr) do
|
||||
quote do
|
||||
tmp1 = unquote(buffer)
|
||||
tmp1 <> String.Chars.to_string(unquote(expr))
|
||||
tmp_1 = unquote(buffer)
|
||||
tmp_2 = to_binary(unquote(expr))
|
||||
tmp_1 <> tmp_2
|
||||
end
|
||||
end
|
||||
|
||||
def handle_expr(buffer, "", expr) do
|
||||
def handle_expr(buffer, '', expr) do
|
||||
quote do
|
||||
tmp2 = unquote(buffer)
|
||||
tmp = unquote(buffer)
|
||||
unquote(expr)
|
||||
tmp2
|
||||
tmp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def behaviour_info(:callbacks) do
|
||||
[handle_text: 2, handle_expr: 3]
|
||||
end
|
||||
end
|
||||
@@ -1,41 +1,97 @@
|
||||
defmodule EEx.SmartEngine do
|
||||
defmodule EEx.TransformerEngine do
|
||||
@moduledoc """
|
||||
The default engine used by EEx.
|
||||
An abstract engine that is meant to be used and
|
||||
built upon in other modules. This engine implements
|
||||
the `EEx.Engine` behavior and provides a `transform`
|
||||
overridable directive that allows a developer to
|
||||
customize the expression returned by the engine.
|
||||
|
||||
It includes assigns (like `@foo`) and possibly other
|
||||
conveniences in the future.
|
||||
Check `EEx.AssignsEngine` and `EEx.SmartEngine` for
|
||||
examples of using this module.
|
||||
"""
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
@behavior EEx.Engine
|
||||
|
||||
def handle_text(buffer, text) do
|
||||
EEx.Engine.handle_text(buffer, text)
|
||||
end
|
||||
|
||||
def handle_expr(buffer, mark, expr) do
|
||||
EEx.Engine.handle_expr(buffer, mark, transform(expr))
|
||||
end
|
||||
|
||||
defp transform({ a, b, c }) do
|
||||
{ transform(a), b, transform(c) }
|
||||
end
|
||||
|
||||
defp transform({ a, b }) do
|
||||
{ transform(a), transform(b) }
|
||||
end
|
||||
|
||||
defp transform(list) when is_list(list) do
|
||||
lc i inlist list, do: transform(i)
|
||||
end
|
||||
|
||||
defp transform(other) do
|
||||
other
|
||||
end
|
||||
|
||||
defoverridable [transform: 1, handle_expr: 3, handle_text: 2]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule EEx.AssignsEngine do
|
||||
@moduledoc """
|
||||
An abstract engine that, when used with the
|
||||
`TransformerEngine`, allows a developer to access
|
||||
assigns using `@` as syntax.
|
||||
|
||||
This engine is included by default on the SmartEngine.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> EEx.eval_string("<%= @foo %>", assigns: [foo: 1])
|
||||
"1"
|
||||
|
||||
In the example above, we can access the value `foo` under
|
||||
the binding `assigns` using `@foo`. This is useful because
|
||||
a template, after being compiled, can receive different
|
||||
assigns and would not require recompilation for each
|
||||
variable set.
|
||||
|
||||
Assigns can also be used when compiled to a function:
|
||||
|
||||
# sample.eex
|
||||
<%= @a + @b %>
|
||||
|
||||
# sample.ex
|
||||
defmodule Sample do
|
||||
require EEx
|
||||
EEx.function_from_file :def, :sample, "sample.eex", [:assigns]
|
||||
defmodule MyEngine do
|
||||
use EEx.TransformerEngine
|
||||
use EEx.AssignsEngine
|
||||
end
|
||||
|
||||
# iex
|
||||
Sample.sample(a: 1, b: 2) #=> "3"
|
||||
EEx.eval_string("<%= @foo %>", assigns: [foo: 1])
|
||||
#=> 1
|
||||
|
||||
In the example above, we can access the value `foo` under
|
||||
the binding `assigns` using `@foo`. This is useful when
|
||||
a template, after compiled, may receive different assigns
|
||||
and the developer don't want to recompile it for each
|
||||
variable set.
|
||||
"""
|
||||
|
||||
use EEx.Engine
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote unquote: false do
|
||||
defp transform({ :@, line, [{ name, _, atom }] }) when is_atom(name) and is_atom(atom) do
|
||||
quote(do: Keyword.get var!(assigns), unquote(name))
|
||||
end
|
||||
|
||||
def handle_expr(buffer, mark, expr) do
|
||||
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
|
||||
super(buffer, mark, expr)
|
||||
defp transform(_) do
|
||||
super
|
||||
end
|
||||
|
||||
defoverridable [transform: 1]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule EEx.SmartEngine do
|
||||
use EEx.TransformerEngine
|
||||
use EEx.AssignsEngine
|
||||
|
||||
@moduledoc """
|
||||
An engine meant for end-user usage that includes
|
||||
`AssignsEngine` and other conveniences. Read
|
||||
`EEx.AssignsEngine` for examples.
|
||||
"""
|
||||
end
|
||||
+61
-125
@@ -3,102 +3,82 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
@doc """
|
||||
Tokenizes the given char list or binary.
|
||||
It returns 4 different types of tokens as result:
|
||||
|
||||
It returns {:ok, list} with the following tokens:
|
||||
* { :text, line, contents }
|
||||
* { :expr, line, marker, contents }
|
||||
* { :start_expr, line, marker, contents }
|
||||
* { :end_expr, line, marker, contents }
|
||||
|
||||
* `{:text, contents}`
|
||||
* `{:expr, line, marker, contents}`
|
||||
* `{:start_expr, line, marker, contents}`
|
||||
* `{:middle_expr, line, marker, contents}`
|
||||
* `{:end_expr, line, marker, contents}`
|
||||
|
||||
Or `{:error, line, error}` in case of errors.
|
||||
"""
|
||||
def tokenize(bin, line, opts \\ [])
|
||||
|
||||
def tokenize(bin, line, opts) when is_binary(bin) do
|
||||
tokenize(String.to_char_list(bin), line, opts)
|
||||
def tokenize(bin, line) when is_binary(bin) do
|
||||
tokenize(binary_to_list(bin), line)
|
||||
end
|
||||
|
||||
def tokenize(list, line, opts) do
|
||||
tokenize(list, line, opts, [], [])
|
||||
def tokenize(list, line) do
|
||||
List.reverse(tokenize(list, line, line, [], []))
|
||||
end
|
||||
|
||||
defp tokenize('<%%' ++ t, line, opts, buffer, acc) do
|
||||
tokenize t, line, opts, [?%, ?<|buffer], acc
|
||||
defp tokenize([?<,?%|t], current_line, line, buffer, acc) do
|
||||
{ marker, t } = retrieve_marker(t)
|
||||
{ expr, new_line, rest } = tokenize_expr t, line, []
|
||||
|
||||
token = token_name(expr)
|
||||
acc = tokenize_text(current_line, buffer, acc)
|
||||
final = { token, line, marker, List.reverse(expr) }
|
||||
tokenize rest, new_line, new_line, [], [final | acc]
|
||||
end
|
||||
|
||||
defp tokenize('<%#' ++ t, line, opts, buffer, acc) do
|
||||
case expr(t, line, []) do
|
||||
{:error, _, _} = error -> error
|
||||
{:ok, _, new_line, rest} ->
|
||||
{rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
tokenize rest, new_line, opts, buffer, acc
|
||||
end
|
||||
defp tokenize('\n' ++ t, current_line, line, buffer, acc) do
|
||||
tokenize t, current_line, line + 1, [?\n|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize('<%' ++ t, line, opts, buffer, acc) do
|
||||
{marker, t} = retrieve_marker(t)
|
||||
|
||||
case expr(t, line, []) do
|
||||
{:error, _, _} = error -> error
|
||||
{:ok, expr, new_line, rest} ->
|
||||
token = token_name(expr)
|
||||
{rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
acc = tokenize_text(buffer, acc)
|
||||
final = {token, line, marker, Enum.reverse(expr)}
|
||||
tokenize rest, new_line, opts, [], [final | acc]
|
||||
end
|
||||
defp tokenize([h|t], current_line, line, buffer, acc) do
|
||||
tokenize t, current_line, line, [h|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize('\n' ++ t, line, opts, buffer, acc) do
|
||||
tokenize t, line + 1, opts, [?\n|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize([h|t], line, opts, buffer, acc) do
|
||||
tokenize t, line, opts, [h|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize([], _line, _opts, buffer, acc) do
|
||||
{:ok, Enum.reverse(tokenize_text(buffer, acc))}
|
||||
defp tokenize([], current_line, _line, buffer, acc) do
|
||||
tokenize_text(current_line, buffer, acc)
|
||||
end
|
||||
|
||||
# Retrieve marker for <%
|
||||
|
||||
defp retrieve_marker('=' ++ t) do
|
||||
{'=', t}
|
||||
{ '=', t }
|
||||
end
|
||||
|
||||
defp retrieve_marker(t) do
|
||||
{'', t}
|
||||
{ '', t }
|
||||
end
|
||||
|
||||
# Tokenize an expression until we find %>
|
||||
|
||||
defp expr([?%, ?>|t], line, buffer) do
|
||||
{:ok, buffer, line, t}
|
||||
defp tokenize_expr([?%,?>|t], line, buffer) do
|
||||
{ buffer, line, t }
|
||||
end
|
||||
|
||||
defp expr('\n' ++ t, line, buffer) do
|
||||
expr t, line + 1, [?\n|buffer]
|
||||
defp tokenize_expr('\n' ++ t, line, buffer) do
|
||||
tokenize_expr t, line + 1, [?\n|buffer]
|
||||
end
|
||||
|
||||
defp expr([h|t], line, buffer) do
|
||||
expr t, line, [h|buffer]
|
||||
defp tokenize_expr([h|t], line, buffer) do
|
||||
tokenize_expr t, line, [h|buffer]
|
||||
end
|
||||
|
||||
defp expr([], line, _buffer) do
|
||||
{:error, line, "missing token '%>'"}
|
||||
# Raise an error if the expected token is not found
|
||||
|
||||
defp tokenize_expr([], _line, _buffer) do
|
||||
raise EEx.SyntaxError, message: "missing token: %>"
|
||||
end
|
||||
|
||||
# Receive an expression content and check
|
||||
# if it is a start, middle or an end token.
|
||||
#
|
||||
# Start tokens finish with "do" and "fn ->"
|
||||
# Middle tokens are marked with "->" or keywords
|
||||
# End tokens contain only the end word and optionally ")"
|
||||
# Start tokens finish with `do` and `fn ->`
|
||||
# Middle tokens are marked with `->` or keywords
|
||||
# End tokens contain only the end word
|
||||
|
||||
defp token_name([h|t]) when h in [?\s, ?\t, ?)] do
|
||||
defp token_name([h|t]) when h in [?\s, ?\t] do
|
||||
token_name(t)
|
||||
end
|
||||
|
||||
@@ -107,16 +87,16 @@ defmodule EEx.Tokenizer do
|
||||
end
|
||||
|
||||
defp token_name('>-' ++ rest) do
|
||||
rest = Enum.reverse(rest)
|
||||
rest = List.reverse(rest)
|
||||
|
||||
# Tokenize the remaining passing check_terminators as
|
||||
# false, which relax the tokenizer to not error on
|
||||
# unmatched pairs. Then, we check if there is a "fn"
|
||||
# token and, if so, it is not followed by an "end"
|
||||
# token. If this is the case, we are on a start expr.
|
||||
case :elixir_tokenizer.tokenize(rest, 1, file: "eex", check_terminators: false) do
|
||||
{:ok, _line, _column, tokens} ->
|
||||
tokens = Enum.reverse(tokens)
|
||||
# Tokenize the remaining passing "__internal__" as file,
|
||||
# which relax the tokenizer to not error on unmatched
|
||||
# pairs. Then, we check if there is a "fn" token and,
|
||||
# if so, it is not followed by an "end" token. If this
|
||||
# is the case, we are on a start expr.
|
||||
case :elixir_tokenizer.tokenize(rest, 1, "__internal__") do
|
||||
{ :ok, tokens } ->
|
||||
tokens = List.reverse(tokens)
|
||||
fn_index = fn_index(tokens)
|
||||
|
||||
if fn_index && end_index(tokens) > fn_index do
|
||||
@@ -124,7 +104,7 @@ defmodule EEx.Tokenizer do
|
||||
else
|
||||
:middle_expr
|
||||
end
|
||||
_error ->
|
||||
error ->
|
||||
:middle_expr
|
||||
end
|
||||
end
|
||||
@@ -140,76 +120,32 @@ defmodule EEx.Tokenizer do
|
||||
end
|
||||
|
||||
defp fn_index(tokens) do
|
||||
Enum.find_index tokens, fn
|
||||
{:fn_paren, _} -> true
|
||||
{:fn, _} -> true
|
||||
Enum.find_index(tokens, function do
|
||||
{ :fn_paren, _ } -> true
|
||||
{ :fn, _ } -> true
|
||||
_ -> false
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp end_index(tokens) do
|
||||
Enum.find_index(tokens, &match?({:end, _}, &1)) || :infinity
|
||||
Enum.find_index(tokens, match?({ :end, _ }, &1)) || :infinity
|
||||
end
|
||||
|
||||
defp check_spaces(string, token) do
|
||||
if Enum.all?(string, &(&1 in [?\s, ?\t])) do
|
||||
token
|
||||
else
|
||||
:expr
|
||||
end
|
||||
if only_spaces?(string), do: token, else: :expr
|
||||
end
|
||||
|
||||
defp only_spaces?([h|t]) when h in [?\s, ?\t], do: only_spaces?(t)
|
||||
defp only_spaces?(other), do: other == []
|
||||
|
||||
# Tokenize the buffered text by appending
|
||||
# it to the given accumulator.
|
||||
|
||||
defp tokenize_text([], acc) do
|
||||
defp tokenize_text(_line, [], acc) do
|
||||
acc
|
||||
end
|
||||
|
||||
defp tokenize_text(buffer, acc) do
|
||||
[{:text, Enum.reverse(buffer)} | acc]
|
||||
end
|
||||
|
||||
# If trim mode is enabled and the token is on a line with
|
||||
# only itself and whitespace, trim the whitespace around it,
|
||||
# including the line break following it if there is one.
|
||||
|
||||
defp trim_if_needed(rest, line, opts, buffer, acc) do
|
||||
original = {rest, line, buffer}
|
||||
if opts[:trim] do
|
||||
case {trim_left(buffer, acc), trim_right(rest, line)} do
|
||||
{{true, new_buffer}, {true, new_rest, new_line}} ->
|
||||
{new_rest, new_line, new_buffer}
|
||||
_ ->
|
||||
original
|
||||
end
|
||||
else
|
||||
original
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_left(buffer, acc) do
|
||||
case {trim_whitespace(buffer), acc} do
|
||||
{[?\n|_] = trimmed_buffer, _} -> {true, trimmed_buffer}
|
||||
{[], []} -> {true, []}
|
||||
_ -> {false, buffer}
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_right(rest, line) do
|
||||
case trim_whitespace(rest) do
|
||||
[?\r, ?\n|trimmed_rest] -> {true, trimmed_rest, line + 1}
|
||||
[?\n|trimmed_rest] -> {true, trimmed_rest, line + 1}
|
||||
[] -> {true, [], line}
|
||||
_ -> {false, rest, line}
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_whitespace([h|t]) when h == ?\s or h == ?\t do
|
||||
trim_whitespace(t)
|
||||
end
|
||||
|
||||
defp trim_whitespace(list) do
|
||||
list
|
||||
defp tokenize_text(line, buffer, acc) do
|
||||
[{ :text, line, list_to_binary(List.reverse(buffer)) } | acc]
|
||||
end
|
||||
end
|
||||
|
||||
+2
-4
@@ -2,8 +2,6 @@ defmodule EEx.Mixfile do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[app: :eex,
|
||||
version: System.version,
|
||||
build_per_environment: false]
|
||||
[app: :eex, version: System.version]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
Code.require_file "../test_helper.exs", __DIR__
|
||||
Code.require_file "../../test_helper", __FILE__
|
||||
|
||||
defmodule EEx.SmartEngineTest do
|
||||
use ExUnit.Case, async: true
|
||||
@@ -7,30 +7,16 @@ defmodule EEx.SmartEngineTest do
|
||||
assert_eval "foo bar", "foo bar"
|
||||
end
|
||||
|
||||
test "evaluates with assigns as keywords" do
|
||||
test "evaluates with assigns" do
|
||||
assert_eval "1", "<%= @foo %>", assigns: [foo: 1]
|
||||
end
|
||||
|
||||
test "evaluates with assigns as a map" do
|
||||
assert_eval "1", "<%= @foo %>", assigns: %{foo: 1}
|
||||
end
|
||||
|
||||
test "evaluates with loops" do
|
||||
assert_eval "1\n2\n3\n", "<%= for x <- [1, 2, 3] do %><%= x %>\n<% end %>"
|
||||
assert_eval "1\n2\n3\n", "<%= lc x inlist [1,2,3] do %><%= x %>\n<% end %>"
|
||||
end
|
||||
|
||||
test "compiled preserved line numbers" do
|
||||
result = EEx.compile_string("<%= @hello %>", engine: EEx.SmartEngine)
|
||||
Macro.prewalk(result, fn
|
||||
{_left, meta, _right} ->
|
||||
assert Keyword.get(meta, :line, 0) in [0, 1]
|
||||
_ ->
|
||||
:ok
|
||||
end)
|
||||
end
|
||||
|
||||
defp assert_eval(expected, actual, binding \\ []) do
|
||||
result = EEx.eval_string(actual, binding, file: __ENV__.file)
|
||||
defp assert_eval(expected, actual, binding // []) do
|
||||
result = EEx.eval_string(actual, binding, file: __FILE__)
|
||||
assert result == expected
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,30 +1,27 @@
|
||||
Code.require_file "../test_helper.exs", __DIR__
|
||||
Code.require_file "../../test_helper", __FILE__
|
||||
|
||||
defmodule EEx.TokenizerTest do
|
||||
use ExUnit.Case, async: true
|
||||
require EEx.Tokenizer, as: T
|
||||
|
||||
test "simple chars lists" do
|
||||
assert T.tokenize('foo', 1) == {:ok, [{:text, 'foo'}]}
|
||||
assert T.tokenize('foo', 1) == [ { :text, 1, "foo" } ]
|
||||
end
|
||||
|
||||
test "simple strings" do
|
||||
assert T.tokenize("foo", 1) == {:ok, [{:text, 'foo'}]}
|
||||
assert T.tokenize("foo", 1) == [ { :text, 1, "foo" } ]
|
||||
end
|
||||
|
||||
test "strings with embedded code" do
|
||||
assert T.tokenize('foo <% bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '', ' bar '}]}
|
||||
assert T.tokenize('foo <% bar %>', 1) == [ { :text, 1, "foo " }, { :expr, 1, [], ' bar ' } ]
|
||||
end
|
||||
|
||||
test "strings with embedded equals code" do
|
||||
assert T.tokenize('foo <%= bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '=', ' bar '}]}
|
||||
assert T.tokenize('foo <%= bar %>', 1) == [ { :text, 1, "foo " }, { :expr, 1, '=', ' bar ' } ]
|
||||
end
|
||||
|
||||
test "strings with more than one line" do
|
||||
assert T.tokenize('foo\n<%= bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo\n'}, {:expr, 2, '=', ' bar '}]}
|
||||
assert T.tokenize('foo\n<%= bar %>', 1) == [ { :text, 1, "foo\n" },{ :expr, 2, '=', ' bar ' } ]
|
||||
end
|
||||
|
||||
test "strings with more than one line and expression with more than one line" do
|
||||
@@ -35,128 +32,50 @@ baz %>
|
||||
<% foo %>
|
||||
'''
|
||||
|
||||
assert T.tokenize(string, 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
assert T.tokenize(string, 1) == [
|
||||
{:text, 1, "foo "},
|
||||
{:expr, 1, '=', ' bar\n\nbaz '},
|
||||
{:text, '\n'},
|
||||
{:expr, 4, '', ' foo '},
|
||||
{:text, '\n'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "quotation" do
|
||||
assert T.tokenize('foo <%% true %>', 1) == {:ok, [
|
||||
{:text, 'foo <% true %>'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "quotation with do/end" do
|
||||
assert T.tokenize('foo <%% true do %>bar<%% end %>', 1) == {:ok, [
|
||||
{:text, 'foo <% true do %>bar<% end %>'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "quotation with interpolation" do
|
||||
assert T.tokenize('a <%% b <%= c %> <%= d %> e %> f', 1) == {:ok, [
|
||||
{:text, 'a <% b '},
|
||||
{:expr, 1, '=', ' c '},
|
||||
{:text, ' '},
|
||||
{:expr, 1, '=', ' d '},
|
||||
{:text, ' e %> f'}
|
||||
]}
|
||||
|
||||
assert T.tokenize('<%%% a <%%= b %> c %>', 1) == {:ok, [
|
||||
{:text, '<%% a <%= b %> c %>'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "comments" do
|
||||
assert T.tokenize('foo <%# true %>', 1) == {:ok, [
|
||||
{:text, 'foo '}
|
||||
]}
|
||||
end
|
||||
|
||||
test "comments with do/end" do
|
||||
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1) == {:ok, [
|
||||
{:text, 'foo bar'}
|
||||
]}
|
||||
{:text, 3, "\n"},
|
||||
{:expr, 4, [], ' foo '},
|
||||
{:text, 4, "\n"}
|
||||
]
|
||||
end
|
||||
|
||||
test "strings with embedded do end" do
|
||||
assert T.tokenize('foo <% if true do %>bar<% end %>', 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:end_expr, 1, '', ' end '}
|
||||
]}
|
||||
assert T.tokenize('foo <% if true do %>bar<% end %>', 1) == [
|
||||
{ :text, 1, "foo " },
|
||||
{ :start_expr, 1, '', ' if true do ' },
|
||||
{ :text, 1, "bar" },
|
||||
{ :end_expr, 1, '', ' end ' }
|
||||
]
|
||||
end
|
||||
|
||||
test "strings with embedded -> end" do
|
||||
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' cond do '},
|
||||
{:middle_expr, 1, '', ' false -> '},
|
||||
{:text, 'bar'},
|
||||
{:middle_expr, 1, '', ' true -> '},
|
||||
{:text, 'baz'},
|
||||
{:end_expr, 1, '', ' end '}
|
||||
]}
|
||||
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1) == [
|
||||
{ :text, 1, "foo " },
|
||||
{ :start_expr, 1, '', ' cond do ' },
|
||||
{ :middle_expr, 1, '', ' false -> ' },
|
||||
{ :text, 1, "bar" },
|
||||
{ :middle_expr, 1, '', ' true -> ' },
|
||||
{ :text, 1, "baz" },
|
||||
{ :end_expr, 1, '', ' end ' }
|
||||
]
|
||||
end
|
||||
|
||||
test "strings with embedded keywords blocks" do
|
||||
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:middle_expr, 1, '', ' else '},
|
||||
{:text, 'baz'},
|
||||
{:end_expr, 1, '', ' end '}
|
||||
]}
|
||||
end
|
||||
|
||||
test "trim mode" do
|
||||
template = '\t<%= if true do %> \n TRUE \n <% else %>\n FALSE \n <% end %> '
|
||||
assert T.tokenize(template, 1, trim: true) == {:ok, [
|
||||
{:start_expr, 1, '=', ' if true do '},
|
||||
{:text, ' TRUE \n'},
|
||||
{:middle_expr, 3, '', ' else '},
|
||||
{:text, ' FALSE \n'},
|
||||
{:end_expr, 5, '', ' end '}
|
||||
]}
|
||||
end
|
||||
|
||||
test "trim mode with comment" do
|
||||
assert T.tokenize(' <%# comment %> \n123', 1, trim: true) == {:ok, [
|
||||
{:text, '123'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "trim mode with CRLF" do
|
||||
assert T.tokenize('0\r\n <%= 12 %> \r\n34', 1, trim: true) == {:ok, [
|
||||
{:text, '0\r\n'},
|
||||
{:expr, 2, '=', ' 12 '},
|
||||
{:text, '34'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "trim mode set to false" do
|
||||
assert T.tokenize(' <%= 12 %> \n', 1, trim: false) == {:ok, [
|
||||
{:text, ' '},
|
||||
{:expr, 1, '=', ' 12 '},
|
||||
{:text, ' \n'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "trim mode no false positives" do
|
||||
assert_not_trimmed = fn x -> assert T.tokenize(x, 1, trim: true) == T.tokenize(x, 1) end
|
||||
|
||||
assert_not_trimmed.('foo <%= "bar" %> ')
|
||||
assert_not_trimmed.('\n <%= "foo" %>bar')
|
||||
assert_not_trimmed.(' <%% hello %> ')
|
||||
assert_not_trimmed.(' <%= 01 %><%= 23 %>\n')
|
||||
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1) == [
|
||||
{ :text, 1, "foo " },
|
||||
{ :start_expr, 1, '', ' if true do ' },
|
||||
{ :text, 1, "bar" },
|
||||
{ :middle_expr, 1, '', ' else ' },
|
||||
{ :text, 1, "baz" },
|
||||
{ :end_expr, 1, '', ' end ' }
|
||||
]
|
||||
end
|
||||
|
||||
test "raise syntax error when there is start mark and no end mark" do
|
||||
assert T.tokenize('foo <% :bar', 1) == {:error, 1, "missing token '%>'"}
|
||||
assert_raise EEx.SyntaxError, "missing token: %>", fn ->
|
||||
T.tokenize('foo <% :bar', 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+57
-136
@@ -1,39 +1,41 @@
|
||||
Code.require_file "test_helper.exs", __DIR__
|
||||
Code.require_file "../test_helper", __FILE__
|
||||
|
||||
require EEx
|
||||
|
||||
defmodule EExTest.Compiled do
|
||||
defmodule EExText.Compiled do
|
||||
def before_compile do
|
||||
fill_in_stacktrace
|
||||
{__ENV__.line, hd(tl(System.stacktrace))}
|
||||
{ __ENV__.line, hd(tl(System.stacktrace)) }
|
||||
end
|
||||
|
||||
{ :erlang, 1, 2 }.tuple_to_list
|
||||
EEx.function_from_string :def, :string_sample, "<%= a + b %>", [:a, :b]
|
||||
|
||||
filename = Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex")
|
||||
filename = File.expand_path("../fixtures/eex_template_with_bindings.eex", __FILE__)
|
||||
EEx.function_from_file :defp, :private_file_sample, filename, [:bar]
|
||||
|
||||
filename = Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex")
|
||||
EEx.function_from_file :def, :public_file_sample, filename, [:bar]
|
||||
|
||||
def file_sample(arg), do: private_file_sample(arg)
|
||||
|
||||
def after_compile do
|
||||
fill_in_stacktrace
|
||||
{__ENV__.line, hd(tl(System.stacktrace))}
|
||||
{ __ENV__.line, hd(tl(System.stacktrace)) }
|
||||
end
|
||||
|
||||
@file "unknown"
|
||||
def unknown do
|
||||
fill_in_stacktrace
|
||||
{__ENV__.line, hd(tl(System.stacktrace))}
|
||||
{ __ENV__.line, hd(tl(System.stacktrace)) }
|
||||
end
|
||||
|
||||
@file __ENV__
|
||||
def other do
|
||||
fill_in_stacktrace
|
||||
{ __ENV__.line, hd(tl(System.stacktrace)) }
|
||||
end
|
||||
|
||||
defp fill_in_stacktrace do
|
||||
try do
|
||||
:erlang.error "failed"
|
||||
Erlang.erlang.error "failed"
|
||||
catch
|
||||
:error, _ -> System.stacktrace
|
||||
:error, _, stack -> stack
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -49,10 +51,6 @@ end
|
||||
defmodule EExTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
doctest EEx
|
||||
doctest EEx.Engine
|
||||
doctest EEx.SmartEngine
|
||||
|
||||
test "evaluates simple string" do
|
||||
assert_eval "foo bar", "foo bar"
|
||||
end
|
||||
@@ -98,16 +96,12 @@ defmodule EExTest do
|
||||
assert_eval "foo baz", "foo <%= if true do %><%= if false do %>bar<% else %>baz<% end %><% end %>"
|
||||
end
|
||||
|
||||
test "evaluates with parentheses after end in end token" do
|
||||
assert_eval " 101 102 103 ", "<%= Enum.map([1,2,3], (fn x -> %> <%= 100 + x %> <% end) ) %>"
|
||||
end
|
||||
|
||||
test "evaluates with defined variable" do
|
||||
assert_eval "foo 1", "foo <% bar = 1 %><%= bar %>"
|
||||
end
|
||||
|
||||
test "evaluates with require code" do
|
||||
assert_eval "foo 1,2,3", "foo <% require Enum, as: E %><%= E.join [1, 2, 3], \",\" %>"
|
||||
assert_eval "foo 1,2,3", "foo <% require Enum, as: E %><%= E.join [1,2,3], \",\" %>"
|
||||
end
|
||||
|
||||
test "evaluates with end of token" do
|
||||
@@ -115,25 +109,25 @@ defmodule EExTest do
|
||||
end
|
||||
|
||||
test "raises a syntax error when the token is invalid" do
|
||||
assert_raise EEx.SyntaxError, "nofile:1: missing token '%>'", fn ->
|
||||
assert_raise EEx.SyntaxError, "missing token: %>", fn ->
|
||||
EEx.compile_string "foo <%= bar"
|
||||
end
|
||||
end
|
||||
|
||||
test "raises a syntax error when end expression is found without a start expression" do
|
||||
assert_raise EEx.SyntaxError, "nofile:1: unexpected token ' end '", fn ->
|
||||
assert_raise EEx.SyntaxError, "unexpected token: ' end ' at line 1", fn ->
|
||||
EEx.compile_string "foo <% end %>"
|
||||
end
|
||||
end
|
||||
|
||||
test "raises a syntax error when start expression is found without an end expression" do
|
||||
assert_raise EEx.SyntaxError, "nofile:2: unexpected end of string, expected a closing '<% end %>'", fn ->
|
||||
EEx.compile_string "foo\n<% if true do %>"
|
||||
assert_raise EEx.SyntaxError, "unexpected end of string. expecting a closing <% end %>.", fn ->
|
||||
EEx.compile_string "foo <% if true do %>"
|
||||
end
|
||||
end
|
||||
|
||||
test "raises a syntax error when nested end expression is found without a start expression" do
|
||||
assert_raise EEx.SyntaxError, "nofile:1: unexpected token ' end '", fn ->
|
||||
test "raises a syntax error when nested end expression is found without an start expression" do
|
||||
assert_raise EEx.SyntaxError, "unexpected token: ' end ' at line 1", fn ->
|
||||
EEx.compile_string "foo <% if true do %><% end %><% end %>"
|
||||
end
|
||||
end
|
||||
@@ -236,10 +230,6 @@ foo
|
||||
assert_eval expected, string
|
||||
end
|
||||
|
||||
test "respects files" do
|
||||
assert_eval "sample.ex", "<%= __ENV__.file %>", [], file: "sample.ex"
|
||||
end
|
||||
|
||||
test "properly handle functions" do
|
||||
expected = """
|
||||
|
||||
@@ -252,7 +242,7 @@ Number 3
|
||||
"""
|
||||
|
||||
string = """
|
||||
<%= Enum.map [1, 2, 3], fn x -> %>
|
||||
<%= Enum.map [1,2,3], fn x -> %>
|
||||
Number <%= x %>
|
||||
<% end %>
|
||||
"""
|
||||
@@ -260,7 +250,7 @@ Number <%= x %>
|
||||
assert_eval expected, string
|
||||
end
|
||||
|
||||
test "properly handle functions on the left side of clauses" do
|
||||
test "do not consider already finished functions" do
|
||||
expected = """
|
||||
foo
|
||||
|
||||
@@ -280,65 +270,14 @@ foo
|
||||
assert_eval expected, string
|
||||
end
|
||||
|
||||
test "evaluates nested do expressions" do
|
||||
string = """
|
||||
<% y = ["a", "b", "c"] %>
|
||||
<%= cond do %>
|
||||
<% "a" in y -> %>
|
||||
Good
|
||||
<% true -> %>
|
||||
<% if true do %>true<% else %>false<% end %>
|
||||
Bad
|
||||
<% end %>
|
||||
"""
|
||||
|
||||
assert_eval "\n\n Good\n \n", string
|
||||
end
|
||||
|
||||
test "evaluates expressions with buffers" do
|
||||
string = """
|
||||
<%= 123 %>
|
||||
<% if true do %>
|
||||
<%= 456 %>
|
||||
<% end %>
|
||||
<%= 789 %>
|
||||
"""
|
||||
|
||||
assert_eval "123\n\n789\n", string
|
||||
end
|
||||
|
||||
test "for comprehensions" do
|
||||
string = """
|
||||
<%= for _name <- packages || [] do %>
|
||||
<% end %>
|
||||
<%= all || :done %>
|
||||
"""
|
||||
assert_eval "\ndone\n", string, packages: nil, all: nil
|
||||
end
|
||||
|
||||
test "unicode" do
|
||||
template = """
|
||||
• <%= "•" %> •
|
||||
<%= "Jößé Vâlìm" %> Jößé Vâlìm
|
||||
"""
|
||||
result = EEx.eval_string(template)
|
||||
assert result == " • • •\n Jößé Vâlìm Jößé Vâlìm\n"
|
||||
end
|
||||
|
||||
test "trim mode" do
|
||||
string = "<%= 123 %> \n456\n <%= 789 %>"
|
||||
expected = "123456\n789"
|
||||
assert_eval expected, string, [], trim: true
|
||||
end
|
||||
|
||||
test "evaluates the source from a given file" do
|
||||
filename = Path.join(__DIR__, "fixtures/eex_template.eex")
|
||||
filename = File.expand_path("../fixtures/eex_template.eex", __FILE__)
|
||||
result = EEx.eval_file(filename)
|
||||
assert result == "foo bar.\n"
|
||||
end
|
||||
|
||||
test "evaluates the source from a given file with bindings" do
|
||||
filename = Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex")
|
||||
filename = File.expand_path("../fixtures/eex_template_with_bindings.eex", __FILE__)
|
||||
result = EEx.eval_file(filename, [bar: 1])
|
||||
assert result == "foo 1\n"
|
||||
end
|
||||
@@ -350,72 +289,54 @@ foo
|
||||
end
|
||||
end
|
||||
|
||||
test "sets external resource attribute" do
|
||||
assert EExTest.Compiled.__info__(:attributes)[:external_resource] ==
|
||||
[Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex")]
|
||||
end
|
||||
|
||||
test "defined from string" do
|
||||
assert EExTest.Compiled.string_sample(1, 2) == "3"
|
||||
assert EExText.Compiled.string_sample(1, 2) == "3"
|
||||
end
|
||||
|
||||
test "defined from file" do
|
||||
assert EExTest.Compiled.file_sample(1) == "foo 1\n"
|
||||
assert EExTest.Compiled.public_file_sample(1) == "foo 1\n"
|
||||
assert EExText.Compiled.file_sample(1) == "foo 1\n"
|
||||
end
|
||||
|
||||
test "defined from file do not affect backtrace" do
|
||||
assert EExTest.Compiled.before_compile ==
|
||||
{8,
|
||||
{EExTest.Compiled,
|
||||
assert EExText.Compiled.before_compile ==
|
||||
{ 8,
|
||||
{ EExText.Compiled,
|
||||
:before_compile,
|
||||
0,
|
||||
[file: to_char_list(Path.relative_to_cwd(__ENV__.file)), line: 7]
|
||||
}
|
||||
}
|
||||
[file: binary_to_list(__FILE__), line: 7]
|
||||
}
|
||||
}
|
||||
|
||||
assert EExTest.Compiled.after_compile ==
|
||||
{23,
|
||||
{EExTest.Compiled,
|
||||
assert EExText.Compiled.after_compile ==
|
||||
{ 19,
|
||||
{ EExText.Compiled,
|
||||
:after_compile,
|
||||
0,
|
||||
[file: to_char_list(Path.relative_to_cwd(__ENV__.file)), line: 22]
|
||||
}
|
||||
}
|
||||
[file: binary_to_list(__FILE__), line: 18]
|
||||
}
|
||||
}
|
||||
|
||||
assert EExTest.Compiled.unknown ==
|
||||
{29,
|
||||
{EExTest.Compiled,
|
||||
assert EExText.Compiled.unknown ==
|
||||
{ 25,
|
||||
{ EExText.Compiled,
|
||||
:unknown,
|
||||
0,
|
||||
[file: 'unknown', line: 28]
|
||||
}
|
||||
}
|
||||
[file: 'unknown', line: 24]
|
||||
}
|
||||
}
|
||||
|
||||
assert EExText.Compiled.other ==
|
||||
{ 31,
|
||||
{ EExText.Compiled,
|
||||
:other,
|
||||
0,
|
||||
[file: binary_to_list(__FILE__), line: 30]
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
defmodule TestEngine do
|
||||
@behaviour EEx.Engine
|
||||
|
||||
def handle_body(body) do
|
||||
{:wrapped, body}
|
||||
end
|
||||
|
||||
def handle_text(buffer, text) do
|
||||
EEx.Engine.handle_text(buffer, text)
|
||||
end
|
||||
|
||||
def handle_expr(buffer, mark, expr) do
|
||||
EEx.Engine.handle_expr(buffer, mark, expr)
|
||||
end
|
||||
end
|
||||
|
||||
test "calls handle_body" do
|
||||
assert {:wrapped, "foo"} = EEx.eval_string("foo", [], engine: TestEngine)
|
||||
end
|
||||
|
||||
defp assert_eval(expected, actual, binding \\ [], opts \\ []) do
|
||||
opts = Enum.into [file: __ENV__.file, engine: EEx.Engine], opts
|
||||
result = EEx.eval_string(actual, binding, opts)
|
||||
defp assert_eval(expected, actual) do
|
||||
result = EEx.eval_string(actual, [], file: __FILE__, engine: EEx.Engine)
|
||||
assert result == expected
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
ExUnit.start [trace: "--trace" in System.argv]
|
||||
# Configure ExUnit, no options supported yet.
|
||||
ExUnit.start []
|
||||
@@ -0,0 +1,39 @@
|
||||
-define(ELIXIR_WRAP_CALL(Line, Module, Function, Args),
|
||||
{ call, Line,
|
||||
{ remote, Line, { atom, Line, Module }, { atom, Line, Function } },
|
||||
Args
|
||||
}).
|
||||
|
||||
-define(ELIXIR_ATOM_CONCAT(Atoms), list_to_atom(lists:concat(Atoms))).
|
||||
-define(ELIXIR_MACRO(Macro), list_to_atom(lists:concat(['MACRO-',Macro]))).
|
||||
|
||||
-record(elixir_scope, {
|
||||
context=nil, %% can be assign, guards or nil
|
||||
noname=false, %% when true, don't add new names (used by try)
|
||||
check_clauses=true, %% when true, check def clauses ordering
|
||||
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=dict:new(), %% a dict of defined variables and their alias
|
||||
temp_vars=dict:new(), %% a dict of all variables defined in a particular assign
|
||||
clause_vars=dict:new(), %% a dict of all variables defined in a particular clause
|
||||
quote_vars=dict:new(), %% a dict of all quoted variables
|
||||
extra_guards=nil, %% extra guards from args expansion
|
||||
counter=0, %% a counter for the variables defined
|
||||
file=(<<"nofile">>), %% the current scope filename
|
||||
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
|
||||
|
||||
-record(elixir_quote, {
|
||||
line=0,
|
||||
marker=quoted,
|
||||
unquote=true
|
||||
}).
|
||||
+49
-177
@@ -1,194 +1,66 @@
|
||||
defmodule Access do
|
||||
import Kernel, except: [access: 2]
|
||||
|
||||
defprotocol Access do
|
||||
@moduledoc """
|
||||
Key-based access to data structures via the `foo[bar]` syntax.
|
||||
|
||||
Elixir provides two syntaxes for accessing values. `user[:name]`
|
||||
is used by dynamic structures, like maps and keywords, while
|
||||
`user.name` is used by structs. The main difference is that
|
||||
`user[:name]` won't raise if the key `:name` is missing but
|
||||
`user.name` will raise if there is no `:name` key.
|
||||
|
||||
## Key-based lookups
|
||||
|
||||
Out of the box, Access works with `Keyword` and `Map`:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:a]
|
||||
1
|
||||
|
||||
iex> map = %{a: 1, b: 2}
|
||||
iex> map[:a]
|
||||
1
|
||||
|
||||
iex> star_ratings = %{1.0 => "★", 1.5 => "★☆", 2.0 => "★★"}
|
||||
iex> star_ratings[1.5]
|
||||
"★☆"
|
||||
|
||||
Access can be combined with `Kernel.put_in/3` to put a value
|
||||
in a given key:
|
||||
|
||||
iex> map = %{a: 1, b: 2}
|
||||
iex> put_in map[:a], 3
|
||||
%{a: 3, b: 2}
|
||||
|
||||
This syntax is very convenient as it can be nested arbitrarily:
|
||||
|
||||
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
|
||||
iex> put_in users["john"][:age], 28
|
||||
%{"john" => %{age: 28}, "meg" => %{age: 23}}
|
||||
|
||||
Furthermore, Access transparently ignores `nil` values:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:c][:unknown]
|
||||
nil
|
||||
|
||||
Since Access is a behaviour, it can be implemented to key-value
|
||||
data structures. Access requires the key comparison to be
|
||||
implemented using the `===` operator.
|
||||
|
||||
## Field-based lookups
|
||||
|
||||
The Access syntax (`foo[bar]`) cannot be used to access fields in
|
||||
structs. That's by design, as Access is meant to be used for
|
||||
dynamic key-value structures, like maps and keywords, and not
|
||||
by static ones like structs.
|
||||
|
||||
However Elixir already provides a field-based lookup for structs.
|
||||
Imagine a struct named `User` with name and age fields. The
|
||||
following would raise:
|
||||
|
||||
user = %User{name: "john"}
|
||||
user[:name]
|
||||
** (UndefinedFunctionError) undefined function User.fetch/2
|
||||
(User does not implement the Access behaviour)
|
||||
|
||||
Structs instead use the `user.name` syntax:
|
||||
|
||||
user.name
|
||||
#=> "john"
|
||||
|
||||
The same `user.name` syntax can also be used by `Kernel.put_in/2`
|
||||
to for updating structs fields:
|
||||
|
||||
put_in user.name, "mary"
|
||||
%User{name: "mary"}
|
||||
|
||||
Differently from `user[:name]`, `user.name` cannot be extended by
|
||||
the developers, and will be always restricted to only maps and
|
||||
structs.
|
||||
|
||||
Summing up:
|
||||
|
||||
* `user[:name]` is used by dynamic structures, is extensible and
|
||||
does not raise on missing keys
|
||||
* `user.name` is used by static structures, it is not extensible
|
||||
and it will raise on missing keys
|
||||
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.
|
||||
|
||||
This protocol is limited and is implemented only for the
|
||||
following built-in types: keywords, tuples, atoms and
|
||||
functions.
|
||||
"""
|
||||
|
||||
@type t :: list | map | nil
|
||||
@type key :: any
|
||||
@type value :: any
|
||||
|
||||
@callback fetch(t, key) :: {:ok, value} | :error
|
||||
@callback get_and_update(t, key, (value -> {value, value})) :: {value, t}
|
||||
|
||||
defmacrop raise_undefined_behaviour(e, struct, top) do
|
||||
quote do
|
||||
stacktrace = System.stacktrace
|
||||
e =
|
||||
case stacktrace do
|
||||
[unquote(top)|_] ->
|
||||
%{unquote(e) | reason: "#{inspect unquote(struct)} does not implement the Access behaviour"}
|
||||
_ ->
|
||||
unquote(e)
|
||||
end
|
||||
reraise e, stacktrace
|
||||
end
|
||||
end
|
||||
@only [List, Function, Record, Atom]
|
||||
|
||||
@doc """
|
||||
Fetches the container's value for the given key.
|
||||
Receives the element being accessed and the access item.
|
||||
"""
|
||||
@spec fetch(t, term) :: {:ok, term} | :error
|
||||
def fetch(container, key)
|
||||
|
||||
def fetch(%{__struct__: struct} = container, key) do
|
||||
struct.fetch(container, key)
|
||||
rescue
|
||||
e in UndefinedFunctionError ->
|
||||
raise_undefined_behaviour e, struct, {^struct, :fetch, [^container, ^key], _}
|
||||
end
|
||||
|
||||
def fetch(%{} = map, key) do
|
||||
:maps.find(key, map)
|
||||
end
|
||||
|
||||
def fetch(list, key) when is_list(list) and is_atom(key) do
|
||||
case :lists.keyfind(key, 1, list) do
|
||||
{^key, value} -> {:ok, value}
|
||||
false -> :error
|
||||
end
|
||||
end
|
||||
|
||||
def fetch(list, key) when is_list(list) do
|
||||
raise ArgumentError,
|
||||
"the Access calls for keywords expect the key to be an atom, got: " <> inspect(key)
|
||||
end
|
||||
|
||||
def fetch(nil, _key) do
|
||||
:error
|
||||
end
|
||||
def access(element, qualifier)
|
||||
end
|
||||
|
||||
defimpl Access, for: List do
|
||||
@doc """
|
||||
Gets the container's value for the given key.
|
||||
Access the given key in a keywords list.
|
||||
|
||||
## Examples
|
||||
|
||||
keywords = [a: 1, b: 2]
|
||||
keywords[:a] #=> 1
|
||||
|
||||
"""
|
||||
@spec get(t, term, term) :: term
|
||||
def get(container, key, default \\ nil) do
|
||||
case fetch(container, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> default
|
||||
end
|
||||
|
||||
def access(list, atom) when is_atom(atom) do
|
||||
atom_access(list, atom)
|
||||
end
|
||||
|
||||
defp atom_access([{k, _}|_], key) when key < k, do: nil
|
||||
defp atom_access([{k, _}|d], key) when key > k, do: atom_access(d, key)
|
||||
defp atom_access([{_k, value}|_], _key), do: value
|
||||
defp atom_access([], _), do: nil
|
||||
end
|
||||
|
||||
defimpl Access, for: Atom do
|
||||
@doc """
|
||||
Gets and updates the container's value for the given key, in a single pass.
|
||||
|
||||
The argument function `fun` must receive the value for the given `key` (or
|
||||
`nil` if the key doesn't exist in `container`). It must return a tuple
|
||||
containing the `get` value and the new value to be stored in the `container`.
|
||||
|
||||
This function returns a two-element tuple.
|
||||
The first element is the `get` value, as returned by `fun`.
|
||||
The second element is the container, updated with the value returned by `fun`.
|
||||
The access protocol can only be accessed by atoms
|
||||
at compilation time. If we reach this, we should raise
|
||||
an exception.
|
||||
"""
|
||||
@spec get_and_update(t, term, (term -> {get, term})) :: {get, t} when get: var
|
||||
def get_and_update(container, key, fun)
|
||||
|
||||
def get_and_update(%{__struct__: struct} = container, key, fun) do
|
||||
struct.get_and_update(container, key, fun)
|
||||
rescue
|
||||
e in UndefinedFunctionError ->
|
||||
raise_undefined_behaviour e, struct, {^struct, :get_and_update, [^container, ^key, ^fun], _}
|
||||
end
|
||||
|
||||
def get_and_update(%{} = map, key, fun) do
|
||||
current_value = case :maps.find(key, map) do
|
||||
{:ok, value} -> value
|
||||
:error -> nil
|
||||
end
|
||||
|
||||
{get, update} = fun.(current_value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
def access(atom, _) do
|
||||
raise "The access protocol can only be invoked for atoms at " <>
|
||||
"compilation time, tried to invoke it for #{inspect atom}"
|
||||
end
|
||||
|
||||
def get_and_update(list, key, fun) when is_list(list) do
|
||||
Keyword.get_and_update(list, key, fun)
|
||||
end
|
||||
|
||||
def get_and_update(nil, key, _fun) do
|
||||
raise ArgumentError,
|
||||
"could not put/update key #{inspect key} on a nil value"
|
||||
end
|
||||
|
||||
defimpl Access, for: Function do
|
||||
@doc """
|
||||
The Access protocol for functions simply invokes
|
||||
the function passing the item as argument. This
|
||||
is useful because it allows a function to be
|
||||
passed as argument in places a dict would also fit.
|
||||
"""
|
||||
def access(function, item) do
|
||||
function.(item)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,319 +0,0 @@
|
||||
defmodule Agent do
|
||||
@moduledoc """
|
||||
Agents are a simple abstraction around state.
|
||||
|
||||
Often in Elixir there is a need to share or store state that
|
||||
must be accessed from different processes or by the same process
|
||||
at different points in time.
|
||||
|
||||
The Agent module provides a basic server implementation that
|
||||
allows state to be retrieved and updated via a simple API.
|
||||
|
||||
## Examples
|
||||
|
||||
For example, in the Mix tool that ships with Elixir, we need
|
||||
to keep a set of all tasks executed by a given project. Since
|
||||
this set is shared, we can implement it with an Agent:
|
||||
|
||||
defmodule Mix.TasksServer do
|
||||
def start_link do
|
||||
Agent.start_link(fn -> MapSet.new end, name: __MODULE__)
|
||||
end
|
||||
|
||||
@doc "Checks if the task has already executed"
|
||||
def executed?(task, project) do
|
||||
item = {task, project}
|
||||
Agent.get(__MODULE__, fn set ->
|
||||
item in set
|
||||
end)
|
||||
end
|
||||
|
||||
@doc "Marks a task as executed"
|
||||
def put_task(task, project) do
|
||||
item = {task, project}
|
||||
Agent.update(__MODULE__, &MapSet.put(&1, item))
|
||||
end
|
||||
|
||||
@doc "Resets the executed tasks and returns the previous list of tasks"
|
||||
def take_all() do
|
||||
Agent.get_and_update(__MODULE__, fn set ->
|
||||
{Enum.into(set, []), MapSet.new}
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
Note that agents still provide a segregation between the
|
||||
client and server APIs, as seen in GenServers. In particular,
|
||||
all code inside the function passed to the agent is executed
|
||||
by the agent. This distinction is important because you may
|
||||
want to avoid expensive operations inside the agent, as it will
|
||||
effectively block the agent until the request is fulfilled.
|
||||
|
||||
Consider these two examples:
|
||||
|
||||
# Compute in the agent/server
|
||||
def get_something(agent) do
|
||||
Agent.get(agent, fn state -> do_something_expensive(state) end)
|
||||
end
|
||||
|
||||
# Compute in the agent/client
|
||||
def get_something(agent) do
|
||||
Agent.get(agent, &(&1)) |> do_something_expensive()
|
||||
end
|
||||
|
||||
The first function blocks the agent. The second function copies
|
||||
all the state to the client and then executes the operation in the
|
||||
client. The difference is whether the data is large enough to require
|
||||
processing in the server, at least initially, or small enough to be
|
||||
sent to the client cheaply.
|
||||
|
||||
## Name Registration
|
||||
|
||||
An Agent is bound to the same name registration rules as GenServers.
|
||||
Read more about it in the `GenServer` docs.
|
||||
|
||||
## A word on distributed agents
|
||||
|
||||
It is important to consider the limitations of distributed agents. Agents
|
||||
provide two APIs, one that works with anonymous functions and another
|
||||
that expects an explicit module, function, and arguments.
|
||||
|
||||
In a distributed setup with multiple nodes, the API that accepts anonymous
|
||||
functions only works if the caller (client) and the agent have the same
|
||||
version of the caller module.
|
||||
|
||||
Keep in mind this issue also shows up when performing "rolling upgrades"
|
||||
with agents. By rolling upgrades we mean the following situation: you wish
|
||||
to deploy a new version of your software by *shutting down* some of your
|
||||
nodes and replacing them with nodes running a new version of the software.
|
||||
In this setup, part of your environment will have one version of a given
|
||||
module and the other part another version (the newer one) of the same module.
|
||||
|
||||
The best solution is to simply use the explicit module, function, and arguments
|
||||
APIs when working with distributed agents.
|
||||
|
||||
## Hot code swapping
|
||||
|
||||
An agent can have its code hot swapped live by simply passing a module,
|
||||
function, and args tuple to the update instruction. For example, imagine
|
||||
you have an agent named `:sample` and you want to convert its inner state
|
||||
from some dict structure to a map. It can be done with the following
|
||||
instruction:
|
||||
|
||||
{:update, :sample, {:advanced, {Enum, :into, [%{}]}}}
|
||||
|
||||
The agent's state will be added to the given list as the first argument.
|
||||
"""
|
||||
|
||||
@typedoc "Return values of `start*` functions"
|
||||
@type on_start :: {:ok, pid} | {:error, {:already_started, pid} | term}
|
||||
|
||||
@typedoc "The agent name"
|
||||
@type name :: atom | {:global, term} | {:via, module, term}
|
||||
|
||||
@typedoc "The agent reference"
|
||||
@type agent :: pid | {atom, node} | name
|
||||
|
||||
@typedoc "The agent state"
|
||||
@type state :: term
|
||||
|
||||
@doc """
|
||||
Starts an agent linked to the current process with the given function.
|
||||
|
||||
This is often used to start the agent as part of a supervision tree.
|
||||
|
||||
Once the agent is spawned, the given function is invoked and its return
|
||||
value is used as the agent state. Note that `start_link` does not return
|
||||
until the given function has returned.
|
||||
|
||||
## Options
|
||||
|
||||
The `:name` option is used for registration as described in the module
|
||||
documentation.
|
||||
|
||||
If the `:timeout` option is present, the agent is allowed to spend at most
|
||||
the given number of milliseconds on initialization or it will be terminated
|
||||
and the start function will return `{:error, :timeout}`.
|
||||
|
||||
If the `:debug` option is present, the corresponding function in the
|
||||
[`:sys` module](http://www.erlang.org/doc/man/sys.html) will be invoked.
|
||||
|
||||
If the `:spawn_opt` option is present, its value will be passed as options
|
||||
to the underlying process as in `Process.spawn/4`.
|
||||
|
||||
## Return values
|
||||
|
||||
If the server is successfully created and initialized, the function returns
|
||||
`{:ok, pid}`, where `pid` is the pid of the server. If an agent with the
|
||||
specified name already exists, the function returns
|
||||
`{:error, {:already_started, pid}}` with the pid of that process.
|
||||
|
||||
If the given function callback fails with `reason`, the function returns
|
||||
`{:error, reason}`.
|
||||
"""
|
||||
@spec start_link((() -> term), GenServer.options) :: on_start
|
||||
def start_link(fun, options \\ []) when is_function(fun, 0) do
|
||||
GenServer.start_link(Agent.Server, fun, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts an agent linked to the current process with the given module
|
||||
function and arguments.
|
||||
|
||||
Same as `start_link/2` but a module, function and args are expected
|
||||
instead of an anonymous function.
|
||||
"""
|
||||
@spec start_link(module, atom, [any], GenServer.options) :: on_start
|
||||
def start_link(module, fun, args, options \\ []) do
|
||||
GenServer.start_link(Agent.Server, {module, fun, args}, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts an agent process without links (outside of a supervision tree).
|
||||
|
||||
See `start_link/2` for more information.
|
||||
"""
|
||||
@spec start((() -> term), GenServer.options) :: on_start
|
||||
def start(fun, options \\ []) when is_function(fun, 0) do
|
||||
GenServer.start(Agent.Server, fun, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts an agent with the given module function and arguments.
|
||||
|
||||
Similar to `start/2` but a module, function and args are expected
|
||||
instead of an anonymous function.
|
||||
"""
|
||||
@spec start(module, atom, [any], GenServer.options) :: on_start
|
||||
def start(module, fun, args, options \\ []) do
|
||||
GenServer.start(Agent.Server, {module, fun, args}, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets an agent value via the given function.
|
||||
|
||||
The function `fun` is sent to the `agent` which invokes the function
|
||||
passing the agent state. The result of the function invocation is
|
||||
returned.
|
||||
|
||||
A timeout can also be specified (it has a default value of 5000).
|
||||
"""
|
||||
@spec get(agent, (state -> a), timeout) :: a when a: var
|
||||
def get(agent, fun, timeout \\ 5000) when is_function(fun, 1) do
|
||||
GenServer.call(agent, {:get, fun}, timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets an agent value via the given function.
|
||||
|
||||
Same as `get/3` but a module, function and args are expected
|
||||
instead of an anonymous function. The state is added as first
|
||||
argument to the given list of args.
|
||||
"""
|
||||
@spec get(agent, module, atom, [term], timeout) :: any
|
||||
def get(agent, module, fun, args, timeout \\ 5000) do
|
||||
GenServer.call(agent, {:get, {module, fun, args}}, timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets and updates the agent state in one operation.
|
||||
|
||||
The function `fun` is sent to the `agent` which invokes the function
|
||||
passing the agent state. The function must return a tuple with two
|
||||
elements, the first being the value to return (i.e. the `get` value)
|
||||
and the second one is the new state.
|
||||
|
||||
A timeout can also be specified (it has a default value of 5000).
|
||||
"""
|
||||
@spec get_and_update(agent, (state -> {a, state}), timeout) :: a when a: var
|
||||
def get_and_update(agent, fun, timeout \\ 5000) when is_function(fun, 1) do
|
||||
GenServer.call(agent, {:get_and_update, fun}, timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets and updates the agent state in one operation.
|
||||
|
||||
Same as `get_and_update/3` but a module, function and args are expected
|
||||
instead of an anonymous function. The state is added as first
|
||||
argument to the given list of args.
|
||||
"""
|
||||
@spec get_and_update(agent, module, atom, [term], timeout) :: any
|
||||
def get_and_update(agent, module, fun, args, timeout \\ 5000) do
|
||||
GenServer.call(agent, {:get_and_update, {module, fun, args}}, timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the agent state.
|
||||
|
||||
The function `fun` is sent to the `agent` which invokes the function
|
||||
passing the agent state. The function must return the new state.
|
||||
|
||||
A timeout can also be specified (it has a default value of 5000).
|
||||
This function always returns `:ok`.
|
||||
"""
|
||||
@spec update(agent, (state -> state), timeout) :: :ok
|
||||
def update(agent, fun, timeout \\ 5000) when is_function(fun, 1) do
|
||||
GenServer.call(agent, {:update, fun}, timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the agent state.
|
||||
|
||||
Same as `update/3` but a module, function and args are expected
|
||||
instead of an anonymous function. The state is added as first
|
||||
argument to the given list of args.
|
||||
"""
|
||||
@spec update(agent, module, atom, [term], timeout) :: :ok
|
||||
def update(agent, module, fun, args, timeout \\ 5000) do
|
||||
GenServer.call(agent, {:update, {module, fun, args}}, timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a cast (fire and forget) operation on the agent state.
|
||||
|
||||
The function `fun` is sent to the `agent` which invokes the function
|
||||
passing the agent state. The function must return the new state.
|
||||
|
||||
Note that `cast` returns `:ok` immediately, regardless of whether the
|
||||
destination node or agent exists.
|
||||
"""
|
||||
@spec cast(agent, (state -> state)) :: :ok
|
||||
def cast(agent, fun) when is_function(fun, 1) do
|
||||
GenServer.cast(agent, {:cast, fun})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a cast (fire and forget) operation on the agent state.
|
||||
|
||||
Same as `cast/2` but a module, function and args are expected
|
||||
instead of an anonymous function. The state is added as first
|
||||
argument to the given list of args.
|
||||
"""
|
||||
@spec cast(agent, module, atom, [term]) :: :ok
|
||||
def cast(agent, module, fun, args) do
|
||||
GenServer.cast(agent, {:cast, {module, fun, args}})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the agent with the given `reason`.
|
||||
|
||||
It returns `:ok` if the server terminates with the given
|
||||
reason, if it terminates with another reason, the call will
|
||||
exit.
|
||||
|
||||
This function keeps OTP semantics regarding error reporting.
|
||||
If the reason is any other than `:normal`, `:shutdown` or
|
||||
`{:shutdown, _}`, an error report will be logged.
|
||||
"""
|
||||
@spec stop(agent, reason :: term, timeout) :: :ok
|
||||
def stop(agent, reason \\ :normal, timeout \\ :infinity) do
|
||||
if is_integer(reason) or reason == :infinity do
|
||||
IO.write :stderr, "warning: Agent.stop(agent, timeout) is deprecated, " <>
|
||||
"please use Agent.stop(agent, :normal, timeout) instead\n" <>
|
||||
Exception.format_stacktrace
|
||||
:gen.stop(agent, :normal, reason)
|
||||
else
|
||||
:gen.stop(agent, reason, timeout)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,59 +0,0 @@
|
||||
defmodule Agent.Server do
|
||||
@moduledoc false
|
||||
|
||||
use GenServer
|
||||
|
||||
def init(fun) do
|
||||
_ = initial_call(fun)
|
||||
{:ok, run(fun, [])}
|
||||
end
|
||||
|
||||
def handle_call({:get, fun}, _from, state) do
|
||||
{:reply, run(fun, [state]), state}
|
||||
end
|
||||
|
||||
def handle_call({:get_and_update, fun}, _from, state) do
|
||||
case run(fun, [state]) do
|
||||
{reply, state} -> {:reply, reply, state}
|
||||
other -> {:stop, {:bad_return_value, other}, state}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_call({:update, fun}, _from, state) do
|
||||
{:reply, :ok, run(fun, [state])}
|
||||
end
|
||||
|
||||
def handle_call(msg, from, state) do
|
||||
super(msg, from, state)
|
||||
end
|
||||
|
||||
def handle_cast({:cast, fun}, state) do
|
||||
{:noreply, run(fun, [state])}
|
||||
end
|
||||
|
||||
def handle_cast(msg, state) do
|
||||
super(msg, state)
|
||||
end
|
||||
|
||||
def code_change(_old, state, fun) do
|
||||
{:ok, run(fun, [state])}
|
||||
end
|
||||
|
||||
defp initial_call(mfa) do
|
||||
_ = Process.put(:"$initial_call", get_initial_call(mfa))
|
||||
:ok
|
||||
end
|
||||
|
||||
defp get_initial_call(fun) when is_function(fun, 0) do
|
||||
{:module, module} = :erlang.fun_info(fun, :module)
|
||||
{:name, name} = :erlang.fun_info(fun, :name)
|
||||
{module, name, 0}
|
||||
end
|
||||
|
||||
defp get_initial_call({mod, fun, args}) do
|
||||
{mod, fun, length(args)}
|
||||
end
|
||||
|
||||
defp run({m, f, a}, extra), do: apply(m, f, extra ++ a)
|
||||
defp run(fun, extra), do: apply(fun, extra)
|
||||
end
|
||||
@@ -1,482 +0,0 @@
|
||||
defmodule Application do
|
||||
@moduledoc """
|
||||
A module for working with applications and defining application callbacks.
|
||||
|
||||
In Elixir (actually, in Erlang/OTP), an application is a component
|
||||
implementing some specific functionality, that can be started and stopped
|
||||
as a unit, and which can be re-used in other systems.
|
||||
|
||||
Applications are defined with an application file named `APP.app` where
|
||||
`APP` is the application name, usually in `underscore_case`. The application
|
||||
file must reside in the same `ebin` directory as the compiled modules of the
|
||||
application.
|
||||
|
||||
In Elixir, Mix is responsible for compiling your source code and
|
||||
generating your application `.app` file. Furthermore, Mix is also
|
||||
responsible for configuring, starting and stopping your application
|
||||
and its dependencies. For this reason, this documentation will focus
|
||||
on the remaining aspects of your application: the application environment
|
||||
and the application callback module.
|
||||
|
||||
You can learn more about Mix generation of `.app` files by typing
|
||||
`mix help compile.app`.
|
||||
|
||||
## Application environment
|
||||
|
||||
Once an application is started, OTP provides an application environment
|
||||
that can be used to configure the application.
|
||||
|
||||
Assuming you are inside a Mix project, you can edit the `application`
|
||||
function in the `mix.exs` file to the following:
|
||||
|
||||
def application do
|
||||
[env: [hello: :world]]
|
||||
end
|
||||
|
||||
In the application function, we can define the default environment values
|
||||
for our application. By starting your application with `iex -S mix`, you
|
||||
can access the default value:
|
||||
|
||||
Application.get_env(:APP_NAME, :hello)
|
||||
#=> :world
|
||||
|
||||
It is also possible to put and delete values from the application value,
|
||||
including new values that are not defined in the environment file (although
|
||||
this should be avoided).
|
||||
|
||||
Keep in mind that each application is responsible for its environment.
|
||||
Do not use the functions in this module for directly accessing or modifying
|
||||
the environment of other applications (as it may lead to inconsistent
|
||||
data in the application environment).
|
||||
|
||||
## Application module callback
|
||||
|
||||
Often times, an application defines a supervision tree that must be started
|
||||
and stopped when the application starts and stops. For such, we need to
|
||||
define an application module callback. The first step is to define the
|
||||
module callback in the application definition in the `mix.exs` file:
|
||||
|
||||
def application do
|
||||
[mod: {MyApp, []}]
|
||||
end
|
||||
|
||||
Our application now requires the `MyApp` module to provide an application
|
||||
callback. This can be done by invoking `use Application` in that module and
|
||||
defining a `start/2` callback, for example:
|
||||
|
||||
defmodule MyApp do
|
||||
use Application
|
||||
|
||||
def start(_type, _args) do
|
||||
MyApp.Supervisor.start_link()
|
||||
end
|
||||
end
|
||||
|
||||
`start/2` typically returns `{:ok, pid}` or `{:ok, pid, state}` where
|
||||
`pid` identifies the supervision tree and `state` is the application state.
|
||||
`args` is the second element of the tuple given to the `:mod` option.
|
||||
|
||||
The `type` argument passed to `start/2` is usually `:normal` unless in a
|
||||
distributed setup where application takeovers and failovers are configured.
|
||||
This particular aspect of applications is explained in more detail in the
|
||||
OTP documentation:
|
||||
|
||||
* [`:application` module](http://www.erlang.org/doc/man/application.html)
|
||||
* [Applications – OTP Design Principles](http://www.erlang.org/doc/design_principles/applications.html)
|
||||
|
||||
A developer may also implement the `stop/1` callback (automatically defined
|
||||
by `use Application`) which does any application cleanup. It receives the
|
||||
application state and can return any value. Note that shutting down the
|
||||
supervisor is automatically handled by the VM.
|
||||
"""
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote location: :keep do
|
||||
@behaviour :application
|
||||
|
||||
@doc false
|
||||
def stop(_state) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defoverridable [stop: 1]
|
||||
end
|
||||
end
|
||||
|
||||
@type app :: atom
|
||||
@type key :: atom
|
||||
@type value :: term
|
||||
@type start_type :: :permanent | :transient | :temporary
|
||||
|
||||
@application_keys [:description, :id, :vsn, :modules, :maxP, :maxT, :registered,
|
||||
:included_applications, :applications, :mod, :start_phases]
|
||||
|
||||
@doc """
|
||||
Returns the spec for `app`.
|
||||
|
||||
The following keys are returned:
|
||||
|
||||
* #{Enum.map_join @application_keys, "\n * ", &inspect/1}
|
||||
|
||||
Note the environment is not returned as it can be accessed via
|
||||
`fetch_env/2`. Returns `nil` if the application is not loaded.
|
||||
"""
|
||||
@spec spec(app) :: [{key, value}] | nil
|
||||
def spec(app) do
|
||||
case :application.get_all_key(app) do
|
||||
{:ok, info} -> :lists.keydelete(:env, 1, info)
|
||||
:undefined -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value for `key` in `app`'s specification.
|
||||
|
||||
See `spec/1` for the supported keys. If the given
|
||||
specification parameter does not exist, this function
|
||||
will raise. Returns `nil` if the application is not loaded.
|
||||
"""
|
||||
@spec spec(app, key) :: value | nil
|
||||
def spec(app, key) when key in @application_keys do
|
||||
case :application.get_key(app, key) do
|
||||
{:ok, value} -> value
|
||||
:undefined -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Get the application for the given module.
|
||||
|
||||
The application is located by analyzing the spec
|
||||
of all loaded applications. Returns `nil` if
|
||||
the module is not listed in any application spec.
|
||||
"""
|
||||
@spec get_application(atom) :: atom | nil
|
||||
def get_application(module) when is_atom(module) do
|
||||
case :application.get_application(module) do
|
||||
{:ok, app} -> app
|
||||
:undefined -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all key-value pairs for `app`.
|
||||
"""
|
||||
@spec get_all_env(app) :: [{key, value}]
|
||||
def get_all_env(app) do
|
||||
:application.get_all_env(app)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value for `key` in `app`'s environment.
|
||||
|
||||
If the configuration parameter does not exist, the function returns the
|
||||
`default` value.
|
||||
"""
|
||||
@spec get_env(app, key, value) :: value
|
||||
def get_env(app, key, default \\ nil) do
|
||||
:application.get_env(app, key, default)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value for `key` in `app`'s environment in a tuple.
|
||||
|
||||
If the configuration parameter does not exist, the function returns `:error`.
|
||||
"""
|
||||
@spec fetch_env(app, key) :: {:ok, value} | :error
|
||||
def fetch_env(app, key) do
|
||||
case :application.get_env(app, key) do
|
||||
{:ok, value} -> {:ok, value}
|
||||
:undefined -> :error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value for `key` in `app`'s environment.
|
||||
|
||||
If the configuration parameter does not exist, raises `ArgumentError`.
|
||||
"""
|
||||
@spec fetch_env!(app, key) :: value | no_return
|
||||
def fetch_env!(app, key) do
|
||||
case fetch_env(app, key) do
|
||||
{:ok, value} -> value
|
||||
:error ->
|
||||
raise ArgumentError,
|
||||
"application #{inspect app} is not loaded, " <>
|
||||
"or the configuration parameter #{inspect key} is not set"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the `value` in `key` for the given `app`.
|
||||
|
||||
## Options
|
||||
|
||||
* `:timeout` - the timeout for the change (defaults to 5000ms)
|
||||
* `:persistent` - persists the given value on application load and reloads
|
||||
|
||||
If `put_env/4` is called before the application is loaded, the application
|
||||
environment values specified in the `.app` file will override the ones
|
||||
previously set.
|
||||
|
||||
The persistent option can be set to `true` when there is a need to guarantee
|
||||
parameters set with this function will not be overridden by the ones defined
|
||||
in the application resource file on load. This means persistent values will
|
||||
stick after the application is loaded and also on application reload.
|
||||
"""
|
||||
@spec put_env(app, key, value, [timeout: timeout, persistent: boolean]) :: :ok
|
||||
def put_env(app, key, value, opts \\ []) do
|
||||
:application.set_env(app, key, value, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the `key` from the given `app` environment.
|
||||
|
||||
See `put_env/4` for a description of the options.
|
||||
"""
|
||||
@spec delete_env(app, key, [timeout: timeout, persistent: boolean]) :: :ok
|
||||
def delete_env(app, key, opts \\ []) do
|
||||
:application.unset_env(app, key, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the given `app` is started.
|
||||
|
||||
Same as `start/2` but returns `:ok` if the application was already
|
||||
started. This is useful in scripts and in test setup, where test
|
||||
applications need to be explicitly started:
|
||||
|
||||
:ok = Application.ensure_started(:my_test_dep)
|
||||
|
||||
"""
|
||||
@spec ensure_started(app, start_type) :: :ok | {:error, term}
|
||||
def ensure_started(app, type \\ :temporary) when is_atom(app) do
|
||||
:application.ensure_started(app, type)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the given `app` and its applications are started.
|
||||
|
||||
Same as `start/2` but also starts the applications listed under
|
||||
`:applications` in the `.app` file in case they were not previously
|
||||
started.
|
||||
"""
|
||||
@spec ensure_all_started(app, start_type) :: {:ok, [app]} | {:error, {app, term}}
|
||||
def ensure_all_started(app, type \\ :temporary) when is_atom(app) do
|
||||
:application.ensure_all_started(app, type)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts the given `app`.
|
||||
|
||||
If the `app` is not loaded, the application will first be loaded using `load/1`.
|
||||
Any included application, defined in the `:included_applications` key of the
|
||||
`.app` file will also be loaded, but they won't be started.
|
||||
|
||||
Furthermore, all applications listed in the `:applications` key must be explicitly
|
||||
started before this application is. If not, `{:error, {:not_started, app}}` is
|
||||
returned, where `app` is the name of the missing application.
|
||||
|
||||
In case you want to automatically load **and start** all of `app`'s dependencies,
|
||||
see `ensure_all_started/2`.
|
||||
|
||||
The `type` argument specifies the type of the application:
|
||||
|
||||
* `:permanent` - if `app` terminates, all other applications and the entire
|
||||
node are also terminated.
|
||||
|
||||
* `:transient` - if `app` terminates with `:normal` reason, it is reported
|
||||
but no other applications are terminated. If a transient application
|
||||
terminates abnormally, all other applications and the entire node are
|
||||
also terminated.
|
||||
|
||||
* `:temporary` - if `app` terminates, it is reported but no other
|
||||
applications are terminated (the default).
|
||||
|
||||
Note that it is always possible to stop an application explicitly by calling
|
||||
`stop/1`. Regardless of the type of the application, no other applications will
|
||||
be affected.
|
||||
|
||||
Note also that the `:transient` type is of little practical use, since when a
|
||||
supervision tree terminates, the reason is set to `:shutdown`, not `:normal`.
|
||||
"""
|
||||
@spec start(app, start_type) :: :ok | {:error, term}
|
||||
def start(app, type \\ :temporary) when is_atom(app) do
|
||||
:application.start(app, type)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the given `app`.
|
||||
|
||||
When stopped, the application is still loaded.
|
||||
"""
|
||||
@spec stop(app) :: :ok | {:error, term}
|
||||
def stop(app) do
|
||||
:application.stop(app)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Loads the given `app`.
|
||||
|
||||
In order to be loaded, an `.app` file must be in the load paths.
|
||||
All `:included_applications` will also be loaded.
|
||||
|
||||
Loading the application does not start it nor load its modules, but
|
||||
it does load its environment.
|
||||
"""
|
||||
@spec load(app) :: :ok | {:error, term}
|
||||
def load(app) when is_atom(app) do
|
||||
:application.load(app)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Unloads the given `app`.
|
||||
|
||||
It will also unload all `:included_applications`.
|
||||
Note that the function does not purge the application modules.
|
||||
"""
|
||||
@spec unload(app) :: :ok | {:error, term}
|
||||
def unload(app) when is_atom(app) do
|
||||
:application.unload(app)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the directory for app.
|
||||
|
||||
This information is returned based on the code path. Here is an
|
||||
example:
|
||||
|
||||
File.mkdir_p!("foo/ebin")
|
||||
Code.prepend_path("foo/ebin")
|
||||
Application.app_dir(:foo)
|
||||
#=> "foo"
|
||||
|
||||
Even though the directory is empty and there is no `.app` file
|
||||
it is considered the application directory based on the name
|
||||
"foo/ebin". The name may contain a dash `-` which is considered
|
||||
to be the app version and it is removed for the lookup purposes:
|
||||
|
||||
File.mkdir_p!("bar-123/ebin")
|
||||
Code.prepend_path("bar-123/ebin")
|
||||
Application.app_dir(:bar)
|
||||
#=> "bar-123"
|
||||
|
||||
For more information on code paths, check the `Code` module in
|
||||
Elixir and also Erlang's `:code` module.
|
||||
"""
|
||||
@spec app_dir(app) :: String.t
|
||||
def app_dir(app) when is_atom(app) do
|
||||
case :code.lib_dir(app) do
|
||||
lib when is_list(lib) -> IO.chardata_to_string(lib)
|
||||
{:error, :bad_name} -> raise ArgumentError, "unknown application: #{inspect app}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the given path inside `app_dir/1`.
|
||||
"""
|
||||
@spec app_dir(app, String.t) :: String.t
|
||||
def app_dir(app, path) when is_binary(path) do
|
||||
Path.join(app_dir(app), path)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list with information about the applications which are currently running.
|
||||
"""
|
||||
@spec started_applications(timeout) :: [tuple]
|
||||
def started_applications(timeout \\ 5000) do
|
||||
:application.which_applications(timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list with information about the applications which have been loaded.
|
||||
"""
|
||||
@spec loaded_applications :: [tuple]
|
||||
def loaded_applications do
|
||||
:application.loaded_applications
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats the error reason returned by `start/2`,
|
||||
`ensure_started/2`, `stop/1`, `load/1` and `unload/1`,
|
||||
returns a string.
|
||||
"""
|
||||
@spec format_error(any) :: String.t
|
||||
def format_error(reason) do
|
||||
try do
|
||||
impl_format_error(reason)
|
||||
catch
|
||||
# A user could create an error that looks like a builtin one
|
||||
# causing an error.
|
||||
:error, _ ->
|
||||
inspect(reason)
|
||||
end
|
||||
end
|
||||
|
||||
# exit(:normal) call is special cased, undo the special case.
|
||||
defp impl_format_error({{:EXIT, :normal}, {mod, :start, args}}) do
|
||||
Exception.format_exit({:normal, {mod, :start, args}})
|
||||
end
|
||||
|
||||
# {:error, reason} return value
|
||||
defp impl_format_error({reason, {mod, :start, args}}) do
|
||||
Exception.format_mfa(mod, :start, args) <> " returned an error: " <>
|
||||
Exception.format_exit(reason)
|
||||
end
|
||||
|
||||
# error or exit(reason) call, use exit reason as reason.
|
||||
defp impl_format_error({:bad_return, {{mod, :start, args}, {:EXIT, reason}}}) do
|
||||
Exception.format_exit({reason, {mod, :start, args}})
|
||||
end
|
||||
|
||||
# bad return value
|
||||
defp impl_format_error({:bad_return, {{mod, :start, args}, return}}) do
|
||||
Exception.format_mfa(mod, :start, args) <>
|
||||
" returned a bad value: " <> inspect(return)
|
||||
end
|
||||
|
||||
defp impl_format_error({:already_started, app}) when is_atom(app) do
|
||||
"already started application #{app}"
|
||||
end
|
||||
|
||||
defp impl_format_error({:not_started, app}) when is_atom(app) do
|
||||
"not started application #{app}"
|
||||
end
|
||||
|
||||
defp impl_format_error({:bad_application, app}) do
|
||||
"bad application: #{inspect(app)}"
|
||||
end
|
||||
|
||||
defp impl_format_error({:already_loaded, app}) when is_atom(app) do
|
||||
"already loaded application #{app}"
|
||||
end
|
||||
|
||||
defp impl_format_error({:not_loaded, app}) when is_atom(app) do
|
||||
"not loaded application #{app}"
|
||||
end
|
||||
|
||||
defp impl_format_error({:invalid_restart_type, restart}) do
|
||||
"invalid application restart type: #{inspect(restart)}"
|
||||
end
|
||||
|
||||
defp impl_format_error({:invalid_name, name}) do
|
||||
"invalid application name: #{inspect(name)}"
|
||||
end
|
||||
|
||||
defp impl_format_error({:invalid_options, opts}) do
|
||||
"invalid application options: #{inspect(opts)}"
|
||||
end
|
||||
|
||||
defp impl_format_error({:badstartspec, spec}) do
|
||||
"bad application start specs: #{inspect(spec)}"
|
||||
end
|
||||
|
||||
defp impl_format_error({'no such file or directory', file}) do
|
||||
"could not find application file: #{file}"
|
||||
end
|
||||
|
||||
defp impl_format_error(reason) do
|
||||
Exception.format_exit(reason)
|
||||
end
|
||||
end
|
||||
@@ -1,39 +0,0 @@
|
||||
defmodule Atom do
|
||||
@moduledoc """
|
||||
Convenience functions for working with atoms.
|
||||
|
||||
See also `Kernel.is_atom/1`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Converts an atom to a string.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Atom.to_string(:foo)
|
||||
"foo"
|
||||
|
||||
"""
|
||||
@spec to_string(atom) :: String.t
|
||||
def to_string(atom) do
|
||||
:erlang.atom_to_binary(atom, :utf8)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts an atom to a char list.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Atom.to_char_list(:"An atom")
|
||||
'An atom'
|
||||
|
||||
"""
|
||||
@spec to_char_list(atom) :: char_list
|
||||
def to_char_list(atom) do
|
||||
:erlang.atom_to_list(atom)
|
||||
end
|
||||
end
|
||||
@@ -1,854 +0,0 @@
|
||||
defmodule Base do
|
||||
import Bitwise
|
||||
|
||||
@moduledoc """
|
||||
This module provides data encoding and decoding functions
|
||||
according to [RFC 4648](http://tools.ietf.org/html/rfc4648).
|
||||
|
||||
This document defines the commonly used base 16, base 32, and base
|
||||
64 encoding schemes.
|
||||
|
||||
## Base 16 alphabet
|
||||
|
||||
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|
||||
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
|
||||
| 0| 0| 4| 4| 8| 8| 12| C|
|
||||
| 1| 1| 5| 5| 9| 9| 13| D|
|
||||
| 2| 2| 6| 6| 10| A| 14| E|
|
||||
| 3| 3| 7| 7| 11| B| 15| F|
|
||||
|
||||
## Base 32 alphabet
|
||||
|
||||
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|
||||
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
|
||||
| 0| A| 9| J| 18| S| 27| 3|
|
||||
| 1| B| 10| K| 19| T| 28| 4|
|
||||
| 2| C| 11| L| 20| U| 29| 5|
|
||||
| 3| D| 12| M| 21| V| 30| 6|
|
||||
| 4| E| 13| N| 22| W| 31| 7|
|
||||
| 5| F| 14| O| 23| X| | |
|
||||
| 6| G| 15| P| 24| Y| (pad)| =|
|
||||
| 7| H| 16| Q| 25| Z| | |
|
||||
| 8| I| 17| R| 26| 2| | |
|
||||
|
||||
|
||||
## Base 32 (extended hex) alphabet
|
||||
|
||||
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|
||||
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
|
||||
| 0| 0| 9| 9| 18| I| 27| R|
|
||||
| 1| 1| 10| A| 19| J| 28| S|
|
||||
| 2| 2| 11| B| 20| K| 29| T|
|
||||
| 3| 3| 12| C| 21| L| 30| U|
|
||||
| 4| 4| 13| D| 22| M| 31| V|
|
||||
| 5| 5| 14| E| 23| N| | |
|
||||
| 6| 6| 15| F| 24| O| (pad)| =|
|
||||
| 7| 7| 16| G| 25| P| | |
|
||||
| 8| 8| 17| H| 26| Q| | |
|
||||
|
||||
## Base 64 alphabet
|
||||
|
||||
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|
||||
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
|
||||
| 0| A| 17| R| 34| i| 51| z|
|
||||
| 1| B| 18| S| 35| j| 52| 0|
|
||||
| 2| C| 19| T| 36| k| 53| 1|
|
||||
| 3| D| 20| U| 37| l| 54| 2|
|
||||
| 4| E| 21| V| 38| m| 55| 3|
|
||||
| 5| F| 22| W| 39| n| 56| 4|
|
||||
| 6| G| 23| X| 40| o| 57| 5|
|
||||
| 7| H| 24| Y| 41| p| 58| 6|
|
||||
| 8| I| 25| Z| 42| q| 59| 7|
|
||||
| 9| J| 26| a| 43| r| 60| 8|
|
||||
| 10| K| 27| b| 44| s| 61| 9|
|
||||
| 11| L| 28| c| 45| t| 62| +|
|
||||
| 12| M| 29| d| 46| u| 63| /|
|
||||
| 13| N| 30| e| 47| v| | |
|
||||
| 14| O| 31| f| 48| w| (pad)| =|
|
||||
| 15| P| 32| g| 49| x| | |
|
||||
| 16| Q| 33| h| 50| y| | |
|
||||
|
||||
## Base 64 (URL and filename safe) alphabet
|
||||
|
||||
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|
||||
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
|
||||
| 0| A| 17| R| 34| i| 51| z|
|
||||
| 1| B| 18| S| 35| j| 52| 0|
|
||||
| 2| C| 19| T| 36| k| 53| 1|
|
||||
| 3| D| 20| U| 37| l| 54| 2|
|
||||
| 4| E| 21| V| 38| m| 55| 3|
|
||||
| 5| F| 22| W| 39| n| 56| 4|
|
||||
| 6| G| 23| X| 40| o| 57| 5|
|
||||
| 7| H| 24| Y| 41| p| 58| 6|
|
||||
| 8| I| 25| Z| 42| q| 59| 7|
|
||||
| 9| J| 26| a| 43| r| 60| 8|
|
||||
| 10| K| 27| b| 44| s| 61| 9|
|
||||
| 11| L| 28| c| 45| t| 62| -|
|
||||
| 12| M| 29| d| 46| u| 63| _|
|
||||
| 13| N| 30| e| 47| v| | |
|
||||
| 14| O| 31| f| 48| w| (pad)| =|
|
||||
| 15| P| 32| g| 49| x| | |
|
||||
| 16| Q| 33| h| 50| y| | |
|
||||
|
||||
"""
|
||||
|
||||
b16_alphabet = Enum.with_index '0123456789ABCDEF'
|
||||
b64_alphabet = Enum.with_index 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
||||
b64url_alphabet = Enum.with_index 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
|
||||
b32_alphabet = Enum.with_index 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
|
||||
b32hex_alphabet = Enum.with_index '0123456789ABCDEFGHIJKLMNOPQRSTUV'
|
||||
|
||||
Enum.each [{:enc16, :dec16, b16_alphabet},
|
||||
{:enc32, :dec32, b32_alphabet},
|
||||
{:enc64, :dec64, b64_alphabet},
|
||||
{:enc64url, :dec64url, b64url_alphabet},
|
||||
{:enc32hex, :dec32hex, b32hex_alphabet}], fn({enc, dec, alphabet}) ->
|
||||
for {encoding, value} <- alphabet do
|
||||
defp unquote(enc)(unquote(value)), do: unquote(encoding)
|
||||
defp unquote(dec)(unquote(encoding)), do: unquote(value)
|
||||
end
|
||||
defp unquote(dec)(c) do
|
||||
raise ArgumentError, "non-alphabet digit found: #{inspect <<c>>, binaries: :as_strings} (byte #{c})"
|
||||
end
|
||||
end
|
||||
|
||||
@compile {:inline, from_upper: 1, from_lower: 1, from_mixed: 1,
|
||||
to_lower: 1, to_upper: 1, enc16: 1, dec16: 1,
|
||||
enc32: 1, dec32: 1, enc32hex: 1, dec32hex: 1,
|
||||
enc64: 1, dec64: 1, enc64url: 1, dec64url: 1}
|
||||
|
||||
defp to_lower(char) when char in ?A..?Z,
|
||||
do: char + (?a - ?A)
|
||||
defp to_lower(char),
|
||||
do: char
|
||||
|
||||
defp to_upper(char), do: char
|
||||
|
||||
defp from_upper(char), do: char
|
||||
|
||||
defp from_lower(char) when char in ?a..?z,
|
||||
do: char - (?a - ?A)
|
||||
defp from_lower(char) when not char in ?A..?Z,
|
||||
do: char
|
||||
defp from_lower(char),
|
||||
do: raise(ArgumentError, "non-alphabet digit found: \"#{<<char>>}\" (byte #{char})")
|
||||
|
||||
defp from_mixed(char) when char in ?a..?z,
|
||||
do: char - (?a - ?A)
|
||||
defp from_mixed(char),
|
||||
do: char
|
||||
|
||||
defp maybe_pad(subject, false, _, _),
|
||||
do: subject
|
||||
defp maybe_pad(subject, _, group_size, pad) do
|
||||
case rem(byte_size(subject), group_size) do
|
||||
0 -> subject
|
||||
x -> subject <> String.duplicate(pad, group_size - x)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 16 encoded string.
|
||||
|
||||
Accepts an atom `:upper` (default) for encoding to upper case characters or
|
||||
`:lower` for lower case characters.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.encode16("foobar")
|
||||
"666F6F626172"
|
||||
|
||||
iex> Base.encode16("foobar", case: :lower)
|
||||
"666f6f626172"
|
||||
|
||||
"""
|
||||
@spec encode16(binary) :: binary
|
||||
@spec encode16(binary, Keyword.t) :: binary
|
||||
def encode16(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
do_encode16(case, data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 16 encoded string into a binary string.
|
||||
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.decode16("666F6F626172")
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode16("666f6f626172", case: :lower)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode16("666f6F626172", case: :mixed)
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec decode16(binary) :: {:ok, binary} | :error
|
||||
@spec decode16(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def decode16(string, opts \\ []) do
|
||||
{:ok, decode16!(string, opts)}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 16 encoded string into a binary string.
|
||||
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.decode16!("666F6F626172")
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode16!("666f6f626172", case: :lower)
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode16!("666f6F626172", case: :mixed)
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec decode16!(binary) :: binary
|
||||
@spec decode16!(binary, Keyword.t) :: binary
|
||||
def decode16!(string, opts \\ [])
|
||||
|
||||
def decode16!(string, opts) when is_binary(string) and rem(byte_size(string), 2) == 0 do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
do_decode16(case, string)
|
||||
end
|
||||
|
||||
def decode16!(string, _opts) when is_binary(string) do
|
||||
raise ArgumentError, "odd-length string"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 64 encoded string.
|
||||
|
||||
Accepts `padding: false` option which will omit padding from
|
||||
the output string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.encode64("foobar")
|
||||
"Zm9vYmFy"
|
||||
|
||||
iex> Base.encode64("foob")
|
||||
"Zm9vYg=="
|
||||
|
||||
iex> Base.encode64("foob", padding: false)
|
||||
"Zm9vYg"
|
||||
|
||||
"""
|
||||
@spec encode64(binary) :: binary
|
||||
@spec encode64(binary, Keyword.t) :: binary
|
||||
def encode64(data, opts \\ []) when is_binary(data) do
|
||||
pad_flag = Keyword.get(opts, :padding, true)
|
||||
do_encode64(data, pad_flag)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 64 encoded string into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.decode64("Zm9vYmFy")
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode64("Zm9vYmFy\\n", ignore: :whitespace)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode64("Zm9vYg==")
|
||||
{:ok, "foob"}
|
||||
|
||||
iex> Base.decode64("Zm9vYg", padding: false)
|
||||
{:ok, "foob"}
|
||||
|
||||
"""
|
||||
@spec decode64(binary) :: {:ok, binary} | :error
|
||||
@spec decode64(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def decode64(string, opts \\ []) when is_binary(string) do
|
||||
{:ok, decode64!(string, opts)}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 64 encoded string into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.decode64!("Zm9vYmFy")
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode64!("Zm9vYmFy\\n", ignore: :whitespace)
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode64!("Zm9vYg==")
|
||||
"foob"
|
||||
|
||||
iex> Base.decode64!("Zm9vYg", padding: false)
|
||||
"foob"
|
||||
|
||||
"""
|
||||
@spec decode64!(binary) :: binary
|
||||
@spec decode64!(binary, Keyword.t) :: binary
|
||||
def decode64!(string, opts \\ []) when is_binary(string) do
|
||||
pad_flag = Keyword.get(opts, :padding, true)
|
||||
string |> filter_ignored(opts[:ignore]) |> do_decode64(pad_flag)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 64 encoded string with URL and filename
|
||||
safe alphabet.
|
||||
|
||||
Accepts `padding: false` option which will omit padding from
|
||||
the output string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.url_encode64(<<255, 127, 254, 252>>)
|
||||
"_3_-_A=="
|
||||
|
||||
iex> Base.url_encode64(<<255, 127, 254, 252>>, padding: false)
|
||||
"_3_-_A"
|
||||
|
||||
"""
|
||||
@spec url_encode64(binary) :: binary
|
||||
@spec url_encode64(binary, Keyword.t) :: binary
|
||||
def url_encode64(data, opts \\ []) when is_binary(data) do
|
||||
pad_flag = Keyword.get(opts, :padding, true)
|
||||
do_encode64url(data, pad_flag)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 64 encoded string with URL and filename safe alphabet
|
||||
into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.url_decode64("_3_-_A==")
|
||||
{:ok, <<255, 127, 254, 252>>}
|
||||
|
||||
iex> Base.url_decode64("_3_-_A==\\n", ignore: :whitespace)
|
||||
{:ok, <<255, 127, 254, 252>>}
|
||||
|
||||
iex> Base.url_decode64("_3_-_A", padding: false)
|
||||
{:ok, <<255, 127, 254, 252>>}
|
||||
|
||||
"""
|
||||
@spec url_decode64(binary) :: {:ok, binary} | :error
|
||||
@spec url_decode64(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def url_decode64(string, opts \\ []) when is_binary(string) do
|
||||
{:ok, url_decode64!(string, opts)}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 64 encoded string with URL and filename safe alphabet
|
||||
into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.url_decode64!("_3_-_A==")
|
||||
<<255, 127, 254, 252>>
|
||||
|
||||
iex> Base.url_decode64!("_3_-_A==\\n", ignore: :whitespace)
|
||||
<<255, 127, 254, 252>>
|
||||
|
||||
iex> Base.url_decode64!("_3_-_A", padding: false)
|
||||
<<255, 127, 254, 252>>
|
||||
|
||||
"""
|
||||
@spec url_decode64!(binary) :: binary
|
||||
@spec url_decode64!(binary, Keyword.t) :: binary
|
||||
def url_decode64!(string, opts \\ []) when is_binary(string) do
|
||||
pad_flag = Keyword.get(opts, :padding, true)
|
||||
string |> filter_ignored(opts[:ignore]) |> do_decode64url(pad_flag)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 32 encoded string.
|
||||
|
||||
Accepts an atom `:upper` (default) for encoding to upper case characters or
|
||||
`:lower` for lower case characters.
|
||||
|
||||
Accepts `padding: false` option which will omit padding from
|
||||
the output string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.encode32("foobar")
|
||||
"MZXW6YTBOI======"
|
||||
|
||||
iex> Base.encode32("foobar", case: :lower)
|
||||
"mzxw6ytboi======"
|
||||
|
||||
iex> Base.encode32("foobar", padding: false)
|
||||
"MZXW6YTBOI"
|
||||
|
||||
"""
|
||||
@spec encode32(binary) :: binary
|
||||
@spec encode32(binary, Keyword.t) :: binary
|
||||
def encode32(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad_flag = Keyword.get(opts, :padding, true)
|
||||
do_encode32(case, data, pad_flag)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 32 encoded string into a binary string.
|
||||
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.decode32("MZXW6YTBOI======")
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode32("mzxw6ytboi======", case: :lower)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode32("mzXW6ytBOi======", case: :mixed)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode32("MZXW6YTBOI", padding: false)
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec decode32(binary) :: {:ok, binary} | :error
|
||||
@spec decode32(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def decode32(string, opts \\ []) do
|
||||
{:ok, decode32!(string, opts)}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 32 encoded string into a binary string.
|
||||
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.decode32!("MZXW6YTBOI======")
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode32!("mzxw6ytboi======", case: :lower)
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode32!("mzXW6ytBOi======", case: :mixed)
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode32!("MZXW6YTBOI", padding: false)
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec decode32!(binary) :: binary
|
||||
@spec decode32!(binary, Keyword.t) :: binary
|
||||
def decode32!(string, opts \\ []) when is_binary(string) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad_flag = Keyword.get(opts, :padding, true)
|
||||
do_decode32(case, string, pad_flag)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 32 encoded string with an
|
||||
extended hexadecimal alphabet.
|
||||
|
||||
Accepts an atom `:upper` (default) for encoding to upper case characters or
|
||||
`:lower` for lower case characters.
|
||||
|
||||
Accepts `padding: false` option which will omit padding from
|
||||
the output string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.hex_encode32("foobar")
|
||||
"CPNMUOJ1E8======"
|
||||
|
||||
iex> Base.hex_encode32("foobar", case: :lower)
|
||||
"cpnmuoj1e8======"
|
||||
|
||||
iex> Base.hex_encode32("foobar", padding: false)
|
||||
"CPNMUOJ1E8"
|
||||
|
||||
"""
|
||||
@spec hex_encode32(binary) :: binary
|
||||
@spec hex_encode32(binary, Keyword.t) :: binary
|
||||
def hex_encode32(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad_flag = Keyword.get(opts, :padding, true)
|
||||
do_hex_encode32(case, data, pad_flag)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 32 encoded string with extended hexadecimal alphabet
|
||||
into a binary string.
|
||||
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.hex_decode32("CPNMUOJ1E8======")
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.hex_decode32("cpnmuoj1e8======", case: :lower)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.hex_decode32("cpnMuOJ1E8======", case: :mixed)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.hex_decode32("CPNMUOJ1E8", padding: false)
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec hex_decode32(binary) :: {:ok, binary} | :error
|
||||
@spec hex_decode32(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def hex_decode32(string, opts \\ []) do
|
||||
{:ok, hex_decode32!(string, opts)}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 32 encoded string with extended hexadecimal alphabet
|
||||
into a binary string.
|
||||
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.hex_decode32!("CPNMUOJ1E8======")
|
||||
"foobar"
|
||||
|
||||
iex> Base.hex_decode32!("cpnmuoj1e8======", case: :lower)
|
||||
"foobar"
|
||||
|
||||
iex> Base.hex_decode32!("cpnMuOJ1E8======", case: :mixed)
|
||||
"foobar"
|
||||
|
||||
iex> Base.hex_decode32!("CPNMUOJ1E8", padding: false)
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec hex_decode32!(binary) :: binary
|
||||
@spec hex_decode32!(binary, Keyword.t) :: binary
|
||||
def hex_decode32!(string, opts \\ []) when is_binary(string) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad_flag = Keyword.get(opts, :padding, true)
|
||||
do_hex_decode32(case, string, pad_flag)
|
||||
end
|
||||
|
||||
defp filter_ignored(string, nil), do: string
|
||||
defp filter_ignored(string, :whitespace) do
|
||||
for <<c::8 <- string>>, not c in '\s\t\r\n', into: <<>>, do: <<c::8>>
|
||||
end
|
||||
|
||||
defp do_encode16(_, <<>>), do: <<>>
|
||||
defp do_encode16(:upper, data) do
|
||||
for <<c::4 <- data>>, into: <<>>, do: <<enc16(c)::8>>
|
||||
end
|
||||
defp do_encode16(:lower, data) do
|
||||
for <<c::4 <- data>>, into: <<>>, do: <<to_lower(enc16(c))::8>>
|
||||
end
|
||||
|
||||
defp do_decode16(_, <<>>), do: <<>>
|
||||
defp do_decode16(:upper, string) when rem(byte_size(string), 2) == 0 do
|
||||
for <<c1::8, c2::8 <- string>>, into: <<>> do
|
||||
<<dec16(c1)::4, dec16(c2)::4>>
|
||||
end
|
||||
end
|
||||
defp do_decode16(:lower, string) when rem(byte_size(string), 2) == 0 do
|
||||
for <<c1::8, c2::8 <- string>>, into: <<>> do
|
||||
<<dec16(from_lower(c1))::4, dec16(from_lower(c2))::4>>
|
||||
end
|
||||
end
|
||||
defp do_decode16(:mixed, string) when rem(byte_size(string), 2) == 0 do
|
||||
for <<c1::8, c2::8 <- string>>, into: <<>> do
|
||||
<<dec16(from_mixed(c1))::4, dec16(from_mixed(c2))::4>>
|
||||
end
|
||||
end
|
||||
|
||||
defp do_encode64(<<>>, _), do: <<>>
|
||||
defp do_encode64(data, pad_flag) do
|
||||
split = 3 * div(byte_size(data), 3)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::6 <- main>>, into: <<>>, do: <<enc64(c)::8>>
|
||||
tail = case rest do
|
||||
<<c1::6, c2::6, c3::4>> ->
|
||||
<<enc64(c1)::8, enc64(c2)::8, enc64(bsl(c3, 2))::8>>
|
||||
<<c1::6, c2::2>> ->
|
||||
<<enc64(c1)::8, enc64(bsl(c2, 4))::8>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> maybe_pad(tail, pad_flag, 4, "=")
|
||||
end
|
||||
|
||||
defp do_decode64(<<>>, _), do: <<>>
|
||||
defp do_decode64(string, false) do
|
||||
maybe_pad(string, true, 4, "=") |> do_decode64(true)
|
||||
end
|
||||
defp do_decode64(string, _pad_flag) when rem(byte_size(string), 4) == 0 do
|
||||
split = byte_size(string) - 4
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec64(c)::6>>
|
||||
tail = case rest do
|
||||
<<c1::8, c2::8, ?=, ?=>> ->
|
||||
<<dec64(c1)::6, bsr(dec64(c2), 4)::2>>
|
||||
<<c1::8, c2::8, c3::8, ?=>> ->
|
||||
<<dec64(c1)::6, dec64(c2)::6, bsr(dec64(c3), 2)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8>> ->
|
||||
<<dec64(c1)::6, dec64(c2)::6, dec64(c3)::6, dec64(c4)::6>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
defp do_decode64(_, _) do
|
||||
raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
defp do_encode64url(<<>>, _), do: <<>>
|
||||
defp do_encode64url(data, pad_flag) do
|
||||
split = 3 * div(byte_size(data), 3)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::6 <- main>>, into: <<>>, do: <<enc64url(c)::8>>
|
||||
tail = case rest do
|
||||
<<c1::6, c2::6, c3::4>> ->
|
||||
<<enc64url(c1)::8, enc64url(c2)::8, enc64url(bsl(c3, 2))::8>>
|
||||
<<c1::6, c2::2>> ->
|
||||
<<enc64url(c1)::8, enc64url(bsl(c2, 4))::8>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> maybe_pad(tail, pad_flag, 4, "=")
|
||||
end
|
||||
|
||||
defp do_decode64url(<<>>, _), do: <<>>
|
||||
defp do_decode64url(string, false) do
|
||||
maybe_pad(string, true, 4, "=") |> do_decode64url(true)
|
||||
end
|
||||
defp do_decode64url(string, _pad_flag) when rem(byte_size(string), 4) == 0 do
|
||||
split = byte_size(string) - 4
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec64url(c)::6>>
|
||||
tail = case rest do
|
||||
<<c1::8, c2::8, ?=, ?=>> ->
|
||||
<<dec64url(c1)::6, bsr(dec64url(c2), 4)::2>>
|
||||
<<c1::8, c2::8, c3::8, ?=>> ->
|
||||
<<dec64url(c1)::6, dec64url(c2)::6, bsr(dec64url(c3), 2)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8>> ->
|
||||
<<dec64url(c1)::6, dec64url(c2)::6, dec64url(c3)::6, dec64url(c4)::6>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
defp do_decode64url(_, _) do
|
||||
raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
defp do_encode32(_, <<>>, _), do: <<>>
|
||||
|
||||
for {case, fun} <- [upper: :to_upper, lower: :to_lower] do
|
||||
defp do_encode32(unquote(case), data, pad_flag) do
|
||||
split = 5 * div(byte_size(data), 5)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::5 <- main>>, into: <<>>, do: <<unquote(fun)(enc32(c))::8>>
|
||||
tail = case rest do
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::5, c6::5, c7::2>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
unquote(fun)(enc32(c3))::8, unquote(fun)(enc32(c4))::8,
|
||||
unquote(fun)(enc32(c5))::8, unquote(fun)(enc32(c6))::8,
|
||||
unquote(fun)(enc32(bsl(c7, 3)))::8>>
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::4>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
unquote(fun)(enc32(c3))::8, unquote(fun)(enc32(c4))::8,
|
||||
unquote(fun)(enc32(bsl(c5, 1)))::8>>
|
||||
<<c1::5, c2::5, c3::5, c4::1>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
unquote(fun)(enc32(c3))::8, unquote(fun)(enc32(bsl(c4, 4)))::8>>
|
||||
<<c1::5, c2::3>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(bsl(c2, 2)))::8>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> maybe_pad(tail, pad_flag, 8, "=")
|
||||
end
|
||||
end
|
||||
|
||||
defp do_decode32(_, <<>>, _), do: <<>>
|
||||
defp do_decode32(case, string, false),
|
||||
do: do_decode32(case, maybe_pad(string, true, 8, "="), true)
|
||||
|
||||
for {case, fun} <- [upper: :from_upper, lower: :from_lower, mixed: :from_mixed] do
|
||||
defp do_decode32(unquote(case), string, _pad_flag) when rem(byte_size(string), 8) == 0 do
|
||||
split = byte_size(string) - 8
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec32(unquote(fun)(c))::5>>
|
||||
tail = case rest do
|
||||
<<c1::8, c2::8, ?=, ?=, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, bsr(dec32(unquote(fun)(c2)), 2)::3>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, bsr(dec32(unquote(fun)(c4)), 4)::1>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, ?=, ?=, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, dec32(unquote(fun)(c4))::5,
|
||||
bsr(dec32(unquote(fun)(c5)), 1)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, dec32(unquote(fun)(c4))::5,
|
||||
dec32(unquote(fun)(c5))::5, dec32(unquote(fun)(c6))::5,
|
||||
bsr(dec32(unquote(fun)(c7)), 3)::2>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, c8::8>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, dec32(unquote(fun)(c4))::5,
|
||||
dec32(unquote(fun)(c5))::5, dec32(unquote(fun)(c6))::5,
|
||||
dec32(unquote(fun)(c7))::5, dec32(unquote(fun)(c8))::5>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
end
|
||||
|
||||
defp do_decode32(_, _, _),
|
||||
do: raise ArgumentError, "incorrect padding"
|
||||
|
||||
defp do_hex_encode32(_, <<>>, _), do: <<>>
|
||||
|
||||
for {case, fun} <- [upper: :to_upper, lower: :to_lower] do
|
||||
defp do_hex_encode32(unquote(case), data, pad_flag) do
|
||||
split = 5 * div(byte_size(data), 5)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::5 <- main>>, into: <<>>, do: <<unquote(fun)(enc32hex(c))::8>>
|
||||
tail = case rest do
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::5, c6::5, c7::2>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
unquote(fun)(enc32hex(c3))::8, unquote(fun)(enc32hex(c4))::8,
|
||||
unquote(fun)(enc32hex(c5))::8, unquote(fun)(enc32hex(c6))::8,
|
||||
unquote(fun)(enc32hex(bsl(c7, 3)))::8>>
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::4>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
unquote(fun)(enc32hex(c3))::8, unquote(fun)(enc32hex(c4))::8,
|
||||
unquote(fun)(enc32hex(bsl(c5, 1)))::8>>
|
||||
<<c1::5, c2::5, c3::5, c4::1>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
unquote(fun)(enc32hex(c3))::8, unquote(fun)(enc32hex(bsl(c4, 4)))::8>>
|
||||
<<c1::5, c2::3>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(bsl(c2, 2)))::8>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> maybe_pad(tail, pad_flag, 8, "=")
|
||||
end
|
||||
end
|
||||
|
||||
defp do_hex_decode32(_, <<>>, _), do: <<>>
|
||||
defp do_hex_decode32(case, string, false),
|
||||
do: do_hex_decode32(case, maybe_pad(string, true, 8, "="), true)
|
||||
|
||||
for {case, fun} <- [upper: :from_upper, lower: :from_lower, mixed: :from_mixed] do
|
||||
defp do_hex_decode32(unquote(case), string, _pad_flag) when rem(byte_size(string), 8) == 0 do
|
||||
split = byte_size(string) - 8
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec32hex(unquote(fun)(c))::5>>
|
||||
tail = case rest do
|
||||
<<c1::8, c2::8, ?=, ?=, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, bsr(dec32hex(unquote(fun)(c2)), 2)::3>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, bsr(dec32hex(unquote(fun)(c4)), 4)::1>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, ?=, ?=, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, dec32hex(unquote(fun)(c4))::5,
|
||||
bsr(dec32hex(unquote(fun)(c5)), 1)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, dec32hex(unquote(fun)(c4))::5,
|
||||
dec32hex(unquote(fun)(c5))::5, dec32hex(unquote(fun)(c6))::5,
|
||||
bsr(dec32hex(unquote(fun)(c7)), 3)::2>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, c8::8>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, dec32hex(unquote(fun)(c4))::5,
|
||||
dec32hex(unquote(fun)(c5))::5, dec32hex(unquote(fun)(c6))::5,
|
||||
dec32hex(unquote(fun)(c7))::5, dec32hex(unquote(fun)(c8))::5>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
end
|
||||
|
||||
defp do_hex_decode32(_, _, _),
|
||||
do: raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
@@ -1,100 +0,0 @@
|
||||
defmodule Behaviour do
|
||||
@moduledoc """
|
||||
This module has been deprecated.
|
||||
|
||||
Instead of `defcallback`, one can simply use `@callback`.
|
||||
Instead of `defmacrocallback`, one can simply use `@macrocallback`.
|
||||
Instead of `__behaviour__(:callbacks)`, one can simply use `behaviour_info(:callbacks)`.
|
||||
"""
|
||||
|
||||
# TODO: Deprecate by 1.2
|
||||
# TODO: Remove by 2.0
|
||||
|
||||
@doc """
|
||||
Defines a function callback according to the given type specification.
|
||||
"""
|
||||
defmacro defcallback(spec) do
|
||||
do_defcallback(:def, split_spec(spec, quote(do: term)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Defines a macro callback according to the given type specification.
|
||||
"""
|
||||
defmacro defmacrocallback(spec) do
|
||||
do_defcallback(:defmacro, split_spec(spec, quote(do: Macro.t)))
|
||||
end
|
||||
|
||||
defp split_spec({:when, _, [{:::, _, [spec, return]}, guard]}, _default) do
|
||||
{spec, return, guard}
|
||||
end
|
||||
|
||||
defp split_spec({:when, _, [spec, guard]}, default) do
|
||||
{spec, default, guard}
|
||||
end
|
||||
|
||||
defp split_spec({:::, _, [spec, return]}, _default) do
|
||||
{spec, return, []}
|
||||
end
|
||||
|
||||
defp split_spec(spec, default) do
|
||||
{spec, default, []}
|
||||
end
|
||||
|
||||
defp do_defcallback(kind, {spec, return, guards}) do
|
||||
case Macro.decompose_call(spec) do
|
||||
{name, args} ->
|
||||
do_callback(kind, name, args, return, guards)
|
||||
_ ->
|
||||
raise ArgumentError, "invalid syntax in #{kind}callback #{Macro.to_string(spec)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp do_callback(kind, name, args, return, guards) do
|
||||
:lists.foreach fn
|
||||
{:::, _, [left, right]} ->
|
||||
ensure_not_default(left)
|
||||
ensure_not_default(right)
|
||||
left
|
||||
other ->
|
||||
ensure_not_default(other)
|
||||
other
|
||||
end, args
|
||||
|
||||
spec =
|
||||
quote do
|
||||
unquote(name)(unquote_splicing(args)) :: unquote(return) when unquote(guards)
|
||||
end
|
||||
|
||||
case kind do
|
||||
:def -> quote(do: @callback unquote(spec))
|
||||
:defmacro -> quote(do: @macrocallback unquote(spec))
|
||||
end
|
||||
end
|
||||
|
||||
defp ensure_not_default({:\\, _, [_, _]}) do
|
||||
raise ArgumentError, "default arguments \\\\ not supported in defcallback/defmacrocallback"
|
||||
end
|
||||
|
||||
defp ensure_not_default(_), do: :ok
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
@doc false
|
||||
def __behaviour__(:callbacks) do
|
||||
__MODULE__.behaviour_info(:callbacks)
|
||||
end
|
||||
|
||||
def __behaviour__(:docs) do
|
||||
for {tuple, line, kind, docs} <- Code.get_docs(__MODULE__, :callback_docs) do
|
||||
case kind do
|
||||
:callback -> {tuple, line, :def, docs}
|
||||
:macrocallback -> {tuple, line, :defmacro, docs}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
import unquote(__MODULE__)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,218 @@
|
||||
defmodule Binary do
|
||||
@moduledoc """
|
||||
Functions for working with binaries.
|
||||
"""
|
||||
|
||||
@doc %B"""
|
||||
Receives a char list and escapes all special chars (like \n)
|
||||
and interpolation markers. A last argument is given and wraps
|
||||
the whole char list given.
|
||||
|
||||
## Examples
|
||||
|
||||
Binary.escape "foo", ?'
|
||||
#=> "'foo'"
|
||||
|
||||
"""
|
||||
def escape(other, char) do
|
||||
<<char>> <> do_escape(other, char)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Check if a binary is printable considering it is encoded
|
||||
as UTF-8. Returns true if so, false otherwise.
|
||||
|
||||
## Examples
|
||||
|
||||
Binary.printable?("abc") #=> true
|
||||
|
||||
"""
|
||||
|
||||
# Allow basic ascii chars
|
||||
def printable?(<<c, t|:binary>>) when c in ?\s..?~ do
|
||||
printable?(t)
|
||||
end
|
||||
|
||||
# From 16#A0 to 16#BF
|
||||
def printable?(<<194, c, t|:binary>>) when c in 160..191 do
|
||||
printable?(t)
|
||||
end
|
||||
|
||||
# From 16#C0 to 16#7FF
|
||||
def printable?(<<m, o1, t|:binary>>) when m in 195..223 and o1 in 128..191 do
|
||||
printable?(t)
|
||||
end
|
||||
|
||||
# From 16#800 to 16#CFFF
|
||||
def printable?(<<m, o1, o2, t|:binary>>) when m in 224..236 and
|
||||
o1 >= 128 and o1 < 192 and o2 >= 128 and o2 < 192 do
|
||||
printable?(t)
|
||||
end
|
||||
|
||||
# From 16#D000 to 16#D7FF
|
||||
def printable?(<<237, o1, o2, t|:binary>>) when
|
||||
o1 >= 128 and o1 < 160 and o2 >= 128 and o2 < 192 do
|
||||
printable?(t)
|
||||
end
|
||||
|
||||
# Reject 16#FFFF and 16#FFFE
|
||||
def printable?(<<239, 191, o>>) when o == 190 or o == 191 do
|
||||
false
|
||||
end
|
||||
|
||||
# From 16#E000 to 16#EFFF
|
||||
def printable?(<<m, o1, o2, t|:binary>>) when (m == 238 or m == 239) and
|
||||
o1 in 128..191 and o2 in 128..191 do
|
||||
printable?(t)
|
||||
end
|
||||
|
||||
# From 16#F000 to 16#FFFD
|
||||
def printable?(<<239, o1, o2, t|:binary>>) when
|
||||
o1 in 128..191 and o2 in 128..191 do
|
||||
printable?(t)
|
||||
end
|
||||
|
||||
# From 16#10000 to 16#3FFFF
|
||||
def printable?(<<240, o1, o2, o3, t|:binary>>) when
|
||||
o1 in 144..191 and o2 in 128..191 and o3 in 128..191 do
|
||||
printable?(t)
|
||||
end
|
||||
|
||||
# Reject 16#110000 onwards
|
||||
def printable?(<<244, o1, _, _, _|:binary>>) when o1 >= 144 do
|
||||
false
|
||||
end
|
||||
|
||||
# From 16#4000 to 16#10FFFF
|
||||
def printable?(<<m, o1, o2, o3, t|:binary>>) when m in 241..244 and
|
||||
o1 in 128..191 and o2 in 128..191 and o3 in 128..191 do
|
||||
printable?(t)
|
||||
end
|
||||
|
||||
def printable?(<<?\n, t|:binary>>), do: printable?(t)
|
||||
def printable?(<<?\r, t|:binary>>), do: printable?(t)
|
||||
def printable?(<<?\t, t|:binary>>), do: printable?(t)
|
||||
def printable?(<<?\v, t|:binary>>), do: printable?(t)
|
||||
def printable?(<<?\b, t|:binary>>), do: printable?(t)
|
||||
def printable?(<<?\f, t|:binary>>), do: printable?(t)
|
||||
def printable?(<<?\e, t|:binary>>), do: printable?(t)
|
||||
|
||||
def printable?(<<>>), do: true
|
||||
def printable?(_), do: false
|
||||
|
||||
@doc %B"""
|
||||
Unescape the given chars. The unescaping is driven by the same
|
||||
rules as single- and double-quoted strings. Check `unescape/2`
|
||||
for information on how to customize the escaping map.
|
||||
|
||||
In this setup, Elixir will escape the following: `\b`, `\d`,
|
||||
`\e`, `\f`, `\n`, `\r`, `\s`, `\t` and `\v`. Octals are also
|
||||
escaped according to the latin1 set they represent.
|
||||
|
||||
## Examples
|
||||
|
||||
Binary.unescape "example\\n"
|
||||
#=> "example\n"
|
||||
|
||||
In the example above, we pass a string with `\n` escaped
|
||||
and we return a version with it unescaped.
|
||||
"""
|
||||
def unescape(chars) do
|
||||
Erlang.elixir_interpolation.unescape_chars(chars)
|
||||
end
|
||||
|
||||
@doc %B"""
|
||||
Unescape the given chars according to the map given.
|
||||
Check `unescape/1` if you want to use the same map as Elixir
|
||||
single- and double-quoted strings.
|
||||
|
||||
## Map
|
||||
|
||||
The map must be a function. The function receives an integer
|
||||
representing the number of the characters it wants to unescape.
|
||||
Here is the default mapping function implemented by Elixir:
|
||||
|
||||
def unescape_map(?b), do: ?\b
|
||||
def unescape_map(?d), do: ?\d
|
||||
def unescape_map(?e), do: ?\e
|
||||
def unescape_map(?f), do: ?\f
|
||||
def unescape_map(?n), do: ?\n
|
||||
def unescape_map(?r), do: ?\r
|
||||
def unescape_map(?s), do: ?\s
|
||||
def unescape_map(?t), do: ?\t
|
||||
def unescape_map(?v), do: ?\v
|
||||
def unescape_map(e), do: e
|
||||
|
||||
If the `unescape_map` function returns false. The char is
|
||||
not escaped and `\` is kept in the char list.
|
||||
|
||||
## Octals
|
||||
|
||||
Octals will by default be escaped unless the map function
|
||||
returns false for ?0.
|
||||
|
||||
## Examples
|
||||
|
||||
Using the unescape_map defined above is easy:
|
||||
|
||||
Binary.unescape "example\\n", unescape_map(&1)
|
||||
|
||||
"""
|
||||
def unescape(chars, map) do
|
||||
Erlang.elixir_interpolation.unescape_chars(chars, map)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Unescape the given tokens according to the default map.
|
||||
Check `unescape/1` and `unescape/2` for more information
|
||||
about unescaping. Only tokens that are char lists are
|
||||
unescaped, all others are ignored. This method is useful
|
||||
when implementing your own sigils. Check the implementation
|
||||
of `Kernel.__b__` for examples.
|
||||
"""
|
||||
def unescape_tokens(tokens) do
|
||||
Erlang.elixir_interpolation.unescape_tokens(tokens)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Unescape the given tokens according to the given map.
|
||||
Check `unescape_tokens/1` and `unescaped/2` for more information.
|
||||
"""
|
||||
def unescape_tokens(tokens, map) do
|
||||
Erlang.elixir_interpolation.unescape_tokens(tokens, map)
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp do_escape(<<char, t|:binary>>, char) do
|
||||
<<?\\, char, do_escape(t, char)|:binary>>
|
||||
end
|
||||
|
||||
defp do_escape(<<h, t|:binary>>, char) when
|
||||
h == ?# or h == ?\b or
|
||||
h == ?\d or h == ?\e or
|
||||
h == ?\f or h == ?\n or
|
||||
h == ?\r or h == ?\\ or
|
||||
h == ?\t or h == ?\v do
|
||||
<<?\\, escape_map(h), do_escape(t, char)|:binary>>
|
||||
end
|
||||
|
||||
defp do_escape(<<h, t|:binary>>, char) do
|
||||
<<h, do_escape(t,char)|:binary>>
|
||||
end
|
||||
|
||||
defp do_escape(<<>>, char) do
|
||||
<<char>>
|
||||
end
|
||||
|
||||
defp escape_map(?#), do: ?#
|
||||
defp escape_map(?\b), do: ?b
|
||||
defp escape_map(?\d), do: ?d
|
||||
defp escape_map(?\e), do: ?e
|
||||
defp escape_map(?\f), do: ?f
|
||||
defp escape_map(?\n), do: ?n
|
||||
defp escape_map(?\r), do: ?r
|
||||
defp escape_map(?\\), do: ?\\
|
||||
defp escape_map(?\t), do: ?t
|
||||
defp escape_map(?\v), do: ?v
|
||||
end
|
||||
@@ -0,0 +1,74 @@
|
||||
import Kernel, except: [to_binary: 1]
|
||||
|
||||
defprotocol Binary.Chars do
|
||||
@moduledoc %B"""
|
||||
The Binary.Chars protocol is responsible for
|
||||
converting a structure to a Binary (only if applicable).
|
||||
The only function required to be implemented is
|
||||
`to_binary` which does the conversion.
|
||||
|
||||
The `to_binary` function automatically imported
|
||||
by Kernel invokes this protocol. String
|
||||
interpolation also invokes to_binary in its
|
||||
arguments. For example, `"foo#{bar}"` is the same
|
||||
as `"foo" <> to_binary(bar)`.
|
||||
"""
|
||||
|
||||
@only [BitString, List, Number, Atom, Record]
|
||||
|
||||
def to_binary(thing)
|
||||
end
|
||||
|
||||
defimpl Binary.Chars, for: Atom do
|
||||
@doc """
|
||||
Convert the atom literally to a binary, except
|
||||
`nil` which is converted to an empty string.
|
||||
"""
|
||||
def to_binary(nil) do
|
||||
""
|
||||
end
|
||||
|
||||
def to_binary(atom) do
|
||||
atom_to_binary(atom, :utf8)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Binary.Chars, for: BitString do
|
||||
@doc """
|
||||
Simply returns the binary itself.
|
||||
"""
|
||||
def to_binary(thing) when is_binary(thing) do
|
||||
thing
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Binary.Chars, for: List do
|
||||
@doc """
|
||||
Consider the list is an iolist and converts it
|
||||
to a binary. This allows a list of binaries, or
|
||||
a charlist, or a mix of both, to be converted
|
||||
successfully.
|
||||
|
||||
## Examples
|
||||
|
||||
to_binary 'foo' #=> "foo"
|
||||
to_binary ["foo", 'bar'] #=> "foobar"
|
||||
|
||||
"""
|
||||
def to_binary(thing) do
|
||||
iolist_to_binary(thing)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Binary.Chars, for: Number do
|
||||
@doc """
|
||||
Simply converts the number (integer or a float) to a binary.
|
||||
"""
|
||||
def to_binary(thing) when is_integer(thing) do
|
||||
list_to_binary integer_to_list(thing)
|
||||
end
|
||||
|
||||
def to_binary(thing) do
|
||||
list_to_binary float_to_list(thing)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,301 @@
|
||||
import Kernel, except: [inspect: 1]
|
||||
|
||||
defprotocol Binary.Inspect do
|
||||
@moduledoc """
|
||||
The `Binary.Inspect` protocol is responsible for
|
||||
converting any structure to a Binary for textual
|
||||
representation. All basic data structures (tuple,
|
||||
list, function, pid, etc) implement the inspect
|
||||
protocol. Other structures are adviced to implement
|
||||
the protocol in order to provide pretty printing.
|
||||
"""
|
||||
|
||||
@only [BitString, List, Record, Tuple, Atom, Number, Any]
|
||||
|
||||
def inspect(thing)
|
||||
end
|
||||
|
||||
defimpl Binary.Inspect, for: Atom do
|
||||
require Macro
|
||||
|
||||
@doc """
|
||||
Represents the atom as an Elixir term. The atoms false, true
|
||||
and nil are simply quoted. Modules are properly represented
|
||||
as modules using the dot notation.
|
||||
|
||||
Notice that in Elixir, all operators can be represented using
|
||||
literal atoms (`:+`, `:-`, etc).
|
||||
|
||||
## Examples
|
||||
|
||||
inspect(:foo) #=> ":foo"
|
||||
inspect(nil) #=> "nil"
|
||||
inspect(Foo.Bar) #=> "Foo.Bar"
|
||||
|
||||
"""
|
||||
def inspect(false), do: "false"
|
||||
def inspect(true), do: "true"
|
||||
def inspect(nil), do: "nil"
|
||||
def inspect(:""), do: ":\"\""
|
||||
def inspect(Elixir), do: "Elixir"
|
||||
|
||||
def inspect(atom) do
|
||||
binary = atom_to_binary(atom)
|
||||
|
||||
cond do
|
||||
valid_atom_identifier?(binary) ->
|
||||
":" <> binary
|
||||
valid_ref_identifier?(binary) ->
|
||||
"Elixir-" <> rest = binary
|
||||
bc <<r>> inbits rest, do: <<to_dot(r)>>
|
||||
atom in Macro.binary_ops or atom in Macro.unary_ops ->
|
||||
":" <> binary
|
||||
true ->
|
||||
":" <> Binary.escape(binary, ?")
|
||||
end
|
||||
end
|
||||
|
||||
# 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
|
||||
|
||||
defp valid_ref_identifier?(_), do: false
|
||||
|
||||
defp valid_ref_piece?(<<?-, h, t|:binary>>) when h in ?A..?Z do
|
||||
valid_ref_piece? valid_identifier?(t)
|
||||
end
|
||||
|
||||
defp valid_ref_piece?(<<>>), do: true
|
||||
defp valid_ref_piece?(_), do: false
|
||||
|
||||
# Detect if atom
|
||||
|
||||
defp valid_atom_identifier?(<<h, t|:binary>>) when h in ?a..?z or h in ?A..?Z or h == ?_ do
|
||||
case valid_identifier?(t) do
|
||||
<<>> -> true
|
||||
<<??>> -> true
|
||||
<<?!>> -> true
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
defp valid_atom_identifier?(_), do: false
|
||||
|
||||
defp valid_identifier?(<<h, t|:binary>>)
|
||||
when h in ?a..?z
|
||||
when h in ?A..?Z
|
||||
when h in ?0..?9
|
||||
when h == ?_ do
|
||||
valid_identifier? t
|
||||
end
|
||||
|
||||
defp valid_identifier?(other), do: other
|
||||
end
|
||||
|
||||
defimpl Binary.Inspect, for: BitString do
|
||||
@doc %B"""
|
||||
Represents the string as itself escaping
|
||||
all necessary characters.
|
||||
|
||||
## Examples
|
||||
|
||||
inspect("bar") #=> "bar"
|
||||
inspect("f\"oo") #=> "f\"oo"
|
||||
|
||||
"""
|
||||
def inspect(thing) when is_binary(thing) do
|
||||
if Binary.printable?(thing) do
|
||||
Binary.escape(thing, ?")
|
||||
else
|
||||
as_bitstring(thing)
|
||||
end
|
||||
end
|
||||
|
||||
def inspect(thing) do
|
||||
as_bitstring(thing)
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp as_bitstring(thing) do
|
||||
erlang = Erlang.io_lib.format('~p', [thing])
|
||||
list_to_binary List.reverse(replace(erlang, []))
|
||||
end
|
||||
|
||||
defp replace([?:|t], acc), do: replace(t, [?||acc])
|
||||
defp replace([h|t], acc) when is_list(h), do: replace(t, replace(h, acc))
|
||||
defp replace([h|t], acc), do: replace(t, [h|acc])
|
||||
defp replace([], acc), do: acc
|
||||
end
|
||||
|
||||
defimpl Binary.Inspect, for: List do
|
||||
|
||||
@doc %B"""
|
||||
Represents a list checking if it can be printed or not.
|
||||
If so, a single-quoted representation is returned,
|
||||
otherwise the brackets syntax is used.
|
||||
|
||||
Inspecting a list is conservative as it does not try
|
||||
to guess how the list is encoded. That said, `'josé'`
|
||||
will likely be inspected as `[106,111,115,195,169]`
|
||||
because we can't know if it is encoded in utf-8
|
||||
or iso-5569-1, which is common in Erlang libraries.
|
||||
|
||||
## Examples
|
||||
|
||||
inspect('bar') #=> 'bar'
|
||||
inspect([0|'bar']) #=> "[0,98,97,114]"
|
||||
inspect([:foo,:bar]) #=> "[:foo, :bar]"
|
||||
|
||||
"""
|
||||
|
||||
def inspect([]), do: "[]"
|
||||
|
||||
def inspect(thing) do
|
||||
if printable?(thing) do
|
||||
Binary.escape(list_to_binary(thing), ?')
|
||||
else
|
||||
container_join(thing, "[", "]")
|
||||
end
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
def container_join([h], acc, last) do
|
||||
acc <> Binary.Inspect.inspect(h) <> last
|
||||
end
|
||||
|
||||
def container_join([h|t], acc, last) when is_list(t) do
|
||||
acc = acc <> Binary.Inspect.inspect(h) <> ","
|
||||
container_join(t, acc, last)
|
||||
end
|
||||
|
||||
def container_join([h|t], acc, last) do
|
||||
acc <> Binary.Inspect.inspect(h) <> "|" <> Binary.Inspect.inspect(t) <> last
|
||||
end
|
||||
|
||||
def container_join([], acc, last) do
|
||||
acc <> last
|
||||
end
|
||||
|
||||
## printable?
|
||||
|
||||
defp printable?([c|cs]) when is_integer(c) and c in 32..126 do
|
||||
printable?(cs)
|
||||
end
|
||||
|
||||
defp printable?([c|cs]) when c in [?\n, ?\r, ?\t, ?\v, ?\b, ?\f, ?\e] do
|
||||
printable?(cs)
|
||||
end
|
||||
|
||||
defp printable?([]), do: true
|
||||
defp printable?(_), do: false
|
||||
end
|
||||
|
||||
defimpl Binary.Inspect, for: Tuple do
|
||||
@doc """
|
||||
Inspect tuples. If the tuple represents a record,
|
||||
it shows it nicely formatted using the access syntax.
|
||||
|
||||
## Examples
|
||||
|
||||
inspect({1,2,3}) #=> "{1,2,3}"
|
||||
inspect(ArgumentError.new) #=> ArgumentError[message: "argument error"]
|
||||
|
||||
"""
|
||||
def inspect({}), do: "{}"
|
||||
|
||||
def inspect(exception) when is_exception(exception) do
|
||||
[name,_|tail] = tuple_to_list(exception)
|
||||
[_|fields] = lc { field, _ } inlist name.__record__(:fields), do: field
|
||||
Binary.Inspect.Atom.inspect(name) <> records_join(fields, tail, "[", "]")
|
||||
end
|
||||
|
||||
def inspect(thing) do
|
||||
list = tuple_to_list(thing)
|
||||
[name|tail] = list
|
||||
|
||||
if is_record?(name) do
|
||||
fields = lc { field, _ } inlist name.__record__(:fields), do: field
|
||||
if length(fields) != size(thing) - 1 do
|
||||
Binary.Inspect.List.container_join(list, "{", "}")
|
||||
else
|
||||
Binary.Inspect.Atom.inspect(name) <> records_join(fields, tail, "[", "]")
|
||||
end
|
||||
else
|
||||
Binary.Inspect.List.container_join(list, "{", "}")
|
||||
end
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp is_record?(name) do
|
||||
is_atom(name) and match?("Elixir-" <> _, atom_to_binary(name, :utf8)) and
|
||||
function_exported?(name, :__record__, 1)
|
||||
end
|
||||
|
||||
defp records_join([f], [v], acc, last) do
|
||||
acc <> atom_to_binary(f, :utf8) <> ": " <> Binary.Inspect.inspect(v) <> last
|
||||
end
|
||||
|
||||
defp records_join([fh|ft], [vh|vt], acc, last) do
|
||||
acc = acc <> atom_to_binary(fh, :utf8) <> ": " <> Binary.Inspect.inspect(vh) <> ", "
|
||||
records_join(ft, vt, acc, last)
|
||||
end
|
||||
|
||||
defp records_join([], [], acc, last) do
|
||||
acc <> last
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Binary.Inspect, for: Number do
|
||||
@doc """
|
||||
Represents the number as a binary.
|
||||
|
||||
## Examples
|
||||
|
||||
inspect(1) #=> "1"
|
||||
|
||||
"""
|
||||
def inspect(thing) when is_integer(thing) do
|
||||
list_to_binary integer_to_list(thing)
|
||||
end
|
||||
|
||||
def inspect(thing) do
|
||||
list_to_binary float_to_list(thing)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Binary.Inspect, for: Regex do
|
||||
@doc %B"""
|
||||
Represents the Regex using the `%r""` syntax.
|
||||
|
||||
## Examples
|
||||
|
||||
inspect(%r/foo/m) #=> "%r\"foo\"m"
|
||||
|
||||
"""
|
||||
def inspect(thing) do
|
||||
"%r" <> Binary.Inspect.inspect(Regex.source(thing)) <> Regex.opts(thing)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Binary.Inspect, for: Any do
|
||||
@doc """
|
||||
For all other terms not implemented, we use the default
|
||||
Erlang representation.
|
||||
|
||||
## Examples
|
||||
|
||||
inspect Process.self #=> "<0.35.0>"
|
||||
|
||||
"""
|
||||
def inspect(thing) do
|
||||
iolist_to_binary Erlang.io_lib.format('~p', [thing])
|
||||
end
|
||||
end
|
||||
+45
-126
@@ -1,42 +1,37 @@
|
||||
defmodule Bitwise do
|
||||
@moduledoc """
|
||||
This module provides macro-based operators that perform calculations
|
||||
on (sets of) bits.
|
||||
This module provide macros and operators for bitwise operators.
|
||||
These macros can be used in guards.
|
||||
|
||||
In general, you should `use` the Bitwise module as a whole:
|
||||
The easiest way to use is to simply import them into
|
||||
your module:
|
||||
|
||||
iex> use Bitwise
|
||||
iex> bnot 1
|
||||
-2
|
||||
iex> 1 &&& 1
|
||||
1
|
||||
use Bitwise
|
||||
|
||||
When used, it accepts the following options:
|
||||
bnot 1 #=> -2
|
||||
1 &&& 1 #=> 1
|
||||
|
||||
* `:only_operators` - include only operators
|
||||
* `:skip_operators` - skip operators
|
||||
You can select to include only or skip operators by passing options:
|
||||
|
||||
iex> use Bitwise, only_operators: true
|
||||
iex> 1 &&& 1
|
||||
1
|
||||
|
||||
|
||||
These macros can be used in guards:
|
||||
|
||||
iex> use Bitwise
|
||||
iex> odd? = fn(int) when band(int, 1) == 1 -> true; (_) -> false end
|
||||
iex> odd?.(1)
|
||||
true
|
||||
use Bitwise, only_operators: true
|
||||
1 &&& 1 #=> 1
|
||||
|
||||
"""
|
||||
|
||||
@doc false
|
||||
@doc """
|
||||
Allow a developer to use this module in their programs with
|
||||
the following options:
|
||||
|
||||
* `:only_operators` - Include only operators;
|
||||
* `:skip_operators` - Skip operators;
|
||||
|
||||
"""
|
||||
defmacro __using__(options) do
|
||||
except = cond do
|
||||
Keyword.get(options, :only_operators) ->
|
||||
[bnot: 1, band: 2, bor: 2, bxor: 2, bsl: 2, bsr: 2]
|
||||
[:bnot, :band, :bor, :bxor, :bsl, :bsr]
|
||||
Keyword.get(options, :skip_operators) ->
|
||||
[~~~: 1, &&&: 2, |||: 2, ^^^: 2, <<<: 2, >>>: 2]
|
||||
[:~~~, :&&&, :|||, :^^^, :<<<, :>>>]
|
||||
true -> []
|
||||
end
|
||||
|
||||
@@ -46,162 +41,86 @@ defmodule Bitwise do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise NOT of its argument.
|
||||
|
||||
iex> bnot(2)
|
||||
-3
|
||||
iex> bnot(2) &&& 3
|
||||
1
|
||||
|
||||
Bitwise not.
|
||||
"""
|
||||
defmacro bnot(expr) do
|
||||
quote do: :erlang.bnot(unquote(expr))
|
||||
quote do: __op__ :bnot, unquote(expr)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prefix (unary) operator; calculates the bitwise NOT of its argument.
|
||||
|
||||
iex> ~~~2
|
||||
-3
|
||||
iex> ~~~2 &&& 3
|
||||
1
|
||||
|
||||
Bitwise not as operator.
|
||||
"""
|
||||
defmacro ~~~expr do
|
||||
quote do: :erlang.bnot(unquote(expr))
|
||||
quote do: __op__ :bnot, unquote(expr)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise AND of its arguments.
|
||||
|
||||
iex> band(9, 3)
|
||||
1
|
||||
|
||||
Bitwise and.
|
||||
"""
|
||||
defmacro band(left, right) do
|
||||
quote do: :erlang.band(unquote(left), unquote(right))
|
||||
quote do: __op__ :band, unquote(left), unquote(right)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Infix operator; calculates the bitwise AND of its arguments.
|
||||
|
||||
iex> 9 &&& 3
|
||||
1
|
||||
|
||||
Bitwise and as operator.
|
||||
"""
|
||||
defmacro left &&& right do
|
||||
quote do: :erlang.band(unquote(left), unquote(right))
|
||||
quote do: __op__ :band, unquote(left), unquote(right)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise OR of its arguments.
|
||||
|
||||
iex> bor(9, 3)
|
||||
11
|
||||
|
||||
Bitwise or.
|
||||
"""
|
||||
defmacro bor(left, right) do
|
||||
quote do: :erlang.bor(unquote(left), unquote(right))
|
||||
quote do: __op__ :bor, unquote(left), unquote(right)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Infix operator; calculates the bitwise OR of its arguments.
|
||||
|
||||
iex> 9 ||| 3
|
||||
11
|
||||
|
||||
Bitwise or as operator.
|
||||
"""
|
||||
defmacro left ||| right do
|
||||
quote do: :erlang.bor(unquote(left), unquote(right))
|
||||
quote do: __op__ :bor, unquote(left), unquote(right)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise XOR of its arguments.
|
||||
|
||||
iex> bxor(9, 3)
|
||||
10
|
||||
|
||||
Bitwise xor.
|
||||
"""
|
||||
defmacro bxor(left, right) do
|
||||
quote do: :erlang.bxor(unquote(left), unquote(right))
|
||||
quote do: __op__ :bxor, unquote(left), unquote(right)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Infix operator; calculates the bitwise XOR of its arguments.
|
||||
|
||||
iex> 9 ^^^ 3
|
||||
10
|
||||
|
||||
Bitwise xor as operator.
|
||||
"""
|
||||
defmacro left ^^^ right do
|
||||
quote do: :erlang.bxor(unquote(left), unquote(right))
|
||||
quote do: __op__ :bxor, unquote(left), unquote(right)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the result of an arithmetic left bitshift.
|
||||
|
||||
iex> bsl(1, 2)
|
||||
4
|
||||
iex> bsl(1, -2)
|
||||
0
|
||||
iex> bsl(-1, 2)
|
||||
-4
|
||||
iex> bsl(-1, -2)
|
||||
-1
|
||||
|
||||
Arithmetic bitshift left.
|
||||
"""
|
||||
defmacro bsl(left, right) do
|
||||
quote do: :erlang.bsl(unquote(left), unquote(right))
|
||||
quote do: __op__ :bsl, unquote(left), unquote(right)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Infix operator; calculates the result of an arithmetic left bitshift.
|
||||
|
||||
iex> 1 <<< 2
|
||||
4
|
||||
iex> 1 <<< -2
|
||||
0
|
||||
iex> -1 <<< 2
|
||||
-4
|
||||
iex> -1 <<< -2
|
||||
-1
|
||||
|
||||
Arithmetic bitshift left as operator.
|
||||
"""
|
||||
defmacro left <<< right do
|
||||
quote do: :erlang.bsl(unquote(left), unquote(right))
|
||||
quote do: __op__ :bsl, unquote(left), unquote(right)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the result of an arithmetic right bitshift.
|
||||
|
||||
iex> bsr(1, 2)
|
||||
0
|
||||
iex> bsr(1, -2)
|
||||
4
|
||||
iex> bsr(-1, 2)
|
||||
-1
|
||||
iex> bsr(-1, -2)
|
||||
-4
|
||||
|
||||
Arithmetic bitshift right.
|
||||
"""
|
||||
defmacro bsr(left, right) do
|
||||
quote do: :erlang.bsr(unquote(left), unquote(right))
|
||||
quote do: __op__ :bsr, unquote(left), unquote(right)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Infix operator; calculates the result of an arithmetic right bitshift.
|
||||
|
||||
iex> 1 >>> 2
|
||||
0
|
||||
iex> 1 >>> -2
|
||||
4
|
||||
iex> -1 >>> 2
|
||||
-1
|
||||
iex> -1 >>> -2
|
||||
-4
|
||||
|
||||
Arithmetic bitshift right as operator.
|
||||
"""
|
||||
defmacro left >>> right do
|
||||
quote do: :erlang.bsr(unquote(left), unquote(right))
|
||||
quote do: __op__ :bsr, unquote(left), unquote(right)
|
||||
end
|
||||
end
|
||||
end
|
||||
+182
-533
@@ -1,553 +1,280 @@
|
||||
defmodule Code do
|
||||
@moduledoc """
|
||||
Utilities for managing code compilation, code evaluation and code loading.
|
||||
The Code module is responsible to manage code compilation,
|
||||
code evaluation and code loading.
|
||||
|
||||
It complements (Erlang's code module)[1] to add behavior
|
||||
which is specific to Elixir.
|
||||
|
||||
[1]: (www.erlang.org/doc/man/code.html)
|
||||
|
||||
This module complements Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html)
|
||||
to add behaviour which is specific to Elixir. Almost all of the functions in this module
|
||||
have global side effects on the behaviour of Elixir.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Lists all loaded files.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.require_file("../eex/test/eex_test.exs")
|
||||
List.first(Code.loaded_files) =~ "eex_test.exs" #=> true
|
||||
|
||||
Returns all the loaded files.
|
||||
"""
|
||||
def loaded_files do
|
||||
:elixir_code_server.call :loaded
|
||||
server_call :loaded
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes files from the loaded files list.
|
||||
|
||||
The modules defined in the file are not removed;
|
||||
calling this function only removes them from the list,
|
||||
allowing them to be required again.
|
||||
|
||||
## Examples
|
||||
|
||||
# Load EEx test code, unload file, check for functions still available
|
||||
Code.load_file("../eex/test/eex_test.exs")
|
||||
Code.unload_files(Code.loaded_files)
|
||||
function_exported?(EExTest.Compiled, :before_compile, 0) #=> true
|
||||
|
||||
"""
|
||||
def unload_files(files) do
|
||||
:elixir_code_server.cast {:unload_files, files}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Appends a path to the end of the Erlang VM code path list.
|
||||
|
||||
This is the list of directories the Erlang VM uses for
|
||||
finding module code.
|
||||
|
||||
The path is expanded with `Path.expand/1` before being appended.
|
||||
If this path does not exist, an error is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.append_path(".") #=> true
|
||||
|
||||
Code.append_path("/does_not_exist") #=> {:error, :bad_directory}
|
||||
|
||||
Appends a path to Erlang VM code path.
|
||||
The path is expanded with `File.expand_path` before added.
|
||||
"""
|
||||
def append_path(path) do
|
||||
:code.add_pathz(to_char_list(Path.expand path))
|
||||
Erlang.code.add_pathz(File.expand_path to_char_list(path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prepends a path to the begining of the Erlang VM code path list.
|
||||
|
||||
This is the list of directories the Erlang VM uses for finding
|
||||
module code.
|
||||
|
||||
The path is expanded with `Path.expand/1` before being prepended.
|
||||
If this path does not exist, an error is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.prepend_path(".") #=> true
|
||||
|
||||
Code.prepend_path("/does_not_exist") #=> {:error, :bad_directory}
|
||||
|
||||
Prepends a path to Erlang VM code path.
|
||||
The path is expanded with `File.expand_path` before added.
|
||||
"""
|
||||
def prepend_path(path) do
|
||||
:code.add_patha(to_char_list(Path.expand path))
|
||||
Erlang.code.add_patha(File.expand_path to_char_list(path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a path from the Erlang VM code path list. This is the list of
|
||||
directories the Erlang VM uses for finding module code.
|
||||
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:
|
||||
|
||||
The path is expanded with `Path.expand/1` before being deleted. If the
|
||||
path does not exist it returns `false`.
|
||||
* `:file` - the file to be considered in the evaluation
|
||||
* `:line` - the line the script starts
|
||||
* `:delegate_locals_to` - delegate local calls to the given module,
|
||||
otherwise functions are evaluated inside Erlang's default scope.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.prepend_path(".")
|
||||
Code.delete_path(".") #=> true
|
||||
Code.eval "a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line
|
||||
#=> { 3, [ {:a, 1}, {:b, 2} ] }
|
||||
|
||||
Code.delete_path("/does_not_exist") #=> false
|
||||
When passing the __ENV__'s file and line, we could simply get
|
||||
the location which already returns both fields as a keywords lists:
|
||||
|
||||
Code.eval "a + b", [a: 1, b: 2], __ENV__.location
|
||||
#=> { 3, [ {:a, 1}, {:b, 2} ] }
|
||||
|
||||
"""
|
||||
def delete_path(path) do
|
||||
:code.del_path(to_char_list(Path.expand path))
|
||||
def eval(string, binding // [], opts // []) do
|
||||
{ value, binding, _scope } =
|
||||
Erlang.elixir.eval :unicode.characters_to_list(string), binding, opts
|
||||
{ value, binding }
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates the contents given by `string`.
|
||||
|
||||
The `binding` argument is a keyword list of variable bindings.
|
||||
The `opts` argument is a keyword list of environment options.
|
||||
|
||||
Those options can be:
|
||||
|
||||
* `:file` - the file to be considered in the evaluation
|
||||
* `:line` - the line on which the script starts
|
||||
|
||||
Additionally, the following scope values can be configured:
|
||||
|
||||
* `: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; the list
|
||||
of function names and arity must be sorted
|
||||
|
||||
* `:macros` - a list of tuples where the first element is a module
|
||||
and the second a list of imported macro names and arity; the list
|
||||
of function names and arity must be sorted
|
||||
|
||||
Notice that setting any of the values above overrides Elixir's default
|
||||
values. For example, setting `:requires` to `[]`, will no longer
|
||||
automatically require the `Kernel` module; in the same way setting
|
||||
`:macros` will no longer auto-import `Kernel` macros like `if`, `case`,
|
||||
etc.
|
||||
|
||||
Returns a tuple of the form `{value, binding}`,
|
||||
where `value` is the value returned from evaluating `string`.
|
||||
If an error occurs while evaluating `string` an exception will be raised.
|
||||
|
||||
`binding` is a keyword list with the value of all variable bindings
|
||||
after evaluating `string`. The binding key is usually an atom, but it
|
||||
may be a tuple for variables defined in a different context.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.eval_string("a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
|
||||
{3, [a: 1, b: 2]}
|
||||
|
||||
iex> Code.eval_string("c = a + b", [a: 1, b: 2], __ENV__)
|
||||
{3, [a: 1, b: 2, c: 3]}
|
||||
|
||||
iex> Code.eval_string("a = a + b", [a: 1, b: 2])
|
||||
{3, [a: 3, b: 2]}
|
||||
|
||||
For convenience, you can pass `__ENV__` as the `opts` argument and
|
||||
all imports, requires and aliases defined in the current environment
|
||||
will be automatically carried over:
|
||||
|
||||
iex> Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
|
||||
{3, [a: 1, b: 2]}
|
||||
|
||||
"""
|
||||
def eval_string(string, binding \\ [], opts \\ [])
|
||||
|
||||
def eval_string(string, binding, %Macro.Env{} = env) do
|
||||
{value, binding, _env, _scope} = :elixir.eval to_char_list(string), binding, Map.to_list(env)
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
def eval_string(string, binding, opts) when is_list(opts) do
|
||||
validate_eval_opts(opts)
|
||||
{value, binding, _env, _scope} = :elixir.eval to_char_list(string), binding, opts
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates the quoted contents.
|
||||
|
||||
See `eval_string/3` for a description of arguments and return values.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> contents = quote(do: var!(a) + var!(b))
|
||||
iex> Code.eval_quoted(contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
|
||||
{3, [a: 1, b: 2]}
|
||||
|
||||
For convenience, you can pass `__ENV__` as the `opts` argument and
|
||||
all options will be automatically extracted from the current environment:
|
||||
|
||||
iex> contents = quote(do: var!(a) + var!(b))
|
||||
iex> Code.eval_quoted(contents, [a: 1, b: 2], __ENV__)
|
||||
{3, [a: 1, b: 2]}
|
||||
|
||||
"""
|
||||
def eval_quoted(quoted, binding \\ [], opts \\ [])
|
||||
|
||||
def eval_quoted(quoted, binding, %Macro.Env{} = env) do
|
||||
{value, binding, _env, _scope} = :elixir.eval_quoted quoted, binding, Map.to_list(env)
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
def eval_quoted(quoted, binding, opts) when is_list(opts) do
|
||||
validate_eval_opts(opts)
|
||||
{value, binding, _env, _scope} = :elixir.eval_quoted quoted, binding, opts
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
defp validate_eval_opts(opts) do
|
||||
if f = opts[:functions], do: validate_imports(:functions, f)
|
||||
if m = opts[:macros], do: validate_imports(:macros, m)
|
||||
if a = opts[:aliases], do: validate_aliases(:aliases, a)
|
||||
if r = opts[:requires], do: validate_requires(:requires, r)
|
||||
end
|
||||
|
||||
defp validate_requires(kind, requires) do
|
||||
valid = is_list(requires) and Enum.all?(requires, &is_atom(&1))
|
||||
|
||||
unless valid do
|
||||
raise ArgumentError, "expected :#{kind} option given to eval in the format: [module]"
|
||||
end
|
||||
end
|
||||
|
||||
defp validate_aliases(kind, aliases) do
|
||||
valid = is_list(aliases) and Enum.all?(aliases, fn {k, v} ->
|
||||
is_atom(k) and is_atom(v)
|
||||
end)
|
||||
|
||||
unless valid do
|
||||
raise ArgumentError, "expected :#{kind} option given to eval in the format: [{module, module}]"
|
||||
end
|
||||
end
|
||||
|
||||
defp validate_imports(kind, imports) do
|
||||
valid = is_list(imports) and Enum.all?(imports, fn {k, v} ->
|
||||
is_atom(k) and is_list(v) and Enum.all?(v, fn {name, arity} ->
|
||||
is_atom(name) and is_integer(arity)
|
||||
end)
|
||||
end)
|
||||
|
||||
unless valid do
|
||||
raise ArgumentError, "expected :#{kind} option given to eval in the format: [{module, [{name, arity}]}]"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given string to its quoted form.
|
||||
|
||||
Returns `{:ok, quoted_form}`
|
||||
if it succeeds, `{:error, {line, error, token}}` otherwise.
|
||||
Evalutes the quoted contents.
|
||||
|
||||
## Options
|
||||
|
||||
* `:file` - the filename to be used in stacktraces
|
||||
and the file reported in the `__ENV__` variable
|
||||
This function accepts a list of options. The supported
|
||||
options are:
|
||||
|
||||
* `:line` - the line reported in the `__ENV__` variable
|
||||
* `:file` - The filename to be used in stacktraces
|
||||
and the file reported in the __ENV__ variable.
|
||||
|
||||
* `:existing_atoms_only` - when `true`, raises an error
|
||||
when non-existing atoms are found by the tokenizer
|
||||
|
||||
## Macro.to_string/2
|
||||
|
||||
The opposite of converting a string to its quoted form is
|
||||
`Macro.to_string/2`, which converts a quoted form to a string/binary
|
||||
representation.
|
||||
"""
|
||||
def string_to_quoted(string, opts \\ []) when is_list(opts) do
|
||||
file = Keyword.get opts, :file, "nofile"
|
||||
line = Keyword.get opts, :line, 1
|
||||
:elixir.string_to_quoted(to_char_list(string), line, file, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given string to its quoted form.
|
||||
|
||||
It returns the ast if it succeeds,
|
||||
raises an exception otherwise. The exception is a `TokenMissingError`
|
||||
in case a token is missing (usually because the expression is incomplete),
|
||||
`SyntaxError` otherwise.
|
||||
|
||||
Check `string_to_quoted/2` for options information.
|
||||
"""
|
||||
def string_to_quoted!(string, opts \\ []) when is_list(opts) do
|
||||
file = Keyword.get opts, :file, "nofile"
|
||||
line = Keyword.get opts, :line, 1
|
||||
:elixir.string_to_quoted!(to_char_list(string), line, file, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evals the given file.
|
||||
|
||||
Accepts `relative_to` as an argument to tell where the file is located.
|
||||
|
||||
While `load_file` loads a file and returns the loaded modules and their
|
||||
byte code, `eval_file` simply evaluates the file contents and returns the
|
||||
evaluation result and its bindings.
|
||||
"""
|
||||
def eval_file(file, relative_to \\ nil) do
|
||||
file = find_file(file, relative_to)
|
||||
eval_string File.read!(file), [], [file: file, line: 1]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Loads the given file.
|
||||
|
||||
Accepts `relative_to` as an argument to tell where the file is located.
|
||||
If the file was already required/loaded, loads it again.
|
||||
|
||||
It returns a list of tuples `{ModuleName, <<byte_code>>}`, one tuple for
|
||||
each module defined in the file.
|
||||
|
||||
Notice that if `load_file` is invoked by different processes concurrently,
|
||||
the target file will be loaded concurrently many times. Check `require_file/2`
|
||||
if you don't want a file to be loaded concurrently.
|
||||
* `:line` - The line reported in the __ENV__ variable.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.load_file("eex_test.exs","../eex/test") |> List.first
|
||||
#=> {EExTest.Compiled, <<70, 79, 82, 49, ...>>}
|
||||
contents = quote hygiene: false, do: a + b
|
||||
|
||||
Code.eval_quoted contents, [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 keywords lists:
|
||||
|
||||
Code.eval_quoted contents, [a: 1, b: 2], __ENV__.location
|
||||
#=> { 3, [ {:a, 1}, {:b, 2} ] }
|
||||
|
||||
"""
|
||||
def load_file(file, relative_to \\ nil) when is_binary(file) do
|
||||
file = find_file(file, relative_to)
|
||||
:elixir_code_server.call {:acquire, file}
|
||||
loaded = :elixir_compiler.file file
|
||||
:elixir_code_server.cast {:loaded, file}
|
||||
loaded
|
||||
def eval_quoted(quoted, binding // [], opts // []) do
|
||||
{ value, binding, _scope } =
|
||||
Erlang.elixir.eval_quoted [quoted], binding, opts
|
||||
{ value, binding }
|
||||
end
|
||||
|
||||
@doc """
|
||||
Requires the given `file`.
|
||||
Converts the given string to AST. It returns { :ok, ast }
|
||||
if it succeeds, { :error, { line, error, token } } otherwise.
|
||||
|
||||
Accepts `relative_to` as an argument to tell where the file is located.
|
||||
The return value is the same as that of `load_file/2`. If the file was already
|
||||
required/loaded, doesn't do anything and returns `nil`.
|
||||
## Options
|
||||
|
||||
Notice that if `require_file` is invoked by different processes concurrently,
|
||||
the first process to invoke `require_file` acquires a lock and the remaining
|
||||
ones will block until the file is available. I.e. if `require_file` is called
|
||||
N times with a given file, it will be loaded only once. The first process to
|
||||
call `require_file` will get the list of loaded modules, others will get `nil`.
|
||||
* `:file` - The filename to be used in stacktraces
|
||||
and the file reported in the __ENV__ variable.
|
||||
|
||||
Check `load_file/2` if you want a file to be loaded multiple times. See also
|
||||
`unload_files/1`
|
||||
|
||||
## Examples
|
||||
|
||||
If the code is already loaded, it returns `nil`:
|
||||
|
||||
Code.require_file("eex_test.exs","../eex/test") #=> nil
|
||||
|
||||
If the code is not loaded yet, it returns the same as `load_file/2`:
|
||||
|
||||
Code.require_file("eex_test.exs","../eex/test") |> List.first
|
||||
#=> {EExTest.Compiled, <<70, 79, 82, 49, ...>>}
|
||||
* `:line` - The line reported in the __ENV__ variable.
|
||||
|
||||
"""
|
||||
def require_file(file, relative_to \\ nil) when is_binary(file) do
|
||||
file = find_file(file, relative_to)
|
||||
|
||||
case :elixir_code_server.call({:acquire, file}) do
|
||||
:loaded ->
|
||||
nil
|
||||
{:queued, ref} ->
|
||||
receive do {:elixir_code_server, ^ref, :loaded} -> nil end
|
||||
:proceed ->
|
||||
loaded = :elixir_compiler.file file
|
||||
:elixir_code_server.cast {:loaded, file}
|
||||
loaded
|
||||
def string_to_ast(string, opts // []) do
|
||||
file = Keyword.get opts, :file, "nofile"
|
||||
line = Keyword.get opts, :line, 1
|
||||
res = :elixir_translator.raw_forms(:unicode.characters_to_list(string), line, file)
|
||||
case res do
|
||||
{ :ok, ast } -> { :ok, unpack_ast(line, ast) }
|
||||
_ -> res
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the compilation options from the code server.
|
||||
Converts the given string to AST. It returns the ast if it succeeds,
|
||||
raises an exception otherwise. The exception is a TokenMissingError
|
||||
in case a token is missing (usually because the expression is incomplete),
|
||||
SyntaxError otherwise.
|
||||
|
||||
Check `compiler_options/1` for more information.
|
||||
## Options
|
||||
|
||||
## Examples
|
||||
* `:file` - The filename to be used in stacktraces
|
||||
and the file reported in the __ENV__ variable.
|
||||
|
||||
Code.compiler_options
|
||||
#=> %{debug_info: true, docs: true,
|
||||
warnings_as_errors: false, ignore_module_conflict: false}
|
||||
* `:line` - The line reported in the __ENV__ variable.
|
||||
|
||||
"""
|
||||
def string_to_ast!(string, opts // []) do
|
||||
file = Keyword.get opts, :file, "nofile"
|
||||
line = Keyword.get opts, :line, 1
|
||||
res = :elixir_translator.forms(:unicode.characters_to_list(string), line, file)
|
||||
unpack_ast(line, res)
|
||||
end
|
||||
|
||||
defp unpack_ast(_line, []), do: nil
|
||||
defp unpack_ast(_line, [forms]) when not is_list(forms), do: forms
|
||||
defp unpack_ast(line, forms), do: { :__block__, line, forms }
|
||||
|
||||
@doc """
|
||||
Loads the given `file`. Accepts `relative_to` as an argument to tell
|
||||
where the file is located. If the file was already required/loaded,
|
||||
loads it again. It returns the full path of the loaded file.
|
||||
|
||||
When loading a file, you may skip passing .exs as extension as Elixir
|
||||
automatically adds it for you.
|
||||
"""
|
||||
def load_file(file, relative_to // nil) when is_binary(file) do
|
||||
file = find_file(file, relative_to)
|
||||
server_call { :loaded, file }
|
||||
Erlang.elixir_compiler.file file
|
||||
file
|
||||
end
|
||||
|
||||
@doc """
|
||||
Requires the given `file`. Accepts `relative_to` as an argument to tell
|
||||
where the file is located. If the file was already required/loaded,
|
||||
returns nil, otherwise the full path of the loaded file.
|
||||
|
||||
When requiring a file, you may skip passing .exs as extension as
|
||||
Elixir automatically adds it for you.
|
||||
"""
|
||||
def require_file(file, relative_to // nil) when is_binary(file) do
|
||||
file = find_file(file, relative_to)
|
||||
|
||||
case server_call({ :loaded, file }) do
|
||||
:ok -> Erlang.elixir_compiler.file file
|
||||
:duplicated -> []
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Loads the compilation options from the code server.
|
||||
Check compiler_options/1 for more information.
|
||||
"""
|
||||
def compiler_options do
|
||||
:elixir_config.get :compiler_options
|
||||
server_call :compiler_options
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list with the available compiler options.
|
||||
|
||||
See `Code.compiler_options/1` for more info.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.available_compiler_options
|
||||
#=> [:docs, :debug_info, :ignore_module_conflict, :warnings_as_errors]
|
||||
|
||||
"""
|
||||
def available_compiler_options do
|
||||
[:docs, :debug_info, :ignore_module_conflict, :warnings_as_errors]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sets compilation options.
|
||||
|
||||
These options are global since they are stored by Elixir's Code Server.
|
||||
Sets compilation options. Those options are global
|
||||
since they are stored by Elixir's Code Server.
|
||||
|
||||
Available options are:
|
||||
|
||||
* `:docs` - when `true`, retain documentation in the compiled module,
|
||||
`true` by default
|
||||
|
||||
* `:debug_info` - when `true`, retain debug information in the compiled
|
||||
module; this allows a developer to reconstruct the original source
|
||||
code, `false` by default
|
||||
|
||||
* `:ignore_module_conflict` - when `true`, override modules that were
|
||||
already defined without raising errors, `false` by default
|
||||
|
||||
* `:warnings_as_errors` - cause compilation to fail when warnings are
|
||||
generated
|
||||
|
||||
It returns the new list of compiler options.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.compiler_options(debug_info: true)
|
||||
#=> %{debug_info: true, docs: true,
|
||||
warnings_as_errors: false, ignore_module_conflict: false}
|
||||
* docs - when true, retain documentation in the compiled module.
|
||||
True by default;
|
||||
* debug_info - when true, retain debug information in the compiled module.
|
||||
This allows a developer to reconstruct the original source
|
||||
code, for such reasons, false by default;
|
||||
* ignore_module_conflict - when true, override modules that were already defined
|
||||
without raising errors, false by default;
|
||||
|
||||
"""
|
||||
def compiler_options(opts) do
|
||||
available = available_compiler_options()
|
||||
|
||||
for {k, _} <- opts,
|
||||
not k in available,
|
||||
do: raise "unknown compiler options: #{k}"
|
||||
|
||||
:elixir_config.update :compiler_options, &Enum.into(opts, &1)
|
||||
server_call { :compiler_options, opts }
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compiles the given string.
|
||||
Compiles the given string and returns a list of tuples where
|
||||
the first element is the module name and the second one is its
|
||||
binary.
|
||||
|
||||
Returns a list of tuples where the first element is the module name
|
||||
and the second one is its byte code (as a binary).
|
||||
|
||||
For compiling many files at once, check `Kernel.ParallelCompiler.files/2`.
|
||||
For compiling many files at once, check `Kernel.ParallelCompiler`.
|
||||
"""
|
||||
def compile_string(string, file \\ "nofile") when is_binary(file) do
|
||||
:elixir_compiler.string to_char_list(string), file
|
||||
def compile_string(string, file // "nofile") when is_binary(file) do
|
||||
Erlang.elixir_compiler.string :unicode.characters_to_list(string), to_binary(file)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compiles the quoted expression.
|
||||
Ensures the given module is loaded. If the module is already
|
||||
loaded, it works as no-op. If the module was not loaded yet,
|
||||
it tries to load it.
|
||||
|
||||
Returns a list of tuples where the first element is the module name and
|
||||
the second one is its byte code (as a binary).
|
||||
"""
|
||||
def compile_quoted(quoted, file \\ "nofile") when is_binary(file) do
|
||||
:elixir_compiler.quoted quoted, file
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the given module is loaded.
|
||||
|
||||
If the module is already loaded, this works as no-op. If the module
|
||||
was not yet loaded, it tries to load it.
|
||||
|
||||
If it succeeds loading the module, it returns `{:module, module}`.
|
||||
If not, returns `{:error, reason}` with the error reason.
|
||||
If it succeeds loading the module anyhow, it returns
|
||||
`{ :module, module }`. If not, returns `{ :error, reason }` with
|
||||
the error reason.
|
||||
|
||||
## Code loading on the Erlang VM
|
||||
|
||||
Erlang has two modes to load code: interactive and embedded.
|
||||
|
||||
By default, the Erlang VM runs in interactive mode, where modules
|
||||
By default, the Erlang VM runs on interactive mode, where modules
|
||||
are loaded as needed. In embedded mode the opposite happens, as all
|
||||
modules need to be loaded upfront or explicitly.
|
||||
|
||||
Therefore, this function is used to check if a module is loaded
|
||||
before using it and allows one to react accordingly. For example, the `URI`
|
||||
module uses this function to check if a specific parser exists for a given
|
||||
URI scheme.
|
||||
Therefore, this function is useful to check if a module is loaded
|
||||
before using it and react accordingly. For example, the `URI` module
|
||||
uses this function to check if a specific parser exists and is for the
|
||||
given URI scheme.
|
||||
|
||||
## Code.ensure_compiled/1
|
||||
## Code.ensure_compiled
|
||||
|
||||
Elixir also contains an `ensure_compiled/1` function that is a
|
||||
superset of `ensure_loaded/1`.
|
||||
|
||||
Since Elixir's compilation happens in parallel, in some situations
|
||||
you may need to use a module that was not yet compiled, therefore
|
||||
you may need to use a module but it was not compiled yet, therefore
|
||||
it can't even be loaded.
|
||||
|
||||
`ensure_compiled/1` halts the current process until the
|
||||
`ensure_compiled/1` puts a halt in the current process until the
|
||||
module we are depending on is available.
|
||||
|
||||
In most cases, `ensure_loaded/1` is enough. `ensure_compiled/1`
|
||||
must be used in rare cases, usually involving macros that need to
|
||||
invoke a module for callback information.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.ensure_loaded(Atom)
|
||||
{:module, Atom}
|
||||
|
||||
iex> Code.ensure_loaded(DoesNotExist)
|
||||
{:error, :nofile}
|
||||
|
||||
In most of the cases, `ensure_loaded` is enough. `ensure_compiled`
|
||||
must be used just in same rare conditions, usually involving macros
|
||||
that needs to invoke a module for callback information.
|
||||
"""
|
||||
def ensure_loaded(module) when is_atom(module) do
|
||||
:code.ensure_loaded(module)
|
||||
Erlang.code.ensure_loaded(module)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the given module is loaded.
|
||||
Ensures the given module is compiled and loaded. If the module
|
||||
is already loaded, it works as no-op. If the module was not
|
||||
loaded yet, it checks if it needs to be compiled first and just
|
||||
then tries to load it.
|
||||
|
||||
Similar to `ensure_loaded/1`, but returns `true` if the module
|
||||
is already loaded or was successfully loaded. Returns `false`
|
||||
otherwise.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.ensure_loaded?(Atom)
|
||||
true
|
||||
|
||||
"""
|
||||
def ensure_loaded?(module) do
|
||||
match?({:module, ^module}, ensure_loaded(module))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the given module is compiled and loaded.
|
||||
|
||||
If the module is already loaded, it works as no-op. If the module was
|
||||
not loaded yet, it checks if it needs to be compiled first and then
|
||||
tries to load it.
|
||||
|
||||
If it succeeds loading the module, it returns `{:module, module}`.
|
||||
If not, returns `{:error, reason}` with the error reason.
|
||||
If it succeeds loading the module anyhow, it returns
|
||||
`{ :module, module }`. If not, returns `{ :error, reason }` with
|
||||
the error reason.
|
||||
|
||||
Check `ensure_loaded/1` for more information on module loading
|
||||
and when to use `ensure_loaded/1` or `ensure_compiled/1`.
|
||||
"""
|
||||
def ensure_compiled(module) when is_atom(module) do
|
||||
case :code.ensure_loaded(module) do
|
||||
{:error, :nofile} = error ->
|
||||
case :erlang.get(:elixir_ensure_compiled) do
|
||||
case Erlang.code.ensure_loaded(module) do
|
||||
{ :error, :nofile } = error ->
|
||||
case :erlang.get(:elixir_compiler_pid) do
|
||||
:undefined -> error
|
||||
_ ->
|
||||
try do
|
||||
module.__info__(:module)
|
||||
{:module, module}
|
||||
module.__info__(:self)
|
||||
{ :module, module }
|
||||
rescue
|
||||
UndefinedFunctionError -> error
|
||||
end
|
||||
@@ -556,110 +283,32 @@ defmodule Code do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the given module is compiled and loaded.
|
||||
|
||||
Similar to `ensure_compiled/1`, but returns `true` if the module
|
||||
is already loaded or was successfully loaded and compiled.
|
||||
Returns `false` otherwise.
|
||||
"""
|
||||
def ensure_compiled?(module) do
|
||||
match?({:module, ^module}, ensure_compiled(module))
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Returns the docs for the given module.
|
||||
|
||||
When given a module name, it finds its BEAM code and reads the docs from it.
|
||||
|
||||
When given a path to a .beam file, it will load the docs directly from that
|
||||
file.
|
||||
|
||||
The return value depends on the `kind` value:
|
||||
|
||||
* `:docs` - list of all docstrings attached to functions and macros
|
||||
using the `@doc` attribute
|
||||
|
||||
* `:moduledoc` - tuple `{<line>, <doc>}` where `line` is the line on
|
||||
which module definition starts and `doc` is the string
|
||||
attached to the module using the `@moduledoc` attribute
|
||||
|
||||
* `:callback_docs` - list of all docstrings attached to
|
||||
`@callbacks` using the `@doc` attribute
|
||||
|
||||
* `:type_docs` - list of all docstrings attached to
|
||||
`@type` callbacks using the `@typedoc` attribute
|
||||
|
||||
* `:all` - a keyword list with `:docs` and `:moduledoc`, `:callback_docs`,
|
||||
and `:type_docs`.
|
||||
|
||||
If the module cannot be found, it returns `nil`.
|
||||
|
||||
## Examples
|
||||
|
||||
# Get the documentation for the first function listed
|
||||
iex> [fun|_] = Code.get_docs(Atom, :docs) |> Enum.sort()
|
||||
iex> {{_function, _arity}, _line, _kind, _signature, text} = fun
|
||||
iex> String.split(text, "\n") |> Enum.at(0)
|
||||
"Converts an atom to a char list."
|
||||
|
||||
# Module doesn't exist
|
||||
iex> Code.get_docs(ModuleNotGood, :all)
|
||||
nil
|
||||
|
||||
"""
|
||||
@doc_kinds [:docs, :moduledoc, :callback_docs, :type_docs, :all]
|
||||
|
||||
def get_docs(module, kind) when is_atom(module) and kind in @doc_kinds do
|
||||
case :code.get_object_code(module) do
|
||||
{_module, bin, _beam_path} ->
|
||||
do_get_docs(bin, kind)
|
||||
|
||||
:error -> nil
|
||||
end
|
||||
end
|
||||
|
||||
def get_docs(binpath, kind) when is_binary(binpath) and kind in @doc_kinds do
|
||||
do_get_docs(String.to_char_list(binpath), kind)
|
||||
end
|
||||
|
||||
@docs_chunk 'ExDc'
|
||||
|
||||
defp do_get_docs(bin_or_path, kind) do
|
||||
case :beam_lib.chunks(bin_or_path, [@docs_chunk]) do
|
||||
{:ok, {_module, [{@docs_chunk, bin}]}} ->
|
||||
lookup_docs(:erlang.binary_to_term(bin), kind)
|
||||
|
||||
{:error, :beam_lib, {:missing_chunk, _, @docs_chunk}} -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp lookup_docs({:elixir_docs_v1, docs}, kind),
|
||||
do: do_lookup_docs(docs, kind)
|
||||
|
||||
# unsupported chunk version
|
||||
defp lookup_docs(_, _), do: nil
|
||||
|
||||
defp do_lookup_docs(docs, :all), do: docs
|
||||
defp do_lookup_docs(docs, kind),
|
||||
do: Keyword.get(docs, kind)
|
||||
|
||||
## Helpers
|
||||
|
||||
# Finds the file given the relative_to path.
|
||||
#
|
||||
# If the file is found, returns its path in binary, fails otherwise.
|
||||
defp find_file(file, relative_to) do
|
||||
file = to_binary(file)
|
||||
|
||||
file = if relative_to do
|
||||
Path.expand(file, relative_to)
|
||||
File.expand_path(file, relative_to)
|
||||
else
|
||||
Path.expand(file)
|
||||
File.expand_path(file)
|
||||
end
|
||||
|
||||
if File.regular?(file) do
|
||||
file
|
||||
else
|
||||
raise Code.LoadError, file: file
|
||||
prefix = "#{file}.exs"
|
||||
if File.regular?(prefix) do
|
||||
prefix
|
||||
else
|
||||
raise ArgumentError, message: "could not load #{file}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp server_call(args) do
|
||||
Erlang.gen_server.call(:elixir_code_server, args)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
defprotocol Collectable do
|
||||
@moduledoc """
|
||||
A protocol to traverse data structures.
|
||||
|
||||
The `Enum.into/2` function uses this protocol to insert an
|
||||
enumerable into a collection:
|
||||
|
||||
iex> Enum.into([a: 1, b: 2], %{})
|
||||
%{a: 1, b: 2}
|
||||
|
||||
## Why Collectable?
|
||||
|
||||
The `Enumerable` protocol is useful to take values out of a collection.
|
||||
In order to support a wide range of values, the functions provided by
|
||||
the `Enumerable` protocol do not keep shape. For example, passing a
|
||||
dictionary to `Enum.map/2` always returns a list.
|
||||
|
||||
This design is intentional. `Enumerable` was designed to support infinite
|
||||
collections, resources and other structures with fixed shape. For example,
|
||||
it doesn't make sense to insert values into a range, as it has a fixed
|
||||
shape where just the range limits are stored.
|
||||
|
||||
The `Collectable` module was designed to fill the gap left by the
|
||||
`Enumerable` protocol. `into/1` can be seen as the opposite of
|
||||
`Enumerable.reduce/3`. If `Enumerable` is about taking values out,
|
||||
`Collectable.into/1` is about collecting those values into a structure.
|
||||
"""
|
||||
|
||||
@type command :: {:cont, term} | :done | :halt
|
||||
|
||||
@doc """
|
||||
Returns a function that collects values alongside
|
||||
the initial accumulation value.
|
||||
|
||||
The returned function receives a collectable and injects a given
|
||||
value into it for every `{:cont, term}` instruction.
|
||||
|
||||
`:done` is passed when no further values will be injected, useful
|
||||
for closing resources and normalizing values. A collectable must
|
||||
be returned on `:done`.
|
||||
|
||||
If injection is suddenly interrupted, `:halt` is passed and it can
|
||||
return any value, as it won't be used.
|
||||
"""
|
||||
@spec into(t) :: {term, (term, command -> t | term)}
|
||||
def into(collectable)
|
||||
end
|
||||
|
||||
defimpl Collectable, for: List do
|
||||
def into(original) do
|
||||
{[], fn
|
||||
list, {:cont, x} -> [x|list]
|
||||
list, :done -> original ++ :lists.reverse(list)
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Collectable, for: BitString do
|
||||
def into(original) do
|
||||
{original, fn
|
||||
acc, {:cont, x} when is_bitstring(x) -> [acc|x]
|
||||
acc, :done -> IO.iodata_to_binary(acc)
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Collectable, for: Map do
|
||||
def into(original) do
|
||||
{original, fn
|
||||
map, {:cont, {k, v}} -> :maps.put(k, v, map)
|
||||
map, :done -> map
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
end
|
||||
end
|
||||
+132
-378
@@ -1,398 +1,152 @@
|
||||
defmodule Dict do
|
||||
@moduledoc ~S"""
|
||||
WARNING: this module is deprecated.
|
||||
defprotocol Dict do
|
||||
@only [Record]
|
||||
|
||||
If you need a general dictionary, use the `Map` module.
|
||||
If you need to manipulate keyword lists, use `Keyword`.
|
||||
|
||||
To convert maps into keywords and vice-versa, use the
|
||||
`new` function in the respective modules.
|
||||
@moduledoc """
|
||||
This module provides the Dict protocol
|
||||
with the goal of being a common API
|
||||
to work with dictionaries.
|
||||
"""
|
||||
|
||||
@type key :: any
|
||||
@type value :: any
|
||||
@type t :: list | map
|
||||
@doc """
|
||||
Returns a list containing all dict's keys.
|
||||
The keys are not guaranteed to be sorted, unless
|
||||
the underlying dict implementation defines so.
|
||||
|
||||
# TODO: Remove callbacks on 1.3
|
||||
# TODO: Deprecate every function on 1.3
|
||||
@callback new :: t
|
||||
@callback delete(t, key) :: t
|
||||
@callback drop(t, Enum.t) :: t
|
||||
@callback equal?(t, t) :: boolean
|
||||
@callback get(t, key) :: value
|
||||
@callback get(t, key, value) :: value
|
||||
@callback get_lazy(t, key, (() -> value)) :: value
|
||||
@callback get_and_update(t, key, (value -> {value, value})) :: {value, t}
|
||||
@callback fetch(t, key) :: {:ok, value} | :error
|
||||
@callback fetch!(t, key) :: value | no_return
|
||||
@callback has_key?(t, key) :: boolean
|
||||
@callback keys(t) :: [key]
|
||||
@callback merge(t, t) :: t
|
||||
@callback merge(t, t, (key, value, value -> value)) :: t
|
||||
@callback pop(t, key) :: {value, t}
|
||||
@callback pop(t, key, value) :: {value, t}
|
||||
@callback pop_lazy(t, key, (() -> value)) :: {value, t}
|
||||
@callback put(t, key, value) :: t
|
||||
@callback put_new(t, key, value) :: t
|
||||
@callback put_new_lazy(t, key, (() -> value)) :: t
|
||||
@callback size(t) :: non_neg_integer()
|
||||
@callback split(t, Enum.t) :: {t, t}
|
||||
@callback take(t, Enum.t) :: t
|
||||
@callback to_list(t) :: list()
|
||||
@callback update(t, key, value, (value -> value)) :: t
|
||||
@callback update!(t, key, (value -> value)) :: t | no_return
|
||||
@callback values(t) :: list(value)
|
||||
## Examples
|
||||
|
||||
defmacro __using__(_) do
|
||||
# Use this import to guarantee proper code expansion
|
||||
import Kernel, except: [size: 1]
|
||||
Dict.keys [a: 1, b: 2] #=> [:a,:b]
|
||||
|
||||
%{file: file, line: line} = __CALLER__
|
||||
:elixir_errors.warn(line, file, "the Dict module is deprecated")
|
||||
"""
|
||||
def keys(dict)
|
||||
|
||||
quote do
|
||||
@behaviour Dict
|
||||
@doc """
|
||||
Returns a list containing all dict's values.
|
||||
|
||||
def get(dict, key, default \\ nil) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> default
|
||||
end
|
||||
## Examples
|
||||
|
||||
Dict.values [a: 1, b: 2] #=> [1,2]
|
||||
|
||||
"""
|
||||
def values(dict)
|
||||
|
||||
@doc """
|
||||
Returns the number of elements in `dict`.
|
||||
|
||||
## Examples
|
||||
|
||||
Dict.size [a: 1, b: 2] #=> 2
|
||||
|
||||
"""
|
||||
def size(dict)
|
||||
|
||||
@doc """
|
||||
Returns whether the given key exists in the given dict.
|
||||
|
||||
## Examples
|
||||
|
||||
Dict.has_key?([a: 1], :a) #=> true
|
||||
Dict.has_key?([a: 1], :b) #=> false
|
||||
|
||||
"""
|
||||
def has_key?(dict, key)
|
||||
|
||||
@doc """
|
||||
Returns the value associated with `key` in `dict`. If `dict` does not
|
||||
contain `key`, returns `default` (or nil if not provided).
|
||||
|
||||
## Examples
|
||||
|
||||
Dict.get [a: 1], :a #=> 1
|
||||
Dict.get [a: 1], :b #=> nil
|
||||
Dict.get [a: 1], :b, 3 #=> 3
|
||||
|
||||
"""
|
||||
def get(dict, key)
|
||||
def get(dict, key, default)
|
||||
|
||||
@doc """
|
||||
Stores the given `value` under `key` in `dict`.
|
||||
If `dict` already has `key`, the stored value is replaced by the new one.
|
||||
|
||||
## Examples
|
||||
|
||||
Dict.put [a: 1, b: 2], :a, 3
|
||||
#=> [a: 3, b: 2]
|
||||
|
||||
"""
|
||||
def put(dict, key, val)
|
||||
|
||||
@doc """
|
||||
Removes the entry stored under the given key from `dict`.
|
||||
If `dict` does not contain `key`, returns the dictionary unchanged.
|
||||
|
||||
## Examples
|
||||
|
||||
Dict.delete [a: 1, b: 2], :a #=> [b: 2]
|
||||
Dict.delete [b: 2], :a #=> [b: 2]
|
||||
|
||||
"""
|
||||
def delete(dict, key)
|
||||
|
||||
@doc """
|
||||
Merges two dicts into one. If the dicts have duplicated entries, the one
|
||||
given as second argument wins.
|
||||
|
||||
## Examples
|
||||
|
||||
Dict.merge [a: 1, b: 2], [a: 3, d: 4]
|
||||
#=> [a:3, b:2, d: 4]
|
||||
|
||||
"""
|
||||
def merge(dict1, dict2)
|
||||
|
||||
@doc """
|
||||
Merges two dicts into one. If the dicts have duplicated entries, the given
|
||||
function is invoked to solve conflicts.
|
||||
|
||||
## Examples
|
||||
|
||||
Dict.merge [a: 1, b: 2], [a: 3, d: 4], fn _k, v1, v2 ->
|
||||
v1 + v2
|
||||
end
|
||||
#=> [a: 4, b: 2, d: 4]
|
||||
|
||||
def get_lazy(dict, key, fun) when is_function(fun, 0) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> fun.()
|
||||
end
|
||||
end
|
||||
"""
|
||||
def merge(dict1, dict2, fun)
|
||||
|
||||
def get_and_update(dict, key, fun) do
|
||||
current_value = get(dict, key)
|
||||
{get, new_value} = fun.(current_value)
|
||||
{get, put(dict, key, new_value)}
|
||||
end
|
||||
@doc """
|
||||
Update a value in `dict` by calling `fun` on the value to get a new
|
||||
value. An exception is generated if `key` is not present in the dict.
|
||||
|
||||
def fetch!(dict, key) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> raise KeyError, key: key, term: dict
|
||||
end
|
||||
end
|
||||
## Examples
|
||||
|
||||
def has_key?(dict, key) do
|
||||
match? {:ok, _}, fetch(dict, key)
|
||||
end
|
||||
Dict.update [a: 1, b: 2], :a, fn val -> -val end
|
||||
#=> [a: -1, b: 2]
|
||||
|
||||
def put_new(dict, key, value) do
|
||||
case has_key?(dict, key) do
|
||||
true -> dict
|
||||
false -> put(dict, key, value)
|
||||
end
|
||||
end
|
||||
"""
|
||||
def update(dict, key, fun)
|
||||
|
||||
def put_new_lazy(dict, key, fun) when is_function(fun, 0) do
|
||||
case has_key?(dict, key) do
|
||||
true -> dict
|
||||
false -> put(dict, key, fun.())
|
||||
end
|
||||
end
|
||||
@doc """
|
||||
Update a value in `dict` by calling `fun` on the value to get a new value. If
|
||||
`key` is not present in `dict` then `initial` will be stored as the first
|
||||
value.
|
||||
|
||||
def drop(dict, keys) do
|
||||
Enum.reduce(keys, dict, &delete(&2, &1))
|
||||
end
|
||||
## Examples
|
||||
|
||||
def take(dict, keys) do
|
||||
Enum.reduce(keys, new, fn key, acc ->
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> put(acc, key, value)
|
||||
:error -> acc
|
||||
end
|
||||
end)
|
||||
end
|
||||
Dict.update [a: 1, b: 2], :c, 3, fn val -> -val end
|
||||
#=> [a: 1, b: 2, c: 3]
|
||||
|
||||
def to_list(dict) do
|
||||
reduce(dict, {:cont, []}, fn
|
||||
kv, acc -> {:cont, [kv|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
"""
|
||||
def update(dict, key, initial, fun)
|
||||
|
||||
def keys(dict) do
|
||||
reduce(dict, {:cont, []}, fn
|
||||
{k, _}, acc -> {:cont, [k|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
@doc """
|
||||
Returns an empty dict of the same type as `dict`.
|
||||
"""
|
||||
def empty(dict)
|
||||
|
||||
def values(dict) do
|
||||
reduce(dict, {:cont, []}, fn
|
||||
{_, v}, acc -> {:cont, [v|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
|
||||
def equal?(dict1, dict2) do
|
||||
# Use this import to avoid conflicts in the user code
|
||||
import Kernel, except: [size: 1]
|
||||
|
||||
case size(dict1) == size(dict2) do
|
||||
false -> false
|
||||
true ->
|
||||
reduce(dict1, {:cont, true}, fn({k, v}, _acc) ->
|
||||
case fetch(dict2, k) do
|
||||
{:ok, ^v} -> {:cont, true}
|
||||
_ -> {:halt, false}
|
||||
end
|
||||
end) |> elem(1)
|
||||
end
|
||||
end
|
||||
|
||||
def merge(dict1, dict2, fun \\ fn(_k, _v1, v2) -> v2 end) do
|
||||
# Use this import to avoid conflicts in the user code
|
||||
import Kernel, except: [size: 1]
|
||||
|
||||
if size(dict1) < size(dict2) do
|
||||
reduce(dict1, {:cont, dict2}, fn {k, v1}, acc ->
|
||||
{:cont, update(acc, k, v1, &fun.(k, v1, &1))}
|
||||
end)
|
||||
else
|
||||
reduce(dict2, {:cont, dict1}, fn {k, v2}, acc ->
|
||||
{:cont, update(acc, k, v2, &fun.(k, &1, v2))}
|
||||
end)
|
||||
end |> elem(1)
|
||||
end
|
||||
|
||||
def update(dict, key, initial, fun) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
put(dict, key, fun.(value))
|
||||
:error ->
|
||||
put(dict, key, initial)
|
||||
end
|
||||
end
|
||||
|
||||
def update!(dict, key, fun) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
put(dict, key, fun.(value))
|
||||
:error ->
|
||||
raise KeyError, key: key, term: dict
|
||||
end
|
||||
end
|
||||
|
||||
def pop(dict, key, default \\ nil) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(dict, key)}
|
||||
:error ->
|
||||
{default, dict}
|
||||
end
|
||||
end
|
||||
|
||||
def pop_lazy(dict, key, fun) when is_function(fun, 0) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(dict, key)}
|
||||
:error ->
|
||||
{fun.(), dict}
|
||||
end
|
||||
end
|
||||
|
||||
def split(dict, keys) do
|
||||
Enum.reduce(keys, {new, dict}, fn key, {inc, exc} = acc ->
|
||||
case fetch(exc, key) do
|
||||
{:ok, value} ->
|
||||
{put(inc, key, value), delete(exc, key)}
|
||||
:error ->
|
||||
acc
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defoverridable merge: 2, merge: 3, equal?: 2, to_list: 1, keys: 1,
|
||||
values: 1, take: 2, drop: 2, get: 2, get: 3, fetch!: 2,
|
||||
has_key?: 2, put_new: 3, pop: 2, pop: 3, split: 2,
|
||||
update: 4, update!: 3, get_and_update: 3, get_lazy: 3,
|
||||
pop_lazy: 3, put_new_lazy: 3
|
||||
end
|
||||
end
|
||||
|
||||
defmacrop target(dict) do
|
||||
quote do
|
||||
case unquote(dict) do
|
||||
%{__struct__: x} when is_atom(x) ->
|
||||
x
|
||||
%{} ->
|
||||
Map
|
||||
x when is_list(x) ->
|
||||
Keyword
|
||||
x ->
|
||||
unsupported_dict(x)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@spec keys(t) :: [key]
|
||||
def keys(dict) do
|
||||
target(dict).keys(dict)
|
||||
end
|
||||
|
||||
@spec values(t) :: [value]
|
||||
def values(dict) do
|
||||
target(dict).values(dict)
|
||||
end
|
||||
|
||||
@spec size(t) :: non_neg_integer
|
||||
def size(dict) do
|
||||
target(dict).size(dict)
|
||||
end
|
||||
|
||||
@spec has_key?(t, key) :: boolean
|
||||
def has_key?(dict, key) do
|
||||
target(dict).has_key?(dict, key)
|
||||
end
|
||||
|
||||
@spec get(t, key, value) :: value
|
||||
def get(dict, key, default \\ nil) do
|
||||
target(dict).get(dict, key, default)
|
||||
end
|
||||
|
||||
@spec get_lazy(t, key, (() -> value)) :: value
|
||||
def get_lazy(dict, key, fun) do
|
||||
target(dict).get_lazy(dict, key, fun)
|
||||
end
|
||||
|
||||
@spec get_and_update(t, key, (value -> {value, value})) :: {value, t}
|
||||
def get_and_update(dict, key, fun) do
|
||||
target(dict).get_and_update(dict, key, fun)
|
||||
end
|
||||
|
||||
@spec fetch(t, key) :: value
|
||||
def fetch(dict, key) do
|
||||
target(dict).fetch(dict, key)
|
||||
end
|
||||
|
||||
@spec fetch!(t, key) :: value | no_return
|
||||
def fetch!(dict, key) do
|
||||
target(dict).fetch!(dict, key)
|
||||
end
|
||||
|
||||
@spec put(t, key, value) :: t
|
||||
def put(dict, key, val) do
|
||||
target(dict).put(dict, key, val)
|
||||
end
|
||||
|
||||
@spec put_new(t, key, value) :: t
|
||||
def put_new(dict, key, val) do
|
||||
target(dict).put_new(dict, key, val)
|
||||
end
|
||||
|
||||
@spec put_new_lazy(t, key, (() -> value)) :: t
|
||||
def put_new_lazy(dict, key, fun) do
|
||||
target(dict).put_new_lazy(dict, key, fun)
|
||||
end
|
||||
|
||||
@spec delete(t, key) :: t
|
||||
def delete(dict, key) do
|
||||
target(dict).delete(dict, key)
|
||||
end
|
||||
|
||||
@spec merge(t, t) :: t
|
||||
def merge(dict1, dict2) do
|
||||
target1 = target(dict1)
|
||||
target2 = target(dict2)
|
||||
|
||||
if target1 == target2 do
|
||||
target1.merge(dict1, dict2)
|
||||
else
|
||||
do_merge(target1, dict1, dict2, fn(_k, _v1, v2) -> v2 end)
|
||||
end
|
||||
end
|
||||
|
||||
@spec merge(t, t, (key, value, value -> value)) :: t
|
||||
def merge(dict1, dict2, fun) do
|
||||
target1 = target(dict1)
|
||||
target2 = target(dict2)
|
||||
|
||||
if target1 == target2 do
|
||||
target1.merge(dict1, dict2, fun)
|
||||
else
|
||||
do_merge(target1, dict1, dict2, fun)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_merge(target1, dict1, dict2, fun) do
|
||||
Enumerable.reduce(dict2, {:cont, dict1}, fn({k, v}, acc) ->
|
||||
{:cont, target1.update(acc, k, v, fn(other) -> fun.(k, other, v) end)}
|
||||
end) |> elem(1)
|
||||
end
|
||||
|
||||
@spec pop(t, key, value) :: {value, t}
|
||||
def pop(dict, key, default \\ nil) do
|
||||
target(dict).pop(dict, key, default)
|
||||
end
|
||||
|
||||
@spec pop_lazy(t, key, (() -> value)) :: {value, t}
|
||||
def pop_lazy(dict, key, fun) do
|
||||
target(dict).pop_lazy(dict, key, fun)
|
||||
end
|
||||
|
||||
@spec update!(t, key, (value -> value)) :: t
|
||||
def update!(dict, key, fun) do
|
||||
target(dict).update!(dict, key, fun)
|
||||
end
|
||||
|
||||
@spec update(t, key, value, (value -> value)) :: t
|
||||
def update(dict, key, initial, fun) do
|
||||
target(dict).update(dict, key, initial, fun)
|
||||
end
|
||||
|
||||
@spec split(t, [key]) :: {t, t}
|
||||
def split(dict, keys) do
|
||||
target(dict).split(dict, keys)
|
||||
end
|
||||
|
||||
@spec drop(t, [key]) :: t
|
||||
def drop(dict, keys) do
|
||||
target(dict).drop(dict, keys)
|
||||
end
|
||||
|
||||
@spec take(t, [key]) :: t
|
||||
def take(dict, keys) do
|
||||
target(dict).take(dict, keys)
|
||||
end
|
||||
|
||||
@spec empty(t) :: t
|
||||
def empty(dict) do
|
||||
target(dict).empty(dict)
|
||||
end
|
||||
|
||||
@spec equal?(t, t) :: boolean
|
||||
def equal?(dict1, dict2) do
|
||||
target1 = target(dict1)
|
||||
target2 = target(dict2)
|
||||
|
||||
cond do
|
||||
target1 == target2 ->
|
||||
target1.equal?(dict1, dict2)
|
||||
|
||||
target1.size(dict1) == target2.size(dict2) ->
|
||||
Enumerable.reduce(dict2, {:cont, true}, fn({k, v}, _acc) ->
|
||||
case target1.fetch(dict1, k) do
|
||||
{:ok, ^v} -> {:cont, true}
|
||||
_ -> {:halt, false}
|
||||
end
|
||||
end) |> elem(1)
|
||||
|
||||
true ->
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
@spec to_list(t) :: list
|
||||
def to_list(dict) do
|
||||
target(dict).to_list(dict)
|
||||
end
|
||||
|
||||
defp unsupported_dict(dict) do
|
||||
raise ArgumentError, "unsupported dict: #{inspect dict}"
|
||||
end
|
||||
end
|
||||
@doc """
|
||||
Returns a list of key-value pairs stored in `dict`.
|
||||
No particular order is enforced.
|
||||
"""
|
||||
def to_list(dict)
|
||||
end
|
||||
@@ -0,0 +1,45 @@
|
||||
defmodule Dict.Common do
|
||||
@moduledoc false
|
||||
|
||||
defmacro __using__(ref) do
|
||||
quote do
|
||||
@doc """
|
||||
Creates a new empty dict.
|
||||
"""
|
||||
def new do
|
||||
unquote(ref).empty(nil)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a new dict from a list of pairs.
|
||||
|
||||
## Examples
|
||||
|
||||
#{inspect(__MODULE__)}.new [{:b,1},{:a,2}]
|
||||
#=> [a: 1, b: 2]
|
||||
|
||||
"""
|
||||
def new(pairs) do
|
||||
Enum.reduce pairs, new, fn { k, v }, dict ->
|
||||
unquote(ref).put(dict, k, v)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a new dict from a list of elements with the
|
||||
help of the transformation function.
|
||||
|
||||
## Examples
|
||||
|
||||
#{inspect(__MODULE__)}.new ["a", "b"], fn x -> {x, x} end
|
||||
#=> ["a": "a", "b": "b"]
|
||||
"""
|
||||
def new(list, transform) when is_function(transform) do
|
||||
Enum.reduce list, new(), fn i, dict ->
|
||||
{ k, v } = transform.(i)
|
||||
unquote(ref).put(dict, k, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
+972
-2478
File diff suppressed because it is too large
Load Diff
+121
-762
@@ -1,854 +1,213 @@
|
||||
defmodule Exception do
|
||||
@moduledoc """
|
||||
Functions to format throw/catch/exit and exceptions.
|
||||
|
||||
Note that stacktraces in Elixir are updated on throw,
|
||||
errors and exits. For example, at any given moment,
|
||||
`System.stacktrace/0` will return the stacktrace for the
|
||||
last throw/error/exit that occurred in the current process.
|
||||
|
||||
Do not rely on the particular format returned by the `format`
|
||||
functions in this module. They may be changed in future releases
|
||||
in order to better suit Elixir's tool chain. In other words,
|
||||
by using the functions in this module it is guaranteed you will
|
||||
format exceptions as in the current Elixir version being used.
|
||||
Several convenience functions to work and pretty print
|
||||
exceptions and backtraces.
|
||||
"""
|
||||
|
||||
@typedoc "The exception type"
|
||||
@type t :: %{__struct__: module, __exception__: true}
|
||||
# Normalize an exception converting Erlang exceptions
|
||||
# to Elixir style exceptions. This is meant to be used
|
||||
# internally.
|
||||
@doc false
|
||||
def normalize(exception) when is_exception(exception) do
|
||||
exception
|
||||
end
|
||||
|
||||
@typedoc "The kind handled by formatting functions"
|
||||
@type kind :: :error | :exit | :throw | {:EXIT, pid}
|
||||
def normalize(:badarg) do
|
||||
ArgumentError.new
|
||||
end
|
||||
|
||||
@type stacktrace :: [stacktrace_entry]
|
||||
@type stacktrace_entry ::
|
||||
{module, atom, arity_or_args, location} |
|
||||
{(... -> any), arity_or_args, location}
|
||||
def normalize(:badarith) do
|
||||
ArithmeticError.new
|
||||
end
|
||||
|
||||
@typep arity_or_args :: non_neg_integer | list
|
||||
@typep location :: Keyword.t
|
||||
def normalize(:system_limit) do
|
||||
SystemLimitError.new
|
||||
end
|
||||
|
||||
@callback exception(term) :: t
|
||||
@callback message(t) :: String.t
|
||||
def normalize({ :badarity, { fun, args } }) do
|
||||
BadArityError.new(function: fun, args: args)
|
||||
end
|
||||
|
||||
def normalize({ :badfun, actual }) do
|
||||
BadFunctionError.new(actual: actual)
|
||||
end
|
||||
|
||||
def normalize({ :badmatch, actual }) do
|
||||
MatchError.new(actual: actual)
|
||||
end
|
||||
|
||||
def normalize({ :case_clause, actual }) do
|
||||
CaseClauseError.new(actual: actual)
|
||||
end
|
||||
|
||||
def normalize(:undef) do
|
||||
UndefinedFunctionError.new from_stacktrace(System.stacktrace)
|
||||
end
|
||||
|
||||
def normalize(:function_clause) do
|
||||
FunctionClauseError.new from_stacktrace(System.stacktrace)
|
||||
end
|
||||
|
||||
def normalize({ :badarg, payload }) do
|
||||
ArgumentError.new message: "argument error: #{inspect(payload)}"
|
||||
end
|
||||
|
||||
def normalize(other) do
|
||||
ErlangError.new original: other
|
||||
end
|
||||
|
||||
# Check the given module is a valid record.
|
||||
@doc false
|
||||
def check!(module) do
|
||||
unless :erlang.function_exported(module, :message, 1) do
|
||||
raise "Expected #{inspect module} to implement message/1"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the given `term` is an exception.
|
||||
Receives a module, fun and arity and returns a string
|
||||
representing such invocation. Arity may also be a list
|
||||
of arguments. It follows the same syntax as in stacktraces.
|
||||
"""
|
||||
def exception?(term)
|
||||
def format_module_fun_arity(module, fun, arity) do
|
||||
<< ?:, fun | :binary >> = inspect(fun)
|
||||
|
||||
def exception?(%{__struct__: struct, __exception__: true}) when is_atom(struct),
|
||||
do: true
|
||||
|
||||
def exception?(_), do: false
|
||||
|
||||
@doc """
|
||||
Gets the message for an `exception`.
|
||||
"""
|
||||
def message(%{__struct__: module, __exception__: true} = exception) when is_atom(module) do
|
||||
try do
|
||||
module.message(exception)
|
||||
rescue
|
||||
e ->
|
||||
"got #{inspect e.__struct__} with message #{inspect message(e)} " <>
|
||||
"while retrieving Exception.message/1 for #{inspect(exception)}"
|
||||
if is_list(arity) do
|
||||
inspected = lc x inlist arity, do: inspect(x)
|
||||
"#{inspect module}.#{fun}(#{Enum.join(inspected, ", ")})"
|
||||
else
|
||||
x when is_binary(x) -> x
|
||||
x ->
|
||||
"got #{inspect(x)} " <>
|
||||
"while retrieving Exception.message/1 for #{inspect(exception)} " <>
|
||||
"(expected a string)"
|
||||
"#{inspect module}.#{fun}/#{arity}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Normalizes an exception, converting Erlang exceptions
|
||||
to Elixir exceptions.
|
||||
|
||||
It takes the `kind` spilled by `catch` as an argument and
|
||||
normalizes only `:error`, returning the untouched payload
|
||||
for others.
|
||||
|
||||
The third argument, a stacktrace, is optional. If it is
|
||||
not supplied `System.stacktrace/0` will sometimes be used
|
||||
to get additional information for the `kind` `:error`. If
|
||||
the stacktrace is unknown and `System.stacktrace/0` would
|
||||
not return the stacktrace corresponding to the exception
|
||||
an empty stacktrace, `[]`, must be used.
|
||||
Formats each line in the stacktrace.
|
||||
"""
|
||||
@spec normalize(:error, any, stacktrace) :: t
|
||||
@spec normalize(kind, payload, stacktrace) :: payload when payload: var
|
||||
|
||||
# Generating a stacktrace is expensive, default to nil
|
||||
# to only fetch it when needed.
|
||||
def normalize(kind, payload, stacktrace \\ nil)
|
||||
|
||||
def normalize(:error, exception, stacktrace) do
|
||||
if exception?(exception) do
|
||||
exception
|
||||
else
|
||||
ErlangError.normalize(exception, stacktrace)
|
||||
end
|
||||
end
|
||||
|
||||
def normalize(_kind, payload, _stacktrace) do
|
||||
payload
|
||||
def format_stacktrace({module, fun, arity, file_line}) do
|
||||
"#{format_file_line(file_line)}#{format_module_fun_arity(module, fun, arity)}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Normalizes and formats any throw/error/exit.
|
||||
|
||||
The message is formatted and displayed in the same
|
||||
format as used by Elixir's CLI.
|
||||
|
||||
The third argument, a stacktrace, is optional. If it is
|
||||
not supplied `System.stacktrace/0` will sometimes be used
|
||||
to get additional information for the `kind` `:error`. If
|
||||
the stacktrace is unknown and `System.stacktrace/0` would
|
||||
not return the stacktrace corresponding to the exception
|
||||
an empty stacktrace, `[]`, must be used.
|
||||
Formats file and line information present in stacktraces.
|
||||
Expect them to be given in a keywords list.
|
||||
"""
|
||||
@spec format_banner(kind, any, stacktrace | nil) :: String.t
|
||||
def format_banner(kind, exception, stacktrace \\ nil)
|
||||
|
||||
def format_banner(:error, exception, stacktrace) do
|
||||
exception = normalize(:error, exception, stacktrace)
|
||||
"** (" <> inspect(exception.__struct__) <> ") " <> message(exception)
|
||||
end
|
||||
|
||||
def format_banner(:throw, reason, _stacktrace) do
|
||||
"** (throw) " <> inspect(reason)
|
||||
end
|
||||
|
||||
def format_banner(:exit, reason, _stacktrace) do
|
||||
"** (exit) " <> format_exit(reason, <<"\n ">>)
|
||||
end
|
||||
|
||||
def format_banner({:EXIT, pid}, reason, _stacktrace) do
|
||||
"** (EXIT from #{inspect pid}) " <> format_exit(reason, <<"\n ">>)
|
||||
def format_file_line(file_line) do
|
||||
format_file_line(Keyword.get(file_line, :file), Keyword.get(file_line, :line))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Normalizes and formats throw/errors/exits and stacktraces.
|
||||
|
||||
It relies on `format_banner/3` and `format_stacktrace/1`
|
||||
to generate the final format.
|
||||
|
||||
Note that `{:EXIT, pid}` do not generate a stacktrace though
|
||||
(as they are retrieved as messages without stacktraces).
|
||||
"""
|
||||
|
||||
@spec format(kind, any, stacktrace | nil) :: String.t
|
||||
|
||||
def format(kind, payload, stacktrace \\ nil)
|
||||
|
||||
def format({:EXIT, _} = kind, any, _) do
|
||||
format_banner(kind, any)
|
||||
end
|
||||
|
||||
def format(kind, payload, stacktrace) do
|
||||
stacktrace = stacktrace || System.stacktrace
|
||||
message = format_banner(kind, payload, stacktrace)
|
||||
case stacktrace do
|
||||
[] -> message
|
||||
_ -> message <> "\n" <> format_stacktrace(stacktrace)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats an exit. It returns a string.
|
||||
|
||||
Often there are errors/exceptions inside exits. Exits are often
|
||||
wrapped by the caller and provide stacktraces too. This function
|
||||
formats exits in a way to nicely show the exit reason, caller
|
||||
and stacktrace.
|
||||
"""
|
||||
@spec format_exit(any) :: String.t
|
||||
def format_exit(reason) do
|
||||
format_exit(reason, <<"\n ">>)
|
||||
end
|
||||
|
||||
# 2-Tuple could be caused by an error if the second element is a stacktrace.
|
||||
defp format_exit({exception, maybe_stacktrace} = reason, joiner)
|
||||
when is_list(maybe_stacktrace) and maybe_stacktrace !== [] do
|
||||
try do
|
||||
Enum.map(maybe_stacktrace, &format_stacktrace_entry/1)
|
||||
else
|
||||
formatted_stacktrace ->
|
||||
# Assume a non-empty list formattable as stacktrace is a
|
||||
# stacktrace, so exit was caused by an error.
|
||||
message = "an exception was raised:" <> joiner <>
|
||||
format_banner(:error, exception, maybe_stacktrace)
|
||||
Enum.join([message | formatted_stacktrace], joiner <> <<" ">>)
|
||||
catch
|
||||
:error, _ ->
|
||||
# Not a stacktrace, was an exit.
|
||||
format_exit_reason(reason)
|
||||
end
|
||||
end
|
||||
|
||||
# :supervisor.start_link returns this error reason when it fails to init
|
||||
# because a child's start_link raises.
|
||||
defp format_exit({:shutdown,
|
||||
{:failed_to_start_child, child, {:EXIT, reason}}}, joiner) do
|
||||
format_start_child(child, reason, joiner)
|
||||
end
|
||||
|
||||
# :supervisor.start_link returns this error reason when it fails to init
|
||||
# because a child's start_link returns {:error, reason}.
|
||||
defp format_exit({:shutdown, {:failed_to_start_child, child, reason}},
|
||||
joiner) do
|
||||
format_start_child(child, reason, joiner)
|
||||
end
|
||||
|
||||
# 2-Tuple could be an exit caused by mfa if second element is mfa, args
|
||||
# must be a list of arguments - max length 255 due to max arity.
|
||||
defp format_exit({reason2, {mod, fun, args}} = reason, joiner)
|
||||
when length(args) < 256 do
|
||||
try do
|
||||
format_mfa(mod, fun, args)
|
||||
else
|
||||
mfa ->
|
||||
# Assume tuple formattable as an mfa is an mfa, so exit was caused by
|
||||
# failed mfa.
|
||||
"exited in: " <> mfa <> joiner <>
|
||||
"** (EXIT) " <> format_exit(reason2, joiner <> <<" ">>)
|
||||
catch
|
||||
:error, _ ->
|
||||
# Not an mfa, was an exit.
|
||||
format_exit_reason(reason)
|
||||
end
|
||||
end
|
||||
|
||||
defp format_exit(reason, _joiner) do
|
||||
format_exit_reason(reason)
|
||||
end
|
||||
|
||||
defp format_exit_reason(:normal), do: "normal"
|
||||
defp format_exit_reason(:shutdown), do: "shutdown"
|
||||
|
||||
defp format_exit_reason({:shutdown, reason}) do
|
||||
"shutdown: #{inspect(reason)}"
|
||||
end
|
||||
|
||||
defp format_exit_reason(:timeout), do: "time out"
|
||||
defp format_exit_reason(:killed), do: "killed"
|
||||
defp format_exit_reason(:noconnection), do: "no connection"
|
||||
|
||||
defp format_exit_reason(:noproc) do
|
||||
"no process"
|
||||
end
|
||||
|
||||
defp format_exit_reason({:nodedown, node_name}) when is_atom(node_name) do
|
||||
"no connection to #{node_name}"
|
||||
end
|
||||
|
||||
# :gen_server exit reasons
|
||||
|
||||
defp format_exit_reason({:already_started, pid}) do
|
||||
"already started: " <> inspect(pid)
|
||||
end
|
||||
|
||||
defp format_exit_reason({:bad_return_value, value}) do
|
||||
"bad return value: " <> inspect(value)
|
||||
end
|
||||
|
||||
defp format_exit_reason({:bad_call, request}) do
|
||||
"bad call: " <> inspect(request)
|
||||
end
|
||||
|
||||
defp format_exit_reason({:bad_cast, request}) do
|
||||
"bad cast: " <> inspect(request)
|
||||
end
|
||||
|
||||
# :supervisor.start_link error reasons
|
||||
|
||||
# If value is a list will be formatted by mfa exit in format_exit/1
|
||||
defp format_exit_reason({:bad_return, {mod, :init, value}})
|
||||
when is_atom(mod) do
|
||||
format_mfa(mod, :init, 1) <> " returned a bad value: " <> inspect(value)
|
||||
end
|
||||
|
||||
defp format_exit_reason({:bad_start_spec, start_spec}) do
|
||||
"bad start spec: invalid children: " <> inspect(start_spec)
|
||||
end
|
||||
|
||||
defp format_exit_reason({:start_spec, start_spec}) do
|
||||
"bad start spec: " <> format_sup_spec(start_spec)
|
||||
end
|
||||
|
||||
defp format_exit_reason({:supervisor_data, data}) do
|
||||
"bad supervisor data: " <> format_sup_data(data)
|
||||
end
|
||||
|
||||
defp format_exit_reason(reason), do: inspect(reason)
|
||||
|
||||
defp format_start_child(child, reason, joiner) do
|
||||
"shutdown: failed to start child: " <> inspect(child) <> joiner <>
|
||||
"** (EXIT) " <> format_exit(reason, joiner <> <<" ">>)
|
||||
end
|
||||
|
||||
defp format_sup_data({:invalid_type, type}) do
|
||||
"invalid type: " <> inspect(type)
|
||||
end
|
||||
|
||||
defp format_sup_data({:invalid_strategy, strategy}) do
|
||||
"invalid strategy: " <> inspect(strategy)
|
||||
end
|
||||
|
||||
defp format_sup_data({:invalid_intensity, intensity}) do
|
||||
"invalid intensity: " <> inspect(intensity)
|
||||
end
|
||||
|
||||
defp format_sup_data({:invalid_period, period}) do
|
||||
"invalid period: " <> inspect(period)
|
||||
end
|
||||
|
||||
defp format_sup_data(other), do: inspect(other)
|
||||
|
||||
defp format_sup_spec({:invalid_child_spec, child_spec}) do
|
||||
"invalid child spec: " <> inspect(child_spec)
|
||||
end
|
||||
|
||||
defp format_sup_spec({:invalid_child_type, type}) do
|
||||
"invalid child type: " <> inspect(type)
|
||||
end
|
||||
|
||||
defp format_sup_spec({:invalid_mfa, mfa}) do
|
||||
"invalid mfa: " <> inspect(mfa)
|
||||
end
|
||||
|
||||
defp format_sup_spec({:invalid_restart_type, restart}) do
|
||||
"invalid restart type: " <> inspect(restart)
|
||||
end
|
||||
|
||||
defp format_sup_spec({:invalid_shutdown, shutdown}) do
|
||||
"invalid shutdown: " <> inspect(shutdown)
|
||||
end
|
||||
|
||||
defp format_sup_spec({:invalid_module, mod}) do
|
||||
"invalid module: " <> inspect(mod)
|
||||
end
|
||||
|
||||
defp format_sup_spec({:invalid_modules, modules}) do
|
||||
"invalid modules: " <> inspect(modules)
|
||||
end
|
||||
|
||||
defp format_sup_spec(other), do: inspect(other)
|
||||
|
||||
@doc """
|
||||
Receives a stacktrace entry and formats it into a string.
|
||||
"""
|
||||
@spec format_stacktrace_entry(stacktrace_entry) :: String.t
|
||||
def format_stacktrace_entry(entry)
|
||||
|
||||
# From Macro.Env.stacktrace
|
||||
def format_stacktrace_entry({module, :__MODULE__, 0, location}) do
|
||||
format_location(location) <> inspect(module) <> " (module)"
|
||||
end
|
||||
|
||||
# From :elixir_compiler_*
|
||||
def format_stacktrace_entry({_module, :__MODULE__, 1, location}) do
|
||||
format_location(location) <> "(module)"
|
||||
end
|
||||
|
||||
# From :elixir_compiler_*
|
||||
def format_stacktrace_entry({_module, :__FILE__, 1, location}) do
|
||||
format_location(location) <> "(file)"
|
||||
end
|
||||
|
||||
def format_stacktrace_entry({module, fun, arity, location}) do
|
||||
format_application(module) <> format_location(location) <> format_mfa(module, fun, arity)
|
||||
end
|
||||
|
||||
def format_stacktrace_entry({fun, arity, location}) do
|
||||
format_location(location) <> format_fa(fun, arity)
|
||||
end
|
||||
|
||||
defp format_application(module) do
|
||||
if app = Application.get_application(module) do
|
||||
"(" <> Atom.to_string(app) <> ") "
|
||||
else
|
||||
""
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats the stacktrace.
|
||||
|
||||
A stacktrace must be given as an argument. If not, the stacktrace
|
||||
is retrieved from `Process.info/2`.
|
||||
"""
|
||||
def format_stacktrace(trace \\ nil) do
|
||||
trace = trace || case Process.info(self, :current_stacktrace) do
|
||||
{:current_stacktrace, t} -> Enum.drop(t, 3)
|
||||
end
|
||||
|
||||
case trace do
|
||||
[] -> "\n"
|
||||
s -> " " <> Enum.map_join(s, "\n ", &format_stacktrace_entry(&1)) <> "\n"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives an anonymous function and arity and formats it as
|
||||
shown in stacktraces. The arity may also be a list of arguments.
|
||||
|
||||
## Examples
|
||||
|
||||
Exception.format_fa(fn -> nil end, 1)
|
||||
#=> "#Function<...>/1"
|
||||
|
||||
"""
|
||||
def format_fa(fun, arity) when is_function(fun) do
|
||||
"#{inspect fun}#{format_arity(arity)}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a module, fun and arity and formats it
|
||||
as shown in stacktraces. The arity may also be a list
|
||||
of arguments.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Exception.format_mfa Foo, :bar, 1
|
||||
"Foo.bar/1"
|
||||
|
||||
iex> Exception.format_mfa Foo, :bar, []
|
||||
"Foo.bar()"
|
||||
|
||||
iex> Exception.format_mfa nil, :bar, []
|
||||
"nil.bar()"
|
||||
|
||||
Anonymous functions are reported as -func/arity-anonfn-count-,
|
||||
where func is the name of the enclosing function. Convert to
|
||||
"anonymous fn in func/arity"
|
||||
"""
|
||||
def format_mfa(module, fun, arity) when is_atom(module) and is_atom(fun) do
|
||||
fun =
|
||||
case inspect(fun) do
|
||||
":" <> fun -> fun
|
||||
fun -> fun
|
||||
end
|
||||
|
||||
case match?("\"-" <> _, fun) and String.split(fun, "-") do
|
||||
[ "\"", outer_fun, "fun", _count, "\"" ] ->
|
||||
"anonymous fn#{format_arity(arity)} in #{inspect module}.#{outer_fun}"
|
||||
_ ->
|
||||
"#{inspect module}.#{fun}#{format_arity(arity)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp format_arity(arity) when is_list(arity) do
|
||||
inspected = for x <- arity, do: inspect(x)
|
||||
"(#{Enum.join(inspected, ", ")})"
|
||||
end
|
||||
|
||||
defp format_arity(arity) when is_integer(arity) do
|
||||
"/" <> Integer.to_string(arity)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats the given `file` and `line` as shown in stacktraces.
|
||||
If any of the values are `nil`, they are omitted.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Exception.format_file_line("foo", 1)
|
||||
"foo:1:"
|
||||
|
||||
iex> Exception.format_file_line("foo", nil)
|
||||
"foo:"
|
||||
|
||||
iex> Exception.format_file_line(nil, nil)
|
||||
""
|
||||
|
||||
Formats the given file and line.
|
||||
"""
|
||||
def format_file_line(file, line) do
|
||||
format_file_line(file, line, "")
|
||||
end
|
||||
|
||||
defp format_file_line(file, line, suffix) do
|
||||
if file do
|
||||
file = to_binary(file)
|
||||
if line && line != 0 do
|
||||
"#{file}:#{line}:#{suffix}"
|
||||
"#{file}:#{line}: "
|
||||
else
|
||||
"#{file}:#{suffix}"
|
||||
"#{file}: "
|
||||
end
|
||||
else
|
||||
""
|
||||
end
|
||||
end
|
||||
|
||||
defp format_location(opts) when is_list(opts) do
|
||||
format_file_line Keyword.get(opts, :file), Keyword.get(opts, :line), " "
|
||||
## Helpers
|
||||
|
||||
defp from_stacktrace([{ module, function, arity, _ }|_]) do
|
||||
[module: module, function: function, arity: arity]
|
||||
end
|
||||
|
||||
defp from_stacktrace(_), do: []
|
||||
end
|
||||
|
||||
# Some exceptions implement "message/1" instead of "exception/1" mostly
|
||||
# for bootstrap reasons. It is recommended for applications to implement
|
||||
# "exception/1" instead of "message/1" as described in "defexception/1"
|
||||
# docs.
|
||||
|
||||
defmodule RuntimeError do
|
||||
defexception message: "runtime error"
|
||||
end
|
||||
|
||||
defmodule ArgumentError do
|
||||
defexception message: "argument error"
|
||||
end
|
||||
|
||||
defmodule ArithmeticError do
|
||||
defexception []
|
||||
|
||||
def message(_) do
|
||||
"bad argument in arithmetic expression"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule SystemLimitError do
|
||||
defexception []
|
||||
|
||||
def message(_) do
|
||||
"a system limit has been reached"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule SyntaxError do
|
||||
defexception [file: nil, line: nil, description: "syntax error"]
|
||||
defexception RuntimeError, message: "runtime error"
|
||||
defexception ArgumentError, message: "argument error"
|
||||
defexception ArithmeticError, message: "bad argument in arithmetic expression"
|
||||
defexception SystemLimitError, message: "a system limit has been reached"
|
||||
|
||||
defexception SyntaxError, [file: nil, line: nil, description: "syntax error"] do
|
||||
def message(exception) do
|
||||
Exception.format_file_line(Path.relative_to_cwd(exception.file), exception.line) <>
|
||||
" " <> exception.description
|
||||
"#{Exception.format_file_line(exception.file, exception.line)}#{exception.description}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule TokenMissingError do
|
||||
defexception [file: nil, line: nil, description: "expression is incomplete"]
|
||||
|
||||
defexception TokenMissingError, [file: nil, line: nil, description: "expression is incomplete"] do
|
||||
def message(exception) do
|
||||
Exception.format_file_line(Path.relative_to_cwd(exception.file), exception.line) <>
|
||||
" " <> exception.description
|
||||
"#{Exception.format_file_line(exception.file, exception.line)}#{exception.description}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule CompileError do
|
||||
defexception [file: nil, line: nil, description: "compile error"]
|
||||
|
||||
defexception CompileError, [file: nil, line: nil, description: "compile error"] do
|
||||
def message(exception) do
|
||||
Exception.format_file_line(Path.relative_to_cwd(exception.file), exception.line) <>
|
||||
" " <> exception.description
|
||||
"#{Exception.format_file_line(exception.file, exception.line)}#{exception.description}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule BadFunctionError do
|
||||
defexception [term: nil]
|
||||
|
||||
defexception BadFunctionError, [actual: nil] do
|
||||
def message(exception) do
|
||||
"expected a function, got: #{inspect(exception.term)}"
|
||||
"bad function: #{inspect(exception.actual)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule BadStructError do
|
||||
defexception [struct: nil, term: nil]
|
||||
|
||||
defexception MatchError, [actual: nil] do
|
||||
def message(exception) do
|
||||
"expected a struct named #{inspect(exception.struct)}, got: #{inspect(exception.term)}"
|
||||
"no match of right hand side value: #{inspect(exception.actual)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule BadMapError do
|
||||
defexception [term: nil]
|
||||
|
||||
defexception CaseClauseError, [actual: nil] do
|
||||
def message(exception) do
|
||||
"expected a map, got: #{inspect(exception.term)}"
|
||||
"no case clause matching: #{inspect(exception.actual)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule MatchError do
|
||||
defexception [term: nil]
|
||||
|
||||
defexception BadArityError, [function: nil, args: nil] do
|
||||
def message(exception) do
|
||||
"no match of right hand side value: #{inspect(exception.term)}"
|
||||
"bad arity error: #{inspect(exception.function)} called with #{inspect(exception.args)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule CaseClauseError do
|
||||
defexception [term: nil]
|
||||
|
||||
defexception UndefinedFunctionError, [module: nil, function: nil, arity: nil] do
|
||||
def message(exception) do
|
||||
"no case clause matching: #{inspect(exception.term)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule CondClauseError do
|
||||
defexception []
|
||||
|
||||
def message(_exception) do
|
||||
"no cond clause evaluated to a true value"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule TryClauseError do
|
||||
defexception [term: nil]
|
||||
|
||||
def message(exception) do
|
||||
"no try clause matching: #{inspect(exception.term)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule BadArityError do
|
||||
defexception [function: nil, args: nil]
|
||||
|
||||
def message(exception) do
|
||||
fun = exception.function
|
||||
args = exception.args
|
||||
insp = Enum.map_join(args, ", ", &inspect/1)
|
||||
{:arity, arity} = :erlang.fun_info(fun, :arity)
|
||||
"#{inspect(fun)} with arity #{arity} called with #{count(length(args), insp)}"
|
||||
end
|
||||
|
||||
defp count(0, _insp), do: "no arguments"
|
||||
defp count(1, insp), do: "1 argument (#{insp})"
|
||||
defp count(x, insp), do: "#{x} arguments (#{insp})"
|
||||
end
|
||||
|
||||
defmodule UndefinedFunctionError do
|
||||
defexception [module: nil, function: nil, arity: nil, reason: nil]
|
||||
|
||||
def message(%{reason: nil, module: module, function: function, arity: arity} = e) do
|
||||
cond do
|
||||
is_nil(function) or is_nil(arity) ->
|
||||
"undefined function"
|
||||
not is_nil(module) and :code.is_loaded(module) === false ->
|
||||
message(%{e | reason: :"module could not be loaded"})
|
||||
true ->
|
||||
message(%{e | reason: :"function not exported"})
|
||||
if exception.function do
|
||||
formatted = Exception.format_module_fun_arity exception.module, exception.function, to_arity(exception.arity)
|
||||
"undefined function: #{formatted}"
|
||||
else
|
||||
"undefined function"
|
||||
end
|
||||
end
|
||||
|
||||
def message(%{reason: :"module could not be loaded", module: module, function: function, arity: arity}) do
|
||||
"undefined function " <> Exception.format_mfa(module, function, arity) <>
|
||||
" (module #{inspect module} is not available)"
|
||||
end
|
||||
|
||||
def message(%{reason: :"function not exported", module: module, function: function, arity: arity}) do
|
||||
"undefined function " <> Exception.format_mfa(module, function, arity)
|
||||
end
|
||||
|
||||
def message(%{reason: :"function not available", module: module, function: function, arity: arity}) do
|
||||
"nil." <> fa = Exception.format_mfa(nil, function, arity)
|
||||
"undefined function " <> Exception.format_mfa(module, function, arity) <>
|
||||
" (function #{fa} is not available)"
|
||||
end
|
||||
|
||||
def message(%{reason: reason, module: module, function: function, arity: arity}) do
|
||||
"undefined function " <> Exception.format_mfa(module, function, arity) <> " (#{reason})"
|
||||
end
|
||||
defp to_arity(arity) when is_integer(arity), do: arity
|
||||
defp to_arity(list) when is_list(list), do: length(list)
|
||||
end
|
||||
|
||||
defmodule FunctionClauseError do
|
||||
defexception [module: nil, function: nil, arity: nil]
|
||||
|
||||
defexception FunctionClauseError, [module: nil, function: nil, arity: nil] do
|
||||
def message(exception) do
|
||||
if exception.function do
|
||||
formatted = Exception.format_mfa exception.module, exception.function, exception.arity
|
||||
"no function clause matching in #{formatted}"
|
||||
formatted = Exception.format_module_fun_arity exception.module, exception.function, exception.arity
|
||||
"no function clause matching: #{formatted}"
|
||||
else
|
||||
"no function clause matches"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Code.LoadError do
|
||||
defexception [:file, :message]
|
||||
|
||||
def exception(opts) do
|
||||
file = Keyword.fetch!(opts, :file)
|
||||
%Code.LoadError{message: "could not load #{file}", file: file}
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Protocol.UndefinedError do
|
||||
defexception [protocol: nil, value: nil, description: nil]
|
||||
|
||||
defexception Protocol.UndefinedError, [protocol: nil, structure: nil] do
|
||||
def message(exception) do
|
||||
msg = "protocol #{inspect exception.protocol} not implemented for #{inspect exception.value}"
|
||||
if exception.description do
|
||||
msg <> ", " <> exception.description
|
||||
else
|
||||
msg
|
||||
end
|
||||
"protocol #{inspect exception.protocol} not implemented for #{inspect exception.structure}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule KeyError do
|
||||
defexception key: nil, term: nil
|
||||
|
||||
def message(exception) do
|
||||
msg = "key #{inspect exception.key} not found"
|
||||
if exception.term != nil do
|
||||
msg <> " in: #{inspect exception.term}"
|
||||
else
|
||||
msg
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule UnicodeConversionError do
|
||||
defexception [:encoded, :message]
|
||||
|
||||
def exception(opts) do
|
||||
%UnicodeConversionError{
|
||||
encoded: Keyword.fetch!(opts, :encoded),
|
||||
message: "#{Keyword.fetch!(opts, :kind)} #{detail Keyword.fetch!(opts, :rest)}"
|
||||
}
|
||||
end
|
||||
|
||||
defp detail(rest) when is_binary(rest) do
|
||||
"encoding starting at #{inspect rest}"
|
||||
end
|
||||
|
||||
defp detail([h|_]) when is_integer(h) do
|
||||
"code point #{h}"
|
||||
end
|
||||
|
||||
defp detail([h|_]) do
|
||||
detail(h)
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Enum.OutOfBoundsError do
|
||||
defexception []
|
||||
|
||||
def message(_) do
|
||||
"out of bounds error"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Enum.EmptyError do
|
||||
defexception []
|
||||
|
||||
def message(_) do
|
||||
"empty error"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule File.Error do
|
||||
defexception [reason: nil, action: "", path: nil]
|
||||
|
||||
def message(exception) do
|
||||
formatted = IO.iodata_to_binary(:file.format_error(exception.reason))
|
||||
"could not #{exception.action} #{exception.path}: #{formatted}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule File.CopyError do
|
||||
defexception [reason: nil, action: "", source: nil, destination: nil, on: nil]
|
||||
|
||||
def message(exception) do
|
||||
formatted = IO.iodata_to_binary(:file.format_error(exception.reason))
|
||||
location = if on = exception.on, do: ". #{on}", else: ""
|
||||
"could not #{exception.action} from #{exception.source} to " <>
|
||||
"#{exception.destination}#{location}: #{formatted}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule ErlangError do
|
||||
defexception [original: nil]
|
||||
|
||||
defexception ErlangError, [original: nil] do
|
||||
def message(exception) do
|
||||
"erlang error: #{inspect(exception.original)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def normalize(:badarg, _stacktrace) do
|
||||
%ArgumentError{}
|
||||
end
|
||||
|
||||
def normalize(:badarith, _stacktrace) do
|
||||
%ArithmeticError{}
|
||||
end
|
||||
|
||||
def normalize(:system_limit, _stacktrace) do
|
||||
%SystemLimitError{}
|
||||
end
|
||||
|
||||
def normalize(:cond_clause, _stacktrace) do
|
||||
%CondClauseError{}
|
||||
end
|
||||
|
||||
def normalize({:badarity, {fun, args}}, _stacktrace) do
|
||||
%BadArityError{function: fun, args: args}
|
||||
end
|
||||
|
||||
def normalize({:badfun, term}, _stacktrace) do
|
||||
%BadFunctionError{term: term}
|
||||
end
|
||||
|
||||
def normalize({:badstruct, struct, term}, _stacktrace) do
|
||||
%BadStructError{struct: struct, term: term}
|
||||
end
|
||||
|
||||
def normalize({:badmatch, term}, _stacktrace) do
|
||||
%MatchError{term: term}
|
||||
end
|
||||
|
||||
def normalize({:badmap, term}, _stacktrace) do
|
||||
%BadMapError{term: term}
|
||||
end
|
||||
|
||||
def normalize({:badkey, key}, stacktrace) do
|
||||
term =
|
||||
case stacktrace || :erlang.get_stacktrace do
|
||||
[{Map, :get_and_update!, [map, _, _], _}|_] -> map
|
||||
[{Map, :update!, [map, _, _], _}|_] -> map
|
||||
[{:maps, :update, [_, _, map], _}|_] -> map
|
||||
[{:maps, :get, [_, map], _}|_] -> map
|
||||
_ -> nil
|
||||
end
|
||||
%KeyError{key: key, term: term}
|
||||
end
|
||||
|
||||
def normalize({:badkey, key, map}, _stacktrace) do
|
||||
%KeyError{key: key, term: map}
|
||||
end
|
||||
|
||||
def normalize({:case_clause, term}, _stacktrace) do
|
||||
%CaseClauseError{term: term}
|
||||
end
|
||||
|
||||
def normalize({:try_clause, term}, _stacktrace) do
|
||||
%TryClauseError{term: term}
|
||||
end
|
||||
|
||||
def normalize(:undef, stacktrace) do
|
||||
stacktrace = stacktrace || :erlang.get_stacktrace
|
||||
{mod, fun, arity} = from_stacktrace(stacktrace)
|
||||
%UndefinedFunctionError{module: mod, function: fun, arity: arity}
|
||||
end
|
||||
|
||||
def normalize(:function_clause, stacktrace) do
|
||||
{mod, fun, arity} = from_stacktrace(stacktrace || :erlang.get_stacktrace)
|
||||
%FunctionClauseError{module: mod, function: fun, arity: arity}
|
||||
end
|
||||
|
||||
def normalize({:badarg, payload}, _stacktrace) do
|
||||
%ArgumentError{message: "argument error: #{inspect(payload)}"}
|
||||
end
|
||||
|
||||
def normalize(other, _stacktrace) do
|
||||
%ErlangError{original: other}
|
||||
end
|
||||
|
||||
defp from_stacktrace([{module, function, args, _}|_]) when is_list(args) do
|
||||
{module, function, length(args)}
|
||||
end
|
||||
|
||||
defp from_stacktrace([{module, function, arity, _}|_]) do
|
||||
{module, function, arity}
|
||||
end
|
||||
|
||||
defp from_stacktrace(_) do
|
||||
{nil, nil, nil}
|
||||
defexception Keyword.KeyError, key: nil do
|
||||
def message(exception) do
|
||||
"key not found: #{inspect exception.key}"
|
||||
end
|
||||
end
|
||||
|
||||
defexception Enum.OutOfBoundsError, message: "out of bounds error"
|
||||
+741
-846
File diff suppressed because it is too large
Load Diff
@@ -1,77 +0,0 @@
|
||||
require Record
|
||||
|
||||
defmodule File.Stat do
|
||||
@moduledoc """
|
||||
A struct that holds file information.
|
||||
|
||||
In Erlang, this struct is represented by a `:file_info` record.
|
||||
Therefore this module also provides functions for converting
|
||||
between the Erlang record and the Elixir struct.
|
||||
|
||||
Its fields are:
|
||||
|
||||
* `size` - size of file in bytes.
|
||||
|
||||
* `type` - `:device | :directory | :regular | :other`; the type of the
|
||||
file.
|
||||
|
||||
* `access` - `:read | :write | :read_write | :none`; the current system
|
||||
access to the file.
|
||||
|
||||
* `atime` - the last time the file was read.
|
||||
|
||||
* `mtime` - the last time the file was written.
|
||||
|
||||
* `ctime` - the interpretation of this time field depends on the operating
|
||||
system. On Unix, it is the last time the file or the inode was changed.
|
||||
In Windows, it is the time of creation.
|
||||
|
||||
* `mode` - the file permissions.
|
||||
|
||||
* `links` - the number of links to this file. This is always 1 for file
|
||||
systems which have no concept of links.
|
||||
|
||||
* `major_device` - identifies the file system where the file is located.
|
||||
In windows, the number indicates a drive as follows: 0 means A:, 1 means
|
||||
B:, and so on.
|
||||
|
||||
* `minor_device` - only valid for character devices on Unix. In all other
|
||||
cases, this field is zero.
|
||||
|
||||
* `inode` - gives the inode number. On non-Unix file systems, this field
|
||||
will be zero.
|
||||
|
||||
* `uid` - indicates the owner of the file. Will be zero for non-Unix file
|
||||
systems.
|
||||
|
||||
* `gid` - indicates the group that owns the file. Will be zero for
|
||||
non-Unix file systems.
|
||||
|
||||
The time type returned in `atime`, `mtime`, and `ctime` is dependent on the
|
||||
time type set in options. `{:time, type}` where type can be `:local`,
|
||||
`:universal`, or `:posix`. Default is `:universal`.
|
||||
"""
|
||||
|
||||
record = Record.extract(:file_info, from_lib: "kernel/include/file.hrl")
|
||||
keys = :lists.map(&elem(&1, 0), record)
|
||||
vals = :lists.map(&{&1, [], nil}, keys)
|
||||
pairs = :lists.zip(keys, vals)
|
||||
|
||||
defstruct keys
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
@doc """
|
||||
Converts a `File.Stat` struct to a `:file_info` record.
|
||||
"""
|
||||
def to_record(%File.Stat{unquote_splicing(pairs)}) do
|
||||
{:file_info, unquote_splicing(vals)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a `:file_info` record into a `File.Stat`.
|
||||
"""
|
||||
def from_record(file_info)
|
||||
def from_record({:file_info, unquote_splicing(vals)}) do
|
||||
%File.Stat{unquote_splicing(pairs)}
|
||||
end
|
||||
end
|
||||
@@ -1,98 +0,0 @@
|
||||
defmodule File.Stream do
|
||||
@moduledoc """
|
||||
Defines a `File.Stream` struct returned by `File.stream!/3`.
|
||||
|
||||
The following fields are public:
|
||||
|
||||
* `path` - the file path
|
||||
* `modes` - the file modes
|
||||
* `raw` - a boolean indicating if bin functions should be used
|
||||
* `line_or_bytes` - if reading should read lines or a given amount of bytes
|
||||
|
||||
"""
|
||||
|
||||
defstruct path: nil, modes: [], line_or_bytes: :line, raw: true
|
||||
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
@doc false
|
||||
def __build__(path, modes, line_or_bytes) do
|
||||
raw = :lists.keyfind(:encoding, 1, modes) == false
|
||||
|
||||
modes =
|
||||
if raw do
|
||||
if :lists.keyfind(:read_ahead, 1, modes) == {:read_ahead, false} do
|
||||
[:raw|modes]
|
||||
else
|
||||
[:raw, :read_ahead|modes]
|
||||
end
|
||||
else
|
||||
modes
|
||||
end
|
||||
|
||||
%File.Stream{path: path, modes: modes, raw: raw, line_or_bytes: line_or_bytes}
|
||||
end
|
||||
|
||||
defimpl Collectable do
|
||||
def into(%{path: path, modes: modes, raw: raw} = stream) do
|
||||
modes = for mode <- modes, not mode in [:read], do: mode
|
||||
|
||||
case :file.open(path, [:write|modes]) do
|
||||
{:ok, device} ->
|
||||
{:ok, into(device, stream, raw)}
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "stream", path: path
|
||||
end
|
||||
end
|
||||
|
||||
defp into(device, stream, raw) do
|
||||
fn
|
||||
:ok, {:cont, x} ->
|
||||
case raw do
|
||||
true -> IO.binwrite(device, x)
|
||||
false -> IO.write(device, x)
|
||||
end
|
||||
:ok, :done ->
|
||||
# If delayed_write option is used and the last write failed will
|
||||
# MatchError here as {:error, _} is returned.
|
||||
:ok = :file.close(device)
|
||||
stream
|
||||
:ok, :halt ->
|
||||
# If delayed_write option is used and the last write failed will
|
||||
# MatchError here as {:error, _} is returned.
|
||||
:ok = :file.close(device)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Enumerable do
|
||||
def reduce(%{path: path, modes: modes, line_or_bytes: line_or_bytes, raw: raw}, acc, fun) do
|
||||
modes = for mode <- modes, not mode in [:write, :append], do: mode
|
||||
|
||||
start_fun =
|
||||
fn ->
|
||||
case :file.open(path, modes) do
|
||||
{:ok, device} -> device
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "stream", path: path
|
||||
end
|
||||
end
|
||||
|
||||
next_fun =
|
||||
case raw do
|
||||
true -> &IO.each_binstream(&1, line_or_bytes)
|
||||
false -> &IO.each_stream(&1, line_or_bytes)
|
||||
end
|
||||
|
||||
Stream.resource(start_fun, next_fun, &:file.close/1).(acc, fun)
|
||||
end
|
||||
|
||||
def count(_stream) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
|
||||
def member?(_stream, _term) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,251 +0,0 @@
|
||||
import Kernel, except: [round: 1]
|
||||
|
||||
defmodule Float do
|
||||
@moduledoc """
|
||||
Functions for working with floating point numbers.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Parses a binary into a float.
|
||||
|
||||
If successful, returns a tuple of the form `{float, remainder_of_binary}`;
|
||||
when the binary cannot be coerced into a valid float, the atom `:error` is
|
||||
returned.
|
||||
|
||||
If the size of float exceeds the maximum size of `1.7976931348623157e+308`,
|
||||
the `ArgumentError` exception is raised.
|
||||
|
||||
If a float formatted string wants to be directly converted to a float,
|
||||
`String.to_float/1` can be used instead.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.parse("34")
|
||||
{34.0, ""}
|
||||
|
||||
iex> Float.parse("34.25")
|
||||
{34.25, ""}
|
||||
|
||||
iex> Float.parse("56.5xyz")
|
||||
{56.5, "xyz"}
|
||||
|
||||
iex> Float.parse("pi")
|
||||
:error
|
||||
|
||||
"""
|
||||
@spec parse(binary) :: {float, binary} | :error
|
||||
def parse("-" <> binary) do
|
||||
case parse_unsigned(binary) do
|
||||
:error -> :error
|
||||
{number, remainder} -> {-number, remainder}
|
||||
end
|
||||
end
|
||||
|
||||
def parse("+" <> binary) do
|
||||
parse_unsigned(binary)
|
||||
end
|
||||
|
||||
def parse(binary) do
|
||||
parse_unsigned(binary)
|
||||
end
|
||||
|
||||
defp parse_unsigned(<<digit, rest::binary>>) when digit in ?0..?9, do:
|
||||
parse_unsigned(rest, false, false, <<digit>>)
|
||||
|
||||
defp parse_unsigned(binary) when is_binary(binary), do:
|
||||
:error
|
||||
|
||||
defp parse_unsigned(<<digit, rest::binary>>, dot?, e?, acc) when digit in ?0..?9, do:
|
||||
parse_unsigned(rest, dot?, e?, <<acc::binary, digit>>)
|
||||
|
||||
defp parse_unsigned(<<?., digit, rest::binary>>, false, false, acc) when digit in ?0..?9, do:
|
||||
parse_unsigned(rest, true, false, <<acc::binary, ?., digit>>)
|
||||
|
||||
defp parse_unsigned(<<exp_marker, digit, rest::binary>>, dot?, false, acc) when exp_marker in 'eE' and digit in ?0..?9, do:
|
||||
parse_unsigned(rest, true, true, <<add_dot(acc, dot?)::binary, ?e, digit>>)
|
||||
|
||||
defp parse_unsigned(<<exp_marker, sign, digit, rest::binary>>, dot?, false, acc) when exp_marker in 'eE' and sign in '-+' and digit in ?0..?9, do:
|
||||
parse_unsigned(rest, true, true, <<add_dot(acc, dot?)::binary, ?e, sign, digit>>)
|
||||
|
||||
defp parse_unsigned(rest, dot?, _e?, acc), do:
|
||||
{:erlang.binary_to_float(add_dot(acc, dot?)), rest}
|
||||
|
||||
defp add_dot(acc, true), do: acc
|
||||
defp add_dot(acc, false), do: acc <> ".0"
|
||||
|
||||
@doc """
|
||||
Rounds a float to the largest integer less than or equal to `num`.
|
||||
|
||||
`floor/2` also accepts a precision to round a floating point value down
|
||||
to an arbitrary number of fractional digits (between 0 and 15).
|
||||
|
||||
This function always returns a float. `Kernel.trunc/1` may be used instead to
|
||||
truncate the result to an integer afterwards.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.floor(34.25)
|
||||
34.0
|
||||
|
||||
iex> Float.floor(-56.5)
|
||||
-57.0
|
||||
|
||||
iex> Float.floor(34.259, 2)
|
||||
34.25
|
||||
|
||||
"""
|
||||
@spec floor(float, 0..15) :: float
|
||||
def floor(number, precision \\ 0) when is_float(number) and precision in 0..15 do
|
||||
power = power_of_10(precision)
|
||||
number = number * power
|
||||
truncated = trunc(number)
|
||||
variance = if number - truncated < 0, do: -1.0, else: 0.0
|
||||
(truncated + variance) / power
|
||||
end
|
||||
|
||||
@doc """
|
||||
Rounds a float to the smallest integer greater than or equal to `num`.
|
||||
|
||||
`ceil/2` also accepts a precision to round a floating point value down
|
||||
to an arbitrary number of fractional digits (between 0 and 15).
|
||||
|
||||
This function always returns floats. `Kernel.trunc/1` may be used instead to
|
||||
truncate the result to an integer afterwards.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.ceil(34.25)
|
||||
35.0
|
||||
|
||||
iex> Float.ceil(-56.5)
|
||||
-56.0
|
||||
|
||||
iex> Float.ceil(34.251, 2)
|
||||
34.26
|
||||
|
||||
"""
|
||||
@spec ceil(float, 0..15) :: float
|
||||
def ceil(number, precision \\ 0) when is_float(number) and precision in 0..15 do
|
||||
power = power_of_10(precision)
|
||||
number = number * power
|
||||
truncated = trunc(number)
|
||||
variance = if number - truncated > 0, do: 1.0, else: 0.0
|
||||
(truncated + variance) / power
|
||||
end
|
||||
|
||||
@doc """
|
||||
Rounds a floating point value to an arbitrary number of fractional digits
|
||||
(between 0 and 15).
|
||||
|
||||
This function only accepts floats and always returns a float. Use
|
||||
`Kernel.round/1` if you want a function that accepts both floats and integers
|
||||
and always returns an integer.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.round(5.5674, 3)
|
||||
5.567
|
||||
|
||||
iex> Float.round(5.5675, 3)
|
||||
5.568
|
||||
|
||||
iex> Float.round(-5.5674, 3)
|
||||
-5.567
|
||||
|
||||
iex> Float.round(-5.5675, 3)
|
||||
-5.568
|
||||
|
||||
"""
|
||||
@spec round(float, 0..15) :: float
|
||||
def round(number, precision \\ 0) when is_float(number) and precision in 0..15 do
|
||||
power = power_of_10(precision)
|
||||
Kernel.round(number * power) / power
|
||||
end
|
||||
|
||||
Enum.reduce 0..15, 1, fn x, acc ->
|
||||
defp power_of_10(unquote(x)), do: unquote(acc)
|
||||
acc * 10
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a char list which corresponds to the text representation of the given float.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_char_list(7.0)
|
||||
'7.00000000000000000000e+00'
|
||||
|
||||
"""
|
||||
@spec to_char_list(float) :: char_list
|
||||
def to_char_list(float) do
|
||||
:erlang.float_to_list(float)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list which corresponds to the text representation
|
||||
of the given float.
|
||||
|
||||
## Options
|
||||
|
||||
* `:decimals` - number of decimal points to show
|
||||
* `:scientific` - number of decimal points to show, in scientific format
|
||||
* `:compact` - when `true`, use the most compact representation (ignored
|
||||
with the `scientific` option)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_char_list 7.1, [decimals: 2, compact: true]
|
||||
'7.1'
|
||||
|
||||
"""
|
||||
@spec to_char_list(float, list) :: char_list
|
||||
def to_char_list(float, options) do
|
||||
:erlang.float_to_list(float, expand_compact(options))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of the given float.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_string(7.0)
|
||||
"7.00000000000000000000e+00"
|
||||
|
||||
"""
|
||||
@spec to_string(float) :: String.t
|
||||
def to_string(float) do
|
||||
:erlang.float_to_binary(float)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `float`.
|
||||
|
||||
## Options
|
||||
|
||||
* `:decimals` - number of decimal points to show
|
||||
* `:scientific` - number of decimal points to show, in scientific format
|
||||
* `:compact` - when `true`, use the most compact representation (ignored
|
||||
with the `scientific` option)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_string 7.1, [decimals: 2, compact: true]
|
||||
"7.1"
|
||||
|
||||
"""
|
||||
@spec to_string(float, list) :: String.t
|
||||
def to_string(float, options) do
|
||||
:erlang.float_to_binary(float, expand_compact(options))
|
||||
end
|
||||
|
||||
defp expand_compact([{:compact, false}|t]), do: expand_compact(t)
|
||||
defp expand_compact([{:compact, true}|t]), do: [:compact|expand_compact(t)]
|
||||
defp expand_compact([h|t]), do: [h|expand_compact(t)]
|
||||
defp expand_compact([]), do: []
|
||||
end
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,171 +0,0 @@
|
||||
defmodule GenEvent.Stream do
|
||||
@moduledoc """
|
||||
Defines a `GenEvent` stream.
|
||||
|
||||
This is a struct returned by `GenEvent.stream/2`. The struct is public and
|
||||
contains the following fields:
|
||||
|
||||
* `:manager` - the manager reference given to `GenEvent.stream/2`
|
||||
* `:timeout` - the timeout between events, defaults to `:infinity`
|
||||
|
||||
"""
|
||||
defstruct manager: nil, timeout: :infinity
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
manager: GenEvent.manager,
|
||||
timeout: timeout}
|
||||
|
||||
@doc false
|
||||
def init({_pid, _ref} = state) do
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_event(event, _state) do
|
||||
# We do this to trick dialyzer to not complain about non-local returns.
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit({:bad_event, event})
|
||||
1 -> :remove_handler
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_call(msg, _state) do
|
||||
# We do this to trick dialyzer to not complain about non-local returns.
|
||||
reason = {:bad_call, msg}
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit(reason)
|
||||
1 -> {:remove_handler, reason}
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(_msg, state) do
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def terminate(_reason, _state) do
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc false
|
||||
def code_change(_old, state, _extra) do
|
||||
{:ok, state}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Enumerable, for: GenEvent.Stream do
|
||||
def reduce(stream, acc, fun) do
|
||||
start_fun = fn() -> start(stream) end
|
||||
next_fun = &next(stream, &1)
|
||||
stop_fun = &stop(stream, &1)
|
||||
Stream.resource(start_fun, next_fun, stop_fun).(acc, wrap_reducer(fun))
|
||||
end
|
||||
|
||||
def count(_stream) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
|
||||
def member?(_stream, _item) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
|
||||
defp wrap_reducer(fun) do
|
||||
fn
|
||||
{:ack, manager, ref, event}, acc ->
|
||||
send manager, {ref, :ok}
|
||||
fun.(event, acc)
|
||||
{:async, _manager, _ref, event}, acc ->
|
||||
fun.(event, acc)
|
||||
{:sync, manager, ref, event}, acc ->
|
||||
try do
|
||||
fun.(event, acc)
|
||||
after
|
||||
send manager, {ref, :ok}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp start(%{manager: manager} = stream) do
|
||||
try do
|
||||
{:ok, {pid, ref}} = :gen.call(manager, self(),
|
||||
{:add_process_handler, self(), self()}, :infinity)
|
||||
mon_ref = Process.monitor(pid)
|
||||
{pid, ref, mon_ref}
|
||||
catch
|
||||
:exit, reason -> exit({reason, {__MODULE__, :start, [stream]}})
|
||||
end
|
||||
end
|
||||
|
||||
defp next(%{timeout: timeout} = stream, {pid, ref, mon_ref} = acc) do
|
||||
self = self()
|
||||
|
||||
receive do
|
||||
# Got an async event.
|
||||
{_from, {^pid, ^ref}, {:notify, event}} ->
|
||||
{[{:async, pid, ref, event}], acc}
|
||||
|
||||
# Got a sync event.
|
||||
{_from, {^pid, ^ref}, {:sync_notify, event}} ->
|
||||
{[{:sync, pid, ref, event}], acc}
|
||||
|
||||
# Got an ack event.
|
||||
{_from, {^pid, ^ref}, {:ack_notify, event}} ->
|
||||
{[{:ack, pid, ref, event}], acc}
|
||||
|
||||
# The handler was removed. Stop iteration, resolve the
|
||||
# event later. We need to demonitor now, otherwise DOWN
|
||||
# appears with higher priority in the shutdown process.
|
||||
{:gen_event_EXIT, {^pid, ^ref}, _reason} = event ->
|
||||
Process.demonitor(mon_ref, [:flush])
|
||||
send(self, event)
|
||||
{:halt, {:removed, acc}}
|
||||
|
||||
# The manager died. Stop iteration, resolve the event later.
|
||||
{:DOWN, ^mon_ref, _, _, _} = event ->
|
||||
send(self, event)
|
||||
{:halt, {:removed, acc}}
|
||||
after
|
||||
timeout ->
|
||||
exit({:timeout, {__MODULE__, :next, [stream, acc]}})
|
||||
end
|
||||
end
|
||||
|
||||
# If we reach this branch, we know the handler was already
|
||||
# removed, so we don't trigger a request for doing so.
|
||||
defp stop(stream, {:removed, {pid, ref, mon_ref} = acc}) do
|
||||
case wait_for_handler_removal(pid, ref, mon_ref) do
|
||||
:ok ->
|
||||
flush_events(ref)
|
||||
{:error, reason} ->
|
||||
exit({reason, {__MODULE__, :stop, [stream, acc]}})
|
||||
end
|
||||
end
|
||||
|
||||
# If we reach this branch, the handler was not removed yet,
|
||||
# so we trigger a request for doing so.
|
||||
defp stop(stream, {pid, ref, _} = acc) do
|
||||
_ = GenEvent.remove_handler(pid, {pid, ref}, :shutdown)
|
||||
stop(stream, {:removed, acc})
|
||||
end
|
||||
|
||||
defp wait_for_handler_removal(pid, ref, mon_ref) do
|
||||
receive do
|
||||
{:gen_event_EXIT, {^pid, ^ref}, _reason} ->
|
||||
Process.demonitor(mon_ref, [:flush])
|
||||
:ok
|
||||
{:DOWN, ^mon_ref, _, _, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp flush_events(ref) do
|
||||
receive do
|
||||
{_from, {_pid, ^ref}, {notify, _event}} when notify in [:notify, :ack_notify, :sync_notify] ->
|
||||
flush_events(ref)
|
||||
after
|
||||
0 -> :ok
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,730 +0,0 @@
|
||||
defmodule GenServer do
|
||||
@moduledoc """
|
||||
A behaviour module for implementing the server of a client-server relation.
|
||||
|
||||
A GenServer is a process like any other Elixir process and it can be used
|
||||
to keep state, execute code asynchronously and so on. The advantage of using
|
||||
a generic server process (GenServer) implemented using this module is that it
|
||||
will have a standard set of interface functions and include functionality for
|
||||
tracing and error reporting. It will also fit into a supervision tree.
|
||||
|
||||
## Example
|
||||
|
||||
The GenServer behaviour abstracts the common client-server interaction.
|
||||
Developers are only required to implement the callbacks and functionality they are
|
||||
interested in.
|
||||
|
||||
Let's start with a code example and then explore the available callbacks.
|
||||
Imagine we want a GenServer that works like a stack, allowing us to push
|
||||
and pop items:
|
||||
|
||||
defmodule Stack do
|
||||
use GenServer
|
||||
|
||||
# Callbacks
|
||||
|
||||
def handle_call(:pop, _from, [h|t]) do
|
||||
{:reply, h, t}
|
||||
end
|
||||
|
||||
def handle_cast({:push, item}, state) do
|
||||
{:noreply, [item|state]}
|
||||
end
|
||||
end
|
||||
|
||||
# Start the server
|
||||
{:ok, pid} = GenServer.start_link(Stack, [:hello])
|
||||
|
||||
# This is the client
|
||||
GenServer.call(pid, :pop)
|
||||
#=> :hello
|
||||
|
||||
GenServer.cast(pid, {:push, :world})
|
||||
#=> :ok
|
||||
|
||||
GenServer.call(pid, :pop)
|
||||
#=> :world
|
||||
|
||||
We start our `Stack` by calling `start_link/3`, passing the module
|
||||
with the server implementation and its initial argument (a list
|
||||
representing the stack containing the item `:hello`). We can primarily
|
||||
interact with the server by sending two types of messages. **call**
|
||||
messages expect a reply from the server (and are therefore synchronous)
|
||||
while **cast** messages do not.
|
||||
|
||||
Every time you do a `GenServer.call/3`, the client will send a message
|
||||
that must be handled by the `handle_call/3` callback in the GenServer.
|
||||
A `cast/2` message must be handled by `handle_cast/2`.
|
||||
|
||||
## Callbacks
|
||||
|
||||
There are 6 callbacks required to be implemented in a `GenServer`. By
|
||||
adding `use GenServer` to your module, Elixir will automatically define
|
||||
all 6 callbacks for you, leaving it up to you to implement the ones
|
||||
you want to customize.
|
||||
|
||||
## Name Registration
|
||||
|
||||
Both `start_link/3` and `start/3` support the `GenServer` to register
|
||||
a name on start via the `:name` option. Registered names are also
|
||||
automatically cleaned up on termination. The supported values are:
|
||||
|
||||
* an atom - the GenServer is registered locally with the given name
|
||||
using `Process.register/2`.
|
||||
|
||||
* `{:global, term}`- the GenServer is registered globally with the given
|
||||
term using the functions in the `:global` module.
|
||||
|
||||
* `{:via, module, term}` - the GenServer is registered with the given
|
||||
mechanism and name. The `:via` option expects a module that exports
|
||||
`register_name/2`, `unregister_name/1`, `whereis_name/1` and `send/2`.
|
||||
One such example is the `:global` module which uses these functions
|
||||
for keeping the list of names of processes and their associated pid's
|
||||
that are available globally for a network of Erlang nodes.
|
||||
|
||||
For example, we could start and register our Stack server locally as follows:
|
||||
|
||||
# Start the server and register it locally with name MyStack
|
||||
{:ok, _} = GenServer.start_link(Stack, [:hello], name: MyStack)
|
||||
|
||||
# Now messages can be sent directly to MyStack
|
||||
GenServer.call(MyStack, :pop) #=> :hello
|
||||
|
||||
Once the server is started, the remaining functions in this module (`call/3`,
|
||||
`cast/2`, and friends) will also accept an atom, or any `:global` or `:via`
|
||||
tuples. In general, the following formats are supported:
|
||||
|
||||
* a `pid`
|
||||
* an `atom` if the server is locally registered
|
||||
* `{atom, node}` if the server is locally registered at another node
|
||||
* `{:global, term}` if the server is globally registered
|
||||
* `{:via, module, name}` if the server is registered through an alternative
|
||||
registry
|
||||
|
||||
## Client / Server APIs
|
||||
|
||||
Although in the example above we have used `GenServer.start_link/3` and
|
||||
friends to directly start and communicate with the server, most of the
|
||||
time we don't call the `GenServer` functions directly. Instead, we wrap
|
||||
the calls in new functions representing the public API of the server.
|
||||
|
||||
Here is a better implementation of our Stack module:
|
||||
|
||||
defmodule Stack do
|
||||
use GenServer
|
||||
|
||||
# Client
|
||||
|
||||
def start_link(default) do
|
||||
GenServer.start_link(__MODULE__, default)
|
||||
end
|
||||
|
||||
def push(pid, item) do
|
||||
GenServer.cast(pid, {:push, item})
|
||||
end
|
||||
|
||||
def pop(pid) do
|
||||
GenServer.call(pid, :pop)
|
||||
end
|
||||
|
||||
# Server (callbacks)
|
||||
|
||||
def handle_call(:pop, _from, [h|t]) do
|
||||
{:reply, h, t}
|
||||
end
|
||||
|
||||
def handle_call(request, from, state) do
|
||||
# Call the default implementation from GenServer
|
||||
super(request, from, state)
|
||||
end
|
||||
|
||||
def handle_cast({:push, item}, state) do
|
||||
{:noreply, [item|state]}
|
||||
end
|
||||
|
||||
def handle_cast(request, state) do
|
||||
super(request, state)
|
||||
end
|
||||
end
|
||||
|
||||
In practice, it is common to have both server and client functions in
|
||||
the same module. If the server and/or client implementations are growing
|
||||
complex, you may want to have them in different modules.
|
||||
|
||||
## Receiving custom messages
|
||||
|
||||
The goal of a `GenServer` is to abstract the "receive" loop for developers,
|
||||
automatically handling system messages, support code change, synchronous
|
||||
calls and more. Therefore, you should never call your own "receive" inside
|
||||
the GenServer callbacks as doing so will cause the GenServer to misbehave.
|
||||
If you want to receive custom messages, always receive them in `handle_info/2`.
|
||||
|
||||
## Learn more
|
||||
|
||||
If you wish to find out more about gen servers, the Elixir Getting Started
|
||||
guide provides a tutorial-like introduction. The documentation and links
|
||||
in Erlang can also provide extra insight.
|
||||
|
||||
* [GenServer – Elixir's Getting Started Guide](http://elixir-lang.org/getting-started/mix-otp/genserver.html)
|
||||
* [`:gen_server` module documentation](http://www.erlang.org/doc/man/gen_server.html)
|
||||
* [gen_server Behaviour – OTP Design Principles](http://www.erlang.org/doc/design_principles/gen_server_concepts.html)
|
||||
* [Clients and Servers – Learn You Some Erlang for Great Good!](http://learnyousomeerlang.com/clients-and-servers)
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Invoked when the server is started. `start_link/3` (or `start/3`) will
|
||||
block until it returns.
|
||||
|
||||
`args` is the argument term (second argument) passed to `start_link/3`.
|
||||
|
||||
Returning `{:ok, state}` will cause `start_link/3` to return
|
||||
`{:ok, pid}` and the process to enter its loop.
|
||||
|
||||
Returning `{:ok, state, timeout}` is similar to `{:ok, state}`
|
||||
except `handle_info(:timeout, state)` will be called after `timeout`
|
||||
milliseconds if no messages are received within the timeout.
|
||||
|
||||
Returning `{:ok, state, :hibernate}` is similar to
|
||||
`{:ok, state}` except the process is hibernated before entering the loop. See
|
||||
`handle_call/3` for more information on hibernation.
|
||||
|
||||
Returning `:ignore` will cause `start_link/3` to return `:ignore` and the
|
||||
process will exit normally without entering the loop or calling `terminate/2`.
|
||||
If used when part of a supervision tree the parent supervisor will not fail
|
||||
to start nor immediately try to restart the `GenServer`. The remainder of the
|
||||
supervision tree will be (re)started and so the `GenServer` should not be
|
||||
required by other processes. It can be started later with
|
||||
`Supervisor.restart_child/2` as the child specification is saved in the parent
|
||||
supervisor. The main use cases for this are:
|
||||
|
||||
- The `GenServer` is disabled by configuration but might be enabled later.
|
||||
- An error occured and it will be handled by a different mechanism than the
|
||||
`Supervisor`. Likely this approach involves calling `Supervisor.restart_child/2`
|
||||
after a delay to attempt a restart.
|
||||
|
||||
Returning `{:stop, reason}` will cause `start_link/3` to return
|
||||
`{:error, reason}` and the process to exit with reason `reason` without
|
||||
entering the loop or calling `terminate/2`.
|
||||
"""
|
||||
@callback init(args :: term) ::
|
||||
{:ok, state} |
|
||||
{:ok, state, timeout | :hibernate} |
|
||||
:ignore |
|
||||
{:stop, reason :: any} when state: any
|
||||
|
||||
@doc """
|
||||
Invoked to handle synchronous `call/3` messages. `call/3` will block until a
|
||||
reply is received (unless the call times out or nodes are disconnected).
|
||||
|
||||
`request` is the request message sent by a `call/3`, `from` is a 2-tuple
|
||||
containing the caller's pid and a term that uniquely identifies the call, and
|
||||
`state` is the current state of the `GenServer`.
|
||||
|
||||
Returning `{:reply, reply, new_state}` sends the response `reply` to the
|
||||
caller and continues the loop with new state `new_state`.
|
||||
|
||||
Returning `{:reply, reply, new_state, timeout}` is similar to
|
||||
`{:reply, reply, new_state}` except `handle_info(:timeout, new_state)` will be
|
||||
called after `timeout` milliseconds if no messages are received.
|
||||
|
||||
Returning `{:reply, reply, new_state, :hibernate}` is similar to
|
||||
`{:reply, reply, new_state}` except the process is hibernated and will
|
||||
continue the loop once a message is its message queue. If a message is already
|
||||
in the message queue this will be immediately. Hibernating a `GenServer`
|
||||
causes garbage collection and leaves a continuous heap that minimises the
|
||||
memory used by the process.
|
||||
|
||||
Hibernating should not be used aggressively as too much time could be spent
|
||||
garbage collecting. Normally it should only be used when a message is not
|
||||
expected soon and minimising the memory of the process is shown to be
|
||||
beneficial.
|
||||
|
||||
Returning `{:noreply, new_state}` does not send a response to the caller and
|
||||
continues the loop with new state `new_state`. The response must be sent with
|
||||
`reply/2`.
|
||||
|
||||
There are three main use cases for not replying using the return value:
|
||||
|
||||
- To reply before returning from the callback because the response is known
|
||||
before calling a slow function.
|
||||
- To reply after returning from the callback because the response is not yet
|
||||
available.
|
||||
- To reply from another process, such as a task.
|
||||
|
||||
When replying from another process the `GenServer` should exit if the other
|
||||
process exits without replying as the caller will be blocking awaiting a
|
||||
reply.
|
||||
|
||||
Returning `{:noreply, new_state, timeout | :hibernate}` is similar to
|
||||
`{:noreply, new_state}` except a timeout or hibernation occurs as with a
|
||||
`:reply` tuple.
|
||||
|
||||
Returning `{:stop, reason, reply, new_state}` stops the loop and `terminate/2`
|
||||
is called with reason `reason` and state `new_state`. Then the `reply` is sent
|
||||
as the response to call and the process exits with reason `reason`.
|
||||
|
||||
Returning `{:stop, reason, new_state}` is similar to
|
||||
`{:stop, reason, reply, new_state}` except a reply is not sent.
|
||||
"""
|
||||
@callback handle_call(request :: term, from, state :: term) ::
|
||||
{:reply, reply, new_state} |
|
||||
{:reply, reply, new_state, timeout | :hibernate} |
|
||||
{:noreply, new_state} |
|
||||
{:noreply, new_state, timeout | :hibernate} |
|
||||
{:stop, reason, reply, new_state} |
|
||||
{:stop, reason, new_state} when reply: term, new_state: term, reason: term
|
||||
|
||||
@doc """
|
||||
Invoked to handle asynchronous `cast/2` messages.
|
||||
|
||||
`request` is the request message sent by a `cast/2` and `state` is the current
|
||||
state of the `GenServer`.
|
||||
|
||||
Returning `{:noreply, new_state}` continues the loop with new state `new_state`.
|
||||
|
||||
Returning `{:noreply, new_state, timeout}` is similar to
|
||||
`{:noreply, reply, new_state}` except `handle_info(:timeout, new_state)` will
|
||||
be called after `timeout` milliseconds if no messages are received.
|
||||
|
||||
Returning `{:noreply, new_state, :hibernate}` is similar to
|
||||
`{:noreply, new_state}` except the process is hibernated before continuing the
|
||||
loop. See `handle_call/3` for more information.
|
||||
|
||||
Returning `{:stop, reason, new_state}` stops the loop and `terminate/2` is
|
||||
called with the reason `reason` and state `new_state`. The process exits with
|
||||
reason `reason`.
|
||||
"""
|
||||
@callback handle_cast(request :: term, state :: term) ::
|
||||
{:noreply, new_state} |
|
||||
{:noreply, new_state, timeout | :hibernate} |
|
||||
{:stop, reason :: term, new_state} when new_state: term
|
||||
|
||||
@doc """
|
||||
Invoked to handle all other messages.
|
||||
|
||||
`msg` is the message and `state` is the current state of the `GenServer`. When
|
||||
a timeout occurs the message is `:timeout`.
|
||||
|
||||
Return values are the same as `handle_cast/2`.
|
||||
"""
|
||||
@callback handle_info(msg :: :timeout | term, state :: term) ::
|
||||
{:noreply, new_state} |
|
||||
{:noreply, new_state, timeout | :hibernate} |
|
||||
{:stop, reason :: term, new_state} when new_state: term
|
||||
|
||||
@doc """
|
||||
Invoked when the server is about to exit. It should do any cleanup required.
|
||||
|
||||
`reason` is exit reason and `state` is the current state of the `GenServer`.
|
||||
The return value is ignored.
|
||||
|
||||
`terminate/2` is called if a callback (except `init/1`) returns a `:stop`
|
||||
tuple, raises, calls `Kernel.exit/1` or returns an invalid value. It may also
|
||||
be called if the `GenServer` traps exits using `Process.flag/2` *and* the
|
||||
parent process sends an exit signal.
|
||||
|
||||
If part of a supervision tree a `GenServer`'s `Supervisor` will send an exit
|
||||
signal when shutting it down. The exit signal is based on the shutdown
|
||||
strategy in the child's specification. If it is `:brutal_kill` the `GenServer`
|
||||
is killed and so `terminate/2` is not called. However if it is a timeout the
|
||||
`Supervisor` will send the exit signal `:shutdown` and the `GenServer` will
|
||||
have the duration of the timeout to call `terminate/2` - if the process is
|
||||
still alive after the timeout it is killed.
|
||||
|
||||
If the `GenServer` receives an exit signal (that is not `:normal`) from any
|
||||
process when it is not trapping exits it will exit abruptly with the same
|
||||
reason and so not call `terminate/2`. Note that a process does *NOT* trap
|
||||
exits by default and an exit signal is sent when a linked process exits or its
|
||||
node is disconnected.
|
||||
|
||||
Therefore it is not guaranteed that `terminate/2` is called when a `GenServer`
|
||||
exits. For such reasons, we usually recommend important clean-up rules to
|
||||
happen in separated processes either by use of monitoring or by links
|
||||
themselves. For example if the `GenServer` controls a `port` (e.g.
|
||||
`:gen_tcp.socket`) or `File.io_device`, they will be closed on receiving a
|
||||
`GenServer`'s exit signal and do not need to be closed in `terminate/2`.
|
||||
|
||||
If `reason` is not `:normal`, `:shutdown` nor `{:shutdown, term}` an error is
|
||||
logged.
|
||||
"""
|
||||
@callback terminate(reason, state :: term) ::
|
||||
term when reason: :normal | :shutdown | {:shutdown, term} | term
|
||||
|
||||
@doc """
|
||||
Invoked to change the state of the `GenServer` when a different version of a
|
||||
module is loaded (hot code swapping) and the state's term structure should be
|
||||
changed.
|
||||
|
||||
`old_vsn` is the previous version of the module (defined by the `@vsn`
|
||||
attribute) when upgrading. When downgrading the previous version is wrapped in
|
||||
a 2-tuple with first element `:down`. `state` is the current state of the
|
||||
`GenServer` and `extra` is any extra data required to change the state.
|
||||
|
||||
Returning `{:ok, new_state}` changes the state to `new_state` and the code
|
||||
change is successful.
|
||||
|
||||
Returning `{:error, reason}` fails the code change with reason `reason` and
|
||||
the state remains as the previous state.
|
||||
|
||||
If `code_change/3` raises the code change fails and the loop will continue
|
||||
with its previous state. Therefore this callback does not usually contain side effects.
|
||||
"""
|
||||
@callback code_change(old_vsn, state :: term, extra :: term) ::
|
||||
{:ok, new_state :: term} |
|
||||
{:error, reason :: term} when old_vsn: term | {:down, term}
|
||||
|
||||
@typedoc "Return values of `start*` functions"
|
||||
@type on_start :: {:ok, pid} | :ignore | {:error, {:already_started, pid} | term}
|
||||
|
||||
@typedoc "The GenServer name"
|
||||
@type name :: atom | {:global, term} | {:via, module, term}
|
||||
|
||||
@typedoc "Options used by the `start*` functions"
|
||||
@type options :: [option]
|
||||
|
||||
@typedoc "Option values used by the `start*` functions"
|
||||
@type option :: {:debug, debug} |
|
||||
{:name, name} |
|
||||
{:timeout, timeout} |
|
||||
{:spawn_opt, Process.spawn_opt}
|
||||
|
||||
@typedoc "Debug options supported by the `start*` functions"
|
||||
@type debug :: [:trace | :log | :statistics | {:log_to_file, Path.t}]
|
||||
|
||||
@typedoc "The server reference"
|
||||
@type server :: pid | name | {atom, node}
|
||||
|
||||
@typedoc """
|
||||
Tuple describing the client of a call request.
|
||||
|
||||
`pid` is the pid of the caller and `tag` is a unique term used to identify the
|
||||
call.
|
||||
"""
|
||||
@type from :: {pid, tag :: term}
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote location: :keep do
|
||||
@behaviour :gen_server
|
||||
|
||||
@doc false
|
||||
def init(args) do
|
||||
{:ok, args}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_call(msg, _from, state) do
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
reason = {:bad_call, msg}
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit(reason)
|
||||
1 -> {:stop, reason, state}
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(_msg, state) do
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_cast(msg, state) do
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
reason = {:bad_cast, msg}
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit(reason)
|
||||
1 -> {:stop, reason, state}
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def terminate(_reason, _state) do
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc false
|
||||
def code_change(_old, state, _extra) do
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
defoverridable [init: 1, handle_call: 3, handle_info: 2,
|
||||
handle_cast: 2, terminate: 2, code_change: 3]
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a `GenServer` process linked to the current process.
|
||||
|
||||
This is often used to start the `GenServer` as part of a supervision tree.
|
||||
|
||||
Once the server is started, it calls the `init/1` function in the given `module`
|
||||
passing the given `args` to initialize it. To ensure a synchronized start-up
|
||||
procedure, this function does not return until `init/1` has returned.
|
||||
|
||||
Note that a `GenServer` started with `start_link/3` is linked to the
|
||||
parent process and will exit in case of crashes. The GenServer will also
|
||||
exit due to the `:normal` reasons in case it is configured to trap exits
|
||||
in the `init/1` callback.
|
||||
|
||||
## Options
|
||||
|
||||
The `:name` option is used for name registration as described in the module
|
||||
documentation. If the option `:timeout` option is present, the server is
|
||||
allowed to spend the given milliseconds initializing or it will be
|
||||
terminated and the start function will return `{:error, :timeout}`.
|
||||
|
||||
If the `:debug` option is present, the corresponding function in the
|
||||
[`:sys` module](http://www.erlang.org/doc/man/sys.html) will be invoked.
|
||||
|
||||
If the `:spawn_opt` option is present, its value will be passed as options
|
||||
to the underlying process as in `Process.spawn/4`.
|
||||
|
||||
## Return values
|
||||
|
||||
If the server is successfully created and initialized, the function returns
|
||||
`{:ok, pid}`, where pid is the pid of the server. If a process with the
|
||||
specified server name already exists, the function returns
|
||||
`{:error, {:already_started, pid}}` with the pid of that process.
|
||||
|
||||
If the `init/1` callback fails with `reason`, the function returns
|
||||
`{:error, reason}`. Otherwise, if it returns `{:stop, reason}`
|
||||
or `:ignore`, the process is terminated and the function returns
|
||||
`{:error, reason}` or `:ignore`, respectively.
|
||||
"""
|
||||
@spec start_link(module, any, options) :: on_start
|
||||
def start_link(module, args, options \\ []) when is_atom(module) and is_list(options) do
|
||||
do_start(:link, module, args, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a `GenServer` process without links (outside of a supervision tree).
|
||||
|
||||
See `start_link/3` for more information.
|
||||
"""
|
||||
@spec start(module, any, options) :: on_start
|
||||
def start(module, args, options \\ []) when is_atom(module) and is_list(options) do
|
||||
do_start(:nolink, module, args, options)
|
||||
end
|
||||
|
||||
defp do_start(link, module, args, options) do
|
||||
case Keyword.pop(options, :name) do
|
||||
{nil, opts} ->
|
||||
:gen.start(:gen_server, link, module, args, opts)
|
||||
{atom, opts} when is_atom(atom) ->
|
||||
:gen.start(:gen_server, link, {:local, atom}, module, args, opts)
|
||||
{other, opts} when is_tuple(other) ->
|
||||
:gen.start(:gen_server, link, other, module, args, opts)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the server with the given `reason`.
|
||||
|
||||
The `terminate/2` callback will be invoked before exiting.
|
||||
It returns `:ok` if the server terminates with the given
|
||||
reason, if it terminates with another reason, the call will
|
||||
exit.
|
||||
|
||||
This function keeps OTP semantics regarding error reporting.
|
||||
If the reason is any other than `:normal`, `:shutdown` or
|
||||
`{:shutdown, _}`, an error report will be logged.
|
||||
"""
|
||||
@spec stop(server, reason :: term, timeout) :: :ok
|
||||
def stop(server, reason \\ :normal, timeout \\ :infinity) do
|
||||
:gen.stop(server, reason, timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Makes a synchronous call to the `server` and waits for its reply.
|
||||
|
||||
The client sends the given `request` to the server and waits until a reply
|
||||
arrives or a timeout occurs. `handle_call/3` will be called on the server
|
||||
to handle the request.
|
||||
|
||||
The server can be any of the values described in the `Name Registration`
|
||||
section of the module documentation.
|
||||
|
||||
## Timeouts
|
||||
|
||||
The `timeout` is an integer greater than zero which specifies how many
|
||||
milliseconds to wait for a reply, or the atom `:infinity` to wait
|
||||
indefinitely. The default value is 5000. If no reply is received within
|
||||
the specified time, the function call fails. If the caller catches the
|
||||
failure and continues running, and the server is just late with the reply,
|
||||
it may arrive at any time later into the caller's message queue. The caller
|
||||
must in this case be prepared for this and discard any such garbage messages
|
||||
that are two-element tuples with a reference as the first element.
|
||||
"""
|
||||
@spec call(server, term, timeout) :: term
|
||||
def call(server, request, timeout \\ 5000) do
|
||||
try do
|
||||
:gen.call(server, :"$gen_call", request, timeout)
|
||||
catch
|
||||
:exit, reason ->
|
||||
exit({reason, {__MODULE__, :call, [server, request, timeout]}})
|
||||
else
|
||||
{:ok, res} -> res
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends an asynchronous request to the `server`.
|
||||
|
||||
This function returns `:ok` without waiting for the
|
||||
destination `server` to handle the message. Therefore it
|
||||
is unknown whether the destination `server` successfully
|
||||
handled the message. If the `server` is an atom without
|
||||
an associated process an `ArgumentError` is raised. In
|
||||
all other cases the function returns `:ok` regardless of
|
||||
whether the destination `server` (or node) exists. Note
|
||||
that `{name, node()}` can be used when an exception is
|
||||
not desired if no process is locally associated with the
|
||||
atom `name`.
|
||||
|
||||
`handle_cast/2` will be called on the server to handle
|
||||
the request. In case the `server` is on a node which is
|
||||
not yet connected to the caller one, the call is going to
|
||||
block until a connection happens. This is different than
|
||||
the behaviour in OTP's `:gen_server` where the message
|
||||
is sent by another process in this case, which could cause
|
||||
messages to other nodes to arrive out of order.
|
||||
"""
|
||||
@spec cast(server, term) :: :ok
|
||||
def cast(server, request)
|
||||
|
||||
def cast({:global, name}, request) do
|
||||
try do
|
||||
:global.send(name, cast_msg(request))
|
||||
:ok
|
||||
catch
|
||||
_, _ -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
def cast({:via, mod, name}, request) do
|
||||
try do
|
||||
mod.send(name, cast_msg(request))
|
||||
:ok
|
||||
catch
|
||||
_, _ -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
def cast({name, node}, request) when is_atom(name) and is_atom(node),
|
||||
do: do_send({name, node}, cast_msg(request))
|
||||
|
||||
def cast(dest, request) when is_atom(dest) or is_pid(dest),
|
||||
do: do_send(dest, cast_msg(request))
|
||||
|
||||
@doc """
|
||||
Casts all servers locally registered as `name` at the specified nodes.
|
||||
|
||||
The function returns immediately and ignores nodes that do not exist, or where the
|
||||
server name does not exist.
|
||||
|
||||
See `multi_call/4` for more information.
|
||||
"""
|
||||
@spec abcast([node], name :: atom, term) :: :abcast
|
||||
def abcast(nodes \\ nodes(), name, request) when is_list(nodes) and is_atom(name) do
|
||||
msg = cast_msg(request)
|
||||
_ = for node <- nodes, do: do_send({name, node}, msg)
|
||||
:abcast
|
||||
end
|
||||
|
||||
defp cast_msg(req) do
|
||||
{:"$gen_cast", req}
|
||||
end
|
||||
|
||||
defp do_send(dest, msg) do
|
||||
send(dest, msg)
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calls all servers locally registered as `name` at the specified `nodes`.
|
||||
|
||||
The `request` is first sent to every node and then we wait for the
|
||||
replies. This function returns a tuple containing the node and its reply
|
||||
as first element and all bad nodes as second element. The bad nodes is a
|
||||
list of nodes that either did not exist, or where a server with the given
|
||||
`name` did not exist or did not reply.
|
||||
|
||||
Nodes is a list of node names to which the request is sent. The default
|
||||
value is the list of all known nodes.
|
||||
|
||||
To avoid that late answers (after the timeout) pollute the caller's message
|
||||
queue, a middleman process is used to do the actual calls. Late answers will
|
||||
then be discarded when they arrive to a terminated process.
|
||||
"""
|
||||
@spec multi_call([node], name :: atom, term, timeout) ::
|
||||
{replies :: [{node, term}], bad_nodes :: [node]}
|
||||
def multi_call(nodes \\ nodes(), name, request, timeout \\ :infinity) do
|
||||
:gen_server.multi_call(nodes, name, request, timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Replies to a client.
|
||||
|
||||
This function can be used by a server to explicitly send a reply to a
|
||||
client that called `call/3` or `multi_call/4`. When the reply cannot be
|
||||
defined in the return value of `handle_call/3`.
|
||||
|
||||
The `client` must be the `from` argument (the second argument) received
|
||||
in `handle_call/3` callbacks. Reply is an arbitrary term which will be
|
||||
given back to the client as the return value of the call.
|
||||
|
||||
This function always returns `:ok`.
|
||||
"""
|
||||
@spec reply(from, term) :: :ok
|
||||
def reply(client, reply)
|
||||
|
||||
def reply({to, tag}, reply) do
|
||||
try do
|
||||
send(to, {tag, reply})
|
||||
:ok
|
||||
catch
|
||||
_, _ -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the `pid` or `{name, node}` of a GenServer process.
|
||||
Returns `nil` if no process is associated with the given name.
|
||||
|
||||
For example, to lookup a server process, monitor it and send a cast:
|
||||
|
||||
process = GenServer.whereis(server)
|
||||
monitor = Process.monitor(process)
|
||||
GenServer.cast(process, :hello)
|
||||
|
||||
"""
|
||||
@spec whereis(server) :: pid | {atom, node} | nil
|
||||
def whereis(pid) when is_pid(pid), do: pid
|
||||
def whereis(name) when is_atom(name) do
|
||||
Process.whereis(name)
|
||||
end
|
||||
def whereis({:global, name}) do
|
||||
case :global.whereis_name(name) do
|
||||
pid when is_pid(pid) -> pid
|
||||
:undefined -> nil
|
||||
end
|
||||
end
|
||||
def whereis({:via, mod, name}) do
|
||||
case apply(mod, :whereis_name, [name]) do
|
||||
pid when is_pid(pid) -> pid
|
||||
:undefined -> nil
|
||||
end
|
||||
end
|
||||
def whereis({name, local}) when is_atom(name) and local == node() do
|
||||
Process.whereis(name)
|
||||
end
|
||||
def whereis({name, node} = server) when is_atom(name) and is_atom(node) do
|
||||
server
|
||||
end
|
||||
|
||||
@compile {:inline, [nodes: 0]}
|
||||
|
||||
defp nodes do
|
||||
[node()|:erlang.nodes()]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,48 @@
|
||||
defmodule GenServer.Behavior do
|
||||
@doc """
|
||||
By using this module, you get default GenServer callbacks
|
||||
for `handle_call`, `handle_info`, `handle_cast`, `terminate`
|
||||
and `code_change`. `init` still needs to be implemented by the
|
||||
developer. Since these functions are defined as overridable,
|
||||
they can be partially customized and have a global clause
|
||||
that simply invokes `super`. See `ExUnit.Server` for some
|
||||
code examples.
|
||||
|
||||
This module also tags the behavior as :gen_server. For more
|
||||
information on gen_server, please refer to the Erlang
|
||||
documentation:
|
||||
|
||||
http://www.erlang.org/doc/man/gen_server.html
|
||||
http://www.erlang.org/doc/design_principles/gen_server_concepts.html
|
||||
|
||||
"""
|
||||
defmacro __using__(_) do
|
||||
quote location: :keep do
|
||||
@behavior :gen_server
|
||||
|
||||
def handle_call(_request, _from, state) do
|
||||
{ :reply, :undef, state }
|
||||
end
|
||||
|
||||
def handle_info(_msg, state) do
|
||||
{ :noreply, state }
|
||||
end
|
||||
|
||||
def handle_cast(_msg, state) do
|
||||
{ :noreply, state }
|
||||
end
|
||||
|
||||
def terminate(reason, state) do
|
||||
:error_logger.error_report('#{inspect __MODULE__} crashed:\n#{inspect reason}')
|
||||
:error_logger.error_report('#{inspect __MODULE__} snapshot:\n#{inspect state}')
|
||||
:ok
|
||||
end
|
||||
|
||||
def code_change(_old, state, _extra) do
|
||||
{ :ok, state }
|
||||
end
|
||||
|
||||
defoverridable [handle_call: 3, handle_info: 2, handle_cast: 2, terminate: 2, code_change: 3]
|
||||
end
|
||||
end
|
||||
end
|
||||
+76
-238
@@ -1,243 +1,81 @@
|
||||
defimpl Dict, for: HashDict do
|
||||
defmacrop dict(data) do
|
||||
quote do
|
||||
{ HashDict, unquote(data) }
|
||||
end
|
||||
end
|
||||
|
||||
def keys(dict(data)) do
|
||||
:dict.fetch_keys data
|
||||
end
|
||||
|
||||
def values(dict(data)) do
|
||||
:dict.fold fn _key, value, acc ->
|
||||
[value|acc]
|
||||
end, [], data
|
||||
end
|
||||
|
||||
def size(dict(data)) do
|
||||
:dict.size data
|
||||
end
|
||||
|
||||
def has_key?(dict(data), key) do
|
||||
:dict.is_key key, data
|
||||
end
|
||||
|
||||
def get(dict(data), key, default // nil) do
|
||||
case :dict.find(key, data) do
|
||||
{:ok, value} ->
|
||||
value
|
||||
:error ->
|
||||
default
|
||||
end
|
||||
end
|
||||
|
||||
def put(dict(data), key, value) do
|
||||
dict(:dict.store key, value, data)
|
||||
end
|
||||
|
||||
def delete(dict(data), key) do
|
||||
dict(:dict.erase key, data)
|
||||
end
|
||||
|
||||
def merge(dict(d1), dict(d2)) do
|
||||
dict(:dict.merge fn _k, _v1, v2 -> v2 end, d1, d2)
|
||||
end
|
||||
|
||||
def merge(dict(d1), dict(d2), fun) do
|
||||
dict(:dict.merge fun, d1, d2)
|
||||
end
|
||||
|
||||
def update(dict(data), key, fun) do
|
||||
dict(:dict.update key, fun, data)
|
||||
end
|
||||
|
||||
def update(dict(data), key, initial, fun) do
|
||||
dict(:dict.update key, fun, initial, data)
|
||||
end
|
||||
|
||||
def empty(_) do
|
||||
dict(:dict.new)
|
||||
end
|
||||
|
||||
def to_list(dict(data)) do
|
||||
:dict.to_list data
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Enum.Iterator, for: HashDict do
|
||||
def iterator({ HashDict, data }), do: :dict.to_list(data)
|
||||
def count({ HashDict, data }), do: :dict.size(data)
|
||||
end
|
||||
|
||||
defmodule HashDict do
|
||||
@moduledoc """
|
||||
WARNING: this module is deprecated.
|
||||
|
||||
Use the `Map` module instead.
|
||||
This module implements a dictionary based on hashing of the keys.
|
||||
It is a simple wrapper around [Erlang's dict module](http://www.erlang.org/doc/man/dict.html)
|
||||
and exposed via the `Dict` protocol.
|
||||
"""
|
||||
|
||||
use Dict
|
||||
|
||||
@node_bitmap 0b111
|
||||
@node_shift 3
|
||||
@node_size 8
|
||||
@node_template :erlang.make_tuple(@node_size, [])
|
||||
|
||||
@opaque t :: %__MODULE__{size: non_neg_integer, root: term}
|
||||
@doc false
|
||||
defstruct size: 0, root: @node_template
|
||||
|
||||
# Inline common instructions
|
||||
@compile :inline_list_funcs
|
||||
@compile {:inline, key_hash: 1, key_mask: 1, key_shift: 1}
|
||||
|
||||
@doc """
|
||||
Creates a new empty dict.
|
||||
"""
|
||||
@spec new :: Dict.t
|
||||
def new do
|
||||
%HashDict{}
|
||||
end
|
||||
|
||||
def put(%HashDict{root: root, size: size}, key, value) do
|
||||
{root, counter} = do_put(root, key, value, key_hash(key))
|
||||
%HashDict{root: root, size: size + counter}
|
||||
end
|
||||
|
||||
def update!(%HashDict{root: root, size: size} = dict, key, fun) when is_function(fun, 1) do
|
||||
{root, counter} = do_update(root, key, fn -> raise KeyError, key: key, term: dict end,
|
||||
fun, key_hash(key))
|
||||
%HashDict{root: root, size: size + counter}
|
||||
end
|
||||
|
||||
def update(%HashDict{root: root, size: size}, key, initial, fun) when is_function(fun, 1) do
|
||||
{root, counter} = do_update(root, key, fn -> initial end, fun, key_hash(key))
|
||||
%HashDict{root: root, size: size + counter}
|
||||
end
|
||||
|
||||
def fetch(%HashDict{root: root}, key) do
|
||||
do_fetch(root, key, key_hash(key))
|
||||
end
|
||||
|
||||
def delete(dict, key) do
|
||||
case dict_delete(dict, key) do
|
||||
{dict, _value} -> dict
|
||||
:error -> dict
|
||||
end
|
||||
end
|
||||
|
||||
def pop(dict, key, default \\ nil) do
|
||||
case dict_delete(dict, key) do
|
||||
{dict, value} -> {value, dict}
|
||||
:error -> {default, dict}
|
||||
end
|
||||
end
|
||||
|
||||
def size(%HashDict{size: size}) do
|
||||
size
|
||||
end
|
||||
|
||||
@doc false
|
||||
def reduce(%HashDict{root: root}, acc, fun) do
|
||||
do_reduce(root, acc, fun, @node_size, fn
|
||||
{:suspend, acc} -> {:suspended, acc, &{:done, elem(&1, 1)}}
|
||||
{:halt, acc} -> {:halted, acc}
|
||||
{:cont, acc} -> {:done, acc}
|
||||
end)
|
||||
end
|
||||
|
||||
## General helpers
|
||||
|
||||
@doc false
|
||||
def dict_delete(%HashDict{root: root, size: size}, key) do
|
||||
case do_delete(root, key, key_hash(key)) do
|
||||
{root, value} -> {%HashDict{root: root, size: size - 1}, value}
|
||||
:error -> :error
|
||||
end
|
||||
end
|
||||
|
||||
## Dict manipulation
|
||||
|
||||
defp do_fetch(node, key, hash) do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[^key|v] -> {:ok, v}
|
||||
{^key, v, _} -> {:ok, v}
|
||||
{_, _, n} -> do_fetch(n, key, key_shift(hash))
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
defp do_put(node, key, value, hash) do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [key|value]), 1}
|
||||
[^key|_] ->
|
||||
{put_elem(node, index, [key|value]), 0}
|
||||
[k|v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key|value])
|
||||
{put_elem(node, index, {k, v, n}), 1}
|
||||
{^key, _, n} ->
|
||||
{put_elem(node, index, {key, value, n}), 0}
|
||||
{k, v, n} ->
|
||||
{n, counter} = do_put(n, key, value, key_shift(hash))
|
||||
{put_elem(node, index, {k, v, n}), counter}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_update(node, key, initial, fun, hash) do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [key|initial.()]), 1}
|
||||
[^key|value] ->
|
||||
{put_elem(node, index, [key|fun.(value)]), 0}
|
||||
[k|v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key|initial.()])
|
||||
{put_elem(node, index, {k, v, n}), 1}
|
||||
{^key, value, n} ->
|
||||
{put_elem(node, index, {key, fun.(value), n}), 0}
|
||||
{k, v, n} ->
|
||||
{n, counter} = do_update(n, key, initial, fun, key_shift(hash))
|
||||
{put_elem(node, index, {k, v, n}), counter}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_delete(node, key, hash) do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
:error
|
||||
[^key|value] ->
|
||||
{put_elem(node, index, []), value}
|
||||
[_|_] ->
|
||||
:error
|
||||
{^key, value, n} ->
|
||||
{put_elem(node, index, do_compact_node(n)), value}
|
||||
{k, v, n} ->
|
||||
case do_delete(n, key, key_shift(hash)) do
|
||||
{@node_template, value} ->
|
||||
{put_elem(node, index, [k|v]), value}
|
||||
{n, value} ->
|
||||
{put_elem(node, index, {k, v, n}), value}
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Enum.each 0..(@node_size - 1), fn index ->
|
||||
defp do_compact_node(node) when elem(node, unquote(index)) != [] do
|
||||
case elem(node, unquote(index)) do
|
||||
[k|v] ->
|
||||
case put_elem(node, unquote(index), []) do
|
||||
@node_template -> [k|v]
|
||||
n -> {k, v, n}
|
||||
end
|
||||
{k, v, n} ->
|
||||
{k, v, put_elem(node, unquote(index), do_compact_node(n))}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
## Dict reduce
|
||||
|
||||
defp do_reduce_each(_node, {:halt, acc}, _fun, _next) do
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp do_reduce_each(node, {:suspend, acc}, fun, next) do
|
||||
{:suspended, acc, &do_reduce_each(node, &1, fun, next)}
|
||||
end
|
||||
|
||||
defp do_reduce_each([], acc, _fun, next) do
|
||||
next.(acc)
|
||||
end
|
||||
|
||||
defp do_reduce_each([k|v], {:cont, acc}, fun, next) do
|
||||
next.(fun.({k, v}, acc))
|
||||
end
|
||||
|
||||
defp do_reduce_each({k, v, n}, {:cont, acc}, fun, next) do
|
||||
do_reduce(n, fun.({k, v}, acc), fun, @node_size, next)
|
||||
end
|
||||
|
||||
defp do_reduce(node, acc, fun, count, next) when count > 0 do
|
||||
do_reduce_each(:erlang.element(count, node), acc, fun, &do_reduce(node, &1, fun, count - 1, next))
|
||||
end
|
||||
|
||||
defp do_reduce(_node, acc, _fun, 0, next) do
|
||||
next.(acc)
|
||||
end
|
||||
|
||||
## Key operations
|
||||
|
||||
import Bitwise
|
||||
|
||||
defp key_hash(key) do
|
||||
:erlang.phash2(key)
|
||||
end
|
||||
|
||||
defp key_mask(hash) do
|
||||
hash &&& @node_bitmap
|
||||
end
|
||||
|
||||
defp key_shift(hash) do
|
||||
hash >>> @node_shift
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Enumerable, for: HashDict do
|
||||
def reduce(dict, acc, fun), do: HashDict.reduce(dict, acc, fun)
|
||||
def member?(dict, {k, v}), do: {:ok, match?({:ok, ^v}, HashDict.fetch(dict, k))}
|
||||
def member?(_dict, _), do: {:ok, false}
|
||||
def count(dict), do: {:ok, HashDict.size(dict)}
|
||||
end
|
||||
|
||||
defimpl Collectable, for: HashDict do
|
||||
def into(original) do
|
||||
{original, fn
|
||||
dict, {:cont, {k, v}} -> HashDict.put(dict, k, v)
|
||||
dict, :done -> dict
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: HashDict do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(dict, opts) do
|
||||
concat ["#HashDict<", Inspect.List.inspect(HashDict.to_list(dict), opts), ">"]
|
||||
end
|
||||
use Dict.Common, Dict.HashDict
|
||||
end
|
||||
|
||||
@@ -1,259 +0,0 @@
|
||||
defmodule HashSet do
|
||||
@moduledoc """
|
||||
WARNING: this module is deprecated.
|
||||
|
||||
Use the `MapSet` module instead.
|
||||
"""
|
||||
|
||||
@behaviour Set
|
||||
|
||||
@node_bitmap 0b111
|
||||
@node_shift 3
|
||||
@node_size 8
|
||||
@node_template :erlang.make_tuple(@node_size, [])
|
||||
|
||||
@opaque t :: %__MODULE__{size: non_neg_integer, root: term}
|
||||
@doc false
|
||||
defstruct size: 0, root: @node_template
|
||||
|
||||
# Inline common instructions
|
||||
@compile :inline_list_funcs
|
||||
@compile {:inline, key_hash: 1, key_mask: 1, key_shift: 1}
|
||||
|
||||
@spec new :: Set.t
|
||||
def new do
|
||||
%HashSet{}
|
||||
end
|
||||
|
||||
def union(%HashSet{size: size1} = set1, %HashSet{size: size2} = set2) when size1 <= size2 do
|
||||
set_fold set1, set2, fn v, acc -> put(acc, v) end
|
||||
end
|
||||
|
||||
def union(%HashSet{} = set1, %HashSet{} = set2) do
|
||||
set_fold set2, set1, fn v, acc -> put(acc, v) end
|
||||
end
|
||||
|
||||
def intersection(%HashSet{} = set1, %HashSet{} = set2) do
|
||||
set_fold set1, %HashSet{}, fn v, acc ->
|
||||
if member?(set2, v), do: put(acc, v), else: acc
|
||||
end
|
||||
end
|
||||
|
||||
def difference(%HashSet{} = set1, %HashSet{} = set2) do
|
||||
set_fold set2, set1, fn v, acc -> delete(acc, v) end
|
||||
end
|
||||
|
||||
def to_list(set) do
|
||||
set_fold(set, [], &[&1|&2]) |> :lists.reverse
|
||||
end
|
||||
|
||||
def equal?(%HashSet{size: size1} = set1, %HashSet{size: size2} = set2) do
|
||||
case size1 do
|
||||
^size2 -> subset?(set1, set2)
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
def subset?(%HashSet{} = set1, %HashSet{} = set2) do
|
||||
reduce(set1, {:cont, true}, fn member, acc ->
|
||||
case member?(set2, member) do
|
||||
true -> {:cont, acc}
|
||||
_ -> {:halt, false}
|
||||
end
|
||||
end) |> elem(1)
|
||||
end
|
||||
|
||||
def disjoint?(%HashSet{} = set1, %HashSet{} = set2) do
|
||||
reduce(set2, {:cont, true}, fn member, acc ->
|
||||
case member?(set1, member) do
|
||||
false -> {:cont, acc}
|
||||
_ -> {:halt, false}
|
||||
end
|
||||
end) |> elem(1)
|
||||
end
|
||||
|
||||
def member?(%HashSet{root: root}, term) do
|
||||
do_member?(root, term, key_hash(term))
|
||||
end
|
||||
|
||||
def put(%HashSet{root: root, size: size}, term) do
|
||||
{root, counter} = do_put(root, term, key_hash(term))
|
||||
%HashSet{root: root, size: size + counter}
|
||||
end
|
||||
|
||||
def delete(%HashSet{root: root, size: size} = set, term) do
|
||||
case do_delete(root, term, key_hash(term)) do
|
||||
{:ok, root} -> %HashSet{root: root, size: size - 1}
|
||||
:error -> set
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def reduce(%HashSet{root: root}, acc, fun) do
|
||||
do_reduce(root, acc, fun, @node_size, fn
|
||||
{:suspend, acc} -> {:suspended, acc, &{:done, elem(&1, 1)}}
|
||||
{:halt, acc} -> {:halted, acc}
|
||||
{:cont, acc} -> {:done, acc}
|
||||
end)
|
||||
end
|
||||
|
||||
def size(%HashSet{size: size}) do
|
||||
size
|
||||
end
|
||||
|
||||
## Set helpers
|
||||
|
||||
defp set_fold(%HashSet{root: root}, acc, fun) do
|
||||
do_fold(root, acc, fun, @node_size)
|
||||
end
|
||||
|
||||
## Set manipulation
|
||||
|
||||
defp do_member?(node, term, hash) do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] -> false
|
||||
[^term|_] -> true
|
||||
[_] -> false
|
||||
[_|n] -> do_member?(n, term, key_shift(hash))
|
||||
end
|
||||
end
|
||||
|
||||
defp do_put(node, term, hash) do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [term]), 1}
|
||||
[^term|_] ->
|
||||
{node, 0}
|
||||
[t] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [term])
|
||||
{put_elem(node, index, [t|n]), 1}
|
||||
[t|n] ->
|
||||
{n, counter} = do_put(n, term, key_shift(hash))
|
||||
{put_elem(node, index, [t|n]), counter}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_delete(node, term, hash) do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
:error
|
||||
[^term] ->
|
||||
{:ok, put_elem(node, index, [])}
|
||||
[_] ->
|
||||
:error
|
||||
[^term|n] ->
|
||||
{:ok, put_elem(node, index, do_compact_node(n))}
|
||||
[t|n] ->
|
||||
case do_delete(n, term, key_shift(hash)) do
|
||||
{:ok, @node_template} ->
|
||||
{:ok, put_elem(node, index, [t])}
|
||||
{:ok, n} ->
|
||||
{:ok, put_elem(node, index, [t|n])}
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Enum.each 0..(@node_size - 1), fn index ->
|
||||
defp do_compact_node(node) when elem(node, unquote(index)) != [] do
|
||||
case elem(node, unquote(index)) do
|
||||
[t] ->
|
||||
case put_elem(node, unquote(index), []) do
|
||||
@node_template -> [t]
|
||||
n -> [t|n]
|
||||
end
|
||||
[t|n] ->
|
||||
[t|put_elem(node, unquote(index), do_compact_node(n))]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
## Set fold
|
||||
|
||||
defp do_fold_each([], acc, _fun), do: acc
|
||||
defp do_fold_each([t], acc, fun), do: fun.(t, acc)
|
||||
defp do_fold_each([t|n], acc, fun), do: do_fold(n, fun.(t, acc), fun, @node_size)
|
||||
|
||||
defp do_fold(node, acc, fun, count) when count > 0 do
|
||||
acc = do_fold_each(:erlang.element(count, node), acc, fun)
|
||||
do_fold(node, acc, fun, count - 1)
|
||||
end
|
||||
|
||||
defp do_fold(_node, acc, _fun, 0) do
|
||||
acc
|
||||
end
|
||||
|
||||
## Set reduce
|
||||
|
||||
defp do_reduce_each(_node, {:halt, acc}, _fun, _next) do
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp do_reduce_each(node, {:suspend, acc}, fun, next) do
|
||||
{:suspended, acc, &do_reduce_each(node, &1, fun, next)}
|
||||
end
|
||||
|
||||
defp do_reduce_each([], acc, _fun, next) do
|
||||
next.(acc)
|
||||
end
|
||||
|
||||
defp do_reduce_each([t], {:cont, acc}, fun, next) do
|
||||
next.(fun.(t, acc))
|
||||
end
|
||||
|
||||
defp do_reduce_each([t|n], {:cont, acc}, fun, next) do
|
||||
do_reduce(n, fun.(t, acc), fun, @node_size, next)
|
||||
end
|
||||
|
||||
defp do_reduce(node, acc, fun, count, next) when count > 0 do
|
||||
do_reduce_each(:erlang.element(count, node), acc, fun, &do_reduce(node, &1, fun, count - 1, next))
|
||||
end
|
||||
|
||||
defp do_reduce(_node, acc, _fun, 0, next) do
|
||||
next.(acc)
|
||||
end
|
||||
|
||||
## Key operations
|
||||
|
||||
import Bitwise
|
||||
|
||||
defp key_hash(key) do
|
||||
:erlang.phash2(key)
|
||||
end
|
||||
|
||||
defp key_mask(hash) do
|
||||
hash &&& @node_bitmap
|
||||
end
|
||||
|
||||
defp key_shift(hash) do
|
||||
hash >>> @node_shift
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Enumerable, for: HashSet do
|
||||
def reduce(set, acc, fun), do: HashSet.reduce(set, acc, fun)
|
||||
def member?(set, v), do: {:ok, HashSet.member?(set, v)}
|
||||
def count(set), do: {:ok, HashSet.size(set)}
|
||||
end
|
||||
|
||||
defimpl Collectable, for: HashSet do
|
||||
def into(original) do
|
||||
{original, fn
|
||||
set, {:cont, x} -> HashSet.put(set, x)
|
||||
set, :done -> set
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: HashSet do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(set, opts) do
|
||||
concat ["#HashSet<", Inspect.List.inspect(HashSet.to_list(set), opts), ">"]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,212 @@
|
||||
defrecord IEx.Config, io: nil, binding: nil, cache: '', counter: 1, scope: nil, result: nil
|
||||
|
||||
defmodule IEx do
|
||||
@moduledoc """
|
||||
This module implements interactive Elixir. It provides a main
|
||||
function, `start` which will either delegate to `tty` or `simple`.
|
||||
The former is meant for systems where tty is available and relies
|
||||
on it in order to work properly. This makes all control commands
|
||||
available in tty available to the developer.
|
||||
|
||||
In case `tty` is not available (for example, Windows), a
|
||||
developer may invoke `simple` which starts a stripped
|
||||
down version.
|
||||
"""
|
||||
|
||||
import Exception, only: [format_stacktrace: 1]
|
||||
|
||||
@doc """
|
||||
Interface to start IEx from CLI.
|
||||
"""
|
||||
def cli do
|
||||
run([remsh: get_remsh(:init.get_plain_arguments)])
|
||||
end
|
||||
|
||||
defp get_remsh(['--remsh',h|_]), do: list_to_binary(h)
|
||||
defp get_remsh([_|t]), do: get_remsh(t)
|
||||
defp get_remsh([]), do: nil
|
||||
|
||||
@doc """
|
||||
Runs IEx checking if tty is available or not.
|
||||
If so, invoke tty, otherwise go with the simple iex.
|
||||
"""
|
||||
def run(opts // []) when is_list(opts) do
|
||||
case :os.type do
|
||||
{ :unix, _ } -> tty(opts)
|
||||
_ -> simple(opts)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts IEx using a tty server.
|
||||
"""
|
||||
def tty(opts // []) when is_list(opts) do
|
||||
config = boot_config(opts)
|
||||
|
||||
remote =
|
||||
if remsh = opts[:remsh] do
|
||||
if node() == :nonode@nohost do
|
||||
raise ArgumentError, message: "In order to use --remsh, you need to name the node"
|
||||
end
|
||||
if is_atom(remsh), do: remsh, else: binary_to_atom(remsh)
|
||||
end
|
||||
|
||||
function = fn ->
|
||||
# We are inside the new tty and in a new process,
|
||||
# reattach it the error logger.
|
||||
attach_error_logger
|
||||
|
||||
start config
|
||||
end
|
||||
|
||||
# Dettach the error logger because we are going to unregister
|
||||
# the user process and start a new tty which will get control
|
||||
# over the standardio. Dettaching it here allows us to get rid
|
||||
# of warnings. We reattach it again when we get the new tty.
|
||||
dettach_error_logger
|
||||
|
||||
# Unregister the user process, user_drv command below
|
||||
# will register the new one.
|
||||
unregister_user_process
|
||||
|
||||
# Close the default io port, user_drv start command below
|
||||
# will take control over the io.
|
||||
close_io_port
|
||||
|
||||
args =
|
||||
if remote do
|
||||
{ remote, :erlang, :apply, [function, []] }
|
||||
else
|
||||
{ :erlang, :apply, [function, []] }
|
||||
end
|
||||
|
||||
Erlang.user_drv.start([:"tty_sl -c -e", args])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts IEx simply using the current stdio.
|
||||
"""
|
||||
def simple(opts // []) when is_list(opts) do
|
||||
start boot_config(opts)
|
||||
end
|
||||
|
||||
# This is a callback invoked by Erlang shell utilities.
|
||||
@doc false
|
||||
def start(config // nil) do
|
||||
spawn fn ->
|
||||
config = config || boot_config([])
|
||||
gl = :erlang.group_leader
|
||||
glnode = node gl
|
||||
if glnode != node do
|
||||
ensure_module_exists glnode, IEx.Remsh
|
||||
expand_fun = IEx.Remsh.expand node
|
||||
else
|
||||
expand_fun = IEx.Autocomplete.expand &1
|
||||
end
|
||||
:io.setopts gl, [expand_fun: expand_fun]
|
||||
start_loop(config)
|
||||
end
|
||||
end
|
||||
|
||||
## Boot Helpers
|
||||
|
||||
defp boot_config(opts) do
|
||||
IO.puts "Interactive Elixir (#{System.version}) - press Ctrl+C to exit"
|
||||
|
||||
scope = Erlang.elixir.scope_for_eval(
|
||||
file: 'iex',
|
||||
delegate_locals_to: IEx.Helpers
|
||||
)
|
||||
|
||||
IEx.Config[
|
||||
io: opts[:io] || IEx.UnicodeIO,
|
||||
binding: opts[:binding] || [],
|
||||
scope: scope
|
||||
]
|
||||
end
|
||||
|
||||
defp dettach_error_logger do
|
||||
:error_logger.delete_report_handler(:error_logger_tty_h)
|
||||
end
|
||||
|
||||
defp attach_error_logger do
|
||||
:error_logger.add_report_handler(:error_logger_tty_h)
|
||||
end
|
||||
|
||||
defp unregister_user_process do
|
||||
if is_pid(Process.whereis(:user)), do: Process.unregister :user
|
||||
end
|
||||
|
||||
defp close_io_port do
|
||||
if port = Enum.find(Port.list, io_port?(&1)) do
|
||||
Port.close(port)
|
||||
end
|
||||
end
|
||||
|
||||
defp io_port?(port) do
|
||||
Port.info(port, :name) == {:name,'0/1'} && port
|
||||
end
|
||||
|
||||
## Loop helpers
|
||||
|
||||
defp start_loop(config) do
|
||||
Process.put :iex_history, []
|
||||
{ _, _, scope } = Erlang.elixir.eval('import IEx.Helpers', [], 0, config.scope)
|
||||
do_loop(config.scope(scope))
|
||||
end
|
||||
|
||||
defp do_loop(config) do
|
||||
io = config.io
|
||||
|
||||
counter = config.counter
|
||||
cache = config.cache
|
||||
code = cache ++ io.get(config)
|
||||
|
||||
new_config =
|
||||
try do
|
||||
{ result, new_binding, scope } =
|
||||
Erlang.elixir.eval(code, config.binding, counter, config.scope)
|
||||
|
||||
io.put result
|
||||
|
||||
config = config.result(result)
|
||||
update_history(config.cache(code).scope(nil))
|
||||
config.increment_counter.cache('').binding(new_binding).scope(scope)
|
||||
rescue
|
||||
TokenMissingError ->
|
||||
config.cache(code)
|
||||
exception ->
|
||||
stacktrace = System.stacktrace
|
||||
io.error "** (#{inspect exception.__record__(:name)}) #{exception.message}"
|
||||
print_stacktrace io, stacktrace
|
||||
config.cache('')
|
||||
catch
|
||||
kind, error ->
|
||||
stacktrace = System.stacktrace
|
||||
io.error "** (#{kind}) #{inspect(error)}"
|
||||
print_stacktrace io, stacktrace
|
||||
config.cache('')
|
||||
end
|
||||
|
||||
do_loop(new_config)
|
||||
end
|
||||
|
||||
defp update_history(config) do
|
||||
current = Process.get :iex_history
|
||||
Process.put :iex_history, [config|current]
|
||||
end
|
||||
|
||||
defp print_stacktrace(io, stacktrace) do
|
||||
Enum.each stacktrace, fn s -> io.error " #{format_stacktrace(s)}" end
|
||||
end
|
||||
|
||||
## Code injection helper
|
||||
|
||||
defp ensure_module_exists(node, mod) do
|
||||
unless :rpc.call node, :code, :is_loaded, [mod] do
|
||||
{m,b,f} = :code.get_object_code mod
|
||||
{:module, mod} = :rpc.call node, :code, :load_binary, [m,f,b]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
defmodule IEx.Autocomplete do
|
||||
@moduledoc """
|
||||
Autocompletion for the Elixir shell.
|
||||
"""
|
||||
|
||||
defrecord Mod, name: nil, type: nil
|
||||
defrecord Fun, name: nil, arities: []
|
||||
|
||||
defprotocol Entry do
|
||||
@moduledoc false
|
||||
def to_entries(entry)
|
||||
def to_hint(entry, hint)
|
||||
end
|
||||
|
||||
defimpl Entry, for: Mod do
|
||||
def to_entries(mod) do
|
||||
[mod.name]
|
||||
end
|
||||
|
||||
def to_hint(Mod[name: name], hint) do
|
||||
:lists.nthtail(length(hint), name) ++ '.'
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Entry, for: Fun do
|
||||
def to_entries(fun) do
|
||||
lc a inlist fun.arities, do: '#{fun.name}/#{a}'
|
||||
end
|
||||
|
||||
def to_hint(Fun[name: name], hint) do
|
||||
:lists.nthtail(length(hint), name)
|
||||
end
|
||||
end
|
||||
|
||||
def expand([]) do
|
||||
funs = module_funs(IEx.Helpers) ++ module_funs(Kernel)
|
||||
mods = [Mod[name: 'Elixir', type: :elixir], Mod[name: 'Erlang', type: :elixir]]
|
||||
format_expansion mods ++ funs
|
||||
end
|
||||
|
||||
def expand([h|t]=expr) do
|
||||
cond do
|
||||
h === ?. ->
|
||||
expand_dot reduce(t)
|
||||
h === ?: ->
|
||||
expand_erlang_modules
|
||||
(h in ?a..?z) or (h in ?A..?Z) or h === ?_ ->
|
||||
expand_expr reduce(expr)
|
||||
h in '(+[' ->
|
||||
expand ''
|
||||
true ->
|
||||
no_match
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_dot(expr) do
|
||||
case Code.string_to_ast expr do
|
||||
{:ok, atom} when is_atom(atom) ->
|
||||
expand_module_funs atom
|
||||
{:ok, {:__aliases__,_,[:Erlang]}} ->
|
||||
expand_erlang_modules
|
||||
{:ok, {:__aliases__,_,list}} ->
|
||||
expand_elixir_modules list
|
||||
{:ok, {{:.,_,[{:__aliases__,_,[:Erlang]},mod]},_,[]}} when is_atom(mod) ->
|
||||
expand_module_funs mod
|
||||
_ ->
|
||||
no_match
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_expr(expr) do
|
||||
case Code.string_to_ast expr do
|
||||
{:ok, atom} when is_atom(atom) ->
|
||||
expand_erlang_modules atom_to_list(atom)
|
||||
{:ok, { atom, _, nil }} when is_atom(atom) ->
|
||||
expand_module_funs Kernel, atom_to_list(atom)
|
||||
{:ok, {:__aliases__,_,[root]}} ->
|
||||
expand_elixir_modules [], atom_to_list(root)
|
||||
{:ok, {:__aliases__,_,list}} ->
|
||||
hint = atom_to_list(List.last(list))
|
||||
list = :lists.sublist(list, length(list)-1)
|
||||
expand_elixir_modules list, hint
|
||||
{:ok, {{:., _, [mod,fun]},_,[]}} when is_atom(fun) ->
|
||||
expand_call mod, atom_to_list(fun)
|
||||
_ -> no_match
|
||||
end
|
||||
end
|
||||
|
||||
defp reduce(expr) do
|
||||
last_token(List.reverse(expr), [' ', '(', '[', '+', '-'])
|
||||
end
|
||||
|
||||
defp last_token(s, []) do
|
||||
s
|
||||
end
|
||||
|
||||
defp last_token(s, [h|t]) do
|
||||
last_token(List.last(:string.tokens(s, h)), t)
|
||||
end
|
||||
|
||||
defp no_match, do: { :no, '', [] }
|
||||
|
||||
## Formatting
|
||||
|
||||
defp format_expansion(list, hint // '')
|
||||
|
||||
defp format_expansion([], _) do
|
||||
no_match
|
||||
end
|
||||
|
||||
defp format_expansion([uniq], hint) do
|
||||
{ :yes, Entry.to_hint(uniq, hint), [] }
|
||||
end
|
||||
|
||||
defp format_expansion([first|_]=entries, hint) do
|
||||
binary = Enum.map entries, fn e -> list_to_binary(e.name) end
|
||||
length = length hint
|
||||
prefix = :binary.longest_common_prefix(binary)
|
||||
if prefix == 0 or (prefix == length) do
|
||||
{:yes, '',
|
||||
Enum.reduce entries, [], fn e, acc -> Entry.to_entries(e) ++ acc end }
|
||||
else
|
||||
{:yes, :lists.sublist(first.name, 1 + length, prefix-length), [] }
|
||||
end
|
||||
end
|
||||
|
||||
## Root Modules
|
||||
|
||||
defp root_modules do
|
||||
Enum.reduce :code.all_loaded, [], fn {m,_}, acc ->
|
||||
mod = atom_to_list(m)
|
||||
case mod do
|
||||
'Elixir' ++ _ ->
|
||||
tokens = :string.tokens(mod, '-')
|
||||
if length(tokens) === 2 do
|
||||
[Mod.new(name: List.last(tokens), type: :elixir)|acc]
|
||||
else
|
||||
acc
|
||||
end
|
||||
_ ->
|
||||
[Mod.new(name: mod, type: :erlang)|acc]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
## Expand calls
|
||||
|
||||
# :atom.fun
|
||||
defp expand_call(mod, hint) when is_atom(mod) do
|
||||
expand_module_funs mod, hint
|
||||
end
|
||||
|
||||
# Erlang.mod.fun
|
||||
defp expand_call({ { :., _, [{ :__aliases__, _, [:Erlang] }, mod] }, _, [] }, hint) when is_atom(mod) do
|
||||
expand_module_funs mod, hint
|
||||
end
|
||||
|
||||
# Erlang.mod
|
||||
defp expand_call({ :__aliases__, _, [:Erlang] }, hint) do
|
||||
expand_erlang_modules hint
|
||||
end
|
||||
|
||||
# Elixir.fun
|
||||
defp expand_call({ :__aliases__, _, list }, hint) do
|
||||
expand_module_funs Module.concat(list), hint
|
||||
end
|
||||
|
||||
defp expand_call(_, _) do
|
||||
no_match
|
||||
end
|
||||
|
||||
## Erlang modules
|
||||
|
||||
defp expand_erlang_modules(hint // '') do
|
||||
format_expansion match_erlang_modules(hint), hint
|
||||
end
|
||||
|
||||
defp match_erlang_modules('') do
|
||||
Enum.filter root_modules, fn m -> m.type === :erlang end
|
||||
end
|
||||
|
||||
defp match_erlang_modules(hint) do
|
||||
Enum.filter root_modules, fn m -> :lists.prefix(hint, m.name) end
|
||||
end
|
||||
|
||||
## Elixir modules
|
||||
|
||||
defp expand_elixir_modules(list, hint // '') do
|
||||
mod = Module.concat(list)
|
||||
format_expansion elixir_submodules(mod, hint, list == []) ++ module_funs(mod, hint), hint
|
||||
end
|
||||
|
||||
defp elixir_submodules(mod, hint, root) do
|
||||
modname = atom_to_list(mod)
|
||||
depth = length(:string.tokens(modname, '-')) + 1
|
||||
base = modname ++ [?-|hint]
|
||||
|
||||
Enum.reduce modules_as_lists(root), [], fn(m, acc) ->
|
||||
if :lists.prefix(base, m) do
|
||||
tokens = :string.tokens(m, '-')
|
||||
if length(tokens) == depth do
|
||||
name = List.last(tokens)
|
||||
[Mod.new(type: :elixir, name: name)|acc]
|
||||
else
|
||||
acc
|
||||
end
|
||||
else
|
||||
acc
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp modules_as_lists(true) do
|
||||
['Elixir-Elixir', 'Elixir-Erlang'] ++ modules_as_lists(false)
|
||||
end
|
||||
|
||||
defp modules_as_lists(false) do
|
||||
Enum.map(:code.all_loaded, fn({ m, _ }) -> atom_to_list(m) end)
|
||||
end
|
||||
|
||||
## Functions
|
||||
|
||||
defp expand_module_funs(mod, hint // '') do
|
||||
format_expansion module_funs(mod, hint), hint
|
||||
end
|
||||
|
||||
defp module_funs(mod, hint // '') do
|
||||
case ensure_loaded(mod) do
|
||||
{ :module, _ } ->
|
||||
falist = get_funs(mod)
|
||||
|
||||
list = Enum.reduce falist, [], fn {f,a}, acc ->
|
||||
case :lists.keyfind(f, 1, acc) do
|
||||
{f,aa} -> :lists.keyreplace(f, 1, acc, {f, [a|aa]})
|
||||
false -> [{f, [a]}|acc]
|
||||
end
|
||||
end
|
||||
|
||||
lc {fun, arities} inlist list, name = atom_to_list(fun), is_prefix?(hint, name) do
|
||||
Fun[name: name, arities: arities]
|
||||
end
|
||||
_ ->
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
## Generic Helpers
|
||||
|
||||
defp get_funs(mod) do
|
||||
if function_exported?(mod, :__info__, 1) do
|
||||
if docs = mod.__info__(:docs) do
|
||||
lc { pair, _line, _kind, _sign, doc } inlist docs, doc != false, do: pair
|
||||
else
|
||||
(mod.__info__(:functions) -- [__info__: 1]) ++ mod.__info__(:macros)
|
||||
end
|
||||
else
|
||||
mod.module_info(:exports)
|
||||
end
|
||||
end
|
||||
|
||||
defp is_prefix?('', _), do: true
|
||||
defp is_prefix?(hint, name), do: :lists.prefix(hint, name)
|
||||
|
||||
defp ensure_loaded(Elixir), do: { :error, :nofile }
|
||||
defp ensure_loaded(mod), do: Code.ensure_loaded(mod)
|
||||
end
|
||||
@@ -0,0 +1,244 @@
|
||||
defmodule IEx.Helpers do
|
||||
@moduledoc """
|
||||
A bunch of helpers available in IEx.
|
||||
|
||||
* `c` - compiles a file in the given path
|
||||
* `d` - prints documentation
|
||||
* `h` - prints history
|
||||
* `m` - prints loaded modules
|
||||
* `r` - recompiles and reloads the given module's source file
|
||||
* `v` - retrieves nth value from console
|
||||
|
||||
Documentation for functions in this module can be consulted
|
||||
directly from the command line, as an example, try:
|
||||
|
||||
d(:c, 1)
|
||||
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Expects a list of files to compile and a path
|
||||
to write their object code to. It returns the name
|
||||
of the compiled modules.
|
||||
|
||||
## Examples
|
||||
|
||||
c ["foo.ex"], "ebin"
|
||||
#=> Foo
|
||||
|
||||
"""
|
||||
def c(files, path // ".") do
|
||||
tuples = Kernel.ParallelCompiler.files_to_path List.wrap(files), path
|
||||
Enum.map tuples, elem(&1, 1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the name and module of all modules loaded.
|
||||
"""
|
||||
def m do
|
||||
all = Enum.map :code.all_loaded, fn { mod, file } -> { inspect(mod), file } end
|
||||
sorted = List.sort(all)
|
||||
size = Enum.reduce sorted, 0, fn({ mod, _ }, acc) -> max(byte_size(mod), acc) end
|
||||
format = "~-#{size}s ~s~n"
|
||||
|
||||
Enum.each sorted, fn({ mod, file }) ->
|
||||
:io.format(format, [mod, file])
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prints commands history and their result.
|
||||
"""
|
||||
def h do
|
||||
history = List.reverse(Process.get(:iex_history))
|
||||
Enum.each(history, print_history(&1))
|
||||
end
|
||||
|
||||
defp print_history(config) do
|
||||
IO.puts "#{config.counter}: #{config.cache}#=> #{inspect config.result}\n"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Shows the documentation for IEx.Helpers.
|
||||
"""
|
||||
def d() do
|
||||
d(IEx.Helpers, :all)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Shows the documentation for the given module
|
||||
or for the given function/arity pair.
|
||||
|
||||
## Examples
|
||||
|
||||
d(Enum)
|
||||
#=> Prints documentation for Enum
|
||||
|
||||
It also accepts functions in the format `fun/arity`
|
||||
and `module.fun/arity`, for example:
|
||||
|
||||
d receive/1
|
||||
d Enum.all?/2
|
||||
|
||||
"""
|
||||
defmacro d({ :/, _, [{ fun, _, nil }, arity] }) do
|
||||
quote do
|
||||
d(unquote(fun), unquote(arity))
|
||||
end
|
||||
end
|
||||
|
||||
defmacro d({ :/, _, [{ { :., _, [mod, fun] }, _, [] }, arity] }) do
|
||||
quote do
|
||||
d(unquote(mod), unquote(fun), unquote(arity))
|
||||
end
|
||||
end
|
||||
|
||||
defmacro d(other) do
|
||||
quote do
|
||||
d(unquote(other), :all)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prints the documentation for the given function and arity.
|
||||
|
||||
The function may either be a function defined inside `IEx.Helpers`
|
||||
or in `Kernel`. To see functions from other module, use
|
||||
`d/3` instead.
|
||||
|
||||
## Examples
|
||||
|
||||
d(:d, 2)
|
||||
#=> Prints documentation for this function
|
||||
|
||||
"""
|
||||
def d(:d, 1) do
|
||||
d(__MODULE__, :d, 1)
|
||||
end
|
||||
|
||||
def d(function, arity) when is_atom(function) and is_integer(arity) do
|
||||
if function_exported?(__MODULE__, function, arity) do
|
||||
d(__MODULE__, function, arity)
|
||||
else
|
||||
d(Kernel, function, arity)
|
||||
end
|
||||
end
|
||||
|
||||
def d(module, :all) when is_atom(module) do
|
||||
case Code.ensure_loaded(module) do
|
||||
{ :module, _ } ->
|
||||
case module.__info__(:moduledoc) do
|
||||
{ _, binary } when is_binary(binary) ->
|
||||
IO.puts "# #{inspect module}\n"
|
||||
IO.write binary
|
||||
{ _, _ } ->
|
||||
IO.puts "No docs for #{inspect module}"
|
||||
_ ->
|
||||
IO.puts "#{inspect module} was not compiled with docs"
|
||||
end
|
||||
{ :error, reason } ->
|
||||
IO.puts "Could not load module #{inspect module}: #{reason}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Shows the documentation for the `function/arity` in `module`.
|
||||
"""
|
||||
def d(module, function, arity) when is_atom(module) and is_atom(function) and is_integer(arity) do
|
||||
if docs = module.__info__(:docs) do
|
||||
doc =
|
||||
if tuple = List.keyfind(docs, { function, arity }, 1) do
|
||||
print_signature(tuple)
|
||||
end
|
||||
|
||||
if doc do
|
||||
IO.write "\n" <> doc
|
||||
else
|
||||
IO.puts "No docs for #{function}/#{arity}"
|
||||
end
|
||||
else
|
||||
IO.puts "#{inspect module} was not compiled with docs"
|
||||
end
|
||||
end
|
||||
|
||||
# Get the full signature from a function.
|
||||
defp print_signature({ _info, _line, _kind, _args, false }) do
|
||||
false
|
||||
end
|
||||
|
||||
defp print_signature({ { name, _arity }, _line, kind, args, docs }) do
|
||||
args = Enum.map_join(args, ", ", signature_arg(&1))
|
||||
IO.puts "* #{kind} #{name}(#{args})"
|
||||
docs
|
||||
end
|
||||
|
||||
defp signature_arg({ ://, _, [left, right] }) do
|
||||
signature_arg(left) <> " // " <> Macro.to_binary(right)
|
||||
end
|
||||
|
||||
defp signature_arg({ var, _, _ }) do
|
||||
atom_to_binary(var)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Retrieves nth query's value from the history. Use negative
|
||||
values to lookup query's value from latest to earliest.
|
||||
For instance, v(-1) returns the latest result.
|
||||
"""
|
||||
def v(n) when n < 0 do
|
||||
history = Process.get(:iex_history)
|
||||
Enum.nth!(history, abs(n)).result
|
||||
end
|
||||
|
||||
def v(n) do
|
||||
history = Process.get(:iex_history) /> List.reverse
|
||||
Enum.nth!(history, n).result
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reloads all modules that were already reloaded
|
||||
at some point with `r/1`.
|
||||
"""
|
||||
def r do
|
||||
Enum.map iex_reloaded, r(&1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Recompiles and reloads the specified module's source file.
|
||||
|
||||
Please note that all the modules defined in the specified
|
||||
files are recompiled and reloaded.
|
||||
"""
|
||||
def r(module) do
|
||||
if source = source(module) do
|
||||
Process.put(:iex_reloaded, :ordsets.add_element(module, iex_reloaded))
|
||||
{ module, Code.load_file source }
|
||||
else
|
||||
:nosource
|
||||
end
|
||||
end
|
||||
|
||||
defp iex_reloaded do
|
||||
Process.get(:iex_reloaded) || :ordsets.new
|
||||
end
|
||||
|
||||
defp source(module) do
|
||||
compile = module.module_info(:compile)
|
||||
|
||||
# Get the source of the compiled module. Due to a bug in Erlang
|
||||
# R15 and before, we need to look for the source first in the
|
||||
# options and then into the real source.
|
||||
options =
|
||||
case List.keyfind(compile, :options, 1) do
|
||||
{ :options, opts } -> opts
|
||||
_ -> []
|
||||
end
|
||||
|
||||
source = List.keyfind(options, :source, 1) || List.keyfind(compile, :source, 1)
|
||||
|
||||
case source do
|
||||
{ :source, source } -> list_to_binary(source)
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
defmodule IEx.Remsh do
|
||||
@moduledoc """
|
||||
Helper function injected into connecting remote nodes
|
||||
to properly handle autocompletion. Elixir supports:
|
||||
|
||||
* remsh from an elixir node to an elixir node
|
||||
* remsh from a plain erlang node to an elixir node (through the ^G menu)
|
||||
* remsh from an elixir node to a plain erlang node (and get an erl shell there)
|
||||
|
||||
In order to get an Elixir shell from the ^G menu,
|
||||
you need to use 'Elixir-IEx' as the shell name.
|
||||
|
||||
Connecting an Elixir shell to a remote node without
|
||||
Elixir is **not** supported.
|
||||
"""
|
||||
def expand(node) do
|
||||
fn e ->
|
||||
case :rpc.call node, Elixir.IEx.Autocomplete, :expand, [e] do
|
||||
{:badrpc, _} -> {:no, '', []}
|
||||
r -> r
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,45 @@
|
||||
defmodule IEx.UnicodeIO do
|
||||
@moduledoc """
|
||||
This module implements the API used by IEX to
|
||||
interact with the console. This API may change
|
||||
in the future without warnings.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Implements the get IO API used by IEx. It receives the
|
||||
code cache, the instructions counter and needs to
|
||||
return a list with the new characters inserted.
|
||||
"""
|
||||
def get(config) do
|
||||
prefix = case config.cache do
|
||||
[] -> "iex"
|
||||
_ -> "..."
|
||||
end
|
||||
prompt = case node do
|
||||
:nonode@nohost ->
|
||||
"#{prefix}(#{config.counter})> "
|
||||
n ->
|
||||
"#{prefix}(#{n})#{config.counter}> "
|
||||
end
|
||||
case IO.gets(prompt) do
|
||||
{ :error, _ } -> ''
|
||||
data -> :unicode.characters_to_list(data)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Implements the put IO API used by IEx. It receives the
|
||||
result and prints it.
|
||||
"""
|
||||
def put(result) do
|
||||
IO.inspect result
|
||||
end
|
||||
|
||||
@doc """
|
||||
Implements the error IO API used by IEx. It prints error
|
||||
messages.
|
||||
"""
|
||||
def error(result) do
|
||||
IO.puts result
|
||||
end
|
||||
end
|
||||
@@ -1,528 +0,0 @@
|
||||
import Kernel, except: [inspect: 1]
|
||||
import Inspect.Algebra
|
||||
|
||||
defprotocol Inspect do
|
||||
@moduledoc """
|
||||
The `Inspect` protocol is responsible for converting any Elixir
|
||||
data structure into an algebra document. This document is then
|
||||
formatted, either in pretty printing format or a regular one.
|
||||
|
||||
The `inspect/2` function receives the entity to be inspected
|
||||
followed by the inspecting options, represented by the struct
|
||||
`Inspect.Opts`.
|
||||
|
||||
Inspection is done using the functions available in `Inspect.Algebra`.
|
||||
|
||||
## Examples
|
||||
|
||||
Many times, inspecting a structure can be implemented in function
|
||||
of existing entities. For example, here is `MapSet`'s `inspect`
|
||||
implementation:
|
||||
|
||||
defimpl Inspect, for: MapSet do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(dict, opts) do
|
||||
concat ["#MapSet<", to_doc(MapSet.to_list(dict), opts), ">"]
|
||||
end
|
||||
end
|
||||
|
||||
The `concat` function comes from `Inspect.Algebra` and it
|
||||
concatenates algebra documents together. In the example above,
|
||||
it is concatenating the string `"MapSet<"` (all strings are
|
||||
valid algebra documents that keep their formatting when pretty
|
||||
printed), the document returned by `Inspect.Algebra.to_doc/2` and the
|
||||
other string `">"`.
|
||||
|
||||
Since regular strings are valid entities in an algebra document,
|
||||
an implementation of inspect may simply return a string,
|
||||
although that will devoid it of any pretty-printing.
|
||||
|
||||
## Error handling
|
||||
|
||||
In case there is an error while your structure is being inspected,
|
||||
Elixir will raise an `ArgumentError` error and will automatically fall back
|
||||
to a raw representation for printing the structure.
|
||||
|
||||
You can however access the underlying error by invoking the Inspect
|
||||
implementation directly. For example, to test Inspect.MapSet above,
|
||||
you can invoke it as:
|
||||
|
||||
Inspect.MapSet.inspect(MapSet.new, %Inspect.Opts{})
|
||||
|
||||
"""
|
||||
|
||||
# Handle structs in Any
|
||||
@fallback_to_any true
|
||||
|
||||
def inspect(thing, opts)
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Atom do
|
||||
require Macro
|
||||
|
||||
def inspect(atom, _opts) do
|
||||
inspect(atom)
|
||||
end
|
||||
|
||||
def inspect(false), do: "false"
|
||||
def inspect(true), do: "true"
|
||||
def inspect(nil), do: "nil"
|
||||
def inspect(:""), do: ":\"\""
|
||||
|
||||
def inspect(atom) do
|
||||
binary = Atom.to_string(atom)
|
||||
|
||||
cond do
|
||||
valid_ref_identifier?(binary) ->
|
||||
if only_elixir?(binary) do
|
||||
binary
|
||||
else
|
||||
"Elixir." <> rest = binary
|
||||
rest
|
||||
end
|
||||
valid_atom_identifier?(binary) ->
|
||||
":" <> binary
|
||||
atom in [:%{}, :{}, :<<>>, :..., :%] ->
|
||||
":" <> binary
|
||||
atom in Macro.binary_ops or atom in Macro.unary_ops ->
|
||||
":" <> binary
|
||||
true ->
|
||||
<<?:, ?", Inspect.BitString.escape(binary, ?")::binary, ?">>
|
||||
end
|
||||
end
|
||||
|
||||
defp only_elixir?("Elixir." <> rest), do: only_elixir?(rest)
|
||||
defp only_elixir?("Elixir"), do: true
|
||||
defp only_elixir?(_), do: false
|
||||
|
||||
# Detect if atom is an atom alias (Elixir.Foo.Bar.Baz)
|
||||
|
||||
defp valid_ref_identifier?("Elixir" <> rest) do
|
||||
valid_ref_piece?(rest)
|
||||
end
|
||||
|
||||
defp valid_ref_identifier?(_), do: false
|
||||
|
||||
defp valid_ref_piece?(<<?., h, t::binary>>) when h in ?A..?Z do
|
||||
valid_ref_piece? valid_identifier?(t)
|
||||
end
|
||||
|
||||
defp valid_ref_piece?(<<>>), do: true
|
||||
defp valid_ref_piece?(_), do: false
|
||||
|
||||
# Detect if atom
|
||||
|
||||
defp valid_atom_identifier?(<<h, t::binary>>) when h in ?a..?z or h in ?A..?Z or h == ?_ do
|
||||
valid_atom_piece?(t)
|
||||
end
|
||||
|
||||
defp valid_atom_identifier?(_), do: false
|
||||
|
||||
defp valid_atom_piece?(t) do
|
||||
case valid_identifier?(t) do
|
||||
<<>> -> true
|
||||
<<??>> -> true
|
||||
<<?!>> -> true
|
||||
<<?@, t::binary>> -> valid_atom_piece?(t)
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
defp valid_identifier?(<<h, t::binary>>)
|
||||
when h in ?a..?z
|
||||
when h in ?A..?Z
|
||||
when h in ?0..?9
|
||||
when h == ?_ do
|
||||
valid_identifier? t
|
||||
end
|
||||
|
||||
defp valid_identifier?(other), do: other
|
||||
end
|
||||
|
||||
defimpl Inspect, for: BitString do
|
||||
def inspect(thing, %Inspect.Opts{binaries: bins} = opts) when is_binary(thing) do
|
||||
if bins == :as_strings or (bins == :infer and String.printable?(thing)) do
|
||||
<<?", escape(thing, ?")::binary, ?">>
|
||||
else
|
||||
inspect_bitstring(thing, opts)
|
||||
end
|
||||
end
|
||||
|
||||
def inspect(thing, opts) do
|
||||
inspect_bitstring(thing, opts)
|
||||
end
|
||||
|
||||
## Escaping
|
||||
|
||||
@doc false
|
||||
def escape(other, char) do
|
||||
escape(other, char, <<>>)
|
||||
end
|
||||
|
||||
defp escape(<<char, t::binary >>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, char>>)
|
||||
end
|
||||
defp escape(<<?#, ?{, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?#, ?{>>)
|
||||
end
|
||||
defp escape(<<?\a, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?a>>)
|
||||
end
|
||||
defp escape(<<?\b, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?b>>)
|
||||
end
|
||||
defp escape(<<?\d, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?d>>)
|
||||
end
|
||||
defp escape(<<?\e, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?e>>)
|
||||
end
|
||||
defp escape(<<?\f, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?f>>)
|
||||
end
|
||||
defp escape(<<?\n, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?n>>)
|
||||
end
|
||||
defp escape(<<?\r, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?r>>)
|
||||
end
|
||||
defp escape(<<?\\, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?\\>>)
|
||||
end
|
||||
defp escape(<<?\t, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?t>>)
|
||||
end
|
||||
defp escape(<<?\v, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?v>>)
|
||||
end
|
||||
defp escape(<<h::utf8, t::binary>>, char, binary) do
|
||||
head = <<h::utf8 >>
|
||||
if String.printable?(head) do
|
||||
escape(t, char, append(head, binary))
|
||||
else
|
||||
<<byte::8, h::binary >> = head
|
||||
t = <<h::binary, t::binary>>
|
||||
escape(t, char, <<binary::binary, escape_char(byte)::binary>>)
|
||||
end
|
||||
end
|
||||
defp escape(<<h, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, escape_char(h)::binary>>)
|
||||
end
|
||||
defp escape(<<>>, _char, binary), do: binary
|
||||
|
||||
@doc false
|
||||
# Also used by Regex
|
||||
def escape_char(0) do
|
||||
<<?\\, ?0>>
|
||||
end
|
||||
|
||||
def escape_char(char) when char < 0x100 do
|
||||
<<a::4, b::4>> = <<char::8>>
|
||||
<<?\\, ?x, to_hex(a), to_hex(b)>>
|
||||
end
|
||||
|
||||
def escape_char(char) when char < 0x10000 do
|
||||
<<a::4, b::4, c::4, d::4>> = <<char::16>>
|
||||
<<?\\, ?x, ?{, to_hex(a), to_hex(b), to_hex(c), to_hex(d), ?}>>
|
||||
end
|
||||
|
||||
def escape_char(char) when char < 0x1000000 do
|
||||
<<a::4, b::4, c::4, d::4, e::4, f::4>> = <<char::24>>
|
||||
<<?\\, ?x, ?{, to_hex(a), to_hex(b), to_hex(c),
|
||||
to_hex(d), to_hex(e), to_hex(f), ?}>>
|
||||
end
|
||||
|
||||
defp to_hex(c) when c in 0..9, do: ?0+c
|
||||
defp to_hex(c) when c in 10..15, do: ?A+c-10
|
||||
|
||||
defp append(<<h, t::binary>>, binary), do: append(t, <<binary::binary, h>>)
|
||||
defp append(<<>>, binary), do: binary
|
||||
|
||||
## Bitstrings
|
||||
|
||||
defp inspect_bitstring(bitstring, opts) do
|
||||
each_bit(bitstring, opts.limit, "<<") <> ">>"
|
||||
end
|
||||
|
||||
defp each_bit(_, 0, acc) do
|
||||
acc <> "..."
|
||||
end
|
||||
|
||||
defp each_bit(<<h, t::bitstring>>, counter, acc) when t != <<>> do
|
||||
each_bit(t, decrement(counter), acc <> Integer.to_string(h) <> ", ")
|
||||
end
|
||||
|
||||
defp each_bit(<<h::8>>, _counter, acc) do
|
||||
acc <> Integer.to_string(h)
|
||||
end
|
||||
|
||||
defp each_bit(<<>>, _counter, acc) do
|
||||
acc
|
||||
end
|
||||
|
||||
defp each_bit(bitstring, _counter, acc) do
|
||||
size = bit_size(bitstring)
|
||||
<<h::size(size)>> = bitstring
|
||||
acc <> Integer.to_string(h) <> "::size(" <> Integer.to_string(size) <> ")"
|
||||
end
|
||||
|
||||
defp decrement(:infinity), do: :infinity
|
||||
defp decrement(counter), do: counter - 1
|
||||
end
|
||||
|
||||
defimpl Inspect, for: List do
|
||||
def inspect([], _opts), do: "[]"
|
||||
|
||||
def inspect(thing, %Inspect.Opts{char_lists: lists} = opts) do
|
||||
cond do
|
||||
lists == :as_char_lists or (lists == :infer and printable?(thing)) ->
|
||||
<<?', Inspect.BitString.escape(IO.chardata_to_string(thing), ?')::binary, ?'>>
|
||||
keyword?(thing) ->
|
||||
surround_many("[", thing, "]", opts, &keyword/2)
|
||||
true ->
|
||||
surround_many("[", thing, "]", opts, &to_doc/2)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def keyword({key, value}, opts) do
|
||||
concat(
|
||||
key_to_binary(key) <> ": ",
|
||||
to_doc(value, opts)
|
||||
)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def keyword?([{key, _value} | rest]) when is_atom(key) do
|
||||
case Atom.to_char_list(key) do
|
||||
'Elixir.' ++ _ -> false
|
||||
_ -> keyword?(rest)
|
||||
end
|
||||
end
|
||||
|
||||
def keyword?([]), do: true
|
||||
def keyword?(_other), do: false
|
||||
|
||||
@doc false
|
||||
def printable?([c|cs]) when is_integer(c) and c in 32..126, do: printable?(cs)
|
||||
def printable?([?\n|cs]), do: printable?(cs)
|
||||
def printable?([?\r|cs]), do: printable?(cs)
|
||||
def printable?([?\t|cs]), do: printable?(cs)
|
||||
def printable?([?\v|cs]), do: printable?(cs)
|
||||
def printable?([?\b|cs]), do: printable?(cs)
|
||||
def printable?([?\f|cs]), do: printable?(cs)
|
||||
def printable?([?\e|cs]), do: printable?(cs)
|
||||
def printable?([?\a|cs]), do: printable?(cs)
|
||||
def printable?([]), do: true
|
||||
def printable?(_), do: false
|
||||
|
||||
## Private
|
||||
|
||||
defp key_to_binary(key) do
|
||||
case Inspect.Atom.inspect(key) do
|
||||
":" <> right -> right
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Tuple do
|
||||
def inspect({}, _opts), do: "{}"
|
||||
|
||||
def inspect(tuple, opts) do
|
||||
surround_many("{", Tuple.to_list(tuple), "}", opts, &to_doc/2)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Map do
|
||||
def inspect(map, opts) do
|
||||
nest inspect(map, "", opts), 1
|
||||
end
|
||||
|
||||
def inspect(map, name, opts) do
|
||||
map = :maps.to_list(map)
|
||||
surround_many("%" <> name <> "{", map, "}", opts, traverse_fun(map))
|
||||
end
|
||||
|
||||
defp traverse_fun(list) do
|
||||
if Inspect.List.keyword?(list) do
|
||||
&Inspect.List.keyword/2
|
||||
else
|
||||
&to_map/2
|
||||
end
|
||||
end
|
||||
|
||||
defp to_map({key, value}, opts) do
|
||||
concat(
|
||||
concat(to_doc(key, opts), " => "),
|
||||
to_doc(value, opts)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Integer do
|
||||
def inspect(thing, %Inspect.Opts{base: base}) do
|
||||
Integer.to_string(thing, base_to_value(base))
|
||||
|> prepend_prefix(base)
|
||||
end
|
||||
|
||||
defp base_to_value(base) do
|
||||
case base do
|
||||
:binary -> 2
|
||||
:decimal -> 10
|
||||
:octal -> 8
|
||||
:hex -> 16
|
||||
end
|
||||
end
|
||||
|
||||
defp prepend_prefix(value, :decimal), do: value
|
||||
defp prepend_prefix(value, base) do
|
||||
prefix = case base do
|
||||
:binary -> "0b"
|
||||
:octal -> "0o"
|
||||
:hex -> "0x"
|
||||
end
|
||||
prefix <> value
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Float do
|
||||
def inspect(thing, _opts) do
|
||||
IO.iodata_to_binary(:io_lib_format.fwrite_g(thing))
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Regex do
|
||||
def inspect(regex, _opts) do
|
||||
delim = ?/
|
||||
concat ["~r",
|
||||
<<delim, escape(regex.source, delim)::binary, delim>>,
|
||||
regex.opts]
|
||||
end
|
||||
|
||||
defp escape(bin, term),
|
||||
do: escape(bin, <<>>, term)
|
||||
|
||||
defp escape(<<?\\, term>> <> rest, buf, term),
|
||||
do: escape(rest, buf <> <<?\\, term>>, term)
|
||||
|
||||
defp escape(<<term>> <> rest, buf, term),
|
||||
do: escape(rest, buf <> <<?\\, term>>, term)
|
||||
|
||||
# the list of characters is from "String.printable?" impl
|
||||
# minus characters treated specially by regex: \s, \d, \b, \e
|
||||
|
||||
defp escape(<<?\n>> <> rest, buf, term),
|
||||
do: escape(rest, <<buf::binary, ?\\, ?n>>, term)
|
||||
|
||||
defp escape(<<?\r>> <> rest, buf, term),
|
||||
do: escape(rest, <<buf::binary, ?\\, ?r>>, term)
|
||||
|
||||
defp escape(<<?\t>> <> rest, buf, term),
|
||||
do: escape(rest, <<buf::binary, ?\\, ?t>>, term)
|
||||
|
||||
defp escape(<<?\v>> <> rest, buf, term),
|
||||
do: escape(rest, <<buf::binary, ?\\, ?v>>, term)
|
||||
|
||||
defp escape(<<?\f>> <> rest, buf, term),
|
||||
do: escape(rest, <<buf::binary, ?\\, ?f>>, term)
|
||||
|
||||
defp escape(<<?\a>> <> rest, buf, term),
|
||||
do: escape(rest, <<buf::binary, ?\\, ?a>>, term)
|
||||
|
||||
defp escape(<<c::utf8>> <> rest, buf, term) do
|
||||
charstr = <<c::utf8>>
|
||||
if String.printable?(charstr) and not c in [?\d, ?\b, ?\e] do
|
||||
escape(rest, buf <> charstr, term)
|
||||
else
|
||||
escape(rest, buf <> Inspect.BitString.escape_char(c), term)
|
||||
end
|
||||
end
|
||||
|
||||
defp escape(<<c>> <> rest, buf, term),
|
||||
do: escape(rest, <<buf::binary, Inspect.BitString.escape_char(c)>>, term)
|
||||
|
||||
defp escape(<<>>, buf, _), do: buf
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Function do
|
||||
def inspect(function, _opts) do
|
||||
fun_info = :erlang.fun_info(function)
|
||||
mod = fun_info[:module]
|
||||
|
||||
if fun_info[:type] == :external and fun_info[:env] == [] do
|
||||
"&#{Inspect.Atom.inspect(mod)}.#{fun_info[:name]}/#{fun_info[:arity]}"
|
||||
else
|
||||
case Atom.to_char_list(mod) do
|
||||
'elixir_compiler_' ++ _ ->
|
||||
if function_exported?(mod, :__RELATIVE__, 0) do
|
||||
"#Function<#{uniq(fun_info)} in file:#{mod.__RELATIVE__}>"
|
||||
else
|
||||
default_inspect(mod, fun_info)
|
||||
end
|
||||
_ ->
|
||||
default_inspect(mod, fun_info)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp default_inspect(mod, fun_info) do
|
||||
"#Function<#{uniq(fun_info)}/#{fun_info[:arity]} in " <>
|
||||
"#{Inspect.Atom.inspect(mod)}#{extract_name(fun_info[:name])}>"
|
||||
end
|
||||
|
||||
defp extract_name([]) do
|
||||
""
|
||||
end
|
||||
|
||||
defp extract_name(name) do
|
||||
name = Atom.to_string(name)
|
||||
case :binary.split(name, "-", [:global]) do
|
||||
["", name | _] -> "." <> name
|
||||
_ -> "." <> name
|
||||
end
|
||||
end
|
||||
|
||||
defp uniq(fun_info) do
|
||||
Integer.to_string(fun_info[:new_index]) <> "." <>
|
||||
Integer.to_string(fun_info[:uniq])
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: PID do
|
||||
def inspect(pid, _opts) do
|
||||
"#PID" <> IO.iodata_to_binary(:erlang.pid_to_list(pid))
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Port do
|
||||
def inspect(port, _opts) do
|
||||
IO.iodata_to_binary :erlang.port_to_list(port)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Reference do
|
||||
def inspect(ref, _opts) do
|
||||
'#Ref' ++ rest = :erlang.ref_to_list(ref)
|
||||
"#Reference" <> IO.iodata_to_binary(rest)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Any do
|
||||
def inspect(%{__struct__: struct} = map, opts) do
|
||||
try do
|
||||
struct.__struct__
|
||||
rescue
|
||||
_ -> Inspect.Map.inspect(map, opts)
|
||||
else
|
||||
dunder ->
|
||||
if :maps.keys(dunder) == :maps.keys(map) do
|
||||
pruned = :maps.remove(:__exception__, :maps.remove(:__struct__, map))
|
||||
Inspect.Map.inspect(pruned, Inspect.Atom.inspect(struct, opts), opts)
|
||||
else
|
||||
Inspect.Map.inspect(map, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,564 +0,0 @@
|
||||
defmodule Inspect.Opts do
|
||||
@moduledoc """
|
||||
Defines the Inspect.Opts used by the Inspect protocol.
|
||||
|
||||
The following fields are available:
|
||||
|
||||
* `:structs` - when `false`, structs are not formatted by the inspect
|
||||
protocol, they are instead printed as maps, defaults to `true`.
|
||||
|
||||
* `:binaries` - when `:as_strings` all binaries will be printed as strings,
|
||||
non-printable bytes will be escaped.
|
||||
|
||||
When `:as_binaries` all binaries will be printed in bit syntax.
|
||||
|
||||
When the default `:infer`, the binary will be printed as a string if it
|
||||
is printable, otherwise in bit syntax.
|
||||
|
||||
* `:char_lists` - when `:as_char_lists` all lists will be printed as char
|
||||
lists, non-printable elements will be escaped.
|
||||
|
||||
When `:as_lists` all lists will be printed as lists.
|
||||
|
||||
When the default `:infer`, the list will be printed as a char list if it
|
||||
is printable, otherwise as list.
|
||||
|
||||
* `:limit` - limits the number of items that are printed for tuples,
|
||||
bitstrings, and lists, does not apply to strings nor char lists, defaults
|
||||
to 50.
|
||||
|
||||
* `:pretty` - if set to `true` enables pretty printing, defaults to `false`.
|
||||
|
||||
* `:width` - defaults to the 80 characters, used when pretty is `true` or
|
||||
when printing to IO devices.
|
||||
|
||||
* `:base` - print integers as :binary, :octal, :decimal, or :hex, defaults
|
||||
to :decimal
|
||||
|
||||
* `:safe` - when `false`, failures while inspecting structs will be raised
|
||||
as errors instead of being wrapped in the Inspect.Error exception. This
|
||||
is useful when debugging failures and crashes for custom inspect
|
||||
implementations
|
||||
|
||||
"""
|
||||
|
||||
defstruct structs: true,
|
||||
binaries: :infer,
|
||||
char_lists: :infer,
|
||||
limit: 50,
|
||||
width: 80,
|
||||
base: :decimal,
|
||||
pretty: false,
|
||||
safe: true
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
structs: boolean,
|
||||
binaries: :infer | :as_binaries | :as_strings,
|
||||
char_lists: :infer | :as_lists | :as_char_lists,
|
||||
limit: pos_integer | :infinity,
|
||||
width: pos_integer | :infinity,
|
||||
base: :decimal | :binary | :hex | :octal,
|
||||
pretty: boolean,
|
||||
safe: boolean}
|
||||
end
|
||||
|
||||
defmodule Inspect.Error do
|
||||
@moduledoc """
|
||||
Raised when a struct cannot be inspected.
|
||||
"""
|
||||
defexception [:message]
|
||||
end
|
||||
|
||||
defmodule Inspect.Algebra do
|
||||
@moduledoc ~S"""
|
||||
A set of functions for creating and manipulating algebra
|
||||
documents.
|
||||
|
||||
This module implements the functionality described in
|
||||
["Strictly Pretty" (2000) by Christian Lindig][0] with small
|
||||
additions, like support for String nodes, and a custom
|
||||
rendering function that maximises horizontal space use.
|
||||
|
||||
iex> Inspect.Algebra.empty
|
||||
:doc_nil
|
||||
|
||||
iex> "foo"
|
||||
"foo"
|
||||
|
||||
With the functions in this module, we can concatenate different
|
||||
elements together and render them:
|
||||
|
||||
iex> doc = Inspect.Algebra.concat(Inspect.Algebra.empty, "foo")
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["foo"]
|
||||
|
||||
The functions `nest/2`, `space/2` and `line/2` help you put the
|
||||
document together into a rigid structure. However, the document
|
||||
algebra gets interesting when using functions like `break/2`, which
|
||||
converts the given string into a line break depending on how much space
|
||||
there is to print. Let's glue two docs together with a break and then
|
||||
render it:
|
||||
|
||||
iex> doc = Inspect.Algebra.glue("a", " ", "b")
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["a", " ", "b"]
|
||||
|
||||
Notice the break was represented as is, because we haven't reached
|
||||
a line limit. Once we do, it is replaced by a newline:
|
||||
|
||||
iex> doc = Inspect.Algebra.glue(String.duplicate("a", 20), " ", "b")
|
||||
iex> Inspect.Algebra.format(doc, 10)
|
||||
["aaaaaaaaaaaaaaaaaaaa", "\n", "b"]
|
||||
|
||||
Finally, this module also contains Elixir related functions, a bit
|
||||
tied to Elixir formatting, namely `surround/3` and `surround_many/5`.
|
||||
|
||||
## Implementation details
|
||||
|
||||
The original Haskell implementation of the algorithm by [Wadler][1]
|
||||
relies on lazy evaluation to unfold document groups on two alternatives:
|
||||
`:flat` (breaks as spaces) and `:break` (breaks as newlines).
|
||||
Implementing the same logic in a strict language such as Elixir leads
|
||||
to an exponential growth of possible documents, unless document groups
|
||||
are encoded explictly as `:flat` or `:break`. Those groups are then reduced
|
||||
to a simple document, where the layout is already decided, per [Lindig][0].
|
||||
|
||||
This implementation slightly changes the semantic of Lindig's algorithm
|
||||
to allow elements that belong to the same group to be printed together
|
||||
in the same line, even if they do not fit the line fully. This was achieved
|
||||
by changing `:break` to mean a possible break and `:flat` to force a flat
|
||||
structure. Then deciding if a break works as a newline is just a matter
|
||||
of checking if we have enough space until the next break that is not
|
||||
inside a group (which is still flat).
|
||||
|
||||
Custom pretty printers can be implemented using the documents returned
|
||||
by this module and by providing their own rendering functions.
|
||||
|
||||
[0]: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.34.2200
|
||||
[1]: http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf
|
||||
|
||||
"""
|
||||
|
||||
@surround_separator ","
|
||||
@tail_separator " |"
|
||||
@newline "\n"
|
||||
@nesting 1
|
||||
@break " "
|
||||
|
||||
# Functional interface to "doc" records
|
||||
|
||||
@type t :: :doc_nil | :doc_line | doc_cons | doc_nest | doc_break | doc_group | binary
|
||||
|
||||
@typep doc_cons :: {:doc_cons, t, t}
|
||||
defmacrop doc_cons(left, right) do
|
||||
quote do: {:doc_cons, unquote(left), unquote(right)}
|
||||
end
|
||||
|
||||
@typep doc_nest :: {:doc_nest, t, non_neg_integer}
|
||||
defmacrop doc_nest(doc, indent) do
|
||||
quote do: {:doc_nest, unquote(doc), unquote(indent) }
|
||||
end
|
||||
|
||||
@typep doc_break :: {:doc_break, binary}
|
||||
defmacrop doc_break(break) do
|
||||
quote do: {:doc_break, unquote(break)}
|
||||
end
|
||||
|
||||
@typep doc_group :: {:doc_group, t}
|
||||
defmacrop doc_group(group) do
|
||||
quote do: {:doc_group, unquote(group)}
|
||||
end
|
||||
|
||||
defmacrop is_doc(doc) do
|
||||
if Macro.Env.in_guard?(__CALLER__) do
|
||||
do_is_doc(doc)
|
||||
else
|
||||
var = quote do: doc
|
||||
quote do
|
||||
unquote(var) = unquote(doc)
|
||||
unquote(do_is_doc(var))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp do_is_doc(doc) do
|
||||
quote do
|
||||
is_binary(unquote(doc)) or
|
||||
unquote(doc) in [:doc_nil, :doc_line] or
|
||||
(is_tuple(unquote(doc)) and
|
||||
elem(unquote(doc), 0) in [:doc_cons, :doc_nest, :doc_break, :doc_group])
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts an Elixir structure to an algebra document
|
||||
according to the inspect protocol.
|
||||
"""
|
||||
@spec to_doc(any, Inspect.Opts.t) :: t
|
||||
def to_doc(%{__struct__: struct} = map, %Inspect.Opts{} = opts) when is_atom(struct) do
|
||||
if opts.structs do
|
||||
try do
|
||||
Inspect.inspect(map, opts)
|
||||
rescue
|
||||
e ->
|
||||
stacktrace = System.stacktrace
|
||||
|
||||
# Because we try to raise a nice error message in case
|
||||
# we can't inspect a struct, there is a chance the error
|
||||
# message itself relies on the struct being printed, so
|
||||
# we need to trap the inspected messages to guarantee
|
||||
# we won't try to render any failed instruct when building
|
||||
# the error message.
|
||||
if Process.get(:inspect_trap) do
|
||||
Inspect.Map.inspect(map, opts)
|
||||
else
|
||||
try do
|
||||
Process.put(:inspect_trap, true)
|
||||
|
||||
res = Inspect.Map.inspect(map, opts)
|
||||
res = IO.iodata_to_binary(format(res, :infinity))
|
||||
|
||||
exception = Inspect.Error.exception(
|
||||
message: "got #{inspect e.__struct__} with message " <>
|
||||
"#{inspect Exception.message(e)} while inspecting #{res}"
|
||||
)
|
||||
|
||||
if opts.safe do
|
||||
Inspect.inspect(exception, opts)
|
||||
else
|
||||
reraise(exception, stacktrace)
|
||||
end
|
||||
after
|
||||
Process.delete(:inspect_trap)
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
Inspect.Map.inspect(map, opts)
|
||||
end
|
||||
end
|
||||
|
||||
def to_doc(arg, %Inspect.Opts{} = opts) do
|
||||
Inspect.inspect(arg, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a document entity used to represent nothingness.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Inspect.Algebra.empty
|
||||
:doc_nil
|
||||
|
||||
"""
|
||||
@spec empty() :: :doc_nil
|
||||
def empty, do: :doc_nil
|
||||
|
||||
@doc """
|
||||
Concatenates two document entities.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.concat "hello", "world"
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["hello", "world"]
|
||||
|
||||
"""
|
||||
@spec concat(t, t) :: doc_cons
|
||||
def concat(x, y) when is_doc(x) and is_doc(y) do
|
||||
doc_cons(x, y)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Concatenates a list of documents.
|
||||
"""
|
||||
@spec concat([t]) :: doc_cons
|
||||
def concat(docs) do
|
||||
fold_doc(docs, &concat(&1, &2))
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Nests document entity `x` positions deep.
|
||||
|
||||
Nesting will be appended to the line breaks.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.nest(Inspect.Algebra.glue("hello", "world"), 5)
|
||||
iex> Inspect.Algebra.format(doc, 5)
|
||||
["hello", "\n ", "world"]
|
||||
|
||||
"""
|
||||
@spec nest(t, non_neg_integer) :: doc_nest
|
||||
def nest(x, 0) when is_doc(x) do
|
||||
x
|
||||
end
|
||||
|
||||
def nest(x, i) when is_doc(x) and is_integer(i) do
|
||||
doc_nest(x, i)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Document entity representing a break.
|
||||
|
||||
This break can be rendered as a linebreak or as spaces,
|
||||
depending on the `mode` of the chosen layout or the provided
|
||||
separator.
|
||||
|
||||
## Examples
|
||||
|
||||
Let's glue two docs together with a break and then render it:
|
||||
|
||||
iex> doc = Inspect.Algebra.glue("a", " ", "b")
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["a", " ", "b"]
|
||||
|
||||
Notice the break was represented as is, because we haven't reached
|
||||
a line limit. Once we do, it is replaced by a newline:
|
||||
|
||||
iex> doc = Inspect.Algebra.glue(String.duplicate("a", 20), " ", "b")
|
||||
iex> Inspect.Algebra.format(doc, 10)
|
||||
["aaaaaaaaaaaaaaaaaaaa", "\n", "b"]
|
||||
|
||||
"""
|
||||
@spec break(binary) :: doc_break
|
||||
def break(s) when is_binary(s), do: doc_break(s)
|
||||
|
||||
@spec break() :: doc_break
|
||||
def break(), do: doc_break(@break)
|
||||
|
||||
@doc """
|
||||
Inserts a break between two docs. See `break/1` for more info.
|
||||
"""
|
||||
@spec glue(t, t) :: doc_cons
|
||||
def glue(x, y), do: concat(x, concat(break, y))
|
||||
|
||||
@doc """
|
||||
Inserts a break, passed as the second argument, between two docs,
|
||||
the first and the third arguments.
|
||||
"""
|
||||
@spec glue(t, binary, t) :: doc_cons
|
||||
def glue(x, g, y) when is_binary(g), do: concat(x, concat(break(g), y))
|
||||
|
||||
@doc ~S"""
|
||||
Returns a group containing the specified document.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.group(
|
||||
...> Inspect.Algebra.concat(
|
||||
...> Inspect.Algebra.group(
|
||||
...> Inspect.Algebra.concat(
|
||||
...> "Hello,",
|
||||
...> Inspect.Algebra.concat(
|
||||
...> Inspect.Algebra.break,
|
||||
...> "A"
|
||||
...> )
|
||||
...> )
|
||||
...> ),
|
||||
...> Inspect.Algebra.concat(
|
||||
...> Inspect.Algebra.break,
|
||||
...> "B"
|
||||
...> )
|
||||
...> ))
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["Hello,", " ", "A", " ", "B"]
|
||||
iex> Inspect.Algebra.format(doc, 6)
|
||||
["Hello,", "\n", "A", " ", "B"]
|
||||
|
||||
"""
|
||||
@spec group(t) :: doc_group
|
||||
def group(d) when is_doc(d) do
|
||||
doc_group(d)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inserts a mandatory single space between two document entities.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.space "Hughes", "Wadler"
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["Hughes", " ", "Wadler"]
|
||||
|
||||
"""
|
||||
@spec space(t, t) :: doc_cons
|
||||
def space(x, y), do: concat(x, concat(" ", y))
|
||||
|
||||
@doc ~S"""
|
||||
Inserts a mandatory linebreak between two document entities.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.line "Hughes", "Wadler"
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["Hughes", "\n", "Wadler"]
|
||||
|
||||
"""
|
||||
@spec line(t, t) :: doc_cons
|
||||
def line(x, y), do: concat(x, concat(:doc_line, y))
|
||||
|
||||
@doc """
|
||||
Folds a list of document entities into a document entity
|
||||
using a function that is passed as the first argument.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = ["A", "B"]
|
||||
iex> doc = Inspect.Algebra.fold_doc(doc, fn(x, y) ->
|
||||
...> Inspect.Algebra.concat [x, "!", y]
|
||||
...> end)
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["A", "!", "B"]
|
||||
|
||||
"""
|
||||
@spec fold_doc([t], ((t, t) -> t)) :: t
|
||||
def fold_doc(list, fun)
|
||||
def fold_doc([], _), do: empty
|
||||
def fold_doc([doc], _), do: doc
|
||||
def fold_doc([d|ds], fun), do: fun.(d, fold_doc(ds, fun))
|
||||
|
||||
# Elixir conveniences
|
||||
|
||||
@doc ~S"""
|
||||
Surrounds a document with characters.
|
||||
|
||||
Puts the document between left and right enclosing and nesting it.
|
||||
The document is marked as a group, to show the maximum as possible
|
||||
concisely together.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.surround "[", Inspect.Algebra.glue("a", "b"), "]"
|
||||
iex> Inspect.Algebra.format(doc, 3)
|
||||
["[", "a", "\n ", "b", "]"]
|
||||
|
||||
"""
|
||||
@spec surround(binary, t, binary) :: t
|
||||
def surround(left, doc, right) do
|
||||
group concat left, concat(nest(doc, @nesting), right)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Maps and glues a collection of items.
|
||||
|
||||
It uses the given left and right as surrounding and a separator for
|
||||
each item. A limit can be passed which, once reached, stops gluing
|
||||
and outputs "..." instead.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> doc = Inspect.Algebra.surround_many("[", Enum.to_list(1..5), "]",
|
||||
...> %Inspect.Opts{limit: :infinity}, fn i, _opts -> to_string(i) end)
|
||||
iex> Inspect.Algebra.format(doc, 5) |> IO.iodata_to_binary
|
||||
"[1,\n 2,\n 3,\n 4,\n 5]"
|
||||
|
||||
iex> doc = Inspect.Algebra.surround_many("[", Enum.to_list(1..5), "]",
|
||||
...> %Inspect.Opts{limit: 3}, fn i, _opts -> to_string(i) end)
|
||||
iex> Inspect.Algebra.format(doc, 20) |> IO.iodata_to_binary
|
||||
"[1, 2, 3, ...]"
|
||||
|
||||
iex> doc = Inspect.Algebra.surround_many("[", Enum.to_list(1..5), "]",
|
||||
...> %Inspect.Opts{limit: 3}, fn i, _opts -> to_string(i) end, "!")
|
||||
iex> Inspect.Algebra.format(doc, 20) |> IO.iodata_to_binary
|
||||
"[1! 2! 3! ...]"
|
||||
"""
|
||||
@spec surround_many(binary, [any], binary, Inspect.Opts.t, (term, Inspect.Opts.t -> t), binary) :: t
|
||||
def surround_many(left, docs, right, opts, fun, separator \\ @surround_separator) do
|
||||
do_surround_many(left, docs, right, opts.limit, opts, fun, separator)
|
||||
end
|
||||
|
||||
defp do_surround_many(left, [], right, _, _opts, _fun, _) do
|
||||
concat(left, right)
|
||||
end
|
||||
|
||||
defp do_surround_many(left, docs, right, limit, opts, fun, sep) do
|
||||
surround(left, do_surround_many(docs, limit, opts, fun, sep), right)
|
||||
end
|
||||
|
||||
defp do_surround_many(_, 0, _opts, _fun, _sep) do
|
||||
"..."
|
||||
end
|
||||
|
||||
defp do_surround_many([], _limit, _opts, _fun, _sep) do
|
||||
:doc_nil
|
||||
end
|
||||
|
||||
defp do_surround_many([h], limit, opts, fun, _sep) do
|
||||
fun.(h, %{opts | limit: limit})
|
||||
end
|
||||
|
||||
defp do_surround_many([h|t], limit, opts, fun, sep) when is_list(t) do
|
||||
limit = decrement(limit)
|
||||
h = fun.(h, %{opts | limit: limit})
|
||||
t = do_surround_many(t, limit, opts, fun, sep)
|
||||
do_join(h, t, sep)
|
||||
end
|
||||
|
||||
defp do_surround_many([h|t], limit, opts, fun, _sep) do
|
||||
limit = decrement(limit)
|
||||
h = fun.(h, %{opts | limit: limit})
|
||||
t = fun.(t, %{opts | limit: limit})
|
||||
do_join(h, t, @tail_separator)
|
||||
end
|
||||
|
||||
defp do_join(:doc_nil, :doc_nil, _), do: :doc_nil
|
||||
defp do_join(h, :doc_nil, _), do: h
|
||||
defp do_join(:doc_nil, t, _), do: t
|
||||
defp do_join(h, t, sep), do: glue(concat(h, sep), t)
|
||||
|
||||
defp decrement(:infinity), do: :infinity
|
||||
defp decrement(counter), do: counter - 1
|
||||
|
||||
@doc """
|
||||
The formatting function.
|
||||
|
||||
Takes the maximum width and a document to print as its arguments
|
||||
and returns an IO data representation of the best layout for the
|
||||
document to fit in the given width.
|
||||
"""
|
||||
@spec format(t, non_neg_integer | :infinity) :: iodata
|
||||
def format(d, w) do
|
||||
format(w, 0, [{0, default_mode(w), doc_group(d)}])
|
||||
end
|
||||
|
||||
defp default_mode(:infinity), do: :flat
|
||||
defp default_mode(_), do: :break
|
||||
|
||||
# Record representing the document mode to be rendered: flat or broken
|
||||
@typep mode :: :flat | :break
|
||||
|
||||
@spec fits?(integer, [{integer, mode, t}]) :: boolean
|
||||
defp fits?(w, _) when w < 0, do: false
|
||||
defp fits?(_, []), do: true
|
||||
defp fits?(_, [{_, _, :doc_line} | _]), do: true
|
||||
defp fits?(w, [{_, _, :doc_nil} | t]), do: fits?(w, t)
|
||||
defp fits?(w, [{i, m, doc_cons(x, y)} | t]), do: fits?(w, [{i, m, x} | [{i, m, y} | t]])
|
||||
defp fits?(w, [{i, m, doc_nest(x, j)} | t]), do: fits?(w, [{i + j, m, x} | t])
|
||||
defp fits?(w, [{i, _, doc_group(x)} | t]), do: fits?(w, [{i, :flat, x} | t])
|
||||
defp fits?(w, [{_, _, s} | t]) when is_binary(s), do: fits?((w - byte_size s), t)
|
||||
defp fits?(w, [{_, :flat, doc_break(s)} | t]), do: fits?((w - byte_size s), t)
|
||||
defp fits?(_, [{_, :break, doc_break(_)} | _]), do: true
|
||||
|
||||
@spec format(integer | :infinity, integer, [{integer, mode, t}]) :: [binary]
|
||||
defp format(_, _, []), do: []
|
||||
defp format(w, _, [{i, _, :doc_line} | t]), do: [indent(i) | format(w, i, t)]
|
||||
defp format(w, k, [{_, _, :doc_nil} | t]), do: format(w, k, t)
|
||||
defp format(w, k, [{i, m, doc_cons(x, y)} | t]), do: format(w, k, [{i, m, x} | [{i, m, y} | t]])
|
||||
defp format(w, k, [{i, m, doc_nest(x, j)} | t]), do: format(w, k, [{i + j, m, x} | t])
|
||||
defp format(w, k, [{i, m, doc_group(x)} | t]), do: format(w, k, [{i, m, x} | t])
|
||||
defp format(w, k, [{_, _, s} | t]) when is_binary(s), do: [s | format(w, (k + byte_size s), t)]
|
||||
defp format(w, k, [{_, :flat, doc_break(s)} | t]), do: [s | format(w, (k + byte_size s), t)]
|
||||
defp format(w, k, [{i, :break, doc_break(s)} | t]) do
|
||||
k = k + byte_size(s)
|
||||
|
||||
if w == :infinity or fits?(w - k, t) do
|
||||
[s | format(w, k, t)]
|
||||
else
|
||||
[indent(i) | format(w, i, t)]
|
||||
end
|
||||
end
|
||||
|
||||
defp indent(0), do: @newline
|
||||
defp indent(i), do: @newline <> :binary.copy(" ", i)
|
||||
end
|
||||
@@ -1,264 +0,0 @@
|
||||
defmodule Integer do
|
||||
@moduledoc """
|
||||
Functions for working with integers.
|
||||
"""
|
||||
|
||||
import Bitwise
|
||||
|
||||
@doc """
|
||||
Determines if an integer is odd.
|
||||
|
||||
Returns `true` if `n` is an odd number, otherwise `false`.
|
||||
|
||||
Allowed in guard clauses.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.is_odd(3)
|
||||
true
|
||||
|
||||
iex> Integer.is_odd(4)
|
||||
false
|
||||
"""
|
||||
defmacro is_odd(n) do
|
||||
quote do: (unquote(n) &&& 1) == 1
|
||||
end
|
||||
|
||||
@doc """
|
||||
Determines if an integer is even.
|
||||
|
||||
Returns `true` if `n` is an even number, otherwise `false`.
|
||||
|
||||
Allowed in guard clauses.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.is_even(10)
|
||||
true
|
||||
|
||||
iex> Integer.is_even(5)
|
||||
false
|
||||
"""
|
||||
defmacro is_even(n) do
|
||||
quote do: (unquote(n) &&& 1) == 0
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the ordered digits for the given non-negative integer.
|
||||
|
||||
An optional base value may be provided representing the radix for the returned
|
||||
digits.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.digits(101)
|
||||
[1, 0, 1]
|
||||
|
||||
iex> Integer.digits(58127, 2)
|
||||
[1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
|
||||
|
||||
"""
|
||||
@spec digits(non_neg_integer, pos_integer) :: [non_neg_integer]
|
||||
def digits(n, base \\ 10) when is_integer(n) and n >= 0
|
||||
and is_integer(base) and base >= 2 do
|
||||
do_digits(n, base, [])
|
||||
end
|
||||
|
||||
defp do_digits(0, _base, []), do: [0]
|
||||
defp do_digits(0, _base, acc), do: acc
|
||||
defp do_digits(n, base, acc) do
|
||||
do_digits div(n, base), base, [rem(n, base) | acc]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the integer represented by the ordered digits.
|
||||
|
||||
An optional base value may be provided representing the radix for the digits.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.undigits([1, 0, 1])
|
||||
101
|
||||
|
||||
iex> Integer.undigits([1, 4], 16)
|
||||
20
|
||||
"""
|
||||
@spec undigits([integer], integer) :: integer
|
||||
def undigits(digits, base \\ 10) when is_integer(base) do
|
||||
do_undigits(digits, base, 0)
|
||||
end
|
||||
|
||||
defp do_undigits([], _base, acc), do: acc
|
||||
defp do_undigits([digit | tail], base, acc) do
|
||||
do_undigits(tail, base, acc * base + digit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a binary from a text representation of an integer
|
||||
in an optional base `base` to the corresponding integer.
|
||||
|
||||
If the base `base` is not given, base 10 will be used.
|
||||
|
||||
If successful, returns a tuple of the form `{integer, remainder_of_binary}`.
|
||||
Otherwise `:error`.
|
||||
|
||||
Raises an error if `base` is less than 2 or more than 36.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.parse("34")
|
||||
{34, ""}
|
||||
|
||||
iex> Integer.parse("34.5")
|
||||
{34, ".5"}
|
||||
|
||||
iex> Integer.parse("three")
|
||||
:error
|
||||
|
||||
iex> Integer.parse("34", 10)
|
||||
{34, ""}
|
||||
|
||||
iex> Integer.parse("f4", 16)
|
||||
{244, ""}
|
||||
|
||||
iex> Integer.parse("Awww++", 36)
|
||||
{509216, "++"}
|
||||
|
||||
iex> Integer.parse("fab", 10)
|
||||
:error
|
||||
|
||||
iex> Integer.parse("a2", 38)
|
||||
** (ArgumentError) invalid base 38
|
||||
|
||||
"""
|
||||
@spec parse(binary, 2..36) :: {integer, binary} | :error | no_return
|
||||
def parse(binary, base \\ 10)
|
||||
|
||||
def parse(binary, base) when is_integer(base) and base in 2..36 do
|
||||
parse_in_base(binary, base)
|
||||
end
|
||||
|
||||
def parse(_, base) do
|
||||
raise ArgumentError, "invalid base #{base}"
|
||||
end
|
||||
|
||||
defp parse_in_base("-" <> bin, base) do
|
||||
case do_parse(bin, base) do
|
||||
:error -> :error
|
||||
{number, remainder} -> {-number, remainder}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_in_base("+" <> bin, base) do
|
||||
do_parse(bin, base)
|
||||
end
|
||||
|
||||
defp parse_in_base(bin, base) when is_binary(bin) do
|
||||
do_parse(bin, base)
|
||||
end
|
||||
|
||||
defp do_parse(<<char, rest::binary>>, base) do
|
||||
if valid_digit_in_base?(char, base) do
|
||||
do_parse(rest, base, parse_digit(char, base))
|
||||
else
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
defp do_parse(_, _), do: :error
|
||||
|
||||
defp do_parse(<<char, rest::binary>> = bin, base, acc) do
|
||||
if valid_digit_in_base?(char, base) do
|
||||
do_parse(rest, base, base * acc + parse_digit(char, base))
|
||||
else
|
||||
{acc, bin}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_parse(bitstring, _, acc) do
|
||||
{acc, bitstring}
|
||||
end
|
||||
|
||||
defp parse_digit(char, _) do
|
||||
cond do
|
||||
char in ?0..?9 -> char - ?0
|
||||
char in ?A..?Z -> char - ?A + 10
|
||||
true -> char - ?a + 10
|
||||
end
|
||||
end
|
||||
|
||||
defp valid_digit_in_base?(char, base) do
|
||||
if base <= 10 do
|
||||
char in ?0..(?0 + base - 1)
|
||||
else
|
||||
char in ?0..?9 or char in ?A..(?A + base - 11) or char in ?a..(?a + base - 11)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `some_integer`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_string(123)
|
||||
"123"
|
||||
|
||||
"""
|
||||
@spec to_string(integer) :: String.t
|
||||
def to_string(some_integer) do
|
||||
:erlang.integer_to_binary(some_integer)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `some_integer` in base `base`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_string(100, 16)
|
||||
"64"
|
||||
|
||||
"""
|
||||
@spec to_string(integer, 2..36) :: String.t
|
||||
def to_string(some_integer, base) do
|
||||
:erlang.integer_to_binary(some_integer, base)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a char list which corresponds to the text representation of the given integer.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_char_list(7)
|
||||
'7'
|
||||
|
||||
"""
|
||||
@spec to_char_list(integer) :: char_list
|
||||
def to_char_list(number) do
|
||||
:erlang.integer_to_list(number)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a char list which corresponds to the text representation of the
|
||||
given integer in the given base.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_char_list(1023, 16)
|
||||
'3FF'
|
||||
|
||||
"""
|
||||
@spec to_char_list(integer, 2..36) :: char_list
|
||||
def to_char_list(number, base) do
|
||||
:erlang.integer_to_list(number, base)
|
||||
end
|
||||
end
|
||||
+74
-382
@@ -1,145 +1,59 @@
|
||||
defmodule IO do
|
||||
@moduledoc """
|
||||
Functions handling IO.
|
||||
Module responsible for doing IO. The function in this
|
||||
module expects an iodata as argument encoded in UTF-8.
|
||||
An iodata can be:
|
||||
|
||||
Many functions in this module expect an IO device as an argument.
|
||||
An IO device must be a pid or an atom representing a process.
|
||||
For convenience, Elixir provides `:stdio` and `:stderr` as
|
||||
shortcuts to Erlang's `:standard_io` and `:standard_error`.
|
||||
|
||||
The majority of the functions expect char data, i.e. strings or
|
||||
lists of characters and strings. In case another type is given,
|
||||
functions will convert to string via the `String.Chars` protocol
|
||||
(as shown in typespecs).
|
||||
|
||||
The functions starting with `bin*` expect iodata as an argument,
|
||||
i.e. binaries or lists of bytes and binaries.
|
||||
|
||||
## IO devices
|
||||
|
||||
An IO device may be an atom or a pid. In case it is an atom,
|
||||
the atom must be the name of a registered process. In addition,
|
||||
Elixir provides two shorcuts:
|
||||
|
||||
* `:stdio` - a shortcut for `:standard_io`, which maps to
|
||||
the current `Process.group_leader/0` in Erlang
|
||||
|
||||
* `:stderr` - a shortcut for the named process `:standard_error`
|
||||
provided in Erlang
|
||||
|
||||
IO devices maintain their position, that means subsequent calls to any
|
||||
reading or writing functions will start from the place when the device
|
||||
was last accessed. Position of files can be changed using the
|
||||
`:file.position/2` function.
|
||||
* A list of integers representing a string. Any unicode
|
||||
character must be represented with one entry in the list,
|
||||
this entry being an integer with the codepoint value;
|
||||
* A binary in which unicode characters are represented
|
||||
with many bytes (Elixir's default representation);
|
||||
* A list of binaries or a list of char lists (as described above);
|
||||
* If none of the above, `to_binary` is invoked in the
|
||||
given argument;
|
||||
|
||||
"""
|
||||
|
||||
@type device :: atom | pid
|
||||
@type nodata :: {:error, term} | :eof
|
||||
@type chardata() :: :unicode.chardata()
|
||||
@doc """
|
||||
Reads `count` bytes from the IO device. It returns:
|
||||
|
||||
import :erlang, only: [group_leader: 0]
|
||||
* `data` - The input characters.
|
||||
|
||||
defmacrop is_iodata(data) do
|
||||
quote do
|
||||
is_list(unquote(data)) or is_binary(unquote(data))
|
||||
end
|
||||
* :eof - End of file was encountered.
|
||||
|
||||
* {:error, reason} - Other (rare) error condition,
|
||||
for instance {:error, :estale} if reading from an
|
||||
NFS file system.
|
||||
"""
|
||||
def read(device // :stdio, count) do
|
||||
Erlang.io.get_chars(map_dev(device), "", count)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads `count` characters from the IO device, a whole
|
||||
`:line` or the whole device with `:all`.
|
||||
Read a line from the IO device. It returns:
|
||||
|
||||
It returns:
|
||||
* `data` - The input characters.
|
||||
|
||||
* `data` - the input characters
|
||||
* :eof - End of file was encountered.
|
||||
|
||||
* `:eof` - end of file was encountered
|
||||
* {:error, reason} - Other (rare) error condition,
|
||||
for instance {:error, :estale} if reading from an
|
||||
NFS file system.
|
||||
|
||||
* `{:error, reason}` - other (rare) error condition;
|
||||
for instance, `{:error, :estale}` if reading from an
|
||||
NFS volume
|
||||
|
||||
If `:all` is given, `:eof` is never returned, but an
|
||||
empty string in case the device has reached EOF.
|
||||
This function does the same as `gets/2`,
|
||||
except the prompt is not required as argument.
|
||||
"""
|
||||
@spec read(device, :all | :line | non_neg_integer) :: chardata | nodata
|
||||
def read(device \\ group_leader, chars_or_line)
|
||||
|
||||
def read(device, :all) do
|
||||
do_read_all(map_dev(device), "")
|
||||
end
|
||||
|
||||
def read(device, :line) do
|
||||
:io.get_line(map_dev(device), '')
|
||||
end
|
||||
|
||||
def read(device, count) when count >= 0 do
|
||||
:io.get_chars(map_dev(device), '', count)
|
||||
end
|
||||
|
||||
defp do_read_all(mapped_dev, acc) do
|
||||
case :io.get_line(mapped_dev, "") do
|
||||
line when is_binary(line) -> do_read_all(mapped_dev, acc <> line)
|
||||
:eof -> acc
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads `count` characters from the IO device, a whole
|
||||
`:line` or the whole device with `:all`.
|
||||
|
||||
It returns:
|
||||
|
||||
* `data` - the input characters
|
||||
|
||||
* `:eof` - end of file was encountered
|
||||
|
||||
* `{:error, reason}` - other (rare) error condition;
|
||||
for instance, `{:error, :estale}` if reading from an
|
||||
NFS volume
|
||||
|
||||
If `:all` is given, `:eof` is never returned, but an
|
||||
empty string in case the device has reached EOF.
|
||||
|
||||
Note: do not use this function on IO devices in unicode mode
|
||||
as it will return the wrong result.
|
||||
"""
|
||||
@spec binread(device, :all | :line | non_neg_integer) :: iodata | nodata
|
||||
def binread(device \\ group_leader, chars_or_line)
|
||||
|
||||
def binread(device, :all) do
|
||||
do_binread_all(map_dev(device), "")
|
||||
end
|
||||
|
||||
def binread(device, :line) do
|
||||
case :file.read_line(map_dev(device)) do
|
||||
{:ok, data} -> data
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
def binread(device, count) when count >= 0 do
|
||||
case :file.read(map_dev(device), count) do
|
||||
{:ok, data} -> data
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
@read_all_size 4096
|
||||
defp do_binread_all(mapped_dev, acc) do
|
||||
case :file.read(mapped_dev, @read_all_size) do
|
||||
{:ok, data} -> do_binread_all(mapped_dev, acc <> data)
|
||||
:eof -> acc
|
||||
other -> other
|
||||
end
|
||||
def readline(device // :stdio) do
|
||||
Erlang.io.get_line(map_dev(device), "")
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes the given argument to the given device.
|
||||
|
||||
By default the device is the standard output.
|
||||
The argument is expected to be a chardata (i.e.
|
||||
a char list or an unicode binary).
|
||||
|
||||
It returns `:ok` if it succeeds.
|
||||
|
||||
## Examples
|
||||
@@ -151,293 +65,71 @@ defmodule IO do
|
||||
#=> "error"
|
||||
|
||||
"""
|
||||
@spec write(device, chardata | String.Chars.t) :: :ok
|
||||
def write(device \\ group_leader(), item) do
|
||||
:io.put_chars map_dev(device), to_chardata(item)
|
||||
def write(device // :stdio, item) do
|
||||
Erlang.io.put_chars map_dev(device), to_iodata(item)
|
||||
end
|
||||
|
||||
def print(device // :stdio, item) do
|
||||
IO.puts "IO.print is deprecated in favor of IO.write"
|
||||
Erlang.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.
|
||||
|
||||
Note: do not use this function on IO devices in unicode mode
|
||||
as it will return the wrong result.
|
||||
"""
|
||||
@spec binwrite(device, iodata) :: :ok | {:error, term}
|
||||
def binwrite(device \\ group_leader(), item) when is_iodata(item) do
|
||||
:file.write map_dev(device), item
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes the argument to the device, similar to `write/2`,
|
||||
but adds a newline at the end. The argument is expected
|
||||
Writes the argument to the device, similarly to write
|
||||
but adds a new line at the end. The argument is expected
|
||||
to be a chardata.
|
||||
"""
|
||||
@spec puts(device, chardata | String.Chars.t) :: :ok
|
||||
def puts(device \\ group_leader(), item) do
|
||||
def puts(device // :stdio, item) do
|
||||
erl_dev = map_dev(device)
|
||||
:io.put_chars erl_dev, [to_chardata(item), ?\n]
|
||||
Erlang.io.put_chars erl_dev, to_iodata(item)
|
||||
Erlang.io.nl(erl_dev)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inspects and writes the given argument to the device.
|
||||
|
||||
It enables pretty printing by default with width of
|
||||
80 characters. The width can be changed by explicitly
|
||||
passing the `:width` option.
|
||||
|
||||
See `Inspect.Opts` for a full list of options.
|
||||
|
||||
## Examples
|
||||
|
||||
IO.inspect Process.list, width: 40
|
||||
|
||||
Inspects and writes the given argument to the device
|
||||
followed by a new line. Returns the item given.
|
||||
"""
|
||||
@spec inspect(item, Keyword.t) :: item when item: var
|
||||
def inspect(item, opts \\ []) do
|
||||
inspect group_leader(), item, opts
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inspects the item with options using the given device.
|
||||
|
||||
See `Inspect.Opts` for a full list of options.
|
||||
"""
|
||||
@spec inspect(device, item, Keyword.t) :: item when item: var
|
||||
def inspect(device, item, opts) when is_list(opts) do
|
||||
opts = struct(Inspect.Opts, opts)
|
||||
iodata = Inspect.Algebra.format(Inspect.Algebra.to_doc(item, opts), opts.width)
|
||||
puts device, iodata
|
||||
def inspect(device // :stdio, item) do
|
||||
puts device, Kernel.inspect(item)
|
||||
item
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a number of bytes from the io device. If the
|
||||
io device is a unicode device, `count` implies
|
||||
the number of unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
It returns:
|
||||
Gets `count` bytes from the IO device. It returns:
|
||||
|
||||
* `data` - the input characters
|
||||
* `data` - The input characters.
|
||||
|
||||
* `:eof` - end of file was encountered
|
||||
* :eof - End of file was encountered.
|
||||
|
||||
* `{:error, reason}` - other (rare) error condition;
|
||||
for instance, `{:error, :estale}` if reading from an
|
||||
NFS volume
|
||||
* {:error, reason} - Other (rare) error condition,
|
||||
for instance {:error, :estale} if reading from an
|
||||
NFS file system.
|
||||
"""
|
||||
@spec getn(chardata | String.Chars.t, pos_integer) :: chardata | nodata
|
||||
@spec getn(device, chardata | String.Chars.t) :: chardata | nodata
|
||||
def getn(prompt, count \\ 1)
|
||||
|
||||
def getn(prompt, count) when is_integer(count) do
|
||||
getn(group_leader, prompt, count)
|
||||
end
|
||||
|
||||
def getn(device, prompt) do
|
||||
getn(device, prompt, 1)
|
||||
def getb(device // :stdio, prompt, count // 1) do
|
||||
Erlang.io.get_chars(map_dev(device), to_iodata(prompt), count)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a number of bytes from the io device. If the
|
||||
io device is a unicode device, `count` implies
|
||||
the number of unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
Reads a line from the IO device. It returns:
|
||||
|
||||
* `data` - The characters in the line terminated
|
||||
by a LF (or end of file).
|
||||
|
||||
* :eof - End of file was encountered.
|
||||
|
||||
* {:error, reason} - Other (rare) error condition,
|
||||
for instance {:error, :estale} if reading from an
|
||||
NFS file system.
|
||||
"""
|
||||
@spec getn(device, chardata | String.Chars.t, pos_integer) :: chardata | nodata
|
||||
def getn(device, prompt, count) do
|
||||
:io.get_chars(map_dev(device), to_chardata(prompt), count)
|
||||
def gets(device // :stdio, prompt) do
|
||||
Erlang.io.get_line(map_dev(device), to_iodata(prompt))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads a line from the IO device.
|
||||
|
||||
It returns:
|
||||
|
||||
* `data` - the characters in the line terminated
|
||||
by a line-feed (LF) or end of file (EOF)
|
||||
|
||||
* `:eof` - end of file was encountered
|
||||
|
||||
* `{:error, reason}` - other (rare) error condition;
|
||||
for instance, `{:error, :estale}` if reading from an
|
||||
NFS volume
|
||||
|
||||
## Examples
|
||||
|
||||
To display "What is your name?" as a prompt and await user input:
|
||||
|
||||
IO.gets "What is your name?"
|
||||
"""
|
||||
@spec gets(device, chardata | String.Chars.t) :: chardata | nodata
|
||||
def gets(device \\ group_leader(), prompt) do
|
||||
:io.get_line(map_dev(device), to_chardata(prompt))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the io device into a `IO.Stream`.
|
||||
|
||||
An `IO.Stream` implements both `Enumerable` and
|
||||
`Collectable`, allowing it to be used for both read
|
||||
and write.
|
||||
|
||||
The device is iterated line by line if `:line` is given or
|
||||
by a given number of codepoints.
|
||||
|
||||
This reads the IO as utf-8. Check out
|
||||
`IO.binstream/2` to handle the IO as a raw binary.
|
||||
|
||||
Note that an IO stream has side effects and every time
|
||||
you go over the stream you may get different results.
|
||||
|
||||
## Examples
|
||||
|
||||
Here is an example on how we mimic an echo server
|
||||
from the command line:
|
||||
|
||||
Enum.each IO.stream(:stdio, :line), &IO.write(&1)
|
||||
|
||||
"""
|
||||
@spec stream(device, :line | pos_integer) :: Enumerable.t
|
||||
def stream(device, line_or_codepoints) do
|
||||
IO.Stream.__build__(map_dev(device), false, line_or_codepoints)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the IO device into a `IO.Stream`.
|
||||
|
||||
An `IO.Stream` implements both `Enumerable` and
|
||||
`Collectable`, allowing it to be used for both read
|
||||
and write.
|
||||
|
||||
The device is iterated line by line or by a number of bytes.
|
||||
This reads the IO device as a raw binary.
|
||||
|
||||
Note that an IO stream has side effects and every time
|
||||
you go over the stream you may get different results.
|
||||
|
||||
Finally, do not use this function on IO devices in unicode
|
||||
mode as it will return the wrong result.
|
||||
"""
|
||||
@spec binstream(device, :line | pos_integer) :: Enumerable.t
|
||||
def binstream(device, line_or_bytes) do
|
||||
IO.Stream.__build__(map_dev(device), true, line_or_bytes)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts chardata (a list of integers representing codepoints,
|
||||
lists and strings) into a string.
|
||||
|
||||
In case the conversion fails, it raises a `UnicodeConversionError`.
|
||||
If a string is given, returns the string itself.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> IO.chardata_to_string([0x00E6, 0x00DF])
|
||||
"æß"
|
||||
|
||||
iex> IO.chardata_to_string([0x0061, "bc"])
|
||||
"abc"
|
||||
|
||||
"""
|
||||
@spec chardata_to_string(chardata) :: String.t | no_return
|
||||
def chardata_to_string(string) when is_binary(string) do
|
||||
string
|
||||
end
|
||||
|
||||
def chardata_to_string(list) when is_list(list) do
|
||||
case :unicode.characters_to_binary(list) do
|
||||
result when is_binary(result) ->
|
||||
result
|
||||
|
||||
{:error, encoded, rest} ->
|
||||
raise UnicodeConversionError, encoded: encoded, rest: rest, kind: :invalid
|
||||
|
||||
{:incomplete, encoded, rest} ->
|
||||
raise UnicodeConversionError, encoded: encoded, rest: rest, kind: :incomplete
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts iodata (a list of integers representing bytes, lists
|
||||
and binaries) into a binary.
|
||||
|
||||
Notice that this function treats lists of integers as raw bytes
|
||||
and does not perform any kind of encoding conversion. If you want
|
||||
to convert from a char list to a string (UTF-8 encoded), please
|
||||
use `chardata_to_string/1` instead.
|
||||
|
||||
If this function receives a binary, the same binary is returned.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> bin1 = <<1, 2, 3>>
|
||||
iex> bin2 = <<4, 5>>
|
||||
iex> bin3 = <<6>>
|
||||
iex> IO.iodata_to_binary([bin1, 1, [2, 3, bin2], 4|bin3])
|
||||
<<1, 2, 3, 1, 2, 3, 4, 5, 4, 6>>
|
||||
|
||||
iex> bin = <<1, 2, 3>>
|
||||
iex> IO.iodata_to_binary(bin)
|
||||
<<1, 2, 3>>
|
||||
|
||||
"""
|
||||
@spec iodata_to_binary(iodata) :: binary
|
||||
def iodata_to_binary(item) do
|
||||
:erlang.iolist_to_binary(item)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the size of an iodata.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> IO.iodata_length([1, 2|<<3, 4>>])
|
||||
4
|
||||
|
||||
"""
|
||||
@spec iodata_length(iodata) :: non_neg_integer
|
||||
def iodata_length(item) do
|
||||
:erlang.iolist_size(item)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def each_stream(device, what) do
|
||||
case read(device, what) do
|
||||
:eof ->
|
||||
{:halt, device}
|
||||
{:error, reason} ->
|
||||
raise IO.StreamError, reason: reason
|
||||
data ->
|
||||
{[data], device}
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def each_binstream(device, what) do
|
||||
case binread(device, what) do
|
||||
:eof ->
|
||||
{:halt, device}
|
||||
{:error, reason} ->
|
||||
raise IO.StreamError, reason: reason
|
||||
data ->
|
||||
{[data], device}
|
||||
end
|
||||
end
|
||||
|
||||
@compile {:inline, map_dev: 1, to_chardata: 1}
|
||||
|
||||
# Map the Elixir names for standard io and error to Erlang names
|
||||
defp map_dev(:stdio), do: :standard_io
|
||||
defp map_dev(:stderr), do: :standard_error
|
||||
defp map_dev(other) when is_atom(other) or is_pid(other) or is_tuple(other), do: other
|
||||
defp map_dev(other), do: other
|
||||
|
||||
defp to_chardata(list) when is_list(list), do: list
|
||||
defp to_chardata(other), do: to_string(other)
|
||||
defp to_iodata(io) when is_list(io) or is_binary(io), do: io
|
||||
defp to_iodata(other), do: to_binary(other)
|
||||
end
|
||||
|
||||
@@ -1,240 +0,0 @@
|
||||
defmodule IO.ANSI.Sequence do
|
||||
@moduledoc false
|
||||
|
||||
defmacro defsequence(name, code, terminator \\ "m") do
|
||||
quote bind_quoted: [name: name, code: code, terminator: terminator] do
|
||||
def unquote(name)() do
|
||||
"\e[#{unquote(code)}#{unquote(terminator)}"
|
||||
end
|
||||
|
||||
defp format_sequence(unquote(name)) do
|
||||
unquote(name)()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule IO.ANSI do
|
||||
@moduledoc """
|
||||
Functionality to render ANSI escape sequences.
|
||||
|
||||
[ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code)
|
||||
are characters embedded in text used to control formatting, color, and
|
||||
other output options on video text terminals.
|
||||
"""
|
||||
|
||||
import IO.ANSI.Sequence
|
||||
|
||||
@typep ansicode :: atom()
|
||||
@typep ansilist :: maybe_improper_list(char() | ansicode() | binary() | ansilist(), binary() | ansicode() | [])
|
||||
@type ansidata :: ansilist() | ansicode() | binary()
|
||||
|
||||
@doc """
|
||||
Checks if ANSI coloring is supported and enabled on this machine.
|
||||
|
||||
This function simply reads the configuration value for
|
||||
`:ansi_enabled` in the `:elixir` application. The value is by
|
||||
default `false` unless Elixir can detect during startup that
|
||||
both `stdout` and `stderr` are terminals.
|
||||
"""
|
||||
@spec enabled? :: boolean
|
||||
def enabled? do
|
||||
Application.get_env(:elixir, :ansi_enabled, false)
|
||||
end
|
||||
|
||||
@doc "Sets foreground color"
|
||||
@spec color(0..255) :: String.t
|
||||
def color(code) when code in 0..255, do: "\e[38;5;#{code}m"
|
||||
|
||||
@doc ~S"""
|
||||
Sets the foreground color from individual RGB values.
|
||||
|
||||
Valid values for each color are in the range 0 to 5.
|
||||
"""
|
||||
@spec color(0..5, 0..5, 0..5) :: String.t
|
||||
def color(r, g, b) when r in 0..5 and g in 0..5 and b in 0..5 do
|
||||
color(16 + (36 * r) + (6 * g) + b)
|
||||
end
|
||||
|
||||
@doc "Sets background color"
|
||||
@spec color_background(0..255) :: String.t
|
||||
def color_background(code) when code in 0..255, do: "\e[48;5;#{code}m"
|
||||
|
||||
@doc ~S"""
|
||||
Sets the background color from individual RGB values.
|
||||
|
||||
Valid values for each color are in the range 0 to 5.
|
||||
"""
|
||||
@spec color_background(0..5, 0..5, 0..5) :: String.t
|
||||
def color_background(r, g, b) when r in 0..5 and g in 0..5 and b in 0..5 do
|
||||
color_background(16 + (36 * r) + (6 * g) + b)
|
||||
end
|
||||
|
||||
@doc "Resets all attributes"
|
||||
defsequence :reset, 0
|
||||
|
||||
@doc "Bright (increased intensity) or Bold"
|
||||
defsequence :bright, 1
|
||||
|
||||
@doc "Faint (decreased intensity), not widely supported"
|
||||
defsequence :faint, 2
|
||||
|
||||
@doc "Italic: on. Not widely supported. Sometimes treated as inverse"
|
||||
defsequence :italic, 3
|
||||
|
||||
@doc "Underline: Single"
|
||||
defsequence :underline, 4
|
||||
|
||||
@doc "Blink: Slow. Less than 150 per minute"
|
||||
defsequence :blink_slow, 5
|
||||
|
||||
@doc "Blink: Rapid. MS-DOS ANSI.SYS; 150 per minute or more; not widely supported"
|
||||
defsequence :blink_rapid, 6
|
||||
|
||||
@doc "Image: Negative. Swap foreground and background"
|
||||
defsequence :inverse, 7
|
||||
|
||||
@doc "Image: Negative. Swap foreground and background"
|
||||
defsequence :reverse, 7
|
||||
|
||||
@doc "Conceal. Not widely supported"
|
||||
defsequence :conceal, 8
|
||||
|
||||
@doc "Crossed-out. Characters legible, but marked for deletion. Not widely supported"
|
||||
defsequence :crossed_out, 9
|
||||
|
||||
@doc "Sets primary (default) font"
|
||||
defsequence :primary_font, 10
|
||||
|
||||
for font_n <- [1, 2, 3, 4, 5, 6, 7, 8, 9] do
|
||||
@doc "Sets alternative font #{font_n}"
|
||||
defsequence :"font_#{font_n}", font_n + 10
|
||||
end
|
||||
|
||||
@doc "Normal color or intensity"
|
||||
defsequence :normal, 22
|
||||
|
||||
@doc "Not italic"
|
||||
defsequence :not_italic, 23
|
||||
|
||||
@doc "Underline: None"
|
||||
defsequence :no_underline, 24
|
||||
|
||||
@doc "Blink: off"
|
||||
defsequence :blink_off, 25
|
||||
|
||||
colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
|
||||
|
||||
for {color, code} <- Enum.with_index(colors) do
|
||||
@doc "Sets foreground color to #{color}"
|
||||
defsequence color, code + 30
|
||||
|
||||
@doc "Sets background color to #{color}"
|
||||
defsequence :"#{color}_background", code + 40
|
||||
end
|
||||
|
||||
@doc "Default text color"
|
||||
defsequence :default_color, 39
|
||||
|
||||
@doc "Default background color"
|
||||
defsequence :default_background, 49
|
||||
|
||||
@doc "Framed"
|
||||
defsequence :framed, 51
|
||||
|
||||
@doc "Encircled"
|
||||
defsequence :encircled, 52
|
||||
|
||||
@doc "Overlined"
|
||||
defsequence :overlined, 53
|
||||
|
||||
@doc "Not framed or encircled"
|
||||
defsequence :not_framed_encircled, 54
|
||||
|
||||
@doc "Not overlined"
|
||||
defsequence :not_overlined, 55
|
||||
|
||||
@doc "Sends cursor home"
|
||||
defsequence :home, "", "H"
|
||||
|
||||
@doc "Clears screen"
|
||||
defsequence :clear, "2", "J"
|
||||
|
||||
@doc "Clears line"
|
||||
defsequence :clear_line, "2", "K"
|
||||
|
||||
defp format_sequence(other) do
|
||||
raise ArgumentError, "invalid ANSI sequence specification: #{inspect other}"
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Formats a chardata-like argument by converting named ANSI sequences into actual
|
||||
ANSI codes.
|
||||
|
||||
The named sequences are represented by atoms.
|
||||
|
||||
It will also append an `IO.ANSI.reset/0` to the chardata when a conversion is
|
||||
performed. If you don't want this behaviour, use `format_fragment/2`.
|
||||
|
||||
An optional boolean parameter can be passed to enable or disable
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
|
||||
By default checks if ANSI is enabled using the `enabled?/0` function.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> IO.ANSI.format(["Hello, ", :red, :bright, "world!"], true)
|
||||
[[[[[[], "Hello, "] | "\e[31m"] | "\e[1m"], "world!"] | "\e[0m"]
|
||||
|
||||
"""
|
||||
def format(chardata, emit \\ enabled?) when is_boolean(emit) do
|
||||
do_format(chardata, [], [], emit, :maybe)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Formats a chardata-like argument by converting named ANSI sequences into actual
|
||||
ANSI codes.
|
||||
|
||||
The named sequences are represented by atoms.
|
||||
|
||||
An optional boolean parameter can be passed to enable or disable
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
|
||||
By default checks if ANSI is enabled using the `enabled?/0` function.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> IO.ANSI.format_fragment([:bright, 'Word'], true)
|
||||
[[[[[[] | "\e[1m"], 87], 111], 114], 100]
|
||||
|
||||
"""
|
||||
def format_fragment(chardata, emit \\ enabled?) when is_boolean(emit) do
|
||||
do_format(chardata, [], [], emit, false)
|
||||
end
|
||||
|
||||
defp do_format([term | rest], rem, acc, emit, append_reset) do
|
||||
do_format(term, [rest | rem], acc, emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format(term, rem, acc, true, append_reset) when is_atom(term) do
|
||||
do_format([], rem, [acc | format_sequence(term)], true, !!append_reset)
|
||||
end
|
||||
|
||||
defp do_format(term, rem, acc, false, append_reset) when is_atom(term) do
|
||||
do_format([], rem, acc, false, append_reset)
|
||||
end
|
||||
|
||||
defp do_format(term, rem, acc, emit, append_reset) when not is_list(term) do
|
||||
do_format([], rem, [acc | [term]], emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format([], [next | rest], acc, emit, append_reset) do
|
||||
do_format(next, rest, acc, emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format([], [], acc, true, true) do
|
||||
[acc | IO.ANSI.reset]
|
||||
end
|
||||
|
||||
defp do_format([], [], acc, _emit, _append_reset) do
|
||||
acc
|
||||
end
|
||||
end
|
||||
@@ -1,560 +0,0 @@
|
||||
defmodule IO.ANSI.Docs do
|
||||
@moduledoc false
|
||||
|
||||
@bullets [?*, ?-, ?+]
|
||||
|
||||
@doc """
|
||||
The default options used by this module.
|
||||
|
||||
The supported values are:
|
||||
|
||||
* `:enabled` - toggles coloring on and off (true)
|
||||
* `:doc_bold` - bold text (bright)
|
||||
* `:doc_code` - code blocks (cyan, bright)
|
||||
* `:doc_headings` - h1 and h2 headings (yellow, bright)
|
||||
* `:doc_inline_code` - inline code (cyan)
|
||||
* `:doc_table_heading` - style for table headings
|
||||
* `:doc_title` - top level heading (reverse, yellow, bright)
|
||||
* `:doc_underline` - underlined text (underline)
|
||||
* `:width` - the width to format the text (80)
|
||||
|
||||
Values for the color settings are strings with
|
||||
comma-separated ANSI values.
|
||||
"""
|
||||
def default_options do
|
||||
[enabled: true,
|
||||
doc_bold: [:bright],
|
||||
doc_code: [:cyan, :bright],
|
||||
doc_headings: [:yellow],
|
||||
doc_inline_code: [:cyan],
|
||||
doc_table_heading: [:reverse],
|
||||
doc_title: [:reverse, :yellow],
|
||||
doc_underline: [:underline],
|
||||
width: 80]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prints the head of the documentation (i.e. the function signature).
|
||||
|
||||
See `default_options/0` for docs on the supported options.
|
||||
"""
|
||||
def print_heading(heading, options \\ []) do
|
||||
IO.puts IO.ANSI.reset
|
||||
options = Keyword.merge(default_options, options)
|
||||
width = options[:width]
|
||||
padding = div(width + String.length(heading), 2)
|
||||
heading = heading |> String.rjust(padding) |> String.ljust(width)
|
||||
write(:doc_title, heading, options)
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prints the documentation body.
|
||||
|
||||
In addition to the printing string, takes a set of options
|
||||
defined in `default_options/1`.
|
||||
"""
|
||||
def print(doc, options \\ []) do
|
||||
options = Keyword.merge(default_options, options)
|
||||
doc
|
||||
|> String.split(["\r\n", "\n"], trim: false)
|
||||
|> Enum.map(&String.rstrip/1)
|
||||
|> process([], "", options)
|
||||
end
|
||||
|
||||
defp process([], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
end
|
||||
|
||||
defp process(["# " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write_h1(String.strip(heading), options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
|
||||
defp process(["## " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write_h2(String.strip(heading), options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
|
||||
defp process(["### " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write_h3(String.strip(heading), indent, options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
|
||||
defp process(["" | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
defp process([" " <> line | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
process_code(rest, [line], indent, options)
|
||||
end
|
||||
|
||||
defp process(["```" <> _line | rest], text, indent, options) do
|
||||
process_fenced_code_block(rest, text, indent, options, _delimiter = "```")
|
||||
end
|
||||
|
||||
defp process(["~~~" <> _line | rest], text, indent, options) do
|
||||
process_fenced_code_block(rest, text, indent, options, _delimiter = "~~~")
|
||||
end
|
||||
|
||||
defp process(all=[line | rest], text, indent, options) do
|
||||
{stripped, count} = strip_spaces(line, 0, :infinity)
|
||||
cond do
|
||||
link_label?(stripped, count) ->
|
||||
write_text([line], indent, options, true)
|
||||
process(rest, text, indent, options)
|
||||
table_line?(stripped) and rest != [] and table_line?(hd(rest)) ->
|
||||
write_text(text, indent, options)
|
||||
process_table(all, indent, options)
|
||||
true ->
|
||||
process_rest(stripped, rest, count, text, indent, options)
|
||||
end
|
||||
end
|
||||
|
||||
## Headings
|
||||
|
||||
defp write_h1(heading, options) do
|
||||
write_h2(String.upcase(heading), options)
|
||||
end
|
||||
|
||||
defp write_h2(heading, options) do
|
||||
write(:doc_headings, heading, options)
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
defp write_h3(heading, indent, options) do
|
||||
IO.write(indent)
|
||||
write(:doc_headings, heading, options)
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
## Lists
|
||||
|
||||
defp process_rest(stripped, rest, count, text, indent, options) do
|
||||
case stripped do
|
||||
<<bullet, ?\s, item::binary>> when bullet in @bullets ->
|
||||
write_text(text, indent, options)
|
||||
process_list("• ", item, rest, count, indent, options)
|
||||
<<d1, ?., ?\s, item::binary>> when d1 in ?0..?9 ->
|
||||
write_text(text, indent, options)
|
||||
process_list(<<d1, ?., ?\s>>, item, rest, count, indent, options)
|
||||
<<d1, d2, ?., ?\s, item::binary>> when d1 in ?0..?9 and d2 in ?0..?9 ->
|
||||
write_text(text, indent, options)
|
||||
process_list(<<d1, d2, ?., ?\s>>, item, rest, count, indent, options)
|
||||
_ ->
|
||||
process(rest, [stripped | text], indent, options)
|
||||
end
|
||||
end
|
||||
|
||||
defp process_list(entry, line, rest, count, indent, options) do
|
||||
# The first list always win some extra padding
|
||||
if indent == "", do: entry = " " <> entry
|
||||
new_indent = indent <> String.duplicate(" ", String.length(entry))
|
||||
|
||||
{contents, rest, done} = process_list_next(rest, count, byte_size(new_indent), [])
|
||||
process(contents, [indent <> entry <> line, :no_wrap], new_indent, options)
|
||||
|
||||
if done, do: newline_after_block()
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
defp process_list_next([line | rest], count, max, acc) do
|
||||
{stripped, next_count} = strip_spaces(line, 0, max)
|
||||
case process_list_next_kind(stripped, rest, count, next_count) do
|
||||
:next -> process_list_next(rest, count, max, [stripped | acc])
|
||||
:done -> {Enum.reverse(acc), [line | rest], true}
|
||||
:list -> {Enum.reverse(acc), [line | rest], false}
|
||||
end
|
||||
end
|
||||
|
||||
defp process_list_next([], _count, _max, acc) do
|
||||
{Enum.reverse(acc), [], true}
|
||||
end
|
||||
|
||||
defp process_list_next_kind(stripped, rest, count, next_count) do
|
||||
case {stripped, rest} do
|
||||
{<<bullet, ?\s, _::binary>>, _} when bullet in @bullets and next_count <= count ->
|
||||
:list
|
||||
{<<d1, ?., ?\s, _::binary>>, _} when d1 in ?0..?9 and next_count <= count ->
|
||||
:list
|
||||
{<<d1, d2, ?., ?\s, _::binary>>, _} when d1 in ?0..?9 and d2 in ?0..?9 and next_count <= count ->
|
||||
:list
|
||||
{"", [" " <> _ | _]} ->
|
||||
:next
|
||||
{"", _} ->
|
||||
:done
|
||||
_ ->
|
||||
:next
|
||||
end
|
||||
end
|
||||
|
||||
## Text
|
||||
|
||||
defp write_text(text, indent, options) do
|
||||
case Enum.reverse(text) do
|
||||
[:no_wrap|rest] -> write_text(rest, indent, options, true)
|
||||
rest -> write_text(rest, indent, options, false)
|
||||
end
|
||||
end
|
||||
|
||||
defp write_text([], _indent, _options, _no_wrap) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp write_text(lines, indent, options, no_wrap) do
|
||||
lines
|
||||
|> Enum.join(" ")
|
||||
|> handle_links
|
||||
|> handle_inline(options)
|
||||
|> String.split(~r{\s})
|
||||
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap)
|
||||
|
||||
unless no_wrap, do: newline_after_block()
|
||||
end
|
||||
|
||||
## Code blocks
|
||||
|
||||
defp process_code([], code, indent, options) do
|
||||
write_code(code, indent, options)
|
||||
end
|
||||
|
||||
# Blank line between code blocks
|
||||
defp process_code(["", " " <> line | rest], code, indent, options) do
|
||||
process_code(rest, [line, "" | code], indent, options)
|
||||
end
|
||||
|
||||
defp process_code([" " <> line | rest], code, indent, options) do
|
||||
process_code(rest, [line|code], indent, options)
|
||||
end
|
||||
|
||||
defp process_code(rest, code, indent, options) do
|
||||
write_code(code, indent, options)
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
defp process_fenced_code_block(rest, text, indent, options, delimiter) do
|
||||
write_text(text, indent, options)
|
||||
process_fenced_code(rest, [], indent, options, delimiter)
|
||||
end
|
||||
|
||||
defp process_fenced_code([], code, indent, options, _delimiter) do
|
||||
write_code(code, indent, options)
|
||||
end
|
||||
|
||||
defp process_fenced_code([line | rest], code, indent, options, delimiter) do
|
||||
if line === delimiter do
|
||||
process_code(rest, code, indent, options)
|
||||
else
|
||||
process_fenced_code(rest, [line|code], indent, options, delimiter)
|
||||
end
|
||||
end
|
||||
|
||||
defp write_code(code, indent, options) do
|
||||
write(:doc_code, "#{indent}┃ #{Enum.join(Enum.reverse(code), "\n#{indent}┃ ")}", options)
|
||||
newline_after_block
|
||||
end
|
||||
|
||||
## Tables
|
||||
|
||||
defp process_table(lines, indent, options) do
|
||||
{table, rest} = Enum.split_while(lines, &table_line?/1)
|
||||
table_lines(table, options)
|
||||
newline_after_block
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
defp table_lines(lines, options) do
|
||||
lines = Enum.map(lines, &split_into_columns(&1, options))
|
||||
count = Enum.map(lines, &length/1) |> Enum.max
|
||||
lines = Enum.map(lines, &pad_to_number_of_columns(&1, count))
|
||||
|
||||
widths = for line <- lines, do:
|
||||
(for {_col, length} <- line, do: length)
|
||||
|
||||
col_widths = Enum.reduce(widths,
|
||||
List.duplicate(0, count),
|
||||
&max_column_widths/2)
|
||||
|
||||
render_table(lines, col_widths, options)
|
||||
end
|
||||
|
||||
defp split_into_columns(line, options) do
|
||||
line
|
||||
|> String.strip(?|)
|
||||
|> String.strip()
|
||||
|> String.split(~r/\s\|\s/)
|
||||
|> Enum.map(&render_column(&1, options))
|
||||
end
|
||||
|
||||
defp render_column(col, options) do
|
||||
col = col
|
||||
|> String.replace(~r/\\ \|/x, "|")
|
||||
|> handle_links
|
||||
|> handle_inline(options)
|
||||
{col, length_without_escape(col, 0)}
|
||||
end
|
||||
|
||||
defp pad_to_number_of_columns(cols, col_count),
|
||||
do: cols ++ List.duplicate({"", 0}, col_count - length(cols))
|
||||
|
||||
defp max_column_widths(cols, widths),
|
||||
do: Enum.zip(cols, widths) |> Enum.map(fn {a, b} -> max(a, b) end)
|
||||
|
||||
# If second line is heading separator, use the heading style on the first
|
||||
defp render_table([first, second | rest], widths, options) do
|
||||
combined = Enum.zip(first, widths)
|
||||
if table_header?(second) do
|
||||
draw_table_row(combined, options, :heading)
|
||||
render_table(rest, widths, options)
|
||||
else
|
||||
draw_table_row(combined, options)
|
||||
render_table([second | rest], widths, options)
|
||||
end
|
||||
end
|
||||
|
||||
defp render_table([first | rest], widths, options) do
|
||||
combined = Enum.zip(first, widths)
|
||||
draw_table_row(combined, options)
|
||||
render_table(rest, widths, options)
|
||||
end
|
||||
|
||||
defp render_table([], _, _),
|
||||
do: nil
|
||||
|
||||
defp table_header?(row), do:
|
||||
Enum.all?(row, fn {col, _} -> col =~ ~r/^:?-+:?$/ end)
|
||||
|
||||
defp draw_table_row(cols_and_widths, options, heading \\ false) do
|
||||
columns =
|
||||
Enum.map_join(cols_and_widths, " | ", fn {{col, length}, width} ->
|
||||
col <> String.duplicate(" ", width - length)
|
||||
end)
|
||||
|
||||
if heading do
|
||||
write(:doc_table_heading, columns, options)
|
||||
else
|
||||
IO.puts columns
|
||||
end
|
||||
end
|
||||
|
||||
defp table_line?(line) do
|
||||
Regex.match?(~r'''
|
||||
( ^ \s{0,3} \| (?: [^|]+ \|)+ \s* $ )
|
||||
|
|
||||
(\s \| \s)
|
||||
'''x, line)
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp link_label?("[" <> rest, count) when count <= 3, do: link_label?(rest)
|
||||
defp link_label?(_, _), do: false
|
||||
|
||||
defp link_label?("]: " <> _), do: true
|
||||
defp link_label?("]" <> _), do: false
|
||||
defp link_label?(""), do: false
|
||||
defp link_label?(<<_>> <> rest), do: link_label?(rest)
|
||||
|
||||
defp strip_spaces(" " <> line, acc, max) when acc < max,
|
||||
do: strip_spaces(line, acc + 1, max)
|
||||
defp strip_spaces(rest, acc, _max),
|
||||
do: {rest, acc}
|
||||
|
||||
defp write(style, string, options) do
|
||||
IO.puts [color(style, options), string, IO.ANSI.reset]
|
||||
end
|
||||
|
||||
defp write_with_wrap([], _available, _indent, _first) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp write_with_wrap(words, available, indent, first) do
|
||||
{words, rest} = take_words(words, available, [])
|
||||
IO.puts (if first, do: "", else: indent) <> Enum.join(words, " ")
|
||||
write_with_wrap(rest, available, indent, false)
|
||||
end
|
||||
|
||||
defp take_words([word|words], available, acc) do
|
||||
available = available - length_without_escape(word, 0)
|
||||
|
||||
cond do
|
||||
# It fits, take one for space and continue decreasing
|
||||
available > 0 ->
|
||||
take_words(words, available - 1, [word|acc])
|
||||
|
||||
# No space but we got no words
|
||||
acc == [] ->
|
||||
{[word], words}
|
||||
|
||||
# Otherwise
|
||||
true ->
|
||||
{Enum.reverse(acc), [word|words]}
|
||||
end
|
||||
end
|
||||
|
||||
defp take_words([], _available, acc) do
|
||||
{Enum.reverse(acc), []}
|
||||
end
|
||||
|
||||
defp length_without_escape(<<?\e, ?[, _, _, ?m>> <> rest, count) do
|
||||
length_without_escape(rest, count)
|
||||
end
|
||||
|
||||
defp length_without_escape(<<?\e, ?[, _, ?m>> <> rest, count) do
|
||||
length_without_escape(rest, count)
|
||||
end
|
||||
|
||||
defp length_without_escape(rest, count) do
|
||||
case String.next_grapheme(rest) do
|
||||
{_, rest} -> length_without_escape(rest, count + 1)
|
||||
nil -> count
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_links(text) do
|
||||
text
|
||||
|> remove_square_brackets_in_link
|
||||
|> escape_underlines_in_link
|
||||
end
|
||||
|
||||
defp escape_underlines_in_link(text) do
|
||||
Regex.replace(~r{https?\S*}, text, &String.replace(&1, "_", "\\_"))
|
||||
end
|
||||
|
||||
defp remove_square_brackets_in_link(text) do
|
||||
Regex.replace(~r{\[(.*?)\]\((.*?)\)}, text, "\\1 (\\2)")
|
||||
end
|
||||
|
||||
|
||||
# We have four entries: **, *, _ and `.
|
||||
#
|
||||
# The first three behave the same while the last one is simpler
|
||||
# when it comes to delimiters. But, since the first has two
|
||||
# characters, we need to handle 3 cases:
|
||||
#
|
||||
# 1. **
|
||||
# 2. _ and *
|
||||
# 3. `
|
||||
#
|
||||
# Where the first two should have the same code but match differently.
|
||||
@single [?_, ?*]
|
||||
|
||||
# Characters that can mark the beginning or the end of a word.
|
||||
# Only support the most common ones at this moment.
|
||||
@delimiters [?\s, ?', ?", ?!, ?@, ?#, ?$, ?%, ?^, ?&, ?-, ?+, ?(, ?), ?[, ?], ?{, ?}, ?<, ?>, ?.]
|
||||
|
||||
# Inline start
|
||||
|
||||
defp handle_inline(<<?*, ?*, rest::binary>>, options) do
|
||||
handle_inline(rest, ?d, ["**"], [], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, rest::binary>>, options) when mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [], options)
|
||||
end
|
||||
|
||||
defp handle_inline(rest, options) do
|
||||
handle_inline(rest, nil, [], [], options)
|
||||
end
|
||||
|
||||
# Inline delimiters
|
||||
|
||||
defp handle_inline(<<delimiter, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters do
|
||||
handle_inline(rest, ?d, ["**"], [delimiter, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<delimiter, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters and mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [delimiter, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?`, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?`, ["`"], [Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
# Clauses for handling escape
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [?\\, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, rest::binary>>, limit, buffer, acc, options) do
|
||||
handle_inline(rest, limit, [?\\|buffer], acc, options)
|
||||
end
|
||||
|
||||
# An escape is not valid inside `
|
||||
defp handle_inline(<<?\\, mark, rest::binary>>, limit, buffer, acc, options)
|
||||
when not(mark == limit and mark == ?`) do
|
||||
handle_inline(rest, limit, [mark|buffer], acc, options)
|
||||
end
|
||||
|
||||
# Inline end
|
||||
|
||||
defp handle_inline(<<?*, ?*, delimiter, rest::binary>>, ?d, buffer, acc, options)
|
||||
when delimiter in @delimiters do
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, delimiter, rest::binary>>, mark, buffer, acc, options)
|
||||
when delimiter in @delimiters and mark in @single do
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?*, ?*, rest::binary>>, ?d, buffer, acc, options)
|
||||
when rest == "" do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, rest::binary>>, mark, buffer, acc, options)
|
||||
when rest == "" and mark in @single do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?`, rest::binary>>, ?`, buffer, acc, options) do
|
||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
# Catch all
|
||||
|
||||
defp handle_inline(<<char, rest::binary>>, mark, buffer, acc, options) do
|
||||
handle_inline(rest, mark, [char|buffer], acc, options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<>>, _mark, buffer, acc, _options) do
|
||||
IO.iodata_to_binary Enum.reverse([Enum.reverse(buffer)|acc])
|
||||
end
|
||||
|
||||
defp inline_buffer(buffer, options) do
|
||||
[h|t] = Enum.reverse([IO.ANSI.reset|buffer])
|
||||
[color_for(h, options)|t]
|
||||
end
|
||||
|
||||
defp color_for(mark, colors) do
|
||||
case mark do
|
||||
"`" -> color(:doc_inline_code, colors)
|
||||
"_" -> color(:doc_underline, colors)
|
||||
"*" -> color(:doc_bold, colors)
|
||||
"**" -> color(:doc_bold, colors)
|
||||
end
|
||||
end
|
||||
|
||||
defp color(style, colors) do
|
||||
color = colors[style]
|
||||
IO.ANSI.format_fragment(color, colors[:enabled])
|
||||
end
|
||||
|
||||
defp newline_after_block, do: IO.puts(IO.ANSI.reset)
|
||||
end
|
||||
@@ -1,67 +0,0 @@
|
||||
defmodule IO.StreamError do
|
||||
defexception [:reason, :message]
|
||||
|
||||
def exception(opts) do
|
||||
reason = opts[:reason]
|
||||
formatted = IO.iodata_to_binary(:file.format_error(reason))
|
||||
%IO.StreamError{message: "error during streaming: #{formatted}", reason: reason}
|
||||
end
|
||||
end
|
||||
|
||||
defmodule IO.Stream do
|
||||
@moduledoc """
|
||||
Defines an `IO.Stream` struct returned by `IO.stream/2` and `IO.binstream/2`.
|
||||
|
||||
The following fields are public:
|
||||
|
||||
* `device` - the IO device
|
||||
* `raw` - a boolean indicating if bin functions should be used
|
||||
* `line_or_bytes` - if reading should read lines or a given amount of bytes
|
||||
|
||||
"""
|
||||
|
||||
defstruct device: nil, raw: true, line_or_bytes: :line
|
||||
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
@doc false
|
||||
def __build__(device, raw, line_or_bytes) do
|
||||
%IO.Stream{device: device, raw: raw, line_or_bytes: line_or_bytes}
|
||||
end
|
||||
|
||||
defimpl Collectable do
|
||||
def into(%{device: device, raw: raw} = stream) do
|
||||
{:ok, into(stream, device, raw)}
|
||||
end
|
||||
|
||||
defp into(stream, device, raw) do
|
||||
fn
|
||||
:ok, {:cont, x} ->
|
||||
case raw do
|
||||
true -> IO.binwrite(device, x)
|
||||
false -> IO.write(device, x)
|
||||
end
|
||||
:ok, _ -> stream
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Enumerable do
|
||||
def reduce(%{device: device, raw: raw, line_or_bytes: line_or_bytes}, acc, fun) do
|
||||
next_fun =
|
||||
case raw do
|
||||
true -> &IO.each_binstream(&1, line_or_bytes)
|
||||
false -> &IO.each_stream(&1, line_or_bytes)
|
||||
end
|
||||
Stream.resource(fn -> device end, next_fun, &(&1)).(acc, fun)
|
||||
end
|
||||
|
||||
def count(_stream) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
|
||||
def member?(_stream, _term) do
|
||||
{:error, __MODULE__}
|
||||
end
|
||||
end
|
||||
end
|
||||
+2187
-3402
File diff suppressed because it is too large
Load Diff
+157
-382
@@ -1,455 +1,230 @@
|
||||
defrecord Kernel.CLI.Config, commands: [], output: ".",
|
||||
compile: [], halt: true, compiler_options: []
|
||||
|
||||
defmodule Kernel.CLI do
|
||||
@moduledoc false
|
||||
|
||||
@blank_config %{commands: [], output: ".", compile: [],
|
||||
halt: true, compiler_options: [], errors: [],
|
||||
pa: [], pz: [], verbose_compile: false}
|
||||
import Exception, only: [format_stacktrace: 1]
|
||||
|
||||
@doc """
|
||||
This is the API invoked by Elixir boot process.
|
||||
"""
|
||||
def main(argv) do
|
||||
argv = for arg <- argv, do: IO.chardata_to_string(arg)
|
||||
# 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)
|
||||
|
||||
{config, argv} = parse_argv(argv)
|
||||
System.argv(argv)
|
||||
argv = lc arg inlist argv, do: list_to_binary(arg)
|
||||
Erlang.gen_server.call(:elixir_code_server, { :argv, argv })
|
||||
|
||||
run fn _ ->
|
||||
errors = process_commands(config)
|
||||
all_commands = List.reverse(config.commands)
|
||||
|
||||
if errors != [] do
|
||||
Enum.each(errors, &IO.puts(:stderr, &1))
|
||||
System.halt(1)
|
||||
try do
|
||||
Enum.map all_commands, process_command(&1, config)
|
||||
if config.halt do
|
||||
at_exit(0)
|
||||
halt(0)
|
||||
end
|
||||
end, config.halt
|
||||
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
|
||||
{ok_or_shutdown, status} = exec_fun(fun, {:ok, 0})
|
||||
if ok_or_shutdown == :shutdown or halt do
|
||||
{_, status} = at_exit({ok_or_shutdown, status})
|
||||
|
||||
# Ensure Logger messages are flushed before halting
|
||||
case :erlang.whereis(Logger) do
|
||||
pid when is_pid(pid) -> Logger.flush()
|
||||
_ -> :ok
|
||||
end
|
||||
|
||||
System.halt(status)
|
||||
rescue
|
||||
exception ->
|
||||
at_exit(1)
|
||||
stacktrace = System.stacktrace
|
||||
IO.puts :stderr, "** (#{inspect exception.__record__(:name)}) #{exception.message}"
|
||||
print_stacktrace(stacktrace)
|
||||
halt(1)
|
||||
catch
|
||||
:exit, reason when is_integer(reason) ->
|
||||
at_exit(reason)
|
||||
halt(reason)
|
||||
:exit, :normal ->
|
||||
at_exit(0)
|
||||
halt(0)
|
||||
kind, reason ->
|
||||
at_exit(1)
|
||||
stacktrace = System.stacktrace
|
||||
IO.puts :stderr, "** (#{kind}) #{inspect(reason)}"
|
||||
print_stacktrace(stacktrace)
|
||||
halt(1)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def parse_argv(argv) do
|
||||
parse_argv(argv, @blank_config)
|
||||
end
|
||||
## Private
|
||||
|
||||
@doc false
|
||||
def process_commands(config) do
|
||||
results = Enum.map(Enum.reverse(config.commands), &process_command(&1, config))
|
||||
errors = for {:error, msg} <- results, do: msg
|
||||
Enum.reverse(config.errors, errors)
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp at_exit(res) do
|
||||
hooks = :elixir_config.get_and_put(:at_exit, [])
|
||||
res = Enum.reduce(hooks, res, &exec_fun/2)
|
||||
if hooks == [], do: res, else: at_exit(res)
|
||||
end
|
||||
|
||||
defp exec_fun(fun, res) when is_function(fun, 1) and is_tuple(res) do
|
||||
parent = self()
|
||||
|
||||
{pid, ref} =
|
||||
spawn_monitor(fn ->
|
||||
try do
|
||||
fun.(elem(res, 1))
|
||||
catch
|
||||
:exit, {:shutdown, int} when is_integer(int) ->
|
||||
send parent, {self, {:shutdown, int}}
|
||||
exit({:shutdown, int})
|
||||
:exit, reason
|
||||
when reason == :normal
|
||||
when reason == :shutdown
|
||||
when tuple_size(reason) == 2 and elem(reason, 0) == :shutdown ->
|
||||
send parent, {self, {:shutdown, 0}}
|
||||
exit(reason)
|
||||
kind, reason ->
|
||||
stack = System.stacktrace
|
||||
print_error(kind, reason, stack)
|
||||
send parent, {self, {:shutdown, 1}}
|
||||
exit(to_exit(kind, reason, stack))
|
||||
else
|
||||
_ ->
|
||||
send parent, {self, res}
|
||||
end
|
||||
end)
|
||||
|
||||
receive do
|
||||
{^pid, res} ->
|
||||
:erlang.demonitor(ref, [:flush])
|
||||
res
|
||||
{:DOWN, ^ref, _, _, other} ->
|
||||
print_error({:EXIT, pid}, other, [])
|
||||
{:shutdown, 1}
|
||||
defp at_exit(status) do
|
||||
hooks = Erlang.gen_server.call(:elixir_code_server, :at_exit)
|
||||
lc hook inlist hooks do
|
||||
try do
|
||||
hook.(status)
|
||||
rescue
|
||||
exception ->
|
||||
IO.puts :stderr, "** (#{inspect exception.__record__(:name)}) #{exception.message}"
|
||||
print_stacktrace(System.stacktrace)
|
||||
catch
|
||||
kind, reason ->
|
||||
IO.puts :stderr, "** #{kind} #{inspect(reason)}"
|
||||
print_stacktrace(System.stacktrace)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp to_exit(:throw, reason, stack), do: {{:nocatch, reason}, stack}
|
||||
defp to_exit(:error, reason, stack), do: {reason, stack}
|
||||
defp to_exit(:exit, reason, _stack), do: reason
|
||||
defp invalid_option(option) do
|
||||
IO.puts(:stderr, "Unknown option #{list_to_binary(option)}")
|
||||
halt(1)
|
||||
end
|
||||
|
||||
defp shared_option?(list, config, callback) do
|
||||
case parse_shared(list, config) do
|
||||
{[h|hs], _} when h == hd(list) ->
|
||||
new_config = %{config | errors: ["#{h} : Unknown option" | config.errors]}
|
||||
callback.(hs, new_config)
|
||||
{new_list, new_config} ->
|
||||
case process_shared(list, config) do
|
||||
{ [h|t], _ } when h == hd(list) ->
|
||||
invalid_option h
|
||||
{ new_list, new_config } ->
|
||||
callback.(new_list, new_config)
|
||||
end
|
||||
end
|
||||
|
||||
defp print_error(kind, reason, trace) do
|
||||
IO.puts :stderr, Exception.format(kind, reason, prune_stacktrace(trace))
|
||||
defp print_stacktrace(stacktrace) do
|
||||
Enum.each stacktrace, fn s -> IO.puts :stderr, " #{format_stacktrace(s)}" end
|
||||
end
|
||||
|
||||
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_clauses,
|
||||
:elixir_translator, :elixir_expand, :elixir_lexical, :elixir_exp_clauses,
|
||||
:elixir_def]
|
||||
# Process shared options
|
||||
|
||||
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
||||
prune_stacktrace(t)
|
||||
defp process_shared(['-v'|t], config) do
|
||||
IO.puts "Elixir #{System.version}"
|
||||
process_shared t, config
|
||||
end
|
||||
|
||||
defp prune_stacktrace([{__MODULE__, :wrapper, 1, _}|_]) do
|
||||
[]
|
||||
defp process_shared(['--no-halt'|t], config) do
|
||||
process_shared t, config.halt(false)
|
||||
end
|
||||
|
||||
defp prune_stacktrace([h|t]) do
|
||||
[h|prune_stacktrace(t)]
|
||||
defp process_shared(['-e',h|t], config) do
|
||||
process_shared t, config.prepend_commands [eval: h]
|
||||
end
|
||||
|
||||
defp prune_stacktrace([]) do
|
||||
[]
|
||||
defp process_shared(['-pa',h|t], config) do
|
||||
Enum.each File.wildcard(File.expand_path(h)), Code.prepend_path(&1)
|
||||
process_shared t, config
|
||||
end
|
||||
|
||||
# Parse shared options
|
||||
defp process_shared(['-pz',h|t], config) do
|
||||
Enum.each File.wildcard(File.expand_path(h)), Code.append_path(&1)
|
||||
process_shared t, config
|
||||
end
|
||||
|
||||
defp parse_shared([opt|_t], _config) when opt in ["-v", "--version"] do
|
||||
if function_exported?(IEx, :started?, 0) and IEx.started? do
|
||||
IO.puts "IEx #{System.version}"
|
||||
else
|
||||
IO.puts :erlang.system_info(:system_version)
|
||||
{:ok, v} = Version.parse(System.version)
|
||||
|
||||
case v.pre do
|
||||
[] -> IO.puts "Elixir #{System.version}"
|
||||
_ -> IO.puts "Elixir #{System.version} (#{System.build_info().revision})"
|
||||
end
|
||||
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]
|
||||
end
|
||||
|
||||
System.halt 0
|
||||
process_shared t, config
|
||||
end
|
||||
|
||||
defp parse_shared(["-pa", h|t], config) do
|
||||
paths = expand_code_path(h)
|
||||
Enum.each(paths, &:code.add_patha/1)
|
||||
parse_shared t, %{config | pa: config.pa ++ paths}
|
||||
defp process_shared(['-pr',h|t], config) do
|
||||
h = list_to_binary(h)
|
||||
process_shared t, config.prepend_commands [parallel_require: h]
|
||||
end
|
||||
|
||||
defp parse_shared(["-pz", h|t], config) do
|
||||
paths = expand_code_path(h)
|
||||
Enum.each(paths, &:code.add_pathz/1)
|
||||
parse_shared t, %{config | pz: config.pz ++ paths}
|
||||
defp process_shared([erl,_|t], config) when erl in ['--erl', '--sname', '--remsh', '--name'] do
|
||||
process_shared t, config
|
||||
end
|
||||
|
||||
defp parse_shared(["--app", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:app, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["--no-halt"|t], config) do
|
||||
parse_shared t, %{config | halt: false}
|
||||
end
|
||||
|
||||
defp parse_shared(["-e", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:eval, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["-r", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:require, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["-pr", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:parallel_require, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared([erl, _|t], config) when erl in ["--erl", "--sname", "--name", "--cookie"] do
|
||||
parse_shared t, config
|
||||
end
|
||||
|
||||
defp parse_shared([erl|t], config) when erl in ["--detached", "--hidden", "--werl"] do
|
||||
parse_shared t, config
|
||||
end
|
||||
|
||||
defp parse_shared(list, config) do
|
||||
{list, config}
|
||||
end
|
||||
|
||||
defp expand_code_path(path) do
|
||||
path = Path.expand(path)
|
||||
case Path.wildcard(path) do
|
||||
[] -> [to_char_list(path)]
|
||||
list -> Enum.map(list, &to_char_list/1)
|
||||
end
|
||||
defp process_shared(list, config) do
|
||||
{ list, config }
|
||||
end
|
||||
|
||||
# Process init options
|
||||
|
||||
defp parse_argv(["--"|t], config) do
|
||||
{config, t}
|
||||
def process_options(['--'|t], config) do
|
||||
{ config, t }
|
||||
end
|
||||
|
||||
defp parse_argv(["+elixirc"|t], config) do
|
||||
parse_compiler t, config
|
||||
def process_options(['--compile'|t], config) do
|
||||
process_compiler t, config
|
||||
end
|
||||
|
||||
defp parse_argv(["+iex"|t], config) do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_argv(["-S", h|t], config) do
|
||||
{%{config | commands: [{:script, h} | config.commands]}, t}
|
||||
end
|
||||
|
||||
defp parse_argv([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_argv(&1, &2)
|
||||
_ ->
|
||||
if Keyword.has_key?(config.commands, :eval) do
|
||||
{config, list}
|
||||
else
|
||||
{%{config | commands: [{:file, h} | config.commands]}, t}
|
||||
end
|
||||
def process_options(['-S',h|t], config) do
|
||||
exec = System.find_executable(h)
|
||||
if exec do
|
||||
{ config.prepend_commands([require: list_to_binary(exec)]), t }
|
||||
else
|
||||
IO.puts(:stderr, "Could not find executable #{h}")
|
||||
halt(1)
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_argv([], config) do
|
||||
{config, []}
|
||||
end
|
||||
|
||||
# Parse compiler options
|
||||
|
||||
defp parse_compiler(["--"|t], config) do
|
||||
{config, t}
|
||||
end
|
||||
|
||||
defp parse_compiler(["-o", h|t], config) do
|
||||
parse_compiler t, %{config | output: h}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--no-docs"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:docs, false} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--no-debug-info"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:debug_info, false} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--ignore-module-conflict"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:ignore_module_conflict, true} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--warnings-as-errors"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:warnings_as_errors, true} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--verbose"|t], config) do
|
||||
parse_compiler t, %{config | verbose_compile: true}
|
||||
end
|
||||
|
||||
defp parse_compiler([h|t] = list, config) do
|
||||
def process_options([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_compiler(&1, &2)
|
||||
'-' ++ _ ->
|
||||
shared_option? list, config, process_options(&1, &2)
|
||||
_ ->
|
||||
h = list_to_binary(h)
|
||||
{ config.prepend_commands([require: h]), t }
|
||||
end
|
||||
end
|
||||
|
||||
def process_options([], config) do
|
||||
{ config, [] }
|
||||
end
|
||||
|
||||
# Process compiler options
|
||||
|
||||
defp process_compiler(['--'|t], config) do
|
||||
{ config, t }
|
||||
end
|
||||
|
||||
defp process_compiler(['-o',h|t], config) do
|
||||
process_compiler t, config.output(list_to_binary(h))
|
||||
end
|
||||
|
||||
defp process_compiler(['--no-docs'|t], config) do
|
||||
process_compiler t, config.merge_compiler_options(docs: false)
|
||||
end
|
||||
|
||||
defp process_compiler(['--debug-info'|t], config) do
|
||||
process_compiler t, config.merge_compiler_options(debug_info: true)
|
||||
end
|
||||
|
||||
defp process_compiler(['--ignore-module-conflict'|t], config) do
|
||||
process_compiler t, config.merge_compiler_options(ignore_module_conflict: true)
|
||||
end
|
||||
|
||||
defp process_compiler([h|t] = list, config) do
|
||||
case h do
|
||||
'-' ++ _ ->
|
||||
shared_option? list, config, process_compiler(&1, &2)
|
||||
_ ->
|
||||
h = list_to_binary(h)
|
||||
pattern = if File.dir?(h), do: "#{h}/**/*.ex", else: h
|
||||
parse_compiler t, %{config | compile: [pattern | config.compile]}
|
||||
process_compiler t, config.prepend_compile [pattern]
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_compiler([], config) do
|
||||
{%{config | commands: [{:compile, config.compile}|config.commands]}, []}
|
||||
end
|
||||
|
||||
# Parse iex options
|
||||
|
||||
defp parse_iex(["--"|t], config) do
|
||||
{config, t}
|
||||
end
|
||||
|
||||
# This clause is here so that Kernel.CLI does not
|
||||
# error out with "unknown option"
|
||||
defp parse_iex(["--dot-iex", _|t], config) do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_iex([opt, _|t], config) when opt in ["--remsh"] do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_iex(["-S", h|t], config) do
|
||||
{%{config | commands: [{:script, h} | config.commands]}, t}
|
||||
end
|
||||
|
||||
defp parse_iex([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_iex(&1, &2)
|
||||
_ ->
|
||||
{%{config | commands: [{:file, h} | config.commands]}, t}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_iex([], config) do
|
||||
{config, []}
|
||||
defp process_compiler([], config) do
|
||||
{ config.prepend_commands([compile: config.compile]), [] }
|
||||
end
|
||||
|
||||
# Process commands
|
||||
|
||||
defp process_command({:cookie, h}, _config) do
|
||||
if Node.alive? do
|
||||
wrapper fn -> Node.set_cookie(String.to_atom(h)) end
|
||||
else
|
||||
{:error, "--cookie : Cannot set cookie if the node is not alive (set --name or --sname)"}
|
||||
end
|
||||
defp process_command({:eval, expr}, _config) when is_list(expr) do
|
||||
Erlang.elixir.eval(expr, [])
|
||||
end
|
||||
|
||||
defp process_command({:eval, expr}, _config) when is_binary(expr) do
|
||||
wrapper fn -> Code.eval_string(expr, []) end
|
||||
end
|
||||
|
||||
defp process_command({:app, app}, _config) when is_binary(app) do
|
||||
case Application.ensure_all_started(String.to_atom(app)) do
|
||||
{:error, {app, reason}} ->
|
||||
{:error, "--app : Could not start application #{app}: " <>
|
||||
Application.format_error(reason)}
|
||||
{:ok, _} ->
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
defp process_command({:script, file}, _config) when is_binary(file) do
|
||||
if exec = find_elixir_executable(file) do
|
||||
wrapper fn -> Code.require_file(exec) end
|
||||
else
|
||||
{:error, "-S : Could not find executable #{file}"}
|
||||
end
|
||||
end
|
||||
|
||||
defp process_command({:file, file}, _config) when is_binary(file) do
|
||||
if File.regular?(file) do
|
||||
wrapper fn -> Code.require_file(file) end
|
||||
else
|
||||
{:error, "No file named #{file}"}
|
||||
end
|
||||
end
|
||||
|
||||
defp process_command({:require, pattern}, _config) when is_binary(pattern) do
|
||||
files = filter_patterns(pattern)
|
||||
|
||||
if files != [] do
|
||||
wrapper fn -> Enum.map files, &Code.require_file(&1) end
|
||||
else
|
||||
{:error, "-r : No files matched pattern #{pattern}"}
|
||||
end
|
||||
defp process_command({:require, file}, _config) when is_binary(file) do
|
||||
Code.require_file(file)
|
||||
end
|
||||
|
||||
defp process_command({:parallel_require, pattern}, _config) when is_binary(pattern) do
|
||||
files = filter_patterns(pattern)
|
||||
|
||||
if files != [] do
|
||||
wrapper fn -> Kernel.ParallelRequire.files(files) end
|
||||
else
|
||||
{:error, "-pr : No files matched pattern #{pattern}"}
|
||||
end
|
||||
files = File.wildcard(pattern)
|
||||
files = List.uniq(files)
|
||||
files = Enum.filter files, File.regular?(&1)
|
||||
Kernel.ParallelRequire.files(files)
|
||||
end
|
||||
|
||||
defp process_command({:compile, patterns}, config) do
|
||||
# If ensuring the dir returns an error no files will be found.
|
||||
_ = :filelib.ensure_dir(:filename.join(config.output, "."))
|
||||
File.mkdir_p(config.output)
|
||||
|
||||
case filter_multiple_patterns(patterns) do
|
||||
{:ok, []} ->
|
||||
{:error, "No files matched provided patterns"}
|
||||
{:ok, files} ->
|
||||
wrapper fn ->
|
||||
Code.compiler_options(config.compiler_options)
|
||||
Kernel.ParallelCompiler.files_to_path(files, config.output,
|
||||
each_file: fn file -> if config.verbose_compile do IO.puts "Compiled #{file}" end end)
|
||||
end
|
||||
{:missing, missing} ->
|
||||
{:error, "No files matched pattern(s) #{Enum.join(missing, ",")}"}
|
||||
end
|
||||
end
|
||||
files = Enum.map patterns, File.wildcard(&1)
|
||||
files = List.uniq(List.concat(files))
|
||||
files = Enum.filter files, File.regular?(&1)
|
||||
|
||||
defp filter_patterns(pattern) do
|
||||
pattern
|
||||
|> Path.wildcard
|
||||
|> :lists.usort
|
||||
|> Enum.filter(&File.regular?/1)
|
||||
end
|
||||
|
||||
defp filter_multiple_patterns(patterns) do
|
||||
matched_files = Enum.map patterns, fn(pattern) ->
|
||||
case filter_patterns(pattern) do
|
||||
[] -> {:missing, pattern}
|
||||
files -> {:ok, files}
|
||||
end
|
||||
end
|
||||
|
||||
files = Enum.filter_map matched_files,
|
||||
fn(match) -> elem(match, 0) == :ok end,
|
||||
&elem(&1, 1)
|
||||
|
||||
missing_patterns = Enum.filter_map matched_files,
|
||||
fn(match) -> elem(match, 0) == :missing end,
|
||||
&elem(&1, 1)
|
||||
|
||||
if missing_patterns == [] do
|
||||
{:ok, :lists.usort(Enum.concat(files))}
|
||||
else
|
||||
{:missing, :lists.usort(missing_patterns)}
|
||||
end
|
||||
end
|
||||
|
||||
defp wrapper(fun) do
|
||||
_ = fun.()
|
||||
:ok
|
||||
end
|
||||
|
||||
defp find_elixir_executable(file) do
|
||||
if exec = System.find_executable(file) do
|
||||
# If we are on Windows, the executable is going to be
|
||||
# a .bat file that must be in the same directory as
|
||||
# the actual Elixir executable.
|
||||
case :os.type() do
|
||||
{:win32, _} ->
|
||||
base = Path.rootname(exec)
|
||||
if File.regular?(base), do: base, else: exec
|
||||
_ ->
|
||||
exec
|
||||
end
|
||||
end
|
||||
Code.compiler_options(config.compiler_options)
|
||||
Kernel.ParallelCompiler.files_to_path(files, config.output,
|
||||
fn file -> IO.puts "Compiled #{file}" end)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,35 +5,22 @@ defmodule Kernel.ErrorHandler do
|
||||
|
||||
def undefined_function(module, fun, args) do
|
||||
ensure_loaded(module)
|
||||
:error_handler.undefined_function(module, fun, args)
|
||||
Erlang.error_handler.undefined_function(module, fun, args)
|
||||
end
|
||||
|
||||
def undefined_lambda(module, fun, args) do
|
||||
ensure_loaded(module)
|
||||
:error_handler.undefined_lambda(module, fun, args)
|
||||
end
|
||||
|
||||
def release() do
|
||||
# On release, no longer allow elixir_ensure_compiled
|
||||
# directives and revert to the original error handler.
|
||||
# Note we should not delete the elixir_compiler_pid though,
|
||||
# as we still want to send notifications to the compiler.
|
||||
:erlang.erase(:elixir_ensure_compiled)
|
||||
:erlang.process_flag(:error_handler, :error_handler)
|
||||
:ok
|
||||
Erlang.error_handler.undefined_lambda(module, fun, args)
|
||||
end
|
||||
|
||||
defp ensure_loaded(module) do
|
||||
case Code.ensure_loaded(module) do
|
||||
{:module, _} -> :ok
|
||||
{:error, _} ->
|
||||
parent = :erlang.get(:elixir_compiler_pid)
|
||||
ref = :erlang.make_ref
|
||||
send parent, {:waiting, :module, self(), ref, module}
|
||||
:erlang.garbage_collect(self)
|
||||
{ :module, _ } -> []
|
||||
{ :error, _ } ->
|
||||
parent = Process.get(:elixir_compiler_pid)
|
||||
parent <- { :waiting, self(), module }
|
||||
receive do
|
||||
{^ref, :ready} -> :ok
|
||||
{^ref, :release} -> release()
|
||||
{ :release, ^parent } -> :ok
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,178 +0,0 @@
|
||||
# This is an Elixir module responsible for tracking
|
||||
# the usage of aliases, imports and requires in the Elixir scope.
|
||||
#
|
||||
# The implementation simply stores dispatch information in an
|
||||
# ETS table and then consults this table once compilation is done.
|
||||
#
|
||||
# Note that since this is required for bootstrap, we can't use
|
||||
# any of the `GenServer.Behaviour` conveniences.
|
||||
defmodule Kernel.LexicalTracker do
|
||||
@moduledoc false
|
||||
@timeout 30_000
|
||||
@behaviour :gen_server
|
||||
|
||||
@doc """
|
||||
Returns all remotes linked to in this lexical scope.
|
||||
"""
|
||||
def remotes(arg) do
|
||||
:gen_server.call(to_pid(arg), :ets, @timeout)
|
||||
|> :ets.match({{:mode, :'$1'}, :'$2'})
|
||||
|> partition([], [])
|
||||
end
|
||||
|
||||
defp partition([[remote, :compile]|t], compile, runtime),
|
||||
do: partition(t, [remote|compile], runtime)
|
||||
defp partition([[remote, :runtime]|t], compile, runtime),
|
||||
do: partition(t, compile, [remote|runtime])
|
||||
defp partition([], compile, runtime),
|
||||
do: {compile, runtime}
|
||||
|
||||
@doc """
|
||||
Gets the destination the lexical scope is meant to
|
||||
compile to.
|
||||
"""
|
||||
def dest(arg) do
|
||||
:gen_server.call(to_pid(arg), :dest, @timeout)
|
||||
end
|
||||
|
||||
defp to_pid(pid) when is_pid(pid), do: pid
|
||||
defp to_pid(mod) when is_atom(mod) do
|
||||
table = :elixir_module.data_table(mod)
|
||||
[{_, val}] = :ets.lookup(table, {:elixir, :lexical_tracker})
|
||||
val
|
||||
end
|
||||
|
||||
# Internal API
|
||||
|
||||
# Starts the tracker and returns its pid.
|
||||
@doc false
|
||||
def start_link(dest) do
|
||||
:gen_server.start_link(__MODULE__, dest, [])
|
||||
end
|
||||
|
||||
@doc false
|
||||
def stop(pid) do
|
||||
:gen_server.cast(pid, :stop)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def add_import(pid, module, line, warn) do
|
||||
:gen_server.cast(pid, {:add_import, module, line, warn})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def add_alias(pid, module, line, warn) do
|
||||
:gen_server.cast(pid, {:add_alias, module, line, warn})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def remote_dispatch(pid, module, mode) do
|
||||
:gen_server.cast(pid, {:remote_dispatch, module, mode})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def import_dispatch(pid, module) do
|
||||
:gen_server.cast(pid, {:import_dispatch, module})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def alias_dispatch(pid, module) do
|
||||
:gen_server.cast(pid, {:alias_dispatch, module})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def collect_unused_imports(pid) do
|
||||
unused(pid, :import)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def collect_unused_aliases(pid) do
|
||||
unused(pid, :alias)
|
||||
end
|
||||
|
||||
defp unused(pid, tag) do
|
||||
:gen_server.call(pid, :ets, @timeout)
|
||||
|> :ets.select([{{{tag, :"$1"}, :"$2"}, [is_integer: :"$2"], [{{:"$1", :"$2"}}]}])
|
||||
|> Enum.sort
|
||||
end
|
||||
|
||||
# Callbacks
|
||||
|
||||
def init(dest) do
|
||||
{:ok, {:ets.new(__MODULE__, [:protected]), dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_call(:ets, _from, {d, dest}) do
|
||||
{:reply, d, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_call(:dest, _from, {d, dest}) do
|
||||
{:reply, dest, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:remote_dispatch, module, mode}, {d, dest}) do
|
||||
add_compile(d, module, mode)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:import_dispatch, module}, {d, dest}) do
|
||||
add_dispatch(d, module, :import)
|
||||
# Always compile time because we depend
|
||||
# on the module at compile time
|
||||
add_compile(d, module, :compile)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:alias_dispatch, module}, {d, dest}) do
|
||||
add_dispatch(d, module, :alias)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:add_import, module, line, warn}, {d, dest}) do
|
||||
add_directive(d, module, line, warn, :import)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:add_alias, module, line, warn}, {d, dest}) do
|
||||
add_directive(d, module, line, warn, :alias)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast(:stop, {d, dest}) do
|
||||
{:stop, :normal, {d, dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(_msg, {d, dest}) do
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def terminate(_reason, _state) do
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc false
|
||||
def code_change(_old, state, _extra) do
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
# Callbacks helpers
|
||||
|
||||
# In the table we keep imports and aliases.
|
||||
# If the value is false, it was not imported/aliased
|
||||
# If the value is a line, it was imported/aliased and has a pending warning
|
||||
# If the value is true, it was imported/aliased and used
|
||||
defp add_dispatch(d, module, tag) do
|
||||
:ets.insert(d, {{tag, module}, true})
|
||||
end
|
||||
|
||||
defp add_compile(d, module, :runtime), do: :ets.insert_new(d, {{:mode, module}, :runtime})
|
||||
defp add_compile(d, module, :compile), do: :ets.insert(d, {{:mode, module}, :compile})
|
||||
|
||||
defp add_directive(d, module, line, warn, tag) do
|
||||
marker = if warn, do: line, else: true
|
||||
:ets.insert(d, {{tag, module}, marker})
|
||||
end
|
||||
end
|
||||
@@ -1,8 +1,12 @@
|
||||
defmodule Kernel.ParallelCompiler do
|
||||
alias Erlang.orddict, as: Orddict
|
||||
|
||||
@moduledoc """
|
||||
A module responsible for compiling files in parallel.
|
||||
"""
|
||||
|
||||
defmacrop default_callback, do: quote(do: fn x -> x end)
|
||||
|
||||
@doc """
|
||||
Compiles the given files.
|
||||
|
||||
@@ -11,254 +15,103 @@ defmodule Kernel.ParallelCompiler do
|
||||
the current file stops being compiled until the dependency is
|
||||
resolved.
|
||||
|
||||
If there is an error during compilation or if `warnings_as_errors`
|
||||
is set to `true` and there is a warning, this function will fail
|
||||
with an exception.
|
||||
|
||||
This function accepts the following options:
|
||||
|
||||
* `:each_file` - for each file compiled, invokes the callback passing the
|
||||
file
|
||||
|
||||
* `:each_module` - for each module compiled, invokes the callback passing
|
||||
the file, module and the module bytecode
|
||||
|
||||
* `:dest` - the destination directory for the beam files. When using `files/2`,
|
||||
this information is only used to properly annotate the beam files before
|
||||
they are loaded into memory. If you want a file to actually be written to
|
||||
`dest`, use `files_to_path/3` instead.
|
||||
|
||||
Returns the modules generated by each compiled file.
|
||||
A callback that is invoked every time a file is compiled
|
||||
with its name can be optionally given as argument.
|
||||
"""
|
||||
def files(files, options \\ [])
|
||||
|
||||
def files(files, options) when is_list(options) do
|
||||
spawn_compilers(files, nil, options)
|
||||
def files(files, callback // default_callback) do
|
||||
files_to_path(files, nil, callback)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compiles the given files to the given path.
|
||||
Read `files/2` for more information.
|
||||
Read files/2 for more information.
|
||||
"""
|
||||
def files_to_path(files, path, options \\ [])
|
||||
|
||||
def files_to_path(files, path, options) when is_binary(path) and is_list(options) do
|
||||
spawn_compilers(files, path, options)
|
||||
end
|
||||
|
||||
defp spawn_compilers(files, path, options) do
|
||||
true = Code.ensure_loaded?(Kernel.ErrorHandler)
|
||||
compiler_pid = self()
|
||||
:elixir_code_server.cast({:reset_warnings, compiler_pid})
|
||||
schedulers = max(:erlang.system_info(:schedulers_online), 2)
|
||||
|
||||
result = spawn_compilers(files, files, path, options, [], [], schedulers, [])
|
||||
|
||||
# In case --warning-as-errors is enabled and there was a warning,
|
||||
# compilation status will be set to error.
|
||||
case :elixir_code_server.call({:compilation_status, compiler_pid}) do
|
||||
:ok ->
|
||||
result
|
||||
:error ->
|
||||
IO.puts :stderr, "Compilation failed due to warnings while using the --warnings-as-errors option"
|
||||
exit({:shutdown, 1})
|
||||
end
|
||||
def files_to_path(files, path, callback // default_callback) when is_binary(path) do
|
||||
Code.ensure_loaded(Kernel.ErrorHandler)
|
||||
spawn_compilers(files, path, callback, [], [], [])
|
||||
end
|
||||
|
||||
# We already have 4 currently running, don't spawn new ones
|
||||
defp spawn_compilers(entries, original, output, options, waiting, queued, schedulers, result) when
|
||||
length(queued) - length(waiting) >= schedulers do
|
||||
wait_for_messages(entries, original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
|
||||
# Release waiting processes
|
||||
defp spawn_compilers([h|t], original, output, options, waiting, queued, schedulers, result) when is_pid(h) do
|
||||
{_kind, ^h, ref, _module} = List.keyfind(waiting, h, 1)
|
||||
send h, {ref, :ready}
|
||||
waiting = List.keydelete(waiting, h, 1)
|
||||
spawn_compilers(t, original, output, options, waiting, queued, schedulers, result)
|
||||
defp spawn_compilers(files, output, callback, waiting, queued, result) when
|
||||
length(queued) - length(waiting) >= 4 do
|
||||
wait_for_messages(files, output, callback, waiting, queued, result)
|
||||
end
|
||||
|
||||
# Spawn a compiler for each file in the list until we reach the limit
|
||||
defp spawn_compilers([h|t], original, output, options, waiting, queued, schedulers, result) do
|
||||
defp spawn_compilers([h|t], output, callback, waiting, queued, result) do
|
||||
parent = self()
|
||||
|
||||
{pid, ref} =
|
||||
:erlang.spawn_monitor fn ->
|
||||
# Notify Code.ensure_compiled/2 that we should
|
||||
# attempt to compile the module by doing a dispatch.
|
||||
:erlang.put(:elixir_ensure_compiled, true)
|
||||
child = spawn_link fn ->
|
||||
Process.put(:elixir_compiler_pid, parent)
|
||||
Process.flag(:error_handler, Kernel.ErrorHandler)
|
||||
|
||||
# Set the elixir_compiler_pid used by our custom Kernel.ErrorHandler.
|
||||
:erlang.put(:elixir_compiler_pid, parent)
|
||||
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
|
||||
|
||||
exit(try do
|
||||
_ = if output do
|
||||
:elixir_compiler.file_to_path(h, output)
|
||||
else
|
||||
:elixir_compiler.file(h, Keyword.get(options, :dest))
|
||||
end
|
||||
{:shutdown, h}
|
||||
catch
|
||||
kind, reason ->
|
||||
{:failure, kind, reason, System.stacktrace}
|
||||
end)
|
||||
try do
|
||||
if output do
|
||||
Erlang.elixir_compiler.file_to_path(h, output)
|
||||
else
|
||||
Erlang.elixir_compiler.file(h)
|
||||
end
|
||||
parent <- { :compiled, self(), h }
|
||||
catch
|
||||
kind, reason ->
|
||||
parent <- { :failure, self(), kind, reason, System.stacktrace }
|
||||
end
|
||||
end
|
||||
|
||||
spawn_compilers(t, original, output, options, waiting,
|
||||
[{pid, ref, h}|queued], schedulers, result)
|
||||
spawn_compilers(t, output, callback, waiting, [{child,h}|queued], result)
|
||||
end
|
||||
|
||||
# No more files, nothing waiting, queue is empty, we are done
|
||||
defp spawn_compilers([], _original, _output, _options, [], [], _schedulers, result) do
|
||||
for {:module, mod} <- result, do: mod
|
||||
end
|
||||
defp spawn_compilers([], _output, _callback, [], [], result), do: result
|
||||
|
||||
# Queued x, waiting for x: POSSIBLE ERROR! Release processes so we get the failures
|
||||
defp spawn_compilers([], original, output, options, waiting, queued, schedulers, result) when length(waiting) == length(queued) do
|
||||
Enum.each queued, fn {child, _, _} ->
|
||||
{_kind, ^child, ref, _module} = List.keyfind(waiting, child, 1)
|
||||
send child, {ref, :release}
|
||||
end
|
||||
wait_for_messages([], original, output, options, waiting, queued, schedulers, result)
|
||||
defp spawn_compilers([], output, callback, waiting, queued, result) when length(waiting) == length(queued) do
|
||||
Enum.each queued, fn { child, _ } -> child <- { :release, self() } end
|
||||
wait_for_messages([], output, callback, waiting, queued, result)
|
||||
end
|
||||
|
||||
# No more files, but queue and waiting are not full or do not match
|
||||
defp spawn_compilers([], original, output, options, waiting, queued, schedulers, result) do
|
||||
wait_for_messages([], original, output, options, waiting, queued, schedulers, result)
|
||||
defp spawn_compilers([], output, callback, waiting, queued, result) do
|
||||
wait_for_messages([], output, callback, waiting, queued, result)
|
||||
end
|
||||
|
||||
# Wait for messages from child processes
|
||||
defp wait_for_messages(entries, original, output, options, waiting, queued, schedulers, result) do
|
||||
defp wait_for_messages(files, output, callback, waiting, queued, result) do
|
||||
receive do
|
||||
{:struct_available, module} ->
|
||||
available = for {:struct, pid, _, waiting_module} <- waiting,
|
||||
module == waiting_module,
|
||||
not pid in entries,
|
||||
do: pid
|
||||
|
||||
spawn_compilers(available ++ entries, original, output, options,
|
||||
waiting, queued, schedulers, [{:struct, module}|result])
|
||||
|
||||
{:module_available, child, ref, file, module, binary} ->
|
||||
if callback = Keyword.get(options, :each_module) do
|
||||
callback.(file, module, binary)
|
||||
end
|
||||
|
||||
# Release the module loader which is waiting for an ack
|
||||
send child, {ref, :ack}
|
||||
|
||||
available = for {_kind, pid, _, waiting_module} <- waiting,
|
||||
module == waiting_module,
|
||||
not pid in entries,
|
||||
do: pid
|
||||
|
||||
spawn_compilers(available ++ entries, original, output, options,
|
||||
waiting, queued, schedulers, [{:module, module}|result])
|
||||
|
||||
{:waiting, kind, child, ref, on} ->
|
||||
defined = fn {k, m} -> on == m and k in [kind, :module] end
|
||||
|
||||
# Oops, we already got it, do not put it on waiting.
|
||||
if :lists.any(defined, result) do
|
||||
send child, {ref, :ready}
|
||||
else
|
||||
waiting = [{kind, child, ref, on}|waiting]
|
||||
end
|
||||
|
||||
spawn_compilers(entries, original, output, options, waiting, queued, schedulers, result)
|
||||
|
||||
{:DOWN, _down_ref, :process, down_pid, {:shutdown, file}} ->
|
||||
if callback = Keyword.get(options, :each_file) do
|
||||
callback.(file)
|
||||
end
|
||||
|
||||
{ :compiled, child, file } ->
|
||||
callback.(file)
|
||||
new_queued = List.keydelete(queued, child, 1)
|
||||
# Sometimes we may have spurious entries in the waiting
|
||||
# list because someone invoked try/rescue UndefinedFunctionError
|
||||
new_entries = List.delete(entries, down_pid)
|
||||
new_queued = List.keydelete(queued, down_pid, 0)
|
||||
new_waiting = List.keydelete(waiting, down_pid, 1)
|
||||
spawn_compilers(new_entries, original, output, options, new_waiting, new_queued, schedulers, result)
|
||||
|
||||
{:DOWN, down_ref, :process, _down_pid, reason} ->
|
||||
handle_failure(down_ref, reason, entries, waiting, queued)
|
||||
wait_for_messages(entries, original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_failure(ref, reason, entries, waiting, queued) do
|
||||
if file = find_failure(ref, queued) do
|
||||
print_failure(file, reason)
|
||||
|
||||
if all_missing?(entries, waiting, queued) do
|
||||
collect_failures(queued, length(queued) - 1)
|
||||
end
|
||||
|
||||
Enum.each queued, fn {child, _, _} ->
|
||||
Process.exit(child, :kill)
|
||||
end
|
||||
|
||||
exit({:shutdown, 1})
|
||||
end
|
||||
end
|
||||
|
||||
defp find_failure(ref, queued) do
|
||||
case List.keyfind(queued, ref, 1) do
|
||||
{_child, ^ref, file} -> file
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp print_failure(_file, {:shutdown, _}) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp print_failure(file, {:failure, kind, reason, stacktrace}) do
|
||||
IO.puts "\n== Compilation error on file #{Path.relative_to_cwd(file)} =="
|
||||
IO.puts Exception.format(kind, reason, prune_stacktrace(stacktrace))
|
||||
end
|
||||
|
||||
defp print_failure(file, reason) do
|
||||
IO.puts "\n== Compilation error on file #{Path.relative_to_cwd(file)} =="
|
||||
IO.puts Exception.format(:exit, reason, [])
|
||||
end
|
||||
|
||||
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_clauses,
|
||||
:elixir_translator, :elixir_expand, :elixir_lexical, :elixir_exp_clauses,
|
||||
:elixir_def]
|
||||
|
||||
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
||||
prune_stacktrace(t)
|
||||
end
|
||||
|
||||
defp prune_stacktrace([h|t]) do
|
||||
[h|prune_stacktrace(t)]
|
||||
end
|
||||
|
||||
defp prune_stacktrace([]) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp all_missing?(entries, waiting, queued) do
|
||||
entries == [] and waiting != [] and
|
||||
length(waiting) == length(queued)
|
||||
end
|
||||
|
||||
defp collect_failures(_queued, 0), do: :ok
|
||||
|
||||
defp collect_failures(queued, remaining) do
|
||||
receive do
|
||||
{:DOWN, down_ref, :process, _down_pid, reason} ->
|
||||
if file = find_failure(down_ref, queued) do
|
||||
print_failure(file, reason)
|
||||
collect_failures(queued, remaining - 1)
|
||||
else
|
||||
collect_failures(queued, remaining)
|
||||
new_waiting = List.keydelete(waiting, child, 1)
|
||||
spawn_compilers(files, output, callback, new_waiting, new_queued, result)
|
||||
{ :module_available, child, module, binary } ->
|
||||
new_waiting = release_waiting_processes(module, waiting)
|
||||
new_result = [{module, binary}|result]
|
||||
wait_for_messages(files, output, callback, new_waiting, queued, new_result)
|
||||
{ :waiting, child, on } ->
|
||||
new_waiting = Orddict.store(child, on, waiting)
|
||||
spawn_compilers(files, output, callback, new_waiting, queued, result)
|
||||
{ :failure, child, kind, reason, stacktrace } ->
|
||||
extra = if match?({^child, module}, List.keyfind(waiting, child, 1)) do
|
||||
" (undefined module #{inspect module})"
|
||||
end
|
||||
after
|
||||
# Give up if no failure appears in 5 seconds
|
||||
5000 -> :ok
|
||||
|
||||
{^child, file} = List.keyfind(queued, child, 1)
|
||||
IO.puts "== Compilation error on file #{file}#{extra} =="
|
||||
Erlang.erlang.raise(kind, reason, stacktrace)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Release waiting processes that are waiting for the given module
|
||||
defp release_waiting_processes(module, waiting) do
|
||||
Enum.filter waiting, fn { child, waiting_module } ->
|
||||
if waiting_module == module do
|
||||
child <- { :release, self() }
|
||||
false
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,83 +3,49 @@ defmodule Kernel.ParallelRequire do
|
||||
A module responsible for requiring files in parallel.
|
||||
"""
|
||||
|
||||
defmacrop default_callback, do: quote(do: fn x -> x end)
|
||||
|
||||
@doc """
|
||||
Requires the given files.
|
||||
|
||||
A callback that is invoked every time a file is required
|
||||
can be optionally given as argument.
|
||||
|
||||
Returns the modules generated by each required file.
|
||||
"""
|
||||
def files(files, callback \\ fn x -> x end) do
|
||||
compiler_pid = self()
|
||||
:elixir_code_server.cast({:reset_warnings, compiler_pid})
|
||||
schedulers = max(:erlang.system_info(:schedulers_online), 2)
|
||||
result = spawn_requires(files, [], callback, schedulers, [])
|
||||
|
||||
# In case --warning-as-errors is enabled and there was a warning,
|
||||
# compilation status will be set to error.
|
||||
case :elixir_code_server.call({:compilation_status, compiler_pid}) do
|
||||
:ok ->
|
||||
result
|
||||
:error ->
|
||||
IO.puts :stderr, "Compilation failed due to warnings while using the --warnings-as-errors option"
|
||||
exit({:shutdown, 1})
|
||||
end
|
||||
def files(files, callback // default_callback) do
|
||||
spawn_requires(files, [], callback, [])
|
||||
end
|
||||
|
||||
defp spawn_requires([], [], _callback, _schedulers, result), do: result
|
||||
defp spawn_requires([], [], _callback, result), do: result
|
||||
defp spawn_requires([], waiting, callback, result), do: wait_for_messages([], waiting, callback, result)
|
||||
|
||||
defp spawn_requires([], waiting, callback, schedulers, result) do
|
||||
wait_for_messages([], waiting, callback, schedulers, result)
|
||||
defp spawn_requires(files, waiting, callback, result) when length(waiting) >= 4 do
|
||||
wait_for_messages(files, waiting, callback, result)
|
||||
end
|
||||
|
||||
defp spawn_requires(files, waiting, callback, schedulers, result) when length(waiting) >= schedulers do
|
||||
wait_for_messages(files, waiting, callback, schedulers, result)
|
||||
end
|
||||
defp spawn_requires([h|t], waiting, callback, result) do
|
||||
parent = self
|
||||
|
||||
defp spawn_requires([h|t], waiting, callback, schedulers, result) do
|
||||
parent = self()
|
||||
{pid, ref} = :erlang.spawn_monitor fn ->
|
||||
:erlang.put(:elixir_compiler_pid, parent)
|
||||
|
||||
exit(try do
|
||||
new = Code.require_file(h) || []
|
||||
{:required, Enum.map(new, &elem(&1, 0)), h}
|
||||
child = spawn_link fn ->
|
||||
try do
|
||||
new = Code.require_file(h)
|
||||
result = new ++ result
|
||||
callback.(h)
|
||||
parent <- { :required, self }
|
||||
catch
|
||||
kind, reason ->
|
||||
{:failure, kind, reason, System.stacktrace}
|
||||
end)
|
||||
parent <- { :failure, self, kind, reason, System.stacktrace }
|
||||
end
|
||||
end
|
||||
|
||||
spawn_requires(t, [{pid, ref}|waiting], callback, schedulers, result)
|
||||
spawn_requires(t, [child|waiting], callback, result)
|
||||
end
|
||||
|
||||
defp wait_for_messages(files, waiting, callback, schedulers, result) do
|
||||
defp wait_for_messages(files, waiting, callback, result) do
|
||||
receive do
|
||||
{:DOWN, ref, :process, pid, status} ->
|
||||
tuple = {pid, ref}
|
||||
if tuple in waiting do
|
||||
case status do
|
||||
{:required, mods, file} ->
|
||||
callback.(file)
|
||||
result = mods ++ result
|
||||
waiting = List.delete(waiting, tuple)
|
||||
{:failure, kind, reason, stacktrace} ->
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
other ->
|
||||
:erlang.raise(:exit, other, [])
|
||||
end
|
||||
end
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
{:module_available, child, ref, _, _, _} ->
|
||||
send(child, {ref, :ack})
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
{:struct_available, _} ->
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
{:waiting, :struct, child, ref, _} ->
|
||||
send(child, {ref, :release})
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
{ :required, child } ->
|
||||
spawn_requires(files, List.delete(waiting, child), callback, result)
|
||||
{ :failure, _child, kind, reason, stacktrace } ->
|
||||
Erlang.erlang.raise(kind, reason, stacktrace)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
+186
-1532
File diff suppressed because it is too large
Load Diff
+153
-897
File diff suppressed because it is too large
Load Diff
@@ -1,73 +0,0 @@
|
||||
import Kernel, except: [destructure: 2, defdelegate: 2, defstruct: 2]
|
||||
|
||||
defmodule Kernel.Utils do
|
||||
@moduledoc false
|
||||
|
||||
def destructure(list, count) when is_list(list), do: destructure_list(list, count)
|
||||
def destructure(nil, count), do: destructure_nil(count)
|
||||
|
||||
defp destructure_list(_, 0), do: []
|
||||
defp destructure_list([], count), do: destructure_nil(count)
|
||||
defp destructure_list([h|t], count), do: [h|destructure_list(t, count - 1)]
|
||||
|
||||
defp destructure_nil(0), do: []
|
||||
defp destructure_nil(count), do: [nil|destructure_nil(count - 1)]
|
||||
|
||||
def defdelegate(fun, opts, env) do
|
||||
append_first = Keyword.get(opts, :append_first, false)
|
||||
|
||||
{name, args} =
|
||||
case Macro.decompose_call(fun) do
|
||||
{_, _} = pair -> pair
|
||||
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
|
||||
end
|
||||
|
||||
:ok = check_defdelegate_args(args, env)
|
||||
|
||||
as_args =
|
||||
case append_first and args != [] do
|
||||
true -> tl(args) ++ [hd(args)]
|
||||
false -> args
|
||||
end
|
||||
|
||||
as = Keyword.get(opts, :as, name)
|
||||
{name, args, as, as_args}
|
||||
end
|
||||
|
||||
# TODO: Convert this to an error on 1.3
|
||||
defp check_defdelegate_args([], _env),
|
||||
do: :ok
|
||||
defp check_defdelegate_args([{var, _, mod}|rest], env) when is_atom(var) and is_atom(mod),
|
||||
do: check_defdelegate_args(rest, env)
|
||||
defp check_defdelegate_args([code|_], env) do
|
||||
:elixir_errors.warn(env.line, env.file,
|
||||
"defdelegate/2 will only accept variable names in upcoming versions, " <>
|
||||
"got: #{Macro.to_string(code)}")
|
||||
end
|
||||
|
||||
def defstruct(module, fields) do
|
||||
case fields do
|
||||
fs when is_list(fs) -> :ok
|
||||
other ->
|
||||
raise ArgumentError, "struct fields definition must be list, got: #{inspect other}"
|
||||
end
|
||||
|
||||
fields = :lists.map(fn
|
||||
{key, val} when is_atom(key) ->
|
||||
try do
|
||||
Macro.escape(val)
|
||||
rescue
|
||||
e in [ArgumentError] ->
|
||||
raise ArgumentError, "invalid value for struct field #{key}, " <> Exception.message(e)
|
||||
else
|
||||
_ -> {key, val}
|
||||
end
|
||||
key when is_atom(key) ->
|
||||
{key, nil}
|
||||
other ->
|
||||
raise ArgumentError, "struct field names must be atoms, got: #{inspect other}"
|
||||
end, fields)
|
||||
|
||||
:maps.put(:__struct__, module, :maps.from_list(fields))
|
||||
end
|
||||
end
|
||||
+140
-754
@@ -1,854 +1,240 @@
|
||||
defmodule Keyword do
|
||||
@moduledoc """
|
||||
A set of functions for working with keywords.
|
||||
A keyword is a list of tuples where the first element
|
||||
of the tuple is an atom and the second element can be
|
||||
any value. The list is sorted by the first element of
|
||||
each tuple.
|
||||
|
||||
A keyword is a list of 2-element tuples where the first
|
||||
element of the tuple is an atom and the second element
|
||||
can be any value.
|
||||
|
||||
A keyword may have duplicated keys so it is not strictly
|
||||
A keyword may have duplicated keys, so it is not strictly
|
||||
a dictionary. However most of the functions in this module
|
||||
behave exactly as a dictionary so they work similarly to
|
||||
the functions you would find in the `Map` module.
|
||||
allows it to behave exactly as a dictionary. For example,
|
||||
`Keyword.get` will get the first entry matching the given
|
||||
key, regardless if duplicated entries exist. Similarly,
|
||||
`Keyword.put` and `Keyword.delete` ensure all duplicated
|
||||
entries for a given key are removed when invoked.
|
||||
|
||||
For example, `Keyword.get/3` will get the first entry matching
|
||||
the given key, regardless if duplicated entries exist.
|
||||
Similarly, `Keyword.put/3` and `Keyword.delete/3` ensure all
|
||||
duplicated entries for a given key are removed when invoked.
|
||||
|
||||
A handful of functions exist to handle duplicated keys, in
|
||||
particular, `Enum.into/2` allows creating new keywords without
|
||||
removing duplicated keys, `get_values/2` returns all values for
|
||||
a given key and `delete_first/2` deletes just one of the existing
|
||||
entries.
|
||||
|
||||
The functions in Keyword do not guarantee any property when
|
||||
it comes to ordering. However, since a keyword list is simply a
|
||||
list, all the operations defined in `Enum` and `List` can be
|
||||
applied too, specially when ordering is required.
|
||||
This module uses `==` as operator to check if two keys
|
||||
are equal or not.
|
||||
"""
|
||||
|
||||
@compile :inline_list_funcs
|
||||
|
||||
@type key :: atom
|
||||
@type value :: any
|
||||
|
||||
@type t :: [{key, value}]
|
||||
@type t(value) :: [{key, value}]
|
||||
@doc """
|
||||
Creates a Keyword from enum. Differently from `Keyword.new`
|
||||
that behaves as a dict, `Keyword.from_enum` do not remove
|
||||
duplicated entries.
|
||||
"""
|
||||
def from_enum(enum) do
|
||||
Enum.qsort(enum)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if `term` is a keyword list; otherwise returns `false`.
|
||||
Returns an empty keywords list, i.e. an empty list.
|
||||
"""
|
||||
def new do
|
||||
[]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a Keyword from an enumerable. Similarly to dicts,
|
||||
duplicated entries are removed, the latest one prevails.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.keyword?([])
|
||||
true
|
||||
iex> Keyword.keyword?([a: 1])
|
||||
true
|
||||
iex> Keyword.keyword?([{Foo, 1}])
|
||||
true
|
||||
iex> Keyword.keyword?([{}])
|
||||
false
|
||||
iex> Keyword.keyword?([:key])
|
||||
false
|
||||
iex> Keyword.keyword?(%{})
|
||||
false
|
||||
Keyword.new [{:b,1},{:a,2}]
|
||||
#=> [a: 2, b: 1]
|
||||
|
||||
"""
|
||||
@spec keyword?(term) :: boolean
|
||||
def keyword?(term)
|
||||
|
||||
def keyword?([{key, _value} | rest]) when is_atom(key), do: keyword?(rest)
|
||||
def keyword?([]), do: true
|
||||
def keyword?(_other), do: false
|
||||
|
||||
@doc """
|
||||
Returns an empty keyword list, i.e. an empty list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.new()
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec new :: t
|
||||
def new, do: []
|
||||
|
||||
@doc """
|
||||
Creates a keyword from an enumerable.
|
||||
|
||||
Duplicated entries are removed, the latest one prevails.
|
||||
Unlike `Enum.into(enumerable, [])`, `Keyword.new(enumerable)`
|
||||
guarantees the keys are unique.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.new([{:b, 1}, {:a, 2}])
|
||||
[b: 1, a: 2]
|
||||
|
||||
iex> Keyword.new([{:a, 1}, {:a, 2}, {:a, 3}])
|
||||
[a: 3]
|
||||
|
||||
"""
|
||||
@spec new(Enum.t) :: t
|
||||
def new(pairs) do
|
||||
new(pairs, fn pair -> pair end)
|
||||
Enum.reduce pairs, [], fn {k, v}, keywords ->
|
||||
put(keywords, k, v)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a keyword from an enumerable via the transformation function.
|
||||
|
||||
Duplicated entries are removed, the latest one prevails.
|
||||
Unlike `Enum.into(enumerable, [], fun)`,
|
||||
`Keyword.new(enumerable, fun)` guarantees the keys are unique.
|
||||
Creates a Keyword from an enumerable with the
|
||||
help of the transformation function. Duplicated
|
||||
entries are removed, the latest one prevails.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.new([:a, :b], fn (x) -> {x, x} end)
|
||||
[a: :a, b: :b]
|
||||
|
||||
Keyword.new [:a, :b], fn x -> {x,x} end
|
||||
#=> [a: :a, b: :b]
|
||||
"""
|
||||
@spec new(Enum.t, (term -> {key, value})) :: t
|
||||
def new(pairs, transform) do
|
||||
fun = fn el, acc ->
|
||||
{k, v} = transform.(el)
|
||||
put_new(acc, k, v)
|
||||
Enum.reduce pairs, [], fn i, keywords ->
|
||||
{ k, v } = transform.(i)
|
||||
put(keywords, k, v)
|
||||
end
|
||||
:lists.foldl(fun, [], Enum.reverse(pairs))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value for a specific `key`.
|
||||
Gets the value for specific key.
|
||||
|
||||
If `key` does not exist, return the default value
|
||||
(`nil` if no default value).
|
||||
If key not exist return default value (nil if no default value)
|
||||
exists.
|
||||
|
||||
If duplicated entries exist, the first one is returned.
|
||||
Use `get_values/2` to retrieve all entries.
|
||||
Use get_values/2 to retrieve all entries.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.get([], :a)
|
||||
nil
|
||||
iex> Keyword.get([a: 1], :a)
|
||||
1
|
||||
iex> Keyword.get([a: 1], :b)
|
||||
nil
|
||||
iex> Keyword.get([a: 1], :b, 3)
|
||||
3
|
||||
|
||||
With duplicated keys:
|
||||
|
||||
iex> Keyword.get([a: 1, a: 2], :a, 3)
|
||||
1
|
||||
iex> Keyword.get([a: 1, a: 2], :b, 3)
|
||||
3
|
||||
|
||||
Keyword.get [a: 1], :a #=> 1
|
||||
Keyword.get [a: 1], :b #=> nil
|
||||
Keyword.get [a: 1], :b, 3 #=> 3
|
||||
"""
|
||||
@spec get(t, key) :: value
|
||||
@spec get(t, key, value) :: value
|
||||
def get(keywords, key, default \\ nil) when is_list(keywords) and is_atom(key) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, value} -> value
|
||||
false -> default
|
||||
end
|
||||
end
|
||||
def get(keywords, key, default // nil)
|
||||
def get([{k, _}|_], key, default) when key < k, do: default
|
||||
def get([{k, _}|d], key, default) when key > k, do: get(d, key, default)
|
||||
def get([{_, value}|_], _key, _default), do: value
|
||||
def get([], _, default), do: default
|
||||
|
||||
@doc """
|
||||
Gets the value for a specific `key`.
|
||||
|
||||
If `key` does not exist, lazily evaluates `fun` and returns its result.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
If duplicated entries exist, the first one is returned.
|
||||
Use `get_values/2` to retrieve all entries.
|
||||
Gets the value for specific key. If key does not exist,
|
||||
an error is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> keyword = [a: 1]
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 13
|
||||
...> end
|
||||
iex> Keyword.get_lazy(keyword, :a, fun)
|
||||
1
|
||||
iex> Keyword.get_lazy(keyword, :b, fun)
|
||||
13
|
||||
|
||||
Keyword.get! [a: 1], :a #=> 1
|
||||
Keyword.get! [a: 1], :b #=> raises KeyError[key: :b]
|
||||
"""
|
||||
@spec get_lazy(t, key, (() -> value)) :: value
|
||||
def get_lazy(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key) and is_function(fun, 0) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, value} -> value
|
||||
false -> fun.()
|
||||
end
|
||||
end
|
||||
def get!([{k, _}|_], key) when key < k, do: raise(Keyword.KeyError, key: key)
|
||||
def get!([{k, _}|d], key) when key > k, do: get!(d, key)
|
||||
def get!([{_, value}|_], _key), do: value
|
||||
def get!([], key), do: raise(Keyword.KeyError, key: key)
|
||||
|
||||
@doc """
|
||||
Gets the value from `key` and updates it, all in one pass.
|
||||
|
||||
This `fun` argument receives the value of `key` (or `nil` if `key`
|
||||
is not present) and must return a two-elements tuple: the "get" value (the
|
||||
retrieved value, which can be operated on before being returned) and the new
|
||||
value to be stored under `key`.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by `fun` and a new
|
||||
keyword list with the updated value under `key`.
|
||||
Gets all values for a specific key.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.get_and_update([a: 1], :a, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{1, [a: "new value!"]}
|
||||
|
||||
iex> Keyword.get_and_update([a: 1], :b, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{nil, [b: "new value!", a: 1]}
|
||||
Keyword.get_values [a: 1, a: 2], :a
|
||||
#=> [1,2]
|
||||
|
||||
"""
|
||||
@spec get_and_update(t, key, (value -> {get, value})) :: {get, t} when get: term
|
||||
def get_and_update(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key),
|
||||
do: get_and_update(keywords, [], key, fun)
|
||||
|
||||
defp get_and_update([{key, value}|t], acc, key, fun) do
|
||||
{get, new_value} = fun.(value)
|
||||
{get, :lists.reverse(acc, [{key, new_value}|t])}
|
||||
end
|
||||
|
||||
defp get_and_update([h|t], acc, key, fun),
|
||||
do: get_and_update(t, [h|acc], key, fun)
|
||||
|
||||
defp get_and_update([], acc, key, fun) do
|
||||
{get, update} = fun.(nil)
|
||||
{get, [{key, update}|:lists.reverse(acc)]}
|
||||
end
|
||||
def get_values([{k, _}|_], key) when key < k, do: []
|
||||
def get_values([{k, _}|d], key) when key > k, do: get_values(d, key)
|
||||
def get_values([{_, value}|d], key), do: [value|get_values(d, key)]
|
||||
def get_values([], _), do: []
|
||||
|
||||
@doc """
|
||||
Gets the value from `key` and updates it. Raises if there is no `key`.
|
||||
|
||||
This `fun` argument receives the value of `key` and must return a
|
||||
two-elements tuple: the "get" value (the retrieved value, which can be
|
||||
operated on before being returned) and the new value to be stored under
|
||||
`key`.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by `fun` and a new
|
||||
keyword list with the updated value under `key`.
|
||||
Returns all keys from the keywords list. Duplicated
|
||||
keys appear duplicated in the final list of keys.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.get_and_update!([a: 1], :a, fn(current_value) ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{1, [a: "new value!"]}
|
||||
|
||||
iex> Keyword.get_and_update!([a: 1], :b, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
Keyword.keys [a: 1, b: 2] #=> [:a,:b]
|
||||
|
||||
"""
|
||||
@spec get_and_update!(t, key, (value -> {get, value})) :: {get, t} | no_return when get: term
|
||||
def get_and_update!(keywords, key, fun) do
|
||||
get_and_update!(keywords, key, fun, [])
|
||||
end
|
||||
|
||||
defp get_and_update!([{key, value}|keywords], key, fun, acc) do
|
||||
{get, value} = fun.(value)
|
||||
{get, :lists.reverse(acc, [{key, value}|delete(keywords, key)])}
|
||||
end
|
||||
|
||||
defp get_and_update!([{_, _} = e|keywords], key, fun, acc) do
|
||||
get_and_update!(keywords, key, fun, [e|acc])
|
||||
end
|
||||
|
||||
defp get_and_update!([], key, _fun, acc) when is_atom(key) do
|
||||
raise(KeyError, key: key, term: acc)
|
||||
def keys(keywords) do
|
||||
lc { key, _ } inlist keywords, do: key
|
||||
end
|
||||
|
||||
@doc """
|
||||
Fetches the value for a specific `key` and returns it in a tuple.
|
||||
|
||||
If the `key` does not exist, returns `:error`.
|
||||
Returns all values.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.fetch([a: 1], :a)
|
||||
{:ok, 1}
|
||||
iex> Keyword.fetch([a: 1], :b)
|
||||
:error
|
||||
|
||||
Keyword.values [a: 1, b: 2] #=> [1,2]
|
||||
"""
|
||||
@spec fetch(t, key) :: {:ok, value} | :error
|
||||
def fetch(keywords, key) when is_list(keywords) and is_atom(key) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, value} -> {:ok, value}
|
||||
false -> :error
|
||||
end
|
||||
def values(keywords) do
|
||||
lc { _, value } inlist keywords, do: value
|
||||
end
|
||||
|
||||
@doc """
|
||||
Fetches the value for specific `key`.
|
||||
|
||||
If `key` does not exist, a `KeyError` is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.fetch!([a: 1], :a)
|
||||
1
|
||||
iex> Keyword.fetch!([a: 1], :b)
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
|
||||
"""
|
||||
@spec fetch!(t, key) :: value | no_return
|
||||
def fetch!(keywords, key) when is_list(keywords) and is_atom(key) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, value} -> value
|
||||
false -> raise(KeyError, key: key, term: keywords)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets all values for a specific `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.get_values([], :a)
|
||||
[]
|
||||
iex> Keyword.get_values([a: 1], :a)
|
||||
[1]
|
||||
iex> Keyword.get_values([a: 1, a: 2], :a)
|
||||
[1, 2]
|
||||
|
||||
"""
|
||||
@spec get_values(t, key) :: [value]
|
||||
def get_values(keywords, key) when is_list(keywords) and is_atom(key) do
|
||||
fun = fn
|
||||
{k, v} when k === key -> {true, v}
|
||||
{_, _} -> false
|
||||
end
|
||||
:lists.filtermap(fun, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all keys from the keyword list.
|
||||
|
||||
Duplicated keys appear duplicated in the final list of keys.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.keys([a: 1, b: 2])
|
||||
[:a, :b]
|
||||
iex> Keyword.keys([a: 1, b: 2, a: 3])
|
||||
[:a, :b, :a]
|
||||
|
||||
"""
|
||||
@spec keys(t) :: [key]
|
||||
def keys(keywords) when is_list(keywords) do
|
||||
:lists.map(fn {k, _} -> k end, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all values from the keyword list.
|
||||
|
||||
Values from duplicated keys will be kept in the final list of values.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.values([a: 1, b: 2])
|
||||
[1, 2]
|
||||
iex> Keyword.values([a: 1, b: 2, a: 3])
|
||||
[1, 2, 3]
|
||||
|
||||
"""
|
||||
@spec values(t) :: [value]
|
||||
def values(keywords) when is_list(keywords) do
|
||||
:lists.map(fn {_, v} -> v end, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the entries in the keyword list for a `key` with `value`.
|
||||
|
||||
If no `key` with `value` exists, returns the keyword list unchanged.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.delete([a: 1, b: 2], :a, 1)
|
||||
[b: 2]
|
||||
iex> Keyword.delete([a: 1, b: 2, a: 3], :a, 3)
|
||||
[a: 1, b: 2]
|
||||
iex> Keyword.delete([a: 1], :a, 5)
|
||||
[a: 1]
|
||||
iex> Keyword.delete([a: 1], :b, 5)
|
||||
[a: 1]
|
||||
|
||||
"""
|
||||
@spec delete(t, key, value) :: t
|
||||
def delete(keywords, key, value) when is_list(keywords) and is_atom(key) do
|
||||
:lists.filter(fn {k, v} -> k != key or v != value end, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the entries in the keyword list for a specific `key`.
|
||||
|
||||
If the `key` does not exist, returns the keyword list unchanged.
|
||||
Use `delete_first/2` to delete just the first entry in case of
|
||||
Deletes all entries in the keywords list for a specific key.
|
||||
If the key does not exist, returns the keywords list unchanged.
|
||||
Use `delete_first` to delete just the first entry in case of
|
||||
duplicated keys.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.delete([a: 1, b: 2], :a)
|
||||
[b: 2]
|
||||
iex> Keyword.delete([a: 1, b: 2, a: 3], :a)
|
||||
[b: 2]
|
||||
iex> Keyword.delete([b: 2], :a)
|
||||
[b: 2]
|
||||
|
||||
Keyword.delete [a: 1, b: 2], :a #=> [b: 2]
|
||||
Keyword.delete [b: 2], :a #=> [b: 2]
|
||||
"""
|
||||
@spec delete(t, key) :: t
|
||||
def delete(keywords, key) when is_list(keywords) and is_atom(key) do
|
||||
:lists.filter(fn {k, _} -> k != key end, keywords)
|
||||
end
|
||||
def delete([{k, _}|_] = keywords, key) when key < k, do: keywords
|
||||
def delete([{k, _} = e|tail], key) when key > k, do: [e|delete(tail, key)]
|
||||
def delete([{_, _}|tail], key), do: delete(tail, key)
|
||||
def delete([], _), do: []
|
||||
|
||||
@doc """
|
||||
Deletes the first entry in the keyword list for a specific `key`.
|
||||
|
||||
If the `key` does not exist, returns the keyword list unchanged.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.delete_first([a: 1, b: 2, a: 3], :a)
|
||||
[b: 2, a: 3]
|
||||
iex> Keyword.delete_first([b: 2], :a)
|
||||
[b: 2]
|
||||
|
||||
"""
|
||||
@spec delete_first(t, key) :: t
|
||||
def delete_first(keywords, key) when is_list(keywords) and is_atom(key) do
|
||||
:lists.keydelete(key, 1, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under `key`.
|
||||
Sets the given `value` under `key`.
|
||||
|
||||
If a previous value is already stored, all entries are
|
||||
removed and the value is overridden.
|
||||
removed and the value is overriden.
|
||||
|
||||
Use `put_other/3` to add a new value for an existing key
|
||||
without removing previous ones.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.put([a: 1], :b, 2)
|
||||
[b: 2, a: 1]
|
||||
iex> Keyword.put([a: 1, b: 2], :a, 3)
|
||||
[a: 3, b: 2]
|
||||
iex> Keyword.put([a: 1, b: 2, a: 4], :a, 3)
|
||||
[a: 3, b: 2]
|
||||
|
||||
Keyword.put [a: 1, b: 2], :a, 3
|
||||
#=> [a: 3, b: 2]
|
||||
"""
|
||||
@spec put(t, key, value) :: t
|
||||
def put(keywords, key, value) when is_list(keywords) and is_atom(key) do
|
||||
def put([{k, _} = e|keywords], key, value) when key < k and is_atom(key) do
|
||||
[{key, value},e|keywords]
|
||||
end
|
||||
|
||||
def put([{k, _} = e|keywords], key, value) when key > k do
|
||||
[e|put(keywords, key, value)]
|
||||
end
|
||||
|
||||
def put([{key, _}|keywords], key, value) when is_atom(key) do
|
||||
[{key, value}|delete(keywords, key)]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates `fun` and puts the result under `key`
|
||||
in keyword list unless `key` is already present.
|
||||
|
||||
This is useful if the value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> keyword = [a: 1]
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 3
|
||||
...> end
|
||||
iex> Keyword.put_new_lazy(keyword, :a, fun)
|
||||
[a: 1]
|
||||
iex> Keyword.put_new_lazy(keyword, :b, fun)
|
||||
[b: 3, a: 1]
|
||||
|
||||
"""
|
||||
@spec put_new_lazy(t, key, (() -> value)) :: t
|
||||
def put_new_lazy(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key) and is_function(fun, 0) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, _} -> keywords
|
||||
false -> [{key, fun.()}|keywords]
|
||||
end
|
||||
def put([], key, value) when is_atom(key) do
|
||||
[{key, value}]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under `key` unless the entry `key`
|
||||
already exists.
|
||||
Merges two keywords lists into one. If they have duplicated
|
||||
entries, the one given as second argument wins.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.put_new([a: 1], :b, 2)
|
||||
[b: 2, a: 1]
|
||||
iex> Keyword.put_new([a: 1, b: 2], :a, 3)
|
||||
[a: 1, b: 2]
|
||||
|
||||
Keyword.merge [a: 1, b: 2], [a: 3, d: 4]
|
||||
#=> [a:3, b:2, d: 4]
|
||||
"""
|
||||
@spec put_new(t, key, value) :: t
|
||||
def put_new(keywords, key, value) when is_list(keywords) and is_atom(key) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, _} -> keywords
|
||||
false -> [{key, value}|keywords]
|
||||
end
|
||||
def merge(d1, d2) do
|
||||
merge(d1, d2, fn _k, _v1, v2 -> v2 end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two keywords are equal.
|
||||
|
||||
Two keywords are considered to be equal if they contain
|
||||
the same keys and those keys contain the same values.
|
||||
Merges two keywords lists into one. If they have duplicated
|
||||
entries, the given function is invoked to solve conflicts.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.equal?([a: 1, b: 2], [b: 2, a: 1])
|
||||
true
|
||||
iex> Keyword.equal?([a: 1, b: 2], [b: 1, a: 2])
|
||||
false
|
||||
iex> Keyword.equal?([a: 1, b: 2, a: 3], [b: 2, a: 3, a: 1])
|
||||
true
|
||||
|
||||
"""
|
||||
@spec equal?(t, t) :: boolean
|
||||
def equal?(left, right) when is_list(left) and is_list(right) do
|
||||
:lists.sort(left) == :lists.sort(right)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Merges two keyword lists into one.
|
||||
|
||||
All keys, including duplicated keys, given in `keywords2` will be added
|
||||
to `keywords1`, overriding any existing one.
|
||||
|
||||
There are no guarantees about the order of keys in the returned keyword.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4])
|
||||
[b: 2, a: 3, d: 4]
|
||||
|
||||
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4, a: 5])
|
||||
[b: 2, a: 3, d: 4, a: 5]
|
||||
|
||||
"""
|
||||
@spec merge(t, t) :: t
|
||||
def merge(keywords1, keywords2) when is_list(keywords1) and is_list(keywords2) do
|
||||
fun = fn {k, _v} -> not has_key?(keywords2, k) end
|
||||
:lists.filter(fun, keywords1) ++ keywords2
|
||||
end
|
||||
|
||||
@doc """
|
||||
Merges two keyword lists into one.
|
||||
|
||||
All keys, including duplicated keys, given in `keywords2` will be added
|
||||
to `keywords1`. The given function will be invoked to solve conflicts.
|
||||
|
||||
If `keywords2` has duplicate keys, the given function will be invoked
|
||||
for each matching pair in `keywords1`.
|
||||
|
||||
There are no guarantees about the order of keys in the returned keyword.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4], fn _k, v1, v2 ->
|
||||
...> v1 + v2
|
||||
...> end)
|
||||
[b: 2, a: 4, d: 4]
|
||||
|
||||
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4, a: 5], fn :a, v1, v2 ->
|
||||
...> v1 + v2
|
||||
...> end)
|
||||
[b: 2, a: 4, d: 4, a: 5]
|
||||
|
||||
iex> Keyword.merge([a: 1, b: 2, a: 3], [a: 3, d: 4, a: 5], fn :a, v1, v2 ->
|
||||
...> v1 + v2
|
||||
...> end)
|
||||
[b: 2, a: 4, d: 4, a: 8]
|
||||
|
||||
"""
|
||||
@spec merge(t, t, (key, value, value -> value)) :: t
|
||||
def merge(keywords1, keywords2, fun) when is_list(keywords1) and is_list(keywords2) do
|
||||
do_merge(keywords2, [], keywords1, keywords1, fun)
|
||||
end
|
||||
|
||||
defp do_merge([{k, v2}|t], acc, rest, original, fun) do
|
||||
case :lists.keyfind(k, 1, original) do
|
||||
{^k, v1} ->
|
||||
do_merge(t, [{k, fun.(k, v1, v2)}|acc],
|
||||
delete(rest, k), :lists.keydelete(k, 1, original), fun)
|
||||
false ->
|
||||
do_merge(t, [{k, v2}|acc], rest, original, fun)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_merge([], acc, rest, _original, _fun) do
|
||||
rest ++ :lists.reverse(acc)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns whether a given `key` exists in the given `keywords`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.has_key?([a: 1], :a)
|
||||
true
|
||||
iex> Keyword.has_key?([a: 1], :b)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec has_key?(t, key) :: boolean
|
||||
def has_key?(keywords, key) when is_list(keywords) and 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 `KeyError`.
|
||||
|
||||
If there are duplicated keys, they are all removed and only the first one
|
||||
is updated.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.update!([a: 1], :a, &(&1 * 2))
|
||||
[a: 2]
|
||||
iex> Keyword.update!([a: 1, a: 2], :a, &(&1 * 2))
|
||||
[a: 2]
|
||||
|
||||
iex> Keyword.update!([a: 1], :b, &(&1 * 2))
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
|
||||
"""
|
||||
@spec update!(t, key, (value -> value)) :: t | no_return
|
||||
def update!(keywords, key, fun) do
|
||||
update!(keywords, key, fun, keywords)
|
||||
end
|
||||
|
||||
defp update!([{key, value}|keywords], key, fun, _dict) do
|
||||
[{key, fun.(value)}|delete(keywords, key)]
|
||||
end
|
||||
|
||||
defp update!([{_, _} = e|keywords], key, fun, dict) do
|
||||
[e|update!(keywords, key, fun, dict)]
|
||||
end
|
||||
|
||||
defp update!([], key, _fun, dict) when is_atom(key) do
|
||||
raise(KeyError, key: key, term: dict)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the `key` in `keywords` with the given function.
|
||||
|
||||
If the `key` does not exist, inserts the given `initial` value.
|
||||
|
||||
If there are duplicated keys, they are all removed and only the first one
|
||||
is updated.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.update([a: 1], :a, 13, &(&1 * 2))
|
||||
[a: 2]
|
||||
iex> Keyword.update([a: 1, a: 2], :a, 13, &(&1 * 2))
|
||||
[a: 2]
|
||||
iex> Keyword.update([a: 1], :b, 11, &(&1 * 2))
|
||||
[a: 1, b: 11]
|
||||
|
||||
"""
|
||||
@spec update(t, key, value, (value -> value)) :: t
|
||||
def update(keywords, key, initial, fun)
|
||||
|
||||
def update([{key, value}|keywords], key, _initial, fun) do
|
||||
[{key, fun.(value)}|delete(keywords, key)]
|
||||
end
|
||||
|
||||
def update([{_, _} = e|keywords], key, initial, fun) do
|
||||
[e|update(keywords, key, initial, fun)]
|
||||
end
|
||||
|
||||
def update([], key, initial, _fun) when is_atom(key) do
|
||||
[{key, initial}]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Takes all entries corresponding to the given keys and extracts them into a
|
||||
separate keyword list.
|
||||
|
||||
Returns a tuple with the new list and the old list with removed keys.
|
||||
|
||||
Keys for which there are no entires in the keyword list are ignored.
|
||||
|
||||
Entries with duplicated keys end up in the same keyword list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.split([a: 1, b: 2, c: 3], [:a, :c, :e])
|
||||
{[a: 1, c: 3], [b: 2]}
|
||||
iex> Keyword.split([a: 1, b: 2, c: 3, a: 4], [:a, :c, :e])
|
||||
{[a: 1, c: 3, a: 4], [b: 2]}
|
||||
|
||||
"""
|
||||
def split(keywords, keys) when is_list(keywords) do
|
||||
fun = fn {k, v}, {take, drop} ->
|
||||
case k in keys do
|
||||
true -> {[{k, v}|take], drop}
|
||||
false -> {take, [{k, v}|drop]}
|
||||
Keyword.merge [a: 1, b: 2], [a: 3, d: 4], fn _k, v1, v2 ->
|
||||
v1 + v2
|
||||
end
|
||||
end
|
||||
|
||||
acc = {[], []}
|
||||
{take, drop} = :lists.foldl(fun, acc, keywords)
|
||||
{:lists.reverse(take), :lists.reverse(drop)}
|
||||
#=> [a:4, b:2, d: 4]
|
||||
"""
|
||||
def merge([{k1, _} = e1|d1], [{k2, _} = e2|d2], fun) when k1 < k2 and is_atom(k1) do
|
||||
[e1|merge(d1, [e2|d2], fun)]
|
||||
end
|
||||
|
||||
def merge([{k1, _} = e1|d1], [{k2, _} = e2|d2], fun) when k1 > k2 and is_atom(k2) do
|
||||
[e2|merge([e1|d1], d2, fun)]
|
||||
end
|
||||
|
||||
def merge([{k1, v1}|d1], [{k1, v2}|d2], fun) do
|
||||
[{k1, fun.(k1, v1, v2)}|merge(d1, d2, fun)]
|
||||
end
|
||||
|
||||
def merge([], d2, _fun), do: d2
|
||||
def merge(d1, [], _fun), do: d1
|
||||
|
||||
@doc """
|
||||
Takes all entries corresponding to the given keys and returns them in a new
|
||||
keyword list.
|
||||
|
||||
Duplicated keys are preserved in the new keyword list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.take([a: 1, b: 2, c: 3], [:a, :c, :e])
|
||||
[a: 1, c: 3]
|
||||
iex> Keyword.take([a: 1, b: 2, c: 3, a: 5], [:a, :c, :e])
|
||||
[a: 1, c: 3, a: 5]
|
||||
Returns whether a given key exists in the given keywords.
|
||||
|
||||
### Examples
|
||||
Keyword.key?([a: 1], :a)
|
||||
#=> true
|
||||
Keyword.key?([a: 1], :b)
|
||||
#=> false
|
||||
"""
|
||||
def take(keywords, keys) when is_list(keywords) do
|
||||
:lists.filter(fn {k, _} -> k in keys end, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Drops the given keys from the keyword list.
|
||||
|
||||
Duplicated keys are preserved in the new keyword list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.drop([a: 1, b: 2, c: 3], [:b, :d])
|
||||
[a: 1, c: 3]
|
||||
iex> Keyword.drop([a: 1, b: 2, b: 3, c: 3, a: 5], [:b, :d])
|
||||
[a: 1, c: 3, a: 5]
|
||||
|
||||
"""
|
||||
def drop(keywords, keys) when is_list(keywords) do
|
||||
:lists.filter(fn {k, _} -> not k in keys end, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns and removes all values associated with `key` in the keyword list.
|
||||
|
||||
All duplicated keys are removed. See `pop_first/3` for
|
||||
removing only the first entry.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.pop([a: 1], :a)
|
||||
{1, []}
|
||||
iex> Keyword.pop([a: 1], :b)
|
||||
{nil, [a: 1]}
|
||||
iex> Keyword.pop([a: 1], :b, 3)
|
||||
{3, [a: 1]}
|
||||
iex> Keyword.pop([a: 1, a: 2], :a)
|
||||
{1, []}
|
||||
|
||||
"""
|
||||
@spec pop(t, key, value) :: {value, t}
|
||||
def pop(keywords, key, default \\ nil) when is_list(keywords) do
|
||||
case fetch(keywords, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(keywords, key)}
|
||||
:error ->
|
||||
{default, keywords}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily returns and removes all values associated with `key` in the keyword list.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
All duplicated keys are removed. See `pop_first/3` for
|
||||
removing only the first entry.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> keyword = [a: 1]
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 13
|
||||
...> end
|
||||
iex> Keyword.pop_lazy(keyword, :a, fun)
|
||||
{1, []}
|
||||
iex> Keyword.pop_lazy(keyword, :b, fun)
|
||||
{13, [a: 1]}
|
||||
|
||||
"""
|
||||
@spec pop_lazy(t, key, (() -> value)) :: {value, t}
|
||||
def pop_lazy(keywords, key, fun)
|
||||
when is_list(keywords) and is_function(fun, 0) do
|
||||
case fetch(keywords, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(keywords, key)}
|
||||
:error ->
|
||||
{fun.(), keywords}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns and removes the first value associated with `key` in the keyword list.
|
||||
|
||||
Duplicated keys are not removed.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.pop_first [a: 1], :a
|
||||
{1, []}
|
||||
iex> Keyword.pop_first [a: 1], :b
|
||||
{nil, [a: 1]}
|
||||
iex> Keyword.pop_first [a: 1], :b, 3
|
||||
{3, [a: 1]}
|
||||
iex> Keyword.pop_first [a: 1, a: 2], :a
|
||||
{1, [a: 2]}
|
||||
|
||||
"""
|
||||
@spec pop_first(t, key, value) :: {value, t}
|
||||
def pop_first(keywords, key, default \\ nil) when is_list(keywords) do
|
||||
case :lists.keytake(key, 1, keywords) do
|
||||
{:value, {^key, value}, rest} -> {value, rest}
|
||||
false -> {default, keywords}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the keyword list itself.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.to_list([a: 1])
|
||||
[a: 1]
|
||||
|
||||
"""
|
||||
def to_list(keyword) when is_list(keyword) do
|
||||
keyword
|
||||
end
|
||||
|
||||
# TODO: Deprecate by 1.3
|
||||
# TODO: Remove by 1.4
|
||||
@doc false
|
||||
def size(keyword) do
|
||||
length(keyword)
|
||||
end
|
||||
def key?([{k, _}|_], key) when key < k, do: false
|
||||
def key?([{k, _}|d], key) when key > k, do: key?(d, key)
|
||||
def key?([{_, _}|_], _key), do: true
|
||||
def key?([], _), do: false
|
||||
end
|
||||
|
||||
+282
-567
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
defprotocol List.Chars do
|
||||
@moduledoc ~S"""
|
||||
@moduledoc %B"""
|
||||
The List.Chars protocol is responsible for
|
||||
converting a structure to a list (only if applicable).
|
||||
The only function required to be implemented is
|
||||
@@ -9,48 +9,24 @@ defprotocol List.Chars do
|
||||
by Kernel invokes this protocol.
|
||||
"""
|
||||
|
||||
@only [BitString, List, Atom, Number, Record]
|
||||
|
||||
def to_char_list(thing)
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: Atom do
|
||||
def to_char_list(atom), do: Atom.to_char_list(atom)
|
||||
def to_char_list(atom), do: atom_to_list(atom)
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: BitString do
|
||||
@doc """
|
||||
Returns the given binary converted to a char list.
|
||||
"""
|
||||
def to_char_list(thing) when is_binary(thing) do
|
||||
String.to_char_list(thing)
|
||||
end
|
||||
|
||||
def to_char_list(thing) do
|
||||
raise Protocol.UndefinedError,
|
||||
protocol: @protocol,
|
||||
value: thing,
|
||||
description: "cannot convert a bitstring to a char list"
|
||||
end
|
||||
def to_char_list(bitstring), do: bitstring_to_list(bitstring)
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: List do
|
||||
def to_char_list(list), do: list
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: Integer do
|
||||
def to_char_list(thing) do
|
||||
Integer.to_char_list(thing)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: Float do
|
||||
@digits 20
|
||||
@limit :math.pow(10, @digits)
|
||||
|
||||
def to_char_list(thing) when thing > @limit do
|
||||
Float.to_char_list(thing, scientific: @digits)
|
||||
end
|
||||
|
||||
def to_char_list(thing) do
|
||||
Float.to_char_list(thing, compact: true, decimals: @digits)
|
||||
end
|
||||
end
|
||||
defimpl List.Chars, for: Number do
|
||||
def to_char_list(integer) when is_integer(integer), do: integer_to_list(integer)
|
||||
def to_char_list(float) when is_float(float), do: float_to_list(float)
|
||||
end
|
||||
+186
-970
File diff suppressed because it is too large
Load Diff
+68
-120
@@ -1,142 +1,90 @@
|
||||
# We generate this record using a raw module due to
|
||||
# bootstrap constraints. Notice the fields are not
|
||||
# represented by a keywords list because we want
|
||||
# to keep control over the order.
|
||||
defmodule Macro.Env do
|
||||
@moduledoc """
|
||||
A struct that holds compile time environment information.
|
||||
|
||||
The current environment can be accessed at any time as
|
||||
`__ENV__`. Inside macros, the caller environment can be
|
||||
accessed as `__CALLER__`.
|
||||
|
||||
An instance of `Macro.Env` must not be modified by hand. If you need to
|
||||
create a custom environment to pass to `Code.eval_quoted/3`, use the
|
||||
following trick:
|
||||
|
||||
def make_custom_env do
|
||||
import SomeModule, only: [some_function: 2]
|
||||
alias A.B.C
|
||||
__ENV__
|
||||
end
|
||||
|
||||
You may then call `make_custom_env()` to get a struct with the desired
|
||||
imports and aliases included.
|
||||
|
||||
It contains the following fields:
|
||||
|
||||
* `module` - the current module name
|
||||
* `file` - the current file name as a binary
|
||||
* `line` - the current line as an integer
|
||||
* `function` - a tuple as `{atom, integer}`, where the first
|
||||
element is the function name and the second its arity; returns
|
||||
`nil` if not inside a function
|
||||
* `context` - the context of the environment; it can be `nil`
|
||||
(default context), inside a guard or inside a match
|
||||
* `aliases` - a list of two-item tuples, where the first
|
||||
item is the aliased name and the second the actual name
|
||||
* `requires` - the list of required modules
|
||||
* `functions` - a list of functions imported from each module
|
||||
* `macros` - a list of macros imported from each module
|
||||
* `macro_aliases` - a list of aliases defined inside the current macro
|
||||
* `context_modules` - a list of modules defined in the current context
|
||||
* `vars` - a list keeping all defined variables as `{var, context}`
|
||||
* `export_vars` - a list keeping all variables to be exported in a
|
||||
construct (may be `nil`)
|
||||
* `lexical_tracker` - PID of the lexical tracker which is responsible for
|
||||
keeping user info
|
||||
* `local` - the module to expand local functions to
|
||||
@doc """
|
||||
A record that contains compile time environment information,
|
||||
It can be accessed at any time by calling __ENV__.
|
||||
"""
|
||||
|
||||
@type name_arity :: {atom, arity}
|
||||
@type file :: binary
|
||||
@type line :: non_neg_integer
|
||||
@type aliases :: [{module, module}]
|
||||
@type macro_aliases :: [{module, {integer, module}}]
|
||||
@type context :: :match | :guard | nil
|
||||
@type requires :: [module]
|
||||
@type functions :: [{module, [name_arity]}]
|
||||
@type macros :: [{module, [name_arity]}]
|
||||
@type context_modules :: [module]
|
||||
@type vars :: [{atom, atom | non_neg_integer}]
|
||||
@type export_vars :: vars | nil
|
||||
@type lexical_tracker :: pid
|
||||
@type local :: atom | nil
|
||||
def __access__(caller, args) do
|
||||
Record.access(caller, __MODULE__, __record__(:fields), args)
|
||||
end
|
||||
|
||||
@type t :: %{__struct__: __MODULE__,
|
||||
module: atom,
|
||||
file: file,
|
||||
line: line,
|
||||
function: name_arity | nil,
|
||||
context: context,
|
||||
requires: requires,
|
||||
aliases: aliases,
|
||||
functions: functions,
|
||||
macros: macros,
|
||||
macro_aliases: aliases,
|
||||
context_modules: context_modules,
|
||||
vars: vars,
|
||||
export_vars: export_vars,
|
||||
lexical_tracker: lexical_tracker,
|
||||
local: local}
|
||||
def __record__(kind, _), do: __record__(kind)
|
||||
def __record__(:name), do: Macro.Env
|
||||
|
||||
def __struct__ do
|
||||
%{__struct__: __MODULE__,
|
||||
module: nil,
|
||||
file: "nofile",
|
||||
line: 0,
|
||||
function: nil,
|
||||
context: nil,
|
||||
requires: [],
|
||||
aliases: [],
|
||||
functions: [],
|
||||
macros: [],
|
||||
macro_aliases: [],
|
||||
context_modules: [],
|
||||
vars: [],
|
||||
export_vars: nil,
|
||||
lexical_tracker: nil}
|
||||
# When adding removing new fields,
|
||||
# src/elixir_tree_helpers.erl needs to be changed as well.
|
||||
def __record__(:fields) do
|
||||
[
|
||||
{:module,nil},
|
||||
{:file,nil},
|
||||
{:line,nil},
|
||||
{:function,nil},
|
||||
{:aliases,nil},
|
||||
{:context,nil},
|
||||
{:requires,nil},
|
||||
{:macros,nil}
|
||||
]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a keyword list containing the file and line
|
||||
information as keys.
|
||||
Returns the current module name.
|
||||
"""
|
||||
@spec location(t) :: Keyword.t
|
||||
def location(env)
|
||||
def location(%{__struct__: Macro.Env, file: file, line: line}) do
|
||||
[file: file, line: line]
|
||||
end
|
||||
def module(record), do: elem(record, 2)
|
||||
|
||||
@doc """
|
||||
Returns whether the compilation environment is currently
|
||||
Returns the current file name as a binary.
|
||||
"""
|
||||
def file(record), do: elem(record, 3)
|
||||
|
||||
@doc """
|
||||
Returns the current line as an integer.
|
||||
"""
|
||||
def line(record), do: elem(record, 4)
|
||||
|
||||
@doc """
|
||||
Returns a tuple as { Atom, Integer }, where the first element
|
||||
is the function name and the seconds its arity. Returns `nil`
|
||||
if not inside a function.
|
||||
"""
|
||||
def function(record), do: elem(record, 5)
|
||||
|
||||
@doc """
|
||||
Returns a list of two item tuples, where the first
|
||||
item is the aliased name and the second the actual name.
|
||||
"""
|
||||
def aliases(record), do: elem(record, 6)
|
||||
|
||||
@doc """
|
||||
Returns wether the compilation environment is currently
|
||||
inside a guard.
|
||||
"""
|
||||
@spec in_guard?(t) :: boolean
|
||||
def in_guard?(env)
|
||||
def in_guard?(%{__struct__: Macro.Env, context: context}), do: context == :guard
|
||||
def in_guard?(record), do: elem(record, 7) == :guard
|
||||
|
||||
@doc """
|
||||
Returns whether the compilation environment is currently
|
||||
Returns wether the compilation environment is currently
|
||||
inside a match clause.
|
||||
"""
|
||||
@spec in_match?(t) :: boolean
|
||||
def in_match?(env)
|
||||
def in_match?(%{__struct__: Macro.Env, context: context}), do: context == :match
|
||||
def in_match?(record), do: elem(record, 7) == :assign
|
||||
|
||||
@doc """
|
||||
Returns the environment stacktrace.
|
||||
Returns the list of required modules.
|
||||
"""
|
||||
@spec stacktrace(t) :: list
|
||||
def stacktrace(%{__struct__: Macro.Env} = env) do
|
||||
cond do
|
||||
is_nil(env.module) ->
|
||||
[{:elixir_compiler, :__FILE__, 1, relative_location(env)}]
|
||||
is_nil(env.function) ->
|
||||
[{env.module, :__MODULE__, 0, relative_location(env)}]
|
||||
true ->
|
||||
{name, arity} = env.function
|
||||
[{env.module, name, arity, relative_location(env)}]
|
||||
end
|
||||
end
|
||||
def requires(record), do: elem(record, 8)
|
||||
|
||||
defp relative_location(env) do
|
||||
[file: Path.relative_to_cwd(env.file), line: env.line]
|
||||
@doc """
|
||||
Returns a list of macros imported from each module.
|
||||
"""
|
||||
def macros(record), do: elem(record, 9)
|
||||
|
||||
@doc """
|
||||
Returns a keywords list containing the file and line
|
||||
information as keys.
|
||||
"""
|
||||
def location(record) do
|
||||
[file: file(record), line: line(record)]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,607 +0,0 @@
|
||||
defmodule Map do
|
||||
@moduledoc """
|
||||
A set of functions for working with maps.
|
||||
|
||||
Maps are key-value stores where keys can be any value and
|
||||
are compared using the match operator (`===`). Maps can be
|
||||
created with the `%{}` special form defined in the
|
||||
`Kernel.SpecialForms` module.
|
||||
"""
|
||||
|
||||
@type key :: any
|
||||
@type value :: any
|
||||
@compile {:inline, fetch: 2, put: 3, delete: 2, has_key?: 2}
|
||||
|
||||
@doc """
|
||||
Returns all keys from the map.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.keys(%{a: 1, b: 2})
|
||||
[:a, :b]
|
||||
|
||||
"""
|
||||
@spec keys(map) :: [key]
|
||||
defdelegate keys(map), to: :maps
|
||||
|
||||
@doc """
|
||||
Returns all values from the map.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.values(%{a: 1, b: 2})
|
||||
[1, 2]
|
||||
|
||||
"""
|
||||
@spec values(map) :: [value]
|
||||
defdelegate values(map), to: :maps
|
||||
|
||||
@doc """
|
||||
Converts the map to a list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.to_list(%{a: 1})
|
||||
[a: 1]
|
||||
iex> Map.to_list(%{1 => 2})
|
||||
[{1, 2}]
|
||||
|
||||
"""
|
||||
@spec to_list(map) :: [{term, term}]
|
||||
defdelegate to_list(map), to: :maps
|
||||
|
||||
@doc """
|
||||
Returns a new empty map.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.new
|
||||
%{}
|
||||
|
||||
"""
|
||||
@spec new :: map
|
||||
def new, do: %{}
|
||||
|
||||
@doc """
|
||||
Creates a map from an enumerable.
|
||||
|
||||
Duplicated keys are removed; the latest one prevails.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.new([{:b, 1}, {:a, 2}])
|
||||
%{a: 2, b: 1}
|
||||
iex> Map.new([a: 1, a: 2, a: 3])
|
||||
%{a: 3}
|
||||
|
||||
"""
|
||||
@spec new(Enum.t) :: map
|
||||
def new(enumerable) do
|
||||
Enum.reduce(enumerable, %{}, fn {k, v}, acc -> put(acc, k, v) end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a map from an enumerable via the transformation function.
|
||||
|
||||
Duplicated entries are removed; the latest one prevails.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.new([:a, :b], fn x -> {x, x} end)
|
||||
%{a: :a, b: :b}
|
||||
|
||||
"""
|
||||
@spec new(Enum.t, (term -> {key, value})) :: map
|
||||
def new(enumerable, transform) do
|
||||
fun = fn el, acc ->
|
||||
{k, v} = transform.(el)
|
||||
put(acc, k, v)
|
||||
end
|
||||
Enum.reduce(enumerable, %{}, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns whether a given `key` exists in the given `map`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.has_key?(%{a: 1}, :a)
|
||||
true
|
||||
iex> Map.has_key?(%{a: 1}, :b)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec has_key?(map, key) :: boolean
|
||||
def has_key?(map, key), do: :maps.is_key(key, map)
|
||||
|
||||
@doc """
|
||||
Fetches the value for a specific `key` and returns it in a tuple.
|
||||
|
||||
If the `key` does not exist, returns `:error`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.fetch(%{a: 1}, :a)
|
||||
{:ok, 1}
|
||||
iex> Map.fetch(%{a: 1}, :b)
|
||||
:error
|
||||
|
||||
"""
|
||||
@spec fetch(map, key) :: {:ok, value} | :error
|
||||
def fetch(map, key), do: :maps.find(key, map)
|
||||
|
||||
@doc """
|
||||
Fetches the value for specific `key`.
|
||||
|
||||
If `key` does not exist, a `KeyError` is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.fetch!(%{a: 1}, :a)
|
||||
1
|
||||
iex> Map.fetch!(%{a: 1}, :b)
|
||||
** (KeyError) key :b not found in: %{a: 1}
|
||||
|
||||
"""
|
||||
@spec fetch!(map, key) :: value | no_return
|
||||
def fetch!(map, key) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> raise KeyError, key: key, term: map
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under `key` unless the entry `key`
|
||||
already exists.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.put_new(%{a: 1}, :b, 2)
|
||||
%{b: 2, a: 1}
|
||||
iex> Map.put_new(%{a: 1, b: 2}, :a, 3)
|
||||
%{a: 1, b: 2}
|
||||
|
||||
"""
|
||||
@spec put_new(map, key, value) :: map
|
||||
def put_new(map, key, value) do
|
||||
case has_key?(map, key) do
|
||||
true -> map
|
||||
false -> put(map, key, value)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates `fun` and puts the result under `key`
|
||||
in map unless `key` is already present.
|
||||
|
||||
This is useful if the value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = %{a: 1}
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 3
|
||||
...> end
|
||||
iex> Map.put_new_lazy(map, :a, fun)
|
||||
%{a: 1}
|
||||
iex> Map.put_new_lazy(map, :b, fun)
|
||||
%{a: 1, b: 3}
|
||||
|
||||
"""
|
||||
@spec put_new_lazy(map, key, (() -> value)) :: map
|
||||
def put_new_lazy(map, key, fun) when is_function(fun, 0) do
|
||||
case has_key?(map, key) do
|
||||
true -> map
|
||||
false -> put(map, key, fun.())
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Takes all entries corresponding to the given keys and
|
||||
returns them in a new map.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.take(%{a: 1, b: 2, c: 3}, [:a, :c, :e])
|
||||
%{a: 1, c: 3}
|
||||
|
||||
"""
|
||||
@spec take(map, [key]) :: map
|
||||
def take(map, keys) do
|
||||
Enum.reduce(keys, new, fn key, acc ->
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> put(acc, key, value)
|
||||
:error -> acc
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value for a specific `key`.
|
||||
|
||||
If `key` does not exist, return the default value
|
||||
(`nil` if no default value).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.get(%{}, :a)
|
||||
nil
|
||||
iex> Map.get(%{a: 1}, :a)
|
||||
1
|
||||
iex> Map.get(%{a: 1}, :b)
|
||||
nil
|
||||
iex> Map.get(%{a: 1}, :b, 3)
|
||||
3
|
||||
|
||||
"""
|
||||
@spec get(map, key) :: value
|
||||
@spec get(map, key, value) :: value
|
||||
def get(map, key, default \\ nil) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> default
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value for a specific `key`.
|
||||
|
||||
If `key` does not exist, lazily evaluates `fun` and returns its result.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = %{a: 1}
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 13
|
||||
...> end
|
||||
iex> Map.get_lazy(map, :a, fun)
|
||||
1
|
||||
iex> Map.get_lazy(map, :b, fun)
|
||||
13
|
||||
|
||||
"""
|
||||
@spec get_lazy(map, key, (() -> value)) :: value
|
||||
def get_lazy(map, key, fun) when is_function(fun, 0) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> fun.()
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.put(%{a: 1}, :b, 2)
|
||||
%{a: 1, b: 2}
|
||||
iex> Map.put(%{a: 1, b: 2}, :a, 3)
|
||||
%{a: 3, b: 2}
|
||||
|
||||
"""
|
||||
@spec put(map, key, value) :: map
|
||||
def put(map, key, val) do
|
||||
:maps.put(key, val, map)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the entries in the map for a specific `key`.
|
||||
|
||||
If the `key` does not exist, returns the map unchanged.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.delete(%{a: 1, b: 2}, :a)
|
||||
%{b: 2}
|
||||
iex> Map.delete(%{b: 2}, :a)
|
||||
%{b: 2}
|
||||
|
||||
"""
|
||||
@spec delete(map, key) :: map
|
||||
def delete(map, key), do: :maps.remove(key, map)
|
||||
|
||||
@doc """
|
||||
Merges two maps into one.
|
||||
|
||||
All keys in `map2` will be added to `map1`, overriding any existing one.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.merge(%{a: 1, b: 2}, %{a: 3, d: 4})
|
||||
%{a: 3, b: 2, d: 4}
|
||||
|
||||
"""
|
||||
@spec merge(map, map) :: map
|
||||
defdelegate merge(map1, map2), to: :maps
|
||||
|
||||
@doc """
|
||||
Merges two maps into one.
|
||||
|
||||
All keys in `map2` will be added to `map1`. The given function will
|
||||
be invoked with the key, value1 and value2 to solve conflicts.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.merge(%{a: 1, b: 2}, %{a: 3, d: 4}, fn _k, v1, v2 ->
|
||||
...> v1 + v2
|
||||
...> end)
|
||||
%{a: 4, b: 2, d: 4}
|
||||
|
||||
"""
|
||||
@spec merge(map, map, (key, value, value -> value)) :: map
|
||||
def merge(map1, map2, callback) do
|
||||
:maps.fold fn k, v2, acc ->
|
||||
update(acc, k, v2, fn(v1) -> callback.(k, v1, v2) end)
|
||||
end, map1, map2
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the `key` in `map` with the given function.
|
||||
|
||||
If the `key` does not exist, inserts the given `initial` value.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.update(%{a: 1}, :a, 13, &(&1 * 2))
|
||||
%{a: 2}
|
||||
iex> Map.update(%{a: 1}, :b, 11, &(&1 * 2))
|
||||
%{a: 1, b: 11}
|
||||
|
||||
"""
|
||||
@spec update(map, key, value, (value -> value)) :: map
|
||||
def update(map, key, initial, fun) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} ->
|
||||
put(map, key, fun.(value))
|
||||
:error ->
|
||||
put(map, key, initial)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns and removes all values associated with `key` in the `map`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.pop(%{a: 1}, :a)
|
||||
{1, %{}}
|
||||
iex> Map.pop(%{a: 1}, :b)
|
||||
{nil, %{a: 1}}
|
||||
iex> Map.pop(%{a: 1}, :b, 3)
|
||||
{3, %{a: 1}}
|
||||
|
||||
"""
|
||||
@spec pop(map, key, value) :: {value, map}
|
||||
def pop(map, key, default \\ nil) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> {value, delete(map, key)}
|
||||
:error -> {default, map}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily returns and removes all values associated with `key` in the `map`.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = %{a: 1}
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 13
|
||||
...> end
|
||||
iex> Map.pop_lazy(map, :a, fun)
|
||||
{1, %{}}
|
||||
iex> Map.pop_lazy(map, :b, fun)
|
||||
{13, %{a: 1}}
|
||||
|
||||
"""
|
||||
@spec pop_lazy(map, key, (() -> value)) :: {value, map}
|
||||
def pop_lazy(map, key, fun) when is_function(fun, 0) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> {value, delete(map, key)}
|
||||
:error -> {fun.(), map}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Drops the given keys from the map.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.drop(%{a: 1, b: 2, c: 3}, [:b, :d])
|
||||
%{a: 1, c: 3}
|
||||
|
||||
"""
|
||||
@spec drop(map, [key]) :: map
|
||||
def drop(map, keys) do
|
||||
Enum.reduce(keys, map, &delete(&2, &1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Takes all entries corresponding to the given keys and extracts them into a
|
||||
separate map.
|
||||
|
||||
Returns a tuple with the new map and the old map with removed keys.
|
||||
|
||||
Keys for which there are no entires in the map are ignored.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.split(%{a: 1, b: 2, c: 3}, [:a, :c, :e])
|
||||
{%{a: 1, c: 3}, %{b: 2}}
|
||||
|
||||
"""
|
||||
@spec split(map, [key]) :: {map, map}
|
||||
def split(map, keys) do
|
||||
Enum.reduce(keys, {new, map}, fn key, {inc, exc} = acc ->
|
||||
case fetch(exc, key) do
|
||||
{:ok, value} ->
|
||||
{put(inc, key, value), delete(exc, key)}
|
||||
:error ->
|
||||
acc
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the `key` with the given function.
|
||||
|
||||
If the `key` does not exist, raises `KeyError`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.update!(%{a: 1}, :a, &(&1 * 2))
|
||||
%{a: 2}
|
||||
|
||||
iex> Map.update!(%{a: 1}, :b, &(&1 * 2))
|
||||
** (KeyError) key :b not found
|
||||
|
||||
"""
|
||||
@spec update!(map, key, (value -> value)) :: map | no_return
|
||||
def update!(%{} = map, key, fun) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} ->
|
||||
put(map, key, fun.(value))
|
||||
:error ->
|
||||
:erlang.error({:badkey, key})
|
||||
end
|
||||
end
|
||||
|
||||
def update!(map, _key, _fun), do: :erlang.error({:badmap, map})
|
||||
|
||||
@doc """
|
||||
Gets the value from `key` and updates it, all in one pass.
|
||||
|
||||
This `fun` argument receives the value of `key` (or `nil` if `key`
|
||||
is not present) and must return a two-elements tuple: the "get" value (the
|
||||
retrieved value, which can be operated on before being returned) and the new
|
||||
value to be stored under `key`.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by `fun` and a
|
||||
new map with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.get_and_update(%{a: 1}, :a, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{1, %{a: "new value!"}}
|
||||
|
||||
iex> Map.get_and_update(%{a: 1}, :b, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{nil, %{b: "new value!", a: 1}}
|
||||
|
||||
"""
|
||||
@spec get_and_update(map, key, (value -> {get, value})) :: {get, map} when get: term
|
||||
def get_and_update(%{} = map, key, fun) do
|
||||
current_value = case :maps.find(key, map) do
|
||||
{:ok, value} -> value
|
||||
:error -> nil
|
||||
end
|
||||
|
||||
{get, update} = fun.(current_value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
end
|
||||
|
||||
def get_and_update(map, _key, _fun), do: :erlang.error({:badmap, map})
|
||||
|
||||
@doc """
|
||||
Gets the value from `key` and updates it. Raises if there is no `key`.
|
||||
|
||||
This `fun` argument receives the value of `key` and must return a
|
||||
two-elements tuple: the "get" value (the retrieved value, which can be
|
||||
operated on before being returned) and the new value to be stored under
|
||||
`key`.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by `fun` and a
|
||||
new map with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.get_and_update!(%{a: 1}, :a, fn(current_value) ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{1, %{a: "new value!"}}
|
||||
|
||||
iex> Map.get_and_update!(%{a: 1}, :b, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
** (KeyError) key :b not found
|
||||
|
||||
"""
|
||||
@spec get_and_update!(map, key, (value -> {get, value})) :: {get, map} | no_return when get: term
|
||||
def get_and_update!(%{} = map, key, fun) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, value} ->
|
||||
{get, update} = fun.(value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
:error ->
|
||||
:erlang.error({:badkey, key})
|
||||
end
|
||||
end
|
||||
|
||||
def get_and_update!(map, _key, _fun), do: :erlang.error({:badmap, map})
|
||||
|
||||
@doc """
|
||||
Converts a struct to map.
|
||||
|
||||
It accepts the struct module or a struct itself and
|
||||
simply removes the `__struct__` field from the struct.
|
||||
|
||||
## Example
|
||||
|
||||
defmodule User do
|
||||
defstruct [:name]
|
||||
end
|
||||
|
||||
Map.from_struct(User)
|
||||
#=> %{name: nil}
|
||||
|
||||
Map.from_struct(%User{name: "john"})
|
||||
#=> %{name: "john"}
|
||||
|
||||
"""
|
||||
@spec from_struct(atom | struct) :: map
|
||||
def from_struct(struct) when is_atom(struct) do
|
||||
:maps.remove(:__struct__, struct.__struct__)
|
||||
end
|
||||
|
||||
def from_struct(%{__struct__: _} = struct) do
|
||||
:maps.remove(:__struct__, struct)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two maps are equal.
|
||||
|
||||
Two maps are considered to be equal if they contain
|
||||
the same keys and those keys contain the same values.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.equal?(%{a: 1, b: 2}, %{b: 2, a: 1})
|
||||
true
|
||||
iex> Map.equal?(%{a: 1, b: 2}, %{b: 1, a: 2})
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(map, map) :: boolean
|
||||
def equal?(%{} = map1, %{} = map2), do: map1 === map2
|
||||
|
||||
# TODO: Deprecate by 1.3
|
||||
# TODO: Remove by 1.4
|
||||
@doc false
|
||||
def size(map) do
|
||||
map_size(map)
|
||||
end
|
||||
end
|
||||
@@ -1,289 +0,0 @@
|
||||
defmodule MapSet do
|
||||
@moduledoc """
|
||||
A set of functions for working with sets.
|
||||
|
||||
The `MapSet` is represented internally as a struct,
|
||||
therefore `%MapSet{}` can be used whenever there is a
|
||||
need to match on any `MapSet`. Note though the struct
|
||||
fields are private and must not be accessed directly.
|
||||
Instead, use the functions in this module.
|
||||
"""
|
||||
|
||||
@opaque t :: %__MODULE__{map: map}
|
||||
@type value :: term
|
||||
defstruct map: %{}
|
||||
|
||||
@doc """
|
||||
Returns a new set.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.new
|
||||
#MapSet<[]>
|
||||
|
||||
"""
|
||||
@spec new :: t
|
||||
def new(), do: %MapSet{}
|
||||
|
||||
@doc """
|
||||
Creates a set from an enumerable.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.new([:b, :a, 3])
|
||||
#MapSet<[3, :a, :b]>
|
||||
iex> MapSet.new([3, 3, 3, 2, 2, 1])
|
||||
#MapSet<[1, 2, 3]>
|
||||
|
||||
"""
|
||||
@spec new(Enum.t) :: t
|
||||
def new(enumerable) do
|
||||
Enum.reduce(enumerable, %MapSet{}, &put(&2, &1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a mapset from an enumerable via the transformation function.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.new([1, 2, 1], fn x -> 2 * x end)
|
||||
#MapSet<[2, 4]>
|
||||
|
||||
"""
|
||||
@spec new(Enum.t, (term -> term)) :: t
|
||||
def new(enumerable, transform) do
|
||||
Enum.reduce(enumerable, %MapSet{}, &put(&2, transform.(&1)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes `value` from `set`.
|
||||
|
||||
Returns a new set which is a copy of `set` but without `value`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> set = MapSet.new([1, 2, 3])
|
||||
iex> MapSet.delete(set, 4)
|
||||
#MapSet<[1, 2, 3]>
|
||||
iex> MapSet.delete(set, 2)
|
||||
#MapSet<[1, 3]>
|
||||
|
||||
"""
|
||||
@spec delete(t, value) :: t
|
||||
def delete(%MapSet{map: map} = set, term) do
|
||||
%{set | map: Map.delete(map, term)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a set that is `set1` without the members of `set2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.difference(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
|
||||
#MapSet<[1]>
|
||||
|
||||
"""
|
||||
@spec difference(t, t) :: t
|
||||
def difference(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
map = :maps.fold(fn value, _, acc ->
|
||||
Map.delete(acc, value)
|
||||
end, map1, map2)
|
||||
%MapSet{map: map}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if `set1` and `set2` have no members in common.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.disjoint?(MapSet.new([1, 2]), MapSet.new([3, 4]))
|
||||
true
|
||||
iex> MapSet.disjoint?(MapSet.new([1, 2]), MapSet.new([2, 3]))
|
||||
false
|
||||
|
||||
"""
|
||||
@spec disjoint?(t, t) :: boolean
|
||||
def disjoint?(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
if map_size(map1) > map_size(map2), do: {map1, map2} = {map2, map1}
|
||||
:maps.fold(fn value, _, _ ->
|
||||
if Map.has_key?(map2, value) do
|
||||
throw({:halt, false})
|
||||
else
|
||||
true
|
||||
end
|
||||
end, true, map1)
|
||||
catch
|
||||
{:halt, false} -> false
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two sets are equal.
|
||||
|
||||
The comparison between elements must be done using `===`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.equal?(MapSet.new([1, 2]), MapSet.new([2, 1, 1]))
|
||||
true
|
||||
iex> MapSet.equal?(MapSet.new([1, 2]), MapSet.new([3, 4]))
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(t, t) :: boolean
|
||||
def equal?(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
Map.equal?(map1, map2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a set containing only members that `set1` and `set2` have in common.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.intersection(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
|
||||
#MapSet<[2]>
|
||||
|
||||
iex> MapSet.intersection(MapSet.new([1, 2]), MapSet.new([3, 4]))
|
||||
#MapSet<[]>
|
||||
|
||||
"""
|
||||
@spec intersection(t, t) :: t
|
||||
def intersection(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
if map_size(map1) > map_size(map2), do: {map1, map2} = {map2, map1}
|
||||
map = :maps.fold(fn value, _, acc ->
|
||||
if Map.has_key?(map2, value) do
|
||||
Map.put(acc, value, true)
|
||||
else
|
||||
acc
|
||||
end
|
||||
end, %{}, map1)
|
||||
%MapSet{map: map}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if `set` contains `value`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.member?(MapSet.new([1, 2, 3]), 2)
|
||||
true
|
||||
iex> MapSet.member?(MapSet.new([1, 2, 3]), 4)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec member?(t, value) :: boolean
|
||||
def member?(%MapSet{map: map}, value) do
|
||||
Map.has_key?(map, value)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inserts `value` into `set` if `set` doesn't already contain it.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.put(MapSet.new([1, 2, 3]), 3)
|
||||
#MapSet<[1, 2, 3]>
|
||||
iex> MapSet.put(MapSet.new([1, 2, 3]), 4)
|
||||
#MapSet<[1, 2, 3, 4]>
|
||||
|
||||
"""
|
||||
@spec put(t, value) :: t
|
||||
def put(%MapSet{map: map} = set, value) do
|
||||
%{set | map: Map.put(map, value, true)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the number of elements in `set`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.size(MapSet.new([1, 2, 3]))
|
||||
3
|
||||
|
||||
"""
|
||||
@spec size(t) :: non_neg_integer
|
||||
def size(%MapSet{map: map}) do
|
||||
map_size(map)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if `set1`'s members are all contained in `set2`.
|
||||
|
||||
This function checks if `set1` is a subset of `set2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.subset?(MapSet.new([1, 2]), MapSet.new([1, 2, 3]))
|
||||
true
|
||||
iex> MapSet.subset?(MapSet.new([1, 2, 3]), MapSet.new([1, 2]))
|
||||
false
|
||||
|
||||
"""
|
||||
@spec subset?(t, t) :: boolean
|
||||
def subset?(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
if map_size(map1) <= map_size(map2) do
|
||||
:maps.fold(fn value, _, _ ->
|
||||
if Map.has_key?(map2, value) do
|
||||
true
|
||||
else
|
||||
throw({:halt, false})
|
||||
end
|
||||
end, true, map1)
|
||||
else
|
||||
false
|
||||
end
|
||||
catch
|
||||
{:halt, false} -> false
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts `set` to a list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.to_list(MapSet.new([1, 2, 3]))
|
||||
[1, 2, 3]
|
||||
|
||||
"""
|
||||
@spec to_list(t) :: list
|
||||
def to_list(%MapSet{map: map}) do
|
||||
Map.keys(map)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a set containing all members of `set1` and `set2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.union(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
|
||||
#MapSet<[1, 2, 3, 4]>
|
||||
|
||||
"""
|
||||
@spec union(t, t) :: t
|
||||
def union(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
%MapSet{map: Map.merge(map1, map2)}
|
||||
end
|
||||
|
||||
defimpl Enumerable do
|
||||
def reduce(set, acc, fun), do: Enumerable.List.reduce(MapSet.to_list(set), acc, fun)
|
||||
def member?(set, val), do: {:ok, MapSet.member?(set, val)}
|
||||
def count(set), do: {:ok, MapSet.size(set)}
|
||||
end
|
||||
|
||||
defimpl Collectable do
|
||||
def into(original) do
|
||||
{original, fn
|
||||
set, {:cont, x} -> MapSet.put(set, x)
|
||||
set, :done -> set
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(set, opts) do
|
||||
concat ["#MapSet<", Inspect.List.inspect(MapSet.to_list(set), opts), ">"]
|
||||
end
|
||||
end
|
||||
end
|
||||
+252
-806
File diff suppressed because it is too large
Load Diff
@@ -1,360 +0,0 @@
|
||||
# This is an Elixir module responsible for tracking
|
||||
# calls in order to extract Elixir modules' behaviour
|
||||
# during compilation time.
|
||||
#
|
||||
# ## Implementation
|
||||
#
|
||||
# The implementation uses the digraph module to track
|
||||
# all dependencies. The graph starts with one main vertex:
|
||||
#
|
||||
# * `:local` - points to local functions
|
||||
#
|
||||
# We can also have the following vertices:
|
||||
#
|
||||
# * `Module` - a module that was invoked via an import
|
||||
# * `{name, arity}` - a local function/arity pair
|
||||
# * `{:import, name, arity}` - an invoked function/arity import
|
||||
#
|
||||
# Each of those vertices can associate to other vertices
|
||||
# as described below:
|
||||
#
|
||||
# * `Module`
|
||||
# * in neighbours: `{:import, name, arity}`
|
||||
#
|
||||
# * `{name, arity}`
|
||||
# * in neighbours: `:local`, `{name, arity}`
|
||||
# * out neighbours: `{:import, name, arity}`
|
||||
#
|
||||
# * `{:import, name, arity}`
|
||||
# * in neighbours: `{name, arity}`
|
||||
# * out neighbours: `Module`
|
||||
#
|
||||
# Note that since this is required for bootstrap, we can't use
|
||||
# any of the `GenServer` conveniences.
|
||||
defmodule Module.LocalsTracker do
|
||||
@moduledoc false
|
||||
|
||||
@timeout 30_000
|
||||
@behaviour :gen_server
|
||||
|
||||
@type ref :: pid | module
|
||||
@type name :: atom
|
||||
@type name_arity :: {name, arity}
|
||||
|
||||
@type local :: {name, arity}
|
||||
@type import :: {:import, name, arity}
|
||||
|
||||
# Public API
|
||||
|
||||
@doc """
|
||||
Returns all imported modules that had the given
|
||||
`{name, arity}` invoked.
|
||||
"""
|
||||
@spec imports_with_dispatch(ref, name_arity) :: [module]
|
||||
def imports_with_dispatch(ref, {name, arity}) do
|
||||
d = :gen_server.call(to_pid(ref), :digraph, @timeout)
|
||||
:digraph.out_neighbours(d, {:import, name, arity})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all locals that are reachable.
|
||||
|
||||
By default, all public functions are reachable.
|
||||
A private function is only reachable if it has
|
||||
a public function that it invokes directly.
|
||||
"""
|
||||
@spec reachable(ref) :: [local]
|
||||
def reachable(ref) do
|
||||
reachable_from(:gen_server.call(to_pid(ref), :digraph, @timeout), :local)
|
||||
end
|
||||
|
||||
defp reachable_from(d, starting) do
|
||||
:sets.to_list(reduce_reachable(d, starting, :sets.new))
|
||||
end
|
||||
|
||||
defp reduce_reachable(d, vertex, vertices) do
|
||||
neighbours = :digraph.out_neighbours(d, vertex)
|
||||
neighbours = (for {_, _} = t <- neighbours, do: t) |> :sets.from_list
|
||||
remaining = :sets.subtract(neighbours, vertices)
|
||||
vertices = :sets.union(neighbours, vertices)
|
||||
:sets.fold(&reduce_reachable(d, &1, &2), vertices, remaining)
|
||||
end
|
||||
|
||||
defp to_pid(pid) when is_pid(pid), do: pid
|
||||
defp to_pid(mod) when is_atom(mod) do
|
||||
table = :elixir_module.data_table(mod)
|
||||
[{_, val}] = :ets.lookup(table, {:elixir, :locals_tracker})
|
||||
val
|
||||
end
|
||||
|
||||
# Internal API
|
||||
|
||||
# Starts the tracker and returns its pid.
|
||||
@doc false
|
||||
def start_link do
|
||||
:gen_server.start_link(__MODULE__, [], [])
|
||||
end
|
||||
|
||||
# Adds a definition into the tracker. A public
|
||||
# definition is connected with the :local node
|
||||
# while a private one is left unreachable until
|
||||
# a call is made to.
|
||||
@doc false
|
||||
def add_definition(pid, kind, tuple) when kind in [:def, :defp, :defmacro, :defmacrop] do
|
||||
:gen_server.cast(pid, {:add_definition, kind, tuple})
|
||||
end
|
||||
|
||||
# Adds and tracks defaults for a definition into the tracker.
|
||||
@doc false
|
||||
def add_defaults(pid, kind, tuple, defaults) when kind in [:def, :defp, :defmacro, :defmacrop] do
|
||||
:gen_server.cast(pid, {:add_defaults, kind, tuple, defaults})
|
||||
end
|
||||
|
||||
# Adds a local dispatch to the given target.
|
||||
def add_local(pid, to) when is_tuple(to) do
|
||||
:gen_server.cast(pid, {:add_local, :local, to})
|
||||
end
|
||||
|
||||
# Adds a local dispatch from-to the given target.
|
||||
@doc false
|
||||
def add_local(pid, from, to) when is_tuple(from) and is_tuple(to) do
|
||||
:gen_server.cast(pid, {:add_local, from, to})
|
||||
end
|
||||
|
||||
# Adds a import dispatch to the given target.
|
||||
@doc false
|
||||
def add_import(pid, function, module, target) when is_atom(module) and is_tuple(target) do
|
||||
:gen_server.cast(pid, {:add_import, function, module, target})
|
||||
end
|
||||
|
||||
# Yanks a local node. Returns its in and out vertices in a tuple.
|
||||
@doc false
|
||||
def yank(pid, local) do
|
||||
:gen_server.call(to_pid(pid), {:yank, local}, @timeout)
|
||||
end
|
||||
|
||||
# Reattach a previously yanked node
|
||||
@doc false
|
||||
def reattach(pid, kind, tuple, neighbours) do
|
||||
:gen_server.cast(to_pid(pid), {:reattach, kind, tuple, neighbours})
|
||||
end
|
||||
|
||||
# Collecting all conflicting imports with the given functions
|
||||
@doc false
|
||||
def collect_imports_conflicts(pid, all_defined) do
|
||||
d = :gen_server.call(pid, :digraph, @timeout)
|
||||
|
||||
for {name, arity} <- all_defined,
|
||||
:digraph.in_neighbours(d, {:import, name, arity}) != [],
|
||||
n = :digraph.out_neighbours(d, {:import, name, arity}),
|
||||
n != [] do
|
||||
{n, name, arity}
|
||||
end
|
||||
end
|
||||
|
||||
# Collect all unused definitions based on the private
|
||||
# given also accounting the expected amount of default
|
||||
# clauses a private function have.
|
||||
@doc false
|
||||
def collect_unused_locals(ref, private) do
|
||||
d = :gen_server.call(to_pid(ref), :digraph, @timeout)
|
||||
{unreachable(d, private), collect_warnings(d, private)}
|
||||
end
|
||||
|
||||
defp unreachable(d, private) do
|
||||
unreachable = for {tuple, _, _} <- private, do: tuple
|
||||
|
||||
private =
|
||||
for {tuple, :defp, _} <- private do
|
||||
neighbours = :digraph.in_neighbours(d, tuple)
|
||||
neighbours = for {_, _} = t <- neighbours, do: t
|
||||
{tuple, :sets.from_list(neighbours)}
|
||||
end
|
||||
|
||||
reduce_unreachable(private, [], :sets.from_list(unreachable))
|
||||
end
|
||||
|
||||
defp reduce_unreachable([{vertex, callers}|t], acc, unreachable) do
|
||||
if :sets.is_subset(callers, unreachable) do
|
||||
reduce_unreachable(t, [{vertex, callers}|acc], unreachable)
|
||||
else
|
||||
reduce_unreachable(acc ++ t, [], :sets.del_element(vertex, unreachable))
|
||||
end
|
||||
end
|
||||
|
||||
defp reduce_unreachable([], _acc, unreachable) do
|
||||
:sets.to_list(unreachable)
|
||||
end
|
||||
|
||||
defp collect_warnings(d, private) do
|
||||
reachable = reachable_from(d, :local)
|
||||
:lists.foldl(&collect_warnings(&1, &2, reachable), [], private)
|
||||
end
|
||||
|
||||
defp collect_warnings({tuple, kind, 0}, acc, reachable) do
|
||||
if :lists.member(tuple, reachable) do
|
||||
acc
|
||||
else
|
||||
[{:unused_def, tuple, kind}|acc]
|
||||
end
|
||||
end
|
||||
|
||||
defp collect_warnings({tuple, kind, default}, acc, reachable) when default > 0 do
|
||||
{name, arity} = tuple
|
||||
min = arity - default
|
||||
max = arity
|
||||
|
||||
invoked = for {n, a} <- reachable, n == name, a in min..max, do: a
|
||||
|
||||
if invoked == [] do
|
||||
[{:unused_def, tuple, kind}|acc]
|
||||
else
|
||||
case :lists.min(invoked) - min do
|
||||
0 -> acc
|
||||
^default -> [{:unused_args, tuple}|acc]
|
||||
unused_args -> [{:unused_args, tuple, unused_args}|acc]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def cache_env(pid, env) do
|
||||
:gen_server.call(pid, {:cache_env, env}, @timeout)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def get_cached_env(pid, ref) do
|
||||
:gen_server.call(pid, {:get_cached_env, ref}, @timeout)
|
||||
end
|
||||
|
||||
# Stops the gen server
|
||||
@doc false
|
||||
def stop(pid) do
|
||||
:gen_server.cast(pid, :stop)
|
||||
end
|
||||
|
||||
# Callbacks
|
||||
|
||||
def init([]) do
|
||||
d = :digraph.new([:protected])
|
||||
:digraph.add_vertex(d, :local)
|
||||
{:ok, {d, []}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_call({:cache_env, env}, _from, {d, cache}) do
|
||||
case cache do
|
||||
[{i, ^env}|_] ->
|
||||
{:reply, i, {d, cache}}
|
||||
t ->
|
||||
i = length(t)
|
||||
{:reply, i, {d, [{i, env}|t]}}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_call({:get_cached_env, ref}, _from, {_, cache} = state) do
|
||||
{^ref, env} = :lists.keyfind(ref, 1, cache)
|
||||
{:reply, env, state}
|
||||
end
|
||||
|
||||
def handle_call({:yank, local}, _from, {d, _} = state) do
|
||||
out_vertices = :digraph.out_neighbours(d, local)
|
||||
:digraph.del_edges(d, :digraph.out_edges(d, local))
|
||||
{:reply, {[], out_vertices}, state}
|
||||
end
|
||||
|
||||
def handle_call(:digraph, _from, {d, _} = state) do
|
||||
{:reply, d, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(_msg, state) do
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_cast({:add_local, from, to}, {d, _} = state) do
|
||||
handle_add_local(d, from, to)
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_cast({:add_import, function, module, {name, arity}}, {d, _} = state) do
|
||||
handle_import(d, function, module, name, arity)
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_cast({:add_definition, kind, tuple}, {d, _} = state) do
|
||||
handle_add_definition(d, kind, tuple)
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_cast({:add_defaults, kind, {name, arity}, defaults}, {d, _} = state) do
|
||||
for i <- :lists.seq(arity - defaults, arity - 1) do
|
||||
handle_add_definition(d, kind, {name, i})
|
||||
handle_add_local(d, {name, i}, {name, i + 1})
|
||||
end
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_cast({:reattach, _kind, tuple, {in_neigh, out_neigh}}, {d, _} = state) do
|
||||
for from <- in_neigh do
|
||||
:digraph.add_vertex(d, from)
|
||||
replace_edge!(d, from, tuple)
|
||||
end
|
||||
|
||||
for to <- out_neigh do
|
||||
:digraph.add_vertex(d, to)
|
||||
replace_edge!(d, tuple, to)
|
||||
end
|
||||
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_cast(:stop, state) do
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def terminate(_reason, _state) do
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc false
|
||||
def code_change(_old, state, _extra) do
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
defp handle_import(d, function, module, name, arity) do
|
||||
:digraph.add_vertex(d, module)
|
||||
|
||||
tuple = {:import, name, arity}
|
||||
:digraph.add_vertex(d, tuple)
|
||||
replace_edge!(d, tuple, module)
|
||||
|
||||
if function != nil do
|
||||
replace_edge!(d, function, tuple)
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
defp handle_add_local(d, from, to) do
|
||||
:digraph.add_vertex(d, to)
|
||||
replace_edge!(d, from, to)
|
||||
end
|
||||
|
||||
defp handle_add_definition(d, public, tuple) when public in [:def, :defmacro] do
|
||||
:digraph.add_vertex(d, tuple)
|
||||
replace_edge!(d, :local, tuple)
|
||||
end
|
||||
|
||||
defp handle_add_definition(d, private, tuple) when private in [:defp, :defmacrop] do
|
||||
:digraph.add_vertex(d, tuple)
|
||||
end
|
||||
|
||||
defp replace_edge!(d, from, to) do
|
||||
_ = unless :lists.member(to, :digraph.out_neighbours(d, from)) do
|
||||
[:"$e"|_] = :digraph.add_edge(d, from, to)
|
||||
end
|
||||
:ok
|
||||
end
|
||||
end
|
||||
+41
-192
@@ -1,267 +1,116 @@
|
||||
defmodule Node do
|
||||
@moduledoc """
|
||||
Functions related to VM nodes.
|
||||
|
||||
Some of the functions in this module are inlined by the compiler,
|
||||
similar to functions in the `Kernel` module and they are explicitly
|
||||
marked in their docs as "inlined by the compiler". For more information
|
||||
about inlined functions, check out the `Kernel` module.
|
||||
Functions related to Erlang nodes.
|
||||
"""
|
||||
|
||||
@type t :: node
|
||||
|
||||
@doc """
|
||||
Turns a non-distributed node into a distributed node.
|
||||
|
||||
This functionality starts the `:net_kernel` and other
|
||||
related processes.
|
||||
Returns the current node. It returns the same as the built-in node().
|
||||
"""
|
||||
@spec start(node, :longnames | :shortnames, non_neg_integer) ::
|
||||
{:ok, pid} | {:error, term}
|
||||
def start(name, type \\ :longnames, tick_time \\ 15000) do
|
||||
:net_kernel.start([name, type, tick_time])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Turns a distributed node into a non-distributed node.
|
||||
|
||||
For other nodes in the network, this is the same as the node going down.
|
||||
Only possible when the node was started with `Node.start/3`, otherwise
|
||||
returns `{:error, :not_allowed}`. Returns `{:error, :not_found}` if the
|
||||
local node is not alive.
|
||||
"""
|
||||
@spec stop() :: :ok | {:error, :not_allowed | :not_found}
|
||||
def stop() do
|
||||
:net_kernel.stop()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current node.
|
||||
|
||||
It returns the same as the built-in `node()`.
|
||||
"""
|
||||
@spec self :: t
|
||||
def self do
|
||||
:erlang.node()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the local node is alive.
|
||||
|
||||
That is, if the node can be part of a distributed system.
|
||||
Returns true if the local node is alive; that is, if the node can be
|
||||
part of a distributed system. Otherwise, it returns false.
|
||||
"""
|
||||
@spec alive? :: boolean
|
||||
def alive? do
|
||||
:erlang.is_alive()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of all visible nodes in the system, excluding
|
||||
the local node.
|
||||
|
||||
Same as `list(:visible)`.
|
||||
the local node. Same as list(visible).
|
||||
"""
|
||||
@spec list :: [t]
|
||||
def list do
|
||||
:erlang.nodes()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of nodes according to argument given.
|
||||
|
||||
The result returned when the argument is a list, is the list of nodes
|
||||
Returns a list of nodes according to argument given. The result
|
||||
returned when the argument is a list, is the list of nodes
|
||||
satisfying the disjunction(s) of the list elements.
|
||||
|
||||
For more information, see
|
||||
[`:erlang.nodes/1`](http://www.erlang.org/doc/man/erlang.html#nodes-1).
|
||||
|
||||
See http://www.erlang.org/doc/man/erlang.html#nodes-1 for more info.
|
||||
"""
|
||||
@typep state :: :visible | :hidden | :connected | :this | :known
|
||||
@spec list(state | [state]) :: [t]
|
||||
def list(args) do
|
||||
:erlang.nodes(args)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Monitors the status of the node.
|
||||
Monitors the status of the node. If flag is true, monitoring is
|
||||
turned on. If flag is false, monitoring is turned off.
|
||||
|
||||
If `flag` is `true`, monitoring is turned on.
|
||||
If `flag` is `false`, monitoring is turned off.
|
||||
|
||||
For more information, see
|
||||
[`:erlang.monitor_node/2`](http://www.erlang.org/doc/man/erlang.html#monitor_node-2).
|
||||
See http://www.erlang.org/doc/man/erlang.html#monitor_node-2 for more info.
|
||||
"""
|
||||
@spec monitor(t, boolean) :: true
|
||||
def monitor(node, flag) do
|
||||
:erlang.monitor_node(node, flag)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Behaves as `monitor/2` except that it allows an extra
|
||||
option to be given, namely `:allow_passive_connect`.
|
||||
Behaves as monitor_node/2 except that it allows an extra
|
||||
option to be given, namely :allow_passive_connect.
|
||||
|
||||
For more information, see
|
||||
[`:erlang.monitor_node/3`](http://www.erlang.org/doc/man/erlang.html#monitor_node-3).
|
||||
See http://www.erlang.org/doc/man/erlang.html#monitor_node-3 for more info.
|
||||
"""
|
||||
@spec monitor(t, boolean, [:allow_passive_connect]) :: true
|
||||
def monitor(node, flag, options) do
|
||||
:erlang.monitor_node(node, flag, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Tries to set up a connection to node.
|
||||
|
||||
Returns `:pang` if it fails, or `:pong` if it is successful.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Node.ping(:unknown_node)
|
||||
:pang
|
||||
|
||||
Forces the disconnection of a node. This will appear to the `node` as if
|
||||
the local node has crashed. This BIF is mainly used in the Erlang network
|
||||
authentication protocols. Returns true if disconnection succeeds, otherwise
|
||||
false. If the local node is not alive, the function returns ignored.
|
||||
|
||||
See http://www.erlang.org/doc/man/erlang.html#disconnect_node-1 for more info.
|
||||
"""
|
||||
@spec ping(t) :: :pong | :pang
|
||||
def ping(node) do
|
||||
:net_adm.ping(node)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Forces the disconnection of a node.
|
||||
|
||||
This will appear to the `node` as if the local node has crashed.
|
||||
This function is mainly used in the Erlang network authentication
|
||||
protocols. Returns `true` if disconnection succeeds, otherwise `false`.
|
||||
If the local node is not alive, the function returns `:ignored`.
|
||||
|
||||
For more information, see
|
||||
[`:erlang.disconnect_node/1`](http://www.erlang.org/doc/man/erlang.html#disconnect_node-1).
|
||||
"""
|
||||
@spec disconnect(t) :: boolean | :ignored
|
||||
def disconnect(node) do
|
||||
:erlang.disconnect_node(node)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Establishes a connection to `node`.
|
||||
|
||||
Returns `true` if successful, `false` if not, and the atom
|
||||
`:ignored` if the local node is not alive.
|
||||
|
||||
For more information, see
|
||||
[`:erlang.connect_node/1`](http://www.erlang.org/doc/man/net_kernel.html#connect_node-1).
|
||||
"""
|
||||
@spec connect(t) :: boolean | :ignored
|
||||
def connect(node) do
|
||||
:net_kernel.connect_node(node)
|
||||
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.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn/2`](http://www.erlang.org/doc/man/erlang.html#spawn-2).
|
||||
|
||||
Inlined by the compiler.
|
||||
Check http://www.erlang.org/doc/man/erlang.html#spawn_opt-4 for
|
||||
the list of available options.
|
||||
"""
|
||||
@spec spawn(t, (() -> any)) :: pid
|
||||
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.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn_opt/3`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-3).
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec spawn(t, (() -> any), Process.spawn_opts) :: pid | {pid, reference}
|
||||
def spawn(node, fun, opts) do
|
||||
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`.
|
||||
`module.function(args)` on `node`. If `node` does not exists, a useless
|
||||
pid is returned.
|
||||
|
||||
If `node` does not exist, a useless pid is returned.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn/4`](http://www.erlang.org/doc/man/erlang.html#spawn-4).
|
||||
|
||||
Inlined by the compiler.
|
||||
Check http://www.erlang.org/doc/man/erlang.html#spawn_opt-4 for
|
||||
the list of available options.
|
||||
"""
|
||||
@spec spawn(t, module, atom, [any]) :: pid
|
||||
def spawn(node, module, fun, args) do
|
||||
:erlang.spawn(node, module, fun, args)
|
||||
def spawn(node, module, fun, args, opts // []) do
|
||||
:erlang.spawn(node, module, fun, args, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the pid of a new process started by the application of
|
||||
`module.function(args)` on `node`.
|
||||
|
||||
If `node` does not exist, a useless pid is returned.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn/5`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-5).
|
||||
|
||||
Inlined by the compiler.
|
||||
Returns the pid of a new process started by the application of `fun`
|
||||
on `node`. A link is created between the calling process and the
|
||||
new process, atomically. If `node` does not exist, a useless pid is returned
|
||||
(and due to the link, an exit signal with exit reason :noconnection will be
|
||||
received).
|
||||
"""
|
||||
@spec spawn(t, module, atom, [any], Process.spawn_opts) :: pid | {pid, reference}
|
||||
def spawn(node, module, fun, args, opts) do
|
||||
:erlang.spawn_opt(node, module, fun, args, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the pid of a new linked process started by the application of `fun` on `node`.
|
||||
|
||||
A link is created between the calling process and the new process, atomically.
|
||||
If `node` does not exist, a useless pid is returned (and due to the link, an exit
|
||||
signal with exit reason `:noconnection` will be received).
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec spawn_link(t, (() -> any)) :: pid
|
||||
def spawn_link(node, fun) do
|
||||
:erlang.spawn_link(node, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the pid of a new linked process started by the application of
|
||||
`module.function(args)` on `node`.
|
||||
|
||||
A link is created between the calling process and the new process, atomically.
|
||||
If `node` does not exist, a useless pid is returned (and due to the link, an exit
|
||||
signal with exit reason `:noconnection` will be received).
|
||||
|
||||
Inlined by the compiler.
|
||||
Returns the pid of a new process started by the application of
|
||||
`module.function(args)` on `node`. A link is created between the calling
|
||||
process and the new process, atomically. If `node` does not exist, a useless
|
||||
pid is returned (and due to the link, an exit signal with exit reason
|
||||
:noconnection will be received).
|
||||
"""
|
||||
@spec spawn_link(t, module, atom, [any]) :: pid
|
||||
def spawn_link(node, module, fun, args) do
|
||||
:erlang.spawn_link(node, module, fun, args)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sets the magic cookie of `node` to the atom `cookie`.
|
||||
|
||||
The default node is `Node.self/0`, the local node. If `node` is the local node,
|
||||
the function also sets the cookie of all other unknown nodes to `cookie`.
|
||||
|
||||
This function will raise `FunctionClauseError` if the given `node` is not alive.
|
||||
"""
|
||||
def set_cookie(node \\ Node.self, cookie) when is_atom(cookie) do
|
||||
:erlang.set_cookie(node, cookie)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the magic cookie of the local node.
|
||||
|
||||
Returns the cookie if the node is alive, otherwise `:nocookie`.
|
||||
"""
|
||||
def get_cookie() do
|
||||
:erlang.get_cookie()
|
||||
end
|
||||
end
|
||||
end
|
||||
+87
-442
@@ -1,495 +1,140 @@
|
||||
defmodule OptionParser do
|
||||
@moduledoc """
|
||||
This module contains functions to parse command line options.
|
||||
"""
|
||||
|
||||
@type argv :: [String.t]
|
||||
@type parsed :: Keyword.t
|
||||
@type errors :: [{String.t, String.t | nil}]
|
||||
@type options :: [switches: Keyword.t, strict: Keyword.t, aliases: Keyword.t]
|
||||
|
||||
@doc """
|
||||
Parses `argv` into a keywords list.
|
||||
|
||||
It returns a three-element tuple as follows:
|
||||
|
||||
1. parsed switches,
|
||||
2. remaining arguments,
|
||||
3. invalid options.
|
||||
Parses the argv and returns one tuple with parsed options
|
||||
and the arguments.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> OptionParser.parse(["--debug"])
|
||||
{[debug: true], [], []}
|
||||
OptionParser.parse(["--debug"])
|
||||
#=> { [debug: true], [] }
|
||||
|
||||
iex> OptionParser.parse(["--source", "lib"])
|
||||
{[source: "lib"], [], []}
|
||||
OptionParser.parse(["--source", "lib"])
|
||||
#=> { [source: "lib"], [] }
|
||||
|
||||
iex> OptionParser.parse(["--source-path", "lib", "test/enum_test.exs", "--verbose"])
|
||||
{[source_path: "lib", verbose: true], ["test/enum_test.exs"], []}
|
||||
|
||||
By default, Elixir will try to automatically parse all switches.
|
||||
Switches followed by a value will be assigned the value, as a string.
|
||||
Switches without an argument, like `--debug` will automatically
|
||||
be set to `true`.
|
||||
|
||||
Note: Elixir also converts the switches to underscore atoms, so
|
||||
`--source-path` becomes `:source_path`, to better suit Elixir
|
||||
conventions. This means that option names on the command line cannot contain
|
||||
underscores; such options will be put in the invalid options list.
|
||||
|
||||
## Switch Definitions
|
||||
|
||||
Often it is better to explicitly list the known
|
||||
switches and their formats. The switches can be specified via two
|
||||
alternative options:
|
||||
|
||||
* `:switches` - defines some switches. An attempt is still made to parse
|
||||
switches that do not appear in the list.
|
||||
|
||||
* `:strict` - the switches are strict. Any switch that is not specified
|
||||
in the list is returned in the invalid options list.
|
||||
|
||||
Note that you should only supply the `:switches` or `:strict` option. If you
|
||||
supply both, an error will be raised.
|
||||
|
||||
For each switch, the following types are supported:
|
||||
|
||||
* `:boolean` - marks the given switch as a boolean. Boolean switches
|
||||
never consume the following value unless it is `true` or
|
||||
`false`.
|
||||
* `:integer` - parses the switch as an integer.
|
||||
* `:float` - parses the switch as a float.
|
||||
* `:string` - returns the switch as a string.
|
||||
|
||||
If a switch can't be parsed, it is returned in the invalid options list.
|
||||
|
||||
The following extra "types" are supported:
|
||||
|
||||
* `:keep` - keeps duplicated items in the list instead of overriding them.
|
||||
|
||||
Note: if you want to use `:keep` with a non-string type, use a list, e.g.
|
||||
`[foo: [:integer, :keep]]`.
|
||||
|
||||
Examples:
|
||||
|
||||
iex> OptionParser.parse(["--unlock", "path/to/file"], strict: [unlock: :boolean])
|
||||
{[unlock: true], ["path/to/file"], []}
|
||||
|
||||
iex> OptionParser.parse(["--unlock", "--limit", "0", "path/to/file"],
|
||||
...> strict: [unlock: :boolean, limit: :integer])
|
||||
{[unlock: true, limit: 0], ["path/to/file"], []}
|
||||
|
||||
iex> OptionParser.parse(["--limit", "3"], strict: [limit: :integer])
|
||||
{[limit: 3], [], []}
|
||||
|
||||
iex> OptionParser.parse(["--limit", "xyz"], strict: [limit: :integer])
|
||||
{[], [], [{"--limit", "xyz"}]}
|
||||
|
||||
iex> OptionParser.parse(["--unknown", "xyz"], strict: [])
|
||||
{[], ["xyz"], [{"--unknown", nil}]}
|
||||
|
||||
iex> OptionParser.parse(["--limit", "3", "--unknown", "xyz"],
|
||||
...> switches: [limit: :integer])
|
||||
{[limit: 3, unknown: "xyz"], [], []}
|
||||
|
||||
iex> OptionParser.parse(["--unlock", "path/to/file", "--unlock", "path/to/another/file"], strict: [unlock: :keep])
|
||||
{[unlock: "path/to/file", unlock: "path/to/another/file"], [], []}
|
||||
|
||||
## Negation switches
|
||||
|
||||
In case a switch is declared as boolean, it may be passed as `--no-SWITCH`
|
||||
which will set the option to `false`:
|
||||
|
||||
iex> OptionParser.parse(["--no-op", "path/to/file"], switches: [op: :boolean])
|
||||
{[op: false], ["path/to/file"], []}
|
||||
OptionParser.parse(["--source", "lib", "test/enum_test.exs", "--verbose"])
|
||||
#=> { [source: "lib", verbose: true], ["test/enum_test.exs"] }
|
||||
|
||||
## Aliases
|
||||
|
||||
A set of aliases can be given as options too:
|
||||
A set of aliases can be given as second argument:
|
||||
|
||||
iex> OptionParser.parse(["-d"], aliases: [d: :debug])
|
||||
{[debug: true], [], []}
|
||||
OptionParser.parse(["-d"], aliases: [d: :debug])
|
||||
#=> { [debug: true], [] }
|
||||
|
||||
## Flags
|
||||
|
||||
A set of flags can be given as argument too. Those are considered
|
||||
boolean and never consume the next value unless it is a boolean:
|
||||
|
||||
OptionParser.parse(["--unlock path/to/file"], flags: [:unlock])
|
||||
#=> { [unlock: true], ["path/to/file"] }
|
||||
|
||||
OptionParser.parse(["--unlock false path/to/file"], flags: [:unlock])
|
||||
#=> { [unlock: false], ["path/to/file"] }
|
||||
|
||||
## Negation switches
|
||||
|
||||
Any switches starting with `--no-` are always considered to be
|
||||
booleans and never parse the next value.
|
||||
|
||||
OptionParser.parse(["--no-op path/to/file"])
|
||||
#=> { [no_op: true], ["path/to/file"] }
|
||||
|
||||
"""
|
||||
@spec parse(argv, options) :: {parsed, argv, errors}
|
||||
def parse(argv, opts \\ []) when is_list(argv) and is_list(opts) do
|
||||
do_parse(argv, compile_config(opts), [], [], [], true)
|
||||
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)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Similar to `parse/2` but only parses the head of `argv`;
|
||||
as soon as it finds a non-switch, it stops parsing.
|
||||
Similar to parse but only parses the head of the argv.
|
||||
I.e. as soon as it finds a non switch, it stops parsing.
|
||||
|
||||
See `parse/2` for more information.
|
||||
Check `parse/2` for more info.
|
||||
|
||||
## Example
|
||||
|
||||
iex> OptionParser.parse_head(["--source", "lib", "test/enum_test.exs", "--verbose"])
|
||||
{[source: "lib"], ["test/enum_test.exs", "--verbose"], []}
|
||||
|
||||
iex> OptionParser.parse_head(["--verbose", "--source", "lib", "test/enum_test.exs", "--unlock"])
|
||||
{[verbose: true, source: "lib"], ["test/enum_test.exs", "--unlock"], []}
|
||||
OptionParser.parse_head(["--source", "lib", "test/enum_test.exs", "--verbose"])
|
||||
#=> { [source: "lib"], ["test/enum_test.exs", "--verbose"] }
|
||||
|
||||
"""
|
||||
@spec parse_head(argv, options) :: {parsed, argv, errors}
|
||||
def parse_head(argv, opts \\ []) when is_list(argv) and is_list(opts) do
|
||||
do_parse(argv, compile_config(opts), [], [], [], false)
|
||||
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)
|
||||
end
|
||||
|
||||
defp do_parse([], _config, opts, args, invalid, _all?) do
|
||||
{Enum.reverse(opts), Enum.reverse(args), Enum.reverse(invalid)}
|
||||
end
|
||||
|
||||
defp do_parse(argv, {aliases, switches, strict}=config, opts, args, invalid, all?) do
|
||||
case next(argv, aliases, switches, strict) do
|
||||
{:ok, option, value, rest} ->
|
||||
# the option exist and it was successfully parsed
|
||||
kinds = List.wrap Keyword.get(switches, option)
|
||||
new_opts = do_store_option(opts, option, value, kinds)
|
||||
do_parse(rest, config, new_opts, args, invalid, all?)
|
||||
|
||||
{:invalid, option, value, rest} ->
|
||||
# the option exist but it has wrong value
|
||||
do_parse(rest, config, opts, args, [{option, value}|invalid], all?)
|
||||
|
||||
{:undefined, option, _value, rest} ->
|
||||
# the option does not exist (for strict cases)
|
||||
do_parse(rest, config, opts, args, [{option, nil}|invalid], all?)
|
||||
|
||||
{:error, ["--"|rest]} ->
|
||||
{Enum.reverse(opts), Enum.reverse(args, rest), Enum.reverse(invalid)}
|
||||
|
||||
{:error, [arg|rest]=remaining_args} ->
|
||||
# there is no option
|
||||
if all? do
|
||||
do_parse(rest, config, opts, [arg|args], invalid, all?)
|
||||
else
|
||||
{Enum.reverse(opts), Enum.reverse(args, remaining_args), Enum.reverse(invalid)}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Low-level function that parses one option.
|
||||
|
||||
It accepts the same options as `parse/2` and `parse_head/2`
|
||||
as both functions are built on top of next. This function
|
||||
may return:
|
||||
|
||||
* `{:ok, key, value, rest}` - the option `key` with `value` was
|
||||
successfully parsed
|
||||
|
||||
* `{:invalid, key, value, rest}` - the option `key` is invalid with `value`
|
||||
(returned when the switch type does not match the one given via the
|
||||
command line)
|
||||
|
||||
* `{:undefined, key, value, rest}` - the option `key` is undefined
|
||||
(returned in strict mode when the switch is unknown)
|
||||
|
||||
* `{:error, rest}` - there are no switches at the top of the given argv
|
||||
"""
|
||||
|
||||
@spec next(argv, options) ::
|
||||
{:ok, key :: atom, value :: term, argv} |
|
||||
{:invalid, String.t, String.t | nil, argv} |
|
||||
{:undefined, String.t, String.t | nil, argv} |
|
||||
{:error, argv}
|
||||
|
||||
def next(argv, opts \\ []) when is_list(argv) and is_list(opts) do
|
||||
{aliases, switches, strict} = compile_config(opts)
|
||||
next(argv, aliases, switches, strict)
|
||||
end
|
||||
|
||||
defp next([], _aliases, _switches, _strict) do
|
||||
{:error, []}
|
||||
end
|
||||
|
||||
defp next(["--"|_]=argv, _aliases, _switches, _strict) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
defp next(["-"|_]=argv, _aliases, _switches, _strict) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
defp next(["- " <> _|_]=argv, _aliases, _switches, _strict) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
defp next(["-" <> option|rest], aliases, switches, strict) do
|
||||
{option, value} = split_option(option)
|
||||
opt_name_bin = "-" <> option
|
||||
tagged = tag_option(option, switches, aliases)
|
||||
|
||||
if strict and not option_defined?(tagged, switches) do
|
||||
{:undefined, opt_name_bin, value, rest}
|
||||
else
|
||||
{opt_name, kinds, value} = normalize_option(tagged, value, switches)
|
||||
{value, kinds, rest} = normalize_value(value, kinds, rest, strict)
|
||||
case validate_option(value, kinds) do
|
||||
{:ok, new_value} -> {:ok, opt_name, new_value, rest}
|
||||
:invalid -> {:invalid, opt_name_bin, value, rest}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp next(argv, _aliases, _switches, _strict) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a key-value enumerable and converts it to argv.
|
||||
|
||||
Keys must be atoms. Keys with nil value are discarded,
|
||||
boolean values are converted to `--key` or `--no-key`
|
||||
and all other values are converted using `to_string/1`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> OptionParser.to_argv([foo_bar: "baz"])
|
||||
["--foo-bar", "baz"]
|
||||
|
||||
iex> OptionParser.to_argv([bool: true, bool: false, discarded: nil])
|
||||
["--bool", "--no-bool"]
|
||||
|
||||
"""
|
||||
@spec to_argv(Enumerable.t) :: argv
|
||||
def to_argv(enum) do
|
||||
Enum.flat_map(enum, fn
|
||||
{_key, nil} -> []
|
||||
{key, true} -> [to_switch(key)]
|
||||
{key, false} -> [to_switch(key, "--no-")]
|
||||
{key, value} -> [to_switch(key), to_string(value)]
|
||||
end)
|
||||
end
|
||||
|
||||
defp to_switch(key, prefix \\ "--") when is_atom(key) do
|
||||
prefix <> String.replace(Atom.to_string(key), "_", "-")
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Splits a string into argv chunks.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> OptionParser.split("foo bar")
|
||||
["foo", "bar"]
|
||||
|
||||
iex> OptionParser.split("foo \"bar baz\"")
|
||||
["foo", "bar baz"]
|
||||
"""
|
||||
@spec split(String.t) :: argv
|
||||
def split(string) do
|
||||
do_split(strip_leading_spaces(string), "", [], nil)
|
||||
end
|
||||
|
||||
# If we have an escaped quote, simply remove the escape
|
||||
defp do_split(<<?\\, quote, t::binary>>, buffer, acc, quote),
|
||||
do: do_split(t, <<buffer::binary, quote>>, acc, quote)
|
||||
|
||||
# If we have a quote and we were not in a quote, start one
|
||||
defp do_split(<<quote, t::binary>>, buffer, acc, nil) when quote in [?", ?'],
|
||||
do: do_split(t, buffer, acc, quote)
|
||||
|
||||
# If we have a quote and we were inside it, close it
|
||||
defp do_split(<<quote, t::binary>>, buffer, acc, quote),
|
||||
do: do_split(t, buffer, acc, nil)
|
||||
|
||||
# If we have an escaped quote/space, simply remove the escape as long as we are not inside a quote
|
||||
defp do_split(<<?\\, h, t::binary>>, buffer, acc, nil) when h in [?\s, ?', ?"],
|
||||
do: do_split(t, <<buffer::binary, h>>, acc, nil)
|
||||
|
||||
# If we have space and we are outside of a quote, start new segment
|
||||
defp do_split(<<?\s, t::binary>>, buffer, acc, nil),
|
||||
do: do_split(strip_leading_spaces(t), "", [buffer|acc], nil)
|
||||
|
||||
# All other characters are moved to buffer
|
||||
defp do_split(<<h, t::binary>>, buffer, acc, quote) do
|
||||
do_split(t, <<buffer::binary, h>>, acc, quote)
|
||||
end
|
||||
|
||||
# Finish the string expecting a nil marker
|
||||
defp do_split(<<>>, "", acc, nil),
|
||||
do: Enum.reverse(acc)
|
||||
|
||||
defp do_split(<<>>, buffer, acc, nil),
|
||||
do: Enum.reverse([buffer|acc])
|
||||
|
||||
# Otherwise raise
|
||||
defp do_split(<<>>, _, _acc, marker) do
|
||||
raise "argv string did not terminate properly, a #{<<marker>>} was opened but never closed"
|
||||
end
|
||||
|
||||
defp strip_leading_spaces(" " <> t), do: strip_leading_spaces(t)
|
||||
defp strip_leading_spaces(t), do: t
|
||||
|
||||
## Helpers
|
||||
|
||||
defp compile_config(opts) do
|
||||
aliases = opts[:aliases] || []
|
||||
defp parse(["-" <> option|t], aliases, flags, dict, args, all) do
|
||||
{ option, value } = normalize_option(option, aliases)
|
||||
|
||||
{switches, strict} = cond do
|
||||
opts[:switches] && opts[:strict] ->
|
||||
raise ArgumentError, ":switches and :strict cannot be given together"
|
||||
s = opts[:switches] ->
|
||||
{s, false}
|
||||
s = opts[:strict] ->
|
||||
{s, true}
|
||||
true ->
|
||||
{[], false}
|
||||
if value == nil do
|
||||
{ value, t } = if is_flag?(flags, option) do
|
||||
flag_from_tail(t)
|
||||
else
|
||||
value_from_tail(t)
|
||||
end
|
||||
end
|
||||
|
||||
{aliases, switches, strict}
|
||||
dict = store_option dict, option, value
|
||||
parse(t, aliases, flags, dict, args, all)
|
||||
end
|
||||
|
||||
defp validate_option(value, kinds) do
|
||||
{is_invalid, value} = cond do
|
||||
:invalid in kinds ->
|
||||
{true, value}
|
||||
:boolean in kinds ->
|
||||
case value do
|
||||
t when t in [true, "true"] -> {nil, true}
|
||||
f when f in [false, "false"] -> {nil, false}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:integer in kinds ->
|
||||
case Integer.parse(value) do
|
||||
{value, ""} -> {nil, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:float in kinds ->
|
||||
case Float.parse(value) do
|
||||
{value, ""} -> {nil, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
true ->
|
||||
{nil, value}
|
||||
end
|
||||
|
||||
if is_invalid do
|
||||
:invalid
|
||||
else
|
||||
{:ok, value}
|
||||
end
|
||||
defp parse([], _, _, dict, args, true) do
|
||||
{ dict, List.reverse(args) }
|
||||
end
|
||||
|
||||
defp do_store_option(dict, option, value, kinds) do
|
||||
cond do
|
||||
:keep in kinds ->
|
||||
[{option, value}|dict]
|
||||
true ->
|
||||
[{option, value}|Keyword.delete(dict, option)]
|
||||
end
|
||||
defp parse([h|t], aliases, flags, dict, args, true) do
|
||||
parse(t, aliases, flags, dict, [h|args], true)
|
||||
end
|
||||
|
||||
defp tag_option(<<?-, option::binary>>, switches, _aliases) do
|
||||
get_negated(option, switches)
|
||||
defp parse(value, _, _, dict, _args, false) do
|
||||
{ dict, value }
|
||||
end
|
||||
|
||||
defp tag_option(option, _switches, aliases) when is_binary(option) do
|
||||
opt = get_option(option)
|
||||
if alias = aliases[opt] do
|
||||
{:default, alias}
|
||||
else
|
||||
:unknown
|
||||
end
|
||||
defp flag_from_tail([h|t]) when h in ["false", "true"], do: { h, t }
|
||||
defp flag_from_tail(t) , do: { true, t }
|
||||
|
||||
defp value_from_tail(["-" <> _|_] = t), do: { true, t }
|
||||
defp value_from_tail([h|t]), do: { h, t }
|
||||
defp value_from_tail([]), do: { true, [] }
|
||||
|
||||
defp store_option(dict, option, value) when value in ["false", "true"] do
|
||||
store_option(dict, option, binary_to_atom(value))
|
||||
end
|
||||
|
||||
defp option_defined?(:unknown, _switches) do
|
||||
false
|
||||
defp store_option(dict, option, value) do
|
||||
Keyword.put dict, option, value
|
||||
end
|
||||
|
||||
defp option_defined?({:negated, option}, switches) do
|
||||
Keyword.has_key?(switches, option)
|
||||
defp normalize_option(<<?-, option|:binary>>, aliases) do
|
||||
normalize_option(option, aliases)
|
||||
end
|
||||
|
||||
defp option_defined?({:default, option}, switches) do
|
||||
Keyword.has_key?(switches, option)
|
||||
defp normalize_option(option, aliases) do
|
||||
{ option, value } = split_option(option)
|
||||
if is_no?(option), do: value = true
|
||||
atom = option /> to_underscore /> binary_to_atom
|
||||
{ aliases[atom] || atom, value }
|
||||
end
|
||||
|
||||
defp normalize_option(:unknown, value, _switches) do
|
||||
{nil, [:invalid], value}
|
||||
end
|
||||
|
||||
defp normalize_option({:negated, option}, value, switches) do
|
||||
if value do
|
||||
{option, [:invalid], value}
|
||||
else
|
||||
{option, List.wrap(switches[option]), false}
|
||||
end
|
||||
end
|
||||
|
||||
defp normalize_option({:default, option}, value, switches) do
|
||||
{option, List.wrap(switches[option]), value}
|
||||
end
|
||||
|
||||
defp normalize_value(nil, kinds, t, strict) do
|
||||
nil_or_true = if strict, do: nil, else: true
|
||||
cond do
|
||||
:boolean in kinds ->
|
||||
{true, kinds, t}
|
||||
value_in_tail?(t) ->
|
||||
[h|t] = t
|
||||
{h, kinds, t}
|
||||
kinds == [] ->
|
||||
{nil_or_true, kinds, t}
|
||||
true ->
|
||||
{nil, [:invalid], t}
|
||||
end
|
||||
end
|
||||
|
||||
defp normalize_value(value, kinds, t, _) do
|
||||
{value, kinds, t}
|
||||
end
|
||||
|
||||
defp value_in_tail?(["-"|_]), do: true
|
||||
defp value_in_tail?(["- " <> _|_]), do: true
|
||||
defp value_in_tail?(["-" <> _|_]), do: false
|
||||
defp value_in_tail?([]), do: false
|
||||
defp value_in_tail?(_), do: true
|
||||
|
||||
defp split_option(option) do
|
||||
case :binary.split(option, "=") do
|
||||
[h] -> {h, nil}
|
||||
[h, t] -> {h, t}
|
||||
[h] -> { h, nil }
|
||||
[h|t] -> { h, Enum.join(t, "=") }
|
||||
end
|
||||
end
|
||||
|
||||
defp to_underscore(option), do: to_underscore(option, <<>>)
|
||||
|
||||
defp to_underscore("_" <> _rest, _acc), do: nil
|
||||
|
||||
defp to_underscore("-" <> rest, acc),
|
||||
do: to_underscore(rest, acc <> "_")
|
||||
|
||||
defp to_underscore(<<c>> <> rest, acc),
|
||||
do: to_underscore(rest, <<acc::binary, c>>)
|
||||
|
||||
defp to_underscore(<<>>, acc), do: acc
|
||||
|
||||
defp get_option(option) do
|
||||
if str = to_underscore(option) do
|
||||
String.to_atom(str)
|
||||
end
|
||||
defp to_underscore(option) do
|
||||
bc <<c>> inbits option, do: << if c == ?-, do: ?_, else: c >>
|
||||
end
|
||||
|
||||
defp get_negated("no-" <> rest = original, switches) do
|
||||
cond do
|
||||
(negated = get_option(rest)) && :boolean in List.wrap(switches[negated]) ->
|
||||
{:negated, negated}
|
||||
option = get_option(original) ->
|
||||
{:default, option}
|
||||
true ->
|
||||
:unknown
|
||||
end
|
||||
end
|
||||
defp is_no?("no-" <> _), do: true
|
||||
defp is_no?(_), do: false
|
||||
|
||||
defp get_negated(rest, _switches) do
|
||||
if option = get_option(rest) do
|
||||
{:default, option}
|
||||
else
|
||||
:unknown
|
||||
end
|
||||
end
|
||||
end
|
||||
defp is_flag?(flags, option), do: List.member?(flags, option)
|
||||
end
|
||||
@@ -0,0 +1,84 @@
|
||||
defimpl Dict, for: Orddict do
|
||||
defmacrop dict(data) do
|
||||
quote do
|
||||
{ Orddict, unquote(data) }
|
||||
end
|
||||
end
|
||||
|
||||
def keys(dict(data)) do
|
||||
lc { k, _ } inlist data, do: k
|
||||
end
|
||||
|
||||
def values(dict(data)) do
|
||||
lc { _, v } inlist data, do: v
|
||||
end
|
||||
|
||||
def size(dict(data)) do
|
||||
length(data)
|
||||
end
|
||||
|
||||
def has_key?(dict(data), key) do
|
||||
:orddict.is_key key, data
|
||||
end
|
||||
|
||||
def get(dict(data), key, default // nil) do
|
||||
case :orddict.find(key, data) do
|
||||
{:ok, value} ->
|
||||
value
|
||||
:error ->
|
||||
default
|
||||
end
|
||||
end
|
||||
|
||||
def put(dict(data), key, value) do
|
||||
dict(:orddict.store key, value, data)
|
||||
end
|
||||
|
||||
def delete(dict(data), key) do
|
||||
dict(:orddict.erase key, data)
|
||||
end
|
||||
|
||||
def merge(dict(d1), dict(d2)) do
|
||||
dict(:orddict.merge fn _k, _v1, v2 -> v2 end, d1, d2)
|
||||
end
|
||||
|
||||
def merge(dict(d1), dict(d2), fun) do
|
||||
dict(:orddict.merge fun, d1, d2)
|
||||
end
|
||||
|
||||
def update(dict(data), key, fun) do
|
||||
dict(:orddict.update key, fun, data)
|
||||
end
|
||||
|
||||
def update(dict(data), key, initial, fun) do
|
||||
dict(:orddict.update key, fun, initial, data)
|
||||
end
|
||||
|
||||
def empty(_) do
|
||||
dict([])
|
||||
end
|
||||
|
||||
def to_list(dict(data)) do
|
||||
data
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Enum.Iterator, for: Orddict do
|
||||
def iterator({ Orddict, data }), do: data
|
||||
def count({ Orddict, data }), do: length(data)
|
||||
end
|
||||
|
||||
defimpl Enum.OrdIterator, for: Orddict do
|
||||
def iterator({ Orddict, data }), do: data
|
||||
def to_list({ h, next }, _), do: [h|next]
|
||||
end
|
||||
|
||||
defmodule Orddict do
|
||||
@moduledoc """
|
||||
This module implements a dictionary based that stores items
|
||||
as a list of tuples. It is a simple wrapper around
|
||||
[Erlang's orddict module](http://www.erlang.org/doc/man/orddict.html)
|
||||
and exposed via the `Dict` protocol.
|
||||
"""
|
||||
use Dict.Common, Dict.Orddict
|
||||
end
|
||||
@@ -1,657 +0,0 @@
|
||||
defmodule Path do
|
||||
@moduledoc """
|
||||
This module provides conveniences for manipulating or
|
||||
retrieving file system paths.
|
||||
|
||||
The functions in this module may receive a char data as
|
||||
argument (i.e. a string or a list of characters / string)
|
||||
and will always return a string (encoded in UTF-8).
|
||||
|
||||
The majority of the functions in this module do not
|
||||
interact with the file system, except for a few functions
|
||||
that require it (like `wildcard/1` and `expand/1`).
|
||||
"""
|
||||
|
||||
alias :filename, as: FN
|
||||
@type t :: :unicode.chardata()
|
||||
|
||||
@doc """
|
||||
Converts the given path to an absolute one. Unlike
|
||||
`expand/1`, no attempt is made to resolve `..`, `.` or `~`.
|
||||
|
||||
## Unix examples
|
||||
|
||||
Path.absname("foo")
|
||||
#=> "/usr/local/foo"
|
||||
|
||||
Path.absname("../x")
|
||||
#=> "/usr/local/../x"
|
||||
|
||||
## Windows
|
||||
|
||||
Path.absname("foo").
|
||||
"D:/usr/local/foo"
|
||||
Path.absname("../x").
|
||||
"D:/usr/local/../x"
|
||||
|
||||
"""
|
||||
@spec absname(t) :: binary
|
||||
def absname(path) do
|
||||
absname(path, System.cwd!)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a path from `relative_to` to `path`. If `path` is already
|
||||
an absolute path, `relative_to` is ignored. See also `relative_to/2`.
|
||||
|
||||
Unlike `expand/2`, no attempt is made to
|
||||
resolve `..`, `.` or `~`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.absname("foo", "bar")
|
||||
"bar/foo"
|
||||
|
||||
iex> Path.absname("../x", "bar")
|
||||
"bar/../x"
|
||||
|
||||
"""
|
||||
@spec absname(t, t) :: binary
|
||||
def absname(path, relative_to) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case type(path) do
|
||||
:relative -> absname_join(relative_to, path)
|
||||
:absolute -> absname_join([path])
|
||||
:volumerelative ->
|
||||
relative_to = IO.chardata_to_string(relative_to)
|
||||
absname_vr(split(path), split(relative_to), relative_to)
|
||||
end
|
||||
end
|
||||
|
||||
# Absolute path on current drive
|
||||
defp absname_vr(["/"|rest], [volume|_], _relative),
|
||||
do: absname_join([volume|rest])
|
||||
|
||||
# Relative to current directory on current drive.
|
||||
defp absname_vr([<<x, ?:>>|rest], [<<x, _::binary>>|_], relative),
|
||||
do: absname(absname_join(rest), relative)
|
||||
|
||||
# Relative to current directory on another drive.
|
||||
defp absname_vr([<<x, ?:>>|name], _, _relative) do
|
||||
cwd =
|
||||
case :file.get_cwd([x, ?:]) do
|
||||
{:ok, dir} -> IO.chardata_to_string(dir)
|
||||
{:error, _} -> <<x, ?:, ?/>>
|
||||
end
|
||||
absname(absname_join(name), cwd)
|
||||
end
|
||||
|
||||
# Joins a list
|
||||
defp absname_join([name1, name2|rest]), do:
|
||||
absname_join([absname_join(name1, name2)|rest])
|
||||
defp absname_join([name]), do:
|
||||
do_absname_join(IO.chardata_to_string(name), <<>>, [], major_os_type())
|
||||
|
||||
# Joins two paths
|
||||
defp absname_join(left, right),
|
||||
do: do_absname_join(IO.chardata_to_string(left), relative(right), [], major_os_type())
|
||||
|
||||
defp do_absname_join(<<uc_letter, ?:, rest::binary>>, relativename, [], :win32) when uc_letter in ?A..?Z, do:
|
||||
do_absname_join(rest, relativename, [?:, uc_letter+?a-?A], :win32)
|
||||
defp do_absname_join(<<?\\, rest::binary>>, relativename, result, :win32), do:
|
||||
do_absname_join(<<?/, rest::binary>>, relativename, result, :win32)
|
||||
defp do_absname_join(<<?/, rest::binary>>, relativename, [?., ?/|result], os_type), do:
|
||||
do_absname_join(rest, relativename, [?/|result], os_type)
|
||||
defp do_absname_join(<<?/, rest::binary>>, relativename, [?/|result], os_type), do:
|
||||
do_absname_join(rest, relativename, [?/|result], os_type)
|
||||
defp do_absname_join(<<>>, <<>>, result, os_type), do:
|
||||
IO.iodata_to_binary(reverse_maybe_remove_dirsep(result, os_type))
|
||||
defp do_absname_join(<<>>, relativename, [?:|rest], :win32), do:
|
||||
do_absname_join(relativename, <<>>, [?:|rest], :win32)
|
||||
defp do_absname_join(<<>>, relativename, [?/|result], os_type), do:
|
||||
do_absname_join(relativename, <<>>, [?/|result], os_type)
|
||||
defp do_absname_join(<<>>, relativename, result, os_type), do:
|
||||
do_absname_join(relativename, <<>>, [?/|result], os_type)
|
||||
defp do_absname_join(<<char, rest::binary>>, relativename, result, os_type), do:
|
||||
do_absname_join(rest, relativename, [char|result], os_type)
|
||||
|
||||
defp reverse_maybe_remove_dirsep([?/, ?:, letter], :win32), do:
|
||||
[letter, ?:, ?/]
|
||||
defp reverse_maybe_remove_dirsep([?/], _), do:
|
||||
[?/]
|
||||
defp reverse_maybe_remove_dirsep([?/|name], _), do:
|
||||
:lists.reverse(name)
|
||||
defp reverse_maybe_remove_dirsep(name, _), do:
|
||||
:lists.reverse(name)
|
||||
|
||||
@doc """
|
||||
Converts the path to an absolute one and expands
|
||||
any `.` and `..` characters and a leading `~`.
|
||||
|
||||
## Examples
|
||||
|
||||
Path.expand("/foo/bar/../bar")
|
||||
"/foo/bar"
|
||||
|
||||
"""
|
||||
@spec expand(t) :: binary
|
||||
def expand(path) do
|
||||
expand_dot absname(expand_home(path), System.cwd!)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Expands the path relative to the path given as the second argument
|
||||
expanding any `.` and `..` characters. If the path is already an
|
||||
absolute path, `relative_to` is ignored.
|
||||
|
||||
Note, that this function treats `path` with a leading `~` as
|
||||
an absolute one.
|
||||
|
||||
The second argument is first expanded to an absolute path.
|
||||
|
||||
## Examples
|
||||
|
||||
# Assuming that the absolute path to baz is /quux/baz
|
||||
Path.expand("foo/bar/../bar", "baz")
|
||||
#=> "/quux/baz/foo/bar"
|
||||
|
||||
Path.expand("foo/bar/../bar", "/baz")
|
||||
"/baz/foo/bar"
|
||||
Path.expand("/foo/bar/../bar", "/baz")
|
||||
"/foo/bar"
|
||||
|
||||
"""
|
||||
@spec expand(t, t) :: binary
|
||||
def expand(path, relative_to) do
|
||||
expand_dot absname(absname(expand_home(path), expand_home(relative_to)), System.cwd!)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the path type.
|
||||
|
||||
## Unix examples
|
||||
|
||||
Path.type("/") #=> :absolute
|
||||
Path.type("/usr/local/bin") #=> :absolute
|
||||
Path.type("usr/local/bin") #=> :relative
|
||||
Path.type("../usr/local/bin") #=> :relative
|
||||
Path.type("~/file") #=> :relative
|
||||
|
||||
## Windows examples
|
||||
|
||||
Path.type("D:/usr/local/bin") #=> :absolute
|
||||
Path.type("usr/local/bin") #=> :relative
|
||||
Path.type("D:bar.ex") #=> :volumerelative
|
||||
Path.type("/bar/foo.ex") #=> :volumerelative
|
||||
|
||||
"""
|
||||
@spec type(t) :: :absolute | :relative | :volumerelative
|
||||
def type(name) when is_list(name) or is_binary(name) do
|
||||
pathtype(name, major_os_type) |> elem(0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Forces the path to be a relative path.
|
||||
|
||||
## Unix examples
|
||||
|
||||
Path.relative("/usr/local/bin") #=> "usr/local/bin"
|
||||
Path.relative("usr/local/bin") #=> "usr/local/bin"
|
||||
Path.relative("../usr/local/bin") #=> "../usr/local/bin"
|
||||
|
||||
## Windows examples
|
||||
|
||||
Path.relative("D:/usr/local/bin") #=> "usr/local/bin"
|
||||
Path.relative("usr/local/bin") #=> "usr/local/bin"
|
||||
Path.relative("D:bar.ex") #=> "bar.ex"
|
||||
Path.relative("/bar/foo.ex") #=> "bar/foo.ex"
|
||||
|
||||
"""
|
||||
@spec relative(t) :: binary
|
||||
def relative(name) do
|
||||
relative(name, major_os_type())
|
||||
end
|
||||
|
||||
defp relative(name, os_type) do
|
||||
pathtype(name, os_type)
|
||||
|> elem(1)
|
||||
|> IO.chardata_to_string
|
||||
end
|
||||
|
||||
defp pathtype(name, os_type) do
|
||||
case os_type do
|
||||
:win32 -> win32_pathtype(name)
|
||||
_ -> unix_pathtype(name)
|
||||
end
|
||||
end
|
||||
|
||||
defp unix_pathtype(<<?/, relative::binary>>), do:
|
||||
{:absolute, relative}
|
||||
defp unix_pathtype([?/|relative]), do:
|
||||
{:absolute, relative}
|
||||
defp unix_pathtype([list|rest]) when is_list(list), do:
|
||||
unix_pathtype(list ++ rest)
|
||||
defp unix_pathtype(relative), do:
|
||||
{:relative, relative}
|
||||
|
||||
@slash [?/, ?\\]
|
||||
|
||||
defp win32_pathtype([list|rest]) when is_list(list), do:
|
||||
win32_pathtype(list++rest)
|
||||
defp win32_pathtype([char, list|rest]) when is_list(list), do:
|
||||
win32_pathtype([char|list++rest])
|
||||
defp win32_pathtype(<<c1, c2, relative::binary>>) when c1 in @slash and c2 in @slash, do:
|
||||
{:absolute, relative}
|
||||
defp win32_pathtype(<<c, relative::binary>>) when c in @slash, do:
|
||||
{:volumerelative, relative}
|
||||
defp win32_pathtype(<<_letter, ?:, c, relative::binary>>) when c in @slash, do:
|
||||
{:absolute, relative}
|
||||
defp win32_pathtype(<<_letter, ?:, relative::binary>>), do:
|
||||
{:volumerelative, relative}
|
||||
|
||||
defp win32_pathtype([c1, c2 | relative]) when c1 in @slash and c2 in @slash, do:
|
||||
{:absolute, relative}
|
||||
defp win32_pathtype([c | relative]) when c in @slash, do:
|
||||
{:volumerelative, relative}
|
||||
defp win32_pathtype([c1, c2, list|rest]) when is_list(list), do:
|
||||
win32_pathtype([c1, c2|list++rest])
|
||||
defp win32_pathtype([_letter, ?:, c | relative]) when c in @slash, do:
|
||||
{:absolute, relative}
|
||||
defp win32_pathtype([_letter, ?: | relative]), do:
|
||||
{:volumerelative, relative}
|
||||
defp win32_pathtype(relative), do:
|
||||
{:relative, relative}
|
||||
|
||||
@doc """
|
||||
Returns the given `path` relative to the given `from` path.
|
||||
In other words, it tries to strip the `from` prefix from `path`.
|
||||
|
||||
This function does not query the file system, so it assumes
|
||||
no symlinks between the paths.
|
||||
|
||||
In case a direct relative path cannot be found, it returns
|
||||
the original path.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.relative_to("/usr/local/foo", "/usr/local")
|
||||
"foo"
|
||||
|
||||
iex> Path.relative_to("/usr/local/foo", "/")
|
||||
"usr/local/foo"
|
||||
|
||||
iex> Path.relative_to("/usr/local/foo", "/etc")
|
||||
"/usr/local/foo"
|
||||
|
||||
"""
|
||||
@spec relative_to(t, t) :: binary
|
||||
def relative_to(path, from) do
|
||||
path = IO.chardata_to_string(path)
|
||||
relative_to(split(path), split(from), path)
|
||||
end
|
||||
|
||||
defp relative_to([h|t1], [h|t2], original) do
|
||||
relative_to(t1, t2, original)
|
||||
end
|
||||
|
||||
defp relative_to([_|_] = l1, [], _original) do
|
||||
join(l1)
|
||||
end
|
||||
|
||||
defp relative_to(_, _, original) do
|
||||
original
|
||||
end
|
||||
|
||||
@doc """
|
||||
Convenience to get the path relative to the current working
|
||||
directory. If, for some reason, the current working directory
|
||||
cannot be retrieved, returns the full path.
|
||||
"""
|
||||
@spec relative_to_cwd(t) :: binary
|
||||
def relative_to_cwd(path) do
|
||||
case :file.get_cwd do
|
||||
{:ok, base} -> relative_to(path, IO.chardata_to_string(base))
|
||||
_ -> path
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the last component of the path or the path
|
||||
itself if it does not contain any directory separators.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.basename("foo")
|
||||
"foo"
|
||||
|
||||
iex> Path.basename("foo/bar")
|
||||
"bar"
|
||||
|
||||
iex> Path.basename("/")
|
||||
""
|
||||
|
||||
"""
|
||||
@spec basename(t) :: binary
|
||||
def basename(path) do
|
||||
FN.basename(IO.chardata_to_string(path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the last component of `path` with the `extension`
|
||||
stripped. This function should be used to remove a specific
|
||||
extension which may, or may not, be there.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.basename("~/foo/bar.ex", ".ex")
|
||||
"bar"
|
||||
|
||||
iex> Path.basename("~/foo/bar.exs", ".ex")
|
||||
"bar.exs"
|
||||
|
||||
iex> Path.basename("~/foo/bar.old.ex", ".ex")
|
||||
"bar.old"
|
||||
|
||||
"""
|
||||
@spec basename(t, t) :: binary
|
||||
def basename(path, extension) do
|
||||
FN.basename(IO.chardata_to_string(path), IO.chardata_to_string(extension))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the directory component of `path`.
|
||||
|
||||
## Examples
|
||||
|
||||
Path.dirname("/foo/bar.ex")
|
||||
#=> "/foo"
|
||||
Path.dirname("/foo/bar/baz.ex")
|
||||
#=> "/foo/bar"
|
||||
|
||||
"""
|
||||
@spec dirname(t) :: binary
|
||||
def dirname(path) do
|
||||
FN.dirname(IO.chardata_to_string(path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the extension of the last component of `path`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.extname("foo.erl")
|
||||
".erl"
|
||||
|
||||
iex> Path.extname("~/foo/bar")
|
||||
""
|
||||
|
||||
"""
|
||||
@spec extname(t) :: binary
|
||||
def extname(path) do
|
||||
FN.extension(IO.chardata_to_string(path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the `path` with the `extension` stripped.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.rootname("/foo/bar")
|
||||
"/foo/bar"
|
||||
|
||||
iex> Path.rootname("/foo/bar.ex")
|
||||
"/foo/bar"
|
||||
|
||||
"""
|
||||
@spec rootname(t) :: binary
|
||||
def rootname(path) do
|
||||
FN.rootname(IO.chardata_to_string(path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the `path` with the `extension` stripped. This function should be used to
|
||||
remove a specific extension which might, or might not, be there.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.rootname("/foo/bar.erl", ".erl")
|
||||
"/foo/bar"
|
||||
|
||||
iex> Path.rootname("/foo/bar.erl", ".ex")
|
||||
"/foo/bar.erl"
|
||||
|
||||
"""
|
||||
@spec rootname(t, t) :: binary
|
||||
def rootname(path, extension) do
|
||||
FN.rootname(IO.chardata_to_string(path), IO.chardata_to_string(extension))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Joins a list of strings.
|
||||
|
||||
This function should be used to convert a list of strings to a path.
|
||||
Note that any trailing slash is removed on join.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.join(["~", "foo"])
|
||||
"~/foo"
|
||||
|
||||
iex> Path.join(["foo"])
|
||||
"foo"
|
||||
|
||||
iex> Path.join(["/", "foo", "bar/"])
|
||||
"/foo/bar"
|
||||
|
||||
"""
|
||||
@spec join([t]) :: binary
|
||||
def join([name1, name2|rest]), do:
|
||||
join([join(name1, name2)|rest])
|
||||
def join([name]), do:
|
||||
name
|
||||
|
||||
@doc """
|
||||
Joins two paths.
|
||||
|
||||
The right path will always be expanded to its relative format
|
||||
and any trailing slash is removed on join.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.join("foo", "bar")
|
||||
"foo/bar"
|
||||
|
||||
"""
|
||||
@spec join(t, t) :: binary
|
||||
def join(left, right) do
|
||||
left = IO.chardata_to_string(left)
|
||||
os_type = major_os_type()
|
||||
do_join(left, right, os_type) |> remove_dirsep(os_type)
|
||||
end
|
||||
|
||||
defp do_join("", right, os_type), do: relative(right, os_type)
|
||||
defp do_join(left, "", _os_type), do: left
|
||||
defp do_join(left, right, os_type), do: remove_dirsep(left, os_type) <> "/" <> relative(right, os_type)
|
||||
|
||||
defp remove_dirsep("", _os_type), do: ""
|
||||
defp remove_dirsep(bin, os_type) do
|
||||
last = :binary.last(bin)
|
||||
if last == ?/ or (last == ?\\ and os_type == :win32) do
|
||||
binary_part(bin, 0, byte_size(bin) - 1)
|
||||
else
|
||||
bin
|
||||
end
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Splits the path into a list at the path separator.
|
||||
|
||||
If an empty string is given, returns an empty list.
|
||||
|
||||
On Windows, path is split on both "\" and "/" separators
|
||||
and the driver letter, if there is one, is always returned
|
||||
in lowercase.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.split("")
|
||||
[]
|
||||
|
||||
iex> Path.split("foo")
|
||||
["foo"]
|
||||
|
||||
iex> Path.split("/foo/bar")
|
||||
["/", "foo", "bar"]
|
||||
|
||||
"""
|
||||
@spec split(t) :: [binary]
|
||||
|
||||
# Work around a bug in Erlang on UNIX
|
||||
def split(""), do: []
|
||||
|
||||
def split(path) do
|
||||
FN.split(IO.chardata_to_string(path))
|
||||
end
|
||||
|
||||
defmodule Wildcard do
|
||||
@moduledoc false
|
||||
|
||||
def read_link_info(file) do
|
||||
call({:read_link_info, file})
|
||||
end
|
||||
|
||||
# For compatibility with buggy Erlang 17.1.
|
||||
def read_file_info(file) do
|
||||
call({:read_link_info, file})
|
||||
end
|
||||
|
||||
def list_dir(dir) do
|
||||
case call({:list_dir, dir}) do
|
||||
{:ok, files} ->
|
||||
{:ok, for(file <- files, hd(file) != ?., do: file)}
|
||||
other ->
|
||||
other
|
||||
end
|
||||
end
|
||||
|
||||
@compile {:inline, call: 1}
|
||||
|
||||
defp call(tuple) do
|
||||
x = :erlang.dt_spread_tag(true)
|
||||
y = :gen_server.call(:file_server_2, tuple)
|
||||
:erlang.dt_restore_tag(x)
|
||||
y
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Traverses paths according to the given `glob` expression, and returns a
|
||||
list of matches.
|
||||
|
||||
The wildcard looks like an ordinary path, except that certain
|
||||
"wildcard characters" are interpreted in a special way. The
|
||||
following characters are special:
|
||||
|
||||
* `?` - matches one character
|
||||
|
||||
* `*` - matches any number of characters up to the end of the filename, the
|
||||
next dot, or the next slash
|
||||
|
||||
* `**` - two adjacent `*`'s used as a single pattern will match all
|
||||
files and zero or more directories and subdirectories
|
||||
|
||||
* `[char1, char2, ...]` - matches any of the characters listed; two
|
||||
characters separated by a hyphen will match a range of characters
|
||||
|
||||
* `{item1, item2, ...}` - matches one of the alternatives
|
||||
|
||||
Other characters represent themselves. Only paths that have
|
||||
exactly the same character in the same position will match. Note
|
||||
that matching is case-sensitive; i.e. "a" will not match "A".
|
||||
|
||||
By default, the patterns `*` and `?` do not match files starting
|
||||
with a dot `.` unless `match_dot: true` is given in `opts`.
|
||||
|
||||
## Examples
|
||||
|
||||
Imagine you have a directory called `projects` with three Elixir projects
|
||||
inside of it: `elixir`, `ex_doc` and `dynamo`. You can find all `.beam` files
|
||||
inside the `ebin` directory of each project as follows:
|
||||
|
||||
Path.wildcard("projects/*/ebin/**/*.beam")
|
||||
|
||||
If you want to search for both `.beam` and `.app` files, you could do:
|
||||
|
||||
Path.wildcard("projects/*/ebin/**/*.{beam,app}")
|
||||
|
||||
"""
|
||||
@spec wildcard(t, Keyword.t) :: [binary]
|
||||
def wildcard(glob, opts \\ []) do
|
||||
mod = if Keyword.get(opts, :match_dot), do: :file, else: Path.Wildcard
|
||||
glob
|
||||
|> chardata_to_list()
|
||||
|> :filelib.wildcard(mod)
|
||||
|> Enum.map(&IO.chardata_to_string/1)
|
||||
end
|
||||
|
||||
# expand_dot the given path by expanding "..", "." and "~".
|
||||
|
||||
defp chardata_to_list(chardata) do
|
||||
case :unicode.characters_to_list(chardata) do
|
||||
result when is_list(result) ->
|
||||
result
|
||||
|
||||
{:error, encoded, rest} ->
|
||||
raise UnicodeConversionError, encoded: encoded, rest: rest, kind: :invalid
|
||||
|
||||
{:incomplete, encoded, rest} ->
|
||||
raise UnicodeConversionError, encoded: encoded, rest: rest, kind: :incomplete
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_home(type) do
|
||||
case IO.chardata_to_string(type) do
|
||||
"~" <> rest -> resolve_home(rest)
|
||||
rest -> rest
|
||||
end
|
||||
end
|
||||
|
||||
defp resolve_home(""), do: System.user_home!
|
||||
|
||||
defp resolve_home(rest) do
|
||||
case {rest, major_os_type} do
|
||||
{"\\" <> _, :win32} ->
|
||||
System.user_home! <> rest
|
||||
{"/" <> _, _} ->
|
||||
System.user_home! <> rest
|
||||
_ -> rest
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_dot(<<"/", rest::binary>>),
|
||||
do: "/" <> do_expand_dot(rest)
|
||||
defp expand_dot(<<letter, ":/", rest::binary>>) when letter in ?a..?z,
|
||||
do: <<letter, ":/">> <> do_expand_dot(rest)
|
||||
defp expand_dot(path),
|
||||
do: do_expand_dot(path)
|
||||
|
||||
defp do_expand_dot(path),
|
||||
do: do_expand_dot(:binary.split(path, "/", [:global]), [])
|
||||
|
||||
defp do_expand_dot([".."|t], [_, _|acc]),
|
||||
do: do_expand_dot(t, acc)
|
||||
defp do_expand_dot([".."|t], []),
|
||||
do: do_expand_dot(t, [])
|
||||
defp do_expand_dot(["."|t], acc),
|
||||
do: do_expand_dot(t, acc)
|
||||
defp do_expand_dot([h|t], acc),
|
||||
do: do_expand_dot(t, ["/", h|acc])
|
||||
defp do_expand_dot([], []),
|
||||
do: ""
|
||||
defp do_expand_dot([], ["/"|acc]),
|
||||
do: IO.iodata_to_binary(:lists.reverse(acc))
|
||||
|
||||
defp major_os_type do
|
||||
:os.type |> elem(0)
|
||||
end
|
||||
end
|
||||
+13
-92
@@ -3,145 +3,66 @@ defmodule Port do
|
||||
Functions related to Erlang ports.
|
||||
"""
|
||||
|
||||
@type name :: {:spawn, char_list | binary} |
|
||||
{:spawn_driver, char_list | binary} |
|
||||
{:spawn_executable, char_list | atom} |
|
||||
{:fd, non_neg_integer, non_neg_integer}
|
||||
|
||||
@doc """
|
||||
Opens an Erlang port given a tuple `name` and a list of `settings`.
|
||||
|
||||
## Name
|
||||
|
||||
The supported values for `name` are:
|
||||
|
||||
* `{:spawn, command}` - to run an external program. The first space separated
|
||||
word of `command` will be considered as the name of the program to run, so
|
||||
use `{:spawn_executable, command}` to run a program having spaces in its name.
|
||||
* `{:spawn_driver, command}` - similar to `{:spawn, command}`, but to run a
|
||||
loaded driver.
|
||||
* `{:spawn_executable, filename}` - similar to `{:spawn, filename}`, but to run
|
||||
an external executable. With this option, `filename` in its whole is considered
|
||||
the name of the program to execute.
|
||||
* `{:fd, fd_in, fd_out}` - to access file descriptors used by Erlang, `fd_in`
|
||||
being used for standard input, `fd_out` for standard output.
|
||||
|
||||
For more information, see [`:erlang.open_port/2`](http://www.erlang.org/doc/man/erlang.html#open_port-2).
|
||||
|
||||
Inlined by the compiler.
|
||||
See http://www.erlang.org/doc/man/erlang.html#open_port-2.
|
||||
"""
|
||||
@spec open(name, list) :: port
|
||||
def open(name, settings) do
|
||||
:erlang.open_port(name, settings)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Closes the `port`.
|
||||
|
||||
For more information, see [`:erlang.port_close/1`](http://www.erlang.org/doc/man/erlang.html#port_close-1).
|
||||
|
||||
Inlined by the compiler.
|
||||
See http://www.erlang.org/doc/man/erlang.html#port_close-1.
|
||||
"""
|
||||
@spec close(port) :: true
|
||||
def close(port) do
|
||||
:erlang.port_close(port)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends `data` to the port driver `port`.
|
||||
|
||||
For more information, see [`:erlang.port_command/2`](http://www.erlang.org/doc/man/erlang.html#port_command-2).
|
||||
|
||||
Inlined by the compiler.
|
||||
See http://www.erlang.org/doc/man/erlang.html#port_command-2.
|
||||
"""
|
||||
@spec command(port, iodata, [:force | :nosuspend]) :: boolean
|
||||
def command(port, data, options \\ []) do
|
||||
def command(port, data, options // []) do
|
||||
:erlang.port_command(port, data, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Associates the `port` identifier with a `pid`.
|
||||
|
||||
For more information, see [`:erlang.port_connect/2`](http://www.erlang.org/doc/man/erlang.html#port_connect-2).
|
||||
|
||||
Inlined by the compiler.
|
||||
See http://www.erlang.org/doc/man/erlang.html#port_connect-2.
|
||||
"""
|
||||
@spec connect(port, pid) :: true
|
||||
def connect(port, pid) do
|
||||
:erlang.port_connect(port, pid)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends a synchronous control command to the `port` and returns its reply as a binary.
|
||||
|
||||
Not all port drivers support this feature.
|
||||
|
||||
For more information, see [`:erlang.port_control/3`](http://www.erlang.org/doc/man/erlang.html#port_control-3).
|
||||
|
||||
Inlined by the compiler.
|
||||
See http://www.erlang.org/doc/man/erlang.html#port_control-3.
|
||||
"""
|
||||
@spec control(port, integer, iodata) :: iodata | binary
|
||||
def control(port, operation, data) do
|
||||
:erlang.port_control(port, operation, data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Makes a synchronous call to the `port` and returns its reply as a term.
|
||||
|
||||
Not all port drivers support this control feature.
|
||||
|
||||
For more information, see [`:erlang.port_call/3`](http://www.erlang.org/doc/man/erlang.html#port_call-3).
|
||||
|
||||
Inlined by the compiler.
|
||||
See http://www.erlang.org/doc/man/erlang.html#port_call-3.
|
||||
"""
|
||||
@spec call(port, integer, term) :: term
|
||||
def call(port, operation, data) do
|
||||
:erlang.port_call(port, operation, data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the `port`
|
||||
or `nil` if the port is closed.
|
||||
|
||||
For more information, see [`:erlang.port_info/1`](http://www.erlang.org/doc/man/erlang.html#port_info-1).
|
||||
See http://www.erlang.org/doc/man/erlang.html#port_info-1.
|
||||
"""
|
||||
def info(port) do
|
||||
nillify :erlang.port_info(port)
|
||||
:erlang.port_info(port)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the `port`
|
||||
or `nil` if the port is closed.
|
||||
|
||||
For more information, see [`:erlang.port_info/2`](http://www.erlang.org/doc/man/erlang.html#port_info-2).
|
||||
See http://www.erlang.org/doc/man/erlang.html#port_info-2.
|
||||
"""
|
||||
@spec info(port, atom) :: {atom, term} | nil
|
||||
def info(port, spec)
|
||||
|
||||
def info(port, :registered_name) do
|
||||
case :erlang.port_info(port, :registered_name) do
|
||||
[] -> {:registered_name, []}
|
||||
other -> nillify(other)
|
||||
end
|
||||
end
|
||||
|
||||
def info(port, item) do
|
||||
nillify :erlang.port_info(port, item)
|
||||
:erlang.port_info(port, item)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of the ports for the current node.
|
||||
|
||||
For more information, see [`:erlang.ports/0`](http://www.erlang.org/doc/man/erlang.html#ports-0).
|
||||
|
||||
Inlined by the compiler.
|
||||
See http://www.erlang.org/doc/man/erlang.html#ports-0.
|
||||
"""
|
||||
@spec list :: [port]
|
||||
def list do
|
||||
:erlang.ports
|
||||
end
|
||||
|
||||
@compile {:inline, nillify: 1}
|
||||
defp nillify(:undefined), do: nil
|
||||
defp nillify(other), do: other
|
||||
end
|
||||
end
|
||||
+116
-290
@@ -1,48 +1,43 @@
|
||||
defmodule Process do
|
||||
@moduledoc """
|
||||
Conveniences for working with processes and the process dictionary.
|
||||
|
||||
Besides the functions available in this module, the `Kernel` module
|
||||
exposes and auto-imports some basic functionality related to processes
|
||||
available through the functions:
|
||||
|
||||
* `Kernel.spawn/1` and `Kernel.spawn/3`
|
||||
* `Kernel.spawn_link/1` and `Kernel.spawn_link/3`
|
||||
* `Kernel.spawn_monitor/1` and `Kernel.spawn_monitor/3`
|
||||
* `Kernel.self/0`
|
||||
* `Kernel.send/2`
|
||||
|
||||
This module provides convenience functions around processes and
|
||||
the process dictionary. In Erlang, most of these functions are
|
||||
auto-imported, but in Elixir they are grouped in a module for
|
||||
convenience. Notice that these functions, different from Erlang's,
|
||||
always return nil instead of undefined. You can use their Erlang
|
||||
version if you want the undefined value.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Returns `true` if the process exists and is alive (i.e. it is not exiting
|
||||
and has not exited yet). Otherwise, returns `false`.
|
||||
Returns true if the process exists and is alive, that is,
|
||||
is not exiting and has not exited. Otherwise, returns false.
|
||||
|
||||
`pid` must refer to a process at the local node.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec alive?(pid) :: boolean
|
||||
def alive?(pid) do
|
||||
:erlang.is_process_alive(pid)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all key-values in the dictionary.
|
||||
Returns the current process.
|
||||
"""
|
||||
def self do
|
||||
:erlang.self()
|
||||
end
|
||||
|
||||
Inlined by the compiler.
|
||||
@doc """
|
||||
Returns all key-values in the dictionary
|
||||
with no specific ordering (i.e. they are
|
||||
not a keywords list).
|
||||
"""
|
||||
@spec get :: [{term, term}]
|
||||
def get do
|
||||
:erlang.get()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value for the given `key`.
|
||||
Returns the value for the given key.
|
||||
"""
|
||||
@spec get(term) :: term
|
||||
@spec get(term, default :: term) :: term
|
||||
def get(key, default \\ nil) do
|
||||
def get(key, default // nil) do
|
||||
case :erlang.get(key) do
|
||||
:undefined ->
|
||||
default
|
||||
@@ -51,41 +46,30 @@ defmodule Process do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all keys in the process dictionary.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec get_keys() :: [term]
|
||||
def get_keys() do
|
||||
:erlang.get_keys()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all keys that have the given `value`.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec get_keys(term) :: [term]
|
||||
def get_keys(value) do
|
||||
:erlang.get_keys(value)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stores the given key-value in the process dictionary.
|
||||
|
||||
The return value is the value that was previously stored under the key `key`
|
||||
(or `nil` in case no value was stored under `key`).
|
||||
"""
|
||||
@spec put(term, term) :: term | nil
|
||||
def put(key, value) do
|
||||
nillify :erlang.put(key, value)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the given `key` from the dictionary.
|
||||
Deletes all items in the dictionary.
|
||||
"""
|
||||
def delete() do
|
||||
:erlang.erase()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the given key from the dictionary.
|
||||
"""
|
||||
@spec delete(term) :: term | nil
|
||||
def delete(key) do
|
||||
nillify :erlang.erase(key)
|
||||
end
|
||||
@@ -93,192 +77,108 @@ defmodule Process do
|
||||
@doc """
|
||||
Sends an exit signal with the given reason to the pid.
|
||||
|
||||
The following behaviour applies if reason is any term except `:normal` or `:kill`:
|
||||
The following behavior apply if reason is any term except `:normal` or `:kill`:
|
||||
|
||||
1. If pid is not trapping exits, pid will exit with the given reason.
|
||||
1) If pid is not trapping exits, pid itself will exist with the given reason;
|
||||
|
||||
2. If pid is trapping exits, the exit signal is transformed into a message
|
||||
`{:EXIT, from, reason}` and delivered to the message queue of pid.
|
||||
2) If pid is trapping exits, the exit signal is transformed into a message
|
||||
{'EXIT', from, reason} and delivered to the message queue of pid;
|
||||
|
||||
3. If reason is the atom `:normal`, pid will not exit (unless it is the calling
|
||||
process's pid, in which case it will exit with the reason `:normal`).
|
||||
If it is trapping exits, the exit signal is transformed into a message
|
||||
`{:EXIT, from, :normal}` and delivered to its message queue.
|
||||
3) If reason is the atom `:normal`, pid will not exit. If it is trapping exits,
|
||||
the exit signal is transformed into a message {'EXIT', from, :normal} and
|
||||
delivered to its message queue;
|
||||
|
||||
4. If reason is the atom `:kill`, that is if `exit(pid, :kill)` is called,
|
||||
an untrappable exit signal is sent to pid which will unconditionally
|
||||
exit with exit reason `:killed`.
|
||||
|
||||
Inlined by the compiler.
|
||||
4) If reason is the atom `:kill`, that is if `exit(pid, :kill)` is called, an
|
||||
untrappable exit signal is sent to pid which will unconditionally exit with
|
||||
exit reason `:killed`.
|
||||
|
||||
## Examples
|
||||
|
||||
Process.exit(pid, :kill)
|
||||
|
||||
"""
|
||||
@spec exit(pid, term) :: true
|
||||
def exit(pid, reason) do
|
||||
:erlang.exit(pid, reason)
|
||||
def exit(pid, status) do
|
||||
:erlang.exit(pid, status)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends a message to the given process.
|
||||
|
||||
If the option `:noconnect` is used and sending the message would require an
|
||||
auto-connection to another node the message is not sent and `:noconnect` is
|
||||
returned.
|
||||
|
||||
If the option `:nosuspend` is used and sending the message would cause the
|
||||
sender to be suspended the message is not sent and `:nosuspend` is returned.
|
||||
|
||||
Otherwise the message is sent and `:ok` is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Process.send({:name, :node_does_not_exist}, :hi, [:noconnect])
|
||||
:noconnect
|
||||
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
|
||||
"""
|
||||
@spec send(dest, msg, [option]) :: :ok | :noconnect | :nosuspend when
|
||||
dest: pid | port | atom | {atom, node},
|
||||
msg: any,
|
||||
option: :noconnect | :nosuspend
|
||||
def send(dest, msg, options) do
|
||||
:erlang.send(dest, msg, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends `msg` to `dest` after `time` milliseconds.
|
||||
|
||||
If `dest` is a pid, it must be the pid of a local process, dead or alive.
|
||||
If `dest` is an atom, it must be the name of a registered process
|
||||
which is looked up at the time of delivery. No error is given if the name does
|
||||
not refer to a process.
|
||||
|
||||
This function returns a timer reference, which can be read or canceled with
|
||||
`read_timer/1` and `cancel_timer/1`.
|
||||
|
||||
Finally, the timer will be automatically canceled if the given `dest` is a pid
|
||||
which is not alive or when the given pid exits. Note that timers will not be
|
||||
automatically canceled when `dest` is an atom (as the atom resolution is done
|
||||
on delivery).
|
||||
"""
|
||||
@spec send_after(pid | atom, term, non_neg_integer) :: reference
|
||||
def send_after(dest, msg, time) do
|
||||
:erlang.send_after(time, dest, msg)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Cancels a timer created by `send_after/3`.
|
||||
|
||||
When the result is an integer, it represents the time in milli-seconds
|
||||
left until the timer will expire.
|
||||
|
||||
When the result is `false`, a timer corresponding to `timer_ref` could
|
||||
not be found. This can be either because the timer expired, already has
|
||||
been canceled, or because `timer_ref` never corresponded to a timer.
|
||||
|
||||
If the timer has expired, the timeout message has been sent, but it does
|
||||
not tell you whether or not it has arrived at its destination yet.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec cancel_timer(reference) :: non_neg_integer | false
|
||||
def cancel_timer(timer_ref) do
|
||||
:erlang.cancel_timer(timer_ref)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads a timer created by `send_after/3`.
|
||||
|
||||
When the result is an integer, it represents the time in milli-seconds
|
||||
left until the timer will expire.
|
||||
|
||||
When the result is `false`, a timer corresponding to `timer_ref` could
|
||||
not be found. This can be either because the timer expired, already has
|
||||
been canceled, or because `timer_ref` never corresponded to a timer.
|
||||
|
||||
If the timer has expired, the timeout message has been sent, but it does
|
||||
not tell you whether or not it has arrived at its destination yet.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec read_timer(reference) :: non_neg_integer | false
|
||||
def read_timer(timer_ref) do
|
||||
:erlang.read_timer(timer_ref)
|
||||
end
|
||||
|
||||
@type spawn_opt :: :link | :monitor | {:priority, :low | :normal | :high} |
|
||||
{:fullsweep_after, non_neg_integer} |
|
||||
{:min_heap_size, non_neg_integer} |
|
||||
{:min_bin_vheap_size, non_neg_integer}
|
||||
@type spawn_opts :: [spawn_opt]
|
||||
|
||||
@doc """
|
||||
Spawns the given module and function passing the given args
|
||||
according to the given options.
|
||||
|
||||
The result depends on the given options. In particular,
|
||||
if `:monitor` is given as an option, it will return a tuple
|
||||
containing the pid and the monitoring reference, otherwise
|
||||
just the spawned process pid.
|
||||
|
||||
It also accepts extra options, for the list of available options
|
||||
check [`:erlang.spawn_opt/4`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-4).
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec spawn((() -> any), spawn_opts) :: pid | {pid, reference}
|
||||
def spawn(fun, opts) do
|
||||
def spawn(fun, opts // []) do
|
||||
:erlang.spawn_opt(fun, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Spawns the given module and function passing the given args
|
||||
according to the given options.
|
||||
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.
|
||||
|
||||
The result depends on the given options. In particular,
|
||||
if `:monitor` is given as an option, it will return a tuple
|
||||
containing the pid and the monitoring reference, otherwise
|
||||
just the spawned process pid.
|
||||
It behaves exactly the same as the `Kernel.spawn/3` 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 also accepts extra options, for the list of available options
|
||||
check [`:erlang.spawn_opt/4`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-4).
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec spawn(module, atom, list, spawn_opts) :: pid | {pid, reference}
|
||||
def spawn(mod, fun, args, opts) do
|
||||
def spawn(mod, fun, args, opts // []) do
|
||||
:erlang.spawn_opt(mod, fun, args, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the pid of a new process started by the application of `fun`.
|
||||
A link is created between the calling process and the new
|
||||
process, atomically.
|
||||
"""
|
||||
def spawn_link(fun) do
|
||||
:erlang.spawn_link(fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the pid of a new process started by the application of
|
||||
`module.function(args)`. A link is created between the calling process
|
||||
and the new process, atomically. Otherwise works like spawn/3.
|
||||
"""
|
||||
def spawn_link(mod, fun, args) do
|
||||
:erlang.spawn_link(mod, fun, args)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the pid of a new process started by the application of `fun`
|
||||
and reference for a monitor created to the new process.
|
||||
"""
|
||||
def spawn_monitor(fun) do
|
||||
:erlang.spawn_monitor(fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
A new process is started by the application of `module.function(args)`
|
||||
and the process is monitored at the same time. Returns the pid and a
|
||||
reference for the monitor. Otherwise works like spawn/3.
|
||||
"""
|
||||
def spawn_monitor(mod, fun, args) do
|
||||
:erlang.spawn_monitor(mod, fun, args)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The calling process starts monitoring the item given.
|
||||
It returns the monitor reference.
|
||||
|
||||
See [the need for monitoring](http://elixir-lang.org/getting-started/mix-otp/genserver.html#the-need-for-monitoring)
|
||||
for an example.
|
||||
See [`:erlang.monitor/2`](http://www.erlang.org/doc/man/erlang.html#monitor-2) for more info.
|
||||
|
||||
Inlined by the compiler.
|
||||
See http://www.erlang.org/doc/man/erlang.html#monitor-2 for more info.
|
||||
"""
|
||||
@spec monitor(pid | {reg_name :: atom, node :: atom} | reg_name :: atom) :: reference
|
||||
def monitor(item) do
|
||||
:erlang.monitor(:process, item)
|
||||
end
|
||||
|
||||
@doc """
|
||||
If `monitor_ref` is a reference which the calling process
|
||||
obtained by calling `monitor/1`, this monitoring is turned off.
|
||||
If monitor_ref is a reference which the calling process
|
||||
obtained by calling monitor/1, this monitoring is turned off.
|
||||
If the monitoring is already turned off, nothing happens.
|
||||
|
||||
See [`:erlang.demonitor/2`](http://www.erlang.org/doc/man/erlang.html#demonitor-2) for more info.
|
||||
|
||||
Inlined by the compiler.
|
||||
See http://www.erlang.org/doc/man/erlang.html#demonitor-2 for more info.
|
||||
"""
|
||||
@spec demonitor(reference) :: true
|
||||
@spec demonitor(reference, options :: [:flush | :info]) :: boolean
|
||||
def demonitor(monitor_ref, options \\ []) do
|
||||
def demonitor(monitor_ref, options // []) do
|
||||
:erlang.demonitor(monitor_ref, options)
|
||||
end
|
||||
|
||||
@@ -287,12 +187,11 @@ defmodule Process do
|
||||
processes currently existing on the local node.
|
||||
|
||||
Note that a process that is exiting, exists but is not alive, i.e.,
|
||||
`alive?/1` will return `false` for a process that is exiting,
|
||||
alive?/1 will return false for a process that is exiting,
|
||||
but its process identifier will be part of the result returned.
|
||||
|
||||
See [`:erlang.processes/0`](http://www.erlang.org/doc/man/erlang.html#processes-0) for more info.
|
||||
See http://www.erlang.org/doc/man/erlang.html#processes-0 for more info.
|
||||
"""
|
||||
@spec list :: [pid]
|
||||
def list do
|
||||
:erlang.processes()
|
||||
end
|
||||
@@ -301,164 +200,91 @@ defmodule Process do
|
||||
Creates a link between the calling process and another process
|
||||
(or port) `pid`, if there is not such a link already.
|
||||
|
||||
See [`:erlang.link/1`](http://www.erlang.org/doc/man/erlang.html#link-1) for more info.
|
||||
|
||||
Inlined by the compiler.
|
||||
See http://www.erlang.org/doc/man/erlang.html#link-1 for more info.
|
||||
"""
|
||||
@spec link(pid | port) :: true
|
||||
def link(pid) do
|
||||
:erlang.link(pid)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes the link, if there is one, between the calling process and
|
||||
the process or port referred to by `pid`. Returns `true` and does not
|
||||
the process or port referred to by `pid`. Returns true and does not
|
||||
fail, even if there is no link or `id` does not exist
|
||||
|
||||
See [`:erlang.unlink/1`](http://www.erlang.org/doc/man/erlang.html#unlink-1) for more info.
|
||||
|
||||
Inlined by the compiler.
|
||||
See http://www.erlang.org/doc/man/erlang.html#unlink-1 for more info.
|
||||
"""
|
||||
@spec unlink(pid | port) :: true
|
||||
def unlink(pid) do
|
||||
:erlang.unlink(pid)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Associates the name with a pid or a port identifier. `name`, which must
|
||||
be an atom, can be used instead of the pid / port identifier with the
|
||||
`Kernel.send/2` function.
|
||||
Associates the name with a pid or a port identifier. name, which must
|
||||
be an atom, can be used instead of the pid / port identifier in the
|
||||
send operator (name <- message).
|
||||
|
||||
`Process.register/2` will fail with `ArgumentError` if the pid supplied
|
||||
is no longer alive, (check with `alive?/1`) or if the name is
|
||||
already registered (check with `whereis/1`).
|
||||
See http://www.erlang.org/doc/man/erlang.html#register-2 for more info.
|
||||
"""
|
||||
@spec register(pid | port, atom) :: true
|
||||
def register(pid, name) when not name in [nil, false, true] do
|
||||
def register(name, pid) do
|
||||
:erlang.register(name, pid)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes the registered name, associated with a pid or a port identifier.
|
||||
|
||||
See [`:erlang.unregister/1`](http://www.erlang.org/doc/man/erlang.html#unregister-1) for more info.
|
||||
See http://www.erlang.org/doc/man/erlang.html#unregister-1 for more info.
|
||||
"""
|
||||
@spec unregister(atom) :: true
|
||||
def unregister(name) do
|
||||
:erlang.unregister(name)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the pid or port identifier with the registered name.
|
||||
Returns `nil` if the name is not registered.
|
||||
Returns undefined if the name is not registered.
|
||||
|
||||
See [`:erlang.whereis/1`](http://www.erlang.org/doc/man/erlang.html#whereis-1) for more info.
|
||||
See http://www.erlang.org/doc/man/erlang.html#whereis-1 for more info.
|
||||
"""
|
||||
@spec whereis(atom) :: pid | port | nil
|
||||
def whereis(name) do
|
||||
nillify :erlang.whereis(name)
|
||||
:erlang.whereis(name)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the pid of the group leader for the process which evaluates the function.
|
||||
Returns a list of names which have been registered using register/2.
|
||||
"""
|
||||
@spec group_leader :: pid
|
||||
def group_leader do
|
||||
:erlang.group_leader
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sets the group leader of `pid` to `leader`. Typically, this is used when a processes
|
||||
started from a certain shell should have a group leader other than `:init`.
|
||||
"""
|
||||
@spec group_leader(pid, leader :: pid) :: true
|
||||
def group_leader(pid, leader) do
|
||||
:erlang.group_leader(leader, pid)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of names which have been registered using `register/2`.
|
||||
"""
|
||||
@spec registered :: [atom]
|
||||
def registered do
|
||||
:erlang.registered()
|
||||
end
|
||||
|
||||
@typep process_flag :: :trap_exit | :error_handler | :min_heap_size |
|
||||
:min_bin_vheap_size | :priority | :save_calls |
|
||||
:sensitive
|
||||
@doc """
|
||||
Sets certain flags for the process which calls this function.
|
||||
Returns the old value of the flag.
|
||||
|
||||
See [`:erlang.process_flag/2`](http://www.erlang.org/doc/man/erlang.html#process_flag-2) for more info.
|
||||
See http://www.erlang.org/doc/man/erlang.html#process_flag-2 for more info.
|
||||
"""
|
||||
@spec flag(process_flag, term) :: term
|
||||
def flag(flag, value) do
|
||||
:erlang.process_flag(flag, value)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sets certain flags for the process `pid`, in the same manner as `flag/2`.
|
||||
Returns the old value of the flag. The allowed values for `flag` are
|
||||
only a subset of those allowed in `flag/2`, namely: `save_calls`.
|
||||
Sets certain flags for the process Pid, in the same manner as flag/2.
|
||||
Returns the old value of the flag. The allowed values for Flag are
|
||||
only a subset of those allowed in flag/2, namely: save_calls.
|
||||
|
||||
See [`:erlang.process_flag/3`](http://www.erlang.org/doc/man/erlang.html#process_flag-3) for more info.
|
||||
See http://www.erlang.org/doc/man/erlang.html#process_flag-3 for more info.
|
||||
"""
|
||||
@spec flag(pid, :save_calls, non_neg_integer) :: non_neg_integer
|
||||
def flag(pid, flag, value) do
|
||||
:erlang.process_flag(pid, flag, value)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the process identified by `pid` or `nil` if the process
|
||||
is not alive.
|
||||
Use this only for debugging information.
|
||||
Returns information about the process identified by pid
|
||||
or undefined if the process is not alive.
|
||||
|
||||
See [`:erlang.process_info/1`](http://www.erlang.org/doc/man/erlang.html#process_info-1) for more info.
|
||||
See http://www.erlang.org/doc/man/erlang.html#process_info-2 for more info.
|
||||
"""
|
||||
@spec info(pid) :: Keyword.t
|
||||
def info(pid) do
|
||||
nillify :erlang.process_info(pid)
|
||||
def info(pid, spec) do
|
||||
:erlang.process_info(pid, spec)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the process identified by `pid`
|
||||
or `nil` if the process is not alive.
|
||||
|
||||
See [`:erlang.process_info/2`](http://www.erlang.org/doc/man/erlang.html#process_info-2) for more info.
|
||||
"""
|
||||
@spec info(pid, atom | [atom]) :: {atom, term} | [{atom, term}] | nil
|
||||
def info(pid, spec)
|
||||
|
||||
def info(pid, :registered_name) do
|
||||
case :erlang.process_info(pid, :registered_name) do
|
||||
:undefined -> nil
|
||||
[] -> {:registered_name, []}
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
def info(pid, spec) when is_atom(spec) or is_list(spec) do
|
||||
nillify :erlang.process_info(pid, spec)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the calling process into a wait state
|
||||
where its memory allocation has been reduced as much as possible,
|
||||
which is useful if the process does not expect to receive any messages
|
||||
in the near future.
|
||||
|
||||
See [`:erlang.hibernate/3`](http://www.erlang.org/doc/man/erlang.html#hibernate-3) for more info.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec hibernate(module, atom, list) :: no_return
|
||||
def hibernate(mod, fun, args) do
|
||||
:erlang.hibernate(mod, fun, args)
|
||||
end
|
||||
|
||||
@compile {:inline, nillify: 1}
|
||||
defp nillify(:undefined), do: nil
|
||||
defp nillify(other), do: other
|
||||
end
|
||||
|
||||
+255
-602
@@ -1,655 +1,308 @@
|
||||
defmodule Protocol do
|
||||
@moduledoc """
|
||||
Functions for working with protocols.
|
||||
"""
|
||||
@moduledoc false
|
||||
|
||||
# We need to use Erlang.lists because Enum is not available yet
|
||||
require Erlang.lists, as: L
|
||||
|
||||
@doc """
|
||||
Defines a new protocol function.
|
||||
Handle `defprotocol`. It will define a function for each
|
||||
protocol plus two extra functions:
|
||||
|
||||
Protocols do not allow functions to be defined directly, instead, the
|
||||
regular `Kernel.def/*` macros are replaced by this macro which
|
||||
defines the protocol functions with the appropriate callbacks.
|
||||
* `__protocol__/1` - returns the protocol name when :name is given,
|
||||
and a keywords list with the protocol functions
|
||||
when :functions is given;
|
||||
|
||||
* `__impl_for__/1` - receives one argument and returns a module
|
||||
that implements the protocol for the given
|
||||
data type. If no implementation matches, returns nil;
|
||||
|
||||
* `__impl_for__!/1` - same as above but raises an error if an implementation is not found
|
||||
"""
|
||||
defmacro def(signature)
|
||||
|
||||
defmacro def({_, _, args}) when args == [] or is_atom(args) do
|
||||
raise ArgumentError, "protocol functions expect at least one argument"
|
||||
end
|
||||
|
||||
defmacro def({name, _, args}) when is_atom(name) and is_list(args) do
|
||||
arity = length(args)
|
||||
|
||||
type_args = :lists.map(fn _ -> quote(do: term) end,
|
||||
:lists.seq(2, arity))
|
||||
type_args = [quote(do: t) | type_args]
|
||||
|
||||
call_args = :lists.map(fn i -> {String.to_atom(<<?x, i + 64>>), [], __MODULE__} end,
|
||||
:lists.seq(2, arity))
|
||||
call_args = [quote(do: t) | call_args]
|
||||
|
||||
quote do
|
||||
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))
|
||||
|
||||
# Generate the actual implementation
|
||||
Kernel.def unquote(name)(unquote_splicing(call_args)) do
|
||||
impl_for!(t).unquote(name)(unquote_splicing(call_args))
|
||||
end
|
||||
|
||||
# Convert the spec to callback if possible,
|
||||
# otherwise generate a dummy callback
|
||||
Protocol.__spec__?(__MODULE__, name, arity) ||
|
||||
@callback unquote(name)(unquote_splicing(type_args)) :: term
|
||||
end
|
||||
end
|
||||
|
||||
defmacro def(_) do
|
||||
raise ArgumentError, "invalid args for def inside defprotocol"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the given module is loaded and is protocol.
|
||||
|
||||
Returns `:ok` if so, otherwise raises ArgumentError.
|
||||
"""
|
||||
@spec assert_protocol!(module) :: :ok | no_return
|
||||
def assert_protocol!(module) do
|
||||
assert_protocol!(module, "")
|
||||
end
|
||||
|
||||
defp assert_protocol!(module, extra) do
|
||||
case Code.ensure_compiled(module) do
|
||||
{:module, ^module} -> :ok
|
||||
_ -> raise ArgumentError, "#{inspect module} is not available" <> extra
|
||||
end
|
||||
|
||||
try do
|
||||
module.__protocol__(:module)
|
||||
rescue
|
||||
UndefinedFunctionError ->
|
||||
raise ArgumentError, "#{inspect module} is not a protocol" <> extra
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the given module is loaded and is an implementation
|
||||
of the given protocol.
|
||||
|
||||
Returns `:ok` if so, otherwise raises ArgumentError.
|
||||
"""
|
||||
@spec assert_impl!(module, module) :: :ok | no_return
|
||||
def assert_impl!(protocol, base) do
|
||||
assert_impl!(protocol, base, "")
|
||||
end
|
||||
|
||||
defp assert_impl!(protocol, base, extra) do
|
||||
impl = Module.concat(protocol, base)
|
||||
|
||||
case Code.ensure_compiled(impl) do
|
||||
{:module, ^impl} -> :ok
|
||||
_ -> raise ArgumentError,
|
||||
"#{inspect impl} is not available" <> extra
|
||||
end
|
||||
|
||||
try do
|
||||
impl.__impl__(:protocol)
|
||||
rescue
|
||||
UndefinedFunctionError ->
|
||||
raise ArgumentError,
|
||||
"#{inspect impl} is not an implementation of a protocol" <> extra
|
||||
else
|
||||
^protocol ->
|
||||
:ok
|
||||
other ->
|
||||
raise ArgumentError,
|
||||
"expected #{inspect impl} to be an implementation of #{inspect protocol}, got: #{inspect other}" <> extra
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Derives the `protocol` for `module` with the given options.
|
||||
"""
|
||||
defmacro derive(protocol, module, options \\ []) do
|
||||
quote do
|
||||
module = unquote(module)
|
||||
Protocol.__derive__([{unquote(protocol), unquote(options)}], module, __ENV__)
|
||||
end
|
||||
end
|
||||
|
||||
## Consolidation
|
||||
|
||||
@doc """
|
||||
Extracts all protocols from the given paths.
|
||||
|
||||
The paths can be either a char list or a string. Internally
|
||||
they are worked on as char lists, so passing them as lists
|
||||
avoid extra conversion.
|
||||
|
||||
Does not load any of the protocols.
|
||||
|
||||
## Examples
|
||||
|
||||
# Get Elixir's ebin and retrieve all protocols
|
||||
iex> path = :code.lib_dir(:elixir, :ebin)
|
||||
iex> mods = Protocol.extract_protocols([path])
|
||||
iex> Enumerable in mods
|
||||
true
|
||||
|
||||
"""
|
||||
@spec extract_protocols([char_list | String.t]) :: [atom]
|
||||
def extract_protocols(paths) do
|
||||
extract_matching_by_attribute paths, 'Elixir.',
|
||||
fn module, attributes ->
|
||||
case attributes[:protocol] do
|
||||
[fallback_to_any: _] -> module
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Extracts all types implemented for the given protocol from
|
||||
the given paths.
|
||||
|
||||
The paths can be either a char list or a string. Internally
|
||||
they are worked on as char lists, so passing them as lists
|
||||
avoid extra conversion.
|
||||
|
||||
Does not load any of the implementations.
|
||||
|
||||
## Examples
|
||||
|
||||
# Get Elixir's ebin and retrieve all protocols
|
||||
iex> path = :code.lib_dir(:elixir, :ebin)
|
||||
iex> mods = Protocol.extract_impls(Enumerable, [path])
|
||||
iex> List in mods
|
||||
true
|
||||
|
||||
"""
|
||||
@spec extract_impls(module, [char_list | String.t]) :: [atom]
|
||||
def extract_impls(protocol, paths) when is_atom(protocol) do
|
||||
prefix = Atom.to_char_list(protocol) ++ '.'
|
||||
extract_matching_by_attribute paths, prefix, fn
|
||||
_mod, attributes ->
|
||||
case attributes[:impl] do
|
||||
[protocol: ^protocol, for: for] -> for
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp extract_matching_by_attribute(paths, prefix, callback) do
|
||||
for path <- paths,
|
||||
file <- list_dir(path),
|
||||
mod = extract_from_file(path, file, prefix, callback),
|
||||
do: mod
|
||||
end
|
||||
|
||||
defp list_dir(path) when is_list(path) do
|
||||
case :file.list_dir(path) do
|
||||
{:ok, files} -> files
|
||||
_ -> []
|
||||
end
|
||||
end
|
||||
|
||||
defp list_dir(path), do: list_dir(to_char_list(path))
|
||||
|
||||
defp extract_from_file(path, file, prefix, callback) do
|
||||
if :lists.prefix(prefix, file) and :filename.extension(file) == '.beam' do
|
||||
extract_from_beam(:filename.join(path, file), callback)
|
||||
end
|
||||
end
|
||||
|
||||
defp extract_from_beam(file, callback) do
|
||||
case :beam_lib.chunks(file, [:attributes]) do
|
||||
{:ok, {module, [attributes: attributes]}} ->
|
||||
callback.(module, attributes)
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the protocol was consolidated.
|
||||
"""
|
||||
@spec consolidated?(module) :: boolean
|
||||
def consolidated?(protocol) do
|
||||
protocol.__protocol__(:consolidated?)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a protocol and a list of implementations and
|
||||
consolidates the given protocol.
|
||||
|
||||
Consolidation happens by changing the protocol `impl_for`
|
||||
in the abstract format to have fast lookup rules. Usually
|
||||
the list of implementations to use during consolidation
|
||||
are retrieved with the help of `extract_impls/2`.
|
||||
|
||||
It returns the updated version of the protocol bytecode.
|
||||
A given bytecode or protocol implementation can be checked
|
||||
to be consolidated or not by analyzing the protocol
|
||||
attribute:
|
||||
|
||||
Protocol.consolidated?(Enumerable)
|
||||
|
||||
If the first element of the tuple is `true`, it means
|
||||
the protocol was consolidated.
|
||||
|
||||
This function does not load the protocol at any point
|
||||
nor loads the new bytecode for the compiled module.
|
||||
However each implementation must be available and
|
||||
it will be loaded.
|
||||
"""
|
||||
@spec consolidate(module, [module]) ::
|
||||
{:ok, binary} |
|
||||
{:error, :not_a_protocol} |
|
||||
{:error, :no_beam_info}
|
||||
def consolidate(protocol, types) when is_atom(protocol) do
|
||||
with {:ok, info} <- beam_protocol(protocol),
|
||||
{:ok, code, docs} <- change_debug_info(info, types),
|
||||
do: compile(code, docs)
|
||||
end
|
||||
|
||||
@docs_chunk 'ExDc'
|
||||
|
||||
defp beam_protocol(protocol) do
|
||||
chunk_ids = [:abstract_code, :attributes, @docs_chunk]
|
||||
opts = [:allow_missing_chunks]
|
||||
case :beam_lib.chunks(beam_file(protocol), chunk_ids, opts) do
|
||||
{:ok, {^protocol, [{:abstract_code, {_raw, abstract_code}},
|
||||
{:attributes, attributes},
|
||||
{@docs_chunk, docs}]}} ->
|
||||
case attributes[:protocol] do
|
||||
[fallback_to_any: any] ->
|
||||
{:ok, {protocol, any, abstract_code, docs}}
|
||||
_ ->
|
||||
{:error, :not_a_protocol}
|
||||
end
|
||||
_ ->
|
||||
{:error, :no_beam_info}
|
||||
end
|
||||
end
|
||||
|
||||
defp beam_file(module) when is_atom(module) do
|
||||
case :code.which(module) do
|
||||
atom when is_atom(atom) -> module
|
||||
file -> file
|
||||
end
|
||||
end
|
||||
|
||||
# Change the debug information to the optimized
|
||||
# impl_for/1 dispatch version.
|
||||
defp change_debug_info({protocol, any, code, docs}, types) do
|
||||
types = if any, do: types, else: List.delete(types, Any)
|
||||
all = [Any] ++ for {_guard, mod} <- builtin, do: mod
|
||||
structs = types -- all
|
||||
case change_impl_for(code, protocol, types, structs, false, []) do
|
||||
{:ok, ret} -> {:ok, ret, docs}
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
defp change_impl_for([{:function, line, :__protocol__, 1, clauses}|t], protocol, types, structs, _, acc) do
|
||||
clauses = :lists.map(fn
|
||||
{:clause, l, [{:atom, _, :consolidated?}], [], [{:atom, _, _}]} ->
|
||||
{:clause, l, [{:atom, 0, :consolidated?}], [], [{:atom, 0, true}]}
|
||||
{:clause, _, _, _, _} = c ->
|
||||
c
|
||||
end, clauses)
|
||||
|
||||
change_impl_for(t, protocol, types, structs, true,
|
||||
[{:function, line, :__protocol__, 1, clauses}|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([{:function, line, :impl_for, 1, _}|t], protocol, types, structs, is_protocol, acc) do
|
||||
fallback = if Any in types, do: load_impl(protocol, Any)
|
||||
|
||||
clauses = for {guard, mod} <- builtin,
|
||||
mod in types,
|
||||
do: builtin_clause_for(mod, guard, protocol, line)
|
||||
|
||||
clauses = [struct_clause_for(line)|clauses] ++
|
||||
[fallback_clause_for(fallback, protocol, line)]
|
||||
|
||||
change_impl_for(t, protocol, types, structs, is_protocol,
|
||||
[{:function, line, :impl_for, 1, clauses}|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([{:function, line, :struct_impl_for, 1, _}|t], protocol, types, structs, is_protocol, acc) do
|
||||
fallback = if Any in types, do: load_impl(protocol, Any)
|
||||
clauses = for struct <- structs, do: each_struct_clause_for(struct, protocol, line)
|
||||
clauses = clauses ++ [fallback_clause_for(fallback, protocol, line)]
|
||||
|
||||
change_impl_for(t, protocol, types, structs, is_protocol,
|
||||
[{:function, line, :struct_impl_for, 1, clauses}|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([h|t], protocol, info, types, is_protocol, acc) do
|
||||
change_impl_for(t, protocol, info, types, is_protocol, [h|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([], protocol, _info, _types, is_protocol, acc) do
|
||||
if is_protocol do
|
||||
{:ok, {protocol, Enum.reverse(acc)}}
|
||||
else
|
||||
{:error, :not_a_protocol}
|
||||
end
|
||||
end
|
||||
|
||||
defp builtin_clause_for(mod, guard, protocol, line) do
|
||||
{:clause, line,
|
||||
[{:var, line, :x}],
|
||||
[[{:call, line,
|
||||
{:remote, line, {:atom, line, :erlang}, {:atom, line, guard}},
|
||||
[{:var, line, :x}],
|
||||
}]],
|
||||
[{:atom, line, load_impl(protocol, mod)}]}
|
||||
end
|
||||
|
||||
defp struct_clause_for(line) do
|
||||
{:clause, line,
|
||||
[{:map, line, [
|
||||
{:map_field_exact, line, {:atom, line, :__struct__}, {:var, line, :x}}
|
||||
]}],
|
||||
[[{:call, line,
|
||||
{:remote, line, {:atom, line, :erlang}, {:atom, line, :is_atom}},
|
||||
[{:var, line, :x}],
|
||||
}]],
|
||||
[{:call, line,
|
||||
{:atom, line, :struct_impl_for},
|
||||
[{:var, line, :x}]}]}
|
||||
end
|
||||
|
||||
defp each_struct_clause_for(other, protocol, line) do
|
||||
{:clause, line, [{:atom, line, other}], [],
|
||||
[{:atom, line, load_impl(protocol, other)}]}
|
||||
end
|
||||
|
||||
defp fallback_clause_for(value, _protocol, line) do
|
||||
{:clause, line, [{:var, line, :_}], [],
|
||||
[{:atom, line, value}]}
|
||||
end
|
||||
|
||||
defp load_impl(protocol, for) do
|
||||
Module.concat(protocol, for).__impl__(:target)
|
||||
end
|
||||
|
||||
# Finally compile the module and emit its bytecode.
|
||||
defp compile({protocol, code}, docs) do
|
||||
opts = if Code.compiler_options[:debug_info], do: [:debug_info], else: []
|
||||
{:ok, ^protocol, binary, _warnings} = :compile.forms(code, [:return|opts])
|
||||
unless docs == :missing_chunk do
|
||||
binary = :elixir_module.add_beam_chunk(binary, @docs_chunk, docs)
|
||||
end
|
||||
{:ok, binary}
|
||||
end
|
||||
|
||||
## Definition callbacks
|
||||
|
||||
@doc false
|
||||
def __protocol__(name, [do: block]) do
|
||||
def defprotocol(name, [do: block]) do
|
||||
quote do
|
||||
defmodule unquote(name) do
|
||||
# We don't allow function definition inside protocols
|
||||
# Remove "harmful" macros
|
||||
# We don't want to allow function definition inside protocols
|
||||
import Kernel, except: [
|
||||
defmacrop: 1, defmacrop: 2, defmacro: 1, defmacro: 2,
|
||||
defp: 1, defp: 2, def: 1, def: 2
|
||||
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, only: [def: 1]
|
||||
|
||||
# Compile with debug info for consolidation
|
||||
@compile :debug_info
|
||||
import Protocol.DSL
|
||||
|
||||
# Set up a clear slate to store defined functions
|
||||
@functions []
|
||||
@fallback_to_any false
|
||||
|
||||
# Invoke the user given block
|
||||
_ = unquote(block)
|
||||
unquote(block)
|
||||
|
||||
# Finalize expansion
|
||||
unquote(after_defprotocol)
|
||||
# Define callbacks and meta information
|
||||
{ conversions, fallback } = Protocol.conversions_for(__MODULE__, @only, @except)
|
||||
Protocol.impl_for(__ENV__, conversions)
|
||||
Protocol.meta(__ENV__, @functions, fallback)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp after_defprotocol do
|
||||
quote bind_quoted: [builtin: builtin] do
|
||||
@spec impl_for(term) :: atom() | nil
|
||||
Kernel.def impl_for(data)
|
||||
|
||||
# Define the implementation for structs.
|
||||
#
|
||||
# It simply delegates to struct_impl_for which is then
|
||||
# optimized during protocol consolidation.
|
||||
Kernel.def impl_for(%{__struct__: struct}) when :erlang.is_atom(struct) do
|
||||
struct_impl_for(struct)
|
||||
end
|
||||
|
||||
# Define the implementation for builtins.
|
||||
:lists.foreach(fn {guard, mod} ->
|
||||
target = Module.concat(__MODULE__, mod)
|
||||
|
||||
Kernel.def impl_for(data) when :erlang.unquote(guard)(data) do
|
||||
case impl_for?(unquote(target)) do
|
||||
true -> unquote(target).__impl__(:target)
|
||||
false -> any_impl_for
|
||||
end
|
||||
end
|
||||
end, builtin)
|
||||
|
||||
@spec impl_for!(term) :: atom() | no_return()
|
||||
Kernel.def impl_for!(data) do
|
||||
impl_for(data) || raise(Protocol.UndefinedError, protocol: __MODULE__, value: data)
|
||||
end
|
||||
|
||||
# Internal handler for Any
|
||||
if @fallback_to_any do
|
||||
Kernel.defp any_impl_for, do: __MODULE__.Any.__impl__(:target)
|
||||
else
|
||||
Kernel.defp any_impl_for, do: nil
|
||||
end
|
||||
|
||||
# Internal handler for Structs
|
||||
Kernel.defp struct_impl_for(struct) do
|
||||
target = Module.concat(__MODULE__, struct)
|
||||
case impl_for?(target) do
|
||||
true -> target.__impl__(:target)
|
||||
false -> any_impl_for
|
||||
end
|
||||
end
|
||||
|
||||
# Check if compilation is available internally
|
||||
Kernel.defp impl_for?(target) do
|
||||
Code.ensure_compiled?(target) and
|
||||
function_exported?(target, :__impl__, 1)
|
||||
end
|
||||
|
||||
# Inline any and struct implementations
|
||||
@compile {:inline, any_impl_for: 0, struct_impl_for: 1, impl_for?: 1}
|
||||
|
||||
unless Kernel.Typespec.defines_type?(__MODULE__, :t, 0) do
|
||||
@type t :: term
|
||||
end
|
||||
|
||||
# Store information as an attribute so it
|
||||
# can be read without loading the module.
|
||||
Module.register_attribute(__MODULE__, :protocol, persist: true)
|
||||
@protocol [fallback_to_any: !!@fallback_to_any]
|
||||
|
||||
@doc false
|
||||
@spec __protocol__(:module) :: __MODULE__
|
||||
@spec __protocol__(:functions) :: unquote(Protocol.__functions_spec__(@functions))
|
||||
@spec __protocol__(:consolidated?) :: boolean
|
||||
Kernel.def __protocol__(:module), do: __MODULE__
|
||||
Kernel.def __protocol__(:functions), do: unquote(:lists.sort(@functions))
|
||||
Kernel.def __protocol__(:consolidated?), do: false
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __functions_spec__([]),
|
||||
do: []
|
||||
def __functions_spec__([h|t]),
|
||||
do: [:lists.foldl(&{:|, [], [&1, &2]}, h, t), quote(do: ...)]
|
||||
|
||||
@doc false
|
||||
def __impl__(protocol, opts) do
|
||||
do_defimpl(protocol, :lists.keysort(1, opts))
|
||||
end
|
||||
|
||||
defp do_defimpl(protocol, [do: block, for: for]) when is_list(for) do
|
||||
for f <- for, do: do_defimpl(protocol, [do: block, for: f])
|
||||
end
|
||||
|
||||
defp do_defimpl(protocol, [do: block, for: for]) do
|
||||
# Unquote the implementation just later
|
||||
# when all variables will already be injected
|
||||
# into the module body.
|
||||
impl =
|
||||
quote unquote: false do
|
||||
@doc false
|
||||
@spec __impl__(:for) :: unquote(for)
|
||||
@spec __impl__(:target) :: __MODULE__
|
||||
@spec __impl__(:protocol) :: unquote(protocol)
|
||||
def __impl__(:for), do: unquote(for)
|
||||
def __impl__(:target), do: __MODULE__
|
||||
def __impl__(:protocol), do: unquote(protocol)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Implement the given protocol for the given module.
|
||||
It also defines a `__impl__` function which
|
||||
returns the protocol being implemented.
|
||||
"""
|
||||
def defimpl(protocol, [do: block, for: for]) do
|
||||
quote do
|
||||
protocol = unquote(protocol)
|
||||
for = unquote(for)
|
||||
name = Module.concat(protocol, for)
|
||||
|
||||
# TODO: Remove this by 1.3
|
||||
if Atom.to_string(protocol) =~ "Elixir.Access" do
|
||||
:elixir_errors.warn __ENV__.line, __ENV__.file,
|
||||
"implementation of the Access protocol is deprecated. For customization of " <>
|
||||
"the dict[key] syntax, please implement the Dict behaviour instead"
|
||||
else
|
||||
Protocol.assert_protocol!(protocol)
|
||||
Protocol.__ensure_defimpl__(protocol, for, __ENV__)
|
||||
end
|
||||
Protocol.assert_protocol(protocol)
|
||||
|
||||
defmodule name do
|
||||
@behaviour protocol
|
||||
@protocol protocol
|
||||
@for for
|
||||
|
||||
def __impl__, do: unquote(protocol)
|
||||
unquote(block)
|
||||
|
||||
Module.register_attribute(__MODULE__, :impl, persist: true)
|
||||
@impl [protocol: @protocol, for: @for]
|
||||
|
||||
unquote(impl)
|
||||
end
|
||||
|
||||
Protocol.assert_impl(name, protocol)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __derive__(derives, for, %Macro.Env{} = env) when is_atom(for) do
|
||||
struct =
|
||||
if for == env.module do
|
||||
Module.get_attribute(for, :struct) ||
|
||||
raise "struct is not defined for #{inspect for}"
|
||||
else
|
||||
for.__struct__
|
||||
end
|
||||
@doc """
|
||||
Check if the given module is a protocol. Raises an error
|
||||
if not loaded or not a protocol.
|
||||
"""
|
||||
def assert_protocol(module) do
|
||||
case :code.ensure_loaded(module) do
|
||||
{ :module, ^module } -> nil
|
||||
_ -> raise ArgumentError, message: "#{module} is not loaded"
|
||||
end
|
||||
|
||||
:lists.foreach(fn
|
||||
proto when is_atom(proto) ->
|
||||
derive(proto, for, struct, [], env)
|
||||
{proto, opts} when is_atom(proto) ->
|
||||
derive(proto, for, struct, opts, env)
|
||||
end, :lists.flatten(derives))
|
||||
|
||||
:ok
|
||||
try do
|
||||
module.__protocol__(:name)
|
||||
rescue
|
||||
UndefinedFunctionError ->
|
||||
raise ArgumentError, message: "#{module} is not a protocol"
|
||||
end
|
||||
end
|
||||
|
||||
defp derive(protocol, for, struct, opts, env) do
|
||||
extra = ", cannot derive #{inspect protocol} for #{inspect for}"
|
||||
assert_protocol!(protocol, extra)
|
||||
__ensure_defimpl__(protocol, for, env)
|
||||
assert_impl!(protocol, Any, extra)
|
||||
@doc """
|
||||
Check if the given `impl` is a valid impl for `protocol`.
|
||||
Raises an error if not.
|
||||
|
||||
# Clean up variables from eval context
|
||||
env = %{env | vars: [], export_vars: nil}
|
||||
args = [for, struct, opts]
|
||||
impl = Module.concat(protocol, Any)
|
||||
"""
|
||||
def assert_impl(impl, protocol) do
|
||||
remaining = protocol.__protocol__(:functions) -- impl.__info__(:functions)
|
||||
|
||||
:elixir_module.expand_callback(env.line, impl, :__deriving__, args, env, fn
|
||||
mod, fun, args ->
|
||||
if function_exported?(mod, fun, length(args)) do
|
||||
apply(mod, fun, args)
|
||||
if remaining != [] do
|
||||
pp = Enum.map_join remaining, ", ", fn {x,y} -> "#{x}/#{y}" end
|
||||
raise ArgumentError,
|
||||
message: "#{inspect impl} did not implement #{inspect protocol}, missing: #{pp}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Defines meta information about the protocol and internal callbacks.
|
||||
"""
|
||||
def meta(env, functions, fallback) do
|
||||
contents = quote do
|
||||
def __protocol__(:name), do: __MODULE__
|
||||
def __protocol__(:functions), do: unquote(:lists.sort(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
|
||||
rescue
|
||||
UndefinedFunctionError -> __fallback__
|
||||
end
|
||||
other ->
|
||||
other
|
||||
end
|
||||
end
|
||||
|
||||
def __impl_for__!(arg) do
|
||||
if module = __impl_for__(arg) do
|
||||
module
|
||||
else
|
||||
Module.create(Module.concat(protocol, for), quote do
|
||||
Module.register_attribute(__MODULE__, :impl, persist: true)
|
||||
@impl [protocol: unquote(protocol), for: unquote(for)]
|
||||
|
||||
@doc false
|
||||
@spec __impl__(:target) :: unquote(impl)
|
||||
@spec __impl__(:protocol) :: unquote(protocol)
|
||||
@spec __impl__(:for) :: unquote(for)
|
||||
def __impl__(:target), do: unquote(impl)
|
||||
def __impl__(:protocol), do: unquote(protocol)
|
||||
def __impl__(:for), do: unquote(for)
|
||||
end, Macro.Env.location(env))
|
||||
raise Protocol.UndefinedError, protocol: __MODULE__, structure: arg
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __ensure_defimpl__(protocol, for, env) do
|
||||
if Protocol.consolidated?(protocol) do
|
||||
message =
|
||||
"the #{inspect protocol} protocol has already been consolidated" <>
|
||||
", an implementation for #{inspect for} has no effect"
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
defp __fallback__, do: unquote(fallback)
|
||||
end
|
||||
:ok
|
||||
|
||||
Module.eval_quoted env, contents
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __spec__?(module, name, arity) do
|
||||
signature = {name, arity}
|
||||
@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(env, conversions) do
|
||||
contents = lc kind inlist conversions, do: each_impl_for(kind, conversions)
|
||||
|
||||
specs = Module.get_attribute(module, :spec)
|
||||
found =
|
||||
:lists.map(fn {:spec, expr, caller} ->
|
||||
if Kernel.Typespec.spec_to_signature(expr) == signature do
|
||||
Kernel.Typespec.define_spec(:callback, expr, caller)
|
||||
true
|
||||
# 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.member({ Any, :is_any }, conversions) && length(conversions) < 10 do
|
||||
contents = contents ++ [quote do
|
||||
defp __raw_impl__(_) do
|
||||
nil
|
||||
end
|
||||
end, specs)
|
||||
end]
|
||||
end
|
||||
|
||||
:lists.any(& &1 == true, found)
|
||||
Module.eval_quoted env, contents
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the default conversions according to the given
|
||||
only/except options.
|
||||
"""
|
||||
def conversions_for(module, only, except) do
|
||||
kinds = all_types
|
||||
|
||||
conversions =
|
||||
if only do
|
||||
L.map(fn i -> L.keyfind(i, 1, kinds) end, only)
|
||||
else
|
||||
except = except || [Any]
|
||||
L.foldl(fn i, list -> L.keydelete(i, 1, list) end, kinds, except)
|
||||
end
|
||||
|
||||
fallback = cond do
|
||||
L.keyfind(Tuple, 1, conversions) ->
|
||||
Module.concat module, Tuple
|
||||
L.keyfind(Any, 1, conversions) ->
|
||||
Module.concat module, Any
|
||||
true ->
|
||||
nil
|
||||
end
|
||||
|
||||
{ conversions, fallback }
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp builtin do
|
||||
[is_tuple: Tuple,
|
||||
is_atom: Atom,
|
||||
is_list: List,
|
||||
is_map: Map,
|
||||
is_bitstring: BitString,
|
||||
is_integer: Integer,
|
||||
is_float: Float,
|
||||
is_function: Function,
|
||||
is_pid: PID,
|
||||
is_port: Port,
|
||||
is_reference: Reference]
|
||||
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 }
|
||||
]
|
||||
end
|
||||
|
||||
# Returns a quoted expression that allow to checks
|
||||
# if a variable named first is built in or not.
|
||||
defp is_builtin?([{h,_}]) do
|
||||
quote do
|
||||
first == unquote(h)
|
||||
end
|
||||
end
|
||||
|
||||
defp is_builtin?([{h,_}|t]) do
|
||||
quote do
|
||||
first == unquote(h) or unquote(is_builtin?(t))
|
||||
end
|
||||
end
|
||||
|
||||
# Specially handle tuples as they can also be record.
|
||||
# If this is the case, module.Record will be returned.
|
||||
defp each_impl_for({ _, :is_record }, conversions) 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 -> __MODULE__.Tuple
|
||||
false ->
|
||||
case atom_to_list(first) do
|
||||
'Elixir-' ++ _ -> __MODULE__.Record
|
||||
_ -> __MODULE__.Tuple
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Special case any as we don't need to generate a guard.
|
||||
defp each_impl_for({ _, :is_any }, _) do
|
||||
quote do
|
||||
defp __raw_impl__(_) do
|
||||
__MODULE__.Any
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Generate all others protocols.
|
||||
defp each_impl_for({ kind, fun }, _) do
|
||||
quote do
|
||||
defp __raw_impl__(arg) when unquote(fun).(arg) do
|
||||
Module.concat __MODULE__, unquote(kind)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Protocol.DSL do
|
||||
@moduledoc false
|
||||
|
||||
defmacro def(expression) do
|
||||
{ name, arity } =
|
||||
case expression do
|
||||
{ _, _, args } when args == [] or is_atom(args) ->
|
||||
raise ArgumentError, message: "protocol functions expect at least one argument"
|
||||
{ name, _, args } when is_atom(name) and is_list(args) ->
|
||||
{ name, length(args) }
|
||||
_ ->
|
||||
raise ArgumentError, message: "invalid args for defprotocol"
|
||||
end
|
||||
|
||||
# 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
|
||||
|
||||
quote do
|
||||
# Append new function to the list
|
||||
@functions [unquote({name, arity})|@functions]
|
||||
|
||||
Kernel.def unquote(name).(unquote_splicing(args)) do
|
||||
args = [unquote_splicing(args)]
|
||||
case __raw_impl__(xA) do
|
||||
__MODULE__.Record ->
|
||||
try do
|
||||
target = Module.concat(__MODULE__, :erlang.element(1, xA))
|
||||
apply target, unquote(name), args
|
||||
rescue
|
||||
UndefinedFunctionError ->
|
||||
case __fallback__ do
|
||||
nil ->
|
||||
raise Protocol.UndefinedError, protocol: __MODULE__, structure: xA
|
||||
other ->
|
||||
apply other, unquote(name), args
|
||||
end
|
||||
end
|
||||
nil ->
|
||||
raise Protocol.UndefinedError, protocol: __MODULE__, structure: xA
|
||||
other ->
|
||||
apply other, unquote(name), args
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
+65
-99
@@ -1,118 +1,84 @@
|
||||
defmodule Range do
|
||||
@moduledoc """
|
||||
Defines a range.
|
||||
|
||||
A range represents a discrete number of values where
|
||||
the first and last values are integers.
|
||||
|
||||
Ranges can be either increasing (first <= last) or
|
||||
decreasing (first > last). Ranges are also always
|
||||
inclusive.
|
||||
|
||||
A Range is represented internally as a struct. However,
|
||||
the most common form of creating and matching on ranges
|
||||
is via the `../2` macro, auto-imported from Kernel:
|
||||
|
||||
iex> range = 1..3
|
||||
1..3
|
||||
iex> first..last = range
|
||||
iex> first
|
||||
1
|
||||
iex> last
|
||||
3
|
||||
|
||||
"""
|
||||
|
||||
defstruct first: nil, last: nil
|
||||
|
||||
@type t :: %Range{}
|
||||
@type t(first, last) :: %Range{first: first, last: last}
|
||||
def __access__(caller, arg), do: Record.access(caller, __MODULE__, __record__(:fields), arg)
|
||||
def __record__(kind, _), do: __record__(kind)
|
||||
def __record__(:name), do: __MODULE__
|
||||
def __record__(:fields), do: [{:first,nil},{:last,nil}]
|
||||
|
||||
@doc """
|
||||
Creates a new range.
|
||||
"""
|
||||
@spec new(integer, integer) :: t
|
||||
def new(first, last) when is_integer(first) and is_integer(last) do
|
||||
%Range{first: first, last: last}
|
||||
end
|
||||
|
||||
def new(first, last) do
|
||||
raise ArgumentError,
|
||||
"ranges (first..last) expect both sides to be integers, " <>
|
||||
"got: #{inspect first}..#{inspect last}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the given `term` is a range.
|
||||
|
||||
It does not check if the range is valid.
|
||||
Returns a new range based on the given options.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Range.range?(1..3)
|
||||
true
|
||||
|
||||
iex> Range.range?(0)
|
||||
false
|
||||
Range.new first: 1, last: 10
|
||||
|
||||
"""
|
||||
@spec range?(%Range{}) :: true
|
||||
@spec range?(term) :: false
|
||||
def range?(term)
|
||||
def range?(%Range{}), do: true
|
||||
def range?(_), do: false
|
||||
def new(options) do
|
||||
{
|
||||
__MODULE__,
|
||||
Keyword.get!(options, :first),
|
||||
Keyword.get!(options, :last)
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the first item of the range.
|
||||
"""
|
||||
def first(range) do
|
||||
elem(range, 2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the last item of the range.
|
||||
"""
|
||||
def last(range) do
|
||||
elem(range, 3)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Enumerable, for: Range do
|
||||
def reduce(first .. last, acc, fun) do
|
||||
reduce(first, last, acc, fun, last >= first)
|
||||
defprotocol Range.Iterator do
|
||||
@doc """
|
||||
How to iterate the range, receives the first
|
||||
and range as arguments. It needs to return a
|
||||
function that receives an item and returns
|
||||
a tuple with two elements: the given item
|
||||
and the next item in the iteration.
|
||||
"""
|
||||
def iterator(first, range)
|
||||
|
||||
@doc """
|
||||
Count how many items are in the range.
|
||||
"""
|
||||
def count(first, range)
|
||||
end
|
||||
|
||||
defimpl Enum.Iterator, for: Range do
|
||||
def iterator(Range[first: first] = range) do
|
||||
iterator = Range.Iterator.iterator(first, range)
|
||||
{ iterator, iterator.(first) }
|
||||
end
|
||||
|
||||
defp reduce(_x, _y, {:halt, acc}, _fun, _up) do
|
||||
{:halted, acc}
|
||||
def count(Range[first: first] = range) do
|
||||
Range.Iterator.count(first, range)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Enum.OrdIterator, for: Range do
|
||||
def iterator(range) do
|
||||
Enum.Iterator.Range.iterator(range)
|
||||
end
|
||||
|
||||
defp reduce(x, y, {:suspend, acc}, fun, up) do
|
||||
{:suspended, acc, &reduce(x, y, &1, fun, up)}
|
||||
end
|
||||
def to_list({ h, next }, iterator), do: [h|to_list(iterator.(next), iterator)]
|
||||
def to_list(:stop, _), do: []
|
||||
end
|
||||
|
||||
defp reduce(x, y, {:cont, acc}, fun, true) when x <= y do
|
||||
reduce(x + 1, y, fun.(x, acc), fun, true)
|
||||
end
|
||||
|
||||
defp reduce(x, y, {:cont, acc}, fun, false) when x >= y do
|
||||
reduce(x - 1, y, fun.(x, acc), fun, false)
|
||||
end
|
||||
|
||||
defp reduce(_, _, {:cont, acc}, _fun, _up) do
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
def member?(first .. last, value) when is_integer(value) do
|
||||
if first <= last do
|
||||
{:ok, first <= value and value <= last}
|
||||
else
|
||||
{:ok, last <= value and value <= first}
|
||||
defimpl Range.Iterator, for: Number do
|
||||
def iterator(first, Range[last: last]) when is_integer(first) and is_integer(last) do
|
||||
fn(current) ->
|
||||
if current > last, do: :stop, else: { current, current + 1 }
|
||||
end
|
||||
end
|
||||
|
||||
def member?(_ .. _, _value) do
|
||||
{:ok, false}
|
||||
def count(first, Range[last: last]) when is_integer(first) and is_integer(last) do
|
||||
last - first + 1
|
||||
end
|
||||
|
||||
def count(first .. last) do
|
||||
if first <= last do
|
||||
{:ok, last - first + 1}
|
||||
else
|
||||
{:ok, first - last + 1}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Range do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(first .. last, opts) do
|
||||
concat [to_doc(first, opts), "..", to_doc(last, opts)]
|
||||
end
|
||||
end
|
||||
end
|
||||
+348
-339
@@ -1,375 +1,384 @@
|
||||
defmodule Record do
|
||||
@moduledoc """
|
||||
Module to work with, define and import records.
|
||||
|
||||
Records are simply tuples where the first element is an atom:
|
||||
|
||||
iex> Record.is_record {User, "john", 27}
|
||||
true
|
||||
|
||||
This module provides conveniences for working with records at
|
||||
compilation time, where compile-time field names are used to
|
||||
manipulate the tuples, providing fast operations on top of
|
||||
the tuples' compact structure.
|
||||
|
||||
In Elixir, records are used mostly in two situations:
|
||||
|
||||
1. to work with short, internal data
|
||||
2. to interface with Erlang records
|
||||
|
||||
The macros `defrecord/3` and `defrecordp/3` can be used to create
|
||||
records while `extract/2` can be used to extract records from Erlang
|
||||
files.
|
||||
|
||||
## Types
|
||||
|
||||
Types can be defined for tuples with the `record/2` macro (only available
|
||||
in typespecs). Like with the generated record macros it will expand to
|
||||
a tuple.
|
||||
|
||||
defmodule MyModule do
|
||||
require Record
|
||||
Record.defrecord :user, name: "john", age: 25
|
||||
|
||||
@type user :: record(:user, name: String.t, age: integer)
|
||||
# expands to: "@type user :: {:user, String.t, integer}"
|
||||
end
|
||||
Functions to define and interact with Erlang records
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Extracts record information from an Erlang file.
|
||||
|
||||
Returns a quoted expression containing the fields as a list
|
||||
of tuples. It expects the record name to be an atom and the
|
||||
library path to be a string at expansion time.
|
||||
Extract record information from an Erlang file and
|
||||
return the fields as a list of tuples.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Record.extract(:file_info, from_lib: "kernel/include/file.hrl")
|
||||
[size: :undefined, type: :undefined, access: :undefined, atime: :undefined,
|
||||
mtime: :undefined, ctime: :undefined, mode: :undefined, links: :undefined,
|
||||
major_device: :undefined, minor_device: :undefined, inode: :undefined,
|
||||
uid: :undefined, gid: :undefined]
|
||||
defrecord FileInfo, Record.extract(:file_info, from_lib: "kernel/include/file.hrl")
|
||||
|
||||
"""
|
||||
def extract(name, opts) when is_atom(name) and is_list(opts) do
|
||||
Record.Extractor.extract(name, opts)
|
||||
def extract(name, opts) do
|
||||
Record.Extractor.retrieve(name, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Extracts all records information from an Erlang file.
|
||||
|
||||
Returns a keyword list containing extracted record names as keys, and
|
||||
lists of tuples describing the fields as values. It expects a named
|
||||
argument :from or :from_lib, which correspond to *include* or
|
||||
*include_lib* attribute from Erlang modules, respectively.
|
||||
|
||||
Implements the access macro used by records.
|
||||
It returns a quoted expression that represents
|
||||
the access given by the keywords.
|
||||
"""
|
||||
def extract_all(opts) when is_list(opts) do
|
||||
Record.Extractor.extract_all(opts)
|
||||
end
|
||||
def access(caller, atom, fields, keyword) do
|
||||
unless is_orddict(keyword) do
|
||||
raise "expected contents inside brackets to be a Keyword"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the given `data` is a record of `kind`.
|
||||
in_match = caller.in_match?
|
||||
|
||||
This is implemented as a macro so it can be used in guard clauses.
|
||||
has_underscore_value = Keyword.key?(keyword, :_)
|
||||
underscore_value = Keyword.get(keyword, :_, { :_, 0, nil })
|
||||
keyword = Keyword.delete keyword, :_
|
||||
|
||||
## Examples
|
||||
|
||||
iex> record = {User, "john", 27}
|
||||
iex> Record.is_record(record, User)
|
||||
true
|
||||
|
||||
"""
|
||||
defmacro is_record(data, kind) do
|
||||
case Macro.Env.in_guard?(__CALLER__) do
|
||||
true ->
|
||||
quote do
|
||||
is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0
|
||||
and :erlang.element(1, unquote(data)) == unquote(kind)
|
||||
end
|
||||
false ->
|
||||
quote do
|
||||
result = unquote(data)
|
||||
is_tuple(result) and tuple_size(result) > 0
|
||||
and :erlang.element(1, result) == unquote(kind)
|
||||
iterator = fn({field, default}, each_keyword) ->
|
||||
new_fields =
|
||||
case Keyword.key?(each_keyword, field) do
|
||||
true -> Keyword.get(each_keyword, field)
|
||||
false ->
|
||||
case in_match or has_underscore_value do
|
||||
true -> underscore_value
|
||||
false -> Macro.escape(default)
|
||||
end
|
||||
end
|
||||
|
||||
{ new_fields, Keyword.delete(each_keyword, field) }
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the given `data` is a record.
|
||||
|
||||
This is implemented as a macro so it can be used in guard clauses.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> record = {User, "john", 27}
|
||||
iex> Record.is_record(record)
|
||||
true
|
||||
iex> tuple = {}
|
||||
iex> Record.is_record(tuple)
|
||||
false
|
||||
|
||||
"""
|
||||
defmacro is_record(data) do
|
||||
case Macro.Env.in_guard?(__CALLER__) do
|
||||
true ->
|
||||
quote do
|
||||
is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0
|
||||
and is_atom(:erlang.element(1, unquote(data)))
|
||||
end
|
||||
false ->
|
||||
quote do
|
||||
result = unquote(data)
|
||||
is_tuple(result) and tuple_size(result) > 0
|
||||
and is_atom(:erlang.element(1, result))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Defines a set of macros to create and access a record.
|
||||
|
||||
The macros are going to have `name`, a tag (which defaults)
|
||||
to the name if none is given, and a set of fields given by
|
||||
`kv`.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule User do
|
||||
require Record
|
||||
Record.defrecord :user, [name: "meg", age: "25"]
|
||||
end
|
||||
|
||||
In the example above, a set of macros named `user` but with different
|
||||
arities will be defined to manipulate the underlying record:
|
||||
|
||||
# To create records
|
||||
record = user() #=> {:user, "meg", 25}
|
||||
record = user(age: 26) #=> {:user, "meg", 26}
|
||||
|
||||
# To get a field from the record
|
||||
user(record, :name) #=> "meg"
|
||||
|
||||
# To update the record
|
||||
user(record, age: 26) #=> {:user, "meg", 26}
|
||||
|
||||
# Convert a record to a keyword list
|
||||
user(record) #=> [name: "meg", age: 26]
|
||||
|
||||
The generated macros can also be used in order to pattern match on records and
|
||||
to bind variables during the match:
|
||||
|
||||
record = user() #=> {:user, "meg", 25}
|
||||
|
||||
user(name: name) = record
|
||||
name #=> "meg"
|
||||
|
||||
By default, Elixir uses the record name as the first element of
|
||||
the tuple (the tag). But it can be changed to something else:
|
||||
|
||||
defmodule User do
|
||||
require Record
|
||||
Record.defrecord :user, User, name: nil
|
||||
end
|
||||
|
||||
require User
|
||||
User.user() #=> {User, nil}
|
||||
|
||||
## Defining extracted records with anonymous functions
|
||||
|
||||
If a record defines an anonymous function, an ArgumentError
|
||||
will occur if you attempt to create a record with it.
|
||||
This can occur unintentionally when defining a record after extracting
|
||||
it from an Erlang library that uses anonymous functions for defaults.
|
||||
|
||||
Record.defrecord :my_rec, Record.extract(...)
|
||||
#=> ** (ArgumentError) invalid value for record field fun_field,
|
||||
cannot escape #Function<12.90072148/2 in :erl_eval.expr/5>.
|
||||
|
||||
To work around this error, redefine the field with your own &M.f/a function,
|
||||
like so:
|
||||
|
||||
defmodule MyRec do
|
||||
require Record
|
||||
Record.defrecord :my_rec, Record.extract(...) |> Keyword.merge(fun_field: &__MODULE__.foo/2)
|
||||
def foo(bar, baz), do: IO.inspect({bar, baz})
|
||||
end
|
||||
"""
|
||||
defmacro defrecord(name, tag \\ nil, kv) do
|
||||
quote bind_quoted: [name: name, tag: tag, kv: kv] do
|
||||
tag = tag || name
|
||||
fields = Record.__fields__(:defrecord, kv)
|
||||
|
||||
defmacro(unquote(name)(args \\ [])) do
|
||||
Record.__access__(unquote(tag), unquote(fields), args, __CALLER__)
|
||||
end
|
||||
|
||||
defmacro(unquote(name)(record, args)) do
|
||||
Record.__access__(unquote(tag), unquote(fields), record, args, __CALLER__)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Same as `defrecord/3` but generates private macros.
|
||||
"""
|
||||
defmacro defrecordp(name, tag \\ nil, kv) do
|
||||
quote bind_quoted: [name: name, tag: tag, kv: kv] do
|
||||
tag = tag || name
|
||||
fields = Record.__fields__(:defrecordp, kv)
|
||||
|
||||
defmacrop(unquote(name)(args \\ [])) do
|
||||
Record.__access__(unquote(tag), unquote(fields), args, __CALLER__)
|
||||
end
|
||||
|
||||
defmacrop(unquote(name)(record, args)) do
|
||||
Record.__access__(unquote(tag), unquote(fields), record, args, __CALLER__)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Normalizes of record fields to have default values.
|
||||
@doc false
|
||||
def __fields__(type, fields) do
|
||||
:lists.map(fn
|
||||
{key, val} when is_atom(key) ->
|
||||
try do
|
||||
Macro.escape(val)
|
||||
rescue
|
||||
e in [ArgumentError] ->
|
||||
raise ArgumentError, "invalid value for record field #{key}, " <> Exception.message(e)
|
||||
else
|
||||
val -> {key, val}
|
||||
end
|
||||
key when is_atom(key) ->
|
||||
{key, nil}
|
||||
other ->
|
||||
raise ArgumentError, "#{type} fields must be atoms, got: #{inspect other}"
|
||||
end, fields)
|
||||
end
|
||||
|
||||
# Callback invoked from record/0 and record/1 macros.
|
||||
@doc false
|
||||
def __access__(atom, fields, args, caller) do
|
||||
cond do
|
||||
is_atom(args) ->
|
||||
index(atom, fields, args)
|
||||
Keyword.keyword?(args) ->
|
||||
create(atom, fields, args, caller)
|
||||
true ->
|
||||
case Macro.expand(args, caller) do
|
||||
{:{}, _, [^atom|list]} when length(list) == length(fields) ->
|
||||
record = List.to_tuple([atom|list])
|
||||
Macro.escape(Record.__keyword__(atom, fields, record))
|
||||
{^atom, arg} when length(fields) == 1 ->
|
||||
Macro.escape(Record.__keyword__(atom, fields, {atom, arg}))
|
||||
_ ->
|
||||
quote do: Record.__keyword__(unquote(atom), unquote(fields), unquote(args))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Callback invoked from the record/2 macro.
|
||||
@doc false
|
||||
def __access__(atom, fields, record, args, caller) do
|
||||
cond do
|
||||
is_atom(args) ->
|
||||
get(atom, fields, record, args)
|
||||
Keyword.keyword?(args) ->
|
||||
update(atom, fields, record, args, caller)
|
||||
true ->
|
||||
msg = "expected arguments to be a compile time atom or keywords, got: #{Macro.to_string args}"
|
||||
raise ArgumentError, msg
|
||||
end
|
||||
end
|
||||
|
||||
# Gets the index of field.
|
||||
defp index(atom, fields, field) do
|
||||
if index = find_index(fields, field, 0) do
|
||||
index - 1 # Convert to Elixir index
|
||||
else
|
||||
raise ArgumentError, "record #{inspect atom} does not have the key: #{inspect field}"
|
||||
end
|
||||
end
|
||||
|
||||
# Creates a new record with the given default fields and keyword values.
|
||||
defp create(atom, fields, keyword, caller) do
|
||||
in_match = Macro.Env.in_match?(caller)
|
||||
|
||||
{match, remaining} =
|
||||
Enum.map_reduce(fields, keyword, fn({field, default}, each_keyword) ->
|
||||
new_fields =
|
||||
case Keyword.has_key?(each_keyword, field) do
|
||||
true -> Keyword.get(each_keyword, field)
|
||||
false ->
|
||||
case in_match do
|
||||
true -> {:_, [], nil}
|
||||
false -> Macro.escape(default)
|
||||
end
|
||||
end
|
||||
|
||||
{new_fields, Keyword.delete(each_keyword, field)}
|
||||
end)
|
||||
{ match, remaining } = :lists.mapfoldl(iterator, keyword, fields)
|
||||
|
||||
case remaining do
|
||||
[] ->
|
||||
{:{}, [], [atom|match]}
|
||||
[] -> { :{}, caller.line, [atom|match] }
|
||||
_ ->
|
||||
keys = for {key, _} <- remaining, do: key
|
||||
raise ArgumentError, "record #{inspect atom} does not have the key: #{inspect hd(keys)}"
|
||||
keys = lc { key, _ } inlist remaining, do: key
|
||||
raise "record #{inspect atom} does not have the keys: #{inspect keys}"
|
||||
end
|
||||
end
|
||||
|
||||
# Updates a record given by var with the given keyword.
|
||||
defp update(atom, fields, var, keyword, caller) do
|
||||
if Macro.Env.in_match?(caller) do
|
||||
raise ArgumentError, "cannot invoke update style macro inside match"
|
||||
end
|
||||
defp is_orddict(list) when is_list(list), do: :lists.all(is_orddict_tuple(&1), list)
|
||||
defp is_orddict(_), do: false
|
||||
|
||||
Enum.reduce keyword, var, fn({key, value}, acc) ->
|
||||
index = find_index(fields, key, 0)
|
||||
if index do
|
||||
quote do
|
||||
:erlang.setelement(unquote(index), unquote(acc), unquote(value))
|
||||
end
|
||||
else
|
||||
raise ArgumentError, "record #{inspect atom} does not have the key: #{inspect key}"
|
||||
defp is_orddict_tuple({ x, _ }) when is_atom(x), do: true
|
||||
defp is_orddict_tuple(_), do: false
|
||||
|
||||
|
||||
@doc """
|
||||
Main entry point for records definition.
|
||||
This is invoked directly by `Kernel.defrecord`.
|
||||
Returns the quoted expression of a module given by name.
|
||||
"""
|
||||
def defrecord(name, values, opts) do
|
||||
moduledoc = Keyword.get(opts, :moduledoc, false)
|
||||
block = Keyword.get(opts, :do)
|
||||
definition = Keyword.get(opts, :definition, Record.Definition)
|
||||
|
||||
quote do
|
||||
defmodule unquote(name) do
|
||||
@moduledoc unquote(moduledoc)
|
||||
Record.define_functions(__ENV__, unquote(values), unquote(definition))
|
||||
unquote(block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Gets a record key from the given var.
|
||||
defp get(atom, fields, var, key) do
|
||||
index = find_index(fields, key, 0)
|
||||
if index do
|
||||
quote do
|
||||
:erlang.element(unquote(index), unquote(var))
|
||||
end
|
||||
else
|
||||
raise ArgumentError, "record #{inspect atom} does not have the key: #{inspect key}"
|
||||
end
|
||||
end
|
||||
|
||||
defp find_index([{k, _}|_], k, i), do: i + 2
|
||||
defp find_index([{_, _}|t], k, i), do: find_index(t, k, i + 1)
|
||||
defp find_index([], _k, _i), do: nil
|
||||
|
||||
# Returns a keyword list of the record
|
||||
@doc false
|
||||
def __keyword__(atom, fields, record) do
|
||||
if is_record(record, atom) do
|
||||
[_tag|values] = Tuple.to_list(record)
|
||||
join_keyword(fields, values, [])
|
||||
else
|
||||
msg = "expected argument to be a literal atom, literal keyword or a #{inspect atom} record, got runtime: #{inspect record}"
|
||||
raise ArgumentError, msg
|
||||
# Private endpoint that defines the functions for the Record.
|
||||
def define_functions(env, values, definition) do
|
||||
# Escape the values so they are valid syntax nodes
|
||||
values = Macro.escape(values)
|
||||
|
||||
contents = [
|
||||
reflection(values),
|
||||
getters_and_setters(values, 1, [], definition),
|
||||
initializers(values),
|
||||
converters(values)
|
||||
]
|
||||
|
||||
Module.eval_quoted env, contents
|
||||
end
|
||||
|
||||
# Define __record__/1 and __record__/2 as reflection functions
|
||||
# that returns the record names and fields.
|
||||
#
|
||||
# Note that fields are *not* keywords. They are in the same
|
||||
# order as given as parameter and reflects the order of the
|
||||
# fields in the tuple.
|
||||
#
|
||||
# ## Examples
|
||||
#
|
||||
# defrecord FileInfo, atime: nil, mtime: nil
|
||||
#
|
||||
# FileInfo.__record__(:name) #=> FileInfo
|
||||
# FileInfo.__record__(:fields) #=> [atime: nil, mtime: nil]
|
||||
#
|
||||
defp reflection(values) do
|
||||
quote do
|
||||
def __access__(caller, args), do: Record.access(caller, __MODULE__, __record__(:fields), args)
|
||||
def __record__(kind, _), do: __record__(kind)
|
||||
def __record__(:name), do: __MODULE__
|
||||
def __record__(:fields), do: unquote(values)
|
||||
end
|
||||
end
|
||||
|
||||
defp join_keyword([{field, _default}|fields], [value|values], acc),
|
||||
do: join_keyword(fields, values, [{field, value}| acc])
|
||||
defp join_keyword([], [], acc),
|
||||
do: :lists.reverse(acc)
|
||||
# Define initializers methods. For a declaration like:
|
||||
#
|
||||
# defrecord FileInfo, atime: nil, mtime: nil
|
||||
#
|
||||
# It will define three methods:
|
||||
#
|
||||
# def new() do
|
||||
# new([])
|
||||
# end
|
||||
#
|
||||
# def new([]) do
|
||||
# { FileInfo, nil, nil }
|
||||
# end
|
||||
#
|
||||
# def new(opts) do
|
||||
# { FileInfo, Keyword.get(opts, :atime), Keyword.get(opts, :mtime) }
|
||||
# end
|
||||
#
|
||||
defp initializers(values) do
|
||||
defaults = lc value inlist values, do: elem(value, 2)
|
||||
|
||||
# For each value, define a piece of code that will receive
|
||||
# an ordered dict of options (opts) and it will try to fetch
|
||||
# the given key from the ordered dict, falling back to the
|
||||
# default value if one does not exist.
|
||||
selective = lc { k, v } inlist values do
|
||||
quote do: Keyword.get(opts, unquote(k), unquote(v))
|
||||
end
|
||||
|
||||
quote do
|
||||
def new(), do: new([])
|
||||
def new([]), do: { __MODULE__, unquote_splicing(defaults) }
|
||||
def new(opts) when is_list(opts), do: { __MODULE__, unquote_splicing(selective) }
|
||||
def new(tuple) when is_tuple(tuple), do: setelem(tuple, 1, __MODULE__)
|
||||
end
|
||||
end
|
||||
|
||||
# Define converters method(s). For a declaration like:
|
||||
#
|
||||
# defrecord FileInfo, atime: nil, mtime: nil
|
||||
#
|
||||
# It will define one method, to_keywords, which will return a Keyword
|
||||
#
|
||||
# [atime: nil, mtime: nil]
|
||||
#
|
||||
defp converters(values) do
|
||||
sorted = lc { k, _ } inlist values do
|
||||
index = find_index(values, k, 1)
|
||||
{ k, quote(do: :erlang.element(unquote(index + 1), record)) }
|
||||
end
|
||||
|
||||
quote do
|
||||
def to_keywords(record) do
|
||||
unquote(:orddict.from_list(sorted))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp find_index([{ k, _ }|_], k, i), do: i
|
||||
defp find_index([{ _, _ }|t], k, i), do: find_index(t, k, i + 1)
|
||||
|
||||
# Implement getters and setters for each attribute.
|
||||
# For a declaration like:
|
||||
#
|
||||
# defrecord FileInfo, atime: nil, mtime: nil
|
||||
#
|
||||
# It will define four methods:
|
||||
#
|
||||
# def :atime.(record) do
|
||||
# elem(record, 2)
|
||||
# end
|
||||
#
|
||||
# def :atime.(record, value) do
|
||||
# setelem(record, 2, value)
|
||||
# end
|
||||
#
|
||||
# def :mtime.(record) do
|
||||
# elem(record, 3)
|
||||
# end
|
||||
#
|
||||
# def :mtime.(record, value) do
|
||||
# setelem(record, value, 3)
|
||||
# end
|
||||
#
|
||||
# `element` and `setelement` will simply get and set values
|
||||
# from the record tuple. Notice that `:atime.(record)` is just
|
||||
# a dynamic way to say `atime(record)`. We need to use this
|
||||
# syntax as `unquote(key)(record)` wouldn't be valid (as Elixir
|
||||
# allows you to parenthesis just on specific cases as `foo()`
|
||||
# and `foo.bar()`)
|
||||
defp getters_and_setters([{ key, default }|t], i, acc, definition) do
|
||||
i = i + 1
|
||||
functions = definition.functions_for(key, default, i)
|
||||
getters_and_setters(t, i, [functions | acc], definition)
|
||||
end
|
||||
|
||||
defp getters_and_setters([], _i, acc, _), do: acc
|
||||
end
|
||||
|
||||
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 Erlang.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] = Erlang.filename.split(to_char_list(file))
|
||||
case Erlang.code.lib_dir(to_char_list(app)) do
|
||||
{ :error, _ } ->
|
||||
raise ArgumentError, "Lib file #{to_binary(file)} could not be found"
|
||||
libpath ->
|
||||
retrieve_record name, Erlang.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, 1) 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 Erlang.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, _ } = Erlang.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.Definition do
|
||||
@moduledoc false
|
||||
|
||||
# Main entry point. It defines both default functions
|
||||
# via `default_for` and extensions via `extension_for`.
|
||||
def functions_for(key, default, i) do
|
||||
[
|
||||
default_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(key).(record) do
|
||||
:erlang.element(unquote(i), record)
|
||||
end
|
||||
|
||||
def unquote(key).(value, record) do
|
||||
:erlang.setelement(unquote(i), record, value)
|
||||
end
|
||||
|
||||
def unquote(update).(function, record) do
|
||||
current = :erlang.element(unquote(i), record)
|
||||
:erlang.setelement(unquote(i), 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}"
|
||||
|
||||
quote do
|
||||
def unquote(prepend).(value, record) do
|
||||
current = :erlang.element(unquote(i), record)
|
||||
:erlang.setelement(unquote(i), record, value ++ current)
|
||||
end
|
||||
|
||||
def unquote(merge).(value, record) do
|
||||
current = :erlang.element(unquote(i), record)
|
||||
:erlang.setelement(unquote(i), 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}"
|
||||
|
||||
quote do
|
||||
def unquote(increment).(value // 1, record) do
|
||||
current = :erlang.element(unquote(i), record)
|
||||
:erlang.setelement(unquote(i), 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}"
|
||||
|
||||
quote do
|
||||
def unquote(toggle).(value // false, record) do
|
||||
current = :erlang.element(unquote(i), record)
|
||||
:erlang.setelement(unquote(i), record, not current)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def extension_for(_, _, _), do: nil
|
||||
end
|
||||
|
||||
@@ -1,117 +0,0 @@
|
||||
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 extract(name, from: file) when is_binary(file) do
|
||||
extract_record(name, from_file(file))
|
||||
end
|
||||
|
||||
# Retrieve a record definition from an Erlang file using
|
||||
# the same lookup as the *include_lib* attribute from Erlang modules.
|
||||
def extract(name, from_lib: file) when is_binary(file) do
|
||||
extract_record(name, from_lib_file(file))
|
||||
end
|
||||
|
||||
# Retrieve all records definitions from an Erlang file using
|
||||
# the same lookup as the *include* attribute from Erlang modules.
|
||||
def extract_all(from: file) when is_binary(file) do
|
||||
extract_all_records(from_file(file))
|
||||
end
|
||||
|
||||
# Retrieve all records definitions from an Erlang file using
|
||||
# the same lookup as the *include_lib* attribute from Erlang modules.
|
||||
def extract_all(from_lib: file) when is_binary(file) do
|
||||
extract_all_records(from_lib_file(file))
|
||||
end
|
||||
|
||||
# Find file using the same lookup as the *include* attribute from Erlang modules.
|
||||
defp from_file(file) do
|
||||
file = String.to_char_list(file)
|
||||
case :code.where_is_file(file) do
|
||||
:non_existing -> file
|
||||
realfile -> realfile
|
||||
end
|
||||
end
|
||||
|
||||
# Find file using the same lookup as the *include_lib* attribute from Erlang modules.
|
||||
defp from_lib_file(file) do
|
||||
[app|path] = :filename.split(String.to_char_list(file))
|
||||
case :code.lib_dir(List.to_atom(app)) do
|
||||
{:error, _} ->
|
||||
raise ArgumentError, "lib file #{file} could not be found"
|
||||
libpath ->
|
||||
:filename.join([libpath|path])
|
||||
end
|
||||
end
|
||||
|
||||
# Retrieve the record with the given name from the given file
|
||||
defp extract_record(name, file) do
|
||||
form = read_file(file)
|
||||
records = extract_records(form)
|
||||
if record = List.keyfind(records, name, 0) do
|
||||
parse_record(record, form)
|
||||
else
|
||||
raise ArgumentError, "no record #{name} found at #{file}"
|
||||
end
|
||||
end
|
||||
|
||||
# Retrieve all records from the given file
|
||||
defp extract_all_records(file) do
|
||||
form = read_file(file)
|
||||
records = extract_records(form)
|
||||
for rec = {name, _fields} <- records, do: {name, parse_record(rec, form)}
|
||||
end
|
||||
|
||||
# Parse the given file and extract all existent records.
|
||||
defp extract_records(form) do
|
||||
for {:attribute, _, :record, record} <- form, do: record
|
||||
end
|
||||
|
||||
# Read a file and return its abstract syntax form that also
|
||||
# includes record but with macros and other attributes expanded,
|
||||
# such as "-include(...)" and "-include_lib(...)". This is done
|
||||
# by using Erlang's epp.
|
||||
defp read_file(file) do
|
||||
case :epp.parse_file(file, []) do
|
||||
{:ok, form} ->
|
||||
form
|
||||
other ->
|
||||
raise "error parsing file #{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}, form) do
|
||||
cons = List.foldr fields, {nil, 0}, fn f, acc ->
|
||||
{:cons, 0, parse_field(f), acc}
|
||||
end
|
||||
eval_record(cons, form)
|
||||
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
|
||||
|
||||
defp eval_record(cons, form) do
|
||||
form = form ++
|
||||
[{:function, 0, :hello, 0, [
|
||||
{:clause, 0, [], [], [cons]}]}]
|
||||
|
||||
{:function, 0, :hello, 0, [
|
||||
{:clause, 0, [], [], [record_ast]}]} = :erl_expand_records.module(form, []) |> List.last
|
||||
|
||||
{:value, record, _} = :erl_eval.expr(record_ast, [])
|
||||
record
|
||||
end
|
||||
end
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user