Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6d98af49c9 | ||
|
|
98485daab0 | ||
|
|
59f47949a9 | ||
|
|
fca5f1cf73 | ||
|
|
153ec434de | ||
|
|
061a989935 | ||
|
|
21f10f7296 | ||
|
|
336452f81b | ||
|
|
511a51ba89 | ||
|
|
7e4f9c5647 | ||
|
|
b37ecb0f8e | ||
|
|
ac947a5d1d | ||
|
|
82a511aafe | ||
|
|
7c2e65f418 | ||
|
|
3d04cb0aa0 | ||
|
|
8fa9d728f1 | ||
|
|
2eee2fc6a4 | ||
|
|
b1a10f2bb8 | ||
|
|
dcb4f22af2 | ||
|
|
6ba4cf00ab | ||
|
|
3eeded089d | ||
|
|
f4c827b68c | ||
|
|
9ef89241ed | ||
|
|
49351886cf | ||
|
|
9c7edf3117 | ||
|
|
32825ebaa0 | ||
|
|
5e715aa16e | ||
|
|
9b54efb9dd | ||
|
|
82182818cb | ||
|
|
53f16f5a82 | ||
|
|
d86751501f | ||
|
|
906df8c981 | ||
|
|
04717e4c19 | ||
|
|
a3e313b582 | ||
|
|
a4044d898d | ||
|
|
b932a52d55 | ||
|
|
8d269932a6 | ||
|
|
c1c4b1ace9 | ||
|
|
e284007968 | ||
|
|
b6ef2bd656 | ||
|
|
12b0906d61 | ||
|
|
2b59405756 | ||
|
|
4231a62c67 | ||
|
|
35e7c8c38c | ||
|
|
8e4d718b93 | ||
|
|
3ad9193b4b | ||
|
|
d655a2884e | ||
|
|
5529c5fa4e |
@@ -0,0 +1,18 @@
|
||||
version: 1-{branch}+{build}
|
||||
|
||||
build_script:
|
||||
- cmd: C:\MinGW\msys\1.0\bin\make
|
||||
- cmd: rmdir /s /q .git
|
||||
before_test:
|
||||
- cmd: set PATH=%PATH%;C:\Program Files\erl8.3\erts-8.3\bin
|
||||
test_script:
|
||||
- cmd: C:\MinGW\msys\1.0\bin\make --keep-going test_windows
|
||||
|
||||
environment:
|
||||
ELIXIR_ASSERT_TIMEOUT: 2000
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- platform: x86
|
||||
- platform: x64
|
||||
- platform: Any CPU
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
env:
|
||||
CIRRUS_CLONE_DEPTH: 50
|
||||
ELIXIR_ASSERT_TIMEOUT: 2000
|
||||
ELIXIRC_OPTS: "--warnings-as-errors"
|
||||
ERLC_OPTS: "warnings_as_errors"
|
||||
LANG: C.UTF-8
|
||||
|
||||
test_template: &DEFAULT_TEST_SETTINGS
|
||||
# don't cancel the task execution if it's master or a release branch
|
||||
auto_cancellation: $CIRRUS_BRANCH != 'master' && $CIRRUS_BRANCH !=~ 'v\d+\.\d+.*'
|
||||
|
||||
test_freebsd_task:
|
||||
<<: *DEFAULT_TEST_SETTINGS
|
||||
|
||||
name: FreeBSD 13.0
|
||||
alias: FreeBSD Stable
|
||||
|
||||
freebsd_instance:
|
||||
image_family: freebsd-13-0
|
||||
cpu: 8
|
||||
memory: 7424Mi
|
||||
|
||||
env:
|
||||
CHECK_REPRODUCIBLE: true
|
||||
LC_ALL: en_US.UTF-8
|
||||
PATH: $PATH:/usr/local/lib/erlang22/bin
|
||||
|
||||
install_script:
|
||||
- pkg install -y erlang-runtime22 git gmake
|
||||
- rm -rf .git
|
||||
- gmake compile
|
||||
|
||||
build_info_script: bin/elixir --version
|
||||
|
||||
test_formatted_script:
|
||||
- gmake test_formatted &&
|
||||
echo "All Elixir source code files are properly formatted."
|
||||
|
||||
dialyzer_script: dialyzer -pa lib/elixir/ebin --build_plt --output_plt elixir.plt --apps lib/elixir/ebin/elixir.beam lib/elixir/ebin/Elixir.Kernel.beam
|
||||
|
||||
test_erlang_script: gmake test_erlang
|
||||
|
||||
test_elixir_script: gmake test_elixir
|
||||
|
||||
check_reproducible_script: |
|
||||
if [ -n "$CHECK_REPRODUCIBLE" ]; then
|
||||
gmake check_reproducible
|
||||
else
|
||||
echo "The reproducibility of the build is only checked in the last stable Erlang/OTP version."
|
||||
fi
|
||||
+2
-3
@@ -1,10 +1,9 @@
|
||||
[
|
||||
inputs: [
|
||||
"lib/*/{lib,unicode,test}/**/*.{ex,exs}",
|
||||
"lib/*/*.exs",
|
||||
"lib/ex_unit/examples/*.exs",
|
||||
".formatter.exs"
|
||||
"lib/*/mix.exs"
|
||||
],
|
||||
|
||||
locals_without_parens: [
|
||||
# Formatter tests
|
||||
assert_format: 2,
|
||||
|
||||
@@ -1,3 +1 @@
|
||||
lib/elixir/test/elixir/fixtures/*.txt text eol=lf
|
||||
*.ex diff=elixir
|
||||
*.exs diff=elixir
|
||||
|
||||
@@ -1,106 +0,0 @@
|
||||
name: CI
|
||||
|
||||
on: [pull_request, push]
|
||||
|
||||
env:
|
||||
ELIXIR_ASSERT_TIMEOUT: 2000
|
||||
ELIXIRC_OPTS: "--warnings-as-errors"
|
||||
ERLC_OPTS: "warnings_as_errors"
|
||||
LANG: C.UTF-8
|
||||
|
||||
jobs:
|
||||
test_linux:
|
||||
name: Linux, ${{ matrix.otp_release }}, Ubuntu 18.04
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
otp_release: ['OTP-24.0', 'OTP-23.3', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0']
|
||||
development: [false]
|
||||
include:
|
||||
- otp_release: master
|
||||
development: true
|
||||
- otp_release: maint
|
||||
development: true
|
||||
runs-on: ubuntu-18.04
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 50
|
||||
- name: Install Erlang/OTP
|
||||
run: |
|
||||
cd $RUNNER_TEMP
|
||||
wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-18.04/${{ matrix.otp_release }}.tar.gz
|
||||
mkdir -p otp
|
||||
tar zxf otp.tar.gz -C otp --strip-components=1
|
||||
otp/Install -minimal $(pwd)/otp
|
||||
echo "$(pwd)/otp/bin" >> $GITHUB_PATH
|
||||
- name: Compile Elixir
|
||||
run: |
|
||||
rm -rf .git
|
||||
make compile
|
||||
- name: Build info
|
||||
run: bin/elixir --version
|
||||
- name: Check format
|
||||
run: make test_formatted && echo "All Elixir source code files are properly formatted."
|
||||
- name: Dyalizer
|
||||
run: dialyzer -pa lib/elixir/ebin --build_plt --output_plt elixir.plt --apps lib/elixir/ebin/elixir.beam lib/elixir/ebin/Elixir.Kernel.beam
|
||||
- name: Erlang test suite
|
||||
run: make test_erlang
|
||||
continue-on-error: ${{ matrix.development }}
|
||||
- name: Elixir test suite
|
||||
run: make test_elixir
|
||||
continue-on-error: ${{ matrix.development }}
|
||||
- name: Check reproducible builds
|
||||
run: taskset 1 make check_reproducible
|
||||
if: matrix.otp_release == 'OTP-24.0'
|
||||
|
||||
test_windows:
|
||||
name: Windows, OTP-${{ matrix.otp_release }}, Windows Server 2019
|
||||
strategy:
|
||||
matrix:
|
||||
otp_release: ['22.3']
|
||||
runs-on: windows-2019
|
||||
steps:
|
||||
- name: Configure Git
|
||||
run: git config --global core.autocrlf input
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 50
|
||||
- name: Cache Erlang/OTP package
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: C:\Users\runneradmin\AppData\Local\Temp\chocolatey\erlang
|
||||
key: OTP-${{ matrix.otp_release }}-windows-2019
|
||||
- name: Install Erlang/OTP
|
||||
run: choco install -y erlang --version ${{ matrix.otp_release }}
|
||||
- name: Compile Elixir
|
||||
run: |
|
||||
remove-item '.git' -recurse -force
|
||||
make compile
|
||||
- name: Build info
|
||||
run: bin/elixir --version
|
||||
- name: Check format
|
||||
run: make test_formatted && echo "All Elixir source code files are properly formatted."
|
||||
- name: Erlang test suite
|
||||
run: make --keep-going test_erlang
|
||||
- name: Elixir test suite
|
||||
run: |
|
||||
del c:/Windows/System32/drivers/etc/hosts
|
||||
make --keep-going test_elixir
|
||||
|
||||
check_posix_compliant:
|
||||
name: Check POSIX-compliant
|
||||
runs-on: ubuntu-18.04
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 50
|
||||
- name: Install Shellcheck
|
||||
run: |
|
||||
sudo apt update
|
||||
sudo apt install -y shellcheck
|
||||
- name: Check POSIX-compliant
|
||||
run: |
|
||||
shellcheck -e SC2039,2086 bin/elixir && echo "bin/elixir is POSIX compliant"
|
||||
shellcheck bin/elixirc && echo "bin/elixirc is POSIX compliant"
|
||||
shellcheck bin/iex && echo "bin/iex is POSIX compliant"
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
language: bash
|
||||
sudo: false
|
||||
|
||||
env:
|
||||
global:
|
||||
- ELIXIR_ASSERT_TIMEOUT=2000
|
||||
matrix:
|
||||
- OTP_RELEASE=OTP-20.0
|
||||
- OTP_RELEASE=OTP-20.1
|
||||
- OTP_RELEASE=OTP-20.2
|
||||
- OTP_RELEASE=OTP-20.3
|
||||
- OTP_RELEASE=OTP-21.0
|
||||
- OTP_RELEASE=OTP-21.1
|
||||
- OTP_RELEASE=maint
|
||||
- OTP_RELEASE=master
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
allow_failures:
|
||||
- env: OTP_RELEASE=maint
|
||||
- env: OTP_RELEASE=master
|
||||
|
||||
install:
|
||||
- wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-14.04/${OTP_RELEASE}.tar.gz
|
||||
- mkdir -p otp
|
||||
- tar zxf otp.tar.gz -C otp --strip-components=1
|
||||
- otp/Install -minimal $(pwd)/otp
|
||||
- PATH=$(pwd)/otp/bin:$PATH
|
||||
|
||||
script:
|
||||
- make compile
|
||||
- rm -rf .git
|
||||
- make test
|
||||
- dialyzer -pa lib/elixir/ebin --build_plt --output_plt elixir.plt --apps lib/elixir/ebin/elixir.beam lib/elixir/ebin/Elixir.Kernel.beam
|
||||
|
||||
notifications:
|
||||
recipients:
|
||||
- jose.valim@gmail.com
|
||||
- eric.meadows.jonsson@gmail.com
|
||||
- lexmag@me.com
|
||||
- an.leopardi@gmail.com
|
||||
+145
-268
@@ -1,337 +1,214 @@
|
||||
# Changelog for Elixir v1.13
|
||||
# Changelog for Elixir v1.8
|
||||
|
||||
The focus behind Elixir v1.13 has been on tooling, mainly tooling related to code formatting, code fragments, code reflection, and code recompilation. A lot of this functionality will directly impact developers working on large codebases and provide meaningful quality of life improvements for those working on Elixir tooling and environments, such as IDEs, notebooks, etc.
|
||||
Elixir v1.8 comes with many improvements at the infrastructure level, improving compilation time, speeding up common patterns, and adding features around introspection of the system.
|
||||
|
||||
## Semantic recompilation
|
||||
## Custom struct inspections
|
||||
|
||||
Elixir v1.13 comes with many improvements to the compiler, so it recompiles your files less frequently. In particular:
|
||||
|
||||
* The digest of the files are considered in addition to their size. This avoids recompiling many files when switching or rebasing branches.
|
||||
|
||||
* Changing your `mix.exs` will no longer trigger a full recompilation, unless you specifically change the configurations used by the Elixir compiler (`:elixirc_paths` and `:elixirc_options`).
|
||||
|
||||
* Changing compile-time configuration files (`config/config.exs` and any other file imported from it) now only recompiles the project files that depend on the reconfigured applications, instead of a full recompilation. However, if you change the configuration of your application itself, the whole project is still recompiled.
|
||||
|
||||
* Adding, updating or removing a dependency now only recompiles the project files that depend on the modified a dependency.
|
||||
|
||||
* If your project has both Erlang and Elixir files, changing an Erlang file will now recompile only the Elixir files that depend on it.
|
||||
|
||||
In a nutshell, Elixir went from triggering full recompilations whenever any of `mix.exs`, `config/config.exs`, `src/*`, and `mix.lock` changed on disk to semantic recompilations. Now it only fully recompiles when:
|
||||
|
||||
* you change the compilation options in `mix.exs`
|
||||
* you change the configuration for the current project in `config/config.exs`
|
||||
|
||||
## mix xref
|
||||
|
||||
`mix xref` is a tool that analyzes relationships between files. By analyzing the compile-time and runtime dependencies between files, it allows developers to understand what files have to be recompiled whenever a file changes.
|
||||
|
||||
Elixir v1.13 comes with many improvements to `mix xref`, such as:
|
||||
|
||||
* `mix xref graph` now supports `--label` to be set to "compile-connected", which returns all compile-time dependencies that lead to additional transitive dependencies.
|
||||
|
||||
* A new `mix xref trace FILE` subcommand receives a file and returns all dependencies in said file, including the line and what caused said dependency (a function/macro call, an alias, a struct, etc).
|
||||
|
||||
* All `mix xref` subcommands support the `--fail-above` flag, which allows you to enforce your project has at most a certain number of compile-time cycles, transitive compile-time dependencies, etc.
|
||||
|
||||
* `mix xref graph` now supports multiple `--sink` and `--source` to be given.
|
||||
|
||||
With these improvements, it has become simpler to understand the impact code recompilation has in our codebases and how to limit it.
|
||||
|
||||
## Code fragments
|
||||
|
||||
The `Code` module got a companion module called `Code.Fragment`, which hosts functions that work on incomplete code, as is often the scenario in editors, interactive shells, etc. The module contains different heuristics to analyze the source code and return context informational.
|
||||
|
||||
Thanks to these improvements, `IEx`' autocomplete got several quality of life improvements, such as the autocompletion of sigils, structs, and paths. For example, typing `~<TAB>` now shows:
|
||||
|
||||
```iex
|
||||
iex(1)> ~
|
||||
~C (sigil_C) ~D (sigil_D) ~N (sigil_N) ~R (sigil_R)
|
||||
~S (sigil_S) ~T (sigil_T) ~U (sigil_U) ~W (sigil_W)
|
||||
~c (sigil_c) ~r (sigil_r) ~s (sigil_s) ~w (sigil_w)
|
||||
|
||||
```
|
||||
|
||||
Adding the sigil letter and pressing tab then shows the available delimiters:
|
||||
|
||||
```iex
|
||||
iex(1)> ~r
|
||||
" """ ' ''' ( / < [ { |
|
||||
|
||||
```
|
||||
|
||||
Similarly, `%<TAB>` now shows only the available structs (exceptions excluded), instead of all modules:
|
||||
Elixir now provides a derivable implementation of the `Inspect` protocol. In a nutshell, this means it is really easy to filter data from your data structures whenever they are inspected. For example, imagine you have a user struct with security and privacy sensitive information:
|
||||
|
||||
```elixir
|
||||
iex(1)> %File.St
|
||||
File.Stat File.Stream
|
||||
```
|
||||
|
||||
Once you define the struct, you can hit `tab` to show all struct fields available:
|
||||
|
||||
```elixir
|
||||
iex(1)> %URI{
|
||||
authority: fragment: host: path: port:
|
||||
query: scheme: userinfo:
|
||||
```
|
||||
|
||||
As you fill a field in, the already filled fields no longer show up:
|
||||
|
||||
```elixir
|
||||
iex(1)> %URI{path: "/example",
|
||||
authority: fragment: host: port: query:
|
||||
scheme: userinfo:
|
||||
```
|
||||
|
||||
Along the same lines, `SyntaxError` and `TokenMissingError` were improved to show a code snippet whenever possible:
|
||||
|
||||
```elixir
|
||||
$ elixir -e "hello + * world"
|
||||
** (SyntaxError) nofile:1:9: syntax error before: '*'
|
||||
|
|
||||
1 | hello + * world
|
||||
| ^
|
||||
```
|
||||
|
||||
Finally, new compilation tracers have been added, alongside a handful of functions in `Module` to retrieve module metadata, which can be used to enrich suggestions in programming environments.
|
||||
|
||||
## Extended code formatting
|
||||
|
||||
The `mix format` task has been augmented with the notion of plugins. Plugins can teach the formatter how to format new files and how to format sigils, via the `Mix.Tasks.Format` behaviour.
|
||||
|
||||
For example, imagine that your project uses Markdown in two distinct ways: via a custom `~M` sigil and via files with the `.md` and `.markdown` extensions. A custom plugin would look like this:
|
||||
|
||||
```elixir
|
||||
defmodule MixMarkdownFormatter do
|
||||
@behaviour Mix.Tasks.Format
|
||||
|
||||
def features(_opts) do
|
||||
[sigils: [:M], extensions: [".md", ".markdown"]]
|
||||
end
|
||||
|
||||
def format(contents, opts) do
|
||||
# logic that formats markdown
|
||||
end
|
||||
defmodule User do
|
||||
defstruct [:id, :name, :age, :email, :encrypted_password]
|
||||
end
|
||||
```
|
||||
|
||||
Now any application can use your formatter as follows:
|
||||
By default, if you inspect a user via `inspect(user)`, it will include all fields. This can cause fields such as `:email` and `:encrypted_password` to appear in logs, error reports, etc. You could always define a custom implementation of the `Inspect` protocol for such cases but Elixir v1.8 makes it simpler by allowing you to derive the `Inspect` protocol:
|
||||
|
||||
```elixir
|
||||
# .formatter.exs
|
||||
[
|
||||
# Define the desired plugins
|
||||
plugins: [MixMarkdownFormatter],
|
||||
# Remember to update the inputs list to include the new extensions
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}", "posts/*.{md,markdown}"]
|
||||
]
|
||||
defmodule User do
|
||||
@derive {Inspect, only: [:id, :name, :age]}
|
||||
defstruct [:id, :name, :age, :email, :encrypted_password]
|
||||
end
|
||||
```
|
||||
|
||||
Finally, the `Code` module has also been augmented with two functions: `Code.string_to_quoted_with_comments/2` and `Code.quoted_to_algebra/2`. Those functions allow someone to retrieve the Elixir AST with their original source code comments, and then convert this AST to formatted code. In other words, those functions provide a wrapper around the Elixir Code Formatter, supporting developers who wish to create tools that directly manipulate and custom format Elixir source code.
|
||||
Now all user structs will be printed with all remaining fields collapsed:
|
||||
|
||||
## v1.13.1 (2021-12-14)
|
||||
#User<id: 1, name: "Jane", age: 33, ...>
|
||||
|
||||
You can also pass `@derive {Inspect, except: [...]}` in case you want to keep all fields by default and exclude only some.
|
||||
|
||||
## Time zone database support
|
||||
|
||||
In Elixir v1.3, Elixir added four types, known as Calendar types, to work with dates and times: `Time`, `Date`, `NaiveDateTime` (without time zone) and `DateTime` (with time zone). Over the last releases we have added many enhancements to the Calendar types but the `DateTime` module always evolved at a slower pace since Elixir did not provide support for a time zone database.
|
||||
|
||||
Elixir v1.8 now defines a `Calendar.TimeZoneDatabase` behaviour, allowing developers to bring in their own time zone databases. By defining an explicit contract for time zone behaviours, Elixir can now extend the `DateTime` API, adding functions such as `DateTime.shift_zone/3`. By default, Elixir ships with a time zone database called `Calendar.UTCOnlyTimeZoneDatabase` that only handles UTC.
|
||||
|
||||
Other Calendar related improvements include the addition of `Date.day_of_year/1`, `Date.quarter_of_year/1`, `Date.year_of_era/1`, and `Date.day_of_era/1`.
|
||||
|
||||
## Faster compilation and other performance improvements
|
||||
|
||||
Due to improvements to the compiler made over the last year, Elixir v1.8 should compile code about 5% faster on average. This is yet another release where we have been able to reduce compilation times and provide a more joyful development experience to everyone.
|
||||
|
||||
The compiler also emits more efficient code for range checks in guards (such as `x in y..z`), for charlists with interpolation (such as `'foo #{bar} baz'`), and when working with records via the `Record` module.
|
||||
|
||||
Finally, EEx templates got their own share of optimizations, emitting more compact code that runs faster.
|
||||
|
||||
## Improved instrumentation and ownership with `$callers`
|
||||
|
||||
The `Task` module is one of the most common ways to spawn light-weight processes to perform work concurrently. Whenever you spawn a new process, Elixir annotates the parent of that process through the `$ancestors` key. This information can be used by instrumentation tools to track the relationship between events occurring within multiple processes. However, many times, tracking only the `$ancestors` is not enough.
|
||||
|
||||
For example, we recommend developers to always start tasks under a supervisor. This provides more visibility and allows us to control how those tasks are terminated when a node shuts down. In your code, this can be done by invoking something like: `Task.Supervisor.start_child(MySupervisor, task_specification)`. This means that, although your code is the one who invokes the task, the actual parent of the task would be the supervisor, as the supervisor is the one spawning it. We would list the supervisor as one of the `$ancestors` for the task, but the relationship between your code and the task is lost.
|
||||
|
||||
In Elixir v1.8, we now track the relationship between your code and the task via the `$callers` key in the process dictionary, which aligns well with the existing `$ancestors` key. Therefore, assuming the `Task.Supervisor` call above, we have:
|
||||
|
||||
[your code] -- calls --> [supervisor] ---- spawns --> [task]
|
||||
|
||||
which means we store the following relationships:
|
||||
|
||||
[your code] [supervisor] <-- ancestor -- [task]
|
||||
^ |
|
||||
|--------------------- caller ---------------------|
|
||||
|
||||
When a task is spawned directly from your code, without a supervisor, then the process running your code will be listed under both `$ancestors` and `$callers`.
|
||||
|
||||
This small feature is very powerful. It allows instrumentation and monitoring tools to better track and relate the events happening in your system. This feature can also be used by tools like the "Ecto Sandbox". The "Ecto Sandbox" allows developers to run tests concurrently against the database, by using transactions and an ownership mechanism where each process explicitly gets a connection assigned to it. Without `$callers`, every time you spawned a task that queries the database, the task would not know its caller, and therefore it would be unable to know which connection was assigned to it. This often meant features that relies on tasks could not be tested concurrently. With `$callers`, figuring out this relationship is trivial and you have more tests using the full power of your machine.
|
||||
|
||||
## v1.8.2 (2019-05-11)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx] Raise readable error message on bad EEx state
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Protocol] Ensure `:debug_info` is kept in protocols
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Make sure Logger v1.8 does not get stuck in discard mode
|
||||
* [Logger.Translator] Translate remote process crash in Logger
|
||||
|
||||
## v1.8.1 (2019-01-30)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] Do not show code snippets in `SyntaxError` and `TokenMissingError` if line is empty
|
||||
* [Exception] Do not fail blaming `ArgumentError` for improper lists on `apply/3`
|
||||
* [Macro] Set a max `line_length` for `Macro.to_string/1`
|
||||
* [Macro] Fix formatting of lists on module attributes for `Macro.to_string/1`
|
||||
* [String] Fix incorrect codepoint byte counting in `slice` with negative positions in ranges
|
||||
* [Task] Ensure async streams can be consumed from another process than the one that creates them
|
||||
* [URI] Undeprecate `URI.parse/1` as `URI.new/1` is too strict in many common cases
|
||||
* [URI] Make sure `URI.new/1` returns nil for empty paths
|
||||
* [Float] Fix rounding for subnormal floats
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx] Make sure the `--version` flag halts IEx
|
||||
* [IEx] Fix `IEx.pry` crash when IEx isn't running
|
||||
* [IEx.CLI] Add IEx warning when using `--remsh` with dumb terminal
|
||||
* [IEx.Helpers] Sort results by arity on `h` helper
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Make protocol consolidation part of the `Mix.install/2` cache
|
||||
* [mix compile] Do not include optional dependencies in extra applications as it is incompatible with shared deps in umbrellas
|
||||
|
||||
## v1.13.0 (2021-12-03)
|
||||
## v1.8.0 (2019-01-14)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx] Add `:parser_options` to EEx functions
|
||||
* [EEx] Optimize the default template engine to compile and execute more efficiently
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Calendar] Add `c:Calendar.year_of_era/3` to support calendars where the beginning of a new era does not align with the beginning of a new year
|
||||
* [CLI] Support `--short-version` on the CLI that does not boot the VM
|
||||
* [Code] Add `Code.string_to_quoted_with_comments/2` and `Code.quoted_to_algebra/2`
|
||||
* [Code] Add more `:token_metadata` to aliases and remote calls when parsing strings
|
||||
* [Code] Add `Code.Fragment` module to provide best-effort information from code fragments. The module currently provides an updated `Code.Fragment.cursor_context/2` with operator support and `Code.Fragment.surround_context/2` which looks at a given position in a fragment and find its surrounding delimiters
|
||||
* [Code] Allow custom sigil formatting on `Code.format_string!/2`
|
||||
* [Code] Add `{:on_module, bytecode, :none}` trace to compilation tracers
|
||||
* [Enum] Optimize `Enum.concat/1` for lists of lists
|
||||
* [Enum] Add `Enum.slide/3`
|
||||
* [Exception] Better format Elixir exceptions in Erlang
|
||||
* [Inspect] Allow default inspect fun to be set globally with `Inspect.Opts.default_inspect_fun/1`
|
||||
* [IO] Allow `:eof` to be given as limit to `IO.getn/2`
|
||||
* [Kernel] Support the `:sigils` option in `import Mod, only: :sigils` and allow the sigil modifiers to be also digits
|
||||
* [Kernel] Make `get_in` consistently abort and return `nil` when `nil` values are found (previously Elixir would raise an error in this case). This allows a user to use `get_in` as a safe navigation operator.
|
||||
* [Kernel] Improve compilation times by reducing the amount of copies of the AST across compiler processes
|
||||
* [Kernel] Raise if trying to define a module with a slash in its name
|
||||
* [Kernel] Warn when `?\` is used and there is no need for a escape character
|
||||
* [Kernel] Track structs in typespecs as export deps instead of compile-time deps
|
||||
* [Kernel] Add power operator (`**/2`)
|
||||
* [Keyword] Add `Keyword.validate/2`
|
||||
* [Keyword] Implement `Keyword.filter/2` and `Keyword.map/2`
|
||||
* [List] Add `List.keyfind!/3`
|
||||
* [Macro] Add `Macro.prewalker/1` and `Macro.postwalker/1`
|
||||
* [Macro.Env] Add the following reflection functions: `required?/2`, `lookup_import/2`, `fetch_alias/2`, and `fetch_macro_alias/2`
|
||||
* [Map] Implement `Map.filter/2` and `Map.map/2`
|
||||
* [Module] Support `:nillify_clauses` in `Module.get_definition/3`
|
||||
* [Module] Add `Module.attributes_in/1` and `Module.overridables_in/1`
|
||||
* [OptionParser] Add "did you mean?" suggestions to `OptionParser.ParseError` messages
|
||||
* [Record] Add record reflection via `@__records__`
|
||||
* [Task] Add `Task.completed/1`
|
||||
* [Task] Add `Task.ignore/1` to keep a task running but ignoring all of its results
|
||||
* [Task] Reduce the amount of copying `Task.async*` functions
|
||||
* [URI] Add `URI.new/1` and `URI.new!/1`
|
||||
* [Calendar] Add `Calendar.TimeZoneDatabase` and a `Calendar.UTCOnlyTimeZoneDatabase` implementation
|
||||
* [Calendar] Add callbacks `day_of_year/3`, `quarter_of_year/3`, `year_of_era/1`, and `day_of_era/3`
|
||||
* [Code.Formatter] Preserve user's choice of new line after most operators
|
||||
* [Date] Add `Date.day_of_year/1`, `Date.quarter_of_year/1`, `Date.year_of_era/1`, and `Date.day_of_era/1`
|
||||
* [DateTime] Add `DateTime.from_naive/3`, `DateTime.now/1`, and `DateTime.shift_zone/3`
|
||||
* [File] Allow `:raw` option in `File.exists?/2`, `File.regular?/2`, and `File.dir?/2`
|
||||
* [File] Allow POSIX time as an integer in `File.touch/2` and `File.touch!/2`
|
||||
* [Inspect] Allow `Inspect` protocol to be derivable with the `:only`/`:except` options
|
||||
* [Kernel] Do not propagate counters to variables in quote inside another quote
|
||||
* [Kernel] Warn on ambiguous use of `::` and `|` in typespecs
|
||||
* [Kernel] Add `:delegate_to` `@doc` metadata tag when using `defdelegate`
|
||||
* [Kernel] Improve compile-time building of ranges via the `..` operator
|
||||
* [Kernel] Compile charlist interpolation more efficiently
|
||||
* [Kernel] Add `floor/1` and `ceil/1` guards
|
||||
* [Kernel.SpecialForms] Add `:reduce` option to `for` comprehensions
|
||||
* [List] Add `List.myers_difference/3` and `List.improper?/1`
|
||||
* [Macro] Add `Macro.struct!/2` for proper struct resolution during compile time
|
||||
* [Map] Optimize and merge nested maps `put` and `merge` operations
|
||||
* [Range] Add `Range.disjoint?/2`
|
||||
* [Record] Reduce memory allocation when updating multiple fields in a record
|
||||
* [Registry] Allow associating a value on `:via` tuple
|
||||
* [String] Add `String.bag_distance/2`
|
||||
* [Task] Add `$callers` tracking to `Task` - this makes it easier to find which process spawned a task and use it for tracking ownership and monitoring
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Show hint if comparing different but equivalent strings
|
||||
* [ExUnit.CaptureIO] Add `with_io/3` to return result with captured io
|
||||
* [ExUnit.CaptureLog] Add `with_log/2` to return result with captured logs
|
||||
* [ExUnit] Add `ExUnit.after_suite/1` callback
|
||||
* [ExUnit.Assertions] Show last N messages (instead of first N) from mailbox on `assert_receive` fail
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.Autocomplete] Add path autocompletion whenever when the cursor follows `"./` or `"/` or `"DRIVER:` where `DRIVER` is a single letter
|
||||
* [IEx.Autocomplete] Add autocompletion for sigils, struct names, and struct fields
|
||||
* [IEx.Helpers] Allow multiple modules to be given to `r/1`
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Add `Logger.put_application_level/2`
|
||||
* [IEx.Helpers] Add `port/1` and `port/2`
|
||||
* [IEx.Server] Expose `IEx.Server.run/1` for custom IEx sessions with the ability to broker pry sessions
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Add `MIX_INSTALL_FORCE` environment variable support
|
||||
* [Mix] Support `:config` and `:system_env` in `Mix.install/2`
|
||||
* [Mix] Add `Mix.installed?/0`
|
||||
* [Mix.Shell] Add `:default` option to `Mix.Shell.yes?`
|
||||
* [mix archive.install] Run `loadconfig` before building archive
|
||||
* [mix compile] Move Elixir version check to before deps are compiled, in order to give feedback earlier
|
||||
* [mix compile.elixir] Do not recompile files if their modification time change but their contents are still the same and the .beam files are still on disk
|
||||
* [mix compile.elixir] Do not recompile all Elixir sources when Erlang modules change, only dependent ones
|
||||
* [mix compile.elixir] Do not recompile Elixir files if `mix.exs` changes, instead recompile only files using `Mix.Project` or trigger a recompilation if a compiler option changes
|
||||
* [mix compile.elixir] Only recompile needed files when a dependency is added, updated or removed
|
||||
* [mix compile.elixir] Only recompile needed files when a dependency is configured
|
||||
* [mix deps] Add `:subdir` option to git deps
|
||||
* [mix escript.install] Run `loadconfig` before building escript
|
||||
* [mix format] Support `:plugins` in `mix format` that can hook into custom extensions and sigils
|
||||
* [mix format] Add `Mix.Tasks.Format.formatter_for_file/2`
|
||||
* [mix local.rebar] No longer support `sub_dirs` in Rebar 2 to help migration towards Rebar 3
|
||||
* [mix local.rebar] Support `--if-missing` option when installing Rebar
|
||||
* [mix local.rebar] Set `REBAR_PROFILE=prod` when compiling Rebar dependencies
|
||||
* [mix test] Support `--profile-require=time` to profile the time loading test files themselves
|
||||
* [mix test] Allow filtering modules from coverage using regex
|
||||
* [mix test] Allow the exit status of ExUnit to be configured and set the default to 2
|
||||
* [mix test] Exit with a status of 3 when coverage falls below threshold
|
||||
* [mix test] Write failed manifest when suite fails due to --warnings-as-errors
|
||||
* [mix test] Ignore `MIX_TEST_PARTITION` when partitions set to 1
|
||||
* [mix xref] Support multiple sinks and sources in `mix xref graph`
|
||||
* [mix xref] Add `trace` subcommand to print compilation dependencies between files
|
||||
* [mix xref] Add `--fail-above` option to `mix xref`
|
||||
* [mix xref] Add `--label compile-connected` to `mix xref`
|
||||
* [Mix] Add `Mix.target/0` and `Mix.target/1` to control dependency management per target
|
||||
* [Mix.Project] Add `:depth` and `:parents` options to `deps_paths/1`
|
||||
* [mix archive.install] Add a timeout when installing archives
|
||||
* [mix compile] Include optional dependencies in `:extra_applications`
|
||||
* [mix escript.install] Add a timeout when installing escripts
|
||||
* [mix format] Warn when the same file may be formatted by multiple `.formatter.exs`
|
||||
* [mix test] Allow setting the maximum number of failures via `--max-failures`
|
||||
* [mix test] Print a message instead of raising on unmatched tests inside umbrella projects
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx] Accept comments in EEx between do and the first clause
|
||||
* [EEx] Accept EEx expressions where `->` is followed by newline
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Application] Allow any expression as first argument of `compile_env`
|
||||
* [Application] Warn if `Application.compile_env` or `Application.compile_env!` are called without a require
|
||||
* [Code] Make sure `:static_atoms_encoder` in `Code.string_to_quoted/2` also applies to quoted keyword keys
|
||||
* [Code] Ensure bindings with no context are returned as atoms instead of `{binding, nil}` in eval operations
|
||||
* [Inspect] Fix a bug when inspecting a non-binary bitstring with colors
|
||||
* [Kernel] Reject bidirectional formatting characters in strings and comments
|
||||
* [Kernel] Support escaping of terminators in uppercase sigils heredocs for consistency
|
||||
* [Kernel] Raise if `__CALLER__` or `__ENV__` or `__STACKTRACE__` are used in match
|
||||
* [Kernel] Improve error message on invalid argument for `byte_size` from binary concat
|
||||
* [Kernel] Raise when aliasing non-Elixir modules without `:as`
|
||||
* [Kernel] Allow `unquote_splicing` inside `%{...}` without parens
|
||||
* [Kernel] Ensure that waiting on a struct expansion inside a typespec is correctly tracked as waiting time in the compiler
|
||||
* [Kernel] Correctly parse the atom `.` as a keyword list key
|
||||
* [Kernel] Do not leak variables from the first generator in `with` and `for` special forms
|
||||
* [Kernel] Fix column number on strings with NFD characters
|
||||
* [Kernel] Fix a bug where a combination of dynamic line in `quote` with `unquote` of remote calls would emit invalid AST metadata
|
||||
* [OptionParser] Validate switch types/modifiers early on to give more precise feedback
|
||||
* [Protocol] Add `defdelegate` to the list of unallowed macros inside protocols as protocols do not allow function definitions
|
||||
* [Protocol] Warn if `@callback`, `@macrocallback` and `@optional_callbacks` are defined inside protocol
|
||||
* [Protocol] Ensure protocol metadata is deterministic on consolidation
|
||||
* [Range] Always show step when range is descending
|
||||
* [String] Update Unicode database to version 14.0
|
||||
* [URI] Only percent decode if followed by hex digits (according to https://url.spec.whatwg.org/#percent-decode)
|
||||
* [Version] Ensure proper precedence of `and`/`or` in version requirements
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Fix formatter and counters from `ExUnit.run/0` to consider all tests in a module whenever if a module's `setup_all` fails
|
||||
* [ExUnit] Allow doctests newlines to be terminated by CRLF
|
||||
* [Calendar] Allow printing dates with more than 9999 years
|
||||
* [Exception] Exclude deprecated functions in "did you mean?" hints
|
||||
* [Float] Handle subnormal floats in `Float.ratio/1`
|
||||
* [Kernel] Remove `Guard test tuple_size(...) can never succeed` Dialyzer warning on `try`
|
||||
* [Kernel] Expand operands in `size*unit` bitstring modifier instead of expecting `size` and `unit` to be literal integers
|
||||
* [Kernel] Do not deadlock on circular struct dependencies in typespecs
|
||||
* [Kernel] Raise proper error message when passing flags to the Erlang compiler that Elixir cannot handle
|
||||
* [Kernel] Do not leak variables in `cond` clauses with a single matching at compile-time clause
|
||||
* [NaiveDateTime] Do not accept leap seconds in builder and parsing functions
|
||||
* [String] Fix ZWJ handling in Unicode grapheme clusters
|
||||
* [StringIO] Handle non-printable args in StringIO gracefully
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx] Fix the loss of `.iex.exs` context after a pry session
|
||||
* [IEx] Stop evaluator before exiting IEx server to avoid evaluators leaking
|
||||
* [IEx.Helpers] Use typespec info (instead of docs chunk) and properly format callbacks in `b/1`
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Raise clear error message for invalid `:compile_time_purge_matching` configuration
|
||||
* [Logger] Fix a bug where Logger would not reset its discard counter under some scenarios
|
||||
* [Logger] Allow Logger backends to be dynamically removed when an application is shutting down
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix compile.elixir] Track transitive runtime dependencies coming from local/path dependencies
|
||||
* [mix compile.elixir] Recompile file if `@external_resource` is deleted
|
||||
* [mix compile.elixir] Print number of compiling files on all compiler cycles. This will make the `Compiling N files (.ex)` show up multiple times if necessary
|
||||
* [mix deps] Raise if local dep is unavailable while compiling
|
||||
* [mix deps.unlock] Fix blank output when unlocking a dependency that is not locked
|
||||
* [mix local.install] Do not respect `MIX_DEPS_PATH` for install commands
|
||||
* [mix release] Improve release scripts by making sure shell errors cascade (this is done by avoiding exporting and defining variables in a single step)
|
||||
* [mix release] Do not boot release if `RELEASE_COOKIE` is empty
|
||||
* [mix release] Allow releases running as a daemon to be restarted
|
||||
* [mix release] Raise proper error message when non-serializable values are in configs
|
||||
* [mix test] Fix coverage engine to also tag `case`, `cond`, and `receive` branches where the right side is a literal
|
||||
* [mix compile] Ensure changes in deps propagate to all umbrella children - this fix a long standing issue where updating a dependency would not recompile all projects accordingly, requiring a complete removal of `_build`
|
||||
* [mix compile] Avoid time drift when checking and updating compiler manifest files
|
||||
* [mix compile.app] Respect the `:only` option between umbrella siblings
|
||||
* [mix compile.protocols] Reconsolidate protocols if local dependencies are stale
|
||||
* [mix deps] Properly mark dependencies with different `:system_env` as diverged
|
||||
* [mix new] Use `--module` value when setting up filenames
|
||||
|
||||
### 3. Soft-deprecations (no warnings emitted)
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] Environment options in `Code.eval_quoted/3` and `Code.eval_string/3`, such as `:aliases` and `:tracers`, have been deprecated in favor of passing an environment
|
||||
* [IO] `:all` on `IO.getn` is deprecated in favor of `:eof`
|
||||
* [URI] `URI.parse/1` is deprecated in favor of `URI.new/1` and `URI.new!/1`
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix format] `Mix.Tasks.Format.formatter_opts_for_file/2` is deprecated in favor of `Mix.Tasks.Format.formatter_for_file/2`
|
||||
None.
|
||||
|
||||
### 4. Hard-deprecations
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] `Code.cursor_context/2` is deprecated, use `Code.Fragment.cursor_context/2` instead
|
||||
* [Macro] `Macro.to_string/2` is deprecated, use `Macro.to_string/1` instead
|
||||
* [System] `System.get_pid/0` is deprecated, use `System.pid/0` instead
|
||||
* [Version] Using `!` or `!=` in version requirements is deprecated, use `~>` or `>=` instead
|
||||
* [Enum] Passing a non-empty list to `Enum.into/2` was inconsistent with maps and is deprecated in favor of `Kernel.++/2` or `Keyword.merge/2`
|
||||
* [Inspect.Algebra] `surround/3` is deprecated in favor of `Inspect.Algebra.concat/2` and `Inspect.Algebra.nest/2`
|
||||
* [Inspect.Algebra] `surround_many/6` is deprecated in favor of `container_doc/6`
|
||||
* [Kernel] Using `@since` will now emit a unused attribute warning. Use `@doc since: "1.7.2"` instead
|
||||
* [Kernel] Passing a non-empty list as `:into` in `for` comprehensions was inconsistent with maps and is deprecated in favor of `Kernel.++/2` or `Keyword.merge/2`
|
||||
* [Kernel.ParallelCompiler] `files/2` is deprecated in favor of `compile/2`
|
||||
* [Kernel.ParallelCompiler] `files_to_path/2` is deprecated in favor of `compile_to_path/2`
|
||||
* [Kernel.ParallelRequire] `files/2` is deprecated in favor of `Kernel.ParallelCompiler.require/2`
|
||||
* [System] `:seconds`, `:milliseconds`, etc. as time units is deprecated in favor of `:second`, `:millisecond`, etc.
|
||||
* [System] `System.cwd/0` and `System.cwd!/0` are deprecated in favor of `File.cwd/0` and `File.cwd!/0`
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix escript.build] `:strip_beam` option is deprecated in favor of `:strip_beams`
|
||||
* [Mix] `:exit_code` in `Mix.raise/2` has been deprecated in favor of `:exit_status`
|
||||
* [Mix.Config] `Mix.Config` is deprecated in favor of `Config` module
|
||||
* [mix compile.erlang] Returning `{:ok, contents}` or `:error` as the callback in `Mix.Compilers.Erlang.compile/6` is deprecated in favor of returning `{:ok, contents, warnings}` or `{:error, errors, warnings}`
|
||||
|
||||
## v1.12
|
||||
## v1.7
|
||||
|
||||
The CHANGELOG for v1.12 releases can be found [in the v1.12 branch](https://github.com/elixir-lang/elixir/blob/v1.12/CHANGELOG.md).
|
||||
The CHANGELOG for v1.7 releases can be found [in the v1.7 branch](https://github.com/elixir-lang/elixir/blob/v1.7/CHANGELOG.md).
|
||||
|
||||
+10
-20
@@ -13,23 +13,17 @@ The goal of the Code of Conduct is to specify a baseline standard of behavior so
|
||||
These are the values Elixir developers should aspire to:
|
||||
|
||||
* Be friendly and welcoming
|
||||
* Be kind
|
||||
* Be patient
|
||||
* Remember that people have varying communication styles and that not everyone is using their native language. (Meaning and tone can be lost in translation.)
|
||||
* Interpret the arguments of others in good faith, do not seek to disagree.
|
||||
* When we do disagree, try to understand why.
|
||||
* Be thoughtful
|
||||
* Productive communication requires effort. Think about how your words will be interpreted.
|
||||
* Remember that sometimes it is best to refrain entirely from commenting.
|
||||
* Be respectful
|
||||
* In particular, respect differences of opinion. It is important that we resolve disagreements and differing views constructively.
|
||||
* Be constructive
|
||||
* Avoid derailing: stay on topic; if you want to talk about something else, start a new conversation.
|
||||
* Avoid unconstructive criticism: don't merely decry the current state of affairs; offer — or at least solicit — suggestions as to how things may be improved.
|
||||
* Avoid harsh words and stern tone: we are all aligned towards the well-being of the community and the progress of the ecosystem. Harsh words exclude, demotivate, and lead to unnecessary conflict.
|
||||
* Avoid snarking (pithy, unproductive, sniping comments).
|
||||
* Avoid microaggressions (brief and commonplace verbal, behavioral and environmental indignities that communicate hostile, derogatory or negative slights and insults towards a project, person or group).
|
||||
* Be responsible
|
||||
* What you say and do matters. Take responsibility for your words and actions, including their consequences, whether intended or otherwise.
|
||||
* Avoid destructive behavior
|
||||
* Derailing: stay on topic; if you want to talk about something else, start a new conversation.
|
||||
* Unconstructive criticism: don't merely decry the current state of affairs; offer (or at least solicit) suggestions as to how things may be improved.
|
||||
* Snarking (pithy, unproductive, sniping comments).
|
||||
|
||||
The following actions are explicitly forbidden:
|
||||
|
||||
@@ -45,13 +39,13 @@ If you participate in or contribute to the Elixir ecosystem in any way, you are
|
||||
|
||||
Explicit enforcement of the Code of Conduct applies to the official mediums operated by the Elixir project:
|
||||
|
||||
* The [official GitHub projects][1] and code reviews.
|
||||
* The official GitHub projects and code reviews.
|
||||
* The official elixir-lang mailing lists.
|
||||
* The **[#elixir][2]** IRC channel on [Libera.Chat][3].
|
||||
* The #elixir-lang IRC channel on Freenode.
|
||||
|
||||
Other Elixir activities (such as conferences, meetups, and unofficial forums) are encouraged to adopt this Code of Conduct. Such groups must provide their own contact information.
|
||||
Other Elixir activities (such as conferences, meetups, and other unofficial forums) are encouraged to adopt this Code of Conduct. Such groups must provide their own contact information.
|
||||
|
||||
Project maintainers may block, remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
|
||||
Project maintainers may remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by emailing: elixir-lang-conduct@googlegroups.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. **All reports will be kept confidential**.
|
||||
|
||||
@@ -59,8 +53,4 @@ Instances of abusive, harassing, or otherwise unacceptable behavior may be repor
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
This document was based on the Code of Conduct from the Go project (dated Sep/2021) and the Contributor Covenant (v1.4).
|
||||
|
||||
[1]: https://github.com/elixir-lang/
|
||||
[2]: https://web.libera.chat/#elixir
|
||||
[3]: https://libera.chat/
|
||||
This document was based on the Code of Conduct from the Go project with parts derived from Django's Code of Conduct, Rust's Code of Conduct and the Contributor Covenant.
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
### Precheck
|
||||
|
||||
* Do not use the issues tracker for help or support (try Elixir Forum, Stack Overflow, IRC, etc.)
|
||||
* For proposing a new feature, please start a discussion on the Elixir Core mailing list: https://groups.google.com/group/elixir-lang-core
|
||||
* For bugs, do a quick search and make sure the bug has not yet been reported
|
||||
* Please disclose security vulnerabilities privately at elixir-security@googlegroups.com
|
||||
* Do not use the issues tracker for guidance, questions or support (try Elixir Forum, Stack Overflow, Slack, etc. instead)
|
||||
* Finally, be nice and have fun!
|
||||
|
||||
### Environment
|
||||
@@ -14,7 +14,6 @@
|
||||
### Current behavior
|
||||
|
||||
Include code samples, errors and stacktraces if appropriate.
|
||||
If reporting a bug, please include the reproducing steps.
|
||||
|
||||
### Expected behavior
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
PREFIX ?= /usr/local
|
||||
TEST_FILES ?= "*_test.exs"
|
||||
SHARE_PREFIX ?= $(PREFIX)/share
|
||||
MAN_PREFIX ?= $(SHARE_PREFIX)/man
|
||||
CANONICAL := 1.13/
|
||||
CANONICAL ?= master/
|
||||
ELIXIRC := bin/elixirc --ignore-module-conflict $(ELIXIRC_OPTS)
|
||||
ERLC := erlc -I lib/elixir/include
|
||||
ERL_MAKE := if [ -n "$(ERLC_OPTS)" ]; then ERL_COMPILER_OPTIONS=$(ERLC_OPTS) erl -make; else erl -make; fi
|
||||
CANONICAL := v1.8/ # master/ or vMAJOR.MINOR/
|
||||
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict --warnings-as-errors
|
||||
ERLC := erlc -I lib/elixir/include +warnings_as_errors
|
||||
ERL := erl -I lib/elixir/include -noshell -pa lib/elixir/ebin
|
||||
GENERATE_APP := $(CURDIR)/lib/elixir/generate_app.escript
|
||||
VERSION := $(strip $(shell cat VERSION))
|
||||
@@ -19,18 +16,16 @@ INSTALL_DATA = $(INSTALL) -m644
|
||||
INSTALL_PROGRAM = $(INSTALL) -m755
|
||||
GIT_REVISION = $(strip $(shell git rev-parse HEAD 2> /dev/null ))
|
||||
GIT_TAG = $(strip $(shell head="$(call GIT_REVISION)"; git tag --points-at $$head 2> /dev/null | tail -1) )
|
||||
SOURCE_DATE_EPOCH_PATH = lib/elixir/tmp/ebin_reproducible
|
||||
SOURCE_DATE_EPOCH_FILE = $(SOURCE_DATE_EPOCH_PATH)/SOURCE_DATE_EPOCH
|
||||
|
||||
.PHONY: install compile erlang elixir unicode app build_plt clean_plt dialyze test check_reproducible clean clean_residual_files format install_man clean_man docs Docs.zip Precompiled.zip zips
|
||||
.PHONY: install compile erlang elixir unicode app build_plt clean_plt dialyze test clean clean_residual_files install_man clean_man docs Docs.zip Precompiled.zip zips
|
||||
.NOTPARALLEL: compile
|
||||
|
||||
#==> Functions
|
||||
|
||||
define CHECK_ERLANG_RELEASE
|
||||
erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 22)])' -s erlang halt | grep -q '^true'; \
|
||||
erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 20)])' -s erlang halt | grep -q '^true'; \
|
||||
if [ $$? != 0 ]; then \
|
||||
echo "At least Erlang/OTP 22.0 is required to build Elixir"; \
|
||||
echo "At least Erlang/OTP 20.0 is required to build Elixir"; \
|
||||
exit 1; \
|
||||
fi
|
||||
endef
|
||||
@@ -48,19 +43,7 @@ lib/$(1)/ebin/Elixir.$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1
|
||||
|
||||
test_$(1): compile $(1)
|
||||
@ echo "==> $(1) (ex_unit)"
|
||||
$(Q) cd lib/$(1) && ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/$(TEST_FILES)";
|
||||
endef
|
||||
|
||||
define WRITE_SOURCE_DATE_EPOCH
|
||||
$(shell mkdir -p $(SOURCE_DATE_EPOCH_PATH) && bin/elixir -e \
|
||||
'IO.puts System.build_info()[:date] \
|
||||
|> DateTime.from_iso8601() \
|
||||
|> elem(1) \
|
||||
|> DateTime.to_unix()' > $(SOURCE_DATE_EPOCH_FILE))
|
||||
endef
|
||||
|
||||
define READ_SOURCE_DATE_EPOCH
|
||||
$(strip $(shell cat $(SOURCE_DATE_EPOCH_FILE)))
|
||||
$(Q) cd lib/$(1) && ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/*_test.exs";
|
||||
endef
|
||||
|
||||
#==> Compilation tasks
|
||||
@@ -76,14 +59,14 @@ compile: erlang $(APP) elixir
|
||||
|
||||
erlang: $(PARSER)
|
||||
$(Q) if [ ! -f $(APP) ]; then $(call CHECK_ERLANG_RELEASE); fi
|
||||
$(Q) cd lib/elixir && mkdir -p ebin && $(ERL_MAKE)
|
||||
$(Q) cd lib/elixir && mkdir -p ebin && erl -make
|
||||
|
||||
$(PARSER): lib/elixir/src/elixir_parser.yrl
|
||||
$(Q) erlc -o $@ +'{verbose,true}' +'{report,true}' $<
|
||||
|
||||
# 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
|
||||
# then Mix and then compile EEx fully
|
||||
elixir: stdlib lib/eex/ebin/Elixir.EEx.beam mix ex_unit logger eex iex
|
||||
|
||||
stdlib: $(KERNEL) VERSION
|
||||
@@ -92,9 +75,10 @@ $(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
|
||||
echo "==> bootstrap (compile)"; \
|
||||
$(ERL) -s elixir_compiler bootstrap -s erlang halt; \
|
||||
fi
|
||||
$(Q) $(MAKE) unicode
|
||||
@ 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) $(MAKE) app
|
||||
|
||||
app: $(APP)
|
||||
@@ -105,6 +89,7 @@ unicode: $(UNICODE)
|
||||
$(UNICODE): lib/elixir/unicode/*
|
||||
@ echo "==> unicode (compile)";
|
||||
$(Q) $(ELIXIRC) lib/elixir/unicode/unicode.ex -o lib/elixir/ebin;
|
||||
$(Q) $(ELIXIRC) lib/elixir/unicode/properties.ex -o lib/elixir/ebin;
|
||||
$(Q) $(ELIXIRC) lib/elixir/unicode/tokenizer.ex -o lib/elixir/ebin;
|
||||
|
||||
$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
|
||||
@@ -128,32 +113,6 @@ install: compile
|
||||
done
|
||||
$(MAKE) install_man
|
||||
|
||||
check_reproducible: compile
|
||||
$(Q) echo "==> Checking for reproducible builds..."
|
||||
$(Q) rm -rf lib/*/tmp/ebin_reproducible/
|
||||
$(call WRITE_SOURCE_DATE_EPOCH)
|
||||
$(Q) mkdir -p lib/elixir/tmp/ebin_reproducible/ \
|
||||
lib/eex/tmp/ebin_reproducible/ \
|
||||
lib/ex_unit/tmp/ebin_reproducible/ \
|
||||
lib/iex/tmp/ebin_reproducible/ \
|
||||
lib/logger/tmp/ebin_reproducible/ \
|
||||
lib/mix/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/elixir/ebin/* lib/elixir/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/eex/ebin/* lib/eex/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/ex_unit/ebin/* lib/ex_unit/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/iex/ebin/* lib/iex/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/logger/ebin/* lib/logger/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/mix/ebin/* lib/mix/tmp/ebin_reproducible/
|
||||
SOURCE_DATE_EPOCH=$(call READ_SOURCE_DATE_EPOCH) $(MAKE) compile
|
||||
$(Q) echo "Diffing..."
|
||||
$(Q) bin/elixir lib/elixir/diff.exs lib/elixir/ebin/ lib/elixir/tmp/ebin_reproducible/
|
||||
$(Q) bin/elixir lib/elixir/diff.exs lib/eex/ebin/ lib/eex/tmp/ebin_reproducible/
|
||||
$(Q) bin/elixir lib/elixir/diff.exs lib/ex_unit/ebin/ lib/ex_unit/tmp/ebin_reproducible/
|
||||
$(Q) bin/elixir lib/elixir/diff.exs lib/iex/ebin/ lib/iex/tmp/ebin_reproducible/
|
||||
$(Q) bin/elixir lib/elixir/diff.exs lib/logger/ebin/ lib/logger/tmp/ebin_reproducible/
|
||||
$(Q) bin/elixir lib/elixir/diff.exs lib/mix/ebin/ lib/mix/tmp/ebin_reproducible/
|
||||
$(Q) echo "Builds are reproducible"
|
||||
|
||||
clean:
|
||||
rm -rf ebin
|
||||
rm -rf lib/*/ebin
|
||||
@@ -177,41 +136,41 @@ clean_residual_files:
|
||||
#==> Documentation tasks
|
||||
|
||||
LOGO_PATH = $(shell test -f ../docs/logo.png && echo "--logo ../docs/logo.png")
|
||||
SOURCE_REF = $(shell tag="$(call GIT_TAG)" revision="$(call GIT_REVISION)"; echo "$${tag:-$$revision}")
|
||||
SOURCE_REF = $(shell tag="$(call GIT_TAG)" revision="$(call GIT_REVISION)"; echo "$${tag:-$$revision}\c")
|
||||
DOCS_FORMAT = html
|
||||
COMPILE_DOCS = CANONICAL=$(CANONICAL) bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" --main "$(3)" --source-url "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) --output doc/$(2) --canonical "https://hexdocs.pm/$(2)/$(CANONICAL)" --homepage-url "https://elixir-lang.org/docs.html" --formatter "$(DOCS_FORMAT)" $(4)
|
||||
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 "$(call SOURCE_REF)" $(call LOGO_PATH) -o doc/$(2) -n https://hexdocs.pm/$(2)/$(CANONICAL) -p https://elixir-lang.org/docs.html -f "$(DOCS_FORMAT)" $(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,--config "lib/elixir/docs.exs")
|
||||
$(call COMPILE_DOCS,Elixir,elixir,Kernel,-c lib/elixir/docs.exs)
|
||||
|
||||
docs_eex: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (eex)"
|
||||
$(Q) rm -rf doc/eex
|
||||
$(call COMPILE_DOCS,EEx,eex,EEx,--config "lib/mix/docs.exs")
|
||||
$(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,--config "lib/mix/docs.exs")
|
||||
$(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,--config "lib/mix/docs.exs")
|
||||
$(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,--config "lib/mix/docs.exs")
|
||||
$(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,--config "lib/mix/docs.exs")
|
||||
$(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."
|
||||
@@ -231,7 +190,7 @@ Precompiled.zip: build_man compile
|
||||
|
||||
zips: Precompiled.zip Docs.zip
|
||||
@ echo ""
|
||||
@ echo "### Checksums"
|
||||
@ echo "## Checksums"
|
||||
@ echo ""
|
||||
@ shasum -a 1 < Precompiled-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA1:"
|
||||
@ shasum -a 512 < Precompiled-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA512:"
|
||||
@@ -241,7 +200,6 @@ zips: Precompiled.zip Docs.zip
|
||||
|
||||
#==> Test tasks
|
||||
|
||||
# If you modify this task, please update .cirrus.yml accordingly
|
||||
test: test_formatted test_erlang test_elixir
|
||||
|
||||
test_windows: test test_taskkill
|
||||
@@ -254,19 +212,8 @@ 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)))))
|
||||
|
||||
define FORMAT
|
||||
$(Q) if [ "$(OS)" = "Windows_NT" ]; then \
|
||||
cmd //C call ./bin/mix.bat format $(1); \
|
||||
else \
|
||||
bin/elixir bin/mix format $(1); \
|
||||
fi
|
||||
endef
|
||||
|
||||
format: compile
|
||||
$(call FORMAT)
|
||||
|
||||
test_formatted: compile
|
||||
$(call FORMAT,--check-formatted)
|
||||
bin/elixir bin/mix format --check-formatted
|
||||
|
||||
test_erlang: compile $(TEST_ERLS)
|
||||
@ echo "==> elixir (eunit)"
|
||||
@@ -277,15 +224,15 @@ $(TEST_EBIN)/%.beam: $(TEST_ERL)/%.erl
|
||||
$(Q) mkdir -p $(TEST_EBIN)
|
||||
$(Q) $(ERLC) -o $(TEST_EBIN) $<
|
||||
|
||||
test_elixir: test_stdlib test_ex_unit test_logger test_eex test_iex test_mix
|
||||
test_elixir: test_stdlib test_ex_unit test_logger test_mix test_eex test_iex
|
||||
|
||||
test_stdlib: compile
|
||||
@ echo "==> elixir (ex_unit)"
|
||||
$(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_FILES)"; \
|
||||
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_FILES)"; \
|
||||
cd lib/elixir && ../../bin/elixir -r "test/elixir/test_helper.exs" -pr "test/elixir/**/*_test.exs"; \
|
||||
fi
|
||||
|
||||
#==> Dialyzer tasks
|
||||
@@ -295,7 +242,7 @@ 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 crypto runtime_tools ftp tftp mnesia public_key asn1 hipe sasl
|
||||
$(Q) dialyzer --output_plt $(PLT) --build_plt --apps erts kernel stdlib compiler syntax_tools parsetools tools ssl inets
|
||||
|
||||
clean_plt:
|
||||
$(Q) rm -f $(PLT)
|
||||
|
||||
@@ -11,7 +11,7 @@ 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
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
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,
|
||||
@@ -22,13 +22,12 @@ limitations under the License.
|
||||
== All other files
|
||||
|
||||
Copyright 2012 Plataformatec
|
||||
Copyright 2021 The Elixir Team
|
||||
|
||||
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
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
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,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<img src="https://github.com/elixir-lang/elixir-lang.github.com/raw/master/images/logo/logo.png" width="200" alt="Elixir">
|
||||
|
||||
[](https://github.com/elixir-lang/elixir/actions?query=branch%3Amaster+workflow%3ACI) [](https://cirrus-ci.com/github/elixir-lang/elixir)
|
||||

|
||||
=========
|
||||
[](https://travis-ci.org/elixir-lang/elixir)
|
||||
|
||||
Elixir is a dynamic, functional language designed for building scalable
|
||||
and maintainable applications.
|
||||
@@ -8,74 +9,13 @@ and maintainable applications.
|
||||
For more about Elixir, installation and documentation,
|
||||
[check Elixir's website](https://elixir-lang.org/).
|
||||
|
||||
## Policies
|
||||
## Announcements
|
||||
|
||||
New releases are announced in the [announcement mailing list][8].
|
||||
You can subscribe by sending an email to elixir-lang-ann+subscribe@googlegroups.com
|
||||
and replying to the confirmation email.
|
||||
|
||||
All security releases [will be tagged with `[security]`][10]. For more
|
||||
information, please read our [Security Policy][9].
|
||||
|
||||
All interactions in our official communication channels follow our
|
||||
[Code of Conduct][1].
|
||||
|
||||
## Bug reports
|
||||
|
||||
For reporting bugs, [visit our issue tracker][2] and follow the steps
|
||||
for reporting a new issue. **Please disclose security vulnerabilities
|
||||
privately at elixir-security@googlegroups.com**.
|
||||
|
||||
## Issues tracker management
|
||||
|
||||
All currently open bugs related to the Elixir repository are listed
|
||||
in the issues tracker. The Elixir team uses the issues tracker to focus
|
||||
on *actionable items*, including planned enhancements in the short- and
|
||||
medium-term. We also do our best to label entries for clarity and to ease
|
||||
collaboration.
|
||||
|
||||
Our *actionable item policy* has some important consequences, such as:
|
||||
|
||||
* Proposing new features as well as request for support, help, and
|
||||
guidance must be done in their own spaces, detailed next.
|
||||
|
||||
* Issues where we have identified to be outside of Elixir scope,
|
||||
such as a bug upstream, will be closed (and requested to be moved
|
||||
elsewhere if appropriate).
|
||||
|
||||
* We actively close unrelated and non-actionable issues to keep the
|
||||
issues tracker tidy. However, we may get things wrong from time to
|
||||
time, so we are glad to revisit issues and reopen if necessary.
|
||||
|
||||
Keep the tone positive and be kind! For more information, see the
|
||||
[Code of Conduct][1].
|
||||
|
||||
### Proposing new features
|
||||
|
||||
For proposing new features, please start a discussion in the
|
||||
[Elixir Core mailing list][3]. Keep in mind that it is your responsibility
|
||||
to argue and explain why a feature is useful and how it will impact the
|
||||
codebase and the community.
|
||||
|
||||
Once a proposal is accepted, it will be added to [the issue tracker][2].
|
||||
Features and bug fixes that have already been merged and will be included
|
||||
in the next release are then "closed" and added to the [changelog][7].
|
||||
|
||||
### Discussions, support, and help
|
||||
|
||||
For general discussions, support, and help, please use many of the community
|
||||
spaces [listed on the sidebar of the Elixir website](https://elixir-lang.org/),
|
||||
such as forums, chat platforms, etc, where the wider community will be available
|
||||
to help you.
|
||||
New releases are announced in the [announcements mailing list](https://groups.google.com/group/elixir-lang-ann). All security releases [will be tagged with `[security]`](https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date).
|
||||
|
||||
## Compiling from source
|
||||
|
||||
For the many different ways to install Elixir,
|
||||
[see our installation instructions on the website](https://elixir-lang.org/install.html).
|
||||
However, if you want to contribute to Elixir, you will need to compile from source.
|
||||
|
||||
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang).
|
||||
After that, clone this repository to your machine, compile and test it:
|
||||
To run Elixir from source, clone this repository to your machine, compile and test it:
|
||||
|
||||
```sh
|
||||
git clone https://github.com/elixir-lang/elixir.git
|
||||
@@ -87,33 +27,67 @@ make clean test
|
||||
[this article includes important notes for compiling Elixir from source
|
||||
on Windows](https://github.com/elixir-lang/elixir/wiki/Windows).
|
||||
|
||||
In case you want to use this Elixir version as your system version,
|
||||
you need to add the `bin` directory to [your PATH environment variable](https://elixir-lang.org/install.html#setting-path-environment-variable).
|
||||
|
||||
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`.
|
||||
|
||||
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.
|
||||
|
||||
However, if tests fail, it is likely you have an outdated Erlang/OTP version
|
||||
(Elixir requires Erlang/OTP 20.0 or later). You can check your Erlang/OTP version
|
||||
by calling `erl` in the command line. You will see some information as follows:
|
||||
|
||||
Erlang/OTP 20 [erts-9.0] [smp:2:2] [async-threads:10] [kernel-poll:false]
|
||||
|
||||
If you have properly set up your dependencies and tests still fail,
|
||||
you may want to open up a bug report, as explained next.
|
||||
|
||||
## Bug reports
|
||||
|
||||
For reporting bugs, [visit our issues tracker][2] and follow the steps
|
||||
for reporting a new issue. **Please disclose security vulnerabilities
|
||||
privately at elixir-security@googlegroups.com**.
|
||||
|
||||
## Proposing new features
|
||||
|
||||
For proposing new features, please start a discussion in the
|
||||
[Elixir Core mailing list][3]. Keep in mind that it is your responsibility
|
||||
to argue and explain why a feature is useful and how it will impact the
|
||||
codebase and the community.
|
||||
|
||||
Once a proposal is accepted, it will be added to [the issues tracker][2].
|
||||
The issues tracker focuses on *actionable items* and it holds a list of
|
||||
upcoming enhancements and pending bugs. All entries in the tracker are
|
||||
tagged for clarity and to ease collaboration.
|
||||
|
||||
Features and bug fixes that have already been merged and will be included
|
||||
in the next release are marked as "closed" in the issues tracker and are
|
||||
added to the [CHANGELOG](CHANGELOG.md).
|
||||
|
||||
Finally, remember all interactions in our official spaces follow our
|
||||
[Code of Conduct][7].
|
||||
|
||||
## Contributing
|
||||
|
||||
We welcome everyone to contribute to Elixir. 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` - Elixir's kernel and standard library
|
||||
* `elixir` - Contains Elixir's kernel and stdlib
|
||||
|
||||
* `eex` - EEx is the template engine that allows you to embed Elixir
|
||||
* `eex` - Template engine that allows you to embed Elixir
|
||||
|
||||
* `ex_unit` - ExUnit is a simple test framework that ships with Elixir
|
||||
* `ex_unit` - Simple test framework that ships with Elixir
|
||||
|
||||
* `iex` - IEx stands for Interactive Elixir: Elixir's interactive shell
|
||||
* `iex` - IEx, Elixir's interactive shell
|
||||
|
||||
* `logger` - Logger is the built-in logger
|
||||
* `logger` - The built-in logger
|
||||
|
||||
* `mix` - Mix is Elixir's build tool
|
||||
* `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_#{APPLICATION}`, for example,
|
||||
also run tests for a specific framework `make test_#{NAME}`, for example,
|
||||
`make test_ex_unit`. If you just changed something in the Elixir's standard
|
||||
library, you can run only that portion through `make test_stdlib`.
|
||||
|
||||
@@ -132,7 +106,7 @@ To recompile (including Erlang modules):
|
||||
make compile
|
||||
```
|
||||
|
||||
After your changes are done, please remember to run `make format` to guarantee
|
||||
After your changes are done, please remember to run `mix format` to guarantee
|
||||
all files are properly formatted and then run the full suite with
|
||||
`make test`.
|
||||
|
||||
@@ -145,17 +119,17 @@ make clean_elixir compile
|
||||
|
||||
Similarly, if you can't get Elixir to compile or the tests to pass after
|
||||
updating an existing checkout, run `make clean compile`. You can check
|
||||
[the official build status](https://github.com/elixir-lang/elixir/actions/workflows/ci.yml).
|
||||
More tasks can be found by reading the [Makefile](Makefile).
|
||||
[the official build status on Travis-CI](https://travis-ci.org/elixir-lang/elixir).
|
||||
More tasks can be found by reading the [Makefile](./Makefile).
|
||||
|
||||
With tests running and passing, you are ready to contribute to Elixir and
|
||||
[send a pull request](https://help.github.com/articles/using-pull-requests/).
|
||||
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)
|
||||
* [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)
|
||||
|
||||
### Reviewing changes
|
||||
|
||||
@@ -178,8 +152,7 @@ another team member can merge it.
|
||||
|
||||
When the review finishes, your pull request will be squashed and merged
|
||||
into the repository. If you have carefully organized your commits and
|
||||
believe they should be merged without squashing, please mention it in
|
||||
a comment.
|
||||
believe they should be merged without squashing, leave a comment.
|
||||
|
||||
## Building documentation
|
||||
|
||||
@@ -190,45 +163,34 @@ to be installed and built alongside Elixir:
|
||||
# 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
|
||||
```
|
||||
|
||||
Now go back to Elixir's root directory and run:
|
||||
|
||||
```sh
|
||||
make docs # to generate HTML pages
|
||||
make docs DOCS_FORMAT=epub # to generate EPUB documents
|
||||
```
|
||||
|
||||
This will produce documentation sets for `elixir`, `eex`, `ex_unit`, `iex`, `logger`,
|
||||
and `mix` under the `doc` directory. If you are planning to contribute documentation,
|
||||
This will produce documentation sets for `elixir`, `mix`, etc. under
|
||||
the `doc` directory. If you are planning to contribute documentation,
|
||||
[please check our best practices for writing documentation](https://hexdocs.pm/elixir/writing-documentation.html).
|
||||
|
||||
## Development links
|
||||
|
||||
* [Elixir Getting Started guide][1]
|
||||
* [Elixir Documentation][6]
|
||||
* [Elixir Core Mailing list (development)][3]
|
||||
* [Announcement mailing list][8]
|
||||
* [Code of Conduct][1]
|
||||
* [Issue tracker][2]
|
||||
* [Changelog][7]
|
||||
* [Security Policy][9]
|
||||
* **[#elixir][4]** on [Libera.Chat][5] IRC
|
||||
* [Issues tracker][2]
|
||||
* [Code of Conduct][7]
|
||||
* **[#elixir-lang][4]** on [Freenode][5] IRC
|
||||
|
||||
[1]: CODE_OF_CONDUCT.md
|
||||
[1]: https://elixir-lang.org/getting-started/introduction.html
|
||||
[2]: https://github.com/elixir-lang/elixir/issues
|
||||
[3]: https://groups.google.com/group/elixir-lang-core
|
||||
[4]: https://web.libera.chat/#elixir
|
||||
[5]: https://libera.chat
|
||||
[4]: https://webchat.freenode.net/?channels=#elixir-lang
|
||||
[5]: http://www.freenode.net
|
||||
[6]: https://elixir-lang.org/docs.html
|
||||
[7]: CHANGELOG.md
|
||||
[8]: https://groups.google.com/group/elixir-lang-ann
|
||||
[9]: SECURITY.md
|
||||
[10]: https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date
|
||||
[7]: CODE_OF_CONDUCT.md
|
||||
|
||||
## License
|
||||
|
||||
"Elixir" and the Elixir logo are registered trademarks of The Elixir Team.
|
||||
"Elixir" and the Elixir logo are copyright (c) 2012 Plataformatec.
|
||||
|
||||
Elixir source code is released under Apache License 2.0.
|
||||
Elixir source code is released under Apache 2 License.
|
||||
|
||||
Check [NOTICE](NOTICE) and [LICENSE](LICENSE) files for more information.
|
||||
|
||||
+6
-6
@@ -4,7 +4,7 @@
|
||||
|
||||
1. Ensure you are running on the oldest supported Erlang version
|
||||
|
||||
2. Update version in /VERSION, bin/elixir and bin/elixir.bat
|
||||
2. Update version in /VERSION
|
||||
|
||||
3. Ensure /CHANGELOG.md is updated, versioned and add the current date
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
9. Publish new zips with `make zips`, upload `Precompiled.zip` and `Docs.zip` to GitHub Releases, and include SHAs+CHANGELOG
|
||||
|
||||
10. Add the release to `elixir.csv` (all releases), update `erlang.csv` to the precompiled OTP version, and `_data/elixir-versions.yml` (except for RCs) files in `elixir-lang/elixir-lang.github.com`
|
||||
10. Add the release to `elixir.csv` (all releases) and `_data/elixir-versions.yml` (except for RCs) files in `elixir-lang/elixir-lang.github.com`
|
||||
|
||||
11. Send an e-mail to elixir-lang-ann@googlegroups.com with title "Elixir vVERSION released". The body should be a link to the Release page on GitHub and the checksums. If it is a security release, prefix the title with the `[security]` tag
|
||||
|
||||
@@ -30,16 +30,16 @@
|
||||
|
||||
1. Set `CANONICAL=` in /Makefile
|
||||
|
||||
2. Update tables in /SECURITY.md and "Compatibility and Deprecations"
|
||||
2. Update tables in "Compatibility and Deprecations"
|
||||
|
||||
3. Commit "Branch out vMAJOR.MINOR"
|
||||
3. Commit "Prepare vMAJOR.MINOR for release"
|
||||
|
||||
### Back in master
|
||||
|
||||
1. Bump /VERSION file, bin/elixir and bin/elixir.bat
|
||||
1. Bump /VERSION file
|
||||
|
||||
2. Start new /CHANGELOG.md
|
||||
|
||||
3. Update tables in /SECURITY.md in "Compatibility and Deprecations"
|
||||
3. Update tables in "Compatibility and Deprecations"
|
||||
|
||||
4. Commit "Start vMAJOR.MINOR+1"
|
||||
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported versions
|
||||
|
||||
Elixir applies bug fixes only to the latest minor branch. Security patches are available for the last 5 minor branches:
|
||||
|
||||
Elixir version | Support
|
||||
:------------- | :-----------------------------
|
||||
1.13 | Bug fixes and security patches
|
||||
1.12 | Security patches only
|
||||
1.11 | Security patches only
|
||||
1.10 | Security patches only
|
||||
1.9 | Security patches only
|
||||
|
||||
## Announcements
|
||||
|
||||
New releases are announced in the read-only [announcements mailing list](https://groups.google.com/group/elixir-lang-ann). You can subscribe by sending an email to elixir-lang-ann+subscribe@googlegroups.com and replying to the confirmation email.
|
||||
|
||||
All security releases [will be tagged with `[security]`](https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date).
|
||||
|
||||
## Reporting a vulnerability
|
||||
|
||||
Please disclose security vulnerabilities privately at elixir-security@googlegroups.com
|
||||
+63
-184
@@ -1,62 +1,31 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
ELIXIR_VERSION=1.13.1
|
||||
-e COMMAND Evaluates the given command (*)
|
||||
-r FILE Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in PATH
|
||||
-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 (*)
|
||||
|
||||
if [ $# -eq 0 ] || { [ $# -eq 1 ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; }; then
|
||||
cat <<USAGE >&2
|
||||
Usage: $(basename "$0") [options] [.exs file] [data]
|
||||
--app APP Starts the given app and its dependencies (*)
|
||||
--cookie COOKIE Sets a cookie for this distributed node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
--help, -h Prints this message and exits
|
||||
--hidden Makes a hidden node
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--name NAME Makes and assigns a name to the distributed node
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--sname NAME Makes and assigns a short name to the distributed node
|
||||
--version, -v Prints Elixir version and exits
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
|
||||
## General options
|
||||
|
||||
-e "COMMAND" Evaluates the given command (*)
|
||||
-h, --help Prints this message (standalone)
|
||||
-r "FILE" Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in \$PATH
|
||||
-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 (*)
|
||||
-v, --version Prints Erlang/OTP and Elixir versions (standalone)
|
||||
|
||||
--app APP Starts the given app and its dependencies (*)
|
||||
--erl "SWITCHES" Switches to be passed down to Erlang (*)
|
||||
--eval "COMMAND" Evaluates the given command, same as -e (*)
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--short-version Prints Elixir version (standalone)
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
|
||||
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.
|
||||
|
||||
## Distribution options
|
||||
|
||||
The following options are related to node distribution.
|
||||
|
||||
--cookie COOKIE Sets a cookie for this distributed node
|
||||
--hidden Makes a hidden node
|
||||
--name NAME Makes and assigns a name to the distributed node
|
||||
--rpc-eval NODE "COMMAND" Evaluates the given command on the given remote node (*)
|
||||
--sname NAME Makes and assigns a short name to the distributed node
|
||||
|
||||
## Release options
|
||||
|
||||
The following options are generally used under releases.
|
||||
|
||||
--boot "FILE" Uses the given FILE.boot to start the system
|
||||
--boot-var VAR "VALUE" Makes \$VAR available as VALUE to FILE.boot (*)
|
||||
--erl-config "FILE" Loads configuration in FILE.config written in Erlang (*)
|
||||
--pipe-to "PIPEDIR" "LOGDIR" Starts the Erlang VM as a named PIPEDIR and LOGDIR
|
||||
--vm-args "FILE" Passes the contents in file as arguments to the VM
|
||||
|
||||
--pipe-to starts Elixir detached from console (Unix-like only).
|
||||
It will attempt to create PIPEDIR and LOGDIR if they don't exist.
|
||||
See run_erl to learn more. To reattach, run: to_erl PIPEDIR.
|
||||
|
||||
** Options marked with (*) can be given more than once.
|
||||
** Standalone options can't be combined with other options.
|
||||
USAGE
|
||||
** 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
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -66,147 +35,70 @@ readlink_f () {
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
echo "$(pwd -P)/$filename"
|
||||
echo "`pwd -P`/$filename"
|
||||
fi
|
||||
}
|
||||
|
||||
if [ $# -eq 1 ] && [ "$1" = "--short-version" ]; then
|
||||
echo "$ELIXIR_VERSION"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Stores static Erlang arguments and --erl (which is passed as is)
|
||||
ERL=""
|
||||
|
||||
# Stores erl arguments preserving spaces/quotes (mimics an array)
|
||||
erl_set () {
|
||||
eval "E${E}=\$1"
|
||||
E=$((E + 1))
|
||||
}
|
||||
|
||||
# Checks if a string starts with prefix. Usage: starts_with "$STRING" "$PREFIX"
|
||||
starts_with () {
|
||||
case $1 in
|
||||
"$2"*) true;;
|
||||
*) false;;
|
||||
esac
|
||||
}
|
||||
|
||||
ERL_EXEC="erl"
|
||||
MODE="elixir"
|
||||
ERL_EXEC="erl"
|
||||
ERL=""
|
||||
I=1
|
||||
E=0
|
||||
LENGTH=$#
|
||||
set -- "$@" -extra
|
||||
|
||||
while [ $I -le $LENGTH ]; do
|
||||
while [ $I -le $# ]; do
|
||||
S=1
|
||||
case "$1" in
|
||||
eval "PEEK=\${$I}"
|
||||
case "$PEEK" in
|
||||
+iex)
|
||||
set -- "$@" "$1"
|
||||
MODE="iex"
|
||||
;;
|
||||
+elixirc)
|
||||
set -- "$@" "$1"
|
||||
MODE="elixirc"
|
||||
;;
|
||||
-v|--no-halt)
|
||||
set -- "$@" "$1"
|
||||
-v|--compile|--no-halt)
|
||||
;;
|
||||
-e|-r|-pr|-pa|-pz|--app|--eval|--remsh|--dot-iex)
|
||||
-e|-r|-pr|-pa|-pz|--remsh|--app)
|
||||
S=2
|
||||
set -- "$@" "$1" "$2"
|
||||
;;
|
||||
--rpc-eval)
|
||||
S=3
|
||||
set -- "$@" "$1" "$2" "$3"
|
||||
--detached|--hidden)
|
||||
ERL="$ERL `echo $PEEK | cut -c 2-`"
|
||||
;;
|
||||
--detached)
|
||||
echo "warning: the --detached option is deprecated" >&2
|
||||
ERL="$ERL -detached"
|
||||
--cookie)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL -setcookie "$VAL""
|
||||
;;
|
||||
--hidden)
|
||||
ERL="$ERL -hidden"
|
||||
--sname|--name)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL `echo $PEEK | cut -c 2-` "$VAL""
|
||||
;;
|
||||
--logger-otp-reports)
|
||||
S=2
|
||||
if [ "$2" = 'true' ] || [ "$2" = 'false' ]; then
|
||||
ERL="$ERL -logger handle_otp_reports $2"
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
if [ "$VAL" = 'true' ] || [ "$VAL" = 'false' ]; then
|
||||
ERL="$ERL -logger handle_otp_reports "$VAL""
|
||||
fi
|
||||
;;
|
||||
--logger-sasl-reports)
|
||||
S=2
|
||||
if [ "$2" = 'true' ] || [ "$2" = 'false' ]; then
|
||||
ERL="$ERL -logger handle_sasl_reports $2"
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
if [ "$VAL" = 'true' ] || [ "$VAL" = 'false' ]; then
|
||||
ERL="$ERL -logger handle_sasl_reports "$VAL""
|
||||
fi
|
||||
;;
|
||||
--erl)
|
||||
S=2
|
||||
ERL="$ERL $2"
|
||||
;;
|
||||
--cookie)
|
||||
S=2
|
||||
erl_set "-setcookie"
|
||||
erl_set "$2"
|
||||
;;
|
||||
--sname|--name)
|
||||
S=2
|
||||
erl_set "$(echo "$1" | cut -c 2-)"
|
||||
erl_set "$2"
|
||||
;;
|
||||
--erl-config)
|
||||
S=2
|
||||
erl_set "-config"
|
||||
erl_set "$2"
|
||||
;;
|
||||
--vm-args)
|
||||
S=2
|
||||
erl_set "-args_file"
|
||||
erl_set "$2"
|
||||
;;
|
||||
--boot)
|
||||
S=2
|
||||
erl_set "-boot"
|
||||
erl_set "$2"
|
||||
;;
|
||||
--boot-var)
|
||||
S=3
|
||||
erl_set "-boot_var"
|
||||
erl_set "$2"
|
||||
erl_set "$3"
|
||||
;;
|
||||
--pipe-to)
|
||||
S=3
|
||||
RUN_ERL_PIPE="$2"
|
||||
RUN_ERL_LOG="$3"
|
||||
if [ "$(starts_with "$RUN_ERL_PIPE" "-")" ]; then
|
||||
echo "--pipe-to : PIPEDIR cannot be a switch" >&2 && exit 1
|
||||
elif [ "$(starts_with "$RUN_ERL_LOG" "-")" ]; then
|
||||
echo "--pipe-to : LOGDIR cannot be a switch" >&2 && exit 1
|
||||
fi
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL "$VAL""
|
||||
;;
|
||||
--werl)
|
||||
if [ "$OS" = "Windows_NT" ]; then ERL_EXEC="werl"; fi
|
||||
USE_WERL=true
|
||||
;;
|
||||
*)
|
||||
while [ $I -le $LENGTH ]; do
|
||||
I=$((I + 1))
|
||||
set -- "$@" "$1"
|
||||
shift
|
||||
done
|
||||
break
|
||||
;;
|
||||
esac
|
||||
|
||||
I=$((I + S))
|
||||
shift $S
|
||||
done
|
||||
|
||||
I=$((E - 1))
|
||||
while [ $I -ge 0 ]; do
|
||||
eval "VAL=\$E$I"
|
||||
set -- "$VAL" "$@"
|
||||
I=$((I - 1))
|
||||
I=$(expr $I + $S)
|
||||
done
|
||||
|
||||
SELF=$(readlink_f "$0")
|
||||
@@ -215,30 +107,17 @@ 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
|
||||
|
||||
if [ "$OS" != "Windows_NT" ] && [ -z "$NO_COLOR" ]; then
|
||||
# Check for terminal support
|
||||
if [ "$OS" != "Windows_NT" ]; then
|
||||
if test -t 1 -a -t 2; then ERL="-elixir ansi_enabled true $ERL"; fi
|
||||
fi
|
||||
|
||||
# One MAY change ERTS_BIN= but you MUST NOT change
|
||||
# ERTS_BIN=$ERTS_BIN as it is handled by Elixir releases.
|
||||
ERTS_BIN=
|
||||
ERTS_BIN="$ERTS_BIN"
|
||||
|
||||
set -- "$ERTS_BIN$ERL_EXEC" -pa "$SCRIPT_PATH"/../lib/*/ebin $ELIXIR_ERL_OPTIONS $ERL "$@"
|
||||
|
||||
if [ -n "$RUN_ERL_PIPE" ]; then
|
||||
ESCAPED=""
|
||||
for PART in "$@"; do
|
||||
ESCAPED="$ESCAPED $(echo "$PART" | sed 's/[^a-zA-Z0-9_\-\/]/\\&/g')"
|
||||
done
|
||||
mkdir -p "$RUN_ERL_PIPE"
|
||||
mkdir -p "$RUN_ERL_LOG"
|
||||
ERL_EXEC="run_erl"
|
||||
set -- "$ERTS_BIN$ERL_EXEC" -daemon "$RUN_ERL_PIPE/" "$RUN_ERL_LOG/" "$ESCAPED"
|
||||
if [ "$OS" = "Windows_NT" ] && [ $USE_WERL ]; then
|
||||
ERL_EXEC="werl"
|
||||
fi
|
||||
|
||||
if [ -n "$ELIXIR_CLI_DRY_RUN" ]; then
|
||||
echo "$@"
|
||||
else
|
||||
exec "$@"
|
||||
if [ -z "$ERL_PATH" ]; then
|
||||
ERL_PATH="$ERL_EXEC"
|
||||
fi
|
||||
|
||||
exec "$ERL_PATH" -pa "$SCRIPT_PATH"/../lib/*/ebin $ELIXIR_ERL_OPTIONS $ERL -extra "$@"
|
||||
|
||||
+72
-141
@@ -1,183 +1,114 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
|
||||
set ELIXIR_VERSION=1.13.1
|
||||
|
||||
setlocal enabledelayedexpansion
|
||||
if ""%1""=="""" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""--help"" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""-h"" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""/h"" if ""%2""=="""" goto documentation
|
||||
if ""%1""==""/?"" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""--short-version"" if ""%2""=="""" goto shortversion
|
||||
setlocal
|
||||
if ""%1""=="""" goto documentation
|
||||
if /I ""%1""==""--help"" goto documentation
|
||||
if /I ""%1""==""-h"" goto documentation
|
||||
if /I ""%1""==""/h"" goto documentation
|
||||
if ""%1""==""/?"" goto documentation
|
||||
goto parseopts
|
||||
|
||||
:documentation
|
||||
echo Usage: %~nx0 [options] [.exs file] [data]
|
||||
echo.
|
||||
echo ## General options
|
||||
echo -e COMMAND Evaluates the given command (*)
|
||||
echo -r FILE Requires the given files/patterns (*)
|
||||
echo -S SCRIPT Finds and executes the given script in PATH
|
||||
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.
|
||||
echo -e "COMMAND" Evaluates the given command (*)
|
||||
echo -h, --help Prints this message (standalone)
|
||||
echo -r "FILE" Requires the given files/patterns (*)
|
||||
echo -S SCRIPT Finds and executes the given script in $PATH
|
||||
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 -v, --version Prints Erlang/OTP and Elixir versions (standalone)
|
||||
echo --app APP Starts the given app and its dependencies (*)
|
||||
echo --cookie COOKIE Sets a cookie for this distributed node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
echo --help, -h Prints this message and exits
|
||||
echo --hidden Makes a hidden node
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --name NAME Makes and assigns a name to the distributed node
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --sname NAME Makes and assigns a short name to the distributed node
|
||||
echo --version, -v Prints Elixir version and exits
|
||||
echo --werl Uses Erlang's Windows shell GUI
|
||||
echo.
|
||||
echo --app APP Starts the given app and its dependencies (*)
|
||||
echo --erl "SWITCHES" Switches to be passed down to Erlang (*)
|
||||
echo --eval "COMMAND" Evaluates the given command, same as -e (*)
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --short-version Prints Elixir version (standalone)
|
||||
echo --werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
echo.
|
||||
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.
|
||||
echo.
|
||||
echo ## Distribution options
|
||||
echo.
|
||||
echo The following options are related to node distribution.
|
||||
echo.
|
||||
echo --cookie COOKIE Sets a cookie for this distributed node
|
||||
echo --hidden Makes a hidden node
|
||||
echo --name NAME Makes and assigns a name to the distributed node
|
||||
echo --rpc-eval NODE "COMMAND" Evaluates the given command on the given remote node (*)
|
||||
echo --sname NAME Makes and assigns a short name to the distributed node
|
||||
echo.
|
||||
echo ## Release options
|
||||
echo.
|
||||
echo The following options are generally used under releases.
|
||||
echo.
|
||||
echo --boot "FILE" Uses the given FILE.boot to start the system
|
||||
echo --boot-var VAR "VALUE" Makes $VAR available as VALUE to FILE.boot (*)
|
||||
echo --erl-config "FILE" Loads configuration in FILE.config written in Erlang (*)
|
||||
echo --vm-args "FILE" Passes the contents in file as arguments to the VM
|
||||
echo.
|
||||
echo --pipe-to is not supported on Windows. If set, Elixir won't boot.
|
||||
echo.
|
||||
echo ** Options marked with (*) can be given more than once.
|
||||
echo ** Standalone options can't be combined with other options.
|
||||
goto end
|
||||
|
||||
:shortversion
|
||||
echo !ELIXIR_VERSION!
|
||||
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 Elixir
|
||||
set parsElixir=
|
||||
|
||||
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 Option which determines whether the loop is over
|
||||
set endLoop=0
|
||||
rem Option 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 Designates the path to the current script
|
||||
set SCRIPT_PATH=%~dp0
|
||||
|
||||
rem Designates the path to the ERTS system
|
||||
set ERTS_BIN=
|
||||
set ERTS_BIN=!ERTS_BIN!
|
||||
|
||||
rem Recursive loop called for each parameter that parses the cmd line parameters
|
||||
:startloop
|
||||
set "par=%~1"
|
||||
if "!par!"=="" (
|
||||
rem skip if no parameter
|
||||
set par="%1"
|
||||
shift
|
||||
if "%par%"=="" (
|
||||
rem if no parameters defined
|
||||
goto expand_erl_libs
|
||||
)
|
||||
shift
|
||||
set par="!par:"=\"!"
|
||||
if !endLoop! == 1 (
|
||||
set parsElixir=!parsElixir! !par!
|
||||
goto startloop
|
||||
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 && goto startloop)
|
||||
if !par!=="+iex" (set parsElixir=!parsElixir! +iex && set runMode="iex" && goto startloop)
|
||||
if !par!=="+elixirc" (set parsElixir=!parsElixir! +elixirc && set runMode="elixirc" && goto startloop)
|
||||
rem ******* EVAL PARAMETERS ************************
|
||||
if ""==!par:-e=! (
|
||||
set "VAR=%~1"
|
||||
if not defined VAR (set VAR= )
|
||||
set parsElixir=!parsElixir! -e "!VAR:"=\"!"
|
||||
shift
|
||||
goto startloop
|
||||
)
|
||||
if ""==!par:--eval=! (
|
||||
set "VAR=%~1"
|
||||
if not defined VAR (set VAR= )
|
||||
set parsElixir=!parsElixir! --eval "!VAR:"=\"!"
|
||||
shift
|
||||
goto startloop
|
||||
)
|
||||
if ""==!par:--rpc-eval=! (
|
||||
set "VAR=%~2"
|
||||
if not defined VAR (set VAR= )
|
||||
set parsElixir=!parsElixir! --rpc-eval %1 "!VAR:"=\"!"
|
||||
shift
|
||||
shift
|
||||
goto startloop
|
||||
)
|
||||
if "%par%"==""--werl"" (set useWerl=1)
|
||||
if "%par%"==""+iex"" (set runMode="iex")
|
||||
rem ******* ELIXIR PARAMETERS **********************
|
||||
if ""==!par:-r=! (set "parsElixir=!parsElixir! -r %1" && shift && goto startloop)
|
||||
if ""==!par:-pr=! (set "parsElixir=!parsElixir! -pr %1" && shift && goto startloop)
|
||||
if ""==!par:-pa=! (set "parsElixir=!parsElixir! -pa %1" && shift && goto startloop)
|
||||
if ""==!par:-pz=! (set "parsElixir=!parsElixir! -pz %1" && shift && goto startloop)
|
||||
if ""==!par:-v=! (set "parsElixir=!parsElixir! -v" && goto startloop)
|
||||
if ""==!par:--version=! (set "parsElixir=!parsElixir! --version" && goto startloop)
|
||||
if ""==!par:--app=! (set "parsElixir=!parsElixir! --app %1" && shift && goto startloop)
|
||||
if ""==!par:--no-halt=! (set "parsElixir=!parsElixir! --no-halt" && goto startloop)
|
||||
if ""==!par:--remsh=! (set "parsElixir=!parsElixir! --remsh %1" && shift && goto startloop)
|
||||
if ""==!par:--dot-iex=! (set "parsElixir=!parsElixir! --dot-iex %1" && shift && goto startloop)
|
||||
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:--boot=! (set "parsErlang=!parsErlang! -boot %1" && shift && goto startloop)
|
||||
if ""==!par:--boot-var=! (set "parsErlang=!parsErlang! -boot_var %1 %2" && shift && shift && goto startloop)
|
||||
if ""==!par:--cookie=! (set "parsErlang=!parsErlang! -setcookie %1" && shift && goto startloop)
|
||||
if ""==!par:--hidden=! (set "parsErlang=!parsErlang! -hidden" && goto startloop)
|
||||
if ""==!par:--detached=! (set "parsErlang=!parsErlang! -detached" && echo warning: the --detached option is deprecated && goto startloop)
|
||||
if ""==!par:--erl-config=! (set "parsErlang=!parsErlang! -config %1" && shift && goto startloop)
|
||||
if ""==!par:--logger-otp-reports=! (set "parsErlang=!parsErlang! -logger handle_otp_reports %1" && shift && goto startloop)
|
||||
if ""==!par:--logger-sasl-reports=! (set "parsErlang=!parsErlang! -logger handle_sasl_reports %1" && shift && goto startloop)
|
||||
if ""==!par:--name=! (set "parsErlang=!parsErlang! -name %1" && shift && goto startloop)
|
||||
if ""==!par:--sname=! (set "parsErlang=!parsErlang! -sname %1" && shift && goto startloop)
|
||||
if ""==!par:--vm-args=! (set "parsErlang=!parsErlang! -args_file %1" && shift && goto startloop)
|
||||
if ""==!par:--erl=! (set "beforeExtra=!beforeExtra! %~1" && shift && goto startloop)
|
||||
if ""==!par:--pipe-to=! (echo --pipe-to : Option is not supported on Windows && goto end)
|
||||
set endLoop=1
|
||||
set parsElixir=!parsElixir! !par!
|
||||
goto startloop
|
||||
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:--logger-otp-reports=%" (set parsErlang=%parsErlang% -logger handle_otp_reports %1 && shift)
|
||||
if """"=="%par:--logger-sasl-reports=%" (set parsErlang=%parsErlang% -logger handle_sasl_reports %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
|
||||
rem ******* expand all ebin paths as Windows does not support the ..\*\ebin wildcard ********************
|
||||
setlocal enabledelayedexpansion
|
||||
set ext_libs=
|
||||
for /d %%d in ("!SCRIPT_PATH!..\lib\*.") do (
|
||||
for /d %%d in ("%originPath%..\lib\*.") do (
|
||||
set ext_libs=!ext_libs! -pa "%%~fd\ebin"
|
||||
)
|
||||
setlocal disabledelayedexpansion
|
||||
|
||||
:run
|
||||
reg query HKCU\Console /v VirtualTerminalLevel 2>nul | findstr /e "0x1" >nul 2>nul
|
||||
if %errorlevel% == 0 (
|
||||
set beforeExtra=-elixir ansi_enabled true !beforeExtra!
|
||||
if not %runMode% == "iex" (
|
||||
set beforeExtra=-noshell -s elixir start_cli %beforeExtra%
|
||||
)
|
||||
if not !runMode! == "iex" (
|
||||
set beforeExtra=-noshell -s elixir start_cli !beforeExtra!
|
||||
)
|
||||
if defined useWerl (
|
||||
start "" "!ERTS_BIN!werl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
|
||||
if %useWerl% equ 1 (
|
||||
start werl.exe %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
) else (
|
||||
"!ERTS_BIN!erl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
|
||||
erl.exe %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
)
|
||||
:end
|
||||
endlocal
|
||||
|
||||
+8
-13
@@ -1,25 +1,20 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
cat <<USAGE >&2
|
||||
Usage: $(basename "$0") [elixir switches] [compiler switches] [.ex files]
|
||||
echo "Usage: `basename $0` [elixir switches] [compiler switches] [.ex files]
|
||||
|
||||
-h, --help Prints this message and exits
|
||||
-o The directory to output compiled files
|
||||
-v, --version Prints Elixir version and exits (standalone)
|
||||
|
||||
--help, -h Prints this message and exits
|
||||
--ignore-module-conflict Does not emit warnings if a module was previously defined
|
||||
--no-debug-info Does not attach debug info to compiled modules
|
||||
--no-docs Does not attach documentation to compiled modules
|
||||
--profile time Profile the time to compile modules
|
||||
--verbose Prints compilation status
|
||||
--warnings-as-errors Treats warnings as errors and return non-zero exit status
|
||||
--version, -v Prints Elixir version and exits
|
||||
--warnings-as-errors Treats warnings as errors and return non-zero exit code
|
||||
|
||||
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.
|
||||
USAGE
|
||||
** 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
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -29,7 +24,7 @@ readlink_f () {
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
echo "$(pwd -P)/$filename"
|
||||
echo "`pwd -P`/$filename"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
+3
-4
@@ -14,16 +14,15 @@ goto run
|
||||
:documentation
|
||||
echo Usage: %~nx0 [elixir switches] [compiler switches] [.ex files]
|
||||
echo.
|
||||
echo -h, --help Prints this message and exits
|
||||
echo -o The directory to output compiled files
|
||||
echo -v, --version Prints Elixir version and exits (standalone)
|
||||
echo.
|
||||
echo --help, -h Prints this message and exits
|
||||
echo --ignore-module-conflict Does not emit warnings if a module was previously defined
|
||||
echo --no-debug-info Does not attach debug info to compiled modules
|
||||
echo --no-docs Does not attach documentation to compiled modules
|
||||
echo --profile time Profile the time to compile modules
|
||||
echo --verbose Prints compilation status
|
||||
echo --warnings-as-errors Treats warnings as errors and returns non-zero exit status
|
||||
echo --version, -v Prints Elixir version and exits
|
||||
echo --warnings-as-errors Treats warnings as errors and returns non-zero exit code
|
||||
echo.
|
||||
echo ** Options given after -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
|
||||
@@ -1,18 +1,35 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
if [ $# -gt 0 ] && ([ "$1" = "--help" ] || [ "$1" = "-h" ]); then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
cat <<USAGE >&2
|
||||
Usage: $(basename "$0") [options] [.exs file] [data]
|
||||
-e COMMAND Evaluates the given command (*)
|
||||
-r FILE Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in PATH
|
||||
-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 (*)
|
||||
|
||||
The following options are exclusive to IEx:
|
||||
--app APP Starts the given app and its dependencies (*)
|
||||
--cookie COOKIE Sets a cookie for this distributed node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
--help, -h Prints this message and exits
|
||||
--hidden Makes a hidden node
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--name NAME Makes and assigns a name to the distributed node
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--sname NAME Makes and assigns a short name to the distributed node
|
||||
--version, -v Prints IEx version and exits
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
|
||||
--dot-iex "PATH" Overrides default .iex.exs file and uses path instead;
|
||||
path can be empty, then no file will be loaded
|
||||
--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
|
||||
--remsh NAME Connects to a node using a remote shell
|
||||
|
||||
It accepts all other options listed by "elixir --help".
|
||||
USAGE
|
||||
** 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
|
||||
|
||||
@@ -22,7 +39,7 @@ readlink_f () {
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
echo "$(pwd -P)/$filename"
|
||||
echo "`pwd -P`/$filename"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
+28
-9
@@ -1,4 +1,4 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
setlocal
|
||||
if /I ""%1""==""--help"" goto documentation
|
||||
if /I ""%1""==""-h"" goto documentation
|
||||
@@ -9,19 +9,38 @@ goto run
|
||||
:documentation
|
||||
echo Usage: %~nx0 [options] [.exs file] [data]
|
||||
echo.
|
||||
echo The following options are exclusive to IEx:
|
||||
echo -e COMMAND Evaluates the given command (*)
|
||||
echo -r FILE Requires the given files/patterns (*)
|
||||
echo -S SCRIPT Finds and executes the given script in PATH
|
||||
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.
|
||||
echo --dot-iex "PATH" Overrides default .iex.exs file and uses path instead;
|
||||
echo path can be empty, then no file will be loaded
|
||||
echo --remsh NAME Connects to a node using a remote shell
|
||||
echo --werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
echo --app APP Starts the given app and its dependencies (*)
|
||||
echo --cookie COOKIE Sets a cookie for this distributed node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
echo --help, -h Prints this message and exits
|
||||
echo --hidden Makes a hidden node
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --name NAME Makes and assigns a name to the distributed node
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --sname NAME Makes and assigns a short name to the distributed node
|
||||
echo --version, -v Prints IEx version and exits
|
||||
echo --werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
echo.
|
||||
echo Set the IEX_WITH_WERL environment variable to always use werl.
|
||||
echo It accepts all other options listed by "elixir --help".
|
||||
echo --dot-iex PATH Overrides default .iex.exs file and uses path instead;
|
||||
echo path can be empty, then no file will be loaded
|
||||
echo --remsh NAME Connects to a node using a remote shell
|
||||
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 VM using ELIXIR_ERL_OPTIONS or --erl
|
||||
goto end
|
||||
|
||||
:run
|
||||
if defined IEX_WITH_WERL (@set __ELIXIR_IEX_FLAGS=--werl) else (set __ELIXIR_IEX_FLAGS=)
|
||||
@if defined IEX_WITH_WERL (@set __ELIXIR_IEX_FLAGS=--werl) else (set __ELIXIR_IEX_FLAGS=)
|
||||
call "%~dp0\elixir.bat" --no-halt --erl "-noshell -user Elixir.IEx.CLI" +iex %__ELIXIR_IEX_FLAGS% %*
|
||||
:end
|
||||
endlocal
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/usr/bin/env elixir
|
||||
Mix.start()
|
||||
Mix.CLI.main()
|
||||
Mix.start
|
||||
Mix.CLI.main
|
||||
|
||||
+35
-97
@@ -1,9 +1,9 @@
|
||||
defmodule EEx.SyntaxError do
|
||||
defexception [:message, :file, :line, :column]
|
||||
defexception [:message, :file, :line]
|
||||
|
||||
@impl true
|
||||
def message(exception) do
|
||||
"#{exception.file}:#{exception.line}:#{exception.column}: #{exception.message}"
|
||||
"#{exception.file}:#{exception.line}: #{exception.message}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -17,19 +17,19 @@ defmodule EEx do
|
||||
|
||||
## API
|
||||
|
||||
This module provides three main APIs for you to use:
|
||||
This module provides 3 main APIs for you to use:
|
||||
|
||||
1. Evaluate a string (`eval_string/3`) or a file (`eval_file/3`)
|
||||
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 at runtime and not precompiled.
|
||||
slowest, since the code is evaluated and not compiled before.
|
||||
|
||||
2. Define a function from a string (`function_from_string/5`)
|
||||
or a file (`function_from_file/5`). This allows you to embed
|
||||
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/2`) or a file (`compile_file/2`)
|
||||
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.
|
||||
@@ -39,19 +39,11 @@ defmodule EEx do
|
||||
All functions in this module accept 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.
|
||||
* `:line` - the line to be used as the template start. Defaults to `1`.
|
||||
* `:indentation` - (since v1.11.0) an integer added to the column after every
|
||||
new line. Defaults to `0`.
|
||||
file the template is read from or to "nofile" when compiling from a string.
|
||||
* `:engine` - the EEx engine to be used for compilation.
|
||||
* `:trim` - if `true`, trims whitespace left and right of quotation as
|
||||
long as at least one newline is present. All subsequent newlines and
|
||||
spaces are removed but one newline is retained. Defaults to `false`.
|
||||
* `:parser_options` - (since: 1.13.0) allow customizing the parsed code that is generated.
|
||||
See `Code.string_to_quoted/2` for available options. Note that the options
|
||||
`:file`, `:line` and `:column` are ignored if passed in.
|
||||
Defaults to `Code.get_compiler_option(:parser_options)` (which defaults to `[]` if not set).
|
||||
* `:trim` - trims whitespace left/right of quotation tags
|
||||
|
||||
## Engine
|
||||
|
||||
@@ -74,7 +66,7 @@ defmodule EEx do
|
||||
**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
|
||||
`if/2` clauses, they are treated the same in EEx and
|
||||
also require `=` in order to have their result printed:
|
||||
|
||||
<%= if true do %>
|
||||
@@ -83,13 +75,7 @@ defmodule EEx do
|
||||
This will never appear
|
||||
<% end %>
|
||||
|
||||
To escape an EEx expression in EEx use `<%% content %>`. For example:
|
||||
|
||||
<%%= x + 3 %>
|
||||
|
||||
will be rendered as `<%= x + 3 %>`.
|
||||
|
||||
Note that different engines may have different rules
|
||||
Notice that different engines may have different rules
|
||||
for each tag. Other tags may be added in future versions.
|
||||
|
||||
### Macros
|
||||
@@ -110,29 +96,26 @@ defmodule EEx do
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Generates a function definition from the given string.
|
||||
Generates a function definition from the string.
|
||||
|
||||
The first argument is the kind of the generated function (`:def` or `:defp`).
|
||||
The `name` argument is the name that the generated function will have.
|
||||
`template` is the string containing the EEx template. `args` is a list of arguments
|
||||
that the generated function will accept. They will be available inside the EEx
|
||||
template. `options` is a list of EEx compilation options (see the module documentation).
|
||||
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])
|
||||
...> EEx.function_from_string :def, :sample, "<%= a + b %>", [:a, :b]
|
||||
...> end
|
||||
iex> Sample.sample(1, 2)
|
||||
"3"
|
||||
|
||||
"""
|
||||
defmacro function_from_string(kind, name, template, args \\ [], options \\ []) do
|
||||
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(template, info)
|
||||
compiled = EEx.compile_string(source, info)
|
||||
|
||||
case kind do
|
||||
:def -> def unquote(name)(unquote_splicing(args)), do: unquote(compiled)
|
||||
@@ -144,11 +127,8 @@ defmodule EEx do
|
||||
@doc """
|
||||
Generates a function definition from the file contents.
|
||||
|
||||
The first argument is the kind of the generated function (`:def` or `:defp`).
|
||||
The `name` argument is the name that the generated function will have.
|
||||
`file` is the path to the EEx template file. `args` is a list of arguments
|
||||
that the generated function will accept. They will be available inside the EEx
|
||||
template. `options` is a list of EEx compilation options (see the module documentation).
|
||||
The kind (`:def` or `:defp`) must be given, the
|
||||
function name, its arguments and the compilation options.
|
||||
|
||||
This function is useful in case you have templates but
|
||||
you want to precompile inside a module for speed.
|
||||
@@ -161,17 +141,16 @@ defmodule EEx do
|
||||
# sample.ex
|
||||
defmodule Sample do
|
||||
require EEx
|
||||
EEx.function_from_file(:def, :sample, "sample.eex", [:a, :b])
|
||||
EEx.function_from_file :def, :sample, "sample.eex", [:a, :b]
|
||||
end
|
||||
|
||||
# iex
|
||||
Sample.sample(1, 2)
|
||||
#=> "3"
|
||||
Sample.sample(1, 2) #=> "3"
|
||||
|
||||
"""
|
||||
defmacro function_from_file(kind, name, file, args \\ [], options \\ []) do
|
||||
quote bind_quoted: binding() do
|
||||
info = Keyword.merge([file: IO.chardata_to_string(file), line: 1], options)
|
||||
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)
|
||||
|
||||
@@ -185,25 +164,8 @@ defmodule EEx do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a string `source` and generates a quoted expression
|
||||
Gets a string `source` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
|
||||
This is useful if you want to compile a EEx template into code and inject
|
||||
that code somewhere or evaluate it at runtime.
|
||||
|
||||
The generated quoted code will use variables defined in the template that
|
||||
will be taken from the context where the code is evaluated. If you
|
||||
have a template such as `<%= a + b %>`, then the returned quoted code
|
||||
will use the `a` and `b` variables in the context where it's evaluated. See
|
||||
examples below.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> quoted = EEx.compile_string("<%= a + b %>")
|
||||
iex> {result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
|
||||
iex> result
|
||||
"3"
|
||||
|
||||
"""
|
||||
@spec compile_string(String.t(), keyword) :: Macro.t()
|
||||
def compile_string(source, options \\ []) when is_binary(source) and is_list(options) do
|
||||
@@ -211,34 +173,12 @@ defmodule EEx do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a `filename` and generates a quoted expression
|
||||
Gets a `filename` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
|
||||
This is useful if you want to compile a EEx template into code and inject
|
||||
that code somewhere or evaluate it at runtime.
|
||||
|
||||
The generated quoted code will use variables defined in the template that
|
||||
will be taken from the context where the code is evaluated. If you
|
||||
have a template such as `<%= a + b %>`, then the returned quoted code
|
||||
will use the `a` and `b` variables in the context where it's evaluated. See
|
||||
examples below.
|
||||
|
||||
## Examples
|
||||
|
||||
# sample.eex
|
||||
<%= a + b %>
|
||||
|
||||
# In code:
|
||||
quoted = EEx.compile_file("sample.eex")
|
||||
{result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
|
||||
result
|
||||
#=> "3"
|
||||
|
||||
"""
|
||||
@spec compile_file(Path.t(), keyword) :: Macro.t()
|
||||
def compile_file(filename, options \\ []) when is_list(options) do
|
||||
filename = IO.chardata_to_string(filename)
|
||||
options = Keyword.merge([file: filename, line: 1], options)
|
||||
@spec compile_file(String.t(), keyword) :: Macro.t()
|
||||
def compile_file(filename, options \\ []) when is_binary(filename) and is_list(options) do
|
||||
options = Keyword.merge(options, file: filename, line: 1)
|
||||
compile_string(File.read!(filename), options)
|
||||
end
|
||||
|
||||
@@ -251,7 +191,7 @@ defmodule EEx do
|
||||
"foo baz"
|
||||
|
||||
"""
|
||||
@spec eval_string(String.t(), keyword, keyword) :: String.t()
|
||||
@spec eval_string(String.t(), keyword, keyword) :: any
|
||||
def eval_string(source, bindings \\ [], options \\ [])
|
||||
when is_binary(source) and is_list(bindings) and is_list(options) do
|
||||
compiled = compile_string(source, options)
|
||||
@@ -266,16 +206,14 @@ defmodule EEx do
|
||||
# sample.eex
|
||||
foo <%= bar %>
|
||||
|
||||
# IEx
|
||||
EEx.eval_file("sample.eex", bar: "baz")
|
||||
#=> "foo baz"
|
||||
# iex
|
||||
EEx.eval_file "sample.eex", [bar: "baz"] #=> "foo baz"
|
||||
|
||||
"""
|
||||
@spec eval_file(Path.t(), keyword, keyword) :: String.t()
|
||||
@spec eval_file(String.t(), keyword, keyword) :: any
|
||||
def eval_file(filename, bindings \\ [], options \\ [])
|
||||
when is_list(bindings) and is_list(options) do
|
||||
filename = IO.chardata_to_string(filename)
|
||||
options = Keyword.put_new(options, :file, filename)
|
||||
when is_binary(filename) and is_list(bindings) and is_list(options) do
|
||||
options = Keyword.put(options, :file, filename)
|
||||
compiled = compile_file(filename, options)
|
||||
do_eval(compiled, bindings, options)
|
||||
end
|
||||
|
||||
+45
-100
@@ -13,102 +13,63 @@ defmodule EEx.Compiler do
|
||||
def compile(source, opts) when is_binary(source) and is_list(opts) do
|
||||
file = opts[:file] || "nofile"
|
||||
line = opts[:line] || 1
|
||||
column = 1
|
||||
indentation = opts[:indentation] || 0
|
||||
trim = opts[:trim] || false
|
||||
parser_options = opts[:parser_options] || Code.get_compiler_option(:parser_options)
|
||||
tokenizer_options = %{trim: trim, indentation: indentation}
|
||||
|
||||
case EEx.Tokenizer.tokenize(source, line, column, tokenizer_options) do
|
||||
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,
|
||||
start_column: nil,
|
||||
parser_options: parser_options
|
||||
start_line: nil
|
||||
}
|
||||
|
||||
init = state.engine.init(opts)
|
||||
generate_buffer(tokens, init, [], state)
|
||||
|
||||
{:error, line, column, message} ->
|
||||
raise EEx.SyntaxError, file: file, line: line, column: column, message: message
|
||||
{:error, line, message} ->
|
||||
raise EEx.SyntaxError, line: line, file: file, message: message
|
||||
end
|
||||
end
|
||||
|
||||
# Generates the buffers by handling each expression from the tokenizer.
|
||||
# It returns Macro.t/0 or it raises.
|
||||
|
||||
defp generate_buffer([{:text, line, column, chars} | rest], buffer, scope, state) do
|
||||
buffer =
|
||||
if function_exported?(state.engine, :handle_text, 3) do
|
||||
meta = [line: line, column: column]
|
||||
state.engine.handle_text(buffer, meta, IO.chardata_to_string(chars))
|
||||
else
|
||||
# TODO: Remove this branch on Elixir v2.0
|
||||
state.engine.handle_text(buffer, IO.chardata_to_string(chars))
|
||||
end
|
||||
|
||||
defp generate_buffer([{:text, chars} | rest], buffer, scope, state) do
|
||||
buffer = state.engine.handle_text(buffer, IO.chardata_to_string(chars))
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:expr, line, column, mark, chars} | rest], buffer, scope, state) do
|
||||
options = [file: state.file, line: line, column: column(column, mark)] ++ state.parser_options
|
||||
expr = Code.string_to_quoted!(chars, options)
|
||||
defp generate_buffer([{:expr, line, mark, chars} | rest], 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)
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer(
|
||||
[{:start_expr, start_line, start_column, mark, chars} | rest],
|
||||
buffer,
|
||||
scope,
|
||||
state
|
||||
) do
|
||||
if mark != '=' do
|
||||
message =
|
||||
"the contents of this expression won't be output unless the EEx block starts with \"<%=\""
|
||||
|
||||
:elixir_errors.erl_warn({start_line, start_column}, state.file, message)
|
||||
end
|
||||
|
||||
{rest, line, contents} =
|
||||
look_ahead_middle(rest, start_line, chars) || {rest, start_line, chars}
|
||||
defp generate_buffer([{:start_expr, start_line, mark, chars} | rest], buffer, scope, state) do
|
||||
{contents, line, rest} = look_ahead_text(rest, start_line, chars)
|
||||
|
||||
{contents, rest} =
|
||||
generate_buffer(
|
||||
rest,
|
||||
state.engine.handle_begin(buffer),
|
||||
[contents | scope],
|
||||
%{
|
||||
state
|
||||
| quoted: [],
|
||||
line: line,
|
||||
start_line: start_line,
|
||||
start_column: column(start_column, mark)
|
||||
}
|
||||
)
|
||||
generate_buffer(rest, state.engine.handle_begin(buffer), [contents | scope], %{
|
||||
state
|
||||
| quoted: [],
|
||||
line: line,
|
||||
start_line: start_line
|
||||
})
|
||||
|
||||
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), contents)
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer(
|
||||
[{:middle_expr, line, _column, '', chars} | rest],
|
||||
buffer,
|
||||
[current | scope],
|
||||
state
|
||||
) do
|
||||
defp generate_buffer([{:middle_expr, line, '', chars} | rest], buffer, [current | scope], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
state = %{state | line: line}
|
||||
generate_buffer(rest, state.engine.handle_begin(buffer), [wrapped | scope], state)
|
||||
end
|
||||
|
||||
defp generate_buffer(
|
||||
[{:middle_expr, line, column, modifier, chars} | t],
|
||||
[{:middle_expr, line, modifier, chars} | t],
|
||||
buffer,
|
||||
[_ | _] = scope,
|
||||
state
|
||||
@@ -117,68 +78,53 @@ defmodule EEx.Compiler do
|
||||
"unexpected beginning of EEx tag \"<%#{modifier}\" on \"<%#{modifier}#{chars}%>\", " <>
|
||||
"please remove \"#{modifier}\" accordingly"
|
||||
|
||||
:elixir_errors.erl_warn({line, column}, state.file, message)
|
||||
generate_buffer([{:middle_expr, line, column, '', chars} | t], buffer, scope, state)
|
||||
:elixir_errors.warn(line, state.file, message)
|
||||
generate_buffer([{:middle_expr, line, '', chars} | t], buffer, scope, state)
|
||||
# TODO: Make this an error on Elixir v2.0 since it accidentally worked previously.
|
||||
# raise EEx.SyntaxError, message: message, file: state.file, line: line
|
||||
end
|
||||
|
||||
defp generate_buffer([{:middle_expr, line, column, _, chars} | _], _buffer, [], state) do
|
||||
defp generate_buffer([{:middle_expr, line, _, chars} | _], _buffer, [], state) do
|
||||
raise EEx.SyntaxError,
|
||||
message: "unexpected middle of expression <%#{chars}%>",
|
||||
file: state.file,
|
||||
line: line,
|
||||
column: column
|
||||
line: line
|
||||
end
|
||||
|
||||
defp generate_buffer(
|
||||
[{:end_expr, line, _column, '', chars} | rest],
|
||||
buffer,
|
||||
[current | _],
|
||||
state
|
||||
) do
|
||||
defp generate_buffer([{:end_expr, line, '', chars} | rest], buffer, [current | _], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
column = state.start_column
|
||||
options = [file: state.file, line: state.start_line, column: column] ++ state.parser_options
|
||||
tuples = Code.string_to_quoted!(wrapped, options)
|
||||
tuples = Code.string_to_quoted!(wrapped, line: state.start_line, file: state.file)
|
||||
buffer = insert_quoted(tuples, state.quoted)
|
||||
{buffer, rest}
|
||||
end
|
||||
|
||||
defp generate_buffer(
|
||||
[{:end_expr, line, column, modifier, chars} | t],
|
||||
buffer,
|
||||
[_ | _] = scope,
|
||||
state
|
||||
) do
|
||||
defp generate_buffer([{:end_expr, line, modifier, chars} | t], buffer, [_ | _] = scope, state) do
|
||||
message =
|
||||
"unexpected beginning of EEx tag \"<%#{modifier}\" on end of " <>
|
||||
"expression \"<%#{modifier}#{chars}%>\", please remove \"#{modifier}\" accordingly"
|
||||
|
||||
:elixir_errors.erl_warn({line, column}, state.file, message)
|
||||
generate_buffer([{:end_expr, line, column, '', chars} | t], buffer, scope, state)
|
||||
:elixir_errors.warn(line, state.file, message)
|
||||
generate_buffer([{:end_expr, line, '', chars} | t], buffer, scope, state)
|
||||
# TODO: Make this an error on Elixir v2.0 since it accidentally worked previously.
|
||||
# raise EEx.SyntaxError, message: message, file: state.file, line: line, column: column
|
||||
# raise EEx.SyntaxError, message: message, file: state.file, line: line
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, column, _, chars} | _], _buffer, [], state) do
|
||||
defp generate_buffer([{:end_expr, line, _, chars} | _], _buffer, [], state) do
|
||||
raise EEx.SyntaxError,
|
||||
message: "unexpected end of expression <%#{chars}%>",
|
||||
file: state.file,
|
||||
line: line,
|
||||
column: column
|
||||
line: line
|
||||
end
|
||||
|
||||
defp generate_buffer([{:eof, _, _}], buffer, [], state) do
|
||||
defp generate_buffer([], buffer, [], state) do
|
||||
state.engine.handle_body(buffer)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:eof, line, column}], _buffer, _scope, state) do
|
||||
defp generate_buffer([], _buffer, _scope, state) do
|
||||
raise EEx.SyntaxError,
|
||||
message: "unexpected end of string, expected a closing '<% end %>'",
|
||||
file: state.file,
|
||||
line: line,
|
||||
column: column
|
||||
line: state.line
|
||||
end
|
||||
|
||||
# Creates a placeholder and wrap it inside the expression block
|
||||
@@ -193,22 +139,26 @@ defmodule EEx.Compiler do
|
||||
{count, new_state}
|
||||
end
|
||||
|
||||
# Look middle expressions that immediately follow a start_expr
|
||||
# Look text ahead on expressions
|
||||
|
||||
defp look_ahead_middle([{:text, _, _, text} | rest], start, contents) do
|
||||
defp look_ahead_text(
|
||||
[{:text, text}, {:middle_expr, line, _, chars} | rest] = tokens,
|
||||
start,
|
||||
contents
|
||||
) do
|
||||
if only_spaces?(text) do
|
||||
look_ahead_middle(rest, start, contents ++ text)
|
||||
{contents ++ text ++ chars, line, rest}
|
||||
else
|
||||
nil
|
||||
{contents, start, tokens}
|
||||
end
|
||||
end
|
||||
|
||||
defp look_ahead_middle([{:middle_expr, line, _column, _, chars} | rest], _start, contents) do
|
||||
{rest, line, contents ++ chars}
|
||||
defp look_ahead_text([{:middle_expr, line, _, chars} | rest], _start, contents) do
|
||||
{contents ++ chars, line, rest}
|
||||
end
|
||||
|
||||
defp look_ahead_middle(_tokens, _start, _contents) do
|
||||
nil
|
||||
defp look_ahead_text(tokens, start, contents) do
|
||||
{contents, start, tokens}
|
||||
end
|
||||
|
||||
defp only_spaces?(chars) do
|
||||
@@ -237,9 +187,4 @@ defmodule EEx.Compiler do
|
||||
defp insert_quoted(other, _quoted) do
|
||||
other
|
||||
end
|
||||
|
||||
defp column(column, mark) do
|
||||
# length('<%') == 2
|
||||
column + 2 + length(mark)
|
||||
end
|
||||
end
|
||||
|
||||
+14
-16
@@ -4,8 +4,10 @@ defmodule EEx.Engine do
|
||||
|
||||
An engine needs to implement all callbacks below.
|
||||
|
||||
This module also ships with a default engine implementation
|
||||
you can delegate to. See `EEx.SmartEngine` as an example.
|
||||
An engine may also `use EEx.Engine` to get the default behaviour
|
||||
but this is not advised. In such cases, if any of the callbacks
|
||||
are overridden, they must call `super()` to delegate to the
|
||||
underlying `EEx.Engine`.
|
||||
"""
|
||||
|
||||
@type state :: term
|
||||
@@ -29,8 +31,7 @@ defmodule EEx.Engine do
|
||||
|
||||
It must return the updated state.
|
||||
"""
|
||||
@callback handle_text(state, [line: pos_integer, column: pos_integer], text :: String.t()) ::
|
||||
state
|
||||
@callback handle_text(state, text :: String.t()) :: state
|
||||
|
||||
@doc """
|
||||
Called for the dynamic/code parts of a template.
|
||||
@@ -68,7 +69,6 @@ defmodule EEx.Engine do
|
||||
@callback handle_end(state) :: Macro.t()
|
||||
|
||||
@doc false
|
||||
@deprecated "Use explicit delegation to EEx.Engine instead"
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
@behaviour EEx.Engine
|
||||
@@ -90,7 +90,7 @@ defmodule EEx.Engine do
|
||||
end
|
||||
|
||||
def handle_text(state, text) do
|
||||
EEx.Engine.handle_text(state, [], text)
|
||||
EEx.Engine.handle_text(state, text)
|
||||
end
|
||||
|
||||
def handle_expr(state, marker, expr) do
|
||||
@@ -127,7 +127,7 @@ defmodule EEx.Engine do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Raise on v2.0
|
||||
# TODO: Raise on 2.0
|
||||
@spec fetch_assign!(Access.t(), Access.key()) :: term | nil
|
||||
def fetch_assign!(assigns, key) do
|
||||
case Access.fetch(assigns, key) do
|
||||
@@ -147,7 +147,7 @@ defmodule EEx.Engine do
|
||||
end
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:init/1`."
|
||||
@doc false
|
||||
def init(_opts) do
|
||||
%{
|
||||
binary: [],
|
||||
@@ -156,18 +156,18 @@ defmodule EEx.Engine do
|
||||
}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_begin/1`."
|
||||
@doc false
|
||||
def handle_begin(state) do
|
||||
check_state!(state)
|
||||
%{state | binary: [], dynamic: []}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_end/1`."
|
||||
@doc false
|
||||
def handle_end(quoted) do
|
||||
handle_body(quoted)
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_body/1`."
|
||||
@doc false
|
||||
def handle_body(state) do
|
||||
check_state!(state)
|
||||
%{binary: binary, dynamic: dynamic} = state
|
||||
@@ -176,16 +176,14 @@ defmodule EEx.Engine do
|
||||
{:__block__, [], Enum.reverse(dynamic)}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_text/3`."
|
||||
def handle_text(state, _meta, text) do
|
||||
check_state!(state)
|
||||
@doc false
|
||||
def handle_text(state, text) do
|
||||
%{binary: binary} = state
|
||||
%{state | binary: [text | binary]}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_expr/3`."
|
||||
@doc false
|
||||
def handle_expr(state, "=", ast) do
|
||||
check_state!(state)
|
||||
%{binary: binary, dynamic: dynamic, vars_count: vars_count} = state
|
||||
var = Macro.var(:"arg#{vars_count}", __MODULE__)
|
||||
|
||||
|
||||
@@ -24,35 +24,18 @@ defmodule EEx.SmartEngine do
|
||||
# sample.ex
|
||||
defmodule Sample do
|
||||
require EEx
|
||||
EEx.function_from_file(:def, :sample, "sample.eex", [:assigns])
|
||||
EEx.function_from_file :def, :sample, "sample.eex", [:assigns]
|
||||
end
|
||||
|
||||
# iex
|
||||
Sample.sample(a: 1, b: 2)
|
||||
#=> "3"
|
||||
Sample.sample(a: 1, b: 2) #=> "3"
|
||||
|
||||
"""
|
||||
|
||||
@behaviour EEx.Engine
|
||||
use EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate init(opts), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_body(state), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_begin(state), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_end(state), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_text(state, meta, text), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
def handle_expr(state, marker, expr) do
|
||||
def handle_expr(buffer, mark, expr) do
|
||||
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
|
||||
EEx.Engine.handle_expr(state, marker, expr)
|
||||
super(buffer, mark, expr)
|
||||
end
|
||||
end
|
||||
|
||||
+136
-149
@@ -3,105 +3,78 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
@type content :: IO.chardata()
|
||||
@type line :: non_neg_integer
|
||||
@type column :: non_neg_integer
|
||||
@type marker :: '=' | '/' | '|' | ''
|
||||
@type token ::
|
||||
{:text, line, column, content}
|
||||
| {:expr | :start_expr | :middle_expr | :end_expr, line, column, marker, content}
|
||||
| {:eof, line, column}
|
||||
|
||||
@spaces [?\s, ?\t]
|
||||
{:text, content}
|
||||
| {:expr | :start_expr | :middle_expr | :end_expr, line, marker, content}
|
||||
|
||||
@doc """
|
||||
Tokenizes the given charlist or binary.
|
||||
|
||||
It returns {:ok, list} with the following tokens:
|
||||
|
||||
* `{:text, line, column, content}`
|
||||
* `{:expr, line, column, marker, content}`
|
||||
* `{:start_expr, line, column, marker, content}`
|
||||
* `{:middle_expr, line, column, marker, content}`
|
||||
* `{:end_expr, line, column, marker, content}`
|
||||
* `{:eof, line, column}`
|
||||
* `{:text, content}`
|
||||
* `{:expr, line, marker, content}`
|
||||
* `{:start_expr, line, marker, content}`
|
||||
* `{:middle_expr, line, marker, content}`
|
||||
* `{:end_expr, line, marker, content}`
|
||||
|
||||
Or `{:error, line, column, message}` in case of errors.
|
||||
Or `{:error, line, error}` in case of errors.
|
||||
"""
|
||||
@spec tokenize(binary | charlist, line, column, map) ::
|
||||
{:ok, [token]} | {:error, line, column, String.t()}
|
||||
@spec tokenize(binary | charlist, line, keyword) :: {:ok, [token]} | {:error, line, String.t()}
|
||||
def tokenize(bin, line, opts \\ [])
|
||||
|
||||
def tokenize(bin, line, column, opts) when is_binary(bin) do
|
||||
tokenize(String.to_charlist(bin), line, column, opts)
|
||||
def tokenize(bin, line, opts)
|
||||
when is_binary(bin) and is_integer(line) and line >= 0 and is_list(opts) do
|
||||
tokenize(String.to_charlist(bin), line, opts)
|
||||
end
|
||||
|
||||
def tokenize(list, line, column, opts)
|
||||
when is_list(list) and is_integer(line) and line >= 0 and is_integer(column) and column >= 0 do
|
||||
column = opts.indentation + column
|
||||
|
||||
{list, line, column} =
|
||||
(opts.trim && trim_init(list, line, column, opts)) || {list, line, column}
|
||||
|
||||
tokenize(list, line, column, opts, [{line, column}], [])
|
||||
def tokenize(list, line, opts)
|
||||
when is_list(list) and is_integer(line) and line >= 0 and is_list(opts) do
|
||||
tokenize(list, line, opts, [], [])
|
||||
end
|
||||
|
||||
defp tokenize('<%%' ++ t, line, column, opts, buffer, acc) do
|
||||
tokenize(t, line, column + 3, opts, [?%, ?< | buffer], acc)
|
||||
defp tokenize('<%%' ++ t, line, opts, buffer, acc) do
|
||||
tokenize(t, line, opts, [?%, ?< | buffer], acc)
|
||||
end
|
||||
|
||||
defp tokenize('<%#' ++ t, line, column, opts, buffer, acc) do
|
||||
case expr(t, line, column + 3, opts, []) do
|
||||
{:error, _, _, _} = error ->
|
||||
defp tokenize('<%#' ++ t, line, opts, buffer, acc) do
|
||||
case expr(t, line, []) do
|
||||
{:error, _, _} = error ->
|
||||
error
|
||||
|
||||
{:ok, _, new_line, new_column, rest} ->
|
||||
{rest, new_line, new_column, buffer} =
|
||||
trim_if_needed(rest, new_line, new_column, opts, buffer)
|
||||
|
||||
acc = tokenize_text(buffer, acc)
|
||||
tokenize(rest, new_line, new_column, opts, [{new_line, new_column}], acc)
|
||||
{: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
|
||||
end
|
||||
|
||||
defp tokenize('<%' ++ t, line, column, opts, buffer, acc) do
|
||||
defp tokenize('<%' ++ t, line, opts, buffer, acc) do
|
||||
{marker, t} = retrieve_marker(t)
|
||||
|
||||
case expr(t, line, column + 2 + length(marker), opts, []) do
|
||||
{:error, _, _, _} = error ->
|
||||
case expr(t, line, []) do
|
||||
{:error, _, _} = error ->
|
||||
error
|
||||
|
||||
{:ok, expr, new_line, new_column, rest} ->
|
||||
{key, expr} =
|
||||
case :elixir_tokenizer.tokenize(expr, 1, file: "eex", check_terminators: false) do
|
||||
{:ok, _line, _column, warnings, tokens} ->
|
||||
Enum.each(Enum.reverse(warnings), fn {location, file, msg} ->
|
||||
:elixir_errors.erl_warn(location, file, msg)
|
||||
end)
|
||||
|
||||
token_key(tokens, expr)
|
||||
|
||||
{:error, _, _, _, _} ->
|
||||
{:expr, expr}
|
||||
end
|
||||
|
||||
{rest, new_line, new_column, buffer} =
|
||||
trim_if_needed(rest, new_line, new_column, opts, buffer)
|
||||
|
||||
{: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 = {key, line, column, marker, expr}
|
||||
tokenize(rest, new_line, new_column, opts, [{new_line, new_column}], [final | acc])
|
||||
final = {token, line, marker, Enum.reverse(expr)}
|
||||
tokenize(rest, new_line, opts, [], [final | acc])
|
||||
end
|
||||
end
|
||||
|
||||
defp tokenize('\n' ++ t, line, _column, opts, buffer, acc) do
|
||||
tokenize(t, line + 1, opts.indentation + 1, opts, [?\n | buffer], acc)
|
||||
defp tokenize('\n' ++ t, line, opts, buffer, acc) do
|
||||
tokenize(t, line + 1, opts, [?\n | buffer], acc)
|
||||
end
|
||||
|
||||
defp tokenize([h | t], line, column, opts, buffer, acc) do
|
||||
tokenize(t, line, column + 1, opts, [h | buffer], acc)
|
||||
defp tokenize([h | t], line, opts, buffer, acc) do
|
||||
tokenize(t, line, opts, [h | buffer], acc)
|
||||
end
|
||||
|
||||
defp tokenize([], line, column, _opts, buffer, acc) do
|
||||
eof = {:eof, line, column}
|
||||
{:ok, Enum.reverse([eof | tokenize_text(buffer, acc)])}
|
||||
defp tokenize([], _line, _opts, buffer, acc) do
|
||||
{:ok, Enum.reverse(tokenize_text(buffer, acc))}
|
||||
end
|
||||
|
||||
# Retrieve marker for <%
|
||||
@@ -116,129 +89,143 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
# Tokenize an expression until we find %>
|
||||
|
||||
defp expr([?%, ?> | t], line, column, _opts, buffer) do
|
||||
{:ok, Enum.reverse(buffer), line, column + 2, t}
|
||||
defp expr([?%, ?> | t], line, buffer) do
|
||||
{:ok, buffer, line, t}
|
||||
end
|
||||
|
||||
defp expr('\n' ++ t, line, _column, opts, buffer) do
|
||||
expr(t, line + 1, opts.indentation + 1, opts, [?\n | buffer])
|
||||
defp expr('\n' ++ t, line, buffer) do
|
||||
expr(t, line + 1, [?\n | buffer])
|
||||
end
|
||||
|
||||
defp expr([h | t], line, column, opts, buffer) do
|
||||
expr(t, line, column + 1, opts, [h | buffer])
|
||||
defp expr([h | t], line, buffer) do
|
||||
expr(t, line, [h | buffer])
|
||||
end
|
||||
|
||||
defp expr([], line, column, _opts, _buffer) do
|
||||
{:error, line, column, "missing token '%>'"}
|
||||
defp expr([], line, _buffer) do
|
||||
{:error, line, "missing token '%>'"}
|
||||
end
|
||||
|
||||
# Receives tokens and check if it is a start, middle or an end token.
|
||||
defp token_key(tokens, expr) do
|
||||
case {tokens, tokens |> Enum.reverse() |> drop_eol()} do
|
||||
{[{:end, _} | _], [{:do, _} | _]} ->
|
||||
{:middle_expr, expr}
|
||||
# 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 ")"
|
||||
|
||||
{_, [{:do, _} | _]} ->
|
||||
{:start_expr, maybe_append_space(expr)}
|
||||
defp token_name([h | t]) when h in [?\s, ?\t, ?)] do
|
||||
token_name(t)
|
||||
end
|
||||
|
||||
{_, [{:block_identifier, _, _} | _]} ->
|
||||
{:middle_expr, maybe_append_space(expr)}
|
||||
defp token_name('od' ++ [h | _]) when h in [?\s, ?\t, ?)] do
|
||||
:start_expr
|
||||
end
|
||||
|
||||
{[{:end, _} | _], [{:stab_op, _, _} | _]} ->
|
||||
{:middle_expr, expr}
|
||||
defp token_name('>-' ++ rest) do
|
||||
rest = Enum.reverse(rest)
|
||||
|
||||
{_, [{:stab_op, _, _} | reverse_tokens]} ->
|
||||
fn_index = Enum.find_index(reverse_tokens, &match?({:fn, _}, &1)) || :infinity
|
||||
end_index = Enum.find_index(reverse_tokens, &match?({:end, _}, &1)) || :infinity
|
||||
# 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, tokens} ->
|
||||
tokens = Enum.reverse(tokens)
|
||||
fn_index = fn_index(tokens)
|
||||
|
||||
if end_index > fn_index do
|
||||
{:start_expr, expr}
|
||||
if fn_index && end_index(tokens) > fn_index do
|
||||
:start_expr
|
||||
else
|
||||
{:middle_expr, expr}
|
||||
:middle_expr
|
||||
end
|
||||
|
||||
{tokens, _} ->
|
||||
case Enum.drop_while(tokens, &closing_bracket?/1) do
|
||||
[{:end, _} | _] -> {:end_expr, expr}
|
||||
_ -> {:expr, expr}
|
||||
end
|
||||
_error ->
|
||||
:middle_expr
|
||||
end
|
||||
end
|
||||
|
||||
defp drop_eol([{:eol, _} | rest]), do: drop_eol(rest)
|
||||
defp drop_eol(rest), do: rest
|
||||
defp token_name('esle' ++ t), do: check_spaces(t, :middle_expr)
|
||||
defp token_name('retfa' ++ t), do: check_spaces(t, :middle_expr)
|
||||
defp token_name('hctac' ++ t), do: check_spaces(t, :middle_expr)
|
||||
defp token_name('eucser' ++ t), do: check_spaces(t, :middle_expr)
|
||||
defp token_name('dne' ++ t), do: check_spaces(t, :end_expr)
|
||||
|
||||
defp maybe_append_space([?\s]), do: [?\s]
|
||||
defp maybe_append_space([h]), do: [h, ?\s]
|
||||
defp maybe_append_space([h | t]), do: [h | maybe_append_space(t)]
|
||||
defp token_name(_) do
|
||||
:expr
|
||||
end
|
||||
|
||||
defp closing_bracket?({closing, _}) when closing in ~w"( [ {"a, do: true
|
||||
defp closing_bracket?(_), do: false
|
||||
defp fn_index(tokens) do
|
||||
Enum.find_index(tokens, fn
|
||||
{:fn_paren, _} -> true
|
||||
{:fn, _} -> true
|
||||
_ -> false
|
||||
end)
|
||||
end
|
||||
|
||||
defp end_index(tokens) do
|
||||
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
|
||||
end
|
||||
|
||||
# Tokenize the buffered text by appending
|
||||
# it to the given accumulator.
|
||||
|
||||
defp tokenize_text([{_line, _column}], acc) do
|
||||
defp tokenize_text([], acc) do
|
||||
acc
|
||||
end
|
||||
|
||||
defp tokenize_text(buffer, acc) do
|
||||
[{line, column} | buffer] = Enum.reverse(buffer)
|
||||
[{:text, line, column, buffer} | acc]
|
||||
[{:text, Enum.reverse(buffer)} | acc]
|
||||
end
|
||||
|
||||
defp trim_if_needed(rest, line, column, opts, buffer) do
|
||||
if opts.trim do
|
||||
buffer = trim_left(buffer, 0)
|
||||
{rest, line, column} = trim_right(rest, line, column, 0, opts)
|
||||
{rest, line, column, buffer}
|
||||
# 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
|
||||
{rest, line, column, buffer}
|
||||
original
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_init([h | t], line, column, opts) when h in @spaces,
|
||||
do: trim_init(t, line, column + 1, opts)
|
||||
|
||||
defp trim_init([?\r, ?\n | t], line, _column, opts),
|
||||
do: trim_init(t, line + 1, opts.indentation + 1, opts)
|
||||
|
||||
defp trim_init([?\n | t], line, _column, opts),
|
||||
do: trim_init(t, line + 1, opts.indentation + 1, opts)
|
||||
|
||||
defp trim_init([?<, ?% | _] = rest, line, column, _opts),
|
||||
do: {rest, line, column}
|
||||
|
||||
defp trim_init(_, _, _, _), do: false
|
||||
|
||||
defp trim_left(buffer, count) do
|
||||
case trim_whitespace(buffer, 0) do
|
||||
{[?\n, ?\r | rest], _} -> trim_left(rest, count + 1)
|
||||
{[?\n | rest], _} -> trim_left(rest, count + 1)
|
||||
_ when count > 0 -> [?\n | buffer]
|
||||
_ -> buffer
|
||||
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, column, last_column, opts) do
|
||||
case trim_whitespace(rest, column) do
|
||||
{[?\r, ?\n | rest], column} ->
|
||||
trim_right(rest, line + 1, opts.indentation + 1, column + 1, opts)
|
||||
|
||||
{[?\n | rest], column} ->
|
||||
trim_right(rest, line + 1, opts.indentation + 1, column, opts)
|
||||
|
||||
{[], column} ->
|
||||
{[], line, column}
|
||||
|
||||
_ when last_column > 0 ->
|
||||
{[?\n | rest], line - 1, last_column}
|
||||
|
||||
_ ->
|
||||
{rest, line, column}
|
||||
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], column) when h in @spaces, do: trim_whitespace(t, column + 1)
|
||||
defp trim_whitespace(list, column), do: {list, column}
|
||||
defp trim_whitespace([h | t]) when h == ?\s or h == ?\t do
|
||||
trim_whitespace(t)
|
||||
end
|
||||
|
||||
defp trim_whitespace(list) do
|
||||
list
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
Code.require_file("../test_helper.exs", __DIR__)
|
||||
|
||||
defmodule EEx.SmartEngineTest do
|
||||
use ExUnit.Case, async: true
|
||||
# TODO: Make this async: true once capture_io is removed
|
||||
use ExUnit.Case
|
||||
|
||||
test "evaluates simple string" do
|
||||
assert_eval("foo bar", "foo bar")
|
||||
@@ -28,28 +29,16 @@ defmodule EEx.SmartEngineTest do
|
||||
assert_eval("1\n2\n3\n", "<%= for x <- [1, 2, 3] do %><%= x %>\n<% end %>")
|
||||
end
|
||||
|
||||
test "preserves line numbers in assignments" do
|
||||
result = EEx.compile_string("foo\n<%= @hello %>", engine: EEx.SmartEngine)
|
||||
test "preserves line numbers" do
|
||||
result = EEx.compile_string("<%= @hello %>", engine: EEx.SmartEngine)
|
||||
|
||||
Macro.prewalk(result, fn
|
||||
{_left, meta, [_, :hello]} ->
|
||||
assert Keyword.get(meta, :line) == 2
|
||||
send(self(), :found)
|
||||
{_left, meta, _right} ->
|
||||
assert Keyword.get(meta, :line, 0) in [0, 1]
|
||||
|
||||
node ->
|
||||
node
|
||||
_ ->
|
||||
:ok
|
||||
end)
|
||||
|
||||
assert_received :found
|
||||
end
|
||||
|
||||
test "error with unused \"do\" block without \"<%=\" modifier" do
|
||||
stderr =
|
||||
ExUnit.CaptureIO.capture_io(:stderr, fn ->
|
||||
assert_eval("", "<% if true do %>I'm invisible!<% end %>", assigns: %{})
|
||||
end)
|
||||
|
||||
assert stderr =~ "the contents of this expression won't be output"
|
||||
end
|
||||
|
||||
defp assert_eval(expected, actual, binding \\ []) do
|
||||
|
||||
@@ -4,39 +4,32 @@ defmodule EEx.TokenizerTest do
|
||||
use ExUnit.Case, async: true
|
||||
require EEx.Tokenizer, as: T
|
||||
|
||||
@opts %{indentation: 0, trim: false}
|
||||
|
||||
test "simple chars lists" do
|
||||
assert T.tokenize('foo', 1, 1, @opts) == {:ok, [{:text, 1, 1, 'foo'}, {:eof, 1, 4}]}
|
||||
assert T.tokenize('foo', 1) == {:ok, [{:text, 'foo'}]}
|
||||
end
|
||||
|
||||
test "simple strings" do
|
||||
assert T.tokenize("foo", 1, 1, @opts) == {:ok, [{:text, 1, 1, 'foo'}, {:eof, 1, 4}]}
|
||||
assert T.tokenize("foo", 1) == {:ok, [{:text, 'foo'}]}
|
||||
end
|
||||
|
||||
test "strings with embedded code" do
|
||||
assert T.tokenize('foo <% bar %>', 1, 1, @opts) ==
|
||||
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '', ' bar '}, {:eof, 1, 14}]}
|
||||
assert T.tokenize('foo <% bar %>', 1) == {:ok, [{:text, 'foo '}, {:expr, 1, '', ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with embedded equals code" do
|
||||
assert T.tokenize('foo <%= bar %>', 1, 1, @opts) ==
|
||||
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '=', ' bar '}, {:eof, 1, 15}]}
|
||||
assert T.tokenize('foo <%= bar %>', 1) == {:ok, [{:text, 'foo '}, {:expr, 1, '=', ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with embedded slash code" do
|
||||
assert T.tokenize('foo <%/ bar %>', 1, 1, @opts) ==
|
||||
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '/', ' bar '}, {:eof, 1, 15}]}
|
||||
assert T.tokenize('foo <%/ bar %>', 1) == {:ok, [{:text, 'foo '}, {:expr, 1, '/', ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with embedded pipe code" do
|
||||
assert T.tokenize('foo <%| bar %>', 1, 1, @opts) ==
|
||||
{:ok, [{:text, 1, 1, 'foo '}, {:expr, 1, 5, '|', ' bar '}, {:eof, 1, 15}]}
|
||||
assert T.tokenize('foo <%| bar %>', 1) == {:ok, [{:text, 'foo '}, {:expr, 1, '|', ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with more than one line" do
|
||||
assert T.tokenize('foo\n<%= bar %>', 1, 1, @opts) ==
|
||||
{:ok, [{:text, 1, 1, 'foo\n'}, {:expr, 2, 1, '=', ' bar '}, {:eof, 2, 11}]}
|
||||
assert T.tokenize('foo\n<%= bar %>', 1) == {:ok, [{:text, 'foo\n'}, {:expr, 2, '=', ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with more than one line and expression with more than one line" do
|
||||
@@ -48,244 +41,144 @@ defmodule EEx.TokenizerTest do
|
||||
'''
|
||||
|
||||
exprs = [
|
||||
{:text, 1, 1, 'foo '},
|
||||
{:expr, 1, 5, '=', ' bar\n\nbaz '},
|
||||
{:text, 3, 7, '\n'},
|
||||
{:expr, 4, 1, '', ' foo '},
|
||||
{:text, 4, 10, '\n'},
|
||||
{:eof, 5, 1}
|
||||
{:text, 'foo '},
|
||||
{:expr, 1, '=', ' bar\n\nbaz '},
|
||||
{:text, '\n'},
|
||||
{:expr, 4, '', ' foo '},
|
||||
{:text, '\n'}
|
||||
]
|
||||
|
||||
assert T.tokenize(string, 1, 1, @opts) == {:ok, exprs}
|
||||
assert T.tokenize(string, 1) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "quotation" do
|
||||
assert T.tokenize('foo <%% true %>', 1, 1, @opts) ==
|
||||
{:ok, [{:text, 1, 1, 'foo <% true %>'}, {:eof, 1, 16}]}
|
||||
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, 1, @opts) ==
|
||||
{:ok, [{:text, 1, 1, 'foo <% true do %>bar<% end %>'}, {:eof, 1, 32}]}
|
||||
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
|
||||
exprs = [
|
||||
{:text, 1, 1, 'a <% b '},
|
||||
{:expr, 1, 9, '=', ' c '},
|
||||
{:text, 1, 17, ' '},
|
||||
{:expr, 1, 18, '=', ' d '},
|
||||
{:text, 1, 26, ' e %> f'},
|
||||
{:eof, 1, 33}
|
||||
{:text, 'a <% b '},
|
||||
{:expr, 1, '=', ' c '},
|
||||
{:text, ' '},
|
||||
{:expr, 1, '=', ' d '},
|
||||
{:text, ' e %> f'}
|
||||
]
|
||||
|
||||
assert T.tokenize('a <%% b <%= c %> <%= d %> e %> f', 1, 1, @opts) == {:ok, exprs}
|
||||
assert T.tokenize('a <%% b <%= c %> <%= d %> e %> f', 1) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "improperly formatted quotation with interpolation" do
|
||||
exprs = [
|
||||
{:text, 1, 1, '<%% a <%= b %> c %>'},
|
||||
{:eof, 1, 22}
|
||||
{:text, '<%% a <%= b %> c %>'}
|
||||
]
|
||||
|
||||
assert T.tokenize('<%%% a <%%= b %> c %>', 1, 1, @opts) == {:ok, exprs}
|
||||
assert T.tokenize('<%%% a <%%= b %> c %>', 1) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "EEx comments" do
|
||||
test "comments" do
|
||||
exprs = [
|
||||
{:text, 1, 1, 'foo '},
|
||||
{:eof, 1, 16}
|
||||
{:text, 'foo '}
|
||||
]
|
||||
|
||||
assert T.tokenize('foo <%# true %>', 1, 1, @opts) == {:ok, exprs}
|
||||
assert T.tokenize('foo <%# true %>', 1) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "EEx comments with do-end" do
|
||||
test "comments with do/end" do
|
||||
exprs = [
|
||||
{:text, 1, 1, 'foo '},
|
||||
{:text, 1, 19, 'bar'},
|
||||
{:eof, 1, 32}
|
||||
{:text, 'foo bar'}
|
||||
]
|
||||
|
||||
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1, 1, @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "EEx comments inside do-end" do
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, '', ' if true do '},
|
||||
{:text, 1, 31, 'bar'},
|
||||
{:end_expr, 1, 34, [], ' end '},
|
||||
{:eof, 1, 43}
|
||||
]
|
||||
|
||||
assert T.tokenize('<% if true do %><%# comment %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
|
||||
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, [], ' case true do '},
|
||||
{:middle_expr, 1, 33, '', ' true -> '},
|
||||
{:text, 1, 46, 'bar'},
|
||||
{:end_expr, 1, 49, [], ' end '},
|
||||
{:eof, 1, 58}
|
||||
]
|
||||
|
||||
assert T.tokenize('<% case true do %><%# comment %><% true -> %>bar<% end %>', 1, 1, @opts) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "Elixir comments" do
|
||||
exprs = [
|
||||
{:text, 1, 1, 'foo '},
|
||||
{:expr, 1, 5, [], ' true # this is a boolean '},
|
||||
{:eof, 1, 35}
|
||||
]
|
||||
|
||||
assert T.tokenize('foo <% true # this is a boolean %>', 1, 1, @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "Elixir comments with do-end" do
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, [], ' if true do # startif '},
|
||||
{:text, 1, 27, 'text'},
|
||||
{:end_expr, 1, 31, [], ' end # closeif '},
|
||||
{:eof, 1, 50}
|
||||
]
|
||||
|
||||
assert T.tokenize('<% if true do # startif %>text<% end # closeif %>', 1, 1, @opts) ==
|
||||
{:ok, exprs}
|
||||
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with embedded do end" do
|
||||
exprs = [
|
||||
{:text, 1, 1, 'foo '},
|
||||
{:start_expr, 1, 5, '', ' if true do '},
|
||||
{:text, 1, 21, 'bar'},
|
||||
{:end_expr, 1, 24, '', ' end '},
|
||||
{:eof, 1, 33}
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:end_expr, 1, '', ' end '}
|
||||
]
|
||||
|
||||
assert T.tokenize('foo <% if true do %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
|
||||
assert T.tokenize('foo <% if true do %>bar<% end %>', 1) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with embedded -> end" do
|
||||
exprs = [
|
||||
{:text, 1, 1, 'foo '},
|
||||
{:start_expr, 1, 5, '', ' cond do '},
|
||||
{:middle_expr, 1, 18, '', ' false -> '},
|
||||
{:text, 1, 32, 'bar'},
|
||||
{:middle_expr, 1, 35, '', ' true -> '},
|
||||
{:text, 1, 48, 'baz'},
|
||||
{:end_expr, 1, 51, '', ' end '},
|
||||
{:eof, 1, 60}
|
||||
{: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, 1, @opts) ==
|
||||
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with fn-end with newline" do
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, '=', ' a fn ->\n'},
|
||||
{:text, 2, 3, 'foo'},
|
||||
{:end_expr, 2, 6, [], ' end '},
|
||||
{:eof, 2, 15}
|
||||
]
|
||||
|
||||
assert T.tokenize('<%= a fn ->\n%>foo<% end %>', 1, 1, @opts) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with multiple fn-end" do
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, '=', ' a fn -> '},
|
||||
{:text, 1, 15, 'foo'},
|
||||
{:middle_expr, 1, 18, '', ' end, fn -> '},
|
||||
{:text, 1, 34, 'bar'},
|
||||
{:end_expr, 1, 37, '', ' end '},
|
||||
{:eof, 1, 46}
|
||||
]
|
||||
|
||||
assert T.tokenize('<%= a fn -> %>foo<% end, fn -> %>bar<% end %>', 1, 1, @opts) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with fn-end followed by do block" do
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, '=', ' a fn -> '},
|
||||
{:text, 1, 15, 'foo'},
|
||||
{:middle_expr, 1, 18, '', ' end do '},
|
||||
{:text, 1, 30, 'bar'},
|
||||
{:end_expr, 1, 33, '', ' end '},
|
||||
{:eof, 1, 42}
|
||||
]
|
||||
|
||||
assert T.tokenize('<%= a fn -> %>foo<% end do %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with embedded keywords blocks" do
|
||||
exprs = [
|
||||
{:text, 1, 1, 'foo '},
|
||||
{:start_expr, 1, 5, '', ' if true do '},
|
||||
{:text, 1, 21, 'bar'},
|
||||
{:middle_expr, 1, 24, '', ' else '},
|
||||
{:text, 1, 34, 'baz'},
|
||||
{:end_expr, 1, 37, '', ' end '},
|
||||
{:eof, 1, 46}
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:middle_expr, 1, '', ' else '},
|
||||
{:text, 'baz'},
|
||||
{:end_expr, 1, '', ' end '}
|
||||
]
|
||||
|
||||
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1, 1, @opts) ==
|
||||
{:ok, exprs}
|
||||
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "trim mode" do
|
||||
template = '\t<%= if true do %> \n TRUE \n <% else %>\n FALSE \n <% end %> \n\n '
|
||||
template = '\t<%= if true do %> \n TRUE \n <% else %>\n FALSE \n <% end %> '
|
||||
|
||||
exprs = [
|
||||
{:start_expr, 1, 2, '=', ' if true do '},
|
||||
{:text, 1, 20, '\n TRUE \n'},
|
||||
{:middle_expr, 3, 3, '', ' else '},
|
||||
{:text, 3, 13, '\n FALSE \n'},
|
||||
{:end_expr, 5, 3, '', ' end '},
|
||||
{:eof, 7, 3}
|
||||
{:start_expr, 1, '=', ' if true do '},
|
||||
{:text, ' TRUE \n'},
|
||||
{:middle_expr, 3, '', ' else '},
|
||||
{:text, ' FALSE \n'},
|
||||
{:end_expr, 5, '', ' end '}
|
||||
]
|
||||
|
||||
assert T.tokenize(template, 1, 1, %{@opts | trim: true}) == {:ok, exprs}
|
||||
assert T.tokenize(template, 1, trim: true) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "trim mode with comment" do
|
||||
exprs = [
|
||||
{:text, 1, 19, '\n123'},
|
||||
{:eof, 2, 4}
|
||||
{:text, '123'}
|
||||
]
|
||||
|
||||
assert T.tokenize(' <%# comment %> \n123', 1, 1, %{@opts | trim: true}) == {:ok, exprs}
|
||||
assert T.tokenize(' <%# comment %> \n123', 1, trim: true) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "trim mode with CRLF" do
|
||||
exprs = [
|
||||
{:text, 1, 1, '0\n'},
|
||||
{:expr, 2, 3, '=', ' 12 '},
|
||||
{:text, 2, 15, '\n34'},
|
||||
{:eof, 3, 3}
|
||||
{:text, '0\r\n'},
|
||||
{:expr, 2, '=', ' 12 '},
|
||||
{:text, '34'}
|
||||
]
|
||||
|
||||
assert T.tokenize('0\r\n <%= 12 %> \r\n34', 1, 1, %{@opts | trim: true}) == {:ok, exprs}
|
||||
assert T.tokenize('0\r\n <%= 12 %> \r\n34', 1, trim: true) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "trim mode set to false" do
|
||||
exprs = [
|
||||
{:text, 1, 1, ' '},
|
||||
{:expr, 1, 2, '=', ' 12 '},
|
||||
{:text, 1, 11, ' \n'},
|
||||
{:eof, 2, 1}
|
||||
{:text, ' '},
|
||||
{:expr, 1, '=', ' 12 '},
|
||||
{:text, ' \n'}
|
||||
]
|
||||
|
||||
assert T.tokenize(' <%= 12 %> \n', 1, 1, %{@opts | trim: false}) == {:ok, exprs}
|
||||
assert T.tokenize(' <%= 12 %> \n', 1, trim: false) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "trim mode no false positives" do
|
||||
assert_not_trimmed = fn x ->
|
||||
assert T.tokenize(x, 1, 1, %{@opts | trim: false}) == T.tokenize(x, 1, 1, @opts)
|
||||
end
|
||||
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')
|
||||
@@ -293,13 +186,8 @@ defmodule EEx.TokenizerTest do
|
||||
assert_not_trimmed.(' <%= 01 %><%= 23 %>\n')
|
||||
end
|
||||
|
||||
test "returns error when there is start mark and no end mark" do
|
||||
assert T.tokenize('foo <% :bar', 1, 1, @opts) == {:error, 1, 12, "missing token '%>'"}
|
||||
assert T.tokenize('<%# true ', 1, 1, @opts) == {:error, 1, 10, "missing token '%>'"}
|
||||
end
|
||||
|
||||
test "marks invalid expressions as regular expressions" do
|
||||
assert T.tokenize('<% 1 $ 2 %>', 1, 1, @opts) ==
|
||||
{:ok, [{:expr, 1, 1, [], ' 1 $ 2 '}, {:eof, 1, 12}]}
|
||||
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 T.tokenize('<%# true ', 1) == {:error, 1, "missing token '%>'"}
|
||||
end
|
||||
end
|
||||
|
||||
+30
-277
@@ -64,43 +64,9 @@ defmodule EExTest do
|
||||
assert_eval(" • • •\n Jößé Vâlìm Jößé Vâlìm\n", template)
|
||||
end
|
||||
|
||||
test "no spaces" do
|
||||
string = """
|
||||
<%=cond do%>
|
||||
<%false ->%>
|
||||
this
|
||||
<%true ->%>
|
||||
that
|
||||
<%end%>
|
||||
"""
|
||||
|
||||
expected = "\n that\n\n"
|
||||
assert_eval(expected, string, [])
|
||||
end
|
||||
|
||||
test "trim mode" do
|
||||
string = "<%= 123 %> \n \n <%= 789 %>"
|
||||
expected = "123\n789"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
|
||||
string = "<%= 123 %> \n456\n <%= 789 %>"
|
||||
expected = "123\n456\n789"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
|
||||
string = "<%= 123 %> \n\n456\n\n <%= 789 %>"
|
||||
expected = "123\n456\n789"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
|
||||
string = "<%= 123 %> \n \n456\n \n <%= 789 %>"
|
||||
expected = "123\n456\n789"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
|
||||
string = "\n <%= 123 %> \n <%= 456 %> \n <%= 789 %> \n"
|
||||
expected = "123\n456\n789"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
|
||||
string = "\r\n <%= 123 %> \r\n <%= 456 %> \r\n <%= 789 %> \r\n"
|
||||
expected = "123\n456\n789"
|
||||
expected = "123456\n789"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
end
|
||||
|
||||
@@ -114,44 +80,7 @@ defmodule EExTest do
|
||||
<% end %>
|
||||
"""
|
||||
|
||||
expected = "\n that\n"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
end
|
||||
|
||||
test "trim mode with multiple lines" do
|
||||
string = """
|
||||
<%= "First line" %>
|
||||
<%= "Second line" %>
|
||||
<%= "Third line" %>
|
||||
<%= "Fourth line" %>
|
||||
"""
|
||||
|
||||
expected = "First line\nSecond line\nThird line\nFourth line"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
end
|
||||
|
||||
test "trim mode with no spaces" do
|
||||
string = """
|
||||
<%=if true do%>
|
||||
this
|
||||
<%else%>
|
||||
that
|
||||
<%end%>
|
||||
"""
|
||||
|
||||
expected = "\n this\n"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
|
||||
string = """
|
||||
<%=cond do%>
|
||||
<%false ->%>
|
||||
this
|
||||
<%true ->%>
|
||||
that
|
||||
<%end%>
|
||||
"""
|
||||
|
||||
expected = "\n that\n"
|
||||
expected = " that\n"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
end
|
||||
|
||||
@@ -171,12 +100,6 @@ defmodule EExTest do
|
||||
assert_eval("foo ", "foo <%= if false do %>bar<% end %>")
|
||||
end
|
||||
|
||||
test "embedded code with do preceded by bracket" do
|
||||
assert_eval("foo bar", "foo <%= if {true}do %>bar<% end %>")
|
||||
assert_eval("foo bar", "foo <%= if (true)do %>bar<% end %>")
|
||||
assert_eval("foo bar", "foo <%= if [true]do %>bar<% end %>")
|
||||
end
|
||||
|
||||
test "embedded code with do end and expression" do
|
||||
assert_eval("foo bar", "foo <%= if true do %><%= :bar %><% end %>")
|
||||
end
|
||||
@@ -198,15 +121,6 @@ defmodule EExTest do
|
||||
assert_eval("foo baz", "foo <%= if false do %>bar<% else %>baz<% end %>")
|
||||
end
|
||||
|
||||
test "embedded code with comments in do end" do
|
||||
assert_eval("foo bar", "foo <%= case true do %><%# comment %><% true -> %>bar<% end %>")
|
||||
|
||||
assert_eval(
|
||||
"foo\n\nbar\n",
|
||||
"foo\n<%= case true do %>\n<%# comment %>\n<% true -> %>\nbar\n<% end %>"
|
||||
)
|
||||
end
|
||||
|
||||
test "embedded code with nested do end" do
|
||||
assert_eval("foo bar", "foo <%= if true do %><%= if true do %>bar<% end %><% end %>")
|
||||
end
|
||||
@@ -218,32 +132,7 @@ defmodule EExTest do
|
||||
)
|
||||
end
|
||||
|
||||
test "embedded code with end followed by bracket" do
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= Enum.map([1, 2, 3], fn x -> %> <%= 100 + x %> <% end) %>"
|
||||
)
|
||||
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= Enum.map([1, 2, 3], fn x ->\n%> <%= 100 + x %> <% end) %>"
|
||||
)
|
||||
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= apply Enum, :map, [[1, 2, 3], fn x -> %> <%= 100 + x %> <% end] %>"
|
||||
)
|
||||
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= #{__MODULE__}.tuple_map {[1, 2, 3], fn x -> %> <%= 100 + x %> <% end} %>"
|
||||
)
|
||||
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= apply(Enum, :map, [[1, 2, 3], fn x -> %> <%= 100 + x %> <% end]) %>"
|
||||
)
|
||||
|
||||
test "embedded code with parentheses after end in end token" do
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= Enum.map([1, 2, 3], (fn x -> %> <%= 100 + x %> <% end) ) %>"
|
||||
@@ -265,36 +154,34 @@ defmodule EExTest do
|
||||
|
||||
describe "raises syntax errors" do
|
||||
test "when the token is invalid" do
|
||||
assert_raise EEx.SyntaxError, "nofile:1:12: missing token '%>'", fn ->
|
||||
assert_raise EEx.SyntaxError, "nofile:1: missing token '%>'", fn ->
|
||||
EEx.compile_string("foo <%= bar")
|
||||
end
|
||||
end
|
||||
|
||||
test "when middle expression is found without a start expression" do
|
||||
assert_raise EEx.SyntaxError,
|
||||
"nofile:1:18: unexpected middle of expression <% else %>",
|
||||
fn ->
|
||||
EEx.compile_string("<%= if true %>foo<% else %>bar<% end %>")
|
||||
end
|
||||
assert_raise EEx.SyntaxError, "nofile:1: unexpected middle of expression <% else %>", fn ->
|
||||
EEx.compile_string("<% if true %> foo<% else %>bar<% end %>")
|
||||
end
|
||||
end
|
||||
|
||||
test "when end expression is found without a start expression" do
|
||||
assert_raise EEx.SyntaxError, "nofile:1:5: unexpected end of expression <% end %>", fn ->
|
||||
assert_raise EEx.SyntaxError, "nofile:1: unexpected end of expression <% end %>", fn ->
|
||||
EEx.compile_string("foo <% end %>")
|
||||
end
|
||||
end
|
||||
|
||||
test "when start expression is found without an end expression" do
|
||||
msg = "nofile:2:18: unexpected end of string, expected a closing '<% end %>'"
|
||||
msg = "nofile:2: unexpected end of string, expected a closing '<% end %>'"
|
||||
|
||||
assert_raise EEx.SyntaxError, msg, fn ->
|
||||
EEx.compile_string("foo\n<%= if true do %>")
|
||||
EEx.compile_string("foo\n<% if true do %>")
|
||||
end
|
||||
end
|
||||
|
||||
test "when nested end expression is found without a start expression" do
|
||||
assert_raise EEx.SyntaxError, "nofile:1:31: unexpected end of expression <% end %>", fn ->
|
||||
EEx.compile_string("foo <%= if true do %><% end %><% end %>")
|
||||
assert_raise EEx.SyntaxError, "nofile:1: unexpected end of expression <% end %>", fn ->
|
||||
EEx.compile_string("foo <% if true do %><% end %><% end %>")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -330,20 +217,6 @@ defmodule EExTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "error messages" do
|
||||
test "honor line numbers" do
|
||||
assert_raise EEx.SyntaxError, "nofile:99:12: missing token '%>'", fn ->
|
||||
EEx.compile_string("foo <%= bar", line: 99)
|
||||
end
|
||||
end
|
||||
|
||||
test "honor file names" do
|
||||
assert_raise EEx.SyntaxError, "my_file.eex:1:12: missing token '%>'", fn ->
|
||||
EEx.compile_string("foo <%= bar", file: "my_file.eex")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "environment" do
|
||||
test "respects line numbers" do
|
||||
expected = """
|
||||
@@ -469,52 +342,6 @@ defmodule EExTest do
|
||||
assert_eval(expected, string)
|
||||
end
|
||||
|
||||
test "inside multiple functions" do
|
||||
expected = """
|
||||
|
||||
A 1
|
||||
|
||||
B 2
|
||||
|
||||
A 3
|
||||
|
||||
"""
|
||||
|
||||
string = """
|
||||
<%= #{__MODULE__}.switching_map [1, 2, 3], fn x -> %>
|
||||
A <%= x %>
|
||||
<% end, fn x -> %>
|
||||
B <%= x %>
|
||||
<% end %>
|
||||
"""
|
||||
|
||||
assert_eval(expected, string)
|
||||
end
|
||||
|
||||
test "inside callback and do block" do
|
||||
expected = """
|
||||
|
||||
|
||||
A 1
|
||||
|
||||
B 2
|
||||
|
||||
A 3
|
||||
|
||||
"""
|
||||
|
||||
string = """
|
||||
<% require #{__MODULE__} %>
|
||||
<%= #{__MODULE__}.switching_macro [1, 2, 3], fn x -> %>
|
||||
A <%= x %>
|
||||
<% end do %>
|
||||
B <%= x %>
|
||||
<% end %>
|
||||
"""
|
||||
|
||||
assert_eval(expected, string)
|
||||
end
|
||||
|
||||
test "inside cond" do
|
||||
expected = """
|
||||
foo
|
||||
@@ -542,51 +369,28 @@ defmodule EExTest do
|
||||
<% "a" in y -> %>
|
||||
Good
|
||||
<% true -> %>
|
||||
<%= if true do %>true<% else %>false<% end %>
|
||||
<% if true do %>true<% else %>false<% end %>
|
||||
Bad
|
||||
<% end %>
|
||||
"""
|
||||
|
||||
assert_eval("\n\n Good\n \n", string)
|
||||
end
|
||||
|
||||
test "line and column meta" do
|
||||
parser_options = Code.get_compiler_option(:parser_options)
|
||||
Code.put_compiler_option(:parser_options, columns: true)
|
||||
|
||||
try do
|
||||
indentation = 12
|
||||
|
||||
ast =
|
||||
EEx.compile_string(
|
||||
"""
|
||||
<%= f() %> <% f() %>
|
||||
<%= f fn -> %>
|
||||
<%= f() %>
|
||||
<% end %>
|
||||
""",
|
||||
indentation: indentation
|
||||
)
|
||||
|
||||
{_, calls} =
|
||||
Macro.prewalk(ast, [], fn
|
||||
{:f, meta, _args} = expr, acc -> {expr, [meta | acc]}
|
||||
other, acc -> {other, acc}
|
||||
end)
|
||||
|
||||
assert Enum.reverse(calls) == [
|
||||
[line: 1, column: indentation + 5],
|
||||
[line: 1, column: indentation + 15],
|
||||
[line: 2, column: indentation + 7],
|
||||
[line: 3, column: indentation + 9]
|
||||
]
|
||||
after
|
||||
Code.put_compiler_option(:parser_options, parser_options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "buffers" do
|
||||
test "unused buffers are kept out" do
|
||||
string = """
|
||||
<%= 123 %>
|
||||
<% if true do %>
|
||||
<%= 456 %>
|
||||
<% end %>
|
||||
<%= 789 %>
|
||||
"""
|
||||
|
||||
assert_eval("123\n\n789\n", string)
|
||||
end
|
||||
|
||||
test "inside comprehensions" do
|
||||
string = """
|
||||
<%= for _name <- packages || [] do %>
|
||||
@@ -624,24 +428,6 @@ defmodule EExTest do
|
||||
assert EExTest.Compiled.__info__(:attributes)[:external_resource] ==
|
||||
[Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex")]
|
||||
end
|
||||
|
||||
test "supports t:Path.t() paths" do
|
||||
filename = to_charlist(Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex"))
|
||||
result = EEx.eval_file(filename, bar: 1)
|
||||
assert_normalized_newline_equal("foo 1\n", result)
|
||||
end
|
||||
|
||||
assert_raise EEx.SyntaxError, "my_file.eex:1:12: missing token '%>'", fn ->
|
||||
EEx.compile_string("foo <%= bar", file: "my_file.eex")
|
||||
end
|
||||
|
||||
test "supports overriding file and line through options" do
|
||||
filename = Path.join(__DIR__, "fixtures/eex_template_with_syntax_error.eex")
|
||||
|
||||
assert_raise EEx.SyntaxError, "my_file.eex:11:1: missing token '%>'", fn ->
|
||||
EEx.eval_file(filename, _bindings = [], file: "my_file.eex", line: 10)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "precompiled" do
|
||||
@@ -687,8 +473,8 @@ defmodule EExTest do
|
||||
buffer <> ":END"
|
||||
end
|
||||
|
||||
def handle_text(buffer, meta, text) do
|
||||
buffer <> ":TEXT-#{meta[:line]}-#{meta[:column]}(#{String.trim(text)})"
|
||||
def handle_text(buffer, text) do
|
||||
buffer <> ":TEXT(#{String.trim(text)})"
|
||||
end
|
||||
|
||||
def handle_expr(buffer, "/", expr) do
|
||||
@@ -706,16 +492,16 @@ defmodule EExTest do
|
||||
|
||||
describe "custom engines" do
|
||||
test "text" do
|
||||
assert_eval("BODY(INIT:TEXT-1-1(foo))", "foo", [], engine: TestEngine)
|
||||
assert_eval("BODY(INIT:TEXT(foo))", "foo", [], engine: TestEngine)
|
||||
end
|
||||
|
||||
test "custom marker" do
|
||||
assert_eval("BODY(INIT:TEXT-1-1(foo):DIV(:bar))", "foo <%/ :bar %>", [], engine: TestEngine)
|
||||
assert_eval("BODY(INIT:TEXT(foo):DIV(:bar))", "foo <%/ :bar %>", [], engine: TestEngine)
|
||||
end
|
||||
|
||||
test "begin/end" do
|
||||
assert_eval(
|
||||
~s[BODY(INIT:TEXT-1-1(foo):EQUAL(if do\n "BEGIN:TEXT-1-17(this):END"\nelse\n "BEGIN:TEXT-1-31(that):END"\nend))],
|
||||
~s[BODY(INIT:TEXT(foo):EQUAL(if do\n "BEGIN:TEXT(this):END"\nelse\n "BEGIN:TEXT(that):END"\nend))],
|
||||
"foo <%= if do %>this<% else %>that<% end %>",
|
||||
[],
|
||||
engine: TestEngine
|
||||
@@ -732,16 +518,6 @@ defmodule EExTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "parser options" do
|
||||
test "customizes parsed code" do
|
||||
atoms_encoder = fn "not_jose", _ -> {:ok, :jose} end
|
||||
|
||||
assert_eval("valid", "<%= not_jose %>", [jose: "valid"],
|
||||
parser_options: [static_atoms_encoder: atoms_encoder]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp assert_eval(expected, actual, binding \\ [], opts \\ []) do
|
||||
opts = Keyword.merge([file: __ENV__.file, engine: opts[:engine] || EEx.Engine], opts)
|
||||
result = EEx.eval_string(actual, binding, opts)
|
||||
@@ -751,27 +527,4 @@ defmodule EExTest do
|
||||
defp assert_normalized_newline_equal(expected, actual) do
|
||||
assert String.replace(expected, "\r\n", "\n") == String.replace(actual, "\r\n", "\n")
|
||||
end
|
||||
|
||||
def tuple_map({list, callback}) do
|
||||
Enum.map(list, callback)
|
||||
end
|
||||
|
||||
def switching_map(list, a, b) do
|
||||
list
|
||||
|> Enum.with_index()
|
||||
|> Enum.map(fn
|
||||
{element, index} when rem(index, 2) == 0 -> a.(element)
|
||||
{element, index} when rem(index, 2) == 1 -> b.(element)
|
||||
end)
|
||||
end
|
||||
|
||||
defmacro switching_macro(list, a, do: block) do
|
||||
quote do
|
||||
b = fn var!(x) ->
|
||||
unquote(block)
|
||||
end
|
||||
|
||||
unquote(__MODULE__).switching_map(unquote(list), unquote(a), b)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
foo <%= bar
|
||||
@@ -11,6 +11,7 @@
|
||||
warn_exported_vars,
|
||||
%% warn_missing_spec,
|
||||
%% warn_untyped_record,
|
||||
warnings_as_errors,
|
||||
debug_info,
|
||||
{outdir, "ebin/"}
|
||||
]}.
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
defmodule Diff do
|
||||
@moduledoc """
|
||||
Utilities for comparing build artifacts.
|
||||
"""
|
||||
|
||||
@known_chunks ~w(
|
||||
abstract_code
|
||||
debug_info
|
||||
attributes
|
||||
compile_info
|
||||
exports
|
||||
labeled_exports
|
||||
imports
|
||||
indexed_imports
|
||||
locals
|
||||
labeled_locals
|
||||
atoms
|
||||
)a
|
||||
|
||||
@doc """
|
||||
Compares the build artifacts of two build directories.
|
||||
"""
|
||||
@spec compare_dirs(Path.t(), Path.t()) ::
|
||||
{
|
||||
only1_paths :: list(Path.t()),
|
||||
only2_paths :: list(Path.t()),
|
||||
diff :: list({Path.t(), diff :: String.t()})
|
||||
}
|
||||
def compare_dirs(dir1, dir2) do
|
||||
dir1 = Path.expand(dir1)
|
||||
dir2 = Path.expand(dir2)
|
||||
|
||||
assert_dir!(dir1)
|
||||
assert_dir!(dir2)
|
||||
|
||||
dir1_paths = relative_paths(dir1)
|
||||
dir2_paths = relative_paths(dir2)
|
||||
|
||||
only1_paths = dir1_paths -- dir2_paths
|
||||
only2_paths = dir2_paths -- dir1_paths
|
||||
common_paths = dir1_paths -- only1_paths
|
||||
common_files = Enum.reject(common_paths, &File.dir?/1)
|
||||
|
||||
diff =
|
||||
Enum.flat_map(common_files, fn path ->
|
||||
file1 = Path.join(dir1, path)
|
||||
file2 = Path.join(dir2, path)
|
||||
|
||||
case compare_files(file1, file2) do
|
||||
:eq -> []
|
||||
{:diff, diff} -> [{path, diff}]
|
||||
end
|
||||
end)
|
||||
|
||||
{only1_paths, only2_paths, diff}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compares the contents of two files.
|
||||
|
||||
If the files are BEAM files, it performs a more human-friendly
|
||||
"BEAM-diff".
|
||||
"""
|
||||
@spec compare_files(Path.t(), Path.t()) :: :eq | {:diff, diff :: String.t()}
|
||||
def compare_files(file1, file2) do
|
||||
content1 = File.read!(file1)
|
||||
content2 = File.read!(file2)
|
||||
|
||||
if content1 == content2 do
|
||||
:eq
|
||||
else
|
||||
diff =
|
||||
if String.ends_with?(file1, ".beam") do
|
||||
beam_diff(file1, content1, file2, content2)
|
||||
else
|
||||
file_diff(file1, file2)
|
||||
end
|
||||
|
||||
{:diff, diff}
|
||||
end
|
||||
end
|
||||
|
||||
defp beam_diff(file1, content1, file2, content2) do
|
||||
with {:ok, {module, chunks1}} <- :beam_lib.chunks(content1, @known_chunks),
|
||||
{:ok, {^module, chunks2}} <- :beam_lib.chunks(content2, @known_chunks),
|
||||
true <- chunks1 != chunks2 do
|
||||
for {chunk1, chunk2} <- Enum.zip(chunks1, chunks2), chunk1 != chunk2 do
|
||||
tmp_file1 =
|
||||
chunk1
|
||||
|> inspect(pretty: true, limit: :infinity)
|
||||
|> write_tmp()
|
||||
|
||||
tmp_file2 =
|
||||
chunk2
|
||||
|> inspect(pretty: true, limit: :infinity)
|
||||
|> write_tmp()
|
||||
|
||||
file_diff(tmp_file1, tmp_file2)
|
||||
end
|
||||
else
|
||||
_ ->
|
||||
file_diff(file1, file2)
|
||||
end
|
||||
end
|
||||
|
||||
defp file_diff(file1, file2) do
|
||||
{diff, _} = System.cmd("diff", [file1, file2])
|
||||
diff
|
||||
end
|
||||
|
||||
defp relative_paths(dir) do
|
||||
dir
|
||||
|> Path.join("**")
|
||||
|> Path.wildcard()
|
||||
|> Enum.map(&Path.relative_to(&1, dir))
|
||||
end
|
||||
|
||||
defp assert_dir!(dir) do
|
||||
unless File.dir?(dir) do
|
||||
raise ArgumentError, "#{inspect(dir)} is not a directory"
|
||||
end
|
||||
end
|
||||
|
||||
defp write_tmp(content) do
|
||||
filename = generate_tmp_filename()
|
||||
File.mkdir_p!("tmp")
|
||||
File.write!(Path.join("tmp", filename), content)
|
||||
Path.join("tmp", filename)
|
||||
end
|
||||
|
||||
defp generate_tmp_filename do
|
||||
sec = :os.system_time(:second)
|
||||
rand = :rand.uniform(999_999_999)
|
||||
scheduler_id = :erlang.system_info(:scheduler_id)
|
||||
"tmp-#{sec}-#{rand}-#{scheduler_id}"
|
||||
end
|
||||
end
|
||||
|
||||
case System.argv() do
|
||||
[dir1, dir2] ->
|
||||
case Diff.compare_dirs(dir1, dir2) do
|
||||
{[], [], []} ->
|
||||
IO.puts("#{inspect(dir1)} and #{inspect(dir2)} are equal")
|
||||
|
||||
{only1, only2, diff} ->
|
||||
for path <- only1, do: IO.puts("Only in #{dir1}: #{path}")
|
||||
for path <- only2, do: IO.puts("Only in #{dir2}: #{path}")
|
||||
for {path, diff} <- diff, do: IO.puts("Diff #{path}:\n#{diff}")
|
||||
|
||||
System.halt(1)
|
||||
end
|
||||
|
||||
_ ->
|
||||
IO.puts("Please, provide two directories as arguments")
|
||||
System.halt(1)
|
||||
end
|
||||
+20
-38
@@ -1,20 +1,10 @@
|
||||
# Returns config for Elixir docs
|
||||
|
||||
canonical = System.fetch_env!("CANONICAL")
|
||||
|
||||
[
|
||||
extras: Path.wildcard("lib/elixir/pages/*.md") ++ ["CHANGELOG.md"],
|
||||
deps: [
|
||||
eex: "https://hexdocs.pm/eex/#{canonical}",
|
||||
ex_unit: "https://hexdocs.pm/ex_unit/#{canonical}",
|
||||
iex: "https://hexdocs.pm/iex/#{canonical}",
|
||||
logger: "https://hexdocs.pm/logger/#{canonical}",
|
||||
mix: "https://hexdocs.pm/mix/#{canonical}"
|
||||
],
|
||||
extras: Path.wildcard("lib/elixir/pages/*.md"),
|
||||
groups_for_functions: [
|
||||
Guards: &(&1[:guard] == true)
|
||||
Guards: & &1[:guard] == true
|
||||
],
|
||||
skip_undefined_reference_warnings_on: ["lib/elixir/pages/compatibility-and-deprecations.md"],
|
||||
skip_undefined_reference_warnings_on: ["compatibility-and-deprecations"],
|
||||
groups_for_modules: [
|
||||
# [Kernel, Kernel.SpecialForms],
|
||||
|
||||
@@ -28,7 +18,6 @@ canonical = System.fetch_env!("CANONICAL")
|
||||
Float,
|
||||
Function,
|
||||
Integer,
|
||||
Module,
|
||||
NaiveDateTime,
|
||||
Record,
|
||||
Regex,
|
||||
@@ -36,8 +25,7 @@ canonical = System.fetch_env!("CANONICAL")
|
||||
Time,
|
||||
Tuple,
|
||||
URI,
|
||||
Version,
|
||||
Version.Requirement
|
||||
Version
|
||||
],
|
||||
"Collections & Enumerables": [
|
||||
Access,
|
||||
@@ -63,18 +51,22 @@ canonical = System.fetch_env!("CANONICAL")
|
||||
StringIO,
|
||||
System
|
||||
],
|
||||
Calendar: [
|
||||
"Calendar": [
|
||||
Calendar,
|
||||
Calendar.ISO,
|
||||
Calendar.TimeZoneDatabase,
|
||||
Calendar.UTCOnlyTimeZoneDatabase
|
||||
],
|
||||
"Modules & Code": [
|
||||
Code,
|
||||
Kernel.ParallelCompiler,
|
||||
Macro,
|
||||
Macro.Env,
|
||||
Module
|
||||
],
|
||||
"Processes & Applications": [
|
||||
Agent,
|
||||
Application,
|
||||
Config,
|
||||
Config.Provider,
|
||||
Config.Reader,
|
||||
DynamicSupervisor,
|
||||
GenServer,
|
||||
Node,
|
||||
@@ -94,24 +86,14 @@ canonical = System.fetch_env!("CANONICAL")
|
||||
Protocol,
|
||||
String.Chars
|
||||
],
|
||||
"Code & Macros": [
|
||||
Code,
|
||||
Code.Fragment,
|
||||
Kernel.ParallelCompiler,
|
||||
Macro,
|
||||
Macro.Env
|
||||
Deprecated: [
|
||||
Behaviour,
|
||||
Dict,
|
||||
GenEvent,
|
||||
HashDict,
|
||||
HashSet,
|
||||
Set,
|
||||
Supervisor.Spec
|
||||
]
|
||||
|
||||
## Automatically detected groups
|
||||
|
||||
# Deprecated: [
|
||||
# Behaviour,
|
||||
# Dict,
|
||||
# GenEvent,
|
||||
# HashDict,
|
||||
# HashSet,
|
||||
# Set,
|
||||
# Supervisor.Spec
|
||||
# ]
|
||||
]
|
||||
]
|
||||
|
||||
@@ -10,4 +10,4 @@ main([Source, Target, Version]) ->
|
||||
Props = lists:keyreplace(vsn, 1, Props1, {vsn, Version}),
|
||||
AppDef = io_lib:format("~tp.~n", [{application, Name, Props}]),
|
||||
ok = file:write_file(Target, AppDef),
|
||||
io:format("Generated ~ts app~n", [Name]).
|
||||
io:format("Generated ~ts.app~n", [Name]).
|
||||
|
||||
+134
-147
@@ -2,12 +2,46 @@ defmodule Access do
|
||||
@moduledoc """
|
||||
Key-based access to data structures.
|
||||
|
||||
The `Access` module defines a behaviour for dynamically accessing
|
||||
keys of any type in a data structure via the `data[key]` syntax.
|
||||
Elixir supports three main key-value constructs: keywords,
|
||||
maps, and structs. It also supports two mechanisms to access those keys:
|
||||
by brackets (via `data[key]`) and by dot-syntax (via `data.field`).
|
||||
|
||||
`Access` supports keyword lists (`Keyword`) and maps (`Map`) out
|
||||
of the box. Keywords supports only atoms keys, keys for maps can
|
||||
be of any type. Both return `nil` if the key does not exist:
|
||||
In the next section we will briefly recap the key-value constructs and then
|
||||
discuss the access mechanisms.
|
||||
|
||||
## Key-value constructs
|
||||
|
||||
Elixir provides three main key-value constructs, summarized below:
|
||||
|
||||
* keyword lists - they are lists of two-element tuples where
|
||||
the first element is an atom. Commonly written in the
|
||||
`[key: value]` syntax, they support only atom keys. Keyword
|
||||
lists are used almost exclusively to pass options to functions
|
||||
and macros. They keep the user ordering and allow duplicate
|
||||
keys. See the `Keyword` module.
|
||||
|
||||
* maps - they are the "go to" key-value data structure in Elixir.
|
||||
They are capable of supporting billions of keys of any type. They are
|
||||
written using the `%{key => value}` syntax and also support the
|
||||
`%{key: value}` syntax when the keys are atoms. They do not
|
||||
have any specified ordering and do not allow duplicate keys.
|
||||
See the `Map` module.
|
||||
|
||||
* structs - they are named maps with a pre-determined set of keys.
|
||||
They are defined with `defstruct/1` and written using the
|
||||
`%StructName{key: value}` syntax.
|
||||
|
||||
## Key-based accessors
|
||||
|
||||
Elixir provides two mechanisms to access data structures by key,
|
||||
described next.
|
||||
|
||||
### Bracket-based access
|
||||
|
||||
The `data[key]` syntax is used to access data structures with a
|
||||
dynamic number of keys, such as keywords and maps. The key can
|
||||
be of any type. The bracket-based access syntax returns `nil`
|
||||
if the key does not exist:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:a]
|
||||
@@ -25,40 +59,62 @@ defmodule Access do
|
||||
|
||||
This syntax is very convenient as it can be nested arbitrarily:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:c][:unknown]
|
||||
nil
|
||||
|
||||
This works because accessing anything on a `nil` value, returns
|
||||
`nil` itself:
|
||||
|
||||
iex> nil[:a]
|
||||
nil
|
||||
|
||||
The access syntax can also be used with the `Kernel.put_in/2`,
|
||||
`Kernel.update_in/2` and `Kernel.get_and_update_in/2` macros
|
||||
to allow values to be set in nested data structures:
|
||||
|
||||
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
|
||||
iex> put_in(users["john"][:age], 28)
|
||||
%{"john" => %{age: 28}, "meg" => %{age: 23}}
|
||||
|
||||
> Attention! While the access syntax is allowed in maps via
|
||||
> `map[key]`, if your map is made of predefined atom keys,
|
||||
> you should prefer to access those atom keys with `map.key`
|
||||
> instead of `map[key]`, as `map.key` will raise if the key
|
||||
> is missing (which is not supposed to happen if the keys are
|
||||
> predefined). Similarly, since structs are maps and structs
|
||||
> have predefined keys, they only allow the `struct.key`
|
||||
> syntax and they do not allow the `struct[key]` access syntax.
|
||||
> See the `Map` module for more information.
|
||||
Furthermore, the bracket-based access syntax transparently ignores
|
||||
`nil` values. When trying to access anything on a `nil` value, `nil`
|
||||
is returned:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:c][:unknown]
|
||||
nil
|
||||
|
||||
iex> nil[:a]
|
||||
nil
|
||||
|
||||
Internally, `data[key]` translates to `Access.get(term, key, nil)`.
|
||||
Developers interested in implementing their own key-value data
|
||||
structures can implement the `Access` behaviour to provide the
|
||||
bracket-based access syntax. `Access` requires the key comparison
|
||||
to be implemented using the `===/2` operator.
|
||||
|
||||
### Dot-based syntax
|
||||
|
||||
The `data.field` syntax is used exclusively to access atom fields
|
||||
in maps and structs. If the accessed field does not exist, an error is
|
||||
raised. This is a deliberate decision: since all of the
|
||||
fields in a struct are pre-determined, structs support only the
|
||||
dot-based syntax and not the access one.
|
||||
|
||||
Imagine a struct named `User` with a `:name` field. The following would raise:
|
||||
|
||||
user = %User{name: "John"}
|
||||
user[:name]
|
||||
# ** (UndefinedFunctionError) undefined function User.fetch/2 (User does not implement the Access behaviour)
|
||||
|
||||
Instead we should use the `user.name` syntax to access fields:
|
||||
|
||||
user.name
|
||||
#=> "John"
|
||||
|
||||
Differently from `user[:name]`, `user.name` is not extensible via
|
||||
a behaviour and is restricted only to structs and atom keys in maps.
|
||||
|
||||
### Summing up
|
||||
|
||||
The bracket-based syntax, `user[:name]`, is used by dynamic structures,
|
||||
is extensible and returns nil on missing keys.
|
||||
|
||||
The dot-based syntax, `user.name`, is used exclusively to access atom
|
||||
keys in maps and structs, and it raises on missing keys.
|
||||
|
||||
## Nested data structures
|
||||
|
||||
Both key-based access syntaxes can be used with the nested update
|
||||
functions and macros in `Kernel`, such as `Kernel.get_in/2`,
|
||||
`Kernel.put_in/3`, `Kernel.update_in/3`, `Kernel.pop_in/2`, and
|
||||
`Kernel.get_and_update_in/3`.
|
||||
functions and macros in `Kernel`, such as `Kernel.get_in/2`, `Kernel.put_in/3`,
|
||||
`Kernel.update_in/3`, `Kernel.pop_in/2`, and `Kernel.get_and_update_in/3`.
|
||||
|
||||
For example, to update a map inside another map:
|
||||
|
||||
@@ -70,9 +126,9 @@ defmodule Access do
|
||||
structures, like tuples and lists. These functions can be used
|
||||
in all the `Access`-related functions and macros in `Kernel`.
|
||||
|
||||
For instance, given a user map with the `:name` and `:languages`
|
||||
keys, here is how to deeply traverse the map and convert all
|
||||
language names to uppercase:
|
||||
For instance, given a user map with the `:name` and `:languages` keys,
|
||||
here is how to deeply traverse the map and convert all language names
|
||||
to uppercase:
|
||||
|
||||
iex> languages = [
|
||||
...> %{name: "elixir", type: :functional},
|
||||
@@ -88,8 +144,8 @@ defmodule Access do
|
||||
]
|
||||
}
|
||||
|
||||
See the functions `key/1`, `key!/1`, `elem/1`, and `all/0` for
|
||||
some of the available accessors.
|
||||
See the functions `key/1`, `key!/1`, `elem/1`, and `all/0` for some of the
|
||||
available accessors.
|
||||
"""
|
||||
|
||||
@type container :: keyword | struct | map
|
||||
@@ -99,15 +155,16 @@ defmodule Access do
|
||||
@type key :: any
|
||||
@type value :: any
|
||||
|
||||
@type get_fun(data) ::
|
||||
(:get, data, (term -> term) -> new_data :: container)
|
||||
@type get_fun(data, get_value) ::
|
||||
(:get, data, (term -> term) ->
|
||||
{get_value, new_data :: container})
|
||||
|
||||
@type get_and_update_fun(data, current_value) ::
|
||||
@type get_and_update_fun(data, get_value) ::
|
||||
(:get_and_update, data, (term -> term) ->
|
||||
{current_value, new_data :: container} | :pop)
|
||||
{get_value, new_data :: container} | :pop)
|
||||
|
||||
@type access_fun(data, current_value) ::
|
||||
get_fun(data) | get_and_update_fun(data, current_value)
|
||||
@type access_fun(data, get_value) ::
|
||||
get_fun(data, get_value) | get_and_update_fun(data, get_value)
|
||||
|
||||
@doc """
|
||||
Invoked in order to access the value stored under `key` in the given term `term`.
|
||||
@@ -133,16 +190,16 @@ defmodule Access do
|
||||
|
||||
The implementation of this callback should invoke `fun` with the value under
|
||||
`key` in the passed structure `data`, or with `nil` if `key` is not present in it.
|
||||
This function must return either `{current_value, new_value}` or `:pop`.
|
||||
This function must return either `{get_value, update_value}` or `:pop`.
|
||||
|
||||
If the passed function returns `{current_value, new_value}`,
|
||||
the return value of this callback should be `{current_value, new_data}`, where:
|
||||
If the passed function returns `{get_value, update_value}`,
|
||||
the return value of this callback should be `{get_value, new_data}`, where:
|
||||
|
||||
* `current_value` is the retrieved value (which can be operated on before being returned)
|
||||
* `get_value` is the retrieved value (which can be operated on before being returned)
|
||||
|
||||
* `new_value` is the new value to be stored under `key`
|
||||
* `update_value` is the new value to be stored under `key`
|
||||
|
||||
* `new_data` is `data` after updating the value of `key` with `new_value`.
|
||||
* `new_data` is `data` after updating the value of `key` with `update_value`.
|
||||
|
||||
If the passed function returns `:pop`, the return value of this callback
|
||||
must be `{value, new_data}` where `value` is the value under `key`
|
||||
@@ -151,9 +208,8 @@ defmodule Access do
|
||||
See the implementations of `Map.get_and_update/3` or `Keyword.get_and_update/3`
|
||||
for more examples.
|
||||
"""
|
||||
@callback get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, new_data :: data}
|
||||
when current_value: value, data: container | any_container
|
||||
@callback get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
|
||||
when get_value: var, data: container | any_container
|
||||
|
||||
@doc """
|
||||
Invoked to "pop" the value under `key` out of the given data structure.
|
||||
@@ -235,25 +291,6 @@ defmodule Access do
|
||||
:error
|
||||
end
|
||||
|
||||
@doc """
|
||||
Same as `fetch/2` but returns the value directly,
|
||||
or raises a `KeyError` exception if `key` is not found.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Access.fetch!(%{name: "meg", age: 26}, :name)
|
||||
"meg"
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec fetch!(container, term) :: term
|
||||
def fetch!(container, key) do
|
||||
case fetch(container, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> raise(KeyError, key: key, term: container)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value for the given key in a container (a map, keyword
|
||||
list, or struct that implements the `Access` behaviour).
|
||||
@@ -320,26 +357,17 @@ defmodule Access do
|
||||
a struct that implements the `Access` behaviour).
|
||||
|
||||
The `fun` argument receives the value of `key` (or `nil` if `key` is not
|
||||
present in `container`) and must return a two-element tuple `{current_value, new_value}`:
|
||||
the "get" value `current_value` (the retrieved value, which can be operated on before
|
||||
being returned) and the new value to be stored under `key` (`new_value`).
|
||||
present in `container`) and must return a two-element tuple `{get_value, update_value}`:
|
||||
the "get" value `get_value` (the retrieved value, which can be operated on before
|
||||
being returned) and the new value to be stored under `key` (`update_value`).
|
||||
`fun` may also return `:pop`, which means the current value
|
||||
should be removed from the container and returned.
|
||||
|
||||
The returned value is a two-element tuple with the "get" value returned by
|
||||
`fun` and a new container with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Access.get_and_update([a: 1], :a, fn current_value ->
|
||||
...> {current_value, current_value + 1}
|
||||
...> end)
|
||||
{1, [a: 2]}
|
||||
|
||||
"""
|
||||
@spec get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, new_data :: data}
|
||||
when current_value: var, data: container
|
||||
@spec get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
|
||||
when get_value: var, data: container
|
||||
def get_and_update(container, key, fun)
|
||||
|
||||
def get_and_update(%module{} = container, key, fun) do
|
||||
@@ -422,7 +450,7 @@ defmodule Access do
|
||||
The returned function uses the default value if the key does not exist.
|
||||
This can be used to specify defaults and safely traverse missing keys:
|
||||
|
||||
iex> get_in(%{}, [Access.key(:user, %{}), Access.key(:name, "meg")])
|
||||
iex> get_in(%{}, [Access.key(:user, %{name: "meg"}), Access.key(:name)])
|
||||
"meg"
|
||||
|
||||
Such is also useful when using update functions, allowing us to introduce
|
||||
@@ -445,11 +473,14 @@ defmodule Access do
|
||||
|
||||
An error is raised if the accessed structure is not a map or a struct:
|
||||
|
||||
iex> get_in(nil, [Access.key(:foo)])
|
||||
** (BadMapError) expected a map, got: nil
|
||||
|
||||
iex> get_in([], [Access.key(:foo)])
|
||||
** (BadMapError) expected a map, got: []
|
||||
|
||||
"""
|
||||
@spec key(key, term) :: access_fun(data :: struct | map, current_value :: term)
|
||||
@spec key(key, term) :: access_fun(data :: struct | map, get_value :: term)
|
||||
def key(key, default \\ nil) do
|
||||
fn
|
||||
:get, data, next ->
|
||||
@@ -493,7 +524,7 @@ defmodule Access do
|
||||
** (RuntimeError) Access.key!/1 expected a map/struct, got: []
|
||||
|
||||
"""
|
||||
@spec key!(key) :: access_fun(data :: struct | map, current_value :: term)
|
||||
@spec key!(key) :: access_fun(data :: struct | map, get_value :: term)
|
||||
def key!(key) do
|
||||
fn
|
||||
:get, %{} = data, next ->
|
||||
@@ -541,7 +572,7 @@ defmodule Access do
|
||||
** (RuntimeError) Access.elem/1 expected a tuple, got: %{}
|
||||
|
||||
"""
|
||||
@spec elem(non_neg_integer) :: access_fun(data :: tuple, current_value :: term)
|
||||
@spec elem(non_neg_integer) :: access_fun(data :: tuple, get_value :: term)
|
||||
def elem(index) when is_integer(index) and index >= 0 do
|
||||
pos = index + 1
|
||||
|
||||
@@ -595,7 +626,7 @@ defmodule Access do
|
||||
** (RuntimeError) Access.all/0 expected a list, got: %{}
|
||||
|
||||
"""
|
||||
@spec all() :: access_fun(data :: list, current_value :: list)
|
||||
@spec all() :: access_fun(data :: list, get_value :: list)
|
||||
def all() do
|
||||
&all/3
|
||||
end
|
||||
@@ -634,16 +665,10 @@ defmodule Access do
|
||||
iex> list = [%{name: "john"}, %{name: "mary"}]
|
||||
iex> get_in(list, [Access.at(1), :name])
|
||||
"mary"
|
||||
iex> get_in(list, [Access.at(-1), :name])
|
||||
"mary"
|
||||
iex> get_and_update_in(list, [Access.at(0), :name], fn prev ->
|
||||
...> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{"john", [%{name: "JOHN"}, %{name: "mary"}]}
|
||||
iex> get_and_update_in(list, [Access.at(-1), :name], fn prev ->
|
||||
...> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{"mary", [%{name: "john"}, %{name: "MARY"}]}
|
||||
|
||||
`at/1` can also be used to pop elements out of a list or
|
||||
a key inside of a list:
|
||||
@@ -664,14 +689,19 @@ defmodule Access do
|
||||
...> end)
|
||||
{nil, [%{name: "john"}, %{name: "mary"}]}
|
||||
|
||||
An error is raised for negative indexes:
|
||||
|
||||
iex> get_in([], [Access.at(-1)])
|
||||
** (FunctionClauseError) no function clause matching in Access.at/1
|
||||
|
||||
An error is raised if the accessed structure is not a list:
|
||||
|
||||
iex> get_in(%{}, [Access.at(1)])
|
||||
** (RuntimeError) Access.at/1 expected a list, got: %{}
|
||||
|
||||
"""
|
||||
@spec at(integer) :: access_fun(data :: list, current_value :: term)
|
||||
def at(index) when is_integer(index) do
|
||||
@spec at(non_neg_integer) :: access_fun(data :: list, get_value :: term)
|
||||
def at(index) when is_integer(index) and index >= 0 do
|
||||
fn op, data, next -> at(op, data, index, next) end
|
||||
end
|
||||
|
||||
@@ -680,69 +710,26 @@ defmodule Access do
|
||||
end
|
||||
|
||||
defp at(:get_and_update, data, index, next) when is_list(data) do
|
||||
get_and_update_at(data, index, next, [], fn -> nil end)
|
||||
get_and_update_at(data, index, next, [])
|
||||
end
|
||||
|
||||
defp at(_op, data, _index, _next) do
|
||||
raise "Access.at/1 expected a list, got: #{inspect(data)}"
|
||||
end
|
||||
|
||||
defp get_and_update_at([head | rest], 0, next, updates, _default_fun) do
|
||||
defp get_and_update_at([head | rest], 0, next, updates) do
|
||||
case next.(head) do
|
||||
{get, update} -> {get, :lists.reverse([update | updates], rest)}
|
||||
:pop -> {head, :lists.reverse(updates, rest)}
|
||||
end
|
||||
end
|
||||
|
||||
defp get_and_update_at([_ | _] = list, index, next, updates, default_fun) when index < 0 do
|
||||
list_length = length(list)
|
||||
|
||||
if list_length + index >= 0 do
|
||||
get_and_update_at(list, list_length + index, next, updates, default_fun)
|
||||
else
|
||||
{default_fun.(), list}
|
||||
end
|
||||
defp get_and_update_at([head | rest], index, next, updates) do
|
||||
get_and_update_at(rest, index - 1, next, [head | updates])
|
||||
end
|
||||
|
||||
defp get_and_update_at([head | rest], index, next, updates, default_fun) when index > 0 do
|
||||
get_and_update_at(rest, index - 1, next, [head | updates], default_fun)
|
||||
end
|
||||
|
||||
defp get_and_update_at([], _index, _next, updates, default_fun) do
|
||||
{default_fun.(), :lists.reverse(updates)}
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Same as `at/1` except that it raises `Enum.OutOfBoundsError`
|
||||
if the given index is out of bounds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_in([:a, :b, :c], [Access.at!(2)])
|
||||
:c
|
||||
iex> get_in([:a, :b, :c], [Access.at!(3)])
|
||||
** (Enum.OutOfBoundsError) out of bounds error
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec at!(integer) :: access_fun(data :: list, current_value :: term)
|
||||
def at!(index) when is_integer(index) do
|
||||
fn op, data, next -> at!(op, data, index, next) end
|
||||
end
|
||||
|
||||
defp at!(:get, data, index, next) when is_list(data) do
|
||||
case Enum.fetch(data, index) do
|
||||
{:ok, value} -> next.(value)
|
||||
:error -> raise Enum.OutOfBoundsError
|
||||
end
|
||||
end
|
||||
|
||||
defp at!(:get_and_update, data, index, next) when is_list(data) do
|
||||
get_and_update_at(data, index, next, [], fn -> raise Enum.OutOfBoundsError end)
|
||||
end
|
||||
|
||||
defp at!(_op, data, _index, _next) do
|
||||
raise "Access.at!/1 expected a list, got: #{inspect(data)}"
|
||||
defp get_and_update_at([], _index, _next, updates) do
|
||||
{nil, :lists.reverse(updates)}
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -792,7 +779,7 @@ defmodule Access do
|
||||
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec filter((term -> boolean)) :: access_fun(data :: list, current_value :: list)
|
||||
@spec filter((term -> boolean)) :: access_fun(data :: list, get_value :: list)
|
||||
def filter(func) when is_function(func) do
|
||||
fn op, data, next -> filter(op, data, func, next) end
|
||||
end
|
||||
|
||||
+7
-41
@@ -32,19 +32,11 @@ defmodule Agent do
|
||||
Usage would be:
|
||||
|
||||
Counter.start_link(0)
|
||||
#=> {:ok, #PID<0.123.0>}
|
||||
|
||||
Counter.value()
|
||||
#=> 0
|
||||
|
||||
Counter.increment()
|
||||
#=> :ok
|
||||
|
||||
Counter.increment()
|
||||
#=> :ok
|
||||
|
||||
Counter.value()
|
||||
#=> 2
|
||||
Counter.value #=> 0
|
||||
Counter.increment #=> :ok
|
||||
Counter.increment #=> :ok
|
||||
Counter.value #=> 2
|
||||
|
||||
Thanks to the agent server process, the counter can be safely incremented
|
||||
concurrently.
|
||||
@@ -123,6 +115,7 @@ defmodule Agent do
|
||||
The generated `child_spec/1` can be customized with the following options:
|
||||
|
||||
* `:id` - the child specification identifier, defaults to the current module
|
||||
* `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
|
||||
* `:restart` - when the child should be restarted, defaults to `:permanent`
|
||||
* `:shutdown` - how to shut down the child, either immediately or by giving it time to shut down
|
||||
|
||||
@@ -201,7 +194,7 @@ defmodule Agent do
|
||||
@doc false
|
||||
defmacro __using__(opts) do
|
||||
quote location: :keep, bind_quoted: [opts: opts] do
|
||||
unless Module.has_attribute?(__MODULE__, :doc) do
|
||||
if Module.get_attribute(__MODULE__, :doc) == nil do
|
||||
@doc """
|
||||
Returns a specification to start this module under a supervisor.
|
||||
|
||||
@@ -241,7 +234,7 @@ defmodule Agent do
|
||||
and the start function will return `{:error, :timeout}`.
|
||||
|
||||
If the `:debug` option is present, the corresponding function in the
|
||||
[`:sys` module](`:sys`) will be invoked.
|
||||
[`: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`.
|
||||
@@ -423,15 +416,6 @@ defmodule Agent do
|
||||
Same as `update/3` but a module, function, and arguments are expected
|
||||
instead of an anonymous function. The state is added as first
|
||||
argument to the given list of arguments.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
|
||||
iex> Agent.update(pid, Kernel, :+, [12])
|
||||
:ok
|
||||
iex> Agent.get(pid, fn state -> state end)
|
||||
54
|
||||
|
||||
"""
|
||||
@spec update(agent, module, atom, [term], timeout) :: :ok
|
||||
def update(agent, module, fun, args, timeout \\ 5000) do
|
||||
@@ -447,15 +431,6 @@ defmodule Agent do
|
||||
|
||||
Note that `cast` returns `:ok` immediately, regardless of whether `agent` (or
|
||||
the node it should live on) exists.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
|
||||
iex> Agent.cast(pid, fn state -> state + 1 end)
|
||||
:ok
|
||||
iex> Agent.get(pid, fn state -> state end)
|
||||
43
|
||||
|
||||
"""
|
||||
@spec cast(agent, (state -> state)) :: :ok
|
||||
def cast(agent, fun) when is_function(fun, 1) do
|
||||
@@ -468,15 +443,6 @@ defmodule Agent do
|
||||
Same as `cast/2` but a module, function, and arguments are expected
|
||||
instead of an anonymous function. The state is added as first
|
||||
argument to the given list of arguments.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
|
||||
iex> Agent.cast(pid, Kernel, :+, [12])
|
||||
:ok
|
||||
iex> Agent.get(pid, fn state -> state end)
|
||||
54
|
||||
|
||||
"""
|
||||
@spec cast(agent, module, atom, [term]) :: :ok
|
||||
def cast(agent, module, fun, args) do
|
||||
|
||||
+117
-359
@@ -7,126 +7,79 @@ defmodule Application do
|
||||
programming languages, but with some additional characteristics.
|
||||
|
||||
An application is a component implementing some specific functionality, with a
|
||||
standardized directory structure, configuration, and life cycle. Applications
|
||||
are *loaded*, *started*, and *stopped*. Each application also has its own
|
||||
environment, which provides a unified API for configuring each application.
|
||||
standardized directory structure, configuration, and lifecycle. Applications
|
||||
are *loaded*, *started*, and *stopped*.
|
||||
|
||||
Developers typically interact with the application environment and its
|
||||
callback module. Therefore those will be the topics we will cover first
|
||||
before jumping into details about the application resource file and life-cycle.
|
||||
## The application resource file
|
||||
|
||||
Applications are specified in their [*resource
|
||||
file*](http://erlang.org/doc/man/app.html), which is a file called `APP.app`,
|
||||
where `APP` is the application name. For example, the application resource
|
||||
file of the OTP application `ex_unit` is called `ex_unit.app`.
|
||||
|
||||
You'll find the resource file of an application in its `ebin` directory, it is
|
||||
generated automatically by Mix. Some of its keys are taken from the keyword
|
||||
lists returned by the `project/0` and `application/0` functions defined in
|
||||
`mix.exs`, and others are generated by Mix itself.
|
||||
|
||||
You can learn more about the generation of application resource files in the
|
||||
documentation of `Mix.Tasks.Compile.App`, available as well by running
|
||||
`mix help compile.app`.
|
||||
|
||||
## The application environment
|
||||
|
||||
Each application has its own environment. The environment is a keyword list
|
||||
that maps atoms to terms. Note that this environment is unrelated to the
|
||||
operating system environment.
|
||||
The key `env` of an application resource file has a list of tuples that map
|
||||
atoms to terms, and its contents are known as the application *environment*.
|
||||
Note that this environment is unrelated to the operating system environment.
|
||||
|
||||
By default, the environment of an application is an empty list. In a Mix
|
||||
project's `mix.exs` file, you can set the `:env` key in `application/0`:
|
||||
project you can set that key in `application/0`:
|
||||
|
||||
def application do
|
||||
[env: [db_host: "localhost"]]
|
||||
[env: [redis_host: "localhost"]]
|
||||
end
|
||||
|
||||
Now, in your application, you can read this environment by using functions
|
||||
such as `fetch_env!/2` and friends:
|
||||
and the generated application resource file is going to have it included.
|
||||
|
||||
defmodule MyApp.DBClient do
|
||||
def start_link() do
|
||||
SomeLib.DBClient.start_link(host: db_host())
|
||||
end
|
||||
The environment is available after loading the application, which is a process
|
||||
explained later:
|
||||
|
||||
defp db_host do
|
||||
Application.fetch_env!(:my_app, :db_host)
|
||||
end
|
||||
end
|
||||
Application.load(:APP_NAME)
|
||||
#=> :ok
|
||||
|
||||
Application.get_env(:APP_NAME, :redis_host)
|
||||
#=> "localhost"
|
||||
|
||||
In Mix projects, the environment of the application and its dependencies can
|
||||
be overridden via the `config/config.exs` and `config/runtime.exs` files. The
|
||||
former is loaded at build-time, before your code compiles, and the latter at
|
||||
runtime, just before your app starts. For example, someone using your application
|
||||
can override its `:db_host` environment variable as follows:
|
||||
be overridden via the `config/config.exs` file. If you start the application
|
||||
with Mix, that configuration is available at compile time, and at runtime too,
|
||||
but take into account it is not included in the generated application resource
|
||||
file, and it is not available if you start the application without Mix.
|
||||
|
||||
import Config
|
||||
config :my_app, :db_host, "db.local"
|
||||
For example, someone using your application can override its `:redis_host`
|
||||
environment variable as follows:
|
||||
|
||||
See the "Configuration" section in the `Mix` module for more information.
|
||||
config :APP_NAME, redis_host: "redis.local"
|
||||
|
||||
You can also change the application environment dynamically by using functions
|
||||
such as `put_env/3` and `delete_env/2`. However, as a rule of thumb, each application
|
||||
is responsible for its own environment. Please do not use the functions in this
|
||||
module for directly accessing or modifying the environment of other applications.
|
||||
The function `put_env/3` allows dynamic configuration of the application
|
||||
environment, but as a rule of thumb each application is responsible for its
|
||||
own environment. Please do not use the functions in this module for directly
|
||||
accessing or modifying the environment of other applications.
|
||||
|
||||
### Compile-time environment
|
||||
|
||||
In the previous example, we read the application environment at runtime:
|
||||
|
||||
defmodule MyApp.DBClient do
|
||||
def start_link() do
|
||||
SomeLib.DBClient.start_link(host: db_host())
|
||||
end
|
||||
|
||||
defp db_host do
|
||||
Application.fetch_env!(:my_app, :db_host)
|
||||
end
|
||||
end
|
||||
|
||||
In other words, the environment key `:db_host` for application `:my_app`
|
||||
will only be read when `MyApp.DBClient` effectively starts. While reading
|
||||
the application environment at runtime is the preferred approach, in some
|
||||
rare occasions you may want to use the application environment to configure
|
||||
the compilation of a certain project. This is often done by calling `get_env/3`
|
||||
outside of a function:
|
||||
|
||||
defmodule MyApp.DBClient do
|
||||
@db_host Application.get_env(:my_app, :db_host, "db.local")
|
||||
|
||||
def start_link() do
|
||||
SomeLib.DBClient.start_link(host: @db_host)
|
||||
end
|
||||
end
|
||||
|
||||
This approach has one big limitation: if you change the value of the
|
||||
application environment after the code is compiled, the value used at
|
||||
runtime is not going to change! For example, if your `config/runtime.exs`
|
||||
has:
|
||||
|
||||
config :my_app, :db_host, "db.production"
|
||||
|
||||
This value will have no effect as the code was compiled to connect to "db.local",
|
||||
which is mostly likely unavailable in the production environment.
|
||||
|
||||
For those reasons, reading the application environment at runtime should be the
|
||||
first choice. However, if you really have to read the application environment
|
||||
during compilation, we recommend you to use `compile_env/3` instead:
|
||||
|
||||
require Application
|
||||
@db_host Application.compile_env(:my_app, :db_host, "db.local")
|
||||
|
||||
By using `compile_env/3`, tools like Mix will store the values used during
|
||||
compilation and compare the compilation values with the runtime values whenever
|
||||
your system starts, raising an error in case they differ.
|
||||
The application environment can be overridden via the `-config` option of
|
||||
`erl`, as well as command-line options, as we are going to see below.
|
||||
|
||||
## The application callback module
|
||||
|
||||
Applications can be loaded, started, and stopped. Generally, build tools
|
||||
like Mix take care of starting an application and all of its dependencies
|
||||
for you, but you can also do it manually by calling:
|
||||
|
||||
{:ok, _} = Application.ensure_all_started(:some_app)
|
||||
|
||||
When an application starts, developers may configure a callback module
|
||||
that executes custom code. Developers use this callback to start the
|
||||
application supervision tree.
|
||||
|
||||
The first step to do so is to add a `:mod` key to the `application/0`
|
||||
definition in your `mix.exs` file. It expects a tuple, with the application
|
||||
callback module and start argument (commonly an empty list):
|
||||
The `mod` key of an application resource file configures an application
|
||||
callback module and start argument:
|
||||
|
||||
def application do
|
||||
[mod: {MyApp, []}]
|
||||
end
|
||||
|
||||
This key is optional, only needed for applications that start a supervision tree.
|
||||
|
||||
The `MyApp` module given to `:mod` needs to implement the `Application` behaviour.
|
||||
This can be done by putting `use Application` in that module and implementing the
|
||||
`c:start/2` callback, for example:
|
||||
@@ -163,20 +116,7 @@ defmodule Application do
|
||||
tree is terminated. Its argument is the state returned by `c:start/2`, if it did,
|
||||
or `[]` otherwise, and its return value is passed to `c:stop/1`.
|
||||
|
||||
## The application resource file
|
||||
|
||||
In the sections above, we have configured an application in the
|
||||
`application/0` section of the `mix.exs` file. Ultimately, Mix will use
|
||||
this configuration to create an [*application resource
|
||||
file*](https://erlang.org/doc/man/application.html), which is a file called
|
||||
`APP_NAME.app`. For example, the application resource file of the OTP
|
||||
application `ex_unit` is called `ex_unit.app`.
|
||||
|
||||
You can learn more about the generation of application resource files in
|
||||
the documentation of `Mix.Tasks.Compile.App`, available as well by running
|
||||
`mix help compile.app`.
|
||||
|
||||
## The application life cycle
|
||||
## The application lifecycle
|
||||
|
||||
### Loading applications
|
||||
|
||||
@@ -186,8 +126,16 @@ defmodule Application do
|
||||
Application.load(:ex_unit)
|
||||
#=> :ok
|
||||
|
||||
If an application has included applications, they are also loaded. And the
|
||||
procedure recurses if they in turn have included applications. Included
|
||||
applications are unrelated to applications in Mix umbrella projects, they are
|
||||
an Erlang/OTP concept that has to do with coordinated starts.
|
||||
|
||||
When an application is loaded, the environment specified in its resource file
|
||||
is merged with any overrides from config files.
|
||||
is merged with any overrides from config files passed to `erl` via the
|
||||
`-config` option. It is worth highlighting that releases pass `sys.config`
|
||||
this way. The resulting environment can still be overridden again via specific
|
||||
`-Application` options passed to `erl`.
|
||||
|
||||
Loading an application *does not* load its modules.
|
||||
|
||||
@@ -207,15 +155,17 @@ defmodule Application do
|
||||
system. Instead, you start one or more applications, each with their own
|
||||
initialization and termination logic.
|
||||
|
||||
When an application is started, the `Application.load/1` is automatically
|
||||
invoked if it hasn't been done yet. Then, it checks if the dependencies listed
|
||||
in the `applications` key of the resource file are already started. Having at
|
||||
least one dependency not started is an error condition. Functions like
|
||||
`ensure_all_started/1` takes care of starting an application and all of its
|
||||
dependencies for you.
|
||||
When an application is started, the runtime loads it if it hasn't been loaded
|
||||
yet (in the technical sense described above). Then, it checks if the
|
||||
dependencies listed in the `applications` key of the resource file are already
|
||||
started. Having at least one dependency not started is an error condition, but
|
||||
when you start an application with `mix run`, Mix takes care of starting all
|
||||
the dependencies for you, so in practice you don't need to worry about it
|
||||
unless you are starting applications manually with the API provided by this
|
||||
module.
|
||||
|
||||
If the application does not have a callback module configured, starting is
|
||||
done at this point. Otherwise, its `c:start/2` callback is invoked. The PID of
|
||||
done at this point. Otherwise, its `c:start/2` callback if invoked. The PID of
|
||||
the top-level supervisor returned by this function is stored by the runtime
|
||||
for later use, and the returned application state is saved too, if any.
|
||||
|
||||
@@ -231,9 +181,9 @@ defmodule Application do
|
||||
|
||||
Stopping an application with a callback module has three steps:
|
||||
|
||||
1. If present, invoke the optional callback `c:prep_stop/1`.
|
||||
2. Terminate the top-level supervisor.
|
||||
3. Invoke the required callback `c:stop/1`.
|
||||
1. If present, invoke the optional callback `c:prep_stop/1`.
|
||||
2. Terminate the top-level supervisor.
|
||||
3. Invoke the required callback `c:stop/1`.
|
||||
|
||||
The arguments passed to the callbacks are related to the state optionally
|
||||
returned by `c:start/2`, and are documented in the section about the callback
|
||||
@@ -248,21 +198,22 @@ defmodule Application do
|
||||
will shut down every application in the opposite order they had been started.
|
||||
|
||||
By default, a SIGTERM from the operating system will automatically translate to
|
||||
`System.stop/0`. You can also have more explicit control over operating system
|
||||
signals via the `:os.set_signal/2` function.
|
||||
`System.stop/0`. You can also have more explicit control over OS signals via the
|
||||
`:os.set_signal/2` function.
|
||||
|
||||
## Tooling
|
||||
|
||||
The Mix build tool automates most of the application management tasks. For example,
|
||||
The Mix build tool can also be used to start your applications. For example,
|
||||
`mix test` automatically starts your application dependencies and your application
|
||||
itself before your test runs. `mix run --no-halt` boots your current project and
|
||||
can be used to start a long running system. See `mix help run`.
|
||||
|
||||
Developers can also use `mix release` to build **releases**. Releases are able to
|
||||
package all of your source code as well as the Erlang VM into a single directory.
|
||||
Releases also give you explicit control over how each application is started and in
|
||||
which order. They also provide a more streamlined mechanism for starting and
|
||||
stopping systems, debugging, logging, as well as system monitoring.
|
||||
Developers can also use tools like [Distillery](https://github.com/bitwalker/distillery)
|
||||
that build **releases**. Releases are able to package all of your source code
|
||||
as well as the Erlang VM into a single directory. Releases also give you explicit
|
||||
control over how each application is started and in which order. They also provide
|
||||
a more streamlined mechanism for starting and stopping systems, debugging, logging,
|
||||
as well as system monitoring.
|
||||
|
||||
Finally, Elixir provides tools such as escripts and archives, which are
|
||||
different mechanisms for packaging your application. Those are typically used
|
||||
@@ -272,10 +223,11 @@ defmodule Application do
|
||||
## Further information
|
||||
|
||||
For further details on applications please check the documentation of the
|
||||
[`:application` Erlang module](`:application`), and the
|
||||
[Applications](https://erlang.org/doc/design_principles/applications.html)
|
||||
[`application`](http://www.erlang.org/doc/man/application.html) Erlang module,
|
||||
and the
|
||||
[Applications](http://www.erlang.org/doc/design_principles/applications.html)
|
||||
section of the [OTP Design Principles User's
|
||||
Guide](https://erlang.org/doc/design_principles/users_guide.html).
|
||||
Guide](http://erlang.org/doc/design_principles/users_guide.html).
|
||||
"""
|
||||
|
||||
@doc """
|
||||
@@ -301,7 +253,7 @@ defmodule Application do
|
||||
application specification key `:start_phases` is not `:undefined`.
|
||||
|
||||
`start_args` are the arguments passed to the application in the `:mod`
|
||||
specification key (for example, `mod: {MyApp, [:my_args]}`).
|
||||
specification key (e.g., `mod: {MyApp, [:my_args]}`).
|
||||
|
||||
This function should either return `{:ok, pid}` or `{:ok, pid, state}` if
|
||||
startup is successful. `pid` should be the PID of the top supervisor. `state`
|
||||
@@ -381,6 +333,13 @@ defmodule Application do
|
||||
end
|
||||
end
|
||||
|
||||
@type app :: atom
|
||||
@type key :: atom
|
||||
@type value :: term
|
||||
@type state :: term
|
||||
@type start_type :: :normal | {:takeover, node} | {:failover, node}
|
||||
@type restart_type :: :permanent | :transient | :temporary
|
||||
|
||||
@application_keys [
|
||||
:description,
|
||||
:id,
|
||||
@@ -390,22 +349,11 @@ defmodule Application do
|
||||
:maxT,
|
||||
:registered,
|
||||
:included_applications,
|
||||
:optional_applications,
|
||||
:applications,
|
||||
:mod,
|
||||
:start_phases
|
||||
]
|
||||
|
||||
application_key_specs = Enum.reduce(@application_keys, &{:|, [], [&1, &2]})
|
||||
|
||||
@type app :: atom
|
||||
@type key :: atom
|
||||
@type application_key :: unquote(application_key_specs)
|
||||
@type value :: term
|
||||
@type state :: term
|
||||
@type start_type :: :normal | {:takeover, node} | {:failover, node}
|
||||
@type restart_type :: :permanent | :transient | :temporary
|
||||
|
||||
@doc """
|
||||
Returns the spec for `app`.
|
||||
|
||||
@@ -416,7 +364,7 @@ defmodule Application do
|
||||
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) :: [{application_key, value}] | nil
|
||||
@spec spec(app) :: [{key, value}] | nil
|
||||
def spec(app) when is_atom(app) do
|
||||
case :application.get_all_key(app) do
|
||||
{:ok, info} -> :lists.keydelete(:env, 1, info)
|
||||
@@ -431,7 +379,7 @@ defmodule Application do
|
||||
specification parameter does not exist, this function
|
||||
will raise. Returns `nil` if the application is not loaded.
|
||||
"""
|
||||
@spec spec(app, application_key) :: value | nil
|
||||
@spec spec(app, key) :: value | nil
|
||||
def spec(app, key) when is_atom(app) and key in @application_keys do
|
||||
case :application.get_key(app, key) do
|
||||
{:ok, value} -> value
|
||||
@@ -462,149 +410,12 @@ defmodule Application do
|
||||
:application.get_all_env(app)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads the application environment at compilation time.
|
||||
|
||||
Similar to `get_env/3`, except it must be used to read values
|
||||
at compile time. This allows Elixir to track when configuration
|
||||
values change between compile time and runtime.
|
||||
|
||||
The first argument is the application name. The second argument
|
||||
`key_or_path` is either an atom key or a path to traverse in
|
||||
search of the configuration, starting with an atom key.
|
||||
|
||||
For example, imagine the following configuration:
|
||||
|
||||
config :my_app, :key, [foo: [bar: :baz]]
|
||||
|
||||
We can access it during compile time as:
|
||||
|
||||
Application.compile_env(:my_app, :key)
|
||||
#=> [foo: [bar: :baz]]
|
||||
|
||||
Application.compile_env(:my_app, [:key, :foo])
|
||||
#=> [bar: :baz]
|
||||
|
||||
Application.compile_env(:my_app, [:key, :foo, :bar])
|
||||
#=> :baz
|
||||
|
||||
A default value can also be given as third argument. If
|
||||
any of the keys in the path along the way is missing, the
|
||||
default value is used:
|
||||
|
||||
Application.compile_env(:my_app, [:unknown, :foo, :bar], :default)
|
||||
#=> :default
|
||||
|
||||
Application.compile_env(:my_app, [:key, :unknown, :bar], :default)
|
||||
#=> :default
|
||||
|
||||
Application.compile_env(:my_app, [:key, :foo, :unknown], :default)
|
||||
#=> :default
|
||||
|
||||
Giving a path is useful to let Elixir know that only certain paths
|
||||
in a large configuration are compile time dependent.
|
||||
"""
|
||||
# TODO: Warn on v1.14 if get_env/fetch_env/fetch_env! is used at
|
||||
# compile time instead of compile_env
|
||||
@doc since: "1.10.0"
|
||||
@spec compile_env(app, key | list, value) :: value
|
||||
defmacro compile_env(app, key_or_path, default \\ nil) do
|
||||
if __CALLER__.function do
|
||||
raise "Application.compile_env/3 cannot be called inside functions, only in the module body"
|
||||
end
|
||||
|
||||
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
|
||||
|
||||
quote do
|
||||
Application.__compile_env__(unquote(app), unquote(key_or_path), unquote(default), __ENV__)
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_key_or_path({:__aliases__, _, _} = alias, env),
|
||||
do: Macro.expand(alias, %{env | function: {:__info__, 1}})
|
||||
|
||||
defp expand_key_or_path(list, env) when is_list(list),
|
||||
do: Enum.map(list, &expand_key_or_path(&1, env))
|
||||
|
||||
defp expand_key_or_path(other, _env),
|
||||
do: other
|
||||
|
||||
@doc false
|
||||
def __compile_env__(app, key_or_path, default, env) do
|
||||
case fetch_compile_env(app, key_or_path, env) do
|
||||
{:ok, value} -> value
|
||||
:error -> default
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads the application environment at compilation time or raises.
|
||||
|
||||
This is the same as `compile_env/3` but it raises an
|
||||
`ArgumentError` if the configuration is not available.
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec compile_env!(app, key | list) :: value
|
||||
defmacro compile_env!(app, key_or_path) do
|
||||
if __CALLER__.function do
|
||||
raise "Application.compile_env!/2 cannot be called inside functions, only in the module body"
|
||||
end
|
||||
|
||||
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
|
||||
|
||||
quote do
|
||||
Application.__compile_env__!(unquote(app), unquote(key_or_path), __ENV__)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __compile_env__!(app, key_or_path, env) do
|
||||
case fetch_compile_env(app, key_or_path, env) do
|
||||
{:ok, value} ->
|
||||
value
|
||||
|
||||
:error ->
|
||||
raise ArgumentError,
|
||||
"could not fetch application environment #{inspect(key_or_path)} for application " <>
|
||||
"#{inspect(app)} #{fetch_env_failed_reason(app, key_or_path)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp fetch_compile_env(app, key, env) when is_atom(key),
|
||||
do: fetch_compile_env(app, key, [], env)
|
||||
|
||||
defp fetch_compile_env(app, [key | paths], env) when is_atom(key),
|
||||
do: fetch_compile_env(app, key, paths, env)
|
||||
|
||||
defp fetch_compile_env(app, key, path, env) do
|
||||
return = traverse_env(fetch_env(app, key), path)
|
||||
|
||||
for tracer <- env.tracers do
|
||||
tracer.trace({:compile_env, app, [key | path], return}, env)
|
||||
end
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
defp traverse_env(return, []), do: return
|
||||
defp traverse_env(:error, _paths), do: :error
|
||||
defp traverse_env({:ok, value}, [key | keys]), do: traverse_env(Access.fetch(value, key), keys)
|
||||
|
||||
@doc """
|
||||
Returns the value for `key` in `app`'s environment.
|
||||
|
||||
If the configuration parameter does not exist, the function returns the
|
||||
`default` value.
|
||||
|
||||
**Important:** if you are reading the application environment at compilation
|
||||
time, for example, inside the module definition instead of inside of a
|
||||
function, see `compile_env/3` instead.
|
||||
|
||||
**Important:** if you are writing a library to be used by other developers,
|
||||
it is generally recommended to avoid the application environment, as the
|
||||
application environment is effectively a global storage. For more information,
|
||||
read our [library guidelines](library-guidelines.md).
|
||||
|
||||
## Examples
|
||||
|
||||
`get_env/3` is commonly used to read the configuration of your OTP applications.
|
||||
@@ -630,22 +441,18 @@ defmodule Application do
|
||||
|
||||
Our database engine used by `:my_app` needs to know what databases exist, and
|
||||
what the database configurations are. The database engine can make a call to
|
||||
`Application.get_env(:my_app, :my_app_databases, [])` to retrieve the list of
|
||||
databases (specified by module names).
|
||||
|
||||
The engine can then traverse each repository in the list and call
|
||||
`Application.get_env(:my_app, Databases.RepoOne)` and so forth to retrieve the
|
||||
configuration of each one. In this case, each configuration will be a keyword
|
||||
list, so you can use the functions in the `Keyword` module or even the `Access`
|
||||
module to traverse it, for example:
|
||||
|
||||
config = Application.get_env(:my_app, Databases.RepoOne)
|
||||
config[:ip]
|
||||
`get_env(:my_app, :my_app_databases)` to retrieve the list of databases (specified
|
||||
by module names). Our database engine can then traverse each repository in the
|
||||
list and then call `get_env(:my_app, Databases.RepoOne)` and so forth to retrieve
|
||||
the configuration of each one.
|
||||
|
||||
**Important:** if you are writing a library to be used by other developers,
|
||||
it is generally recommended to avoid the application environment, as the
|
||||
application environment is effectively a global storage. For more information,
|
||||
read our [library guidelines](library-guidelines.html).
|
||||
"""
|
||||
@spec get_env(app, key, value) :: value
|
||||
def get_env(app, key, default \\ nil) when is_atom(app) do
|
||||
maybe_warn_on_app_env_key(app, key)
|
||||
:application.get_env(app, key, default)
|
||||
end
|
||||
|
||||
@@ -656,8 +463,6 @@ defmodule Application do
|
||||
"""
|
||||
@spec fetch_env(app, key) :: {:ok, value} | :error
|
||||
def fetch_env(app, key) when is_atom(app) do
|
||||
maybe_warn_on_app_env_key(app, key)
|
||||
|
||||
case :application.get_env(app, key) do
|
||||
{:ok, value} -> {:ok, value}
|
||||
:undefined -> :error
|
||||
@@ -668,10 +473,6 @@ defmodule Application do
|
||||
Returns the value for `key` in `app`'s environment.
|
||||
|
||||
If the configuration parameter does not exist, raises `ArgumentError`.
|
||||
|
||||
**Important:** if you are reading the application environment at compilation
|
||||
time, for example, inside the module definition instead of inside of a
|
||||
function, see `compile_env!/2` instead.
|
||||
"""
|
||||
@spec fetch_env!(app, key) :: value
|
||||
def fetch_env!(app, key) when is_atom(app) do
|
||||
@@ -680,21 +481,23 @@ defmodule Application do
|
||||
value
|
||||
|
||||
:error ->
|
||||
raise ArgumentError,
|
||||
"could not fetch application environment #{inspect(key)} for application " <>
|
||||
"#{inspect(app)} #{fetch_env_failed_reason(app, key)}"
|
||||
end
|
||||
end
|
||||
vsn = :application.get_key(app, :vsn)
|
||||
app = inspect(app)
|
||||
key = inspect(key)
|
||||
|
||||
defp fetch_env_failed_reason(app, key) do
|
||||
vsn = :application.get_key(app, :vsn)
|
||||
case vsn do
|
||||
{:ok, _} ->
|
||||
raise ArgumentError,
|
||||
"could not fetch application environment #{key} for application #{app} " <>
|
||||
"because configuration #{key} was not set"
|
||||
|
||||
case vsn do
|
||||
{:ok, _} ->
|
||||
"because configuration at #{inspect(key)} was not set"
|
||||
|
||||
:undefined ->
|
||||
"because the application was not loaded nor configured"
|
||||
:undefined ->
|
||||
raise ArgumentError,
|
||||
"could not fetch application environment #{key} for application #{app} " <>
|
||||
"because the application was not loaded/started. If your application " <>
|
||||
"depends on #{app} at runtime, make sure to load/start it or list it " <>
|
||||
"under :extra_applications in your mix.exs file"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -717,48 +520,19 @@ defmodule Application do
|
||||
"""
|
||||
@spec put_env(app, key, value, timeout: timeout, persistent: boolean) :: :ok
|
||||
def put_env(app, key, value, opts \\ []) when is_atom(app) do
|
||||
maybe_warn_on_app_env_key(app, key)
|
||||
:application.set_env(app, key, value, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the environment for multiple apps at the same time.
|
||||
|
||||
The given config should not:
|
||||
|
||||
* have the same application listed more than once
|
||||
* have the same key inside the same application listed more than once
|
||||
|
||||
If those conditions are not met, it will raise.
|
||||
|
||||
It receives the same options as `put_env/4`. Returns `:ok`.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec put_all_env([{app, [{key, value}]}], timeout: timeout, persistent: boolean) :: :ok
|
||||
def put_all_env(config, opts \\ []) when is_list(config) and is_list(opts) do
|
||||
:application.set_env(config, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the `key` from the given `app` environment.
|
||||
|
||||
It receives the same options as `put_env/4`. Returns `:ok`.
|
||||
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 \\ []) when is_atom(app) do
|
||||
maybe_warn_on_app_env_key(app, key)
|
||||
:application.unset_env(app, key, opts)
|
||||
end
|
||||
|
||||
defp maybe_warn_on_app_env_key(_app, key) when is_atom(key),
|
||||
do: :ok
|
||||
|
||||
# TODO: Remove this deprecation warning on 2.0+ and allow list lookups as in compile_env.
|
||||
defp maybe_warn_on_app_env_key(app, key) do
|
||||
message = "passing non-atom as application env key is deprecated, got: #{inspect(key)}"
|
||||
IO.warn_once({Application, :key, app, key}, message, _stacktrace_drop_levels = 2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the given `app` is started.
|
||||
|
||||
@@ -774,22 +548,6 @@ defmodule Application do
|
||||
:application.ensure_started(app, type)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the given `app` is loaded.
|
||||
|
||||
Same as `load/2` but returns `:ok` if the application was already
|
||||
loaded.
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec ensure_loaded(app) :: :ok | {:error, term}
|
||||
def ensure_loaded(app) when is_atom(app) do
|
||||
case :application.load(app) do
|
||||
:ok -> :ok
|
||||
{:error, {:already_loaded, ^app}} -> :ok
|
||||
{:error, _} = error -> error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the given `app` and its applications are started.
|
||||
|
||||
@@ -898,7 +656,7 @@ defmodule Application do
|
||||
#=> "bar-123"
|
||||
|
||||
For more information on code paths, check the `Code` module in
|
||||
Elixir and also Erlang's [`:code` module](`:code`).
|
||||
Elixir and also Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html).
|
||||
"""
|
||||
@spec app_dir(app) :: String.t()
|
||||
def app_dir(app) when is_atom(app) do
|
||||
|
||||
+3
-40
@@ -1,46 +1,8 @@
|
||||
defmodule Atom do
|
||||
@moduledoc """
|
||||
Atoms are constants whose values are their own name.
|
||||
|
||||
They are often useful to enumerate over distinct values, such as:
|
||||
|
||||
iex> :apple
|
||||
:apple
|
||||
iex> :orange
|
||||
:orange
|
||||
iex> :watermelon
|
||||
:watermelon
|
||||
|
||||
Atoms are equal if their names are equal.
|
||||
|
||||
iex> :apple == :apple
|
||||
true
|
||||
iex> :apple == :orange
|
||||
false
|
||||
|
||||
Often they are used to express the state of an operation, by using
|
||||
values such as `:ok` and `:error`.
|
||||
|
||||
The booleans `true` and `false` are also atoms:
|
||||
|
||||
iex> true == :true
|
||||
true
|
||||
iex> is_atom(false)
|
||||
true
|
||||
iex> is_boolean(:false)
|
||||
true
|
||||
|
||||
Elixir allows you to skip the leading `:` for the atoms `false`, `true`,
|
||||
and `nil`.
|
||||
|
||||
Atoms must be composed of Unicode characters such as letters, numbers,
|
||||
underscore, and `@`. If the keyword has a character that does not
|
||||
belong to the category above, such as spaces, you can wrap it in
|
||||
quotes:
|
||||
|
||||
iex> :"this is an atom with spaces"
|
||||
:"this is an atom with spaces"
|
||||
Convenience functions for working with atoms.
|
||||
|
||||
See also `Kernel.is_atom/1`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
@@ -75,6 +37,7 @@ defmodule Atom do
|
||||
:erlang.atom_to_list(atom)
|
||||
end
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
@doc false
|
||||
@deprecated "Use Atom.to_charlist/1 instead"
|
||||
@spec to_char_list(atom) :: charlist
|
||||
|
||||
+81
-84
@@ -10,91 +10,88 @@ defmodule Base do
|
||||
|
||||
## 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 |
|
||||
| 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 | | |
|
||||
| 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 | | |
|
||||
| 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 | | |
|
||||
| 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 | | |
|
||||
| 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| | |
|
||||
|
||||
"""
|
||||
|
||||
@type encode_case :: :upper | :lower
|
||||
@type decode_case :: :upper | :lower | :mixed
|
||||
|
||||
b16_alphabet = '0123456789ABCDEF'
|
||||
b64_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
||||
b64url_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
|
||||
@@ -270,7 +267,7 @@ defmodule Base do
|
||||
"666f6f626172"
|
||||
|
||||
"""
|
||||
@spec encode16(binary, case: encode_case) :: binary
|
||||
@spec encode16(binary, keyword) :: binary
|
||||
def encode16(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
do_encode16(case, data)
|
||||
@@ -303,7 +300,7 @@ defmodule Base do
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec decode16(binary, case: decode_case) :: {:ok, binary} | :error
|
||||
@spec decode16(binary, keyword) :: {:ok, binary} | :error
|
||||
def decode16(string, opts \\ []) do
|
||||
{:ok, decode16!(string, opts)}
|
||||
rescue
|
||||
@@ -340,7 +337,7 @@ defmodule Base do
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec decode16!(binary, case: encode_case) :: binary
|
||||
@spec decode16!(binary, keyword) :: binary
|
||||
def decode16!(string, opts \\ [])
|
||||
|
||||
def decode16!(string, opts) when is_binary(string) and rem(byte_size(string), 2) == 0 do
|
||||
@@ -370,7 +367,7 @@ defmodule Base do
|
||||
"Zm9vYg"
|
||||
|
||||
"""
|
||||
@spec encode64(binary, padding: boolean) :: binary
|
||||
@spec encode64(binary, keyword) :: binary
|
||||
def encode64(data, opts \\ []) when is_binary(data) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_encode64(data, pad?)
|
||||
@@ -400,7 +397,7 @@ defmodule Base do
|
||||
{:ok, "foob"}
|
||||
|
||||
"""
|
||||
@spec decode64(binary, ignore: :whitespace, padding: boolean) :: {:ok, binary} | :error
|
||||
@spec decode64(binary, keyword) :: {:ok, binary} | :error
|
||||
def decode64(string, opts \\ []) when is_binary(string) do
|
||||
{:ok, decode64!(string, opts)}
|
||||
rescue
|
||||
@@ -434,7 +431,7 @@ defmodule Base do
|
||||
"foob"
|
||||
|
||||
"""
|
||||
@spec decode64!(binary, ignore: :whitespace, padding: boolean) :: binary
|
||||
@spec decode64!(binary, keyword) :: binary
|
||||
def decode64!(string, opts \\ []) when is_binary(string) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
string |> remove_ignored(opts[:ignore]) |> do_decode64(pad?)
|
||||
@@ -456,7 +453,7 @@ defmodule Base do
|
||||
"_3_-_A"
|
||||
|
||||
"""
|
||||
@spec url_encode64(binary, padding: boolean) :: binary
|
||||
@spec url_encode64(binary, keyword) :: binary
|
||||
def url_encode64(data, opts \\ []) when is_binary(data) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_encode64url(data, pad?)
|
||||
@@ -484,7 +481,7 @@ defmodule Base do
|
||||
{:ok, <<255, 127, 254, 252>>}
|
||||
|
||||
"""
|
||||
@spec url_decode64(binary, ignore: :whitespace, padding: boolean) :: {:ok, binary} | :error
|
||||
@spec url_decode64(binary, keyword) :: {:ok, binary} | :error
|
||||
def url_decode64(string, opts \\ []) when is_binary(string) do
|
||||
{:ok, url_decode64!(string, opts)}
|
||||
rescue
|
||||
@@ -516,7 +513,7 @@ defmodule Base do
|
||||
<<255, 127, 254, 252>>
|
||||
|
||||
"""
|
||||
@spec url_decode64!(binary, ignore: :whitespace, padding: boolean) :: binary
|
||||
@spec url_decode64!(binary, keyword) :: binary
|
||||
def url_decode64!(string, opts \\ []) when is_binary(string) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
string |> remove_ignored(opts[:ignore]) |> do_decode64url(pad?)
|
||||
@@ -554,7 +551,7 @@ defmodule Base do
|
||||
"MZXW6YTBOI"
|
||||
|
||||
"""
|
||||
@spec encode32(binary, case: encode_case, padding: boolean) :: binary
|
||||
@spec encode32(binary, keyword) :: binary
|
||||
def encode32(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
@@ -597,7 +594,7 @@ defmodule Base do
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec decode32(binary, case: decode_case, padding: boolean) :: {:ok, binary} | :error
|
||||
@spec decode32(binary, keyword) :: {:ok, binary} | :error
|
||||
def decode32(string, opts \\ []) do
|
||||
{:ok, decode32!(string, opts)}
|
||||
rescue
|
||||
@@ -643,7 +640,7 @@ defmodule Base do
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec decode32!(binary, case: decode_case, padding: boolean) :: binary
|
||||
@spec decode32!(binary, keyword) :: binary
|
||||
def decode32!(string, opts \\ []) when is_binary(string) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
@@ -683,7 +680,7 @@ defmodule Base do
|
||||
"CPNMUOJ1E8"
|
||||
|
||||
"""
|
||||
@spec hex_encode32(binary, case: encode_case, padding: boolean) :: binary
|
||||
@spec hex_encode32(binary, keyword) :: binary
|
||||
def hex_encode32(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
@@ -727,7 +724,7 @@ defmodule Base do
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec hex_decode32(binary, case: decode_case, padding: boolean) :: {:ok, binary} | :error
|
||||
@spec hex_decode32(binary, keyword) :: {:ok, binary} | :error
|
||||
def hex_decode32(string, opts \\ []) do
|
||||
{:ok, hex_decode32!(string, opts)}
|
||||
rescue
|
||||
@@ -774,7 +771,7 @@ defmodule Base do
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec hex_decode32!(binary, case: decode_case, padding: boolean) :: binary
|
||||
@spec hex_decode32!(binary, keyword) :: binary
|
||||
def hex_decode32!(string, opts \\ []) when is_binary(string) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
|
||||
@@ -4,7 +4,7 @@ defmodule Behaviour do
|
||||
|
||||
This module is deprecated. Instead of `defcallback/1` and
|
||||
`defmacrocallback/1`, the `@callback` and `@macrocallback`
|
||||
module attributes can be used respectively. See the
|
||||
module attributes can be used (respectively). See the
|
||||
documentation for `Module` for more information on these
|
||||
attributes.
|
||||
|
||||
@@ -17,7 +17,6 @@ defmodule Behaviour do
|
||||
@doc """
|
||||
Defines a function callback according to the given type specification.
|
||||
"""
|
||||
@deprecated "Use the @callback module attribute instead"
|
||||
defmacro defcallback(spec) do
|
||||
do_defcallback(:def, split_spec(spec, quote(do: term)))
|
||||
end
|
||||
@@ -25,12 +24,11 @@ defmodule Behaviour do
|
||||
@doc """
|
||||
Defines a macro callback according to the given type specification.
|
||||
"""
|
||||
@deprecated "Use the @macrocallback module attribute instead"
|
||||
defmacro defmacrocallback(spec) do
|
||||
do_defcallback(:defmacro, split_spec(spec, quote(do: Macro.t())))
|
||||
end
|
||||
|
||||
defp split_spec({:when, _, [{:"::", _, [spec, return]}, guard]}, _default) do
|
||||
defp split_spec({:when, _, [{:::, _, [spec, return]}, guard]}, _default) do
|
||||
{spec, return, guard}
|
||||
end
|
||||
|
||||
@@ -38,7 +36,7 @@ defmodule Behaviour do
|
||||
{spec, default, guard}
|
||||
end
|
||||
|
||||
defp split_spec({:"::", _, [spec, return]}, _default) do
|
||||
defp split_spec({:::, _, [spec, return]}, _default) do
|
||||
{spec, return, []}
|
||||
end
|
||||
|
||||
@@ -58,7 +56,7 @@ defmodule Behaviour do
|
||||
|
||||
defp do_callback(kind, name, args, return, guards) do
|
||||
fun = fn
|
||||
{:"::", _, [left, right]} ->
|
||||
{:::, _, [left, right]} ->
|
||||
ensure_not_default(left)
|
||||
ensure_not_default(right)
|
||||
left
|
||||
@@ -113,9 +111,9 @@ defmodule Behaviour do
|
||||
end
|
||||
end
|
||||
|
||||
defp __behaviour__doc_value(%{"en" => doc}), do: doc
|
||||
defp __behaviour__doc_value(:none), do: nil
|
||||
defp __behaviour__doc_value(:hidden), do: false
|
||||
defp __behaviour__doc_value(_), do: nil
|
||||
defp __behaviour__doc_value(%{"en" => doc}), do: doc
|
||||
|
||||
import unquote(__MODULE__)
|
||||
end
|
||||
|
||||
+43
-118
@@ -1,11 +1,8 @@
|
||||
defmodule Bitwise do
|
||||
@moduledoc """
|
||||
A set of functions that perform calculations on bits.
|
||||
A set of macros that perform calculations on bits.
|
||||
|
||||
All bitwise functions work only on integers; otherwise an
|
||||
`ArithmeticError` is raised.
|
||||
|
||||
The functions in this module come in two flavors: named or
|
||||
The macros in this module come in two flavors: named or
|
||||
operators. For example:
|
||||
|
||||
iex> use Bitwise
|
||||
@@ -29,16 +26,16 @@ defmodule Bitwise do
|
||||
When invoked with no options, `use Bitwise` is equivalent
|
||||
to `import Bitwise`.
|
||||
|
||||
All bitwise functions can be used in guards:
|
||||
All bitwise macros can be used in guards:
|
||||
|
||||
iex> use Bitwise
|
||||
iex> odd? = fn
|
||||
...> int when Bitwise.band(int, 1) == 1 -> true
|
||||
...> int when band(int, 1) == 1 -> true
|
||||
...> _ -> false
|
||||
...> end
|
||||
iex> odd?.(1)
|
||||
true
|
||||
|
||||
All functions in this module are inlined by the compiler.
|
||||
"""
|
||||
|
||||
@doc false
|
||||
@@ -61,246 +58,174 @@ defmodule Bitwise do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise NOT of the argument.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
Calculates the bitwise NOT of its argument.
|
||||
|
||||
iex> bnot(2)
|
||||
-3
|
||||
|
||||
iex> bnot(2) &&& 3
|
||||
1
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec bnot(integer) :: integer
|
||||
def bnot(expr) do
|
||||
:erlang.bnot(expr)
|
||||
defmacro bnot(expr) do
|
||||
quote(do: :erlang.bnot(unquote(expr)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Bitwise NOT unary operator.
|
||||
|
||||
Calculates the bitwise NOT of the argument.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
Prefix (unary) operator; calculates the bitwise NOT of its argument.
|
||||
|
||||
iex> ~~~2
|
||||
-3
|
||||
|
||||
iex> ~~~2 &&& 3
|
||||
1
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec ~~~integer :: integer
|
||||
def ~~~expr do
|
||||
:erlang.bnot(expr)
|
||||
defmacro ~~~expr do
|
||||
quote(do: :erlang.bnot(unquote(expr)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise AND of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> band(9, 3)
|
||||
1
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec band(integer, integer) :: integer
|
||||
def band(left, right) do
|
||||
:erlang.band(left, right)
|
||||
defmacro band(left, right) do
|
||||
quote(do: :erlang.band(unquote(left), unquote(right)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Bitwise AND operator.
|
||||
|
||||
Calculates the bitwise AND of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
Infix operator; calculates the bitwise AND of its arguments.
|
||||
|
||||
iex> 9 &&& 3
|
||||
1
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec integer &&& integer :: integer
|
||||
def left &&& right do
|
||||
:erlang.band(left, right)
|
||||
defmacro left &&& right do
|
||||
quote(do: :erlang.band(unquote(left), unquote(right)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise OR of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> bor(9, 3)
|
||||
11
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec bor(integer, integer) :: integer
|
||||
def bor(left, right) do
|
||||
:erlang.bor(left, right)
|
||||
defmacro bor(left, right) do
|
||||
quote(do: :erlang.bor(unquote(left), unquote(right)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Bitwise OR operator.
|
||||
|
||||
Calculates the bitwise OR of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
Infix operator; calculates the bitwise OR of its arguments.
|
||||
|
||||
iex> 9 ||| 3
|
||||
11
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec integer ||| integer :: integer
|
||||
def left ||| right do
|
||||
:erlang.bor(left, right)
|
||||
defmacro left ||| right do
|
||||
quote(do: :erlang.bor(unquote(left), unquote(right)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise XOR of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> bxor(9, 3)
|
||||
10
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec bxor(integer, integer) :: integer
|
||||
def bxor(left, right) do
|
||||
:erlang.bxor(left, right)
|
||||
defmacro bxor(left, right) do
|
||||
quote(do: :erlang.bxor(unquote(left), unquote(right)))
|
||||
end
|
||||
|
||||
@doc false
|
||||
def unquote(:^^^)(left, right) do
|
||||
:erlang.bxor(left, right)
|
||||
@doc """
|
||||
Infix operator; calculates the bitwise XOR of its arguments.
|
||||
|
||||
iex> 9 ^^^ 3
|
||||
10
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro left ^^^ right do
|
||||
quote(do: :erlang.bxor(unquote(left), unquote(right)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the result of an arithmetic left bitshift.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> bsl(1, 2)
|
||||
4
|
||||
|
||||
iex> bsl(1, -2)
|
||||
0
|
||||
|
||||
iex> bsl(-1, 2)
|
||||
-4
|
||||
|
||||
iex> bsl(-1, -2)
|
||||
-1
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec bsl(integer, integer) :: integer
|
||||
def bsl(left, right) do
|
||||
:erlang.bsl(left, right)
|
||||
defmacro bsl(left, right) do
|
||||
quote(do: :erlang.bsl(unquote(left), unquote(right)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Arithmetic left bitshift operator.
|
||||
|
||||
Calculates the result of an arithmetic left bitshift.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
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
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec integer <<< integer :: integer
|
||||
def left <<< right do
|
||||
:erlang.bsl(left, right)
|
||||
defmacro left <<< right do
|
||||
quote(do: :erlang.bsl(unquote(left), unquote(right)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the result of an arithmetic right bitshift.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> bsr(1, 2)
|
||||
0
|
||||
|
||||
iex> bsr(1, -2)
|
||||
4
|
||||
|
||||
iex> bsr(-1, 2)
|
||||
-1
|
||||
|
||||
iex> bsr(-1, -2)
|
||||
-4
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec bsr(integer, integer) :: integer
|
||||
def bsr(left, right) do
|
||||
:erlang.bsr(left, right)
|
||||
defmacro bsr(left, right) do
|
||||
quote(do: :erlang.bsr(unquote(left), unquote(right)))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Arithmetic right bitshift operator.
|
||||
|
||||
Calculates the result of an arithmetic right bitshift.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
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
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec integer >>> integer :: integer
|
||||
def left >>> right do
|
||||
:erlang.bsr(left, right)
|
||||
defmacro left >>> right do
|
||||
quote(do: :erlang.bsr(unquote(left), unquote(right)))
|
||||
end
|
||||
end
|
||||
|
||||
+10
-576
@@ -11,7 +11,7 @@ defmodule Calendar do
|
||||
For the actual date, time and datetime structures, see `Date`,
|
||||
`Time`, `NaiveDateTime` and `DateTime`.
|
||||
|
||||
Note designations for year, month, day, and the like, are overspecified
|
||||
Note the year, month, day, etc. designations are overspecified
|
||||
(i.e. an integer instead of `1..12` for months) because different
|
||||
calendars may have a different number of days per month, months per year and so on.
|
||||
"""
|
||||
@@ -23,11 +23,6 @@ defmodule Calendar do
|
||||
@type day_of_week :: non_neg_integer
|
||||
@type era :: non_neg_integer
|
||||
|
||||
@typedoc """
|
||||
A tuple representing the `day` and the `era`.
|
||||
"""
|
||||
@type day_of_era :: {day :: non_neg_integer(), era}
|
||||
|
||||
@type hour :: non_neg_integer
|
||||
@type minute :: non_neg_integer
|
||||
@type second :: non_neg_integer
|
||||
@@ -57,29 +52,21 @@ defmodule Calendar do
|
||||
representing the microseconds to external format. If the precision is 0,
|
||||
it means microseconds must be skipped.
|
||||
"""
|
||||
@type microsecond :: {non_neg_integer, non_neg_integer}
|
||||
@type microsecond :: {0..999_999, 0..6}
|
||||
|
||||
@typedoc "A calendar implementation"
|
||||
@type calendar :: module
|
||||
|
||||
@typedoc "The time zone ID according to the IANA tz database (for example, Europe/Zurich)"
|
||||
@typedoc "The time zone ID according to the IANA tz database (e.g. Europe/Zurich)"
|
||||
@type time_zone :: String.t()
|
||||
|
||||
@typedoc "The time zone abbreviation (for example, CET or CEST or BST, and such)"
|
||||
@typedoc "The time zone abbreviation (e.g. CET or CEST or BST etc.)"
|
||||
@type zone_abbr :: String.t()
|
||||
|
||||
@typedoc """
|
||||
The time zone UTC offset in seconds for standard time.
|
||||
|
||||
See also `t:std_offset/0`.
|
||||
"""
|
||||
@typedoc "The time zone UTC offset in seconds"
|
||||
@type utc_offset :: integer
|
||||
|
||||
@typedoc """
|
||||
The time zone standard offset in seconds (typically not zero in summer times).
|
||||
|
||||
It must be added to `t:utc_offset/0` to get the total offset from UTC used for "wall time".
|
||||
"""
|
||||
@typedoc "The time zone standard offset in seconds (not zero in summer times)"
|
||||
@type std_offset :: integer
|
||||
|
||||
@typedoc "Any map/struct that contains the date fields"
|
||||
@@ -135,7 +122,7 @@ defmodule Calendar do
|
||||
for any other time zone.
|
||||
|
||||
Other time zone databases (including ones provided by packages)
|
||||
can be configured as default either via configuration:
|
||||
can be configure as default either via configuration:
|
||||
|
||||
config :elixir, :time_zone_database, CustomTimeZoneDatabase
|
||||
|
||||
@@ -167,14 +154,8 @@ defmodule Calendar do
|
||||
|
||||
@doc """
|
||||
Calculates the day of the week from the given `year`, `month`, and `day`.
|
||||
|
||||
The `starting_on` represents the starting day of the week. All
|
||||
calendars must support at least the `:default` value. They may
|
||||
also support other values representing their days of the week.
|
||||
"""
|
||||
@callback day_of_week(year, month, day, starting_on :: :default | atom) ::
|
||||
{day_of_week(), first_day_of_week :: non_neg_integer(),
|
||||
last_day_of_week :: non_neg_integer()}
|
||||
@callback day_of_week(year, month, day) :: day_of_week()
|
||||
|
||||
@doc """
|
||||
Calculates the day of the year from the given `year`, `month`, and `day`.
|
||||
@@ -189,12 +170,12 @@ defmodule Calendar do
|
||||
@doc """
|
||||
Calculates the year and era from the given `year`.
|
||||
"""
|
||||
@callback year_of_era(year, month, day) :: {year, era}
|
||||
@callback year_of_era(year) :: {year, era}
|
||||
|
||||
@doc """
|
||||
Calculates the day and era from the given `year`, `month`, and `day`.
|
||||
"""
|
||||
@callback day_of_era(year, month, day) :: day_of_era()
|
||||
@callback day_of_era(year, month, day) :: {non_neg_integer(), era}
|
||||
|
||||
@doc """
|
||||
Converts the date into a string according to the calendar.
|
||||
@@ -284,47 +265,6 @@ defmodule Calendar do
|
||||
"""
|
||||
@callback valid_time?(hour, minute, second, microsecond) :: boolean
|
||||
|
||||
@doc """
|
||||
Parses the string representation for a time returned by `c:time_to_string/4`
|
||||
into a time-tuple.
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@callback parse_time(String.t()) ::
|
||||
{:ok, {hour, minute, second, microsecond}}
|
||||
| {:error, atom}
|
||||
|
||||
@doc """
|
||||
Parses the string representation for a date returned by `c:date_to_string/3`
|
||||
into a date-tuple.
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@callback parse_date(String.t()) ::
|
||||
{:ok, {year, month, day}}
|
||||
| {:error, atom}
|
||||
|
||||
@doc """
|
||||
Parses the string representation for a naive datetime returned by
|
||||
`c:naive_datetime_to_string/7` into a naive-datetime-tuple.
|
||||
|
||||
The given string may contain a timezone offset but it is ignored.
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@callback parse_naive_datetime(String.t()) ::
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}}
|
||||
| {:error, atom}
|
||||
|
||||
@doc """
|
||||
Parses the string representation for a datetime returned by
|
||||
`c:datetime_to_string/11` into a datetime-tuple.
|
||||
|
||||
The returned datetime must be in UTC. The original `utc_offset`
|
||||
it was written in must be returned in the result.
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@callback parse_utc_datetime(String.t()) ::
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}, utc_offset}
|
||||
| {:error, atom}
|
||||
|
||||
# General Helpers
|
||||
|
||||
@doc """
|
||||
@@ -377,510 +317,4 @@ defmodule Calendar do
|
||||
def get_time_zone_database() do
|
||||
Application.get_env(:elixir, :time_zone_database, Calendar.UTCOnlyTimeZoneDatabase)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats received datetime into a string.
|
||||
|
||||
The datetime can be any of the Calendar types (`Time`, `Date`,
|
||||
`NaiveDateTime`, and `DateTime`) or any map, as long as they
|
||||
contain all of the relevant fields necessary for formatting.
|
||||
For example, if you use `%Y` to format the year, the datetime
|
||||
must have the `:year` field. Therefore, if you pass a `Time`,
|
||||
or a map without the `:year` field to a format that expects `%Y`,
|
||||
an error will be raised.
|
||||
|
||||
## Options
|
||||
|
||||
* `:preferred_datetime` - a string for the preferred format to show datetimes,
|
||||
it can't contain the `%c` format and defaults to `"%Y-%m-%d %H:%M:%S"`
|
||||
if the option is not received
|
||||
|
||||
* `:preferred_date` - a string for the preferred format to show dates,
|
||||
it can't contain the `%x` format and defaults to `"%Y-%m-%d"`
|
||||
if the option is not received
|
||||
|
||||
* `:preferred_time` - a string for the preferred format to show times,
|
||||
it can't contain the `%X` format and defaults to `"%H:%M:%S"`
|
||||
if the option is not received
|
||||
|
||||
* `:am_pm_names` - a function that receives either `:am` or `:pm` and returns
|
||||
the name of the period of the day, if the option is not received it defaults
|
||||
to a function that returns `"am"` and `"pm"`, respectively
|
||||
|
||||
* `:month_names` - a function that receives a number and returns the name of
|
||||
the corresponding month, if the option is not received it defaults to a
|
||||
function that returns the month names in English
|
||||
|
||||
* `:abbreviated_month_names` - a function that receives a number and returns the
|
||||
abbreviated name of the corresponding month, if the option is not received it
|
||||
defaults to a function that returns the abbreviated month names in English
|
||||
|
||||
* `:day_of_week_names` - a function that receives a number and returns the name of
|
||||
the corresponding day of week, if the option is not received it defaults to a
|
||||
function that returns the day of week names in English
|
||||
|
||||
* `:abbreviated_day_of_week_names` - a function that receives a number and returns
|
||||
the abbreviated name of the corresponding day of week, if the option is not received
|
||||
it defaults to a function that returns the abbreviated day of week names in English
|
||||
|
||||
## Formatting syntax
|
||||
|
||||
The formatting syntax for strftime is a sequence of characters in the following format:
|
||||
|
||||
%<padding><width><format>
|
||||
|
||||
where:
|
||||
|
||||
* `%`: indicates the start of a formatted section
|
||||
* `<padding>`: set the padding (see below)
|
||||
* `<width>`: a number indicating the minimum size of the formatted section
|
||||
* `<format>`: the format itself (see below)
|
||||
|
||||
### Accepted padding options
|
||||
|
||||
* `-`: no padding, removes all padding from the format
|
||||
* `_`: pad with spaces
|
||||
* `0`: pad with zeroes
|
||||
|
||||
### Accepted formats
|
||||
|
||||
The accepted formats are:
|
||||
|
||||
Format | Description | Examples (in ISO)
|
||||
:----- | :-----------------------------------------------------------------------| :------------------------
|
||||
a | Abbreviated name of day | Mon
|
||||
A | Full name of day | Monday
|
||||
b | Abbreviated month name | Jan
|
||||
B | Full month name | January
|
||||
c | Preferred date+time representation | 2018-10-17 12:34:56
|
||||
d | Day of the month | 01, 31
|
||||
f | Microseconds *(does not support width and padding modifiers)* | 000000, 999999, 0123
|
||||
H | Hour using a 24-hour clock | 00, 23
|
||||
I | Hour using a 12-hour clock | 01, 12
|
||||
j | Day of the year | 001, 366
|
||||
m | Month | 01, 12
|
||||
M | Minute | 00, 59
|
||||
p | "AM" or "PM" (noon is "PM", midnight as "AM") | AM, PM
|
||||
P | "am" or "pm" (noon is "pm", midnight as "am") | am, pm
|
||||
q | Quarter | 1, 2, 3, 4
|
||||
S | Second | 00, 59, 60
|
||||
u | Day of the week | 1 (Monday), 7 (Sunday)
|
||||
x | Preferred date (without time) representation | 2018-10-17
|
||||
X | Preferred time (without date) representation | 12:34:56
|
||||
y | Year as 2-digits | 01, 01, 86, 18
|
||||
Y | Year | -0001, 0001, 1986
|
||||
z | +hhmm/-hhmm time zone offset from UTC (empty string if naive) | +0300, -0530
|
||||
Z | Time zone abbreviation (empty string if naive) | CET, BRST
|
||||
% | Literal "%" character | %
|
||||
|
||||
Any other character will be interpreted as an invalid format and raise an error
|
||||
|
||||
## Examples
|
||||
|
||||
Without options:
|
||||
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%y-%m-%d %I:%M:%S %p")
|
||||
"19-08-26 01:52:06 PM"
|
||||
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%a, %B %d %Y")
|
||||
"Mon, August 26 2019"
|
||||
|
||||
iex> Calendar.strftime(~U[2020-04-02 13:52:06.0Z], "%B %-d, %Y")
|
||||
"April 2, 2020"
|
||||
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%c")
|
||||
"2019-08-26 13:52:06"
|
||||
|
||||
With options:
|
||||
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%c", preferred_datetime: "%H:%M:%S %d-%m-%y")
|
||||
"13:52:06 26-08-19"
|
||||
|
||||
iex> Calendar.strftime(
|
||||
...> ~U[2019-08-26 13:52:06.0Z],
|
||||
...> "%A",
|
||||
...> day_of_week_names: fn day_of_week ->
|
||||
...> {"segunda-feira", "terça-feira", "quarta-feira", "quinta-feira",
|
||||
...> "sexta-feira", "sábado", "domingo"}
|
||||
...> |> elem(day_of_week - 1)
|
||||
...> end
|
||||
...>)
|
||||
"segunda-feira"
|
||||
|
||||
iex> Calendar.strftime(
|
||||
...> ~U[2019-08-26 13:52:06.0Z],
|
||||
...> "%B",
|
||||
...> month_names: fn month ->
|
||||
...> {"январь", "февраль", "март", "апрель", "май", "июнь",
|
||||
...> "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь"}
|
||||
...> |> elem(month - 1)
|
||||
...> end
|
||||
...>)
|
||||
"август"
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec strftime(map(), String.t(), keyword()) :: String.t()
|
||||
def strftime(date_or_time_or_datetime, string_format, user_options \\ [])
|
||||
when is_map(date_or_time_or_datetime) and is_binary(string_format) do
|
||||
parse(
|
||||
string_format,
|
||||
date_or_time_or_datetime,
|
||||
options(user_options),
|
||||
[]
|
||||
)
|
||||
|> IO.iodata_to_binary()
|
||||
end
|
||||
|
||||
defp parse("", _datetime, _format_options, acc),
|
||||
do: Enum.reverse(acc)
|
||||
|
||||
defp parse("%" <> rest, datetime, format_options, acc),
|
||||
do: parse_modifiers(rest, nil, nil, {datetime, format_options, acc})
|
||||
|
||||
defp parse(<<char, rest::binary>>, datetime, format_options, acc),
|
||||
do: parse(rest, datetime, format_options, [char | acc])
|
||||
|
||||
defp parse_modifiers("-" <> rest, width, nil, parser_data) do
|
||||
parse_modifiers(rest, width, "", parser_data)
|
||||
end
|
||||
|
||||
defp parse_modifiers("0" <> rest, width, nil, parser_data) do
|
||||
parse_modifiers(rest, width, ?0, parser_data)
|
||||
end
|
||||
|
||||
defp parse_modifiers("_" <> rest, width, nil, parser_data) do
|
||||
parse_modifiers(rest, width, ?\s, parser_data)
|
||||
end
|
||||
|
||||
defp parse_modifiers(<<digit, rest::binary>>, width, pad, parser_data) when digit in ?0..?9 do
|
||||
new_width = (width || 0) * 10 + (digit - ?0)
|
||||
|
||||
parse_modifiers(rest, new_width, pad, parser_data)
|
||||
end
|
||||
|
||||
# set default padding if none was specified
|
||||
defp parse_modifiers(<<format, _::binary>> = rest, width, nil, parser_data) do
|
||||
parse_modifiers(rest, width, default_pad(format), parser_data)
|
||||
end
|
||||
|
||||
# set default width if none was specified
|
||||
defp parse_modifiers(<<format, _::binary>> = rest, nil, pad, parser_data) do
|
||||
parse_modifiers(rest, default_width(format), pad, parser_data)
|
||||
end
|
||||
|
||||
defp parse_modifiers(rest, width, pad, {datetime, format_options, acc}) do
|
||||
format_modifiers(rest, width, pad, datetime, format_options, acc)
|
||||
end
|
||||
|
||||
defp am_pm(hour, format_options) when hour > 11 do
|
||||
format_options.am_pm_names.(:pm)
|
||||
end
|
||||
|
||||
defp am_pm(hour, format_options) when hour <= 11 do
|
||||
format_options.am_pm_names.(:am)
|
||||
end
|
||||
|
||||
defp default_pad(format) when format in 'aAbBpPZ', do: ?\s
|
||||
defp default_pad(_format), do: ?0
|
||||
|
||||
defp default_width(format) when format in 'dHImMSy', do: 2
|
||||
defp default_width(?j), do: 3
|
||||
defp default_width(format) when format in 'Yz', do: 4
|
||||
defp default_width(_format), do: 0
|
||||
|
||||
# Literally just %
|
||||
defp format_modifiers("%" <> rest, width, pad, datetime, format_options, acc) do
|
||||
parse(rest, datetime, format_options, [pad_leading("%", width, pad) | acc])
|
||||
end
|
||||
|
||||
# Abbreviated name of day
|
||||
defp format_modifiers("a" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
datetime
|
||||
|> Date.day_of_week()
|
||||
|> format_options.abbreviated_day_of_week_names.()
|
||||
|> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Full name of day
|
||||
defp format_modifiers("A" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
datetime
|
||||
|> Date.day_of_week()
|
||||
|> format_options.day_of_week_names.()
|
||||
|> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Abbreviated month name
|
||||
defp format_modifiers("b" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
datetime.month
|
||||
|> format_options.abbreviated_month_names.()
|
||||
|> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Full month name
|
||||
defp format_modifiers("B" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.month |> format_options.month_names.() |> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Preferred date+time representation
|
||||
defp format_modifiers(
|
||||
"c" <> _rest,
|
||||
_width,
|
||||
_pad,
|
||||
_datetime,
|
||||
%{preferred_datetime_invoked: true},
|
||||
_acc
|
||||
) do
|
||||
raise ArgumentError,
|
||||
"tried to format preferred_datetime within another preferred_datetime format"
|
||||
end
|
||||
|
||||
defp format_modifiers("c" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
format_options.preferred_datetime
|
||||
|> parse(datetime, %{format_options | preferred_datetime_invoked: true}, [])
|
||||
|> pad_preferred(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Day of the month
|
||||
defp format_modifiers("d" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.day |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Microseconds
|
||||
defp format_modifiers("f" <> rest, _width, _pad, datetime, format_options, acc) do
|
||||
{microsecond, precision} = datetime.microsecond
|
||||
|
||||
result =
|
||||
microsecond
|
||||
|> Integer.to_string()
|
||||
|> String.pad_leading(6, "0")
|
||||
|> binary_part(0, max(precision, 1))
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Hour using a 24-hour clock
|
||||
defp format_modifiers("H" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.hour |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Hour using a 12-hour clock
|
||||
defp format_modifiers("I" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = (rem(datetime.hour() + 23, 12) + 1) |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Day of the year
|
||||
defp format_modifiers("j" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime |> Date.day_of_year() |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Month
|
||||
defp format_modifiers("m" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.month |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Minute
|
||||
defp format_modifiers("M" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.minute |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# "AM" or "PM" (noon is "PM", midnight as "AM")
|
||||
defp format_modifiers("p" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.hour |> am_pm(format_options) |> String.upcase() |> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# "am" or "pm" (noon is "pm", midnight as "am")
|
||||
defp format_modifiers("P" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
datetime.hour
|
||||
|> am_pm(format_options)
|
||||
|> String.downcase()
|
||||
|> pad_leading(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Quarter
|
||||
defp format_modifiers("q" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime |> Date.quarter_of_year() |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Second
|
||||
defp format_modifiers("S" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.second |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Day of the week
|
||||
defp format_modifiers("u" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime |> Date.day_of_week() |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Preferred date (without time) representation
|
||||
defp format_modifiers(
|
||||
"x" <> _rest,
|
||||
_width,
|
||||
_pad,
|
||||
_datetime,
|
||||
%{preferred_date_invoked: true},
|
||||
_acc
|
||||
) do
|
||||
raise ArgumentError,
|
||||
"tried to format preferred_date within another preferred_date format"
|
||||
end
|
||||
|
||||
defp format_modifiers("x" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
format_options.preferred_date
|
||||
|> parse(datetime, %{format_options | preferred_date_invoked: true}, [])
|
||||
|> pad_preferred(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Preferred time (without date) representation
|
||||
defp format_modifiers(
|
||||
"X" <> _rest,
|
||||
_width,
|
||||
_pad,
|
||||
_datetime,
|
||||
%{preferred_time_invoked: true},
|
||||
_acc
|
||||
) do
|
||||
raise ArgumentError,
|
||||
"tried to format preferred_time within another preferred_time format"
|
||||
end
|
||||
|
||||
defp format_modifiers("X" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result =
|
||||
format_options.preferred_time
|
||||
|> parse(datetime, %{format_options | preferred_time_invoked: true}, [])
|
||||
|> pad_preferred(width, pad)
|
||||
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Year as 2-digits
|
||||
defp format_modifiers("y" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.year |> rem(100) |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# Year
|
||||
defp format_modifiers("Y" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime.year |> Integer.to_string() |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
# +hhmm/-hhmm time zone offset from UTC (empty string if naive)
|
||||
defp format_modifiers(
|
||||
"z" <> rest,
|
||||
width,
|
||||
pad,
|
||||
datetime = %{utc_offset: utc_offset, std_offset: std_offset},
|
||||
format_options,
|
||||
acc
|
||||
) do
|
||||
absolute_offset = abs(utc_offset + std_offset)
|
||||
|
||||
offset_number =
|
||||
Integer.to_string(div(absolute_offset, 3600) * 100 + rem(div(absolute_offset, 60), 60))
|
||||
|
||||
sign = if utc_offset + std_offset >= 0, do: "+", else: "-"
|
||||
result = "#{sign}#{pad_leading(offset_number, width, pad)}"
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
defp format_modifiers("z" <> rest, _width, _pad, datetime, format_options, acc) do
|
||||
parse(rest, datetime, format_options, ["" | acc])
|
||||
end
|
||||
|
||||
# Time zone abbreviation (empty string if naive)
|
||||
defp format_modifiers("Z" <> rest, width, pad, datetime, format_options, acc) do
|
||||
result = datetime |> Map.get(:zone_abbr, "") |> pad_leading(width, pad)
|
||||
parse(rest, datetime, format_options, [result | acc])
|
||||
end
|
||||
|
||||
defp format_modifiers(rest, _width, _pad, _datetime, _format_options, _acc) do
|
||||
{next, _rest} = String.next_grapheme(rest) || {"", ""}
|
||||
raise ArgumentError, "invalid strftime format: %#{next}"
|
||||
end
|
||||
|
||||
defp pad_preferred(result, width, pad) when length(result) < width do
|
||||
pad_preferred([pad | result], width, pad)
|
||||
end
|
||||
|
||||
defp pad_preferred(result, _width, _pad), do: result
|
||||
|
||||
defp pad_leading(string, count, padding) do
|
||||
to_pad = count - byte_size(string)
|
||||
if to_pad > 0, do: do_pad_leading(to_pad, padding, string), else: string
|
||||
end
|
||||
|
||||
defp do_pad_leading(0, _, acc), do: acc
|
||||
|
||||
defp do_pad_leading(count, padding, acc),
|
||||
do: do_pad_leading(count - 1, padding, [padding | acc])
|
||||
|
||||
defp options(user_options) do
|
||||
default_options = %{
|
||||
preferred_date: "%Y-%m-%d",
|
||||
preferred_time: "%H:%M:%S",
|
||||
preferred_datetime: "%Y-%m-%d %H:%M:%S",
|
||||
am_pm_names: fn
|
||||
:am -> "am"
|
||||
:pm -> "pm"
|
||||
end,
|
||||
month_names: fn month ->
|
||||
{"January", "February", "March", "April", "May", "June", "July", "August", "September",
|
||||
"October", "November", "December"}
|
||||
|> elem(month - 1)
|
||||
end,
|
||||
day_of_week_names: fn day_of_week ->
|
||||
{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
|
||||
|> elem(day_of_week - 1)
|
||||
end,
|
||||
abbreviated_month_names: fn month ->
|
||||
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
|
||||
|> elem(month - 1)
|
||||
end,
|
||||
abbreviated_day_of_week_names: fn day_of_week ->
|
||||
{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"} |> elem(day_of_week - 1)
|
||||
end,
|
||||
preferred_datetime_invoked: false,
|
||||
preferred_date_invoked: false,
|
||||
preferred_time_invoked: false
|
||||
}
|
||||
|
||||
Enum.reduce(user_options, default_options, fn {key, value}, acc ->
|
||||
if Map.has_key?(acc, key) do
|
||||
%{acc | key => value}
|
||||
else
|
||||
raise ArgumentError, "unknown option #{inspect(key)} given to Calendar.strftime/3"
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
+58
-314
@@ -83,70 +83,28 @@ defmodule Date do
|
||||
366
|
||||
iex> Enum.member?(range, ~D[2001-02-01])
|
||||
true
|
||||
iex> Enum.take(range, 3)
|
||||
[~D[2001-01-01], ~D[2001-01-02], ~D[2001-01-03]]
|
||||
iex> Enum.reduce(range, 0, fn _date, acc -> acc - 1 end)
|
||||
-366
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec range(Calendar.date(), Calendar.date()) :: Date.Range.t()
|
||||
def range(%{calendar: calendar} = first, %{calendar: calendar} = last) do
|
||||
@spec range(Date.t(), Date.t()) :: Date.Range.t()
|
||||
def range(%Date{calendar: calendar} = first, %Date{calendar: calendar} = last) do
|
||||
{first_days, _} = to_iso_days(first)
|
||||
{last_days, _} = to_iso_days(last)
|
||||
# TODO: Deprecate inferring a range with a step of -1 on Elixir v1.16
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
range(first, first_days, last, last_days, calendar, step)
|
||||
end
|
||||
|
||||
def range(%{calendar: _, year: _, month: _, day: _}, %{calendar: _, year: _, month: _, day: _}) do
|
||||
raise ArgumentError, "both dates must have matching calendars"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a range of dates with a step.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> range = Date.range(~D[2001-01-01], ~D[2002-01-01], 2)
|
||||
iex> range
|
||||
#DateRange<~D[2001-01-01], ~D[2002-01-01], 2>
|
||||
iex> Enum.count(range)
|
||||
183
|
||||
iex> Enum.member?(range, ~D[2001-01-03])
|
||||
true
|
||||
iex> Enum.take(range, 3)
|
||||
[~D[2001-01-01], ~D[2001-01-03], ~D[2001-01-05]]
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec range(Calendar.date(), Calendar.date(), step :: pos_integer | neg_integer) ::
|
||||
Date.Range.t()
|
||||
def range(%{calendar: calendar} = first, %{calendar: calendar} = last, step)
|
||||
when is_integer(step) and step != 0 do
|
||||
{first_days, _} = to_iso_days(first)
|
||||
{last_days, _} = to_iso_days(last)
|
||||
range(first, first_days, last, last_days, calendar, step)
|
||||
end
|
||||
|
||||
def range(
|
||||
%{calendar: _, year: _, month: _, day: _} = first,
|
||||
%{calendar: _, year: _, month: _, day: _} = last,
|
||||
step
|
||||
) do
|
||||
raise ArgumentError,
|
||||
"both dates must have matching calendar and the step must be a " <>
|
||||
"non-zero integer, got: #{inspect(first)}, #{inspect(last)}, #{step}"
|
||||
end
|
||||
|
||||
defp range(first, first_days, last, last_days, calendar, step) do
|
||||
%Date.Range{
|
||||
first: %Date{calendar: calendar, year: first.year, month: first.month, day: first.day},
|
||||
last: %Date{calendar: calendar, year: last.year, month: last.month, day: last.day},
|
||||
first: first,
|
||||
last: last,
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
step: step
|
||||
last_in_iso_days: last_days
|
||||
}
|
||||
end
|
||||
|
||||
def range(%Date{}, %Date{}) do
|
||||
raise ArgumentError, "both dates must have matching calendars"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current date in UTC.
|
||||
|
||||
@@ -266,33 +224,6 @@ defmodule Date do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a new ISO date.
|
||||
|
||||
Expects all values to be integers. Returns `date` if each
|
||||
entry fits its appropriate range, raises if the date is invalid.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Date.new!(2000, 1, 1)
|
||||
~D[2000-01-01]
|
||||
iex> Date.new!(2000, 13, 1)
|
||||
** (ArgumentError) cannot build date, reason: :invalid_date
|
||||
iex> Date.new!(2000, 2, 29)
|
||||
~D[2000-02-29]
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec new!(Calendar.year(), Calendar.month(), Calendar.day(), Calendar.calendar()) :: t
|
||||
def new!(year, month, day, calendar \\ Calendar.ISO) do
|
||||
case new(year, month, day, calendar) do
|
||||
{:ok, value} ->
|
||||
value
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError, "cannot build date, reason: #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given date to a string according to its calendar.
|
||||
|
||||
@@ -315,7 +246,7 @@ defmodule Date do
|
||||
|
||||
@doc """
|
||||
Parses the extended "Dates" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
The year parsed by this function is limited to four digits.
|
||||
|
||||
@@ -332,15 +263,36 @@ defmodule Date do
|
||||
|
||||
"""
|
||||
@spec from_iso8601(String.t(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
|
||||
def from_iso8601(string, calendar \\ Calendar.ISO) do
|
||||
with {:ok, {year, month, day}} <- Calendar.ISO.parse_date(string) do
|
||||
convert(%Date{year: year, month: month, day: day}, calendar)
|
||||
def from_iso8601(string, calendar \\ Calendar.ISO)
|
||||
|
||||
def from_iso8601(<<?-, rest::binary>>, calendar) do
|
||||
with {:ok, %{year: year} = date} <- raw_from_iso8601(rest, calendar) do
|
||||
{:ok, %{date | year: -year}}
|
||||
end
|
||||
end
|
||||
|
||||
def from_iso8601(<<rest::binary>>, calendar) do
|
||||
raw_from_iso8601(rest, calendar)
|
||||
end
|
||||
|
||||
[match_date, guard_date, read_date] = Calendar.ISO.__match_date__()
|
||||
|
||||
defp raw_from_iso8601(string, calendar) do
|
||||
with unquote(match_date) <- string,
|
||||
true <- unquote(guard_date) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
|
||||
with {:ok, date} <- new(year, month, day, Calendar.ISO) do
|
||||
convert(date, calendar)
|
||||
end
|
||||
else
|
||||
_ -> {:error, :invalid_format}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses the extended "Dates" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Raises if the format is invalid.
|
||||
|
||||
@@ -365,7 +317,7 @@ defmodule Date do
|
||||
|
||||
@doc """
|
||||
Converts the given `date` to
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
By default, `Date.to_iso8601/2` returns dates formatted in the "extended"
|
||||
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
|
||||
@@ -391,7 +343,7 @@ defmodule Date do
|
||||
|
||||
def to_iso8601(%{calendar: Calendar.ISO} = date, format) when format in [:basic, :extended] do
|
||||
%{year: year, month: month, day: day} = date
|
||||
Calendar.ISO.date_to_string(year, month, day, format)
|
||||
Calendar.ISO.date_to_iso8601(year, month, day, format)
|
||||
end
|
||||
|
||||
def to_iso8601(%{calendar: _} = date, format) when format in [:basic, :extended] do
|
||||
@@ -467,45 +419,6 @@ defmodule Date do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a number of gregorian days to a `Date` struct.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Date.from_gregorian_days(1)
|
||||
~D[0000-01-02]
|
||||
iex> Date.from_gregorian_days(730_485)
|
||||
~D[2000-01-01]
|
||||
iex> Date.from_gregorian_days(-1)
|
||||
~D[-0001-12-31]
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec from_gregorian_days(integer(), Calendar.calendar()) :: t
|
||||
def from_gregorian_days(days, calendar \\ Calendar.ISO) when is_integer(days) do
|
||||
from_iso_days({days, 0}, calendar)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a `date` struct to a number of gregorian days.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Date.to_gregorian_days(~D[0000-01-02])
|
||||
1
|
||||
iex> Date.to_gregorian_days(~D[2000-01-01])
|
||||
730_485
|
||||
iex> Date.to_gregorian_days(~N[2000-01-01 00:00:00])
|
||||
730_485
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec to_gregorian_days(Calendar.date()) :: integer()
|
||||
def to_gregorian_days(date) do
|
||||
{days, _} = to_iso_days(date)
|
||||
days
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compares two date structs.
|
||||
|
||||
@@ -542,8 +455,8 @@ defmodule Date do
|
||||
end
|
||||
end
|
||||
|
||||
def compare(%{calendar: calendar1} = date1, %{calendar: calendar2} = date2) do
|
||||
if Calendar.compatible_calendars?(calendar1, calendar2) do
|
||||
def compare(date1, date2) do
|
||||
if Calendar.compatible_calendars?(date1.calendar, date2.calendar) do
|
||||
case {to_iso_days(date1), to_iso_days(date2)} do
|
||||
{first, second} when first > second -> :gt
|
||||
{first, second} when first < second -> :lt
|
||||
@@ -701,12 +614,11 @@ defmodule Date do
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def to_iso_days(%{calendar: Calendar.ISO, year: year, month: month, day: day}) do
|
||||
defp to_iso_days(%{calendar: Calendar.ISO, year: year, month: month, day: day}) do
|
||||
{Calendar.ISO.date_to_iso_days(year, month, day), {0, 86_400_000_000}}
|
||||
end
|
||||
|
||||
def to_iso_days(%{calendar: calendar, year: year, month: month, day: day}) do
|
||||
defp to_iso_days(%{calendar: calendar, year: year, month: month, day: day}) do
|
||||
calendar.naive_datetime_to_iso_days(year, month, day, 0, 0, 0, {0, 0})
|
||||
end
|
||||
|
||||
@@ -727,11 +639,6 @@ defmodule Date do
|
||||
calendar (the default), it is an integer from 1 to 7, where
|
||||
1 is Monday and 7 is Sunday.
|
||||
|
||||
An optional `starting_on` value may be supplied, which
|
||||
configures the weekday the week starts on. The default value
|
||||
for it is `:default`, which translates to `:monday` for the
|
||||
built-in ISO calendar. Any other weekday may be given to.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Date.day_of_week(~D[2016-10-31])
|
||||
@@ -743,131 +650,13 @@ defmodule Date do
|
||||
iex> Date.day_of_week(~D[-0015-10-30])
|
||||
3
|
||||
|
||||
iex> Date.day_of_week(~D[2016-10-31], :sunday)
|
||||
2
|
||||
iex> Date.day_of_week(~D[2016-11-01], :sunday)
|
||||
3
|
||||
iex> Date.day_of_week(~N[2016-11-01 01:23:45], :sunday)
|
||||
3
|
||||
iex> Date.day_of_week(~D[-0015-10-30], :sunday)
|
||||
4
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec day_of_week(Calendar.date(), starting_on :: :default | atom) :: Calendar.day_of_week()
|
||||
def day_of_week(date, starting_on \\ :default)
|
||||
@spec day_of_week(Calendar.date()) :: Calendar.day()
|
||||
def day_of_week(date)
|
||||
|
||||
def day_of_week(%{calendar: calendar, year: year, month: month, day: day}, starting_on) do
|
||||
{day_of_week, _first, _last} = calendar.day_of_week(year, month, day, starting_on)
|
||||
day_of_week
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates a date that is the first day of the week for the given `date`.
|
||||
|
||||
If the day is already the first day of the week, it returns the
|
||||
day itself. For the built-in ISO calendar, the week starts on Monday.
|
||||
A weekday rather than `:default` can be given as `starting_on`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Date.beginning_of_week(~D[2020-07-11])
|
||||
~D[2020-07-06]
|
||||
iex> Date.beginning_of_week(~D[2020-07-06])
|
||||
~D[2020-07-06]
|
||||
iex> Date.beginning_of_week(~D[2020-07-11], :sunday)
|
||||
~D[2020-07-05]
|
||||
iex> Date.beginning_of_week(~D[2020-07-11], :saturday)
|
||||
~D[2020-07-11]
|
||||
iex> Date.beginning_of_week(~N[2020-07-11 01:23:45])
|
||||
~D[2020-07-06]
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec beginning_of_week(Calendar.date(), starting_on :: :default | atom) :: Date.t()
|
||||
def beginning_of_week(date, starting_on \\ :default)
|
||||
|
||||
def beginning_of_week(%{calendar: Calendar.ISO} = date, starting_on) do
|
||||
%{year: year, month: month, day: day} = date
|
||||
iso_days = Calendar.ISO.date_to_iso_days(year, month, day)
|
||||
|
||||
{year, month, day} =
|
||||
case Calendar.ISO.iso_days_to_day_of_week(iso_days, starting_on) do
|
||||
1 ->
|
||||
{year, month, day}
|
||||
|
||||
day_of_week ->
|
||||
Calendar.ISO.date_from_iso_days(iso_days - day_of_week + 1)
|
||||
end
|
||||
|
||||
%Date{calendar: Calendar.ISO, year: year, month: month, day: day}
|
||||
end
|
||||
|
||||
def beginning_of_week(%{calendar: calendar} = date, starting_on) do
|
||||
%{year: year, month: month, day: day} = date
|
||||
|
||||
case calendar.day_of_week(year, month, day, starting_on) do
|
||||
{day_of_week, day_of_week, _} ->
|
||||
%Date{calendar: calendar, year: year, month: month, day: day}
|
||||
|
||||
{day_of_week, first_day_of_week, _} ->
|
||||
add(date, -(day_of_week - first_day_of_week))
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates a date that is the last day of the week for the given `date`.
|
||||
|
||||
If the day is already the last day of the week, it returns the
|
||||
day itself. For the built-in ISO calendar, the week ends on Sunday.
|
||||
A weekday rather than `:default` can be given as `starting_on`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Date.end_of_week(~D[2020-07-11])
|
||||
~D[2020-07-12]
|
||||
iex> Date.end_of_week(~D[2020-07-05])
|
||||
~D[2020-07-05]
|
||||
iex> Date.end_of_week(~D[2020-07-06], :sunday)
|
||||
~D[2020-07-11]
|
||||
iex> Date.end_of_week(~D[2020-07-06], :sunday)
|
||||
~D[2020-07-11]
|
||||
iex> Date.end_of_week(~D[2020-07-06], :saturday)
|
||||
~D[2020-07-10]
|
||||
iex> Date.end_of_week(~N[2020-07-11 01:23:45])
|
||||
~D[2020-07-12]
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec end_of_week(Calendar.date(), starting_on :: :default | atom) :: Date.t()
|
||||
def end_of_week(date, starting_on \\ :default)
|
||||
|
||||
def end_of_week(%{calendar: Calendar.ISO} = date, starting_on) do
|
||||
%{year: year, month: month, day: day} = date
|
||||
iso_days = Calendar.ISO.date_to_iso_days(year, month, day)
|
||||
|
||||
{year, month, day} =
|
||||
case Calendar.ISO.iso_days_to_day_of_week(iso_days, starting_on) do
|
||||
7 ->
|
||||
{year, month, day}
|
||||
|
||||
day_of_week ->
|
||||
Calendar.ISO.date_from_iso_days(iso_days + 7 - day_of_week)
|
||||
end
|
||||
|
||||
%Date{calendar: Calendar.ISO, year: year, month: month, day: day}
|
||||
end
|
||||
|
||||
def end_of_week(%{calendar: calendar} = date, starting_on) do
|
||||
%{year: year, month: month, day: day} = date
|
||||
|
||||
case calendar.day_of_week(year, month, day, starting_on) do
|
||||
{day_of_week, _, day_of_week} ->
|
||||
%Date{calendar: calendar, year: year, month: month, day: day}
|
||||
|
||||
{day_of_week, _, last_day_of_week} ->
|
||||
add(date, last_day_of_week - day_of_week)
|
||||
end
|
||||
def day_of_week(%{calendar: calendar, year: year, month: month, day: day}) do
|
||||
calendar.day_of_week(year, month, day)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -943,14 +732,8 @@ defmodule Date do
|
||||
@spec year_of_era(Calendar.date()) :: {Calendar.year(), non_neg_integer()}
|
||||
def year_of_era(date)
|
||||
|
||||
def year_of_era(%{calendar: calendar, year: year, month: month, day: day}) do
|
||||
# TODO: Remove me on 1.17
|
||||
# The behaviour implementation already warns on missing callback.
|
||||
if function_exported?(calendar, :year_of_era, 3) do
|
||||
calendar.year_of_era(year, month, day)
|
||||
else
|
||||
calendar.year_of_era(year)
|
||||
end
|
||||
def year_of_era(%{calendar: calendar, year: year}) do
|
||||
calendar.year_of_era(year)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -962,11 +745,10 @@ defmodule Date do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Date.day_of_era(~D[0001-01-01])
|
||||
{1, 1}
|
||||
|
||||
iex> Date.day_of_era(~D[0000-12-31])
|
||||
{1, 0}
|
||||
iex> Date.day_of_era(~D[0001-01-01])
|
||||
{1, 1}
|
||||
iex> Date.day_of_era(~D[0000-12-31])
|
||||
{1, 0}
|
||||
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@@ -977,45 +759,6 @@ defmodule Date do
|
||||
calendar.day_of_era(year, month, day)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates a date that is the first day of the month for the given `date`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Date.beginning_of_month(~D[2000-01-31])
|
||||
~D[2000-01-01]
|
||||
iex> Date.beginning_of_month(~D[2000-01-01])
|
||||
~D[2000-01-01]
|
||||
iex> Date.beginning_of_month(~N[2000-01-31 01:23:45])
|
||||
~D[2000-01-01]
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec beginning_of_month(Calendar.date()) :: t()
|
||||
def beginning_of_month(%{year: year, month: month, calendar: calendar}) do
|
||||
%Date{year: year, month: month, day: 1, calendar: calendar}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates a date that is the last day of the month for the given `date`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Date.end_of_month(~D[2000-01-01])
|
||||
~D[2000-01-31]
|
||||
iex> Date.end_of_month(~D[2000-01-31])
|
||||
~D[2000-01-31]
|
||||
iex> Date.end_of_month(~N[2000-01-01 01:23:45])
|
||||
~D[2000-01-31]
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec end_of_month(Calendar.date()) :: t()
|
||||
def end_of_month(%{year: year, month: month, calendar: calendar} = date) do
|
||||
day = Date.days_in_month(date)
|
||||
%Date{year: year, month: month, day: day, calendar: calendar}
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defimpl String.Chars do
|
||||
@@ -1025,11 +768,12 @@ defmodule Date do
|
||||
end
|
||||
|
||||
defimpl Inspect do
|
||||
def inspect(%{calendar: calendar, year: year, month: month, day: day}, _) do
|
||||
"~D[" <> calendar.date_to_string(year, month, day) <> suffix(calendar) <> "]"
|
||||
def inspect(%{calendar: Calendar.ISO, year: year, month: month, day: day}, _) do
|
||||
"~D[" <> Calendar.ISO.date_to_string(year, month, day) <> "]"
|
||||
end
|
||||
|
||||
defp suffix(Calendar.ISO), do: ""
|
||||
defp suffix(calendar), do: " " <> inspect(calendar)
|
||||
def inspect(date, opts) do
|
||||
Inspect.Any.inspect(date, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,13 +2,12 @@ defmodule Date.Range do
|
||||
@moduledoc """
|
||||
Returns an inclusive range between dates.
|
||||
|
||||
Ranges must be created with the `Date.range/2` or `Date.range/3` function.
|
||||
Ranges must be created with the `Date.range/2` function.
|
||||
|
||||
The following fields are public:
|
||||
|
||||
* `:first` - the initial date on the range
|
||||
* `:last` - the last date on the range
|
||||
* `:step` - (since v1.12.0) the step
|
||||
|
||||
The remaining fields are private and should not be accessed.
|
||||
"""
|
||||
@@ -17,133 +16,98 @@ defmodule Date.Range do
|
||||
first: Date.t(),
|
||||
last: Date.t(),
|
||||
first_in_iso_days: iso_days(),
|
||||
last_in_iso_days: iso_days(),
|
||||
step: pos_integer | neg_integer
|
||||
last_in_iso_days: iso_days()
|
||||
}
|
||||
|
||||
@typep iso_days() :: Calendar.iso_days()
|
||||
|
||||
@enforce_keys [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
|
||||
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
|
||||
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days]
|
||||
|
||||
defimpl Enumerable do
|
||||
def member?(
|
||||
%Date.Range{
|
||||
first: %{calendar: calendar},
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
step: step
|
||||
} = range,
|
||||
%Date{calendar: calendar} = date
|
||||
) do
|
||||
{days, _} = Date.to_iso_days(date)
|
||||
def member?(%{first: %{calendar: calendar}} = range, %Date{calendar: calendar} = date) do
|
||||
%{
|
||||
first: first,
|
||||
last: last,
|
||||
first_in_iso_days: first_in_iso_days,
|
||||
last_in_iso_days: last_in_iso_days
|
||||
} = range
|
||||
|
||||
cond do
|
||||
empty?(range) ->
|
||||
{:ok, false}
|
||||
%{year: first_year, month: first_month, day: first_day} = first
|
||||
%{year: last_year, month: last_month, day: last_day} = last
|
||||
%{year: year, month: month, day: day} = date
|
||||
first = {first_year, first_month, first_day}
|
||||
last = {last_year, last_month, last_day}
|
||||
date = {year, month, day}
|
||||
|
||||
first_days <= last_days ->
|
||||
{:ok, first_days <= days and days <= last_days and rem(days - first_days, step) == 0}
|
||||
|
||||
true ->
|
||||
{:ok, last_days <= days and days <= first_days and rem(days - first_days, step) == 0}
|
||||
if first_in_iso_days <= last_in_iso_days do
|
||||
{:ok, date >= first and date <= last}
|
||||
else
|
||||
{:ok, date >= last and date <= first}
|
||||
end
|
||||
end
|
||||
|
||||
def member?(%Date.Range{step: _}, _) do
|
||||
def member?(_, _) do
|
||||
{:ok, false}
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def member?(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range,
|
||||
date
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
member?(Map.put(date_range, :step, step), date)
|
||||
def count(%{first_in_iso_days: first, last_in_iso_days: last}) do
|
||||
{:ok, abs(first - last) + 1}
|
||||
end
|
||||
|
||||
def count(range) do
|
||||
{:ok, size(range)}
|
||||
def slice(range) do
|
||||
%{
|
||||
first_in_iso_days: first,
|
||||
last_in_iso_days: last,
|
||||
first: %{calendar: calendar}
|
||||
} = range
|
||||
|
||||
if first <= last do
|
||||
{:ok, last - first + 1, &slice_asc(first + &1, &2, calendar)}
|
||||
else
|
||||
{:ok, first - last + 1, &slice_desc(first - &1, &2, calendar)}
|
||||
end
|
||||
end
|
||||
|
||||
def slice(
|
||||
%Date.Range{
|
||||
first_in_iso_days: first,
|
||||
first: %{calendar: calendar},
|
||||
step: step
|
||||
} = range
|
||||
) do
|
||||
{:ok, size(range), &slice(first + &1 * step, step, &2, calendar)}
|
||||
defp slice_asc(current, 1, calendar), do: [date_from_iso_days(current, calendar)]
|
||||
|
||||
defp slice_asc(current, remaining, calendar) do
|
||||
[date_from_iso_days(current, calendar) | slice_asc(current + 1, remaining - 1, calendar)]
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def slice(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
slice(Map.put(date_range, :step, step))
|
||||
defp slice_desc(current, 1, calendar), do: [date_from_iso_days(current, calendar)]
|
||||
|
||||
defp slice_desc(current, remaining, calendar) do
|
||||
[date_from_iso_days(current, calendar) | slice_desc(current - 1, remaining - 1, calendar)]
|
||||
end
|
||||
|
||||
defp slice(current, _step, 1, calendar) do
|
||||
[date_from_iso_days(current, calendar)]
|
||||
def reduce(range, acc, fun) do
|
||||
%{
|
||||
first_in_iso_days: first_in_iso_days,
|
||||
last_in_iso_days: last_in_iso_days,
|
||||
first: %{calendar: calendar}
|
||||
} = range
|
||||
|
||||
up? = first_in_iso_days <= last_in_iso_days
|
||||
reduce(first_in_iso_days, last_in_iso_days, acc, fun, calendar, up?)
|
||||
end
|
||||
|
||||
defp slice(current, step, remaining, calendar) do
|
||||
[
|
||||
date_from_iso_days(current, calendar)
|
||||
| slice(current + step, step, remaining - 1, calendar)
|
||||
]
|
||||
end
|
||||
|
||||
def reduce(
|
||||
%Date.Range{
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
first: %{calendar: calendar},
|
||||
step: step
|
||||
},
|
||||
acc,
|
||||
fun
|
||||
) do
|
||||
reduce(first_days, last_days, acc, fun, step, calendar)
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def reduce(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range,
|
||||
acc,
|
||||
fun
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
reduce(Map.put(date_range, :step, step), acc, fun)
|
||||
end
|
||||
|
||||
defp reduce(_first_days, _last_days, {:halt, acc}, _fun, _step, _calendar) do
|
||||
defp reduce(_x, _y, {:halt, acc}, _fun, _calendar, _up?) do
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp reduce(first_days, last_days, {:suspend, acc}, fun, step, calendar) do
|
||||
{:suspended, acc, &reduce(first_days, last_days, &1, fun, step, calendar)}
|
||||
defp reduce(x, y, {:suspend, acc}, fun, calendar, up?) do
|
||||
{:suspended, acc, &reduce(x, y, &1, fun, calendar, up?)}
|
||||
end
|
||||
|
||||
defp reduce(first_days, last_days, {:cont, acc}, fun, step, calendar)
|
||||
when step > 0 and first_days <= last_days
|
||||
when step < 0 and first_days >= last_days do
|
||||
reduce(
|
||||
first_days + step,
|
||||
last_days,
|
||||
fun.(date_from_iso_days(first_days, calendar), acc),
|
||||
fun,
|
||||
step,
|
||||
calendar
|
||||
)
|
||||
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = true) when x <= y do
|
||||
reduce(x + 1, y, fun.(date_from_iso_days(x, calendar), acc), fun, calendar, up?)
|
||||
end
|
||||
|
||||
defp reduce(_, _, {:cont, acc}, _fun, _step, _calendar) do
|
||||
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = false) when x >= y do
|
||||
reduce(x - 1, y, fun.(date_from_iso_days(x, calendar), acc), fun, calendar, up?)
|
||||
end
|
||||
|
||||
defp reduce(_, _, {:cont, acc}, _fun, _calendar, _up) do
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
@@ -158,70 +122,11 @@ defmodule Date.Range do
|
||||
|
||||
%Date{year: year, month: month, day: day, calendar: calendar}
|
||||
end
|
||||
|
||||
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
|
||||
when step > 0 and first_days > last_days,
|
||||
do: 0
|
||||
|
||||
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
|
||||
when step < 0 and first_days < last_days,
|
||||
do: 0
|
||||
|
||||
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step}),
|
||||
do: abs(div(last_days - first_days, step)) + 1
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
defp size(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
size(Map.put(date_range, :step, step))
|
||||
end
|
||||
|
||||
defp empty?(%Date.Range{
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
step: step
|
||||
})
|
||||
when step > 0 and first_days > last_days,
|
||||
do: true
|
||||
|
||||
defp empty?(%Date.Range{
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
step: step
|
||||
})
|
||||
when step < 0 and first_days < last_days,
|
||||
do: true
|
||||
|
||||
defp empty?(%Date.Range{step: _}), do: false
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
defp empty?(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
empty?(Map.put(date_range, :step, step))
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect do
|
||||
import Kernel, except: [inspect: 2]
|
||||
|
||||
def inspect(%Date.Range{first: first, last: last, step: 1}, _) do
|
||||
def inspect(%Date.Range{first: first, last: last}, _) do
|
||||
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ">"
|
||||
end
|
||||
|
||||
def inspect(%Date.Range{first: first, last: last, step: step}, _) do
|
||||
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ", #{step}>"
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def inspect(%{__struct__: Date.Range, first: first, last: last} = date_range, opts) do
|
||||
step = if first <= last, do: 1, else: -1
|
||||
inspect(Map.put(date_range, :step, step), opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+174
-565
@@ -2,14 +2,11 @@ defmodule DateTime do
|
||||
@moduledoc """
|
||||
A datetime implementation with a time zone.
|
||||
|
||||
This datetime can be seen as a snapshot of a date and time
|
||||
at a given time zone. For such purposes, it also includes both
|
||||
UTC and Standard offsets, as well as the zone abbreviation
|
||||
field used exclusively for formatting purposes. Note future
|
||||
datetimes are not necessarily guaranteed to exist, as time
|
||||
zones may change any time in the future due to geopolitical
|
||||
reasons. See the "Datetimes as snapshots" section for more
|
||||
information.
|
||||
This datetime can be seen as an ephemeral snapshot
|
||||
of a datetime at a given time zone. For such purposes,
|
||||
it also includes both UTC and Standard offsets, as
|
||||
well as the zone abbreviation field used exclusively
|
||||
for formatting purposes.
|
||||
|
||||
Remember, comparisons in Elixir using `==/2`, `>/2`, `</2` and friends
|
||||
are structural and based on the DateTime struct fields. For proper
|
||||
@@ -28,72 +25,12 @@ defmodule DateTime do
|
||||
datetimes and returns `{:error, :utc_only_time_zone_database}`
|
||||
for any other time zone.
|
||||
|
||||
Other time zone databases can also be configured. For example,
|
||||
two of the available options are:
|
||||
Other time zone databases (including ones provided by packages)
|
||||
can be configure as default either via configuration:
|
||||
|
||||
* [`tz`](https://hexdocs.pm/tz/)
|
||||
* [`tzdata`](https://hexdocs.pm/tzdata/)
|
||||
config :elixir, :time_zone_database, CustomTimeZoneDatabase
|
||||
|
||||
To use them, first make sure it is added as a dependency in `mix.exs`.
|
||||
It can then be configured either via configuration:
|
||||
|
||||
config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase
|
||||
|
||||
or by calling `Calendar.put_time_zone_database/1`:
|
||||
|
||||
Calendar.put_time_zone_database(Tzdata.TimeZoneDatabase)
|
||||
|
||||
See the proper names in the library installation instructions.
|
||||
|
||||
## Datetimes as snapshots
|
||||
|
||||
In the first section, we described datetimes as a "snapshot of
|
||||
a date and time at a given time zone". To understand precisely
|
||||
what we mean, let's see an example.
|
||||
|
||||
Imagine someone in Poland wants to schedule a meeting with someone
|
||||
in Brazil in the next year. The meeting will happen at 2:30 AM
|
||||
in the Polish time zone. At what time will the meeting happen in
|
||||
Brazil?
|
||||
|
||||
You can consult the time zone database today, one year before,
|
||||
using the API in this module and it will give you an answer that
|
||||
is valid right now. However, this answer may not be valid in the
|
||||
future. Why? Because both Brazil and Poland may change their timezone
|
||||
rules, ultimately affecting the result. For example, a country may
|
||||
choose to enter or abandon "Daylight Saving Time", which is a
|
||||
process where we adjust the clock one hour forward or one hour
|
||||
back once per year. Whenener the rules change, the exact instant
|
||||
that 2:30 AM in Polish time will be in Brazil may change.
|
||||
|
||||
In other words, whenever working with future DateTimes, there is
|
||||
no guarantee the results you get will always be correct, until
|
||||
the event actually happens. Therefore, when you ask for a future
|
||||
time, the answers you get are a snapshot that reflects the current
|
||||
state of the time zone rules. For datetimes in the past, this is
|
||||
not a problem, because time zone rules do not change for past
|
||||
events.
|
||||
|
||||
To make matters worse, it may be that the 2:30 AM in Polish time
|
||||
does not actually even exist or it is ambiguous. If a certain
|
||||
time zone observes "Daylight Saving Time", they will move their
|
||||
clock forward once a year. When this happens, there is a whole
|
||||
hour that does not exist. Then, when they move the clock back,
|
||||
there is a certain hour that will happen twice. So if you want
|
||||
to schedule a meeting when this shift back happens, you would
|
||||
need to explicitly say which of the 2:30 AM you precisely mean.
|
||||
Applications that are date and time sensitive, need to take
|
||||
these scenarios into account and correctly communicate them to
|
||||
users.
|
||||
|
||||
The good news is: Elixir contains all of the building blocks
|
||||
necessary to tackle those problems. The default timezone database
|
||||
used by Elixir, `Calendar.UTCOnlyTimeZoneDatabase`, only works
|
||||
with UTC, which does not observe those issues. Once you bring
|
||||
a proper time zone database, the functions in this module will
|
||||
query the database and return the relevant information. For
|
||||
example, look at how `DateTime.new/4` returns different results
|
||||
based on the scenarios described in this section.
|
||||
or by calling `Calendar.put_time_zone_database/1`.
|
||||
"""
|
||||
|
||||
@enforce_keys [:year, :month, :day, :hour, :minute, :second] ++
|
||||
@@ -130,7 +67,6 @@ defmodule DateTime do
|
||||
}
|
||||
|
||||
@unix_days :calendar.date_to_gregorian_days({1970, 1, 1})
|
||||
@seconds_per_day 24 * 60 * 60
|
||||
|
||||
@doc """
|
||||
Returns the current datetime in UTC.
|
||||
@@ -147,164 +83,12 @@ defmodule DateTime do
|
||||
System.os_time() |> from_unix!(:native, calendar)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a datetime from date and time structs.
|
||||
|
||||
It expects a time zone to put the `DateTime` in.
|
||||
If the time zone is not passed it will default to `"Etc/UTC"`,
|
||||
which always succeeds. Otherwise, the `DateTime` is checked against the time zone database
|
||||
given as `time_zone_database`. See the "Time zone database"
|
||||
section in the module documentation.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> DateTime.new(~D[2016-05-24], ~T[13:26:08.003], "Etc/UTC")
|
||||
{:ok, ~U[2016-05-24 13:26:08.003Z]}
|
||||
|
||||
When the datetime is ambiguous - for instance during changing from summer
|
||||
to winter time - the two possible valid datetimes are returned in a tuple.
|
||||
The first datetime is also the one which comes first chronologically, while
|
||||
the second one comes last.
|
||||
|
||||
iex> {:ambiguous, first_dt, second_dt} = DateTime.new(~D[2018-10-28], ~T[02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> first_dt
|
||||
#DateTime<2018-10-28 02:30:00+02:00 CEST Europe/Copenhagen>
|
||||
iex> second_dt
|
||||
#DateTime<2018-10-28 02:30:00+01:00 CET Europe/Copenhagen>
|
||||
|
||||
When there is a gap in wall time - for instance in spring when the clocks are
|
||||
turned forward - the latest valid datetime just before the gap and the first
|
||||
valid datetime just after the gap.
|
||||
|
||||
iex> {:gap, just_before, just_after} = DateTime.new(~D[2019-03-31], ~T[02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> just_before
|
||||
#DateTime<2019-03-31 01:59:59.999999+01:00 CET Europe/Copenhagen>
|
||||
iex> just_after
|
||||
#DateTime<2019-03-31 03:00:00+02:00 CEST Europe/Copenhagen>
|
||||
|
||||
Most of the time there is one, and just one, valid datetime for a certain
|
||||
date and time in a certain time zone.
|
||||
|
||||
iex> {:ok, datetime} = DateTime.new(~D[2018-07-28], ~T[12:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> datetime
|
||||
#DateTime<2018-07-28 12:30:00+02:00 CEST Europe/Copenhagen>
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec new(Date.t(), Time.t(), Calendar.time_zone(), Calendar.time_zone_database()) ::
|
||||
{:ok, t}
|
||||
| {:ambiguous, first_datetime :: t, second_datetime :: t}
|
||||
| {:gap, t, t}
|
||||
| {:error,
|
||||
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
|
||||
def new(
|
||||
date,
|
||||
time,
|
||||
time_zone \\ "Etc/UTC",
|
||||
time_zone_database \\ Calendar.get_time_zone_database()
|
||||
)
|
||||
|
||||
def new(%Date{calendar: calendar} = date, %Time{calendar: calendar} = time, "Etc/UTC", _db) do
|
||||
%{year: year, month: month, day: day} = date
|
||||
%{hour: hour, minute: minute, second: second, microsecond: microsecond} = time
|
||||
|
||||
datetime = %DateTime{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
std_offset: 0,
|
||||
utc_offset: 0,
|
||||
zone_abbr: "UTC",
|
||||
time_zone: "Etc/UTC"
|
||||
}
|
||||
|
||||
{:ok, datetime}
|
||||
end
|
||||
|
||||
def new(date, time, time_zone, time_zone_database) do
|
||||
with {:ok, naive_datetime} <- NaiveDateTime.new(date, time) do
|
||||
from_naive(naive_datetime, time_zone, time_zone_database)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a datetime from date and time structs, raising on errors.
|
||||
|
||||
It expects a time zone to put the `DateTime` in.
|
||||
If the time zone is not passed it will default to `"Etc/UTC"`,
|
||||
which always succeeds. Otherwise, the DateTime is checked against the time zone database
|
||||
given as `time_zone_database`. See the "Time zone database"
|
||||
section in the module documentation.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> DateTime.new!(~D[2016-05-24], ~T[13:26:08.003], "Etc/UTC")
|
||||
~U[2016-05-24 13:26:08.003Z]
|
||||
|
||||
When the datetime is ambiguous - for instance during changing from summer
|
||||
to winter time - an error will be raised.
|
||||
|
||||
iex> DateTime.new!(~D[2018-10-28], ~T[02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
** (ArgumentError) cannot build datetime with ~D[2018-10-28] and ~T[02:30:00] because such instant is ambiguous in time zone Europe/Copenhagen as there is an overlap between #DateTime<2018-10-28 02:30:00+02:00 CEST Europe/Copenhagen> and #DateTime<2018-10-28 02:30:00+01:00 CET Europe/Copenhagen>
|
||||
|
||||
When there is a gap in wall time - for instance in spring when the clocks are
|
||||
turned forward - an error will be raised.
|
||||
|
||||
iex> DateTime.new!(~D[2019-03-31], ~T[02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
** (ArgumentError) cannot build datetime with ~D[2019-03-31] and ~T[02:30:00] because such instant does not exist in time zone Europe/Copenhagen as there is a gap between #DateTime<2019-03-31 01:59:59.999999+01:00 CET Europe/Copenhagen> and #DateTime<2019-03-31 03:00:00+02:00 CEST Europe/Copenhagen>
|
||||
|
||||
Most of the time there is one, and just one, valid datetime for a certain
|
||||
date and time in a certain time zone.
|
||||
|
||||
iex> datetime = DateTime.new!(~D[2018-07-28], ~T[12:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> datetime
|
||||
#DateTime<2018-07-28 12:30:00+02:00 CEST Europe/Copenhagen>
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec new!(Date.t(), Time.t(), Calendar.time_zone(), Calendar.time_zone_database()) :: t
|
||||
def new!(
|
||||
date,
|
||||
time,
|
||||
time_zone \\ "Etc/UTC",
|
||||
time_zone_database \\ Calendar.get_time_zone_database()
|
||||
)
|
||||
|
||||
def new!(date, time, time_zone, time_zone_database) do
|
||||
case new(date, time, time_zone, time_zone_database) do
|
||||
{:ok, datetime} ->
|
||||
datetime
|
||||
|
||||
{:ambiguous, dt1, dt2} ->
|
||||
raise ArgumentError,
|
||||
"cannot build datetime with #{inspect(date)} and #{inspect(time)} because such " <>
|
||||
"instant is ambiguous in time zone #{time_zone} as there is an overlap " <>
|
||||
"between #{inspect(dt1)} and #{inspect(dt2)}"
|
||||
|
||||
{:gap, dt1, dt2} ->
|
||||
raise ArgumentError,
|
||||
"cannot build datetime with #{inspect(date)} and #{inspect(time)} because such " <>
|
||||
"instant does not exist in time zone #{time_zone} as there is a gap " <>
|
||||
"between #{inspect(dt1)} and #{inspect(dt2)}"
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError,
|
||||
"cannot build datetime with #{inspect(date)} and #{inspect(time)}, reason: #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given Unix time to `DateTime`.
|
||||
|
||||
The integer can be given in different unit
|
||||
according to `System.convert_time_unit/3` and it will
|
||||
be converted to microseconds internally. Up to
|
||||
253402300799 seconds is supported.
|
||||
be converted to microseconds internally.
|
||||
|
||||
Unix times are always in UTC and therefore the DateTime
|
||||
will be returned in UTC.
|
||||
@@ -313,32 +97,20 @@ defmodule DateTime do
|
||||
|
||||
iex> {:ok, datetime} = DateTime.from_unix(1_464_096_368)
|
||||
iex> datetime
|
||||
~U[2016-05-24 13:26:08Z]
|
||||
#DateTime<2016-05-24 13:26:08Z>
|
||||
|
||||
iex> {:ok, datetime} = DateTime.from_unix(1_432_560_368_868_569, :microsecond)
|
||||
iex> datetime
|
||||
~U[2015-05-25 13:26:08.868569Z]
|
||||
|
||||
iex> {:ok, datetime} = DateTime.from_unix(253_402_300_799)
|
||||
iex> datetime
|
||||
~U[9999-12-31 23:59:59Z]
|
||||
|
||||
iex> {:error, :invalid_unix_time} = DateTime.from_unix(253_402_300_800)
|
||||
#DateTime<2015-05-25 13:26:08.868569Z>
|
||||
|
||||
The unit can also be an integer as in `t:System.time_unit/0`:
|
||||
|
||||
iex> {:ok, datetime} = DateTime.from_unix(143_256_036_886_856, 1024)
|
||||
iex> datetime
|
||||
~U[6403-03-17 07:05:22.320312Z]
|
||||
|
||||
Negative Unix times are supported up to -377705116800 seconds:
|
||||
|
||||
iex> {:ok, datetime} = DateTime.from_unix(-377_705_116_800)
|
||||
iex> datetime
|
||||
~U[-9999-01-01 00:00:00Z]
|
||||
|
||||
iex> {:error, :invalid_unix_time} = DateTime.from_unix(-377_705_116_801)
|
||||
#DateTime<6403-03-17 07:05:22.320Z>
|
||||
|
||||
Negative Unix times are supported, up to -62167219200 seconds,
|
||||
which is equivalent to "0000-01-01T00:00:00Z" or 0 Gregorian seconds.
|
||||
"""
|
||||
@spec from_unix(integer, :native | System.time_unit(), Calendar.calendar()) ::
|
||||
{:ok, t} | {:error, atom}
|
||||
@@ -380,20 +152,17 @@ defmodule DateTime do
|
||||
|
||||
# An easy way to get the Unix epoch is passing 0 to this function
|
||||
iex> DateTime.from_unix!(0)
|
||||
~U[1970-01-01 00:00:00Z]
|
||||
#DateTime<1970-01-01 00:00:00Z>
|
||||
|
||||
iex> DateTime.from_unix!(1_464_096_368)
|
||||
~U[2016-05-24 13:26:08Z]
|
||||
#DateTime<2016-05-24 13:26:08Z>
|
||||
|
||||
iex> DateTime.from_unix!(1_432_560_368_868_569, :microsecond)
|
||||
~U[2015-05-25 13:26:08.868569Z]
|
||||
|
||||
iex> DateTime.from_unix!(143_256_036_886_856, 1024)
|
||||
~U[6403-03-17 07:05:22.320312Z]
|
||||
#DateTime<2015-05-25 13:26:08.868569Z>
|
||||
|
||||
"""
|
||||
@spec from_unix!(integer, :native | System.time_unit(), Calendar.calendar()) :: t
|
||||
def from_unix!(integer, unit \\ :second, calendar \\ Calendar.ISO) do
|
||||
def from_unix!(integer, unit \\ :second, calendar \\ Calendar.ISO) when is_atom(unit) do
|
||||
case from_unix(integer, unit, calendar) do
|
||||
{:ok, datetime} ->
|
||||
datetime
|
||||
@@ -414,13 +183,13 @@ defmodule DateTime do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> DateTime.from_naive(~N[2016-05-24 13:26:08.003], "Etc/UTC")
|
||||
{:ok, ~U[2016-05-24 13:26:08.003Z]}
|
||||
iex> {:ok, datetime} = DateTime.from_naive(~N[2016-05-24 13:26:08.003], "Etc/UTC")
|
||||
iex> datetime
|
||||
#DateTime<2016-05-24 13:26:08.003Z>
|
||||
|
||||
When the datetime is ambiguous - for instance during changing from summer
|
||||
to winter time - the two possible valid datetimes are returned in a tuple.
|
||||
The first datetime is also the one which comes first chronologically, while
|
||||
the second one comes last.
|
||||
to winter time - the two possible valid datetimes are returned. First the one
|
||||
that happens first, then the one that happens after.
|
||||
|
||||
iex> {:ambiguous, first_dt, second_dt} = DateTime.from_naive(~N[2018-10-28 02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> first_dt
|
||||
@@ -458,7 +227,7 @@ defmodule DateTime do
|
||||
iex> cph_datetime = DateTime.from_naive!(~N[2018-08-24 10:00:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> {:ok, utc_datetime} = DateTime.from_naive(cph_datetime, "Etc/UTC", FakeTimeZoneDatabase)
|
||||
iex> utc_datetime
|
||||
~U[2018-08-24 10:00:00Z]
|
||||
#DateTime<2018-08-24 10:00:00Z>
|
||||
|
||||
If instead you want a `DateTime` for the same point time in a different time zone see the
|
||||
`DateTime.shift_zone/3` function which would convert 2018-08-24 10:00:00 in Copenhagen
|
||||
@@ -471,7 +240,7 @@ defmodule DateTime do
|
||||
Calendar.time_zone_database()
|
||||
) ::
|
||||
{:ok, t}
|
||||
| {:ambiguous, first_datetime :: t, second_datetime :: t}
|
||||
| {:ambiguous, t, t}
|
||||
| {:gap, t, t}
|
||||
| {:error,
|
||||
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
|
||||
@@ -503,7 +272,7 @@ defmodule DateTime do
|
||||
# we get the last microsecond just before.
|
||||
before_naive =
|
||||
first_period_until_wall
|
||||
|> Map.replace!(:microsecond, {999_999, 6})
|
||||
|> Map.put(:microsecond, {999_999, 6})
|
||||
|> NaiveDateTime.add(-1)
|
||||
|
||||
after_naive = second_period_from_wall
|
||||
@@ -587,7 +356,7 @@ defmodule DateTime do
|
||||
## Examples
|
||||
|
||||
iex> DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC")
|
||||
~U[2016-05-24 13:26:08.003Z]
|
||||
#DateTime<2016-05-24 13:26:08.003Z>
|
||||
|
||||
iex> DateTime.from_naive!(~N[2018-05-24 13:26:08.003], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
#DateTime<2018-05-24 13:26:08.003+02:00 CEST Europe/Copenhagen>
|
||||
@@ -641,13 +410,11 @@ defmodule DateTime do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pacific_datetime} = DateTime.shift_zone(~U[2018-07-16 10:00:00Z], "America/Los_Angeles", FakeTimeZoneDatabase)
|
||||
iex> cph_datetime = DateTime.from_naive!(~N[2018-07-16 12:00:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> {:ok, pacific_datetime} = DateTime.shift_zone(cph_datetime, "America/Los_Angeles", FakeTimeZoneDatabase)
|
||||
iex> pacific_datetime
|
||||
#DateTime<2018-07-16 03:00:00-07:00 PDT America/Los_Angeles>
|
||||
|
||||
iex> DateTime.shift_zone(~U[2018-07-16 10:00:00Z], "bad timezone", FakeTimeZoneDatabase)
|
||||
{:error, :time_zone_not_found}
|
||||
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec shift_zone(t, Calendar.time_zone(), Calendar.time_zone_database()) ::
|
||||
@@ -702,34 +469,6 @@ defmodule DateTime do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Changes the time zone of a `DateTime` or raises on errors.
|
||||
|
||||
See `shift_zone/3` for more information.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> DateTime.shift_zone!(~U[2018-07-16 10:00:00Z], "America/Los_Angeles", FakeTimeZoneDatabase)
|
||||
#DateTime<2018-07-16 03:00:00-07:00 PDT America/Los_Angeles>
|
||||
|
||||
iex> DateTime.shift_zone!(~U[2018-07-16 10:00:00Z], "bad timezone", FakeTimeZoneDatabase)
|
||||
** (ArgumentError) cannot shift ~U[2018-07-16 10:00:00Z] to "bad timezone" time zone, reason: :time_zone_not_found
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec shift_zone!(t, Calendar.time_zone(), Calendar.time_zone_database()) :: t
|
||||
def shift_zone!(datetime, time_zone, time_zone_database \\ Calendar.get_time_zone_database()) do
|
||||
case shift_zone(datetime, time_zone, time_zone_database) do
|
||||
{:ok, datetime} ->
|
||||
datetime
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError,
|
||||
"cannot shift #{inspect(datetime)} to #{inspect(time_zone)} time zone" <>
|
||||
", reason: #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current datetime in the provided time zone.
|
||||
|
||||
@@ -744,11 +483,9 @@ defmodule DateTime do
|
||||
iex> {:ok, datetime} = DateTime.now("Etc/UTC")
|
||||
iex> datetime.time_zone
|
||||
"Etc/UTC"
|
||||
|
||||
iex> DateTime.now("Europe/Copenhagen")
|
||||
{:error, :utc_only_time_zone_database}
|
||||
|
||||
iex> DateTime.now("bad timezone", FakeTimeZoneDatabase)
|
||||
iex> DateTime.now("not a real time zone name", FakeTimeZoneDatabase)
|
||||
{:error, :time_zone_not_found}
|
||||
|
||||
"""
|
||||
@@ -765,38 +502,6 @@ defmodule DateTime do
|
||||
shift_zone(utc_now(), time_zone, time_zone_database)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current datetime in the provided time zone or raises on errors
|
||||
|
||||
See `now/2` for more information.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> datetime = DateTime.now!("Etc/UTC")
|
||||
iex> datetime.time_zone
|
||||
"Etc/UTC"
|
||||
|
||||
iex> DateTime.now!("Europe/Copenhagen")
|
||||
** (ArgumentError) cannot get current datetime in "Europe/Copenhagen" time zone, reason: :utc_only_time_zone_database
|
||||
|
||||
iex> DateTime.now!("bad timezone", FakeTimeZoneDatabase)
|
||||
** (ArgumentError) cannot get current datetime in "bad timezone" time zone, reason: :time_zone_not_found
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec now!(Calendar.time_zone(), Calendar.time_zone_database()) :: t
|
||||
def now!(time_zone, time_zone_database \\ Calendar.get_time_zone_database()) do
|
||||
case now(time_zone, time_zone_database) do
|
||||
{:ok, datetime} ->
|
||||
datetime
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError,
|
||||
"cannot get current datetime in #{inspect(time_zone)} time zone, reason: " <>
|
||||
inspect(reason)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given `datetime` to Unix time.
|
||||
|
||||
@@ -806,10 +511,6 @@ defmodule DateTime do
|
||||
It will return the integer with the given unit,
|
||||
according to `System.convert_time_unit/3`.
|
||||
|
||||
If you want to get the current time in Unix seconds,
|
||||
do not do `DateTime.utc_now() |> DateTime.to_unix()`.
|
||||
Simply call `System.os_time(:second)` instead.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> 1_464_096_368 |> DateTime.from_unix!() |> DateTime.to_unix()
|
||||
@@ -854,17 +555,18 @@ defmodule DateTime do
|
||||
|
||||
"""
|
||||
@spec to_naive(Calendar.datetime()) :: NaiveDateTime.t()
|
||||
def to_naive(%{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
time_zone: _
|
||||
}) do
|
||||
def to_naive(datetime) do
|
||||
%{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
} = datetime
|
||||
|
||||
%NaiveDateTime{
|
||||
year: year,
|
||||
month: month,
|
||||
@@ -893,17 +595,8 @@ defmodule DateTime do
|
||||
|
||||
"""
|
||||
@spec to_date(Calendar.datetime()) :: Date.t()
|
||||
def to_date(%{
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
calendar: calendar,
|
||||
hour: _,
|
||||
minute: _,
|
||||
second: _,
|
||||
microsecond: _,
|
||||
time_zone: _
|
||||
}) do
|
||||
def to_date(datetime) do
|
||||
%{year: year, month: month, day: day, calendar: calendar} = datetime
|
||||
%Date{year: year, month: month, day: day, calendar: calendar}
|
||||
end
|
||||
|
||||
@@ -923,17 +616,10 @@ defmodule DateTime do
|
||||
|
||||
"""
|
||||
@spec to_time(Calendar.datetime()) :: Time.t()
|
||||
def to_time(%{
|
||||
year: _,
|
||||
month: _,
|
||||
day: _,
|
||||
calendar: calendar,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
time_zone: _
|
||||
}) do
|
||||
def to_time(datetime) do
|
||||
%{hour: hour, minute: minute, second: second, microsecond: microsecond, calendar: calendar} =
|
||||
datetime
|
||||
|
||||
%Time{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
@@ -945,14 +631,13 @@ defmodule DateTime do
|
||||
|
||||
@doc """
|
||||
Converts the given datetime to
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601) format.
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601) format.
|
||||
|
||||
By default, `DateTime.to_iso8601/2` returns datetimes formatted in the "extended"
|
||||
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
|
||||
|
||||
Only supports converting datetimes which are in the ISO calendar,
|
||||
attempting to convert datetimes from other calendars will raise.
|
||||
You can also optionally specify an offset for the formatted string.
|
||||
|
||||
WARNING: the ISO 8601 datetime format does not contain the time zone nor
|
||||
its abbreviation, which means information is lost when converting to such
|
||||
@@ -984,31 +669,11 @@ defmodule DateTime do
|
||||
iex> DateTime.to_iso8601(dt, :basic)
|
||||
"20000229T230007-0400"
|
||||
|
||||
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
|
||||
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
|
||||
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
|
||||
iex> DateTime.to_iso8601(dt, :extended, 3600)
|
||||
"2000-03-01T04:00:07+01:00"
|
||||
|
||||
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
|
||||
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
|
||||
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
|
||||
iex> DateTime.to_iso8601(dt, :extended, 0)
|
||||
"2000-03-01T03:00:07+00:00"
|
||||
|
||||
iex> dt = %DateTime{year: 2000, month: 3, day: 01, zone_abbr: "UTC",
|
||||
...> hour: 03, minute: 0, second: 7, microsecond: {0, 0},
|
||||
...> utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"}
|
||||
iex> DateTime.to_iso8601(dt, :extended, 0)
|
||||
"2000-03-01T03:00:07Z"
|
||||
|
||||
iex> {:ok, dt, offset} = DateTime.from_iso8601("2000-03-01T03:00:07Z")
|
||||
iex> "2000-03-01T03:00:07Z" = DateTime.to_iso8601(dt, :extended, offset)
|
||||
"""
|
||||
@spec to_iso8601(Calendar.datetime(), :basic | :extended, nil | integer()) :: String.t()
|
||||
def to_iso8601(datetime, format \\ :extended, offset \\ nil)
|
||||
@spec to_iso8601(Calendar.datetime(), :extended | :basic) :: String.t()
|
||||
def to_iso8601(datetime, format \\ :extended)
|
||||
|
||||
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, nil)
|
||||
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format)
|
||||
when format in [:extended, :basic] do
|
||||
%{
|
||||
year: year,
|
||||
@@ -1019,60 +684,36 @@ defmodule DateTime do
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
time_zone: time_zone,
|
||||
zone_abbr: zone_abbr,
|
||||
utc_offset: utc_offset,
|
||||
std_offset: std_offset
|
||||
} = datetime
|
||||
|
||||
datetime_to_string(year, month, day, hour, minute, second, microsecond, format) <>
|
||||
Calendar.ISO.offset_to_string(utc_offset, std_offset, time_zone, format)
|
||||
Calendar.ISO.datetime_to_iso8601(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
microsecond,
|
||||
time_zone,
|
||||
zone_abbr,
|
||||
utc_offset,
|
||||
std_offset,
|
||||
format
|
||||
)
|
||||
end
|
||||
|
||||
def to_iso8601(
|
||||
%{calendar: Calendar.ISO, microsecond: {_, precision}, time_zone: "Etc/UTC"} = datetime,
|
||||
format,
|
||||
0
|
||||
)
|
||||
when format in [:extended, :basic] do
|
||||
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, 0)
|
||||
|
||||
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
|
||||
"Z"
|
||||
end
|
||||
|
||||
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, offset)
|
||||
when format in [:extended, :basic] do
|
||||
{_, precision} = datetime.microsecond
|
||||
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, offset)
|
||||
|
||||
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
|
||||
Calendar.ISO.offset_to_string(offset, 0, nil, format)
|
||||
end
|
||||
|
||||
def to_iso8601(%{calendar: _} = datetime, format, offset) when format in [:extended, :basic] do
|
||||
def to_iso8601(%{calendar: _} = datetime, format) when format in [:extended, :basic] do
|
||||
datetime
|
||||
|> convert!(Calendar.ISO)
|
||||
|> to_iso8601(format, offset)
|
||||
end
|
||||
|
||||
defp shift_by_offset(%{calendar: calendar} = datetime, offset) do
|
||||
total_offset = datetime.utc_offset + datetime.std_offset
|
||||
|
||||
datetime
|
||||
|> to_iso_days()
|
||||
# Subtract total original offset in order to get UTC and add the new offset
|
||||
|> Calendar.ISO.add_day_fraction_to_iso_days(offset - total_offset, 86400)
|
||||
|> calendar.naive_datetime_from_iso_days()
|
||||
end
|
||||
|
||||
defp datetime_to_string(year, month, day, hour, minute, second, microsecond, format) do
|
||||
Calendar.ISO.date_to_string(year, month, day, format) <>
|
||||
"T" <>
|
||||
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
|
||||
|> to_iso8601(format)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses the extended "Date and time of day" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Since ISO 8601 does not include the proper time zone, the given
|
||||
string will be converted to UTC and its offset in seconds will be
|
||||
@@ -1082,29 +723,32 @@ defmodule DateTime do
|
||||
As specified in the standard, the separator "T" may be omitted if
|
||||
desired as there is no ambiguity within this function.
|
||||
|
||||
The year parsed by this function is limited to four digits and,
|
||||
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
|
||||
hour of the next day, this notation is not supported by Elixir.
|
||||
Note leap seconds are not supported by the built-in Calendar.ISO.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, datetime, 0} = DateTime.from_iso8601("2015-01-23T23:50:07Z")
|
||||
iex> datetime
|
||||
~U[2015-01-23 23:50:07Z]
|
||||
#DateTime<2015-01-23 23:50:07Z>
|
||||
|
||||
iex> {:ok, datetime, 9000} = DateTime.from_iso8601("2015-01-23T23:50:07.123+02:30")
|
||||
iex> datetime
|
||||
~U[2015-01-23 21:20:07.123Z]
|
||||
#DateTime<2015-01-23 21:20:07.123Z>
|
||||
|
||||
iex> {:ok, datetime, 9000} = DateTime.from_iso8601("2015-01-23T23:50:07,123+02:30")
|
||||
iex> datetime
|
||||
~U[2015-01-23 21:20:07.123Z]
|
||||
#DateTime<2015-01-23 21:20:07.123Z>
|
||||
|
||||
iex> {:ok, datetime, 0} = DateTime.from_iso8601("-2015-01-23T23:50:07Z")
|
||||
iex> datetime
|
||||
~U[-2015-01-23 23:50:07Z]
|
||||
#DateTime<-2015-01-23 23:50:07Z>
|
||||
|
||||
iex> {:ok, datetime, 9000} = DateTime.from_iso8601("-2015-01-23T23:50:07,123+02:30")
|
||||
iex> datetime
|
||||
~U[-2015-01-23 21:20:07.123Z]
|
||||
#DateTime<-2015-01-23 21:20:07.123Z>
|
||||
|
||||
iex> DateTime.from_iso8601("2015-01-23P23:50:07")
|
||||
{:error, :invalid_format}
|
||||
@@ -1121,116 +765,94 @@ defmodule DateTime do
|
||||
@doc since: "1.4.0"
|
||||
@spec from_iso8601(String.t(), Calendar.calendar()) ::
|
||||
{:ok, t, Calendar.utc_offset()} | {:error, atom}
|
||||
def from_iso8601(string, calendar \\ Calendar.ISO) do
|
||||
with {:ok, {year, month, day, hour, minute, second, microsecond}, offset} <-
|
||||
Calendar.ISO.parse_utc_datetime(string) do
|
||||
datetime = %DateTime{
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
std_offset: 0,
|
||||
utc_offset: 0,
|
||||
zone_abbr: "UTC",
|
||||
time_zone: "Etc/UTC"
|
||||
}
|
||||
def from_iso8601(string, calendar \\ Calendar.ISO)
|
||||
|
||||
with {:ok, converted} <- convert(datetime, calendar) do
|
||||
{:ok, converted, offset}
|
||||
def from_iso8601(<<?-, rest::binary>>, calendar) do
|
||||
raw_from_iso8601(rest, calendar, true)
|
||||
end
|
||||
|
||||
def from_iso8601(<<rest::binary>>, calendar) do
|
||||
raw_from_iso8601(rest, calendar, false)
|
||||
end
|
||||
|
||||
@sep [?\s, ?T]
|
||||
[match_date, guard_date, read_date] = Calendar.ISO.__match_date__()
|
||||
[match_time, guard_time, read_time] = Calendar.ISO.__match_time__()
|
||||
|
||||
defp raw_from_iso8601(string, calendar, is_year_negative) do
|
||||
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
|
||||
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
|
||||
{microsecond, rest} <- Calendar.ISO.parse_microsecond(rest),
|
||||
{offset, ""} <- Calendar.ISO.parse_offset(rest) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
{hour, minute, second} = unquote(read_time)
|
||||
year = if is_year_negative, do: -year, else: year
|
||||
|
||||
cond do
|
||||
not calendar.valid_date?(year, month, day) ->
|
||||
{:error, :invalid_date}
|
||||
|
||||
not calendar.valid_time?(hour, minute, second, microsecond) ->
|
||||
{:error, :invalid_time}
|
||||
|
||||
offset == 0 ->
|
||||
datetime = %DateTime{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
std_offset: 0,
|
||||
utc_offset: 0,
|
||||
zone_abbr: "UTC",
|
||||
time_zone: "Etc/UTC"
|
||||
}
|
||||
|
||||
{:ok, datetime, 0}
|
||||
|
||||
is_nil(offset) ->
|
||||
{:error, :missing_offset}
|
||||
|
||||
true ->
|
||||
day_fraction = Calendar.ISO.time_to_day_fraction(hour, minute, second, {0, 0})
|
||||
|
||||
{{year, month, day}, {hour, minute, second, _}} =
|
||||
case apply_tz_offset({0, day_fraction}, offset) do
|
||||
{0, day_fraction} ->
|
||||
{{year, month, day}, Calendar.ISO.time_from_day_fraction(day_fraction)}
|
||||
|
||||
{extra_days, day_fraction} ->
|
||||
base_days = Calendar.ISO.date_to_iso_days(year, month, day)
|
||||
|
||||
{Calendar.ISO.date_from_iso_days(base_days + extra_days),
|
||||
Calendar.ISO.time_from_day_fraction(day_fraction)}
|
||||
end
|
||||
|
||||
datetime = %DateTime{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
std_offset: 0,
|
||||
utc_offset: 0,
|
||||
zone_abbr: "UTC",
|
||||
time_zone: "Etc/UTC"
|
||||
}
|
||||
|
||||
{:ok, datetime, offset}
|
||||
end
|
||||
else
|
||||
_ -> {:error, :invalid_format}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a number of gregorian seconds to a `DateTime` struct.
|
||||
|
||||
The returned `DateTime` will have `UTC` timezone, if you want other timezone, please use
|
||||
`DateTime.shift_zone/3`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> DateTime.from_gregorian_seconds(1)
|
||||
~U[0000-01-01 00:00:01Z]
|
||||
iex> DateTime.from_gregorian_seconds(63_755_511_991, {5000, 3})
|
||||
~U[2020-05-01 00:26:31.005Z]
|
||||
iex> DateTime.from_gregorian_seconds(-1)
|
||||
~U[-0001-12-31 23:59:59Z]
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec from_gregorian_seconds(integer(), Calendar.microsecond(), Calendar.calendar()) :: t
|
||||
def from_gregorian_seconds(
|
||||
seconds,
|
||||
{microsecond, precision} \\ {0, 0},
|
||||
calendar \\ Calendar.ISO
|
||||
)
|
||||
when is_integer(seconds) do
|
||||
iso_days = Calendar.ISO.gregorian_seconds_to_iso_days(seconds, microsecond)
|
||||
|
||||
{year, month, day, hour, minute, second, {microsecond, _}} =
|
||||
calendar.naive_datetime_from_iso_days(iso_days)
|
||||
|
||||
%DateTime{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: {microsecond, precision},
|
||||
std_offset: 0,
|
||||
utc_offset: 0,
|
||||
zone_abbr: "UTC",
|
||||
time_zone: "Etc/UTC"
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a `DateTime` struct to a number of gregorian seconds and microseconds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dt = %DateTime{year: 0000, month: 1, day: 1, zone_abbr: "UTC",
|
||||
...> hour: 0, minute: 0, second: 1, microsecond: {0, 0},
|
||||
...> utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"}
|
||||
iex> DateTime.to_gregorian_seconds(dt)
|
||||
{1, 0}
|
||||
|
||||
iex> dt = %DateTime{year: 2020, month: 5, day: 1, zone_abbr: "UTC",
|
||||
...> hour: 0, minute: 26, second: 31, microsecond: {5000, 0},
|
||||
...> utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"}
|
||||
iex> DateTime.to_gregorian_seconds(dt)
|
||||
{63_755_511_991, 5000}
|
||||
|
||||
iex> dt = %DateTime{year: 2020, month: 5, day: 1, zone_abbr: "CET",
|
||||
...> hour: 1, minute: 26, second: 31, microsecond: {5000, 0},
|
||||
...> utc_offset: 3600, std_offset: 0, time_zone: "Europe/Warsaw"}
|
||||
iex> DateTime.to_gregorian_seconds(dt)
|
||||
{63_755_511_991, 5000}
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec to_gregorian_seconds(Calendar.datetime()) :: {integer(), non_neg_integer()}
|
||||
def to_gregorian_seconds(
|
||||
%{
|
||||
std_offset: std_offset,
|
||||
utc_offset: utc_offset,
|
||||
microsecond: {microsecond, _}
|
||||
} = datetime
|
||||
) do
|
||||
{days, day_fraction} =
|
||||
datetime
|
||||
|> to_iso_days()
|
||||
|> apply_tz_offset(utc_offset + std_offset)
|
||||
|
||||
seconds_in_day = seconds_from_day_fraction(day_fraction)
|
||||
{days * @seconds_per_day + seconds_in_day, microsecond}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given `datetime` to a string according to its calendar.
|
||||
|
||||
@@ -1399,8 +1021,9 @@ defmodule DateTime do
|
||||
iex> dt |> DateTime.add(3600, :second, FakeTimeZoneDatabase)
|
||||
#DateTime<2018-11-15 11:00:00+01:00 CET Europe/Copenhagen>
|
||||
|
||||
iex> DateTime.add(~U[2018-11-15 10:00:00Z], 3600, :second)
|
||||
~U[2018-11-15 11:00:00Z]
|
||||
iex> dt = DateTime.from_naive!(~N[2018-11-15 10:00:00], "Etc/UTC")
|
||||
iex> dt |> DateTime.add(3600, :second)
|
||||
#DateTime<2018-11-15 11:00:00Z>
|
||||
|
||||
When adding 3 seconds just before "spring forward" we go from 1:59:59 to 3:00:02
|
||||
|
||||
@@ -1449,7 +1072,7 @@ defmodule DateTime do
|
||||
|
||||
@doc """
|
||||
Returns the given datetime with the microsecond field truncated to the given
|
||||
precision (`:microsecond`, `:millisecond` or `:second`).
|
||||
precision (`:microsecond`, `millisecond` or `:second`).
|
||||
|
||||
The given datetime is returned unchanged if it already has lower precision than
|
||||
the given precision.
|
||||
@@ -1631,12 +1254,6 @@ defmodule DateTime do
|
||||
}
|
||||
end
|
||||
|
||||
defp seconds_from_day_fraction({parts_in_day, @seconds_per_day}),
|
||||
do: parts_in_day
|
||||
|
||||
defp seconds_from_day_fraction({parts_in_day, parts_per_day}),
|
||||
do: div(parts_in_day * @seconds_per_day, parts_per_day)
|
||||
|
||||
defimpl String.Chars do
|
||||
def to_string(datetime) do
|
||||
%{
|
||||
@@ -1671,7 +1288,7 @@ defmodule DateTime do
|
||||
end
|
||||
|
||||
defimpl Inspect do
|
||||
def inspect(datetime, _) do
|
||||
def inspect(%{calendar: Calendar.ISO} = datetime, _) do
|
||||
%{
|
||||
year: year,
|
||||
month: month,
|
||||
@@ -1683,12 +1300,11 @@ defmodule DateTime do
|
||||
time_zone: time_zone,
|
||||
zone_abbr: zone_abbr,
|
||||
utc_offset: utc_offset,
|
||||
std_offset: std_offset,
|
||||
calendar: calendar
|
||||
std_offset: std_offset
|
||||
} = datetime
|
||||
|
||||
formatted =
|
||||
calendar.datetime_to_string(
|
||||
"#DateTime<" <>
|
||||
Calendar.ISO.datetime_to_string(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
@@ -1700,18 +1316,11 @@ defmodule DateTime do
|
||||
zone_abbr,
|
||||
utc_offset,
|
||||
std_offset
|
||||
)
|
||||
|
||||
case datetime do
|
||||
%{utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"} ->
|
||||
"~U[" <> formatted <> suffix(calendar) <> "]"
|
||||
|
||||
_ ->
|
||||
"#DateTime<" <> formatted <> suffix(calendar) <> ">"
|
||||
end
|
||||
) <> ">"
|
||||
end
|
||||
|
||||
defp suffix(Calendar.ISO), do: ""
|
||||
defp suffix(calendar), do: " " <> inspect(calendar)
|
||||
def inspect(datetime, opts) do
|
||||
Inspect.Any.inspect(datetime, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+197
-858
File diff suppressed because it is too large
Load Diff
@@ -78,8 +78,6 @@ defmodule NaiveDateTime do
|
||||
microsecond: Calendar.microsecond()
|
||||
}
|
||||
|
||||
@seconds_per_day 24 * 60 * 60
|
||||
|
||||
@doc """
|
||||
Returns the current naive datetime in UTC.
|
||||
|
||||
@@ -119,53 +117,6 @@ defmodule NaiveDateTime do
|
||||
|> DateTime.to_naive()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the "local time" for the machine the Elixir program is running on.
|
||||
|
||||
WARNING: This function can cause insidious bugs. It depends on the time zone
|
||||
configuration at run time. This can changed and be set to a time zone that has
|
||||
daylight saving jumps (spring forward or fall back).
|
||||
|
||||
This function can be used to display what the time is right now for the time
|
||||
zone configuration that the machine happens to have. An example would be a
|
||||
desktop program displaying a clock to the user. For any other uses it is
|
||||
probably a bad idea to use this function.
|
||||
|
||||
For most cases, use `DateTime.now/2` or `DateTime.utc_now/1` instead.
|
||||
|
||||
Does not include fractional seconds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> naive_datetime = NaiveDateTime.local_now()
|
||||
iex> naive_datetime.year >= 2019
|
||||
true
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec local_now(Calendar.calendar()) :: t
|
||||
def local_now(calendar \\ Calendar.ISO)
|
||||
|
||||
def local_now(Calendar.ISO) do
|
||||
{{year, month, day}, {hour, minute, second}} = :erlang.localtime()
|
||||
{:ok, ndt} = NaiveDateTime.new(year, month, day, hour, minute, second)
|
||||
ndt
|
||||
end
|
||||
|
||||
def local_now(calendar) do
|
||||
naive_datetime = local_now()
|
||||
|
||||
case convert(naive_datetime, calendar) do
|
||||
{:ok, value} ->
|
||||
value
|
||||
|
||||
{:error, :incompatible_calendars} ->
|
||||
raise ArgumentError,
|
||||
~s(cannot get "local now" in target calendar #{inspect(calendar)}, ) <>
|
||||
"reason: cannot convert from Calendar.ISO to #{inspect(calendar)}."
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a new ISO naive datetime.
|
||||
|
||||
@@ -210,7 +161,7 @@ defmodule NaiveDateTime do
|
||||
Calendar.hour(),
|
||||
Calendar.minute(),
|
||||
Calendar.second(),
|
||||
Calendar.microsecond() | non_neg_integer,
|
||||
Calendar.microsecond(),
|
||||
Calendar.calendar()
|
||||
) :: {:ok, t} | {:error, atom}
|
||||
def new(year, month, day, hour, minute, second, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)
|
||||
@@ -244,61 +195,6 @@ defmodule NaiveDateTime do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a new ISO naive datetime.
|
||||
|
||||
Expects all values to be integers. Returns `naive_datetime`
|
||||
if each entry fits its appropriate range, raises if
|
||||
time or date is invalid.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> NaiveDateTime.new!(2000, 1, 1, 0, 0, 0)
|
||||
~N[2000-01-01 00:00:00]
|
||||
iex> NaiveDateTime.new!(2000, 2, 29, 0, 0, 0)
|
||||
~N[2000-02-29 00:00:00]
|
||||
iex> NaiveDateTime.new!(2000, 1, 1, 23, 59, 59, {0, 1})
|
||||
~N[2000-01-01 23:59:59.0]
|
||||
iex> NaiveDateTime.new!(2000, 1, 1, 23, 59, 59, 999_999)
|
||||
~N[2000-01-01 23:59:59.999999]
|
||||
iex> NaiveDateTime.new!(2000, 1, 1, 23, 59, 59, {0, 1}, Calendar.ISO)
|
||||
~N[2000-01-01 23:59:59.0]
|
||||
iex> NaiveDateTime.new!(2000, 1, 1, 24, 59, 59, 999_999)
|
||||
** (ArgumentError) cannot build naive datetime, reason: :invalid_time
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec new!(
|
||||
Calendar.year(),
|
||||
Calendar.month(),
|
||||
Calendar.day(),
|
||||
Calendar.hour(),
|
||||
Calendar.minute(),
|
||||
Calendar.second(),
|
||||
Calendar.microsecond() | non_neg_integer,
|
||||
Calendar.calendar()
|
||||
) :: t
|
||||
def new!(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
microsecond \\ {0, 0},
|
||||
calendar \\ Calendar.ISO
|
||||
)
|
||||
|
||||
def new!(year, month, day, hour, minute, second, microsecond, calendar) do
|
||||
case new(year, month, day, hour, minute, second, microsecond, calendar) do
|
||||
{:ok, naive_datetime} ->
|
||||
naive_datetime
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError, "cannot build naive datetime, reason: #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a naive datetime from date and time structs.
|
||||
|
||||
@@ -329,24 +225,6 @@ defmodule NaiveDateTime do
|
||||
{:ok, naive_datetime}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a naive datetime from date and time structs.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> NaiveDateTime.new!(~D[2010-01-13], ~T[23:00:07.005])
|
||||
~N[2010-01-13 23:00:07.005]
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec new!(Date.t(), Time.t()) :: t
|
||||
def new!(date, time)
|
||||
|
||||
def new!(%Date{calendar: calendar} = date, %Time{calendar: calendar} = time) do
|
||||
{:ok, naive_datetime} = new(date, time)
|
||||
naive_datetime
|
||||
end
|
||||
|
||||
@doc """
|
||||
Adds a specified amount of time to a `NaiveDateTime`.
|
||||
|
||||
@@ -380,7 +258,7 @@ defmodule NaiveDateTime do
|
||||
iex> NaiveDateTime.add(~N[0000-01-01 00:00:00], 63_579_428_950)
|
||||
~N[2014-10-02 00:29:10]
|
||||
|
||||
Passing a `DateTime` automatically converts it to `NaiveDateTime`,
|
||||
Passing a `Datetime` automatically converts it to `NaiveDateTime`,
|
||||
discarding the time zone information:
|
||||
|
||||
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CET",
|
||||
@@ -514,6 +392,10 @@ defmodule NaiveDateTime do
|
||||
|
||||
"""
|
||||
@spec to_date(Calendar.naive_datetime()) :: Date.t()
|
||||
def to_date(%NaiveDateTime{year: year, month: month, day: day, calendar: calendar}) do
|
||||
%Date{year: year, month: month, day: day, calendar: calendar}
|
||||
end
|
||||
|
||||
def to_date(%{
|
||||
year: year,
|
||||
month: month,
|
||||
@@ -540,6 +422,24 @@ defmodule NaiveDateTime do
|
||||
|
||||
"""
|
||||
@spec to_time(Calendar.naive_datetime()) :: Time.t()
|
||||
def to_time(%NaiveDateTime{} = naive_datetime) do
|
||||
%{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
calendar: calendar
|
||||
} = naive_datetime
|
||||
|
||||
%Time{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
calendar: calendar
|
||||
}
|
||||
end
|
||||
|
||||
def to_time(%{
|
||||
year: _,
|
||||
month: _,
|
||||
@@ -598,7 +498,7 @@ defmodule NaiveDateTime do
|
||||
|
||||
@doc """
|
||||
Parses the extended "Date and time of day" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Time zone offset may be included in the string but they will be
|
||||
simply discarded as such information is not included in naive date
|
||||
@@ -607,6 +507,9 @@ defmodule NaiveDateTime do
|
||||
As specified in the standard, the separator "T" may be omitted if
|
||||
desired as there is no ambiguity within this function.
|
||||
|
||||
The year parsed by this function is limited to four digits and,
|
||||
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
|
||||
hour of the next day, this notation is not supported by Elixir.
|
||||
Note leap seconds are not supported by the built-in Calendar.ISO.
|
||||
|
||||
## Examples
|
||||
@@ -653,27 +556,41 @@ defmodule NaiveDateTime do
|
||||
|
||||
"""
|
||||
@spec from_iso8601(String.t(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
|
||||
def from_iso8601(string, calendar \\ Calendar.ISO) do
|
||||
with {:ok, {year, month, day, hour, minute, second, microsecond}} <-
|
||||
Calendar.ISO.parse_naive_datetime(string) do
|
||||
convert(
|
||||
%NaiveDateTime{
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
},
|
||||
calendar
|
||||
)
|
||||
def from_iso8601(string, calendar \\ Calendar.ISO)
|
||||
|
||||
def from_iso8601(<<?-, rest::binary>>, calendar) do
|
||||
with {:ok, %{year: year} = naive_datetime} <- raw_from_iso8601(rest, calendar) do
|
||||
{:ok, %{naive_datetime | year: -year}}
|
||||
end
|
||||
end
|
||||
|
||||
def from_iso8601(<<rest::binary>>, calendar) do
|
||||
raw_from_iso8601(rest, calendar)
|
||||
end
|
||||
|
||||
@sep [?\s, ?T]
|
||||
[match_date, guard_date, read_date] = Calendar.ISO.__match_date__()
|
||||
[match_time, guard_time, read_time] = Calendar.ISO.__match_time__()
|
||||
|
||||
defp raw_from_iso8601(string, calendar) do
|
||||
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
|
||||
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
|
||||
{microsec, rest} <- Calendar.ISO.parse_microsecond(rest),
|
||||
{_offset, ""} <- Calendar.ISO.parse_offset(rest) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
{hour, min, sec} = unquote(read_time)
|
||||
|
||||
with {:ok, iso_naive_dt} <- new(year, month, day, hour, min, sec, microsec, Calendar.ISO) do
|
||||
convert(iso_naive_dt, calendar)
|
||||
end
|
||||
else
|
||||
_ -> {:error, :invalid_format}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses the extended "Date and time of day" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Raises if the format is invalid.
|
||||
|
||||
@@ -701,7 +618,7 @@ defmodule NaiveDateTime do
|
||||
|
||||
@doc """
|
||||
Converts the given naive datetime to
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
By default, `NaiveDateTime.to_iso8601/2` returns naive datetimes formatted in the "extended"
|
||||
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
|
||||
@@ -745,8 +662,16 @@ defmodule NaiveDateTime do
|
||||
microsecond: microsecond
|
||||
} = naive_datetime
|
||||
|
||||
Calendar.ISO.date_to_string(year, month, day, format) <>
|
||||
"T" <> Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
|
||||
Calendar.ISO.naive_datetime_to_iso8601(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
microsecond,
|
||||
format
|
||||
)
|
||||
end
|
||||
|
||||
def to_iso8601(%{calendar: _} = naive_datetime, format) when format in [:basic, :extended] do
|
||||
@@ -769,8 +694,8 @@ defmodule NaiveDateTime do
|
||||
iex> NaiveDateTime.to_erl(~N[2000-01-01 13:30:15])
|
||||
{{2000, 1, 1}, {13, 30, 15}}
|
||||
|
||||
This function can also be used to convert a DateTime to an Erlang
|
||||
datetime tuple without the time zone information:
|
||||
This function can also be used to convert a DateTime to a erl format
|
||||
without the time zone information:
|
||||
|
||||
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CET",
|
||||
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
|
||||
@@ -841,82 +766,6 @@ defmodule NaiveDateTime do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a number of gregorian seconds to a `NaiveDateTime` struct.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> NaiveDateTime.from_gregorian_seconds(1)
|
||||
~N[0000-01-01 00:00:01]
|
||||
iex> NaiveDateTime.from_gregorian_seconds(63_755_511_991, {5000, 3})
|
||||
~N[2020-05-01 00:26:31.005]
|
||||
iex> NaiveDateTime.from_gregorian_seconds(-1)
|
||||
~N[-0001-12-31 23:59:59]
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec from_gregorian_seconds(integer(), Calendar.microsecond(), Calendar.calendar()) :: t
|
||||
def from_gregorian_seconds(
|
||||
seconds,
|
||||
{microsecond, precision} \\ {0, 0},
|
||||
calendar \\ Calendar.ISO
|
||||
)
|
||||
when is_integer(seconds) do
|
||||
iso_days = Calendar.ISO.gregorian_seconds_to_iso_days(seconds, microsecond)
|
||||
|
||||
{year, month, day, hour, minute, second, {microsecond, _}} =
|
||||
calendar.naive_datetime_from_iso_days(iso_days)
|
||||
|
||||
%NaiveDateTime{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: {microsecond, precision}
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a `NaiveDateTime` struct to a number of gregorian seconds and microseconds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> NaiveDateTime.to_gregorian_seconds(~N[0000-01-01 00:00:01])
|
||||
{1, 0}
|
||||
iex> NaiveDateTime.to_gregorian_seconds(~N[2020-05-01 00:26:31.005])
|
||||
{63_755_511_991, 5000}
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec to_gregorian_seconds(Calendar.naive_datetime()) :: {integer(), non_neg_integer()}
|
||||
def to_gregorian_seconds(%{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: {microsecond, precision}
|
||||
}) do
|
||||
{days, day_fraction} =
|
||||
calendar.naive_datetime_to_iso_days(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
{microsecond, precision}
|
||||
)
|
||||
|
||||
seconds_in_day = seconds_from_day_fraction(day_fraction)
|
||||
{days * @seconds_per_day + seconds_in_day, microsecond}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compares two `NaiveDateTime` structs.
|
||||
|
||||
@@ -1061,12 +910,6 @@ defmodule NaiveDateTime do
|
||||
|
||||
## Helpers
|
||||
|
||||
defp seconds_from_day_fraction({parts_in_day, @seconds_per_day}),
|
||||
do: parts_in_day
|
||||
|
||||
defp seconds_from_day_fraction({parts_in_day, parts_per_day}),
|
||||
do: div(parts_in_day * @seconds_per_day, parts_per_day)
|
||||
|
||||
# Keep it multiline for proper function clause errors.
|
||||
defp to_iso_days(%{
|
||||
calendar: calendar,
|
||||
@@ -1115,7 +958,7 @@ defmodule NaiveDateTime do
|
||||
end
|
||||
|
||||
defimpl Inspect do
|
||||
def inspect(naive_datetime, _) do
|
||||
def inspect(%{calendar: Calendar.ISO} = naive_datetime, _) do
|
||||
%{
|
||||
year: year,
|
||||
month: month,
|
||||
@@ -1123,17 +966,17 @@ defmodule NaiveDateTime do
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
calendar: calendar
|
||||
microsecond: microsecond
|
||||
} = naive_datetime
|
||||
|
||||
formatted =
|
||||
calendar.naive_datetime_to_string(year, month, day, hour, minute, second, microsecond)
|
||||
Calendar.ISO.naive_datetime_to_string(year, month, day, hour, minute, second, microsecond)
|
||||
|
||||
"~N[" <> formatted <> suffix(calendar) <> "]"
|
||||
"~N[" <> formatted <> "]"
|
||||
end
|
||||
|
||||
defp suffix(Calendar.ISO), do: ""
|
||||
defp suffix(calendar), do: " " <> inspect(calendar)
|
||||
def inspect(naive, opts) do
|
||||
Inspect.Any.inspect(naive, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+43
-115
@@ -46,7 +46,6 @@ defmodule Time do
|
||||
}
|
||||
|
||||
@parts_per_day 86_400_000_000
|
||||
@seconds_per_day 24 * 60 * 60
|
||||
|
||||
@doc """
|
||||
Returns the current time in UTC.
|
||||
@@ -111,7 +110,7 @@ defmodule Time do
|
||||
Calendar.hour(),
|
||||
Calendar.minute(),
|
||||
Calendar.second(),
|
||||
Calendar.microsecond() | non_neg_integer,
|
||||
Calendar.microsecond() | integer,
|
||||
Calendar.calendar()
|
||||
) :: {:ok, t} | {:error, atom}
|
||||
def new(hour, minute, second, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)
|
||||
@@ -140,44 +139,6 @@ defmodule Time do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a new time.
|
||||
|
||||
Expects all values to be integers. Returns `time` if each
|
||||
entry fits its appropriate range, raises if the time is invalid.
|
||||
|
||||
Microseconds can also be given with a precision, which must be an
|
||||
integer between 0 and 6.
|
||||
|
||||
The built-in calendar does not support leap seconds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.new!(0, 0, 0, 0)
|
||||
~T[00:00:00.000000]
|
||||
iex> Time.new!(23, 59, 59, 999_999)
|
||||
~T[23:59:59.999999]
|
||||
iex> Time.new!(24, 59, 59, 999_999)
|
||||
** (ArgumentError) cannot build time, reason: :invalid_time
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec new!(
|
||||
Calendar.hour(),
|
||||
Calendar.minute(),
|
||||
Calendar.second(),
|
||||
Calendar.microsecond() | non_neg_integer,
|
||||
Calendar.calendar()
|
||||
) :: t
|
||||
def new!(hour, minute, second, microsecond \\ {0, 0}, calendar \\ Calendar.ISO) do
|
||||
case new(hour, minute, second, microsecond, calendar) do
|
||||
{:ok, time} ->
|
||||
time
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError, "cannot build time, reason: #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given `time` to a string.
|
||||
|
||||
@@ -211,7 +172,7 @@ defmodule Time do
|
||||
|
||||
@doc """
|
||||
Parses the extended "Local time" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Time zone offset may be included in the string but they will be
|
||||
simply discarded as such information is not included in times.
|
||||
@@ -219,6 +180,12 @@ defmodule Time do
|
||||
As specified in the standard, the separator "T" may be omitted if
|
||||
desired as there is no ambiguity within this function.
|
||||
|
||||
Time representations with reduced accuracy are not supported.
|
||||
|
||||
Note that while ISO 8601 allows times to specify 24:00:00 as the
|
||||
zero hour of the next day, this notation is not supported by Elixir.
|
||||
Leap seconds are not supported as well by the built-in Calendar.ISO.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.from_iso8601("23:50:07")
|
||||
@@ -246,18 +213,36 @@ defmodule Time do
|
||||
|
||||
"""
|
||||
@spec from_iso8601(String.t(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
|
||||
def from_iso8601(string, calendar \\ Calendar.ISO) do
|
||||
with {:ok, {hour, minute, second, microsecond}} <- Calendar.ISO.parse_time(string) do
|
||||
convert(
|
||||
%Time{hour: hour, minute: minute, second: second, microsecond: microsecond},
|
||||
calendar
|
||||
)
|
||||
def from_iso8601(string, calendar \\ Calendar.ISO)
|
||||
|
||||
def from_iso8601(<<?T, rest::binary>>, calendar) do
|
||||
raw_from_iso8601(rest, calendar)
|
||||
end
|
||||
|
||||
def from_iso8601(<<rest::binary>>, calendar) do
|
||||
raw_from_iso8601(rest, calendar)
|
||||
end
|
||||
|
||||
[match_time, guard_time, read_time] = Calendar.ISO.__match_time__()
|
||||
|
||||
defp raw_from_iso8601(string, calendar) do
|
||||
with <<unquote(match_time), rest::binary>> <- string,
|
||||
true <- unquote(guard_time),
|
||||
{microsec, rest} <- Calendar.ISO.parse_microsecond(rest),
|
||||
{_offset, ""} <- Calendar.ISO.parse_offset(rest) do
|
||||
{hour, min, sec} = unquote(read_time)
|
||||
|
||||
with {:ok, utc_time} <- new(hour, min, sec, microsec, Calendar.ISO) do
|
||||
convert(utc_time, calendar)
|
||||
end
|
||||
else
|
||||
_ -> {:error, :invalid_format}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses the extended "Local time" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Raises if the format is invalid.
|
||||
|
||||
@@ -284,7 +269,7 @@ defmodule Time do
|
||||
|
||||
@doc """
|
||||
Converts the given time to
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
By default, `Time.to_iso8601/2` returns times formatted in the "extended"
|
||||
format, for human readability. It also supports the "basic" format through
|
||||
@@ -316,7 +301,7 @@ defmodule Time do
|
||||
microsecond: microsecond
|
||||
} = time
|
||||
|
||||
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
|
||||
Calendar.ISO.time_to_iso8601(hour, minute, second, microsecond, format)
|
||||
end
|
||||
|
||||
def to_iso8601(%{calendar: _} = time, format) when format in [:extended, :basic] do
|
||||
@@ -391,63 +376,6 @@ defmodule Time do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a number of seconds after midnight to a `Time` struct.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.from_seconds_after_midnight(10_000)
|
||||
~T[02:46:40]
|
||||
iex> Time.from_seconds_after_midnight(30_000, {5000, 3})
|
||||
~T[08:20:00.005]
|
||||
iex> Time.from_seconds_after_midnight(-1)
|
||||
~T[23:59:59]
|
||||
iex> Time.from_seconds_after_midnight(100_000)
|
||||
~T[03:46:40]
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec from_seconds_after_midnight(
|
||||
integer(),
|
||||
Calendar.microsecond(),
|
||||
Calendar.calendar()
|
||||
) :: t
|
||||
def from_seconds_after_midnight(seconds, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)
|
||||
when is_integer(seconds) do
|
||||
seconds_in_day = Integer.mod(seconds, @seconds_per_day)
|
||||
|
||||
{hour, minute, second, {_, _}} =
|
||||
calendar.time_from_day_fraction({seconds_in_day, @seconds_per_day})
|
||||
|
||||
%Time{
|
||||
calendar: calendar,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a `Time` struct to a number of seconds after midnight.
|
||||
|
||||
The returned value is a two-element tuple with the number of seconds and microseconds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.to_seconds_after_midnight(~T[23:30:15])
|
||||
{84615, 0}
|
||||
iex> Time.to_seconds_after_midnight(~N[2010-04-17 23:30:15.999])
|
||||
{84615, 999000}
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec to_seconds_after_midnight(Calendar.time()) :: {integer(), non_neg_integer()}
|
||||
def to_seconds_after_midnight(%{microsecond: {microsecond, _precision}} = time) do
|
||||
iso_days = {0, to_day_fraction(time)}
|
||||
{Calendar.ISO.iso_days_to_unit(iso_days, :second), microsecond}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Adds the `number` of `unit`s to the given `time`.
|
||||
|
||||
@@ -646,12 +574,12 @@ defmodule Time do
|
||||
|
||||
As with the `compare/2` function both `Time` structs and other structures
|
||||
containing time can be used. If for instance a `NaiveDateTime` or `DateTime`
|
||||
is passed, only the hour, minute, second, and microsecond is considered. Any
|
||||
is passed, only the hour, month, second, and microsecond is considered. Any
|
||||
additional information about a date or time zone is ignored when calculating
|
||||
the difference.
|
||||
|
||||
The answer can be returned in any `unit` available from
|
||||
`t:System.time_unit/0`. If the first time value is earlier than
|
||||
`t:System.time_unit/0`. If the first unit is smaller than
|
||||
the second, a negative number is returned.
|
||||
|
||||
This function returns the difference in seconds where seconds
|
||||
@@ -765,20 +693,20 @@ defmodule Time do
|
||||
end
|
||||
|
||||
defimpl Inspect do
|
||||
def inspect(time, _) do
|
||||
def inspect(%{calendar: Calendar.ISO} = time, _) do
|
||||
%{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
calendar: calendar
|
||||
calendar: Calendar.ISO
|
||||
} = time
|
||||
|
||||
"~T[" <>
|
||||
calendar.time_to_string(hour, minute, second, microsecond) <> suffix(calendar) <> "]"
|
||||
"~T[" <> Calendar.ISO.time_to_string(hour, minute, second, microsecond) <> "]"
|
||||
end
|
||||
|
||||
defp suffix(Calendar.ISO), do: ""
|
||||
defp suffix(calendar), do: " " <> inspect(calendar)
|
||||
def inspect(time, opts) do
|
||||
Inspect.Any.inspect(time, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -26,7 +26,7 @@ defmodule Calendar.TimeZoneDatabase do
|
||||
|
||||
A beginning is inclusive. An ending is exclusive. Eg. if a period is from
|
||||
2015-03-29 01:00:00 and until 2015-10-25 01:00:00, the period includes and
|
||||
begins from the beginning of 2015-03-29 01:00:00 and lasts until just before
|
||||
begins from the begining of 2015-03-29 01:00:00 and lasts until just before
|
||||
2015-10-25 01:00:00.
|
||||
|
||||
A beginning or end for certain periods are infinite. For instance the latest
|
||||
@@ -59,7 +59,7 @@ defmodule Calendar.TimeZoneDatabase do
|
||||
with a limit for when the period ends (wall time). The second nested two-tuple is the period
|
||||
just after the gap and a datetime (wall time) for when the period begins just after the gap.
|
||||
|
||||
If there is only a single possible period for the provided `datetime`, then a tuple with `:ok`
|
||||
If there is only a single possible period for the provided `datetime`, the a tuple with `:single`
|
||||
and the `time_zone_period` is returned.
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
|
||||
+285
-871
File diff suppressed because it is too large
Load Diff
+446
-436
File diff suppressed because it is too large
Load Diff
@@ -1,841 +0,0 @@
|
||||
defmodule Code.Fragment do
|
||||
@moduledoc """
|
||||
This module provides conveniences for analyzing fragments of
|
||||
textual code and extract available information whenever possible.
|
||||
|
||||
Most of the functions in this module provide a best-effort
|
||||
and may not be accurate under all circumstances. Read each
|
||||
documentation for more information.
|
||||
|
||||
This module should be considered experimental.
|
||||
"""
|
||||
|
||||
@type position :: {line :: pos_integer(), column :: pos_integer()}
|
||||
|
||||
@doc """
|
||||
Receives a string and returns the cursor context.
|
||||
|
||||
This function receives a string with an Elixir code fragment,
|
||||
representing a cursor position, and based on the string, it
|
||||
provides contextual information about said position. The
|
||||
return of this function can then be used to provide tips,
|
||||
suggestions, and autocompletion functionality.
|
||||
|
||||
This function provides a best-effort detection and may not be
|
||||
accurate under all circumstances. See the "Limitations"
|
||||
section below.
|
||||
|
||||
Consider adding a catch-all clause when handling the return
|
||||
type of this function as new cursor information may be added
|
||||
in future releases.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.Fragment.cursor_context("")
|
||||
:expr
|
||||
|
||||
iex> Code.Fragment.cursor_context("hello_wor")
|
||||
{:local_or_var, 'hello_wor'}
|
||||
|
||||
## Return values
|
||||
|
||||
* `{:alias, charlist}` - the context is an alias, potentially
|
||||
a nested one, such as `Hello.Wor` or `HelloWor`
|
||||
|
||||
* `{:dot, inside_dot, charlist}` - the context is a dot
|
||||
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
|
||||
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
|
||||
itself. If a var is given, this may either be a remote call or a map
|
||||
field access. Examples are `Hello.wor`, `:hello.wor`, `hello.wor`,
|
||||
`Hello.nested.wor`, `hello.nested.wor`, and `@hello.world`
|
||||
|
||||
* `{:dot_arity, inside_dot, charlist}` - the context is a dot arity
|
||||
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
|
||||
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
|
||||
itself. If a var is given, it must be a remote arity. Examples are
|
||||
`Hello.world/`, `:hello.world/`, `hello.world/2`, and `@hello.world/2`
|
||||
|
||||
* `{:dot_call, inside_dot, charlist}` - the context is a dot
|
||||
call. This means parentheses or space have been added after the expression.
|
||||
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
|
||||
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
|
||||
itself. If a var is given, it must be a remote call. Examples are
|
||||
`Hello.world(`, `:hello.world(`, `Hello.world `, `hello.world(`, `hello.world `,
|
||||
and `@hello.world(`
|
||||
|
||||
* `:expr` - may be any expression. Autocompletion may suggest an alias,
|
||||
local or var
|
||||
|
||||
* `{:local_or_var, charlist}` - the context is a variable or a local
|
||||
(import or local) call, such as `hello_wor`
|
||||
|
||||
* `{:local_arity, charlist}` - the context is a local (import or local)
|
||||
arity, such as `hello_world/`
|
||||
|
||||
* `{:local_call, charlist}` - the context is a local (import or local)
|
||||
call, such as `hello_world(` and `hello_world `
|
||||
|
||||
* `{:module_attribute, charlist}` - the context is a module attribute,
|
||||
such as `@hello_wor`
|
||||
|
||||
* `{:operator, charlist}` - the context is an operator, such as `+` or
|
||||
`==`. Note textual operators, such as `when` do not appear as operators
|
||||
but rather as `:local_or_var`. `@` is never an `:operator` and always a
|
||||
`:module_attribute`
|
||||
|
||||
* `{:operator_arity, charlist}` - the context is an operator arity, which
|
||||
is an operator followed by /, such as `+/`, `not/` or `when/`
|
||||
|
||||
* `{:operator_call, charlist}` - the context is an operator call, which is
|
||||
an operator followed by space, such as `left + `, `not ` or `x when `
|
||||
|
||||
* `:none` - no context possible
|
||||
|
||||
* `{:sigil, charlist}` - the context is a sigil. It may be either the beginning
|
||||
of a sigil, such as `~` or `~s`, or an operator starting with `~`, such as
|
||||
`~>` and `~>>`
|
||||
|
||||
* `{:struct, charlist}` - the context is a struct, such as `%`, `%UR` or `%URI`
|
||||
|
||||
* `{:unquoted_atom, charlist}` - the context is an unquoted atom. This
|
||||
can be any atom or an atom representing a module
|
||||
|
||||
## Limitations
|
||||
|
||||
The current algorithm only considers the last line of the input. This means
|
||||
it will also show suggestions inside strings, heredocs, etc, which is
|
||||
intentional as it helps with doctests, references, and more.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec cursor_context(List.Chars.t(), keyword()) ::
|
||||
{:alias, charlist}
|
||||
| {:dot, inside_dot, charlist}
|
||||
| {:dot_arity, inside_dot, charlist}
|
||||
| {:dot_call, inside_dot, charlist}
|
||||
| :expr
|
||||
| {:local_or_var, charlist}
|
||||
| {:local_arity, charlist}
|
||||
| {:local_call, charlist}
|
||||
| {:module_attribute, charlist}
|
||||
| {:operator, charlist}
|
||||
| {:operator_arity, charlist}
|
||||
| {:operator_call, charlist}
|
||||
| :none
|
||||
| {:sigil, charlist}
|
||||
| {:struct, charlist}
|
||||
| {:unquoted_atom, charlist}
|
||||
when inside_dot:
|
||||
{:alias, charlist}
|
||||
| {:dot, inside_dot, charlist}
|
||||
| {:module_attribute, charlist}
|
||||
| {:unquoted_atom, charlist}
|
||||
| {:var, charlist}
|
||||
def cursor_context(fragment, opts \\ [])
|
||||
|
||||
def cursor_context(binary, opts) when is_binary(binary) and is_list(opts) do
|
||||
binary =
|
||||
case :binary.matches(binary, "\n") do
|
||||
[] ->
|
||||
binary
|
||||
|
||||
matches ->
|
||||
{position, _} = List.last(matches)
|
||||
binary_part(binary, position + 1, byte_size(binary) - position - 1)
|
||||
end
|
||||
|
||||
binary
|
||||
|> String.to_charlist()
|
||||
|> :lists.reverse()
|
||||
|> codepoint_cursor_context(opts)
|
||||
|> elem(0)
|
||||
end
|
||||
|
||||
def cursor_context(charlist, opts) when is_list(charlist) and is_list(opts) do
|
||||
charlist =
|
||||
case charlist |> Enum.chunk_by(&(&1 == ?\n)) |> List.last([]) do
|
||||
[?\n | _] -> []
|
||||
rest -> rest
|
||||
end
|
||||
|
||||
charlist
|
||||
|> :lists.reverse()
|
||||
|> codepoint_cursor_context(opts)
|
||||
|> elem(0)
|
||||
end
|
||||
|
||||
def cursor_context(other, opts) when is_list(opts) do
|
||||
cursor_context(to_charlist(other), opts)
|
||||
end
|
||||
|
||||
@operators '\\<>+-*/:=|&~^%!'
|
||||
@starter_punctuation ',([{;'
|
||||
@non_starter_punctuation ')]}"\'.$'
|
||||
@space '\t\s'
|
||||
@trailing_identifier '?!'
|
||||
@tilde_op_prefix '<=~'
|
||||
|
||||
@non_identifier @trailing_identifier ++
|
||||
@operators ++ @starter_punctuation ++ @non_starter_punctuation ++ @space
|
||||
|
||||
@textual_operators ~w(when not and or in)c
|
||||
@incomplete_operators ~w(^^ ~~ ~)c
|
||||
|
||||
defp codepoint_cursor_context(reverse, _opts) do
|
||||
{stripped, spaces} = strip_spaces(reverse, 0)
|
||||
|
||||
case stripped do
|
||||
# It is empty
|
||||
[] -> {:expr, 0}
|
||||
# Structs
|
||||
[?%, ?:, ?: | _] -> {{:struct, ''}, 1}
|
||||
[?%, ?: | _] -> {{:unquoted_atom, '%'}, 2}
|
||||
[?% | _] -> {{:struct, ''}, 1}
|
||||
# Token/AST only operators
|
||||
[?>, ?= | rest] when rest == [] or hd(rest) != ?: -> {:expr, 0}
|
||||
[?>, ?- | rest] when rest == [] or hd(rest) != ?: -> {:expr, 0}
|
||||
# Two-digit containers
|
||||
[?<, ?< | rest] when rest == [] or hd(rest) != ?< -> {:expr, 0}
|
||||
# Ambiguity around :
|
||||
[?: | rest] when rest == [] or hd(rest) != ?: -> unquoted_atom_or_expr(spaces)
|
||||
# Dots
|
||||
[?.] -> {:none, 0}
|
||||
[?. | rest] when hd(rest) not in '.:' -> dot(rest, spaces + 1, '')
|
||||
# It is a local or remote call with parens
|
||||
[?( | rest] -> call_to_cursor_context(strip_spaces(rest, spaces + 1))
|
||||
# A local arity definition
|
||||
[?/ | rest] -> arity_to_cursor_context(strip_spaces(rest, spaces + 1))
|
||||
# Starting a new expression
|
||||
[h | _] when h in @starter_punctuation -> {:expr, 0}
|
||||
# It is a local or remote call without parens
|
||||
rest when spaces > 0 -> call_to_cursor_context({rest, spaces})
|
||||
# It is an identifier
|
||||
_ -> identifier_to_cursor_context(reverse, 0, false)
|
||||
end
|
||||
end
|
||||
|
||||
defp strip_spaces([h | rest], count) when h in @space, do: strip_spaces(rest, count + 1)
|
||||
defp strip_spaces(rest, count), do: {rest, count}
|
||||
|
||||
defp unquoted_atom_or_expr(0), do: {{:unquoted_atom, ''}, 1}
|
||||
defp unquoted_atom_or_expr(_), do: {:expr, 0}
|
||||
|
||||
defp arity_to_cursor_context({reverse, spaces}) do
|
||||
case identifier_to_cursor_context(reverse, spaces, true) do
|
||||
{{:local_or_var, acc}, count} -> {{:local_arity, acc}, count}
|
||||
{{:dot, base, acc}, count} -> {{:dot_arity, base, acc}, count}
|
||||
{{:operator, acc}, count} -> {{:operator_arity, acc}, count}
|
||||
{_, _} -> {:none, 0}
|
||||
end
|
||||
end
|
||||
|
||||
defp call_to_cursor_context({reverse, spaces}) do
|
||||
case identifier_to_cursor_context(reverse, spaces, true) do
|
||||
{{:local_or_var, acc}, count} -> {{:local_call, acc}, count}
|
||||
{{:dot, base, acc}, count} -> {{:dot_call, base, acc}, count}
|
||||
{{:operator, acc}, count} -> {{:operator_call, acc}, count}
|
||||
{_, _} -> {:none, 0}
|
||||
end
|
||||
end
|
||||
|
||||
defp identifier_to_cursor_context([?., ?., ?: | _], n, _), do: {{:unquoted_atom, '..'}, n + 3}
|
||||
defp identifier_to_cursor_context([?., ?., ?. | _], n, _), do: {{:local_or_var, '...'}, n + 3}
|
||||
defp identifier_to_cursor_context([?., ?: | _], n, _), do: {{:unquoted_atom, '.'}, n + 2}
|
||||
defp identifier_to_cursor_context([?., ?. | _], n, _), do: {{:operator, '..'}, n + 2}
|
||||
|
||||
defp identifier_to_cursor_context(reverse, count, call_op?) do
|
||||
case identifier(reverse, count) do
|
||||
:none ->
|
||||
{:none, 0}
|
||||
|
||||
:operator ->
|
||||
operator(reverse, count, [], call_op?)
|
||||
|
||||
{:module_attribute, acc, count} ->
|
||||
{{:module_attribute, acc}, count}
|
||||
|
||||
{:sigil, acc, count} ->
|
||||
{{:sigil, acc}, count}
|
||||
|
||||
{:unquoted_atom, acc, count} ->
|
||||
{{:unquoted_atom, acc}, count}
|
||||
|
||||
{:alias, rest, acc, count} ->
|
||||
case strip_spaces(rest, count) do
|
||||
{'.' ++ rest, count} when rest == [] or hd(rest) != ?. ->
|
||||
nested_alias(rest, count + 1, acc)
|
||||
|
||||
{'%' ++ _, count} ->
|
||||
{{:struct, acc}, count + 1}
|
||||
|
||||
_ ->
|
||||
{{:alias, acc}, count}
|
||||
end
|
||||
|
||||
{:identifier, _, acc, count} when call_op? and acc in @textual_operators ->
|
||||
{{:operator, acc}, count}
|
||||
|
||||
{:identifier, rest, acc, count} ->
|
||||
case strip_spaces(rest, count) do
|
||||
{'.' ++ rest, count} when rest == [] or hd(rest) != ?. ->
|
||||
dot(rest, count + 1, acc)
|
||||
|
||||
_ ->
|
||||
{{:local_or_var, acc}, count}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp identifier([?? | rest], count), do: check_identifier(rest, count + 1, [??])
|
||||
defp identifier([?! | rest], count), do: check_identifier(rest, count + 1, [?!])
|
||||
defp identifier(rest, count), do: check_identifier(rest, count, [])
|
||||
|
||||
defp check_identifier([h | t], count, acc) when h not in @non_identifier,
|
||||
do: rest_identifier(t, count + 1, [h | acc])
|
||||
|
||||
defp check_identifier(_, _, _), do: :operator
|
||||
|
||||
defp rest_identifier([h | rest], count, acc) when h not in @non_identifier do
|
||||
rest_identifier(rest, count + 1, [h | acc])
|
||||
end
|
||||
|
||||
defp rest_identifier(rest, count, [?@ | acc]) do
|
||||
case tokenize_identifier(rest, count, acc) do
|
||||
{:identifier, _rest, acc, count} -> {:module_attribute, acc, count}
|
||||
:none when acc == [] -> {:module_attribute, '', count}
|
||||
_ -> :none
|
||||
end
|
||||
end
|
||||
|
||||
defp rest_identifier([?~ | rest], count, [letter])
|
||||
when (letter in ?A..?Z or letter in ?a..?z) and
|
||||
(rest == [] or hd(rest) not in @tilde_op_prefix) do
|
||||
{:sigil, [letter], count + 1}
|
||||
end
|
||||
|
||||
defp rest_identifier([?: | rest], count, acc) when rest == [] or hd(rest) != ?: do
|
||||
case String.Tokenizer.tokenize(acc) do
|
||||
{_, _, [], _, _, _} -> {:unquoted_atom, acc, count + 1}
|
||||
_ -> :none
|
||||
end
|
||||
end
|
||||
|
||||
defp rest_identifier([?? | _], _count, _acc) do
|
||||
:none
|
||||
end
|
||||
|
||||
defp rest_identifier(rest, count, acc) do
|
||||
tokenize_identifier(rest, count, acc)
|
||||
end
|
||||
|
||||
defp tokenize_identifier(rest, count, acc) do
|
||||
case String.Tokenizer.tokenize(acc) do
|
||||
# Not actually an atom cause rest is not a :
|
||||
{:atom, _, _, _, _, _} ->
|
||||
:none
|
||||
|
||||
# Aliases must be ascii only
|
||||
{:alias, _, _, _, false, _} ->
|
||||
:none
|
||||
|
||||
{kind, _, [], _, _, extra} ->
|
||||
if ?@ in extra do
|
||||
:none
|
||||
else
|
||||
{kind, rest, acc, count}
|
||||
end
|
||||
|
||||
_ ->
|
||||
:none
|
||||
end
|
||||
end
|
||||
|
||||
defp nested_alias(rest, count, acc) do
|
||||
{rest, count} = strip_spaces(rest, count)
|
||||
|
||||
case identifier_to_cursor_context(rest, count, true) do
|
||||
{{:struct, prev}, count} -> {{:struct, prev ++ '.' ++ acc}, count}
|
||||
{{:alias, prev}, count} -> {{:alias, prev ++ '.' ++ acc}, count}
|
||||
_ -> {:none, 0}
|
||||
end
|
||||
end
|
||||
|
||||
defp dot(rest, count, acc) do
|
||||
{rest, count} = strip_spaces(rest, count)
|
||||
|
||||
case identifier_to_cursor_context(rest, count, true) do
|
||||
{{:local_or_var, var}, count} -> {{:dot, {:var, var}, acc}, count}
|
||||
{{:unquoted_atom, _} = prev, count} -> {{:dot, prev, acc}, count}
|
||||
{{:alias, _} = prev, count} -> {{:dot, prev, acc}, count}
|
||||
{{:dot, _, _} = prev, count} -> {{:dot, prev, acc}, count}
|
||||
{{:module_attribute, _} = prev, count} -> {{:dot, prev, acc}, count}
|
||||
{{:struct, acc}, count} -> {{:struct, acc ++ '.'}, count}
|
||||
{_, _} -> {:none, 0}
|
||||
end
|
||||
end
|
||||
|
||||
defp operator([h | rest], count, acc, call_op?) when h in @operators do
|
||||
operator(rest, count + 1, [h | acc], call_op?)
|
||||
end
|
||||
|
||||
defp operator(rest, count, acc, call_op?) when acc in @incomplete_operators do
|
||||
{rest, dot_count} = strip_spaces(rest, count)
|
||||
|
||||
cond do
|
||||
call_op? ->
|
||||
{:none, 0}
|
||||
|
||||
match?([?. | rest] when rest == [] or hd(rest) != ?., rest) ->
|
||||
dot(tl(rest), dot_count + 1, acc)
|
||||
|
||||
acc == '~' ->
|
||||
{{:sigil, ''}, count}
|
||||
|
||||
true ->
|
||||
{{:operator, acc}, count}
|
||||
end
|
||||
end
|
||||
|
||||
# If we are opening a sigil, ignore the operator.
|
||||
defp operator([letter, ?~ | rest], _count, [op], _call_op?)
|
||||
when op in '<|/' and (letter in ?A..?Z or letter in ?a..?z) and
|
||||
(rest == [] or hd(rest) not in @tilde_op_prefix) do
|
||||
{:none, 0}
|
||||
end
|
||||
|
||||
defp operator(rest, count, acc, _call_op?) do
|
||||
case :elixir_tokenizer.tokenize(acc, 1, 1, []) do
|
||||
{:ok, _, _, _, [{:atom, _, _}]} ->
|
||||
{{:unquoted_atom, tl(acc)}, count}
|
||||
|
||||
{:ok, _, _, _, [{_, _, op}]} ->
|
||||
{rest, dot_count} = strip_spaces(rest, count)
|
||||
|
||||
cond do
|
||||
Code.Identifier.unary_op(op) == :error and Code.Identifier.binary_op(op) == :error ->
|
||||
:none
|
||||
|
||||
match?([?. | rest] when rest == [] or hd(rest) != ?., rest) ->
|
||||
dot(tl(rest), dot_count + 1, acc)
|
||||
|
||||
true ->
|
||||
{{:operator, acc}, count}
|
||||
end
|
||||
|
||||
_ ->
|
||||
{:none, 0}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a string and returns the surround context.
|
||||
|
||||
This function receives a string with an Elixir code fragment
|
||||
and a `position`. It returns a map containing the beginning
|
||||
and ending of the identifier alongside its context, or `:none`
|
||||
if there is nothing with a known context.
|
||||
|
||||
The difference between `cursor_context/2` and `surround_context/3`
|
||||
is that the former assumes the expression in the code fragment
|
||||
is incomplete. For example, `do` in `cursor_context/2` may be
|
||||
a keyword or a variable or a local call, while `surround_context/3`
|
||||
assumes the expression in the code fragment is complete, therefore
|
||||
`do` would always be a keyword.
|
||||
|
||||
The `position` contains both the `line` and `column`, both starting
|
||||
with the index of 1. The column must precede the surrounding expression.
|
||||
For example, the expression `foo`, will return something for the columns
|
||||
1, 2, and 3, but not 4:
|
||||
|
||||
foo
|
||||
^ column 1
|
||||
|
||||
foo
|
||||
^ column 2
|
||||
|
||||
foo
|
||||
^ column 3
|
||||
|
||||
foo
|
||||
^ column 4
|
||||
|
||||
The returned map contains the column the expression starts and the
|
||||
first column after the expression ends.
|
||||
|
||||
Similar to `cursor_context/2`, this function also provides a best-effort
|
||||
detection and may not be accurate under all circumstances. See the
|
||||
"Return values" and "Limitations" section under `cursor_context/2` for
|
||||
more information.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.Fragment.surround_context("foo", {1, 1})
|
||||
%{begin: {1, 1}, context: {:local_or_var, 'foo'}, end: {1, 4}}
|
||||
|
||||
## Differences to `cursor_context/2`
|
||||
|
||||
Because `surround_context/3` deals with complete code, it has some
|
||||
difference to `cursor_context/2`:
|
||||
|
||||
* `dot_call`/`dot_arity` and `operator_call`/`operator_arity`
|
||||
are collapsed into `dot` and `operator` contexts respectively
|
||||
as there aren't any meaningful distinctions between them
|
||||
|
||||
* On the other hand, this function still makes a distinction between
|
||||
`local_call`/`local_arity` and `local_or_var`, since the latter can
|
||||
be a local or variable
|
||||
|
||||
* `@` when not followed by any identifier is returned as `{:operator, '@'}`
|
||||
(in contrast to `{:module_attribute, ''}` in `cursor_context/2`
|
||||
|
||||
* This function never returns empty sigils `{:sigil, ''}` or empty structs
|
||||
`{:struct, ''}` as context
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec surround_context(List.Chars.t(), position(), keyword()) ::
|
||||
%{begin: position, end: position, context: context} | :none
|
||||
when context:
|
||||
{:alias, charlist}
|
||||
| {:dot, inside_dot, charlist}
|
||||
| {:local_or_var, charlist}
|
||||
| {:local_arity, charlist}
|
||||
| {:local_call, charlist}
|
||||
| {:module_attribute, charlist}
|
||||
| {:operator, charlist}
|
||||
| {:unquoted_atom, charlist},
|
||||
inside_dot:
|
||||
{:alias, charlist}
|
||||
| {:dot, inside_dot, charlist}
|
||||
| {:module_attribute, charlist}
|
||||
| {:unquoted_atom, charlist}
|
||||
| {:var, charlist}
|
||||
def surround_context(fragment, position, options \\ [])
|
||||
|
||||
def surround_context(binary, {line, column}, opts) when is_binary(binary) do
|
||||
binary
|
||||
|> String.split("\n")
|
||||
|> Enum.at(line - 1, '')
|
||||
|> String.to_charlist()
|
||||
|> position_surround_context(line, column, opts)
|
||||
end
|
||||
|
||||
def surround_context(charlist, {line, column}, opts) when is_list(charlist) do
|
||||
charlist
|
||||
|> :string.split('\n', :all)
|
||||
|> Enum.at(line - 1, '')
|
||||
|> position_surround_context(line, column, opts)
|
||||
end
|
||||
|
||||
def surround_context(other, {_, _} = position, opts) do
|
||||
surround_context(to_charlist(other), position, opts)
|
||||
end
|
||||
|
||||
defp position_surround_context(charlist, line, column, opts)
|
||||
when is_integer(line) and line >= 1 and is_integer(column) and column >= 1 do
|
||||
{reversed_pre, post} = string_reverse_at(charlist, column - 1, [])
|
||||
{reversed_pre, post} = adjust_position(reversed_pre, post)
|
||||
|
||||
case take_identifier(post, []) do
|
||||
{_, [], _} ->
|
||||
maybe_operator(reversed_pre, post, line, opts)
|
||||
|
||||
{:identifier, reversed_post, rest} ->
|
||||
{rest, _} = strip_spaces(rest, 0)
|
||||
reversed = reversed_post ++ reversed_pre
|
||||
|
||||
case codepoint_cursor_context(reversed, opts) do
|
||||
{{:struct, acc}, offset} ->
|
||||
build_surround({:struct, acc}, reversed, line, offset)
|
||||
|
||||
{{:alias, acc}, offset} ->
|
||||
build_surround({:alias, acc}, reversed, line, offset)
|
||||
|
||||
{{:dot, _, [_ | _]} = dot, offset} ->
|
||||
build_surround(dot, reversed, line, offset)
|
||||
|
||||
{{:local_or_var, acc}, offset} when hd(rest) == ?( ->
|
||||
build_surround({:local_call, acc}, reversed, line, offset)
|
||||
|
||||
{{:local_or_var, acc}, offset} when hd(rest) == ?/ ->
|
||||
build_surround({:local_arity, acc}, reversed, line, offset)
|
||||
|
||||
{{:local_or_var, acc}, offset} when acc in @textual_operators ->
|
||||
build_surround({:operator, acc}, reversed, line, offset)
|
||||
|
||||
{{:local_or_var, acc}, offset} when acc not in ~w(do end after else catch rescue)c ->
|
||||
build_surround({:local_or_var, acc}, reversed, line, offset)
|
||||
|
||||
{{:module_attribute, ''}, offset} ->
|
||||
build_surround({:operator, '@'}, reversed, line, offset)
|
||||
|
||||
{{:module_attribute, acc}, offset} ->
|
||||
build_surround({:module_attribute, acc}, reversed, line, offset)
|
||||
|
||||
{{:sigil, acc}, offset} ->
|
||||
build_surround({:sigil, acc}, reversed, line, offset)
|
||||
|
||||
{{:unquoted_atom, acc}, offset} ->
|
||||
build_surround({:unquoted_atom, acc}, reversed, line, offset)
|
||||
|
||||
_ ->
|
||||
maybe_operator(reversed_pre, post, line, opts)
|
||||
end
|
||||
|
||||
{:alias, reversed_post, _rest} ->
|
||||
reversed = reversed_post ++ reversed_pre
|
||||
|
||||
case codepoint_cursor_context(reversed, opts) do
|
||||
{{:alias, acc}, offset} ->
|
||||
build_surround({:alias, acc}, reversed, line, offset)
|
||||
|
||||
{{:struct, acc}, offset} ->
|
||||
build_surround({:struct, acc}, reversed, line, offset)
|
||||
|
||||
_ ->
|
||||
:none
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp maybe_operator(reversed_pre, post, line, opts) do
|
||||
case take_operator(post, []) do
|
||||
{[], _rest} ->
|
||||
:none
|
||||
|
||||
{reversed_post, rest} ->
|
||||
reversed = reversed_post ++ reversed_pre
|
||||
|
||||
case codepoint_cursor_context(reversed, opts) do
|
||||
{{:operator, acc}, offset} when acc not in @incomplete_operators ->
|
||||
build_surround({:operator, acc}, reversed, line, offset)
|
||||
|
||||
{{:sigil, ''}, offset} when hd(rest) in ?A..?Z or hd(rest) in ?a..?z ->
|
||||
build_surround({:sigil, [hd(rest)]}, [hd(rest) | reversed], line, offset + 1)
|
||||
|
||||
{{:dot, _, [_ | _]} = dot, offset} ->
|
||||
build_surround(dot, reversed, line, offset)
|
||||
|
||||
_ ->
|
||||
:none
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp build_surround(context, reversed, line, offset) do
|
||||
{post, reversed_pre} = enum_reverse_at(reversed, offset, [])
|
||||
pre = :lists.reverse(reversed_pre)
|
||||
pre_length = :string.length(pre) + 1
|
||||
|
||||
%{
|
||||
context: context,
|
||||
begin: {line, pre_length},
|
||||
end: {line, pre_length + :string.length(post)}
|
||||
}
|
||||
end
|
||||
|
||||
defp take_identifier([h | t], acc) when h in @trailing_identifier,
|
||||
do: {:identifier, [h | acc], t}
|
||||
|
||||
defp take_identifier([h | t], acc) when h not in @non_identifier,
|
||||
do: take_identifier(t, [h | acc])
|
||||
|
||||
defp take_identifier(rest, acc) do
|
||||
with {[?. | t], _} <- strip_spaces(rest, 0),
|
||||
{[h | _], _} when h in ?A..?Z <- strip_spaces(t, 0) do
|
||||
take_alias(rest, acc)
|
||||
else
|
||||
_ -> {:identifier, acc, rest}
|
||||
end
|
||||
end
|
||||
|
||||
defp take_alias([h | t], acc) when h not in @non_identifier,
|
||||
do: take_alias(t, [h | acc])
|
||||
|
||||
defp take_alias(rest, acc) do
|
||||
with {[?. | t], acc} <- move_spaces(rest, acc),
|
||||
{[h | t], acc} when h in ?A..?Z <- move_spaces(t, [?. | acc]) do
|
||||
take_alias(t, [h | acc])
|
||||
else
|
||||
_ -> {:alias, acc, rest}
|
||||
end
|
||||
end
|
||||
|
||||
defp take_operator([h | t], acc) when h in @operators, do: take_operator(t, [h | acc])
|
||||
defp take_operator([h | t], acc) when h == ?., do: take_operator(t, [h | acc])
|
||||
defp take_operator(rest, acc), do: {acc, rest}
|
||||
|
||||
# Unquoted atom handling
|
||||
defp adjust_position(reversed_pre, [?: | post])
|
||||
when hd(post) != ?: and (reversed_pre == [] or hd(reversed_pre) != ?:) do
|
||||
{[?: | reversed_pre], post}
|
||||
end
|
||||
|
||||
defp adjust_position(reversed_pre, [?% | post]) do
|
||||
adjust_position([?% | reversed_pre], post)
|
||||
end
|
||||
|
||||
# Dot/struct handling
|
||||
defp adjust_position(reversed_pre, post) do
|
||||
case move_spaces(post, reversed_pre) do
|
||||
# If we are between spaces and a dot, move past the dot
|
||||
{[?. | post], reversed_pre} when hd(post) != ?. and hd(reversed_pre) != ?. ->
|
||||
{post, reversed_pre} = move_spaces(post, [?. | reversed_pre])
|
||||
{reversed_pre, post}
|
||||
|
||||
_ ->
|
||||
case strip_spaces(reversed_pre, 0) do
|
||||
# If there is a dot to our left, make sure to move to the first character
|
||||
{[?. | rest], _} when rest == [] or hd(rest) not in '.:' ->
|
||||
{post, reversed_pre} = move_spaces(post, reversed_pre)
|
||||
{reversed_pre, post}
|
||||
|
||||
# If there is a % to our left, make sure to move to the first character
|
||||
{[?% | _], _} ->
|
||||
case move_spaces(post, reversed_pre) do
|
||||
{[h | _] = post, reversed_pre} when h in ?A..?Z ->
|
||||
{reversed_pre, post}
|
||||
|
||||
_ ->
|
||||
{reversed_pre, post}
|
||||
end
|
||||
|
||||
_ ->
|
||||
{reversed_pre, post}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp move_spaces([h | t], acc) when h in @space, do: move_spaces(t, [h | acc])
|
||||
defp move_spaces(t, acc), do: {t, acc}
|
||||
|
||||
defp string_reverse_at(charlist, 0, acc), do: {acc, charlist}
|
||||
|
||||
defp string_reverse_at(charlist, n, acc) do
|
||||
case :unicode_util.gc(charlist) do
|
||||
[gc | cont] when is_integer(gc) -> string_reverse_at(cont, n - 1, [gc | acc])
|
||||
[gc | cont] when is_list(gc) -> string_reverse_at(cont, n - 1, :lists.reverse(gc, acc))
|
||||
[] -> {acc, []}
|
||||
end
|
||||
end
|
||||
|
||||
defp enum_reverse_at([h | t], n, acc) when n > 0, do: enum_reverse_at(t, n - 1, [h | acc])
|
||||
defp enum_reverse_at(rest, _, acc), do: {acc, rest}
|
||||
|
||||
@doc """
|
||||
Receives a code fragment and returns a quoted expression
|
||||
with a cursor at the nearest argument position.
|
||||
|
||||
A container is any Elixir expression starting with `(`,
|
||||
`{`, and `[`. This includes function calls, tuples, lists,
|
||||
maps, and so on. For example, take this code, which would
|
||||
be given as input:
|
||||
|
||||
max(some_value,
|
||||
|
||||
This function will return the AST equivalent to:
|
||||
|
||||
max(some_value, __cursor__())
|
||||
|
||||
In other words, this function is capable of closing any open
|
||||
brackets and insert the cursor position. Any content at the
|
||||
cursor position that is after a comma or an opening bracket
|
||||
is discarded. For example, if this is given as input:
|
||||
|
||||
max(some_value, another_val
|
||||
|
||||
It will return the same AST:
|
||||
|
||||
max(some_value, __cursor__())
|
||||
|
||||
Similarly, if only this is given:
|
||||
|
||||
max(some_va
|
||||
|
||||
Then it returns:
|
||||
|
||||
max(__cursor__())
|
||||
|
||||
Calls without parenthesis are also supported, as we assume the
|
||||
brackets are implicit.
|
||||
|
||||
Operators and anonymous functions are not containers, and therefore
|
||||
will be discarded. The following will all return the same AST:
|
||||
|
||||
max(some_value,
|
||||
max(some_value, fn x -> x end
|
||||
max(some_value, 1 + another_val
|
||||
max(some_value, 1 |> some_fun() |> another_fun
|
||||
|
||||
On the other hand, tuples, lists, maps, etc all retain the
|
||||
cursor position:
|
||||
|
||||
max(some_value, [1, 2,
|
||||
|
||||
Returns the following AST:
|
||||
|
||||
max(some_value, [1, 2, __cursor__()])
|
||||
|
||||
Keyword lists (and do-end blocks) are also retained. The following:
|
||||
|
||||
if(some_value, do:
|
||||
if(some_value, do: :token
|
||||
if(some_value, do: 1 + val
|
||||
|
||||
all return:
|
||||
|
||||
if(some_value, do: __cursor__())
|
||||
|
||||
The AST returned by this function is not safe to evaluate but
|
||||
it can be analyzed and expanded.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.Fragment.container_cursor_to_quoted("max(some_value, ")
|
||||
{:ok, {:max, [line: 1], [{:some_value, [line: 1], nil}, {:__cursor__, [line: 1], []}]}}
|
||||
|
||||
## Options
|
||||
|
||||
* `:file` - the filename to be reported in case of parsing errors.
|
||||
Defaults to `"nofile"`.
|
||||
|
||||
* `:line` - the starting line of the string being parsed.
|
||||
Defaults to 1.
|
||||
|
||||
* `:column` - the starting column of the string being parsed.
|
||||
Defaults to 1.
|
||||
|
||||
* `:columns` - when `true`, attach a `:column` key to the quoted
|
||||
metadata. Defaults to `false`.
|
||||
|
||||
* `:token_metadata` - when `true`, includes token-related
|
||||
metadata in the expression AST, such as metadata for `do` and `end`
|
||||
tokens, for closing tokens, end of expressions, as well as delimiters
|
||||
for sigils. See `t:Macro.metadata/0`. Defaults to `false`.
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec container_cursor_to_quoted(List.Chars.t(), keyword()) ::
|
||||
{:ok, Macro.t()} | {:error, {location :: keyword, binary | {binary, binary}, binary}}
|
||||
def container_cursor_to_quoted(fragment, opts \\ []) do
|
||||
file = Keyword.get(opts, :file, "nofile")
|
||||
line = Keyword.get(opts, :line, 1)
|
||||
column = Keyword.get(opts, :column, 1)
|
||||
columns = Keyword.get(opts, :columns, false)
|
||||
token_metadata = Keyword.get(opts, :token_metadata, false)
|
||||
|
||||
fragment = to_charlist(fragment)
|
||||
tokenizer_opts = [file: file, cursor_completion: true, columns: columns]
|
||||
|
||||
case :elixir_tokenizer.tokenize(fragment, line, column, tokenizer_opts) do
|
||||
{:ok, _, _, _warnings, tokens} ->
|
||||
:elixir.tokens_to_quoted(tokens, file, columns: columns, token_metadata: token_metadata)
|
||||
|
||||
{:error, {line, column, {prefix, suffix}, token}, _rest, _warnings, _so_far} ->
|
||||
location = [line: line, column: column]
|
||||
{:error, {location, {to_string(prefix), to_string(suffix)}, to_string(token)}}
|
||||
|
||||
{:error, {line, column, error, token}, _rest, _warnings, _so_far} ->
|
||||
location = [line: line, column: column]
|
||||
{:error, {location, to_string(error), to_string(token)}}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -34,21 +34,19 @@ defmodule Code.Identifier do
|
||||
cond do
|
||||
op in [:<-, :\\] -> {:left, 40}
|
||||
op in [:when] -> {:right, 50}
|
||||
op in [:"::"] -> {:right, 60}
|
||||
op in [:::] -> {:right, 60}
|
||||
op in [:|] -> {:right, 70}
|
||||
op in [:=] -> {:right, 100}
|
||||
op in [:||, :|||, :or] -> {:left, 120}
|
||||
op in [:&&, :&&&, :and] -> {:left, 130}
|
||||
op in [:==, :!=, :=~, :===, :!==] -> {:left, 140}
|
||||
op in [:<, :<=, :>=, :>] -> {:left, 150}
|
||||
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>] -> {:left, 160}
|
||||
op in [:in] -> {:left, 170}
|
||||
op in [:^^^] -> {:left, 180}
|
||||
op in [:"//"] -> {:right, 190}
|
||||
op in [:++, :--, :.., :<>, :+++, :---] -> {:right, 200}
|
||||
op in [:||, :|||, :or] -> {:left, 130}
|
||||
op in [:&&, :&&&, :and] -> {:left, 140}
|
||||
op in [:==, :!=, :=~, :===, :!==] -> {:left, 150}
|
||||
op in [:<, :<=, :>=, :>] -> {:left, 160}
|
||||
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>] -> {:left, 170}
|
||||
op in [:in] -> {:left, 180}
|
||||
op in [:^^^] -> {:left, 190}
|
||||
op in [:++, :--, :.., :<>] -> {:right, 200}
|
||||
op in [:+, :-] -> {:left, 210}
|
||||
op in [:*, :/] -> {:left, 220}
|
||||
op in [:**] -> {:left, 230}
|
||||
op in [:.] -> {:left, 310}
|
||||
true -> :error
|
||||
end
|
||||
@@ -62,19 +60,14 @@ defmodule Code.Identifier do
|
||||
* `:callable_local` - an atom that can be used as a local call;
|
||||
this category includes identifiers like `:foo`
|
||||
|
||||
* `:callable_operator` - all callable operators, such as `:<>`. Note
|
||||
* `:callable_operators` - all callable operators, such as `:<>`. Note
|
||||
operators such as `:..` are not callable because of ambiguity
|
||||
|
||||
* `:not_atomable` - callable operators that must be wrapped in quotes when
|
||||
defined as an atom. For example, `::` must be written as `:"::"` to avoid
|
||||
the ambiguity between the atom and the keyword identifier
|
||||
|
||||
* `:not_callable` - an atom that cannot be used as a function call after the
|
||||
`.` operator. Those are typically AST nodes that are special forms (such as
|
||||
`:%{}` and `:<<>>>`) as well as nodes that are ambiguous in calls (such as
|
||||
`:..` and `:...`). This category also includes atoms like `:Foo`, since
|
||||
they are valid identifiers but they need quotes to be used in function
|
||||
calls (`Foo."Bar"`)
|
||||
`.` operator (for example, `:<<>>` is not callable because `Foo.<<>>` is a
|
||||
syntax error); this category includes atoms like `:Foo`, since they are
|
||||
valid identifiers but they need quotes to be used in function calls
|
||||
(`Foo."Bar"`)
|
||||
|
||||
* `:other` - any other atom (these are usually escaped when inspected, like
|
||||
`:"foo and bar"`)
|
||||
@@ -84,12 +77,9 @@ defmodule Code.Identifier do
|
||||
charlist = Atom.to_charlist(atom)
|
||||
|
||||
cond do
|
||||
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :"..//", :->] ->
|
||||
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :->] ->
|
||||
:not_callable
|
||||
|
||||
atom in [:"::", :"//"] ->
|
||||
:not_atomable
|
||||
|
||||
unary_op(atom) != :error or binary_op(atom) != :error ->
|
||||
:callable_operator
|
||||
|
||||
@@ -97,7 +87,7 @@ defmodule Code.Identifier do
|
||||
:alias
|
||||
|
||||
true ->
|
||||
case :elixir_config.identifier_tokenizer().tokenize(charlist) do
|
||||
case :elixir_config.get(:identifier_tokenizer, String.Tokenizer).tokenize(charlist) do
|
||||
{kind, _acc, [], _, _, special} ->
|
||||
if kind == :identifier and not :lists.member(?@, special) do
|
||||
:callable_local
|
||||
@@ -153,7 +143,7 @@ defmodule Code.Identifier do
|
||||
type when type in [:callable_local, :callable_operator, :not_callable] ->
|
||||
":" <> binary
|
||||
|
||||
_ ->
|
||||
:other ->
|
||||
{escaped, _} = escape(binary, ?")
|
||||
IO.iodata_to_binary([?:, ?", escaped, ?"])
|
||||
end
|
||||
@@ -182,7 +172,7 @@ defmodule Code.Identifier do
|
||||
binary = Atom.to_string(atom)
|
||||
|
||||
case classify(atom) do
|
||||
type when type in [:callable_local, :callable_operator, :not_atomable] ->
|
||||
type when type in [:callable_local, :callable_operator] ->
|
||||
binary
|
||||
|
||||
type ->
|
||||
|
||||
@@ -1,581 +0,0 @@
|
||||
defmodule Code.Normalizer do
|
||||
@moduledoc false
|
||||
|
||||
defguard is_literal(x)
|
||||
when is_integer(x) or
|
||||
is_float(x) or
|
||||
is_binary(x) or
|
||||
is_atom(x)
|
||||
|
||||
@doc """
|
||||
Wraps literals in the quoted expression to conform to the AST format expected
|
||||
by the formatter.
|
||||
"""
|
||||
def normalize(quoted, opts \\ []) do
|
||||
line = Keyword.get(opts, :line, nil)
|
||||
escape = Keyword.get(opts, :escape, true)
|
||||
locals_without_parens = Keyword.get(opts, :locals_without_parens, [])
|
||||
|
||||
state = %{
|
||||
escape: escape,
|
||||
parent_meta: [line: line],
|
||||
locals_without_parens: locals_without_parens ++ Code.Formatter.locals_without_parens()
|
||||
}
|
||||
|
||||
do_normalize(quoted, state)
|
||||
end
|
||||
|
||||
# Wrapped literals should receive the block meta
|
||||
defp do_normalize({:__block__, meta, [literal]}, state)
|
||||
when not is_tuple(literal) or tuple_size(literal) == 2 do
|
||||
normalize_literal(literal, meta, state)
|
||||
end
|
||||
|
||||
# Only normalize the first argument of an alias if it's not an atom
|
||||
defp do_normalize({:__aliases__, meta, [first | rest]}, state) when not is_atom(first) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
first = do_normalize(first, %{state | parent_meta: meta})
|
||||
{:__aliases__, meta, [first | rest]}
|
||||
end
|
||||
|
||||
defp do_normalize({:__aliases__, _, _} = quoted, _state) do
|
||||
quoted
|
||||
end
|
||||
|
||||
# Skip captured arguments like &1
|
||||
defp do_normalize({:&, meta, [term]}, state) when is_integer(term) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
{:&, meta, [term]}
|
||||
end
|
||||
|
||||
# Ranges
|
||||
defp do_normalize(left..right//step, state) do
|
||||
left = do_normalize(left, state)
|
||||
right = do_normalize(right, state)
|
||||
meta = meta_line(state)
|
||||
|
||||
if step == 1 do
|
||||
{:.., meta, [left, right]}
|
||||
else
|
||||
step = do_normalize(step, state)
|
||||
{:"..//", meta, [left, right, step]}
|
||||
end
|
||||
end
|
||||
|
||||
# Bit containers
|
||||
defp do_normalize({:<<>>, _, args} = quoted, state) when is_list(args) do
|
||||
normalize_bitstring(quoted, state)
|
||||
end
|
||||
|
||||
# Atoms with interpolations
|
||||
defp do_normalize(
|
||||
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta,
|
||||
[{:<<>>, _, args} = string, :utf8]},
|
||||
state
|
||||
)
|
||||
when is_list(args) do
|
||||
dot_meta = patch_meta_line(dot_meta, state.parent_meta)
|
||||
call_meta = patch_meta_line(call_meta, dot_meta)
|
||||
|
||||
string =
|
||||
if state.escape do
|
||||
normalize_bitstring(string, state, true)
|
||||
else
|
||||
normalize_bitstring(string, state)
|
||||
end
|
||||
|
||||
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta, [string, :utf8]}
|
||||
end
|
||||
|
||||
# Charlists with interpolations
|
||||
defp do_normalize({{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]}, state) do
|
||||
parts =
|
||||
Enum.map(parts, fn
|
||||
{{:., part_dot_meta, [Kernel, :to_string]}, part_call_meta, args} ->
|
||||
args = normalize_args(args, state)
|
||||
|
||||
{{:., part_dot_meta, [Kernel, :to_string]}, part_call_meta, args}
|
||||
|
||||
part ->
|
||||
if state.escape do
|
||||
maybe_escape_literal(part, state)
|
||||
else
|
||||
part
|
||||
end
|
||||
end)
|
||||
|
||||
{{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]}
|
||||
end
|
||||
|
||||
# Don't normalize the `Access` atom in access syntax
|
||||
defp do_normalize({:., meta, [Access, :get]}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
{:., meta, [Access, :get]}
|
||||
end
|
||||
|
||||
# Only normalize the left side of the dot operator
|
||||
# The right hand side is an atom in the AST but it's not an atom literal, so
|
||||
# it should not be wrapped
|
||||
defp do_normalize({:., meta, [left, right]}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
|
||||
left = do_normalize(left, %{state | parent_meta: meta})
|
||||
|
||||
{:., meta, [left, right]}
|
||||
end
|
||||
|
||||
# A list of left to right arrows is not considered as a list literal, so it's not wrapped
|
||||
defp do_normalize([{:->, _, [_ | _]} | _] = quoted, state) do
|
||||
normalize_args(quoted, state)
|
||||
end
|
||||
|
||||
# left -> right
|
||||
defp do_normalize({:->, meta, [left, right]}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
|
||||
left = normalize_args(left, %{state | parent_meta: meta})
|
||||
right = do_normalize(right, %{state | parent_meta: meta})
|
||||
{:->, meta, [left, right]}
|
||||
end
|
||||
|
||||
# Maps
|
||||
defp do_normalize({:%{}, meta, args}, state) when is_list(args) do
|
||||
meta =
|
||||
if meta == [] do
|
||||
line = state.parent_meta[:line]
|
||||
[line: line, closing: [line: line]]
|
||||
else
|
||||
meta
|
||||
end
|
||||
|
||||
state = %{state | parent_meta: meta}
|
||||
|
||||
args =
|
||||
case args do
|
||||
[{:|, pipe_meta, [left, right]}] ->
|
||||
left = do_normalize(left, state)
|
||||
right = normalize_map_args(right, state)
|
||||
[{:|, pipe_meta, [left, right]}]
|
||||
|
||||
[{_, _, _} = call] ->
|
||||
[do_normalize(call, state)]
|
||||
|
||||
args ->
|
||||
normalize_map_args(args, state)
|
||||
end
|
||||
|
||||
{:%{}, meta, args}
|
||||
end
|
||||
|
||||
# Sigils
|
||||
defp do_normalize({sigil, meta, [{:<<>>, _, args} = string, modifiers]} = quoted, state)
|
||||
when is_list(args) and is_atom(sigil) do
|
||||
case Atom.to_string(sigil) do
|
||||
<<"sigil_", _name>> ->
|
||||
meta =
|
||||
meta
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
|> Keyword.put_new(:delimiter, "\"")
|
||||
|
||||
{sigil, meta, [do_normalize(string, %{state | parent_meta: meta}), modifiers]}
|
||||
|
||||
_ ->
|
||||
normalize_call(quoted, state)
|
||||
end
|
||||
end
|
||||
|
||||
# Tuples
|
||||
defp do_normalize({:{}, meta, args} = quoted, state) when is_list(args) do
|
||||
{last_arg, args} = List.pop_at(args, -1)
|
||||
|
||||
if args != [] and match?([_ | _], last_arg) and keyword?(last_arg) do
|
||||
args = normalize_args(args, state)
|
||||
kw_list = normalize_kw_args(last_arg, state, true)
|
||||
{:{}, meta, args ++ kw_list}
|
||||
else
|
||||
normalize_call(quoted, state)
|
||||
end
|
||||
end
|
||||
|
||||
# Module attributes
|
||||
defp do_normalize({:@, meta, [{name, name_meta, [value]}]}, state) do
|
||||
value =
|
||||
cond do
|
||||
keyword?(value) ->
|
||||
normalize_kw_args(value, state, true)
|
||||
|
||||
is_list(value) ->
|
||||
normalize_literal(value, meta, state)
|
||||
|
||||
true ->
|
||||
do_normalize(value, state)
|
||||
end
|
||||
|
||||
{:@, meta, [{name, name_meta, [value]}]}
|
||||
end
|
||||
|
||||
# Regular blocks
|
||||
defp do_normalize({:__block__, meta, args}, state) when is_list(args) do
|
||||
{:__block__, meta, normalize_args(args, state)}
|
||||
end
|
||||
|
||||
# Calls
|
||||
defp do_normalize({_, _, args} = quoted, state) when is_list(args) do
|
||||
normalize_call(quoted, state)
|
||||
end
|
||||
|
||||
# Vars
|
||||
defp do_normalize({_, _, context} = quoted, _state) when is_atom(context) do
|
||||
quoted
|
||||
end
|
||||
|
||||
# Literals
|
||||
defp do_normalize(quoted, state) do
|
||||
normalize_literal(quoted, [], state)
|
||||
end
|
||||
|
||||
# Numbers
|
||||
defp normalize_literal(number, meta, state) when is_number(number) do
|
||||
meta =
|
||||
meta
|
||||
|> Keyword.put_new(:token, inspect(number))
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
|
||||
{:__block__, meta, [number]}
|
||||
end
|
||||
|
||||
# Atom, Strings
|
||||
defp normalize_literal(literal, meta, state) when is_atom(literal) or is_binary(literal) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
literal = maybe_escape_literal(literal, state)
|
||||
|
||||
if is_atom(literal) and Code.Identifier.classify(literal) == :alias and
|
||||
is_nil(meta[:delimiter]) do
|
||||
"Elixir." <> segments = Atom.to_string(literal)
|
||||
|
||||
segments =
|
||||
segments
|
||||
|> String.split(".")
|
||||
|> Enum.map(&String.to_atom/1)
|
||||
|
||||
{:__aliases__, meta, segments}
|
||||
else
|
||||
{:__block__, meta, [literal]}
|
||||
end
|
||||
end
|
||||
|
||||
# 2-tuples
|
||||
defp normalize_literal({left, right}, meta, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
state = %{state | parent_meta: meta}
|
||||
|
||||
if match?([_ | _], right) and keyword?(right) do
|
||||
{:__block__, meta, [{do_normalize(left, state), normalize_kw_args(right, state, true)}]}
|
||||
else
|
||||
{:__block__, meta, [{do_normalize(left, state), do_normalize(right, state)}]}
|
||||
end
|
||||
end
|
||||
|
||||
# Lists
|
||||
defp normalize_literal(list, meta, state) when is_list(list) do
|
||||
if list != [] and List.ascii_printable?(list) do
|
||||
# It's a charlist
|
||||
list =
|
||||
if state.escape do
|
||||
{string, _} = Code.Identifier.escape(IO.chardata_to_string(list), -1)
|
||||
IO.iodata_to_binary(string) |> to_charlist()
|
||||
else
|
||||
list
|
||||
end
|
||||
|
||||
meta =
|
||||
meta
|
||||
|> Keyword.put_new(:delimiter, "'")
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
|
||||
{:__block__, meta, [list]}
|
||||
else
|
||||
meta =
|
||||
if line = state.parent_meta[:line] do
|
||||
meta
|
||||
|> Keyword.put_new(:closing, line: line)
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
else
|
||||
meta
|
||||
end
|
||||
|
||||
{:__block__, meta, [normalize_kw_args(list, state, false)]}
|
||||
end
|
||||
end
|
||||
|
||||
# Probably an invalid value, wrap it and send it upstream
|
||||
defp normalize_literal(quoted, meta, _state) do
|
||||
{:__block__, meta, [quoted]}
|
||||
end
|
||||
|
||||
defp normalize_call({form, meta, args}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
arity = length(args)
|
||||
|
||||
# Only normalize the form if it's a qualified call
|
||||
form =
|
||||
if is_atom(form) do
|
||||
form
|
||||
else
|
||||
do_normalize(form, %{state | parent_meta: meta})
|
||||
end
|
||||
|
||||
meta =
|
||||
if is_nil(meta[:no_parens]) and is_nil(meta[:closing]) and is_nil(meta[:do]) and
|
||||
not Code.Formatter.local_without_parens?(form, arity, state.locals_without_parens) do
|
||||
[closing: [line: meta[:line]]] ++ meta
|
||||
else
|
||||
meta
|
||||
end
|
||||
|
||||
cond do
|
||||
Keyword.has_key?(meta, :do) or match?([{{:__block__, _, [:do]}, _} | _], List.last(args)) ->
|
||||
# def foo do :ok end
|
||||
# def foo, do: :ok
|
||||
normalize_kw_blocks(form, meta, args, state)
|
||||
|
||||
match?([{:do, _} | _], List.last(args)) ->
|
||||
# Non normalized kw blocks
|
||||
line = state.parent_meta[:line]
|
||||
meta = meta ++ [do: [line: line], end: [line: line]]
|
||||
normalize_kw_blocks(form, meta, args, state)
|
||||
|
||||
allow_keyword?(form, arity) ->
|
||||
args = normalize_args(args, %{state | parent_meta: state.parent_meta})
|
||||
{last_arg, leading_args} = List.pop_at(args, -1, [])
|
||||
|
||||
last_args =
|
||||
case last_arg do
|
||||
{:__block__, _, [[{{:__block__, key_meta, _}, _} | _]] = last_args} ->
|
||||
if key_meta[:format] == :keyword do
|
||||
last_args
|
||||
else
|
||||
[last_arg]
|
||||
end
|
||||
|
||||
[] ->
|
||||
[]
|
||||
|
||||
_ ->
|
||||
[last_arg]
|
||||
end
|
||||
|
||||
{form, meta, leading_args ++ last_args}
|
||||
|
||||
true ->
|
||||
args = normalize_args(args, %{state | parent_meta: state.parent_meta})
|
||||
{form, meta, args}
|
||||
end
|
||||
end
|
||||
|
||||
defp allow_keyword?(:when, 2), do: true
|
||||
defp allow_keyword?(:{}, _), do: false
|
||||
defp allow_keyword?(op, arity), do: not is_atom(op) or not Macro.operator?(op, arity)
|
||||
|
||||
defp normalize_bitstring({:<<>>, meta, parts} = quoted, state, escape_interpolation \\ false) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
|
||||
parts =
|
||||
if interpolated?(quoted) do
|
||||
normalize_interpolation_parts(parts, %{state | parent_meta: meta}, escape_interpolation)
|
||||
else
|
||||
state = %{state | parent_meta: meta}
|
||||
|
||||
Enum.map(parts, fn part ->
|
||||
with {:"::", meta, [left, _]} <- part,
|
||||
true <- meta[:inferred_bitstring_spec] do
|
||||
do_normalize(left, state)
|
||||
else
|
||||
_ -> do_normalize(part, state)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
{:<<>>, meta, parts}
|
||||
end
|
||||
|
||||
defp normalize_interpolation_parts(parts, state, escape_interpolation) do
|
||||
Enum.map(parts, fn
|
||||
{:"::", interpolation_meta,
|
||||
[
|
||||
{{:., dot_meta, [Kernel, :to_string]}, middle_meta, [middle]},
|
||||
{:binary, binary_meta, context}
|
||||
]} ->
|
||||
middle = do_normalize(middle, %{state | parent_meta: dot_meta})
|
||||
|
||||
{:"::", interpolation_meta,
|
||||
[
|
||||
{{:., dot_meta, [Kernel, :to_string]}, middle_meta, [middle]},
|
||||
{:binary, binary_meta, context}
|
||||
]}
|
||||
|
||||
part ->
|
||||
if escape_interpolation do
|
||||
maybe_escape_literal(part, state)
|
||||
else
|
||||
part
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp normalize_map_args(args, state) do
|
||||
Enum.map(normalize_kw_args(args, state, false), fn
|
||||
{:__block__, _, [{_, _} = pair]} -> pair
|
||||
pair -> pair
|
||||
end)
|
||||
end
|
||||
|
||||
defp normalize_kw_blocks(form, meta, args, state) do
|
||||
{kw_blocks, leading_args} = List.pop_at(args, -1)
|
||||
|
||||
kw_blocks =
|
||||
Enum.map(kw_blocks, fn {tag, block} ->
|
||||
block = do_normalize(block, %{state | parent_meta: meta})
|
||||
|
||||
block =
|
||||
case block do
|
||||
{_, _, [[{:->, _, _} | _] = block]} -> block
|
||||
block -> block
|
||||
end
|
||||
|
||||
# Only wrap the tag if it isn't already wrapped
|
||||
tag =
|
||||
case tag do
|
||||
{:__block__, _, _} -> tag
|
||||
_ -> {:__block__, [line: meta[:line]], [tag]}
|
||||
end
|
||||
|
||||
{tag, block}
|
||||
end)
|
||||
|
||||
leading_args = normalize_args(leading_args, %{state | parent_meta: meta})
|
||||
{form, meta, leading_args ++ [kw_blocks]}
|
||||
end
|
||||
|
||||
defp normalize_kw_args(elems, state, keyword?)
|
||||
|
||||
defp normalize_kw_args(
|
||||
[{{:__block__, key_meta, [key]}, value} = first | rest] = current,
|
||||
state,
|
||||
keyword?
|
||||
)
|
||||
when is_atom(key) do
|
||||
keyword? = keyword? or keyword?(current)
|
||||
|
||||
first =
|
||||
if key_meta[:format] == :keyword and not keyword? do
|
||||
key_meta = Keyword.delete(key_meta, :format)
|
||||
line = key_meta[:line] || meta_line(state)
|
||||
{:__block__, [line: line], [{{:__block__, key_meta, [key]}, value}]}
|
||||
else
|
||||
first
|
||||
end
|
||||
|
||||
[first | normalize_kw_args(rest, state, keyword?)]
|
||||
end
|
||||
|
||||
defp normalize_kw_args([{left, right} | rest] = current, state, keyword?) do
|
||||
keyword? = keyword? or keyword?(current)
|
||||
|
||||
left =
|
||||
if keyword? do
|
||||
meta = [format: :keyword] ++ meta_line(state)
|
||||
{:__block__, meta, [maybe_escape_literal(left, state)]}
|
||||
else
|
||||
do_normalize(left, state)
|
||||
end
|
||||
|
||||
right = do_normalize(right, state)
|
||||
|
||||
pair =
|
||||
with {:__block__, meta, _} <- left,
|
||||
:keyword <- meta[:format] do
|
||||
{left, right}
|
||||
else
|
||||
_ -> {:__block__, meta_line(state), [{left, right}]}
|
||||
end
|
||||
|
||||
[pair | normalize_kw_args(rest, state, keyword?)]
|
||||
end
|
||||
|
||||
defp normalize_kw_args([first | rest], state, keyword?) do
|
||||
[do_normalize(first, state) | normalize_kw_args(rest, state, keyword?)]
|
||||
end
|
||||
|
||||
defp normalize_kw_args([], _state, _keyword?) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp normalize_args(args, state) do
|
||||
Enum.map(args, &do_normalize(&1, state))
|
||||
end
|
||||
|
||||
defp maybe_escape_literal(string, %{escape: true}) when is_binary(string) do
|
||||
{string, _} = Code.Identifier.escape(string, -1)
|
||||
IO.iodata_to_binary(string)
|
||||
end
|
||||
|
||||
defp maybe_escape_literal(atom, %{escape: true} = state) when is_atom(atom) do
|
||||
atom
|
||||
|> Atom.to_string()
|
||||
|> maybe_escape_literal(state)
|
||||
|> String.to_atom()
|
||||
end
|
||||
|
||||
defp maybe_escape_literal(term, _) do
|
||||
term
|
||||
end
|
||||
|
||||
# Check if we have an interpolated string.
|
||||
defp interpolated?({:<<>>, _, [_ | _] = parts}) do
|
||||
Enum.all?(parts, fn
|
||||
{:"::", _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
|
||||
binary when is_binary(binary) -> true
|
||||
_ -> false
|
||||
end)
|
||||
end
|
||||
|
||||
defp interpolated?(_) do
|
||||
false
|
||||
end
|
||||
|
||||
defp patch_meta_line(meta, parent_meta) do
|
||||
with nil <- meta[:line],
|
||||
line when is_integer(line) <- parent_meta[:line] do
|
||||
[line: line] ++ meta
|
||||
else
|
||||
_ -> meta
|
||||
end
|
||||
end
|
||||
|
||||
defp meta_line(state) do
|
||||
if line = state.parent_meta[:line] do
|
||||
[line: line]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
defp keyword?([{{:__block__, key_meta, [key]}, _} | rest]) when is_atom(key) do
|
||||
if key_meta[:format] == :keyword do
|
||||
keyword?(rest)
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
defp keyword?([{key, _value} | rest]) when is_atom(key) do
|
||||
case Atom.to_charlist(key) do
|
||||
'Elixir.' ++ _ -> false
|
||||
_ -> keyword?(rest)
|
||||
end
|
||||
end
|
||||
|
||||
defp keyword?([]), do: true
|
||||
defp keyword?(_other), do: false
|
||||
end
|
||||
@@ -7,9 +7,9 @@ defmodule Code.Typespec do
|
||||
@spec spec_to_quoted(atom, tuple) :: {atom, keyword, [Macro.t()]}
|
||||
def spec_to_quoted(name, spec)
|
||||
|
||||
def spec_to_quoted(name, {:type, anno, :fun, [{:type, _, :product, args}, result]})
|
||||
def spec_to_quoted(name, {:type, line, :fun, [{:type, _, :product, args}, result]})
|
||||
when is_atom(name) do
|
||||
meta = meta(anno)
|
||||
meta = [line: line]
|
||||
body = {name, meta, Enum.map(args, &typespec_to_quoted/1)}
|
||||
|
||||
vars =
|
||||
@@ -18,7 +18,7 @@ defmodule Code.Typespec do
|
||||
uniq: true,
|
||||
do: {var, {:var, meta, nil}}
|
||||
|
||||
spec = {:"::", meta, [body, typespec_to_quoted(result)]}
|
||||
spec = {:::, meta, [body, typespec_to_quoted(result)]}
|
||||
|
||||
if vars == [] do
|
||||
spec
|
||||
@@ -27,13 +27,11 @@ defmodule Code.Typespec do
|
||||
end
|
||||
end
|
||||
|
||||
def spec_to_quoted(name, {:type, anno, :fun, []}) when is_atom(name) do
|
||||
meta = meta(anno)
|
||||
{:"::", meta, [{name, meta, []}, quote(do: term)]}
|
||||
def spec_to_quoted(name, {:type, line, :fun, []}) when is_atom(name) do
|
||||
{:::, [line: line], [{name, [line: line], []}, quote(do: term)]}
|
||||
end
|
||||
|
||||
def spec_to_quoted(name, {:type, anno, :bounded_fun, [type, constrs]}) when is_atom(name) do
|
||||
meta = meta(anno)
|
||||
def spec_to_quoted(name, {:type, line, :bounded_fun, [type, constrs]}) when is_atom(name) do
|
||||
{:type, _, :fun, [{:type, _, :product, args}, result]} = type
|
||||
|
||||
guards =
|
||||
@@ -41,6 +39,7 @@ defmodule Code.Typespec do
|
||||
{erl_to_ex_var(var), typespec_to_quoted(type)}
|
||||
end
|
||||
|
||||
meta = [line: line]
|
||||
ignore_vars = Keyword.keys(guards)
|
||||
|
||||
vars =
|
||||
@@ -53,7 +52,7 @@ defmodule Code.Typespec do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
|
||||
when_args = [
|
||||
{:"::", meta, [{name, meta, args}, typespec_to_quoted(result)]},
|
||||
{:::, meta, [{name, [line: line], args}, typespec_to_quoted(result)]},
|
||||
guards ++ vars
|
||||
]
|
||||
|
||||
@@ -189,27 +188,27 @@ defmodule Code.Typespec do
|
||||
|
||||
## To AST conversion
|
||||
|
||||
defp collect_vars({:ann_type, _anno, args}) when is_list(args) do
|
||||
defp collect_vars({:ann_type, _line, args}) when is_list(args) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp collect_vars({:type, _anno, _kind, args}) when is_list(args) do
|
||||
defp collect_vars({:type, _line, _kind, args}) when is_list(args) do
|
||||
Enum.flat_map(args, &collect_vars/1)
|
||||
end
|
||||
|
||||
defp collect_vars({:remote_type, _anno, args}) when is_list(args) do
|
||||
defp collect_vars({:remote_type, _line, args}) when is_list(args) do
|
||||
Enum.flat_map(args, &collect_vars/1)
|
||||
end
|
||||
|
||||
defp collect_vars({:typed_record_field, _anno, type}) do
|
||||
defp collect_vars({:typed_record_field, _line, type}) do
|
||||
collect_vars(type)
|
||||
end
|
||||
|
||||
defp collect_vars({:paren_type, _anno, [type]}) do
|
||||
defp collect_vars({:paren_type, _line, [type]}) do
|
||||
collect_vars(type)
|
||||
end
|
||||
|
||||
defp collect_vars({:var, _anno, var}) do
|
||||
defp collect_vars({:var, _line, var}) do
|
||||
[erl_to_ex_var(var)]
|
||||
end
|
||||
|
||||
@@ -217,48 +216,47 @@ defmodule Code.Typespec do
|
||||
[]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:user_type, anno, name, args}) do
|
||||
defp typespec_to_quoted({:user_type, line, name, args}) do
|
||||
typespec_to_quoted({:type, line, name, args})
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, line, :tuple, :any}) do
|
||||
{:tuple, [line: line], []}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, line, :tuple, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
{name, meta(anno), args}
|
||||
{:{}, [line: line], args}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :tuple, :any}) do
|
||||
{:tuple, meta(anno), []}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :tuple, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
{:{}, meta(anno), args}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _anno, :list, [{:type, _, :union, unions} = arg]}) do
|
||||
defp typespec_to_quoted({:type, _line, :list, [{:type, _, :union, unions} = arg]}) do
|
||||
case unpack_typespec_kw(unions, []) do
|
||||
{:ok, ast} -> ast
|
||||
:error -> [typespec_to_quoted(arg)]
|
||||
end
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :list, []}) do
|
||||
{:list, meta(anno), []}
|
||||
defp typespec_to_quoted({:type, line, :list, []}) do
|
||||
{:list, [line: line], []}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _anno, :list, [arg]}) do
|
||||
defp typespec_to_quoted({:type, _line, :list, [arg]}) do
|
||||
[typespec_to_quoted(arg)]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :nonempty_list, []}) do
|
||||
[{:..., meta(anno), nil}]
|
||||
defp typespec_to_quoted({:type, line, :nonempty_list, []}) do
|
||||
[{:..., [line: line], nil}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :nonempty_list, [arg]}) do
|
||||
[typespec_to_quoted(arg), {:..., meta(anno), nil}]
|
||||
defp typespec_to_quoted({:type, line, :nonempty_list, [arg]}) do
|
||||
[typespec_to_quoted(arg), {:..., [line: line], nil}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :map, :any}) do
|
||||
{:map, meta(anno), []}
|
||||
defp typespec_to_quoted({:type, line, :map, :any}) do
|
||||
{:map, [line: line], []}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :map, fields}) do
|
||||
defp typespec_to_quoted({:type, line, :map, fields}) do
|
||||
fields =
|
||||
Enum.map(fields, fn
|
||||
{:type, _, :map_field_assoc, :any} ->
|
||||
@@ -274,19 +272,18 @@ defmodule Code.Typespec do
|
||||
{{:optional, [], [typespec_to_quoted(k)]}, typespec_to_quoted(v)}
|
||||
end)
|
||||
|
||||
case List.keytake(fields, :__struct__, 0) do
|
||||
{{:__struct__, struct}, fields_pruned} when is_atom(struct) and struct != nil ->
|
||||
map_pruned = {:%{}, meta(anno), fields_pruned}
|
||||
{:%, meta(anno), [struct, map_pruned]}
|
||||
{struct, fields} = Keyword.pop(fields, :__struct__)
|
||||
map = {:%{}, [line: line], fields}
|
||||
|
||||
_ ->
|
||||
{:%{}, meta(anno), fields}
|
||||
if struct do
|
||||
{:%, [line: line], [struct, map]}
|
||||
else
|
||||
map
|
||||
end
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :binary, [arg1, arg2]}) do
|
||||
defp typespec_to_quoted({:type, line, :binary, [arg1, arg2]}) do
|
||||
[arg1, arg2] = for arg <- [arg1, arg2], do: typespec_to_quoted(arg)
|
||||
line = meta(anno)[:line]
|
||||
|
||||
case {typespec_to_quoted(arg1), typespec_to_quoted(arg2)} do
|
||||
{arg1, 0} ->
|
||||
@@ -300,57 +297,57 @@ defmodule Code.Typespec do
|
||||
end
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :union, args}) do
|
||||
defp typespec_to_quoted({:type, line, :union, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
Enum.reduce(Enum.reverse(args), fn arg, expr -> {:|, meta(anno), [arg, expr]} end)
|
||||
Enum.reduce(Enum.reverse(args), fn arg, expr -> {:|, [line: line], [arg, expr]} end)
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :fun, [{:type, _, :product, args}, result]}) do
|
||||
defp typespec_to_quoted({:type, line, :fun, [{:type, _, :product, args}, result]}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
[{:->, meta(anno), [args, typespec_to_quoted(result)]}]
|
||||
[{:->, [line: line], [args, typespec_to_quoted(result)]}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :fun, [args, result]}) do
|
||||
[{:->, meta(anno), [[typespec_to_quoted(args)], typespec_to_quoted(result)]}]
|
||||
defp typespec_to_quoted({:type, line, :fun, [args, result]}) do
|
||||
[{:->, [line: line], [[typespec_to_quoted(args)], typespec_to_quoted(result)]}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :fun, []}) do
|
||||
typespec_to_quoted({:type, anno, :fun, [{:type, anno, :any}, {:type, anno, :any, []}]})
|
||||
defp typespec_to_quoted({:type, line, :fun, []}) do
|
||||
typespec_to_quoted({:type, line, :fun, [{:type, line, :any}, {:type, line, :any, []}]})
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :range, [left, right]}) do
|
||||
{:.., meta(anno), [typespec_to_quoted(left), typespec_to_quoted(right)]}
|
||||
defp typespec_to_quoted({:type, line, :range, [left, right]}) do
|
||||
{:.., [line: line], [typespec_to_quoted(left), typespec_to_quoted(right)]}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _anno, nil, []}) do
|
||||
defp typespec_to_quoted({:type, _line, nil, []}) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, name, args}) do
|
||||
defp typespec_to_quoted({:type, line, name, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
{name, meta(anno), args}
|
||||
{name, [line: line], args}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:var, anno, var}) do
|
||||
{erl_to_ex_var(var), meta(anno), nil}
|
||||
defp typespec_to_quoted({:var, line, var}) do
|
||||
{erl_to_ex_var(var), line, nil}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:op, anno, op, arg}) do
|
||||
{op, meta(anno), [typespec_to_quoted(arg)]}
|
||||
defp typespec_to_quoted({:op, line, op, arg}) do
|
||||
{op, [line: line], [typespec_to_quoted(arg)]}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:remote_type, anno, [mod, name, args]}) do
|
||||
remote_type(anno, mod, name, args)
|
||||
defp typespec_to_quoted({:remote_type, line, [mod, name, args]}) do
|
||||
remote_type(line, mod, name, args)
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:ann_type, anno, [var, type]}) do
|
||||
{:"::", meta(anno), [typespec_to_quoted(var), typespec_to_quoted(type)]}
|
||||
defp typespec_to_quoted({:ann_type, line, [var, type]}) do
|
||||
{:::, [line: line], [typespec_to_quoted(var), typespec_to_quoted(type)]}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted(
|
||||
{:typed_record_field, {:record_field, anno1, {:atom, anno2, name}}, type}
|
||||
{:typed_record_field, {:record_field, line, {:atom, line1, name}}, type}
|
||||
) do
|
||||
typespec_to_quoted({:ann_type, anno1, [{:var, anno2, name}, type]})
|
||||
typespec_to_quoted({:ann_type, line, [{:var, line1, name}, type]})
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _, :any}) do
|
||||
@@ -361,7 +358,7 @@ defmodule Code.Typespec do
|
||||
typespec_to_quoted(type)
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({type, _anno, atom}) when is_atom(type) do
|
||||
defp typespec_to_quoted({type, _line, atom}) when is_atom(type) do
|
||||
atom
|
||||
end
|
||||
|
||||
@@ -369,30 +366,30 @@ defmodule Code.Typespec do
|
||||
|
||||
## Helpers
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :charlist}, []) do
|
||||
typespec_to_quoted({:type, anno, :charlist, []})
|
||||
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :charlist}, []) do
|
||||
typespec_to_quoted({:type, line, :charlist, []})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :nonempty_charlist}, []) do
|
||||
typespec_to_quoted({:type, anno, :nonempty_charlist, []})
|
||||
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :nonempty_charlist}, []) do
|
||||
typespec_to_quoted({:type, line, :nonempty_charlist, []})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :struct}, []) do
|
||||
typespec_to_quoted({:type, anno, :struct, []})
|
||||
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :struct}, []) do
|
||||
typespec_to_quoted({:type, line, :struct, []})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]) do
|
||||
typespec_to_quoted({:type, anno, :as_boolean, [arg]})
|
||||
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]) do
|
||||
typespec_to_quoted({:type, line, :as_boolean, [arg]})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :keyword}, args) do
|
||||
typespec_to_quoted({:type, anno, :keyword, args})
|
||||
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :keyword}, args) do
|
||||
typespec_to_quoted({:type, line, :keyword, args})
|
||||
end
|
||||
|
||||
defp remote_type(anno, mod, name, args) do
|
||||
defp remote_type(line, mod, name, args) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
dot = {:., meta(anno), [typespec_to_quoted(mod), typespec_to_quoted(name)]}
|
||||
{dot, meta(anno), args}
|
||||
dot = {:., [line: line], [typespec_to_quoted(mod), typespec_to_quoted(name)]}
|
||||
{dot, [line: line], args}
|
||||
end
|
||||
|
||||
defp erl_to_ex_var(var) do
|
||||
@@ -416,6 +413,4 @@ defmodule Code.Typespec do
|
||||
defp unpack_typespec_kw(_, _acc) do
|
||||
:error
|
||||
end
|
||||
|
||||
defp meta(anno), do: [line: :erl_anno.line(anno)]
|
||||
end
|
||||
|
||||
@@ -17,18 +17,18 @@ defprotocol Collectable do
|
||||
|
||||
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 only the range limits and step are stored.
|
||||
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. `Collectable.into/1` can be seen as the opposite of
|
||||
`Enumerable.reduce/3`. If the functions in `Enumerable` are about taking values out,
|
||||
then `Collectable.into/1` is about collecting those values into a structure.
|
||||
`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.
|
||||
|
||||
## Examples
|
||||
|
||||
To show how to manually use the `Collectable` protocol, let's play with a
|
||||
simplified implementation for `MapSet`.
|
||||
To show how to manually use the `Collectable` protocol, let's play with its
|
||||
implementation for `MapSet`.
|
||||
|
||||
iex> {initial_acc, collector_fun} = Collectable.into(MapSet.new())
|
||||
iex> updated_acc = Enum.reduce([1, 2, 3], initial_acc, fn elem, acc ->
|
||||
@@ -37,34 +37,22 @@ defprotocol Collectable do
|
||||
iex> collector_fun.(updated_acc, :done)
|
||||
#MapSet<[1, 2, 3]>
|
||||
|
||||
To show how the protocol can be implemented, we can again look at the
|
||||
simplified implementation for `MapSet`. In this implementation "collecting" elements
|
||||
To show how the protocol can be implemented, we can take again a look at the
|
||||
implementation for `MapSet`. In this implementation "collecting" elements
|
||||
simply means inserting them in the set through `MapSet.put/2`.
|
||||
|
||||
defimpl Collectable, for: MapSet do
|
||||
def into(map_set) do
|
||||
def into(original) do
|
||||
collector_fun = fn
|
||||
map_set_acc, {:cont, elem} ->
|
||||
MapSet.put(map_set_acc, elem)
|
||||
|
||||
map_set_acc, :done ->
|
||||
map_set_acc
|
||||
|
||||
_map_set_acc, :halt ->
|
||||
:ok
|
||||
set, {:cont, elem} -> MapSet.put(set, elem)
|
||||
set, :done -> set
|
||||
_set, :halt -> :ok
|
||||
end
|
||||
|
||||
initial_acc = map_set
|
||||
|
||||
{initial_acc, collector_fun}
|
||||
{original, collector_fun}
|
||||
end
|
||||
end
|
||||
|
||||
So now we can call `Enum.into/2`:
|
||||
|
||||
iex> Enum.into([1, 2, 3], MapSet.new())
|
||||
#MapSet<[1, 2, 3]>
|
||||
|
||||
"""
|
||||
|
||||
@type command :: {:cont, term} | :done | :halt
|
||||
@@ -72,11 +60,8 @@ defprotocol Collectable do
|
||||
@doc """
|
||||
Returns an initial accumulator and a "collector" function.
|
||||
|
||||
Receives a `collectable` which can be used as the initial accumulator that will
|
||||
be passed to the function.
|
||||
|
||||
The collector function receives a term and a command and injects the term into
|
||||
the collectable accumulator on every `{:cont, term}` command.
|
||||
The returned function receives a term and a command and injects the term into
|
||||
the collectable on every `{:cont, term}` command.
|
||||
|
||||
`:done` is passed as a command when no further values will be injected. This
|
||||
is useful when there's a need to close resources or normalizing values. A
|
||||
@@ -88,17 +73,16 @@ defprotocol Collectable do
|
||||
For examples on how to use the `Collectable` protocol and `into/1` see the
|
||||
module documentation.
|
||||
"""
|
||||
@spec into(t) :: {initial_acc :: term, collector :: (term, command -> t | term)}
|
||||
@spec into(t) :: {term, (term, command -> t | term)}
|
||||
def into(collectable)
|
||||
end
|
||||
|
||||
defimpl Collectable, for: List do
|
||||
def into(list) do
|
||||
# TODO: Change the behaviour so the into always comes last on Elixir v2.0
|
||||
if list != [] do
|
||||
def into(original) do
|
||||
if original != [] do
|
||||
IO.warn(
|
||||
"the Collectable protocol is deprecated for non-empty lists. The behaviour of " <>
|
||||
"Enum.into/2 and \"for\" comprehensions with an :into option is incorrect " <>
|
||||
"things like Enum.into/2 or \"for\" comprehensions with an :into option is incorrect " <>
|
||||
"when collecting into non-empty lists. If you're collecting into a non-empty keyword " <>
|
||||
"list, consider using Keyword.merge/2 instead. If you're collecting into a non-empty " <>
|
||||
"list, consider concatenating the two lists with the ++ operator."
|
||||
@@ -106,14 +90,9 @@ defimpl Collectable, for: List do
|
||||
end
|
||||
|
||||
fun = fn
|
||||
list_acc, {:cont, elem} ->
|
||||
[elem | list_acc]
|
||||
|
||||
list_acc, :done ->
|
||||
list ++ :lists.reverse(list_acc)
|
||||
|
||||
_list_acc, :halt ->
|
||||
:ok
|
||||
list, {:cont, x} -> [x | list]
|
||||
list, :done -> original ++ :lists.reverse(list)
|
||||
_, :halt -> :ok
|
||||
end
|
||||
|
||||
{[], fun}
|
||||
@@ -121,7 +100,7 @@ defimpl Collectable, for: List do
|
||||
end
|
||||
|
||||
defimpl Collectable, for: BitString do
|
||||
def into(binary) when is_binary(binary) do
|
||||
def into(original) when is_binary(original) do
|
||||
fun = fn
|
||||
acc, {:cont, x} when is_binary(x) and is_list(acc) ->
|
||||
[acc | x]
|
||||
@@ -138,14 +117,14 @@ defimpl Collectable, for: BitString do
|
||||
acc, :done ->
|
||||
IO.iodata_to_binary(acc)
|
||||
|
||||
__acc, :halt ->
|
||||
_, :halt ->
|
||||
:ok
|
||||
end
|
||||
|
||||
{[binary], fun}
|
||||
{[original], fun}
|
||||
end
|
||||
|
||||
def into(bitstring) do
|
||||
def into(original) when is_bitstring(original) do
|
||||
fun = fn
|
||||
acc, {:cont, x} when is_bitstring(x) ->
|
||||
<<acc::bitstring, x::bitstring>>
|
||||
@@ -153,27 +132,22 @@ defimpl Collectable, for: BitString do
|
||||
acc, :done ->
|
||||
acc
|
||||
|
||||
_acc, :halt ->
|
||||
_, :halt ->
|
||||
:ok
|
||||
end
|
||||
|
||||
{bitstring, fun}
|
||||
{original, fun}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Collectable, for: Map do
|
||||
def into(map) do
|
||||
def into(original) do
|
||||
fun = fn
|
||||
map_acc, {:cont, {key, value}} ->
|
||||
Map.put(map_acc, key, value)
|
||||
|
||||
map_acc, :done ->
|
||||
map_acc
|
||||
|
||||
_map_acc, :halt ->
|
||||
:ok
|
||||
map, {:cont, {k, v}} -> :maps.put(k, v, map)
|
||||
map, :done -> map
|
||||
_, :halt -> :ok
|
||||
end
|
||||
|
||||
{map, fun}
|
||||
{original, fun}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,333 +0,0 @@
|
||||
defmodule Config do
|
||||
@moduledoc ~S"""
|
||||
A simple keyword-based configuration API.
|
||||
|
||||
## Example
|
||||
|
||||
This module is most commonly used to define application configuration,
|
||||
typically in `config/config.exs`:
|
||||
|
||||
import Config
|
||||
|
||||
config :some_app,
|
||||
key1: "value1",
|
||||
key2: "value2"
|
||||
|
||||
import_config "#{config_env()}.exs"
|
||||
|
||||
`import Config` will import the functions `config/2`, `config/3`
|
||||
`config_env/0`, `config_target/0`, and `import_config/1`
|
||||
to help you manage your configuration.
|
||||
|
||||
`config/2` and `config/3` are used to define key-value configuration
|
||||
for a given application. Once Mix starts, it will automatically
|
||||
evaluate the configuration file and persist the configuration above
|
||||
into `:some_app`'s application environment, which can be accessed in
|
||||
as follows:
|
||||
|
||||
"value1" = Application.fetch_env!(:some_app, :key1)
|
||||
|
||||
Finally, the line `import_config "#{config_env()}.exs"` will import
|
||||
other config files based on the current configuration environment,
|
||||
such as `config/dev.exs` and `config/test.exs`.
|
||||
|
||||
`Config` also provides a low-level API for evaluating and reading
|
||||
configuration, under the `Config.Reader` module.
|
||||
|
||||
**Important:** if you are writing a library to be used by other developers,
|
||||
it is generally recommended to avoid the application environment, as the
|
||||
application environment is effectively a global storage. Also note that
|
||||
the `config/config.exs` of a library is not evaluated when the library is
|
||||
used as a dependency, as configuration is always meant to configure the
|
||||
current project. For more information, read our [library guidelines](library-guidelines.md).
|
||||
|
||||
## Migrating from `use Mix.Config`
|
||||
|
||||
The `Config` module in Elixir was introduced in v1.9 as a replacement to
|
||||
`Mix.Config`, which was specific to Mix and has been deprecated.
|
||||
|
||||
You can leverage `Config` instead of `Mix.Config` in three steps. The first
|
||||
step is to replace `use Mix.Config` at the top of your config files by
|
||||
`import Config`.
|
||||
|
||||
The second is to make sure your `import_config/1` calls do not have a
|
||||
wildcard character. If so, you need to perform the wildcard lookup
|
||||
manually. For example, if you did:
|
||||
|
||||
import_config "../apps/*/config/config.exs"
|
||||
|
||||
It has to be replaced by:
|
||||
|
||||
for config <- "../apps/*/config/config.exs" |> Path.expand(__DIR__) |> Path.wildcard() do
|
||||
import_config config
|
||||
end
|
||||
|
||||
The last step is to replace all `Mix.env()` calls by `config_env()`.
|
||||
|
||||
## config/runtime.exs
|
||||
|
||||
For runtime configuration, you can use the `config/runtime.exs` file.
|
||||
It is executed right before applications start in both Mix and releases
|
||||
(assembled with `mix release`).
|
||||
"""
|
||||
|
||||
@opts_key {__MODULE__, :opts}
|
||||
@config_key {__MODULE__, :config}
|
||||
@imports_key {__MODULE__, :imports}
|
||||
|
||||
defp get_opts!(), do: Process.get(@opts_key) || raise_improper_use!()
|
||||
defp put_opts(value), do: Process.put(@opts_key, value)
|
||||
defp delete_opts(), do: Process.delete(@opts_key)
|
||||
|
||||
defp get_config!(), do: Process.get(@config_key) || raise_improper_use!()
|
||||
defp put_config(value), do: Process.put(@config_key, value)
|
||||
defp delete_config(), do: Process.delete(@config_key)
|
||||
|
||||
defp get_imports!(), do: Process.get(@imports_key) || raise_improper_use!()
|
||||
defp put_imports(value), do: Process.put(@imports_key, value)
|
||||
defp delete_imports(), do: Process.delete(@imports_key)
|
||||
|
||||
defp raise_improper_use!() do
|
||||
raise "could not set configuration via Config. " <>
|
||||
"This usually means you are trying to execute a configuration file " <>
|
||||
"directly, instead of reading it with Config.Reader"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Configures the given `root_key`.
|
||||
|
||||
Keyword lists are always deep-merged.
|
||||
|
||||
## Examples
|
||||
|
||||
The given `opts` are merged into the existing configuration
|
||||
for the given `root_key`. Conflicting keys are overridden by the
|
||||
ones specified in `opts`, unless they are keywords, which are
|
||||
deep merged recursively. For example, the application configuration
|
||||
below
|
||||
|
||||
config :logger,
|
||||
level: :warn,
|
||||
backends: [:console]
|
||||
|
||||
config :logger,
|
||||
level: :info,
|
||||
truncate: 1024
|
||||
|
||||
will have a final configuration for `:logger` of:
|
||||
|
||||
[level: :info, backends: [:console], truncate: 1024]
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
def config(root_key, opts) when is_atom(root_key) and is_list(opts) do
|
||||
unless Keyword.keyword?(opts) do
|
||||
raise ArgumentError, "config/2 expected a keyword list, got: #{inspect(opts)}"
|
||||
end
|
||||
|
||||
get_config!()
|
||||
|> __merge__([{root_key, opts}])
|
||||
|> put_config()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Configures the given `key` for the given `root_key`.
|
||||
|
||||
Keyword lists are always deep merged.
|
||||
|
||||
## Examples
|
||||
|
||||
The given `opts` are merged into the existing values for `key`
|
||||
in the given `root_key`. Conflicting keys are overridden by the
|
||||
ones specified in `opts`, unless they are keywords, which are
|
||||
deep merged recursively. For example, the application configuration
|
||||
below
|
||||
|
||||
config :ecto, Repo,
|
||||
log_level: :warn,
|
||||
adapter: Ecto.Adapters.Postgres
|
||||
|
||||
config :ecto, Repo,
|
||||
log_level: :info,
|
||||
pool_size: 10
|
||||
|
||||
will have a final value of the configuration for the `Repo`
|
||||
key in the `:ecto` application of:
|
||||
|
||||
[log_level: :info, pool_size: 10, adapter: Ecto.Adapters.Postgres]
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
def config(root_key, key, opts) when is_atom(root_key) and is_atom(key) do
|
||||
get_config!()
|
||||
|> __merge__([{root_key, [{key, opts}]}])
|
||||
|> put_config()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the environment this configuration file is executed on.
|
||||
|
||||
In Mix projects this function returns the environment this configuration
|
||||
file is executed on. In releases, the environment when `mix release` ran.
|
||||
|
||||
This is most often used to execute conditional code:
|
||||
|
||||
if config_env() == :prod do
|
||||
config :my_app, :debug, false
|
||||
end
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
defmacro config_env() do
|
||||
quote do
|
||||
Config.__env__!()
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec __env__!() :: atom()
|
||||
def __env__!() do
|
||||
elem(get_opts!(), 0) || raise "no :env key was given to this configuration file"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the target this configuration file is executed on.
|
||||
|
||||
This is most often used to execute conditional code:
|
||||
|
||||
if config_target() == :host do
|
||||
config :my_app, :debug, false
|
||||
end
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
defmacro config_target() do
|
||||
quote do
|
||||
Config.__target__!()
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec __target__!() :: atom()
|
||||
def __target__!() do
|
||||
elem(get_opts!(), 1) || raise "no :target key was given to this configuration file"
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Imports configuration from the given file.
|
||||
|
||||
In case the file doesn't exist, an error is raised.
|
||||
|
||||
If file is a relative, it will be expanded relatively to the
|
||||
directory the current configuration file is in.
|
||||
|
||||
## Examples
|
||||
|
||||
This is often used to emulate configuration across environments:
|
||||
|
||||
import_config "#{config_env()}.exs"
|
||||
|
||||
Note, however, some configuration files, such as `config/runtime.exs`
|
||||
does not support imports, as they are meant to be copied across
|
||||
systems.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
defmacro import_config(file) do
|
||||
quote do
|
||||
Config.__import__!(Path.expand(unquote(file), __DIR__))
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec __import__!(Path.t()) :: {term, Code.binding()}
|
||||
def __import__!(file) when is_binary(file) do
|
||||
import_config!(file, File.read!(file), true)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec __eval__!(Path.t(), binary(), keyword) :: {keyword, [Path.t()] | :disabled}
|
||||
def __eval__!(file, content, opts \\ []) when is_binary(file) and is_list(opts) do
|
||||
env = Keyword.get(opts, :env)
|
||||
target = Keyword.get(opts, :target)
|
||||
imports = Keyword.get(opts, :imports, [])
|
||||
|
||||
previous_opts = put_opts({env, target})
|
||||
previous_config = put_config([])
|
||||
previous_imports = put_imports(imports)
|
||||
|
||||
try do
|
||||
{eval_config, _} = import_config!(file, content, false)
|
||||
|
||||
case get_config!() do
|
||||
[] when is_list(eval_config) ->
|
||||
{validate!(eval_config, file), get_imports!()}
|
||||
|
||||
pdict_config ->
|
||||
{pdict_config, get_imports!()}
|
||||
end
|
||||
after
|
||||
if previous_opts, do: put_opts(previous_opts), else: delete_opts()
|
||||
if previous_config, do: put_config(previous_config), else: delete_config()
|
||||
if previous_imports, do: put_imports(previous_imports), else: delete_imports()
|
||||
end
|
||||
end
|
||||
|
||||
defp import_config!(file, contents, raise_when_disabled?) do
|
||||
current_imports = get_imports!()
|
||||
|
||||
cond do
|
||||
current_imports == :disabled ->
|
||||
if raise_when_disabled? do
|
||||
raise "import_config/1 is not enabled for this configuration file. " <>
|
||||
"Some configuration files do not allow importing other files " <>
|
||||
"as they are often copied to external systems"
|
||||
end
|
||||
|
||||
file in current_imports ->
|
||||
raise ArgumentError,
|
||||
"attempting to load configuration #{Path.relative_to_cwd(file)} recursively"
|
||||
|
||||
true ->
|
||||
put_imports([file | current_imports])
|
||||
:ok
|
||||
end
|
||||
|
||||
# TODO: Emit a warning if Mix.env() is found in said files in Elixir v1.15.
|
||||
# Note this won't be a deprecation warning as it will always be emitted.
|
||||
Code.eval_string(contents, [], file: file)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __merge__(config1, config2) when is_list(config1) and is_list(config2) do
|
||||
Keyword.merge(config1, config2, fn _, app1, app2 ->
|
||||
Keyword.merge(app1, app2, &deep_merge/3)
|
||||
end)
|
||||
end
|
||||
|
||||
defp deep_merge(_key, value1, value2) do
|
||||
if Keyword.keyword?(value1) and Keyword.keyword?(value2) do
|
||||
Keyword.merge(value1, value2, &deep_merge/3)
|
||||
else
|
||||
value2
|
||||
end
|
||||
end
|
||||
|
||||
defp validate!(config, file) do
|
||||
Enum.all?(config, fn
|
||||
{app, value} when is_atom(app) ->
|
||||
if Keyword.keyword?(value) do
|
||||
true
|
||||
else
|
||||
raise ArgumentError,
|
||||
"expected config for app #{inspect(app)} in #{Path.relative_to_cwd(file)} " <>
|
||||
"to return keyword list, got: #{inspect(value)}"
|
||||
end
|
||||
|
||||
_ ->
|
||||
false
|
||||
end)
|
||||
|
||||
config
|
||||
end
|
||||
end
|
||||
@@ -1,416 +0,0 @@
|
||||
defmodule Config.Provider do
|
||||
@moduledoc """
|
||||
Specifies a provider API that loads configuration during boot.
|
||||
|
||||
Config providers are typically used during releases to load
|
||||
external configuration while the system boots. This is done
|
||||
by starting the VM with the minimum amount of applications
|
||||
running, then invoking all of the providers, and then
|
||||
restarting the system. This requires a mutable configuration
|
||||
file on disk, as the results of the providers are written to
|
||||
the file system. For more information on runtime configuration,
|
||||
see `mix release`.
|
||||
|
||||
## Multiple config files
|
||||
|
||||
One common use of config providers is to specify multiple
|
||||
configuration files in a release. Elixir ships with one provider,
|
||||
called `Config.Reader`, which is capable of handling Elixir's
|
||||
built-in config files.
|
||||
|
||||
For example, imagine you want to list some basic configuration
|
||||
on Mix's built-in `config/runtime.exs` file, but you also want
|
||||
to support additional configuration files. To do so, you can add
|
||||
this inside the `def project` portion of your `mix.exs`:
|
||||
|
||||
releases: [
|
||||
demo: [
|
||||
config_providers: [
|
||||
{Config.Reader, {:system, "RELEASE_ROOT", "/extra_config.exs"}}
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
You can place this `extra_config.exs` file in your release in
|
||||
multiple ways:
|
||||
|
||||
1. If it is available on the host when assembling the release,
|
||||
you can place it on "rel/overlays/extra_config.exs" and it
|
||||
will be automatically copied to the release root
|
||||
|
||||
2. If it is available on the target during deployment, you can
|
||||
simply copy it to the release root as a step in your deployment
|
||||
|
||||
Now once the system boots, it will load both `config/runtime.exs`
|
||||
and `extra_config.exs` early in the boot process. You can learn
|
||||
more options on `Config.Reader`.
|
||||
|
||||
## Custom config provider
|
||||
|
||||
You can also implement custom config providers, similar to how
|
||||
`Config.Reader` works. For example, imagine you need to load
|
||||
some configuration from a JSON file and load that into the system.
|
||||
Said configuration provider would look like:
|
||||
|
||||
defmodule JSONConfigProvider do
|
||||
@behaviour Config.Provider
|
||||
|
||||
# Let's pass the path to the JSON file as config
|
||||
@impl true
|
||||
def init(path) when is_binary(path), do: path
|
||||
|
||||
@impl true
|
||||
def load(config, path) do
|
||||
# We need to start any app we may depend on.
|
||||
{:ok, _} = Application.ensure_all_started(:jason)
|
||||
|
||||
json = path |> File.read!() |> Jason.decode!()
|
||||
|
||||
Config.Reader.merge(
|
||||
config,
|
||||
my_app: [
|
||||
some_value: json["my_app_some_value"],
|
||||
another_value: json["my_app_another_value"],
|
||||
]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Then, when specifying your release, you can specify the provider in
|
||||
the release configuration:
|
||||
|
||||
releases: [
|
||||
demo: [
|
||||
config_providers: [
|
||||
{JSONConfigProvider, "/etc/config.json"}
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
"""
|
||||
|
||||
@type config :: keyword
|
||||
@type state :: term
|
||||
|
||||
@typedoc """
|
||||
A path pointing to a configuration file.
|
||||
|
||||
Since configuration files are often accessed on target machines,
|
||||
it can be expressed either as:
|
||||
|
||||
* a binary representing an absolute path
|
||||
|
||||
* a `{:system, system_var, path}` tuple where the config is the
|
||||
concatenation of the environment variable `system_var` with
|
||||
the given `path`
|
||||
|
||||
"""
|
||||
@type config_path :: {:system, binary(), binary()} | binary()
|
||||
|
||||
@doc """
|
||||
Invoked when initializing a config provider.
|
||||
|
||||
A config provider is typically initialized on the machine
|
||||
where the system is assembled and not on the target machine.
|
||||
The `c:init/1` callback is useful to verify the arguments
|
||||
given to the provider and prepare the state that will be
|
||||
given to `c:load/2`.
|
||||
|
||||
Furthermore, because the state returned by `c:init/1` can
|
||||
be written to text-based config files, it should be
|
||||
restricted only to simple data types, such as integers,
|
||||
strings, atoms, tuples, maps, and lists. Entries such as
|
||||
PIDs, references, and functions cannot be serialized.
|
||||
"""
|
||||
@callback init(term) :: state
|
||||
|
||||
@doc """
|
||||
Loads configuration (typically during system boot).
|
||||
|
||||
It receives the current `config` and the `state` returned by
|
||||
`c:init/1`. Then, you typically read the extra configuration
|
||||
from an external source and merge it into the received `config`.
|
||||
Merging should be done with `Config.Reader.merge/2`, as it
|
||||
performs deep merge. It should return the updated config.
|
||||
|
||||
Note that `c:load/2` is typically invoked very early in the
|
||||
boot process, therefore if you need to use an application
|
||||
in the provider, it is your responsibility to start it.
|
||||
"""
|
||||
@callback load(config, state) :: config
|
||||
|
||||
@doc false
|
||||
defstruct [
|
||||
:providers,
|
||||
:config_path,
|
||||
extra_config: [],
|
||||
prune_runtime_sys_config_after_boot: false,
|
||||
reboot_system_after_config: false,
|
||||
validate_compile_env: false
|
||||
]
|
||||
|
||||
@reserved_apps [:kernel, :stdlib]
|
||||
|
||||
@doc """
|
||||
Validates a `t:config_path/0`.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec validate_config_path!(config_path) :: :ok
|
||||
def validate_config_path!({:system, name, path})
|
||||
when is_binary(name) and is_binary(path),
|
||||
do: :ok
|
||||
|
||||
def validate_config_path!(path) do
|
||||
if is_binary(path) and Path.type(path) != :relative do
|
||||
:ok
|
||||
else
|
||||
raise ArgumentError, """
|
||||
expected configuration path to be:
|
||||
|
||||
* a binary representing an absolute path
|
||||
* a tuple {:system, system_var, path} where the config is the \
|
||||
concatenation of the `system_var` with the given `path`
|
||||
|
||||
Got: #{inspect(path)}
|
||||
"""
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Resolves a `t:config_path/0` to an actual path.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec resolve_config_path!(config_path) :: binary
|
||||
def resolve_config_path!(path) when is_binary(path), do: path
|
||||
def resolve_config_path!({:system, name, path}), do: System.fetch_env!(name) <> path
|
||||
|
||||
# Private keys
|
||||
@init_key :config_provider_init
|
||||
@booted_key :config_provider_booted
|
||||
|
||||
# Public keys
|
||||
@reboot_mode_key :config_provider_reboot_mode
|
||||
|
||||
@doc false
|
||||
def init(providers, config_path, opts \\ []) when is_list(providers) and is_list(opts) do
|
||||
validate_config_path!(config_path)
|
||||
providers = for {provider, init} <- providers, do: {provider, provider.init(init)}
|
||||
init = struct!(%Config.Provider{config_path: config_path, providers: providers}, opts)
|
||||
[elixir: [{@init_key, init}]]
|
||||
end
|
||||
|
||||
@doc false
|
||||
def boot(reboot_fun \\ &restart_and_sleep/0) do
|
||||
# The config provider typically runs very early in the
|
||||
# release process, so we need to make sure Elixir is started
|
||||
# before we go around running Elixir code.
|
||||
{:ok, _} = :application.ensure_all_started(:elixir)
|
||||
|
||||
case Application.fetch_env(:elixir, @booted_key) do
|
||||
{:ok, {:booted, path}} ->
|
||||
path && File.rm(path)
|
||||
|
||||
with {:ok, %Config.Provider{} = provider} <- Application.fetch_env(:elixir, @init_key) do
|
||||
maybe_validate_compile_env(provider)
|
||||
end
|
||||
|
||||
:booted
|
||||
|
||||
_ ->
|
||||
case Application.fetch_env(:elixir, @init_key) do
|
||||
{:ok, %Config.Provider{} = provider} ->
|
||||
path = resolve_config_path!(provider.config_path)
|
||||
reboot_config = [elixir: [{@booted_key, booted_value(provider, path)}]]
|
||||
boot_providers(path, provider, reboot_config, reboot_fun)
|
||||
|
||||
_ ->
|
||||
:skip
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp boot_providers(path, provider, reboot_config, reboot_fun) do
|
||||
original_config = read_config!(path)
|
||||
|
||||
config =
|
||||
original_config
|
||||
|> Config.__merge__(provider.extra_config)
|
||||
|> run_providers(provider)
|
||||
|
||||
if provider.reboot_system_after_config do
|
||||
config
|
||||
|> Config.__merge__(reboot_config)
|
||||
|> write_config!(path)
|
||||
|
||||
reboot_fun.()
|
||||
else
|
||||
for app <- @reserved_apps, config[app] != original_config[app] do
|
||||
abort("""
|
||||
Cannot configure #{inspect(app)} because :reboot_system_after_config has been set \
|
||||
to false and #{inspect(app)} has already been loaded, meaning any further \
|
||||
configuration won't have an effect.
|
||||
|
||||
The configuration for #{inspect(app)} before config providers was:
|
||||
|
||||
#{inspect(original_config[app])}
|
||||
|
||||
The configuration for #{inspect(app)} after config providers was:
|
||||
|
||||
#{inspect(config[app])}
|
||||
""")
|
||||
end
|
||||
|
||||
_ = Application.put_all_env(config, persistent: true)
|
||||
maybe_validate_compile_env(provider)
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
defp maybe_validate_compile_env(provider) do
|
||||
with [_ | _] = compile_env <- provider.validate_compile_env do
|
||||
validate_compile_env(compile_env)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def validate_compile_env(compile_env, ensure_loaded? \\ true) do
|
||||
for {app, [key | path], compile_return} <- compile_env,
|
||||
ensure_app_loaded?(app, ensure_loaded?) do
|
||||
try do
|
||||
traverse_env(Application.fetch_env(app, key), path)
|
||||
rescue
|
||||
e ->
|
||||
abort("""
|
||||
application #{inspect(app)} failed reading its compile environment #{path(key, path)}:
|
||||
|
||||
#{Exception.format(:error, e, __STACKTRACE__)}
|
||||
|
||||
Expected it to match the compile time value of #{return_to_text(compile_return)}.
|
||||
|
||||
#{compile_env_tips(app)}
|
||||
""")
|
||||
else
|
||||
^compile_return ->
|
||||
:ok
|
||||
|
||||
runtime_return ->
|
||||
abort("""
|
||||
the application #{inspect(app)} has a different value set #{path(key, path)} \
|
||||
during runtime compared to compile time. Since this application environment entry was \
|
||||
marked as compile time, this difference can lead to different behaviour than expected:
|
||||
|
||||
* Compile time value #{return_to_text(compile_return)}
|
||||
* Runtime value #{return_to_text(runtime_return)}
|
||||
|
||||
#{compile_env_tips(app)}
|
||||
""")
|
||||
end
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
defp ensure_app_loaded?(app, true), do: Application.ensure_loaded(app) == :ok
|
||||
defp ensure_app_loaded?(app, false), do: Application.spec(app, :vsn) != nil
|
||||
|
||||
defp path(key, []), do: "for key #{inspect(key)}"
|
||||
defp path(key, path), do: "for path #{inspect(path)} inside key #{inspect(key)}"
|
||||
|
||||
defp compile_env_tips(app),
|
||||
do: """
|
||||
To fix this error, you might:
|
||||
|
||||
* Make the runtime value match the compile time one
|
||||
|
||||
* Recompile your project. If the misconfigured application is a dependency, \
|
||||
you may need to run "mix deps.compile #{app} --force"
|
||||
|
||||
* Alternatively, you can disable this check. If you are using releases, you can \
|
||||
set :validate_compile_env to false in your release configuration. If you are \
|
||||
using Mix to start your system, you can pass the --no-validate-compile-env flag
|
||||
"""
|
||||
|
||||
defp return_to_text({:ok, value}), do: "was set to: #{inspect(value)}"
|
||||
defp return_to_text(:error), do: "was not set"
|
||||
|
||||
defp traverse_env(return, []), do: return
|
||||
defp traverse_env(:error, _paths), do: :error
|
||||
defp traverse_env({:ok, value}, [key | keys]), do: traverse_env(Access.fetch(value, key), keys)
|
||||
|
||||
@compile {:no_warn_undefined, {:init, :restart, 1}}
|
||||
defp restart_and_sleep() do
|
||||
mode = Application.get_env(:elixir, @reboot_mode_key)
|
||||
|
||||
# TODO: Remove otp_release check once we require Erlang/OTP 23+
|
||||
if :erlang.system_info(:otp_release) >= '23' and mode in [:embedded, :interactive] do
|
||||
:init.restart(mode: mode)
|
||||
else
|
||||
:init.restart()
|
||||
end
|
||||
|
||||
Process.sleep(:infinity)
|
||||
end
|
||||
|
||||
defp booted_value(%{prune_runtime_sys_config_after_boot: true}, path), do: {:booted, path}
|
||||
defp booted_value(%{prune_runtime_sys_config_after_boot: false}, _path), do: {:booted, nil}
|
||||
|
||||
defp read_config!(path) do
|
||||
case :file.consult(path) do
|
||||
{:ok, [inner]} ->
|
||||
inner
|
||||
|
||||
{:error, reason} ->
|
||||
bad_path_abort(
|
||||
"Could not read runtime configuration due to reason: #{inspect(reason)}",
|
||||
path
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp run_providers(config, %{providers: providers}) do
|
||||
Enum.reduce(providers, config, fn {provider, state}, acc ->
|
||||
try do
|
||||
provider.load(acc, state)
|
||||
catch
|
||||
kind, error ->
|
||||
IO.puts(:stderr, "ERROR! Config provider #{inspect(provider)} failed with:")
|
||||
IO.puts(:stderr, Exception.format(kind, error, __STACKTRACE__))
|
||||
:erlang.raise(kind, error, __STACKTRACE__)
|
||||
else
|
||||
term when is_list(term) ->
|
||||
term
|
||||
|
||||
term ->
|
||||
abort("Expected provider #{inspect(provider)} to return a list, got: #{inspect(term)}")
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp write_config!(config, path) do
|
||||
contents = :io_lib.format("%% coding: utf-8~n~tw.~n", [config])
|
||||
|
||||
case File.write(path, IO.chardata_to_string(contents)) do
|
||||
:ok ->
|
||||
:ok
|
||||
|
||||
{:error, reason} ->
|
||||
bad_path_abort(
|
||||
"Could not write runtime configuration due to reason: #{inspect(reason)}",
|
||||
path
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp bad_path_abort(msg, path) do
|
||||
abort(
|
||||
msg <>
|
||||
". Please make sure #{inspect(path)} is writable and accessible " <>
|
||||
"or choose a different path"
|
||||
)
|
||||
end
|
||||
|
||||
defp abort(msg) do
|
||||
IO.puts(:stderr, "ERROR! " <> msg)
|
||||
:erlang.raise(:error, "aborting boot", [{Config.Provider, :boot, 2, []}])
|
||||
end
|
||||
end
|
||||
@@ -1,138 +0,0 @@
|
||||
defmodule Config.Reader do
|
||||
@moduledoc """
|
||||
API for reading config files defined with `Config`.
|
||||
|
||||
## As a provider
|
||||
|
||||
`Config.Reader` can also be used as a `Config.Provider`. A config
|
||||
provider is used during releases to customize how applications are
|
||||
configured. When used as a provider, it expects a single argument:
|
||||
the configuration path (as outlined in `t:Config.Provider.config_path/0`)
|
||||
for the file to be read and loaded during the system boot.
|
||||
|
||||
For example, if you expect the target system to have a config file
|
||||
in an absolute path, you can add this inside the `def project` portion
|
||||
of your `mix.exs`:
|
||||
|
||||
releases: [
|
||||
demo: [
|
||||
config_providers: [
|
||||
{Config.Reader, "/etc/config.exs"}
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
Or if you want to read a custom path inside the release:
|
||||
|
||||
config_providers: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
|
||||
|
||||
You can also pass a keyword list of options to the reader,
|
||||
where the `:path` is a required key:
|
||||
|
||||
config_providers: [
|
||||
{Config.Reader,
|
||||
path: "/etc/config.exs",
|
||||
env: :prod,
|
||||
imports: :disabled}
|
||||
]
|
||||
|
||||
Remember Mix already loads `config/runtime.exs` by default.
|
||||
For more examples and scenarios, see the `Config.Providers` module.
|
||||
"""
|
||||
|
||||
@behaviour Config.Provider
|
||||
|
||||
@impl true
|
||||
def init(opts) when is_list(opts) do
|
||||
{path, opts} = Keyword.pop!(opts, :path)
|
||||
Config.Provider.validate_config_path!(path)
|
||||
{path, opts}
|
||||
end
|
||||
|
||||
def init(path) do
|
||||
init(path: path)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def load(config, {path, opts}) do
|
||||
merge(config, path |> Config.Provider.resolve_config_path!() |> read!(opts))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates the configuration `contents` for the given `file`.
|
||||
|
||||
Accepts the same options as `read!/2`.
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec eval!(Path.t(), binary, keyword) :: keyword
|
||||
def eval!(file, contents, opts \\ [])
|
||||
when is_binary(file) and is_binary(contents) and is_list(opts) do
|
||||
Config.__eval__!(Path.expand(file), contents, opts) |> elem(0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads the configuration file.
|
||||
|
||||
## Options
|
||||
|
||||
* `:imports` - a list of already imported paths or `:disabled`
|
||||
to disable imports
|
||||
|
||||
* `:env` - the environment the configuration file runs on.
|
||||
See `Config.config_env/0` for sample usage
|
||||
|
||||
* `:target` - the target the configuration file runs on.
|
||||
See `Config.config_target/0` for sample usage
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec read!(Path.t(), keyword) :: keyword
|
||||
def read!(file, opts \\ []) when is_binary(file) and is_list(opts) do
|
||||
file = Path.expand(file)
|
||||
Config.__eval__!(file, File.read!(file), opts) |> elem(0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads the given configuration file and returns the configuration
|
||||
with its imports.
|
||||
|
||||
Accepts the same options as `read!/2`. Although note the `:imports`
|
||||
option cannot be disabled in `read_imports!/2`.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec read_imports!(Path.t(), keyword) :: {keyword, [Path.t()]}
|
||||
def read_imports!(file, opts \\ []) when is_binary(file) and is_list(opts) do
|
||||
if opts[:imports] == :disabled do
|
||||
raise ArgumentError, ":imports must be a list of paths"
|
||||
end
|
||||
|
||||
file = Path.expand(file)
|
||||
Config.__eval__!(file, File.read!(file), opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Merges two configurations.
|
||||
|
||||
The configurations are merged together with the values in
|
||||
the second one having higher preference than the first in
|
||||
case of conflicts. In case both values are set to keyword
|
||||
lists, it deep merges them.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Config.Reader.merge([app: [k: :v1]], [app: [k: :v2]])
|
||||
[app: [k: :v2]]
|
||||
|
||||
iex> Config.Reader.merge([app: [k: [v1: 1, v2: 2]]], [app: [k: [v2: :a, v3: :b]]])
|
||||
[app: [k: [v1: 1, v2: :a, v3: :b]]]
|
||||
|
||||
iex> Config.Reader.merge([app1: []], [app2: []])
|
||||
[app1: [], app2: []]
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec merge(keyword, keyword) :: keyword
|
||||
def merge(config1, config2) when is_list(config1) and is_list(config2) do
|
||||
Config.__merge__(config1, config2)
|
||||
end
|
||||
end
|
||||
@@ -18,14 +18,13 @@ defmodule Dict do
|
||||
message =
|
||||
"Use the Map module for working with maps or the Keyword module for working with keyword lists"
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
|
||||
@deprecated message
|
||||
defmacro __using__(_) do
|
||||
# Use this import to guarantee proper code expansion
|
||||
import Kernel, except: [size: 1]
|
||||
|
||||
if __CALLER__.module != HashDict do
|
||||
IO.warn("use Dict is deprecated. " <> unquote(message), Macro.Env.stacktrace(__CALLER__))
|
||||
end
|
||||
|
||||
quote do
|
||||
message = "Use maps and the Map module instead"
|
||||
|
||||
@@ -155,13 +154,13 @@ defmodule Dict do
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def update(dict, key, default, fun) do
|
||||
def update(dict, key, initial, fun) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
put(dict, key, fun.(value))
|
||||
|
||||
:error ->
|
||||
put(dict, key, default)
|
||||
put(dict, key, initial)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -378,8 +377,8 @@ defmodule Dict do
|
||||
|
||||
@deprecated message
|
||||
@spec update(t, key, value, (value -> value)) :: t
|
||||
def update(dict, key, default, fun) do
|
||||
target(dict).update(dict, key, default, fun)
|
||||
def update(dict, key, initial, fun) do
|
||||
target(dict).update(dict, key, initial, fun)
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
|
||||
@@ -11,8 +11,9 @@ defmodule DynamicSupervisor do
|
||||
|
||||
## Examples
|
||||
|
||||
A dynamic supervisor is started with no children, a supervision strategy
|
||||
(the only strategy currently supported is `:one_for_one`), and a name:
|
||||
A dynamic supervisor is started with no children, often under a
|
||||
supervisor with the supervision strategy (the only strategy currently
|
||||
supported is `:one_for_one`) and a name:
|
||||
|
||||
children = [
|
||||
{DynamicSupervisor, strategy: :one_for_one, name: MyApp.DynamicSupervisor}
|
||||
@@ -46,19 +47,19 @@ defmodule DynamicSupervisor do
|
||||
# Automatically defines child_spec/1
|
||||
use DynamicSupervisor
|
||||
|
||||
def start_link(init_arg) do
|
||||
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
|
||||
def start_link(arg) do
|
||||
DynamicSupervisor.start_link(__MODULE__, arg, name: __MODULE__)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(_init_arg) do
|
||||
def init(_arg) do
|
||||
DynamicSupervisor.init(strategy: :one_for_one)
|
||||
end
|
||||
end
|
||||
|
||||
See the `Supervisor` docs for a discussion of when you may want to use
|
||||
module-based supervisors. A `@doc` annotation immediately preceding
|
||||
`use DynamicSupervisor` will be attached to the generated `child_spec/1`
|
||||
module-based supervisors. The `@doc` annotation immediately preceding
|
||||
`use DymamicSupervisor` will be attached to the generated `child_spec/1`
|
||||
function.
|
||||
|
||||
## Name registration
|
||||
@@ -77,20 +78,20 @@ defmodule DynamicSupervisor do
|
||||
defmodule MySupervisor do
|
||||
use Supervisor
|
||||
|
||||
def start_link(init_arg) do
|
||||
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
|
||||
def start_link(arg) do
|
||||
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
|
||||
end
|
||||
|
||||
def start_child(foo, bar, baz) do
|
||||
# This will start child by calling MyWorker.start_link(init_arg, foo, bar, baz)
|
||||
# This will start child by calling MyWorker.start_link(initial_arg, foo, bar, baz)
|
||||
Supervisor.start_child(__MODULE__, [foo, bar, baz])
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(init_arg) do
|
||||
def init(initial_arg) do
|
||||
children = [
|
||||
# Or the deprecated: worker(MyWorker, [init_arg])
|
||||
%{id: MyWorker, start: {MyWorker, :start_link, [init_arg]}}
|
||||
# Or the deprecated: worker(MyWorker, [initial_arg])
|
||||
%{id: MyWorker, start: {MyWorker, :start_link, [initial_arg]}}
|
||||
]
|
||||
|
||||
Supervisor.init(children, strategy: :simple_one_for_one)
|
||||
@@ -102,8 +103,8 @@ defmodule DynamicSupervisor do
|
||||
defmodule MySupervisor do
|
||||
use DynamicSupervisor
|
||||
|
||||
def start_link(init_arg) do
|
||||
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
|
||||
def start_link(arg) do
|
||||
DynamicSupervisor.start_link(__MODULE__, arg, name: __MODULE__)
|
||||
end
|
||||
|
||||
def start_child(foo, bar, baz) do
|
||||
@@ -114,10 +115,10 @@ defmodule DynamicSupervisor do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(init_arg) do
|
||||
def init(initial_arg) do
|
||||
DynamicSupervisor.init(
|
||||
strategy: :one_for_one,
|
||||
extra_arguments: [init_arg]
|
||||
extra_arguments: [initial_arg]
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -147,10 +148,13 @@ defmodule DynamicSupervisor do
|
||||
extra_arguments: [term()]
|
||||
}
|
||||
|
||||
@typedoc "Options given to `start_link` functions"
|
||||
@type option :: GenServer.option()
|
||||
@typedoc "Option values used by the `start*` functions"
|
||||
@type option :: {:name, Supervisor.name()} | init_option()
|
||||
|
||||
@typedoc "Options given to `start_link` and `init/1` functions"
|
||||
@typedoc "Options used by the `start*` functions"
|
||||
@type options :: [option, ...]
|
||||
|
||||
@typedoc "Options given to `start_link/2` and `init/1`"
|
||||
@type init_option ::
|
||||
{:strategy, strategy()}
|
||||
| {:max_restarts, non_neg_integer()}
|
||||
@@ -207,7 +211,7 @@ defmodule DynamicSupervisor do
|
||||
defmacro __using__(opts) do
|
||||
quote location: :keep, bind_quoted: [opts: opts] do
|
||||
@behaviour DynamicSupervisor
|
||||
unless Module.has_attribute?(__MODULE__, :doc) do
|
||||
if Module.get_attribute(__MODULE__, :doc) == nil do
|
||||
@doc """
|
||||
Returns a specification to start this module under a supervisor.
|
||||
|
||||
@@ -251,7 +255,7 @@ defmodule DynamicSupervisor do
|
||||
with `:normal` reason.
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec start_link([option | init_option]) :: Supervisor.on_start()
|
||||
@spec start_link(options) :: Supervisor.on_start()
|
||||
def start_link(options) when is_list(options) do
|
||||
keys = [:extra_arguments, :max_children, :max_seconds, :max_restarts, :strategy]
|
||||
{sup_opts, start_opts} = Keyword.split(options, keys)
|
||||
@@ -277,7 +281,7 @@ defmodule DynamicSupervisor do
|
||||
section in the `GenServer` module docs.
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec start_link(module, term, [option]) :: Supervisor.on_start()
|
||||
@spec start_link(module, term, GenServer.options()) :: Supervisor.on_start()
|
||||
def start_link(mod, init_arg, opts \\ []) do
|
||||
GenServer.start_link(__MODULE__, {mod, init_arg, opts[:name]}, opts)
|
||||
end
|
||||
@@ -286,7 +290,7 @@ defmodule DynamicSupervisor do
|
||||
Dynamically adds a child specification to `supervisor` and starts that child.
|
||||
|
||||
`child_spec` should be a valid child specification as detailed in the
|
||||
"Child specification" section of the documentation for `Supervisor`. The child
|
||||
"child_spec/1" section of the documentation for `Supervisor`. The child
|
||||
process will be started as defined in the child specification.
|
||||
|
||||
If the child process start function returns `{:ok, child}` or `{:ok, child,
|
||||
@@ -298,21 +302,15 @@ defmodule DynamicSupervisor do
|
||||
|
||||
If the child process start function returns an error tuple or an erroneous
|
||||
value, or if it fails, the child specification is discarded and this function
|
||||
returns `{:error, error}` where `error` is the error or erroneous value
|
||||
returned from child process start function, or failure reason if it fails.
|
||||
returns `{:error, error}` where `error` is a term containing information about
|
||||
the error and child specification.
|
||||
|
||||
If the supervisor already has N children in a way that N exceeds the amount
|
||||
of `:max_children` set on the supervisor initialization (see `init/1`), then
|
||||
this function returns `{:error, :max_children}`.
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec start_child(
|
||||
Supervisor.supervisor(),
|
||||
Supervisor.child_spec()
|
||||
| {module, term}
|
||||
| module
|
||||
| (old_erlang_child_spec :: :supervisor.child_spec())
|
||||
) ::
|
||||
@spec start_child(Supervisor.supervisor(), Supervisor.child_spec() | {module, term} | module) ::
|
||||
on_start_child()
|
||||
def start_child(supervisor, {_, _, _, _, _, _} = child_spec) do
|
||||
validate_and_start_child(supervisor, child_spec)
|
||||
@@ -407,7 +405,7 @@ defmodule DynamicSupervisor do
|
||||
|
||||
* `id` - it is always `:undefined` for dynamic supervisors
|
||||
|
||||
* `child` - the PID of the corresponding child process or the
|
||||
* `child` - the pid of the corresponding child process or the
|
||||
atom `:restarting` if the process is about to be restarted
|
||||
|
||||
* `type` - `:worker` or `:supervisor` as defined in the child
|
||||
@@ -418,8 +416,7 @@ defmodule DynamicSupervisor do
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec which_children(Supervisor.supervisor()) :: [
|
||||
# module() | :dynamic here because :supervisor.modules() is not exported
|
||||
{:undefined, pid | :restarting, :worker | :supervisor, [module()] | :dynamic}
|
||||
{:undefined, pid | :restarting, :worker | :supervisor, :supervisor.modules()}
|
||||
]
|
||||
def which_children(supervisor) do
|
||||
call(supervisor, :which_children)
|
||||
@@ -476,7 +473,7 @@ defmodule DynamicSupervisor do
|
||||
module-based supervisors. See the "Module-based supervisors" section
|
||||
in the module documentation for more information.
|
||||
|
||||
The `options` received by this function are also supported by `start_link/1`.
|
||||
The `options` received by this function are also supported by `start_link/2`.
|
||||
|
||||
This function returns a tuple containing the supervisor options.
|
||||
|
||||
@@ -490,7 +487,7 @@ defmodule DynamicSupervisor do
|
||||
|
||||
* `:strategy` - the restart strategy option. The only supported
|
||||
value is `:one_for_one` which means that no other child is
|
||||
terminated if a child process terminates. You can learn more
|
||||
terminate if a child process terminates. You can learn more
|
||||
about strategies in the `Supervisor` module docs.
|
||||
|
||||
* `:max_restarts` - the maximum number of restarts allowed in
|
||||
@@ -748,20 +745,7 @@ defmodule DynamicSupervisor do
|
||||
end
|
||||
|
||||
def handle_info(msg, state) do
|
||||
:logger.error(
|
||||
%{
|
||||
label: {DynamicSupervisor, :unexpected_msg},
|
||||
report: %{
|
||||
msg: msg
|
||||
}
|
||||
},
|
||||
%{
|
||||
domain: [:otp, :elixir],
|
||||
error_logger: %{tag: :error_msg},
|
||||
report_cb: &__MODULE__.format_report/1
|
||||
}
|
||||
)
|
||||
|
||||
:error_logger.error_msg('DynamicSupervisor received unexpected message: ~p~n', [msg])
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
@@ -1004,22 +988,12 @@ defmodule DynamicSupervisor do
|
||||
end
|
||||
|
||||
defp report_error(error, reason, pid, child, %{name: name, extra_arguments: extra}) do
|
||||
:logger.error(
|
||||
%{
|
||||
label: {:supervisor, error},
|
||||
report: [
|
||||
{:supervisor, name},
|
||||
{:errorContext, error},
|
||||
{:reason, reason},
|
||||
{:offender, extract_child(pid, child, extra)}
|
||||
]
|
||||
},
|
||||
%{
|
||||
domain: [:otp, :sasl],
|
||||
report_cb: &:logger.format_otp_report/1,
|
||||
logger_formatter: %{title: "SUPERVISOR REPORT"},
|
||||
error_logger: %{tag: :error_report, type: :supervisor_report}
|
||||
}
|
||||
:error_logger.error_report(
|
||||
:supervisor_report,
|
||||
supervisor: name,
|
||||
errorContext: error,
|
||||
reason: reason,
|
||||
offender: extract_child(pid, child, extra)
|
||||
)
|
||||
end
|
||||
|
||||
@@ -1050,12 +1024,4 @@ defmodule DynamicSupervisor do
|
||||
defp call(supervisor, req) do
|
||||
GenServer.call(supervisor, req, :infinity)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def format_report(%{
|
||||
label: {__MODULE__, :unexpected_msg},
|
||||
report: %{msg: msg}
|
||||
}) do
|
||||
{'DynamicSupervisor received unexpected message: ~p~n', [msg]}
|
||||
end
|
||||
end
|
||||
|
||||
+423
-1582
File diff suppressed because it is too large
Load Diff
+122
-330
@@ -21,15 +21,15 @@ defmodule Exception do
|
||||
|
||||
@typedoc "The kind handled by formatting functions"
|
||||
@type kind :: :error | non_error_kind
|
||||
@type non_error_kind :: :exit | :throw | {:EXIT, pid}
|
||||
@typep non_error_kind :: :exit | :throw | {:EXIT, pid}
|
||||
|
||||
@type stacktrace :: [stacktrace_entry]
|
||||
@type stacktrace_entry ::
|
||||
{module, atom, arity_or_args, location}
|
||||
| {(... -> any), arity_or_args, location}
|
||||
|
||||
@type arity_or_args :: non_neg_integer | list
|
||||
@type location :: keyword
|
||||
@typep arity_or_args :: non_neg_integer | list
|
||||
@typep location :: keyword
|
||||
|
||||
@callback exception(term) :: t
|
||||
@callback message(t) :: String.t()
|
||||
@@ -43,17 +43,9 @@ defmodule Exception do
|
||||
@callback blame(t, stacktrace) :: {t, stacktrace}
|
||||
@optional_callbacks [blame: 2]
|
||||
|
||||
@doc false
|
||||
# Callback for formatting Erlang exceptions
|
||||
def format_error(%struct{} = exception, _stacktrace) do
|
||||
%{general: message(exception), reason: "#" <> Atom.to_string(struct)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the given `term` is an exception.
|
||||
"""
|
||||
# TODO: Deprecate this on Elixir v1.15
|
||||
@doc deprecated: "Use Kernel.is_exception/1 instead"
|
||||
def exception?(term)
|
||||
def exception?(%_{__exception__: true}), do: true
|
||||
def exception?(_), do: false
|
||||
@@ -68,7 +60,7 @@ defmodule Exception do
|
||||
caught_exception ->
|
||||
"got #{inspect(caught_exception.__struct__)} with message " <>
|
||||
"#{inspect(message(caught_exception))} while retrieving Exception.message/1 " <>
|
||||
"for #{inspect(exception)}. Stacktrace:\n#{format_stacktrace(__STACKTRACE__)}"
|
||||
"for #{inspect(exception)}"
|
||||
else
|
||||
result when is_binary(result) ->
|
||||
result
|
||||
@@ -196,7 +188,7 @@ defmodule Exception do
|
||||
Where `definition` is `:def`, `:defp`, `:defmacro` or `:defmacrop`.
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec blame_mfa(module, function :: atom, args :: [term]) ::
|
||||
@spec blame_mfa(module, function, args :: [term]) ::
|
||||
{:ok, :def | :defp | :defmacro | :defmacrop, [{args :: [term], guards :: [term]}]}
|
||||
| :error
|
||||
def blame_mfa(module, function, args)
|
||||
@@ -216,18 +208,17 @@ defmodule Exception do
|
||||
{_, kind, _, clauses} <- List.keyfind(defs, {function, arity}, 0) do
|
||||
clauses =
|
||||
for {meta, ex_args, guards, _block} <- clauses do
|
||||
scope = :elixir_erl.scope(meta, true)
|
||||
ann = :elixir_erl.get_ann(meta)
|
||||
scope = :elixir_erl.scope(meta)
|
||||
|
||||
{erl_args, scope} =
|
||||
:elixir_erl_clauses.match(ann, &:elixir_erl_pass.translate_args/3, ex_args, scope)
|
||||
:elixir_erl_clauses.match(&:elixir_erl_pass.translate_args/2, ex_args, scope)
|
||||
|
||||
{args, binding} =
|
||||
[call_args, ex_args, erl_args]
|
||||
|> Enum.zip()
|
||||
|> Enum.map_reduce([], &blame_arg/2)
|
||||
|
||||
guards = Enum.map(guards, &blame_guard(&1, ann, scope, binding))
|
||||
guards = Enum.map(guards, &blame_guard(&1, scope, binding))
|
||||
{args, guards}
|
||||
end
|
||||
|
||||
@@ -246,11 +237,7 @@ defmodule Exception do
|
||||
binding = :orddict.store(:VAR, call_arg, binding)
|
||||
|
||||
try do
|
||||
ann = :erl_anno.new(0)
|
||||
|
||||
{:value, _, binding} =
|
||||
:erl_eval.expr({:match, ann, erl_arg, {:var, ann, :VAR}}, binding, :none)
|
||||
|
||||
{:value, _, binding} = :erl_eval.expr({:match, 0, erl_arg, {:var, 0, :VAR}}, binding, :none)
|
||||
{true, binding}
|
||||
rescue
|
||||
_ -> {false, binding}
|
||||
@@ -259,32 +246,26 @@ defmodule Exception do
|
||||
|
||||
defp rewrite_arg(arg) do
|
||||
Macro.prewalk(arg, fn
|
||||
{:%{}, meta, [__struct__: Range, first: first, last: last, step: step]} ->
|
||||
{:"..//", meta, [first, last, step]}
|
||||
{:%{}, meta, [__struct__: Range, first: first, last: last]} ->
|
||||
{:.., meta, [first, last]}
|
||||
|
||||
other ->
|
||||
other
|
||||
end)
|
||||
end
|
||||
|
||||
defp blame_guard({{:., _, [:erlang, op]}, meta, [left, right]}, ann, scope, binding)
|
||||
defp blame_guard({{:., _, [:erlang, op]}, meta, [left, right]}, scope, binding)
|
||||
when op == :andalso or op == :orelse do
|
||||
guards = [
|
||||
blame_guard(left, ann, scope, binding),
|
||||
blame_guard(right, ann, scope, binding)
|
||||
blame_guard(left, scope, binding),
|
||||
blame_guard(right, scope, binding)
|
||||
]
|
||||
|
||||
kernel_op =
|
||||
case op do
|
||||
:orelse -> :or
|
||||
:andalso -> :and
|
||||
end
|
||||
|
||||
{kernel_op, meta, guards}
|
||||
{rewrite_guard_call(op), meta, guards}
|
||||
end
|
||||
|
||||
defp blame_guard(ex_guard, ann, scope, binding) do
|
||||
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, ann, scope)
|
||||
defp blame_guard(ex_guard, scope, binding) do
|
||||
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, scope)
|
||||
|
||||
match? =
|
||||
try do
|
||||
@@ -299,17 +280,32 @@ defmodule Exception do
|
||||
|
||||
defp rewrite_guard(guard) do
|
||||
Macro.prewalk(guard, fn
|
||||
{{:., _, [mod, fun]}, meta, args} -> erl_to_ex(mod, fun, args, meta)
|
||||
other -> other
|
||||
{{:., _, [:erlang, :element]}, _, [{{:., _, [:erlang, :+]}, _, [int, 1]}, arg]} ->
|
||||
{:elem, [], [arg, int]}
|
||||
|
||||
{{:., _, [:erlang, :element]}, _, [int, arg]} when is_integer(int) ->
|
||||
{:elem, [], [arg, int - 1]}
|
||||
|
||||
{:., _, [:erlang, call]} ->
|
||||
rewrite_guard_call(call)
|
||||
|
||||
other ->
|
||||
other
|
||||
end)
|
||||
end
|
||||
|
||||
defp erl_to_ex(mod, fun, args, meta) do
|
||||
case :elixir_rewrite.erl_to_ex(mod, fun, args) do
|
||||
{Kernel, fun, args} -> {fun, meta, args}
|
||||
{mod, fun, args} -> {{:., [], [mod, fun]}, meta, args}
|
||||
end
|
||||
end
|
||||
defp rewrite_guard_call(:orelse), do: :or
|
||||
defp rewrite_guard_call(:andalso), do: :and
|
||||
defp rewrite_guard_call(:"=<"), do: :<=
|
||||
defp rewrite_guard_call(:"/="), do: :!=
|
||||
defp rewrite_guard_call(:"=:="), do: :===
|
||||
defp rewrite_guard_call(:"=/="), do: :!==
|
||||
|
||||
defp rewrite_guard_call(op) when op in [:band, :bor, :bnot, :bsl, :bsr, :bxor],
|
||||
do: {:., [], [Bitwise, op]}
|
||||
|
||||
defp rewrite_guard_call(op) when op in [:xor, :element, :size], do: {:., [], [:erlang, op]}
|
||||
defp rewrite_guard_call(op), do: op
|
||||
|
||||
defp blame_wrap(match?, ast), do: %{match?: match?, node: ast}
|
||||
|
||||
@@ -548,17 +544,8 @@ defmodule Exception do
|
||||
defp format_application(module) do
|
||||
# We cannot use Application due to bootstrap issues
|
||||
case :application.get_application(module) do
|
||||
{:ok, app} ->
|
||||
case :application.get_key(app, :vsn) do
|
||||
{:ok, vsn} when is_list(vsn) ->
|
||||
"(" <> Atom.to_string(app) <> " " <> List.to_string(vsn) <> ") "
|
||||
|
||||
_ ->
|
||||
"(" <> Atom.to_string(app) <> ") "
|
||||
end
|
||||
|
||||
:undefined ->
|
||||
""
|
||||
{:ok, app} -> "(" <> Atom.to_string(app) <> ") "
|
||||
:undefined -> ""
|
||||
end
|
||||
end
|
||||
|
||||
@@ -642,7 +629,6 @@ defmodule Exception do
|
||||
|
||||
@doc """
|
||||
Formats the given `file` and `line` as shown in stacktraces.
|
||||
|
||||
If any of the values are `nil`, they are omitted.
|
||||
|
||||
## Examples
|
||||
@@ -658,56 +644,17 @@ defmodule Exception do
|
||||
|
||||
"""
|
||||
def format_file_line(file, line, suffix \\ "") do
|
||||
cond do
|
||||
is_nil(file) -> ""
|
||||
is_nil(line) or line == 0 -> "#{file}:#{suffix}"
|
||||
true -> "#{file}:#{line}:#{suffix}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats the given `file`, `line`, and `column` as shown in stacktraces.
|
||||
|
||||
If any of the values are `nil`, they are omitted.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Exception.format_file_line_column("foo", 1, 2)
|
||||
"foo:1:2:"
|
||||
|
||||
iex> Exception.format_file_line_column("foo", 1, nil)
|
||||
"foo:1:"
|
||||
|
||||
iex> Exception.format_file_line_column("foo", nil, nil)
|
||||
"foo:"
|
||||
|
||||
iex> Exception.format_file_line_column("foo", nil, 2)
|
||||
"foo:"
|
||||
|
||||
iex> Exception.format_file_line_column(nil, nil, nil)
|
||||
if file do
|
||||
if line && line != 0 do
|
||||
"#{file}:#{line}:#{suffix}"
|
||||
else
|
||||
"#{file}:#{suffix}"
|
||||
end
|
||||
else
|
||||
""
|
||||
|
||||
"""
|
||||
def format_file_line_column(file, line, column, suffix \\ "") do
|
||||
cond do
|
||||
is_nil(file) -> ""
|
||||
is_nil(line) or line == 0 -> "#{file}:#{suffix}"
|
||||
is_nil(column) or column == 0 -> "#{file}:#{line}:#{suffix}"
|
||||
true -> "#{file}:#{line}:#{column}:#{suffix}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def format_snippet(snippet, error_line) do
|
||||
line_digits = error_line |> Integer.to_string() |> byte_size()
|
||||
placeholder = String.duplicate(" ", max(line_digits, 2))
|
||||
padding = if line_digits < 2, do: " "
|
||||
|
||||
" #{placeholder} |\n" <>
|
||||
" #{padding}#{error_line} | #{snippet.content}\n" <>
|
||||
" #{placeholder} | #{String.duplicate(" ", snippet.offset)}^"
|
||||
end
|
||||
|
||||
defp format_location(opts) when is_list(opts) do
|
||||
format_file_line(Keyword.get(opts, :file), Keyword.get(opts, :line), " ")
|
||||
end
|
||||
@@ -727,20 +674,16 @@ defmodule ArgumentError do
|
||||
|
||||
@impl true
|
||||
def blame(
|
||||
exception,
|
||||
%{message: "argument error"} = exception,
|
||||
[{:erlang, :apply, [module, function, args], _} | _] = stacktrace
|
||||
) do
|
||||
message =
|
||||
cond do
|
||||
not proper_list?(args) ->
|
||||
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)} " <>
|
||||
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a proper list"
|
||||
|
||||
# Note that args may be an empty list even if they were supplied
|
||||
not is_atom(module) and is_atom(function) and args == [] ->
|
||||
"you attempted to apply a function named #{inspect(function)} on #{inspect(module)}. " <>
|
||||
"If you are using Kernel.apply/3, make sure the module is an atom. " <>
|
||||
"If you are using the dot syntax, such as map.field or module.function(), " <>
|
||||
"you attempted to apply #{inspect(function)} on #{inspect(module)}. " <>
|
||||
"If you are using apply/3, make sure the module is an atom. " <>
|
||||
"If you are using the dot syntax, such as map.field or module.function, " <>
|
||||
"make sure the left side of the dot is an atom or a map"
|
||||
|
||||
not is_atom(module) ->
|
||||
@@ -748,9 +691,12 @@ defmodule ArgumentError do
|
||||
"Modules (the first argument of apply) must always be an atom"
|
||||
|
||||
not is_atom(function) ->
|
||||
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)}. " <>
|
||||
"However #{inspect(function)} is not a valid function name. Function names (the second argument " <>
|
||||
"of apply) must always be an atom"
|
||||
"you attempted to apply #{inspect(function)} on module #{inspect(module)}. " <>
|
||||
"Functions (the second argument of apply) must always be an atom"
|
||||
|
||||
not is_list(args) ->
|
||||
"you attempted to apply #{inspect(function)} on module #{inspect(module)} " <>
|
||||
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a list"
|
||||
end
|
||||
|
||||
{%{exception | message: message}, stacktrace}
|
||||
@@ -759,9 +705,6 @@ defmodule ArgumentError do
|
||||
def blame(exception, stacktrace) do
|
||||
{exception, stacktrace}
|
||||
end
|
||||
|
||||
defp proper_list?(list) when length(list) >= 0, do: true
|
||||
defp proper_list?(_), do: false
|
||||
end
|
||||
|
||||
defmodule ArithmeticError do
|
||||
@@ -805,62 +748,30 @@ defmodule ArithmeticError do
|
||||
end
|
||||
|
||||
defmodule SystemLimitError do
|
||||
defexception message: "a system limit has been reached"
|
||||
defexception []
|
||||
|
||||
@impl true
|
||||
def message(_) do
|
||||
"a system limit has been reached"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule SyntaxError do
|
||||
defexception [:file, :line, :column, :snippet, description: "syntax error"]
|
||||
defexception [:file, :line, description: "syntax error"]
|
||||
|
||||
@impl true
|
||||
def message(%{
|
||||
file: file,
|
||||
line: line,
|
||||
column: column,
|
||||
description: description,
|
||||
snippet: snippet
|
||||
})
|
||||
when not is_nil(snippet) and not is_nil(column) do
|
||||
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
|
||||
" " <> description <> "\n" <> Exception.format_snippet(snippet, line)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def message(%{
|
||||
file: file,
|
||||
line: line,
|
||||
column: column,
|
||||
description: description
|
||||
}) do
|
||||
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
|
||||
" " <> description
|
||||
def message(exception) do
|
||||
Exception.format_file_line(Path.relative_to_cwd(exception.file), exception.line) <>
|
||||
" " <> exception.description
|
||||
end
|
||||
end
|
||||
|
||||
defmodule TokenMissingError do
|
||||
defexception [:file, :line, :snippet, :column, description: "expression is incomplete"]
|
||||
defexception [:file, :line, description: "expression is incomplete"]
|
||||
|
||||
@impl true
|
||||
def message(%{
|
||||
file: file,
|
||||
line: line,
|
||||
column: column,
|
||||
description: description,
|
||||
snippet: snippet
|
||||
})
|
||||
when not is_nil(snippet) and not is_nil(column) do
|
||||
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
|
||||
" " <> description <> "\n" <> Exception.format_snippet(snippet, line)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def message(%{
|
||||
file: file,
|
||||
line: line,
|
||||
column: column,
|
||||
description: description
|
||||
}) do
|
||||
Exception.format_file_line_column(file && Path.relative_to_cwd(file), line, column) <>
|
||||
" " <> description
|
||||
def message(%{file: file, line: line, description: description}) do
|
||||
Exception.format_file_line(file && Path.relative_to_cwd(file), line) <> " " <> description
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1030,7 +941,7 @@ defmodule UndefinedFunctionError do
|
||||
end
|
||||
|
||||
defp hint(nil, _function, 0, _loaded?) do
|
||||
". If you are using the dot syntax, such as map.field or module.function(), " <>
|
||||
". If you are using the dot syntax, such as map.field or module.function, " <>
|
||||
"make sure the left side of the dot is an atom or a map"
|
||||
end
|
||||
|
||||
@@ -1045,23 +956,11 @@ defmodule UndefinedFunctionError do
|
||||
|
||||
@doc false
|
||||
def hint_for_loaded_module(module, function, arity, exports) do
|
||||
cond do
|
||||
macro_exported?(module, function, arity) ->
|
||||
". However there is a macro with the same name and arity. " <>
|
||||
"Be sure to require #{inspect(module)} if you intend to invoke this macro"
|
||||
|
||||
message = otp_obsolete(module, function, arity) ->
|
||||
", #{message}"
|
||||
|
||||
true ->
|
||||
IO.iodata_to_binary(did_you_mean(module, function, exports))
|
||||
end
|
||||
end
|
||||
|
||||
defp otp_obsolete(module, function, arity) do
|
||||
case :otp_internal.obsolete(module, function, arity) do
|
||||
{:removed, [_ | _] = string} -> string
|
||||
_ -> nil
|
||||
if macro_exported?(module, function, arity) do
|
||||
". However there is a macro with the same name and arity. " <>
|
||||
"Be sure to require #{inspect(module)} if you intend to invoke this macro"
|
||||
else
|
||||
IO.iodata_to_binary(did_you_mean(module, function, exports))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1091,7 +990,7 @@ defmodule UndefinedFunctionError do
|
||||
|
||||
case result do
|
||||
[] -> []
|
||||
suggestions -> [". Did you mean:\n\n" | Enum.map(suggestions, &format_fa/1)]
|
||||
suggestions -> [". Did you mean one of:\n\n" | Enum.map(suggestions, &format_fa/1)]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1122,9 +1021,7 @@ defmodule UndefinedFunctionError do
|
||||
end
|
||||
|
||||
defp expects_callback?(behaviour, function, arity) do
|
||||
callbacks =
|
||||
behaviour.behaviour_info(:callbacks) -- behaviour.behaviour_info(:optional_callbacks)
|
||||
|
||||
callbacks = behaviour.behaviour_info(:callbacks)
|
||||
Enum.member?(callbacks, {function, arity})
|
||||
end
|
||||
|
||||
@@ -1156,8 +1053,6 @@ end
|
||||
defmodule FunctionClauseError do
|
||||
defexception [:module, :function, :arity, :kind, :args, :clauses]
|
||||
|
||||
@clause_limit 10
|
||||
|
||||
@impl true
|
||||
def message(exception) do
|
||||
case exception do
|
||||
@@ -1166,7 +1061,7 @@ defmodule FunctionClauseError do
|
||||
|
||||
%{module: module, function: function, arity: arity} ->
|
||||
formatted = Exception.format_mfa(module, function, arity)
|
||||
blamed = blame(exception, &inspect/1, &blame_match/1)
|
||||
blamed = blame(exception, &inspect/1, &blame_match/2)
|
||||
"no function clause matching in #{formatted}" <> blamed
|
||||
end
|
||||
end
|
||||
@@ -1188,66 +1083,52 @@ defmodule FunctionClauseError do
|
||||
end
|
||||
end
|
||||
|
||||
defp blame_match(%{match?: true, node: node}), do: Macro.to_string(node)
|
||||
defp blame_match(%{match?: false, node: node}), do: "-" <> Macro.to_string(node) <> "-"
|
||||
defp blame_match(%{match?: true, node: node}, _), do: Macro.to_string(node)
|
||||
defp blame_match(%{match?: false, node: node}, _), do: "-" <> Macro.to_string(node) <> "-"
|
||||
defp blame_match(_, string), do: string
|
||||
|
||||
@doc false
|
||||
def blame(%{args: nil}, _, _) do
|
||||
""
|
||||
end
|
||||
|
||||
def blame(exception, inspect_fun, fun) do
|
||||
def blame(exception, inspect_fun, ast_fun) do
|
||||
%{module: module, function: function, arity: arity, kind: kind, args: args, clauses: clauses} =
|
||||
exception
|
||||
|
||||
mfa = Exception.format_mfa(module, function, arity)
|
||||
|
||||
format_clause_fun = fn {args, guards} ->
|
||||
args = Enum.map_join(args, ", ", fun)
|
||||
base = " #{kind} #{function}(#{args})"
|
||||
Enum.reduce(guards, base, &"#{&2} when #{clause_to_string(&1, fun)}") <> "\n"
|
||||
end
|
||||
formatted_args =
|
||||
args
|
||||
|> Enum.with_index(1)
|
||||
|> Enum.map(fn {arg, i} ->
|
||||
["\n # ", Integer.to_string(i), "\n ", pad(inspect_fun.(arg)), "\n"]
|
||||
end)
|
||||
|
||||
"\n\nThe following arguments were given to #{mfa}:\n" <>
|
||||
"#{format_args(args, inspect_fun)}" <>
|
||||
"#{format_clauses(clauses, format_clause_fun, @clause_limit)}"
|
||||
formatted_clauses =
|
||||
if clauses do
|
||||
format_clause_fun = fn {args, guards} ->
|
||||
code = Enum.reduce(guards, {function, [], args}, &{:when, [], [&2, &1]})
|
||||
" #{kind} " <> Macro.to_string(code, ast_fun) <> "\n"
|
||||
end
|
||||
|
||||
top_10 =
|
||||
clauses
|
||||
|> Enum.take(10)
|
||||
|> Enum.map(format_clause_fun)
|
||||
|
||||
[
|
||||
"\nAttempted function clauses (showing #{length(top_10)} out of #{length(clauses)}):",
|
||||
"\n\n",
|
||||
top_10
|
||||
]
|
||||
else
|
||||
""
|
||||
end
|
||||
|
||||
"\n\nThe following arguments were given to #{mfa}:\n#{formatted_args}#{formatted_clauses}"
|
||||
end
|
||||
|
||||
defp clause_to_string({op, _, [left, right]}, fun),
|
||||
do: clause_to_string(left, fun) <> " #{op} " <> clause_to_string(right, fun)
|
||||
|
||||
defp clause_to_string(node, fun), do: fun.(node)
|
||||
|
||||
defp format_args(args, inspect_fun) do
|
||||
args
|
||||
|> Enum.with_index(1)
|
||||
|> Enum.map(fn {arg, i} ->
|
||||
[pad("\n# "), Integer.to_string(i), pad("\n"), pad(inspect_fun.(arg)), "\n"]
|
||||
end)
|
||||
end
|
||||
|
||||
defp format_clauses(clauses, format_clause_fun, limit)
|
||||
defp format_clauses(nil, _, _), do: ""
|
||||
defp format_clauses([], _, _), do: ""
|
||||
|
||||
defp format_clauses(clauses, format_clause_fun, limit) do
|
||||
top_clauses =
|
||||
clauses
|
||||
|> Enum.take(limit)
|
||||
|> Enum.map(format_clause_fun)
|
||||
|
||||
[
|
||||
"\nAttempted function clauses (showing #{length(top_clauses)} out of #{length(clauses)}):",
|
||||
"\n\n",
|
||||
top_clauses,
|
||||
non_visible_clauses(length(clauses) - limit)
|
||||
]
|
||||
end
|
||||
|
||||
defp non_visible_clauses(n) when n <= 0, do: []
|
||||
defp non_visible_clauses(1), do: [" ...\n (1 clause not shown)\n"]
|
||||
defp non_visible_clauses(n), do: [" ...\n (#{n} clauses not shown)\n"]
|
||||
|
||||
defp pad(string) do
|
||||
String.replace(string, "\n", "\n ")
|
||||
end
|
||||
@@ -1267,23 +1148,10 @@ defmodule Protocol.UndefinedError do
|
||||
|
||||
@impl true
|
||||
def message(%{protocol: protocol, value: value, description: description}) do
|
||||
"protocol #{inspect(protocol)} not implemented for #{inspect(value)} of type " <>
|
||||
value_type(value) <> maybe_description(description) <> maybe_available(protocol)
|
||||
"protocol #{inspect(protocol)} not implemented for #{inspect(value)}" <>
|
||||
maybe_description(description) <> maybe_available(protocol)
|
||||
end
|
||||
|
||||
defp value_type(%{__struct__: struct}), do: "#{inspect(struct)} (a struct)"
|
||||
defp value_type(value) when is_atom(value), do: "Atom"
|
||||
defp value_type(value) when is_bitstring(value), do: "BitString"
|
||||
defp value_type(value) when is_float(value), do: "Float"
|
||||
defp value_type(value) when is_function(value), do: "Function"
|
||||
defp value_type(value) when is_integer(value), do: "Integer"
|
||||
defp value_type(value) when is_list(value), do: "List"
|
||||
defp value_type(value) when is_map(value), do: "Map"
|
||||
defp value_type(value) when is_pid(value), do: "PID"
|
||||
defp value_type(value) when is_port(value), do: "Port"
|
||||
defp value_type(value) when is_reference(value), do: "Reference"
|
||||
defp value_type(value) when is_tuple(value), do: "Tuple"
|
||||
|
||||
defp maybe_description(""), do: ""
|
||||
defp maybe_description(description), do: ", " <> description
|
||||
|
||||
@@ -1293,8 +1161,7 @@ defmodule Protocol.UndefinedError do
|
||||
". There are no implementations for this protocol."
|
||||
|
||||
{:consolidated, types} ->
|
||||
". This protocol is implemented for the following type(s): " <>
|
||||
Enum.map_join(types, ", ", &inspect/1)
|
||||
". This protocol is implemented for: #{Enum.map_join(types, ", ", &inspect/1)}"
|
||||
|
||||
:not_consolidated ->
|
||||
""
|
||||
@@ -1320,10 +1187,6 @@ defmodule KeyError do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def blame(exception = %{message: message}, stacktrace) when is_binary(message) do
|
||||
{exception, stacktrace}
|
||||
end
|
||||
|
||||
def blame(exception = %{term: nil}, stacktrace) do
|
||||
message = message(exception.key, exception.term)
|
||||
{%{exception | message: message}, stacktrace}
|
||||
@@ -1362,7 +1225,7 @@ defmodule KeyError do
|
||||
|
||||
case suggestions do
|
||||
[] -> []
|
||||
suggestions -> [". Did you mean:\n\n" | format_suggestions(suggestions)]
|
||||
suggestions -> [". Did you mean one of:\n\n" | format_suggestions(suggestions)]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1432,25 +1295,7 @@ defmodule File.CopyError do
|
||||
formatted = IO.iodata_to_binary(:file.format_error(exception.reason))
|
||||
|
||||
location =
|
||||
case exception.on do
|
||||
"" -> ""
|
||||
on -> ". #{on}"
|
||||
end
|
||||
|
||||
"could not #{exception.action} from #{inspect(exception.source)} to " <>
|
||||
"#{inspect(exception.destination)}#{location}: #{formatted}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule File.RenameError do
|
||||
defexception [:reason, :source, :destination, on: "", action: ""]
|
||||
|
||||
@impl true
|
||||
def message(exception) do
|
||||
formatted = IO.iodata_to_binary(:file.format_error(exception.reason))
|
||||
|
||||
location =
|
||||
case exception.on do
|
||||
case exception.on() do
|
||||
"" -> ""
|
||||
on -> ". #{on}"
|
||||
end
|
||||
@@ -1481,32 +1326,16 @@ defmodule ErlangError do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def normalize(:badarg, stacktrace) do
|
||||
case error_info(:badarg, stacktrace) do
|
||||
{:ok, args} ->
|
||||
message = "errors were found at the given arguments:\n\n#{args}"
|
||||
%ArgumentError{message: message}
|
||||
|
||||
:error ->
|
||||
%ArgumentError{}
|
||||
end
|
||||
def normalize(:badarg, _stacktrace) do
|
||||
%ArgumentError{}
|
||||
end
|
||||
|
||||
def normalize(:badarith, _stacktrace) do
|
||||
%ArithmeticError{}
|
||||
end
|
||||
|
||||
def normalize(:system_limit, stacktrace) do
|
||||
case error_info(:system_limit, stacktrace) do
|
||||
{:ok, args} ->
|
||||
message =
|
||||
"a system limit has been reached due to errors at the given arguments:\n\n#{args}"
|
||||
|
||||
%SystemLimitError{message: message}
|
||||
|
||||
:error ->
|
||||
%SystemLimitError{}
|
||||
end
|
||||
def normalize(:system_limit, _stacktrace) do
|
||||
%SystemLimitError{}
|
||||
end
|
||||
|
||||
def normalize(:cond_clause, _stacktrace) do
|
||||
@@ -1596,41 +1425,4 @@ defmodule ErlangError do
|
||||
defp from_stacktrace(_) do
|
||||
{nil, nil, nil}
|
||||
end
|
||||
|
||||
defp error_info(:badarg, [{:erlang, fun, _, _} | _]) when fun in [:byte_size, :bit_size] do
|
||||
{:ok,
|
||||
"""
|
||||
* 1st argument: not a bitstring
|
||||
|
||||
This typically happens when calling Kernel.#{fun}/1 with an invalid argument \
|
||||
or when performing binary construction or binary concatenation with <> and \
|
||||
one of the arguments is not a binary\
|
||||
"""}
|
||||
end
|
||||
|
||||
defp error_info(erl_exception, stacktrace) do
|
||||
with [{module, _, args_or_arity, opts} | _] <- stacktrace,
|
||||
%{} = error_info <- opts[:error_info] do
|
||||
module = Map.get(error_info, :module, module)
|
||||
function = Map.get(error_info, :function, :format_error)
|
||||
arity = if is_integer(args_or_arity), do: args_or_arity, else: length(args_or_arity)
|
||||
extra = apply(module, function, [erl_exception, stacktrace])
|
||||
args_errors = Map.take(extra, Enum.to_list(1..arity//1))
|
||||
|
||||
if map_size(args_errors) > 0 do
|
||||
{:ok, IO.iodata_to_binary(Enum.map(args_errors, &arg_error/1))}
|
||||
else
|
||||
:error
|
||||
end
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
defp arg_error({n, message}), do: " * #{nth(n)} argument: #{message}\n"
|
||||
|
||||
defp nth(1), do: "1st"
|
||||
defp nth(2), do: "2nd"
|
||||
defp nth(3), do: "3rd"
|
||||
defp nth(n), do: "#{n}th"
|
||||
end
|
||||
|
||||
+47
-103
@@ -6,7 +6,7 @@ defmodule File do
|
||||
to interact with files or IO devices, like `open/2`,
|
||||
`copy/3` and others. This module also provides higher
|
||||
level functions that work with filenames and have their naming
|
||||
based on Unix variants. For example, one can copy a file
|
||||
based on UNIX variants. For example, one can copy a file
|
||||
via `cp/3` and remove files and directories recursively
|
||||
via `rm_rf/1`.
|
||||
|
||||
@@ -30,7 +30,7 @@ defmodule File do
|
||||
always treated as UTF-8. In particular, we expect that the
|
||||
shell and the operating system are configured to use UTF-8
|
||||
encoding. Binary filenames are considered raw and passed
|
||||
to the operating system as is.
|
||||
to the OS as is.
|
||||
|
||||
## API
|
||||
|
||||
@@ -110,8 +110,6 @@ defmodule File do
|
||||
|
||||
@type stream_mode ::
|
||||
encoding_mode()
|
||||
| :append
|
||||
| :compressed
|
||||
| :trim_bom
|
||||
| {:read_ahead, pos_integer | false}
|
||||
| {:delayed_write, non_neg_integer, non_neg_integer}
|
||||
@@ -375,8 +373,6 @@ defmodule File do
|
||||
machine
|
||||
* `:posix` - returns the time as integer seconds since epoch
|
||||
|
||||
Note: Since file times are stored in POSIX time format on most operating systems,
|
||||
it is faster to retrieve file information with the `time: :posix` option.
|
||||
"""
|
||||
@spec stat(Path.t(), stat_options) :: {:ok, File.Stat.t()} | {:error, posix}
|
||||
def stat(path, opts \\ []) do
|
||||
@@ -428,8 +424,6 @@ defmodule File do
|
||||
* `:local` - returns a `{date, time}` tuple using the machine time
|
||||
* `:posix` - returns the time as integer seconds since epoch
|
||||
|
||||
Note: Since file times are stored in POSIX time format on most operating systems,
|
||||
it is faster to retrieve file information with the `time: :posix` option.
|
||||
"""
|
||||
@spec lstat(Path.t(), stat_options) :: {:ok, File.Stat.t()} | {:error, posix}
|
||||
def lstat(path, opts \\ []) do
|
||||
@@ -538,12 +532,6 @@ defmodule File do
|
||||
(as returned by `:erlang.universaltime()`) or an integer
|
||||
representing the POSIX timestamp (as returned by `System.os_time(:second)`).
|
||||
|
||||
In Unix-like systems, changing the modification time may require
|
||||
you to be either `root` or the owner of the file. Having write
|
||||
access may not be enough. In those cases, touching the file the
|
||||
first time (to create it) will succeed, but touching an existing
|
||||
file with fail with `{:error, :eperm}`.
|
||||
|
||||
## Examples
|
||||
|
||||
File.touch("/tmp/a.txt", {{2018, 1, 30}, {13, 59, 59}})
|
||||
@@ -587,7 +575,7 @@ defmodule File do
|
||||
File.touch!("/tmp/a.txt", {{2018, 1, 30}, {13, 59, 59}})
|
||||
#=> :ok
|
||||
File.touch!("/fakedir/b.txt", {{2018, 1, 30}, {13, 59, 59}})
|
||||
** (File.Error) could not touch "/fakedir/b.txt": no such file or directory
|
||||
#=> ** (File.Error) could not touch "/fakedir/b.txt": no such file or directory
|
||||
|
||||
File.touch!("/tmp/a.txt", 1544519753)
|
||||
|
||||
@@ -724,8 +712,8 @@ defmodule File do
|
||||
|
||||
Returns `:ok` in case of success, `{:error, reason}` otherwise.
|
||||
|
||||
Note: The command `mv` in Unix-like systems behaves differently depending on
|
||||
whether `source` is a file and the `destination` is an existing directory.
|
||||
Note: The command `mv` in Unix systems behaves differently depending
|
||||
if `source` is a file and the `destination` is an existing directory.
|
||||
We have chosen to explicitly disallow this behaviour.
|
||||
|
||||
## Examples
|
||||
@@ -737,62 +725,36 @@ defmodule File do
|
||||
File.rename("samples", "tmp")
|
||||
|
||||
"""
|
||||
@doc since: "1.1.0"
|
||||
@spec rename(Path.t(), Path.t()) :: :ok | {:error, posix}
|
||||
def rename(source, destination) do
|
||||
:file.rename(source, destination)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The same as `rename/2` but raises a `File.RenameError` exception if it fails.
|
||||
Returns `:ok` otherwise.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec rename!(Path.t(), Path.t()) :: :ok
|
||||
def rename!(source, destination) do
|
||||
case rename(source, destination) do
|
||||
:ok ->
|
||||
:ok
|
||||
Copies the contents in `source` to `destination` preserving its mode.
|
||||
|
||||
{:error, reason} ->
|
||||
raise File.RenameError,
|
||||
reason: reason,
|
||||
action: "rename",
|
||||
source: IO.chardata_to_string(source),
|
||||
destination: IO.chardata_to_string(destination)
|
||||
end
|
||||
end
|
||||
If a file already exists in the destination, it invokes a
|
||||
callback which should return `true` if the existing file
|
||||
should be overwritten, `false` otherwise. The callback defaults to return `true`.
|
||||
|
||||
@doc """
|
||||
Copies the contents of `source_file` to `destination_file` preserving its modes.
|
||||
|
||||
`source_file` must be a file or a symbolic link to one. `destination_file` must
|
||||
be a path to a non-existent file. If either is a directory, `{:error, :eisdir}`
|
||||
will be returned.
|
||||
|
||||
The `callback` function is invoked if the `destination_file` already exists.
|
||||
The function receives arguments for `source_file` and `destination_file`;
|
||||
it should return `true` if the existing file should be overwritten, `false` if
|
||||
otherwise. The default callback returns `true`.
|
||||
|
||||
The function returns `:ok` in case of success. Otherwise, it returns
|
||||
`{:error, reason}`.
|
||||
The function returns `:ok` in case of success, returns
|
||||
`{:error, reason}` otherwise.
|
||||
|
||||
If you want to copy contents from an IO device to another device
|
||||
or do a straight copy from a source to a destination without
|
||||
preserving modes, check `copy/3` instead.
|
||||
|
||||
Note: The command `cp` in Unix-like systems behaves differently depending on
|
||||
whether the destination is an existing directory or not. We have chosen to
|
||||
explicitly disallow copying to a destination which is a directory,
|
||||
and an error will be returned if tried.
|
||||
Note: The command `cp` in Unix systems behaves differently depending
|
||||
if `destination` is an existing directory or not. We have chosen to
|
||||
explicitly disallow this behaviour. If destination is a directory, an
|
||||
error will be returned.
|
||||
"""
|
||||
@spec cp(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) :: :ok | {:error, posix}
|
||||
def cp(source_file, destination_file, callback \\ fn _, _ -> true end) do
|
||||
source_file = IO.chardata_to_string(source_file)
|
||||
destination_file = IO.chardata_to_string(destination_file)
|
||||
def cp(source, destination, callback \\ fn _, _ -> true end) do
|
||||
source = IO.chardata_to_string(source)
|
||||
destination = IO.chardata_to_string(destination)
|
||||
|
||||
case do_cp_file(source_file, destination_file, callback, []) do
|
||||
case do_cp_file(source, destination, callback, []) do
|
||||
{:error, reason, _} -> {:error, reason}
|
||||
_ -> :ok
|
||||
end
|
||||
@@ -809,8 +771,8 @@ defmodule File do
|
||||
Returns `:ok` otherwise.
|
||||
"""
|
||||
@spec cp!(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) :: :ok
|
||||
def cp!(source_file, destination_file, callback \\ fn _, _ -> true end) do
|
||||
case cp(source_file, destination_file, callback) do
|
||||
def cp!(source, destination, callback \\ fn _, _ -> true end) do
|
||||
case cp(source, destination, callback) do
|
||||
:ok ->
|
||||
:ok
|
||||
|
||||
@@ -818,29 +780,26 @@ defmodule File do
|
||||
raise File.CopyError,
|
||||
reason: reason,
|
||||
action: "copy",
|
||||
source: IO.chardata_to_string(source_file),
|
||||
destination: IO.chardata_to_string(destination_file)
|
||||
source: IO.chardata_to_string(source),
|
||||
destination: IO.chardata_to_string(destination)
|
||||
end
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Copies the contents in `source` to `destination` recursively, maintaining the
|
||||
source directory structure and modes.
|
||||
|
||||
If `source` is a file or a symbolic link to it, `destination` must be a path
|
||||
to an existent file, a symbolic link to one, or a path to a non-existent file.
|
||||
|
||||
If `source` is a directory, or a symbolic link to it, then `destination` must
|
||||
be an existent `directory` or a symbolic link to one, or a path to a non-existent directory.
|
||||
Copies the contents in source to destination.
|
||||
|
||||
If the source is a file, it copies `source` to
|
||||
`destination`. If the `source` is a directory, it copies
|
||||
the contents inside source into the `destination` directory.
|
||||
`destination`. If the source is a directory, it copies
|
||||
the contents inside source into the destination.
|
||||
|
||||
If a file already exists in the destination, it invokes `callback`.
|
||||
`callback` must be a function that takes two arguments: `source` and `destination`.
|
||||
The callback should return `true` if the existing file should be overwritten and `false` otherwise.
|
||||
|
||||
If a directory already exists in the destination
|
||||
where a file is meant to be (or vice versa), this
|
||||
function will fail.
|
||||
|
||||
This function may fail while copying files,
|
||||
in such cases, it will leave the destination
|
||||
directory in a dirty state, where file which have already been copied
|
||||
@@ -850,10 +809,9 @@ defmodule File do
|
||||
success, `files_and_directories` lists all files and directories copied in no
|
||||
specific order. It returns `{:error, reason, file}` otherwise.
|
||||
|
||||
Note: The command `cp` in Unix-like systems behaves differently depending on
|
||||
whether `destination` is an existing directory or not. We have chosen to
|
||||
explicitly disallow this behaviour. If `source` is a `file` and `destination`
|
||||
is a directory, `{:error, :eisdir}` will be returned.
|
||||
Note: The command `cp` in Unix systems behaves differently
|
||||
depending if `destination` is an existing directory or not.
|
||||
We have chosen to explicitly disallow this behaviour.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -908,6 +866,8 @@ defmodule File do
|
||||
end
|
||||
end
|
||||
|
||||
# src may be a file or a directory, dest is definitely
|
||||
# a directory. Returns nil unless an error is found.
|
||||
defp do_cp_r(src, dest, callback, acc) when is_list(acc) do
|
||||
case :elixir_utils.read_link_type(src) do
|
||||
{:ok, :regular} ->
|
||||
@@ -1247,7 +1207,7 @@ defmodule File do
|
||||
end
|
||||
|
||||
# On Windows, symlinks are treated as directory and must be removed
|
||||
# with rmdir/1. But on Unix-like systems, we remove them via rm/1. So we first try
|
||||
# with rmdir/1. But on Unix, we remove them via rm/1. So we first try
|
||||
# to remove it as a directory and, if we get :enotdir, we fall back to
|
||||
# a file removal.
|
||||
defp do_rm_directory(path, {:ok, acc} = entry) do
|
||||
@@ -1314,7 +1274,7 @@ defmodule File do
|
||||
|
||||
The allowed modes:
|
||||
|
||||
* `:binary` - opens the file in binary mode, disabling special handling of Unicode sequences
|
||||
* `:binary` - opens the file in binary mode, disabling special handling of unicode sequences
|
||||
(default mode).
|
||||
|
||||
* `:read` - the file, which must exist, is opened for reading.
|
||||
@@ -1359,11 +1319,9 @@ defmodule File do
|
||||
* `{:ok, io_device}` - the file has been opened in the requested mode.
|
||||
|
||||
`io_device` is actually the PID of the process which handles the file.
|
||||
This process monitors the process that originally opened the file (the
|
||||
owner process). If the owner process terminates, the file is closed and
|
||||
the process itself terminates too. If any process to which the `io_device`
|
||||
is linked terminates, the file will be closed and the process itself will
|
||||
be terminated.
|
||||
This process is linked to the process which originally opened the file.
|
||||
If any process to which the `io_device` is linked terminates, the file
|
||||
will be closed and the process itself will be terminated.
|
||||
|
||||
An `io_device` returned from this call can be used as an argument to the
|
||||
`IO` module functions.
|
||||
@@ -1468,7 +1426,7 @@ defmodule File do
|
||||
@doc """
|
||||
Gets the current working directory.
|
||||
|
||||
In rare circumstances, this function can fail on Unix-like systems. It may happen
|
||||
In rare circumstances, this function can fail on Unix. It may happen
|
||||
if read permissions do not exist for the parent directories of the
|
||||
current directory. For this reason, returns `{:ok, cwd}` in case
|
||||
of success, `{:error, reason}` otherwise.
|
||||
@@ -1507,12 +1465,6 @@ defmodule File do
|
||||
@doc """
|
||||
Sets the current working directory.
|
||||
|
||||
The current working directory is set for the BEAM globally. This can lead to
|
||||
race conditions if multiple processes are changing the current working
|
||||
directory concurrently. To run an external command in a given directory
|
||||
without changing the global current working directory, use the `:cd` option
|
||||
of `System.cmd/3` and `Port.open/2`.
|
||||
|
||||
Returns `:ok` if successful, `{:error, reason}` otherwise.
|
||||
"""
|
||||
@spec cd(Path.t()) :: :ok | {:error, posix}
|
||||
@@ -1542,12 +1494,6 @@ defmodule File do
|
||||
executes the given function and then reverts back
|
||||
to the previous path regardless of whether there is an exception.
|
||||
|
||||
The current working directory is temporarily set for the BEAM globally. This
|
||||
can lead to race conditions if multiple processes are changing the current
|
||||
working directory concurrently. To run an external command in a given
|
||||
directory without changing the global current working directory, use the
|
||||
`:cd` option of `System.cmd/3` and `Port.open/2`.
|
||||
|
||||
Raises an error if retrieving or changing the current
|
||||
directory fails.
|
||||
"""
|
||||
@@ -1614,9 +1560,7 @@ defmodule File do
|
||||
which means it can be used both for read and write.
|
||||
|
||||
The `line_or_bytes` argument configures how the file is read when
|
||||
streaming, by `:line` (default) or by a given number of bytes. When
|
||||
using the `:line` option, CRLF line breaks (`"\r\n"`) are normalized
|
||||
to LF (`"\n"`).
|
||||
streaming, by `:line` (default) or by a given number of bytes.
|
||||
|
||||
Operating the stream can fail on open for the same reasons as
|
||||
`File.open!/2`. Note that the file is automatically opened each time streaming
|
||||
@@ -1630,7 +1574,7 @@ defmodule File do
|
||||
in raw mode for performance reasons. Therefore, Elixir **will** open
|
||||
streams in `:raw` mode with the `:read_ahead` option unless an encoding
|
||||
is specified. This means any data streamed into the file must be
|
||||
converted to `t:iodata/0` type. If you pass, for example, `[encoding: :utf8]`
|
||||
converted to `t:iodata/0` type. If you pass e.g. `[encoding: :utf8]`
|
||||
or `[encoding: {:utf16, :little}]` in the modes parameter,
|
||||
the underlying stream will use `IO.write/2` and the `String.Chars` protocol
|
||||
to convert the data. See `IO.binwrite/2` and `IO.write/2` .
|
||||
@@ -1655,7 +1599,7 @@ defmodule File do
|
||||
|
||||
See `Stream.run/1` for an example of streaming into a file.
|
||||
"""
|
||||
@spec stream!(Path.t(), [stream_mode], :line | pos_integer) :: File.Stream.t()
|
||||
@spec stream!(Path.t(), stream_mode, :line | pos_integer) :: File.Stream.t()
|
||||
def stream!(path, modes \\ [], line_or_bytes \\ :line) do
|
||||
modes = normalize_modes(modes, true)
|
||||
File.Stream.__build__(IO.chardata_to_string(path), modes, line_or_bytes)
|
||||
@@ -1711,7 +1655,7 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Changes the group given by the group ID `gid`
|
||||
Changes the group given by the group id `gid`
|
||||
for a given `file`. Returns `:ok` on success, or
|
||||
`{:error, reason}` on failure.
|
||||
"""
|
||||
@@ -1739,7 +1683,7 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Changes the owner given by the user ID `uid`
|
||||
Changes the owner given by the user id `uid`
|
||||
for a given `file`. Returns `:ok` on success,
|
||||
or `{:error, reason}` on failure.
|
||||
"""
|
||||
@@ -1789,7 +1733,7 @@ defmodule File do
|
||||
[read_ahead: @read_ahead_size] ++ normalize_modes(rest, binary?)
|
||||
end
|
||||
|
||||
# TODO: Remove :char_list mode on v2.0
|
||||
# TODO: Remove :char_list mode by 2.0
|
||||
defp normalize_modes([mode | rest], _binary?) when mode in [:charlist, :char_list] do
|
||||
if mode == :char_list do
|
||||
IO.warn("the :char_list mode is deprecated, use :charlist")
|
||||
|
||||
@@ -23,7 +23,7 @@ defmodule File.Stat do
|
||||
* `mtime` - the last time the file was written.
|
||||
|
||||
* `ctime` - the interpretation of this time field depends on the operating
|
||||
system. On Unix-like operating systems, it is the last time the file or the inode was changed.
|
||||
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.
|
||||
@@ -35,17 +35,17 @@ defmodule File.Stat do
|
||||
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-like systems. In all other
|
||||
* `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-like file systems, this field
|
||||
* `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-like file
|
||||
* `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-like file systems.
|
||||
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`,
|
||||
@@ -63,9 +63,9 @@ defmodule File.Stat do
|
||||
size: non_neg_integer(),
|
||||
type: :device | :directory | :regular | :other | :symlink,
|
||||
access: :read | :write | :read_write | :none,
|
||||
atime: :calendar.datetime() | integer(),
|
||||
mtime: :calendar.datetime() | integer(),
|
||||
ctime: :calendar.datetime() | integer(),
|
||||
atime: :calendar.datetime(),
|
||||
mtime: :calendar.datetime(),
|
||||
ctime: :calendar.datetime(),
|
||||
mode: non_neg_integer(),
|
||||
links: non_neg_integer(),
|
||||
major_device: non_neg_integer(),
|
||||
@@ -78,7 +78,6 @@ defmodule File.Stat do
|
||||
@doc """
|
||||
Converts a `File.Stat` struct to a `:file_info` record.
|
||||
"""
|
||||
@spec to_record(t()) :: :file.file_info()
|
||||
def to_record(%File.Stat{unquote_splicing(pairs)}) do
|
||||
{:file_info, unquote_splicing(vals)}
|
||||
end
|
||||
@@ -86,7 +85,6 @@ defmodule File.Stat do
|
||||
@doc """
|
||||
Converts a `:file_info` record into a `File.Stat`.
|
||||
"""
|
||||
@spec from_record(:file.file_info()) :: t()
|
||||
def from_record(file_info)
|
||||
|
||||
def from_record({:file_info, unquote_splicing(vals)}) do
|
||||
|
||||
+45
-98
@@ -36,7 +36,7 @@ defmodule Float do
|
||||
To learn more about floating-point arithmetic visit:
|
||||
|
||||
* [0.30000000000000004.com](http://0.30000000000000004.com/)
|
||||
* [What Every Programmer Should Know About Floating-Point Arithmetic](https://floating-point-gui.de/)
|
||||
* [What Every Programmer Should Know About Floating-Point Arithmetic](http://floating-point-gui.de/)
|
||||
|
||||
"""
|
||||
|
||||
@@ -46,46 +46,6 @@ defmodule Float do
|
||||
@precision_range 0..15
|
||||
@type precision_range :: 0..15
|
||||
|
||||
@doc """
|
||||
Computes `base` raised to power of `exponent`.
|
||||
|
||||
`base` must be a float and `exponent` can be any number.
|
||||
However, if a negative base and a fractional exponent
|
||||
are given, it raises `ArithmeticError`.
|
||||
|
||||
It always returns a float. See `Integer.pow/2` for
|
||||
exponentiation that returns integers.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.pow(2.0, 0)
|
||||
1.0
|
||||
iex> Float.pow(2.0, 1)
|
||||
2.0
|
||||
iex> Float.pow(2.0, 10)
|
||||
1024.0
|
||||
iex> Float.pow(2.0, -1)
|
||||
0.5
|
||||
iex> Float.pow(2.0, -3)
|
||||
0.125
|
||||
|
||||
iex> Float.pow(3.0, 1.5)
|
||||
5.196152422706632
|
||||
|
||||
iex> Float.pow(-2.0, 3)
|
||||
-8.0
|
||||
iex> Float.pow(-2.0, 4)
|
||||
16.0
|
||||
|
||||
iex> Float.pow(-1.0, 0.5)
|
||||
** (ArithmeticError) bad argument in arithmetic expression
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec pow(float, number) :: float
|
||||
def pow(base, exponent) when is_float(base) and is_number(exponent),
|
||||
do: :math.pow(base, exponent)
|
||||
|
||||
@doc """
|
||||
Parses a binary into a float.
|
||||
|
||||
@@ -308,11 +268,11 @@ defmodule Float do
|
||||
raise ArgumentError, invalid_precision_message(precision)
|
||||
end
|
||||
|
||||
defp round(0.0 = num, _precision, _rounding), do: num
|
||||
defp round(0.0, _precision, _rounding), do: 0.0
|
||||
|
||||
defp round(float, precision, rounding) do
|
||||
<<sign::1, exp::11, significant::52-bitstring>> = <<float::float>>
|
||||
{num, count} = decompose(significant, 1)
|
||||
{num, count, _} = decompose(significant, 1)
|
||||
count = count - exp + 1023
|
||||
|
||||
cond do
|
||||
@@ -345,7 +305,7 @@ defmodule Float do
|
||||
num = rounding(rounding, sign, num, div)
|
||||
|
||||
# Convert back to float without loss
|
||||
# https://www.exploringbinary.com/correct-decimal-to-floating-point-using-big-integers/
|
||||
# http://www.exploringbinary.com/correct-decimal-to-floating-point-using-big-integers/
|
||||
den = power_of_10(precision)
|
||||
boundary = den <<< 52
|
||||
|
||||
@@ -364,22 +324,6 @@ defmodule Float do
|
||||
end
|
||||
end
|
||||
|
||||
defp decompose(significant, initial) do
|
||||
decompose(significant, 1, 0, initial)
|
||||
end
|
||||
|
||||
defp decompose(<<1::1, bits::bitstring>>, count, last_count, acc) do
|
||||
decompose(bits, count + 1, count, (acc <<< (count - last_count)) + 1)
|
||||
end
|
||||
|
||||
defp decompose(<<0::1, bits::bitstring>>, count, last_count, acc) do
|
||||
decompose(bits, count + 1, last_count, acc)
|
||||
end
|
||||
|
||||
defp decompose(<<>>, _count, last_count, acc) do
|
||||
{acc, last_count}
|
||||
end
|
||||
|
||||
defp scale_up(num, boundary, exp) when num >= boundary, do: {num, exp}
|
||||
defp scale_up(num, boundary, exp), do: scale_up(num <<< 1, boundary, exp - 1)
|
||||
|
||||
@@ -459,55 +403,55 @@ defmodule Float do
|
||||
def ratio(0.0), do: {0, 1}
|
||||
|
||||
def ratio(float) when is_float(float) do
|
||||
<<sign::1, exp::11, mantissa::52>> = <<float::float>>
|
||||
case <<float::float>> do
|
||||
<<sign::1, 0::11, significant::52-bitstring>> ->
|
||||
{num, _, den} = decompose(significant, 0)
|
||||
{sign(sign, num), shift_left(den, 1022)}
|
||||
|
||||
{num, den_exp} =
|
||||
if exp != 0 do
|
||||
# Floats are expressed like this:
|
||||
# (2**52 + mantissa) * 2**(-52 + exp - 1023)
|
||||
#
|
||||
# We compute the root factors of the mantissa so we have this:
|
||||
# (2**52 + mantissa * 2**count) * 2**(-52 + exp - 1023)
|
||||
{mantissa, count} = root_factors(mantissa, 0)
|
||||
<<sign::1, exp::11, significant::52-bitstring>> ->
|
||||
{num, _, den} = decompose(significant, 1)
|
||||
num = sign(sign, num)
|
||||
|
||||
# Now we can move the count around so we have this:
|
||||
# (2**(52-count) + mantissa) * 2**(count + -52 + exp - 1023)
|
||||
if mantissa == 0 do
|
||||
{1, exp - 1023}
|
||||
else
|
||||
num = (1 <<< (52 - count)) + mantissa
|
||||
den_exp = count - 52 + exp - 1023
|
||||
{num, den_exp}
|
||||
case exp - 1023 do
|
||||
exp when exp > 0 ->
|
||||
{den, exp} = shift_right(den, exp)
|
||||
{shift_left(num, exp), den}
|
||||
|
||||
exp when exp < 0 ->
|
||||
{num, shift_left(den, -exp)}
|
||||
|
||||
0 ->
|
||||
{num, den}
|
||||
end
|
||||
else
|
||||
# Subnormals are expressed like this:
|
||||
# (mantissa) * 2**(-52 + 1 - 1023)
|
||||
#
|
||||
# So we compute it to this:
|
||||
# (mantissa * 2**(count)) * 2**(-52 + 1 - 1023)
|
||||
#
|
||||
# Which becomes:
|
||||
# mantissa * 2**(count-1074)
|
||||
root_factors(mantissa, -1074)
|
||||
end
|
||||
|
||||
if den_exp > 0 do
|
||||
{sign(sign, num <<< den_exp), 1}
|
||||
else
|
||||
{sign(sign, num), 1 <<< -den_exp}
|
||||
end
|
||||
end
|
||||
|
||||
defp root_factors(mantissa, count) when mantissa != 0 and (mantissa &&& 1) == 0,
|
||||
do: root_factors(mantissa >>> 1, count + 1)
|
||||
defp decompose(significant, initial) do
|
||||
decompose(significant, 1, 0, 2, 1, initial)
|
||||
end
|
||||
|
||||
defp root_factors(mantissa, count),
|
||||
do: {mantissa, count}
|
||||
defp decompose(<<1::1, bits::bitstring>>, count, last_count, power, _last_power, acc) do
|
||||
decompose(bits, count + 1, count, power <<< 1, power, shift_left(acc, count - last_count) + 1)
|
||||
end
|
||||
|
||||
@compile {:inline, sign: 2}
|
||||
defp decompose(<<0::1, bits::bitstring>>, count, last_count, power, last_power, acc) do
|
||||
decompose(bits, count + 1, last_count, power <<< 1, last_power, acc)
|
||||
end
|
||||
|
||||
defp decompose(<<>>, _count, last_count, _power, last_power, acc) do
|
||||
{acc, last_count, last_power}
|
||||
end
|
||||
|
||||
@compile {:inline, sign: 2, shift_left: 2}
|
||||
defp sign(0, num), do: num
|
||||
defp sign(1, num), do: -num
|
||||
|
||||
defp shift_left(num, times), do: num <<< times
|
||||
|
||||
defp shift_right(num, 0), do: {num, 0}
|
||||
defp shift_right(1, times), do: {1, times}
|
||||
defp shift_right(num, times), do: shift_right(num >>> 1, times - 1)
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation
|
||||
of the given float.
|
||||
@@ -549,16 +493,19 @@ defmodule Float do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use Float.to_charlist/1 instead"
|
||||
def to_char_list(float), do: Float.to_charlist(float)
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use :erlang.float_to_list/2 instead"
|
||||
def to_char_list(float, options) do
|
||||
:erlang.float_to_list(float, expand_compact(options))
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use :erlang.float_to_binary/2 instead"
|
||||
def to_string(float, options) do
|
||||
:erlang.float_to_binary(float, expand_compact(options))
|
||||
|
||||
@@ -2,59 +2,11 @@ defmodule Function do
|
||||
@moduledoc """
|
||||
A set of functions for working with functions.
|
||||
|
||||
Anonymous functions are typically created by using `fn`:
|
||||
|
||||
iex> add = fn a, b -> a + b end
|
||||
iex> add.(1, 2)
|
||||
3
|
||||
|
||||
Anonymous functions can also have multiple clauses. All clauses
|
||||
should expect the same number of arguments:
|
||||
|
||||
iex> negate = fn
|
||||
...> true -> false
|
||||
...> false -> true
|
||||
...> end
|
||||
iex> negate.(false)
|
||||
true
|
||||
|
||||
## The capture operator
|
||||
|
||||
It is also possible to capture public module functions and pass them
|
||||
around as if they were anonymous functions by using the capture
|
||||
operator `Kernel.SpecialForms.&/1`:
|
||||
|
||||
iex> add = &Kernel.+/2
|
||||
iex> add.(1, 2)
|
||||
3
|
||||
|
||||
iex> length = &String.length/1
|
||||
iex> length.("hello")
|
||||
5
|
||||
|
||||
To capture a definition within the current module, you can skip the
|
||||
module prefix, such as `&my_fun/2`. In those cases, the captured
|
||||
function can be public (`def`) or private (`defp`).
|
||||
|
||||
The capture operator can also be used to create anonymous functions
|
||||
that expect at least one argument:
|
||||
|
||||
iex> add = &(&1 + &2)
|
||||
iex> add.(1, 2)
|
||||
3
|
||||
|
||||
In such cases, using the capture operator is no different than using `fn`.
|
||||
|
||||
## Internal and external functions
|
||||
|
||||
We say that functions that point to definitions residing in modules, such
|
||||
as `&String.length/1`, are **external** functions. All other functions are
|
||||
**local** and they are always bound to the file or module that defined them.
|
||||
|
||||
Besides the functions in this module to work with functions, `Kernel` also
|
||||
has an `apply/2` function that invokes a function with a dynamic number of
|
||||
arguments, as well as `is_function/1` and `is_function/2`, to check
|
||||
respectively if a given value is a function or a function of a given arity.
|
||||
There are two types of captured functions: **external** and **local**.
|
||||
External functions are functions residing in modules that are captured
|
||||
with `&/1`, such as `&String.length/1`. Local functions are anonymous functions
|
||||
defined with `fn/1` or with the capture operator `&/1` using `&1`, `&2`,
|
||||
and so on as replacements.
|
||||
"""
|
||||
|
||||
@type information ::
|
||||
@@ -182,27 +134,4 @@ defmodule Function do
|
||||
@doc since: "1.7.0"
|
||||
@spec info(fun, item) :: {item, term} when item: information
|
||||
def info(fun, item), do: :erlang.fun_info(fun, item)
|
||||
|
||||
@doc """
|
||||
Returns its input `value`. This function can be passed as an anonymous function
|
||||
to transformation functions.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Function.identity("Hello world!")
|
||||
"Hello world!"
|
||||
|
||||
iex> 'abcdaabccc' |> Enum.sort() |> Enum.chunk_by(&Function.identity/1)
|
||||
['aaa', 'bb', 'cccc', 'd']
|
||||
|
||||
iex> Enum.group_by('abracadabra', &Function.identity/1)
|
||||
%{97 => 'aaaaa', 98 => 'bb', 99 => 'c', 100 => 'd', 114 => 'rr'}
|
||||
|
||||
iex> Enum.map([1, 2, 3, 4], &Function.identity/1)
|
||||
[1, 2, 3, 4]
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec identity(value) :: value when value: var
|
||||
def identity(value), do: value
|
||||
end
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
defmodule GenEvent do
|
||||
# TODO: Remove by 2.0
|
||||
|
||||
# Functions from this module are deprecated in elixir_dispatch.
|
||||
|
||||
@moduledoc """
|
||||
@@ -7,7 +9,7 @@ defmodule GenEvent do
|
||||
If you are interested in implementing an event manager, please read the
|
||||
"Alternatives" section below. If you have to implement an event handler to
|
||||
integrate with an existing system, such as Elixir's Logger, please use
|
||||
[`:gen_event`](`:gen_event`) instead.
|
||||
`:gen_event` instead.
|
||||
|
||||
## Alternatives
|
||||
|
||||
@@ -38,7 +40,7 @@ defmodule GenEvent do
|
||||
|
||||
If your use case requires exactly what GenEvent provided, or you have to
|
||||
integrate with an existing `:gen_event`-based system, you can still use the
|
||||
[`:gen_event`](`:gen_event`) Erlang module.
|
||||
[`:gen_event`](http://erlang.org/doc/man/gen_event.html) Erlang module.
|
||||
"""
|
||||
|
||||
@moduledoc deprecated: "Use Erlang/OTP's :gen_event module instead"
|
||||
@@ -88,10 +90,12 @@ defmodule GenEvent do
|
||||
@deprecated message
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
%{file: file, line: line} = __CALLER__
|
||||
|
||||
deprecation_message =
|
||||
"the GenEvent module is deprecated, see its documentation for alternatives"
|
||||
|
||||
IO.warn(deprecation_message, Macro.Env.stacktrace(__CALLER__))
|
||||
:elixir_errors.warn(line, file, deprecation_message)
|
||||
|
||||
quote location: :keep do
|
||||
@behaviour :gen_event
|
||||
@@ -324,7 +328,14 @@ defmodule GenEvent do
|
||||
|
||||
def init_it(starter, parent, name, _mod, _args, options) do
|
||||
Process.put(:"$initial_call", {__MODULE__, :init_it, 6})
|
||||
debug = :gen.debug_options(name, options)
|
||||
|
||||
debug =
|
||||
if function_exported?(:gen, :debug_options, 2) do
|
||||
:gen.debug_options(name, options)
|
||||
else
|
||||
:gen.debug_options(options)
|
||||
end
|
||||
|
||||
:proc_lib.init_ack(starter, {:ok, self()})
|
||||
loop(parent, name(name), [], debug, false)
|
||||
end
|
||||
|
||||
+68
-113
@@ -16,7 +16,7 @@ defmodule GenServer do
|
||||
|
||||
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 elements:
|
||||
and pop items:
|
||||
|
||||
defmodule Stack do
|
||||
use GenServer
|
||||
@@ -34,8 +34,8 @@ defmodule GenServer do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_cast({:push, element}, state) do
|
||||
{:noreply, [element | state]}
|
||||
def handle_cast({:push, item}, state) do
|
||||
{:noreply, [item | state]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -54,16 +54,16 @@ defmodule GenServer do
|
||||
|
||||
We start our `Stack` by calling `start_link/2`, passing the module
|
||||
with the server implementation and its initial argument (a list
|
||||
representing the stack containing the element `:hello`). We can primarily
|
||||
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 `c:handle_call/3` callback in the GenServer.
|
||||
A `cast/2` message must be handled by `c:handle_cast/2`. There are 8 possible
|
||||
A `cast/2` message must be handled by `c:handle_cast/2`. There are 7 possible
|
||||
callbacks to be implemented when you use a `GenServer`. The only required
|
||||
callback is `c:init/1`.
|
||||
callback is `init/1`.
|
||||
|
||||
## Client / Server APIs
|
||||
|
||||
@@ -83,8 +83,8 @@ defmodule GenServer do
|
||||
GenServer.start_link(__MODULE__, default)
|
||||
end
|
||||
|
||||
def push(pid, element) do
|
||||
GenServer.cast(pid, {:push, element})
|
||||
def push(pid, item) do
|
||||
GenServer.cast(pid, {:push, item})
|
||||
end
|
||||
|
||||
def pop(pid) do
|
||||
@@ -104,8 +104,8 @@ defmodule GenServer do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_cast({:push, element}, state) do
|
||||
{:noreply, [element | state]}
|
||||
def handle_cast({:push, item}, state) do
|
||||
{:noreply, [item | state]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -142,6 +142,7 @@ defmodule GenServer do
|
||||
The generated `child_spec/1` can be customized with the following options:
|
||||
|
||||
* `:id` - the child specification identifier, defaults to the current module
|
||||
* `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
|
||||
* `:restart` - when the child should be restarted, defaults to `:permanent`
|
||||
* `:shutdown` - how to shut down the child, either immediately or by giving it time to shut down
|
||||
|
||||
@@ -159,16 +160,16 @@ defmodule GenServer do
|
||||
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 (to the current node)
|
||||
with the given name using `Process.register/2`.
|
||||
* 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](`:global`).
|
||||
term using the functions in the [`:global` module](http://www.erlang.org/doc/man/global.html).
|
||||
|
||||
* `{: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](`:global`) which uses these functions
|
||||
One such example is the [`:global` module](http://www.erlang.org/doc/man/global.html) which uses these functions
|
||||
for keeping the list of names of processes and their associated PIDs
|
||||
that are available globally for a network of Elixir nodes. Elixir also
|
||||
ships with a local, decentralized and scalable registry called `Registry`
|
||||
@@ -233,7 +234,7 @@ defmodule GenServer do
|
||||
@impl true
|
||||
def handle_info(:work, state) do
|
||||
# Do the desired work here
|
||||
# ...
|
||||
...
|
||||
|
||||
# Reschedule once more
|
||||
schedule_work()
|
||||
@@ -242,33 +243,11 @@ defmodule GenServer do
|
||||
end
|
||||
|
||||
defp schedule_work do
|
||||
# We schedule the work to happen in 2 hours (written in milliseconds).
|
||||
# Alternatively, one might write :timer.hours(2)
|
||||
# In 2 hours
|
||||
Process.send_after(self(), :work, 2 * 60 * 60 * 1000)
|
||||
end
|
||||
end
|
||||
|
||||
## Timeouts
|
||||
|
||||
The return value of `c:init/1` or any of the `handle_*` callbacks may include
|
||||
a timeout value in milliseconds; if not, `:infinity` is assumed.
|
||||
The timeout can be used to detect a lull in incoming messages.
|
||||
|
||||
The `timeout()` value is used as follows:
|
||||
|
||||
* If the process has any message already waiting when the `timeout()` value
|
||||
is returned, the timeout is ignored and the waiting message is handled as
|
||||
usual. This means that even a timeout of `0` milliseconds is not guaranteed
|
||||
to execute (if you want to take another action immediately and unconditionally,
|
||||
use a `:continue` instruction instead).
|
||||
|
||||
* If any message arrives before the specified number of milliseconds
|
||||
elapse, the timeout is cleared and that message is handled as usual.
|
||||
|
||||
* Otherwise, when the specified number of milliseconds have elapsed with no
|
||||
message arriving, `handle_info/2` is called with `:timeout` as the first
|
||||
argument.
|
||||
|
||||
## When (not) to use a GenServer
|
||||
|
||||
So far, we have learned that a `GenServer` can be used as a supervised process
|
||||
@@ -286,10 +265,6 @@ defmodule GenServer do
|
||||
GenServer.call(__MODULE__, {:add, a, b})
|
||||
end
|
||||
|
||||
def subtract(a, b) do
|
||||
GenServer.call(__MODULE__, {:subtract, a, b})
|
||||
end
|
||||
|
||||
def handle_call({:add, a, b}, _from, state) do
|
||||
{:reply, a + b, state}
|
||||
end
|
||||
@@ -317,14 +292,14 @@ defmodule GenServer do
|
||||
|
||||
## Debugging with the :sys module
|
||||
|
||||
GenServers, as [special processes](https://erlang.org/doc/design_principles/spec_proc.html),
|
||||
can be debugged using the [`:sys` module](`:sys`).
|
||||
GenServers, as [special processes](http://erlang.org/doc/design_principles/spec_proc.html),
|
||||
can be debugged using the [`:sys` module](http://www.erlang.org/doc/man/sys.html).
|
||||
Through various hooks, this module allows developers to introspect the state of
|
||||
the process and trace system events that happen during its execution, such as
|
||||
received messages, sent replies and state changes.
|
||||
|
||||
Let's explore the basic functions from the
|
||||
[`:sys` module](`:sys`) used for debugging:
|
||||
[`:sys` module](http://www.erlang.org/doc/man/sys.html) used for debugging:
|
||||
|
||||
* `:sys.get_state/2` - allows retrieval of the state of the process.
|
||||
In the case of a GenServer process, it will be the callback module state,
|
||||
@@ -406,8 +381,8 @@ defmodule GenServer do
|
||||
in Erlang can also provide extra insight.
|
||||
|
||||
* [GenServer - Elixir's Getting Started Guide](https://elixir-lang.org/getting-started/mix-otp/genserver.html)
|
||||
* [`:gen_server` module documentation](`:gen_server`)
|
||||
* [gen_server Behaviour - OTP Design Principles](https://erlang.org/doc/design_principles/gen_server_concepts.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)
|
||||
|
||||
"""
|
||||
@@ -421,16 +396,16 @@ defmodule GenServer do
|
||||
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 that it also sets a timeout. See the "Timeouts" section
|
||||
in the module documentation for more information.
|
||||
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
|
||||
`c:handle_call/3` for more information on hibernation.
|
||||
|
||||
Returning `{:ok, state, {:continue, continue}}` is similar to
|
||||
`{:ok, state}` except that immediately after entering the loop,
|
||||
`{:ok, state}` except that immediately after entering the loop
|
||||
the `c:handle_continue/2` callback will be invoked with the value
|
||||
`continue` as first argument.
|
||||
|
||||
@@ -472,13 +447,13 @@ defmodule GenServer do
|
||||
caller and continues the loop with new state `new_state`.
|
||||
|
||||
Returning `{:reply, reply, new_state, timeout}` is similar to
|
||||
`{:reply, reply, new_state}` except that it also sets a timeout.
|
||||
See the "Timeouts" section in the module documentation for more information.
|
||||
`{: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 in its message queue. However, if a message is
|
||||
already in the message queue, the process will continue the loop immediately. Hibernating a
|
||||
continue the loop once a message is in 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.
|
||||
|
||||
@@ -512,7 +487,7 @@ defmodule GenServer do
|
||||
occurs as with a `:reply` tuple.
|
||||
|
||||
Returning `{:stop, reason, reply, new_state}` stops the loop and `c:terminate/2`
|
||||
is called with reason `reason` and state `new_state`. Then, the `reply` is sent
|
||||
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
|
||||
@@ -538,9 +513,9 @@ defmodule GenServer do
|
||||
|
||||
Returning `{:noreply, new_state}` continues the loop with new state `new_state`.
|
||||
|
||||
Returning `{:noreply, new_state, timeout}` is similar to `{:noreply, new_state}`
|
||||
except that it also sets a timeout. See the "Timeouts" section in the module
|
||||
documentation for more information.
|
||||
Returning `{:noreply, new_state, timeout}` is similar to
|
||||
`{:noreply, 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
|
||||
@@ -590,6 +565,8 @@ defmodule GenServer do
|
||||
|
||||
This callback is optional. If one is not implemented, the server will fail
|
||||
if a continue instruction is used.
|
||||
|
||||
This callback is only supported on Erlang/OTP 21+.
|
||||
"""
|
||||
@callback handle_continue(continue :: term, state :: term) ::
|
||||
{:noreply, new_state}
|
||||
@@ -603,17 +580,19 @@ defmodule GenServer do
|
||||
`reason` is exit reason and `state` is the current state of the `GenServer`.
|
||||
The return value is ignored.
|
||||
|
||||
`c:terminate/2` is called if the `GenServer` traps exits (using `Process.flag/2`)
|
||||
*and* the parent process sends an exit signal, or a callback (except `c:init/1`)
|
||||
does one of the following:
|
||||
`c:terminate/2` is called if a callback (except `c:init/1`) does one of the
|
||||
following:
|
||||
|
||||
* returns a `:stop` tuple
|
||||
* raises (via `Kernel.raise/2`) or exits (via `Kernel.exit/1`)
|
||||
* raises
|
||||
* calls `Kernel.exit/1`
|
||||
* returns an invalid value
|
||||
* the `GenServer` traps exits (using `Process.flag/2`) *and* the parent
|
||||
process sends an exit signal
|
||||
|
||||
If part of a supervision tree, a `GenServer` will receive an exit
|
||||
If part of a supervision tree, a `GenServer`'s will receive an exit
|
||||
signal when the tree is shutting down. The exit signal is based on
|
||||
the shutdown strategy in the child's specification, where this
|
||||
the shutdown strategy in the child's specification, where we this
|
||||
value can be:
|
||||
|
||||
* `:brutal_kill`: the `GenServer` is killed and so `c:terminate/2` is not called.
|
||||
@@ -635,7 +614,7 @@ defmodule GenServer do
|
||||
Therefore it is not guaranteed that `c: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. There is no cleanup needed when the `GenServer` controls a `port` (for example,
|
||||
themselves. There is no cleanup needed when the `GenServer` controls a `port` (e.g.
|
||||
`:gen_tcp.socket`) or `t:File.io_device/0`, because these will be closed on
|
||||
receiving a `GenServer`'s exit signal and do not need to be closed manually
|
||||
in `c:terminate/2`.
|
||||
@@ -646,7 +625,7 @@ defmodule GenServer do
|
||||
This callback is optional.
|
||||
"""
|
||||
@callback terminate(reason, state :: term) :: term
|
||||
when reason: :normal | :shutdown | {:shutdown, term} | term
|
||||
when reason: :normal | :shutdown | {:shutdown, term}
|
||||
|
||||
@doc """
|
||||
Invoked to change the state of the `GenServer` when a different version of a
|
||||
@@ -672,7 +651,8 @@ defmodule GenServer do
|
||||
@callback code_change(old_vsn, state :: term, extra :: term) ::
|
||||
{:ok, new_state :: term}
|
||||
| {:error, reason :: term}
|
||||
when old_vsn: term | {:down, term}
|
||||
| {:down, term}
|
||||
when old_vsn: term
|
||||
|
||||
@doc """
|
||||
Invoked in some cases to retrieve a formatted version of the `GenServer` status.
|
||||
@@ -716,18 +696,13 @@ defmodule GenServer do
|
||||
{:debug, debug}
|
||||
| {:name, name}
|
||||
| {:timeout, timeout}
|
||||
| {:spawn_opt, [Process.spawn_opt()]}
|
||||
| {:spawn_opt, Process.spawn_opt()}
|
||||
| {:hibernate_after, timeout}
|
||||
|
||||
@typedoc "Debug options supported by the `start*` functions"
|
||||
@type debug :: [:trace | :log | :statistics | {:log_to_file, Path.t()}]
|
||||
|
||||
@typedoc """
|
||||
The server reference.
|
||||
|
||||
This is either a plain PID or a value representing a registered name.
|
||||
See the "Name registration" section of this document for more information.
|
||||
"""
|
||||
@typedoc "The server reference"
|
||||
@type server :: pid | name | {atom, node}
|
||||
|
||||
@typedoc """
|
||||
@@ -743,7 +718,7 @@ defmodule GenServer do
|
||||
quote location: :keep, bind_quoted: [opts: opts] do
|
||||
@behaviour GenServer
|
||||
|
||||
unless Module.has_attribute?(__MODULE__, :doc) do
|
||||
if Module.get_attribute(__MODULE__, :doc) == nil do
|
||||
@doc """
|
||||
Returns a specification to start this module under a supervisor.
|
||||
|
||||
@@ -762,7 +737,7 @@ defmodule GenServer do
|
||||
|
||||
defoverridable child_spec: 1
|
||||
|
||||
# TODO: Remove this on v2.0
|
||||
# TODO: Remove this on Elixir v2.0
|
||||
@before_compile GenServer
|
||||
|
||||
@doc false
|
||||
@@ -791,22 +766,8 @@ defmodule GenServer do
|
||||
{_, name} -> name
|
||||
end
|
||||
|
||||
:logger.error(
|
||||
%{
|
||||
label: {GenServer, :no_handle_info},
|
||||
report: %{
|
||||
module: __MODULE__,
|
||||
message: msg,
|
||||
name: proc
|
||||
}
|
||||
},
|
||||
%{
|
||||
domain: [:otp, :elixir],
|
||||
error_logger: %{tag: :error_msg},
|
||||
report_cb: &GenServer.format_report/1
|
||||
}
|
||||
)
|
||||
|
||||
pattern = '~p ~p received unexpected message in handle_info/2: ~p~n'
|
||||
:error_logger.error_msg(pattern, [__MODULE__, proc, msg])
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
@@ -858,7 +819,7 @@ defmodule GenServer do
|
||||
the arguments given to GenServer.start_link/3 to the server state.
|
||||
"""
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
|
||||
quote do
|
||||
@doc false
|
||||
@@ -895,7 +856,7 @@ defmodule GenServer do
|
||||
milliseconds initializing or it will be terminated and the start function
|
||||
will return `{:error, :timeout}`
|
||||
|
||||
* `:debug` - if present, the corresponding function in the [`:sys` module](`:sys`) is invoked
|
||||
* `:debug` - if present, the corresponding function in the [`:sys` module](http://www.erlang.org/doc/man/sys.html) is invoked
|
||||
|
||||
* `:spawn_opt` - if present, its value is passed as options to the
|
||||
underlying process as in `Process.spawn/4`
|
||||
@@ -1012,13 +973,11 @@ defmodule GenServer do
|
||||
element.
|
||||
"""
|
||||
@spec call(server, term, timeout) :: term
|
||||
def call(server, request, timeout \\ 5000)
|
||||
when (is_integer(timeout) and timeout >= 0) or timeout == :infinity do
|
||||
def call(server, request, timeout \\ 5000) do
|
||||
case whereis(server) do
|
||||
nil ->
|
||||
exit({:noproc, {__MODULE__, :call, [server, request, timeout]}})
|
||||
|
||||
# TODO: remove this clause when we require Erlang/OTP 25+
|
||||
pid when pid == self() ->
|
||||
exit({:calling_self, {__MODULE__, :call, [server, request, timeout]}})
|
||||
|
||||
@@ -1042,8 +1001,15 @@ defmodule GenServer do
|
||||
is unknown whether the destination `server` successfully
|
||||
handled the message.
|
||||
|
||||
`server` can be any of the values described in the "Name registration"
|
||||
section of the documentation for this module.
|
||||
`c: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 semantics differ
|
||||
depending on the used Erlang/OTP version.
|
||||
|
||||
Before Erlang/OTP 21, the call is going to block until a
|
||||
connection happens. This was done to guarantee ordering.
|
||||
Starting with Erlang/OTP 21, both Erlang and Elixir do
|
||||
not block the call.
|
||||
"""
|
||||
@spec cast(server, term) :: :ok
|
||||
def cast(server, request)
|
||||
@@ -1174,11 +1140,8 @@ defmodule GenServer do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the `pid` or `{name, node}` of a GenServer process, `nil` otherwise.
|
||||
|
||||
To be precise, `nil` is returned whenever a `pid` or `{name, node}` cannot
|
||||
be returned. Note there is no guarantee the returned `pid` or `{name, node}`
|
||||
is alive, as a process could terminate immediately after it is looked up.
|
||||
Returns the `pid` or `{name, node}` of a GenServer process, or `nil` if
|
||||
no process is associated with the given `server`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1219,12 +1182,4 @@ defmodule GenServer do
|
||||
def whereis({name, node} = server) when is_atom(name) and is_atom(node) do
|
||||
server
|
||||
end
|
||||
|
||||
@doc false
|
||||
def format_report(%{
|
||||
label: {GenServer, :no_handle_info},
|
||||
report: %{module: mod, message: msg, name: proc}
|
||||
}) do
|
||||
{'~p ~p received unexpected message in handle_info/2: ~p~n', [mod, proc, msg]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,6 +7,8 @@ defmodule HashDict do
|
||||
|
||||
@moduledoc deprecated: "Use Map instead"
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
|
||||
use Dict
|
||||
|
||||
@node_bitmap 0b111
|
||||
@@ -48,8 +50,8 @@ defmodule HashDict do
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def update(%HashDict{root: root, size: size}, key, default, fun) when is_function(fun, 1) do
|
||||
{root, counter} = do_update(root, key, fn -> default end, fun, key_hash(key))
|
||||
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
|
||||
|
||||
@@ -135,25 +137,25 @@ defmodule HashDict do
|
||||
end
|
||||
end
|
||||
|
||||
defp do_update(node, key, default, fun, hash) do
|
||||
defp do_update(node, key, initial, fun, hash) do
|
||||
index = key_mask(hash)
|
||||
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [key | default.()]), 1}
|
||||
{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 | default.()])
|
||||
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, default, fun, key_shift(hash))
|
||||
{n, counter} = do_update(n, key, initial, fun, key_shift(hash))
|
||||
{put_elem(node, index, {k, v, n}), counter}
|
||||
end
|
||||
end
|
||||
|
||||
+38
-73
@@ -26,22 +26,21 @@ defprotocol Inspect do
|
||||
defimpl Inspect, for: MapSet do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(map_set, opts) do
|
||||
concat(["#MapSet<", to_doc(MapSet.to_list(map_set), opts), ">"])
|
||||
def inspect(dict, opts) do
|
||||
concat(["#MapSet<", to_doc(MapSet.to_list(dict), opts), ">"])
|
||||
end
|
||||
end
|
||||
|
||||
The [`concat/1`](`Inspect.Algebra.concat/1`) function comes from
|
||||
`Inspect.Algebra` and it concatenates algebra documents together.
|
||||
In the example above it is concatenating the string `"#MapSet<"`,
|
||||
the document returned by `Inspect.Algebra.to_doc/2`, and the final
|
||||
string `">"`. We prefix the module name `#` to denote the inspect
|
||||
presentation is not actually valid Elixir syntax.
|
||||
The `concat/1` 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 `">"`.
|
||||
|
||||
Finally, note strings themselves are valid algebra documents that
|
||||
keep their formatting when pretty printed. This means your `Inspect`
|
||||
implementation may simply return a string, although that will devoid
|
||||
it of any pretty-printing.
|
||||
Since regular strings are valid entities in an algebra document,
|
||||
an implementation of the `Inspect` protocol may simply return a
|
||||
string, although that will devoid it of any pretty-printing.
|
||||
|
||||
## Error handling
|
||||
|
||||
@@ -78,15 +77,6 @@ defprotocol Inspect do
|
||||
# Handle structs in Any
|
||||
@fallback_to_any true
|
||||
|
||||
@doc """
|
||||
Converts `term` into an algebra document.
|
||||
|
||||
This function shouldn't be invoked directly, unless when implementing
|
||||
a custom `inspect_fun` to be given to `Inspect.Opts`. Everywhere else,
|
||||
`Inspect.Algebra.to_doc/2` should be preferred as it handles structs
|
||||
and exceptions.
|
||||
"""
|
||||
@spec inspect(t, Inspect.Opts.t()) :: Inspect.Algebra.t()
|
||||
def inspect(term, opts)
|
||||
end
|
||||
|
||||
@@ -157,7 +147,7 @@ defimpl Inspect, for: BitString do
|
||||
defp each_bit(bitstring, _counter, opts) do
|
||||
size = bit_size(bitstring)
|
||||
<<h::size(size)>> = bitstring
|
||||
concat(Inspect.Integer.inspect(h, opts), "::size(" <> Integer.to_string(size) <> ")")
|
||||
Inspect.Integer.inspect(h, opts) <> "::size(" <> Integer.to_string(size) <> ")"
|
||||
end
|
||||
|
||||
@compile {:inline, decrement: 1}
|
||||
@@ -170,7 +160,7 @@ defimpl Inspect, for: List do
|
||||
color("[]", :list, opts)
|
||||
end
|
||||
|
||||
# TODO: Remove :char_list and :as_char_lists handling on v2.0
|
||||
# TODO: Remove :char_list and :as_char_lists handling in 2.0
|
||||
def inspect(term, opts) do
|
||||
%Inspect.Opts{
|
||||
charlists: lists,
|
||||
@@ -254,7 +244,7 @@ defimpl Inspect, for: Map do
|
||||
end
|
||||
|
||||
def inspect(map, name, opts) do
|
||||
map = Map.to_list(map)
|
||||
map = :maps.to_list(map)
|
||||
open = color("%" <> name <> "{", :map, opts)
|
||||
sep = color(",", :map, opts)
|
||||
close = color("}", :map, opts)
|
||||
@@ -317,20 +307,11 @@ end
|
||||
|
||||
defimpl Inspect, for: Regex do
|
||||
def inspect(regex, opts) do
|
||||
{escaped, _} =
|
||||
regex.source
|
||||
|> normalize(<<>>)
|
||||
|> Identifier.escape(?/, :infinity, &escape_map/1)
|
||||
|
||||
{escaped, _} = Identifier.escape(regex.source, ?/, :infinity, &escape_map/1)
|
||||
source = IO.iodata_to_binary(['~r/', escaped, ?/, regex.opts])
|
||||
color(source, :regex, opts)
|
||||
end
|
||||
|
||||
defp normalize(<<?\\, ?\\, rest::binary>>, acc), do: normalize(rest, <<acc::binary, ?\\, ?\\>>)
|
||||
defp normalize(<<?\\, ?/, rest::binary>>, acc), do: normalize(rest, <<acc::binary, ?/>>)
|
||||
defp normalize(<<char, rest::binary>>, acc), do: normalize(rest, <<acc::binary, char>>)
|
||||
defp normalize(<<>>, acc), do: acc
|
||||
|
||||
defp escape_map(?\a), do: '\\a'
|
||||
defp escape_map(?\f), do: '\\f'
|
||||
defp escape_map(?\n), do: '\\n'
|
||||
@@ -410,7 +391,11 @@ end
|
||||
|
||||
defimpl Inspect, for: Any do
|
||||
defmacro __deriving__(module, struct, options) do
|
||||
fields = Map.keys(struct) -- [:__exception__, :__struct__]
|
||||
fields =
|
||||
struct
|
||||
|> Map.drop([:__exception__, :__struct__])
|
||||
|> Map.keys()
|
||||
|
||||
only = Keyword.get(options, :only, fields)
|
||||
except = Keyword.get(options, :except, [])
|
||||
|
||||
@@ -421,17 +406,18 @@ defimpl Inspect, for: Any do
|
||||
|
||||
inspect_module =
|
||||
if fields == only and except == [] do
|
||||
Inspect.Map
|
||||
quote(do: Inspect.Map)
|
||||
else
|
||||
Inspect.Any
|
||||
quote(do: Inspect.Any)
|
||||
end
|
||||
|
||||
quote do
|
||||
defimpl Inspect, for: unquote(module) do
|
||||
def inspect(var!(struct), var!(opts)) do
|
||||
var!(map) = Map.take(var!(struct), unquote(filtered_fields))
|
||||
var!(name) = Identifier.inspect_as_atom(unquote(module))
|
||||
unquote(inspect_module).inspect(var!(map), var!(name), var!(opts))
|
||||
def inspect(struct, opts) do
|
||||
map = Map.take(struct, unquote(filtered_fields))
|
||||
colorless_opts = %{opts | syntax_colors: []}
|
||||
name = Inspect.Atom.inspect(unquote(module), colorless_opts)
|
||||
unquote(inspect_module).inspect(map, name, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -439,14 +425,15 @@ defimpl Inspect, for: Any do
|
||||
|
||||
def inspect(%module{} = struct, opts) do
|
||||
try do
|
||||
module.__struct__()
|
||||
module.__struct__
|
||||
rescue
|
||||
_ -> Inspect.Map.inspect(struct, opts)
|
||||
else
|
||||
dunder ->
|
||||
if Map.keys(dunder) == Map.keys(struct) do
|
||||
pruned = Map.drop(struct, [:__struct__, :__exception__])
|
||||
Inspect.Map.inspect(pruned, Identifier.inspect_as_atom(module), opts)
|
||||
if :maps.keys(dunder) == :maps.keys(struct) do
|
||||
pruned = :maps.remove(:__exception__, :maps.remove(:__struct__, struct))
|
||||
colorless_opts = %{opts | syntax_colors: []}
|
||||
Inspect.Map.inspect(pruned, Inspect.Atom.inspect(module, colorless_opts), opts)
|
||||
else
|
||||
Inspect.Map.inspect(struct, opts)
|
||||
end
|
||||
@@ -454,37 +441,15 @@ defimpl Inspect, for: Any do
|
||||
end
|
||||
|
||||
def inspect(map, name, opts) do
|
||||
map = Map.to_list(map) ++ [:...]
|
||||
# Use the :limit option and an extra element to force
|
||||
# `container_doc/6` to append "...".
|
||||
opts = %{opts | limit: min(opts.limit, map_size(map))}
|
||||
map = :maps.to_list(map) ++ ["..."]
|
||||
|
||||
open = color("#" <> name <> "<", :map, opts)
|
||||
sep = color(",", :map, opts)
|
||||
close = color(">", :map, opts)
|
||||
|
||||
fun = fn
|
||||
{key, value}, opts -> Inspect.List.keyword({key, value}, opts)
|
||||
:..., _opts -> "..."
|
||||
end
|
||||
|
||||
container_doc(open, map, close, opts, fun, separator: sep, break: :strict)
|
||||
container_doc(open, map, close, opts, &Inspect.List.keyword/2, separator: sep, break: :strict)
|
||||
end
|
||||
end
|
||||
|
||||
require Protocol
|
||||
|
||||
Protocol.derive(
|
||||
Inspect,
|
||||
Macro.Env,
|
||||
only: [
|
||||
:module,
|
||||
:file,
|
||||
:line,
|
||||
:function,
|
||||
:context,
|
||||
:aliases,
|
||||
:requires,
|
||||
:functions,
|
||||
:macros,
|
||||
:macro_aliases,
|
||||
:context_modules,
|
||||
:lexical_tracker
|
||||
]
|
||||
)
|
||||
|
||||
@@ -4,19 +4,16 @@ defmodule Inspect.Opts do
|
||||
|
||||
The following fields are available:
|
||||
|
||||
* `:base` - prints integers as `:binary`, `:octal`, `:decimal`, or `:hex`,
|
||||
defaults to `:decimal`. When inspecting binaries any `:base` other than
|
||||
`:decimal` implies `binaries: :as_binaries`.
|
||||
* `:structs` - when `false`, structs are not formatted by the inspect
|
||||
protocol, they are instead printed as maps, defaults to `true`.
|
||||
|
||||
* `:binaries` - when `:as_binaries` all binaries will be printed in bit
|
||||
syntax.
|
||||
* `:binaries` - when `:as_strings` all binaries will be printed as strings,
|
||||
non-printable bytes will be escaped.
|
||||
|
||||
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. See `String.printable?/1` to learn
|
||||
when a string is printable.
|
||||
is printable, otherwise in bit syntax.
|
||||
|
||||
* `:charlists` - when `:as_charlists` all lists will be printed as charlists,
|
||||
non-printable elements will be escaped.
|
||||
@@ -24,143 +21,70 @@ defmodule Inspect.Opts do
|
||||
When `:as_lists` all lists will be printed as lists.
|
||||
|
||||
When the default `:infer`, the list will be printed as a charlist if it
|
||||
is printable, otherwise as list. See `List.ascii_printable?/1` to learn
|
||||
when a charlist is printable.
|
||||
is printable, otherwise as list.
|
||||
|
||||
* `:custom_options` (since v1.9.0) - a keyword list storing custom user-defined
|
||||
options. Useful when implementing the `Inspect` protocol for nested structs
|
||||
to pass the custom options through.
|
||||
* `:limit` - limits the number of items that are printed for tuples,
|
||||
bitstrings, maps, lists and any other collection of items. It does not
|
||||
apply to strings nor charlists and defaults to 50. If you don't want to limit
|
||||
the number of items to a particular number, use `:infinity`.
|
||||
|
||||
* `:inspect_fun` (since v1.9.0) - a function to build algebra documents.
|
||||
Defaults to `Inspect.Opts.default_inspect_fun/0`.
|
||||
* `:printable_limit` - limits the number of bytes that are printed for strings
|
||||
and charlists. Defaults to 4096. If you don't want to limit the number of items
|
||||
to a particular number, use `:infinity`.
|
||||
|
||||
* `:limit` - limits the number of items that are inspected for tuples,
|
||||
bitstrings, maps, lists and any other collection of items, with the exception of
|
||||
printable strings and printable charlists which use the `:printable_limit` option.
|
||||
If you don't want to limit the number of items to a particular number,
|
||||
use `:infinity`. It accepts a positive integer or `:infinity`.
|
||||
Defaults to `50`.
|
||||
* `:pretty` - if set to `true` enables pretty printing, defaults to `false`.
|
||||
|
||||
* `:pretty` - if set to `true` enables pretty printing. Defaults to `false`.
|
||||
* `:width` - defaults to 80 characters, used when pretty is `true` or when
|
||||
printing to IO devices. Set to 0 to force each item to be printed on its
|
||||
own line. If you don't want to limit the number of items to a particular
|
||||
number, use `:infinity`.
|
||||
|
||||
* `:printable_limit` - limits the number of characters that are inspected
|
||||
on printable strings and printable charlists. You can use `String.printable?/1`
|
||||
and `List.ascii_printable?/1` to check if a given string or charlist is
|
||||
printable. If you don't want to limit the number of characters to a particular
|
||||
number, use `:infinity`. It accepts a positive integer or `:infinity`.
|
||||
Defaults to `4096`.
|
||||
* `:base` - prints integers as `:binary`, `:octal`, `:decimal`, or `:hex`,
|
||||
defaults to `:decimal`. When inspecting binaries any `:base` other than
|
||||
`:decimal` implies `binaries: :as_binaries`.
|
||||
|
||||
* `: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.
|
||||
implementations
|
||||
|
||||
* `:structs` - when `false`, structs are not formatted by the inspect
|
||||
protocol, they are instead printed as maps. Defaults to `true`.
|
||||
|
||||
* `:syntax_colors` - when set to a keyword list of colors the output is
|
||||
colorized. The keys are types and the values are the colors to use for
|
||||
* `:syntax_colors` - when set to a keyword list of colors the output will
|
||||
be colorized. The keys are types and the values are the colors to use for
|
||||
each type (for example, `[number: :red, atom: :blue]`). Types can include
|
||||
`:atom`, `:binary`, `:boolean`, `:list`, `:map`, `:number`, `:regex`,
|
||||
`:string`, and `:tuple`. Custom data types may provide their own options.
|
||||
`:number`, `:atom`, `regex`, `:tuple`, `:map`, `:list`, and `:reset`.
|
||||
Colors can be any `t:IO.ANSI.ansidata/0` as accepted by `IO.ANSI.format/1`.
|
||||
|
||||
* `:width` - number of characters per line used when pretty is `true` or when
|
||||
printing to IO devices. Set to `0` to force each item to be printed on its
|
||||
own line. If you don't want to limit the number of items to a particular
|
||||
number, use `:infinity`. Defaults to `80`.
|
||||
|
||||
"""
|
||||
|
||||
# TODO: Remove :char_lists key on v2.0
|
||||
defstruct base: :decimal,
|
||||
# TODO: Remove :char_lists key by 2.0
|
||||
defstruct structs: true,
|
||||
binaries: :infer,
|
||||
char_lists: :infer,
|
||||
charlists: :infer,
|
||||
custom_options: [],
|
||||
inspect_fun: &Inspect.inspect/2,
|
||||
char_lists: :infer,
|
||||
limit: 50,
|
||||
pretty: false,
|
||||
printable_limit: 4096,
|
||||
width: 80,
|
||||
base: :decimal,
|
||||
pretty: false,
|
||||
safe: true,
|
||||
structs: true,
|
||||
syntax_colors: [],
|
||||
width: 80
|
||||
syntax_colors: []
|
||||
|
||||
@type color_key :: atom
|
||||
|
||||
# TODO: Remove :char_lists key and :as_char_lists value on v2.0
|
||||
# TODO: Remove :char_lists key and :as_char_lists value by 2.0
|
||||
@type t :: %__MODULE__{
|
||||
base: :decimal | :binary | :hex | :octal,
|
||||
binaries: :infer | :as_binaries | :as_strings,
|
||||
char_lists: :infer | :as_lists | :as_char_lists,
|
||||
charlists: :infer | :as_lists | :as_charlists,
|
||||
custom_options: keyword,
|
||||
inspect_fun: (any, t -> Inspect.Algebra.t()),
|
||||
limit: non_neg_integer | :infinity,
|
||||
pretty: boolean,
|
||||
printable_limit: non_neg_integer | :infinity,
|
||||
safe: boolean,
|
||||
structs: boolean,
|
||||
syntax_colors: [{color_key, IO.ANSI.ansidata()}],
|
||||
width: non_neg_integer | :infinity
|
||||
binaries: :infer | :as_binaries | :as_strings,
|
||||
charlists: :infer | :as_lists | :as_charlists,
|
||||
char_lists: :infer | :as_lists | :as_char_lists,
|
||||
limit: pos_integer | :infinity,
|
||||
printable_limit: pos_integer | :infinity,
|
||||
width: pos_integer | :infinity,
|
||||
base: :decimal | :binary | :hex | :octal,
|
||||
pretty: boolean,
|
||||
safe: boolean,
|
||||
syntax_colors: [{color_key, IO.ANSI.ansidata()}]
|
||||
}
|
||||
|
||||
@doc """
|
||||
Builds an `Inspect.Opts` struct.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec new(keyword()) :: t
|
||||
def new(opts) do
|
||||
struct(%Inspect.Opts{inspect_fun: default_inspect_fun()}, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the default inspect function.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec default_inspect_fun() :: (term, t -> Inspect.Algebra.t())
|
||||
def default_inspect_fun do
|
||||
:persistent_term.get({__MODULE__, :inspect_fun}, &Inspect.inspect/2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sets the default inspect function.
|
||||
|
||||
Set this option with care as it will change how all values
|
||||
in the system are inspected. The main use of this functionality
|
||||
is to provide an entry point to filter inspected values,
|
||||
in order for entities to comply with rules and legislations
|
||||
on data security and data privacy.
|
||||
|
||||
It is **extremely discouraged** for libraries to set their own
|
||||
function as this must be controlled by applications. Libraries
|
||||
should instead define their own structs with custom inspect
|
||||
implementations. If a library must change the default inspect
|
||||
function, then it is best to define to ask users of your library
|
||||
to explicitly call `default_inspect_fun/1` with your function of
|
||||
choice.
|
||||
|
||||
The default is `Inspect.inspect/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
previous_fun = Inspect.Opts.default_inspect_fun()
|
||||
|
||||
Inspect.Opts.default_inspect_fun(fn
|
||||
%{address: _} = map, opts ->
|
||||
previous_fun.(%{map | address: "[REDACTED]"}, opts)
|
||||
|
||||
value, opts ->
|
||||
previous_fun.(value, opts)
|
||||
end)
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec default_inspect_fun((term, t -> Inspect.Algebra.t())) :: :ok
|
||||
def default_inspect_fun(fun) when is_function(fun, 2) do
|
||||
:persistent_term.put({__MODULE__, :inspect_fun}, fun)
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Inspect.Error do
|
||||
@@ -206,7 +130,7 @@ defmodule Inspect.Algebra do
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["a", " ", "b"]
|
||||
|
||||
Note that the break was represented as is, because we haven't reached
|
||||
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")
|
||||
@@ -251,17 +175,17 @@ defmodule Inspect.Algebra do
|
||||
|
||||
@type t ::
|
||||
binary
|
||||
| :doc_line
|
||||
| :doc_nil
|
||||
| doc_break
|
||||
| doc_collapse
|
||||
| doc_color
|
||||
| doc_cons
|
||||
| doc_fits
|
||||
| doc_force
|
||||
| doc_group
|
||||
| doc_nest
|
||||
| :doc_line
|
||||
| doc_string
|
||||
| doc_cons
|
||||
| doc_nest
|
||||
| doc_break
|
||||
| doc_group
|
||||
| doc_color
|
||||
| doc_force
|
||||
| doc_fits
|
||||
| doc_collapse
|
||||
|
||||
@typep doc_string :: {:doc_string, t, non_neg_integer}
|
||||
defmacrop doc_string(string, length) do
|
||||
@@ -309,24 +233,21 @@ defmodule Inspect.Algebra do
|
||||
end
|
||||
|
||||
@docs [
|
||||
:doc_break,
|
||||
:doc_collapse,
|
||||
:doc_color,
|
||||
:doc_string,
|
||||
:doc_cons,
|
||||
:doc_fits,
|
||||
:doc_force,
|
||||
:doc_group,
|
||||
:doc_nest,
|
||||
:doc_string
|
||||
:doc_break,
|
||||
:doc_group,
|
||||
:doc_color,
|
||||
:doc_force,
|
||||
:doc_fits,
|
||||
:doc_collapse
|
||||
]
|
||||
|
||||
defguard is_doc(doc)
|
||||
when is_binary(doc) or doc in [:doc_nil, :doc_line] or
|
||||
(is_tuple(doc) and elem(doc, 0) in @docs)
|
||||
|
||||
defguardp is_limit(limit) when limit == :infinity or (is_integer(limit) and limit >= 0)
|
||||
defguardp is_width(limit) when limit == :infinity or (is_integer(limit) and limit >= 0)
|
||||
|
||||
# Elixir + Inspect.Opts conveniences
|
||||
|
||||
@doc """
|
||||
@@ -336,10 +257,10 @@ defmodule Inspect.Algebra do
|
||||
@spec to_doc(any, Inspect.Opts.t()) :: t
|
||||
def to_doc(term, opts)
|
||||
|
||||
def to_doc(%_{} = struct, %Inspect.Opts{inspect_fun: fun} = opts) do
|
||||
def to_doc(%_{} = struct, %Inspect.Opts{} = opts) do
|
||||
if opts.structs do
|
||||
try do
|
||||
fun.(struct, opts)
|
||||
Inspect.inspect(struct, opts)
|
||||
rescue
|
||||
caught_exception ->
|
||||
# Because we try to raise a nice error message in case
|
||||
@@ -354,13 +275,7 @@ defmodule Inspect.Algebra do
|
||||
try do
|
||||
Process.put(:inspect_trap, true)
|
||||
|
||||
res =
|
||||
Inspect.Map.inspect(struct, %{
|
||||
opts
|
||||
| syntax_colors: [],
|
||||
inspect_fun: Inspect.Opts.default_inspect_fun()
|
||||
})
|
||||
|
||||
res = Inspect.Map.inspect(struct, %{opts | syntax_colors: []})
|
||||
res = IO.iodata_to_binary(format(res, :infinity))
|
||||
|
||||
message =
|
||||
@@ -370,10 +285,7 @@ defmodule Inspect.Algebra do
|
||||
exception = Inspect.Error.exception(message: message)
|
||||
|
||||
if opts.safe do
|
||||
Inspect.inspect(exception, %{
|
||||
opts
|
||||
| inspect_fun: Inspect.Opts.default_inspect_fun()
|
||||
})
|
||||
Inspect.inspect(exception, opts)
|
||||
else
|
||||
reraise(exception, __STACKTRACE__)
|
||||
end
|
||||
@@ -387,8 +299,8 @@ defmodule Inspect.Algebra do
|
||||
end
|
||||
end
|
||||
|
||||
def to_doc(arg, %Inspect.Opts{inspect_fun: fun} = opts) do
|
||||
fun.(arg, opts)
|
||||
def to_doc(arg, %Inspect.Opts{} = opts) do
|
||||
Inspect.inspect(arg, opts)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -467,14 +379,13 @@ defmodule Inspect.Algebra do
|
||||
{:lists.reverse(["..." | acc]), simple?}
|
||||
end
|
||||
|
||||
defp container_each([term | terms], limit, opts, fun, acc, simple?)
|
||||
when is_list(terms) and is_limit(limit) do
|
||||
defp container_each([term | terms], limit, opts, fun, acc, simple?) when is_list(terms) do
|
||||
limit = decrement(limit)
|
||||
doc = fun.(term, %{opts | limit: limit})
|
||||
container_each(terms, limit, opts, fun, [doc | acc], simple? and simple?(doc))
|
||||
end
|
||||
|
||||
defp container_each([left | right], limit, opts, fun, acc, simple?) when is_limit(limit) do
|
||||
defp container_each([left | right], limit, opts, fun, acc, simple?) do
|
||||
limit = decrement(limit)
|
||||
left = fun.(left, %{opts | limit: limit})
|
||||
right = fun.(right, %{opts | limit: limit})
|
||||
@@ -499,12 +410,14 @@ defmodule Inspect.Algebra do
|
||||
defp simple?(:doc_nil), do: true
|
||||
defp simple?(other), do: is_binary(other)
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
@doc false
|
||||
@deprecated "Use a combination of concat/2 and nest/2 instead"
|
||||
def surround(left, doc, right) when is_doc(left) and is_doc(doc) and is_doc(right) do
|
||||
concat(concat(left, nest(doc, 1)), right)
|
||||
end
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
@doc false
|
||||
@deprecated "Use Inspect.Algebra.container_doc/6 instead"
|
||||
def surround_many(
|
||||
@@ -601,7 +514,7 @@ defmodule Inspect.Algebra do
|
||||
Colors a document if the `color_key` has a color in the options.
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec color(t, Inspect.Opts.color_key(), Inspect.Opts.t()) :: t
|
||||
@spec color(t, Inspect.Opts.color_key(), Inspect.Opts.t()) :: doc_color
|
||||
def color(doc, color_key, %Inspect.Opts{syntax_colors: syntax_colors}) when is_doc(doc) do
|
||||
if precolor = Keyword.get(syntax_colors, color_key) do
|
||||
postcolor = Keyword.get(syntax_colors, :reset, :reset)
|
||||
@@ -666,7 +579,7 @@ defmodule Inspect.Algebra do
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["a", "\t", "b"]
|
||||
|
||||
Note that the break was represented with the given string, because we didn't
|
||||
Notice the break was represented with the given string, because we didn't
|
||||
reach a line limit. Once we do, it is replaced by a newline:
|
||||
|
||||
iex> break = Inspect.Algebra.break("\t")
|
||||
@@ -773,7 +686,7 @@ defmodule Inspect.Algebra do
|
||||
to the document fitting. On the other hand, they are more expensive
|
||||
since each break needs to be re-evaluated.
|
||||
|
||||
This function is used by `container_doc/6` and friends to the
|
||||
This function is used by `container_doc/4` and friends to the
|
||||
maximum number of entries on the same line.
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@@ -896,7 +809,7 @@ defmodule Inspect.Algebra do
|
||||
@doc ~S"""
|
||||
Inserts a mandatory linebreak between two documents.
|
||||
|
||||
See `line/0`.
|
||||
See `line/1`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -955,30 +868,24 @@ defmodule Inspect.Algebra do
|
||||
|
||||
"""
|
||||
@spec format(t, non_neg_integer | :infinity) :: iodata
|
||||
def format(doc, width) when is_doc(doc) and is_width(width) do
|
||||
def format(doc, width) when is_doc(doc) and (width == :infinity or width >= 0) do
|
||||
format(width, 0, [{0, :flat, doc}])
|
||||
end
|
||||
|
||||
# Type representing the document mode to be rendered:
|
||||
# Type representing the document mode to be rendered
|
||||
#
|
||||
# * flat - represents a document with breaks as flats (a break may fit, as it may break)
|
||||
# * break - represents a document with breaks as breaks (a break always fits, since it breaks)
|
||||
#
|
||||
# The following modes are exclusive to fitting:
|
||||
# The following modes are exclusive to fitting
|
||||
#
|
||||
# * flat_no_break - represents a document with breaks as flat not allowed to enter in break mode
|
||||
# * break_no_flat - represents a document with breaks as breaks not allowed to enter in flat mode
|
||||
#
|
||||
@typep mode :: :flat | :flat_no_break | :break | :break_no_flat
|
||||
@typep mode :: :flat | :flat_no_break | :break
|
||||
|
||||
@spec fits?(
|
||||
width :: non_neg_integer(),
|
||||
column :: non_neg_integer(),
|
||||
break? :: boolean(),
|
||||
entries
|
||||
) :: boolean()
|
||||
when entries:
|
||||
maybe_improper_list({integer(), mode(), t()}, {:tail, boolean(), entries} | [])
|
||||
@spec fits?(width :: integer(), column :: integer(), break? :: boolean(), entries) :: boolean()
|
||||
when entries: [{integer(), mode(), t()}] | {:tail, boolean(), entries}
|
||||
|
||||
# We need at least a break to consider the document does not fit since a
|
||||
# large document without breaks has no option but fitting its current line.
|
||||
@@ -1037,9 +944,7 @@ defmodule Inspect.Algebra do
|
||||
defp fits?(w, k, b?, [{i, m, doc_group(x, _)} | t]),
|
||||
do: fits?(w, k, b?, [{i, m, x} | {:tail, b?, t}])
|
||||
|
||||
@spec format(width :: non_neg_integer() | :infinity, column :: non_neg_integer(), [
|
||||
{integer, mode, t}
|
||||
]) :: [binary]
|
||||
@spec format(integer | :infinity, integer, [{integer, mode, t}]) :: [binary]
|
||||
defp format(_, _, []), do: []
|
||||
defp format(w, k, [{_, _, :doc_nil} | t]), do: format(w, k, t)
|
||||
defp format(w, _, [{i, _, :doc_line} | t]), do: [indent(i) | format(w, i, t)]
|
||||
|
||||
+48
-116
@@ -64,53 +64,10 @@ defmodule Integer do
|
||||
"""
|
||||
defguard is_even(integer) when is_integer(integer) and (integer &&& 1) == 0
|
||||
|
||||
@doc """
|
||||
Computes `base` raised to power of `exponent`.
|
||||
|
||||
Both `base` and `exponent` must be integers.
|
||||
The exponent must be zero or positive.
|
||||
|
||||
See `Float.pow/2` for exponentiation of negative
|
||||
exponents as well as floats.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.pow(2, 0)
|
||||
1
|
||||
iex> Integer.pow(2, 1)
|
||||
2
|
||||
iex> Integer.pow(2, 10)
|
||||
1024
|
||||
iex> Integer.pow(2, 11)
|
||||
2048
|
||||
iex> Integer.pow(2, 64)
|
||||
0x10000000000000000
|
||||
|
||||
iex> Integer.pow(3, 4)
|
||||
81
|
||||
iex> Integer.pow(4, 3)
|
||||
64
|
||||
|
||||
iex> Integer.pow(-2, 3)
|
||||
-8
|
||||
iex> Integer.pow(-2, 4)
|
||||
16
|
||||
|
||||
iex> Integer.pow(2, -2)
|
||||
** (ArithmeticError) bad argument in arithmetic expression
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec pow(integer, non_neg_integer) :: integer
|
||||
def pow(base, exponent) when is_integer(base) and is_integer(exponent) do
|
||||
if exponent < 0, do: :erlang.error(:badarith, [base, exponent])
|
||||
base ** exponent
|
||||
end
|
||||
|
||||
@doc """
|
||||
Computes the modulo remainder of an integer division.
|
||||
|
||||
This function performs a [floored division](`floor_div/2`), which means that
|
||||
`Integer.mod/2` uses floored division, which means that
|
||||
the result will always have the sign of the `divisor`.
|
||||
|
||||
Raises an `ArithmeticError` exception if one of the arguments is not an
|
||||
@@ -142,8 +99,8 @@ defmodule Integer do
|
||||
Raises an `ArithmeticError` exception if one of the arguments is not an
|
||||
integer, or when the `divisor` is `0`.
|
||||
|
||||
This function performs a *floored* integer division, which means that
|
||||
the result will always be rounded towards negative infinity.
|
||||
`Integer.floor_div/2` performs *floored* integer division. This means that
|
||||
the result is always rounded towards negative infinity.
|
||||
|
||||
If you want to perform truncated integer division (rounding towards zero),
|
||||
use `Kernel.div/2` instead.
|
||||
@@ -269,14 +226,14 @@ defmodule Integer do
|
||||
** (ArgumentError) invalid base 38
|
||||
|
||||
"""
|
||||
@spec parse(binary, 2..36) :: {integer, remainder_of_binary :: binary} | :error
|
||||
@spec parse(binary, 2..36) :: {integer, binary} | :error
|
||||
def parse(binary, base \\ 10)
|
||||
|
||||
def parse(_binary, base) when base not in 2..36 do
|
||||
raise ArgumentError, "invalid base #{inspect(base)}"
|
||||
end
|
||||
|
||||
def parse(binary, base) when is_binary(binary) do
|
||||
def parse(binary, base) do
|
||||
case count_digits(binary, base) do
|
||||
0 ->
|
||||
:error
|
||||
@@ -287,14 +244,14 @@ defmodule Integer do
|
||||
end
|
||||
end
|
||||
|
||||
defp count_digits(<<sign, rest::bits>>, base) when sign in '+-' do
|
||||
defp count_digits(<<sign, rest::binary>>, base) when sign in '+-' do
|
||||
case count_digits_nosign(rest, base, 1) do
|
||||
1 -> 0
|
||||
count -> count
|
||||
end
|
||||
end
|
||||
|
||||
defp count_digits(<<rest::bits>>, base) do
|
||||
defp count_digits(<<rest::binary>>, base) do
|
||||
count_digits_nosign(rest, base, 0)
|
||||
end
|
||||
|
||||
@@ -304,20 +261,17 @@ defmodule Integer do
|
||||
char <- chars do
|
||||
digit = char + diff
|
||||
|
||||
defp count_digits_nosign(<<unquote(char), rest::bits>>, base, count)
|
||||
defp count_digits_nosign(<<unquote(char), rest::binary>>, base, count)
|
||||
when base > unquote(digit) do
|
||||
count_digits_nosign(rest, base, count + 1)
|
||||
end
|
||||
end
|
||||
|
||||
defp count_digits_nosign(<<_::bits>>, _, count), do: count
|
||||
defp count_digits_nosign(<<_::binary>>, _, count), do: count
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36. If no `base` is given,
|
||||
it defaults to `10`.
|
||||
of `integer`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -335,6 +289,22 @@ defmodule Integer do
|
||||
iex> Integer.to_string(0123)
|
||||
"123"
|
||||
|
||||
"""
|
||||
@spec to_string(integer) :: String.t()
|
||||
def to_string(integer) do
|
||||
:erlang.integer_to_binary(integer)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_string(100, 16)
|
||||
"64"
|
||||
|
||||
@@ -346,16 +316,12 @@ defmodule Integer do
|
||||
|
||||
"""
|
||||
@spec to_string(integer, 2..36) :: String.t()
|
||||
def to_string(integer, base \\ 10) do
|
||||
def to_string(integer, base) do
|
||||
:erlang.integer_to_binary(integer, base)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation
|
||||
of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36. If no `base` is given,
|
||||
it defaults to `10`.
|
||||
Returns a charlist which corresponds to the text representation of the given `integer`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -373,6 +339,21 @@ defmodule Integer do
|
||||
iex> Integer.to_charlist(0123)
|
||||
'123'
|
||||
|
||||
"""
|
||||
@spec to_charlist(integer) :: charlist
|
||||
def to_charlist(integer) do
|
||||
:erlang.integer_to_list(integer)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_charlist(100, 16)
|
||||
'64'
|
||||
|
||||
@@ -384,7 +365,7 @@ defmodule Integer do
|
||||
|
||||
"""
|
||||
@spec to_charlist(integer, 2..36) :: charlist
|
||||
def to_charlist(integer, base \\ 10) do
|
||||
def to_charlist(integer, base) do
|
||||
:erlang.integer_to_list(integer, base)
|
||||
end
|
||||
|
||||
@@ -418,7 +399,8 @@ defmodule Integer do
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec gcd(integer, integer) :: non_neg_integer
|
||||
@spec gcd(0, 0) :: 0
|
||||
@spec gcd(integer, integer) :: pos_integer
|
||||
def gcd(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
|
||||
gcd_positive(abs(integer1), abs(integer2))
|
||||
end
|
||||
@@ -427,62 +409,12 @@ defmodule Integer do
|
||||
defp gcd_positive(integer1, 0), do: integer1
|
||||
defp gcd_positive(integer1, integer2), do: gcd_positive(integer2, rem(integer1, integer2))
|
||||
|
||||
@doc """
|
||||
Returns the extended greatest common divisor of the two given integers.
|
||||
|
||||
This function uses the extended Euclidean algorithm to return a three-element tuple with the `gcd`
|
||||
and the coefficients `m` and `n` of Bézout's identity such that:
|
||||
|
||||
gcd(a, b) = m*a + n*b
|
||||
|
||||
By convention, `extended_gcd(0, 0)` returns `{0, 0, 0}`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.extended_gcd(240, 46)
|
||||
{2, -9, 47}
|
||||
iex> Integer.extended_gcd(46, 240)
|
||||
{2, 47, -9}
|
||||
iex> Integer.extended_gcd(-46, 240)
|
||||
{2, -47, -9}
|
||||
iex> Integer.extended_gcd(-46, -240)
|
||||
{2, -47, 9}
|
||||
|
||||
iex> Integer.extended_gcd(14, 21)
|
||||
{7, -1, 1}
|
||||
|
||||
iex> Integer.extended_gcd(10, 0)
|
||||
{10, 1, 0}
|
||||
iex> Integer.extended_gcd(0, 10)
|
||||
{10, 0, 1}
|
||||
iex> Integer.extended_gcd(0, 0)
|
||||
{0, 0, 0}
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec extended_gcd(integer, integer) :: {non_neg_integer, integer, integer}
|
||||
def extended_gcd(0, 0), do: {0, 0, 0}
|
||||
def extended_gcd(0, b), do: {b, 0, 1}
|
||||
def extended_gcd(a, 0), do: {a, 1, 0}
|
||||
|
||||
def extended_gcd(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
|
||||
extended_gcd(integer2, integer1, 0, 1, 1, 0)
|
||||
end
|
||||
|
||||
defp extended_gcd(r1, r0, s1, s0, t1, t0) do
|
||||
div = div(r0, r1)
|
||||
|
||||
case r0 - div * r1 do
|
||||
0 when r1 > 0 -> {r1, s1, t1}
|
||||
0 when r1 < 0 -> {-r1, -s1, -t1}
|
||||
r2 -> extended_gcd(r2, r1, s0 - div * s1, s1, t0 - div * t1, t1)
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
@doc false
|
||||
@deprecated "Use Integer.to_charlist/1 instead"
|
||||
def to_char_list(integer), do: Integer.to_charlist(integer)
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
@doc false
|
||||
@deprecated "Use Integer.to_charlist/2 instead"
|
||||
def to_char_list(integer, base), do: Integer.to_charlist(integer, base)
|
||||
|
||||
+80
-251
@@ -1,5 +1,5 @@
|
||||
defmodule IO do
|
||||
@moduledoc ~S"""
|
||||
@moduledoc """
|
||||
Functions handling input/output (IO).
|
||||
|
||||
Many functions in this module expect an IO device as an argument.
|
||||
@@ -7,10 +7,13 @@ defmodule IO do
|
||||
For convenience, Elixir provides `:stdio` and `:stderr` as
|
||||
shortcuts to Erlang's `:standard_io` and `:standard_error`.
|
||||
|
||||
The majority of the functions expect chardata. In case another type is given,
|
||||
functions will convert those types to string via the `String.Chars` protocol
|
||||
(as shown in typespecs). For more information on chardata, see the
|
||||
"IO data" section below.
|
||||
The majority of the functions expect chardata, 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
|
||||
|
||||
@@ -29,107 +32,24 @@ defmodule IO do
|
||||
was last accessed. The position of files can be changed using the
|
||||
`:file.position/2` function.
|
||||
|
||||
## IO data
|
||||
|
||||
IO data is a data type that can be used as a more efficient alternative to binaries
|
||||
in certain situations.
|
||||
|
||||
A term of type **IO data** is a binary or a list containing bytes (integers within the `0..255` range)
|
||||
or nested IO data. The type is recursive. Let's see an example of one of
|
||||
the possible IO data representing the binary `"hello"`:
|
||||
|
||||
[?h, "el", ["l", [?o]]]
|
||||
|
||||
The built-in `t:iodata/0` type is defined in terms of `t:iolist/0`. An IO list is
|
||||
the same as IO data but it doesn't allow for a binary at the top level (but binaries
|
||||
are still allowed in the list itself).
|
||||
|
||||
### Use cases for IO data
|
||||
|
||||
IO data exists because often you need to do many append operations
|
||||
on smaller chunks of binaries in order to create a bigger binary. However, in
|
||||
Erlang and Elixir concatenating binaries will copy the concatenated binaries
|
||||
into a new binary.
|
||||
|
||||
def email(username, domain) do
|
||||
username <> "@" <> domain
|
||||
end
|
||||
|
||||
In this function, creating the email address will copy the `username` and `domain`
|
||||
binaries. Now imagine you want to use the resulting email inside another binary:
|
||||
|
||||
def welcome_message(name, username, domain) do
|
||||
"Welcome #{name}, your email is: #{email(username, domain)}"
|
||||
end
|
||||
|
||||
IO.puts(welcome_message("Meg", "meg", "example.com"))
|
||||
#=> "Welcome Meg, your email is: meg@example.com"
|
||||
|
||||
Every time you concatenate binaries or use interpolation (`#{}`) you are making
|
||||
copies of those binaries. However, in many cases you don't need the complete
|
||||
binary while you create it, but only at the end to print it out or send it
|
||||
somewhere. In such cases, you can construct the binary by creating IO data:
|
||||
|
||||
def email(username, domain) do
|
||||
[username, ?@, domain]
|
||||
end
|
||||
|
||||
def welcome_message(name, username, domain) do
|
||||
["Welcome ", name, ", your email is: ", email(username, domain)]
|
||||
end
|
||||
|
||||
IO.puts(welcome_message("Meg", "meg", "example.com"))
|
||||
#=> "Welcome Meg, your email is: meg@example.com"
|
||||
|
||||
Building IO data is cheaper than concatenating binaries. Concatenating multiple
|
||||
pieces of IO data just means putting them together inside a list since IO data
|
||||
can be arbitrarily nested, and that's a cheap and efficient operation. Most of
|
||||
the IO-based APIs, such as `:gen_tcp` and `IO`, receive IO data and write it
|
||||
to the socket directly without converting it to binary.
|
||||
|
||||
One drawback of IO data is that you can't do things like pattern match on the
|
||||
first part of a piece of IO data like you can with a binary, because you usually
|
||||
don't know the shape of the IO data. In those cases, you may need to convert it
|
||||
to a binary by calling `iodata_to_binary/1`, which is reasonably efficient
|
||||
since it's implemented natively in C. Other functionality, like computing the
|
||||
length of IO data, can be computed directly on the iodata by calling `iodata_length/1`.
|
||||
|
||||
### Chardata
|
||||
|
||||
Erlang and Elixir also have the idea of `t:chardata/0`. Chardata is very
|
||||
similar to IO data: the only difference is that integers in IO data represent
|
||||
bytes while integers in chardata represent Unicode code points. Bytes
|
||||
(`t:byte/0`) are integers within the `0..255` range, while Unicode code points
|
||||
(`t:char/0`) are integers within the `0..0x10FFFF` range. The `IO` module provides
|
||||
the `chardata_to_string/1` function for chardata as the "counter-part" of the
|
||||
`iodata_to_binary/1` function for IO data.
|
||||
|
||||
If you try to use `iodata_to_binary/1` on chardata, it will result in an
|
||||
argument error. For example, let's try to put a code point that is not
|
||||
representable with one byte, like `?π`, inside IO data:
|
||||
|
||||
IO.iodata_to_binary(["The symbol for pi is: ", ?π])
|
||||
#=> ** (ArgumentError) argument error
|
||||
|
||||
If we use chardata instead, it will work as expected:
|
||||
|
||||
iex> IO.chardata_to_string(["The symbol for pi is: ", ?π])
|
||||
"The symbol for pi is: π"
|
||||
|
||||
"""
|
||||
|
||||
@type device :: atom | pid
|
||||
@type nodata :: {:error, term} | :eof
|
||||
@type chardata :: String.t() | maybe_improper_list(char | chardata, String.t() | [])
|
||||
|
||||
defguardp is_device(term) when is_atom(term) or is_pid(term)
|
||||
defguardp is_iodata(data) when is_list(data) or is_binary(data)
|
||||
defmacrop is_iodata(data) do
|
||||
quote do
|
||||
is_list(unquote(data)) or is_binary(unquote(data))
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads from the IO `device`.
|
||||
|
||||
The `device` is iterated by the given number of characters, line by line if
|
||||
`:line` is given, or until `:eof`.
|
||||
The `device` is iterated by the given number of characters or line by line if
|
||||
`:line` is given.
|
||||
Alternatively, if `:all` is given, then whole `device` is returned.
|
||||
|
||||
It returns:
|
||||
|
||||
@@ -141,24 +61,14 @@ defmodule IO do
|
||||
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.
|
||||
"""
|
||||
@spec read(device, :eof | :line | non_neg_integer) :: chardata | nodata
|
||||
@spec read(device, :all | :line | non_neg_integer) :: chardata | nodata
|
||||
def read(device \\ :stdio, line_or_chars)
|
||||
|
||||
# TODO: Deprecate me on v1.17
|
||||
def read(device, :all) do
|
||||
with :eof <- read(device, :eof) do
|
||||
with [_ | _] = opts <- :io.getopts(device),
|
||||
false <- Keyword.get(opts, :binary, true) do
|
||||
''
|
||||
else
|
||||
_ -> ""
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def read(device, :eof) do
|
||||
getn(device, '', :eof)
|
||||
do_read_all(map_dev(device), "")
|
||||
end
|
||||
|
||||
def read(device, :line) do
|
||||
@@ -169,11 +79,20 @@ defmodule IO 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 from the IO `device`. The operation is Unicode unsafe.
|
||||
|
||||
The `device` is iterated by the given number of bytes, line by line if
|
||||
`:line` is given, or until `:eof`.
|
||||
The `device` is iterated by the given number of bytes or line by line if
|
||||
`:line` is given.
|
||||
Alternatively, if `:all` is given, then whole `device` is returned.
|
||||
|
||||
It returns:
|
||||
|
||||
@@ -185,19 +104,17 @@ defmodule IO do
|
||||
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, :eof | :line | non_neg_integer) :: iodata | nodata
|
||||
@spec binread(device, :all | :line | non_neg_integer) :: iodata | nodata
|
||||
def binread(device \\ :stdio, line_or_chars)
|
||||
|
||||
# TODO: Deprecate me on v1.17
|
||||
def binread(device, :all) do
|
||||
with :eof <- binread(device, :eof), do: ""
|
||||
end
|
||||
|
||||
def binread(device, :eof) do
|
||||
binread_eof(map_dev(device), "")
|
||||
do_binread_all(map_dev(device), "")
|
||||
end
|
||||
|
||||
def binread(device, :line) do
|
||||
@@ -215,16 +132,16 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@read_all_size 4096
|
||||
defp binread_eof(mapped_dev, acc) do
|
||||
defp do_binread_all(mapped_dev, acc) do
|
||||
case :file.read(mapped_dev, @read_all_size) do
|
||||
{:ok, data} -> binread_eof(mapped_dev, acc <> data)
|
||||
:eof -> if acc == "", do: :eof, else: acc
|
||||
{:ok, data} -> do_binread_all(mapped_dev, acc <> data)
|
||||
:eof -> acc
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes `chardata` to the given `device`.
|
||||
Writes `item` to the given `device`.
|
||||
|
||||
By default, the `device` is the standard output.
|
||||
|
||||
@@ -238,30 +155,23 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec write(device, chardata | String.Chars.t()) :: :ok
|
||||
def write(device \\ :stdio, chardata) do
|
||||
:io.put_chars(map_dev(device), to_chardata(chardata))
|
||||
def write(device \\ :stdio, item) do
|
||||
:io.put_chars(map_dev(device), to_chardata(item))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes `iodata` to the given `device`.
|
||||
Writes `item` as a binary to the given `device`.
|
||||
No Unicode conversion happens.
|
||||
The operation is Unicode unsafe.
|
||||
|
||||
This operation is meant to be used with "raw" devices
|
||||
that are started without an encoding. The given `iodata`
|
||||
is written as is to the device, without conversion. For
|
||||
more information on IO data, see the "IO data" section in
|
||||
the module documentation.
|
||||
Check `write/2` for more information.
|
||||
|
||||
Use `write/2` for devices with encoding.
|
||||
|
||||
Important: do **not** use this function on IO devices in
|
||||
Unicode mode as it will write the wrong data. In particular,
|
||||
the standard IO device is set to Unicode by default, so writing
|
||||
to stdio with this function will likely result in the wrong data
|
||||
being sent down the wire.
|
||||
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 \\ :stdio, iodata) when is_iodata(iodata) do
|
||||
:file.write(map_dev(device), iodata)
|
||||
def binwrite(device \\ :stdio, item) when is_iodata(item) do
|
||||
:file.write(map_dev(device), item)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -281,7 +191,7 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec puts(device, chardata | String.Chars.t()) :: :ok
|
||||
def puts(device \\ :stdio, item) when is_device(device) do
|
||||
def puts(device \\ :stdio, item) do
|
||||
:io.put_chars(map_dev(device), [to_chardata(item), ?\n])
|
||||
end
|
||||
|
||||
@@ -304,34 +214,15 @@ defmodule IO do
|
||||
"""
|
||||
@spec warn(chardata | String.Chars.t(), Exception.stacktrace()) :: :ok
|
||||
def warn(message, []) do
|
||||
message = [to_chardata(message), ?\n]
|
||||
:elixir_errors.log_and_print_warning(0, nil, message, message)
|
||||
:elixir_errors.bare_warn(nil, nil, [to_chardata(message), ?\n])
|
||||
end
|
||||
|
||||
def warn(message, [{_, _, _, opts} | _] = stacktrace) do
|
||||
message = to_chardata(message)
|
||||
formatted_trace = Enum.map_join(stacktrace, "\n ", &Exception.format_stacktrace_entry(&1))
|
||||
message = [to_chardata(message), ?\n, " ", formatted_trace, ?\n]
|
||||
line = opts[:line]
|
||||
file = opts[:file]
|
||||
|
||||
:elixir_errors.log_and_print_warning(
|
||||
line || 0,
|
||||
file && List.to_string(file),
|
||||
message,
|
||||
[message, ?\n, " ", formatted_trace, ?\n]
|
||||
)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def warn_once(key, message, stacktrace_drop_levels) do
|
||||
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
|
||||
stacktrace = Enum.drop(stacktrace, stacktrace_drop_levels)
|
||||
|
||||
if :elixir_config.warn(key, stacktrace) do
|
||||
warn(message, stacktrace)
|
||||
else
|
||||
:ok
|
||||
end
|
||||
:elixir_errors.bare_warn(line, file && List.to_string(file), message)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -339,11 +230,6 @@ defmodule IO do
|
||||
|
||||
It returns `:ok` if it succeeds.
|
||||
|
||||
Do not call this function at the tail of another function. Due to tail
|
||||
call optimization, a stacktrace entry would not be added and the
|
||||
stacktrace would be incorrectly trimmed. Therefore make sure at least
|
||||
one expression (or an atom such as `:ok`) follows the `IO.warn/1` call.
|
||||
|
||||
## Examples
|
||||
|
||||
IO.warn("variable bar is unused")
|
||||
@@ -416,9 +302,9 @@ defmodule IO do
|
||||
See `inspect/2` for a full list of options.
|
||||
"""
|
||||
@spec inspect(device, item, keyword) :: item when item: var
|
||||
def inspect(device, item, opts) when is_device(device) and is_list(opts) do
|
||||
def inspect(device, item, opts) when is_list(opts) do
|
||||
label = if label = opts[:label], do: [to_chardata(label), ": "], else: []
|
||||
opts = Inspect.Opts.new(opts)
|
||||
opts = struct(Inspect.Opts, opts)
|
||||
doc = Inspect.Algebra.group(Inspect.Algebra.to_doc(item, opts))
|
||||
chardata = Inspect.Algebra.format(doc, opts.width)
|
||||
puts(device, [label, chardata])
|
||||
@@ -429,21 +315,15 @@ defmodule IO do
|
||||
Gets a number of bytes from IO device `:stdio`.
|
||||
|
||||
If `:stdio` is a Unicode device, `count` implies
|
||||
the number of Unicode code points to be retrieved.
|
||||
the number of Unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
|
||||
See `IO.getn/3` for a description of return values.
|
||||
"""
|
||||
@spec getn(
|
||||
device | chardata | String.Chars.t(),
|
||||
pos_integer | :eof | chardata | String.Chars.t()
|
||||
) ::
|
||||
chardata | nodata
|
||||
def getn(prompt, count \\ 1)
|
||||
|
||||
def getn(prompt, :eof) do
|
||||
getn(:stdio, prompt, :eof)
|
||||
end
|
||||
"""
|
||||
@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) and count > 0 do
|
||||
getn(:stdio, prompt, count)
|
||||
@@ -457,7 +337,7 @@ defmodule IO do
|
||||
Gets a number of bytes from the IO `device`.
|
||||
|
||||
If the IO `device` is a Unicode device, `count` implies
|
||||
the number of Unicode code points to be retrieved.
|
||||
the number of Unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
|
||||
It returns:
|
||||
@@ -471,27 +351,11 @@ defmodule IO do
|
||||
NFS volume
|
||||
|
||||
"""
|
||||
@spec getn(device, chardata | String.Chars.t(), pos_integer | :eof) :: chardata | nodata
|
||||
def getn(device, prompt, :eof) do
|
||||
getn_eof(map_dev(device), to_chardata(prompt), [])
|
||||
end
|
||||
|
||||
@spec getn(device, chardata | String.Chars.t(), pos_integer) :: chardata | nodata
|
||||
def getn(device, prompt, count) when is_integer(count) and count > 0 do
|
||||
:io.get_chars(map_dev(device), to_chardata(prompt), count)
|
||||
end
|
||||
|
||||
defp getn_eof(device, prompt, acc) do
|
||||
case :io.get_line(device, prompt) do
|
||||
line when is_binary(line) or is_list(line) -> getn_eof(device, '', [line | acc])
|
||||
:eof -> wrap_eof(:lists.reverse(acc))
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
defp wrap_eof([h | _] = acc) when is_binary(h), do: IO.iodata_to_binary(acc)
|
||||
defp wrap_eof([h | _] = acc) when is_list(h), do: :lists.flatten(acc)
|
||||
defp wrap_eof([]), do: :eof
|
||||
|
||||
@doc ~S"""
|
||||
Reads a line from the IO `device`.
|
||||
|
||||
@@ -518,17 +382,6 @@ defmodule IO do
|
||||
:io.get_line(map_dev(device), to_chardata(prompt))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a line-based `IO.Stream` on `:stdio`.
|
||||
|
||||
This is equivalent to:
|
||||
|
||||
IO.stream(:stdio, :line)
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
def stream, do: stream(:stdio, :line)
|
||||
|
||||
@doc """
|
||||
Converts the IO `device` into an `IO.Stream`.
|
||||
|
||||
@@ -545,9 +398,6 @@ defmodule IO do
|
||||
Note that an IO stream has side effects and every time
|
||||
you go over the stream you may get different results.
|
||||
|
||||
`stream/1` has been introduced in Elixir v1.12.0,
|
||||
while `stream/2` has been available since v1.0.0.
|
||||
|
||||
## Examples
|
||||
|
||||
Here is an example on how we mimic an echo server
|
||||
@@ -557,23 +407,12 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec stream(device, :line | pos_integer) :: Enumerable.t()
|
||||
def stream(device \\ :stdio, line_or_codepoints)
|
||||
def stream(device, line_or_codepoints)
|
||||
when line_or_codepoints == :line
|
||||
when is_integer(line_or_codepoints) and line_or_codepoints > 0 do
|
||||
IO.Stream.__build__(map_dev(device), false, line_or_codepoints)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a raw, line-based `IO.Stream` on `:stdio`. The operation is Unicode unsafe.
|
||||
|
||||
This is equivalent to:
|
||||
|
||||
IO.binstream(:stdio, :line)
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
def binstream, do: binstream(:stdio, :line)
|
||||
|
||||
@doc """
|
||||
Converts the IO `device` into an `IO.Stream`. The operation is Unicode unsafe.
|
||||
|
||||
@@ -582,7 +421,8 @@ defmodule IO do
|
||||
and write.
|
||||
|
||||
The `device` is iterated by the given number of bytes or line by line if
|
||||
`:line` is given. This reads from the IO device as a raw binary.
|
||||
`:line` is given.
|
||||
This reads from 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.
|
||||
@@ -590,21 +430,17 @@ defmodule IO do
|
||||
Finally, do not use this function on IO devices in Unicode
|
||||
mode as it will return the wrong result.
|
||||
|
||||
`binstream/1` has been introduced in Elixir v1.12.0,
|
||||
while `binstream/2` has been available since v1.0.0.
|
||||
"""
|
||||
@spec binstream(device, :line | pos_integer) :: Enumerable.t()
|
||||
def binstream(device \\ :stdio, line_or_bytes)
|
||||
def binstream(device, line_or_bytes)
|
||||
when line_or_bytes == :line
|
||||
when is_integer(line_or_bytes) and line_or_bytes > 0 do
|
||||
IO.Stream.__build__(map_dev(device), true, line_or_bytes)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts chardata into a string.
|
||||
|
||||
For more information about chardata, see the ["Chardata"](#module-chardata)
|
||||
section in the module documentation.
|
||||
Converts chardata (a list of integers representing codepoints,
|
||||
lists and strings) into a string.
|
||||
|
||||
In case the conversion fails, it raises an `UnicodeConversionError`.
|
||||
If a string is given, it returns the string itself.
|
||||
@@ -622,8 +458,6 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec chardata_to_string(chardata) :: String.t()
|
||||
def chardata_to_string(chardata)
|
||||
|
||||
def chardata_to_string(string) when is_binary(string) do
|
||||
string
|
||||
end
|
||||
@@ -633,16 +467,14 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts IO data into a binary
|
||||
|
||||
Converts iodata (a list of integers representing bytes, lists
|
||||
and binaries) into a binary.
|
||||
The operation is Unicode unsafe.
|
||||
|
||||
Note that this function treats integers in the given IO data as
|
||||
raw bytes and does not perform any kind of encoding conversion.
|
||||
If you want to convert from a charlist to a UTF-8-encoded string,
|
||||
use `chardata_to_string/1` instead. For more information about
|
||||
IO data and chardata, see the ["IO data"](#module-io-data) section in the
|
||||
module documentation.
|
||||
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 charlist to a string (UTF-8 encoded), please
|
||||
use `chardata_to_string/1` instead.
|
||||
|
||||
If this function receives a binary, the same binary is returned.
|
||||
|
||||
@@ -662,15 +494,12 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec iodata_to_binary(iodata) :: binary
|
||||
def iodata_to_binary(iodata) do
|
||||
:erlang.iolist_to_binary(iodata)
|
||||
def iodata_to_binary(item) do
|
||||
:erlang.iolist_to_binary(item)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the size of an IO data.
|
||||
|
||||
For more information about IO data, see the ["IO data"](#module-io-data)
|
||||
section in the module documentation.
|
||||
Returns the size of an iodata.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -681,8 +510,8 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec iodata_length(iodata) :: non_neg_integer
|
||||
def iodata_length(iodata) do
|
||||
:erlang.iolist_size(iodata)
|
||||
def iodata_length(item) do
|
||||
:erlang.iolist_size(item)
|
||||
end
|
||||
|
||||
@doc false
|
||||
|
||||
@@ -21,31 +21,6 @@ defmodule IO.ANSI do
|
||||
[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.
|
||||
|
||||
ANSI escapes are typically enabled on all Unix terminals. They are also
|
||||
available on Windows consoles from Windows 10, although it must be
|
||||
explicitly enabled for the current user in the registry by running the
|
||||
following command:
|
||||
|
||||
reg add HKCU\\Console /v VirtualTerminalLevel /t REG_DWORD /d 1
|
||||
|
||||
After running the command above, you must restart your current console.
|
||||
|
||||
## Examples
|
||||
|
||||
Because the ANSI escape sequences are embedded in text, the normal usage of
|
||||
these functions is to concatenate their output with text.
|
||||
|
||||
formatted_text = IO.ANSI.blue_background() <> "Example" <> IO.ANSI.reset()
|
||||
IO.puts(formatted_text)
|
||||
|
||||
A higher level and more convenient API is also available via `IO.ANSI.format/1`,
|
||||
where you use atoms to represent each ANSI escape sequence and by default
|
||||
checks if ANSI is enabled:
|
||||
|
||||
IO.puts(IO.ANSI.format([:blue_background, "Example"]))
|
||||
|
||||
In case ANSI is disabled, the ANSI escape sequences are simply discarded.
|
||||
"""
|
||||
|
||||
import IO.ANSI.Sequence
|
||||
@@ -242,7 +217,7 @@ defmodule IO.ANSI do
|
||||
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 be emitted.
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
|
||||
By default checks if ANSI is enabled using the `enabled?/0` function.
|
||||
|
||||
## Examples
|
||||
@@ -262,7 +237,7 @@ defmodule IO.ANSI do
|
||||
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 be emitted.
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
|
||||
By default checks if ANSI is enabled using the `enabled?/0` function.
|
||||
|
||||
## Examples
|
||||
|
||||
+82
-376
@@ -1,8 +1,6 @@
|
||||
defmodule IO.ANSI.Docs do
|
||||
@moduledoc false
|
||||
|
||||
@bullet_text_unicode "• "
|
||||
@bullet_text_ascii "* "
|
||||
@bullets [?*, ?-, ?+]
|
||||
@spaces [" ", "\n", "\t"]
|
||||
|
||||
@@ -16,7 +14,6 @@ defmodule IO.ANSI.Docs do
|
||||
* `:doc_code` - code blocks (cyan)
|
||||
* `:doc_headings` - h1, h2, h3, h4, h5, h6 headings (yellow)
|
||||
* `:doc_metadata` - documentation metadata keys (yellow)
|
||||
* `:doc_quote` - leading quote character `> ` (light black)
|
||||
* `:doc_inline_code` - inline code (cyan)
|
||||
* `:doc_table_heading` - the style for table headings
|
||||
* `:doc_title` - top level heading (reverse, yellow)
|
||||
@@ -34,7 +31,6 @@ defmodule IO.ANSI.Docs do
|
||||
doc_code: [:cyan],
|
||||
doc_headings: [:yellow],
|
||||
doc_metadata: [:yellow],
|
||||
doc_quote: [:light_black],
|
||||
doc_inline_code: [:cyan],
|
||||
doc_table_heading: [:reverse],
|
||||
doc_title: [:reverse, :yellow],
|
||||
@@ -48,20 +44,15 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
See `default_options/0` for docs on the supported options.
|
||||
"""
|
||||
@spec print_headings([String.t()], keyword) :: :ok
|
||||
def print_headings(headings, options \\ []) do
|
||||
@spec print_heading(String.t(), keyword) :: :ok
|
||||
def print_heading(heading, options \\ []) do
|
||||
IO.puts(IO.ANSI.reset())
|
||||
options = Keyword.merge(default_options(), options)
|
||||
newline_after_block(options)
|
||||
width = options[:width]
|
||||
|
||||
for heading <- headings do
|
||||
padding = div(width + String.length(heading), 2)
|
||||
heading = String.pad_leading(heading, padding)
|
||||
heading = if options[:enabled], do: String.pad_trailing(heading, width), else: heading
|
||||
write(:doc_title, heading, options)
|
||||
end
|
||||
|
||||
newline_after_block(options)
|
||||
padding = div(width + String.length(heading), 2)
|
||||
heading = heading |> String.pad_leading(padding) |> String.pad_trailing(width)
|
||||
write(:doc_title, heading, options)
|
||||
newline_after_block()
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -82,14 +73,14 @@ defmodule IO.ANSI.Docs do
|
||||
{key, value}, _printed when is_binary(value) and key in @metadata_filter ->
|
||||
label = metadata_label(key, options)
|
||||
indent = String.duplicate(" ", length_without_escape(label, 0) + 1)
|
||||
write_with_wrap([label | String.split(value, @spaces)], options[:width], indent, true, "")
|
||||
write_with_wrap([label | String.split(value, @spaces)], options[:width], indent, true)
|
||||
|
||||
{key, value}, _printed when is_boolean(value) and key in @metadata_filter ->
|
||||
IO.puts([metadata_label(key, options), ?\s, to_string(value)])
|
||||
IO.puts([metadata_label(key, options), ' ', to_string(value)])
|
||||
|
||||
{:delegate_to, {m, f, a}}, _printed ->
|
||||
label = metadata_label(:delegate_to, options)
|
||||
IO.puts([label, ?\s, Exception.format_mfa(m, f, a)])
|
||||
IO.puts([label, ' ', Exception.format_mfa(m, f, a)])
|
||||
|
||||
_metadata, printed ->
|
||||
printed
|
||||
@@ -97,214 +88,21 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp metadata_label(key, options) do
|
||||
"#{color(:doc_metadata, options)}#{key}:#{maybe_reset(options)}"
|
||||
if options[:enabled] do
|
||||
"#{color(:doc_metadata, options)}#{key}:#{IO.ANSI.reset()}"
|
||||
else
|
||||
"#{key}:"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prints the documentation body `doc` according to `format`.
|
||||
Prints the documentation body.
|
||||
|
||||
It takes a set of `options` defined in `default_options/0`.
|
||||
In addition to the printing string, takes a set of `options`
|
||||
defined in `default_options/0`.
|
||||
"""
|
||||
@spec print(term(), String.t(), keyword) :: :ok
|
||||
def print(doc, format, options \\ [])
|
||||
|
||||
def print(doc, "text/markdown", options) when is_binary(doc) and is_list(options) do
|
||||
print_markdown(doc, options)
|
||||
end
|
||||
|
||||
def print(doc, "application/erlang+html", options) when is_list(options) do
|
||||
print_erlang_html(doc, options)
|
||||
end
|
||||
|
||||
def print(_doc, format, options) when is_binary(format) and is_list(options) do
|
||||
IO.puts("\nUnknown documentation format #{inspect(format)}\n")
|
||||
end
|
||||
|
||||
## Erlang+html
|
||||
|
||||
def print_erlang_html(doc, options) do
|
||||
options = Keyword.merge(default_options(), options)
|
||||
IO.write(traverse_erlang_html(doc, "", options))
|
||||
end
|
||||
|
||||
defp traverse_erlang_html(text, _indent, _options) when is_binary(text) do
|
||||
text
|
||||
end
|
||||
|
||||
defp traverse_erlang_html(nodes, indent, options) when is_list(nodes) do
|
||||
for node <- nodes do
|
||||
traverse_erlang_html(node, indent, options)
|
||||
end
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:div, [class: class] ++ _, entries}, indent, options) do
|
||||
prefix = indent <> quote_prefix(options)
|
||||
|
||||
content =
|
||||
entries
|
||||
|> traverse_erlang_html(indent, options)
|
||||
|> IO.iodata_to_binary()
|
||||
|> String.trim_trailing()
|
||||
|
||||
[
|
||||
prefix,
|
||||
class |> to_string() |> String.upcase(),
|
||||
"\n#{prefix}\n#{prefix}" | String.replace(content, "\n", "\n#{prefix}")
|
||||
]
|
||||
|> newline_cons()
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:p, _, entries}, indent, options) do
|
||||
[indent | handle_erlang_html_text(entries, indent, options)]
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:h1, _, entries}, indent, options) do
|
||||
entries |> traverse_erlang_html(indent, options) |> heading(1, options) |> newline_cons()
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:h2, _, entries}, indent, options) do
|
||||
entries |> traverse_erlang_html(indent, options) |> heading(2, options) |> newline_cons()
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:h3, _, entries}, indent, options) do
|
||||
entries |> traverse_erlang_html(indent, options) |> heading(3, options) |> newline_cons()
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:h4, _, entries}, indent, options) do
|
||||
entries |> traverse_erlang_html(indent, options) |> heading(4, options) |> newline_cons()
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:h5, _, entries}, indent, options) do
|
||||
entries |> traverse_erlang_html(indent, options) |> heading(5, options) |> newline_cons()
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:h6, _, entries}, indent, options) do
|
||||
entries |> traverse_erlang_html(indent, options) |> heading(6, options) |> newline_cons()
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:br, _, []}, _indent, _options) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:i, _, entries}, indent, options) do
|
||||
inline_text("_", traverse_erlang_html(entries, indent, options), options)
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:em, _, entries}, indent, options) do
|
||||
inline_text("*", traverse_erlang_html(entries, indent, options), options)
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({tag, _, entries}, indent, options) when tag in [:strong, :b] do
|
||||
inline_text("**", traverse_erlang_html(entries, indent, options), options)
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:code, _, entries}, indent, options) do
|
||||
inline_text("`", traverse_erlang_html(entries, indent, options), options)
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:pre, _, [{:code, _, entries}]}, indent, options) do
|
||||
string =
|
||||
entries
|
||||
|> traverse_erlang_html(indent, options)
|
||||
|> IO.iodata_to_binary()
|
||||
|
||||
["#{indent} ", String.replace(string, "\n", "\n#{indent} ")] |> newline_cons()
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:a, attributes, entries}, indent, options) do
|
||||
if href = attributes[:href] do
|
||||
[traverse_erlang_html(entries, indent, options), ?\s, ?(, href, ?)]
|
||||
else
|
||||
traverse_erlang_html(entries, indent, options)
|
||||
end
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:dl, _, entries}, indent, options) do
|
||||
traverse_erlang_html(entries, indent, options)
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:dt, _, entries}, indent, options) do
|
||||
[
|
||||
"#{indent} ",
|
||||
bullet_text(options) | handle_erlang_html_text(entries, indent <> " ", options)
|
||||
]
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:dd, _, entries}, indent, options) do
|
||||
["#{indent} " | handle_erlang_html_text(entries, indent <> " ", options)]
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:ul, attributes, entries}, indent, options) do
|
||||
if attributes[:class] == "types" do
|
||||
types =
|
||||
for {:li, _, lines} <- entries,
|
||||
line <- lines,
|
||||
do: ["#{indent} ", traverse_erlang_html(line, indent <> " ", options), ?\n]
|
||||
|
||||
if types != [] do
|
||||
["#{indent}Typespecs:\n\n", types, ?\n]
|
||||
else
|
||||
[]
|
||||
end
|
||||
else
|
||||
for {:li, _, lines} <- entries do
|
||||
[
|
||||
"#{indent} ",
|
||||
bullet_text(options) | handle_erlang_html_text(lines, indent <> " ", options)
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:ol, _, entries}, indent, options) do
|
||||
for {{:li, _, lines}, i} <- Enum.with_index(entries, 1) do
|
||||
[
|
||||
"#{indent} ",
|
||||
Integer.to_string(i),
|
||||
". " | handle_erlang_html_text(lines, indent <> " ", options)
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({tag, _, entries}, indent, options) do
|
||||
[
|
||||
indent <> "<#{tag}>\n",
|
||||
traverse_erlang_html(entries, indent <> " ", options)
|
||||
|> IO.iodata_to_binary()
|
||||
|> String.trim_trailing(),
|
||||
"\n" <> indent <> "</#{tag}>"
|
||||
]
|
||||
|> newline_cons()
|
||||
end
|
||||
|
||||
defp newline_cons(text) do
|
||||
[text | "\n\n"]
|
||||
end
|
||||
|
||||
defp handle_erlang_html_text(entries, indent, options) do
|
||||
if Enum.all?(entries, &inline_html?/1) do
|
||||
entries
|
||||
|> traverse_erlang_html(indent, options)
|
||||
|> IO.iodata_to_binary()
|
||||
|> String.split(@spaces)
|
||||
|> wrap_text(options[:width], indent, true, "", [])
|
||||
|> tl()
|
||||
|> newline_cons()
|
||||
else
|
||||
entries
|
||||
|> traverse_erlang_html(indent, options)
|
||||
|> IO.iodata_to_binary()
|
||||
|> String.trim_leading()
|
||||
end
|
||||
end
|
||||
|
||||
defp inline_html?(binary) when is_binary(binary), do: true
|
||||
defp inline_html?({tag, _, _}) when tag in [:a, :code, :em, :i, :strong, :b, :br], do: true
|
||||
defp inline_html?(_), do: false
|
||||
|
||||
## Markdown
|
||||
|
||||
def print_markdown(doc, options) do
|
||||
@spec print(String.t(), keyword) :: :ok
|
||||
def print(doc, options \\ []) do
|
||||
options = Keyword.merge(default_options(), options)
|
||||
|
||||
doc
|
||||
@@ -341,11 +139,6 @@ defmodule IO.ANSI.Docs do
|
||||
write_heading(heading, rest, text, indent, options)
|
||||
end
|
||||
|
||||
defp process([">" <> line | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
process_quote(rest, [line], indent, options)
|
||||
end
|
||||
|
||||
defp process(["" | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
process(rest, [], indent, options)
|
||||
@@ -381,61 +174,22 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
end
|
||||
|
||||
### Headings
|
||||
## Headings
|
||||
|
||||
defp write_heading(heading, rest, text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write(:doc_headings, heading, options)
|
||||
newline_after_block(options)
|
||||
newline_after_block()
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
|
||||
### Quotes
|
||||
|
||||
defp process_quote([], lines, indent, options) do
|
||||
write_quote(lines, indent, options, false)
|
||||
end
|
||||
|
||||
defp process_quote([">", ">" <> line | rest], lines, indent, options) do
|
||||
write_quote(lines, indent, options, true)
|
||||
write_empty_quote_line(options)
|
||||
process_quote(rest, [line], indent, options)
|
||||
end
|
||||
|
||||
defp process_quote([">" <> line | rest], lines, indent, options) do
|
||||
process_quote(rest, [line | lines], indent, options)
|
||||
end
|
||||
|
||||
defp process_quote(rest, lines, indent, options) do
|
||||
write_quote(lines, indent, options, false)
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
defp write_quote(lines, indent, options, no_wrap) do
|
||||
lines
|
||||
|> Enum.map(&String.trim/1)
|
||||
|> Enum.reverse()
|
||||
|> write_lines(
|
||||
indent,
|
||||
options,
|
||||
no_wrap,
|
||||
quote_prefix(options)
|
||||
)
|
||||
end
|
||||
|
||||
defp write_empty_quote_line(options) do
|
||||
options
|
||||
|> quote_prefix()
|
||||
|> IO.puts()
|
||||
end
|
||||
|
||||
### Lists
|
||||
## 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(bullet_text(options), item, rest, count, indent, options)
|
||||
process_list("• ", item, rest, count, indent, options)
|
||||
|
||||
<<d1, ?., ?\s, item::binary>> when d1 in ?0..?9 ->
|
||||
write_text(text, indent, options)
|
||||
@@ -455,12 +209,10 @@ defmodule IO.ANSI.Docs do
|
||||
entry = if indent == "", do: " " <> entry, else: entry
|
||||
new_indent = indent <> String.duplicate(" ", String.length(entry))
|
||||
|
||||
{contents, rest, done} =
|
||||
process_list_next(rest, count, byte_size(new_indent) - byte_size(indent), [])
|
||||
|
||||
{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(options)
|
||||
if done, do: newline_after_block()
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
@@ -501,7 +253,7 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
end
|
||||
|
||||
### Text
|
||||
## Text
|
||||
|
||||
defp write_text(text, indent, options) do
|
||||
case Enum.reverse(text) do
|
||||
@@ -515,26 +267,17 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp write_text(lines, indent, options, no_wrap) do
|
||||
write_lines(lines, indent, options, no_wrap, "")
|
||||
end
|
||||
|
||||
defp write_lines(lines, indent, options, no_wrap, prefix) do
|
||||
lines
|
||||
|> Enum.join(" ")
|
||||
|> format_text(options)
|
||||
|> String.split(@spaces)
|
||||
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap, prefix)
|
||||
|
||||
unless no_wrap, do: newline_after_block(options)
|
||||
end
|
||||
|
||||
defp format_text(text, options) do
|
||||
text
|
||||
|> handle_links()
|
||||
|> handle_links
|
||||
|> handle_inline(options)
|
||||
|> String.split(@spaces)
|
||||
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap)
|
||||
|
||||
unless no_wrap, do: newline_after_block()
|
||||
end
|
||||
|
||||
### Code blocks
|
||||
## Code blocks
|
||||
|
||||
defp process_code([], code, indent, options) do
|
||||
write_code(code, indent, options)
|
||||
@@ -573,15 +316,15 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp write_code(code, indent, options) do
|
||||
write(:doc_code, "#{indent} #{Enum.join(Enum.reverse(code), "\n#{indent} ")}", options)
|
||||
newline_after_block(options)
|
||||
newline_after_block()
|
||||
end
|
||||
|
||||
### Tables
|
||||
## Tables
|
||||
|
||||
defp process_table(lines, indent, options) do
|
||||
{table, rest} = Enum.split_while(lines, &table_line?/1)
|
||||
table_lines(table, options)
|
||||
newline_after_block(options)
|
||||
newline_after_block()
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
@@ -605,17 +348,17 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp split_into_columns(line, options) do
|
||||
line
|
||||
|> String.trim(" ")
|
||||
|> String.trim("|")
|
||||
|> String.split(~r{(?<!\\)\|})
|
||||
|> String.trim()
|
||||
|> String.split(" | ")
|
||||
|> Enum.map(&render_column(&1, options))
|
||||
end
|
||||
|
||||
defp render_column(col, options) do
|
||||
col =
|
||||
col
|
||||
|> String.trim()
|
||||
|> String.replace("\\\|", "|")
|
||||
|> String.trim()
|
||||
|> handle_links
|
||||
|> handle_inline(options)
|
||||
|
||||
@@ -707,7 +450,7 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp table_line?(line) do
|
||||
line =~ ~r/[:\ -]\|[:\ -]/
|
||||
line =~ " | "
|
||||
end
|
||||
|
||||
## Helpers
|
||||
@@ -724,30 +467,17 @@ defmodule IO.ANSI.Docs do
|
||||
defp strip_spaces(rest, acc, _max), do: {rest, acc}
|
||||
|
||||
defp write(style, string, options) do
|
||||
IO.puts([color(style, options), string, maybe_reset(options)])
|
||||
IO.puts([color(style, options), string, IO.ANSI.reset()])
|
||||
end
|
||||
|
||||
defp write_with_wrap([], _available, _indent, _first, _prefix) do
|
||||
defp write_with_wrap([], _available, _indent, _first) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp write_with_wrap(words, available, indent, first, prefix) do
|
||||
words
|
||||
|> wrap_text(available, indent, first, prefix, [])
|
||||
|> tl()
|
||||
|> IO.puts()
|
||||
end
|
||||
|
||||
defp wrap_text([], _available, _indent, _first, _prefix, wrapped_lines) do
|
||||
Enum.reverse(wrapped_lines)
|
||||
end
|
||||
|
||||
defp wrap_text(words, available, indent, first, prefix, wrapped_lines) do
|
||||
prefix_length = length_without_escape(prefix, 0)
|
||||
{words, rest} = take_words(words, available - prefix_length, [])
|
||||
line = [if(first, do: "", else: indent), prefix, Enum.join(words, " ")]
|
||||
|
||||
wrap_text(rest, available, indent, false, prefix, [line, ?\n | wrapped_lines])
|
||||
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
|
||||
@@ -795,21 +525,25 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp escape_underlines_in_link(text) do
|
||||
# Regular expression adapted from https://tools.ietf.org/html/rfc3986#appendix-B
|
||||
Regex.replace(~r{[a-z][a-z0-9\+\-\.]*://\S*}i, text, &String.replace(&1, "_", "\\_"))
|
||||
~r{[a-z][a-z0-9\+\-\.]*://\S*}i
|
||||
|> Regex.recompile!()
|
||||
|> Regex.replace(text, &String.replace(&1, "_", "\\_"))
|
||||
end
|
||||
|
||||
defp remove_square_brackets_in_link(text) do
|
||||
Regex.replace(~r{\[([^\]]*?)\]\((.*?)\)}, text, "\\1 (\\2)")
|
||||
~r{\[(.*?)\]\((.*?)\)}
|
||||
|> Regex.recompile!()
|
||||
|> Regex.replace(text, "\\1 (\\2)")
|
||||
end
|
||||
|
||||
# We have four entries: **, __, *, _ and `.
|
||||
# We have four entries: **, *, _ and `.
|
||||
#
|
||||
# The first four behave the same while the last one is simpler
|
||||
# The first three behave the same while the last one is simpler
|
||||
# when it comes to delimiters as it ignores spaces and escape
|
||||
# characters. But, since the first two has two characters,
|
||||
# we need to handle 3 cases:
|
||||
# characters. But, since the first has two characters, we need to
|
||||
# handle 3 cases:
|
||||
#
|
||||
# 1. __ and **
|
||||
# 1. **
|
||||
# 2. _ and *
|
||||
# 3. `
|
||||
#
|
||||
@@ -821,10 +555,10 @@ defmodule IO.ANSI.Docs do
|
||||
@delimiters [?\s, ?', ?", ?!, ?@, ?#, ?$, ?%, ?^, ?&] ++
|
||||
[?-, ?+, ?(, ?), ?[, ?], ?{, ?}, ?<, ?>, ?.]
|
||||
|
||||
### Inline start
|
||||
# Inline start
|
||||
|
||||
defp handle_inline(<<mark, mark, rest::binary>>, options) when mark in @single do
|
||||
handle_inline(rest, [mark | mark], [<<mark, mark>>], [], options)
|
||||
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
|
||||
@@ -835,12 +569,11 @@ defmodule IO.ANSI.Docs do
|
||||
handle_inline(rest, nil, [], [], options)
|
||||
end
|
||||
|
||||
### Inline delimiters
|
||||
# Inline delimiters
|
||||
|
||||
defp handle_inline(<<delimiter, mark, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters and mark in @single do
|
||||
acc = [delimiter, Enum.reverse(buffer) | acc]
|
||||
handle_inline(rest, [mark | mark], [<<mark, mark>>], acc, options)
|
||||
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)
|
||||
@@ -853,12 +586,11 @@ defmodule IO.ANSI.Docs do
|
||||
handle_inline(rest, ?`, ["`"], [Enum.reverse(buffer) | acc], options)
|
||||
end
|
||||
|
||||
### Clauses for handling escape
|
||||
# Clauses for handling escape
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, mark, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and mark in @single do
|
||||
acc = [?\\, Enum.reverse(buffer) | acc]
|
||||
handle_inline(rest, [mark | mark], [<<mark, mark>>], acc, options)
|
||||
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)
|
||||
@@ -875,10 +607,10 @@ defmodule IO.ANSI.Docs do
|
||||
handle_inline(rest, limit, [mark | buffer], acc, options)
|
||||
end
|
||||
|
||||
### Inline end
|
||||
# Inline end
|
||||
|
||||
defp handle_inline(<<mark, mark, delimiter, rest::binary>>, [mark | mark], buffer, acc, options)
|
||||
when delimiter in @delimiters and mark in @single do
|
||||
defp handle_inline(<<?*, ?*, delimiter, rest::binary>>, ?d, buffer, acc, options)
|
||||
when delimiter in @delimiters do
|
||||
inline_buffer = inline_buffer(buffer, options)
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer | acc], options)
|
||||
end
|
||||
@@ -889,8 +621,8 @@ defmodule IO.ANSI.Docs do
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer | acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, mark, rest::binary>>, [mark | mark], buffer, acc, options)
|
||||
when rest == "" and mark in @single do
|
||||
defp handle_inline(<<?*, ?*, rest::binary>>, ?d, buffer, acc, options)
|
||||
when rest == "" do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
end
|
||||
|
||||
@@ -903,7 +635,7 @@ defmodule IO.ANSI.Docs do
|
||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
end
|
||||
|
||||
### Catch all
|
||||
# Catch all
|
||||
|
||||
defp handle_inline(<<char, rest::binary>>, mark, buffer, acc, options) do
|
||||
handle_inline(rest, mark, [char | buffer], acc, options)
|
||||
@@ -914,49 +646,23 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp inline_buffer(buffer, options) do
|
||||
[mark | t] = Enum.reverse(buffer)
|
||||
inline_text(mark, t, options)
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp quote_prefix(options), do: "#{color(:doc_quote, options)}> #{maybe_reset(options)}"
|
||||
|
||||
defp heading(text, n, options) do
|
||||
[color(:doc_headings, options), String.duplicate("#", n), " ", text, maybe_reset(options)]
|
||||
end
|
||||
|
||||
defp inline_text(mark, text, options) do
|
||||
if options[:enabled] do
|
||||
[[color_for(mark, options) | text] | IO.ANSI.reset()]
|
||||
else
|
||||
[[mark | text] | mark]
|
||||
end
|
||||
[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_bold, colors)
|
||||
"**" -> color(:doc_bold, colors)
|
||||
"_" -> color(:doc_underline, colors)
|
||||
"*" -> color(:doc_underline, colors)
|
||||
"`" -> color(:doc_inline_code, colors)
|
||||
"_" -> color(:doc_underline, colors)
|
||||
"*" -> color(:doc_bold, colors)
|
||||
"**" -> color(:doc_bold, colors)
|
||||
end
|
||||
end
|
||||
|
||||
defp bullet_text(options) do
|
||||
if options[:enabled], do: @bullet_text_unicode, else: @bullet_text_ascii
|
||||
end
|
||||
|
||||
defp color(style, colors) do
|
||||
IO.ANSI.format_fragment(colors[style], colors[:enabled])
|
||||
color = colors[style]
|
||||
IO.ANSI.format_fragment(color, colors[:enabled])
|
||||
end
|
||||
|
||||
defp newline_after_block(options) do
|
||||
IO.puts(maybe_reset(options))
|
||||
end
|
||||
|
||||
defp maybe_reset(options) do
|
||||
if options[:enabled], do: IO.ANSI.reset(), else: ""
|
||||
end
|
||||
defp newline_after_block, do: IO.puts(IO.ANSI.reset())
|
||||
end
|
||||
|
||||
+826
-1589
File diff suppressed because it is too large
Load Diff
@@ -1,23 +1,18 @@
|
||||
defmodule Kernel.CLI do
|
||||
@moduledoc false
|
||||
|
||||
@compile {:no_warn_undefined, [Logger, IEx]}
|
||||
|
||||
@blank_config %{
|
||||
commands: [],
|
||||
output: ".",
|
||||
compile: [],
|
||||
no_halt: false,
|
||||
halt: true,
|
||||
compiler_options: [],
|
||||
errors: [],
|
||||
pa: [],
|
||||
pz: [],
|
||||
verbose_compile: false,
|
||||
profile: nil
|
||||
verbose_compile: false
|
||||
}
|
||||
|
||||
@standalone_opts ["-h", "--help", "--short-version"]
|
||||
|
||||
@doc """
|
||||
This is the API invoked by Elixir boot process.
|
||||
"""
|
||||
@@ -26,7 +21,6 @@ defmodule Kernel.CLI do
|
||||
|
||||
{config, argv} = parse_argv(argv)
|
||||
System.argv(argv)
|
||||
System.no_halt(config.no_halt)
|
||||
|
||||
fun = fn _ ->
|
||||
errors = process_commands(config)
|
||||
@@ -37,7 +31,7 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
end
|
||||
|
||||
run(fun)
|
||||
run(fun, config.halt)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -48,10 +42,10 @@ defmodule Kernel.CLI do
|
||||
This function is used by Elixir's CLI and also
|
||||
by escripts generated by Elixir.
|
||||
"""
|
||||
def run(fun) do
|
||||
def run(fun, halt \\ true) do
|
||||
{ok_or_shutdown, status} = exec_fun(fun, {:ok, 0})
|
||||
|
||||
if ok_or_shutdown == :shutdown or not System.no_halt() do
|
||||
if ok_or_shutdown == :shutdown or halt do
|
||||
{_, status} = at_exit({ok_or_shutdown, status})
|
||||
|
||||
# Ensure Logger messages are flushed before halting
|
||||
@@ -64,25 +58,19 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses the CLI arguments. Made public for testing.
|
||||
"""
|
||||
@doc false
|
||||
def parse_argv(argv) do
|
||||
parse_argv(argv, @blank_config)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Process CLI commands. Made public for testing.
|
||||
"""
|
||||
@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
|
||||
|
||||
@doc """
|
||||
Shared helper for error formatting on CLI tools.
|
||||
"""
|
||||
@doc false
|
||||
def format_error(kind, reason, stacktrace) do
|
||||
{blamed, stacktrace} = Exception.blame(kind, reason, stacktrace)
|
||||
|
||||
@@ -90,7 +78,7 @@ defmodule Kernel.CLI do
|
||||
case blamed do
|
||||
%FunctionClauseError{} ->
|
||||
formatted = Exception.format_banner(kind, reason, stacktrace)
|
||||
padded_blame = pad(FunctionClauseError.blame(blamed, &inspect/1, &blame_match/1))
|
||||
padded_blame = pad(FunctionClauseError.blame(blamed, &inspect/1, &blame_match/2))
|
||||
[formatted, padded_blame]
|
||||
|
||||
_ ->
|
||||
@@ -100,15 +88,6 @@ defmodule Kernel.CLI do
|
||||
[iodata, ?\n, Exception.format_stacktrace(prune_stacktrace(stacktrace))]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Function invoked across nodes for `--rpc-eval`.
|
||||
"""
|
||||
def rpc_eval(expr) do
|
||||
wrapper(fn -> Code.eval_string(expr) end)
|
||||
catch
|
||||
kind, reason -> {kind, reason, __STACKTRACE__}
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp at_exit(res) do
|
||||
@@ -178,8 +157,9 @@ defmodule Kernel.CLI do
|
||||
IO.write(:stderr, format_error(kind, reason, stacktrace))
|
||||
end
|
||||
|
||||
defp blame_match(%{match?: true, node: node}), do: blame_ansi(:normal, "+", node)
|
||||
defp blame_match(%{match?: false, node: node}), do: blame_ansi(:red, "-", node)
|
||||
defp blame_match(%{match?: true, node: node}, _), do: blame_ansi(:normal, "+", node)
|
||||
defp blame_match(%{match?: false, node: node}, _), do: blame_ansi(:red, "-", node)
|
||||
defp blame_match(_, string), do: string
|
||||
|
||||
defp blame_ansi(color, no_ansi, node) do
|
||||
if IO.ANSI.enabled?() do
|
||||
@@ -196,9 +176,8 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
|
||||
@elixir_internals [:elixir, :elixir_aliases, :elixir_expand, :elixir_compiler, :elixir_module] ++
|
||||
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map, :elixir_locals] ++
|
||||
[:elixir_erl, :elixir_erl_clauses, :elixir_erl_compiler, :elixir_erl_pass] ++
|
||||
[Kernel.ErrorHandler, Module.ParallelChecker]
|
||||
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map] ++
|
||||
[:elixir_erl, :elixir_erl_clauses, :elixir_erl_pass, Kernel.ErrorHandler]
|
||||
|
||||
defp prune_stacktrace([{mod, _, _, _} | t]) when mod in @elixir_internals do
|
||||
prune_stacktrace(t)
|
||||
@@ -218,16 +197,7 @@ defmodule Kernel.CLI do
|
||||
|
||||
# Parse shared options
|
||||
|
||||
defp warn_standalone(opt) do
|
||||
IO.puts(:stderr, "#{opt} : Standalone options can't be combined with other options")
|
||||
end
|
||||
|
||||
defp parse_shared([opt | _], _config) when opt in @standalone_opts do
|
||||
warn_standalone(opt)
|
||||
System.halt(1)
|
||||
end
|
||||
|
||||
defp parse_shared([opt | t], _config) when opt in ["-v", "--version"] do
|
||||
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.build_info()[:build])
|
||||
else
|
||||
@@ -235,7 +205,6 @@ defmodule Kernel.CLI do
|
||||
IO.puts("Elixir " <> System.build_info()[:build])
|
||||
end
|
||||
|
||||
t != [] && warn_standalone(opt)
|
||||
System.halt(0)
|
||||
end
|
||||
|
||||
@@ -256,22 +225,13 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
|
||||
defp parse_shared(["--no-halt" | t], config) do
|
||||
parse_shared(t, %{config | no_halt: true})
|
||||
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(["--eval", h | t], config) do
|
||||
parse_shared(t, %{config | commands: [{:eval, h} | config.commands]})
|
||||
end
|
||||
|
||||
defp parse_shared(["--rpc-eval", node, h | t], config) do
|
||||
node = append_hostname(node)
|
||||
parse_shared(t, %{config | commands: [{:rpc_eval, node, h} | config.commands]})
|
||||
end
|
||||
|
||||
defp parse_shared(["-r", h | t], config) do
|
||||
parse_shared(t, %{config | commands: [{:require, h} | config.commands]})
|
||||
end
|
||||
@@ -280,15 +240,21 @@ defmodule Kernel.CLI do
|
||||
parse_shared(t, %{config | commands: [{:parallel_require, h} | config.commands]})
|
||||
end
|
||||
|
||||
defp parse_shared(list, config) do
|
||||
{list, config}
|
||||
@erl_arg_options ["--erl", "--sname", "--name", "--cookie"] ++
|
||||
["--logger-otp-reports", "--logger-sasl-reports"]
|
||||
|
||||
@erl_boolean_options ["--detached", "--hidden", "--werl"]
|
||||
|
||||
defp parse_shared([erl, _ | t], config) when erl in @erl_arg_options do
|
||||
parse_shared(t, config)
|
||||
end
|
||||
|
||||
defp append_hostname(node) do
|
||||
case :string.find(node, "@") do
|
||||
:nomatch -> node <> :string.find(Atom.to_string(node()), "@")
|
||||
_ -> node
|
||||
end
|
||||
defp parse_shared([erl | t], config) when erl in @erl_boolean_options do
|
||||
parse_shared(t, config)
|
||||
end
|
||||
|
||||
defp parse_shared(list, config) do
|
||||
{list, config}
|
||||
end
|
||||
|
||||
defp expand_code_path(path) do
|
||||
@@ -324,7 +290,7 @@ defmodule Kernel.CLI do
|
||||
shared_option?(list, config, &parse_argv(&1, &2))
|
||||
|
||||
_ ->
|
||||
if List.keymember?(config.commands, :eval, 0) do
|
||||
if Keyword.has_key?(config.commands, :eval) do
|
||||
{config, list}
|
||||
else
|
||||
{%{config | commands: [{:file, h} | config.commands]}, t}
|
||||
@@ -369,12 +335,6 @@ defmodule Kernel.CLI do
|
||||
parse_compiler(t, %{config | verbose_compile: true})
|
||||
end
|
||||
|
||||
# Private compiler options
|
||||
|
||||
defp parse_compiler(["--profile", "time" | t], config) do
|
||||
parse_compiler(t, %{config | profile: :time})
|
||||
end
|
||||
|
||||
defp parse_compiler([h | t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
@@ -435,15 +395,6 @@ defmodule Kernel.CLI do
|
||||
wrapper(fn -> Code.eval_string(expr, []) end)
|
||||
end
|
||||
|
||||
defp process_command({:rpc_eval, node, expr}, _config) when is_binary(expr) do
|
||||
case :rpc.call(String.to_atom(node), __MODULE__, :rpc_eval, [expr]) do
|
||||
:ok -> :ok
|
||||
{:badrpc, {:EXIT, exit}} -> Process.exit(self(), exit)
|
||||
{:badrpc, reason} -> {:error, "--rpc-eval : RPC failed with reason #{inspect(reason)}"}
|
||||
{kind, error, stack} -> :erlang.raise(kind, error, stack)
|
||||
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}} ->
|
||||
@@ -508,25 +459,13 @@ defmodule Kernel.CLI do
|
||||
wrapper(fn ->
|
||||
Code.compiler_options(config.compiler_options)
|
||||
|
||||
verbose_opts =
|
||||
opts =
|
||||
if config.verbose_compile do
|
||||
[each_file: &IO.puts("Compiling #{Path.relative_to_cwd(&1)}")]
|
||||
else
|
||||
[
|
||||
each_long_compilation:
|
||||
&IO.puts("Compiling #{Path.relative_to_cwd(&1)} (it's taking more than 10s)")
|
||||
]
|
||||
end
|
||||
|
||||
profile_opts =
|
||||
if config.profile do
|
||||
[profile: config.profile]
|
||||
[each_long_compilation: &IO.puts("Compiling #{&1} (it's taking more than 15s)")]
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
||||
opts = verbose_opts ++ profile_opts
|
||||
|
||||
case Kernel.ParallelCompiler.compile_to_path(files, config.output, opts) do
|
||||
{:ok, _, _} -> :ok
|
||||
{:error, _, _} -> exit({:shutdown, 1})
|
||||
@@ -540,7 +479,6 @@ defmodule Kernel.CLI do
|
||||
|
||||
defp filter_patterns(pattern) do
|
||||
pattern
|
||||
|> Path.expand()
|
||||
|> Path.wildcard()
|
||||
|> :lists.usort()
|
||||
|> Enum.filter(&File.regular?/1)
|
||||
|
||||
@@ -5,13 +5,13 @@ defmodule Kernel.ErrorHandler do
|
||||
|
||||
@spec undefined_function(module, atom, list) :: term
|
||||
def undefined_function(module, fun, args) do
|
||||
ensure_loaded(module) or ensure_compiled(module, :module, :raise)
|
||||
ensure_loaded(module) or ensure_compiled(module, :module)
|
||||
:error_handler.undefined_function(module, fun, args)
|
||||
end
|
||||
|
||||
@spec undefined_lambda(module, fun, list) :: term
|
||||
def undefined_lambda(module, fun, args) do
|
||||
ensure_loaded(module) or ensure_compiled(module, :module, :raise)
|
||||
ensure_loaded(module) or ensure_compiled(module, :module)
|
||||
:error_handler.undefined_lambda(module, fun, args)
|
||||
end
|
||||
|
||||
@@ -23,21 +23,21 @@ defmodule Kernel.ErrorHandler do
|
||||
end
|
||||
end
|
||||
|
||||
@spec ensure_compiled(module, atom, atom) :: :found | :not_found | :deadlock
|
||||
@spec ensure_compiled(module, atom) :: boolean
|
||||
# Never wait on nil because it should never be defined.
|
||||
def ensure_compiled(nil, _kind, _deadlock) do
|
||||
:not_found
|
||||
def ensure_compiled(nil, _kind) do
|
||||
false
|
||||
end
|
||||
|
||||
def ensure_compiled(module, kind, deadlock) do
|
||||
{compiler_pid, file_pid} = :erlang.get(:elixir_compiler_info)
|
||||
def ensure_compiled(module, kind) do
|
||||
parent = :erlang.get(:elixir_compiler_pid)
|
||||
ref = :erlang.make_ref()
|
||||
modules = :elixir_module.compiler_modules()
|
||||
send(compiler_pid, {:waiting, kind, self(), ref, file_pid, module, modules, deadlock})
|
||||
send(parent, {:waiting, kind, self(), ref, module, :elixir_module.compiler_modules()})
|
||||
:erlang.garbage_collect(self())
|
||||
|
||||
receive do
|
||||
{^ref, value} -> value
|
||||
{^ref, :found} -> true
|
||||
{^ref, :not_found} -> false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,14 +6,28 @@
|
||||
# any of the `GenServer.Behaviour` conveniences.
|
||||
defmodule Kernel.LexicalTracker do
|
||||
@moduledoc false
|
||||
@timeout :infinity
|
||||
@timeout 30000
|
||||
@behaviour :gen_server
|
||||
|
||||
@doc """
|
||||
Returns all references in this lexical scope.
|
||||
Returns all remotes referenced in this lexical scope.
|
||||
"""
|
||||
def references(pid) do
|
||||
:gen_server.call(pid, :references, @timeout)
|
||||
def remote_references(arg) do
|
||||
:gen_server.call(to_pid(arg), :remote_references, @timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all remote dispatches in this lexical scope.
|
||||
"""
|
||||
def remote_dispatches(arg) do
|
||||
:gen_server.call(to_pid(arg), :remote_dispatches, @timeout)
|
||||
end
|
||||
|
||||
defp to_pid(pid) when is_pid(pid), do: pid
|
||||
|
||||
defp to_pid(mod) when is_atom(mod) do
|
||||
{set, _} = :elixir_module.data_tables(mod)
|
||||
:ets.lookup_element(set, {:elixir, :lexical_tracker}, 2)
|
||||
end
|
||||
|
||||
# Internal API
|
||||
@@ -26,12 +40,7 @@ defmodule Kernel.LexicalTracker do
|
||||
|
||||
@doc false
|
||||
def stop(pid) do
|
||||
:gen_server.call(pid, :stop)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def add_require(pid, module) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:add_require, module})
|
||||
:gen_server.cast(pid, :stop)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -45,13 +54,23 @@ defmodule Kernel.LexicalTracker do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def remote_dispatch(pid, module, mode) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:remote_dispatch, module, mode})
|
||||
def remote_reference(pid, module, mode) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:remote_reference, module, mode})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def import_dispatch(pid, module, fa, mode) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:import_dispatch, module, fa, mode})
|
||||
def remote_dispatch(pid, module, fa, line, mode) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:remote_dispatch, module, fa, line, mode})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def remote_struct(pid, module, line) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:remote_struct, module, line})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def import_dispatch(pid, module, fa, line, mode) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:import_dispatch, module, fa, line, mode})
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -59,11 +78,6 @@ defmodule Kernel.LexicalTracker do
|
||||
:gen_server.cast(pid, {:alias_dispatch, module})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def add_compile_env(pid, app, path, return) do
|
||||
:gen_server.cast(pid, {:compile_env, app, path, return})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def set_file(pid, file) do
|
||||
:gen_server.cast(pid, {:set_file, file})
|
||||
@@ -106,9 +120,10 @@ defmodule Kernel.LexicalTracker do
|
||||
state = %{
|
||||
directives: %{},
|
||||
references: %{},
|
||||
exports: %{},
|
||||
compile: %{},
|
||||
runtime: %{},
|
||||
structs: %{},
|
||||
cache: %{},
|
||||
compile_env: :ordsets.new(),
|
||||
file: nil
|
||||
}
|
||||
|
||||
@@ -125,30 +140,45 @@ defmodule Kernel.LexicalTracker do
|
||||
{:reply, Enum.sort(directives), state}
|
||||
end
|
||||
|
||||
def handle_call(:references, _from, state) do
|
||||
{compile, runtime} = partition(Map.to_list(state.references), [], [])
|
||||
{:reply, {compile, Map.keys(state.exports), runtime, state.compile_env}, state}
|
||||
def handle_call(:remote_references, _from, state) do
|
||||
{compile, runtime} = partition(:maps.to_list(state.references), [], [])
|
||||
{:reply, {compile, :maps.keys(state.structs), runtime}, state}
|
||||
end
|
||||
|
||||
def handle_call(:remote_dispatches, _from, state) do
|
||||
{:reply, {state.compile, state.runtime}, state}
|
||||
end
|
||||
|
||||
def handle_call({:read_cache, key}, _from, %{cache: cache} = state) do
|
||||
{:reply, Map.get(cache, key), state}
|
||||
end
|
||||
|
||||
def handle_call(:stop, _from, state) do
|
||||
{:stop, :normal, :ok, state}
|
||||
{:reply, :maps.get(key, cache), state}
|
||||
end
|
||||
|
||||
def handle_cast({:write_cache, key, value}, %{cache: cache} = state) do
|
||||
{:noreply, %{state | cache: Map.put(cache, key, value)}}
|
||||
{:noreply, %{state | cache: :maps.put(key, value, cache)}}
|
||||
end
|
||||
|
||||
def handle_cast({:remote_dispatch, module, mode}, state) do
|
||||
def handle_cast({:remote_reference, module, mode}, state) do
|
||||
{:noreply, %{state | references: add_reference(state.references, module, mode)}}
|
||||
end
|
||||
|
||||
def handle_cast({:remote_struct, module, line}, state) do
|
||||
state = add_remote_dispatch(state, module, {:__struct__, 0}, line, :compile)
|
||||
structs = :maps.put(module, true, state.structs)
|
||||
{:noreply, %{state | structs: structs}}
|
||||
end
|
||||
|
||||
def handle_cast({:remote_dispatch, module, fa, line, mode}, state) do
|
||||
references = add_reference(state.references, module, mode)
|
||||
state = add_remote_dispatch(state, module, fa, line, mode)
|
||||
{:noreply, %{state | references: references}}
|
||||
end
|
||||
|
||||
def handle_cast({:import_dispatch, module, {function, arity}, mode}, state) do
|
||||
state = add_import_dispatch(state, module, function, arity, mode)
|
||||
def handle_cast({:import_dispatch, module, {function, arity} = fa, line, mode}, state) do
|
||||
state =
|
||||
state
|
||||
|> add_import_dispatch(module, function, arity)
|
||||
|> add_remote_dispatch(module, fa, line, mode)
|
||||
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
@@ -164,20 +194,11 @@ defmodule Kernel.LexicalTracker do
|
||||
{:noreply, %{state | file: nil}}
|
||||
end
|
||||
|
||||
def handle_cast({:compile_env, app, path, return}, state) do
|
||||
{:noreply, update_in(state.compile_env, &:ordsets.add_element({app, path, return}, &1))}
|
||||
end
|
||||
|
||||
def handle_cast({:add_require, module}, state) do
|
||||
{:noreply, put_in(state.exports[module], true)}
|
||||
end
|
||||
|
||||
def handle_cast({:add_import, module, fas, line, warn}, state) do
|
||||
to_remove = for {{:import, {^module, _, _}} = key, _} <- state.directives, do: key
|
||||
|
||||
directives =
|
||||
state.directives
|
||||
|> Map.drop(to_remove)
|
||||
|> Enum.reject(&match?({{:import, {^module, _, _}}, _}, &1))
|
||||
|> :maps.from_list()
|
||||
|> add_directive(module, line, warn, :import)
|
||||
|
||||
directives =
|
||||
@@ -192,6 +213,10 @@ defmodule Kernel.LexicalTracker do
|
||||
{:noreply, %{state | directives: add_directive(state.directives, module, line, warn, :alias)}}
|
||||
end
|
||||
|
||||
def handle_cast(:stop, state) do
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(_msg, state) do
|
||||
{:noreply, state}
|
||||
@@ -218,22 +243,37 @@ defmodule Kernel.LexicalTracker do
|
||||
# Callbacks helpers
|
||||
|
||||
defp add_reference(references, module, :compile) when is_atom(module),
|
||||
do: Map.put(references, module, :compile)
|
||||
do: :maps.put(module, :compile, references)
|
||||
|
||||
defp add_reference(references, module, :runtime) when is_atom(module) do
|
||||
case Map.fetch(references, module) do
|
||||
case :maps.find(module, references) do
|
||||
{:ok, _} -> references
|
||||
:error -> Map.put(references, module, :runtime)
|
||||
:error -> :maps.put(module, :runtime, references)
|
||||
end
|
||||
end
|
||||
|
||||
defp add_import_dispatch(state, module, function, arity, mode) do
|
||||
defp add_remote_dispatch(state, module, fa, line, mode) when is_atom(module) do
|
||||
location = location(state.file, line)
|
||||
|
||||
map_update(mode, %{module => %{fa => [location]}}, state, fn mode_dispatches ->
|
||||
map_update(module, %{fa => [location]}, mode_dispatches, fn module_dispatches ->
|
||||
map_update(fa, [location], module_dispatches, &[location | List.delete(&1, location)])
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
defp location(nil, line), do: line
|
||||
defp location(file, line), do: {file, line}
|
||||
|
||||
defp add_import_dispatch(state, module, function, arity) do
|
||||
directives =
|
||||
state.directives
|
||||
|> add_dispatch(module, :import)
|
||||
add_dispatch(state.directives, module, :import)
|
||||
|> add_dispatch({module, function, arity}, :import)
|
||||
|
||||
references = add_reference(state.references, module, mode)
|
||||
# Always compile time because we depend
|
||||
# on the module at compile time
|
||||
references = add_reference(state.references, module, :compile)
|
||||
|
||||
%{state | directives: directives, references: references}
|
||||
end
|
||||
|
||||
@@ -242,10 +282,17 @@ defmodule Kernel.LexicalTracker do
|
||||
# If the value is true, it was imported/aliased and used
|
||||
defp add_directive(directives, module_or_mfa, line, warn, tag) do
|
||||
marker = if warn, do: line, else: true
|
||||
Map.put(directives, {tag, module_or_mfa}, marker)
|
||||
:maps.put({tag, module_or_mfa}, marker, directives)
|
||||
end
|
||||
|
||||
defp add_dispatch(directives, module_or_mfa, tag) do
|
||||
Map.put(directives, {tag, module_or_mfa}, true)
|
||||
:maps.put({tag, module_or_mfa}, true, directives)
|
||||
end
|
||||
|
||||
defp map_update(key, initial, map, fun) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, val} -> :maps.put(key, fun.(val), map)
|
||||
:error -> :maps.put(key, initial, map)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,12 +3,6 @@ defmodule Kernel.ParallelCompiler do
|
||||
A module responsible for compiling and requiring files in parallel.
|
||||
"""
|
||||
|
||||
@typedoc "The line. 0 indicates no line."
|
||||
@type line() :: non_neg_integer()
|
||||
@type location() :: line() | {line(), column :: non_neg_integer}
|
||||
@type warning() :: {file :: Path.t(), location(), message :: String.t()}
|
||||
@type error() :: {file :: Path.t(), line(), message :: String.t()}
|
||||
|
||||
@doc """
|
||||
Starts a task for parallel compilation.
|
||||
|
||||
@@ -21,29 +15,23 @@ defmodule Kernel.ParallelCompiler do
|
||||
always awaited on by calling `Task.await/1`
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
def async(fun) when is_function(fun, 0) do
|
||||
case :erlang.get(:elixir_compiler_info) do
|
||||
{compiler, _} ->
|
||||
file = :erlang.get(:elixir_compiler_file)
|
||||
dest = :erlang.get(:elixir_compiler_dest)
|
||||
def async(fun) when is_function(fun) do
|
||||
if parent = :erlang.get(:elixir_compiler_pid) do
|
||||
file = :erlang.get(:elixir_compiler_file)
|
||||
dest = :erlang.get(:elixir_compiler_dest)
|
||||
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
|
||||
|
||||
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
|
||||
checker = Module.ParallelChecker.get()
|
||||
|
||||
Task.async(fn ->
|
||||
send(compiler, {:async, self()})
|
||||
Module.ParallelChecker.put(compiler, checker)
|
||||
:erlang.put(:elixir_compiler_info, {compiler, self()})
|
||||
:erlang.put(:elixir_compiler_file, file)
|
||||
dest != :undefined and :erlang.put(:elixir_compiler_dest, dest)
|
||||
:erlang.process_flag(:error_handler, error_handler)
|
||||
fun.()
|
||||
end)
|
||||
|
||||
:undefined ->
|
||||
raise ArgumentError,
|
||||
"cannot spawn parallel compiler task because " <>
|
||||
"the current file is not being compiled/required"
|
||||
Task.async(fn ->
|
||||
:erlang.put(:elixir_compiler_pid, parent)
|
||||
:erlang.put(:elixir_compiler_file, file)
|
||||
dest != :undefined and :erlang.put(:elixir_compiler_dest, dest)
|
||||
:erlang.process_flag(:error_handler, error_handler)
|
||||
fun.()
|
||||
end)
|
||||
else
|
||||
raise ArgumentError,
|
||||
"cannot spawn parallel compiler task because " <>
|
||||
"the current file is not being compiled/required"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -73,43 +61,23 @@ defmodule Kernel.ParallelCompiler do
|
||||
the file, module and the module bytecode
|
||||
|
||||
* `:each_cycle` - after the given files are compiled, invokes this function
|
||||
that should return the following values:
|
||||
* `{:compile, modules, warnings}` - to continue compilation with a list of
|
||||
further modules to compile
|
||||
* `{:runtime, modules, warnings}` - to stop compilation and verify the list
|
||||
of modules because dependent modules have changed
|
||||
that return a list with potentially more files to compile
|
||||
|
||||
* `:long_compilation_threshold` - the timeout (in seconds) to check for modules
|
||||
taking too long to compile. For each file that exceeds the threshold, the
|
||||
`:each_long_compilation` callback is invoked. From Elixir v1.11, only the time
|
||||
spent compiling the actual module is taken into account by the threshold, the
|
||||
time spent waiting is not considered. Defaults to `10` seconds.
|
||||
* `:long_compilation_threshold` - the timeout (in seconds) after the
|
||||
`:each_long_compilation` callback is invoked; defaults to `15`
|
||||
|
||||
* `:profile` - if set to `:time` measure the compilation time of each compilation cycle
|
||||
and group pass checker
|
||||
|
||||
* `:dest` - the destination directory for the BEAM files. When using `compile/2`,
|
||||
* `: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 `compile_to_path/3` instead.
|
||||
|
||||
* `:beam_timestamp` - the modification timestamp to give all BEAM files
|
||||
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec compile([Path.t()], keyword()) :: {:ok, [atom], [warning]} | {:error, [error], [warning]}
|
||||
def compile(files, options \\ []) when is_list(options) do
|
||||
spawn_workers(files, :compile, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compiles the given files and writes resulting BEAM files into path.
|
||||
|
||||
See `compile/2` for more information.
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec compile_to_path([Path.t()], Path.t(), keyword()) ::
|
||||
{:ok, [atom], [warning]} | {:error, [error], [warning]}
|
||||
def compile_to_path(files, path, options \\ []) when is_binary(path) and is_list(options) do
|
||||
spawn_workers(files, {:compile, path}, options)
|
||||
end
|
||||
@@ -135,21 +103,11 @@ defmodule Kernel.ParallelCompiler do
|
||||
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec require([Path.t()], keyword()) ::
|
||||
{:ok, [atom], [warning]} | {:error, [error], [warning]}
|
||||
def require(files, options \\ []) when is_list(options) do
|
||||
spawn_workers(files, :require, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prints a warning returned by the compiler.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec print_warning(warning) :: :ok
|
||||
def print_warning({file, location, warning}) do
|
||||
:elixir_errors.print_warning(location, file, warning)
|
||||
end
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
@doc false
|
||||
@deprecated "Use Kernel.ParallelCompiler.compile/2 instead"
|
||||
def files(files, options \\ []) when is_list(options) do
|
||||
@@ -159,6 +117,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
@doc false
|
||||
@deprecated "Use Kernel.ParallelCompiler.compile_to_path/2 instead"
|
||||
def files_to_path(files, path, options \\ []) when is_binary(path) and is_list(options) do
|
||||
@@ -170,543 +129,321 @@ defmodule Kernel.ParallelCompiler do
|
||||
|
||||
defp spawn_workers(files, output, options) do
|
||||
{:module, _} = :code.ensure_loaded(Kernel.ErrorHandler)
|
||||
compiler_pid = self()
|
||||
:elixir_code_server.cast({:reset_warnings, compiler_pid})
|
||||
schedulers = max(:erlang.system_info(:schedulers_online), 2)
|
||||
{:ok, checker} = Module.ParallelChecker.start_link(schedulers)
|
||||
|
||||
try do
|
||||
outcome = spawn_workers(schedulers, checker, files, output, options)
|
||||
{outcome, Code.get_compiler_option(:warnings_as_errors)}
|
||||
else
|
||||
{{:ok, _, [_ | _] = warnings}, true} ->
|
||||
result =
|
||||
spawn_workers(files, [], [], [], [], %{
|
||||
dest: Keyword.get(options, :dest),
|
||||
each_cycle: Keyword.get(options, :each_cycle, fn -> [] end),
|
||||
each_file: Keyword.get(options, :each_file, fn _file -> :ok end),
|
||||
each_long_compilation: Keyword.get(options, :each_long_compilation, fn _file -> :ok end),
|
||||
each_module: Keyword.get(options, :each_module, fn _file, _module, _binary -> :ok end),
|
||||
output: output,
|
||||
long_compilation_threshold: Keyword.get(options, :long_compilation_threshold, 15),
|
||||
schedulers: schedulers
|
||||
})
|
||||
|
||||
# In case --warning-as-errors is enabled and there was a warning,
|
||||
# compilation status will be set to error.
|
||||
compilation_status = :elixir_code_server.call({:compilation_status, compiler_pid})
|
||||
|
||||
case {result, compilation_status} do
|
||||
{{:ok, _, warnings}, :error} ->
|
||||
message = "Compilation failed due to warnings while using the --warnings-as-errors option"
|
||||
IO.puts(:stderr, message)
|
||||
{:error, warnings, []}
|
||||
|
||||
{{:ok, outcome, warnings}, _} ->
|
||||
beam_timestamp = Keyword.get(options, :beam_timestamp)
|
||||
{:ok, write_module_binaries(outcome, output, beam_timestamp), warnings}
|
||||
|
||||
{{:error, errors, warnings}, true} ->
|
||||
{{:error, errors, warnings}, :error} ->
|
||||
{:error, errors ++ warnings, []}
|
||||
|
||||
{{:error, errors, warnings}, _} ->
|
||||
{:error, errors, warnings}
|
||||
after
|
||||
Module.ParallelChecker.stop(checker)
|
||||
end
|
||||
end
|
||||
|
||||
defp spawn_workers(schedulers, checker, files, output, options) do
|
||||
threshold = Keyword.get(options, :long_compilation_threshold, 10) * 1000
|
||||
timer_ref = Process.send_after(self(), :threshold_check, threshold)
|
||||
|
||||
{outcome, state} =
|
||||
spawn_workers(files, 0, [], [], %{}, [], %{
|
||||
dest: Keyword.get(options, :dest),
|
||||
each_cycle: Keyword.get(options, :each_cycle, fn -> {:runtime, [], []} end),
|
||||
each_file: Keyword.get(options, :each_file, fn _, _ -> :ok end) |> each_file(),
|
||||
each_long_compilation: Keyword.get(options, :each_long_compilation, fn _file -> :ok end),
|
||||
each_module: Keyword.get(options, :each_module, fn _file, _module, _binary -> :ok end),
|
||||
profile: profile_init(Keyword.get(options, :profile)),
|
||||
output: output,
|
||||
timer_ref: timer_ref,
|
||||
long_compilation_threshold: threshold,
|
||||
schedulers: schedulers,
|
||||
checker: checker
|
||||
})
|
||||
|
||||
Process.cancel_timer(state.timer_ref)
|
||||
|
||||
receive do
|
||||
:threshold_check -> :ok
|
||||
after
|
||||
0 -> :ok
|
||||
end
|
||||
|
||||
outcome
|
||||
end
|
||||
|
||||
defp each_file(fun) when is_function(fun, 1), do: fn file, _ -> fun.(file) end
|
||||
defp each_file(fun) when is_function(fun, 2), do: fun
|
||||
|
||||
defp each_file(file, lexical, parent) do
|
||||
ref = Process.monitor(parent)
|
||||
send(parent, {:file_ok, self(), ref, file, lexical})
|
||||
|
||||
receive do
|
||||
^ref -> :ok
|
||||
{:DOWN, ^ref, _, _, _} -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
defp write_module_binaries(result, {:compile, path}, timestamp) do
|
||||
Enum.flat_map(result, fn
|
||||
{{:module, module}, {binary, _map}} ->
|
||||
full_path = Path.join(path, Atom.to_string(module) <> ".beam")
|
||||
File.write!(full_path, binary)
|
||||
if timestamp, do: File.touch!(full_path, timestamp)
|
||||
[module]
|
||||
|
||||
_ ->
|
||||
[]
|
||||
end)
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
defp write_module_binaries(result, _output, _timestamp) do
|
||||
for {{:module, module}, _} <- result, do: module
|
||||
end
|
||||
|
||||
## Verification
|
||||
|
||||
defp verify_modules(result, warnings, dependent_modules, state) do
|
||||
checker_warnings = maybe_check_modules(result, dependent_modules, state)
|
||||
warnings = Enum.reverse(warnings, checker_warnings)
|
||||
{{:ok, result, warnings}, state}
|
||||
end
|
||||
|
||||
defp maybe_check_modules(result, runtime_modules, state) do
|
||||
%{profile: profile, checker: checker} = state
|
||||
|
||||
compiled_modules =
|
||||
for {{:module, _module}, {_binary, info}} <- result,
|
||||
do: info
|
||||
|
||||
runtime_modules =
|
||||
for module <- runtime_modules,
|
||||
path = :code.which(module),
|
||||
is_list(path) and path != [],
|
||||
do: {module, path}
|
||||
|
||||
profile_checker(profile, compiled_modules, runtime_modules, fn ->
|
||||
Module.ParallelChecker.verify(checker, compiled_modules, runtime_modules)
|
||||
end)
|
||||
end
|
||||
|
||||
defp profile_init(:time), do: {:time, System.monotonic_time(), 0}
|
||||
defp profile_init(nil), do: :none
|
||||
|
||||
defp profile_checker({:time, _, _}, compiled_modules, runtime_modules, fun) do
|
||||
{time, result} = :timer.tc(fun)
|
||||
time = div(time, 1000)
|
||||
num_modules = length(compiled_modules) + length(runtime_modules)
|
||||
IO.puts(:stderr, "[profile] Finished group pass check of #{num_modules} modules in #{time}ms")
|
||||
result
|
||||
end
|
||||
|
||||
defp profile_checker(:none, _compiled_modules, _runtime_modules, fun) do
|
||||
fun.()
|
||||
end
|
||||
|
||||
## Compiler worker spawning
|
||||
|
||||
# We already have n=schedulers currently running, don't spawn new ones
|
||||
defp spawn_workers(
|
||||
queue,
|
||||
spawned,
|
||||
waiting,
|
||||
files,
|
||||
result,
|
||||
warnings,
|
||||
%{schedulers: schedulers} = state
|
||||
)
|
||||
when spawned - length(waiting) >= schedulers do
|
||||
wait_for_messages(queue, spawned, waiting, files, result, warnings, state)
|
||||
defp spawn_workers(files, waiting, queued, result, warnings, %{schedulers: schedulers} = state)
|
||||
when length(queued) - length(waiting) >= schedulers do
|
||||
wait_for_messages(files, waiting, queued, result, warnings, state)
|
||||
end
|
||||
|
||||
# Release waiting processes
|
||||
defp spawn_workers([{ref, found} | t], spawned, waiting, files, result, warnings, state) do
|
||||
{files, waiting} =
|
||||
defp spawn_workers([{ref, found} | t], waiting, queued, result, warnings, state) do
|
||||
waiting =
|
||||
case List.keytake(waiting, ref, 2) do
|
||||
{{_kind, pid, ^ref, file_pid, _on, _defining, _deadlock}, waiting} ->
|
||||
{{_kind, pid, ^ref, _on, _defining}, waiting} ->
|
||||
send(pid, {ref, found})
|
||||
{update_timing(files, file_pid, :waiting), waiting}
|
||||
waiting
|
||||
|
||||
nil ->
|
||||
# In case the waiting process died (for example, it was an async process),
|
||||
# it will no longer be on the list. So we need to take it into account here.
|
||||
{files, waiting}
|
||||
waiting
|
||||
end
|
||||
|
||||
spawn_workers(t, spawned, waiting, files, result, warnings, state)
|
||||
spawn_workers(t, waiting, queued, result, warnings, state)
|
||||
end
|
||||
|
||||
defp spawn_workers([file | queue], spawned, waiting, files, result, warnings, state) do
|
||||
%{output: output, dest: dest, checker: checker} = state
|
||||
defp spawn_workers([file | files], waiting, queued, result, warnings, state) do
|
||||
%{output: output, long_compilation_threshold: threshold, dest: dest} = state
|
||||
parent = self()
|
||||
file = Path.expand(file)
|
||||
|
||||
{pid, ref} =
|
||||
:erlang.spawn_monitor(fn ->
|
||||
Module.ParallelChecker.put(parent, checker)
|
||||
:erlang.put(:elixir_compiler_info, {parent, self()})
|
||||
:erlang.put(:elixir_compiler_pid, parent)
|
||||
:erlang.put(:elixir_compiler_file, file)
|
||||
|
||||
try do
|
||||
case output do
|
||||
{:compile, path} -> compile_file(file, path, parent)
|
||||
:compile -> compile_file(file, dest, parent)
|
||||
:require -> require_file(file, parent)
|
||||
end
|
||||
catch
|
||||
kind, reason ->
|
||||
send(parent, {:file_error, self(), file, {kind, reason, __STACKTRACE__}})
|
||||
end
|
||||
result =
|
||||
try do
|
||||
_ =
|
||||
case output do
|
||||
{:compile, path} ->
|
||||
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
|
||||
:erlang.put(:elixir_compiler_dest, path)
|
||||
:elixir_compiler.file_to_path(Path.expand(file), path)
|
||||
|
||||
:compile ->
|
||||
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
|
||||
:erlang.put(:elixir_compiler_dest, dest)
|
||||
Code.compile_file(file)
|
||||
|
||||
:require ->
|
||||
Code.require_file(file)
|
||||
end
|
||||
|
||||
:ok
|
||||
catch
|
||||
kind, reason ->
|
||||
{kind, reason, __STACKTRACE__}
|
||||
end
|
||||
|
||||
send(parent, {:file_done, self(), file, result})
|
||||
exit(:shutdown)
|
||||
end)
|
||||
|
||||
file_data = %{
|
||||
pid: pid,
|
||||
ref: ref,
|
||||
file: file,
|
||||
timestamp: System.monotonic_time(),
|
||||
compiling: 0,
|
||||
waiting: 0,
|
||||
warned: false
|
||||
}
|
||||
|
||||
files = [file_data | files]
|
||||
spawn_workers(queue, spawned + 1, waiting, files, result, warnings, state)
|
||||
timer_ref = Process.send_after(self(), {:timed_out, pid}, threshold * 1000)
|
||||
queued = [{pid, ref, file, timer_ref} | queued]
|
||||
spawn_workers(files, waiting, queued, result, warnings, state)
|
||||
end
|
||||
|
||||
# No more queue, nothing waiting, this cycle is done
|
||||
defp spawn_workers([], 0, [], [], result, warnings, state) do
|
||||
cycle_return = each_cycle_return(state.each_cycle.())
|
||||
state = cycle_timing(result, state)
|
||||
# No more files, nothing waiting, queue is empty, this cycle is done
|
||||
defp spawn_workers([], [], [], result, warnings, state) do
|
||||
case state.each_cycle.() do
|
||||
[] ->
|
||||
modules = for {:module, mod} <- result, do: mod
|
||||
warnings = Enum.reverse(warnings)
|
||||
{:ok, modules, warnings}
|
||||
|
||||
case cycle_return do
|
||||
{:runtime, dependent_modules, extra_warnings} ->
|
||||
verify_modules(result, extra_warnings ++ warnings, dependent_modules, state)
|
||||
|
||||
{:compile, [], extra_warnings} ->
|
||||
verify_modules(result, extra_warnings ++ warnings, [], state)
|
||||
|
||||
{:compile, more, extra_warnings} ->
|
||||
spawn_workers(more, 0, [], [], result, extra_warnings ++ warnings, state)
|
||||
more ->
|
||||
spawn_workers(more, [], [], result, warnings, state)
|
||||
end
|
||||
end
|
||||
|
||||
# files x, waiting for x: POSSIBLE ERROR! Release processes so we get the failures
|
||||
# Queued x, waiting for x: POSSIBLE ERROR! Release processes so we get the failures
|
||||
|
||||
# Single entry, just release it.
|
||||
defp spawn_workers(
|
||||
[],
|
||||
1,
|
||||
[{_, pid, ref, _, _, _, _}] = waiting,
|
||||
[%{pid: pid}] = files,
|
||||
result,
|
||||
warnings,
|
||||
state
|
||||
) do
|
||||
spawn_workers([{ref, :not_found}], 1, waiting, files, result, warnings, state)
|
||||
defp spawn_workers([], [_] = waiting, [_] = queued, result, warnings, state) do
|
||||
[{_, _, ref, _, _}] = waiting
|
||||
spawn_workers([{ref, :not_found}], waiting, queued, result, warnings, state)
|
||||
end
|
||||
|
||||
# Multiple entries, try to release modules.
|
||||
defp spawn_workers([], spawned, waiting, files, result, warnings, state)
|
||||
when length(waiting) == spawned do
|
||||
# There is potentially a deadlock. We will release modules with
|
||||
# the following order:
|
||||
#
|
||||
# 1. Code.ensure_compiled/1 checks without a known definition (deadlock = soft)
|
||||
# 2. Code.ensure_compiled/1 checks with a known definition (deadlock = soft)
|
||||
# 3. Struct/import/require/ensure_compiled! checks without a known definition (deadlock = hard)
|
||||
# 4. Modules without a known definition
|
||||
# 5. Code invocation (deadlock = raise)
|
||||
#
|
||||
# The reason for step 3 and 4 is to not treat typos as deadlocks and
|
||||
# help developers handle those sooner. However, this can have false
|
||||
# positives in case multiple modules are defined in the same file
|
||||
# and the module we are waiting for is defined later on.
|
||||
#
|
||||
# Finally, note there is no difference between hard and raise, the
|
||||
# difference is where the raise is happening, inside the compiler
|
||||
# or in the caller.
|
||||
defp spawn_workers([], waiting, queued, result, warnings, state)
|
||||
when length(waiting) == length(queued) do
|
||||
# The goal of this function is to find leaves in the dependency graph,
|
||||
# i.e. to find code that depends on code that we know is not being defined.
|
||||
without_definition =
|
||||
for {pid, _, _, _} <- queued,
|
||||
entry = waiting_on_without_definition(waiting, pid),
|
||||
do: entry
|
||||
|
||||
deadlocked =
|
||||
deadlocked(waiting, :soft, false) ||
|
||||
deadlocked(waiting, :soft, true) || deadlocked(waiting, :hard, false) ||
|
||||
without_definition(waiting, files)
|
||||
# Note we only release modules because those can be rescued. A missing
|
||||
# struct is a guaranteed compile error, so we never release it and treat
|
||||
# it exclusively a missing entry/deadlock.
|
||||
pending =
|
||||
for {:module, _, ref, on, _} <- without_definition,
|
||||
do: {on, {ref, :not_found}}
|
||||
|
||||
if deadlocked do
|
||||
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, state)
|
||||
# Instead of releasing all files at once, we release them in groups
|
||||
# based on the module they are waiting on. We pick the module being
|
||||
# depended on with less edges, as it is the mostly likely source of
|
||||
# error (for example, someone made a typo). This may not always be
|
||||
# true though. For example, if there is a macro injecting code into
|
||||
# multiple modules and such code becomes faulty, now multiple modules
|
||||
# are waiting on the same module required by the faulty code. However,
|
||||
# since we need to pick something to be first, the one with fewer edges
|
||||
# sounds like a sane choice.
|
||||
pending
|
||||
|> Enum.group_by(&elem(&1, 0), &elem(&1, 1))
|
||||
|> Enum.sort_by(&length(elem(&1, 1)))
|
||||
|> case do
|
||||
[{_on, refs} | _] ->
|
||||
spawn_workers(refs, waiting, queued, result, warnings, state)
|
||||
|
||||
[] ->
|
||||
# There is a deadlock. Instead of printing a deadlock, let's release
|
||||
# structs, as a missing struct error is clearer than a deadlock one.
|
||||
structs = for {:struct, _, ref, _, _} <- without_definition, do: {ref, :not_found}
|
||||
|
||||
if structs != [] do
|
||||
spawn_workers(structs, waiting, queued, result, warnings, state)
|
||||
else
|
||||
errors = handle_deadlock(waiting, queued)
|
||||
{:error, errors, warnings}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# No more files, but queue and waiting are not full or do not match
|
||||
defp spawn_workers([], waiting, queued, result, warnings, state) do
|
||||
wait_for_messages([], waiting, queued, result, warnings, state)
|
||||
end
|
||||
|
||||
defp waiting_on_without_definition(waiting, pid) do
|
||||
{_, ^pid, _, on, _} = entry = List.keyfind(waiting, pid, 1)
|
||||
|
||||
if Enum.any?(waiting, fn {_, _, _, _, defining} -> on in defining end) do
|
||||
nil
|
||||
else
|
||||
errors = handle_deadlock(waiting, files)
|
||||
{{:error, errors, warnings}, state}
|
||||
entry
|
||||
end
|
||||
end
|
||||
|
||||
# No more queue, but spawned and length(waiting) do not match
|
||||
defp spawn_workers([], spawned, waiting, files, result, warnings, state) do
|
||||
wait_for_messages([], spawned, waiting, files, result, warnings, state)
|
||||
end
|
||||
|
||||
defp compile_file(file, path, parent) do
|
||||
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
|
||||
:erlang.put(:elixir_compiler_dest, path)
|
||||
:elixir_compiler.file(file, &each_file(&1, &2, parent))
|
||||
end
|
||||
|
||||
defp require_file(file, parent) do
|
||||
case :elixir_code_server.call({:acquire, file}) do
|
||||
:required ->
|
||||
send(parent, {:file_cancel, self()})
|
||||
|
||||
:proceed ->
|
||||
:elixir_compiler.file(file, &each_file(&1, &2, parent))
|
||||
:elixir_code_server.cast({:required, file})
|
||||
end
|
||||
end
|
||||
|
||||
defp cycle_timing(_result, %{profile: :none} = state) do
|
||||
state
|
||||
end
|
||||
|
||||
defp cycle_timing(result, %{profile: {:time, cycle_start, module_counter}} = state) do
|
||||
num_modules = count_modules(result)
|
||||
diff_modules = num_modules - module_counter
|
||||
now = System.monotonic_time()
|
||||
time = System.convert_time_unit(now - cycle_start, :native, :millisecond)
|
||||
|
||||
IO.puts(
|
||||
:stderr,
|
||||
"[profile] Finished compilation cycle of #{diff_modules} modules in #{time}ms"
|
||||
)
|
||||
|
||||
%{state | profile: {:time, now, num_modules}}
|
||||
end
|
||||
|
||||
defp count_modules(result) do
|
||||
Enum.count(result, &match?({{:module, _}, _}, &1))
|
||||
end
|
||||
|
||||
# TODO: Deprecate other returns on v1.14
|
||||
defp each_cycle_return({kind, modules, warnings}), do: {kind, modules, warnings}
|
||||
defp each_cycle_return({kind, modules}), do: {kind, modules, []}
|
||||
defp each_cycle_return(modules) when is_list(modules), do: {:compile, modules, []}
|
||||
|
||||
# The goal of this function is to find leaves in the dependency graph,
|
||||
# i.e. to find code that depends on code that we know is not being defined.
|
||||
# Note that not all files have been compiled yet, so they may not be in waiting.
|
||||
defp without_definition(waiting, files) do
|
||||
nillify_empty(
|
||||
for %{pid: pid} <- files,
|
||||
{_, _, ref, ^pid, on, _, _} <- waiting,
|
||||
not defining?(on, waiting),
|
||||
do: {ref, :not_found}
|
||||
)
|
||||
end
|
||||
|
||||
defp deadlocked(waiting, type, defining?) do
|
||||
nillify_empty(
|
||||
for {_, _, ref, _, on, _, ^type} <- waiting,
|
||||
defining?(on, waiting) == defining?,
|
||||
do: {ref, :deadlock}
|
||||
)
|
||||
end
|
||||
|
||||
defp defining?(on, waiting) do
|
||||
Enum.any?(waiting, fn {_, _, _, _, _, defining, _} -> on in defining end)
|
||||
end
|
||||
|
||||
defp nillify_empty([]), do: nil
|
||||
defp nillify_empty([_ | _] = list), do: list
|
||||
|
||||
# Wait for messages from child processes
|
||||
defp wait_for_messages(queue, spawned, waiting, files, result, warnings, state) do
|
||||
defp wait_for_messages(files, waiting, queued, result, warnings, state) do
|
||||
%{output: output} = state
|
||||
|
||||
receive do
|
||||
{:async, process} ->
|
||||
Process.monitor(process)
|
||||
wait_for_messages(queue, spawned + 1, waiting, files, result, warnings, state)
|
||||
|
||||
{:available, kind, module} ->
|
||||
{:struct_available, module} ->
|
||||
available =
|
||||
for {^kind, _, ref, _, ^module, _defining, _deadlock} <- waiting,
|
||||
for {:struct, _, ref, waiting_module, _defining} <- waiting,
|
||||
module == waiting_module,
|
||||
do: {ref, :found}
|
||||
|
||||
result = Map.put(result, {kind, module}, true)
|
||||
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
|
||||
result = [{:struct, module} | result]
|
||||
spawn_workers(available ++ files, waiting, queued, result, warnings, state)
|
||||
|
||||
{:module_available, child, ref, file, module, binary, checker_info} ->
|
||||
{:module_available, child, ref, file, module, binary} ->
|
||||
state.each_module.(file, module, binary)
|
||||
|
||||
# Release the module loader which is waiting for an ack
|
||||
send(child, {ref, :ack})
|
||||
|
||||
available =
|
||||
for {:module, _, ref, _, ^module, _defining, _deadlock} <- waiting,
|
||||
for {:module, _, ref, waiting_module, _defining} <- waiting,
|
||||
module == waiting_module,
|
||||
do: {ref, :found}
|
||||
|
||||
result = Map.put(result, {:module, module}, {binary, checker_info})
|
||||
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
|
||||
cancel_waiting_timer(queued, child)
|
||||
|
||||
result = [{:module, module} | result]
|
||||
spawn_workers(available ++ files, waiting, queued, result, warnings, state)
|
||||
|
||||
# If we are simply requiring files, we do not add to waiting.
|
||||
{:waiting, _kind, child, ref, _file_pid, _on, _defining, _deadlock} when output == :require ->
|
||||
{:waiting, _kind, child, ref, _on, _defining} when output == :require ->
|
||||
send(child, {ref, :not_found})
|
||||
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
|
||||
spawn_workers(files, waiting, queued, result, warnings, state)
|
||||
|
||||
{:waiting, kind, child_pid, ref, file_pid, on, defining, deadlock?} ->
|
||||
# If we already got what we were waiting for, do not put it on waiting.
|
||||
# If we're waiting on ourselves, send :found so that we can crash with
|
||||
# a better error.
|
||||
{files, waiting} =
|
||||
if Map.has_key?(result, {kind, on}) or on in defining do
|
||||
send(child_pid, {ref, :found})
|
||||
{files, waiting}
|
||||
{:waiting, kind, child, ref, on, defining} ->
|
||||
# Oops, we already got it, do not put it on waiting.
|
||||
# Alternatively, we're waiting on ourselves,
|
||||
# send :found so that we can crash with a better error.
|
||||
waiting =
|
||||
if :lists.any(&match?({^kind, ^on}, &1), result) or on in defining do
|
||||
send(child, {ref, :found})
|
||||
waiting
|
||||
else
|
||||
files = update_timing(files, file_pid, :compiling)
|
||||
{files, [{kind, child_pid, ref, file_pid, on, defining, deadlock?} | waiting]}
|
||||
[{kind, child, ref, on, defining} | waiting]
|
||||
end
|
||||
|
||||
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
|
||||
spawn_workers(files, waiting, queued, result, warnings, state)
|
||||
|
||||
:threshold_check ->
|
||||
files =
|
||||
for data <- files do
|
||||
if data.warned or List.keymember?(waiting, data.pid, 1) do
|
||||
data
|
||||
else
|
||||
data = update_timing(data, :compiling)
|
||||
data = maybe_warn_long_compilation(data, state)
|
||||
data
|
||||
end
|
||||
end
|
||||
{:timed_out, child} ->
|
||||
case List.keyfind(queued, child, 0) do
|
||||
{^child, _, file, _} ->
|
||||
state.each_long_compilation.(file)
|
||||
|
||||
timer_ref = Process.send_after(self(), :threshold_check, state.long_compilation_threshold)
|
||||
state = %{state | timer_ref: timer_ref}
|
||||
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
|
||||
_ ->
|
||||
:ok
|
||||
end
|
||||
|
||||
{:warning, file, location, message} ->
|
||||
spawn_workers(files, waiting, queued, result, warnings, state)
|
||||
|
||||
{:warning, file, line, message} ->
|
||||
file = file && Path.absname(file)
|
||||
message = :unicode.characters_to_binary(message)
|
||||
warning = {file, location, message}
|
||||
wait_for_messages(queue, spawned, waiting, files, result, [warning | warnings], state)
|
||||
|
||||
{:file_ok, child_pid, ref, file, lexical} ->
|
||||
state.each_file.(file, lexical)
|
||||
send(child_pid, ref)
|
||||
warning = {file, line, message}
|
||||
wait_for_messages(files, waiting, queued, result, [warning | warnings], state)
|
||||
|
||||
{:file_done, child_pid, file, :ok} ->
|
||||
discard_down(child_pid)
|
||||
new_files = discard_and_maybe_log_file(files, child_pid, state)
|
||||
state.each_file.(file)
|
||||
cancel_waiting_timer(queued, child_pid)
|
||||
|
||||
# Sometimes we may have spurious entries in the waiting list
|
||||
# because someone invoked try/rescue UndefinedFunctionError
|
||||
# Sometimes we may have spurious entries in the waiting
|
||||
# list because someone invoked try/rescue UndefinedFunctionError
|
||||
new_files = List.delete(files, child_pid)
|
||||
new_queued = List.keydelete(queued, child_pid, 0)
|
||||
new_waiting = List.keydelete(waiting, child_pid, 1)
|
||||
spawn_workers(queue, spawned - 1, new_waiting, new_files, result, warnings, state)
|
||||
spawn_workers(new_files, new_waiting, new_queued, result, warnings, state)
|
||||
|
||||
{:file_cancel, child_pid} ->
|
||||
{:file_done, child_pid, file, {kind, reason, stack}} ->
|
||||
discard_down(child_pid)
|
||||
new_files = Enum.reject(files, &(&1.pid == child_pid))
|
||||
spawn_workers(queue, spawned - 1, waiting, new_files, result, warnings, state)
|
||||
|
||||
{:file_error, child_pid, file, {kind, reason, stack}} ->
|
||||
print_error(file, kind, reason, stack)
|
||||
discard_down(child_pid)
|
||||
files |> Enum.reject(&(&1.pid == child_pid)) |> terminate()
|
||||
{{:error, [to_error(file, kind, reason, stack)], warnings}, state}
|
||||
cancel_waiting_timer(queued, child_pid)
|
||||
|
||||
{:DOWN, ref, :process, pid, reason} ->
|
||||
waiting = List.keydelete(waiting, pid, 1)
|
||||
queued
|
||||
|> List.keydelete(child_pid, 0)
|
||||
|> terminate()
|
||||
|
||||
case handle_down(files, ref, reason) do
|
||||
:ok -> wait_for_messages(queue, spawned - 1, waiting, files, result, warnings, state)
|
||||
{:error, errors} -> {{:error, errors, warnings}, state}
|
||||
{:error, [to_error(file, kind, reason, stack)], warnings}
|
||||
|
||||
{:DOWN, ref, :process, _pid, reason} ->
|
||||
case handle_down(queued, ref, reason) do
|
||||
:ok -> wait_for_messages(files, waiting, queued, result, warnings, state)
|
||||
{:error, errors} -> {:error, errors, warnings}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp update_timing(files, pid, key) do
|
||||
Enum.map(files, fn data ->
|
||||
if data.pid == pid do
|
||||
time = System.monotonic_time()
|
||||
%{data | key => data[key] + time - data.timestamp, timestamp: time}
|
||||
else
|
||||
data
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp update_timing(data, key) do
|
||||
time = System.monotonic_time()
|
||||
%{data | key => data[key] + time - data.timestamp, timestamp: time}
|
||||
end
|
||||
|
||||
defp maybe_warn_long_compilation(data, state) do
|
||||
compiling = System.convert_time_unit(data.compiling, :native, :millisecond)
|
||||
|
||||
if not data.warned and compiling >= state.long_compilation_threshold do
|
||||
state.each_long_compilation.(data.file)
|
||||
%{data | warned: true}
|
||||
else
|
||||
data
|
||||
end
|
||||
end
|
||||
|
||||
defp discard_and_maybe_log_file(files, pid, state) do
|
||||
Enum.reject(files, fn data ->
|
||||
if data.pid == pid do
|
||||
data = update_timing(data, :compiling)
|
||||
data = maybe_warn_long_compilation(data, state)
|
||||
|
||||
if state.profile != :none do
|
||||
compiling = to_padded_ms(data.compiling)
|
||||
waiting = to_padded_ms(data.waiting)
|
||||
relative = Path.relative_to_cwd(data.file)
|
||||
|
||||
IO.puts(
|
||||
:stderr,
|
||||
"[profile] #{compiling}ms compiling + #{waiting}ms waiting for #{relative}"
|
||||
)
|
||||
end
|
||||
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp to_padded_ms(time) do
|
||||
time
|
||||
|> System.convert_time_unit(:native, :millisecond)
|
||||
|> Integer.to_string()
|
||||
|> String.pad_leading(6, " ")
|
||||
end
|
||||
|
||||
defp discard_down(pid) do
|
||||
receive do
|
||||
{:DOWN, _, :process, ^pid, _} -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_down(_files, _ref, :normal) do
|
||||
defp handle_down(_queued, _ref, :normal) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp handle_down(files, ref, reason) do
|
||||
case Enum.find(files, &(&1.ref == ref)) do
|
||||
%{pid: pid, file: file} ->
|
||||
defp handle_down(queued, ref, reason) do
|
||||
case List.keyfind(queued, ref, 1) do
|
||||
{child_pid, ^ref, file, _timer_ref} ->
|
||||
print_error(file, :exit, reason, [])
|
||||
files |> Enum.reject(&(&1.pid == pid)) |> terminate()
|
||||
|
||||
queued
|
||||
|> List.keydelete(child_pid, 0)
|
||||
|> terminate()
|
||||
|
||||
{:error, [to_error(file, :exit, reason, [])]}
|
||||
|
||||
nil ->
|
||||
_ ->
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_deadlock(waiting, files) do
|
||||
defp handle_deadlock(waiting, queued) do
|
||||
deadlock =
|
||||
for %{pid: pid, file: file} <- files do
|
||||
for {pid, _, file, _} <- queued do
|
||||
{:current_stacktrace, stacktrace} = Process.info(pid, :current_stacktrace)
|
||||
Process.exit(pid, :kill)
|
||||
|
||||
{kind, ^pid, _, _, on, _, _} = List.keyfind(waiting, pid, 1)
|
||||
{kind, ^pid, _, on, _} = List.keyfind(waiting, pid, 1)
|
||||
description = "deadlocked waiting on #{kind} #{inspect(on)}"
|
||||
error = CompileError.exception(description: description, file: nil, line: nil)
|
||||
print_error(file, :error, error, stacktrace)
|
||||
{Path.relative_to_cwd(file), on, description}
|
||||
|
||||
{file, on, description}
|
||||
end
|
||||
|
||||
IO.puts("""
|
||||
@@ -732,9 +469,9 @@ defmodule Kernel.ParallelCompiler do
|
||||
for {file, _, description} <- deadlock, do: {Path.absname(file), nil, description}
|
||||
end
|
||||
|
||||
defp terminate(files) do
|
||||
for %{pid: pid} <- files, do: Process.exit(pid, :kill)
|
||||
for %{pid: pid} <- files, do: discard_down(pid)
|
||||
defp terminate(queued) do
|
||||
for {pid, _, _, _} <- queued, do: Process.exit(pid, :kill)
|
||||
for {pid, _, _, _} <- queued, do: discard_down(pid)
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -745,11 +482,28 @@ defmodule Kernel.ParallelCompiler do
|
||||
])
|
||||
end
|
||||
|
||||
defp cancel_waiting_timer(queued, child_pid) do
|
||||
case List.keyfind(queued, child_pid, 0) do
|
||||
{^child_pid, _ref, _file, timer_ref} ->
|
||||
Process.cancel_timer(timer_ref)
|
||||
# Let's flush the message in case it arrived before we canceled the
|
||||
# timeout.
|
||||
receive do
|
||||
{:timed_out, ^child_pid} -> :ok
|
||||
after
|
||||
0 -> :ok
|
||||
end
|
||||
|
||||
nil ->
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
defp to_error(file, kind, reason, stack) do
|
||||
line = get_line(file, reason, stack)
|
||||
file = Path.absname(file)
|
||||
message = :unicode.characters_to_binary(Kernel.CLI.format_error(kind, reason, stack))
|
||||
{file, line || 0, message}
|
||||
{file, line, message}
|
||||
end
|
||||
|
||||
defp get_line(_file, %{line: line}, _stack) when is_integer(line) and line > 0 do
|
||||
@@ -762,12 +516,6 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
end
|
||||
|
||||
defp get_line(file, _reason, [{_, _, _, [file: 'expanding macro']}, {_, _, _, info} | _]) do
|
||||
if Keyword.get(info, :file) == to_charlist(Path.relative_to_cwd(file)) do
|
||||
Keyword.get(info, :line)
|
||||
end
|
||||
end
|
||||
|
||||
defp get_line(file, _reason, [{_, _, _, info} | _]) do
|
||||
if Keyword.get(info, :file) == to_charlist(Path.relative_to_cwd(file)) do
|
||||
Keyword.get(info, :line)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
defmodule Kernel.ParallelRequire do
|
||||
# TODO: Remove on 2.0
|
||||
@moduledoc false
|
||||
|
||||
@deprecated "Use Kernel.ParallelCompiler.require/2 instead"
|
||||
|
||||
@@ -3,21 +3,14 @@ defmodule Kernel.SpecialForms do
|
||||
Special forms are the basic building blocks of Elixir, and therefore
|
||||
cannot be overridden by the developer.
|
||||
|
||||
The `Kernel.SpecialForms` module consists solely of macros that can be
|
||||
invoked anywhere in Elixir code without the use of the
|
||||
`Kernel.SpecialForms.` prefix. This is possible because they all have
|
||||
been automatically imported, in the same fashion as the functions and
|
||||
macros from the `Kernel` module.
|
||||
|
||||
These building blocks are defined in this module. Some of these special forms are lexical (such as
|
||||
`alias/2` and `case/2`). The macros `{}/1` and `<<>>/1` are also special
|
||||
We define them in this module. Some of these forms are lexical (like
|
||||
`alias/2`, `case/2`, etc.). The macros `{}/1` and `<<>>/1` are also special
|
||||
forms used to define tuple and binary data structures respectively.
|
||||
|
||||
This module also documents macros that return information about Elixir's
|
||||
compilation environment, such as (`__ENV__/0`, `__MODULE__/0`, `__DIR__/0`,
|
||||
`__STACKTRACE__/0`, and `__CALLER__/0`).
|
||||
compilation environment, such as (`__ENV__/0`, `__MODULE__/0`, `__DIR__/0` and `__CALLER__/0`).
|
||||
|
||||
Additionally, it documents two special forms, `__block__/1` and
|
||||
Finally, it also documents two special forms, `__block__/1` and
|
||||
`__aliases__/1`, which are not intended to be called directly by the
|
||||
developer but they appear in quoted contents since they are essential
|
||||
in Elixir's constructs.
|
||||
@@ -44,7 +37,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
## AST representation
|
||||
|
||||
Only two-element tuples are considered literals in Elixir and return themselves
|
||||
Only two-item tuples are considered literals in Elixir and return themselves
|
||||
when quoted. Therefore, all other tuples are represented in the AST as calls to
|
||||
the `:{}` special form.
|
||||
|
||||
@@ -187,13 +180,19 @@ defmodule Kernel.SpecialForms do
|
||||
iex> <<0, "foo">>
|
||||
<<0, 102, 111, 111>>
|
||||
|
||||
Binaries need to be explicitly tagged as `binary`:
|
||||
Variables or any other type need to be explicitly tagged:
|
||||
|
||||
iex> rest = "oo"
|
||||
iex> <<102, rest>>
|
||||
** (ArgumentError) argument error
|
||||
|
||||
We can solve this by explicitly tagging it as `binary`:
|
||||
|
||||
iex> rest = "oo"
|
||||
iex> <<102, rest::binary>>
|
||||
"foo"
|
||||
|
||||
The `utf8`, `utf16`, and `utf32` types are for Unicode code points. They
|
||||
The `utf8`, `utf16`, and `utf32` types are for Unicode codepoints. They
|
||||
can also be applied to literal strings and charlists:
|
||||
|
||||
iex> <<"foo"::utf16>>
|
||||
@@ -201,12 +200,6 @@ defmodule Kernel.SpecialForms do
|
||||
iex> <<"foo"::utf32>>
|
||||
<<0, 0, 0, 102, 0, 0, 0, 111, 0, 0, 0, 111>>
|
||||
|
||||
Otherwise we get an `ArgumentError` when constructing the binary:
|
||||
|
||||
rest = "oo"
|
||||
<<102, rest>>
|
||||
** (ArgumentError) argument error
|
||||
|
||||
## Options
|
||||
|
||||
Many options can be given by using `-` as separator. Order is
|
||||
@@ -233,9 +226,9 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
Sizes for types are a bit more nuanced. The default size for integers is 8.
|
||||
|
||||
For floats, it is 64. For floats, `size * unit` must result in 16, 32, or 64,
|
||||
For floats, it is 64. For floats, `size * unit` must result in 32 or 64,
|
||||
corresponding to [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point)
|
||||
binary16, binary32, and binary64, respectively.
|
||||
binary32 and binary64, respectively.
|
||||
|
||||
For binaries, the default is the size of the binary. Only the last binary in a
|
||||
match can use the default size. All others must have their size specified
|
||||
@@ -351,9 +344,9 @@ defmodule Kernel.SpecialForms do
|
||||
13::size(8), 10::size(8), 26::size(8), 10::size(8)>>
|
||||
@jpg_signature <<255::size(8), 216::size(8)>>
|
||||
|
||||
def type(<<@png_signature, _rest::binary>>), do: :png
|
||||
def type(<<@jpg_signature, _rest::binary>>), do: :jpg
|
||||
def type(_), do: :unknown
|
||||
def type(<<@png_signature, rest::binary>>), do: :png
|
||||
def type(<<@jpg_signature, rest::binary>>), do: :jpg
|
||||
def type(_), do :unknown
|
||||
end
|
||||
|
||||
### Performance & Optimizations
|
||||
@@ -365,13 +358,13 @@ defmodule Kernel.SpecialForms do
|
||||
ERL_COMPILER_OPTIONS=bin_opt_info mix compile
|
||||
|
||||
To learn more about specific optimizations and performance considerations,
|
||||
check out the
|
||||
["Constructing and matching binaries" chapter of the Erlang's Efficiency Guide](https://erlang.org/doc/efficiency_guide/binaryhandling.html).
|
||||
check out
|
||||
[Erlang's Efficiency Guide on handling binaries](http://www.erlang.org/doc/efficiency_guide/binaryhandling.html).
|
||||
"""
|
||||
defmacro unquote(:<<>>)(args), do: error!([args])
|
||||
|
||||
@doc """
|
||||
Dot operator. Defines a remote call, a call to an anonymous function, or an alias.
|
||||
Defines a remote call, a call to an anonymous function, or an alias.
|
||||
|
||||
The dot (`.`) in Elixir can be used for remote calls:
|
||||
|
||||
@@ -438,7 +431,7 @@ defmodule Kernel.SpecialForms do
|
||||
...> end
|
||||
{{:., [], [{:__aliases__, [alias: false], [:String]}, :downcase]}, [], ["FOO"]}
|
||||
|
||||
Note that we have an inner tuple, containing the atom `:.` representing
|
||||
Notice we have an inner tuple, containing the atom `:.` representing
|
||||
the dot as first element:
|
||||
|
||||
{:., [], [{:__aliases__, [alias: false], [:String]}, :downcase]}
|
||||
@@ -493,11 +486,11 @@ defmodule Kernel.SpecialForms do
|
||||
defmacro unquote(:.)(left, right), do: error!([left, right])
|
||||
|
||||
@doc """
|
||||
`alias/2` is used to set up aliases, often useful with modules' names.
|
||||
`alias/2` is used to setup aliases, often useful with modules names.
|
||||
|
||||
## Examples
|
||||
|
||||
`alias/2` can be used to set up an alias for any module:
|
||||
`alias/2` can be used to setup an alias for any module:
|
||||
|
||||
defmodule Math do
|
||||
alias MyKeyword, as: Keyword
|
||||
@@ -510,10 +503,10 @@ defmodule Kernel.SpecialForms do
|
||||
In case one wants to access the original `Keyword`, it can be done
|
||||
by accessing `Elixir`:
|
||||
|
||||
Keyword.values #=> uses MyKeyword.values
|
||||
Keyword.values #=> uses MyKeyword.values
|
||||
Elixir.Keyword.values #=> uses Keyword.values
|
||||
|
||||
Note that calling `alias` without the `:as` option automatically
|
||||
Notice that calling `alias` without the `:as` option automatically
|
||||
sets an alias based on the last part of the module. For example:
|
||||
|
||||
alias Foo.Bar.Baz
|
||||
@@ -549,7 +542,6 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
Both warning behaviours could be changed by explicitly
|
||||
setting the `:warn` option to `true` or `false`.
|
||||
|
||||
"""
|
||||
defmacro alias(module, opts), do: error!([module, opts])
|
||||
|
||||
@@ -584,7 +576,7 @@ defmodule Kernel.SpecialForms do
|
||||
Imports functions and macros from other modules.
|
||||
|
||||
`import/2` allows one to easily access functions or macros from
|
||||
other modules without using the qualified name.
|
||||
others modules without using the qualified name.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -599,7 +591,7 @@ defmodule Kernel.SpecialForms do
|
||||
## Selector
|
||||
|
||||
By default, Elixir imports functions and macros from the given
|
||||
module, except the ones starting with an underscore (which are
|
||||
module, except the ones starting with underscore (which are
|
||||
usually callbacks):
|
||||
|
||||
import List
|
||||
@@ -617,11 +609,9 @@ defmodule Kernel.SpecialForms do
|
||||
import List, only: [flatten: 1]
|
||||
import String, except: [split: 2]
|
||||
|
||||
Importing the same module again will erase the previous imports,
|
||||
except when the `except` option is used, which is always exclusive
|
||||
on a previously declared `import/2`. If there is no previous import,
|
||||
then it applies to all functions and macros in the module. For
|
||||
example:
|
||||
Notice that calling `except` is always exclusive on a previously
|
||||
declared `import/2`. If there is no previous import, then it applies
|
||||
to all functions and macros in the module. For example:
|
||||
|
||||
import List, only: [flatten: 1, keyfind: 4]
|
||||
import List, except: [flatten: 1]
|
||||
@@ -639,7 +629,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
## Lexical scope
|
||||
|
||||
It is important to note that `import/2` is lexical. This means you
|
||||
It is important to notice that `import/2` is lexical. This means you
|
||||
can import specific macros inside specific functions:
|
||||
|
||||
defmodule Math do
|
||||
@@ -723,11 +713,10 @@ defmodule Kernel.SpecialForms do
|
||||
To retrieve the stacktrace of the current process, use
|
||||
`Process.info(self(), :current_stacktrace)` instead.
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
defmacro __STACKTRACE__, do: error!([])
|
||||
|
||||
@doc """
|
||||
Pin operator. Accesses an already bound variable in match clauses.
|
||||
Accesses an already bound variable in match clauses. Also known as the pin operator.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -759,12 +748,12 @@ defmodule Kernel.SpecialForms do
|
||||
defmacro ^var, do: error!([var])
|
||||
|
||||
@doc """
|
||||
Match operator. Matches the value on the right against the pattern on the left.
|
||||
Matches the value on the right against the pattern on the left.
|
||||
"""
|
||||
defmacro left = right, do: error!([left, right])
|
||||
|
||||
@doc """
|
||||
Type operator. Used by types and bitstrings to specify types.
|
||||
Used by types and bitstrings to specify types.
|
||||
|
||||
This operator is used in two distinct occasions in Elixir.
|
||||
It is used in typespecs to specify the type of a variable,
|
||||
@@ -808,7 +797,7 @@ defmodule Kernel.SpecialForms do
|
||||
* The first element of the tuple is always an atom or
|
||||
another tuple in the same representation.
|
||||
|
||||
* The second element of the tuple represents [metadata](`t:Macro.metadata/0`).
|
||||
* The second element of the tuple represents metadata.
|
||||
|
||||
* The third element of the tuple are the arguments for the
|
||||
function call. The third argument may be an atom, which is
|
||||
@@ -830,22 +819,6 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
## Options
|
||||
|
||||
* `:bind_quoted` - passes a binding to the macro. Whenever a binding is
|
||||
given, `unquote/1` is automatically disabled.
|
||||
|
||||
* `:context` - sets the resolution context.
|
||||
|
||||
* `:generated` - marks the given chunk as generated so it does not emit warnings.
|
||||
Currently it only works on special forms (for example, you can annotate a `case`
|
||||
but not an `if`).
|
||||
|
||||
* `:file` - sets the quoted expressions to have the given file.
|
||||
|
||||
* `:line` - sets the quoted expressions to have the given line.
|
||||
|
||||
* `:location` - when set to `:keep`, keeps the current line and file from
|
||||
quote. Read the "Stacktrace information" section below for more information.
|
||||
|
||||
* `:unquote` - when `false`, disables unquoting. This means any `unquote`
|
||||
call will be kept as is in the AST, instead of replaced by the `unquote`
|
||||
arguments. For example:
|
||||
@@ -860,11 +833,26 @@ defmodule Kernel.SpecialForms do
|
||||
...> end
|
||||
{:unquote, [], ["hello"]}
|
||||
|
||||
* `:location` - when set to `:keep`, keeps the current line and file from
|
||||
quote. Read the Stacktrace information section below for more
|
||||
information.
|
||||
|
||||
* `:line` - sets the quoted expressions to have the given line.
|
||||
|
||||
* `:generated` - marks the given chunk as generated so it does not emit warnings.
|
||||
Currently it only works on special forms (for example, you can annotate a `case`
|
||||
but not an `if`).
|
||||
|
||||
* `:context` - sets the resolution context.
|
||||
|
||||
* `:bind_quoted` - passes a binding to the macro. Whenever a binding is
|
||||
given, `unquote/1` is automatically disabled.
|
||||
|
||||
## Quote and macros
|
||||
|
||||
`quote/2` is commonly used with macros for code generation. As an exercise,
|
||||
let's define a macro that multiplies a number by itself (squared). In practice,
|
||||
there is no reason to define such a macro (and it would actually be
|
||||
there is no reason to define such as a macro (and it would actually be
|
||||
seen as a bad practice), but it is simple enough that it allows us to focus
|
||||
on the important aspects of quotes and macros:
|
||||
|
||||
@@ -879,7 +867,7 @@ defmodule Kernel.SpecialForms do
|
||||
We can invoke it as:
|
||||
|
||||
import Math
|
||||
IO.puts("Got #{squared(5)}")
|
||||
IO.puts "Got #{squared(5)}"
|
||||
|
||||
At first, there is nothing in this example that actually reveals it is a
|
||||
macro. But what is happening is that, at compilation time, `squared(5)`
|
||||
@@ -889,10 +877,10 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
import Math
|
||||
my_number = fn ->
|
||||
IO.puts("Returning 5")
|
||||
IO.puts "Returning 5"
|
||||
5
|
||||
end
|
||||
IO.puts("Got #{squared(my_number.())}")
|
||||
IO.puts "Got #{squared(my_number.())}"
|
||||
|
||||
The example above will print:
|
||||
|
||||
@@ -958,8 +946,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
import Math
|
||||
squared(5)
|
||||
x
|
||||
** (CompileError) undefined variable x or undefined function x/0
|
||||
x #=> ** (CompileError) undefined variable x or undefined function x/0
|
||||
|
||||
We can see that `x` did not leak to the user context. This happens
|
||||
because Elixir macros are hygienic, a topic we will discuss at length
|
||||
@@ -980,9 +967,8 @@ defmodule Kernel.SpecialForms do
|
||||
require Hygiene
|
||||
|
||||
a = 10
|
||||
Hygiene.no_interference()
|
||||
a
|
||||
#=> 10
|
||||
Hygiene.no_interference
|
||||
a #=> 10
|
||||
|
||||
In the example above, `a` returns 10 even if the macro
|
||||
is apparently setting it to 1 because variables defined
|
||||
@@ -1001,9 +987,8 @@ defmodule Kernel.SpecialForms do
|
||||
require NoHygiene
|
||||
|
||||
a = 10
|
||||
NoHygiene.interference()
|
||||
a
|
||||
#=> 1
|
||||
NoHygiene.interference
|
||||
a #=> 1
|
||||
|
||||
You cannot even access variables defined in the same module unless
|
||||
you explicitly give it a context:
|
||||
@@ -1022,9 +1007,9 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
Hygiene.write()
|
||||
Hygiene.read()
|
||||
** (RuntimeError) undefined variable a or undefined function a/0
|
||||
Hygiene.write
|
||||
Hygiene.read
|
||||
#=> ** (RuntimeError) undefined variable a or undefined function a/0
|
||||
|
||||
For such, you can explicitly pass the current module scope as
|
||||
argument:
|
||||
@@ -1043,8 +1028,8 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
ContextHygiene.write()
|
||||
ContextHygiene.read()
|
||||
ContextHygiene.write
|
||||
ContextHygiene.read
|
||||
#=> 1
|
||||
|
||||
## Hygiene in aliases
|
||||
@@ -1057,16 +1042,15 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
defmacro no_interference do
|
||||
quote do
|
||||
M.new()
|
||||
M.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require Hygiene
|
||||
Hygiene.no_interference()
|
||||
#=> %{}
|
||||
Hygiene.no_interference #=> %{}
|
||||
|
||||
Note that, even though the alias `M` is not available
|
||||
Notice that, even though the alias `M` is not available
|
||||
in the context the macro is expanded, the code above works
|
||||
because `M` still expands to `Map`.
|
||||
|
||||
@@ -1078,32 +1062,31 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
defmacro no_interference do
|
||||
quote do
|
||||
M.new()
|
||||
M.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require Hygiene
|
||||
alias SomethingElse, as: M
|
||||
Hygiene.no_interference()
|
||||
#=> %{}
|
||||
Hygiene.no_interference #=> %{}
|
||||
|
||||
In some cases, you want to access an alias or a module defined
|
||||
in the caller. For such, you can use the `alias!` macro:
|
||||
|
||||
defmodule Hygiene do
|
||||
# This will expand to Elixir.Nested.hello()
|
||||
# This will expand to Elixir.Nested.hello
|
||||
defmacro no_interference do
|
||||
quote do
|
||||
Nested.hello()
|
||||
Nested.hello
|
||||
end
|
||||
end
|
||||
|
||||
# This will expand to Nested.hello() for
|
||||
# This will expand to Nested.hello for
|
||||
# whatever is Nested in the caller
|
||||
defmacro interference do
|
||||
quote do
|
||||
alias!(Nested).hello()
|
||||
alias!(Nested).hello
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1114,10 +1097,10 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
|
||||
require Hygiene
|
||||
Hygiene.no_interference()
|
||||
** (UndefinedFunctionError) ...
|
||||
Hygiene.no_interference
|
||||
#=> ** (UndefinedFunctionError) ...
|
||||
|
||||
Hygiene.interference()
|
||||
Hygiene.interference
|
||||
#=> "world"
|
||||
end
|
||||
|
||||
@@ -1139,8 +1122,7 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
Hygiene.return_length()
|
||||
#=> 3
|
||||
Hygiene.return_length #=> 3
|
||||
|
||||
Notice how `Hygiene.return_length/0` returns `3` even though the `Kernel.length/1`
|
||||
function is not imported. In fact, even if `return_length/0`
|
||||
@@ -1175,8 +1157,7 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
Lazy.return_length()
|
||||
#=> 5
|
||||
Lazy.return_length #=> 5
|
||||
|
||||
## Stacktrace information
|
||||
|
||||
@@ -1202,8 +1183,8 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
require Sample
|
||||
Sample.add(:one, :two)
|
||||
** (ArithmeticError) bad argument in arithmetic expression
|
||||
adder.ex:5: Sample.add/2
|
||||
#=> ** (ArithmeticError) bad argument in arithmetic expression
|
||||
#=> adder.ex:5: Sample.add/2
|
||||
|
||||
When using `location: :keep` and invalid arguments are given to
|
||||
`Sample.add/2`, the stacktrace information will point to the file
|
||||
@@ -1211,33 +1192,27 @@ defmodule Kernel.SpecialForms do
|
||||
reported to where `defadd` was invoked. `location: :keep` affects
|
||||
only definitions inside the quote.
|
||||
|
||||
> **Important:** do not use location: :keep if the function definition
|
||||
> also `unquote`s some of the macro arguments. If you do so, Elixir
|
||||
> will store the file definition of the current location but the
|
||||
> unquoted arguments may contain line information of the macro caller,
|
||||
> leading to erroneous stacktraces.
|
||||
|
||||
## Binding and unquote fragments
|
||||
|
||||
Elixir quote/unquote mechanisms provide a functionality called
|
||||
Elixir quote/unquote mechanisms provides a functionality called
|
||||
unquote fragments. Unquote fragments provide an easy way to generate
|
||||
functions on the fly. Consider this example:
|
||||
|
||||
kv = [foo: 1, bar: 2]
|
||||
Enum.each(kv, fn {k, v} ->
|
||||
Enum.each kv, fn {k, v} ->
|
||||
def unquote(k)(), do: unquote(v)
|
||||
end)
|
||||
end
|
||||
|
||||
In the example above, we have generated the functions `foo/0` and
|
||||
`bar/0` dynamically. Now, imagine that we want to convert this
|
||||
`bar/0` dynamically. Now, imagine that, we want to convert this
|
||||
functionality into a macro:
|
||||
|
||||
defmacro defkv(kv) do
|
||||
Enum.map(kv, fn {k, v} ->
|
||||
Enum.map kv, fn {k, v} ->
|
||||
quote do
|
||||
def unquote(k)(), do: unquote(v)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
We can invoke this macro as:
|
||||
@@ -1260,9 +1235,9 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
defmacro defkv(kv) do
|
||||
quote do
|
||||
Enum.each(unquote(kv), fn {k, v} ->
|
||||
Enum.each unquote(kv), fn {k, v} ->
|
||||
def unquote(k)(), do: unquote(v)
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1281,9 +1256,9 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
defmacro defkv(kv) do
|
||||
quote bind_quoted: [kv: kv] do
|
||||
Enum.each(kv, fn {k, v} ->
|
||||
Enum.each kv, fn {k, v} ->
|
||||
def unquote(k)(), do: unquote(v)
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1422,7 +1397,7 @@ defmodule Kernel.SpecialForms do
|
||||
The `IO` module provides streams, that are both `Enumerable` and
|
||||
`Collectable`, here is an upcase echo server using comprehensions:
|
||||
|
||||
for line <- IO.stream(), into: IO.stream() do
|
||||
for line <- IO.stream(:stdio, :line), into: IO.stream(:stdio, :line) do
|
||||
String.upcase(line)
|
||||
end
|
||||
|
||||
@@ -1525,15 +1500,15 @@ defmodule Kernel.SpecialForms do
|
||||
iex> width
|
||||
nil
|
||||
|
||||
The behaviour of any expression in a clause is the same as if it was
|
||||
written outside of `with`. For example, `=` will raise a `MatchError`
|
||||
instead of returning the non-matched value:
|
||||
The behaviour of any expression in a clause is the same as outside.
|
||||
For example, `=` will raise a `MatchError` instead of returning the
|
||||
non-matched value:
|
||||
|
||||
with :foo = :bar, do: :ok
|
||||
** (MatchError) no match of right hand side value: :bar
|
||||
#=> ** (MatchError) no match of right hand side value: :bar
|
||||
|
||||
As with any other function or macro call in Elixir, explicit parens can
|
||||
also be used around the arguments before the `do`-`end` block:
|
||||
also be used around the arguments before the `do`/`end` block:
|
||||
|
||||
iex> opts = %{width: 10, height: 15}
|
||||
iex> with(
|
||||
@@ -1546,8 +1521,6 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
The choice between parens and no parens is a matter of preference.
|
||||
|
||||
## Else clauses
|
||||
|
||||
An `else` option can be given to modify what is being returned from
|
||||
`with` in the case of a failed match:
|
||||
|
||||
@@ -1558,68 +1531,17 @@ defmodule Kernel.SpecialForms do
|
||||
...> else
|
||||
...> :error ->
|
||||
...> {:error, :wrong_data}
|
||||
...>
|
||||
...> _other_error ->
|
||||
...> :unexpected_error
|
||||
...> end
|
||||
{:error, :wrong_data}
|
||||
|
||||
The `else` block works like a `case` clause: it can have multiple clauses,
|
||||
and the first match will be used. Variables bound inside `with` (such as
|
||||
`width` in this example) are not available in the `else` block.
|
||||
|
||||
If an `else` block is used and there are no matching clauses, a `WithClauseError`
|
||||
exception is raised.
|
||||
|
||||
### Beware!
|
||||
|
||||
Keep in mind that, one of potential drawback of `with` is that all
|
||||
failure clauses are flattened into a single `else` block. For example,
|
||||
take this code that checks if a given path points to an Elixir file
|
||||
and that it exists before creating a backup copy:
|
||||
|
||||
with ".ex" <- Path.extname(path),
|
||||
true <- File.exists?(path) do
|
||||
backup_path = path <> ".backup"
|
||||
File.cp!(path, backup_path)
|
||||
{:ok, backup_path}
|
||||
else
|
||||
binary when is_binary(binary) ->
|
||||
{:error, :invalid_extension}
|
||||
|
||||
false ->
|
||||
{:error, :missing_file}
|
||||
end
|
||||
|
||||
Note how we are having to reconstruct the result types of `Path.extname/1`
|
||||
and `File.exists?/1` to build error messages. In this case, it is better
|
||||
to change the with clauses to already return the desired format, like this:
|
||||
|
||||
with :ok <- validate_extension(path),
|
||||
:ok <- validate_exists(path) do
|
||||
backup_path = path <> ".backup"
|
||||
File.cp!(path, backup_path)
|
||||
{:ok, backup_path}
|
||||
end
|
||||
|
||||
defp validate_extname(path) do
|
||||
if Path.extname(path) == ".ex", do: :ok, else: {:error, :invalid_extension}
|
||||
end
|
||||
|
||||
defp validate_exists(path) do
|
||||
if File.exists?(path), do: :ok, else: {:error, :missing_file}
|
||||
end
|
||||
|
||||
Note how the code above is better organized and clearer once we
|
||||
make sure each clause in `with` returns a normalize format.
|
||||
"""
|
||||
defmacro with(args), do: error!([args])
|
||||
|
||||
@doc """
|
||||
Defines an anonymous function.
|
||||
|
||||
See `Function` for more information.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> add = fn a, b -> a + b end
|
||||
@@ -1657,7 +1579,7 @@ defmodule Kernel.SpecialForms do
|
||||
defmacro unquote(:__block__)(args), do: error!([args])
|
||||
|
||||
@doc """
|
||||
Capture operator. Captures or creates an anonymous function.
|
||||
Captures or creates an anonymous function.
|
||||
|
||||
## Capture
|
||||
|
||||
@@ -1679,8 +1601,6 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
&local_function/1
|
||||
|
||||
See also `Function.capture/3`.
|
||||
|
||||
## Anonymous functions
|
||||
|
||||
The capture operator can also be used to partially apply
|
||||
@@ -1748,7 +1668,7 @@ defmodule Kernel.SpecialForms do
|
||||
unambiguously identified by the operator `:.`. For example:
|
||||
|
||||
iex> quote do
|
||||
...> Foo.bar()
|
||||
...> Foo.bar
|
||||
...> end
|
||||
{{:., [], [{:__aliases__, [alias: false], [:Foo]}, :bar]}, [], []}
|
||||
|
||||
@@ -1756,7 +1676,7 @@ defmodule Kernel.SpecialForms do
|
||||
it can be sure that it represents a call and the second argument
|
||||
in the list is an atom.
|
||||
|
||||
On the other hand, aliases hold some properties:
|
||||
On the other hand, aliases holds some properties:
|
||||
|
||||
1. The head element of aliases can be any term that must expand to
|
||||
an atom at compilation time.
|
||||
@@ -1780,21 +1700,19 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
## Examples
|
||||
|
||||
case File.read(file) do
|
||||
{:ok, contents} when is_binary(contents) ->
|
||||
String.split(contents, "\n")
|
||||
|
||||
{:error, _reason} ->
|
||||
Logger.warning "could not find #{file}, assuming empty..."
|
||||
[]
|
||||
case thing do
|
||||
{:selector, i, value} when is_integer(i) ->
|
||||
value
|
||||
value ->
|
||||
value
|
||||
end
|
||||
|
||||
In the example above, we match the result of `File.read/1`
|
||||
against each clause "head" and execute the clause "body"
|
||||
corresponding to the first clause that matches.
|
||||
In the example above, we match `thing` against each clause "head"
|
||||
and execute the clause "body" corresponding to the first clause
|
||||
that matches.
|
||||
|
||||
If no clause matches, an error is raised. For this reason,
|
||||
it may be necessary to add a final catch-all clause (like `_`)
|
||||
If no clause matches, an error is raised.
|
||||
For this reason, it may be necessary to add a final catch-all clause (like `_`)
|
||||
which will always match.
|
||||
|
||||
x = 10
|
||||
@@ -1802,39 +1720,39 @@ defmodule Kernel.SpecialForms do
|
||||
case x do
|
||||
0 ->
|
||||
"This clause won't match"
|
||||
|
||||
_ ->
|
||||
"This clause would match any value (x = #{x})"
|
||||
end
|
||||
#=> "This clause would match any value (x = 10)"
|
||||
|
||||
## Variable handling
|
||||
## Variables handling
|
||||
|
||||
Note that variables bound in a clause do not leak to the outer context:
|
||||
Notice that variables bound in a clause "head" do not leak to the
|
||||
outer context:
|
||||
|
||||
case data do
|
||||
{:ok, value} -> value
|
||||
:error -> nil
|
||||
end
|
||||
|
||||
value
|
||||
#=> unbound variable value
|
||||
value #=> unbound variable value
|
||||
|
||||
Variables in the outer context cannot be overridden either:
|
||||
However, variables explicitly bound in the clause "body" are
|
||||
accessible from the outer context:
|
||||
|
||||
value = 7
|
||||
|
||||
case lucky? do
|
||||
false -> value = 13
|
||||
true -> true
|
||||
true -> true
|
||||
end
|
||||
|
||||
value
|
||||
#=> 7
|
||||
value #=> 7 or 13
|
||||
|
||||
In the example above, `value` is going to be `7` regardless of the value of
|
||||
`lucky?`. The variable `value` bound in the clause and the variable `value`
|
||||
bound in the outer context are two entirely separate variables.
|
||||
In the example above, value is going to be `7` or `13` depending on
|
||||
the value of `lucky?`. In case `value` has no previous value before
|
||||
case, clauses that do not explicitly bind a value have the variable
|
||||
bound to `nil`.
|
||||
|
||||
If you want to pattern match against an existing variable,
|
||||
you need to use the `^/1` operator:
|
||||
@@ -1843,23 +1761,10 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
case 10 do
|
||||
^x -> "Won't match"
|
||||
_ -> "Will match"
|
||||
_ -> "Will match"
|
||||
end
|
||||
#=> "Will match"
|
||||
|
||||
## Using guards to match against multiple values
|
||||
|
||||
While it is not possible to match against multiple patterns in a single
|
||||
clause, it's possible to match against multiple values by using guards:
|
||||
|
||||
case data do
|
||||
value when value in [:one, :two] ->
|
||||
"#{value} has been matched"
|
||||
|
||||
:three ->
|
||||
"three has been matched"
|
||||
end
|
||||
|
||||
"""
|
||||
defmacro case(condition, clauses), do: error!([condition, clauses])
|
||||
|
||||
@@ -1902,15 +1807,15 @@ defmodule Kernel.SpecialForms do
|
||||
do_something_that_may_fail(some_arg)
|
||||
rescue
|
||||
ArgumentError ->
|
||||
IO.puts("Invalid argument given")
|
||||
IO.puts "Invalid argument given"
|
||||
catch
|
||||
value ->
|
||||
IO.puts("Caught #{inspect(value)}")
|
||||
IO.puts "Caught #{inspect(value)}"
|
||||
else
|
||||
value ->
|
||||
IO.puts("Success! The result was #{inspect(value)}")
|
||||
IO.puts "Success! The result was #{inspect(value)}"
|
||||
after
|
||||
IO.puts("This is printed regardless if it failed or succeeded")
|
||||
IO.puts "This is printed regardless if it failed or succeed"
|
||||
end
|
||||
|
||||
The `rescue` clause is used to handle exceptions while the `catch`
|
||||
@@ -2005,7 +1910,7 @@ defmodule Kernel.SpecialForms do
|
||||
throw(:some_value)
|
||||
catch
|
||||
thrown_value ->
|
||||
IO.puts("A value was thrown: #{inspect(thrown_value)}")
|
||||
IO.puts "A value was thrown: #{inspect(thrown_value)}"
|
||||
end
|
||||
|
||||
### Catching values of any kind
|
||||
@@ -2017,15 +1922,15 @@ defmodule Kernel.SpecialForms do
|
||||
try do
|
||||
exit(:shutdown)
|
||||
catch
|
||||
:exit, value ->
|
||||
IO.puts("Exited with value #{inspect(value)}")
|
||||
:exit, value
|
||||
IO.puts "Exited with value #{inspect(value)}"
|
||||
end
|
||||
|
||||
try do
|
||||
exit(:shutdown)
|
||||
catch
|
||||
kind, value when kind in [:exit, :throw] ->
|
||||
IO.puts("Caught exit or throw with value #{inspect(value)}")
|
||||
IO.puts "Caught exit or throw with value #{inspect(value)}"
|
||||
end
|
||||
|
||||
The `catch` clause also supports `:error` alongside `:exit` and `:throw` as
|
||||
@@ -2163,8 +2068,7 @@ defmodule Kernel.SpecialForms do
|
||||
_, _ -> :failed
|
||||
end
|
||||
|
||||
x
|
||||
#=> unbound variable "x"
|
||||
x #=> unbound variable "x"
|
||||
|
||||
In the example above, `x` cannot be accessed since it was defined
|
||||
inside the `try` clause. A common practice to address this issue
|
||||
@@ -2197,7 +2101,7 @@ defmodule Kernel.SpecialForms do
|
||||
name when is_atom(name) ->
|
||||
name
|
||||
_ ->
|
||||
IO.puts(:stderr, "Unexpected message received")
|
||||
IO.puts :stderr, "Unexpected message received"
|
||||
end
|
||||
|
||||
An optional `after` clause can be given in case the message was not
|
||||
@@ -2209,10 +2113,10 @@ defmodule Kernel.SpecialForms do
|
||||
name when is_atom(name) ->
|
||||
name
|
||||
_ ->
|
||||
IO.puts(:stderr, "Unexpected message received")
|
||||
IO.puts :stderr, "Unexpected message received"
|
||||
after
|
||||
5000 ->
|
||||
IO.puts(:stderr, "No message in 5 seconds")
|
||||
IO.puts :stderr, "No message in 5 seconds"
|
||||
end
|
||||
|
||||
The `after` clause can be specified even if there are no match clauses.
|
||||
@@ -2229,7 +2133,7 @@ defmodule Kernel.SpecialForms do
|
||||
in hexadecimal notation) - it should be possible to represent the timeout
|
||||
value as an unsigned 32-bit integer.
|
||||
|
||||
## Variable handling
|
||||
## Variables handling
|
||||
|
||||
The `receive/1` special form handles variables exactly as the `case/2`
|
||||
special macro. For more information, check the docs for `case/2`.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
defmodule Kernel.Typespec do
|
||||
# TODO: Remove deprecated code on 2.0 and move this module to Module.Typespec.
|
||||
@moduledoc false
|
||||
|
||||
## Deprecated API moved to Code.Typespec
|
||||
@@ -22,9 +23,9 @@ defmodule Kernel.Typespec do
|
||||
{:docs_v1, _, _, _, _, _, docs} ->
|
||||
for {{:type, name, arity}, _, _, doc, _} <- docs do
|
||||
case doc do
|
||||
%{"en" => doc_string} -> {{name, arity}, doc_string}
|
||||
:none -> {{name, arity}, nil}
|
||||
:hidden -> {{name, arity}, false}
|
||||
_ -> {{name, arity}, nil}
|
||||
%{"en" => doc_string} -> {{name, arity}, doc_string}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -75,21 +76,12 @@ defmodule Kernel.Typespec do
|
||||
|
||||
def spec_to_callback(module, {name, arity} = signature)
|
||||
when is_atom(module) and is_atom(name) and arity in 0..255 do
|
||||
{set, bag} = :elixir_module.data_tables(module)
|
||||
{_set, bag} = :elixir_module.data_tables(module)
|
||||
|
||||
filter = fn {:spec, expr, pos} ->
|
||||
if spec_to_signature(expr) == signature do
|
||||
kind = :callback
|
||||
store_typespec(bag, kind, expr, pos)
|
||||
|
||||
case :ets.lookup(set, {:function, name, arity}) do
|
||||
[{{:function, ^name, ^arity}, _, line, _, doc, doc_meta}] ->
|
||||
store_doc(set, kind, name, arity, line, :doc, doc, doc_meta)
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
|
||||
delete_typespec(bag, :spec, expr, pos)
|
||||
store_typespec(bag, :callback, expr, pos)
|
||||
true
|
||||
else
|
||||
false
|
||||
@@ -117,11 +109,8 @@ defmodule Kernel.Typespec do
|
||||
|
||||
case spec_to_signature(expr) do
|
||||
{name, arity} ->
|
||||
# store doc only once in case callback has multiple clauses
|
||||
unless :ets.member(set, {kind, name, arity}) do
|
||||
{line, doc} = get_doc_info(set, :doc, line)
|
||||
store_doc(set, kind, name, arity, line, :doc, doc, %{})
|
||||
end
|
||||
{line, doc} = get_doc_info(set, :doc, line)
|
||||
store_doc(set, kind, name, arity, line, :doc, doc, %{})
|
||||
|
||||
:error ->
|
||||
:error
|
||||
@@ -130,18 +119,11 @@ defmodule Kernel.Typespec do
|
||||
store_typespec(bag, kind, expr, pos)
|
||||
end
|
||||
|
||||
@reserved_signatures [required: 1, optional: 1]
|
||||
def deftypespec(kind, expr, line, file, module, pos)
|
||||
when kind in [:type, :typep, :opaque] do
|
||||
{set, bag} = :elixir_module.data_tables(module)
|
||||
|
||||
case type_to_signature(expr) do
|
||||
{name, arity} = signature when signature in @reserved_signatures ->
|
||||
compile_error(
|
||||
:elixir_locals.get_cached_env(pos),
|
||||
"type #{name}/#{arity} is a reserved type and it cannot be defined"
|
||||
)
|
||||
|
||||
{name, arity} when kind == :typep ->
|
||||
{line, doc} = get_doc_info(set, :typedoc, line)
|
||||
|
||||
@@ -149,7 +131,7 @@ defmodule Kernel.Typespec do
|
||||
warning =
|
||||
"type #{name}/#{arity} is private, @typedoc's are always discarded for private types"
|
||||
|
||||
:elixir_errors.erl_warn(line, file, warning)
|
||||
:elixir_errors.warn(line, file, warning)
|
||||
end
|
||||
|
||||
{name, arity} ->
|
||||
@@ -187,6 +169,11 @@ defmodule Kernel.Typespec do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp delete_typespec(bag, key, expr, pos) do
|
||||
:ets.delete_object(bag, {{:accumulate, key}, {key, expr, pos}})
|
||||
:ok
|
||||
end
|
||||
|
||||
defp store_doc(set, kind, name, arity, line, doc_kind, doc, spec_meta) do
|
||||
doc_meta = get_doc_meta(spec_meta, doc_kind, set)
|
||||
:ets.insert(set, {{kind, name, arity}, line, doc, doc_meta})
|
||||
@@ -209,11 +196,11 @@ defmodule Kernel.Typespec do
|
||||
defp spec_to_signature({:when, _, [spec, _]}), do: type_to_signature(spec)
|
||||
defp spec_to_signature(other), do: type_to_signature(other)
|
||||
|
||||
defp type_to_signature({:"::", _, [{name, _, context}, _]})
|
||||
when is_atom(name) and name != :"::" and is_atom(context),
|
||||
defp type_to_signature({:::, _, [{name, _, context}, _]})
|
||||
when is_atom(name) and name != ::: and is_atom(context),
|
||||
do: {name, 0}
|
||||
|
||||
defp type_to_signature({:"::", _, [{name, _, args}, _]}) when is_atom(name) and name != :"::",
|
||||
defp type_to_signature({:::, _, [{name, _, args}, _]}) when is_atom(name) and name != :::,
|
||||
do: {name, length(args)}
|
||||
|
||||
defp type_to_signature(_), do: :error
|
||||
@@ -257,13 +244,7 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
if Map.has_key?(type_pairs, type_pair) do
|
||||
{error_full_path, error_line} = type_pairs[type_pair]
|
||||
error_relative_path = Path.relative_to_cwd(error_full_path)
|
||||
|
||||
compile_error(
|
||||
env,
|
||||
"type #{name}/#{arity} is already defined in #{error_relative_path}:#{error_line}"
|
||||
)
|
||||
compile_error(env, "type #{name}/#{arity} is already defined")
|
||||
end
|
||||
|
||||
Map.put(type_pairs, type_pair, {file, line})
|
||||
@@ -280,7 +261,7 @@ defmodule Kernel.Typespec do
|
||||
fun = fn {_kind, {name, arity} = type_pair, _line, _type, export} ->
|
||||
if not export and not :lists.member(type_pair, state.used_type_pairs) do
|
||||
%{^type_pair => {file, line}} = state.defined_type_pairs
|
||||
:elixir_errors.erl_warn(line, file, "type #{name}/#{arity} is unused")
|
||||
:elixir_errors.warn(line, file, "type #{name}/#{arity} is unused")
|
||||
false
|
||||
else
|
||||
true
|
||||
@@ -290,7 +271,7 @@ defmodule Kernel.Typespec do
|
||||
:lists.filter(fun, types)
|
||||
end
|
||||
|
||||
defp translate_type({kind, {:"::", _, [{name, _, args}, definition]}, pos}, state) do
|
||||
defp translate_type({kind, {:::, _, [{name, _, args}, definition]}, pos}, state) do
|
||||
caller = :elixir_locals.get_cached_env(pos)
|
||||
state = clean_local_state(state)
|
||||
|
||||
@@ -298,7 +279,7 @@ defmodule Kernel.Typespec do
|
||||
if is_atom(args) do
|
||||
[]
|
||||
else
|
||||
:lists.map(&variable/1, args)
|
||||
for(arg <- args, do: variable(arg))
|
||||
end
|
||||
|
||||
vars = :lists.filter(&match?({:var, _, _}, &1), args)
|
||||
@@ -308,7 +289,6 @@ defmodule Kernel.Typespec do
|
||||
type = {name, spec, vars}
|
||||
arity = length(args)
|
||||
|
||||
ensure_no_underscore_local_vars!(caller, var_names)
|
||||
ensure_no_unused_local_vars!(caller, state.local_vars)
|
||||
|
||||
{kind, export} =
|
||||
@@ -332,7 +312,7 @@ defmodule Kernel.Typespec do
|
||||
|
||||
if underspecified?(kind, arity, spec) do
|
||||
message = "@#{kind} type #{name}/#{arity} is underspecified and therefore meaningless"
|
||||
:elixir_errors.erl_warn(caller.line, caller.file, message)
|
||||
:elixir_errors.warn(caller.line, caller.file, message)
|
||||
end
|
||||
|
||||
{{kind, {name, arity}, caller.line, type, export}, state}
|
||||
@@ -357,13 +337,13 @@ defmodule Kernel.Typespec do
|
||||
translate_spec(kind, spec, [], caller, state)
|
||||
end
|
||||
|
||||
defp translate_spec(kind, {:"::", meta, [{name, _, args}, return]}, guard, caller, state)
|
||||
when is_atom(name) and name != :"::" do
|
||||
defp translate_spec(kind, {:::, meta, [{name, _, args}, return]}, guard, caller, state)
|
||||
when is_atom(name) and name != ::: do
|
||||
translate_spec(kind, meta, name, args, return, guard, caller, state)
|
||||
end
|
||||
|
||||
defp translate_spec(_kind, {name, _meta, _args} = spec, _guard, caller, _state)
|
||||
when is_atom(name) and name != :"::" do
|
||||
when is_atom(name) and name != ::: do
|
||||
spec = Macro.to_string(spec)
|
||||
compile_error(caller, "type specification missing return type: #{spec}")
|
||||
end
|
||||
@@ -402,7 +382,7 @@ defmodule Kernel.Typespec do
|
||||
{{kind, {name, arity}, caller.line, spec}, state}
|
||||
end
|
||||
|
||||
# TODO: Remove char_list type by v2.0
|
||||
# TODO: Remove char_list type by 2.0
|
||||
defp built_in_type?(:char_list, 0), do: true
|
||||
defp built_in_type?(:charlist, 0), do: true
|
||||
defp built_in_type?(:as_boolean, 1), do: true
|
||||
@@ -415,7 +395,7 @@ defmodule Kernel.Typespec do
|
||||
|
||||
defp ensure_no_defaults!(args) do
|
||||
fun = fn
|
||||
{:"::", _, [left, right]} ->
|
||||
{:::, _, [left, right]} ->
|
||||
ensure_not_default(left)
|
||||
ensure_not_default(right)
|
||||
left
|
||||
@@ -472,17 +452,17 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec(
|
||||
{:<<>>, meta, [{:"::", unit_meta, [{:_, _, ctx1}, {:*, _, [{:_, _, ctx2}, unit]}]}]},
|
||||
{:<<>>, meta, [{:::, unit_meta, [{:_, _, ctx1}, {:*, _, [{:_, _, ctx2}, unit]}]}]},
|
||||
_,
|
||||
_,
|
||||
state
|
||||
)
|
||||
when is_atom(ctx1) and is_atom(ctx2) and unit in 1..256 do
|
||||
when is_atom(ctx1) and is_atom(ctx2) and is_integer(unit) and unit >= 0 do
|
||||
line = line(meta)
|
||||
{{:type, line, :binary, [{:integer, line, 0}, {:integer, line(unit_meta), unit}]}, state}
|
||||
end
|
||||
|
||||
defp typespec({:<<>>, meta, [{:"::", size_meta, [{:_, _, ctx}, size]}]}, _, _, state)
|
||||
defp typespec({:<<>>, meta, [{:::, size_meta, [{:_, _, ctx}, size]}]}, _, _, state)
|
||||
when is_atom(ctx) and is_integer(size) and size >= 0 do
|
||||
line = line(meta)
|
||||
{{:type, line, :binary, [{:integer, line(size_meta), size}, {:integer, line, 0}]}, state}
|
||||
@@ -493,8 +473,8 @@ defmodule Kernel.Typespec do
|
||||
:<<>>,
|
||||
meta,
|
||||
[
|
||||
{:"::", size_meta, [{:_, _, ctx1}, size]},
|
||||
{:"::", unit_meta, [{:_, _, ctx2}, {:*, _, [{:_, _, ctx3}, unit]}]}
|
||||
{:::, size_meta, [{:_, _, ctx1}, size]},
|
||||
{:::, unit_meta, [{:_, _, ctx2}, {:*, _, [{:_, _, ctx3}, unit]}]}
|
||||
]
|
||||
},
|
||||
_,
|
||||
@@ -502,7 +482,7 @@ defmodule Kernel.Typespec do
|
||||
state
|
||||
)
|
||||
when is_atom(ctx1) and is_atom(ctx2) and is_atom(ctx3) and is_integer(size) and
|
||||
size >= 0 and unit in 1..256 do
|
||||
is_integer(unit) and size >= 0 and unit >= 0 do
|
||||
args = [{:integer, line(size_meta), size}, {:integer, line(unit_meta), unit}]
|
||||
{{:type, line(meta), :binary, args}, state}
|
||||
end
|
||||
@@ -510,7 +490,7 @@ defmodule Kernel.Typespec do
|
||||
defp typespec({:<<>>, _meta, _args}, _vars, caller, _state) do
|
||||
message =
|
||||
"invalid binary specification, expected <<_::size>>, <<_::_*unit>>, " <>
|
||||
"or <<_::size, _::_*unit>> with size being non-negative integers, and unit being an integer between 1 and 256"
|
||||
"or <<_::size, _::_*unit>> with size and unit being non-negative integers"
|
||||
|
||||
compile_error(caller, message)
|
||||
end
|
||||
@@ -522,6 +502,11 @@ defmodule Kernel.Typespec do
|
||||
|
||||
defp typespec({:%{}, meta, fields} = map, vars, caller, state) do
|
||||
fun = fn
|
||||
{k, v}, state when is_atom(k) ->
|
||||
{arg1, state} = typespec(k, vars, caller, state)
|
||||
{arg2, state} = typespec(v, vars, caller, state)
|
||||
{{:type, line(meta), :map_field_exact, [arg1, arg2]}, state}
|
||||
|
||||
{{:required, meta2, [k]}, v}, state ->
|
||||
{arg1, state} = typespec(k, vars, caller, state)
|
||||
{arg2, state} = typespec(v, vars, caller, state)
|
||||
@@ -533,9 +518,15 @@ defmodule Kernel.Typespec do
|
||||
{{:type, line(meta2), :map_field_assoc, [arg1, arg2]}, state}
|
||||
|
||||
{k, v}, state ->
|
||||
# TODO: Warn on Elixir v1.8 (since v1.6 is the first version to drop support for 18 and
|
||||
# older)
|
||||
# warning =
|
||||
# "invalid map specification. %{foo => bar} is deprecated in favor of " <>
|
||||
# "%{required(foo) => bar} and %{optional(foo) => bar}."
|
||||
# :elixir_errors.warn(caller.line, caller.file, warning)
|
||||
{arg1, state} = typespec(k, vars, caller, state)
|
||||
{arg2, state} = typespec(v, vars, caller, state)
|
||||
{{:type, line(meta), :map_field_exact, [arg1, arg2]}, state}
|
||||
{{:type, line(meta), :map_field_assoc, [arg1, arg2]}, state}
|
||||
|
||||
{:|, _, [_, _]}, _state ->
|
||||
error =
|
||||
@@ -553,7 +544,9 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec({:%, _, [name, {:%{}, meta, fields}]}, vars, caller, state) do
|
||||
module = Macro.expand(name, %{caller | function: {:__info__, 1}})
|
||||
# We cannot set a function name to avoid tracking
|
||||
# as a compile time dependency, because for structs it actually is one.
|
||||
module = Macro.expand(name, caller)
|
||||
|
||||
struct =
|
||||
module
|
||||
@@ -568,14 +561,14 @@ defmodule Kernel.Typespec do
|
||||
types =
|
||||
:lists.map(
|
||||
fn {field, _} -> {field, Keyword.get(fields, field, quote(do: term()))} end,
|
||||
:lists.sort(struct)
|
||||
struct
|
||||
)
|
||||
|
||||
fun = fn {field, _} ->
|
||||
unless Keyword.has_key?(struct, field) do
|
||||
compile_error(
|
||||
caller,
|
||||
"undefined field #{inspect(field)} on struct #{inspect(module)}"
|
||||
"undefined field #{inspect(field)} on struct #{Macro.to_string(name)}"
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -597,13 +590,7 @@ defmodule Kernel.Typespec do
|
||||
{_, _, [name, fields | _]} when is_list(fields) ->
|
||||
types =
|
||||
:lists.map(
|
||||
fn {field, _} ->
|
||||
{:"::", [],
|
||||
[
|
||||
{field, [], nil},
|
||||
Keyword.get(field_specs, field, quote(do: term()))
|
||||
]}
|
||||
end,
|
||||
fn {field, _} -> Keyword.get(field_specs, field, quote(do: term())) end,
|
||||
fields
|
||||
)
|
||||
|
||||
@@ -641,7 +628,10 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec({:__aliases__, _, _} = alias, vars, caller, state) do
|
||||
typespec(expand_remote(alias, caller), vars, caller, state)
|
||||
# We set a function name to avoid tracking
|
||||
# aliases in typespecs as compile time dependencies.
|
||||
atom = Macro.expand(alias, %{caller | function: {:typespec, 0}})
|
||||
typespec(atom, vars, caller, state)
|
||||
end
|
||||
|
||||
# Handle funs
|
||||
@@ -653,7 +643,7 @@ defmodule Kernel.Typespec do
|
||||
|
||||
# Handle type operator
|
||||
defp typespec(
|
||||
{:"::", meta, [{var_name, var_meta, context}, expr]} = ann_type,
|
||||
{:::, meta, [{var_name, var_meta, context}, expr]} = ann_type,
|
||||
vars,
|
||||
caller,
|
||||
state
|
||||
@@ -665,11 +655,11 @@ defmodule Kernel.Typespec do
|
||||
"invalid type annotation. Type annotations cannot be nested: " <>
|
||||
"#{Macro.to_string(ann_type)}"
|
||||
|
||||
# TODO: Make this an error on v2.0 and remove the code below
|
||||
:elixir_errors.erl_warn(caller.line, caller.file, message)
|
||||
# TODO: make this an error in elixir 2.0 and remove the code below
|
||||
:elixir_errors.warn(caller.line, caller.file, message)
|
||||
|
||||
# This may be generating an invalid typespec but we need to generate it
|
||||
# to avoid breaking existing code that was valid but only broke Dialyzer
|
||||
# to avoid breaking existing code that was valid but only broke dialyzer
|
||||
{right, state} = typespec(expr, vars, caller, state)
|
||||
{{:ann_type, line(meta), [{:var, line(var_meta), var_name}, right]}, state}
|
||||
|
||||
@@ -678,17 +668,17 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
end
|
||||
|
||||
defp typespec({:"::", meta, [left, right]} = expr, vars, caller, state) do
|
||||
defp typespec({:::, meta, [left, right]} = expr, vars, caller, state) do
|
||||
message =
|
||||
"invalid type annotation. When using the | operator to represent the union of types, " <>
|
||||
"make sure to wrap type annotations in parentheses: #{Macro.to_string(expr)}"
|
||||
|
||||
# TODO: Make this an error on v2.0, and remove the code below and
|
||||
# the :undefined_type_error_enabled? key from the state
|
||||
:elixir_errors.erl_warn(caller.line, caller.file, message)
|
||||
# TODO: make this an error in Elixir 2.0, and remove the code below and the
|
||||
# :undefined_type_error_enabled? key from the state
|
||||
:elixir_errors.warn(caller.line, caller.file, message)
|
||||
|
||||
# This may be generating an invalid typespec but we need to generate it
|
||||
# to avoid breaking existing code that was valid but only broke Dialyzer
|
||||
# to avoid breaking existing code that was valid but only broke dialyzer
|
||||
state = %{state | undefined_type_error_enabled?: false}
|
||||
{left, state} = typespec(left, vars, caller, state)
|
||||
state = %{state | undefined_type_error_enabled?: true}
|
||||
@@ -729,21 +719,18 @@ defmodule Kernel.Typespec do
|
||||
|
||||
# Handle remote calls
|
||||
defp typespec({{:., meta, [remote, name]}, _, args} = orig, vars, caller, state) do
|
||||
remote = expand_remote(remote, caller)
|
||||
# We set a function name to avoid tracking
|
||||
# aliases in typespecs as compile time dependencies.
|
||||
remote = Macro.expand(remote, %{caller | function: {:typespec, 0}})
|
||||
|
||||
cond do
|
||||
not is_atom(remote) ->
|
||||
compile_error(caller, "invalid remote in typespec: #{Macro.to_string(orig)}")
|
||||
|
||||
remote == caller.module ->
|
||||
typespec({name, meta, args}, vars, caller, state)
|
||||
|
||||
true ->
|
||||
{remote_spec, state} = typespec(remote, vars, caller, state)
|
||||
{name_spec, state} = typespec(name, vars, caller, state)
|
||||
type = {remote_spec, meta, name_spec, args}
|
||||
remote_type(type, vars, caller, state)
|
||||
unless is_atom(remote) do
|
||||
compile_error(caller, "invalid remote in typespec: #{Macro.to_string(orig)}")
|
||||
end
|
||||
|
||||
{remote_spec, state} = typespec(remote, vars, caller, state)
|
||||
{name_spec, state} = typespec(name, vars, caller, state)
|
||||
type = {remote_spec, meta, name_spec, args}
|
||||
remote_type(type, vars, caller, state)
|
||||
end
|
||||
|
||||
# Handle tuples
|
||||
@@ -782,7 +769,8 @@ defmodule Kernel.Typespec do
|
||||
"For character lists, use charlist() type, for strings, String.t()\n" <>
|
||||
Exception.format_stacktrace(Macro.Env.stacktrace(caller))
|
||||
|
||||
:elixir_errors.erl_warn(caller.line, caller.file, warning)
|
||||
:elixir_errors.warn(caller.line, caller.file, warning)
|
||||
|
||||
{args, state} = :lists.mapfoldl(&typespec(&1, vars, caller, &2), state, args)
|
||||
{{:type, line(meta), :string, args}, state}
|
||||
end
|
||||
@@ -793,15 +781,17 @@ defmodule Kernel.Typespec do
|
||||
"For non-empty character lists, use nonempty_charlist() type, for strings, String.t()\n" <>
|
||||
Exception.format_stacktrace(Macro.Env.stacktrace(caller))
|
||||
|
||||
:elixir_errors.erl_warn(caller.line, caller.file, warning)
|
||||
:elixir_errors.warn(caller.line, caller.file, warning)
|
||||
|
||||
{args, state} = :lists.mapfoldl(&typespec(&1, vars, caller, &2), state, args)
|
||||
{{:type, line(meta), :nonempty_string, args}, state}
|
||||
end
|
||||
|
||||
# TODO: Remove char_list type by 2.0
|
||||
defp typespec({type, _meta, []}, vars, caller, state) when type in [:charlist, :char_list] do
|
||||
if type == :char_list do
|
||||
warning = "the char_list() type is deprecated, use charlist()"
|
||||
:elixir_errors.erl_warn(caller.line, caller.file, warning)
|
||||
:elixir_errors.warn(caller.line, caller.file, warning)
|
||||
end
|
||||
|
||||
typespec(quote(do: :elixir.charlist()), vars, caller, state)
|
||||
@@ -839,10 +829,7 @@ defmodule Kernel.Typespec do
|
||||
false ->
|
||||
if state.undefined_type_error_enabled? and
|
||||
not Map.has_key?(state.defined_type_pairs, {name, arity}) do
|
||||
compile_error(
|
||||
caller,
|
||||
"type #{name}/#{arity} undefined (no such type in #{inspect(caller.module)})"
|
||||
)
|
||||
compile_error(caller, "type #{name}/#{arity} undefined")
|
||||
end
|
||||
|
||||
state =
|
||||
@@ -900,25 +887,6 @@ defmodule Kernel.Typespec do
|
||||
|
||||
## Helpers
|
||||
|
||||
# This is a backport of Macro.expand/2 because we want to expand
|
||||
# aliases but we don't them to become compile-time references.
|
||||
defp expand_remote({:__aliases__, _, _} = alias, env) do
|
||||
case :elixir_aliases.expand(alias, env) do
|
||||
receiver when is_atom(receiver) ->
|
||||
receiver
|
||||
|
||||
aliases ->
|
||||
aliases = :lists.map(&Macro.expand_once(&1, env), aliases)
|
||||
|
||||
case :lists.all(&is_atom/1, aliases) do
|
||||
true -> :elixir_aliases.concat(aliases)
|
||||
false -> alias
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_remote(other, env), do: Macro.expand(other, env)
|
||||
|
||||
defp compile_error(caller, desc) do
|
||||
raise CompileError, file: caller.file, line: caller.line, description: desc
|
||||
end
|
||||
@@ -994,41 +962,10 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
end
|
||||
|
||||
defp ensure_no_underscore_local_vars!(caller, var_names) do
|
||||
case :lists.member(:_, var_names) do
|
||||
true ->
|
||||
compile_error(caller, "type variable '_' is invalid")
|
||||
|
||||
false ->
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
defp ensure_no_unused_local_vars!(caller, local_vars) do
|
||||
fun = fn {name, used_times} ->
|
||||
case {:erlang.atom_to_list(name), used_times} do
|
||||
{[?_ | _], :used_once} ->
|
||||
:ok
|
||||
|
||||
{[?_ | _], :used_multiple} ->
|
||||
warning =
|
||||
"the underscored type variable \"#{name}\" is used more than once in the " <>
|
||||
"type specification. A leading underscore indicates that the value of the " <>
|
||||
"variable should be ignored. If this is intended please rename the variable to " <>
|
||||
"remove the underscore"
|
||||
|
||||
:elixir_errors.erl_warn(caller.line, caller.file, warning)
|
||||
|
||||
{_, :used_once} ->
|
||||
compile_error(
|
||||
caller,
|
||||
"type variable #{name} is used only once. Type variables in typespecs " <>
|
||||
"must be referenced at least twice, otherwise it is equivalent to term()"
|
||||
)
|
||||
|
||||
_ ->
|
||||
:ok
|
||||
end
|
||||
fun = fn
|
||||
{name, :used_once} -> compile_error(caller, "type variable #{name} is unused")
|
||||
_ -> :ok
|
||||
end
|
||||
|
||||
:lists.foreach(fun, :maps.to_list(local_vars))
|
||||
|
||||
@@ -25,20 +25,13 @@ defmodule Kernel.Utils do
|
||||
Callback for defdelegate.
|
||||
"""
|
||||
def defdelegate(fun, opts) when is_list(opts) do
|
||||
# TODO: Remove on v2.0
|
||||
# TODO: Remove by 2.0
|
||||
append_first? = Keyword.get(opts, :append_first, false)
|
||||
|
||||
{name, args} =
|
||||
case fun do
|
||||
{:when, _, [_left, right]} ->
|
||||
raise ArgumentError,
|
||||
"guards are not allowed in defdelegate/2, got: when #{Macro.to_string(right)}"
|
||||
|
||||
_ ->
|
||||
case Macro.decompose_call(fun) do
|
||||
{_, _} = pair -> pair
|
||||
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
|
||||
end
|
||||
case Macro.decompose_call(fun) do
|
||||
{_, _} = pair -> pair
|
||||
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
|
||||
end
|
||||
|
||||
as = Keyword.get(opts, :as, name)
|
||||
@@ -101,9 +94,6 @@ defmodule Kernel.Utils do
|
||||
fields = :lists.map(mapper, fields)
|
||||
enforce_keys = List.wrap(Module.get_attribute(module, :enforce_keys))
|
||||
|
||||
# TODO: Make it raise on v2.0
|
||||
warn_on_duplicate_struct_key(:lists.keysort(1, fields))
|
||||
|
||||
foreach = fn
|
||||
key when is_atom(key) ->
|
||||
:ok
|
||||
@@ -115,38 +105,16 @@ defmodule Kernel.Utils do
|
||||
:lists.foreach(foreach, enforce_keys)
|
||||
|
||||
struct = :maps.put(:__struct__, module, :maps.from_list(fields))
|
||||
|
||||
case enforce_keys -- :maps.keys(struct) do
|
||||
[] ->
|
||||
{struct, enforce_keys, Module.get_attribute(module, :derive)}
|
||||
|
||||
error_keys ->
|
||||
raise ArgumentError,
|
||||
"@enforce_keys required keys (#{inspect(error_keys)}) that are not defined in defstruct: " <>
|
||||
"#{inspect(fields)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp warn_on_duplicate_struct_key([]) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp warn_on_duplicate_struct_key([{key, _} | [{key, _} | _] = rest]) do
|
||||
IO.warn("duplicate key #{inspect(key)} found in struct")
|
||||
warn_on_duplicate_struct_key(rest)
|
||||
end
|
||||
|
||||
defp warn_on_duplicate_struct_key([_ | rest]) do
|
||||
warn_on_duplicate_struct_key(rest)
|
||||
{struct, enforce_keys, Module.get_attribute(module, :derive)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Announcing callback for defstruct.
|
||||
"""
|
||||
def announce_struct(module) do
|
||||
case :erlang.get(:elixir_compiler_info) do
|
||||
case :erlang.get(:elixir_compiler_pid) do
|
||||
:undefined -> :ok
|
||||
{pid, _} -> send(pid, {:available, :struct, module})
|
||||
pid -> send(pid, {:struct_available, module})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -196,13 +164,11 @@ defmodule Kernel.Utils do
|
||||
that checks for its presence in a guard, then unquotes the variable references as
|
||||
appropriate.
|
||||
|
||||
The following code
|
||||
The resulting transformation looks something like this:
|
||||
|
||||
expression = quote do: is_integer(value) and rem(value, 2) == 0
|
||||
variable_references = [value: Elixir]
|
||||
Kernel.Utils.defguard(expression, variable_references) |> Macro.to_string() |> IO.puts()
|
||||
|
||||
would print a code similar to:
|
||||
> expression = quote do: is_integer(value) and rem(value, 2) == 0
|
||||
> variable_references = [value: Elixir]
|
||||
> Kernel.Utils.defguard(expression, variable_references) |> Macro.to_string |> IO.puts
|
||||
|
||||
case Macro.Env.in_guard?(__CALLER__) do
|
||||
true ->
|
||||
@@ -226,7 +192,7 @@ defmodule Kernel.Utils do
|
||||
def defguard(args, expr, env) do
|
||||
{^args, vars} = extract_refs_from_args(args)
|
||||
env = :elixir_env.with_vars(%{env | context: :guard}, vars)
|
||||
{expr, _, _} = :elixir_expand.expand(expr, :elixir_env.env_to_ex(env), env)
|
||||
{expr, _scope} = :elixir_expand.expand(expr, env)
|
||||
|
||||
quote do
|
||||
case Macro.Env.in_guard?(__CALLER__) do
|
||||
@@ -274,7 +240,7 @@ defmodule Kernel.Utils do
|
||||
{new_var, acc}
|
||||
|
||||
%{} ->
|
||||
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc) + 1))
|
||||
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc)))
|
||||
new_var = Macro.var(generated, Elixir)
|
||||
{new_var, Map.put(acc, pair, {new_var, var})}
|
||||
end
|
||||
|
||||
+215
-559
File diff suppressed because it is too large
Load Diff
+125
-216
@@ -1,6 +1,19 @@
|
||||
defmodule List do
|
||||
@moduledoc """
|
||||
Linked lists hold zero, one, or more elements in the chosen order.
|
||||
Functions that work on (linked) lists.
|
||||
|
||||
Many of the functions provided for lists, which implement
|
||||
the `Enumerable` protocol, are found in the `Enum` module.
|
||||
|
||||
Additionally, the following functions and operators for lists are
|
||||
found in `Kernel`:
|
||||
|
||||
* `++/2`
|
||||
* `--/2`
|
||||
* `hd/1`
|
||||
* `tl/1`
|
||||
* `in/2`
|
||||
* `length/1`
|
||||
|
||||
Lists in Elixir are specified between square brackets:
|
||||
|
||||
@@ -15,13 +28,6 @@ defmodule List do
|
||||
iex> [1, true, 2, false, 3, true] -- [true, false]
|
||||
[1, 2, 3, true]
|
||||
|
||||
An element can be prepended to a list using `|`:
|
||||
|
||||
iex> new = 0
|
||||
iex> list = [1, 2, 3]
|
||||
iex> [new | list]
|
||||
[0, 1, 2, 3]
|
||||
|
||||
Lists in Elixir are effectively linked lists, which means
|
||||
they are internally represented in pairs containing the
|
||||
head and the tail of a list:
|
||||
@@ -57,61 +63,29 @@ defmodule List do
|
||||
iex> list ++ [4] # slow
|
||||
[1, 2, 3, 4]
|
||||
|
||||
Most of the functions in this module work in linear time. This means that,
|
||||
that the time it takes to perform an operation grows at the same rate as the
|
||||
length of the list. For example `length/1` and `last/1` will run in linear
|
||||
time because they need to iterate through every element of the list, but
|
||||
`first/1` will run in constant time because it only needs the first element.
|
||||
|
||||
Lists also implement the `Enumerable` protocol, so many functions to work with
|
||||
lists are found in the `Enum` module. Additionally, the following functions and
|
||||
operators for lists are found in `Kernel`:
|
||||
|
||||
* `++/2`
|
||||
* `--/2`
|
||||
* `hd/1`
|
||||
* `tl/1`
|
||||
* `in/2`
|
||||
* `length/1`
|
||||
Additionally, getting a list's length and accessing it by index are
|
||||
linear time operations. Negative indexes are also supported but
|
||||
they imply the list will be iterated twice, once to calculate the
|
||||
proper index and another time to perform the operation.
|
||||
|
||||
## Charlists
|
||||
|
||||
If a list is made of non-negative integers, where each integer represents a
|
||||
Unicode code point, the list can also be called a charlist. These integers
|
||||
must:
|
||||
|
||||
* be within the range `0..0x10FFFF` (`0..1_114_111`);
|
||||
* and be out of the range `0xD800..0xDFFF` (`55_296..57_343`), which is
|
||||
reserved in Unicode for UTF-16 surrogate pairs.
|
||||
|
||||
Elixir uses single quotes to define charlists:
|
||||
If a list is made of non-negative integers, it can also be called
|
||||
a charlist. Elixir uses single quotes to define charlists:
|
||||
|
||||
iex> 'héllo'
|
||||
[104, 233, 108, 108, 111]
|
||||
|
||||
In particular, charlists will be printed back by default in single
|
||||
quotes if they contain only printable ASCII characters:
|
||||
In particular, charlists may be printed back in single
|
||||
quotes if they contain only ASCII-printable codepoints:
|
||||
|
||||
iex> 'abc'
|
||||
'abc'
|
||||
|
||||
Even though the representation changed, the raw data does remain a list of
|
||||
numbers, which can be handled as such:
|
||||
|
||||
iex> inspect('abc', charlists: :as_list)
|
||||
"[97, 98, 99]"
|
||||
iex> Enum.map('abc', fn num -> 1000 + num end)
|
||||
[1097, 1098, 1099]
|
||||
|
||||
You can use the `IEx.Helpers.i/1` helper to get a condensed rundown on
|
||||
charlists in IEx when you encounter them, which shows you the type, description
|
||||
and also the raw representation in one single summary.
|
||||
|
||||
The rationale behind this behaviour is to better support
|
||||
Erlang libraries which may return text as charlists
|
||||
instead of Elixir strings. In Erlang, charlists are the default
|
||||
way of handling strings, while in Elixir it's binaries. One
|
||||
example of such functions is `Application.loaded_applications/0`:
|
||||
instead of Elixir strings. One example of such functions
|
||||
is `Application.loaded_applications/0`:
|
||||
|
||||
Application.loaded_applications()
|
||||
#=> [
|
||||
@@ -122,19 +96,17 @@ defmodule List do
|
||||
#=> {:logger, 'logger', '1.0.0'}
|
||||
#=> ]
|
||||
|
||||
A list can be checked if it is made of only printable ASCII
|
||||
characters with `ascii_printable?/2`.
|
||||
|
||||
Improper lists are never deemed as charlists.
|
||||
A list can be checked if it is made of printable ASCII
|
||||
codepoints with `ascii_printable?/2`.
|
||||
"""
|
||||
|
||||
@compile :inline_list_funcs
|
||||
|
||||
@doc """
|
||||
Deletes the given `element` from the `list`. Returns a new list without
|
||||
the element.
|
||||
Deletes the given `item` from the `list`. Returns a new list without
|
||||
the item.
|
||||
|
||||
If the `element` occurs more than once in the `list`, just
|
||||
If the `item` occurs more than once in the `list`, just
|
||||
the first occurrence is removed.
|
||||
|
||||
## Examples
|
||||
@@ -142,47 +114,29 @@ defmodule List do
|
||||
iex> List.delete([:a, :b, :c], :a)
|
||||
[:b, :c]
|
||||
|
||||
iex> List.delete([:a, :b, :c], :d)
|
||||
[:a, :b, :c]
|
||||
|
||||
iex> List.delete([:a, :b, :b, :c], :b)
|
||||
[:a, :b, :c]
|
||||
|
||||
iex> List.delete([], :b)
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec delete([], any) :: []
|
||||
@spec delete([...], any) :: list
|
||||
def delete(list, element)
|
||||
def delete([element | list], element), do: list
|
||||
def delete([other | list], element), do: [other | delete(list, element)]
|
||||
def delete([], _element), do: []
|
||||
@spec delete(list, any) :: list
|
||||
def delete(list, item)
|
||||
def delete([item | list], item), do: list
|
||||
def delete([other | list], item), do: [other | delete(list, item)]
|
||||
def delete([], _item), do: []
|
||||
|
||||
@doc """
|
||||
Duplicates the given element `n` times in a list.
|
||||
|
||||
`n` is an integer greater than or equal to `0`.
|
||||
|
||||
If `n` is `0`, an empty list is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.duplicate("hello", 0)
|
||||
[]
|
||||
iex> List.duplicate("hello", 3)
|
||||
["hello", "hello", "hello"]
|
||||
|
||||
iex> List.duplicate("hi", 1)
|
||||
["hi"]
|
||||
|
||||
iex> List.duplicate("bye", 2)
|
||||
["bye", "bye"]
|
||||
|
||||
iex> List.duplicate([1, 2], 3)
|
||||
[[1, 2], [1, 2], [1, 2]]
|
||||
iex> List.duplicate([1, 2], 2)
|
||||
[[1, 2], [1, 2]]
|
||||
|
||||
"""
|
||||
@spec duplicate(any, 0) :: []
|
||||
@spec duplicate(elem, pos_integer) :: [elem, ...] when elem: var
|
||||
@spec duplicate(elem, non_neg_integer) :: [elem] when elem: var
|
||||
def duplicate(elem, n) do
|
||||
:lists.duplicate(n, elem)
|
||||
end
|
||||
@@ -190,16 +144,11 @@ defmodule List do
|
||||
@doc """
|
||||
Flattens the given `list` of nested lists.
|
||||
|
||||
Empty list elements are discarded.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.flatten([1, [[2], 3]])
|
||||
[1, 2, 3]
|
||||
|
||||
iex> List.flatten([[], [[], []]])
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec flatten(deep_list) :: list when deep_list: [any | deep_list]
|
||||
def flatten(list) do
|
||||
@@ -211,17 +160,11 @@ defmodule List do
|
||||
The list `tail` will be added at the end of
|
||||
the flattened list.
|
||||
|
||||
Empty list elements from `list` are discarded,
|
||||
but not the ones from `tail`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.flatten([1, [[2], 3]], [4, 5])
|
||||
[1, 2, 3, 4, 5]
|
||||
|
||||
iex> List.flatten([1, [], 2], [3, [], 4])
|
||||
[1, 2, 3, [], 4]
|
||||
|
||||
"""
|
||||
@spec flatten(deep_list, [elem]) :: [elem] when elem: var, deep_list: [elem | deep_list]
|
||||
def flatten(list, tail) do
|
||||
@@ -230,7 +173,7 @@ defmodule List do
|
||||
|
||||
@doc """
|
||||
Folds (reduces) the given list from the left with
|
||||
a function. Requires an accumulator, which can be any value.
|
||||
a function. Requires an accumulator.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -239,9 +182,6 @@ defmodule List do
|
||||
|
||||
iex> List.foldl([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
|
||||
2
|
||||
|
||||
iex> List.foldl([1, 2, 3], {0, 0}, fn x, {a1, a2} -> {a1 + x, a2 - x} end)
|
||||
{6, -6}
|
||||
|
||||
"""
|
||||
@spec foldl([elem], acc, (elem, acc -> acc)) :: acc when elem: var, acc: var
|
||||
@@ -251,15 +191,12 @@ defmodule List do
|
||||
|
||||
@doc """
|
||||
Folds (reduces) the given list from the right with
|
||||
a function. Requires an accumulator, which can be any value.
|
||||
a function. Requires an accumulator.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.foldr([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
|
||||
-2
|
||||
|
||||
iex> List.foldr([1, 2, 3, 4], %{sum: 0, product: 1}, fn x, %{sum: a1, product: a2} -> %{sum: a1 + x, product: a2 * x} end)
|
||||
%{product: 24, sum: 10}
|
||||
|
||||
"""
|
||||
@spec foldr([elem], acc, (elem, acc -> acc)) :: acc when elem: var, acc: var
|
||||
@@ -268,18 +205,13 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the first element in `list` or `default` if `list` is empty.
|
||||
|
||||
`first/2` has been introduced in Elixir v1.12.0, while `first/1` has been available since v1.0.0.
|
||||
Returns the first element in `list` or `nil` if `list` is empty.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.first([])
|
||||
nil
|
||||
|
||||
iex> List.first([], 1)
|
||||
1
|
||||
|
||||
iex> List.first([1])
|
||||
1
|
||||
|
||||
@@ -287,25 +219,18 @@ defmodule List do
|
||||
1
|
||||
|
||||
"""
|
||||
@spec first([], any) :: any
|
||||
@spec first([elem, ...], any) :: elem when elem: var
|
||||
def first(list, default \\ nil)
|
||||
def first([], default), do: default
|
||||
def first([head | _], _default), do: head
|
||||
@spec first([elem]) :: nil | elem when elem: var
|
||||
def first([]), do: nil
|
||||
def first([head | _]), do: head
|
||||
|
||||
@doc """
|
||||
Returns the last element in `list` or `default` if `list` is empty.
|
||||
|
||||
`last/2` has been introduced in Elixir v1.12.0, while `last/1` has been available since v1.0.0.
|
||||
Returns the last element in `list` or `nil` if `list` is empty.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.last([])
|
||||
nil
|
||||
|
||||
iex> List.last([], 1)
|
||||
1
|
||||
|
||||
iex> List.last([1])
|
||||
1
|
||||
|
||||
@@ -313,21 +238,16 @@ defmodule List do
|
||||
3
|
||||
|
||||
"""
|
||||
@spec last([], any) :: any
|
||||
@spec last([elem, ...], any) :: elem when elem: var
|
||||
@compile {:inline, last: 2}
|
||||
def last(list, default \\ nil)
|
||||
def last([], default), do: default
|
||||
def last([head], _default), do: head
|
||||
def last([_ | tail], default), do: last(tail, default)
|
||||
@spec last([elem]) :: nil | elem when elem: var
|
||||
def last([]), do: nil
|
||||
def last([head]), do: head
|
||||
def last([_ | tail]), do: last(tail)
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and returns the first tuple
|
||||
where the element at `position` in the tuple matches the
|
||||
where the item at `position` in the tuple matches the
|
||||
given `key`.
|
||||
|
||||
If no matching tuple is found, `default` is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.keyfind([a: 1, b: 2], :a, 0)
|
||||
@@ -341,43 +261,13 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keyfind([tuple], any, non_neg_integer, any) :: any
|
||||
def keyfind(list, key, position, default \\ nil) when is_integer(position) do
|
||||
def keyfind(list, key, position, default \\ nil) do
|
||||
:lists.keyfind(key, position + 1, list) || default
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and returns the first tuple
|
||||
where the element at `position` in the tuple matches the
|
||||
given `key`.
|
||||
|
||||
If no matching tuple is found, an error is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.keyfind!([a: 1, b: 2], :a, 0)
|
||||
{:a, 1}
|
||||
|
||||
iex> List.keyfind!([a: 1, b: 2], 2, 1)
|
||||
{:b, 2}
|
||||
|
||||
iex> List.keyfind!([a: 1, b: 2], :c, 0)
|
||||
** (KeyError) key :c at position 0 not found in: [a: 1, b: 2]
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec keyfind!([tuple], any, non_neg_integer) :: any
|
||||
def keyfind!(list, key, position) when is_integer(position) do
|
||||
:lists.keyfind(key, position + 1, list) ||
|
||||
raise KeyError,
|
||||
key: key,
|
||||
term: list,
|
||||
message:
|
||||
"key #{inspect(key)} at position #{inspect(position)} not found in: #{inspect(list)}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and returns `true` if there is
|
||||
a tuple where the element at `position` in the tuple matches
|
||||
a tuple where the item at `position` in the tuple matches
|
||||
the given `key`.
|
||||
|
||||
## Examples
|
||||
@@ -393,12 +283,12 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keymember?([tuple], any, non_neg_integer) :: boolean
|
||||
def keymember?(list, key, position) when is_integer(position) do
|
||||
def keymember?(list, key, position) do
|
||||
:lists.keymember(key, position + 1, list)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and if the identified element by `key` at `position`
|
||||
Receives a list of tuples and if the identified item by `key` at `position`
|
||||
exists, it is replaced with `new_tuple`.
|
||||
|
||||
## Examples
|
||||
@@ -411,12 +301,12 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keyreplace([tuple], any, non_neg_integer, tuple) :: [tuple]
|
||||
def keyreplace(list, key, position, new_tuple) when is_integer(position) do
|
||||
def keyreplace(list, key, position, new_tuple) do
|
||||
:lists.keyreplace(key, position + 1, list, new_tuple)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and sorts the elements
|
||||
Receives a list of tuples and sorts the items
|
||||
at `position` of the tuples. The sort is stable.
|
||||
|
||||
## Examples
|
||||
@@ -429,15 +319,15 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keysort([tuple], non_neg_integer) :: [tuple]
|
||||
def keysort(list, position) when is_integer(position) do
|
||||
def keysort(list, position) do
|
||||
:lists.keysort(position + 1, list)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a `list` of tuples and replaces the element
|
||||
Receives a `list` of tuples and replaces the item
|
||||
identified by `key` at `position` with `new_tuple`.
|
||||
|
||||
If the element does not exist, it is added to the end of the `list`.
|
||||
If the item does not exist, it is added to the end of the `list`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -449,13 +339,13 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keystore([tuple], any, non_neg_integer, tuple) :: [tuple, ...]
|
||||
def keystore(list, key, position, new_tuple) when is_integer(position) do
|
||||
def keystore(list, key, position, new_tuple) do
|
||||
:lists.keystore(key, position + 1, list, new_tuple)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a `list` of tuples and deletes the first tuple
|
||||
where the element at `position` matches the
|
||||
where the item at `position` matches the
|
||||
given `key`. Returns the new list.
|
||||
|
||||
## Examples
|
||||
@@ -471,7 +361,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keydelete([tuple], any, non_neg_integer) :: [tuple]
|
||||
def keydelete(list, key, position) when is_integer(position) do
|
||||
def keydelete(list, key, position) do
|
||||
:lists.keydelete(key, position + 1, list)
|
||||
end
|
||||
|
||||
@@ -495,9 +385,9 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keytake([tuple], any, non_neg_integer) :: {tuple, [tuple]} | nil
|
||||
def keytake(list, key, position) when is_integer(position) do
|
||||
def keytake(list, key, position) do
|
||||
case :lists.keytake(key, position + 1, list) do
|
||||
{:value, element, list} -> {element, list}
|
||||
{:value, item, list} -> {item, list}
|
||||
false -> nil
|
||||
end
|
||||
end
|
||||
@@ -520,7 +410,9 @@ defmodule List do
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec wrap(term) :: maybe_improper_list()
|
||||
@spec wrap(nil) :: []
|
||||
@spec wrap(list) :: list when list: maybe_improper_list()
|
||||
@spec wrap(term) :: nonempty_list(term) when term: any()
|
||||
def wrap(term)
|
||||
|
||||
def wrap(list) when is_list(list) do
|
||||
@@ -596,11 +488,8 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec ascii_printable?(list, 0) :: true
|
||||
@spec ascii_printable?([], limit) :: true
|
||||
when limit: :infinity | pos_integer
|
||||
@spec ascii_printable?([...], limit) :: boolean
|
||||
when limit: :infinity | pos_integer
|
||||
@spec ascii_printable?(list, limit) :: boolean
|
||||
when limit: :infinity | non_neg_integer
|
||||
def ascii_printable?(list, limit \\ :infinity)
|
||||
when is_list(list) and (limit == :infinity or (is_integer(limit) and limit >= 0)) do
|
||||
ascii_printable_guarded?(list, limit)
|
||||
@@ -611,9 +500,39 @@ defmodule List do
|
||||
end
|
||||
|
||||
defp ascii_printable_guarded?([char | rest], counter)
|
||||
# 7..13 is the range '\a\b\t\n\v\f\r'. 32..126 are ASCII printables.
|
||||
when is_integer(char) and
|
||||
((char >= 7 and char <= 13) or char == ?\e or (char >= 32 and char <= 126)) do
|
||||
when is_integer(char) and char >= 32 and char <= 126 do
|
||||
ascii_printable_guarded?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
defp ascii_printable_guarded?([?\n | rest], counter) do
|
||||
ascii_printable_guarded?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
defp ascii_printable_guarded?([?\r | rest], counter) do
|
||||
ascii_printable_guarded?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
defp ascii_printable_guarded?([?\t | rest], counter) do
|
||||
ascii_printable_guarded?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
defp ascii_printable_guarded?([?\v | rest], counter) do
|
||||
ascii_printable_guarded?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
defp ascii_printable_guarded?([?\b | rest], counter) do
|
||||
ascii_printable_guarded?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
defp ascii_printable_guarded?([?\f | rest], counter) do
|
||||
ascii_printable_guarded?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
defp ascii_printable_guarded?([?\e | rest], counter) do
|
||||
ascii_printable_guarded?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
defp ascii_printable_guarded?([?\a | rest], counter) do
|
||||
ascii_printable_guarded?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
@@ -629,11 +548,11 @@ defmodule List do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.improper?([1, 2 | 3])
|
||||
true
|
||||
iex> List.improper?([1, 2 | 3])
|
||||
true
|
||||
|
||||
iex> List.improper?([1, 2, 3])
|
||||
false
|
||||
iex> List.improper?([1, 2, 3])
|
||||
false
|
||||
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@@ -817,7 +736,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec starts_with?(nonempty_list, nonempty_list) :: boolean
|
||||
@spec starts_with?(list, list) :: boolean
|
||||
@spec starts_with?(list, []) :: true
|
||||
@spec starts_with?([], nonempty_list) :: false
|
||||
def starts_with?(list, prefix)
|
||||
@@ -830,7 +749,7 @@ defmodule List do
|
||||
Converts a charlist to an atom.
|
||||
|
||||
Elixir supports conversions from charlists which contains any Unicode
|
||||
code point.
|
||||
codepoint.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -853,7 +772,7 @@ defmodule List do
|
||||
if the atom does not exist.
|
||||
|
||||
Elixir supports conversions from charlists which contains any Unicode
|
||||
code point.
|
||||
codepoint.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -867,6 +786,9 @@ defmodule List do
|
||||
iex> List.to_existing_atom('🌢 Elixir')
|
||||
:"🌢 Elixir"
|
||||
|
||||
iex> List.to_existing_atom('this_atom_will_never_exist')
|
||||
** (ArgumentError) argument error
|
||||
|
||||
"""
|
||||
@spec to_existing_atom(charlist) :: atom
|
||||
def to_existing_atom(charlist) do
|
||||
@@ -910,8 +832,6 @@ defmodule List do
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
The base needs to be between `2` and `36`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.to_integer('3FF', 16)
|
||||
@@ -940,19 +860,12 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a list of integers representing code points, lists or
|
||||
Converts a list of integers representing codepoints, lists or
|
||||
strings into a string.
|
||||
|
||||
To be converted to a string, a list must either be empty or only
|
||||
contain the following elements:
|
||||
|
||||
* strings
|
||||
* integers representing Unicode code points
|
||||
* a list containing one of these three elements
|
||||
|
||||
Note that this function expects a list of integers representing
|
||||
Unicode code points. If you have a list of bytes, you must instead use
|
||||
the [`:binary` module](`:binary`).
|
||||
Notice that this function expects a list of integers representing
|
||||
UTF-8 codepoints. If you have a list of bytes, you must instead use
|
||||
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -965,9 +878,6 @@ defmodule List do
|
||||
iex> List.to_string([0x0064, "ee", ['p']])
|
||||
"deep"
|
||||
|
||||
iex> List.to_string([])
|
||||
""
|
||||
|
||||
"""
|
||||
@spec to_string(:unicode.charlist()) :: String.t()
|
||||
def to_string(list) when is_list(list) do
|
||||
@@ -978,12 +888,11 @@ defmodule List do
|
||||
raise ArgumentError, """
|
||||
cannot convert the given list to a string.
|
||||
|
||||
To be converted to a string, a list must either be empty or only
|
||||
contain the following elements:
|
||||
To be converted to a string, a list must contain only:
|
||||
|
||||
* strings
|
||||
* integers representing Unicode code points
|
||||
* a list containing one of these three elements
|
||||
* integers representing Unicode codepoints
|
||||
* or a list containing one of these three elements
|
||||
|
||||
Please check the given list or call inspect/1 to get the list representation, got:
|
||||
|
||||
@@ -1002,12 +911,12 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a list of integers representing Unicode code points, lists or
|
||||
Converts a list of integers representing codepoints, lists or
|
||||
strings into a charlist.
|
||||
|
||||
Note that this function expects a list of integers representing
|
||||
Unicode code points. If you have a list of bytes, you must instead use
|
||||
the [`:binary` module](`:binary`).
|
||||
Notice that this function expects a list of integers representing
|
||||
UTF-8 codepoints. If you have a list of bytes, you must instead use
|
||||
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1034,7 +943,7 @@ defmodule List do
|
||||
To be converted to a charlist, a list must contain only:
|
||||
|
||||
* strings
|
||||
* integers representing Unicode code points
|
||||
* integers representing Unicode codepoints
|
||||
* or a list containing one of these three elements
|
||||
|
||||
Please check the given list or call inspect/1 to get the list representation, got:
|
||||
|
||||
@@ -3,7 +3,7 @@ defprotocol List.Chars do
|
||||
The `List.Chars` protocol is responsible for
|
||||
converting a structure to a charlist (only if applicable).
|
||||
|
||||
The only function that must be implemented is
|
||||
The only function required to be implemented is
|
||||
`to_charlist/1` which does the conversion.
|
||||
|
||||
The `to_charlist/1` function automatically imported
|
||||
@@ -17,6 +17,7 @@ defprotocol List.Chars do
|
||||
def to_charlist(term)
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use List.Chars.to_charlist/1 instead"
|
||||
Kernel.def to_char_list(term) do
|
||||
__MODULE__.to_charlist(term)
|
||||
@@ -24,8 +25,6 @@ defprotocol List.Chars do
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: Atom do
|
||||
def to_charlist(nil), do: ''
|
||||
|
||||
def to_charlist(atom), do: Atom.to_charlist(atom)
|
||||
end
|
||||
|
||||
|
||||
+135
-563
File diff suppressed because it is too large
Load Diff
+79
-174
@@ -21,87 +21,104 @@ defmodule Macro.Env do
|
||||
|
||||
It contains the following fields:
|
||||
|
||||
* `context` - the context of the environment; it can be `nil`
|
||||
(default context), `:guard` (inside a guard) or `:match` (inside a match)
|
||||
* `context_modules` - a list of modules defined in the current context
|
||||
* `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
|
||||
* `line` - the current line as an integer
|
||||
* `module` - the current module name
|
||||
* `context` - the context of the environment; it can be `nil`
|
||||
(default context), `:guard` (inside a guard) or `:match` (inside a match)
|
||||
* `aliases` - a list of two-element tuples, where the first
|
||||
element is the aliased name and the second one 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
|
||||
* `lexical_tracker` - PID of the lexical tracker which is responsible for
|
||||
keeping user info
|
||||
|
||||
The following fields are private to Elixir's macro expansion mechanism and
|
||||
must not be accessed directly:
|
||||
The following fields pertain to variable handling and must not be accessed or
|
||||
relied on. To get a list of all variables, see `vars/1`:
|
||||
|
||||
* `aliases`
|
||||
* `functions`
|
||||
* `macro_aliases`
|
||||
* `macros`
|
||||
* `lexical_tracker`
|
||||
* `requires`
|
||||
* `tracers`
|
||||
* `versioned_vars`
|
||||
* `current_vars`
|
||||
* `unused_vars`
|
||||
* `prematch_vars`
|
||||
* `contextual_vars`
|
||||
|
||||
The following fields are deprecated and must not be accessed or relied on:
|
||||
|
||||
* `vars` - a list keeping all defined variables as `{var, context}`
|
||||
|
||||
"""
|
||||
|
||||
@type context :: :match | :guard | nil
|
||||
@type context_modules :: [module]
|
||||
@type name_arity :: {atom, arity}
|
||||
@type file :: binary
|
||||
@type line :: non_neg_integer
|
||||
@type name_arity :: {atom, arity}
|
||||
@type aliases :: [{module, module}]
|
||||
@type macro_aliases :: [{module, {term, module}}]
|
||||
@type context :: :match | :guard | nil
|
||||
@type requires :: [module]
|
||||
@type functions :: [{module, [name_arity]}]
|
||||
@type macros :: [{module, [name_arity]}]
|
||||
@type context_modules :: [module]
|
||||
@type lexical_tracker :: pid | nil
|
||||
@type variable :: {atom, atom | term}
|
||||
|
||||
@typep aliases :: [{module, module}]
|
||||
@typep functions :: [{module, [name_arity]}]
|
||||
@typep lexical_tracker :: pid | nil
|
||||
@typep macro_aliases :: [{module, {term, module}}]
|
||||
@typep macros :: [{module, [name_arity]}]
|
||||
@typep requires :: [module]
|
||||
@typep tracers :: [module]
|
||||
@typep versioned_vars :: %{optional(variable) => var_version :: non_neg_integer}
|
||||
@typep vars :: [variable]
|
||||
@typep var_type :: :term
|
||||
@typep var_version :: non_neg_integer
|
||||
@typep unused_vars :: %{{variable, var_version} => non_neg_integer | false}
|
||||
@typep current_vars :: %{variable => {var_version, var_type}}
|
||||
@typep prematch_vars :: current_vars | :warn | :raise | :pin | :apply
|
||||
@typep contextual_vars :: [atom]
|
||||
|
||||
@type t :: %{
|
||||
__struct__: __MODULE__,
|
||||
aliases: aliases,
|
||||
context: context,
|
||||
context_modules: context_modules,
|
||||
module: atom,
|
||||
file: file,
|
||||
function: name_arity | nil,
|
||||
functions: functions,
|
||||
lexical_tracker: lexical_tracker,
|
||||
line: line,
|
||||
macro_aliases: macro_aliases,
|
||||
macros: macros,
|
||||
module: module,
|
||||
function: name_arity | nil,
|
||||
context: context,
|
||||
requires: requires,
|
||||
tracers: tracers,
|
||||
versioned_vars: versioned_vars
|
||||
aliases: aliases,
|
||||
functions: functions,
|
||||
macros: macros,
|
||||
macro_aliases: aliases,
|
||||
context_modules: context_modules,
|
||||
vars: vars,
|
||||
unused_vars: unused_vars,
|
||||
current_vars: current_vars,
|
||||
prematch_vars: prematch_vars,
|
||||
lexical_tracker: lexical_tracker,
|
||||
contextual_vars: contextual_vars
|
||||
}
|
||||
|
||||
# Define the __struct__ callbacks by hand for bootstrap reasons.
|
||||
@doc false
|
||||
# TODO: Remove :vars field on v2.0
|
||||
def __struct__ do
|
||||
%{
|
||||
__struct__: __MODULE__,
|
||||
aliases: [],
|
||||
context: nil,
|
||||
context_modules: [],
|
||||
file: "nofile",
|
||||
function: nil,
|
||||
functions: [],
|
||||
lexical_tracker: nil,
|
||||
line: 0,
|
||||
macro_aliases: [],
|
||||
macros: [],
|
||||
module: nil,
|
||||
file: "nofile",
|
||||
line: 0,
|
||||
function: nil,
|
||||
context: nil,
|
||||
requires: [],
|
||||
tracers: [],
|
||||
versioned_vars: %{}
|
||||
aliases: [],
|
||||
functions: [],
|
||||
macros: [],
|
||||
macro_aliases: [],
|
||||
context_modules: [],
|
||||
vars: [],
|
||||
unused_vars: %{},
|
||||
current_vars: %{},
|
||||
prematch_vars: :warn,
|
||||
lexical_tracker: nil,
|
||||
contextual_vars: []
|
||||
}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __struct__(kv) do
|
||||
Enum.reduce(kv, __struct__(), fn {k, v}, acc -> :maps.update(k, v, acc) end)
|
||||
end
|
||||
@@ -118,30 +135,19 @@ defmodule Macro.Env do
|
||||
@spec vars(t) :: [variable]
|
||||
def vars(env)
|
||||
|
||||
def vars(%{__struct__: Macro.Env, versioned_vars: vars}) do
|
||||
Map.keys(vars)
|
||||
def vars(%{__struct__: Macro.Env, current_vars: current_vars}) do
|
||||
Map.keys(current_vars)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if a variable belongs to the environment.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> x = 13
|
||||
iex> x
|
||||
13
|
||||
iex> Macro.Env.has_var?(__ENV__, {:x, nil})
|
||||
true
|
||||
iex> Macro.Env.has_var?(__ENV__, {:unknown, nil})
|
||||
false
|
||||
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec has_var?(t, variable) :: boolean()
|
||||
def has_var?(env, var)
|
||||
|
||||
def has_var?(%{__struct__: Macro.Env, versioned_vars: vars}, var) do
|
||||
Map.has_key?(vars, var)
|
||||
def has_var?(%{__struct__: Macro.Env, current_vars: current_vars}, var) do
|
||||
Map.has_key?(current_vars, var)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -155,117 +161,16 @@ defmodule Macro.Env do
|
||||
[file: file, line: line]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Fetches the alias for the given atom.
|
||||
|
||||
Returns `{:ok, alias}` if the alias exists, `:error`
|
||||
otherwise.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> alias Foo.Bar, as: Baz
|
||||
iex> Baz
|
||||
Foo.Bar
|
||||
iex> Macro.Env.fetch_alias(__ENV__, :Baz)
|
||||
{:ok, Foo.Bar}
|
||||
iex> Macro.Env.fetch_alias(__ENV__, :Unknown)
|
||||
:error
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec fetch_alias(t, atom) :: {:ok, atom} | :error
|
||||
def fetch_alias(%{__struct__: Macro.Env, aliases: aliases}, atom) when is_atom(atom),
|
||||
do: Keyword.fetch(aliases, :"Elixir.#{atom}")
|
||||
|
||||
@doc """
|
||||
Fetches the macro alias for the given atom.
|
||||
|
||||
Returns `{:ok, macro_alias}` if the alias exists, `:error`
|
||||
otherwise.
|
||||
|
||||
A macro alias is only used inside quoted expansion. See
|
||||
`fetch_alias/2` for a more general example.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec fetch_macro_alias(t, atom) :: {:ok, atom} | :error
|
||||
def fetch_macro_alias(%{__struct__: Macro.Env, macro_aliases: aliases}, atom)
|
||||
when is_atom(atom),
|
||||
do: Keyword.fetch(aliases, :"Elixir.#{atom}")
|
||||
|
||||
@doc """
|
||||
Returns the modules from which the given `{name, arity}` was
|
||||
imported.
|
||||
|
||||
It returns a list of two element tuples in the shape of
|
||||
`{:function | :macro, module}`. The elements in the list
|
||||
are in no particular order and the order is not guaranteed.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
|
||||
[]
|
||||
iex> import Tuple, only: [duplicate: 2], warn: false
|
||||
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
|
||||
[{:function, Tuple}]
|
||||
iex> import List, only: [duplicate: 2], warn: false
|
||||
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
|
||||
[{:function, List}, {:function, Tuple}]
|
||||
|
||||
iex> Macro.Env.lookup_import(__ENV__, {:def, 1})
|
||||
[{:macro, Kernel}]
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec lookup_import(t, name_arity) :: [{:function | :macro, module}]
|
||||
def lookup_import(
|
||||
%{__struct__: Macro.Env, functions: functions, macros: macros},
|
||||
{name, arity} = pair
|
||||
)
|
||||
when is_atom(name) and is_integer(arity) do
|
||||
f = for {mod, pairs} <- functions, :ordsets.is_element(pair, pairs), do: {:function, mod}
|
||||
m = for {mod, pairs} <- macros, :ordsets.is_element(pair, pairs), do: {:macro, mod}
|
||||
f ++ m
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns true if the given module has been required.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.Env.required?(__ENV__, Integer)
|
||||
false
|
||||
iex> require Integer
|
||||
iex> Macro.Env.required?(__ENV__, Integer)
|
||||
true
|
||||
|
||||
iex> Macro.Env.required?(__ENV__, Kernel)
|
||||
true
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec required?(t, module) :: boolean
|
||||
def required?(%{__struct__: Macro.Env, requires: requires}, mod) when is_atom(mod),
|
||||
do: mod in requires
|
||||
|
||||
@doc """
|
||||
Prepend a tracer to the list of tracers in the environment.
|
||||
|
||||
## Examples
|
||||
|
||||
Macro.Env.prepend_tracer(__ENV__, MyCustomTracer)
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec prepend_tracer(t, module) :: t
|
||||
def prepend_tracer(%{__struct__: Macro.Env, tracers: tracers} = env, tracer) do
|
||||
%{env | tracers: [tracer | tracers]}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a `Macro.Env` in the match context.
|
||||
"""
|
||||
@spec to_match(t) :: t
|
||||
def to_match(%{__struct__: Macro.Env} = env) do
|
||||
%{env | context: :match}
|
||||
def to_match(%{__struct__: Macro.Env, context: :match} = env) do
|
||||
env
|
||||
end
|
||||
|
||||
def to_match(%{__struct__: Macro.Env, current_vars: vars} = env) do
|
||||
%{env | context: :match, prematch_vars: vars}
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
||||
+102
-277
@@ -1,9 +1,15 @@
|
||||
defmodule Map do
|
||||
@moduledoc """
|
||||
Maps are the "go to" key-value data structure in Elixir.
|
||||
A set of functions for working with maps.
|
||||
|
||||
Maps can be created with the `%{}` syntax, and key-value pairs can be
|
||||
expressed as `key => value`:
|
||||
Many functions for maps, which implement the `Enumerable` protocol,
|
||||
are found in the `Enum` module. Additionally, the following functions
|
||||
for maps are found in `Kernel`:
|
||||
|
||||
* `map_size/1`
|
||||
|
||||
Maps are the "go to" key-value data structure in Elixir. Maps can be created
|
||||
with the `%{}` syntax, and key-value pairs can be expressed as `key => value`:
|
||||
|
||||
iex> %{}
|
||||
%{}
|
||||
@@ -19,13 +25,8 @@ defmodule Map do
|
||||
in a map literal, the last one prevails.
|
||||
|
||||
When the key in a key-value pair is an atom, the `key: value` shorthand syntax
|
||||
can be used (as in many other special forms):
|
||||
|
||||
iex> %{a: 1, b: 2}
|
||||
%{a: 1, b: 2}
|
||||
|
||||
If you want to mix the shorthand syntax with `=>`, the shorthand syntax must come
|
||||
at the end:
|
||||
can be used (as in many other special forms), provided key-value pairs are put at
|
||||
the end:
|
||||
|
||||
iex> %{"hello" => "world", a: 1, b: 2}
|
||||
%{:a => 1, :b => 2, "hello" => "world"}
|
||||
@@ -42,20 +43,16 @@ defmodule Map do
|
||||
iex> map["non_existing_key"]
|
||||
nil
|
||||
|
||||
To access atom keys, one may also use the `map.key` notation. Note that `map.key`
|
||||
will raise a `KeyError` if the `map` doesn't contain the key `:key`, compared to
|
||||
`map[:key]`, that would return `nil`.
|
||||
For accessing atom keys, one may also `map.key`. Note that while `map[key]` will
|
||||
return `nil` if `map` doesn't contain `key`, `map.key` will raise if `map` doesn't
|
||||
contain the key `:key`.
|
||||
|
||||
map = %{foo: "bar", baz: "bong"}
|
||||
map.foo
|
||||
#=> "bar"
|
||||
map.non_existing_key
|
||||
iex> map = %{foo: "bar", baz: "bong"}
|
||||
iex> map.foo
|
||||
"bar"
|
||||
iex> map.non_existing_key
|
||||
** (KeyError) key :non_existing_key not found in: %{baz: "bong", foo: "bar"}
|
||||
|
||||
> Note: do not add parens when accessing fields, such as in `data.key()`.
|
||||
> If parenthesis are used, Elixir will expect `data` to be an atom representing
|
||||
> a module and attempt to call the *function* `key/0` in it.
|
||||
|
||||
The two syntaxes for accessing keys reveal the dual nature of maps. The `map[key]`
|
||||
syntax is used for dynamically created maps that may have any key, of any type.
|
||||
`map.key` is used with maps that hold a predetermined set of atoms keys, which are
|
||||
@@ -72,10 +69,8 @@ defmodule Map do
|
||||
iex> %{a: a} = %{:a => 1, "b" => 2, [:c, :e, :e] => 3}
|
||||
iex> a
|
||||
1
|
||||
|
||||
But this will raise a `MatchError` exception:
|
||||
|
||||
%{:c => 3} = %{:a => 1, 2 => :b}
|
||||
iex> %{:c => 3} = %{:a => 1, 2 => :b}
|
||||
** (MatchError) no match of right hand side value: %{2 => :b, :a => 1}
|
||||
|
||||
Variables can be used as map keys both when writing map literals as well as
|
||||
when matching:
|
||||
@@ -93,23 +88,8 @@ defmodule Map do
|
||||
iex> map = %{one: 1, two: 2}
|
||||
iex> %{map | one: "one"}
|
||||
%{one: "one", two: 2}
|
||||
|
||||
When a key that does not exist in the map is updated a `KeyError` exception will be raised:
|
||||
|
||||
%{map | three: 3}
|
||||
|
||||
The functions in this module that need to find a specific key work in logarithmic time.
|
||||
This means that the time it takes to find keys grows as the map grows, but it's not
|
||||
directly proportional to the map size. In comparison to finding an element in a list,
|
||||
it performs better because lists have a linear time complexity. Some functions,
|
||||
such as `keys/1` and `values/1`, run in linear time because they need to get to every
|
||||
element in the map.
|
||||
|
||||
Maps also implement the `Enumerable` protocol, so many functions to work with maps
|
||||
are found in the `Enum` module. Additionally, the following functions for maps are
|
||||
found in `Kernel`:
|
||||
|
||||
* `map_size/1`
|
||||
iex> %{map | three: 3}
|
||||
** (KeyError) key :three not found
|
||||
|
||||
"""
|
||||
|
||||
@@ -216,10 +196,20 @@ defmodule Map do
|
||||
@spec new(Enumerable.t(), (term -> {key, value})) :: map
|
||||
def new(enumerable, transform) when is_function(transform, 1) do
|
||||
enumerable
|
||||
|> Enum.map(transform)
|
||||
|> Enum.to_list()
|
||||
|> new_transform(transform, [])
|
||||
end
|
||||
|
||||
defp new_transform([], _fun, acc) do
|
||||
acc
|
||||
|> :lists.reverse()
|
||||
|> :maps.from_list()
|
||||
end
|
||||
|
||||
defp new_transform([item | rest], fun, acc) do
|
||||
new_transform(rest, fun, [fun.(item) | acc])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns whether the given `key` exists in the given `map`.
|
||||
|
||||
@@ -239,8 +229,8 @@ defmodule Map do
|
||||
@doc """
|
||||
Fetches the value for a specific `key` in the given `map`.
|
||||
|
||||
If `map` contains the given `key` then its value is returned in the shape of `{:ok, value}`.
|
||||
If `map` doesn't contain `key`, `:error` is returned.
|
||||
If `map` contains the given `key` with value `value`, then `{:ok, value}` is
|
||||
returned. If `map` doesn't contain `key`, `:error` is returned.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -259,7 +249,7 @@ defmodule Map do
|
||||
Fetches the value for a specific `key` in the given `map`, erroring out if
|
||||
`map` doesn't contain `key`.
|
||||
|
||||
If `map` contains `key`, the corresponding value is returned. If
|
||||
If `map` contains the given `key`, the corresponding value is returned. If
|
||||
`map` doesn't contain `key`, a `KeyError` exception is raised.
|
||||
|
||||
Inlined by the compiler.
|
||||
@@ -268,6 +258,8 @@ defmodule Map do
|
||||
|
||||
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
|
||||
@@ -301,24 +293,12 @@ defmodule Map do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts a value under `key` only if the `key` already exists in `map`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.replace(%{a: 1, b: 2}, :a, 3)
|
||||
%{a: 3, b: 2}
|
||||
|
||||
iex> Map.replace(%{a: 1}, :b, 2)
|
||||
%{a: 1}
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec replace(map, key, value) :: map
|
||||
@doc false
|
||||
@deprecated "Use Map.fetch/2 + Map.put/3 instead"
|
||||
def replace(map, key, value) do
|
||||
case map do
|
||||
%{^key => _value} ->
|
||||
%{map | key => value}
|
||||
put(map, key, value)
|
||||
|
||||
%{} ->
|
||||
map
|
||||
@@ -329,7 +309,8 @@ defmodule Map do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts a value under `key` only if the `key` already exists in `map`.
|
||||
Alters the value stored under `key` to `value`, but only
|
||||
if the entry `key` already exists in `map`.
|
||||
|
||||
If `key` is not present in `map`, a `KeyError` exception is raised.
|
||||
|
||||
@@ -355,8 +336,8 @@ defmodule Map do
|
||||
in `map` unless `key` is already present.
|
||||
|
||||
This function is useful in case you want to compute the value to put under
|
||||
`key` only if `key` is not already present, as for example, when the value is expensive to
|
||||
calculate or generally difficult to setup and teardown again.
|
||||
`key` only if `key` is not already present (e.g., the value is expensive to
|
||||
calculate or generally difficult to setup and teardown again).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -397,20 +378,13 @@ defmodule Map do
|
||||
%{a: 1, c: 3}
|
||||
|
||||
"""
|
||||
@spec take(map, [key]) :: map
|
||||
@spec take(map, Enumerable.t()) :: map
|
||||
def take(map, keys)
|
||||
|
||||
def take(map, keys) when is_map(map) and is_list(keys) do
|
||||
take(keys, map, _acc = [])
|
||||
end
|
||||
|
||||
def take(map, keys) when is_map(map) do
|
||||
IO.warn(
|
||||
"Map.take/2 with an Enumerable of keys that is not a list is deprecated. " <>
|
||||
" Use a list of keys instead."
|
||||
)
|
||||
|
||||
take(map, Enum.to_list(keys))
|
||||
keys
|
||||
|> Enum.to_list()
|
||||
|> take(map, [])
|
||||
end
|
||||
|
||||
def take(non_map, _keys) do
|
||||
@@ -434,10 +408,9 @@ defmodule Map do
|
||||
@doc """
|
||||
Gets the value for a specific `key` in `map`.
|
||||
|
||||
If `key` is present in `map` then its value `value` is
|
||||
returned. Otherwise, `default` is returned.
|
||||
|
||||
If `default` is not provided, `nil` is used.
|
||||
If `key` is present in `map` with value `value`, then `value` is
|
||||
returned. Otherwise, `default` is returned (which is `nil` unless
|
||||
specified otherwise).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -468,7 +441,7 @@ defmodule Map do
|
||||
@doc """
|
||||
Gets the value for a specific `key` in `map`.
|
||||
|
||||
If `key` is present in `map` then its value `value` is
|
||||
If `key` is present in `map` with value `value`, then `value` is
|
||||
returned. Otherwise, `fun` is evaluated and its result is returned.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
@@ -596,39 +569,38 @@ defmodule Map do
|
||||
@doc """
|
||||
Updates the `key` in `map` with the given function.
|
||||
|
||||
If `key` is present in `map` then the existing value is passed to `fun` and its result is
|
||||
used as the updated value of `key`. If `key` is
|
||||
not present in `map`, `default` is inserted as the value of `key`. The default
|
||||
If `key` is present in `map` with value `value`, `fun` is invoked with
|
||||
argument `value` and its result is used as the new value of `key`. If `key` is
|
||||
not present in `map`, `initial` is inserted as the value of `key`. The initial
|
||||
value will not be passed through the update function.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.update(%{a: 1}, :a, 13, fn existing_value -> existing_value * 2 end)
|
||||
iex> Map.update(%{a: 1}, :a, 13, &(&1 * 2))
|
||||
%{a: 2}
|
||||
iex> Map.update(%{a: 1}, :b, 11, fn existing_value -> existing_value * 2 end)
|
||||
iex> Map.update(%{a: 1}, :b, 11, &(&1 * 2))
|
||||
%{a: 1, b: 11}
|
||||
|
||||
"""
|
||||
@spec update(map, key, default :: value, (existing_value :: value -> new_value :: value)) ::
|
||||
map
|
||||
def update(map, key, default, fun) when is_function(fun, 1) do
|
||||
@spec update(map, key, value, (value -> value)) :: map
|
||||
def update(map, key, initial, fun) when is_function(fun, 1) do
|
||||
case map do
|
||||
%{^key => value} ->
|
||||
%{map | key => fun.(value)}
|
||||
put(map, key, fun.(value))
|
||||
|
||||
%{} ->
|
||||
put(map, key, default)
|
||||
put(map, key, initial)
|
||||
|
||||
other ->
|
||||
:erlang.error({:badmap, other}, [map, key, default, fun])
|
||||
:erlang.error({:badmap, other}, [map, key, initial, fun])
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes the value associated with `key` in `map` and returns the value and the updated map.
|
||||
Returns and removes the value associated with `key` in `map`.
|
||||
|
||||
If `key` is present in `map`, it returns `{value, updated_map}` where `value` is the value of
|
||||
the key and `updated_map` is the result of removing `key` from `map`. If `key`
|
||||
If `key` is present in `map` with value `value`, `{value, new_map}` is
|
||||
returned where `new_map` is the result of removing `key` from `map`. If `key`
|
||||
is not present in `map`, `{default, map}` is returned.
|
||||
|
||||
## Examples
|
||||
@@ -641,7 +613,7 @@ defmodule Map do
|
||||
{3, %{a: 1}}
|
||||
|
||||
"""
|
||||
@spec pop(map, key, default) :: {value, updated_map :: map} | {default, map} when default: value
|
||||
@spec pop(map, key, value) :: {value, map}
|
||||
def pop(map, key, default \\ nil) do
|
||||
case :maps.take(key, map) do
|
||||
{_, _} = tuple -> tuple
|
||||
@@ -649,36 +621,11 @@ defmodule Map do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes the value associated with `key` in `map` and returns the value
|
||||
and the updated map, or it raises if `key` is not present.
|
||||
|
||||
Behaves the same as `pop/3` but raises if `key` is not present in `map`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.pop!(%{a: 1}, :a)
|
||||
{1, %{}}
|
||||
iex> Map.pop!(%{a: 1, b: 2}, :a)
|
||||
{1, %{b: 2}}
|
||||
iex> Map.pop!(%{a: 1}, :b)
|
||||
** (KeyError) key :b not found in: %{a: 1}
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec pop!(map, key) :: {value, updated_map :: map}
|
||||
def pop!(map, key) do
|
||||
case :maps.take(key, map) do
|
||||
{_, _} = tuple -> tuple
|
||||
:error -> raise KeyError, key: key, term: map
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily returns and removes the value associated with `key` in `map`.
|
||||
|
||||
If `key` is present in `map`, it returns `{value, new_map}` where `value` is the value of
|
||||
the key and `new_map` is the result of removing `key` from `map`. If `key`
|
||||
If `key` is present in `map` with value `value`, `{value, new_map}` is
|
||||
returned where `new_map` is the result of removing `key` from `map`. If `key`
|
||||
is not present in `map`, `{fun_result, map}` is returned, where `fun_result`
|
||||
is the result of applying `fun`.
|
||||
|
||||
@@ -700,9 +647,15 @@ defmodule Map do
|
||||
"""
|
||||
@spec pop_lazy(map, key, (() -> value)) :: {value, map}
|
||||
def pop_lazy(map, key, fun) when is_function(fun, 0) do
|
||||
case :maps.take(key, map) do
|
||||
{_, _} = tuple -> tuple
|
||||
:error -> {fun.(), map}
|
||||
case map do
|
||||
%{^key => value} ->
|
||||
{value, delete(map, key)}
|
||||
|
||||
%{} ->
|
||||
{fun.(), map}
|
||||
|
||||
other ->
|
||||
:erlang.error({:badmap, other}, [map, key, fun])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -717,30 +670,23 @@ defmodule Map do
|
||||
%{a: 1, c: 3}
|
||||
|
||||
"""
|
||||
@spec drop(map, [key]) :: map
|
||||
@spec drop(map, Enumerable.t()) :: map
|
||||
def drop(map, keys)
|
||||
|
||||
def drop(map, keys) when is_map(map) and is_list(keys) do
|
||||
drop_keys(keys, map)
|
||||
end
|
||||
|
||||
def drop(map, keys) when is_map(map) do
|
||||
IO.warn(
|
||||
"Map.drop/2 with an Enumerable of keys that is not a list is deprecated. " <>
|
||||
" Use a list of keys instead."
|
||||
)
|
||||
|
||||
drop(map, Enum.to_list(keys))
|
||||
keys
|
||||
|> Enum.to_list()
|
||||
|> drop_list(map)
|
||||
end
|
||||
|
||||
def drop(non_map, keys) do
|
||||
:erlang.error({:badmap, non_map}, [non_map, keys])
|
||||
end
|
||||
|
||||
defp drop_keys([], acc), do: acc
|
||||
defp drop_list([], acc), do: acc
|
||||
|
||||
defp drop_keys([key | rest], acc) do
|
||||
drop_keys(rest, delete(acc, key))
|
||||
defp drop_list([key | rest], acc) do
|
||||
drop_list(rest, delete(acc, key))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -757,20 +703,13 @@ defmodule Map do
|
||||
{%{a: 1, c: 3}, %{b: 2}}
|
||||
|
||||
"""
|
||||
@spec split(map, [key]) :: {map, map}
|
||||
@spec split(map, Enumerable.t()) :: {map, map}
|
||||
def split(map, keys)
|
||||
|
||||
def split(map, keys) when is_map(map) and is_list(keys) do
|
||||
split(keys, [], map)
|
||||
end
|
||||
|
||||
def split(map, keys) when is_map(map) do
|
||||
IO.warn(
|
||||
"Map.split/2 with an Enumerable of keys that is not a list is deprecated. " <>
|
||||
" Use a list of keys instead."
|
||||
)
|
||||
|
||||
split(map, Enum.to_list(keys))
|
||||
keys
|
||||
|> Enum.to_list()
|
||||
|> split([], map)
|
||||
end
|
||||
|
||||
def split(non_map, keys) do
|
||||
@@ -794,8 +733,8 @@ defmodule Map do
|
||||
@doc """
|
||||
Updates `key` with the given function.
|
||||
|
||||
If `key` is present in `map` then the existing value is passed to `fun` and its result is
|
||||
used as the updated value of `key`. If `key` is
|
||||
If `key` is present in `map` with value `value`, `fun` is invoked with
|
||||
argument `value` and its result is used as the new value of `key`. If `key` is
|
||||
not present in `map`, a `KeyError` exception is raised.
|
||||
|
||||
## Examples
|
||||
@@ -807,23 +746,23 @@ defmodule Map do
|
||||
** (KeyError) key :b not found in: %{a: 1}
|
||||
|
||||
"""
|
||||
@spec update!(map, key, (existing_value :: value -> new_value :: value)) :: map
|
||||
@spec update!(map, key, (value -> value)) :: map
|
||||
def update!(map, key, fun) when is_function(fun, 1) do
|
||||
value = fetch!(map, key)
|
||||
%{map | key => fun.(value)}
|
||||
put(map, key, fun.(value))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value from `key` and updates it, all in one pass.
|
||||
|
||||
`fun` is called with the current value under `key` in `map` (or `nil` if `key`
|
||||
is not present in `map`) and must return a two-element tuple: the current value
|
||||
is not present in `map`) and must return a two-element tuple: the "get" value
|
||||
(the retrieved value, which can be operated on before being returned) and the
|
||||
new value to be stored under `key` in the resulting new map. `fun` may also
|
||||
return `:pop`, which means the current value shall be removed from `map` and
|
||||
returned (making this function behave like `Map.pop(map, key)`).
|
||||
|
||||
The returned value is a two-element tuple with the current value returned by
|
||||
The returned value is a tuple with the "get" value returned by
|
||||
`fun` and a new map with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
@@ -836,7 +775,7 @@ defmodule Map do
|
||||
iex> Map.get_and_update(%{a: 1}, :b, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{nil, %{a: 1, b: "new value!"}}
|
||||
{nil, %{b: "new value!", a: 1}}
|
||||
|
||||
iex> Map.get_and_update(%{a: 1}, :a, fn _ -> :pop end)
|
||||
{1, %{}}
|
||||
@@ -845,9 +784,7 @@ defmodule Map do
|
||||
{nil, %{a: 1}}
|
||||
|
||||
"""
|
||||
@spec get_and_update(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, new_map :: map}
|
||||
when current_value: value
|
||||
@spec get_and_update(map, key, (value -> {get, value} | :pop)) :: {get, map} when get: term
|
||||
def get_and_update(map, key, fun) when is_function(fun, 1) do
|
||||
current = get(map, key)
|
||||
|
||||
@@ -864,7 +801,7 @@ defmodule Map do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value from `key` and updates it, all in one pass. Raises if there is no `key`.
|
||||
Gets the value from `key` and updates it. Raises if there is no `key`.
|
||||
|
||||
Behaves exactly like `get_and_update/3`, but raises a `KeyError` exception if
|
||||
`key` is not present in `map`.
|
||||
@@ -887,15 +824,14 @@ defmodule Map do
|
||||
{1, %{}}
|
||||
|
||||
"""
|
||||
@spec get_and_update!(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, map}
|
||||
when current_value: value
|
||||
@spec get_and_update!(map, key, (value -> {get, value} | :pop)) :: {get, map}
|
||||
when get: term
|
||||
def get_and_update!(map, key, fun) when is_function(fun, 1) do
|
||||
value = fetch!(map, key)
|
||||
|
||||
case fun.(value) do
|
||||
{get, update} ->
|
||||
{get, %{map | key => update}}
|
||||
{get, put(map, key, update)}
|
||||
|
||||
:pop ->
|
||||
{value, delete(map, key)}
|
||||
@@ -940,11 +876,6 @@ defmodule Map do
|
||||
Two maps are considered to be equal if they contain
|
||||
the same keys and those keys contain the same values.
|
||||
|
||||
Note this function exists for completeness so the `Map`
|
||||
and `Keyword` modules provide similar APIs. In practice,
|
||||
developers often compare maps using `==/2` or `===/2`
|
||||
directly.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.equal?(%{a: 1, b: 2}, %{b: 2, a: 1})
|
||||
@@ -952,12 +883,6 @@ defmodule Map do
|
||||
iex> Map.equal?(%{a: 1, b: 2}, %{b: 1, a: 2})
|
||||
false
|
||||
|
||||
Comparison between keys and values is done with `===/3`,
|
||||
which means integers are not equivalent to floats:
|
||||
|
||||
iex> Map.equal?(%{a: 1.0}, %{a: 1})
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(map, map) :: boolean
|
||||
def equal?(map1, map2)
|
||||
@@ -967,109 +892,9 @@ defmodule Map do
|
||||
def equal?(term, other), do: :erlang.error({:badmap, term}, [term, other])
|
||||
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
@deprecated "Use Kernel.map_size/1 instead"
|
||||
def size(map) do
|
||||
map_size(map)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a map containing only those pairs from `map`
|
||||
for which `fun` returns a truthy value.
|
||||
|
||||
`fun` receives the key and value of each of the
|
||||
elements in the map as a key-value pair.
|
||||
|
||||
See also `reject/2` which discards all elements where the
|
||||
function returns a truthy value.
|
||||
|
||||
> Note: if you find yourself doing multiple calls to `Map.map/2`
|
||||
> and `Map.filter/2` in a pipeline, it is likely more efficient
|
||||
> to use `Enum.map/2` and `Enum.filter/2` instead and convert to
|
||||
> a map at the end using `Map.new/1`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.filter(%{one: 1, two: 2, three: 3}, fn {_key, val} -> rem(val, 2) == 1 end)
|
||||
%{one: 1, three: 3}
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec filter(map, ({key, value} -> as_boolean(term))) :: map
|
||||
def filter(map, fun) when is_map(map) and is_function(fun, 1) do
|
||||
iter = :maps.iterator(map)
|
||||
next = :maps.next(iter)
|
||||
:maps.from_list(do_filter(next, fun))
|
||||
end
|
||||
|
||||
defp do_filter(:none, _fun), do: []
|
||||
|
||||
defp do_filter({key, value, iter}, fun) do
|
||||
if fun.({key, value}) do
|
||||
[{key, value} | do_filter(:maps.next(iter), fun)]
|
||||
else
|
||||
do_filter(:maps.next(iter), fun)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns map excluding the pairs from `map` for which `fun` returns
|
||||
a truthy value.
|
||||
|
||||
See also `filter/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.reject(%{one: 1, two: 2, three: 3}, fn {_key, val} -> rem(val, 2) == 1 end)
|
||||
%{two: 2}
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec reject(map, ({key, value} -> as_boolean(term))) :: map
|
||||
def reject(map, fun) when is_map(map) and is_function(fun, 1) do
|
||||
iter = :maps.iterator(map)
|
||||
next = :maps.next(iter)
|
||||
:maps.from_list(do_reject(next, fun))
|
||||
end
|
||||
|
||||
defp do_reject(:none, _fun), do: []
|
||||
|
||||
defp do_reject({key, value, iter}, fun) do
|
||||
if fun.({key, value}) do
|
||||
do_reject(:maps.next(iter), fun)
|
||||
else
|
||||
[{key, value} | do_reject(:maps.next(iter), fun)]
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Maps the function `fun` over all key-value pairs in `map`
|
||||
|
||||
It returns a map with all the values replaced with the result
|
||||
of the function.
|
||||
|
||||
> Note: if you find yourself doing multiple calls to `Map.map/2`
|
||||
> and `Map.filter/2` in a pipeline, it is likely more efficient
|
||||
> to use `Enum.map/2` and `Enum.filter/2` instead and convert to
|
||||
> a map at the end using `Map.new/1`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.map(%{1 => "joe", 2 => "mike", 3 => "robert"}, fn {_key, val} -> String.capitalize(val) end)
|
||||
%{1 => "Joe", 2 => "Mike", 3 => "Robert"}
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec map(map, ({key, value} -> value)) :: map
|
||||
def map(map, fun) when is_map(map) and is_function(fun, 1) do
|
||||
iter = :maps.iterator(map)
|
||||
next = :maps.next(iter)
|
||||
:maps.from_list(do_map(next, fun))
|
||||
end
|
||||
|
||||
defp do_map(:none, _fun), do: []
|
||||
|
||||
defp do_map({key, value, iter}, fun) do
|
||||
new_value = fun.({key, value})
|
||||
[{key, new_value} | do_map(:maps.next(iter), fun)]
|
||||
end
|
||||
end
|
||||
|
||||
+46
-66
@@ -2,26 +2,14 @@ defmodule MapSet do
|
||||
@moduledoc """
|
||||
Functions that work on sets.
|
||||
|
||||
A set is a data structure that can contain unique elements of any kind,
|
||||
without any particular order. `MapSet` is the "go to" set data structure in Elixir.
|
||||
|
||||
A set can be constructed using `MapSet.new/0`:
|
||||
`MapSet` is the "go to" set data structure in Elixir. A set can be constructed
|
||||
using `MapSet.new/0`:
|
||||
|
||||
iex> MapSet.new()
|
||||
#MapSet<[]>
|
||||
|
||||
Elements in a set don't have to be of the same type and they can be
|
||||
populated from an [enumerable](`t:Enumerable.t/0`) using `MapSet.new/1`:
|
||||
|
||||
iex> MapSet.new([1, :two, {"three"}])
|
||||
#MapSet<[1, :two, {"three"}]>
|
||||
|
||||
Elements can be inserted using `MapSet.put/2`:
|
||||
|
||||
iex> MapSet.new([2]) |> MapSet.put(4) |> MapSet.put(0)
|
||||
#MapSet<[0, 2, 4]>
|
||||
|
||||
By definition, sets can't contain duplicate elements: when
|
||||
A set can contain any kind of elements, and elements in a set don't have to be
|
||||
of the same type. By definition, sets can't contain duplicate elements: when
|
||||
inserting an element in a set where it's already present, the insertion is
|
||||
simply a no-op.
|
||||
|
||||
@@ -42,10 +30,6 @@ defmodule MapSet do
|
||||
|
||||
`MapSet`s can also be constructed starting from other collection-type data
|
||||
structures: for example, see `MapSet.new/1` or `Enum.into/2`.
|
||||
|
||||
`MapSet` is built on top of `Map`, this means that they share many properties,
|
||||
including logarithmic time complexity. See the documentation for `Map` for more
|
||||
information on its execution time complexity.
|
||||
"""
|
||||
|
||||
# MapSets have an underlying Map. MapSet elements are keys of said map,
|
||||
@@ -57,7 +41,7 @@ defmodule MapSet do
|
||||
@opaque t(value) :: %__MODULE__{map: %{optional(value) => []}}
|
||||
@type t :: t(term)
|
||||
|
||||
# TODO: Remove version key on Elixir v2.0
|
||||
# TODO: Remove version key on Elixir 2.0
|
||||
defstruct map: %{}, version: 2
|
||||
|
||||
@doc """
|
||||
@@ -117,19 +101,19 @@ defmodule MapSet do
|
||||
end
|
||||
|
||||
defp new_from_list([], acc) do
|
||||
Map.new(acc)
|
||||
:maps.from_list(acc)
|
||||
end
|
||||
|
||||
defp new_from_list([element | rest], acc) do
|
||||
new_from_list(rest, [{element, @dummy_value} | acc])
|
||||
defp new_from_list([item | rest], acc) do
|
||||
new_from_list(rest, [{item, @dummy_value} | acc])
|
||||
end
|
||||
|
||||
defp new_from_list_transform([], _fun, acc) do
|
||||
Map.new(acc)
|
||||
:maps.from_list(acc)
|
||||
end
|
||||
|
||||
defp new_from_list_transform([element | rest], fun, acc) do
|
||||
new_from_list_transform(rest, fun, [{fun.(element), @dummy_value} | acc])
|
||||
defp new_from_list_transform([item | rest], fun, acc) do
|
||||
new_from_list_transform(rest, fun, [{fun.(item), @dummy_value} | acc])
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -163,33 +147,32 @@ defmodule MapSet do
|
||||
@spec difference(t(val1), t(val2)) :: t(val1) when val1: value, val2: value
|
||||
def difference(map_set1, map_set2)
|
||||
|
||||
# If the first set is less than twice the size of the second map, it is fastest
|
||||
# to re-accumulate elements in the first set that are not present in the second set.
|
||||
# If the first set is less than twice the size of the second map,
|
||||
# it is fastest to re-accumulate items in the first set that are not
|
||||
# present in the second set.
|
||||
def difference(%MapSet{map: map1}, %MapSet{map: map2})
|
||||
when map_size(map1) < map_size(map2) * 2 do
|
||||
map =
|
||||
map1
|
||||
|> :maps.iterator()
|
||||
|> :maps.next()
|
||||
|> Map.keys()
|
||||
|> filter_not_in(map2, [])
|
||||
|
||||
%MapSet{map: map}
|
||||
end
|
||||
|
||||
# If the second set is less than half the size of the first set, it's fastest
|
||||
# to simply iterate through each element in the second set, deleting them from
|
||||
# to simply iterate through each item in the second set, deleting them from
|
||||
# the first set.
|
||||
def difference(%MapSet{map: map1} = map_set, %MapSet{map: map2}) do
|
||||
%{map_set | map: Map.drop(map1, Map.keys(map2))}
|
||||
end
|
||||
|
||||
defp filter_not_in(:none, _map2, acc), do: Map.new(acc)
|
||||
defp filter_not_in([], _map2, acc), do: :maps.from_list(acc)
|
||||
|
||||
defp filter_not_in({key, _val, iter}, map2, acc) do
|
||||
if :erlang.is_map_key(key, map2) do
|
||||
filter_not_in(:maps.next(iter), map2, acc)
|
||||
else
|
||||
filter_not_in(:maps.next(iter), map2, [{key, @dummy_value} | acc])
|
||||
defp filter_not_in([key | rest], map2, acc) do
|
||||
case map2 do
|
||||
%{^key => _} -> filter_not_in(rest, map2, acc)
|
||||
_ -> filter_not_in(rest, map2, [{key, @dummy_value} | acc])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -209,23 +192,25 @@ defmodule MapSet do
|
||||
{map1, map2} = order_by_size(map1, map2)
|
||||
|
||||
map1
|
||||
|> :maps.iterator()
|
||||
|> :maps.next()
|
||||
|> Map.keys()
|
||||
|> none_in?(map2)
|
||||
end
|
||||
|
||||
defp none_in?(:none, _), do: true
|
||||
defp none_in?([], _) do
|
||||
true
|
||||
end
|
||||
|
||||
defp none_in?({key, _val, iter}, map2) do
|
||||
not :erlang.is_map_key(key, map2) and none_in?(:maps.next(iter), map2)
|
||||
defp none_in?([key | rest], map2) do
|
||||
case map2 do
|
||||
%{^key => _} -> false
|
||||
_ -> none_in?(rest, map2)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two sets are equal.
|
||||
|
||||
The comparison between elements is done using `===/2`,
|
||||
which a set with `1` is not equivalent to a set with
|
||||
`1.0`.
|
||||
The comparison between elements must be done using `===/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -233,19 +218,17 @@ defmodule MapSet do
|
||||
true
|
||||
iex> MapSet.equal?(MapSet.new([1, 2]), MapSet.new([3, 4]))
|
||||
false
|
||||
iex> MapSet.equal?(MapSet.new([1]), MapSet.new([1.0]))
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(t, t) :: boolean
|
||||
def equal?(%MapSet{map: map1, version: version}, %MapSet{map: map2, version: version}) do
|
||||
map1 === map2
|
||||
Map.equal?(map1, map2)
|
||||
end
|
||||
|
||||
# Elixir v1.5 changed the map representation, so on
|
||||
# Elixir v1.5 change the map representation, so on
|
||||
# version mismatch we need to compare the keys directly.
|
||||
def equal?(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
map_size(map1) == map_size(map2) and all_in?(map1, map2)
|
||||
map_size(map1) == map_size(map2) and map_subset?(Map.keys(map1), map2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -279,7 +262,7 @@ defmodule MapSet do
|
||||
"""
|
||||
@spec member?(t, value) :: boolean
|
||||
def member?(%MapSet{map: map}, value) do
|
||||
:erlang.is_map_key(value, map)
|
||||
match?(%{^value => _}, map)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -327,20 +310,19 @@ defmodule MapSet do
|
||||
"""
|
||||
@spec subset?(t, t) :: boolean
|
||||
def subset?(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
map_size(map1) <= map_size(map2) and all_in?(map1, map2)
|
||||
if map_size(map1) <= map_size(map2) do
|
||||
map1
|
||||
|> Map.keys()
|
||||
|> map_subset?(map2)
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
defp all_in?(:none, _), do: true
|
||||
defp map_subset?([], _), do: true
|
||||
|
||||
defp all_in?({key, _val, iter}, map2) do
|
||||
:erlang.is_map_key(key, map2) and all_in?(:maps.next(iter), map2)
|
||||
end
|
||||
|
||||
defp all_in?(map1, map2) when is_map(map1) and is_map(map2) do
|
||||
map1
|
||||
|> :maps.iterator()
|
||||
|> :maps.next()
|
||||
|> all_in?(map2)
|
||||
defp map_subset?([key | rest], map2) do
|
||||
match?(%{^key => _}, map2) and map_subset?(rest, map2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -392,8 +374,7 @@ defmodule MapSet do
|
||||
end
|
||||
|
||||
def slice(map_set) do
|
||||
size = MapSet.size(map_set)
|
||||
{:ok, size, &Enumerable.List.slice(MapSet.to_list(map_set), &1, &2, size)}
|
||||
{:ok, MapSet.size(map_set), &Enumerable.List.slice(MapSet.to_list(map_set), &1, &2)}
|
||||
end
|
||||
|
||||
def reduce(map_set, acc, fun) do
|
||||
@@ -402,7 +383,6 @@ defmodule MapSet do
|
||||
end
|
||||
|
||||
defimpl Collectable do
|
||||
# TODO: Optimize into an empty mapset by using :maps.from_keys/2 on Erlang/OTP 24+
|
||||
def into(map_set) do
|
||||
fun = fn
|
||||
list, {:cont, x} -> [{x, []} | list]
|
||||
|
||||
+157
-567
File diff suppressed because it is too large
Load Diff
@@ -8,21 +8,19 @@
|
||||
# resembling a graph. The keys and what they point to are:
|
||||
#
|
||||
# * `:reattach` points to `{name, arity}`
|
||||
# * `{:local, {name, arity}}` points to `{{name, arity}, line, macro_dispatch?}`
|
||||
# * `{:local, {name, arity}}` points to `{name, arity}`
|
||||
# * `{:import, {name, arity}}` points to `Module`
|
||||
#
|
||||
# This is built on top of the internal module tables.
|
||||
defmodule Module.LocalsTracker do
|
||||
@moduledoc false
|
||||
|
||||
@defmacros [:defmacro, :defmacrop]
|
||||
|
||||
@doc """
|
||||
Adds and tracks defaults for a definition into the tracker.
|
||||
"""
|
||||
def add_defaults({_set, bag}, kind, {name, arity} = pair, defaults, meta) do
|
||||
def add_defaults({_set, bag}, _kind, {name, arity} = pair, defaults, meta) do
|
||||
for i <- :lists.seq(arity - defaults, arity - 1) do
|
||||
put_edge(bag, {:local, {name, i}}, {pair, get_line(meta), kind in @defmacros})
|
||||
put_edge(bag, {:local, {name, i}}, {pair, get_line(meta)})
|
||||
end
|
||||
|
||||
:ok
|
||||
@@ -31,9 +29,11 @@ defmodule Module.LocalsTracker do
|
||||
@doc """
|
||||
Adds a local dispatch from-to the given target.
|
||||
"""
|
||||
def add_local({_set, bag}, from, to, meta, macro_dispatch?)
|
||||
when is_tuple(from) and is_tuple(to) and is_boolean(macro_dispatch?) do
|
||||
put_edge(bag, {:local, from}, {to, get_line(meta), macro_dispatch?})
|
||||
def add_local({_set, bag}, from, to, meta) when is_tuple(from) and is_tuple(to) do
|
||||
if from != to do
|
||||
put_edge(bag, {:local, from}, {to, get_line(meta)})
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -56,14 +56,14 @@ defmodule Module.LocalsTracker do
|
||||
@doc """
|
||||
Reattach a previously yanked node.
|
||||
"""
|
||||
def reattach({_set, bag}, tuple, kind, function, out_neighbours, meta) do
|
||||
def reattach({_set, bag}, tuple, _kind, function, out_neighbours, meta) do
|
||||
for out_neighbour <- out_neighbours do
|
||||
put_edge(bag, {:local, function}, out_neighbour)
|
||||
end
|
||||
|
||||
# Make a call from the old function to the new one
|
||||
if function != tuple do
|
||||
put_edge(bag, {:local, function}, {tuple, get_line(meta), kind in @defmacros})
|
||||
put_edge(bag, {:local, function}, {tuple, get_line(meta)})
|
||||
end
|
||||
|
||||
# Finally marked the new one as reattached
|
||||
@@ -71,9 +71,8 @@ defmodule Module.LocalsTracker do
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Collect all conflicting imports with the given functions
|
||||
"""
|
||||
# Collecting all conflicting imports with the given functions
|
||||
@doc false
|
||||
def collect_imports_conflicts({set, _bag}, all_defined) do
|
||||
for {pair, _, meta, _} <- all_defined, n = out_neighbour(set, {:import, pair}) do
|
||||
{meta, {n, pair}}
|
||||
@@ -100,37 +99,18 @@ defmodule Module.LocalsTracker do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Collect undefined functions based on local calls and existing definitions.
|
||||
Collect undefined functions based on local calls and existing definitions
|
||||
"""
|
||||
def collect_undefined_locals({set, bag}, all_defined) do
|
||||
undefined =
|
||||
for {pair, _, meta, _} <- all_defined,
|
||||
{local, line, macro_dispatch?} <- out_neighbours(bag, {:local, pair}),
|
||||
error = undefined_local_error(set, local, macro_dispatch?),
|
||||
do: {build_meta(line, meta), local, error}
|
||||
for {pair, _, _, _} <- all_defined,
|
||||
{local, line} <- out_neighbours(bag, {:local, pair}),
|
||||
not :ets.member(set, {:def, local}),
|
||||
do: {build_meta(line), local}
|
||||
|
||||
:lists.usort(undefined)
|
||||
end
|
||||
|
||||
defp undefined_local_error(set, local, true) do
|
||||
case :ets.member(set, {:def, local}) do
|
||||
true -> false
|
||||
false -> :undefined_function
|
||||
end
|
||||
end
|
||||
|
||||
defp undefined_local_error(set, local, false) do
|
||||
try do
|
||||
if :ets.lookup_element(set, {:def, local}, 2) in @defmacros do
|
||||
:incorrect_dispatch
|
||||
else
|
||||
false
|
||||
end
|
||||
catch
|
||||
_, _ -> :undefined_function
|
||||
end
|
||||
end
|
||||
|
||||
defp unreachable(reachable, reattached, private) do
|
||||
for {tuple, kind, _, _} <- private,
|
||||
not reachable?(tuple, kind, reachable, reattached),
|
||||
@@ -138,7 +118,7 @@ defmodule Module.LocalsTracker do
|
||||
end
|
||||
|
||||
defp reachable?(tuple, :defmacrop, reachable, reattached) do
|
||||
# All private macros are unreachable unless they have been
|
||||
# All private micros are unreachable unless they have been
|
||||
# reattached and they are reachable.
|
||||
:lists.member(tuple, reattached) and Map.has_key?(reachable, tuple)
|
||||
end
|
||||
@@ -203,7 +183,7 @@ defmodule Module.LocalsTracker do
|
||||
defp reachable_from(bag, local, vertices) do
|
||||
vertices = Map.put(vertices, local, true)
|
||||
|
||||
Enum.reduce(out_neighbours(bag, {:local, local}), vertices, fn {local, _line, _}, acc ->
|
||||
Enum.reduce(out_neighbours(bag, {:local, local}), vertices, fn {local, _line}, acc ->
|
||||
case acc do
|
||||
%{^local => true} -> acc
|
||||
_ -> reachable_from(bag, local, acc)
|
||||
@@ -213,17 +193,8 @@ defmodule Module.LocalsTracker do
|
||||
|
||||
defp get_line(meta), do: Keyword.get(meta, :line)
|
||||
|
||||
defp build_meta(nil, _meta), do: []
|
||||
|
||||
# We need to transform any file annotation in the function
|
||||
# definition into a keep annotation that is used by the
|
||||
# error handling system in order to respect line/file.
|
||||
defp build_meta(line, meta) do
|
||||
case Keyword.get(meta, :file) do
|
||||
{file, _} -> [keep: {file, line}]
|
||||
_ -> [line: line]
|
||||
end
|
||||
end
|
||||
defp build_meta(nil), do: []
|
||||
defp build_meta(line), do: [line: line]
|
||||
|
||||
## Lightweight digraph implementation
|
||||
|
||||
|
||||
@@ -1,495 +0,0 @@
|
||||
defmodule Module.ParallelChecker do
|
||||
@moduledoc false
|
||||
|
||||
import Kernel, except: [spawn: 3]
|
||||
|
||||
@type cache() :: {pid(), :ets.tid()}
|
||||
@type warning() :: term()
|
||||
@type kind() :: :def | :defmacro
|
||||
@type mode() :: :elixir | :erlang
|
||||
|
||||
@doc """
|
||||
Initializes the parallel checker process.
|
||||
"""
|
||||
def start_link(schedulers \\ nil) do
|
||||
:gen_server.start_link(__MODULE__, schedulers, [])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the parallel checker process.
|
||||
"""
|
||||
def stop(checker) do
|
||||
send(checker, {__MODULE__, :stop})
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the parallel checker data from pdict.
|
||||
"""
|
||||
def get do
|
||||
{_, checker} = :erlang.get(:elixir_checker_info)
|
||||
checker
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stores the parallel checker information.
|
||||
"""
|
||||
def put(pid, checker) do
|
||||
:erlang.put(:elixir_checker_info, {pid, checker})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Spawns a process that runs the parallel checker.
|
||||
"""
|
||||
def spawn({pid, checker}, module, info) do
|
||||
ref = make_ref()
|
||||
|
||||
spawned =
|
||||
spawn(fn ->
|
||||
Process.link(pid)
|
||||
mon_ref = Process.monitor(pid)
|
||||
|
||||
receive do
|
||||
{^ref, :cache, ets} ->
|
||||
loaded_info =
|
||||
if is_map(info) do
|
||||
cache_from_module_map(ets, info)
|
||||
info
|
||||
else
|
||||
info = File.read!(info)
|
||||
cache_from_chunk(ets, module, info)
|
||||
info
|
||||
end
|
||||
|
||||
send(checker, {ref, :cached})
|
||||
|
||||
receive do
|
||||
{^ref, :check} ->
|
||||
warnings = check_module(module, loaded_info, {checker, ets})
|
||||
send(pid, {__MODULE__, module, warnings})
|
||||
send(checker, {__MODULE__, :done})
|
||||
end
|
||||
|
||||
{:DOWN, ^mon_ref, _, _, _} ->
|
||||
:ok
|
||||
end
|
||||
end)
|
||||
|
||||
{spawned, ref}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Verifies the given compilation function
|
||||
by starting a checker if one does not exist.
|
||||
See `verify/3`.
|
||||
"""
|
||||
def verify(fun) do
|
||||
case :erlang.get(:elixir_compiler_info) do
|
||||
:undefined ->
|
||||
previous = :erlang.get(:elixir_checker_info)
|
||||
{:ok, checker} = start_link()
|
||||
put(self(), checker)
|
||||
|
||||
try do
|
||||
{result, compile_info} = Enum.unzip(fun.())
|
||||
_ = verify(checker, compile_info, [])
|
||||
result
|
||||
after
|
||||
if previous != :undefined do
|
||||
:erlang.put(:elixir_checker_info, previous)
|
||||
else
|
||||
:erlang.erase(:elixir_checker_info)
|
||||
end
|
||||
|
||||
stop(checker)
|
||||
end
|
||||
|
||||
_ ->
|
||||
# If we are during compilation, then they will be
|
||||
# reported to the compiler, which will validate them.
|
||||
Enum.map(fun.(), &elem(&1, 0))
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives pairs of module maps and BEAM binaries. In parallel it verifies
|
||||
the modules and adds the ExCk chunk to the binaries. Returns the updated
|
||||
list of warnings from the verification.
|
||||
"""
|
||||
@spec verify(pid(), [{pid(), reference()}], [{module(), binary()}]) :: [warning()]
|
||||
def verify(checker, compiled_info, runtime_files) do
|
||||
runtime_info =
|
||||
for {module, file} <- runtime_files do
|
||||
spawn({self(), checker}, module, file)
|
||||
end
|
||||
|
||||
modules = compiled_info ++ runtime_info
|
||||
:gen_server.cast(checker, {:start, modules})
|
||||
collect_results(modules, [])
|
||||
end
|
||||
|
||||
defp collect_results([], warnings) do
|
||||
warnings
|
||||
end
|
||||
|
||||
defp collect_results([_ | modules], warnings) do
|
||||
receive do
|
||||
{__MODULE__, _module, new_warnings} ->
|
||||
collect_results(modules, new_warnings ++ warnings)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Test cache.
|
||||
"""
|
||||
def test_cache do
|
||||
{:ok, checker} = start_link()
|
||||
{checker, :gen_server.call(checker, :ets)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Preloads a module into the cache. Call this function before any other
|
||||
cache lookups for the module.
|
||||
"""
|
||||
@spec preload_module(cache(), module()) :: :ok
|
||||
def preload_module({server, ets}, module) do
|
||||
case :ets.lookup(ets, {:cached, module}) do
|
||||
[{_key, _}] -> :ok
|
||||
[] -> cache_module({server, ets}, module)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the export kind and deprecation reason for the given MFA from
|
||||
the cache. If the module does not exist return `{:error, :module}`,
|
||||
or if the function does not exist return `{:error, :function}`.
|
||||
"""
|
||||
@spec fetch_export(cache(), module(), atom(), arity()) ::
|
||||
{:ok, mode(), kind(), binary() | nil} | {:error, :function | :module}
|
||||
def fetch_export({_server, ets}, module, fun, arity) do
|
||||
case :ets.lookup(ets, {:cached, module}) do
|
||||
[{_key, false}] ->
|
||||
{:error, :module}
|
||||
|
||||
[{_key, mode}] ->
|
||||
case :ets.lookup(ets, {:export, module, {fun, arity}}) do
|
||||
[{_key, kind, reason}] -> {:ok, mode, kind, reason}
|
||||
[] -> {:error, :function}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all exported functions and macros for the given module from
|
||||
the cache.
|
||||
"""
|
||||
@spec all_exports(cache(), module()) :: [{atom(), arity()}]
|
||||
def all_exports({_server, ets}, module) do
|
||||
# This is only called after we get a deprecation notice
|
||||
# so we can assume it's a cached module
|
||||
ets
|
||||
|> :ets.match({{:export, module, :"$1"}, :_, :_})
|
||||
|> Enum.flat_map(& &1)
|
||||
|> Enum.sort()
|
||||
end
|
||||
|
||||
## Module checking
|
||||
|
||||
defp check_module(module, info, cache) do
|
||||
case extract_definitions(module, info) do
|
||||
{:ok, module, file, definitions, no_warn_undefined} ->
|
||||
Module.Types.warnings(module, file, definitions, no_warn_undefined, cache)
|
||||
|> group_warnings()
|
||||
|> emit_warnings()
|
||||
|
||||
:error ->
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
defp extract_definitions(module, module_map) when is_map(module_map) do
|
||||
no_warn_undefined =
|
||||
module_map.compile_opts
|
||||
|> extract_no_warn_undefined()
|
||||
|> merge_compiler_no_warn_undefined()
|
||||
|
||||
{:ok, module, module_map.file, module_map.definitions, no_warn_undefined}
|
||||
end
|
||||
|
||||
defp extract_definitions(module, binary) when is_binary(binary) do
|
||||
with {:ok, {_, [debug_info: chunk]}} <- :beam_lib.chunks(binary, [:debug_info]),
|
||||
{:debug_info_v1, backend, data} <- chunk,
|
||||
{:ok, module_map} <- backend.debug_info(:elixir_v1, module, data, []) do
|
||||
extract_definitions(module, module_map)
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
defp extract_no_warn_undefined(compile_opts) do
|
||||
for(
|
||||
{:no_warn_undefined, values} <- compile_opts,
|
||||
value <- List.wrap(values),
|
||||
do: value
|
||||
)
|
||||
end
|
||||
|
||||
defp merge_compiler_no_warn_undefined(no_warn_undefined) do
|
||||
case Code.get_compiler_option(:no_warn_undefined) do
|
||||
:all ->
|
||||
:all
|
||||
|
||||
list when is_list(list) ->
|
||||
no_warn_undefined ++ list
|
||||
end
|
||||
end
|
||||
|
||||
## Warning helpers
|
||||
|
||||
def group_warnings(warnings) do
|
||||
warnings
|
||||
|> Enum.reduce(%{}, fn {module, warning, location}, acc ->
|
||||
locations = MapSet.new([location])
|
||||
Map.update(acc, {module, warning}, locations, &MapSet.put(&1, location))
|
||||
end)
|
||||
|> Enum.map(fn {{module, warning}, locations} -> {module, warning, Enum.sort(locations)} end)
|
||||
|> Enum.sort()
|
||||
end
|
||||
|
||||
def emit_warnings(warnings) do
|
||||
Enum.flat_map(warnings, fn {module, warning, locations} ->
|
||||
message = module.format_warning(warning)
|
||||
print_warning([message, ?\n, format_locations(locations)])
|
||||
|
||||
Enum.map(locations, fn {file, line, _mfa} ->
|
||||
{file, line, message}
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
defp format_locations([location]) do
|
||||
format_location(location)
|
||||
end
|
||||
|
||||
defp format_locations(locations) do
|
||||
[
|
||||
"Invalid call found at #{length(locations)} locations:\n",
|
||||
Enum.map(locations, &format_location/1)
|
||||
]
|
||||
end
|
||||
|
||||
defp format_location({file, line, {module, fun, arity}}) do
|
||||
mfa = Exception.format_mfa(module, fun, arity)
|
||||
[format_file_line(file, line), ": ", mfa, ?\n]
|
||||
end
|
||||
|
||||
defp format_location({file, line, nil}) do
|
||||
[format_file_line(file, line), ?\n]
|
||||
end
|
||||
|
||||
defp format_location({file, line, module}) do
|
||||
[format_file_line(file, line), ": ", inspect(module), ?\n]
|
||||
end
|
||||
|
||||
defp format_file_line(file, line) do
|
||||
file = Path.relative_to_cwd(file)
|
||||
line = if line > 0, do: [?: | Integer.to_string(line)], else: []
|
||||
[" ", file, line]
|
||||
end
|
||||
|
||||
defp print_warning(message) do
|
||||
IO.puts(:stderr, [:elixir_errors.warning_prefix(), message])
|
||||
end
|
||||
|
||||
## Cache
|
||||
|
||||
defp cache_module({server, ets}, module) do
|
||||
if lock(server, module) do
|
||||
cache_from_chunk(ets, module) || cache_from_info(ets, module)
|
||||
unlock(server, module)
|
||||
end
|
||||
end
|
||||
|
||||
defp cache_from_chunk(ets, module) do
|
||||
case :code.get_object_code(module) do
|
||||
{^module, binary, _filename} -> cache_from_chunk(ets, module, binary)
|
||||
_other -> false
|
||||
end
|
||||
end
|
||||
|
||||
defp cache_from_chunk(ets, module, binary) do
|
||||
with {:ok, {_, [{'ExCk', chunk}]}} <- :beam_lib.chunks(binary, ['ExCk']),
|
||||
{:elixir_checker_v1, contents} <- :erlang.binary_to_term(chunk) do
|
||||
cache_chunk(ets, module, contents.exports)
|
||||
true
|
||||
else
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
defp cache_from_module_map(ets, map) do
|
||||
exports =
|
||||
[{{:__info__, 1}, :def}] ++
|
||||
behaviour_exports(map) ++
|
||||
definitions_to_exports(map.definitions)
|
||||
|
||||
deprecated = Map.new(map.deprecated)
|
||||
cache_info(ets, map.module, exports, deprecated, :elixir)
|
||||
end
|
||||
|
||||
defp cache_from_info(ets, module) do
|
||||
if Code.ensure_loaded?(module) do
|
||||
{mode, exports} = info_exports(module)
|
||||
deprecated = info_deprecated(module)
|
||||
cache_info(ets, module, exports, deprecated, mode)
|
||||
else
|
||||
:ets.insert(ets, {{:cached, module}, false})
|
||||
end
|
||||
end
|
||||
|
||||
defp info_exports(module) do
|
||||
map =
|
||||
Map.new(
|
||||
[{{:__info__, 1}, :def}] ++
|
||||
behaviour_exports(module) ++
|
||||
Enum.map(module.__info__(:macros), &{&1, :defmacro}) ++
|
||||
Enum.map(module.__info__(:functions), &{&1, :def})
|
||||
)
|
||||
|
||||
{:elixir, map}
|
||||
rescue
|
||||
_ -> {:erlang, Map.new(Enum.map(module.module_info(:exports), &{&1, :def}))}
|
||||
end
|
||||
|
||||
defp info_deprecated(module) do
|
||||
Map.new(module.__info__(:deprecated))
|
||||
rescue
|
||||
_ -> %{}
|
||||
end
|
||||
|
||||
defp cache_info(ets, module, exports, deprecated, mode) do
|
||||
Enum.each(exports, fn {{fun, arity}, kind} ->
|
||||
reason = Map.get(deprecated, {fun, arity})
|
||||
:ets.insert(ets, {{:export, module, {fun, arity}}, kind, reason})
|
||||
{{fun, arity}, kind}
|
||||
end)
|
||||
|
||||
:ets.insert(ets, {{:cached, module}, mode})
|
||||
end
|
||||
|
||||
defp cache_chunk(ets, module, exports) do
|
||||
Enum.each(exports, fn {{fun, arity}, %{kind: kind, deprecated_reason: reason}} ->
|
||||
:ets.insert(ets, {{:export, module, {fun, arity}}, kind, reason})
|
||||
|
||||
{{fun, arity}, kind}
|
||||
end)
|
||||
|
||||
:ets.insert(ets, {{:export, module, {:__info__, 1}}, :def, nil})
|
||||
:ets.insert(ets, {{:cached, module}, :elixir})
|
||||
end
|
||||
|
||||
defp behaviour_exports(%{is_behaviour: true}), do: [{{:behaviour_info, 1}, :def}]
|
||||
defp behaviour_exports(%{is_behaviour: false}), do: []
|
||||
|
||||
defp behaviour_exports(module) when is_atom(module) do
|
||||
if function_exported?(module, :behaviour_info, 1) do
|
||||
[{{:behaviour_info, 1}, :def}]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
defp definitions_to_exports(definitions) do
|
||||
Enum.flat_map(definitions, fn {function, kind, _meta, _clauses} ->
|
||||
if kind in [:def, :defmacro] do
|
||||
[{function, kind}]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp lock(server, module) do
|
||||
:gen_server.call(server, {:lock, module}, :infinity)
|
||||
end
|
||||
|
||||
defp unlock(server, module) do
|
||||
:gen_server.call(server, {:unlock, module})
|
||||
end
|
||||
|
||||
## Server callbacks
|
||||
|
||||
def init(schedulers) do
|
||||
ets = :ets.new(__MODULE__, [:set, :public, {:read_concurrency, true}])
|
||||
|
||||
state = %{
|
||||
ets: ets,
|
||||
waiting: %{},
|
||||
modules: [],
|
||||
spawned: 0,
|
||||
schedulers: schedulers || max(:erlang.system_info(:schedulers_online), 2)
|
||||
}
|
||||
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
def handle_call(:ets, _from, state) do
|
||||
{:reply, state.ets, state}
|
||||
end
|
||||
|
||||
def handle_call({:lock, module}, from, %{waiting: waiting} = state) do
|
||||
case waiting do
|
||||
%{^module => froms} ->
|
||||
waiting = Map.put(state.waiting, module, [from | froms])
|
||||
{:noreply, %{state | waiting: waiting}}
|
||||
|
||||
%{} ->
|
||||
waiting = Map.put(state.waiting, module, [])
|
||||
{:reply, true, %{state | waiting: waiting}}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_call({:unlock, module}, _from, %{waiting: waiting} = state) do
|
||||
froms = Map.fetch!(waiting, module)
|
||||
Enum.each(froms, &:gen_server.reply(&1, false))
|
||||
waiting = Map.delete(waiting, module)
|
||||
{:reply, :ok, %{state | waiting: waiting}}
|
||||
end
|
||||
|
||||
def handle_info({__MODULE__, :done}, state) do
|
||||
state = %{state | spawned: state.spawned - 1}
|
||||
{:noreply, run_checkers(state)}
|
||||
end
|
||||
|
||||
def handle_info({__MODULE__, :stop}, state) do
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
def handle_cast({:start, modules}, %{ets: ets} = state) do
|
||||
for {pid, ref} <- modules do
|
||||
send(pid, {ref, :cache, ets})
|
||||
end
|
||||
|
||||
for {_pid, ref} <- modules do
|
||||
receive do
|
||||
{^ref, :cached} -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
{:noreply, run_checkers(%{state | modules: modules})}
|
||||
end
|
||||
|
||||
defp run_checkers(%{modules: []} = state) do
|
||||
state
|
||||
end
|
||||
|
||||
defp run_checkers(%{spawned: spawned, schedulers: schedulers} = state)
|
||||
when spawned >= schedulers do
|
||||
state
|
||||
end
|
||||
|
||||
defp run_checkers(%{modules: [{pid, ref} | modules]} = state) do
|
||||
send(pid, {ref, :check})
|
||||
run_checkers(%{state | modules: modules, spawned: state.spawned + 1})
|
||||
end
|
||||
end
|
||||
@@ -1,522 +0,0 @@
|
||||
defmodule Module.Types do
|
||||
@moduledoc false
|
||||
|
||||
defmodule Error do
|
||||
defexception [:message]
|
||||
end
|
||||
|
||||
import Module.Types.Helpers
|
||||
alias Module.Types.{Expr, Pattern, Unify}
|
||||
|
||||
@doc false
|
||||
def warnings(module, file, defs, no_warn_undefined, cache) do
|
||||
stack = stack()
|
||||
|
||||
Enum.flat_map(defs, fn {{fun, arity} = function, kind, meta, clauses} ->
|
||||
context = context(with_file_meta(meta, file), module, function, no_warn_undefined, cache)
|
||||
|
||||
Enum.flat_map(clauses, fn {_meta, args, guards, body} ->
|
||||
def_expr = {kind, meta, [guards_to_expr(guards, {fun, [], args})]}
|
||||
|
||||
try do
|
||||
warnings_from_clause(args, guards, body, def_expr, stack, context)
|
||||
rescue
|
||||
e ->
|
||||
def_expr = {kind, meta, [guards_to_expr(guards, {fun, [], args}), [do: body]]}
|
||||
|
||||
error =
|
||||
Error.exception("""
|
||||
found error while checking types for #{Exception.format_mfa(module, fun, arity)}
|
||||
|
||||
#{Macro.to_string(def_expr)}
|
||||
|
||||
Please report this bug: https://github.com/elixir-lang/elixir/issues
|
||||
|
||||
#{Exception.format_banner(:error, e, __STACKTRACE__)}\
|
||||
""")
|
||||
|
||||
reraise error, __STACKTRACE__
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
defp with_file_meta(meta, file) do
|
||||
case Keyword.fetch(meta, :file) do
|
||||
{:ok, {meta_file, _}} -> meta_file
|
||||
:error -> file
|
||||
end
|
||||
end
|
||||
|
||||
defp guards_to_expr([], left) do
|
||||
left
|
||||
end
|
||||
|
||||
defp guards_to_expr([guard | guards], left) do
|
||||
guards_to_expr(guards, {:when, [], [left, guard]})
|
||||
end
|
||||
|
||||
defp warnings_from_clause(args, guards, body, def_expr, stack, context) do
|
||||
head_stack = Unify.push_expr_stack(def_expr, stack)
|
||||
|
||||
with {:ok, _types, context} <- Pattern.of_head(args, guards, head_stack, context),
|
||||
{:ok, _type, context} <- Expr.of_expr(body, :dynamic, stack, context) do
|
||||
context.warnings
|
||||
else
|
||||
{:error, {type, error, context}} ->
|
||||
[error_to_warning(type, error, context) | context.warnings]
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def context(file, module, function, no_warn_undefined, cache) do
|
||||
%{
|
||||
# File of module
|
||||
file: file,
|
||||
# Module of definitions
|
||||
module: module,
|
||||
# Current function
|
||||
function: function,
|
||||
# List of calls to not warn on as undefined
|
||||
no_warn_undefined: no_warn_undefined,
|
||||
# A list of cached modules received from the parallel compiler
|
||||
cache: cache,
|
||||
# Expression variable to type variable
|
||||
vars: %{},
|
||||
# Type variable to expression variable
|
||||
types_to_vars: %{},
|
||||
# Type variable to type
|
||||
types: %{},
|
||||
# Trace of all variables that have been refined to a type,
|
||||
# including the type they were refined to, why, and where
|
||||
traces: %{},
|
||||
# Counter to give type variables unique names
|
||||
counter: 0,
|
||||
# Track if a variable was inferred from a type guard function such is_tuple/1
|
||||
# or a guard function that fails such as elem/2, possible values are:
|
||||
# `:guarded` when `is_tuple(x)`
|
||||
# `:guarded` when `is_tuple and elem(x, 0)`
|
||||
# `:fail` when `elem(x, 0)`
|
||||
guard_sources: %{},
|
||||
# A list with all warnings from the running the code
|
||||
warnings: []
|
||||
}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def stack() do
|
||||
%{
|
||||
# Stack of variables we have refined during unification,
|
||||
# used for creating relevant traces
|
||||
unify_stack: [],
|
||||
# Last expression we have recursed through during inference,
|
||||
# used for tracing
|
||||
last_expr: nil,
|
||||
# When false do not add a trace when a type variable is refined,
|
||||
# useful when merging contexts where the variables already have traces
|
||||
trace: true,
|
||||
# There are two factors that control how we track guards.
|
||||
#
|
||||
# * consider_type_guards?: if type guards should be considered.
|
||||
# This applies only at the root and root-based "and" and "or" nodes.
|
||||
#
|
||||
# * keep_guarded? - if a guarded clause should remain as guarded
|
||||
# even on failure. Used on the right side of and.
|
||||
#
|
||||
type_guards: {_consider_type_guards? = true, _keep_guarded? = false},
|
||||
# Context used to determine if unification is bi-directional, :expr
|
||||
# is directional, :pattern is bi-directional
|
||||
context: nil
|
||||
}
|
||||
end
|
||||
|
||||
## ERROR TO WARNING
|
||||
|
||||
# Collect relevant information from context and traces to report error
|
||||
def error_to_warning(:unable_apply, {mfa, args, expected, signature, stack}, context) do
|
||||
{fun, arity} = context.function
|
||||
line = get_meta(stack.last_expr)[:line]
|
||||
location = {context.file, line, {context.module, fun, arity}}
|
||||
|
||||
traces = type_traces(stack, context)
|
||||
{[signature | args], traces} = lift_all_types([signature | args], traces, context)
|
||||
error = {:unable_apply, mfa, args, expected, signature, {location, stack.last_expr, traces}}
|
||||
{Module.Types, error, location}
|
||||
end
|
||||
|
||||
def error_to_warning(:unable_unify, {left, right, stack}, context) do
|
||||
{fun, arity} = context.function
|
||||
line = get_meta(stack.last_expr)[:line]
|
||||
location = {context.file, line, {context.module, fun, arity}}
|
||||
|
||||
traces = type_traces(stack, context)
|
||||
{[left, right], traces} = lift_all_types([left, right], traces, context)
|
||||
error = {:unable_unify, left, right, {location, stack.last_expr, traces}}
|
||||
{Module.Types, error, location}
|
||||
end
|
||||
|
||||
# Collect relevant traces from context.traces using stack.unify_stack
|
||||
defp type_traces(stack, context) do
|
||||
# TODO: Do we need the unify_stack or is enough to only get the last variable
|
||||
# in the stack since we get related variables anyway?
|
||||
stack =
|
||||
stack.unify_stack
|
||||
|> Enum.flat_map(&[&1 | related_variables(&1, context.types)])
|
||||
|> Enum.uniq()
|
||||
|
||||
Enum.flat_map(stack, fn var_index ->
|
||||
with %{^var_index => traces} <- context.traces,
|
||||
%{^var_index => expr_var} <- context.types_to_vars do
|
||||
Enum.map(traces, &tag_trace(expr_var, &1, context))
|
||||
else
|
||||
_other -> []
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp related_variables(var, types) do
|
||||
Enum.flat_map(types, fn
|
||||
{related_var, {:var, ^var}} ->
|
||||
[related_var | related_variables(related_var, types)]
|
||||
|
||||
_ ->
|
||||
[]
|
||||
end)
|
||||
end
|
||||
|
||||
# Tag if trace is for a concrete type or type variable
|
||||
defp tag_trace(var, {type, expr, location}, context) do
|
||||
with {:var, var_index} <- type,
|
||||
%{^var_index => expr_var} <- context.types_to_vars do
|
||||
{:var, var, expr_var, expr, location}
|
||||
else
|
||||
_ -> {:type, var, type, expr, location}
|
||||
end
|
||||
end
|
||||
|
||||
defp lift_all_types(types, traces, context) do
|
||||
trace_types = for({:type, _, type, _, _} <- traces, do: type)
|
||||
{types, lift_context} = Unify.lift_types(types, context)
|
||||
{trace_types, _lift_context} = Unify.lift_types(trace_types, lift_context)
|
||||
|
||||
{traces, []} =
|
||||
Enum.map_reduce(traces, trace_types, fn
|
||||
{:type, var, _, expr, location}, [type | acc] -> {{:type, var, type, expr, location}, acc}
|
||||
other, acc -> {other, acc}
|
||||
end)
|
||||
|
||||
{types, traces}
|
||||
end
|
||||
|
||||
## FORMAT WARNINGS
|
||||
|
||||
def format_warning({:unable_apply, mfa, args, expected, signature, {location, expr, traces}}) do
|
||||
{module, function, arity} = mfa
|
||||
mfa_args = Macro.generate_arguments(arity, __MODULE__)
|
||||
{module, function, ^arity} = call_to_mfa(erl_to_ex(module, function, mfa_args, []))
|
||||
format_mfa = Exception.format_mfa(module, function, arity)
|
||||
{traces, [] = _hints} = format_traces(traces, [], false)
|
||||
|
||||
clauses =
|
||||
Enum.map(
|
||||
signature,
|
||||
&String.slice(IO.iodata_to_binary(Unify.format_type({:fun, [&1]}, false)), 1..-2)
|
||||
)
|
||||
|
||||
[
|
||||
"expected #{format_mfa} to have signature:\n\n ",
|
||||
Enum.map_join(args, ", ", &Unify.format_type(&1, false)),
|
||||
" -> #{Unify.format_type(expected, false)}",
|
||||
"\n\nbut it has signature:\n\n ",
|
||||
indent(Enum.join(clauses, "\n")),
|
||||
"\n\n",
|
||||
format_expr(expr, location),
|
||||
traces,
|
||||
"Conflict found at"
|
||||
]
|
||||
end
|
||||
|
||||
def format_warning({:unable_unify, left, right, {location, expr, traces}}) do
|
||||
if map_type?(left) and map_type?(right) and match?({:ok, _, _}, missing_field(left, right)) do
|
||||
{:ok, atom, known_atoms} = missing_field(left, right)
|
||||
|
||||
# Drop the last trace which is the expression map.foo
|
||||
traces = Enum.drop(traces, 1)
|
||||
{traces, hints} = format_traces(traces, [left, right], true)
|
||||
|
||||
[
|
||||
"undefined field \"#{atom}\" ",
|
||||
format_expr(expr, location),
|
||||
"expected one of the following fields: ",
|
||||
Enum.map_join(Enum.sort(known_atoms), ", ", & &1),
|
||||
"\n\n",
|
||||
traces,
|
||||
format_message_hints(hints),
|
||||
"Conflict found at"
|
||||
]
|
||||
else
|
||||
simplify_left? = simplify_type?(left, right)
|
||||
simplify_right? = simplify_type?(right, left)
|
||||
|
||||
{traces, hints} = format_traces(traces, [left, right], simplify_left? or simplify_right?)
|
||||
|
||||
[
|
||||
"incompatible types:\n\n ",
|
||||
Unify.format_type(left, simplify_left?),
|
||||
" !~ ",
|
||||
Unify.format_type(right, simplify_right?),
|
||||
"\n\n",
|
||||
format_expr(expr, location),
|
||||
traces,
|
||||
format_message_hints(hints),
|
||||
"Conflict found at"
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
defp missing_field(
|
||||
{:map, [{:required, {:atom, atom} = type, _}, {:optional, :dynamic, :dynamic}]},
|
||||
{:map, fields}
|
||||
) do
|
||||
matched_missing_field(fields, type, atom)
|
||||
end
|
||||
|
||||
defp missing_field(
|
||||
{:map, fields},
|
||||
{:map, [{:required, {:atom, atom} = type, _}, {:optional, :dynamic, :dynamic}]}
|
||||
) do
|
||||
matched_missing_field(fields, type, atom)
|
||||
end
|
||||
|
||||
defp missing_field(_, _), do: :error
|
||||
|
||||
defp matched_missing_field(fields, type, atom) do
|
||||
if List.keymember?(fields, type, 1) do
|
||||
:error
|
||||
else
|
||||
known_atoms = for {_, {:atom, atom}, _} <- fields, do: atom
|
||||
{:ok, atom, known_atoms}
|
||||
end
|
||||
end
|
||||
|
||||
defp format_traces([], _types, _simplify?) do
|
||||
{[], []}
|
||||
end
|
||||
|
||||
defp format_traces(traces, types, simplify?) do
|
||||
traces
|
||||
|> Enum.uniq()
|
||||
|> Enum.reverse()
|
||||
|> Enum.map_reduce([], fn
|
||||
{:type, var, type, expr, location}, hints ->
|
||||
{hint, hints} = format_type_hint(type, types, expr, hints)
|
||||
|
||||
trace = [
|
||||
"where \"",
|
||||
Macro.to_string(var),
|
||||
"\" was given the type ",
|
||||
Unify.format_type(type, simplify?),
|
||||
hint,
|
||||
" in:\n\n # ",
|
||||
format_location(location),
|
||||
" ",
|
||||
indent(expr_to_string(expr)),
|
||||
"\n\n"
|
||||
]
|
||||
|
||||
{trace, hints}
|
||||
|
||||
{:var, var1, var2, expr, location}, hints ->
|
||||
trace = [
|
||||
"where \"",
|
||||
Macro.to_string(var1),
|
||||
"\" was given the same type as \"",
|
||||
Macro.to_string(var2),
|
||||
"\" in:\n\n # ",
|
||||
format_location(location),
|
||||
" ",
|
||||
indent(expr_to_string(expr)),
|
||||
"\n\n"
|
||||
]
|
||||
|
||||
{trace, hints}
|
||||
end)
|
||||
end
|
||||
|
||||
defp format_location({file, line, _mfa}) do
|
||||
format_location({file, line})
|
||||
end
|
||||
|
||||
defp format_location({file, line}) do
|
||||
file = Path.relative_to_cwd(file)
|
||||
line = if line, do: [Integer.to_string(line)], else: []
|
||||
[file, ?:, line, ?\n]
|
||||
end
|
||||
|
||||
defp simplify_type?(type, other) do
|
||||
map_like_type?(type) and not map_like_type?(other)
|
||||
end
|
||||
|
||||
## EXPRESSION FORMATTING
|
||||
|
||||
defp format_expr(nil, _location) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp format_expr(expr, location) do
|
||||
[
|
||||
"in expression:\n\n # ",
|
||||
format_location(location),
|
||||
" ",
|
||||
indent(expr_to_string(expr)),
|
||||
"\n\n"
|
||||
]
|
||||
end
|
||||
|
||||
@doc false
|
||||
def expr_to_string(expr) do
|
||||
expr
|
||||
|> reverse_rewrite()
|
||||
|> Macro.to_string()
|
||||
end
|
||||
|
||||
defp reverse_rewrite(guard) do
|
||||
Macro.prewalk(guard, fn
|
||||
{{:., _, [mod, fun]}, meta, args} -> erl_to_ex(mod, fun, args, meta)
|
||||
other -> other
|
||||
end)
|
||||
end
|
||||
|
||||
defp erl_to_ex(mod, fun, args, meta) do
|
||||
case :elixir_rewrite.erl_to_ex(mod, fun, args) do
|
||||
{Kernel, fun, args} -> {fun, meta, args}
|
||||
{mod, fun, args} -> {{:., [], [mod, fun]}, meta, args}
|
||||
end
|
||||
end
|
||||
|
||||
## Hints
|
||||
|
||||
defp format_message_hints(hints) do
|
||||
hints
|
||||
|> Enum.uniq()
|
||||
|> Enum.reverse()
|
||||
|> Enum.map(&[format_message_hint(&1), "\n"])
|
||||
end
|
||||
|
||||
defp format_message_hint(:inferred_dot) do
|
||||
"""
|
||||
HINT: "var.field" (without parentheses) implies "var" is a map() while \
|
||||
"var.fun()" (with parentheses) implies "var" is an atom()
|
||||
"""
|
||||
end
|
||||
|
||||
defp format_message_hint(:inferred_bitstring_spec) do
|
||||
"""
|
||||
HINT: all expressions given to binaries are assumed to be of type \
|
||||
integer() unless said otherwise. For example, <<expr>> assumes "expr" \
|
||||
is an integer. Pass a modifier, such as <<expr::float>> or <<expr::binary>>, \
|
||||
to change the default behaviour.
|
||||
"""
|
||||
end
|
||||
|
||||
defp format_message_hint({:sized_and_unsize_tuples, {size, var}}) do
|
||||
"""
|
||||
HINT: use pattern matching or "is_tuple(#{Macro.to_string(var)}) and \
|
||||
tuple_size(#{Macro.to_string(var)}) == #{size}" to guard a sized tuple.
|
||||
"""
|
||||
end
|
||||
|
||||
defp format_type_hint(type, types, expr, hints) do
|
||||
case format_type_hint(type, types, expr) do
|
||||
{message, hint} -> {message, [hint | hints]}
|
||||
:error -> {[], hints}
|
||||
end
|
||||
end
|
||||
|
||||
defp format_type_hint(type, types, expr) do
|
||||
cond do
|
||||
dynamic_map_dot?(type, expr) ->
|
||||
{" (due to calling var.field)", :inferred_dot}
|
||||
|
||||
dynamic_remote_call?(type, expr) ->
|
||||
{" (due to calling var.fun())", :inferred_dot}
|
||||
|
||||
inferred_bitstring_spec?(type, expr) ->
|
||||
{[], :inferred_bitstring_spec}
|
||||
|
||||
message = sized_and_unsize_tuples(expr, types) ->
|
||||
{[], {:sized_and_unsize_tuples, message}}
|
||||
|
||||
true ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
defp dynamic_map_dot?(type, expr) do
|
||||
with true <- map_type?(type),
|
||||
{{:., _meta1, [_map, _field]}, meta2, []} <- expr,
|
||||
true <- Keyword.get(meta2, :no_parens, false) do
|
||||
true
|
||||
else
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
defp dynamic_remote_call?(type, expr) do
|
||||
with true <- atom_type?(type),
|
||||
{{:., _meta1, [_module, _field]}, meta2, []} <- expr,
|
||||
false <- Keyword.get(meta2, :no_parens, false) do
|
||||
true
|
||||
else
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
defp inferred_bitstring_spec?(type, expr) do
|
||||
with true <- integer_type?(type),
|
||||
{:<<>>, _, args} <- expr,
|
||||
true <- Enum.any?(args, &match?({:"::", [{:inferred_bitstring_spec, true} | _], _}, &1)) do
|
||||
true
|
||||
else
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
defp sized_and_unsize_tuples({{:., _, [:erlang, :is_tuple]}, _, [var]}, types) do
|
||||
case Enum.find(types, &match?({:tuple, _, _}, &1)) do
|
||||
{:tuple, size, _} ->
|
||||
{size, var}
|
||||
|
||||
nil ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp sized_and_unsize_tuples(_expr, _types) do
|
||||
nil
|
||||
end
|
||||
|
||||
## Formatting helpers
|
||||
|
||||
defp indent(string) do
|
||||
String.replace(string, "\n", "\n ")
|
||||
end
|
||||
|
||||
defp map_type?({:map, _}), do: true
|
||||
defp map_type?(_other), do: false
|
||||
|
||||
defp map_like_type?({:map, _}), do: true
|
||||
defp map_like_type?({:union, union}), do: Enum.any?(union, &map_like_type?/1)
|
||||
defp map_like_type?(_other), do: false
|
||||
|
||||
defp atom_type?(:atom), do: true
|
||||
defp atom_type?({:atom, _}), do: false
|
||||
defp atom_type?({:union, union}), do: Enum.all?(union, &atom_type?/1)
|
||||
defp atom_type?(_other), do: false
|
||||
|
||||
defp integer_type?(:integer), do: true
|
||||
defp integer_type?(_other), do: false
|
||||
|
||||
defp call_to_mfa({{:., _, [mod, fun]}, _, args}), do: {mod, fun, length(args)}
|
||||
defp call_to_mfa({fun, _, args}) when is_atom(fun), do: {Kernel, fun, length(args)}
|
||||
end
|
||||
@@ -1,525 +0,0 @@
|
||||
defmodule Module.Types.Expr do
|
||||
@moduledoc false
|
||||
|
||||
alias Module.Types.{Of, Pattern}
|
||||
import Module.Types.{Helpers, Unify}
|
||||
|
||||
def of_expr(expr, expected, %{context: stack_context} = stack, context)
|
||||
when stack_context != :expr do
|
||||
of_expr(expr, expected, %{stack | context: :expr}, context)
|
||||
end
|
||||
|
||||
# :atom
|
||||
def of_expr(atom, _expected, _stack, context) when is_atom(atom) do
|
||||
{:ok, {:atom, atom}, context}
|
||||
end
|
||||
|
||||
# 12
|
||||
def of_expr(literal, _expected, _stack, context) when is_integer(literal) do
|
||||
{:ok, :integer, context}
|
||||
end
|
||||
|
||||
# 1.2
|
||||
def of_expr(literal, _expected, _stack, context) when is_float(literal) do
|
||||
{:ok, :float, context}
|
||||
end
|
||||
|
||||
# "..."
|
||||
def of_expr(literal, _expected, _stack, context) when is_binary(literal) do
|
||||
{:ok, :binary, context}
|
||||
end
|
||||
|
||||
# #PID<...>
|
||||
def of_expr(literal, _expected, _stack, context) when is_pid(literal) do
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
|
||||
# <<...>>>
|
||||
def of_expr({:<<>>, _meta, args}, _expected, stack, context) do
|
||||
case Of.binary(args, stack, context, &of_expr/4) do
|
||||
{:ok, context} -> {:ok, :binary, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# left | []
|
||||
def of_expr({:|, _meta, [left_expr, []]} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
of_expr(left_expr, :dynamic, stack, context)
|
||||
end
|
||||
|
||||
# left | right
|
||||
def of_expr({:|, _meta, [left_expr, right_expr]} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case of_expr(left_expr, :dynamic, stack, context) do
|
||||
{:ok, left, context} ->
|
||||
case of_expr(right_expr, :dynamic, stack, context) do
|
||||
{:ok, {:list, right}, context} ->
|
||||
{:ok, to_union([left, right], context), context}
|
||||
|
||||
{:ok, right, context} ->
|
||||
{:ok, to_union([left, right], context), context}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# []
|
||||
def of_expr([], _expected, _stack, context) do
|
||||
{:ok, {:list, :dynamic}, context}
|
||||
end
|
||||
|
||||
# [expr, ...]
|
||||
def of_expr(exprs, _expected, stack, context) when is_list(exprs) do
|
||||
stack = push_expr_stack(exprs, stack)
|
||||
|
||||
case map_reduce_ok(exprs, context, &of_expr(&1, :dynamic, stack, &2)) do
|
||||
{:ok, types, context} -> {:ok, {:list, to_union(types, context)}, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# __CALLER__
|
||||
def of_expr({:__CALLER__, _meta, var_context}, _expected, _stack, context)
|
||||
when is_atom(var_context) do
|
||||
struct_pair = {:required, {:atom, :__struct__}, {:atom, Macro.Env}}
|
||||
|
||||
pairs =
|
||||
Enum.map(Map.from_struct(Macro.Env.__struct__()), fn {key, _value} ->
|
||||
{:required, {:atom, key}, :dynamic}
|
||||
end)
|
||||
|
||||
{:ok, {:map, [struct_pair | pairs]}, context}
|
||||
end
|
||||
|
||||
# __STACKTRACE__
|
||||
def of_expr({:__STACKTRACE__, _meta, var_context}, _expected, _stack, context)
|
||||
when is_atom(var_context) do
|
||||
file = {:tuple, 2, [{:atom, :file}, {:list, :integer}]}
|
||||
line = {:tuple, 2, [{:atom, :line}, :integer]}
|
||||
file_line = {:list, {:union, [file, line]}}
|
||||
type = {:list, {:tuple, 4, [:atom, :atom, :integer, file_line]}}
|
||||
{:ok, type, context}
|
||||
end
|
||||
|
||||
# var
|
||||
def of_expr(var, _expected, _stack, context) when is_var(var) do
|
||||
{:ok, get_var!(var, context), context}
|
||||
end
|
||||
|
||||
# {left, right}
|
||||
def of_expr({left, right}, expected, stack, context) do
|
||||
of_expr({:{}, [], [left, right]}, expected, stack, context)
|
||||
end
|
||||
|
||||
# {...}
|
||||
def of_expr({:{}, _meta, exprs} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case map_reduce_ok(exprs, context, &of_expr(&1, :dynamic, stack, &2)) do
|
||||
{:ok, types, context} -> {:ok, {:tuple, length(types), types}, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# left = right
|
||||
def of_expr({:=, _meta, [left_expr, right_expr]} = expr, _expected, stack, context) do
|
||||
# TODO: We might want to bring the expected type forward in case the type of this
|
||||
# pattern is not useful. For example: 1 = _ = expr
|
||||
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
with {:ok, left_type, context} <-
|
||||
Pattern.of_pattern(left_expr, stack, context),
|
||||
{:ok, right_type, context} <- of_expr(right_expr, left_type, stack, context),
|
||||
do: unify(right_type, left_type, stack, context)
|
||||
end
|
||||
|
||||
# %{map | ...}
|
||||
def of_expr({:%{}, _, [{:|, _, [map, args]}]} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
map_type = {:map, [{:optional, :dynamic, :dynamic}]}
|
||||
|
||||
with {:ok, map_type, context} <- of_expr(map, map_type, stack, context),
|
||||
{:ok, {:map, arg_pairs}, context} <- Of.closed_map(args, stack, context, &of_expr/4),
|
||||
dynamic_value_pairs =
|
||||
Enum.map(arg_pairs, fn {:required, key, _value} -> {:required, key, :dynamic} end),
|
||||
args_type = {:map, dynamic_value_pairs ++ [{:optional, :dynamic, :dynamic}]},
|
||||
{:ok, type, context} <- unify(args_type, map_type, stack, context) do
|
||||
# Retrieve map type and overwrite with the new value types from the map update
|
||||
{:map, pairs} = resolve_var(type, context)
|
||||
|
||||
updated_pairs =
|
||||
Enum.reduce(arg_pairs, pairs, fn {:required, key, value}, pairs ->
|
||||
List.keyreplace(pairs, key, 1, {:required, key, value})
|
||||
end)
|
||||
|
||||
{:ok, {:map, updated_pairs}, context}
|
||||
end
|
||||
end
|
||||
|
||||
# %Struct{map | ...}
|
||||
def of_expr(
|
||||
{:%, meta, [module, {:%{}, _, [{:|, _, [_, _]}]} = update]} = expr,
|
||||
_expected,
|
||||
stack,
|
||||
context
|
||||
) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
map_type = {:map, [{:optional, :dynamic, :dynamic}]}
|
||||
|
||||
with {:ok, struct, context} <- Of.struct(module, meta, context),
|
||||
{:ok, update, context} <- of_expr(update, map_type, stack, context) do
|
||||
unify(update, struct, stack, context)
|
||||
end
|
||||
end
|
||||
|
||||
# %{...}
|
||||
def of_expr({:%{}, _meta, args} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
Of.closed_map(args, stack, context, &of_expr/4)
|
||||
end
|
||||
|
||||
# %Struct{...}
|
||||
def of_expr({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
with {:ok, struct, context} <- Of.struct(module, meta1, context),
|
||||
{:ok, map, context} <- Of.open_map(args, stack, context, &of_expr/4) do
|
||||
unify(map, struct, stack, context)
|
||||
end
|
||||
end
|
||||
|
||||
# ()
|
||||
def of_expr({:__block__, _meta, []}, _expected, _stack, context) do
|
||||
{:ok, {:atom, nil}, context}
|
||||
end
|
||||
|
||||
# (expr; expr)
|
||||
def of_expr({:__block__, _meta, exprs}, expected, stack, context) do
|
||||
expected_types = List.duplicate(:dynamic, length(exprs) - 1) ++ [expected]
|
||||
|
||||
result =
|
||||
map_reduce_ok(Enum.zip(exprs, expected_types), context, fn {expr, expected}, context ->
|
||||
of_expr(expr, expected, stack, context)
|
||||
end)
|
||||
|
||||
case result do
|
||||
{:ok, expr_types, context} -> {:ok, Enum.at(expr_types, -1), context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# case expr do pat -> expr end
|
||||
def of_expr({:case, _meta, [case_expr, [{:do, clauses}]]} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
with {:ok, _expr_type, context} <- of_expr(case_expr, :dynamic, stack, context),
|
||||
{:ok, context} <- of_clauses(clauses, stack, context),
|
||||
do: {:ok, :dynamic, context}
|
||||
end
|
||||
|
||||
# fn pat -> expr end
|
||||
def of_expr({:fn, _meta, clauses} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case of_clauses(clauses, stack, context) do
|
||||
{:ok, context} -> {:ok, :dynamic, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
@try_blocks [:do, :after]
|
||||
@try_clause_blocks [:catch, :else, :after]
|
||||
|
||||
# try do expr end
|
||||
def of_expr({:try, _meta, [blocks]} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
{result, context} =
|
||||
reduce_ok(blocks, context, fn
|
||||
{:rescue, clauses}, context ->
|
||||
reduce_ok(clauses, context, fn
|
||||
{:->, _, [[{:in, _, [var, _exceptions]}], body]}, context = acc ->
|
||||
{_type, context} = new_pattern_var(var, context)
|
||||
|
||||
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
|
||||
{:ok, keep_warnings(acc, context)}
|
||||
end
|
||||
|
||||
{:->, _, [[var], body]}, context = acc ->
|
||||
{_type, context} = new_pattern_var(var, context)
|
||||
|
||||
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
|
||||
{:ok, keep_warnings(acc, context)}
|
||||
end
|
||||
end)
|
||||
|
||||
{block, body}, context = acc when block in @try_blocks ->
|
||||
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
|
||||
{:ok, keep_warnings(acc, context)}
|
||||
end
|
||||
|
||||
{block, clauses}, context when block in @try_clause_blocks ->
|
||||
of_clauses(clauses, stack, context)
|
||||
end)
|
||||
|
||||
case result do
|
||||
:ok -> {:ok, :dynamic, context}
|
||||
:error -> {:error, context}
|
||||
end
|
||||
end
|
||||
|
||||
# receive do pat -> expr end
|
||||
def of_expr({:receive, _meta, [blocks]} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
{result, context} =
|
||||
reduce_ok(blocks, context, fn
|
||||
{:do, {:__block__, _, []}}, context ->
|
||||
{:ok, context}
|
||||
|
||||
{:do, clauses}, context ->
|
||||
of_clauses(clauses, stack, context)
|
||||
|
||||
{:after, [{:->, _meta, [head, body]}]}, context = acc ->
|
||||
with {:ok, _type, context} <- of_expr(head, :dynamic, stack, context),
|
||||
{:ok, _type, context} <- of_expr(body, :dynamic, stack, context),
|
||||
do: {:ok, keep_warnings(acc, context)}
|
||||
end)
|
||||
|
||||
case result do
|
||||
:ok -> {:ok, :dynamic, context}
|
||||
:error -> {:error, context}
|
||||
end
|
||||
end
|
||||
|
||||
# for pat <- expr do expr end
|
||||
def of_expr({:for, _meta, args} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
{clauses, [[{:do, block} | opts]]} = Enum.split(args, -1)
|
||||
|
||||
with {:ok, context} <- reduce_ok(clauses, context, &for_clause(&1, stack, &2)),
|
||||
{:ok, context} <- reduce_ok(opts, context, &for_option(&1, stack, &2)) do
|
||||
if Keyword.has_key?(opts, :reduce) do
|
||||
with {:ok, context} <- of_clauses(block, stack, context) do
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
else
|
||||
with {:ok, _type, context} <- of_expr(block, :dynamic, stack, context) do
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# with pat <- expr do expr end
|
||||
def of_expr({:with, _meta, clauses} = expr, _expected, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case reduce_ok(clauses, context, &with_clause(&1, stack, &2)) do
|
||||
{:ok, context} -> {:ok, :dynamic, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# fun.(args)
|
||||
def of_expr({{:., _meta1, [fun]}, _meta2, args} = expr, _expected, stack, context) do
|
||||
# TODO: Use expected type to infer intersection return type
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
with {:ok, _fun_type, context} <- of_expr(fun, :dynamic, stack, context),
|
||||
{:ok, _arg_types, context} <-
|
||||
map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
end
|
||||
|
||||
# expr.key_or_fun
|
||||
def of_expr({{:., _meta1, [expr1, key_or_fun]}, meta2, []} = expr2, _expected, stack, context)
|
||||
when not is_atom(expr1) do
|
||||
stack = push_expr_stack(expr2, stack)
|
||||
|
||||
if Keyword.get(meta2, :no_parens, false) do
|
||||
with {:ok, expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
|
||||
{value_var, context} = add_var(context),
|
||||
pair_type = {:required, {:atom, key_or_fun}, value_var},
|
||||
optional_type = {:optional, :dynamic, :dynamic},
|
||||
map_field_type = {:map, [pair_type, optional_type]},
|
||||
{:ok, _map_type, context} <- unify(map_field_type, expr_type, stack, context),
|
||||
do: {:ok, value_var, context}
|
||||
else
|
||||
# TODO: Use expected type to infer intersection return type
|
||||
with {:ok, expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
|
||||
{:ok, _map_type, context} <- unify(expr_type, :atom, stack, context),
|
||||
do: {:ok, :dynamic, context}
|
||||
end
|
||||
end
|
||||
|
||||
# expr.fun(arg)
|
||||
def of_expr({{:., meta1, [expr1, fun]}, _meta2, args} = expr2, _expected, stack, context) do
|
||||
# TODO: Use expected type to infer intersection return type
|
||||
|
||||
context = Of.remote(expr1, fun, length(args), meta1, context)
|
||||
stack = push_expr_stack(expr2, stack)
|
||||
|
||||
with {:ok, _expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
|
||||
{:ok, _fun_type, context} <- of_expr(fun, :dynamic, stack, context),
|
||||
{:ok, _arg_types, context} <-
|
||||
map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
end
|
||||
|
||||
# &Foo.bar/1
|
||||
def of_expr(
|
||||
{:&, meta, [{:/, _, [{{:., _, [module, fun]}, _, []}, arity]}]},
|
||||
_expected,
|
||||
_stack,
|
||||
context
|
||||
)
|
||||
when is_atom(module) and is_atom(fun) do
|
||||
context = Of.remote(module, fun, arity, meta, context)
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
|
||||
# &foo/1
|
||||
# & &1
|
||||
def of_expr({:&, _meta, _arg}, _expected, _stack, context) do
|
||||
# TODO: Function type
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
|
||||
# fun(arg)
|
||||
def of_expr({fun, _meta, args} = expr, _expected, stack, context)
|
||||
when is_atom(fun) and is_list(args) do
|
||||
# TODO: Use expected type to infer intersection return type
|
||||
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
|
||||
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp for_clause({:<-, _, [left, expr]}, stack, context) do
|
||||
{pattern, guards} = extract_head([left])
|
||||
|
||||
with {:ok, _pattern_type, context} <- Pattern.of_head([pattern], guards, stack, context),
|
||||
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
|
||||
do: {:ok, context}
|
||||
end
|
||||
|
||||
defp for_clause({:<<>>, _, [{:<-, _, [pattern, expr]}]}, stack, context) do
|
||||
# TODO: the compiler guarantees pattern is a binary but we need to check expr is a binary
|
||||
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
|
||||
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
|
||||
do: {:ok, context}
|
||||
end
|
||||
|
||||
defp for_clause(list, stack, context) when is_list(list) do
|
||||
reduce_ok(list, context, &for_option(&1, stack, &2))
|
||||
end
|
||||
|
||||
defp for_clause(expr, stack, context) do
|
||||
of_expr_context(expr, :dynamic, stack, context)
|
||||
end
|
||||
|
||||
defp for_option({:into, expr}, stack, context) do
|
||||
of_expr_context(expr, :dynamic, stack, context)
|
||||
end
|
||||
|
||||
defp for_option({:reduce, expr}, stack, context) do
|
||||
of_expr_context(expr, :dynamic, stack, context)
|
||||
end
|
||||
|
||||
defp for_option({:uniq, _}, _stack, context) do
|
||||
{:ok, context}
|
||||
end
|
||||
|
||||
defp with_clause({:<-, _, [left, expr]}, stack, context) do
|
||||
{pattern, guards} = extract_head([left])
|
||||
|
||||
with {:ok, _pattern_type, context} <- Pattern.of_head([pattern], guards, stack, context),
|
||||
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
|
||||
do: {:ok, context}
|
||||
end
|
||||
|
||||
defp with_clause(list, stack, context) when is_list(list) do
|
||||
reduce_ok(list, context, &with_option(&1, stack, &2))
|
||||
end
|
||||
|
||||
defp with_clause(expr, stack, context) do
|
||||
of_expr_context(expr, :dynamic, stack, context)
|
||||
end
|
||||
|
||||
defp with_option({:do, body}, stack, context) do
|
||||
of_expr_context(body, :dynamic, stack, context)
|
||||
end
|
||||
|
||||
defp with_option({:else, clauses}, stack, context) do
|
||||
of_clauses(clauses, stack, context)
|
||||
end
|
||||
|
||||
defp of_clauses(clauses, stack, context) do
|
||||
reduce_ok(clauses, context, fn {:->, meta, [head, body]}, context = acc ->
|
||||
{patterns, guards} = extract_head(head)
|
||||
|
||||
case Pattern.of_head(patterns, guards, stack, context) do
|
||||
{:ok, _, context} ->
|
||||
with {:ok, _expr_type, context} <- of_expr(body, :dynamic, stack, context) do
|
||||
{:ok, keep_warnings(acc, context)}
|
||||
end
|
||||
|
||||
error ->
|
||||
# Skip the clause if it the head has an error
|
||||
if meta[:generated], do: {:ok, acc}, else: error
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp keep_warnings(context, %{warnings: warnings}) do
|
||||
%{context | warnings: warnings}
|
||||
end
|
||||
|
||||
defp extract_head([{:when, _meta, args}]) do
|
||||
case Enum.split(args, -1) do
|
||||
{patterns, [guards]} -> {patterns, flatten_when(guards)}
|
||||
{patterns, []} -> {patterns, []}
|
||||
end
|
||||
end
|
||||
|
||||
defp extract_head(other) do
|
||||
{other, []}
|
||||
end
|
||||
|
||||
defp flatten_when({:when, _meta, [left, right]}) do
|
||||
[left | flatten_when(right)]
|
||||
end
|
||||
|
||||
defp flatten_when(other) do
|
||||
[other]
|
||||
end
|
||||
|
||||
defp of_expr_context(expr, expected, stack, context) do
|
||||
case of_expr(expr, expected, stack, context) do
|
||||
{:ok, _type, context} -> {:ok, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp new_pattern_var({:_, _meta, var_context}, context) when is_atom(var_context) do
|
||||
{:dynamic, context}
|
||||
end
|
||||
|
||||
defp new_pattern_var(var, context) do
|
||||
new_var(var, context)
|
||||
end
|
||||
end
|
||||
@@ -1,196 +0,0 @@
|
||||
defmodule Module.Types.Helpers do
|
||||
# AST and enumeration helpers.
|
||||
@moduledoc false
|
||||
|
||||
@doc """
|
||||
Guard function to check if an AST node is a variable.
|
||||
"""
|
||||
defmacro is_var(expr) do
|
||||
quote do
|
||||
is_tuple(unquote(expr)) and
|
||||
tuple_size(unquote(expr)) == 3 and
|
||||
is_atom(elem(unquote(expr), 0)) and
|
||||
is_atom(elem(unquote(expr), 2))
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns unique identifier for the current assignment of the variable.
|
||||
"""
|
||||
def var_name({_name, meta, _context}), do: Keyword.fetch!(meta, :version)
|
||||
|
||||
@doc """
|
||||
Returns the AST metadata.
|
||||
"""
|
||||
def get_meta({_, meta, _}), do: meta
|
||||
def get_meta(_other), do: []
|
||||
|
||||
@doc """
|
||||
Like `Enum.reduce/3` but only continues while `fun` returns `{:ok, acc}`
|
||||
and stops on `{:error, reason}`.
|
||||
"""
|
||||
def reduce_ok(list, acc, fun) do
|
||||
do_reduce_ok(list, acc, fun)
|
||||
end
|
||||
|
||||
defp do_reduce_ok([head | tail], acc, fun) do
|
||||
case fun.(head, acc) do
|
||||
{:ok, acc} ->
|
||||
do_reduce_ok(tail, acc, fun)
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_reduce_ok([], acc, _fun), do: {:ok, acc}
|
||||
|
||||
@doc """
|
||||
Like `Enum.unzip/1` but only continues while `fun` returns `{:ok, elem1, elem2}`
|
||||
and stops on `{:error, reason}`.
|
||||
"""
|
||||
def unzip_ok(list) do
|
||||
do_unzip_ok(list, [], [])
|
||||
end
|
||||
|
||||
defp do_unzip_ok([{:ok, head1, head2} | tail], acc1, acc2) do
|
||||
do_unzip_ok(tail, [head1 | acc1], [head2 | acc2])
|
||||
end
|
||||
|
||||
defp do_unzip_ok([{:error, reason} | _tail], _acc1, _acc2), do: {:error, reason}
|
||||
|
||||
defp do_unzip_ok([], acc1, acc2), do: {:ok, Enum.reverse(acc1), Enum.reverse(acc2)}
|
||||
|
||||
@doc """
|
||||
Like `Enum.map/2` but only continues while `fun` returns `{:ok, elem}`
|
||||
and stops on `{:error, reason}`.
|
||||
"""
|
||||
def map_ok(list, fun) do
|
||||
do_map_ok(list, [], fun)
|
||||
end
|
||||
|
||||
defp do_map_ok([head | tail], acc, fun) do
|
||||
case fun.(head) do
|
||||
{:ok, elem} ->
|
||||
do_map_ok(tail, [elem | acc], fun)
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_map_ok([], acc, _fun), do: {:ok, Enum.reverse(acc)}
|
||||
|
||||
@doc """
|
||||
Like `Enum.map_reduce/3` but only continues while `fun` returns `{:ok, elem, acc}`
|
||||
and stops on `{:error, reason}`.
|
||||
"""
|
||||
def map_reduce_ok(list, acc, fun) do
|
||||
do_map_reduce_ok(list, {[], acc}, fun)
|
||||
end
|
||||
|
||||
defp do_map_reduce_ok([head | tail], {list, acc}, fun) do
|
||||
case fun.(head, acc) do
|
||||
{:ok, elem, acc} ->
|
||||
do_map_reduce_ok(tail, {[elem | list], acc}, fun)
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_map_reduce_ok([], {list, acc}, _fun), do: {:ok, Enum.reverse(list), acc}
|
||||
|
||||
@doc """
|
||||
Like `Enum.flat_map/2` but only continues while `fun` returns `{:ok, list}`
|
||||
and stops on `{:error, reason}`.
|
||||
"""
|
||||
def flat_map_ok(list, fun) do
|
||||
do_flat_map_ok(list, [], fun)
|
||||
end
|
||||
|
||||
defp do_flat_map_ok([head | tail], acc, fun) do
|
||||
case fun.(head) do
|
||||
{:ok, elem} ->
|
||||
do_flat_map_ok(tail, [elem | acc], fun)
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_flat_map_ok([], acc, _fun), do: {:ok, Enum.reverse(Enum.concat(acc))}
|
||||
|
||||
@doc """
|
||||
Like `Enum.flat_map_reduce/3` but only continues while `fun` returns `{:ok, list, acc}`
|
||||
and stops on `{:error, reason}`.
|
||||
"""
|
||||
def flat_map_reduce_ok(list, acc, fun) do
|
||||
do_flat_map_reduce_ok(list, {[], acc}, fun)
|
||||
end
|
||||
|
||||
defp do_flat_map_reduce_ok([head | tail], {list, acc}, fun) do
|
||||
case fun.(head, acc) do
|
||||
{:ok, elems, acc} ->
|
||||
do_flat_map_reduce_ok(tail, {[elems | list], acc}, fun)
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_flat_map_reduce_ok([], {list, acc}, _fun),
|
||||
do: {:ok, Enum.reverse(Enum.concat(list)), acc}
|
||||
|
||||
@doc """
|
||||
Given a list of `[{:ok, term()} | {:error, term()}]` it returns a list of
|
||||
errors `{:error, [term()]}` in case of at least one error or `{:ok, [term()]}`
|
||||
if there are no errors.
|
||||
"""
|
||||
def oks_or_errors(list) do
|
||||
case Enum.split_with(list, &match?({:ok, _}, &1)) do
|
||||
{oks, []} -> {:ok, Enum.map(oks, fn {:ok, ok} -> ok end)}
|
||||
{_oks, errors} -> {:error, Enum.map(errors, fn {:error, error} -> error end)}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Combines a list of guard expressions `when x when y when z` to an expression
|
||||
combined with `or`, `x or y or z`.
|
||||
"""
|
||||
# TODO: Remove this and let multiple when be treated as multiple clauses,
|
||||
# meaning they will be intersection types
|
||||
def guards_to_or([]) do
|
||||
[]
|
||||
end
|
||||
|
||||
def guards_to_or(guards) do
|
||||
Enum.reduce(guards, fn guard, acc -> {{:., [], [:erlang, :orelse]}, [], [guard, acc]} end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Like `Enum.zip/1` but will zip multiple lists together instead of only two.
|
||||
"""
|
||||
def zip_many(lists) do
|
||||
zip_many(lists, [], [[]])
|
||||
end
|
||||
|
||||
defp zip_many([], [], [[] | acc]) do
|
||||
map_reverse(acc, [], &Enum.reverse/1)
|
||||
end
|
||||
|
||||
defp zip_many([], remain, [last | acc]) do
|
||||
zip_many(Enum.reverse(remain), [], [[] | [last | acc]])
|
||||
end
|
||||
|
||||
defp zip_many([[] | _], remain, [last | acc]) do
|
||||
zip_many(Enum.reverse(remain), [], [last | acc])
|
||||
end
|
||||
|
||||
defp zip_many([[elem | list1] | list2], remain, [last | acc]) do
|
||||
zip_many(list2, [list1 | remain], [[elem | last] | acc])
|
||||
end
|
||||
|
||||
defp map_reverse([], acc, _fun), do: acc
|
||||
defp map_reverse([head | tail], acc, fun), do: map_reverse(tail, [fun.(head) | acc], fun)
|
||||
end
|
||||
@@ -1,365 +0,0 @@
|
||||
defmodule Module.Types.Of do
|
||||
# Typing functionality shared between Expr and Pattern.
|
||||
# Generic AST and Enum helpers go to Module.Types.Helpers.
|
||||
@moduledoc false
|
||||
|
||||
@prefix quote(do: ...)
|
||||
@suffix quote(do: ...)
|
||||
|
||||
alias Module.ParallelChecker
|
||||
|
||||
import Module.Types.Helpers
|
||||
import Module.Types.Unify
|
||||
|
||||
# There are important assumptions on how we work with maps.
|
||||
#
|
||||
# First, the keys in the map must be ordered by subtyping.
|
||||
#
|
||||
# Second, optional keys must be a superset of the required
|
||||
# keys, i.e. %{required(atom) => integer, optional(:foo) => :bar}
|
||||
# is forbidden.
|
||||
#
|
||||
# Third, in order to preserve co/contra-variance, a supertype
|
||||
# must satisfy its subtypes. I.e. %{foo: :bar, atom() => :baz}
|
||||
# is forbidden, it must be %{foo: :bar, atom() => :baz | :bar}.
|
||||
#
|
||||
# Once we support user declared maps, we need to validate these
|
||||
# assumptions.
|
||||
|
||||
@doc """
|
||||
Handles open maps (with dynamic => dynamic).
|
||||
"""
|
||||
def open_map(args, stack, context, of_fun) do
|
||||
with {:ok, pairs, context} <- map_pairs(args, stack, context, of_fun) do
|
||||
# If we match on a map such as %{"foo" => "bar"}, we cannot
|
||||
# assert that %{binary() => binary()}, since we are matching
|
||||
# only a single binary of infinite possible values. Therefore,
|
||||
# the correct would be to match it to %{binary() => binary() | var}.
|
||||
#
|
||||
# We can skip this in two cases:
|
||||
#
|
||||
# 1. If the key is a singleton, then we know that it has no
|
||||
# other value than the current one
|
||||
#
|
||||
# 2. If the value is a variable, then there is no benefit in
|
||||
# creating another variable, so we can skip it
|
||||
#
|
||||
# For now, we skip generating the var itself and introduce
|
||||
# :dynamic instead.
|
||||
pairs =
|
||||
for {key, value} <- pairs, not has_unbound_var?(key, context) do
|
||||
if singleton?(key, context) or match?({:var, _}, value) do
|
||||
{key, value}
|
||||
else
|
||||
{key, to_union([value, :dynamic], context)}
|
||||
end
|
||||
end
|
||||
|
||||
triplets = pairs_to_unions(pairs, [], context) ++ [{:optional, :dynamic, :dynamic}]
|
||||
{:ok, {:map, triplets}, context}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Handles closed maps (without dynamic => dynamic).
|
||||
"""
|
||||
def closed_map(args, stack, context, of_fun) do
|
||||
with {:ok, pairs, context} <- map_pairs(args, stack, context, of_fun) do
|
||||
{:ok, {:map, closed_to_unions(pairs, context)}, context}
|
||||
end
|
||||
end
|
||||
|
||||
defp map_pairs(pairs, stack, context, of_fun) do
|
||||
map_reduce_ok(pairs, context, fn {key, value}, context ->
|
||||
with {:ok, key_type, context} <- of_fun.(key, :dynamic, stack, context),
|
||||
{:ok, value_type, context} <- of_fun.(value, :dynamic, stack, context),
|
||||
do: {:ok, {key_type, value_type}, context}
|
||||
end)
|
||||
end
|
||||
|
||||
defp closed_to_unions([{key, value}], _context), do: [{:required, key, value}]
|
||||
|
||||
defp closed_to_unions(pairs, context) do
|
||||
case Enum.split_with(pairs, fn {key, _value} -> has_unbound_var?(key, context) end) do
|
||||
{[], pairs} -> pairs_to_unions(pairs, [], context)
|
||||
{[_ | _], pairs} -> pairs_to_unions([{:dynamic, :dynamic} | pairs], [], context)
|
||||
end
|
||||
end
|
||||
|
||||
defp pairs_to_unions([{key, value} | ahead], behind, context) do
|
||||
{matched_ahead, values} = find_matching_values(ahead, key, [], [])
|
||||
|
||||
# In case nothing matches, use the original ahead
|
||||
ahead = matched_ahead || ahead
|
||||
|
||||
all_values =
|
||||
[value | values] ++
|
||||
find_subtype_values(ahead, key, context) ++
|
||||
find_subtype_values(behind, key, context)
|
||||
|
||||
pairs_to_unions(ahead, [{key, to_union(all_values, context)} | behind], context)
|
||||
end
|
||||
|
||||
defp pairs_to_unions([], acc, context) do
|
||||
acc
|
||||
|> Enum.sort(&subtype?(elem(&1, 0), elem(&2, 0), context))
|
||||
|> Enum.map(fn {key, value} -> {:required, key, value} end)
|
||||
end
|
||||
|
||||
defp find_subtype_values(pairs, key, context) do
|
||||
for {pair_key, pair_value} <- pairs, subtype?(pair_key, key, context), do: pair_value
|
||||
end
|
||||
|
||||
defp find_matching_values([{key, value} | ahead], key, acc, values) do
|
||||
find_matching_values(ahead, key, acc, [value | values])
|
||||
end
|
||||
|
||||
defp find_matching_values([{_, _} = pair | ahead], key, acc, values) do
|
||||
find_matching_values(ahead, key, [pair | acc], values)
|
||||
end
|
||||
|
||||
defp find_matching_values([], _key, acc, [_ | _] = values), do: {Enum.reverse(acc), values}
|
||||
defp find_matching_values([], _key, _acc, []), do: {nil, []}
|
||||
|
||||
@doc """
|
||||
Handles structs.
|
||||
"""
|
||||
def struct(struct, meta, context) do
|
||||
context = remote(struct, :__struct__, 0, meta, context)
|
||||
|
||||
entries =
|
||||
for key <- Map.keys(struct.__struct__()), key != :__struct__ do
|
||||
{:required, {:atom, key}, :dynamic}
|
||||
end
|
||||
|
||||
{:ok, {:map, [{:required, {:atom, :__struct__}, {:atom, struct}} | entries]}, context}
|
||||
end
|
||||
|
||||
## Binary
|
||||
|
||||
@doc """
|
||||
Handles binaries.
|
||||
|
||||
In the stack, we add nodes such as <<expr>>, <<..., expr>>, etc,
|
||||
based on the position of the expression within the binary.
|
||||
"""
|
||||
def binary([], _stack, context, _of_fun) do
|
||||
{:ok, context}
|
||||
end
|
||||
|
||||
def binary([head], stack, context, of_fun) do
|
||||
head_stack = push_expr_stack({:<<>>, get_meta(head), [head]}, stack)
|
||||
binary_segment(head, head_stack, context, of_fun)
|
||||
end
|
||||
|
||||
def binary([head | tail], stack, context, of_fun) do
|
||||
head_stack = push_expr_stack({:<<>>, get_meta(head), [head, @suffix]}, stack)
|
||||
|
||||
case binary_segment(head, head_stack, context, of_fun) do
|
||||
{:ok, context} -> binary_many(tail, stack, context, of_fun)
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp binary_many([last], stack, context, of_fun) do
|
||||
last_stack = push_expr_stack({:<<>>, get_meta(last), [@prefix, last]}, stack)
|
||||
binary_segment(last, last_stack, context, of_fun)
|
||||
end
|
||||
|
||||
defp binary_many([head | tail], stack, context, of_fun) do
|
||||
head_stack = push_expr_stack({:<<>>, get_meta(head), [@prefix, head, @suffix]}, stack)
|
||||
|
||||
case binary_segment(head, head_stack, context, of_fun) do
|
||||
{:ok, context} -> binary_many(tail, stack, context, of_fun)
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp binary_segment({:"::", _meta, [expr, specifiers]}, stack, context, of_fun) do
|
||||
expected_type =
|
||||
collect_binary_specifier(specifiers, &binary_type(stack.context, &1)) || :integer
|
||||
|
||||
utf? = collect_binary_specifier(specifiers, &utf_type?/1)
|
||||
float? = collect_binary_specifier(specifiers, &float_type?/1)
|
||||
|
||||
# Special case utf and float specifiers because they can be two types as literals
|
||||
# but only a specific type as a variable in a pattern
|
||||
cond do
|
||||
stack.context == :pattern and utf? and is_binary(expr) ->
|
||||
{:ok, context}
|
||||
|
||||
stack.context == :pattern and float? and is_integer(expr) ->
|
||||
{:ok, context}
|
||||
|
||||
true ->
|
||||
with {:ok, type, context} <- of_fun.(expr, expected_type, stack, context),
|
||||
{:ok, _type, context} <- unify(type, expected_type, stack, context),
|
||||
do: {:ok, context}
|
||||
end
|
||||
end
|
||||
|
||||
# Collect binary type specifiers,
|
||||
# from `<<pattern::integer-size(10)>>` collect `integer`
|
||||
defp collect_binary_specifier({:-, _meta, [left, right]}, fun) do
|
||||
collect_binary_specifier(left, fun) || collect_binary_specifier(right, fun)
|
||||
end
|
||||
|
||||
defp collect_binary_specifier(other, fun) do
|
||||
fun.(other)
|
||||
end
|
||||
|
||||
defp binary_type(:expr, {:float, _, _}), do: {:union, [:integer, :float]}
|
||||
defp binary_type(:expr, {:utf8, _, _}), do: {:union, [:integer, :binary]}
|
||||
defp binary_type(:expr, {:utf16, _, _}), do: {:union, [:integer, :binary]}
|
||||
defp binary_type(:expr, {:utf32, _, _}), do: {:union, [:integer, :binary]}
|
||||
defp binary_type(:pattern, {:utf8, _, _}), do: :integer
|
||||
defp binary_type(:pattern, {:utf16, _, _}), do: :integer
|
||||
defp binary_type(:pattern, {:utf32, _, _}), do: :integer
|
||||
defp binary_type(:pattern, {:float, _, _}), do: :float
|
||||
defp binary_type(_context, {:integer, _, _}), do: :integer
|
||||
defp binary_type(_context, {:bits, _, _}), do: :binary
|
||||
defp binary_type(_context, {:bitstring, _, _}), do: :binary
|
||||
defp binary_type(_context, {:bytes, _, _}), do: :binary
|
||||
defp binary_type(_context, {:binary, _, _}), do: :binary
|
||||
defp binary_type(_context, _specifier), do: nil
|
||||
|
||||
defp utf_type?({specifier, _, _}), do: specifier in [:utf8, :utf16, :utf32]
|
||||
defp utf_type?(_), do: false
|
||||
|
||||
defp float_type?({:float, _, _}), do: true
|
||||
defp float_type?(_), do: false
|
||||
|
||||
## Remote
|
||||
|
||||
@doc """
|
||||
Handles remote calls.
|
||||
"""
|
||||
def remote(module, fun, arity, meta, context) when is_atom(module) do
|
||||
# TODO: In the future we may want to warn for modules defined
|
||||
# in the local context
|
||||
if Keyword.get(meta, :context_module, false) and context.module != module do
|
||||
context
|
||||
else
|
||||
ParallelChecker.preload_module(context.cache, module)
|
||||
check_export(module, fun, arity, meta, context)
|
||||
end
|
||||
end
|
||||
|
||||
def remote(_module, _fun, _arity, _meta, context), do: context
|
||||
|
||||
defp check_export(module, fun, arity, meta, context) do
|
||||
case ParallelChecker.fetch_export(context.cache, module, fun, arity) do
|
||||
{:ok, mode, :def, reason} ->
|
||||
check_deprecated(mode, module, fun, arity, reason, meta, context)
|
||||
|
||||
{:ok, mode, :defmacro, reason} ->
|
||||
context = warn(meta, context, {:unrequired_module, module, fun, arity})
|
||||
check_deprecated(mode, module, fun, arity, reason, meta, context)
|
||||
|
||||
{:error, :module} ->
|
||||
if warn_undefined?(module, fun, arity, context) do
|
||||
warn(meta, context, {:undefined_module, module, fun, arity})
|
||||
else
|
||||
context
|
||||
end
|
||||
|
||||
{:error, :function} ->
|
||||
if warn_undefined?(module, fun, arity, context) do
|
||||
exports = ParallelChecker.all_exports(context.cache, module)
|
||||
warn(meta, context, {:undefined_function, module, fun, arity, exports})
|
||||
else
|
||||
context
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp check_deprecated(:elixir, module, fun, arity, reason, meta, context) do
|
||||
if reason do
|
||||
warn(meta, context, {:deprecated, module, fun, arity, reason})
|
||||
else
|
||||
context
|
||||
end
|
||||
end
|
||||
|
||||
defp check_deprecated(:erlang, module, fun, arity, _reason, meta, context) do
|
||||
case :otp_internal.obsolete(module, fun, arity) do
|
||||
{:deprecated, string} when is_list(string) ->
|
||||
reason = string |> List.to_string() |> String.capitalize()
|
||||
warn(meta, context, {:deprecated, module, fun, arity, reason})
|
||||
|
||||
{:deprecated, string, removal} when is_list(string) and is_list(removal) ->
|
||||
reason = string |> List.to_string() |> String.capitalize()
|
||||
reason = "It will be removed in #{removal}. #{reason}"
|
||||
warn(meta, context, {:deprecated, module, fun, arity, reason})
|
||||
|
||||
_ ->
|
||||
context
|
||||
end
|
||||
end
|
||||
|
||||
# The protocol code dispatches to unknown modules, so we ignore them here.
|
||||
#
|
||||
# try do
|
||||
# SomeProtocol.Atom.__impl__
|
||||
# rescue
|
||||
# ...
|
||||
# end
|
||||
#
|
||||
# But for protocols we don't want to traverse the protocol code anyway.
|
||||
# TODO: remove this clause once we no longer traverse the protocol code.
|
||||
defp warn_undefined?(_module, :__impl__, 1, _context), do: false
|
||||
defp warn_undefined?(_module, :module_info, 0, _context), do: false
|
||||
defp warn_undefined?(_module, :module_info, 1, _context), do: false
|
||||
defp warn_undefined?(:erlang, :orelse, 2, _context), do: false
|
||||
defp warn_undefined?(:erlang, :andalso, 2, _context), do: false
|
||||
|
||||
defp warn_undefined?(_, _, _, %{no_warn_undefined: :all}) do
|
||||
false
|
||||
end
|
||||
|
||||
defp warn_undefined?(module, fun, arity, context) do
|
||||
not Enum.any?(context.no_warn_undefined, &(&1 == module or &1 == {module, fun, arity}))
|
||||
end
|
||||
|
||||
defp warn(meta, context, warning) do
|
||||
{fun, arity} = context.function
|
||||
location = {context.file, meta[:line] || 0, {context.module, fun, arity}}
|
||||
%{context | warnings: [{__MODULE__, warning, location} | context.warnings]}
|
||||
end
|
||||
|
||||
## Warning formatting
|
||||
|
||||
def format_warning({:undefined_module, module, fun, arity}) do
|
||||
[
|
||||
Exception.format_mfa(module, fun, arity),
|
||||
" is undefined (module ",
|
||||
inspect(module),
|
||||
" is not available or is yet to be defined)"
|
||||
]
|
||||
end
|
||||
|
||||
def format_warning({:undefined_function, module, fun, arity, exports}) do
|
||||
[
|
||||
Exception.format_mfa(module, fun, arity),
|
||||
" is undefined or private",
|
||||
UndefinedFunctionError.hint_for_loaded_module(module, fun, arity, exports)
|
||||
]
|
||||
end
|
||||
|
||||
def format_warning({:deprecated, module, fun, arity, reason}) do
|
||||
[
|
||||
Exception.format_mfa(module, fun, arity),
|
||||
" is deprecated. ",
|
||||
reason
|
||||
]
|
||||
end
|
||||
|
||||
def format_warning({:unrequired_module, module, fun, arity}) do
|
||||
[
|
||||
"you must require ",
|
||||
inspect(module),
|
||||
" before invoking the macro ",
|
||||
Exception.format_mfa(module, fun, arity)
|
||||
]
|
||||
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