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
|
||||
-165
@@ -1,165 +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_linux_task:
|
||||
<<: *DEFAULT_TEST_SETTINGS
|
||||
|
||||
container:
|
||||
image: buildpack-deps:trusty
|
||||
cpu: 8
|
||||
memory: 1536Mi
|
||||
|
||||
env:
|
||||
PATH: "${CIRRUS_WORKING_DIR}/otp/bin:${PATH}"
|
||||
|
||||
matrix:
|
||||
- name: Linux, ${OTP_RELEASE}, Ubuntu 14.04
|
||||
alias: Linux Stable
|
||||
matrix:
|
||||
- env:
|
||||
CHECK_POSIX_COMPLIANT: true
|
||||
CHECK_REPRODUCIBLE: true
|
||||
OTP_RELEASE: OTP-23.0
|
||||
- env:
|
||||
OTP_RELEASE: OTP-22.3
|
||||
- env:
|
||||
OTP_RELEASE: OTP-22.0
|
||||
- env:
|
||||
OTP_RELEASE: OTP-21.3.8
|
||||
- env:
|
||||
OTP_RELEASE: OTP-21.0
|
||||
|
||||
- name: Linux, OTP-${OTP_RELEASE}, development, Ubuntu 14.04
|
||||
alias: Linux Development
|
||||
allow_failures: true
|
||||
skip_notifications: true
|
||||
depends_on:
|
||||
- Linux Stable
|
||||
- FreeBSD Stable
|
||||
matrix:
|
||||
- env:
|
||||
OTP_RELEASE: master
|
||||
- env:
|
||||
OTP_RELEASE: maint
|
||||
|
||||
install_script:
|
||||
- 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 ${CIRRUS_WORKING_DIR}/otp
|
||||
- rm -rf .git
|
||||
- make compile
|
||||
|
||||
build_info_script: bin/elixir --version
|
||||
|
||||
test_formatted_script:
|
||||
- make 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: make test_erlang
|
||||
|
||||
test_elixir_script: make test_elixir
|
||||
|
||||
check_posix_compliant_script: |
|
||||
if [ -n "$CHECK_POSIX_COMPLIANT" ]; then
|
||||
apt update
|
||||
apt install -y shellcheck
|
||||
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"
|
||||
else
|
||||
echo "The format of the shell scripts is only checked in the last stable Erlang/OTP version."
|
||||
fi
|
||||
|
||||
check_reproducible_script: |
|
||||
if [ -n "$CHECK_REPRODUCIBLE" ]; then
|
||||
make check_reproducible
|
||||
else
|
||||
echo "The reproducibility of the build is only checked in the last stable Erlang/OTP version."
|
||||
fi
|
||||
|
||||
|
||||
test_windows_task:
|
||||
<<: *DEFAULT_TEST_SETTINGS
|
||||
|
||||
name: Windows, OTP-${OTP_RELEASE}, Windows Server 2019
|
||||
alias: Windows Stable
|
||||
|
||||
matrix:
|
||||
- env:
|
||||
OS_VERSION: 2019
|
||||
OTP_RELEASE: 22.0
|
||||
|
||||
- env:
|
||||
OS_VERSION: 2019
|
||||
OTP_RELEASE: 21.0.1
|
||||
|
||||
windows_container:
|
||||
image: fertapric/elixir-ci:otp-win64-${OTP_RELEASE}
|
||||
os_version: ${OS_VERSION}
|
||||
cpu: 4
|
||||
memory: 6GB
|
||||
|
||||
install_script:
|
||||
- rmdir /s /q .git
|
||||
- make compile
|
||||
|
||||
build_info_script: bin/elixir --version
|
||||
|
||||
test_formatted_script:
|
||||
- make test_formatted &&
|
||||
echo "All Elixir source code files are properly formatted."
|
||||
|
||||
test_erlang_script: make --keep-going test_erlang
|
||||
|
||||
test_elixir_script: make --keep-going test_elixir
|
||||
|
||||
|
||||
test_freebsd_task:
|
||||
<<: *DEFAULT_TEST_SETTINGS
|
||||
|
||||
name: FreeBSD 12.1
|
||||
alias: FreeBSD Stable
|
||||
|
||||
freebsd_instance:
|
||||
image_family: freebsd-12-1
|
||||
cpu: 8
|
||||
memory: 7424Mi
|
||||
|
||||
env:
|
||||
CHECK_REPRODUCIBLE: true
|
||||
LC_ALL: en_US.UTF-8
|
||||
|
||||
install_script:
|
||||
- pkg install -y erlang 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
|
||||
@@ -1,17 +0,0 @@
|
||||
on: check_suite
|
||||
name: CI email
|
||||
jobs:
|
||||
sendEmail:
|
||||
name: Send email
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Send email
|
||||
# Source: https://github.com/elixir-lang/elixir-ci
|
||||
uses: docker://fertapric/elixir-ci-email:latest
|
||||
env:
|
||||
APP_NAME: Cirrus CI
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
MAIL_FROM: ci@elixir-lang.org
|
||||
MAIL_HOST: smtp.sendgrid.net
|
||||
MAIL_PASSWORD: ${{ secrets.CI_EMAIL_PASSWORD }}
|
||||
MAIL_USERNAME: ${{ secrets.CI_EMAIL_USERNAME }}
|
||||
+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
|
||||
+148
-325
@@ -1,391 +1,214 @@
|
||||
# Changelog for Elixir v1.10
|
||||
# Changelog for Elixir v1.8
|
||||
|
||||
## Support for Erlang/OTP 21+
|
||||
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.
|
||||
|
||||
Elixir v1.10 requires Erlang/OTP 21+, allowing Elixir to integrate with Erlang/OTP's new logger. Currently, this means that the logger level, logger metadata, as well as all log messages are now shared between Erlang and Elixir APIs.
|
||||
## Custom struct inspections
|
||||
|
||||
We will continue improving the relationship between the logging systems in future releases. In particular, we plan to expose all log levels and runtime filtering functionalities available in Erlang directly into Elixir in the next Elixir version.
|
||||
|
||||
This release also adds two new guards, `is_struct/1` and `is_map_key/2`, thanks to the strict requirement on Erlang/OTP 21+.
|
||||
|
||||
## Releases improvements
|
||||
|
||||
Elixir v1.9 introduced releases as a mechanism to package self-contained applications. Elixir v1.10 further improves releases with bug fixes and new enhancements based on feedback we got from the community. The highlights are:
|
||||
|
||||
* Allow the dual boot system of releases to be disabled on environments that are boot-time sensitive, such as embedded devices
|
||||
|
||||
* Track and raise if compile-time configuration is set or changes at runtime (more in the next section)
|
||||
|
||||
* Support for easily adding extra files to releases via overlays
|
||||
|
||||
* Allow `RELEASE_DISTRIBUTION` to be set to `none` in order to fully disable it
|
||||
|
||||
* Add a built-in `:tar` step that automatically packages releases
|
||||
|
||||
See the full CHANGELOG for more improvements.
|
||||
|
||||
## Improvements to sort-based APIs in Enum
|
||||
|
||||
`Enum.sort/1` in Elixir by default sorts from lowest to highest:
|
||||
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> Enum.sort(["banana", "apple", "pineapple"])
|
||||
["apple", "banana", "pineapple"]
|
||||
```
|
||||
|
||||
If you want to sort from highest to lowest, you need to call `Enum.sort/2` with a custom sorting function, such as `Enum.sort(collection, &>=/2)`, which is not immediately obvious to someone reading the code:
|
||||
|
||||
```elixir
|
||||
iex> Enum.sort(["banana", "apple", "pineapple"], &>=/2)
|
||||
["pineapple", "banana", "apple"]
|
||||
```
|
||||
|
||||
Furthermore, comparison operators, such as `<=` and `>=`, perform structural sorting, instead of a semantic one. For example, using `>=` to sort dates descendingly won't yield the correct result:
|
||||
|
||||
```elixir
|
||||
iex> Enum.sort([~D[2019-12-31], ~D[2020-01-01]])
|
||||
[~D[2020-01-01], ~D[2019-12-31]]
|
||||
```
|
||||
|
||||
To perform proper semantic comparison for dates, one would also need to pass a custom sorting function:
|
||||
|
||||
```elixir
|
||||
iex> Enum.sort([~D[2019-12-31], ~D[2020-01-01]], &(Date.compare(&1, &2) != :lt))
|
||||
[~D[2019-12-31], ~D[2020-01-01]]
|
||||
```
|
||||
|
||||
Elixir v1.10 streamlines the sorting functions by introducing both `:asc` and `:desc` shortcuts:
|
||||
|
||||
```elixir
|
||||
iex> Enum.sort(["banana", "apple", "pineapple"], :asc)
|
||||
["apple", "banana", "pineapple"]
|
||||
iex> Enum.sort(["banana", "apple", "pineapple"], :desc)
|
||||
["pineapple", "banana", "apple"]
|
||||
```
|
||||
|
||||
As well as adding the possibility to pass a module to perform semantic comparisons. For example, to sort dates, one now only needs to pass the `Date` module or even `{:desc, Date}` for descending semantical sort:
|
||||
|
||||
```elixir
|
||||
iex> Enum.sort([~D[2019-12-31], ~D[2020-01-01]], Date)
|
||||
[~D[2019-12-31], ~D[2020-01-01]]
|
||||
iex> Enum.sort([~D[2019-12-31], ~D[2020-01-01]], {:desc, Date})
|
||||
[~D[2020-01-01], ~D[2019-12-31]]
|
||||
```
|
||||
|
||||
These API improvements make the code more concise and readable and they have also been added to `Enum.sort_by`, `Enum.min_by`, `Enum.max_by`, and friends.
|
||||
|
||||
## Tracking of compile-time configuration
|
||||
|
||||
In Elixir, we organize our code in applications. Libraries, your dependencies, and your own project are all separate applications. All applications in Elixir also come with an application environment.
|
||||
|
||||
The application environment is a key-value store that allows us to configure said application. 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 `Application.get_env/3` outside of a function:
|
||||
|
||||
```elixir
|
||||
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
|
||||
defmodule User do
|
||||
defstruct [:id, :name, :age, :email, :encrypted_password]
|
||||
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 you are using `mix release` and your `config/releases.exs` has:
|
||||
|
||||
config :my_app, :db_host, "db.production"
|
||||
|
||||
Because `config/releases.exs` is read after the code is compiled, the new value will have no effect as the code was compiled to connect to "db.local".
|
||||
|
||||
Of course, the obvious solution to this mismatch is to not read the application environment at compilation time in the first place, and instead move the code to inside a function:
|
||||
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
|
||||
defmodule MyApp.DBClient do
|
||||
def start_link() do
|
||||
SomeLib.DBClient.start_link(host: db_host())
|
||||
end
|
||||
defp db_host() do
|
||||
Application.get_env(:my_app, :db_host, "db.local")
|
||||
end
|
||||
defmodule User do
|
||||
@derive {Inspect, only: [:id, :name, :age]}
|
||||
defstruct [:id, :name, :age, :email, :encrypted_password]
|
||||
end
|
||||
```
|
||||
|
||||
While this is the preferred approach, there are still two scenarios we need to address:
|
||||
Now all user structs will be printed with all remaining fields collapsed:
|
||||
|
||||
1. Not everyone may be aware of this pitfall, so they will mistakenly read the application environemnt at compile-time, until they are bitten by this behaviour
|
||||
#User<id: 1, name: "Jane", age: 33, ...>
|
||||
|
||||
2. In rare occasions, you trully need to read the application environment at compile-time, and you want to be warned when you try to configure at runtime something that is valid only at compilation time
|
||||
You can also pass `@derive {Inspect, except: [...]}` in case you want to keep all fields by default and exclude only some.
|
||||
|
||||
Elixir v1.10 aims to solve these two scenarios by introducing a `Application.compile_env/3` function. For example, to read the value at compile time, you can now do:
|
||||
## Time zone database support
|
||||
|
||||
```elixir
|
||||
@db_host Application.compile_env(:my_app, :db_host, "db.local")
|
||||
```
|
||||
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.
|
||||
|
||||
By using `compile_env/3`, Elixir will store the values used during compilation and compare them with the runtime values whenever your system starts, raising an error in case they differ. This helps developers ensure they are running their production systems with the configuration they intend to.
|
||||
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.
|
||||
|
||||
In future versions, we will deprecate the use `Application.get_env` at compile-time with a clear message pointing users to configuration best practices, effectively addressing the scenario where users read from the application environment at compile time unaware of its pitfalls.
|
||||
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`.
|
||||
|
||||
## Compiler tracing
|
||||
## Faster compilation and other performance improvements
|
||||
|
||||
This release brings enhancements to the Elixir compiler and adds new capabilities for developers to listen to compilation events.
|
||||
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.
|
||||
|
||||
In previous Elixir versions, Elixir would compile a database of cross references between modules (such as function calls, references, structs, etc) for each project in order to perform all kinds of checks, such as deprecations and undefined functions.
|
||||
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.
|
||||
|
||||
Although this database was not public, developers would still use it to run their own checks against their projects. With time, developers would request more data to be included in the database, which was problematic as Elixir itself did not have a use for the additional data, and the database was not meant to be used externally in the first place.
|
||||
Finally, EEx templates got their own share of optimizations, emitting more compact code that runs faster.
|
||||
|
||||
In Elixir v1.10, we have addressed these problems by introducing compiler tracing. The compiler tracing allows developers to listen to events as they are emitted by the compiler, so they can store all of the information they need - and only the information they need.
|
||||
## Improved instrumentation and ownership with `$callers`
|
||||
|
||||
Elixir itself is using the new compiler tracing to provide new functionality. One advantage of this approach is that developers can now disable undefined function warnings directly on the callsite. For example, imagine you have an optional dependency which may not be available in some cases. You can tell the compiler to skip warning on calls to optional modules with:
|
||||
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.
|
||||
|
||||
@compile {:no_warn_undefined, OptionalDependency}
|
||||
defdelegate my_function_call(arg), to: OptionalDependency
|
||||
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.
|
||||
|
||||
Previously, this information had to be added to the overall project configuration, which was far away from where the optional call effectively happened.
|
||||
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:
|
||||
|
||||
## Other enhancements
|
||||
[your code] -- calls --> [supervisor] ---- spawns --> [task]
|
||||
|
||||
Elixir's calendar data types got many improvements, such as sigil support for third-party calendars, as well as the additions of `DateTime.now!/2`, `DateTime.shift_zone!/3`, and `NaiveDateTime.local_now/0`.
|
||||
which means we store the following relationships:
|
||||
|
||||
There are many improvements related to Elixir's AST in this release too. First of all, `Code.string_to_quoted/2` has two new options, `:token_metadata` and `:literal_encoder`, that give more control over Elixir's parser. This information was already available to the Elixir code formatter and has now been made public. We have also extensively documented all of Elixir's AST metadata. These changes alongside compiler tracing means static analyzers and IDE integrations have a better foundation to analyze the source code.
|
||||
[your code] [supervisor] <-- ancestor -- [task]
|
||||
^ |
|
||||
|--------------------- caller ---------------------|
|
||||
|
||||
ExUnit, our test framework, ships two small but important improvements: `ExUnit.CaptureIO` can now be used by tests that run concurrently and we have added "pattern-matching diffing". To understand the last feature, take this code:
|
||||
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`.
|
||||
|
||||
```elixir
|
||||
assert %{"status" => 200, "body" => %{"key" => "foo"}} = json_payload
|
||||
```
|
||||
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.
|
||||
|
||||
Now imagine that `json_payload` is a large JSON blob and the `"key"` inside the `"body"` did not have value of `"foo"`. In previous Elixir versions, if the assertion failed, Elixir would print the right side and let you up to your own devices to figure out what went wrong. In Elixir v1.10, we diff the data structure against the pattern so you can see exactly which parts of the data matched the pattern and which ones did not. Note ExUnit already performed diffing when comparing data types, this new version adds diffing when matching data against a pattern.
|
||||
|
||||
## v1.10.4 (2020-07-04)
|
||||
## v1.8.2 (2019-05-11)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Kernel] Fix a bug where custom types were printed as built-in types
|
||||
* [Kernel] Don't add compile-time dependency on `defdelegate`
|
||||
* [Kernel] Add line numbers to warnings on deprecated imports
|
||||
* [Kernel] Report the correct line number when raising inside a macro
|
||||
* [Task] Include callers in translated Logger metadata for Task
|
||||
* [Task] Fix Task PID and caller in Task Supervisor reports
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit.Formatter] Avoid crashes when diffing guards when the pattern does not match
|
||||
* [ExUnit.Formatter] Also blame exceptions that come from linked and trapped exits
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.Helpers] Do not crash when printing a type that cannot be code formatted
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix app.start] Fix reading `.app` file located in archives (`.ez` files)
|
||||
* [mix local.hex] Provide more guidance when Hex can't be installed
|
||||
* [mix release] Properly encode config in releases
|
||||
|
||||
## v1.10.3 (2020-04-25)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] Return `[{mod, bin}]` from `Code.compile_file/2`, `Code.require_file/2`, `Code.load_file/2`
|
||||
* [Code] Make sure the formatter respects newlines before and after module attributes
|
||||
* [Kernel.ParallelCompiler] Fix a bug where the parallel compiler would raise in long compilation cycles
|
||||
* [Kernel.ParallelCompiler] Fix a bug where the parallel compiler would raise if some of the modules being compiled referred to a module that has been loaded directly to memory
|
||||
* [Module] Fix accidental breaking change where bodiless clauses had their body value on `@on_definition` callbacks set to an empty list instead of `nil`
|
||||
* [String] Undeprecate `String.normalize/2` normalize and fix infinite loop caused by certain invalid strings
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit.Assertions] Fix pattern matching diff when matching on pinned variables
|
||||
* [ExUnit.Assertions] Fix pattern matching diff when matching variable struct names
|
||||
* [ExUnit.Assertions] Fix pattern matching diff when matching on the binary concat operator (`<>`) and the left side is not a literal string
|
||||
* [ExUnit.Assertions] Fix pattern matching diff when matching on pseudo-vars (`__MODULE__`, `__DIR__`, etc)
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix release] Respect the `:path` option when creating a `:tar` file for releases
|
||||
|
||||
## v1.10.2 (2020-02-26)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Macro] Fix a bug where `Macro.to_string/1` would emit invalid code for sigils
|
||||
* [Task] Do not crash `async_stream` monitor if it receives spurious DOWN messages
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Fix a bug where the Logger formatter would fail when handling unknown metadata values
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix compile] Do not write files to disk if `--warnings-as-errors` was given and warnings were emitted
|
||||
|
||||
## v1.10.1 (2020-02-10)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] Do not emit invalid code when formatting `nil`, `false`, and `true` keys in maps
|
||||
* [Kernel] Ensure `with` clauses properly unpack "implicit guards" (such as matching on the struct name)
|
||||
* [Kernel] Do not warn if commas are used by themselves in `~w`/`~W` sigils
|
||||
* [Kernel] Do not validate the `:line` option in quote (the validation has been moved to v1.11 to give users more time to update their code)
|
||||
* [Module] Ensure the code verifier handles the `:erlang.size/1` guard properly
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Properly handle the `report_cb/2` option from Erlang
|
||||
* [Logger] Fix truncation for multi-byte characters
|
||||
* [Logger] Do not rebroadcast messages from remote nodes as this is now taken care by Erlang's logger
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Ensure `assert_receive` produces valid exception messages in case of errors
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix release] Make sure the install command (Window specific) works on paths with spaces in the name
|
||||
* [mix release] Allow using `remote` and `rpc` commands with `Application.compile_env/3`
|
||||
|
||||
## v1.10.0 (2020-01-27)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Application] Add `Application.compile_env/3` and `Application.compile_env!/2` for reading values at compilation time and tracking if they accidentally change during runtime
|
||||
* [Calendar] Allow custom calendar representations in calendar sigils
|
||||
* [Calendar] Add `c:Calendar.parse_time/1`, `c:Calendar.parse_date/1`, `c:Calendar.parse_naive_datetime/1` and `c:Calendar.parse_utc_datetime/1` callbacks to calendar behaviour
|
||||
* [CLI] Add support for `NO_COLOR` environment variable
|
||||
* [Code] Add `:token_metadata` and `:literal_encoder` support to `Code.string_to_quoted/2`
|
||||
* [Code] Add compiler tracing to lift events done by the compiler
|
||||
* [Code] Return `{:error, :unavailable}` in `Code.ensure_compiled/1` if module is in a deadlock
|
||||
* [DateTime] Add `DateTime.now!/2` and `DateTime.shift_zone!/3`
|
||||
* [Enum] Speed up getting one random element from enumerables
|
||||
* [Enum] Add `Enum.frequencies/1`, `Enum.frequencies_by/2`, and `Enum.map_intersperse/2`
|
||||
* [Enum] Allow a sorting function on `Enum.min/max/min_by/max_by`
|
||||
* [Enum] Add `asc/desc` and `compare/1` support to `Enum.sort/2`
|
||||
* [Exception] Add version alongside app names in stacktraces
|
||||
* [Function] Add `Function.identity/1`
|
||||
* [Kernel] Add `Kernel.is_struct/1` and `Kernel.is_map_key/2`
|
||||
* [Kernel] Warn when function head comes immediately after the implementation instead of before the implementation
|
||||
* [Kernel] Warn if duplicate key is found in struct declaration
|
||||
* [Kernel] Print all undefined functions as warnings and then raise. This allows users to see all undefined calls at once, when it would otherwise require them to compile the code multiple times
|
||||
* [Kernel] Allow file, line and context to be dynamically set on `quote`
|
||||
* [Keyword] Add `Keyword.pop!/2` and `Keyword.pop_values/2`
|
||||
* [Map] Add `Map.pop!/2`
|
||||
* [MapSet] Optimize multiple operations
|
||||
* [Module] Add `Module.has_attribute?/2`
|
||||
* [Module] Add `@compile {:no_warn_undefined, mfa_or_module}` to turn off undefined function warnings
|
||||
* [NaiveDateTime] Add `NaiveDateTime.local_now/0`
|
||||
* [Record] Warn if duplicate key is found in record declaration
|
||||
* [String] Update to Unicode 12.1
|
||||
* [StringIO] Add `:encoding` option to StringIO and optimize `get_chars` operation
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit.Assertions] Support diffs in pattern matching and in `assert_receive`
|
||||
* [ExUnit.CaptureIO] Supports capturing named devices in asynchronous tests
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx] Warn on circular file imports when loading default `.iex.exs`
|
||||
* [IEx] Allow customization of the continuation prompt on IEx
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Allow `start_options` to be configured on Logger's GenEvent
|
||||
* [Logger] Integrate Elixir's Logger with Erlang/OTP 21+'s logger. This means setting up the logger level in Elixir will automatically change the logger level for Erlang and vice-versa
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix compile] Add `--profile time` flag to profile compilation steps
|
||||
* [mix deps.compile] Add `--skip-umbrella-children` flag. The new flag does not compile umbrella apps. This is useful for building caches in CD/CI pipelines
|
||||
* [mix deps.unlock] Add `--check-unused` flag. The new flag raises if there are any unused dependencies in the lock file
|
||||
* [mix release] Allow `RELEASE_DISTRIBUTION` to be set to `none`
|
||||
* [mix release] Support overlays in `rel/overlays`
|
||||
* [mix release] Allow configuration reboot to be disabled in releases
|
||||
* [mix test] Add support for simple round-robin test partitioning across multiple machines
|
||||
* [Mix.Project] Add `MIX_DEPS_PATH` environment variable for setting `:deps_path`
|
||||
* [Mix.Project] Add `Mix.Project.deps_scms/1` that returns deps with their SCMs
|
||||
* [Mix.Task] Add `Mix.Task.Compiler.after_compiler/2` callback, to simplify compilers that may need to run something at multiple steps
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx] Ensure multiline do/end with no spaces compile under trim mode
|
||||
* [EEx] Raise readable error message on bad EEx state
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Enum] Allow positive range slices on infinite streams given to `Enum.slice/2`
|
||||
* [Kernel] Raise error on functions/guards without implementation
|
||||
* [Kernel] Do not expand expressions inside interpolation twice
|
||||
* [Keyword] Ensure keyword replace and update preserve order
|
||||
* [Module] Raise instead of silently failing when performing a write module operation during after-compile
|
||||
* [Module] Fix `@macrocallback` definitions with a `when` clause
|
||||
* [Path] Fix `Path.absname/1` to correctly handle UNC paths on Windows
|
||||
* [Stream] Close with correct accumulator in `Stream.resource/3` when called for a single-element list
|
||||
* [Stream] Allow `Stream.cycle/1` to be double nested inside `Stream.cycle/1`
|
||||
* [URI] Preserve slashes in URIs without authority
|
||||
* [URI] Require a nil or an absolute path on URIs with host or authority
|
||||
* [Protocol] Ensure `:debug_info` is kept in protocols
|
||||
|
||||
#### ExUnit
|
||||
#### Logger
|
||||
|
||||
* [ExUnit.Assertions] Fix `assert_receive` and `assert match?` to behave consistently compared `receive` and `match?` when given an invalid macro
|
||||
* [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
|
||||
|
||||
* [Float] Fix rounding for subnormal floats
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx] Exit IEx session if the group leader exits
|
||||
* [IEx] Allow `pry` to be used in non-tty terminals
|
||||
* [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 compile] Do not filter out warning for external files from diagnostics
|
||||
* [Mix.Project] Ensure user given `:manager` to dependencies has higher precedence than the SCM one
|
||||
* [Mix.Project] Recompile umbrella children when config files change and `mix compile` is called from the umbrella root
|
||||
* [Mix.Task] Always recompile before running tasks from dependencies
|
||||
* [Mix.Task] Ensure project's Logger config is used when running Mix tasks
|
||||
* [mix compile] Do not include optional dependencies in extra applications as it is incompatible with shared deps in umbrellas
|
||||
|
||||
### 3. Soft-deprecations (no warnings emitted)
|
||||
## v1.8.0 (2019-01-14)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx] Optimize the default template engine to compile and execute more efficiently
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] `compiler_options/0` is deprecated in favor of `compiler_option/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] Add `ExUnit.after_suite/1` callback
|
||||
* [ExUnit.Assertions] Show last N messages (instead of first N) from mailbox on `assert_receive` fail
|
||||
|
||||
#### IEx
|
||||
|
||||
* [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.Config] `Mix.Config.persist/1` has been deprecated. Instead of `Mix.Config.persist(config)` use `Application.put_all_env(config, persistent: true)` (`Application.put_all_env/2` was added in v1.9)
|
||||
* [mix xref] `calls/0` is deprecated in favor of compiler tracer
|
||||
* [mix xref] The `xref.exclude` option has been moved to `elixirc_options.no_warn_undefined` as the `xref` pass has been moved into the compiler
|
||||
* [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
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [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.Helpers] Use typespec info (instead of docs chunk) and properly format callbacks in `b/1`
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Allow Logger backends to be dynamically removed when an application is shutting down
|
||||
|
||||
#### Mix
|
||||
|
||||
* [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)
|
||||
|
||||
None.
|
||||
|
||||
### 4. Hard-deprecations
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] `Code.load_file/2` has been deprecated in favor of `Code.require_file/2` or `Code.compile_file/2`
|
||||
* [Code] `Code.loaded_files/0` and `Code.unload_file/1` have been deprecated in favor of `Code.required_files/0` and `Code.unrequire_file/1` respectively
|
||||
* [Code] `Code.ensure_compiled?/1` is deprecated in favor of `Code.ensure_compiled/1`
|
||||
* [String] `String.normalize/2` has been deprecated in favor of `:unicode.characters_to_nfc_binary/1` or `:unicode.characters_to_nfd_binary/1` which ship as part of Erlang/OTP 20+
|
||||
* [Supervisor] `Supervisor.Spec.supervise/2` has been deprecated in favor of the new Supervisor child specification
|
||||
* [Supervisor] The `:simple_one_for_one` strategy in `Supervisor` has been deprecated in favor of `DynamicSupervisor`
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] `:compile_time_purge_level` application environment configuration has been deprecated in favor of the more general `:compile_time_purge_matching` config
|
||||
* [Logger] Deprecate logging non-chardata values
|
||||
* [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 compile.xref] This check has been moved into the compiler and has no effect now
|
||||
* [mix xref] `xref` now only tracks dependencies between modules and files, no longer between functions. See "Compilation tracers" to learn more about how to track this information directly
|
||||
* [mix xref deprecations] This check has been moved into the compiler and has no effect now
|
||||
* [mix xref unreachable] This check has been moved into the compiler and has no effect now
|
||||
* [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.9
|
||||
## v1.7
|
||||
|
||||
The CHANGELOG for v1.9 releases can be found [in the v1.9 branch](https://github.com/elixir-lang/elixir/blob/v1.9/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).
|
||||
|
||||
+3
-7
@@ -39,11 +39,11 @@ 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-lang][2]** IRC channel on [Freenode][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 remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
|
||||
|
||||
@@ -54,7 +54,3 @@ 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 with parts derived from Django's Code of Conduct, Rust's Code of Conduct and the Contributor Covenant.
|
||||
|
||||
[1]: https://github.com/elixir-lang/
|
||||
[2]: https://webchat.freenode.net/?channels=#elixir-lang
|
||||
[3]: https://www.freenode.net
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
### Precheck
|
||||
|
||||
* Do not use the issue tracker for help or support (try Elixir Forum, Stack Overflow, IRC, etc.)
|
||||
* 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
|
||||
@@ -1,10 +1,9 @@
|
||||
PREFIX ?= /usr/local
|
||||
TEST_FILES ?= "*_test.exs"
|
||||
SHARE_PREFIX ?= $(PREFIX)/share
|
||||
MAN_PREFIX ?= $(SHARE_PREFIX)/man
|
||||
CANONICAL := v1.10/ # master/ or vMAJOR.MINOR/
|
||||
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict $(ELIXIRC_OPTS)
|
||||
ERLC := erlc -I lib/elixir/include $(ERLC_OPTS)
|
||||
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))
|
||||
@@ -17,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 >= 21)])' -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 21.0 is required to build Elixir"; \
|
||||
echo "At least Erlang/OTP 20.0 is required to build Elixir"; \
|
||||
exit 1; \
|
||||
fi
|
||||
endef
|
||||
@@ -46,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
|
||||
@@ -90,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)
|
||||
@@ -127,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) diff -r lib/elixir/ebin/ lib/elixir/tmp/ebin_reproducible/
|
||||
$(Q) diff -r lib/eex/ebin/ lib/eex/tmp/ebin_reproducible/
|
||||
$(Q) diff -r lib/ex_unit/ebin/ lib/ex_unit/tmp/ebin_reproducible/
|
||||
$(Q) diff -r lib/iex/ebin/ lib/iex/tmp/ebin_reproducible/
|
||||
$(Q) diff -r lib/logger/ebin/ lib/logger/tmp/ebin_reproducible/
|
||||
$(Q) diff -r lib/mix/ebin/ lib/mix/tmp/ebin_reproducible/
|
||||
$(Q) echo "Builds are reproducible"
|
||||
|
||||
clean:
|
||||
rm -rf ebin
|
||||
rm -rf lib/*/ebin
|
||||
@@ -176,16 +136,16 @@ 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 = 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)"
|
||||
@@ -230,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:"
|
||||
@@ -240,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
|
||||
@@ -253,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)"
|
||||
@@ -282,9 +230,9 @@ 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
|
||||
|
||||
@@ -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,
|
||||
@@ -27,7 +27,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,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||

|
||||
=========
|
||||
[](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,28 +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**.
|
||||
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).
|
||||
To compile from source, you can follow the steps below.
|
||||
|
||||
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang). Then 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
|
||||
@@ -45,17 +31,24 @@ 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 can use Interactive Elixir by running `bin/iex` in your terminal.
|
||||
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 that you have an outdated Erlang/OTP version
|
||||
(Elixir requires Erlang/OTP 21.0 or later). You can check your Erlang/OTP version
|
||||
by calling `erl` in the command line. You will see some information similar to:
|
||||
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 21 [erts-9.0] [smp:2:2] [async-threads:10] [kernel-poll:false]
|
||||
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
|
||||
@@ -63,14 +56,17 @@ For proposing new features, please start a discussion in the
|
||||
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].
|
||||
The issue tracker focuses on *actionable items* and it holds a list of
|
||||
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 issue tracker and are
|
||||
added to the [changelog][7].
|
||||
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
|
||||
|
||||
@@ -78,20 +74,20 @@ 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`.
|
||||
|
||||
@@ -110,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`.
|
||||
|
||||
@@ -123,8 +119,8 @@ 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 on Cirrus CI](https://cirrus-ci.com/github/elixir-lang/elixir).
|
||||
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/).
|
||||
@@ -156,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
|
||||
|
||||
@@ -168,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]
|
||||
* [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://webchat.freenode.net/?channels=#elixir-lang
|
||||
[5]: https://www.freenode.net
|
||||
[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 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.
|
||||
|
||||
+3
-3
@@ -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,7 +30,7 @@
|
||||
|
||||
1. Set `CANONICAL=` in /Makefile
|
||||
|
||||
2. Update tables in /SECURITY.md and "Compatibility and Deprecations"
|
||||
2. Update tables in "Compatibility and Deprecations"
|
||||
|
||||
3. Commit "Prepare vMAJOR.MINOR for release"
|
||||
|
||||
@@ -40,6 +40,6 @@
|
||||
|
||||
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.10 | Bug fixes and security patches
|
||||
| 1.9 | Security patches only
|
||||
| 1.8 | Security patches only
|
||||
| 1.7 | Security patches only
|
||||
| 1.6 | 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
|
||||
+62
-170
@@ -1,58 +1,31 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
cat <<USAGE >&2
|
||||
Usage: $(basename "$0") [options] [.exs file] [data]
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
## General options
|
||||
-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 (*)
|
||||
|
||||
-e "COMMAND" Evaluates the given command (*)
|
||||
-h, --help Prints this message and exits
|
||||
-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 Elixir version and exits
|
||||
--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)
|
||||
|
||||
--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
|
||||
--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.
|
||||
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
|
||||
|
||||
@@ -62,142 +35,70 @@ readlink_f () {
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
echo "$(pwd -P)/$filename"
|
||||
echo "`pwd -P`/$filename"
|
||||
fi
|
||||
}
|
||||
|
||||
# Stores static Erlang arguments and --erl (which is passed as is)
|
||||
ERL=""
|
||||
|
||||
# Stores erl arguments preserving spaces/quotes (mimics an array)
|
||||
erl () {
|
||||
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 "-setcookie"
|
||||
erl "$2"
|
||||
;;
|
||||
--sname|--name)
|
||||
S=2
|
||||
erl "$(echo "$1" | cut -c 2-)"
|
||||
erl "$2"
|
||||
;;
|
||||
--erl-config)
|
||||
S=2
|
||||
erl "-config"
|
||||
erl "$2"
|
||||
;;
|
||||
--vm-args)
|
||||
S=2
|
||||
erl "-args_file"
|
||||
erl "$2"
|
||||
;;
|
||||
--boot)
|
||||
S=2
|
||||
erl "-boot"
|
||||
erl "$2"
|
||||
;;
|
||||
--boot-var)
|
||||
S=3
|
||||
erl "-boot_var"
|
||||
erl "$2"
|
||||
erl "$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")
|
||||
@@ -206,26 +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
|
||||
|
||||
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 "$@"
|
||||
|
||||
+68
-118
@@ -1,5 +1,5 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
setlocal enabledelayedexpansion
|
||||
setlocal
|
||||
if ""%1""=="""" goto documentation
|
||||
if /I ""%1""==""--help"" goto documentation
|
||||
if /I ""%1""==""-h"" goto documentation
|
||||
@@ -10,155 +10,105 @@ 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 and exits
|
||||
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 Elixir version and exits
|
||||
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 --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 ** 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=
|
||||
|
||||
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"
|
||||
set parsElixir=!parsElixir! -e "!VAR:"=\"!"
|
||||
shift
|
||||
goto startloop
|
||||
)
|
||||
if ""==!par:--eval=! (
|
||||
set "VAR=%~1"
|
||||
set parsElixir=!parsElixir! --eval "!VAR:"=\"!"
|
||||
shift
|
||||
goto startloop
|
||||
)
|
||||
if ""==!par:--rpc-eval=! (
|
||||
set "VAR=%~2"
|
||||
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:--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
|
||||
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
|
||||
endlocal
|
||||
|
||||
+7
-11
@@ -1,24 +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
|
||||
|
||||
--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
|
||||
--verbose Prints compilation 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
|
||||
|
||||
@@ -28,7 +24,7 @@ readlink_f () {
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
echo "$(pwd -P)/$filename"
|
||||
echo "`pwd -P`/$filename"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
+5
-14
@@ -43,8 +43,7 @@ defmodule EEx do
|
||||
* `:file` - the file to be used in the template. Defaults to the given
|
||||
file the template is read from or to "nofile" when compiling from a string.
|
||||
* `:engine` - the EEx engine to be used for compilation.
|
||||
* `:trim` - trims whitespace left/right of quotation tags. If a quotation
|
||||
tag appears on its own in a given line, line endings are also removed.
|
||||
* `:trim` - trims whitespace left/right of quotation tags
|
||||
|
||||
## Engine
|
||||
|
||||
@@ -76,12 +75,6 @@ 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 %>`.
|
||||
|
||||
Notice that different engines may have different rules
|
||||
for each tag. Other tags may be added in future versions.
|
||||
|
||||
@@ -112,7 +105,7 @@ defmodule EEx do
|
||||
|
||||
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"
|
||||
@@ -148,12 +141,11 @@ 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
|
||||
@@ -215,8 +207,7 @@ defmodule EEx do
|
||||
foo <%= bar %>
|
||||
|
||||
# iex
|
||||
EEx.eval_file("sample.eex", bar: "baz")
|
||||
#=> "foo baz"
|
||||
EEx.eval_file "sample.eex", [bar: "baz"] #=> "foo baz"
|
||||
|
||||
"""
|
||||
@spec eval_file(String.t(), keyword, keyword) :: any
|
||||
|
||||
+24
-34
@@ -41,40 +41,35 @@ defmodule EEx.Compiler do
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:expr, line, mark, chars, _} | rest], buffer, scope, state) do
|
||||
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, mark, chars, _} | rest], buffer, scope, state) do
|
||||
{contents, line, rest} = look_ahead_middle(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}
|
||||
)
|
||||
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, '', 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, modifier, chars, trimmed?} | t],
|
||||
[{:middle_expr, line, modifier, chars} | t],
|
||||
buffer,
|
||||
[_ | _] = scope,
|
||||
state
|
||||
@@ -83,43 +78,38 @@ defmodule EEx.Compiler do
|
||||
"unexpected beginning of EEx tag \"<%#{modifier}\" on \"<%#{modifier}#{chars}%>\", " <>
|
||||
"please remove \"#{modifier}\" accordingly"
|
||||
|
||||
:elixir_errors.erl_warn(line, state.file, message)
|
||||
generate_buffer([{:middle_expr, line, '', chars, trimmed?} | 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, _, 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
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, '', 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)
|
||||
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, modifier, chars, trimmed?} | 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, state.file, message)
|
||||
generate_buffer([{:end_expr, line, '', chars, trimmed?} | 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
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, _, 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,
|
||||
@@ -149,10 +139,10 @@ 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}, {:middle_expr, line, _, chars, _} | rest] = tokens,
|
||||
defp look_ahead_text(
|
||||
[{:text, text}, {:middle_expr, line, _, chars} | rest] = tokens,
|
||||
start,
|
||||
contents
|
||||
) do
|
||||
@@ -163,11 +153,11 @@ defmodule EEx.Compiler do
|
||||
end
|
||||
end
|
||||
|
||||
defp look_ahead_middle([{:middle_expr, line, _, chars, _} | rest], _start, contents) do
|
||||
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
|
||||
defp look_ahead_text(tokens, start, contents) do
|
||||
{contents, start, tokens}
|
||||
end
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -24,12 +24,11 @@ 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"
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@@ -4,13 +4,9 @@ defmodule EEx.Tokenizer do
|
||||
@type content :: IO.chardata()
|
||||
@type line :: non_neg_integer
|
||||
@type marker :: '=' | '/' | '|' | ''
|
||||
@type trimmed? :: boolean
|
||||
@type token ::
|
||||
{:text, content}
|
||||
| {:expr | :start_expr | :middle_expr | :end_expr, line, marker, content, trimmed?}
|
||||
|
||||
@spaces [?\s, ?\t]
|
||||
@closing_brackets ')]}'
|
||||
| {:expr | :start_expr | :middle_expr | :end_expr, line, marker, content}
|
||||
|
||||
@doc """
|
||||
Tokenizes the given charlist or binary.
|
||||
@@ -18,10 +14,10 @@ defmodule EEx.Tokenizer do
|
||||
It returns {:ok, list} with the following tokens:
|
||||
|
||||
* `{:text, content}`
|
||||
* `{:expr, line, marker, content, trimmed?}`
|
||||
* `{:start_expr, line, marker, content, trimmed?}`
|
||||
* `{:middle_expr, line, marker, content, trimmed?}`
|
||||
* `{:end_expr, line, marker, content, trimmed?}`
|
||||
* `{:expr, line, marker, content}`
|
||||
* `{:start_expr, line, marker, content}`
|
||||
* `{:middle_expr, line, marker, content}`
|
||||
* `{:end_expr, line, marker, content}`
|
||||
|
||||
Or `{:error, line, error}` in case of errors.
|
||||
"""
|
||||
@@ -48,7 +44,7 @@ defmodule EEx.Tokenizer do
|
||||
error
|
||||
|
||||
{:ok, _, new_line, rest} ->
|
||||
{_, rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
{rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
tokenize(rest, new_line, opts, buffer, acc)
|
||||
end
|
||||
end
|
||||
@@ -62,10 +58,9 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
{:ok, expr, new_line, rest} ->
|
||||
token = token_name(expr)
|
||||
{trimmed?, rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
expr = pad_if_needed(token, expr, trimmed?)
|
||||
{rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
acc = tokenize_text(buffer, acc)
|
||||
final = {token, line, marker, Enum.reverse(expr), trimmed?}
|
||||
final = {token, line, marker, Enum.reverse(expr)}
|
||||
tokenize(rest, new_line, opts, [], [final | acc])
|
||||
end
|
||||
end
|
||||
@@ -115,28 +110,25 @@ defmodule EEx.Tokenizer do
|
||||
#
|
||||
# Start tokens finish with "do" and "fn ->"
|
||||
# Middle tokens are marked with "->" or keywords
|
||||
# End tokens contain only the end word and optionally
|
||||
# combinations of ")", "]" and "}".
|
||||
# End tokens contain only the end word and optionally ")"
|
||||
|
||||
defp token_name([h | t]) when h in @spaces do
|
||||
defp token_name([h | t]) when h in [?\s, ?\t, ?)] do
|
||||
token_name(t)
|
||||
end
|
||||
|
||||
defp token_name('od' ++ [h | rest]) when h in @spaces or h in @closing_brackets do
|
||||
case tokenize_rest(rest) do
|
||||
{:ok, [{:end, _} | _]} -> :middle_expr
|
||||
_ -> :start_expr
|
||||
end
|
||||
defp token_name('od' ++ [h | _]) when h in [?\s, ?\t, ?)] do
|
||||
:start_expr
|
||||
end
|
||||
|
||||
defp token_name('>-' ++ rest) do
|
||||
case tokenize_rest(rest) do
|
||||
{:ok, [{:end, _} | _]} ->
|
||||
:middle_expr
|
||||
rest = Enum.reverse(rest)
|
||||
|
||||
# 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.
|
||||
# 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)
|
||||
@@ -156,19 +148,10 @@ defmodule EEx.Tokenizer do
|
||||
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 token_name(rest) do
|
||||
case Enum.drop_while(rest, &(&1 in @spaces or &1 in @closing_brackets)) do
|
||||
'dne' ++ t -> check_spaces(t, :end_expr)
|
||||
_ -> :expr
|
||||
end
|
||||
end
|
||||
|
||||
# Tokenize the remaining passing check_terminators as false,
|
||||
# which relax the tokenizer to not error on unmatched pairs.
|
||||
# If the tokens start with an "end" we have a middle expr.
|
||||
defp tokenize_rest(rest) do
|
||||
:elixir_tokenizer.tokenize(Enum.reverse(rest), 1, file: "eex", check_terminators: false)
|
||||
defp token_name(_) do
|
||||
:expr
|
||||
end
|
||||
|
||||
defp fn_index(tokens) do
|
||||
@@ -184,7 +167,7 @@ defmodule EEx.Tokenizer do
|
||||
end
|
||||
|
||||
defp check_spaces(string, token) do
|
||||
if Enum.all?(string, &(&1 in @spaces)) do
|
||||
if Enum.all?(string, &(&1 in [?\s, ?\t])) do
|
||||
token
|
||||
else
|
||||
:expr
|
||||
@@ -206,19 +189,24 @@ defmodule EEx.Tokenizer do
|
||||
# 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
|
||||
with true <- opts[:trim],
|
||||
{true, new_buffer} <- trim_left(buffer, acc),
|
||||
{true, new_rest, new_line} <- trim_right(rest, line) do
|
||||
{true, new_rest, new_line, new_buffer}
|
||||
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
|
||||
_ -> {false, rest, line, buffer}
|
||||
original
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_left(buffer, acc) do
|
||||
case {trim_whitespace(buffer), acc} do
|
||||
{[?\n | _] = trimmed_buffer, _} -> {true, trimmed_buffer}
|
||||
{[], [{_, _, _, _, true} | _]} -> {true, []}
|
||||
{[], []} -> {true, []}
|
||||
_ -> {false, buffer}
|
||||
end
|
||||
@@ -233,14 +221,11 @@ defmodule EEx.Tokenizer do
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_whitespace([h | t]) when h in @spaces do
|
||||
defp trim_whitespace([h | t]) when h == ?\s or h == ?\t do
|
||||
trim_whitespace(t)
|
||||
end
|
||||
|
||||
defp trim_whitespace(list) do
|
||||
list
|
||||
end
|
||||
|
||||
defp pad_if_needed(:start_expr, [h | _] = expr, true) when h not in @spaces, do: [?\s | expr]
|
||||
defp pad_if_needed(_, expr, _), do: expr
|
||||
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,19 +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
|
||||
|
||||
defp assert_eval(expected, actual, binding \\ []) do
|
||||
|
||||
@@ -13,28 +13,23 @@ defmodule EEx.TokenizerTest do
|
||||
end
|
||||
|
||||
test "strings with embedded code" do
|
||||
assert T.tokenize('foo <% bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '', ' bar ', false}]}
|
||||
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) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '=', ' bar ', false}]}
|
||||
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) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '/', ' bar ', false}]}
|
||||
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) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '|', ' bar ', false}]}
|
||||
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) ==
|
||||
{:ok, [{:text, 'foo\n'}, {:expr, 2, '=', ' bar ', false}]}
|
||||
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
|
||||
@@ -47,9 +42,9 @@ defmodule EEx.TokenizerTest do
|
||||
|
||||
exprs = [
|
||||
{:text, 'foo '},
|
||||
{:expr, 1, '=', ' bar\n\nbaz ', false},
|
||||
{:expr, 1, '=', ' bar\n\nbaz '},
|
||||
{:text, '\n'},
|
||||
{:expr, 4, '', ' foo ', false},
|
||||
{:expr, 4, '', ' foo '},
|
||||
{:text, '\n'}
|
||||
]
|
||||
|
||||
@@ -68,9 +63,9 @@ defmodule EEx.TokenizerTest do
|
||||
test "quotation with interpolation" do
|
||||
exprs = [
|
||||
{:text, 'a <% b '},
|
||||
{:expr, 1, '=', ' c ', false},
|
||||
{:expr, 1, '=', ' c '},
|
||||
{:text, ' '},
|
||||
{:expr, 1, '=', ' d ', false},
|
||||
{:expr, 1, '=', ' d '},
|
||||
{:text, ' e %> f'}
|
||||
]
|
||||
|
||||
@@ -104,9 +99,9 @@ defmodule EEx.TokenizerTest do
|
||||
test "strings with embedded do end" do
|
||||
exprs = [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' if true do ', false},
|
||||
{:start_expr, 1, '', ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:end_expr, 1, '', ' end ', false}
|
||||
{:end_expr, 1, '', ' end '}
|
||||
]
|
||||
|
||||
assert T.tokenize('foo <% if true do %>bar<% end %>', 1) == {:ok, exprs}
|
||||
@@ -115,50 +110,26 @@ defmodule EEx.TokenizerTest do
|
||||
test "strings with embedded -> end" do
|
||||
exprs = [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' cond do ', false},
|
||||
{:middle_expr, 1, '', ' false -> ', false},
|
||||
{:start_expr, 1, '', ' cond do '},
|
||||
{:middle_expr, 1, '', ' false -> '},
|
||||
{:text, 'bar'},
|
||||
{:middle_expr, 1, '', ' true -> ', false},
|
||||
{:middle_expr, 1, '', ' true -> '},
|
||||
{:text, 'baz'},
|
||||
{:end_expr, 1, '', ' end ', false}
|
||||
{:end_expr, 1, '', ' end '}
|
||||
]
|
||||
|
||||
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with multiple callbacks" do
|
||||
exprs = [
|
||||
{:start_expr, 1, '=', ' a fn -> ', false},
|
||||
{:text, 'foo'},
|
||||
{:middle_expr, 1, '', ' end, fn -> ', false},
|
||||
{:text, 'bar'},
|
||||
{:end_expr, 1, '', ' end ', false}
|
||||
]
|
||||
|
||||
assert T.tokenize('<%= a fn -> %>foo<% end, fn -> %>bar<% end %>', 1) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with callback followed by do block" do
|
||||
exprs = [
|
||||
{:start_expr, 1, '=', ' a fn -> ', false},
|
||||
{:text, 'foo'},
|
||||
{:middle_expr, 1, '', ' end do ', false},
|
||||
{:text, 'bar'},
|
||||
{:end_expr, 1, '', ' end ', false}
|
||||
]
|
||||
|
||||
assert T.tokenize('<%= a fn -> %>foo<% end do %>bar<% end %>', 1) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with embedded keywords blocks" do
|
||||
exprs = [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' if true do ', false},
|
||||
{:start_expr, 1, '', ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:middle_expr, 1, '', ' else ', false},
|
||||
{:middle_expr, 1, '', ' else '},
|
||||
{:text, 'baz'},
|
||||
{:end_expr, 1, '', ' end ', false}
|
||||
{:end_expr, 1, '', ' end '}
|
||||
]
|
||||
|
||||
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1) == {:ok, exprs}
|
||||
@@ -168,11 +139,11 @@ defmodule EEx.TokenizerTest do
|
||||
template = '\t<%= if true do %> \n TRUE \n <% else %>\n FALSE \n <% end %> '
|
||||
|
||||
exprs = [
|
||||
{:start_expr, 1, '=', ' if true do ', true},
|
||||
{:start_expr, 1, '=', ' if true do '},
|
||||
{:text, ' TRUE \n'},
|
||||
{:middle_expr, 3, '', ' else ', true},
|
||||
{:middle_expr, 3, '', ' else '},
|
||||
{:text, ' FALSE \n'},
|
||||
{:end_expr, 5, '', ' end ', true}
|
||||
{:end_expr, 5, '', ' end '}
|
||||
]
|
||||
|
||||
assert T.tokenize(template, 1, trim: true) == {:ok, exprs}
|
||||
@@ -189,7 +160,7 @@ defmodule EEx.TokenizerTest do
|
||||
test "trim mode with CRLF" do
|
||||
exprs = [
|
||||
{:text, '0\r\n'},
|
||||
{:expr, 2, '=', ' 12 ', true},
|
||||
{:expr, 2, '=', ' 12 '},
|
||||
{:text, '34'}
|
||||
]
|
||||
|
||||
@@ -199,7 +170,7 @@ defmodule EEx.TokenizerTest do
|
||||
test "trim mode set to false" do
|
||||
exprs = [
|
||||
{:text, ' '},
|
||||
{:expr, 1, '=', ' 12 ', false},
|
||||
{:expr, 1, '=', ' 12 '},
|
||||
{:text, ' \n'}
|
||||
]
|
||||
|
||||
|
||||
+1
-150
@@ -64,20 +64,6 @@ 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 %> \n456\n <%= 789 %>"
|
||||
expected = "123456\n789"
|
||||
@@ -98,32 +84,6 @@ defmodule EExTest do
|
||||
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 lineSecond lineThird lineFourth line"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
end
|
||||
|
||||
test "trim mode with no spaces" do
|
||||
string = """
|
||||
<%=cond do%>
|
||||
<%false ->%>
|
||||
this
|
||||
<%true ->%>
|
||||
that
|
||||
<%end%>
|
||||
"""
|
||||
|
||||
expected = " that\n"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
end
|
||||
|
||||
test "embedded code" do
|
||||
assert_eval("foo bar", "foo <%= :bar %>")
|
||||
end
|
||||
@@ -140,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
|
||||
@@ -178,27 +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 ",
|
||||
"<%= 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) ) %>"
|
||||
@@ -283,20 +217,6 @@ defmodule EExTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "error messages" do
|
||||
test "honor line numbers" do
|
||||
assert_raise EEx.SyntaxError, "nofile:99: 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: missing token '%>'", fn ->
|
||||
EEx.compile_string("foo <%= bar", file: "my_file.eex")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "environment" do
|
||||
test "respects line numbers" do
|
||||
expected = """
|
||||
@@ -422,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
|
||||
@@ -653,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
|
||||
|
||||
+8
-12
@@ -18,7 +18,6 @@
|
||||
Float,
|
||||
Function,
|
||||
Integer,
|
||||
Module,
|
||||
NaiveDateTime,
|
||||
Record,
|
||||
Regex,
|
||||
@@ -26,8 +25,7 @@
|
||||
Time,
|
||||
Tuple,
|
||||
URI,
|
||||
Version,
|
||||
Version.Requirement
|
||||
Version
|
||||
],
|
||||
"Collections & Enumerables": [
|
||||
Access,
|
||||
@@ -59,12 +57,16 @@
|
||||
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,
|
||||
@@ -84,12 +86,6 @@
|
||||
Protocol,
|
||||
String.Chars
|
||||
],
|
||||
"Code & Macros": [
|
||||
Code,
|
||||
Kernel.ParallelCompiler,
|
||||
Macro,
|
||||
Macro.Env
|
||||
],
|
||||
Deprecated: [
|
||||
Behaviour,
|
||||
Dict,
|
||||
|
||||
@@ -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]).
|
||||
|
||||
+100
-83
@@ -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. The key can be of any type and it returns `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,41 +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. This is important because, if a map has a predefined
|
||||
> set of keys and a key is missing, it is most likely a bug
|
||||
> in your software or a typo on the key name. For this reason,
|
||||
> because structs are predefined in nature, 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:
|
||||
|
||||
@@ -71,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},
|
||||
@@ -89,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
|
||||
@@ -236,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).
|
||||
@@ -329,14 +365,6 @@ defmodule Access do
|
||||
|
||||
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 -> {get_value, value} | :pop)) :: {get_value, data}
|
||||
when get_value: var, data: container
|
||||
@@ -637,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:
|
||||
@@ -667,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, get_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
|
||||
|
||||
@@ -697,17 +724,7 @@ defmodule Access do
|
||||
end
|
||||
end
|
||||
|
||||
defp get_and_update_at(list, index, next, updates) when index < 0 do
|
||||
list_length = length(list)
|
||||
|
||||
if list_length + index >= 0 do
|
||||
get_and_update_at(list, list_length + index, next, updates)
|
||||
else
|
||||
{nil, list}
|
||||
end
|
||||
end
|
||||
|
||||
defp get_and_update_at([head | rest], index, next, updates) when index > 0 do
|
||||
defp get_and_update_at([head | rest], index, next, updates) do
|
||||
get_and_update_at(rest, index - 1, next, [head | updates])
|
||||
end
|
||||
|
||||
|
||||
+6
-40
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
+106
-326
@@ -8,120 +8,78 @@ defmodule Application do
|
||||
|
||||
An application is a component implementing some specific functionality, with a
|
||||
standardized directory structure, configuration, and lifecycle. Applications
|
||||
are *loaded*, *started*, and *stopped*. Each application also has its own
|
||||
environment, which provides a unified API for configuring each application.
|
||||
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` file. 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:
|
||||
|
||||
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.
|
||||
config :APP_NAME, redis_host: "redis.local"
|
||||
|
||||
### Compile-time environment
|
||||
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.
|
||||
|
||||
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 you are using `mix release`
|
||||
and your `config/releases.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:
|
||||
|
||||
@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:
|
||||
@@ -158,19 +116,6 @@ 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*](http://erlang.org/doc/man/app.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 lifecycle
|
||||
|
||||
### Loading applications
|
||||
@@ -181,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.
|
||||
|
||||
@@ -202,12 +155,14 @@ 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 if invoked. The PID of
|
||||
@@ -226,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
|
||||
@@ -243,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
|
||||
@@ -297,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`
|
||||
@@ -377,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,
|
||||
@@ -391,16 +354,6 @@ defmodule Application do
|
||||
: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`.
|
||||
|
||||
@@ -411,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)
|
||||
@@ -426,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
|
||||
@@ -457,135 +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 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) when is_atom(app) do
|
||||
if __CALLER__.function do
|
||||
raise "Application.compile_env/3 cannot be called inside functions, only in the module body"
|
||||
end
|
||||
|
||||
quote do
|
||||
Application.__compile_env__(unquote(app), unquote(key_or_path), unquote(default), __ENV__)
|
||||
end
|
||||
end
|
||||
|
||||
@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) when is_atom(app) do
|
||||
if __CALLER__.function do
|
||||
raise "Application.compile_env!/2 cannot be called inside functions, only in the module body"
|
||||
end
|
||||
|
||||
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.html).
|
||||
|
||||
## Examples
|
||||
|
||||
`get_env/3` is commonly used to read the configuration of your OTP applications.
|
||||
@@ -615,6 +445,11 @@ defmodule Application do
|
||||
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
|
||||
@@ -638,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
|
||||
@@ -650,23 +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/started. If your application " <>
|
||||
"depends on #{inspect(app)} at runtime, make sure to load/start it or " <>
|
||||
"list it under :extra_applications in your mix.exs file"
|
||||
: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
|
||||
|
||||
@@ -692,45 +523,10 @@ defmodule Application do
|
||||
:application.set_env(app, key, value, opts)
|
||||
end
|
||||
|
||||
# TODO: Remove this once we support Erlang/OTP 22+ exclusively.
|
||||
@compile {:no_warn_undefined, {:application, :set_env, 2}}
|
||||
|
||||
@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, the behaviour is undefined
|
||||
(on Erlang/OTP 21 and earlier) or will raise (on Erlang/OTP 22
|
||||
and later).
|
||||
|
||||
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
|
||||
# TODO: Remove function exported? check when we require Erlang/OTP 22+
|
||||
if function_exported?(:application, :set_env, 2) do
|
||||
:application.set_env(config, opts)
|
||||
else
|
||||
for app_keyword <- config,
|
||||
{app, keyword} = app_keyword,
|
||||
key_value <- keyword,
|
||||
{key, value} = key_value do
|
||||
:application.set_env(app, key, value, opts)
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
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
|
||||
@@ -752,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.
|
||||
|
||||
|
||||
+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
|
||||
|
||||
+66
-66
@@ -10,85 +10,85 @@ 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| | |
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ defmodule Behaviour 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
|
||||
|
||||
@@ -36,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
|
||||
|
||||
@@ -56,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
|
||||
|
||||
+29
-106
@@ -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
|
||||
@@ -63,246 +60,172 @@ defmodule Bitwise do
|
||||
@doc """
|
||||
Calculates the bitwise NOT of its argument.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
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 """
|
||||
Prefix (unary) operator; calculates the bitwise NOT of its argument.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
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 """
|
||||
Infix operator; calculates the bitwise AND of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
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 """
|
||||
Infix operator; calculates the bitwise OR of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
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 """
|
||||
Infix operator; calculates the bitwise XOR of its arguments.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> 9 ^^^ 3
|
||||
10
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
@spec integer ^^^ integer :: integer
|
||||
def left ^^^ right do
|
||||
:erlang.bxor(left, right)
|
||||
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 """
|
||||
Infix operator; calculates the result of an arithmetic left bitshift.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
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 """
|
||||
Infix operator; calculates the result of an arithmetic right bitshift.
|
||||
|
||||
Allowed in guard tests. Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
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
|
||||
|
||||
@@ -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,15 +52,15 @@ 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"
|
||||
@@ -127,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
|
||||
|
||||
@@ -180,7 +175,7 @@ defmodule Calendar do
|
||||
@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.
|
||||
@@ -270,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 """
|
||||
|
||||
@@ -263,9 +263,30 @@ 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
|
||||
|
||||
@@ -322,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
|
||||
@@ -724,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"
|
||||
@@ -748,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
|
||||
|
||||
+152
-173
@@ -25,17 +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, to use the
|
||||
[tzdata](https://hexdocs.pm/tzdata/) database, first make sure it is added as
|
||||
a dependency in `mix.exs`. It can then be configured either via
|
||||
configuration:
|
||||
Other time zone databases (including ones provided by packages)
|
||||
can be configure as default 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)
|
||||
config :elixir, :time_zone_database, CustomTimeZoneDatabase
|
||||
|
||||
or by calling `Calendar.put_time_zone_database/1`.
|
||||
"""
|
||||
|
||||
@enforce_keys [:year, :month, :day, :hour, :minute, :second] ++
|
||||
@@ -102,17 +97,17 @@ 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]
|
||||
#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]
|
||||
#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.
|
||||
@@ -157,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
|
||||
@@ -191,8 +183,9 @@ 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. First the one
|
||||
@@ -234,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
|
||||
@@ -363,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>
|
||||
@@ -417,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()) ::
|
||||
@@ -478,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.
|
||||
|
||||
@@ -520,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}
|
||||
|
||||
"""
|
||||
@@ -541,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.
|
||||
|
||||
@@ -582,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()
|
||||
@@ -630,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,
|
||||
@@ -669,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
|
||||
|
||||
@@ -699,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,
|
||||
@@ -774,14 +684,25 @@ defmodule DateTime do
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
time_zone: time_zone,
|
||||
zone_abbr: zone_abbr,
|
||||
utc_offset: utc_offset,
|
||||
std_offset: std_offset
|
||||
} = datetime
|
||||
|
||||
Calendar.ISO.date_to_string(year, month, day, format) <>
|
||||
"T" <>
|
||||
Calendar.ISO.time_to_string(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: _} = datetime, format) when format in [:extended, :basic] do
|
||||
@@ -811,23 +732,23 @@ defmodule DateTime do
|
||||
|
||||
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}
|
||||
@@ -844,26 +765,91 @@ 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
|
||||
|
||||
@@ -1035,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
|
||||
|
||||
@@ -1085,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.
|
||||
@@ -1301,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,
|
||||
@@ -1313,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,
|
||||
@@ -1330,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
|
||||
|
||||
+156
-494
@@ -20,48 +20,9 @@ defmodule Calendar.ISO do
|
||||
unix_end = 315_569_519_999_999_999 - @unix_epoch * 1_000_000
|
||||
@unix_range_microseconds unix_start..unix_end
|
||||
|
||||
@typedoc """
|
||||
"Before the Current Era" or "Before the Common Era" (BCE), for those years less than `1`.
|
||||
"""
|
||||
@type bce :: 0
|
||||
|
||||
@typedoc """
|
||||
The "Current Era" or the "Common Era" (CE) which starts in year `1`.
|
||||
"""
|
||||
@type ce :: 1
|
||||
|
||||
@typedoc """
|
||||
The calendar era.
|
||||
|
||||
The ISO calendar has two eras:
|
||||
* [CE](`t:ce/0`) - which starts in year `1` and is defined as era `1`.
|
||||
* [BCE](`t:bce/0`) - for those years less than `1` and is defined as era `0`.
|
||||
"""
|
||||
@type era :: bce | ce
|
||||
@type year :: -9999..9999
|
||||
@type month :: 1..12
|
||||
@type day :: 1..31
|
||||
@type hour :: 0..23
|
||||
@type minute :: 0..59
|
||||
@type second :: 0..59
|
||||
|
||||
@typedoc """
|
||||
Microseconds with stored precision.
|
||||
|
||||
The precision represents the number of digits that must be used when
|
||||
representing the microseconds to external format. If the precision is 0,
|
||||
it means microseconds must be skipped.
|
||||
"""
|
||||
@type microsecond :: {0..999_999, 0..6}
|
||||
|
||||
@typedoc """
|
||||
Integer that represents the day of the week, where 1 is Monday and 7 is Sunday.
|
||||
"""
|
||||
@type day_of_week :: 1..7
|
||||
|
||||
@type day_of_year :: 1..366
|
||||
@type quarter_of_year :: 1..4
|
||||
@type year_of_era :: {1..10000, era}
|
||||
|
||||
@seconds_per_minute 60
|
||||
@seconds_per_hour 60 * 60
|
||||
@@ -71,16 +32,18 @@ defmodule Calendar.ISO do
|
||||
@microseconds_per_second 1_000_000
|
||||
@parts_per_day @seconds_per_day * @microseconds_per_second
|
||||
|
||||
@sep [?\s, ?T]
|
||||
@days_per_nonleap_year 365
|
||||
@days_per_leap_year 366
|
||||
|
||||
@months_in_year 12
|
||||
|
||||
# The ISO epoch starts, in this implementation,
|
||||
# with ~D[0000-01-01]. Era "1" starts
|
||||
# on ~D[0001-01-01] which is 366 days later.
|
||||
@iso_epoch 366
|
||||
|
||||
[match_date, guard_date, read_date] =
|
||||
@doc false
|
||||
def __match_date__ do
|
||||
quote do
|
||||
[
|
||||
<<y1, y2, y3, y4, ?-, m1, m2, ?-, d1, d2>>,
|
||||
@@ -94,8 +57,10 @@ defmodule Calendar.ISO do
|
||||
}
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
[match_time, guard_time, read_time] =
|
||||
@doc false
|
||||
def __match_time__ do
|
||||
quote do
|
||||
[
|
||||
<<h1, h2, ?:, i1, i2, ?:, s1, s2>>,
|
||||
@@ -108,276 +73,6 @@ defmodule Calendar.ISO do
|
||||
}
|
||||
]
|
||||
end
|
||||
|
||||
defguardp is_year(year) when year in -9999..9999
|
||||
defguardp is_year_BCE(year) when year in -9999..0
|
||||
defguardp is_year_CE(year) when year in 1..9999
|
||||
defguardp is_month(month) when month in 1..12
|
||||
defguardp is_day(day) when day in 1..31
|
||||
defguardp is_hour(hour) when hour in 0..23
|
||||
defguardp is_minute(minute) when minute in 0..59
|
||||
defguardp is_second(second) when second in 0..59
|
||||
|
||||
defguardp is_microsecond(microsecond, precision)
|
||||
when microsecond in 0..999_999 and precision in 0..6
|
||||
|
||||
defguardp is_time_zone(term) when is_binary(term)
|
||||
defguardp is_zone_abbr(term) when is_binary(term)
|
||||
defguardp is_utc_offset(offset) when is_integer(offset)
|
||||
defguardp is_std_offset(offset) when is_integer(offset)
|
||||
|
||||
@doc """
|
||||
Parses a time string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.parse_time("23:50:07")
|
||||
{:ok, {23, 50, 7, {0, 0}}}
|
||||
iex> Calendar.ISO.parse_time("23:50:07Z")
|
||||
{:ok, {23, 50, 7, {0, 0}}}
|
||||
iex> Calendar.ISO.parse_time("T23:50:07Z")
|
||||
{:ok, {23, 50, 7, {0, 0}}}
|
||||
|
||||
iex> Calendar.ISO.parse_time("23:50:07,0123456")
|
||||
{:ok, {23, 50, 7, {12345, 6}}}
|
||||
iex> Calendar.ISO.parse_time("23:50:07.0123456")
|
||||
{:ok, {23, 50, 7, {12345, 6}}}
|
||||
iex> Calendar.ISO.parse_time("23:50:07.123Z")
|
||||
{:ok, {23, 50, 7, {123000, 3}}}
|
||||
|
||||
iex> Calendar.ISO.parse_time("2015:01:23 23-50-07")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_time("23:50:07A")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_time("23:50:07.")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_time("23:50:61")
|
||||
{:error, :invalid_time}
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@impl true
|
||||
def parse_time("T" <> string) when is_binary(string),
|
||||
do: do_parse_time(string)
|
||||
|
||||
def parse_time(string) when is_binary(string),
|
||||
do: do_parse_time(string)
|
||||
|
||||
defp do_parse_time(string) do
|
||||
with <<unquote(match_time), rest::binary>> <- string,
|
||||
true <- unquote(guard_time),
|
||||
{microsecond, rest} <- parse_microsecond(rest),
|
||||
{_offset, ""} <- parse_offset(rest) do
|
||||
{hour, minute, second} = unquote(read_time)
|
||||
|
||||
if valid_time?(hour, minute, second, microsecond) do
|
||||
{:ok, {hour, minute, second, microsecond}}
|
||||
else
|
||||
{:error, :invalid_time}
|
||||
end
|
||||
else
|
||||
_ -> {:error, :invalid_format}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a date string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.parse_date("2015-01-23")
|
||||
{:ok, {2015, 1, 23}}
|
||||
iex> Calendar.ISO.parse_date("-2015-01-23")
|
||||
{:ok, {-2015, 1, 23}}
|
||||
iex> Calendar.ISO.parse_date("2015:01:23")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_date("2015-01-32")
|
||||
{:error, :invalid_date}
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@impl true
|
||||
def parse_date("-" <> string) when is_binary(string),
|
||||
do: parse_date(string, -1)
|
||||
|
||||
def parse_date(string) when is_binary(string),
|
||||
do: parse_date(string, 1)
|
||||
|
||||
defp parse_date(string, multiplier) do
|
||||
with unquote(match_date) <- string, true <- unquote(guard_date) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
year = multiplier * year
|
||||
|
||||
if valid_date?(year, month, day) do
|
||||
{:ok, {year, month, day}}
|
||||
else
|
||||
{:error, :invalid_date}
|
||||
end
|
||||
else
|
||||
_ ->
|
||||
{:error, :invalid_format}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a naive datetime string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07Z")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
|
||||
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07.0")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {0, 1}}}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07,0123456")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07.0123456")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123Z")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
|
||||
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23P23:50:07")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015:01:23 23-50-07")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07A")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:61")
|
||||
{:error, :invalid_time}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-32 23:50:07")
|
||||
{:error, :invalid_date}
|
||||
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123+02:30")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123+00:00")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-02:30")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-00:00")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-00:60")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-24:00")
|
||||
{:error, :invalid_format}
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@impl true
|
||||
def parse_naive_datetime("-" <> string) when is_binary(string),
|
||||
do: parse_naive_datetime(string, -1)
|
||||
|
||||
def parse_naive_datetime(string) when is_binary(string),
|
||||
do: parse_naive_datetime(string, 1)
|
||||
|
||||
defp parse_naive_datetime(string, multiplier) 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} <- parse_microsecond(rest),
|
||||
{_offset, ""} <- parse_offset(rest) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
{hour, minute, second} = unquote(read_time)
|
||||
year = multiplier * year
|
||||
|
||||
cond do
|
||||
not valid_date?(year, month, day) ->
|
||||
{:error, :invalid_date}
|
||||
|
||||
not valid_time?(hour, minute, second, microsecond) ->
|
||||
{:error, :invalid_time}
|
||||
|
||||
true ->
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}}
|
||||
end
|
||||
else
|
||||
_ -> {:error, :invalid_format}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a UTC datetime string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07Z")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
|
||||
|
||||
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.123+02:30")
|
||||
{:ok, {2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
|
||||
|
||||
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07,123+02:30")
|
||||
{:ok, {2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
|
||||
|
||||
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23T23:50:07Z")
|
||||
{:ok, {-2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
|
||||
|
||||
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23T23:50:07,123+02:30")
|
||||
{:ok, {-2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
|
||||
|
||||
iex> Calendar.ISO.parse_utc_datetime("2015-01-23P23:50:07")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07")
|
||||
{:error, :missing_offset}
|
||||
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:61")
|
||||
{:error, :invalid_time}
|
||||
iex> Calendar.ISO.parse_utc_datetime("2015-01-32 23:50:07")
|
||||
{:error, :invalid_date}
|
||||
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.123-00:00")
|
||||
{:error, :invalid_format}
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@impl true
|
||||
def parse_utc_datetime("-" <> string) when is_binary(string),
|
||||
do: parse_utc_datetime(string, -1)
|
||||
|
||||
def parse_utc_datetime(string) when is_binary(string),
|
||||
do: parse_utc_datetime(string, 1)
|
||||
|
||||
defp parse_utc_datetime(string, multiplier) 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} <- parse_microsecond(rest),
|
||||
{offset, ""} <- parse_offset(rest) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
{hour, minute, second} = unquote(read_time)
|
||||
year = multiplier * year
|
||||
|
||||
cond do
|
||||
not valid_date?(year, month, day) ->
|
||||
{:error, :invalid_date}
|
||||
|
||||
not valid_time?(hour, minute, second, microsecond) ->
|
||||
{:error, :invalid_time}
|
||||
|
||||
offset == 0 ->
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}, offset}
|
||||
|
||||
is_nil(offset) ->
|
||||
{:error, :missing_offset}
|
||||
|
||||
true ->
|
||||
day_fraction = time_to_day_fraction(hour, minute, second, {0, 0})
|
||||
|
||||
{{year, month, day}, {hour, minute, second, _}} =
|
||||
case add_day_fraction_to_iso_days({0, day_fraction}, -offset, 86400) do
|
||||
{0, day_fraction} ->
|
||||
{{year, month, day}, time_from_day_fraction(day_fraction)}
|
||||
|
||||
{extra_days, day_fraction} ->
|
||||
base_days = date_to_iso_days(year, month, day)
|
||||
{date_from_iso_days(base_days + extra_days), time_from_day_fraction(day_fraction)}
|
||||
end
|
||||
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}, offset}
|
||||
end
|
||||
else
|
||||
_ ->
|
||||
{:error, :invalid_format}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -484,7 +179,7 @@ defmodule Calendar.ISO do
|
||||
@doc since: "1.5.0"
|
||||
@impl true
|
||||
@spec time_from_day_fraction(Calendar.day_fraction()) ::
|
||||
{hour(), minute(), second(), microsecond()}
|
||||
{Calendar.hour(), Calendar.minute(), Calendar.second(), Calendar.microsecond()}
|
||||
def time_from_day_fraction({0, _}) do
|
||||
{0, 0, 0, {0, 6}}
|
||||
end
|
||||
@@ -517,8 +212,8 @@ defmodule Calendar.ISO do
|
||||
719_528
|
||||
end
|
||||
|
||||
def date_to_iso_days(year, month, day) do
|
||||
ensure_day_in_month!(year, month, day)
|
||||
def date_to_iso_days(year, month, day) when year in -9999..9999 do
|
||||
true = day <= days_in_month(year, month)
|
||||
|
||||
days_in_previous_years(year) + days_before_month(month) + leap_day_offset(year, month) + day -
|
||||
1
|
||||
@@ -565,19 +260,16 @@ defmodule Calendar.ISO do
|
||||
31
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec days_in_month(year, month) :: 28..31
|
||||
@impl true
|
||||
def days_in_month(year, month) when is_year(year) and is_month(month) do
|
||||
days_in_month_guarded(year, month)
|
||||
end
|
||||
def days_in_month(year, month)
|
||||
|
||||
defp days_in_month_guarded(year, 2) do
|
||||
def days_in_month(year, 2) do
|
||||
if leap_year?(year), do: 29, else: 28
|
||||
end
|
||||
|
||||
defp days_in_month_guarded(_, month) when month in [4, 6, 9, 11], do: 30
|
||||
defp days_in_month_guarded(_, _), do: 31
|
||||
def days_in_month(_, month) when month in [4, 6, 9, 11], do: 30
|
||||
def days_in_month(_, month) when month in 1..12, do: 31
|
||||
|
||||
@doc """
|
||||
Returns how many months there are in the given year.
|
||||
@@ -591,8 +283,8 @@ defmodule Calendar.ISO do
|
||||
@doc since: "1.7.0"
|
||||
@impl true
|
||||
@spec months_in_year(year) :: 12
|
||||
def months_in_year(year) when is_year(year) do
|
||||
12
|
||||
def months_in_year(_year) do
|
||||
@months_in_year
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -612,10 +304,9 @@ defmodule Calendar.ISO do
|
||||
true
|
||||
|
||||
"""
|
||||
@doc since: "1.3.0"
|
||||
@spec leap_year?(year) :: boolean()
|
||||
@impl true
|
||||
def leap_year?(year) when is_year(year) do
|
||||
def leap_year?(year) when is_integer(year) do
|
||||
rem(year, 4) === 0 and (rem(year, 100) !== 0 or rem(year, 400) === 0)
|
||||
end
|
||||
|
||||
@@ -644,12 +335,11 @@ defmodule Calendar.ISO do
|
||||
4
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec day_of_week(year, month, day) :: day_of_week()
|
||||
@spec day_of_week(year, month, day) :: 1..7
|
||||
@impl true
|
||||
def day_of_week(year, month, day) do
|
||||
date_to_iso_days(year, month, day)
|
||||
|> iso_days_to_day_of_week()
|
||||
def day_of_week(year, month, day)
|
||||
when is_integer(year) and is_integer(month) and is_integer(day) do
|
||||
iso_days_to_day_of_week(date_to_iso_days(year, month, day))
|
||||
end
|
||||
|
||||
defp iso_days_to_day_of_week(iso_days) do
|
||||
@@ -672,10 +362,11 @@ defmodule Calendar.ISO do
|
||||
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec day_of_year(year, month, day) :: day_of_year()
|
||||
@spec day_of_year(year, month, day) :: 1..366
|
||||
@impl true
|
||||
def day_of_year(year, month, day) do
|
||||
ensure_day_in_month!(year, month, day)
|
||||
def day_of_year(year, month, day)
|
||||
when is_integer(year) and is_integer(month) and is_integer(day) do
|
||||
true = day <= days_in_month(year, month)
|
||||
days_before_month(month) + leap_day_offset(year, month) + day
|
||||
end
|
||||
|
||||
@@ -697,19 +388,20 @@ defmodule Calendar.ISO do
|
||||
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec quarter_of_year(year, month, day) :: quarter_of_year()
|
||||
@spec quarter_of_year(year, month, day) :: 1..4
|
||||
@impl true
|
||||
def quarter_of_year(year, month, day)
|
||||
when is_year(year) and is_month(month) and is_day(day) do
|
||||
when is_integer(year) and is_integer(month) and is_integer(day) do
|
||||
div(month - 1, 3) + 1
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the year and era from the given `year`.
|
||||
|
||||
The ISO calendar has two eras: the "current era" (CE) which
|
||||
starts in year `1` and is defined as era `1`. And "before the current
|
||||
era" (BCE) for those years less than `1`, defined as era `0`.
|
||||
The ISO calendar has two eras: the current era which
|
||||
starts in year 1 and is defined as era "1". And a
|
||||
second era for those years less than 1 defined as
|
||||
era "0".
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -724,13 +416,13 @@ defmodule Calendar.ISO do
|
||||
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec year_of_era(year) :: {1..10000, era}
|
||||
@spec year_of_era(year) :: {year, era :: 0..1}
|
||||
@impl true
|
||||
def year_of_era(year) when is_year_CE(year) do
|
||||
def year_of_era(year) when is_integer(year) and year > 0 do
|
||||
{year, 1}
|
||||
end
|
||||
|
||||
def year_of_era(year) when is_year_BCE(year) do
|
||||
def year_of_era(year) when is_integer(year) and year < 1 do
|
||||
{abs(year) + 1, 0}
|
||||
end
|
||||
|
||||
@@ -752,14 +444,16 @@ defmodule Calendar.ISO do
|
||||
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec day_of_era(year, month, day) :: Calendar.day_of_era()
|
||||
@spec day_of_era(year, month, day) :: {day :: pos_integer(), era :: 0..1}
|
||||
@impl true
|
||||
def day_of_era(year, month, day) when is_year_CE(year) do
|
||||
def day_of_era(year, month, day)
|
||||
when is_integer(year) and is_integer(month) and is_integer(day) and year > 0 do
|
||||
day = date_to_iso_days(year, month, day) - @iso_epoch + 1
|
||||
{day, 1}
|
||||
end
|
||||
|
||||
def day_of_era(year, month, day) when is_year_BCE(year) do
|
||||
def day_of_era(year, month, day)
|
||||
when is_integer(year) and is_integer(month) and is_integer(day) and year < 1 do
|
||||
day = abs(date_to_iso_days(year, month, day) - @iso_epoch)
|
||||
{day, 0}
|
||||
end
|
||||
@@ -767,10 +461,6 @@ defmodule Calendar.ISO do
|
||||
@doc """
|
||||
Converts the given time into a string.
|
||||
|
||||
By default, returns times formatted in the "extended" format,
|
||||
for human readability. It also supports the "basic" format
|
||||
by passing the `:basic` option.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.time_to_string(2, 2, 2, {2, 6})
|
||||
@@ -780,38 +470,23 @@ defmodule Calendar.ISO do
|
||||
iex> Calendar.ISO.time_to_string(2, 2, 2, {2, 0})
|
||||
"02:02:02"
|
||||
|
||||
iex> Calendar.ISO.time_to_string(2, 2, 2, {2, 6}, :basic)
|
||||
"020202.000002"
|
||||
iex> Calendar.ISO.time_to_string(2, 2, 2, {2, 6}, :extended)
|
||||
"02:02:02.000002"
|
||||
|
||||
"""
|
||||
@impl true
|
||||
@doc since: "1.5.0"
|
||||
@spec time_to_string(
|
||||
Calendar.hour(),
|
||||
Calendar.minute(),
|
||||
Calendar.second(),
|
||||
Calendar.microsecond(),
|
||||
:basic | :extended
|
||||
Calendar.microsecond()
|
||||
) :: String.t()
|
||||
def time_to_string(
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
{ms_value, ms_precision} = microsecond,
|
||||
format \\ :extended
|
||||
)
|
||||
when is_hour(hour) and is_minute(minute) and is_second(second) and
|
||||
is_microsecond(ms_value, ms_precision) and format in [:basic, :extended] do
|
||||
time_to_string_guarded(hour, minute, second, microsecond, format)
|
||||
@impl true
|
||||
def time_to_string(hour, minute, second, microsecond) do
|
||||
time_to_string(hour, minute, second, microsecond, :extended)
|
||||
end
|
||||
|
||||
defp time_to_string_guarded(hour, minute, second, {_, 0}, format) do
|
||||
def time_to_string(hour, minute, second, {_, 0}, format) do
|
||||
time_to_string_format(hour, minute, second, format)
|
||||
end
|
||||
|
||||
defp time_to_string_guarded(hour, minute, second, {microsecond, precision}, format) do
|
||||
def time_to_string(hour, minute, second, {microsecond, precision}, format) do
|
||||
time_to_string_format(hour, minute, second, format) <>
|
||||
"." <> (microsecond |> zero_pad(6) |> binary_part(0, precision))
|
||||
end
|
||||
@@ -827,10 +502,6 @@ defmodule Calendar.ISO do
|
||||
@doc """
|
||||
Converts the given date into a string.
|
||||
|
||||
By default, returns dates formatted in the "extended" format,
|
||||
for human readability. It also supports the "basic" format
|
||||
by passing the `:basic` option.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.date_to_string(2015, 2, 28)
|
||||
@@ -840,36 +511,24 @@ defmodule Calendar.ISO do
|
||||
iex> Calendar.ISO.date_to_string(-99, 1, 31)
|
||||
"-0099-01-31"
|
||||
|
||||
iex> Calendar.ISO.date_to_string(2015, 2, 28, :basic)
|
||||
"20150228"
|
||||
iex> Calendar.ISO.date_to_string(-99, 1, 31, :basic)
|
||||
"-00990131"
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec date_to_string(year, month, day, :basic | :extended) :: String.t()
|
||||
@spec date_to_string(year, month, day) :: String.t()
|
||||
@impl true
|
||||
def date_to_string(year, month, day, format \\ :extended)
|
||||
when is_integer(year) and is_integer(month) and is_integer(day) and
|
||||
format in [:basic, :extended] do
|
||||
date_to_string_guarded(year, month, day, format)
|
||||
def date_to_string(year, month, day) do
|
||||
date_to_string(year, month, day, :extended)
|
||||
end
|
||||
|
||||
defp date_to_string_guarded(year, month, day, :extended) do
|
||||
defp date_to_string(year, month, day, :extended) do
|
||||
zero_pad(year, 4) <> "-" <> zero_pad(month, 2) <> "-" <> zero_pad(day, 2)
|
||||
end
|
||||
|
||||
defp date_to_string_guarded(year, month, day, :basic) do
|
||||
defp date_to_string(year, month, day, :basic) do
|
||||
zero_pad(year, 4) <> zero_pad(month, 2) <> zero_pad(day, 2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the datetime (without time zone) into a string.
|
||||
|
||||
By default, returns datetimes formatted in the "extended" format,
|
||||
for human readability. It also supports the "basic" format
|
||||
by passing the `:basic` option.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.naive_datetime_to_string(2015, 2, 28, 1, 2, 3, {4, 6})
|
||||
@@ -877,11 +536,7 @@ defmodule Calendar.ISO do
|
||||
iex> Calendar.ISO.naive_datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5})
|
||||
"2017-08-01 01:02:03.00000"
|
||||
|
||||
iex> Calendar.ISO.naive_datetime_to_string(2015, 2, 28, 1, 2, 3, {4, 6}, :basic)
|
||||
"20150228 010203.000004"
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@impl true
|
||||
@spec naive_datetime_to_string(
|
||||
year,
|
||||
@@ -890,40 +545,17 @@ defmodule Calendar.ISO do
|
||||
Calendar.hour(),
|
||||
Calendar.minute(),
|
||||
Calendar.second(),
|
||||
Calendar.microsecond(),
|
||||
:basic | :extended
|
||||
Calendar.microsecond()
|
||||
) :: String.t()
|
||||
def naive_datetime_to_string(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
microsecond,
|
||||
format \\ :extended
|
||||
) do
|
||||
date_to_string(year, month, day, format) <>
|
||||
" " <> time_to_string(hour, minute, second, microsecond, format)
|
||||
def naive_datetime_to_string(year, month, day, hour, minute, second, microsecond) do
|
||||
date_to_string(year, month, day) <> " " <> time_to_string(hour, minute, second, microsecond)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the datetime (with time zone) into a string.
|
||||
|
||||
By default, returns datetimes formatted in the "extended" format,
|
||||
for human readability. It also supports the "basic" format
|
||||
by passing the `:basic` option.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> time_zone = "Etc/UTC"
|
||||
iex> Calendar.ISO.datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5}, time_zone, "UTC", 0, 0)
|
||||
"2017-08-01 01:02:03.00000Z"
|
||||
iex> Calendar.ISO.datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5}, time_zone, "UTC", 3600, 0)
|
||||
"2017-08-01 01:02:03.00000+01:00"
|
||||
iex> Calendar.ISO.datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5}, time_zone, "UTC", 3600, 3600)
|
||||
"2017-08-01 01:02:03.00000+02:00"
|
||||
|
||||
iex> time_zone = "Europe/Berlin"
|
||||
iex> Calendar.ISO.datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5}, time_zone, "CET", 3600, 0)
|
||||
"2017-08-01 01:02:03.00000+01:00 CET Europe/Berlin"
|
||||
@@ -936,12 +568,7 @@ defmodule Calendar.ISO do
|
||||
iex> Calendar.ISO.datetime_to_string(2015, 2, 28, 1, 2, 3, {4, 5}, time_zone, "PDT", -28800, 3600)
|
||||
"2015-02-28 01:02:03.00000-07:00 PDT America/Los_Angeles"
|
||||
|
||||
iex> time_zone = "Europe/Berlin"
|
||||
iex> Calendar.ISO.datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5}, time_zone, "CET", 3600, 0, :basic)
|
||||
"20170801 010203.00000+0100 CET Europe/Berlin"
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@impl true
|
||||
@spec datetime_to_string(
|
||||
year,
|
||||
@@ -954,8 +581,7 @@ defmodule Calendar.ISO do
|
||||
Calendar.time_zone(),
|
||||
Calendar.zone_abbr(),
|
||||
Calendar.utc_offset(),
|
||||
Calendar.std_offset(),
|
||||
:basic | :extended
|
||||
Calendar.std_offset()
|
||||
) :: String.t()
|
||||
def datetime_to_string(
|
||||
year,
|
||||
@@ -968,40 +594,15 @@ defmodule Calendar.ISO do
|
||||
time_zone,
|
||||
zone_abbr,
|
||||
utc_offset,
|
||||
std_offset,
|
||||
format \\ :extended
|
||||
)
|
||||
when is_time_zone(time_zone) and is_zone_abbr(zone_abbr) and is_utc_offset(utc_offset) and
|
||||
is_std_offset(std_offset) do
|
||||
date_to_string(year, month, day, format) <>
|
||||
std_offset
|
||||
) do
|
||||
date_to_string(year, month, day) <>
|
||||
" " <>
|
||||
time_to_string(hour, minute, second, microsecond, format) <>
|
||||
offset_to_string(utc_offset, std_offset, time_zone, format) <>
|
||||
time_to_string(hour, minute, second, microsecond) <>
|
||||
offset_to_string(utc_offset, std_offset, time_zone) <>
|
||||
zone_to_string(utc_offset, std_offset, zone_abbr, time_zone)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def offset_to_string(0, 0, "Etc/UTC", _format), do: "Z"
|
||||
|
||||
def offset_to_string(utc, std, _zone, format) do
|
||||
total = utc + std
|
||||
second = abs(total)
|
||||
minute = second |> rem(3600) |> div(60)
|
||||
hour = div(second, 3600)
|
||||
format_offset(total, hour, minute, format)
|
||||
end
|
||||
|
||||
defp format_offset(total, hour, minute, :extended) do
|
||||
sign(total) <> zero_pad(hour, 2) <> ":" <> zero_pad(minute, 2)
|
||||
end
|
||||
|
||||
defp format_offset(total, hour, minute, :basic) do
|
||||
sign(total) <> zero_pad(hour, 2) <> zero_pad(minute, 2)
|
||||
end
|
||||
|
||||
defp zone_to_string(_, _, _, "Etc/UTC"), do: ""
|
||||
defp zone_to_string(_, _, abbr, zone), do: " " <> abbr <> " " <> zone
|
||||
|
||||
@doc """
|
||||
Determines if the date given is valid according to the proleptic Gregorian calendar.
|
||||
|
||||
@@ -1020,9 +621,9 @@ defmodule Calendar.ISO do
|
||||
@doc since: "1.5.0"
|
||||
@impl true
|
||||
@spec valid_date?(year, month, day) :: boolean
|
||||
def valid_date?(year, month, day)
|
||||
when is_integer(year) and is_integer(month) and is_integer(day) do
|
||||
is_year(year) and is_month(month) and day in 1..days_in_month(year, month)
|
||||
def valid_date?(year, month, day) do
|
||||
month in 1..12 and year in -9999..9999 and
|
||||
(is_integer(day) and day >= 1 and day <= days_in_month(year, month))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1046,11 +647,9 @@ defmodule Calendar.ISO do
|
||||
@impl true
|
||||
@spec valid_time?(Calendar.hour(), Calendar.minute(), Calendar.second(), Calendar.microsecond()) ::
|
||||
boolean
|
||||
def valid_time?(hour, minute, second, {ms_value, ms_precision} = _microsecond)
|
||||
when is_integer(hour) and is_integer(minute) and is_integer(second) and is_integer(ms_value) and
|
||||
is_integer(ms_value) do
|
||||
is_hour(hour) and is_minute(minute) and is_second(second) and
|
||||
is_microsecond(ms_value, ms_precision)
|
||||
def valid_time?(hour, minute, second, {microsecond, precision}) do
|
||||
hour in 0..23 and minute in 0..59 and second in 0..59 and microsecond in 0..999_999 and
|
||||
precision in 0..6
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1063,6 +662,28 @@ defmodule Calendar.ISO do
|
||||
{0, 1}
|
||||
end
|
||||
|
||||
defp offset_to_string(utc, std, zone, format \\ :extended)
|
||||
defp offset_to_string(0, 0, "Etc/UTC", _format), do: "Z"
|
||||
|
||||
defp offset_to_string(utc, std, _zone, format) do
|
||||
total = utc + std
|
||||
second = abs(total)
|
||||
minute = second |> rem(3600) |> div(60)
|
||||
hour = div(second, 3600)
|
||||
format_offset(total, hour, minute, format)
|
||||
end
|
||||
|
||||
defp format_offset(total, hour, minute, :extended) do
|
||||
sign(total) <> zero_pad(hour, 2) <> ":" <> zero_pad(minute, 2)
|
||||
end
|
||||
|
||||
defp format_offset(total, hour, minute, :basic) do
|
||||
sign(total) <> zero_pad(hour, 2) <> zero_pad(minute, 2)
|
||||
end
|
||||
|
||||
defp zone_to_string(0, 0, _abbr, "Etc/UTC"), do: ""
|
||||
defp zone_to_string(_, _, abbr, zone), do: " " <> abbr <> " " <> zone
|
||||
|
||||
defp sign(total) when total < 0, do: "-"
|
||||
defp sign(_), do: "+"
|
||||
|
||||
@@ -1093,18 +714,64 @@ defmodule Calendar.ISO do
|
||||
end
|
||||
|
||||
defp precision_for_unit(unit) do
|
||||
case System.convert_time_unit(1, :second, unit) do
|
||||
1 -> 0
|
||||
10 -> 1
|
||||
100 -> 2
|
||||
1_000 -> 3
|
||||
10_000 -> 4
|
||||
100_000 -> 5
|
||||
_ -> 6
|
||||
end
|
||||
subsecond = div(System.convert_time_unit(1, :second, unit), 10)
|
||||
precision_for_unit(subsecond, 0)
|
||||
end
|
||||
|
||||
defp parse_microsecond("." <> rest) do
|
||||
defp precision_for_unit(0, precision), do: precision
|
||||
defp precision_for_unit(_, 6), do: 6
|
||||
|
||||
defp precision_for_unit(number, precision),
|
||||
do: precision_for_unit(div(number, 10), precision + 1)
|
||||
|
||||
@doc false
|
||||
def date_to_iso8601(year, month, day, format \\ :extended) do
|
||||
date_to_string(year, month, day, format)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def time_to_iso8601(hour, minute, second, microsecond, format \\ :extended) do
|
||||
time_to_string(hour, minute, second, microsecond, format)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def naive_datetime_to_iso8601(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
microsecond,
|
||||
format \\ :extended
|
||||
) do
|
||||
date_to_string(year, month, day, format) <>
|
||||
"T" <> time_to_string(hour, minute, second, microsecond, format)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def datetime_to_iso8601(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
microsecond,
|
||||
time_zone,
|
||||
_zone_abbr,
|
||||
utc_offset,
|
||||
std_offset,
|
||||
format \\ :extended
|
||||
) do
|
||||
date_to_string(year, month, day, format) <>
|
||||
"T" <>
|
||||
time_to_string(hour, minute, second, microsecond, format) <>
|
||||
offset_to_string(utc_offset, std_offset, time_zone, format)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def parse_microsecond("." <> rest) do
|
||||
case parse_microsecond(rest, 0, "") do
|
||||
{"", 0, _} ->
|
||||
:error
|
||||
@@ -1118,11 +785,11 @@ defmodule Calendar.ISO do
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_microsecond("," <> rest) do
|
||||
def parse_microsecond("," <> rest) do
|
||||
parse_microsecond("." <> rest)
|
||||
end
|
||||
|
||||
defp parse_microsecond(rest) do
|
||||
def parse_microsecond(rest) do
|
||||
{{0, 0}, rest}
|
||||
end
|
||||
|
||||
@@ -1131,25 +798,26 @@ defmodule Calendar.ISO do
|
||||
|
||||
defp parse_microsecond(rest, precision, acc), do: {acc, precision, rest}
|
||||
|
||||
defp parse_offset(""), do: {nil, ""}
|
||||
defp parse_offset("Z"), do: {0, ""}
|
||||
defp parse_offset("-00:00"), do: :error
|
||||
@doc false
|
||||
def parse_offset(""), do: {nil, ""}
|
||||
def parse_offset("Z"), do: {0, ""}
|
||||
def parse_offset("-00:00"), do: :error
|
||||
|
||||
defp parse_offset(<<?+, hour::2-bytes, ?:, min::2-bytes, rest::binary>>),
|
||||
def parse_offset(<<?+, hour::2-bytes, ?:, min::2-bytes, rest::binary>>),
|
||||
do: parse_offset(1, hour, min, rest)
|
||||
|
||||
defp parse_offset(<<?-, hour::2-bytes, ?:, min::2-bytes, rest::binary>>),
|
||||
def parse_offset(<<?-, hour::2-bytes, ?:, min::2-bytes, rest::binary>>),
|
||||
do: parse_offset(-1, hour, min, rest)
|
||||
|
||||
defp parse_offset(<<?+, hour::2-bytes, min::2-bytes, rest::binary>>),
|
||||
def parse_offset(<<?+, hour::2-bytes, min::2-bytes, rest::binary>>),
|
||||
do: parse_offset(1, hour, min, rest)
|
||||
|
||||
defp parse_offset(<<?-, hour::2-bytes, min::2-bytes, rest::binary>>),
|
||||
def parse_offset(<<?-, hour::2-bytes, min::2-bytes, rest::binary>>),
|
||||
do: parse_offset(-1, hour, min, rest)
|
||||
|
||||
defp parse_offset(<<?+, hour::2-bytes, rest::binary>>), do: parse_offset(1, hour, "00", rest)
|
||||
defp parse_offset(<<?-, hour::2-bytes, rest::binary>>), do: parse_offset(-1, hour, "00", rest)
|
||||
defp parse_offset(_), do: :error
|
||||
def parse_offset(<<?+, hour::2-bytes, rest::binary>>), do: parse_offset(1, hour, "00", rest)
|
||||
def parse_offset(<<?-, hour::2-bytes, rest::binary>>), do: parse_offset(-1, hour, "00", rest)
|
||||
def parse_offset(_), do: :error
|
||||
|
||||
defp parse_offset(sign, hour, min, rest) do
|
||||
with {hour, ""} when hour < 24 <- Integer.parse(hour),
|
||||
@@ -1325,10 +993,4 @@ defmodule Calendar.ISO do
|
||||
|
||||
{hour, minute, second}
|
||||
end
|
||||
|
||||
defp ensure_day_in_month!(year, month, day) when is_integer(day) do
|
||||
if day < 1 or day > days_in_month(year, month) do
|
||||
raise ArgumentError, "invalid date: #{date_to_string(year, month, day)}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -117,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.
|
||||
|
||||
@@ -208,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)
|
||||
@@ -305,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",
|
||||
@@ -439,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,
|
||||
@@ -465,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: _,
|
||||
@@ -581,21 +556,35 @@ 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
|
||||
|
||||
@@ -673,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
|
||||
@@ -697,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},
|
||||
@@ -961,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,
|
||||
@@ -969,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
|
||||
|
||||
@@ -110,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)
|
||||
@@ -213,12 +213,30 @@ 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
|
||||
|
||||
@@ -283,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
|
||||
@@ -675,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
|
||||
|
||||
+117
-373
@@ -1,5 +1,5 @@
|
||||
defmodule Code do
|
||||
@moduledoc ~S"""
|
||||
@moduledoc """
|
||||
Utilities for managing code compilation, code evaluation, and code loading.
|
||||
|
||||
This module complements Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html)
|
||||
@@ -19,8 +19,7 @@ defmodule Code do
|
||||
|
||||
* `eval_file/2` - evaluates the file contents without tracking its name. It
|
||||
returns the result of the last expression in the file, instead of the modules
|
||||
defined in it. Evaluated files do not trigger the compilation tracers described
|
||||
in the next section.
|
||||
defined in it.
|
||||
|
||||
In a nutshell, the first must be used when you want to keep track of the files
|
||||
handled by the system, to avoid the same file from being compiled multiple
|
||||
@@ -29,111 +28,8 @@ defmodule Code do
|
||||
`compile_file/2` must be used when you are interested in the modules defined in a
|
||||
file, without tracking. `eval_file/2` should be used when you are interested in
|
||||
the result of evaluating the file rather than the modules it defines.
|
||||
|
||||
## Compilation tracers
|
||||
|
||||
Elixir supports compilation tracers, which allows modules to observe constructs
|
||||
handled by the Elixir compiler when compiling files. A tracer is a module
|
||||
that implements the `trace/2` function. The function receives the event name
|
||||
as first argument and `Macro.Env` as second and it must return `:ok`. It is
|
||||
very important for a tracer to do as little work as possible synchronously
|
||||
and dispatch the bulk of the work to a separate process. **Slow tracers will
|
||||
slow down compilation**.
|
||||
|
||||
You can configure your list of tracers via `put_compiler_option/2`. The
|
||||
following events are available to tracers:
|
||||
|
||||
* `:start` - (since v1.11.0) invoked whenever the compiler starts to trace
|
||||
a new lexical context, such as a new file. Keep in mind the compiler runs
|
||||
in parallel, so multiple files may invoke `:start` and run at the same
|
||||
time. The value of the `lexical_tracker` of the macro environment, albeit
|
||||
opaque, can be used to uniquely identify the environment.
|
||||
|
||||
* `:stop` - (since v1.11.0) invoked whenever the compiler stops tracing a
|
||||
new lexical context, such as a new file.
|
||||
|
||||
* `{:import, meta, module, opts}` - traced whenever `module` is imported.
|
||||
`meta` is the import AST metadata and `opts` are the import options.
|
||||
|
||||
* `{:imported_function, meta, module, name, arity}` and
|
||||
`{:imported_macro, meta, module, name, arity}` - traced whenever an
|
||||
imported function or macro is invoked. `meta` is the call AST metadata,
|
||||
`module` is the module the import is from, followed by the `name` and `arity`
|
||||
of the imported function/macro.
|
||||
|
||||
* `{:alias, meta, alias, as, opts}` - traced whenever `alias` is aliased
|
||||
to `as`. `meta` is the alias AST metadata and `opts` are the alias options.
|
||||
|
||||
* `{:alias_expansion, meta, as, alias}` traced whenever there is an alias
|
||||
expansion for a previously defined `alias`, i.e. when the user writes `as`
|
||||
which is expanded to `alias`. `meta` is the alias expansion AST metadata.
|
||||
|
||||
* `{:alias_reference, meta, module}` - traced whenever there is an alias
|
||||
in the code, i.e. whenever the user writes `MyModule.Foo.Bar` in the code,
|
||||
regardless if it was expanded or not.
|
||||
|
||||
* `{:require, meta, module, opts}` - traced whenever `module` is required.
|
||||
`meta` is the require AST metadata and `opts` are the require options.
|
||||
|
||||
* `{:struct_expansion, meta, module, keys}` - traced whenever `module`'s struct
|
||||
is expanded. `meta` is the struct AST metadata and `keys` are the keys being
|
||||
used by expansion
|
||||
|
||||
* `{:remote_function, meta, module, name, arity}` and
|
||||
`{:remote_macro, meta, module, name, arity}` - traced whenever a remote
|
||||
function or macro is referenced. `meta` is the call AST metadata, `module`
|
||||
is the invoked module, followed by the `name` and `arity`.
|
||||
|
||||
* `{:local_function, meta, name, arity}` and
|
||||
`{:local_macro, meta, name, arity}` - traced whenever a local
|
||||
function or macro is referenced. `meta` is the call AST metadata, `module`
|
||||
is the invoked module, followed by the `name` and `arity`.
|
||||
|
||||
* `{:compile_env, app, path, return}` - traced whenever `Application.compile_env/3`
|
||||
or `Application.compile_env!/2` are called. `app` is an atom, `path` is a list
|
||||
of keys to traverse in the application environemnt and `return` is either
|
||||
`{:ok, value}` or `:error`.
|
||||
|
||||
The `:tracers` compiler option can be combined with the `:parser_options`
|
||||
compiler option to enrich the metadata of the traced events above.
|
||||
|
||||
New events may be added at any time in the future, therefore it is advised
|
||||
for the `trace/2` function to have a "catch-all" clause.
|
||||
|
||||
Below is an example tracer that prints all remote function invocations:
|
||||
|
||||
defmodule MyTracer do
|
||||
def trace({:remote_function, _meta, module, name, arity}, env) do
|
||||
IO.puts "#{env.file}:#{env.line} #{inspect(module)}.#{name}/#{arity}"
|
||||
:ok
|
||||
end
|
||||
|
||||
def trace(_event, _env) do
|
||||
:ok
|
||||
end
|
||||
end
|
||||
"""
|
||||
|
||||
@typedoc """
|
||||
A list with all variable bindings.
|
||||
|
||||
The binding keys are usually atoms, but they may be a tuple for variables
|
||||
defined in a different context.
|
||||
"""
|
||||
@type binding :: [{atom() | tuple(), any}]
|
||||
|
||||
@boolean_compiler_options [
|
||||
:docs,
|
||||
:debug_info,
|
||||
:ignore_module_conflict,
|
||||
:relative_paths,
|
||||
:warnings_as_errors
|
||||
]
|
||||
|
||||
@list_compiler_options [:no_warn_undefined, :tracers, :parser_options]
|
||||
|
||||
@available_compiler_options @boolean_compiler_options ++ @list_compiler_options
|
||||
|
||||
@doc """
|
||||
Lists all required files.
|
||||
|
||||
@@ -150,7 +46,7 @@ defmodule Code do
|
||||
:elixir_code_server.call(:required)
|
||||
end
|
||||
|
||||
@deprecated "Use Code.required_files/0 instead"
|
||||
# TODO: Deprecate me on 1.9
|
||||
@doc false
|
||||
def loaded_files do
|
||||
required_files()
|
||||
@@ -182,7 +78,7 @@ defmodule Code do
|
||||
:elixir_code_server.cast({:unrequire_files, files})
|
||||
end
|
||||
|
||||
@deprecated "Use Code.unrequire_files/1 instead"
|
||||
# TODO: Deprecate me on 1.9
|
||||
@doc false
|
||||
def unload_files(files) do
|
||||
unrequire_files(files)
|
||||
@@ -259,7 +155,7 @@ defmodule Code do
|
||||
@doc """
|
||||
Evaluates the contents given by `string`.
|
||||
|
||||
The `binding` argument is a list of variable bindings.
|
||||
The `binding` argument is a keyword list of variable bindings.
|
||||
The `opts` argument is a keyword list of environment options.
|
||||
|
||||
**Warning**: `string` can be any Elixir code and will be executed with
|
||||
@@ -300,8 +196,8 @@ defmodule Code do
|
||||
where `value` is the value returned from evaluating `string`.
|
||||
If an error occurs while evaluating `string` an exception will be raised.
|
||||
|
||||
`binding` is a list with all variable bindings
|
||||
after evaluating `string`. The binding keys are usually atoms, but they
|
||||
`binding` is a keyword list with the value of all variable bindings
|
||||
after evaluating `string`. The binding key is usually an atom, but it
|
||||
may be a tuple for variables defined in a different context.
|
||||
|
||||
## Examples
|
||||
@@ -323,22 +219,17 @@ defmodule Code do
|
||||
{3, [a: 1, b: 2]}
|
||||
|
||||
"""
|
||||
@spec eval_string(List.Chars.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
|
||||
@spec eval_string(List.Chars.t(), list, Macro.Env.t() | keyword) :: {term, binding :: list}
|
||||
def eval_string(string, binding \\ [], opts \\ [])
|
||||
|
||||
def eval_string(string, binding, %Macro.Env{} = env) do
|
||||
eval_string_with_error_handling(string, binding, Map.to_list(env))
|
||||
{value, binding, _env, _scope} = :elixir.eval(to_charlist(string), binding, Map.to_list(env))
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
def eval_string(string, binding, opts) when is_list(opts) do
|
||||
validate_eval_opts(opts)
|
||||
eval_string_with_error_handling(string, binding, opts)
|
||||
end
|
||||
|
||||
defp eval_string_with_error_handling(string, binding, opts) do
|
||||
%{line: line, file: file} = env = :elixir.env_for_eval(opts)
|
||||
forms = :elixir.string_to_quoted!(to_charlist(string), line, file, [])
|
||||
{value, binding, _env} = :elixir.eval_forms(forms, binding, env)
|
||||
{value, binding, _env, _scope} = :elixir.eval(to_charlist(string), binding, opts)
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
@@ -373,13 +264,6 @@ defmodule Code do
|
||||
expects a valid `Version` which is usually the minimum Elixir
|
||||
version supported by the project.
|
||||
|
||||
* `:force_do_end_blocks` (since v1.9.0) - when `true`, converts all
|
||||
inline usages of `do: ...`, `else: ...` and friends into `do/end`
|
||||
blocks. Defaults to `false`. Notice this option is convergent:
|
||||
once you set it to `true`, all keywords will be converted. If you
|
||||
set it to `false` later on, `do/end` blocks won't be converted
|
||||
back to keywords.
|
||||
|
||||
## Design principles
|
||||
|
||||
The formatter was designed under three principles.
|
||||
@@ -395,7 +279,7 @@ defmodule Code do
|
||||
a whole.
|
||||
|
||||
The formatter does not hard code names. The formatter will not behave
|
||||
specially because a function is named `defmodule`, `def`, or the like. This
|
||||
specially because a function is named `defmodule`, `def`, etc. This
|
||||
principle mirrors Elixir's goal of being an extensible language where
|
||||
developers can extend the language with new constructs as if they were
|
||||
part of the language. When it is absolutely necessary to change behaviour
|
||||
@@ -525,14 +409,14 @@ defmodule Code do
|
||||
broken into multiple lines if they are followed by a newline in the
|
||||
opening bracket and preceded by a new line in the closing bracket
|
||||
|
||||
* Newlines before certain operators (such as the pipeline operators)
|
||||
and before other operators (such as comparison operators)
|
||||
* Pipeline operators, like `|>` and others with the same precedence,
|
||||
will span multiple lines if they spanned multiple lines in the input
|
||||
|
||||
The behaviours above are not guaranteed. We may remove or add new
|
||||
rules in the future. The goal of documenting them is to provide better
|
||||
understanding on what to expect from the formatter.
|
||||
|
||||
### Multi-line lists, maps, tuples, and the like
|
||||
### Multi-line lists, maps, tuples, etc.
|
||||
|
||||
You can force lists, tuples, bitstrings, maps, structs and function
|
||||
calls to have one entry per line by adding a newline after the opening
|
||||
@@ -677,7 +561,7 @@ defmodule Code do
|
||||
Macro arguments are typically transformed by unquoting them into the
|
||||
returned quoted expressions (instead of evaluated).
|
||||
|
||||
See `eval_string/3` for a description of `binding` and options.
|
||||
See `eval_string/3` for a description of bindings and options.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -693,17 +577,17 @@ defmodule Code do
|
||||
{3, [a: 1, b: 2]}
|
||||
|
||||
"""
|
||||
@spec eval_quoted(Macro.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
|
||||
@spec eval_quoted(Macro.t(), list, Macro.Env.t() | keyword) :: {term, binding :: list}
|
||||
def eval_quoted(quoted, binding \\ [], opts \\ [])
|
||||
|
||||
def eval_quoted(quoted, binding, %Macro.Env{} = env) do
|
||||
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, Map.to_list(env))
|
||||
{value, binding, _env, _scope} = :elixir.eval_quoted(quoted, binding, Map.to_list(env))
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
def eval_quoted(quoted, binding, opts) when is_list(opts) do
|
||||
validate_eval_opts(opts)
|
||||
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, opts)
|
||||
{value, binding, _env, _scope} = :elixir.eval_quoted(quoted, binding, opts)
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
@@ -745,7 +629,7 @@ defmodule Code do
|
||||
end
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@doc """
|
||||
Converts the given string to its quoted form.
|
||||
|
||||
Returns `{:ok, quoted_form}` if it succeeds,
|
||||
@@ -766,24 +650,6 @@ defmodule Code do
|
||||
when non-existing atoms are found by the tokenizer.
|
||||
Defaults to `false`.
|
||||
|
||||
* `:token_metadata` (since v1.10.0) - 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`.
|
||||
|
||||
* `:literal_encoder` (since v1.10.0) - how to encode literals in the AST.
|
||||
It must be a function that receives two arguments, the literal and its
|
||||
metadata, and it must return `{:ok, ast :: Macro.t}` or
|
||||
`{:error, reason :: binary}`. If you return anything than the literal
|
||||
itself as the `term`, then the AST is no longer valid. This option
|
||||
may still useful for textual analysis of the source code.
|
||||
|
||||
* `:static_atoms_encoder` - the static atom encoder function, see
|
||||
"The `:static_atoms_encoder` function" section below. Note this
|
||||
option overrides the `:existing_atoms_only` behaviour for static
|
||||
atoms but `:existing_atoms_only` is still used for dynamic atoms,
|
||||
such as atoms with interpolations.
|
||||
|
||||
* `:warn_on_unnecessary_quotes` - when `false`, does not warn
|
||||
when atoms, keywords or calls have unnecessary quotes on
|
||||
them. Defaults to `true`.
|
||||
@@ -793,37 +659,6 @@ defmodule Code do
|
||||
The opposite of converting a string to its quoted form is
|
||||
`Macro.to_string/2`, which converts a quoted form to a string/binary
|
||||
representation.
|
||||
|
||||
## The `:static_atoms_encoder` function
|
||||
|
||||
When `static_atoms_encoder: &my_encoder/2` is passed as an argument,
|
||||
`my_encoder/2` is called every time the tokenizer needs to create a
|
||||
"static" atom. Static atoms are atoms in the AST that function as
|
||||
aliases, remote calls, local calls, variable names, regular atoms
|
||||
and keyword lists.
|
||||
|
||||
The encoder function will receive the atom name (as a binary) and a
|
||||
keyword list with the current file, line and column. It must return
|
||||
`{:ok, token :: term} | {:error, reason :: binary}`.
|
||||
|
||||
The encoder function is supposed to create an atom from the given
|
||||
string. To produce a valid AST, it is required to return `{:ok, term}`,
|
||||
where `term` is an atom. It is possible to return something other than an atom,
|
||||
however, in that case the AST is no longer "valid" in that it cannot
|
||||
be used to compile or evaluate Elixir code. A use case for this is
|
||||
if you want to use the Elixir parser in a user-facing situation, but
|
||||
you don't want to exhaust the atom table.
|
||||
|
||||
The atom encoder is not called for *all* atoms that are present in
|
||||
the AST. It won't be invoked for the following atoms:
|
||||
|
||||
* operators (`:+`, `:-`, and so on)
|
||||
|
||||
* syntax keywords (`fn`, `do`, `else`, and so on)
|
||||
|
||||
* atoms containing interpolation (`:"#{1 + 1} is two"`), as these
|
||||
atoms are constructed at runtime.
|
||||
|
||||
"""
|
||||
@spec string_to_quoted(List.Chars.t(), keyword) ::
|
||||
{:ok, Macro.t()} | {:error, {line :: pos_integer, term, term}}
|
||||
@@ -864,22 +699,22 @@ defmodule Code do
|
||||
|
||||
While `require_file/2` and `compile_file/2` return the loaded modules and their
|
||||
bytecode, `eval_file/2` simply evaluates the file contents and returns the
|
||||
evaluation result and its binding (exactly the same return value as `eval_string/3`).
|
||||
evaluation result and its bindings (exactly the same return value as `eval_string/3`).
|
||||
"""
|
||||
@spec eval_file(binary, nil | binary) :: {term, binding}
|
||||
@spec eval_file(binary, nil | binary) :: {term, binding :: list}
|
||||
def eval_file(file, relative_to \\ nil) when is_binary(file) do
|
||||
file = find_file(file, relative_to)
|
||||
eval_string(File.read!(file), [], file: file, line: 1)
|
||||
end
|
||||
|
||||
@deprecated "Use Code.require_file/2 or Code.compile_file/2 instead"
|
||||
# TODO: Deprecate me on 1.9
|
||||
@doc false
|
||||
def load_file(file, relative_to \\ nil) when is_binary(file) do
|
||||
file = find_file(file, relative_to)
|
||||
:elixir_code_server.call({:acquire, file})
|
||||
loaded = :elixir_compiler.file(file, fn _, _ -> :ok end)
|
||||
loaded = :elixir_compiler.file(file)
|
||||
:elixir_code_server.cast({:required, file})
|
||||
verify_loaded(loaded)
|
||||
loaded
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -918,184 +753,61 @@ defmodule Code do
|
||||
def require_file(file, relative_to \\ nil) when is_binary(file) do
|
||||
file = find_file(file, relative_to)
|
||||
|
||||
# TODO: Simply block until :required or :proceed once load_file is removed in 2.0
|
||||
case :elixir_code_server.call({:acquire, file}) do
|
||||
:required ->
|
||||
nil
|
||||
|
||||
{:queued, ref} ->
|
||||
receive do
|
||||
{:elixir_code_server, ^ref, :required} -> nil
|
||||
end
|
||||
|
||||
:proceed ->
|
||||
loaded = :elixir_compiler.file(file, fn _, _ -> :ok end)
|
||||
loaded = :elixir_compiler.file(file)
|
||||
:elixir_code_server.cast({:required, file})
|
||||
verify_loaded(loaded)
|
||||
loaded
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets all compilation options from the code server.
|
||||
Gets the compilation options from the code server.
|
||||
|
||||
To get invidual options, see `get_compiler_option/1`.
|
||||
For a description of all options, see `put_compiler_option/2`.
|
||||
Check `compiler_options/1` for more information.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.compiler_options()
|
||||
#=> %{debug_info: true, docs: true, ...}
|
||||
#=> %{debug_info: true, docs: true,
|
||||
#=> warnings_as_errors: false, ignore_module_conflict: false}
|
||||
|
||||
"""
|
||||
@spec compiler_options :: map
|
||||
@spec compiler_options() :: %{optional(atom) => boolean}
|
||||
def compiler_options do
|
||||
for key <- @available_compiler_options, into: %{} do
|
||||
{key, :elixir_config.get(key)}
|
||||
end
|
||||
:elixir_config.get(:compiler_options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stores all given compilation options.
|
||||
Returns a list with the available compiler options.
|
||||
|
||||
To store invidual options, see `put_compiler_option/2`.
|
||||
For a description of all options, see `put_compiler_option/2`.
|
||||
See `compiler_options/1` for more information.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.compiler_options()
|
||||
#=> %{debug_info: true, docs: true, ...}
|
||||
|
||||
"""
|
||||
@spec compiler_options(Enumerable.t()) :: %{optional(atom) => boolean}
|
||||
def compiler_options(opts) do
|
||||
for {key, value} <- opts, into: %{} do
|
||||
put_compiler_option(key, value)
|
||||
{key, value}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value of a given compiler option.
|
||||
|
||||
For a description of all options, see `put_compiler_option/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.get_compiler_option(:debug_info)
|
||||
#=> true
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec get_compiler_option(atom) :: term
|
||||
def get_compiler_option(key) when key in @available_compiler_options do
|
||||
:elixir_config.get(key)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list with all available compiler options.
|
||||
|
||||
For a description of all options, see `put_compiler_option/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.available_compiler_options()
|
||||
#=> [:docs, :debug_info, ...]
|
||||
iex> Code.available_compiler_options()
|
||||
[:docs, :debug_info, :ignore_module_conflict, :relative_paths, :warnings_as_errors]
|
||||
|
||||
"""
|
||||
@spec available_compiler_options() :: [atom]
|
||||
def available_compiler_options do
|
||||
@available_compiler_options
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stores a compilation option.
|
||||
|
||||
These options are global since they are stored by Elixir's code server.
|
||||
|
||||
Available options are:
|
||||
|
||||
* `:docs` - when `true`, retain documentation in the compiled module.
|
||||
Defaults to `true`.
|
||||
|
||||
* `:debug_info` - when `true`, retain debug information in the compiled
|
||||
module. This allows a developer to reconstruct the original source
|
||||
code. Defaults to `true`.
|
||||
|
||||
* `:ignore_module_conflict` - when `true`, override modules that were
|
||||
already defined without raising errors. Defaults to `false`.
|
||||
|
||||
* `:relative_paths` - when `true`, use relative paths in quoted nodes,
|
||||
warnings and errors generated by the compiler. Note disabling this option
|
||||
won't affect runtime warnings and errors. Defaults to `true`.
|
||||
|
||||
* `:warnings_as_errors` - causes compilation to fail when warnings are
|
||||
generated. Defaults to `false`.
|
||||
|
||||
* `:no_warn_undefined` (since v1.10.0) - list of modules and `{Mod, fun, arity}`
|
||||
tuples that will not emit warnings that the module or function does not exist
|
||||
at compilation time. Pass atom `:all` to skip warning for all undefined
|
||||
functions. This can be useful when doing dynamic compilation. Defaults to `[]`.
|
||||
|
||||
* `:tracers` (since v1.10.0) - a list of tracers (modules) to be used during
|
||||
compilation. See the module docs for more information. Defaults to `[]`.
|
||||
|
||||
* `:parser_options` (since v1.10.0) - a keyword list of options to be given
|
||||
to the parser when compiling files. It accepts the same options as
|
||||
`string_to_quoted/2` (except by the options that change the AST itself).
|
||||
This can be used in combination with the tracer to retrieve localized
|
||||
information about events happening during compilation. Defaults to `[]`.
|
||||
|
||||
It always returns `:ok`. Raises an error for invalid options.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.put_compiler_option(:debug_info, true)
|
||||
#=> :ok
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec put_compiler_option(atom, term) :: :ok
|
||||
def put_compiler_option(key, value) when key in @boolean_compiler_options do
|
||||
if not is_boolean(value) do
|
||||
raise "compiler option #{inspect(key)} should be a boolean, got: #{inspect(value)}"
|
||||
end
|
||||
|
||||
:elixir_config.put(key, value)
|
||||
:ok
|
||||
end
|
||||
|
||||
def put_compiler_option(:no_warn_undefined, value) do
|
||||
if value != :all and not is_list(value) do
|
||||
raise "compiler option :no_warn_undefined should be a list or the atom :all, " <>
|
||||
"got: #{inspect(value)}"
|
||||
end
|
||||
|
||||
:elixir_config.put(:no_warn_undefined, value)
|
||||
:ok
|
||||
end
|
||||
|
||||
def put_compiler_option(key, value) when key in @list_compiler_options do
|
||||
if not is_list(value) do
|
||||
raise "compiler option #{inspect(key)} should be a list, got: #{inspect(value)}"
|
||||
end
|
||||
|
||||
if key == :parser_options and not Keyword.keyword?(value) do
|
||||
raise "compiler option #{inspect(key)} should be a keyword list, " <>
|
||||
"got: #{inspect(value)}"
|
||||
end
|
||||
|
||||
if key == :tracers and not Enum.all?(value, &is_atom/1) do
|
||||
raise "compiler option #{inspect(key)} should be a list of modules, " <>
|
||||
"got: #{inspect(value)}"
|
||||
end
|
||||
|
||||
:elixir_config.put(key, value)
|
||||
:ok
|
||||
end
|
||||
|
||||
def put_compiler_option(key, _value) do
|
||||
raise "unknown compiler option: #{inspect(key)}"
|
||||
[:docs, :debug_info, :ignore_module_conflict, :relative_paths, :warnings_as_errors]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Purge compiler modules.
|
||||
|
||||
The compiler utilizes temporary modules to compile code. For example,
|
||||
`elixir_compiler_1`, `elixir_compiler_2`, and so on. In case the compiled code
|
||||
`elixir_compiler_1`, `elixir_compiler_2`, etc. In case the compiled code
|
||||
stores references to anonymous functions or similar, the Elixir compiler
|
||||
may be unable to reclaim those modules, keeping an unnecessary amount of
|
||||
code in memory and eventually leading to modules such as `elixir_compiler_12345`.
|
||||
@@ -1112,6 +824,59 @@ defmodule Code do
|
||||
:elixir_code_server.call(:purge_compiler_modules)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sets compilation options.
|
||||
|
||||
These options are global since they are stored by Elixir's Code Server.
|
||||
|
||||
Available options are:
|
||||
|
||||
* `:docs` - when `true`, retain documentation in the compiled module.
|
||||
Defaults to `true`.
|
||||
|
||||
* `:debug_info` - when `true`, retain debug information in the compiled
|
||||
module. This allows a developer to reconstruct the original source
|
||||
code. Defaults to `false`.
|
||||
|
||||
* `:ignore_module_conflict` - when `true`, override modules that were
|
||||
already defined without raising errors. Defaults to `false`.
|
||||
|
||||
* `:relative_paths` - when `true`, use relative paths in quoted nodes,
|
||||
warnings and errors generated by the compiler. Note disabling this option
|
||||
won't affect runtime warnings and errors. Defaults to `true`.
|
||||
|
||||
* `:warnings_as_errors` - causes compilation to fail when warnings are
|
||||
generated. Defaults to `false`.
|
||||
|
||||
It returns the new map of compiler options.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.compiler_options(debug_info: true)
|
||||
#=> %{debug_info: true, docs: true,
|
||||
#=> warnings_as_errors: false, ignore_module_conflict: false}
|
||||
|
||||
"""
|
||||
@spec compiler_options(Enumerable.t()) :: %{optional(atom) => boolean}
|
||||
def compiler_options(opts) do
|
||||
available = available_compiler_options()
|
||||
|
||||
Enum.each(opts, fn {key, value} ->
|
||||
cond do
|
||||
key not in available ->
|
||||
raise "unknown compiler option: #{inspect(key)}"
|
||||
|
||||
not is_boolean(value) ->
|
||||
raise "compiler option #{inspect(key)} should be a boolean, got: #{inspect(value)}"
|
||||
|
||||
true ->
|
||||
:ok
|
||||
end
|
||||
end)
|
||||
|
||||
:elixir_config.update(:compiler_options, &Enum.into(opts, &1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compiles the given string.
|
||||
|
||||
@@ -1128,8 +893,7 @@ defmodule Code do
|
||||
"""
|
||||
@spec compile_string(List.Chars.t(), binary) :: [{module, binary}]
|
||||
def compile_string(string, file \\ "nofile") when is_binary(file) do
|
||||
loaded = :elixir_compiler.string(to_charlist(string), file, fn _, _ -> :ok end)
|
||||
Enum.map(loaded, fn {module, _map, binary} -> {module, binary} end)
|
||||
:elixir_compiler.string(to_charlist(string), file)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1142,8 +906,7 @@ defmodule Code do
|
||||
"""
|
||||
@spec compile_quoted(Macro.t(), binary) :: [{module, binary}]
|
||||
def compile_quoted(quoted, file \\ "nofile") when is_binary(file) do
|
||||
loaded = :elixir_compiler.quoted(quoted, file, fn _, _ -> :ok end)
|
||||
Enum.map(loaded, fn {module, _map, binary} -> {module, binary} end)
|
||||
:elixir_compiler.quoted(quoted, file)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1160,11 +923,9 @@ defmodule Code do
|
||||
|
||||
For compiling many files concurrently, see `Kernel.ParallelCompiler.compile/2`.
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec compile_file(binary, nil | binary) :: [{module, binary}]
|
||||
def compile_file(file, relative_to \\ nil) when is_binary(file) do
|
||||
loaded = :elixir_compiler.file(find_file(file, relative_to), fn _, _ -> :ok end)
|
||||
verify_loaded(loaded)
|
||||
:elixir_compiler.file(find_file(file, relative_to))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1247,40 +1008,23 @@ defmodule Code do
|
||||
Ensures the given module is compiled and loaded.
|
||||
|
||||
If the module is already loaded, it works as no-op. If the module was
|
||||
not compiled yet, `ensure_compiled/1` halts the compilation of the caller
|
||||
until the module given to `ensure_compiled/1` becomes available or
|
||||
all files for the current project have been compiled. If compilation
|
||||
finishes and the module is not available, an error tuple is returned.
|
||||
|
||||
Given this function halts compilation, use it carefully. In particular,
|
||||
avoid using it to guess which modules are in the system. Overuse of this
|
||||
function can also lead to deadlocks, where two modules check at the same time
|
||||
if the other is compiled. This returns a specific unavailable error code,
|
||||
where we cannot successfully verify a module is available or not.
|
||||
not loaded yet, it checks if it needs to be compiled first and then
|
||||
tries to load it.
|
||||
|
||||
If it succeeds in loading the module, it returns `{:module, module}`.
|
||||
If not, returns `{:error, reason}` with the error reason.
|
||||
|
||||
If the module being checked is currently in a compiler deadlock,
|
||||
this function returns `{:error, :unavailable}`. Unavailable doesn't
|
||||
necessarily mean the module doesn't exist, just that it is not currently
|
||||
available, but it (or may not) become available in the future.
|
||||
|
||||
Check `ensure_loaded/1` for more information on module loading
|
||||
and when to use `ensure_loaded/1` or `ensure_compiled/1`.
|
||||
"""
|
||||
@spec ensure_compiled(module) ::
|
||||
{:module, module}
|
||||
| {:error, :embedded | :badfile | :nofile | :on_load_failure | :unavailable}
|
||||
{:module, module} | {:error, :embedded | :badfile | :nofile | :on_load_failure}
|
||||
def ensure_compiled(module) when is_atom(module) do
|
||||
case :code.ensure_loaded(module) do
|
||||
{:error, :nofile} = error ->
|
||||
if is_pid(:erlang.get(:elixir_compiler_pid)) do
|
||||
case Kernel.ErrorHandler.ensure_compiled(module, :module, :soft) do
|
||||
:found -> {:module, module}
|
||||
:deadlock -> {:error, :unavailable}
|
||||
:not_found -> {:error, :nofile}
|
||||
end
|
||||
if is_pid(:erlang.get(:elixir_compiler_pid)) and
|
||||
Kernel.ErrorHandler.ensure_compiled(module, :module) do
|
||||
{:module, module}
|
||||
else
|
||||
error
|
||||
end
|
||||
@@ -1290,8 +1034,14 @@ defmodule Code do
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Code.ensure_compiled/1 instead (see the proper disclaimers in its docs)"
|
||||
@doc """
|
||||
Ensures the given module is compiled and loaded.
|
||||
|
||||
Similar to `ensure_compiled/1`, but returns `true` if the module
|
||||
is already loaded or was successfully loaded and compiled.
|
||||
Returns `false` otherwise.
|
||||
"""
|
||||
@spec ensure_compiled?(module) :: boolean
|
||||
def ensure_compiled?(module) when is_atom(module) do
|
||||
match?({:module, ^module}, ensure_compiled(module))
|
||||
end
|
||||
@@ -1313,7 +1063,7 @@ defmodule Code do
|
||||
# Module documentation of an existing module
|
||||
iex> {:docs_v1, _, :elixir, _, %{"en" => module_doc}, _, _} = Code.fetch_docs(Atom)
|
||||
iex> module_doc |> String.split("\n") |> Enum.at(0)
|
||||
"Atoms are constants whose values are their own name."
|
||||
"Convenience functions for working with atoms."
|
||||
|
||||
# A module that doesn't exist
|
||||
iex> Code.fetch_docs(ModuleNotGood)
|
||||
@@ -1327,7 +1077,7 @@ defmodule Code do
|
||||
| {:error, :module_not_found | :chunk_not_found | {:invalid_chunk, binary}}
|
||||
when annotation: :erl_anno.anno(),
|
||||
beam_language: :elixir | :erlang | :lfe | :alpaca | atom(),
|
||||
doc_content: %{required(binary) => binary} | :none | :hidden,
|
||||
doc_content: %{binary => binary} | :none | :hidden,
|
||||
doc_element:
|
||||
{{kind :: atom, function_name :: atom, arity}, annotation, signature, doc_content,
|
||||
metadata},
|
||||
@@ -1397,10 +1147,4 @@ defmodule Code do
|
||||
raise Code.LoadError, file: file
|
||||
end
|
||||
end
|
||||
|
||||
defp verify_loaded(loaded) do
|
||||
maps_binaries = Enum.map(loaded, fn {_module, map, binary} -> {map, binary} end)
|
||||
Module.ParallelChecker.verify(maps_binaries, [])
|
||||
Enum.map(loaded, fn {module, _map, binary} -> {module, binary} end)
|
||||
end
|
||||
end
|
||||
|
||||
+191
-222
@@ -28,7 +28,7 @@ defmodule Code.Formatter do
|
||||
@required_parens_logical_binary_operands [:||, :|||, :or, :&&, :&&&, :and]
|
||||
|
||||
# Operators with next break fits. = and :: do not consider new lines though
|
||||
@next_break_fits_operators [:<-, :==, :!=, :=~, :===, :!==, :<, :>, :<=, :>=, :=, :"::"]
|
||||
@next_break_fits_operators [:<-, :==, :!=, :=~, :===, :!==, :<, :>, :<=, :>=, :=, :::]
|
||||
|
||||
# Operators that always require parens on operands when they are the parent
|
||||
@required_parens_on_binary_operands [
|
||||
@@ -49,7 +49,7 @@ defmodule Code.Formatter do
|
||||
:<>
|
||||
]
|
||||
|
||||
@locals_without_parens [
|
||||
locals_without_parens = [
|
||||
# Special forms
|
||||
alias: 1,
|
||||
alias: 2,
|
||||
@@ -77,7 +77,6 @@ defmodule Code.Formatter do
|
||||
defmacro: 2,
|
||||
defmacrop: 1,
|
||||
defmacrop: 2,
|
||||
defmodule: 2,
|
||||
defdelegate: 2,
|
||||
defexception: 1,
|
||||
defoverridable: 1,
|
||||
@@ -99,6 +98,7 @@ defmodule Code.Formatter do
|
||||
defrecordp: 3,
|
||||
|
||||
# Testing
|
||||
all: :*,
|
||||
assert: 1,
|
||||
assert: 2,
|
||||
assert_in_delta: 3,
|
||||
@@ -110,8 +110,12 @@ defmodule Code.Formatter do
|
||||
assert_receive: 3,
|
||||
assert_received: 1,
|
||||
assert_received: 2,
|
||||
check: 1,
|
||||
check: 2,
|
||||
doctest: 1,
|
||||
doctest: 2,
|
||||
property: 1,
|
||||
property: 2,
|
||||
refute: 1,
|
||||
refute: 2,
|
||||
refute_in_delta: 3,
|
||||
@@ -134,7 +138,7 @@ defmodule Code.Formatter do
|
||||
import_config: 1
|
||||
]
|
||||
|
||||
@do_end_keywords [:rescue, :catch, :else, :after]
|
||||
@locals_without_parens MapSet.new(locals_without_parens)
|
||||
|
||||
@doc """
|
||||
Checks if two strings are equivalent.
|
||||
@@ -205,13 +209,8 @@ defmodule Code.Formatter do
|
||||
warn_on_unnecessary_quotes: false
|
||||
]
|
||||
|
||||
parser_options = [
|
||||
literal_encoder: &{:ok, {:__block__, &2, [&1]}},
|
||||
token_metadata: true
|
||||
]
|
||||
|
||||
with {:ok, tokens} <- :elixir.string_to_tokens(charlist, line, file, tokenizer_options),
|
||||
{:ok, forms} <- :elixir.tokens_to_quoted(tokens, file, parser_options) do
|
||||
{:ok, forms} <- :elixir.tokens_to_quoted(tokens, file, formatter_metadata: true) do
|
||||
state =
|
||||
Process.get(:code_formatter_comments)
|
||||
|> Enum.reverse()
|
||||
@@ -243,8 +242,6 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp state(comments, opts) do
|
||||
force_do_end_blocks = Keyword.get(opts, :force_do_end_blocks, false)
|
||||
|
||||
rename_deprecated_at =
|
||||
if version = opts[:rename_deprecated_at] do
|
||||
case Version.parse(version) do
|
||||
@@ -258,10 +255,13 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
locals_without_parens =
|
||||
Keyword.get(opts, :locals_without_parens, []) ++ @locals_without_parens
|
||||
opts
|
||||
|> Keyword.get(:locals_without_parens, [])
|
||||
|> MapSet.new()
|
||||
|> MapSet.union(@locals_without_parens)
|
||||
|> MapSet.to_list()
|
||||
|
||||
%{
|
||||
force_do_end_blocks: force_do_end_blocks,
|
||||
locals_without_parens: locals_without_parens,
|
||||
operand_nesting: 2,
|
||||
rename_deprecated_at: rename_deprecated_at,
|
||||
@@ -353,7 +353,7 @@ defmodule Code.Formatter do
|
||||
not interpolated?(entries) ->
|
||||
bitstring_to_algebra(meta, entries, state)
|
||||
|
||||
meta[:delimiter] == ~s["""] ->
|
||||
meta[:format] == :bin_heredoc ->
|
||||
{doc, state} =
|
||||
entries
|
||||
|> prepend_heredoc_line()
|
||||
@@ -375,7 +375,7 @@ defmodule Code.Formatter do
|
||||
not list_interpolated?(entries) ->
|
||||
remote_to_algebra(quoted, context, state)
|
||||
|
||||
meta[:delimiter] == ~s['''] ->
|
||||
meta[:format] == :list_heredoc ->
|
||||
{doc, state} =
|
||||
entries
|
||||
|> prepend_heredoc_line()
|
||||
@@ -433,12 +433,12 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp quoted_to_algebra({:__block__, meta, [list]}, _context, state) when is_list(list) do
|
||||
case meta[:delimiter] do
|
||||
~s['''] ->
|
||||
case meta[:format] do
|
||||
:list_heredoc ->
|
||||
string = list |> List.to_string() |> escape_heredoc()
|
||||
{@single_heredoc |> concat(string) |> concat(@single_heredoc) |> force_unfit(), state}
|
||||
|
||||
~s['] ->
|
||||
:charlist ->
|
||||
string = list |> List.to_string() |> escape_string(@single_quote)
|
||||
{@single_quote |> concat(string) |> concat(@single_quote), state}
|
||||
|
||||
@@ -448,7 +448,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp quoted_to_algebra({:__block__, meta, [string]}, _context, state) when is_binary(string) do
|
||||
if meta[:delimiter] == ~s["""] do
|
||||
if meta[:format] == :bin_heredoc do
|
||||
string = escape_heredoc(string)
|
||||
{@double_heredoc |> concat(string) |> concat(@double_heredoc) |> force_unfit(), state}
|
||||
else
|
||||
@@ -463,11 +463,11 @@ defmodule Code.Formatter do
|
||||
|
||||
defp quoted_to_algebra({:__block__, meta, [integer]}, _context, state)
|
||||
when is_integer(integer) do
|
||||
{integer_to_algebra(Keyword.fetch!(meta, :token)), state}
|
||||
{integer_to_algebra(Keyword.fetch!(meta, :original)), state}
|
||||
end
|
||||
|
||||
defp quoted_to_algebra({:__block__, meta, [float]}, _context, state) when is_float(float) do
|
||||
{float_to_algebra(Keyword.fetch!(meta, :token)), state}
|
||||
{float_to_algebra(Keyword.fetch!(meta, :original)), state}
|
||||
end
|
||||
|
||||
defp quoted_to_algebra(
|
||||
@@ -484,7 +484,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp quoted_to_algebra({:__block__, meta, _} = block, _context, state) do
|
||||
{block, state} = block_to_algebra(block, line(meta), closing_line(meta), state)
|
||||
{block, state} = block_to_algebra(block, line(meta), end_line(meta), state)
|
||||
{surround("(", block, ")"), state}
|
||||
end
|
||||
|
||||
@@ -518,7 +518,7 @@ defmodule Code.Formatter do
|
||||
defp quoted_to_algebra({:not, meta, [{:in, _, [left, right]} = arg]}, context, state) do
|
||||
%{rename_deprecated_at: since} = state
|
||||
|
||||
# TODO: Remove metadata and always rewrite to left not in right in Elixir v2.0.
|
||||
# TODO: Remove since check on Elixir v2.0 and the OP arrangement is removed.
|
||||
if meta[:operator] == :"not in" || (since && Version.match?(since, "~> 1.5")) do
|
||||
binary_op_to_algebra(:in, "not in", meta, left, right, context, state)
|
||||
else
|
||||
@@ -527,7 +527,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp quoted_to_algebra({:fn, meta, [_ | _] = clauses}, _context, state) do
|
||||
anon_fun_to_algebra(clauses, line(meta), closing_line(meta), state, eol?(meta))
|
||||
anon_fun_to_algebra(clauses, line(meta), end_line(meta), state, eol?(meta))
|
||||
end
|
||||
|
||||
defp quoted_to_algebra({fun, meta, args}, context, state) when is_atom(fun) and is_list(args) do
|
||||
@@ -611,14 +611,14 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp block_args_to_algebra(args, min_line, max_line, state) do
|
||||
quoted_to_algebra = fn {kind, meta, _} = arg, _args, state ->
|
||||
newlines = meta[:end_of_expression][:newlines] || 1
|
||||
quoted_to_algebra = fn {kind, meta, _} = arg, _args, doc_newlines, state ->
|
||||
doc_newlines = Keyword.get(meta, :newlines, doc_newlines)
|
||||
{doc, state} = quoted_to_algebra(arg, :block, state)
|
||||
{{doc, block_next_line(kind), newlines}, state}
|
||||
{doc, block_next_line(kind), doc_newlines, state}
|
||||
end
|
||||
|
||||
{args_docs, _comments?, state} =
|
||||
quoted_to_algebra_with_comments(args, [], min_line, max_line, state, quoted_to_algebra)
|
||||
quoted_to_algebra_with_comments(args, [], min_line, max_line, 2, state, quoted_to_algebra)
|
||||
|
||||
case args_docs do
|
||||
[] -> {@empty, state}
|
||||
@@ -673,11 +673,11 @@ defmodule Code.Formatter do
|
||||
|
||||
# There are five kinds of operators.
|
||||
#
|
||||
# 1. no space binary operators, for example, 1..2
|
||||
# 2. no newline binary operators, for example, left in right
|
||||
# 3. strict newlines before a left precedent operator, for example, foo |> bar |> baz
|
||||
# 4. strict newlines before a right precedent operator, for example, foo when bar when baz
|
||||
# 5. flex newlines after the operator, for example, foo ++ bar ++ baz
|
||||
# 1. no space binary operators, e.g. 1..2
|
||||
# 2. no newline binary operators, e.g. left in right
|
||||
# 3. strict newlines before a left precedent operator, e.g. foo |> bar |> baz
|
||||
# 4. strict newlines before a right precedent operator, e.g. foo when bar when baz
|
||||
# 5. flex newlines after the operator, e.g. foo ++ bar ++ baz
|
||||
#
|
||||
# Cases 1, 2 and 5 are handled fairly easily by relying on the
|
||||
# operator precedence and making sure nesting is applied only once.
|
||||
@@ -706,14 +706,14 @@ defmodule Code.Formatter do
|
||||
unwrap_right(right_arg, op, meta, right_context, [{{:root, left_context}, left_arg}])
|
||||
|
||||
operand_to_algebra = fn
|
||||
{{:root, context}, arg}, _args, state ->
|
||||
{{:root, context}, arg}, _args, newlines, state ->
|
||||
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :left, 2)
|
||||
{{doc, @empty, 1}, state}
|
||||
{doc, @empty, newlines, state}
|
||||
|
||||
{{kind, context}, arg}, _args, state ->
|
||||
{{kind, context}, arg}, _args, newlines, state ->
|
||||
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, kind, 0)
|
||||
doc = doc |> nest_by_length(op_string) |> force_keyword(arg)
|
||||
{{concat(op_string, doc), @empty, 1}, state}
|
||||
{concat(op_string, doc), @empty, newlines, state}
|
||||
end
|
||||
|
||||
{doc, state} =
|
||||
@@ -744,15 +744,15 @@ defmodule Code.Formatter do
|
||||
unwrap_pipes(left_arg, meta, left_context, [{{op, right_context}, right_arg}])
|
||||
|
||||
operand_to_algebra = fn
|
||||
{{:root, context}, arg}, _args, state ->
|
||||
{{:root, context}, arg}, _args, newlines, state ->
|
||||
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :left, 2)
|
||||
{{doc, @empty, 1}, state}
|
||||
{doc, @empty, newlines, state}
|
||||
|
||||
{{op, context}, arg}, _args, state ->
|
||||
{{op, context}, arg}, _args, newlines, state ->
|
||||
op_info = Code.Identifier.binary_op(op)
|
||||
op_string = Atom.to_string(op) <> " "
|
||||
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :right, 0)
|
||||
{{concat(op_string, doc), @empty, 1}, state}
|
||||
{concat(op_string, doc), @empty, newlines, state}
|
||||
end
|
||||
|
||||
operand_to_algebra_with_comments(pipes, meta, min_line, max_line, state, operand_to_algebra)
|
||||
@@ -796,7 +796,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
# TODO: We can remove this workaround once we remove
|
||||
# ?rearrange_uop from the parser on v2.0.
|
||||
# ?rearrange_uop from the parser in Elixir v2.0.
|
||||
# (! left) in right
|
||||
# (not left) in right
|
||||
defp binary_operand_to_algebra(
|
||||
@@ -885,7 +885,7 @@ defmodule Code.Formatter do
|
||||
|
||||
defp operand_to_algebra_with_comments(operands, meta, min_line, max_line, state, fun) do
|
||||
{docs, comments?, state} =
|
||||
quoted_to_algebra_with_comments(operands, [], min_line, max_line, state, fun)
|
||||
quoted_to_algebra_with_comments(operands, [], min_line, max_line, 1, state, fun)
|
||||
|
||||
if comments? or eol?(meta) do
|
||||
{docs |> Enum.reduce(&line(&2, &1)) |> force_unfit(), state}
|
||||
@@ -905,11 +905,11 @@ defmodule Code.Formatter do
|
||||
|
||||
# @foo bar
|
||||
# @foo(bar)
|
||||
defp module_attribute_to_algebra(meta, {name, call_meta, [_] = args} = expr, context, state)
|
||||
defp module_attribute_to_algebra(meta, {name, _, [_] = args} = expr, context, state)
|
||||
when is_atom(name) and name not in [:__block__, :__aliases__] do
|
||||
if Code.Identifier.classify(name) == :callable_local do
|
||||
{{call_doc, state}, wrap_in_parens?} =
|
||||
call_args_to_algebra(args, call_meta, context, :skip_unless_many_args, false, state)
|
||||
call_args_to_algebra(args, meta, context, :skip_unless_many_args, false, state)
|
||||
|
||||
doc =
|
||||
"@#{name}"
|
||||
@@ -1000,7 +1000,8 @@ defmodule Code.Formatter do
|
||||
fun = remote_fun_to_algebra(target, fun, length(args), state)
|
||||
remote_doc = target_doc |> concat(".") |> concat(string(fun))
|
||||
|
||||
if args == [] and not remote_target_is_a_module?(target) and not meta?(meta, :closing) do
|
||||
if args == [] and not remote_target_is_a_module?(target) and
|
||||
Keyword.get(meta, :no_parens, false) do
|
||||
{remote_doc, state}
|
||||
else
|
||||
{{call_doc, state}, wrap_in_parens?} =
|
||||
@@ -1057,7 +1058,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
# We can only rename functions in the same module because
|
||||
# introducing a new module may be wrong due to aliases.
|
||||
# introducing a new module may wrong due to aliases.
|
||||
defp deprecated(Enum, :partition, 2), do: {"split_with", "~> 1.4"}
|
||||
defp deprecated(Code, :unload_files, 2), do: {"unrequire_files", "~> 1.7"}
|
||||
defp deprecated(Code, :loaded_files, 2), do: {"required_files", "~> 1.7"}
|
||||
@@ -1079,7 +1080,7 @@ defmodule Code.Formatter do
|
||||
defp local_to_algebra(fun, meta, args, context, state) when is_atom(fun) do
|
||||
skip_parens =
|
||||
cond do
|
||||
meta?(meta, :closing) -> :skip_if_only_do_end
|
||||
not Keyword.get(meta, :no_parens, false) -> :required
|
||||
local_without_parens?(fun, args, state) -> :skip_unless_many_args
|
||||
true -> :skip_if_do_end
|
||||
end
|
||||
@@ -1097,10 +1098,9 @@ defmodule Code.Formatter do
|
||||
{doc, state}
|
||||
end
|
||||
|
||||
# parens may be one of:
|
||||
# parens may one of:
|
||||
#
|
||||
# * :skip_unless_many_args - skips parens unless we are the argument context
|
||||
# * :skip_if_only_do_end - skip parens if we are do-end and the only arg
|
||||
# * :skip_if_do_end - skip parens if we are do-end
|
||||
# * :required - never skip parens
|
||||
#
|
||||
@@ -1114,18 +1114,13 @@ defmodule Code.Formatter do
|
||||
defp call_args_to_algebra(args, meta, context, parens, list_to_keyword?, state) do
|
||||
{rest, last} = split_last(args)
|
||||
|
||||
if blocks = do_end_blocks(meta, last, state) do
|
||||
if blocks = do_end_blocks(last) do
|
||||
{call_doc, state} =
|
||||
case rest do
|
||||
[] when parens == :required ->
|
||||
{"() do", state}
|
||||
|
||||
[] ->
|
||||
{" do", state}
|
||||
|
||||
_ ->
|
||||
no_parens? = parens not in [:required, :skip_if_only_do_end]
|
||||
call_args_to_algebra_no_blocks(meta, rest, no_parens?, list_to_keyword?, " do", state)
|
||||
if rest == [] do
|
||||
{" do", state}
|
||||
else
|
||||
no_parens? = parens != :required
|
||||
call_args_to_algebra_no_blocks(meta, rest, no_parens?, list_to_keyword?, " do", state)
|
||||
end
|
||||
|
||||
{blocks_doc, state} = do_end_blocks_to_algebra(blocks, state)
|
||||
@@ -1145,7 +1140,7 @@ defmodule Code.Formatter do
|
||||
|
||||
defp call_args_to_algebra_no_blocks(meta, args, skip_parens?, list_to_keyword?, extra, state) do
|
||||
{left, right} = split_last(args)
|
||||
{keyword?, right} = last_arg_to_keyword(right, list_to_keyword?, skip_parens?, state.comments)
|
||||
{keyword?, right} = last_arg_to_keyword(right, list_to_keyword?)
|
||||
|
||||
context =
|
||||
if left == [] and not keyword? do
|
||||
@@ -1166,7 +1161,7 @@ defmodule Code.Formatter do
|
||||
{left_doc, _join, state} =
|
||||
args_to_algebra_with_comments(
|
||||
left,
|
||||
Keyword.delete(meta, :closing),
|
||||
Keyword.delete(meta, :end_line),
|
||||
skip_parens?,
|
||||
:force_comma,
|
||||
join,
|
||||
@@ -1273,19 +1268,16 @@ defmodule Code.Formatter do
|
||||
not Enum.any?(args, &match?({:<-, _, [_, _]}, &1))
|
||||
end
|
||||
|
||||
defp do_end_blocks(meta, [{{:__block__, _, [:do]}, _} | rest] = blocks, state) do
|
||||
if meta?(meta, :do) or can_force_do_end_blocks?(rest, state) do
|
||||
defp do_end_blocks([{{:__block__, meta, [:do]}, _} | _] = blocks) do
|
||||
if meta[:format] == :block do
|
||||
blocks
|
||||
|> Enum.map(fn {{:__block__, meta, [key]}, value} -> {key, line(meta), value} end)
|
||||
|> do_end_blocks_with_range(end_line(meta))
|
||||
end
|
||||
end
|
||||
|
||||
defp do_end_blocks(_, _, _), do: nil
|
||||
|
||||
defp can_force_do_end_blocks?(rest, state) do
|
||||
state.force_do_end_blocks and
|
||||
Enum.all?(rest, fn {{:__block__, _, [key]}, _} -> key in @do_end_keywords end)
|
||||
defp do_end_blocks(_) do
|
||||
nil
|
||||
end
|
||||
|
||||
defp do_end_blocks_with_range([{key1, line1, value1}, {_, line2, _} = h | t], end_line) do
|
||||
@@ -1324,7 +1316,7 @@ defmodule Code.Formatter do
|
||||
|
||||
defp interpolated?(entries) do
|
||||
Enum.all?(entries, fn
|
||||
{:"::", _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
|
||||
entry when is_binary(entry) -> true
|
||||
_ -> false
|
||||
end)
|
||||
@@ -1346,7 +1338,7 @@ defmodule Code.Formatter do
|
||||
|
||||
defp list_interpolation_to_algebra([entry | entries], escape, state, acc, last) do
|
||||
{{:., _, [Kernel, :to_string]}, meta, [quoted]} = entry
|
||||
{doc, state} = block_to_algebra(quoted, line(meta), closing_line(meta), state)
|
||||
{doc, state} = block_to_algebra(quoted, line(meta), end_line(meta), state)
|
||||
doc = surround("\#{", doc, "}")
|
||||
list_interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
|
||||
end
|
||||
@@ -1362,8 +1354,8 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp interpolation_to_algebra([entry | entries], escape, state, acc, last) do
|
||||
{:"::", _, [{{:., _, [Kernel, :to_string]}, meta, [quoted]}, {:binary, _, _}]} = entry
|
||||
{doc, state} = block_to_algebra(quoted, line(meta), closing_line(meta), state)
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, meta, [quoted]}, {:binary, _, _}]} = entry
|
||||
{doc, state} = block_to_algebra(quoted, line(meta), end_line(meta), state)
|
||||
doc = surround("\#{", doc, "}")
|
||||
interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
|
||||
end
|
||||
@@ -1375,36 +1367,36 @@ defmodule Code.Formatter do
|
||||
## Sigils
|
||||
|
||||
defp maybe_sigil_to_algebra(fun, meta, args, state) do
|
||||
with <<"sigil_", name>> <- Atom.to_string(fun),
|
||||
[{:<<>>, _, entries}, modifiers] when is_list(modifiers) <- args,
|
||||
opening_delimiter when not is_nil(opening_delimiter) <- meta[:delimiter] do
|
||||
doc = <<?~, name, opening_delimiter::binary>>
|
||||
case {Atom.to_string(fun), args} do
|
||||
{<<"sigil_", name>>, [{:<<>>, _, entries}, modifiers]} ->
|
||||
opening_terminator = Keyword.fetch!(meta, :terminator)
|
||||
doc = <<?~, name, opening_terminator::binary>>
|
||||
|
||||
if opening_delimiter in [@double_heredoc, @single_heredoc] do
|
||||
closing_delimiter = concat(opening_delimiter, List.to_string(modifiers))
|
||||
if opening_terminator in [@double_heredoc, @single_heredoc] do
|
||||
closing_terminator = concat(opening_terminator, List.to_string(modifiers))
|
||||
|
||||
{doc, state} =
|
||||
entries
|
||||
|> prepend_heredoc_line()
|
||||
|> interpolation_to_algebra(:heredoc, state, doc, closing_delimiter)
|
||||
{doc, state} =
|
||||
entries
|
||||
|> prepend_heredoc_line()
|
||||
|> interpolation_to_algebra(:heredoc, state, doc, closing_terminator)
|
||||
|
||||
{force_unfit(doc), state}
|
||||
else
|
||||
escape = closing_sigil_terminator(opening_terminator)
|
||||
closing_terminator = concat(escape, List.to_string(modifiers))
|
||||
interpolation_to_algebra(entries, escape, state, doc, closing_terminator)
|
||||
end
|
||||
|
||||
{force_unfit(doc), state}
|
||||
else
|
||||
escape = closing_sigil_delimiter(opening_delimiter)
|
||||
closing_delimiter = concat(escape, List.to_string(modifiers))
|
||||
interpolation_to_algebra(entries, escape, state, doc, closing_delimiter)
|
||||
end
|
||||
else
|
||||
_ ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
defp closing_sigil_delimiter("("), do: ")"
|
||||
defp closing_sigil_delimiter("["), do: "]"
|
||||
defp closing_sigil_delimiter("{"), do: "}"
|
||||
defp closing_sigil_delimiter("<"), do: ">"
|
||||
defp closing_sigil_delimiter(other) when other in ["\"", "'", "|", "/"], do: other
|
||||
defp closing_sigil_terminator("("), do: ")"
|
||||
defp closing_sigil_terminator("["), do: "]"
|
||||
defp closing_sigil_terminator("{"), do: "}"
|
||||
defp closing_sigil_terminator("<"), do: ">"
|
||||
defp closing_sigil_terminator(other) when other in ["\"", "'", "|", "/"], do: other
|
||||
|
||||
## Bitstrings
|
||||
|
||||
@@ -1431,7 +1423,7 @@ defmodule Code.Formatter do
|
||||
{bitstring_wrap_parens(doc, i, last), state}
|
||||
end
|
||||
|
||||
defp bitstring_segment_to_algebra({{:"::", _, [segment, spec]}, i}, state, last) do
|
||||
defp bitstring_segment_to_algebra({{:::, _, [segment, spec]}, i}, state, last) do
|
||||
{doc, state} = quoted_to_algebra(segment, :parens_arg, state)
|
||||
{spec, state} = bitstring_spec_to_algebra(spec, state)
|
||||
|
||||
@@ -1550,40 +1542,43 @@ defmodule Code.Formatter do
|
||||
|
||||
defp integer_to_algebra(text) do
|
||||
case text do
|
||||
<<?0, ?x, rest::binary>> ->
|
||||
"0x" <> String.upcase(rest)
|
||||
[?0, ?x | rest] ->
|
||||
"0x" <> String.upcase(List.to_string(rest))
|
||||
|
||||
<<?0, base, _::binary>> = digits when base in [?b, ?o] ->
|
||||
digits
|
||||
[?0, base | _rest] = digits when base in [?b, ?o] ->
|
||||
List.to_string(digits)
|
||||
|
||||
<<??, _::binary>> = char ->
|
||||
char
|
||||
[?? | _rest] = char ->
|
||||
List.to_string(char)
|
||||
|
||||
decimal ->
|
||||
insert_underscores(decimal)
|
||||
List.to_string(insert_underscores(decimal))
|
||||
end
|
||||
end
|
||||
|
||||
defp float_to_algebra(text) do
|
||||
[int_part, decimal_part] = :binary.split(text, ".")
|
||||
decimal_part = String.downcase(decimal_part)
|
||||
insert_underscores(int_part) <> "." <> decimal_part
|
||||
{int_part, [?. | decimal_part]} = Enum.split_while(text, &(&1 != ?.))
|
||||
|
||||
decimal_part =
|
||||
decimal_part
|
||||
|> List.to_string()
|
||||
|> String.downcase()
|
||||
|
||||
List.to_string(insert_underscores(int_part)) <> "." <> decimal_part
|
||||
end
|
||||
|
||||
defp insert_underscores(digits) do
|
||||
cond do
|
||||
digits =~ "_" ->
|
||||
?_ in digits ->
|
||||
digits
|
||||
|
||||
byte_size(digits) >= 6 ->
|
||||
length(digits) >= 6 ->
|
||||
digits
|
||||
|> String.to_charlist()
|
||||
|> Enum.reverse()
|
||||
|> Enum.chunk_every(3)
|
||||
|> Enum.intersperse('_')
|
||||
|> List.flatten()
|
||||
|> Enum.reverse()
|
||||
|> List.to_string()
|
||||
|
||||
true ->
|
||||
digits
|
||||
@@ -1629,9 +1624,9 @@ defmodule Code.Formatter do
|
||||
|
||||
defp args_to_algebra_with_comments(args, meta, skip_parens?, last_arg_mode, join, state, fun) do
|
||||
min_line = line(meta)
|
||||
max_line = closing_line(meta)
|
||||
max_line = end_line(meta)
|
||||
|
||||
arg_to_algebra = fn arg, args, state ->
|
||||
arg_to_algebra = fn arg, args, newlines, state ->
|
||||
{doc, state} = fun.(arg, state)
|
||||
|
||||
doc =
|
||||
@@ -1642,7 +1637,7 @@ defmodule Code.Formatter do
|
||||
[] when last_arg_mode == :none -> doc
|
||||
end
|
||||
|
||||
{{doc, @empty, 1}, state}
|
||||
{doc, @empty, newlines, state}
|
||||
end
|
||||
|
||||
# If skipping parens, we cannot extract the comments of the first
|
||||
@@ -1650,15 +1645,15 @@ defmodule Code.Formatter do
|
||||
{args, acc, state} =
|
||||
case args do
|
||||
[head | tail] when skip_parens? ->
|
||||
{doc_triplet, state} = arg_to_algebra.(head, tail, state)
|
||||
{tail, [doc_triplet], state}
|
||||
{head, next_line, newlines, state} = arg_to_algebra.(head, tail, 1, state)
|
||||
{tail, [{head, next_line, newlines}], state}
|
||||
|
||||
_ ->
|
||||
{args, [], state}
|
||||
end
|
||||
|
||||
{args_docs, comments?, state} =
|
||||
quoted_to_algebra_with_comments(args, acc, min_line, max_line, state, arg_to_algebra)
|
||||
quoted_to_algebra_with_comments(args, acc, min_line, max_line, 1, state, arg_to_algebra)
|
||||
|
||||
cond do
|
||||
args_docs == [] ->
|
||||
@@ -1830,7 +1825,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp clause_to_algebra({:->, meta, [[], body]}, _min_line, state) do
|
||||
{body_doc, state} = block_to_algebra(body, line(meta), closing_line(meta), state)
|
||||
{body_doc, state} = block_to_algebra(body, line(meta), end_line(meta), state)
|
||||
{"() ->" |> glue(body_doc) |> nest(2), state}
|
||||
end
|
||||
|
||||
@@ -1841,7 +1836,7 @@ defmodule Code.Formatter do
|
||||
{args_doc, state} = clause_args_to_algebra(args, min_line, state)
|
||||
|
||||
state = %{state | operand_nesting: nesting}
|
||||
{body_doc, state} = block_to_algebra(body, min_line, closing_line(meta), state)
|
||||
{body_doc, state} = block_to_algebra(body, min_line, end_line(meta), state)
|
||||
|
||||
doc =
|
||||
args_doc
|
||||
@@ -1854,7 +1849,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp add_max_line_to_last_clause([{op, meta, args}], max_line) do
|
||||
[{op, [closing: [line: max_line]] ++ meta, args}]
|
||||
[{op, [end_line: max_line] ++ meta, args}]
|
||||
end
|
||||
|
||||
defp add_max_line_to_last_clause([clause | clauses], max_line) do
|
||||
@@ -1862,13 +1857,13 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp clause_args_to_algebra(args, min_line, state) do
|
||||
arg_to_algebra = fn arg, _args, state ->
|
||||
arg_to_algebra = fn arg, _args, newlines, state ->
|
||||
{doc, state} = clause_args_to_algebra(arg, state)
|
||||
{{doc, @empty, 1}, state}
|
||||
{doc, @empty, newlines, state}
|
||||
end
|
||||
|
||||
{args_docs, comments?, state} =
|
||||
quoted_to_algebra_with_comments([args], [], min_line, @min_line, state, arg_to_algebra)
|
||||
quoted_to_algebra_with_comments([args], [], min_line, @min_line, 1, state, arg_to_algebra)
|
||||
|
||||
if comments? do
|
||||
{Enum.reduce(args_docs, &line(&2, &1)), state}
|
||||
@@ -1896,75 +1891,65 @@ defmodule Code.Formatter do
|
||||
|
||||
## Quoted helpers for comments
|
||||
|
||||
defp quoted_to_algebra_with_comments(args, acc, min_line, max_line, state, fun) do
|
||||
defp quoted_to_algebra_with_comments(args, acc, min_line, max_line, newlines, state, fun) do
|
||||
{pre_comments, state} =
|
||||
get_and_update_in(state.comments, fn comments ->
|
||||
Enum.split_while(comments, fn {line, _, _} -> line <= min_line end)
|
||||
end)
|
||||
|
||||
{docs, comments?, state} =
|
||||
each_quoted_to_algebra_with_comments(args, acc, max_line, state, false, fun)
|
||||
each_quoted_to_algebra_with_comments(args, acc, max_line, newlines, state, false, fun)
|
||||
|
||||
{docs, comments?, update_in(state.comments, &(pre_comments ++ &1))}
|
||||
end
|
||||
|
||||
defp each_quoted_to_algebra_with_comments([], acc, max_line, state, comments?, _fun) do
|
||||
{acc, comments, comments?} = extract_comments_before(max_line, acc, state.comments, comments?)
|
||||
args_docs = merge_algebra_with_comments(Enum.reverse(acc), @empty)
|
||||
{args_docs, comments?, %{state | comments: comments}}
|
||||
defp each_quoted_to_algebra_with_comments([], acc, max_line, _newlines, state, comments?, _fun) do
|
||||
%{comments: comments} = state
|
||||
{current, comments} = Enum.split_with(comments, fn {line, _, _} -> line < max_line end)
|
||||
|
||||
extra = for {_, {previous, _}, doc} <- current, do: {doc, @empty, previous}
|
||||
args_docs = merge_algebra_with_comments(Enum.reverse(acc, extra), @empty)
|
||||
{args_docs, comments? or extra != [], %{state | comments: comments}}
|
||||
end
|
||||
|
||||
defp each_quoted_to_algebra_with_comments([arg | args], acc, max_line, state, comments?, fun) do
|
||||
defp each_quoted_to_algebra_with_comments(args, acc, max_line, newlines, state, comments?, fun) do
|
||||
[arg | args] = args
|
||||
{doc_start, doc_end} = traverse_line(arg, {@max_line, @min_line})
|
||||
|
||||
{acc, comments, comments?} =
|
||||
extract_comments_before(doc_start, acc, state.comments, comments?)
|
||||
{doc_newlines, acc, comments, comments?} =
|
||||
extract_comments_before(doc_start, newlines, acc, state.comments, comments?)
|
||||
|
||||
{doc_triplet, state} = fun.(arg, args, %{state | comments: comments})
|
||||
{doc, next_line, doc_newlines, state} =
|
||||
fun.(arg, args, doc_newlines, %{state | comments: comments})
|
||||
|
||||
{acc, comments, comments?} =
|
||||
extract_comments_trailing(doc_start, doc_end, acc, state.comments, comments?)
|
||||
{doc_newlines, acc, comments, comments?} =
|
||||
extract_comments_trailing(doc_start, doc_end, doc_newlines, acc, state.comments, comments?)
|
||||
|
||||
acc = [adjust_trailing_newlines(doc_triplet, doc_end, comments) | acc]
|
||||
acc = [{doc, next_line, doc_newlines} | acc]
|
||||
state = %{state | comments: comments}
|
||||
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
|
||||
each_quoted_to_algebra_with_comments(args, acc, max_line, newlines, state, comments?, fun)
|
||||
end
|
||||
|
||||
defp extract_comments_before(max, acc, [{line, _, _} = comment | rest], _) when line < max do
|
||||
defp extract_comments_before(max, _, acc, [{line, _, _} = comment | rest], _) when line < max do
|
||||
{_, {previous, next}, doc} = comment
|
||||
acc = [{doc, @empty, next} | add_previous_to_acc(acc, previous)]
|
||||
extract_comments_before(max, acc, rest, true)
|
||||
acc = [{doc, @empty, previous} | acc]
|
||||
extract_comments_before(max, next, acc, rest, true)
|
||||
end
|
||||
|
||||
defp extract_comments_before(_max, acc, rest, comments?) do
|
||||
{acc, rest, comments?}
|
||||
defp extract_comments_before(_max, newlines, acc, rest, comments?) do
|
||||
{newlines, acc, rest, comments?}
|
||||
end
|
||||
|
||||
defp add_previous_to_acc([{doc, next_line, newlines} | acc], previous) when newlines < previous,
|
||||
do: [{doc, next_line, previous} | acc]
|
||||
|
||||
defp add_previous_to_acc(acc, _previous),
|
||||
do: acc
|
||||
|
||||
defp extract_comments_trailing(min, max, acc, [{line, _, doc_comment} | rest], _)
|
||||
defp extract_comments_trailing(min, max, newlines, acc, [{line, _, doc_comment} | rest], _)
|
||||
when line >= min and line <= max do
|
||||
acc = [{doc_comment, @empty, 1} | acc]
|
||||
extract_comments_trailing(min, max, acc, rest, true)
|
||||
acc = [{doc_comment, @empty, newlines} | acc]
|
||||
extract_comments_trailing(min, max, 1, acc, rest, true)
|
||||
end
|
||||
|
||||
defp extract_comments_trailing(_min, _max, acc, rest, comments?) do
|
||||
{acc, rest, comments?}
|
||||
defp extract_comments_trailing(_min, _max, newlines, acc, rest, comments?) do
|
||||
{newlines, acc, rest, comments?}
|
||||
end
|
||||
|
||||
# If the document is immediately followed by comment which is followed by newlines,
|
||||
# its newlines wouldn't have considerd the comment, so we need to adjust it.
|
||||
defp adjust_trailing_newlines({doc, next_line, newlines}, doc_end, [{line, _, _} | _])
|
||||
when newlines > 1 and line == doc_end + 1 do
|
||||
{doc, next_line, 1}
|
||||
end
|
||||
|
||||
defp adjust_trailing_newlines(doc_triplet, _, _), do: doc_triplet
|
||||
|
||||
defp traverse_line({expr, meta, args}, {min, max}) do
|
||||
acc =
|
||||
case Keyword.fetch(meta, :line) do
|
||||
@@ -1994,8 +1979,8 @@ defmodule Code.Formatter do
|
||||
# (except for module attributes)
|
||||
# 3. empty lines are collapsed as to not exceed more than one
|
||||
#
|
||||
defp merge_algebra_with_comments([{doc, next_line, newlines} | docs], left) do
|
||||
right = if newlines >= @newlines, do: line(), else: next_line
|
||||
defp merge_algebra_with_comments([{doc, next_line, _newlines} | docs], left) do
|
||||
right = next_line_separator(docs, next_line)
|
||||
|
||||
doc =
|
||||
if left != @empty do
|
||||
@@ -2036,7 +2021,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
# TODO: We can remove this workaround once we remove
|
||||
# ?rearrange_uop from the parser on v2.0.
|
||||
# ?rearrange_uop from the parser in Elixir v2.0.
|
||||
defp wrap_in_parens_if_operator(doc, {:__block__, _, [expr]}) do
|
||||
wrap_in_parens_if_operator(doc, expr)
|
||||
end
|
||||
@@ -2076,6 +2061,14 @@ defmodule Code.Formatter do
|
||||
end)
|
||||
end
|
||||
|
||||
defp next_line_separator([{_doc, _next_line, newlines} | _], next_line) do
|
||||
if newlines >= @newlines, do: line(), else: next_line
|
||||
end
|
||||
|
||||
defp next_line_separator([], _) do
|
||||
line()
|
||||
end
|
||||
|
||||
defp module_attribute_read?({:@, _, [{var, _, var_context}]})
|
||||
when is_atom(var) and is_atom(var_context) do
|
||||
Code.Identifier.classify(var) == :callable_local
|
||||
@@ -2130,12 +2123,12 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp next_break_fits?({:<<>>, meta, [_ | _] = entries}, state) do
|
||||
meta[:delimiter] == ~s["""] or
|
||||
meta[:format] == :bin_heredoc or
|
||||
(not interpolated?(entries) and eol_or_comments?(meta, state))
|
||||
end
|
||||
|
||||
defp next_break_fits?({{:., _, [List, :to_charlist]}, meta, [[_ | _]]}, _state) do
|
||||
meta[:delimiter] == ~s[''']
|
||||
meta[:format] == :list_heredoc
|
||||
end
|
||||
|
||||
defp next_break_fits?({{:., _, [_left, :{}]}, _, _}, _state) do
|
||||
@@ -2143,11 +2136,11 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp next_break_fits?({:__block__, meta, [string]}, _state) when is_binary(string) do
|
||||
meta[:delimiter] == ~s["""]
|
||||
meta[:format] == :bin_heredoc
|
||||
end
|
||||
|
||||
defp next_break_fits?({:__block__, meta, [list]}, _state) when is_list(list) do
|
||||
meta[:delimeter] != ~s[']
|
||||
meta[:format] != :charlist
|
||||
end
|
||||
|
||||
defp next_break_fits?({form, _, [_ | _]}, _state) when form in [:fn, :%{}, :%] do
|
||||
@@ -2155,7 +2148,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp next_break_fits?({fun, meta, args}, _state) when is_atom(fun) and is_list(args) do
|
||||
meta[:delimiter] in [@double_heredoc, @single_heredoc] and
|
||||
meta[:terminator] in [@double_heredoc, @single_heredoc] and
|
||||
fun |> Atom.to_string() |> String.starts_with?("sigil_")
|
||||
end
|
||||
|
||||
@@ -2171,47 +2164,23 @@ defmodule Code.Formatter do
|
||||
eol?(meta) or
|
||||
(
|
||||
min_line = line(meta)
|
||||
max_line = closing_line(meta)
|
||||
max_line = end_line(meta)
|
||||
Enum.any?(comments, fn {line, _, _} -> line > min_line and line < max_line end)
|
||||
)
|
||||
end
|
||||
|
||||
# A literal list is a keyword or (... -> ...)
|
||||
defp last_arg_to_keyword([_ | _] = arg, _list_to_keyword?, _skip_parens?, _comments) do
|
||||
defp last_arg_to_keyword([_ | _] = arg, _list_to_keyword?) do
|
||||
{keyword?(arg), arg}
|
||||
end
|
||||
|
||||
# This is a list of tuples, it can be converted to keywords.
|
||||
defp last_arg_to_keyword(
|
||||
{:__block__, meta, [[_ | _] = arg]} = block,
|
||||
true,
|
||||
skip_parens?,
|
||||
comments
|
||||
) do
|
||||
cond do
|
||||
not keyword?(arg) ->
|
||||
{false, block}
|
||||
|
||||
skip_parens? ->
|
||||
block_line = line(meta)
|
||||
{{_, arg_meta, _}, _} = hd(arg)
|
||||
first_line = line(arg_meta)
|
||||
|
||||
case Enum.drop_while(comments, fn {line, _, _} -> line <= block_line end) do
|
||||
[{line, _, _} | _] when line <= first_line ->
|
||||
{false, block}
|
||||
|
||||
_ ->
|
||||
{true, arg}
|
||||
end
|
||||
|
||||
true ->
|
||||
{true, arg}
|
||||
end
|
||||
defp last_arg_to_keyword({:__block__, _, [[_ | _] = arg]} = block, true) do
|
||||
if keyword?(arg), do: {true, arg}, else: {false, block}
|
||||
end
|
||||
|
||||
# Otherwise we don't have a keyword.
|
||||
defp last_arg_to_keyword(arg, _list_to_keyword?, _skip_parens?, _comments) do
|
||||
defp last_arg_to_keyword(arg, _list_to_keyword?) do
|
||||
{false, arg}
|
||||
end
|
||||
|
||||
@@ -2243,36 +2212,36 @@ defmodule Code.Formatter do
|
||||
if force_args?(arg), do: force_unfit(doc), else: doc
|
||||
end
|
||||
|
||||
defp keyword?([{_, _} | list]), do: keyword?(list)
|
||||
defp keyword?(rest), do: rest == []
|
||||
|
||||
defp keyword_key?({:__block__, meta, [atom]}) when is_atom(atom),
|
||||
do: meta[:format] == :keyword
|
||||
|
||||
defp keyword_key?({{:., _, [:erlang, :binary_to_atom]}, meta, [{:<<>>, _, _}, :utf8]}),
|
||||
do: meta[:format] == :keyword
|
||||
|
||||
defp keyword_key?(_),
|
||||
do: false
|
||||
|
||||
defp eol?(meta) do
|
||||
Keyword.get(meta, :newlines, 0) > 0
|
||||
defp keyword?([{key, _} | list]) do
|
||||
keyword_key?(key) and keyword?(list)
|
||||
end
|
||||
|
||||
defp meta?(meta, key) do
|
||||
is_list(meta[key])
|
||||
defp keyword?(rest) do
|
||||
rest == []
|
||||
end
|
||||
|
||||
defp keyword_key?({:__block__, meta, [_]}) do
|
||||
meta[:format] == :keyword
|
||||
end
|
||||
|
||||
defp keyword_key?({{:., _, [:erlang, :binary_to_atom]}, _, [{:<<>>, meta, _}, :utf8]}) do
|
||||
meta[:format] == :keyword
|
||||
end
|
||||
|
||||
defp keyword_key?(_) do
|
||||
false
|
||||
end
|
||||
|
||||
defp eol?(meta) do
|
||||
Keyword.get(meta, :eol, false)
|
||||
end
|
||||
|
||||
defp line(meta) do
|
||||
meta[:line] || @max_line
|
||||
Keyword.get(meta, :line, @max_line)
|
||||
end
|
||||
|
||||
defp end_line(meta) do
|
||||
meta[:end][:line] || @min_line
|
||||
end
|
||||
|
||||
defp closing_line(meta) do
|
||||
meta[:closing][:line] || @min_line
|
||||
Keyword.get(meta, :end_line, @min_line)
|
||||
end
|
||||
|
||||
## Algebra helpers
|
||||
|
||||
@@ -34,7 +34,7 @@ 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, 130}
|
||||
@@ -60,13 +60,9 @@ 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 (for example, `:<<>>` is not callable because `Foo.<<>>` is a
|
||||
syntax error); this category includes atoms like `:Foo`, since they are
|
||||
@@ -84,9 +80,6 @@ defmodule Code.Identifier do
|
||||
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :->] ->
|
||||
:not_callable
|
||||
|
||||
atom in [:"::"] ->
|
||||
:not_atomable
|
||||
|
||||
unary_op(atom) != :error or binary_op(atom) != :error ->
|
||||
:callable_operator
|
||||
|
||||
@@ -150,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
|
||||
@@ -179,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 ->
|
||||
|
||||
@@ -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
|
||||
@@ -28,7 +28,7 @@ defmodule Code.Typespec do
|
||||
end
|
||||
|
||||
def spec_to_quoted(name, {:type, line, :fun, []}) when is_atom(name) do
|
||||
{:"::", [line: line], [{name, [line: line], []}, quote(do: term)]}
|
||||
{:::, [line: line], [{name, [line: line], []}, quote(do: term)]}
|
||||
end
|
||||
|
||||
def spec_to_quoted(name, {:type, line, :bounded_fun, [type, constrs]}) when is_atom(name) do
|
||||
@@ -52,7 +52,7 @@ defmodule Code.Typespec do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
|
||||
when_args = [
|
||||
{:"::", meta, [{name, [line: line], args}, typespec_to_quoted(result)]},
|
||||
{:::, meta, [{name, [line: line], args}, typespec_to_quoted(result)]},
|
||||
guards ++ vars
|
||||
]
|
||||
|
||||
@@ -217,8 +217,7 @@ defmodule Code.Typespec do
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:user_type, line, name, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
{name, [line: line], args}
|
||||
typespec_to_quoted({:type, line, name, args})
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, line, :tuple, :any}) do
|
||||
@@ -330,7 +329,7 @@ defmodule Code.Typespec do
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:var, line, var}) do
|
||||
{erl_to_ex_var(var), [line: line], nil}
|
||||
{erl_to_ex_var(var), line, nil}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:op, line, op, arg}) do
|
||||
@@ -342,7 +341,7 @@ defmodule Code.Typespec do
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:ann_type, line, [var, type]}) do
|
||||
{:"::", [line: line], [typespec_to_quoted(var), typespec_to_quoted(type)]}
|
||||
{:::, [line: line], [typespec_to_quoted(var), typespec_to_quoted(type)]}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted(
|
||||
|
||||
@@ -21,9 +21,9 @@ defprotocol Collectable do
|
||||
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
|
||||
|
||||
@@ -37,7 +37,7 @@ 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
|
||||
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`.
|
||||
|
||||
@@ -143,7 +143,7 @@ end
|
||||
defimpl Collectable, for: Map do
|
||||
def into(original) do
|
||||
fun = fn
|
||||
map, {:cont, {k, v}} -> Map.put(map, k, v)
|
||||
map, {:cont, {k, v}} -> :maps.put(k, v, map)
|
||||
map, :done -> map
|
||||
_, :halt -> :ok
|
||||
end
|
||||
|
||||
@@ -1,266 +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 "#{Mix.env()}.exs"
|
||||
|
||||
`import Config` will import the functions `config/2`, `config/3`
|
||||
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 "#{Mix.env()}.exs"` will import other
|
||||
config files, based on the current Mix 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. For more information,
|
||||
read our [library guidelines](library-guidelines.html).
|
||||
|
||||
## 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 two 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
|
||||
|
||||
## config/releases.exs
|
||||
|
||||
If you are using releases, see `mix release`, there is another configuration
|
||||
file called `config/releases.exs`. While `config/config.exs` and friends
|
||||
mentioned in the previous section are executed whenever you run a Mix
|
||||
command, including when you assemble a release, `config/releases.exs` is
|
||||
executed every time your production system boots. Since Mix is not available
|
||||
in a production system, `config/releases.exs` must not use any of the
|
||||
functions from Mix.
|
||||
"""
|
||||
|
||||
@config_key {__MODULE__, :config}
|
||||
@files_key {__MODULE__, :files}
|
||||
|
||||
defp get_config!() do
|
||||
Process.get(@config_key) || raise_improper_use!()
|
||||
end
|
||||
|
||||
defp put_config(value) do
|
||||
Process.put(@config_key, value)
|
||||
end
|
||||
|
||||
defp delete_config() do
|
||||
Process.delete(@config_key)
|
||||
end
|
||||
|
||||
defp get_files!() do
|
||||
Process.get(@files_key) || raise_improper_use!()
|
||||
end
|
||||
|
||||
defp put_files(value) do
|
||||
Process.put(@files_key, value)
|
||||
end
|
||||
|
||||
defp delete_files() do
|
||||
Process.delete(@files_key)
|
||||
end
|
||||
|
||||
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`. 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`. 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 ~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 "#{Mix.env()}.exs"
|
||||
|
||||
"""
|
||||
@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
|
||||
current_files = get_files!()
|
||||
|
||||
if file in current_files do
|
||||
raise ArgumentError,
|
||||
"attempting to load configuration #{Path.relative_to_cwd(file)} recursively"
|
||||
end
|
||||
|
||||
put_files([file | current_files])
|
||||
Code.eval_file(file)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec __eval__!(Path.t(), [Path.t()]) :: {keyword, [Path.t()]}
|
||||
def __eval__!(file, imported_paths \\ []) when is_binary(file) and is_list(imported_paths) do
|
||||
previous_config = put_config([])
|
||||
previous_files = put_files(imported_paths)
|
||||
|
||||
try do
|
||||
{eval_config, _} = __import__!(Path.expand(file))
|
||||
|
||||
case get_config!() do
|
||||
[] when is_list(eval_config) ->
|
||||
{validate!(eval_config, file), get_files!()}
|
||||
|
||||
pdict_config ->
|
||||
{pdict_config, get_files!()}
|
||||
end
|
||||
after
|
||||
if previous_config, do: put_config(previous_config), else: delete_config()
|
||||
if previous_files, do: put_files(previous_files), else: delete_files()
|
||||
end
|
||||
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,377 +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`.
|
||||
|
||||
## Sample config provider
|
||||
|
||||
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
|
||||
def init(path) when is_binary(path), do: path
|
||||
|
||||
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"}]
|
||||
]
|
||||
]
|
||||
|
||||
Now once the system boots, it will invoke the provider early in
|
||||
the boot process, save the merged configuration to the disk, and
|
||||
reboot the system with the new values in place.
|
||||
"""
|
||||
|
||||
@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_after_boot: false,
|
||||
reboot_after_config: true,
|
||||
validate_compile_env: false
|
||||
]
|
||||
|
||||
@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
|
||||
|
||||
@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)}
|
||||
struct!(%Config.Provider{config_path: config_path, providers: providers}, opts)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def boot(app, key, restart_fun \\ &restart_and_sleep/0) do
|
||||
# The app with the config provider settings may not
|
||||
# have been loaded at this point, so make sure we load
|
||||
# its environment before querying it.
|
||||
_ = :application.load(app)
|
||||
|
||||
# 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)
|
||||
|
||||
# The key we store if the system already booted
|
||||
booted_key = :"#{key}_booted"
|
||||
|
||||
case :application.get_env(app, booted_key) do
|
||||
{:ok, {:booted, path}} ->
|
||||
path && File.rm(path)
|
||||
|
||||
with {:ok, %Config.Provider{} = provider} <- :application.get_env(app, key) do
|
||||
maybe_validate_compile_env(provider)
|
||||
end
|
||||
|
||||
:booted
|
||||
|
||||
_ ->
|
||||
case :application.get_env(app, key) do
|
||||
{:ok, %Config.Provider{} = provider} ->
|
||||
path = resolve_config_path!(provider.config_path)
|
||||
reboot_config = [{app, [{booted_key, booted_value(provider, path)}]}]
|
||||
boot_providers(path, provider, reboot_config, restart_fun)
|
||||
|
||||
_ ->
|
||||
:skip
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp boot_providers(path, provider, reboot_config, restart_fun) do
|
||||
validate_no_cyclic_boot!(path)
|
||||
loaded_applications = :application.loaded_applications()
|
||||
original_config = read_config!(path)
|
||||
|
||||
config =
|
||||
original_config
|
||||
|> Config.__merge__(provider.extra_config)
|
||||
|> run_providers(provider)
|
||||
|
||||
if provider.reboot_after_config do
|
||||
config
|
||||
|> Config.__merge__(reboot_config)
|
||||
|> write_config!(path)
|
||||
|
||||
restart_fun.()
|
||||
else
|
||||
for {app, _, _} <- loaded_applications, config[app] != original_config[app] do
|
||||
abort("""
|
||||
Cannot configure #{inspect(app)} because :reboot_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) do
|
||||
for {app, [key | path], compile_return} <- compile_env,
|
||||
Application.ensure_loaded(app) == :ok 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 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)
|
||||
|
||||
defp restart_and_sleep do
|
||||
:init.restart()
|
||||
Process.sleep(:infinity)
|
||||
end
|
||||
|
||||
defp booted_value(%{prune_after_boot: true}, path), do: {:booted, path}
|
||||
defp booted_value(%{prune_after_boot: false}, _path), do: {:booted, nil}
|
||||
|
||||
defp validate_no_cyclic_boot!(path) do
|
||||
if System.get_env("ELIXIR_CONFIG_PROVIDER_BOOTED") do
|
||||
bad_path_abort("Got infinite loop when running Config.Provider", path)
|
||||
else
|
||||
System.put_env("ELIXIR_CONFIG_PROVIDER_BOOTED", "1")
|
||||
end
|
||||
end
|
||||
|
||||
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,100 +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`. 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 configure your `mix release` as:
|
||||
|
||||
config_providers: [{Config.Reader, "/etc/config.exs"}]
|
||||
|
||||
Or if you want to read a custom path inside the release:
|
||||
|
||||
config_provider: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
|
||||
|
||||
Note by default Mix releases supports runtime configuration via
|
||||
a `config/releases.exs`. If a `config/releases.exs` exists in your
|
||||
application, it is automatically copied inside the release and
|
||||
automatically set as a config provider.
|
||||
"""
|
||||
|
||||
@behaviour Config.Provider
|
||||
|
||||
@impl true
|
||||
def init(path) do
|
||||
Config.Provider.validate_config_path!(path)
|
||||
path
|
||||
end
|
||||
|
||||
@impl true
|
||||
def load(config, path) do
|
||||
merge(config, path |> Config.Provider.resolve_config_path!() |> read!())
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads the configuration file.
|
||||
|
||||
The same as `read_imports!/2` but only returns the configuration
|
||||
in the given file, without returning the imported paths.
|
||||
|
||||
It exists for convenience purposes. For example, you could
|
||||
invoke it inside your `mix.exs` to read some external data
|
||||
you decided to move to a configuration file:
|
||||
|
||||
releases: Config.Reader.read!("rel/releases.exs")
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec read!(Path.t(), [Path.t()]) :: keyword
|
||||
def read!(file, imported_paths \\ [])
|
||||
when is_binary(file) and is_list(imported_paths) do
|
||||
Config.__eval__!(file, imported_paths) |> elem(0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads the given configuration file alongside its imports.
|
||||
|
||||
It accepts a list of `imported_paths` that should raise if attempted
|
||||
to be imported again (to avoid recursive imports).
|
||||
|
||||
It returns a tuple with the configuration and the imported paths.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec read_imports!(Path.t(), [Path.t()]) :: {keyword, [Path.t()]}
|
||||
def read_imports!(file, imported_paths \\ [])
|
||||
when is_binary(file) and is_list(imported_paths) do
|
||||
Config.__eval__!(file, imported_paths)
|
||||
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,6 +18,8 @@ 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
|
||||
|
||||
@@ -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
|
||||
@@ -148,9 +149,12 @@ defmodule DynamicSupervisor do
|
||||
}
|
||||
|
||||
@typedoc "Option values used by the `start*` functions"
|
||||
@type option :: GenServer.option()
|
||||
@type option :: {:name, Supervisor.name()} | init_option()
|
||||
|
||||
@typedoc "Options given to `start_link/1` and `init/1`"
|
||||
@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
|
||||
@@ -475,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.
|
||||
|
||||
@@ -489,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
|
||||
@@ -747,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
|
||||
|
||||
@@ -1003,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
|
||||
|
||||
@@ -1049,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
|
||||
|
||||
+280
-650
File diff suppressed because it is too large
Load Diff
+34
-86
@@ -208,7 +208,7 @@ 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)
|
||||
scope = :elixir_erl.scope(meta)
|
||||
|
||||
{erl_args, scope} =
|
||||
:elixir_erl_clauses.match(&:elixir_erl_pass.translate_args/2, ex_args, scope)
|
||||
@@ -544,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
|
||||
|
||||
@@ -1062,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
|
||||
@@ -1109,46 +1098,37 @@ defmodule FunctionClauseError do
|
||||
|
||||
mfa = Exception.format_mfa(module, function, arity)
|
||||
|
||||
format_clause_fun = fn {args, guards} ->
|
||||
code = Enum.reduce(guards, {function, [], args}, &{:when, [], [&2, &1]})
|
||||
" #{kind} " <> Macro.to_string(code, ast_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 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
|
||||
@@ -1168,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
|
||||
|
||||
@@ -1194,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 ->
|
||||
""
|
||||
@@ -1339,24 +1305,6 @@ defmodule File.CopyError do
|
||||
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
|
||||
"" -> ""
|
||||
on -> ". #{on}"
|
||||
end
|
||||
|
||||
"could not #{exception.action} from #{inspect(exception.source)} to " <>
|
||||
"#{inspect(exception.destination)}#{location}: #{formatted}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule File.LinkError do
|
||||
defexception [:reason, :existing, :new, action: ""]
|
||||
|
||||
|
||||
+42
-80
@@ -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
|
||||
|
||||
@@ -373,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
|
||||
@@ -426,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
|
||||
@@ -536,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}})
|
||||
@@ -585,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)
|
||||
|
||||
@@ -722,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
|
||||
@@ -741,54 +731,30 @@ defmodule File do
|
||||
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
|
||||
|
||||
{:error, reason} ->
|
||||
raise File.RenameError,
|
||||
reason: reason,
|
||||
action: "rename",
|
||||
source: IO.chardata_to_string(source),
|
||||
destination: IO.chardata_to_string(destination)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Copies the contents in `source_file` to `destination_file` preserving its modes.
|
||||
|
||||
`source_file` and `destination_file` must be a file or a symbolic link to one,
|
||||
or in the case of destination, a path to a non-existent file. If either one of
|
||||
them is a directory, `{:error, :eisdir}` will be returned.
|
||||
Copies the contents in `source` to `destination` preserving its mode.
|
||||
|
||||
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`.
|
||||
|
||||
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
|
||||
@@ -805,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
|
||||
|
||||
@@ -814,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
|
||||
@@ -846,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
|
||||
|
||||
@@ -904,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} ->
|
||||
@@ -1243,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
|
||||
@@ -1310,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.
|
||||
@@ -1355,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.
|
||||
@@ -1464,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.
|
||||
@@ -1612,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` .
|
||||
@@ -1693,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.
|
||||
"""
|
||||
@@ -1721,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.
|
||||
"""
|
||||
@@ -1771,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
|
||||
|
||||
@@ -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/)
|
||||
|
||||
"""
|
||||
|
||||
@@ -305,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
|
||||
|
||||
@@ -493,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,43 +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
|
||||
|
||||
It is also possible to capture module functions and pass them around
|
||||
as if they were anonymous functions by using the capture operator `&/1`:
|
||||
|
||||
iex> add = &Kernel.+/2
|
||||
iex> add.(1, 2)
|
||||
3
|
||||
|
||||
iex> length = &String.length/1
|
||||
iex> length.("hello")
|
||||
5
|
||||
|
||||
It is also possible to capture a definition in the current module by
|
||||
skipping the module prefix, such as `&my_fun/2`.
|
||||
|
||||
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`.
|
||||
|
||||
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 ::
|
||||
@@ -166,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 """
|
||||
@@ -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
|
||||
|
||||
@@ -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,7 +54,7 @@ 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.
|
||||
@@ -63,7 +63,7 @@ defmodule GenServer do
|
||||
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 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
|
||||
|
||||
@@ -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()
|
||||
@@ -247,22 +248,6 @@ defmodule GenServer do
|
||||
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.
|
||||
|
||||
If the process has no messages waiting when the timeout is set and the
|
||||
number of given milliseconds pass without any message arriving,
|
||||
then `handle_info/2` will be called with `:timeout` as the first argument.
|
||||
The timeout is cleared if any message is waiting or arrives before the
|
||||
given timeout.
|
||||
|
||||
Because a message may arrive before the timeout is set, even a timeout of `0`
|
||||
milliseconds is not guaranteed to execute. To take another action immediately
|
||||
and unconditionally, use a `:continue` instruction.
|
||||
|
||||
## When (not) to use a GenServer
|
||||
|
||||
So far, we have learned that a `GenServer` can be used as a supervised process
|
||||
@@ -411,9 +396,9 @@ 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
|
||||
@@ -462,8 +447,8 @@ 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
|
||||
@@ -528,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
|
||||
@@ -605,9 +590,9 @@ defmodule GenServer do
|
||||
* 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.
|
||||
@@ -629,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`.
|
||||
@@ -640,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
|
||||
@@ -666,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,12 +702,7 @@ defmodule GenServer do
|
||||
@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 """
|
||||
@@ -737,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.
|
||||
|
||||
@@ -756,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
|
||||
@@ -785,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
|
||||
|
||||
@@ -852,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
|
||||
@@ -1006,8 +973,7 @@ 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]}})
|
||||
@@ -1040,9 +1006,6 @@ defmodule GenServer do
|
||||
not yet connected to the caller one, the semantics differ
|
||||
depending on the used Erlang/OTP version.
|
||||
|
||||
`server` can be any of the values described in the "Name registration"
|
||||
section of the documentation for this module.
|
||||
|
||||
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
|
||||
@@ -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
|
||||
|
||||
+26
-58
@@ -31,11 +31,12 @@ defprotocol Inspect do
|
||||
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 `">"`. All strings are
|
||||
valid algebra documents that keep their formatting when pretty printed.
|
||||
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 `">"`.
|
||||
|
||||
Since regular strings are valid entities in an algebra document,
|
||||
an implementation of the `Inspect` protocol may simply return a
|
||||
@@ -76,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
|
||||
|
||||
@@ -168,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,
|
||||
@@ -252,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)
|
||||
@@ -315,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'
|
||||
@@ -408,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, [])
|
||||
|
||||
@@ -419,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
|
||||
@@ -442,9 +430,10 @@ defimpl Inspect, for: Any do
|
||||
_ -> 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
|
||||
@@ -455,7 +444,7 @@ defimpl Inspect, for: Any do
|
||||
# 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 = Map.to_list(map) ++ ["..."]
|
||||
map = :maps.to_list(map) ++ ["..."]
|
||||
|
||||
open = color("#" <> name <> "<", :map, opts)
|
||||
sep = color(",", :map, opts)
|
||||
@@ -464,24 +453,3 @@ defimpl Inspect, for: Any do
|
||||
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,84 +21,69 @@ 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.
|
||||
|
||||
* `:inspect_fun` (since v1.9.0) - a function to build algebra documents,
|
||||
defaults to `Inspect.inspect/2`.
|
||||
|
||||
* `:limit` - limits the number of items that are inspected for tuples,
|
||||
* `: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 printable strings nor printable charlists and defaults to 50.
|
||||
If you don't want to limit the number of items to a particular number,
|
||||
use `:infinity`.
|
||||
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`.
|
||||
|
||||
* `: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`.
|
||||
|
||||
* `:pretty` - if set to `true` enables pretty printing, defaults to `false`.
|
||||
|
||||
* `: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. Defaults to 4096. If you don't want to limit the number of
|
||||
characters to a particular number, use `:infinity`.
|
||||
|
||||
* `: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.
|
||||
|
||||
* `: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
|
||||
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.
|
||||
Colors can be any `t:IO.ANSI.ansidata/0` as accepted by `IO.ANSI.format/1`.
|
||||
|
||||
* `: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`.
|
||||
|
||||
* `: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
|
||||
|
||||
* `: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
|
||||
`:number`, `:atom`, `regex`, `:tuple`, `:map`, `:list`, and `:reset`.
|
||||
Colors can be any `t:IO.ANSI.ansidata/0` as accepted by `IO.ANSI.format/1`.
|
||||
|
||||
"""
|
||||
|
||||
# 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: pos_integer | :infinity,
|
||||
pretty: boolean,
|
||||
printable_limit: pos_integer | :infinity,
|
||||
safe: boolean,
|
||||
structs: boolean,
|
||||
syntax_colors: [{color_key, IO.ANSI.ansidata()}],
|
||||
width: pos_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()}]
|
||||
}
|
||||
end
|
||||
|
||||
@@ -193,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
|
||||
@@ -251,15 +233,15 @@ 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)
|
||||
@@ -275,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
|
||||
@@ -317,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"""
|
||||
@@ -428,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(
|
||||
@@ -702,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"
|
||||
@@ -825,7 +809,7 @@ defmodule Inspect.Algebra do
|
||||
@doc ~S"""
|
||||
Inserts a mandatory linebreak between two documents.
|
||||
|
||||
See `line/0`.
|
||||
See `line/1`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -898,11 +882,10 @@ defmodule Inspect.Algebra do
|
||||
# * 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 :: integer(), column :: integer(), break? :: boolean(), entries) :: boolean()
|
||||
when entries:
|
||||
maybe_improper_list({integer(), mode(), t()}, {:tail, boolean(), entries} | [])
|
||||
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.
|
||||
|
||||
@@ -233,7 +233,7 @@ defmodule Integer 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
|
||||
@@ -244,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
|
||||
|
||||
@@ -261,17 +261,14 @@ 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
|
||||
|
||||
# TODO: Remove Integer.to_string/1 once the minimum supported version is
|
||||
# Erlang/OTP 22, since it is covered by the now BIF Integer.to_string/2.
|
||||
# Please reapply commit 2622fd6b0aa419a983a899a1fbdb5deefba3d85d.
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `integer`.
|
||||
@@ -323,9 +320,6 @@ defmodule Integer do
|
||||
:erlang.integer_to_binary(integer, base)
|
||||
end
|
||||
|
||||
# TODO: Remove Integer.to_charlist/1 once the minimum supported version is
|
||||
# Erlang/OTP 22, since it is covered by the now BIF Integer.to_charlist/2.
|
||||
# Please reapply commit 2622fd6b0aa419a983a899a1fbdb5deefba3d85d.
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation of the given `integer`.
|
||||
|
||||
@@ -405,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
|
||||
@@ -414,10 +409,12 @@ defmodule Integer do
|
||||
defp gcd_positive(integer1, 0), do: integer1
|
||||
defp gcd_positive(integer1, integer2), do: gcd_positive(integer2, rem(integer1, integer2))
|
||||
|
||||
# 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)
|
||||
|
||||
+45
-161
@@ -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,100 +32,17 @@ 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 in `0..255`)
|
||||
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 in the `0..255` range, while Unicode code points
|
||||
(`t:char/0`) are integers in the range `0..0x10FFFF`. 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:
|
||||
|
||||
iex> 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_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`.
|
||||
@@ -148,7 +68,7 @@ defmodule IO do
|
||||
def read(device \\ :stdio, line_or_chars)
|
||||
|
||||
def read(device, :all) do
|
||||
do_read_all(map_dev(device), :empty)
|
||||
do_read_all(map_dev(device), "")
|
||||
end
|
||||
|
||||
def read(device, :line) do
|
||||
@@ -161,27 +81,12 @@ defmodule IO do
|
||||
|
||||
defp do_read_all(mapped_dev, acc) do
|
||||
case :io.get_line(mapped_dev, "") do
|
||||
line when is_binary(line) or is_list(line) -> do_read_all(mapped_dev, concat(acc, line))
|
||||
:eof -> read_eof(mapped_dev, acc)
|
||||
line when is_binary(line) -> do_read_all(mapped_dev, acc <> line)
|
||||
:eof -> acc
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
defp concat(:empty, line), do: line
|
||||
defp concat(acc, line) when is_binary(acc), do: acc <> line
|
||||
defp concat(acc, line) when is_list(acc), do: acc ++ line
|
||||
|
||||
defp read_eof(device, :empty) do
|
||||
with [_ | _] = opts <- :io.getopts(device),
|
||||
false <- Keyword.get(opts, :binary, true) do
|
||||
''
|
||||
else
|
||||
_ -> ""
|
||||
end
|
||||
end
|
||||
|
||||
defp read_eof(_device, acc), do: acc
|
||||
|
||||
@doc """
|
||||
Reads from the IO `device`. The operation is Unicode unsafe.
|
||||
|
||||
@@ -236,7 +141,7 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes `chardata` to the given `device`.
|
||||
Writes `item` to the given `device`.
|
||||
|
||||
By default, the `device` is the standard output.
|
||||
|
||||
@@ -250,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 """
|
||||
@@ -316,22 +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.io_warn(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.io_warn(
|
||||
line || 0,
|
||||
file && List.to_string(file),
|
||||
message,
|
||||
[message, ?\n, " ", formatted_trace, ?\n]
|
||||
)
|
||||
:elixir_errors.bare_warn(line, file && List.to_string(file), message)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -424,7 +315,7 @@ 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.
|
||||
@@ -446,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:
|
||||
@@ -548,10 +439,8 @@ defmodule IO do
|
||||
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.
|
||||
@@ -578,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.
|
||||
|
||||
Notice 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.
|
||||
|
||||
@@ -607,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.
|
||||
|
||||
@@ -626,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
|
||||
|
||||
@@ -237,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
|
||||
|
||||
@@ -14,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)
|
||||
@@ -32,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],
|
||||
@@ -75,7 +73,7 @@ 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), ' ', to_string(value)])
|
||||
@@ -141,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)
|
||||
@@ -190,47 +183,6 @@ defmodule IO.ANSI.Docs do
|
||||
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 quote_prefix(options), do: "#{color(:doc_quote, options)}> #{IO.ANSI.reset()}"
|
||||
|
||||
defp write_empty_quote_line(options) do
|
||||
options
|
||||
|> quote_prefix()
|
||||
|> IO.puts()
|
||||
end
|
||||
|
||||
## Lists
|
||||
|
||||
defp process_rest(stripped, rest, count, text, indent, options) do
|
||||
@@ -315,25 +267,16 @@ 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)
|
||||
|> handle_links
|
||||
|> handle_inline(options)
|
||||
|> String.split(@spaces)
|
||||
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap, prefix)
|
||||
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap)
|
||||
|
||||
unless no_wrap, do: newline_after_block()
|
||||
end
|
||||
|
||||
defp format_text(text, options) do
|
||||
text
|
||||
|> handle_links()
|
||||
|> handle_inline(options)
|
||||
end
|
||||
|
||||
## Code blocks
|
||||
|
||||
defp process_code([], code, indent, options) do
|
||||
@@ -405,17 +348,17 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp split_into_columns(line, options) do
|
||||
line
|
||||
|> String.trim(" ")
|
||||
|> String.trim("|")
|
||||
|> String.split("|")
|
||||
|> 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)
|
||||
|
||||
@@ -507,7 +450,7 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp table_line?(line) do
|
||||
line =~ ~r/[:\ -]\|[:\ -]/
|
||||
line =~ " | "
|
||||
end
|
||||
|
||||
## Helpers
|
||||
@@ -527,27 +470,14 @@ defmodule IO.ANSI.Docs do
|
||||
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, [])
|
||||
|> Enum.join("\n")
|
||||
|> 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 | 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
|
||||
@@ -595,11 +525,15 @@ 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 `.
|
||||
|
||||
+507
-582
File diff suppressed because it is too large
Load Diff
@@ -1,19 +1,16 @@
|
||||
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
|
||||
}
|
||||
|
||||
@doc """
|
||||
@@ -24,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)
|
||||
@@ -35,7 +31,7 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
end
|
||||
|
||||
run(fun)
|
||||
run(fun, config.halt)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -46,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
|
||||
@@ -62,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)
|
||||
|
||||
@@ -98,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
|
||||
@@ -244,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
|
||||
@@ -268,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
|
||||
@@ -312,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}
|
||||
@@ -357,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
|
||||
"-" <> _ ->
|
||||
@@ -423,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}} ->
|
||||
@@ -496,22 +459,13 @@ defmodule Kernel.CLI do
|
||||
wrapper(fn ->
|
||||
Code.compiler_options(config.compiler_options)
|
||||
|
||||
verbose_opts =
|
||||
opts =
|
||||
if config.verbose_compile do
|
||||
[each_long_compilation: &IO.puts("Compiling #{&1} (it's taking more than 15s)")]
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
||||
profile_opts =
|
||||
if config.profile do
|
||||
[profile: config.profile]
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
||||
opts = verbose_opts ++ profile_opts
|
||||
|
||||
case Kernel.ParallelCompiler.compile_to_path(files, config.output, opts) do
|
||||
{:ok, _, _} -> :ok
|
||||
{:error, _, _} -> exit({:shutdown, 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
|
||||
def ensure_compiled(module, kind) do
|
||||
parent = :erlang.get(:elixir_compiler_pid)
|
||||
ref = :erlang.make_ref()
|
||||
modules = :elixir_module.compiler_modules()
|
||||
send(parent, {:waiting, kind, self(), ref, 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,7 +40,7 @@ defmodule Kernel.LexicalTracker do
|
||||
|
||||
@doc false
|
||||
def stop(pid) do
|
||||
:gen_server.call(pid, :stop)
|
||||
:gen_server.cast(pid, :stop)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -40,18 +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 remote_struct(pid, module) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:remote_struct, module})
|
||||
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 import_dispatch(pid, module, fa) when is_atom(module) do
|
||||
:gen_server.cast(pid, {:import_dispatch, module, fa})
|
||||
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: %{},
|
||||
compile: %{},
|
||||
runtime: %{},
|
||||
structs: %{},
|
||||
cache: %{},
|
||||
compile_env: :ordsets.new(),
|
||||
file: nil
|
||||
}
|
||||
|
||||
@@ -125,35 +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.structs), 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_struct, module}, state) do
|
||||
structs = Map.put(state.structs, module, true)
|
||||
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, mode}, state) do
|
||||
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}}, state) do
|
||||
state = add_import_dispatch(state, module, function, arity)
|
||||
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
|
||||
|
||||
@@ -169,15 +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_import, module, fas, line, warn}, state) do
|
||||
directives =
|
||||
state.directives
|
||||
|> Enum.reject(&match?({{:import, {^module, _, _}}, _}, &1))
|
||||
|> Map.new()
|
||||
|> :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,15 +243,28 @@ 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_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 =
|
||||
add_dispatch(state.directives, module, :import)
|
||||
@@ -235,6 +273,7 @@ defmodule Kernel.LexicalTracker do
|
||||
# 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
|
||||
|
||||
@@ -243,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
|
||||
|
||||
@@ -22,7 +22,6 @@ defmodule Kernel.ParallelCompiler do
|
||||
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
|
||||
|
||||
Task.async(fn ->
|
||||
send(parent, {:async, self()})
|
||||
:erlang.put(:elixir_compiler_pid, parent)
|
||||
:erlang.put(:elixir_compiler_file, file)
|
||||
dest != :undefined and :erlang.put(:elixir_compiler_dest, dest)
|
||||
@@ -62,24 +61,16 @@ 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}` - to continue compilation with a list of further modules to compile
|
||||
* `{:runtime, modules}` - 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) 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"
|
||||
def compile(files, options \\ []) when is_list(options) do
|
||||
@@ -116,6 +107,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
spawn_workers(files, :require, options)
|
||||
end
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
@doc false
|
||||
@deprecated "Use Kernel.ParallelCompiler.compile/2 instead"
|
||||
def files(files, options \\ []) when is_list(options) do
|
||||
@@ -125,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
|
||||
@@ -139,20 +132,16 @@ defmodule Kernel.ParallelCompiler do
|
||||
compiler_pid = self()
|
||||
:elixir_code_server.cast({:reset_warnings, compiler_pid})
|
||||
schedulers = max(:erlang.system_info(:schedulers_online), 2)
|
||||
beam_timestamp = Keyword.get(options, :beam_timestamp)
|
||||
|
||||
outcome =
|
||||
spawn_workers(files, 0, [], [], %{}, [], %{
|
||||
result =
|
||||
spawn_workers(files, [], [], [], [], %{
|
||||
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_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),
|
||||
long_compilation_threshold: Keyword.get(options, :long_compilation_threshold, 15),
|
||||
profile: Keyword.get(options, :profile),
|
||||
cycle_start: System.monotonic_time(),
|
||||
module_counter: 0,
|
||||
output: output,
|
||||
long_compilation_threshold: Keyword.get(options, :long_compilation_threshold, 15),
|
||||
schedulers: schedulers
|
||||
})
|
||||
|
||||
@@ -160,7 +149,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
# compilation status will be set to error.
|
||||
compilation_status = :elixir_code_server.call({:compilation_status, compiler_pid})
|
||||
|
||||
case {outcome, compilation_status} do
|
||||
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)
|
||||
@@ -169,376 +158,249 @@ defmodule Kernel.ParallelCompiler do
|
||||
{{:error, errors, warnings}, :error} ->
|
||||
{:error, errors ++ warnings, []}
|
||||
|
||||
{{:ok, outcome, warnings}, _} ->
|
||||
{:ok, write_module_binaries(outcome, output, beam_timestamp), warnings}
|
||||
|
||||
{{:error, errors, warnings}, _} ->
|
||||
{:error, errors, warnings}
|
||||
end
|
||||
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)
|
||||
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}
|
||||
end
|
||||
|
||||
defp maybe_check_modules(result, runtime_modules, state) do
|
||||
%{schedulers: schedulers, profile: profile} = state
|
||||
|
||||
if :elixir_config.get(:bootstrap) do
|
||||
[]
|
||||
else
|
||||
compiled_modules = checker_compiled_modules(result)
|
||||
runtime_modules = checker_runtime_modules(runtime_modules)
|
||||
|
||||
profile_checker(profile, compiled_modules, runtime_modules, fn ->
|
||||
Module.ParallelChecker.verify(compiled_modules, runtime_modules, schedulers)
|
||||
end)
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
defp checker_compiled_modules(result) do
|
||||
for {{:module, _module}, {binary, module_map}} <- result do
|
||||
{module_map, binary}
|
||||
end
|
||||
end
|
||||
|
||||
defp checker_runtime_modules(modules) do
|
||||
for module <- modules,
|
||||
path = :code.which(module),
|
||||
is_list(path) and path != [] do
|
||||
{module, File.read!(path)}
|
||||
end
|
||||
end
|
||||
|
||||
defp profile_checker(_profile = :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(_profile = nil, _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
|
||||
defp spawn_workers([{ref, found} | t], waiting, queued, result, warnings, state) do
|
||||
waiting =
|
||||
case List.keytake(waiting, ref, 2) do
|
||||
{{_kind, pid, ^ref, _on, _defining, _deadlock}, waiting} ->
|
||||
{{_kind, pid, ^ref, _on, _defining}, waiting} ->
|
||||
send(pid, {ref, found})
|
||||
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.
|
||||
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
|
||||
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 ->
|
||||
: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)
|
||||
|
||||
timer_ref = Process.send_after(self(), {:timed_out, pid}, threshold * 1000)
|
||||
files = [{pid, ref, file, timer_ref} | files]
|
||||
spawn_workers(queue, spawned + 1, waiting, files, result, warnings, state)
|
||||
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
|
||||
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 each_cycle_return(state.each_cycle.()) do
|
||||
{:runtime, dependent_modules} ->
|
||||
verify_modules(result, warnings, dependent_modules, state)
|
||||
|
||||
{:compile, []} ->
|
||||
verify_modules(result, warnings, [], state)
|
||||
|
||||
{:compile, more} ->
|
||||
spawn_workers(more, 0, [], [], result, 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, _, _, _}] = 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 (deadlock = soft)
|
||||
# 2. Struct checks (deadlock = hard)
|
||||
# 3. Modules without a known definition
|
||||
# 4. Code invocation (deadlock = raise)
|
||||
#
|
||||
# In theory there is no difference between hard and raise, the
|
||||
# difference is where the raise is happening, inside the compiler
|
||||
# or in the caller.
|
||||
cond do
|
||||
deadlocked = deadlocked(waiting, :soft) || deadlocked(waiting, :hard) ->
|
||||
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, state)
|
||||
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
|
||||
|
||||
without_definition = without_definition(waiting, files) ->
|
||||
spawn_workers(without_definition, spawned, waiting, files, result, warnings, state)
|
||||
# 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}}
|
||||
|
||||
true ->
|
||||
errors = handle_deadlock(waiting, files)
|
||||
{:error, errors, warnings}
|
||||
# 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 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)
|
||||
# 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 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 waiting_on_without_definition(waiting, pid) do
|
||||
{_, ^pid, _, on, _} = entry = List.keyfind(waiting, pid, 1)
|
||||
|
||||
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})
|
||||
if Enum.any?(waiting, fn {_, _, _, _, defining} -> on in defining end) do
|
||||
nil
|
||||
else
|
||||
entry
|
||||
end
|
||||
end
|
||||
|
||||
defp cycle_timing(result, %{profile: :time} = state) do
|
||||
%{cycle_start: cycle_start, module_counter: module_counter} = state
|
||||
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 | cycle_start: now, module_counter: num_modules}
|
||||
end
|
||||
|
||||
defp cycle_timing(_result, %{profile: nil} = state) do
|
||||
state
|
||||
end
|
||||
|
||||
defp count_modules(result) do
|
||||
Enum.count(result, &match?({{:module, _}, _}, &1))
|
||||
end
|
||||
|
||||
# TODO: Deprecate on v1.14
|
||||
defp each_cycle_return(modules) when is_list(modules), do: {:compile, modules}
|
||||
defp each_cycle_return(other), do: other
|
||||
|
||||
# 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 compile yet, so they may not be in waiting.
|
||||
defp without_definition(waiting, files) do
|
||||
nillify_empty(
|
||||
for {pid, _, _, _} <- files,
|
||||
{_, ^pid, ref, on, _, _} <- List.wrap(List.keyfind(waiting, pid, 1)),
|
||||
not Enum.any?(waiting, fn {_, _, _, _, defining, _} -> on in defining end),
|
||||
do: {ref, :not_found}
|
||||
)
|
||||
end
|
||||
|
||||
defp deadlocked(waiting, type) do
|
||||
nillify_empty(for {_, _, ref, _, _, ^type} <- waiting, do: {ref, :deadlock})
|
||||
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, module_map} ->
|
||||
{: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}
|
||||
|
||||
cancel_waiting_timer(files, child)
|
||||
result = Map.put(result, {:module, module}, {binary, module_map})
|
||||
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, _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, ref, on, defining, deadlock?} ->
|
||||
# If we already got what we were waiting for, do not put it on 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 Map.has_key?(result, {kind, on}) or on in defining do
|
||||
if :lists.any(&match?({^kind, ^on}, &1), result) or on in defining do
|
||||
send(child, {ref, :found})
|
||||
waiting
|
||||
else
|
||||
[{kind, child, ref, 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)
|
||||
|
||||
{:timed_out, child} ->
|
||||
case List.keyfind(files, child, 0) do
|
||||
{^child, _, file, _} -> state.each_long_compilation.(file)
|
||||
_ -> :ok
|
||||
case List.keyfind(queued, child, 0) do
|
||||
{^child, _, file, _} ->
|
||||
state.each_long_compilation.(file)
|
||||
|
||||
_ ->
|
||||
:ok
|
||||
end
|
||||
|
||||
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
|
||||
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, line, 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)
|
||||
cancel_waiting_timer(files, child_pid)
|
||||
wait_for_messages(files, waiting, queued, result, [warning | warnings], state)
|
||||
|
||||
{:file_done, child_pid, file, :ok} ->
|
||||
discard_down(child_pid)
|
||||
new_files = List.keydelete(files, child_pid, 0)
|
||||
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} ->
|
||||
cancel_waiting_timer(files, child_pid)
|
||||
{:file_done, child_pid, file, {kind, reason, stack}} ->
|
||||
discard_down(child_pid)
|
||||
new_files = List.keydelete(files, child_pid, 0)
|
||||
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)
|
||||
cancel_waiting_timer(files, child_pid)
|
||||
discard_down(child_pid)
|
||||
files |> List.keydelete(child_pid, 0) |> terminate()
|
||||
cancel_waiting_timer(queued, child_pid)
|
||||
|
||||
queued
|
||||
|> List.keydelete(child_pid, 0)
|
||||
|> terminate()
|
||||
|
||||
{:error, [to_error(file, kind, reason, stack)], warnings}
|
||||
|
||||
{:DOWN, ref, :process, pid, reason} ->
|
||||
waiting = List.keydelete(waiting, pid, 1)
|
||||
|
||||
case handle_down(files, ref, reason) do
|
||||
:ok -> wait_for_messages(queue, spawned - 1, waiting, files, result, warnings, state)
|
||||
{: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
|
||||
@@ -550,16 +412,16 @@ defmodule Kernel.ParallelCompiler do
|
||||
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 List.keyfind(files, ref, 1) do
|
||||
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
|
||||
queued
|
||||
|> List.keydelete(child_pid, 0)
|
||||
|> terminate()
|
||||
|
||||
@@ -570,17 +432,18 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_deadlock(waiting, files) do
|
||||
defp handle_deadlock(waiting, queued) do
|
||||
deadlock =
|
||||
for {pid, _, 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("""
|
||||
@@ -606,9 +469,9 @@ defmodule Kernel.ParallelCompiler do
|
||||
for {file, _, description} <- deadlock, do: {Path.absname(file), nil, description}
|
||||
end
|
||||
|
||||
defp terminate(files) do
|
||||
for {pid, _, _, _} <- files, do: Process.exit(pid, :kill)
|
||||
for {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
|
||||
|
||||
@@ -619,11 +482,12 @@ defmodule Kernel.ParallelCompiler do
|
||||
])
|
||||
end
|
||||
|
||||
defp cancel_waiting_timer(files, child_pid) do
|
||||
case List.keyfind(files, child_pid, 0) do
|
||||
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.
|
||||
# Let's flush the message in case it arrived before we canceled the
|
||||
# timeout.
|
||||
receive do
|
||||
{:timed_out, ^child_pid} -> :ok
|
||||
after
|
||||
@@ -652,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.
|
||||
|
||||
@@ -199,7 +192,7 @@ defmodule Kernel.SpecialForms do
|
||||
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>>
|
||||
@@ -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
|
||||
@@ -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,7 +503,7 @@ 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
|
||||
|
||||
Notice that calling `alias` without the `:as` option automatically
|
||||
@@ -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
|
||||
|
||||
@@ -805,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
|
||||
@@ -827,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:
|
||||
@@ -857,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:
|
||||
|
||||
@@ -876,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)`
|
||||
@@ -886,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:
|
||||
|
||||
@@ -955,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
|
||||
@@ -977,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
|
||||
@@ -998,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:
|
||||
@@ -1019,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:
|
||||
@@ -1040,8 +1028,8 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
ContextHygiene.write()
|
||||
ContextHygiene.read()
|
||||
ContextHygiene.write
|
||||
ContextHygiene.read
|
||||
#=> 1
|
||||
|
||||
## Hygiene in aliases
|
||||
@@ -1054,14 +1042,13 @@ 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 #=> %{}
|
||||
|
||||
Notice that, even though the alias `M` is not available
|
||||
in the context the macro is expanded, the code above works
|
||||
@@ -1075,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
|
||||
@@ -1111,10 +1097,10 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
|
||||
require Hygiene
|
||||
Hygiene.no_interference()
|
||||
** (UndefinedFunctionError) ...
|
||||
Hygiene.no_interference
|
||||
#=> ** (UndefinedFunctionError) ...
|
||||
|
||||
Hygiene.interference()
|
||||
Hygiene.interference
|
||||
#=> "world"
|
||||
end
|
||||
|
||||
@@ -1136,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`
|
||||
@@ -1172,8 +1157,7 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
Lazy.return_length()
|
||||
#=> 5
|
||||
Lazy.return_length #=> 5
|
||||
|
||||
## Stacktrace information
|
||||
|
||||
@@ -1199,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
|
||||
@@ -1210,25 +1194,25 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
## 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:
|
||||
@@ -1251,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
|
||||
|
||||
@@ -1272,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
|
||||
|
||||
@@ -1521,7 +1505,7 @@ defmodule Kernel.SpecialForms do
|
||||
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:
|
||||
@@ -1617,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
|
||||
@@ -1686,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]}, [], []}
|
||||
|
||||
@@ -1694,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.
|
||||
@@ -1743,34 +1725,34 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
#=> "This clause would match any value (x = 10)"
|
||||
|
||||
## Variable handling
|
||||
## Variables handling
|
||||
|
||||
Notice 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
|
||||
|
||||
When binding variables with the same names as variables in the outer context,
|
||||
the variables in the outer context are not affected.
|
||||
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:
|
||||
@@ -1779,7 +1761,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
case 10 do
|
||||
^x -> "Won't match"
|
||||
_ -> "Will match"
|
||||
_ -> "Will match"
|
||||
end
|
||||
#=> "Will match"
|
||||
|
||||
@@ -1825,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`
|
||||
@@ -1928,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
|
||||
@@ -1940,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
|
||||
@@ -2086,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
|
||||
@@ -2120,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
|
||||
@@ -2132,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.
|
||||
@@ -2152,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
|
||||
@@ -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
|
||||
@@ -142,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} ->
|
||||
@@ -180,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})
|
||||
@@ -202,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
|
||||
@@ -267,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
|
||||
@@ -277,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)
|
||||
|
||||
@@ -285,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)
|
||||
@@ -295,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} =
|
||||
@@ -319,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}
|
||||
@@ -344,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
|
||||
@@ -389,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
|
||||
@@ -402,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
|
||||
@@ -459,7 +452,7 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec(
|
||||
{:<<>>, meta, [{:"::", unit_meta, [{:_, _, ctx1}, {:*, _, [{:_, _, ctx2}, unit]}]}]},
|
||||
{:<<>>, meta, [{:::, unit_meta, [{:_, _, ctx1}, {:*, _, [{:_, _, ctx2}, unit]}]}]},
|
||||
_,
|
||||
_,
|
||||
state
|
||||
@@ -469,7 +462,7 @@ defmodule Kernel.Typespec do
|
||||
{{: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}
|
||||
@@ -480,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]}]}
|
||||
]
|
||||
},
|
||||
_,
|
||||
@@ -509,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)
|
||||
@@ -520,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 =
|
||||
@@ -557,7 +561,7 @@ 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, _} ->
|
||||
@@ -586,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
|
||||
)
|
||||
|
||||
@@ -645,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
|
||||
@@ -657,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}
|
||||
|
||||
@@ -670,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}
|
||||
@@ -725,19 +723,14 @@ defmodule Kernel.Typespec do
|
||||
# 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
|
||||
@@ -776,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
|
||||
@@ -787,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)
|
||||
@@ -833,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 =
|
||||
@@ -969,37 +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 unused")
|
||||
|
||||
_ ->
|
||||
: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,7 +25,7 @@ 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} =
|
||||
@@ -94,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
|
||||
@@ -111,26 +108,13 @@ defmodule Kernel.Utils do
|
||||
{struct, enforce_keys, Module.get_attribute(module, :derive)}
|
||||
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)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Announcing callback for defstruct.
|
||||
"""
|
||||
def announce_struct(module) do
|
||||
case :erlang.get(:elixir_compiler_pid) do
|
||||
:undefined -> :ok
|
||||
pid -> send(pid, {:available, :struct, module})
|
||||
pid -> send(pid, {:struct_available, module})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -180,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 ->
|
||||
|
||||
+114
-174
@@ -1,10 +1,10 @@
|
||||
defmodule Keyword do
|
||||
@moduledoc """
|
||||
Keyword lists are lists of two-element tuples, where the first
|
||||
element of the tuple is an atom and the second element can be any
|
||||
value, used mostly to work with optional values.
|
||||
A set of functions for working with keywords.
|
||||
|
||||
## Examples
|
||||
A keyword is a list of two-element tuples where the first
|
||||
element of the tuple is an atom and the second element
|
||||
can be any value.
|
||||
|
||||
For example, the following is a keyword list:
|
||||
|
||||
@@ -15,68 +15,58 @@ defmodule Keyword do
|
||||
|
||||
[exit_on_close: true, active: :once, packet_size: 1024]
|
||||
|
||||
The two syntaxes are completely equivalent. Like atoms, keyword
|
||||
lists keys 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:
|
||||
This is also the syntax that Elixir uses to inspect keyword lists:
|
||||
|
||||
iex> [{:active, :once}]
|
||||
[active: :once]
|
||||
|
||||
The two syntaxes are completely equivalent. Like atoms, keywords
|
||||
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> ["exit on close": true]
|
||||
["exit on close": true]
|
||||
|
||||
Wrapping a keyword in quotes does not make it a string. Keyword lists
|
||||
keys are always atoms. If you use quotes around the key when quoting
|
||||
is not necessary, Elixir will warn.
|
||||
Wrapping a keyword in quotes does not make it a string. Keywords are
|
||||
always atoms. If you use quotes when all characters are a valid part
|
||||
of a keyword without quotes, Elixir will warn.
|
||||
|
||||
## Duplicate keys and ordering
|
||||
|
||||
A keyword may have duplicated keys so it is not strictly a key-value
|
||||
data type. However most of the functions in this module behave exactly
|
||||
as a key-value so they work similarly to the functions you would find
|
||||
in the `Map` module. For example, `Keyword.get/3` will get the first
|
||||
entry matching the given key, regardless if duplicated entries exist.
|
||||
Similarly, `Keyword.put/3` and `Keyword.delete/2` ensure all duplicated
|
||||
entries for a given key are removed when invoked. Note however that
|
||||
keyword list operations need to traverse the list in order to find
|
||||
keys, so these operations are slower than their map counterparts.
|
||||
|
||||
A handful of functions exist to handle duplicated keys, for example,
|
||||
`get_values/2` returns all values for a given key and `delete_first/2`
|
||||
deletes just one of the existing entries.
|
||||
|
||||
The functions in `Keyword` do not guarantee any property when it comes
|
||||
to ordering. However, since a keyword list is simply a list, all the
|
||||
operations defined in `Enum` and `List` can be applied too, especially
|
||||
when ordering is required.
|
||||
|
||||
Most of the functions in this module work in linear time. This means
|
||||
that, the time it takes to perform an operation grows at the same
|
||||
rate as the length of the list.
|
||||
|
||||
## Call syntax
|
||||
|
||||
When keyword lists are passed as the last argument to a function, then
|
||||
the square brackets around the keyword list can be omitted as well. For
|
||||
example, the keyword list syntax:
|
||||
|
||||
String.split("1-0", "-", [trim: true, parts: 2])
|
||||
|
||||
can be written without the enclosing brackets whenever it is the last
|
||||
argument of a function call:
|
||||
Note that when keyword lists are passed as the last argument to a function,
|
||||
if the short-hand syntax is used then the square brackets around the keyword list
|
||||
can be omitted as well. For example, the following:
|
||||
|
||||
String.split("1-0", "-", trim: true, parts: 2)
|
||||
|
||||
Since tuples, lists, maps, and others are treated the same as function
|
||||
calls in Elixir syntax, this property is also available to them:
|
||||
is equivalent to:
|
||||
|
||||
iex> {1, 2, foo: :bar}
|
||||
{1, 2, [{:foo, :bar}]}
|
||||
String.split("1-0", "-", [trim: true, parts: 2])
|
||||
|
||||
iex> [1, 2, foo: :bar]
|
||||
[1, 2, {:foo, :bar}]
|
||||
A keyword may have duplicated keys so it is not strictly
|
||||
a key-value store. However most of the functions in this module
|
||||
behave exactly as a dictionary so they work similarly to
|
||||
the functions you would find in the `Map` module.
|
||||
|
||||
iex> %{1 => 2, foo: :bar}
|
||||
%{1 => 2, :foo => :bar}
|
||||
For example, `Keyword.get/3` will get the first entry matching
|
||||
the given key, regardless if duplicated entries exist.
|
||||
Similarly, `Keyword.put/3` and `Keyword.delete/3` ensure all
|
||||
duplicated entries for a given key are removed when invoked.
|
||||
Note that operations that require keys to be found in the keyword
|
||||
list (like `Keyword.get/3`) need to traverse the list in order
|
||||
to find keys, so these operations may be slower than their map
|
||||
counterparts.
|
||||
|
||||
A handful of functions exist to handle duplicated keys, in
|
||||
particular, `Enum.into/2` allows creating new keywords without
|
||||
removing duplicated keys, `get_values/2` returns all values for
|
||||
a given key and `delete_first/2` deletes just one of the existing
|
||||
entries.
|
||||
|
||||
The functions in `Keyword` do not guarantee any property when
|
||||
it comes to ordering. However, since a keyword list is simply a
|
||||
list, all the operations defined in `Enum` and `List` can be
|
||||
applied too, especially when ordering is required.
|
||||
"""
|
||||
|
||||
@compile :inline_list_funcs
|
||||
@@ -126,7 +116,7 @@ defmodule Keyword do
|
||||
def new, do: []
|
||||
|
||||
@doc """
|
||||
Creates a keyword list from an enumerable.
|
||||
Creates a keyword from an enumerable.
|
||||
|
||||
Duplicated entries are removed, the latest one prevails.
|
||||
Unlike `Enum.into(enumerable, [])`, `Keyword.new(enumerable)`
|
||||
@@ -147,7 +137,7 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a keyword list from an enumerable via the transformation function.
|
||||
Creates a keyword from an enumerable via the transformation function.
|
||||
|
||||
Duplicated entries are removed, the latest one prevails.
|
||||
Unlike `Enum.into(enumerable, [], fun)`,
|
||||
@@ -160,7 +150,7 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec new(Enum.t(), (term -> {key, value})) :: t
|
||||
def new(pairs, transform) when is_function(transform, 1) do
|
||||
def new(pairs, transform) do
|
||||
fun = fn el, acc ->
|
||||
{k, v} = transform.(el)
|
||||
put_new(acc, k, v)
|
||||
@@ -415,12 +405,13 @@ defmodule Keyword do
|
||||
"""
|
||||
@spec get_values(t, key) :: [value]
|
||||
def get_values(keywords, key) when is_list(keywords) and is_atom(key) do
|
||||
get_values(keywords, key, [])
|
||||
end
|
||||
fun = fn
|
||||
{^key, val} -> {true, val}
|
||||
{_, _} -> false
|
||||
end
|
||||
|
||||
defp get_values([{key, value} | tail], key, values), do: get_values(tail, key, [value | values])
|
||||
defp get_values([{_, _} | tail], key, values), do: get_values(tail, key, values)
|
||||
defp get_values([], _key, values), do: :lists.reverse(values)
|
||||
:lists.filtermap(fun, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all keys from the keyword list.
|
||||
@@ -458,8 +449,24 @@ defmodule Keyword do
|
||||
:lists.map(fn {_, v} -> v end, keywords)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Keyword.fetch/2 + Keyword.delete/2 instead"
|
||||
@doc """
|
||||
Deletes the entries in the keyword list for a `key` with `value`.
|
||||
|
||||
If no `key` with `value` exists, returns the keyword list unchanged.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.delete([a: 1, b: 2], :a, 1)
|
||||
[b: 2]
|
||||
iex> Keyword.delete([a: 1, b: 2, a: 3], :a, 3)
|
||||
[a: 1, b: 2]
|
||||
iex> Keyword.delete([a: 1], :a, 5)
|
||||
[a: 1]
|
||||
iex> Keyword.delete([a: 1], :b, 5)
|
||||
[a: 1]
|
||||
|
||||
"""
|
||||
@spec delete(t, key, value) :: t
|
||||
def delete(keywords, key, value) when is_list(keywords) and is_atom(key) do
|
||||
case :lists.keymember(key, 1, keywords) do
|
||||
true -> delete_key_value(keywords, key, value)
|
||||
@@ -505,9 +512,17 @@ defmodule Keyword do
|
||||
end
|
||||
end
|
||||
|
||||
defp delete_key([{key, _} | tail], key), do: delete_key(tail, key)
|
||||
defp delete_key([{_, _} = pair | tail], key), do: [pair | delete_key(tail, key)]
|
||||
defp delete_key([], _key), do: []
|
||||
defp delete_key([{key, _} | tail], key) do
|
||||
delete_key(tail, key)
|
||||
end
|
||||
|
||||
defp delete_key([{_, _} = pair | tail], key) do
|
||||
[pair | delete_key(tail, key)]
|
||||
end
|
||||
|
||||
defp delete_key([], _key) do
|
||||
[]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the first entry in the keyword list for a specific `key`.
|
||||
@@ -629,10 +644,8 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.replace!([a: 1, b: 2, a: 3], :a, :new)
|
||||
[a: :new, b: 2]
|
||||
iex> Keyword.replace!([a: 1, b: 2, c: 3, b: 4], :b, :new)
|
||||
[a: 1, b: :new, c: 3]
|
||||
iex> Keyword.replace!([a: 1, b: 2, a: 4], :a, 3)
|
||||
[a: 3, b: 2]
|
||||
|
||||
iex> Keyword.replace!([a: 1], :b, 2)
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
@@ -641,19 +654,10 @@ defmodule Keyword do
|
||||
@doc since: "1.5.0"
|
||||
@spec replace!(t, key, value) :: t
|
||||
def replace!(keywords, key, value) when is_list(keywords) and is_atom(key) do
|
||||
replace!(keywords, key, value, keywords)
|
||||
end
|
||||
|
||||
defp replace!([{key, _} | keywords], key, value, _original) do
|
||||
[{key, value} | delete(keywords, key)]
|
||||
end
|
||||
|
||||
defp replace!([{_, _} = e | keywords], key, value, original) do
|
||||
[e | replace!(keywords, key, value, original)]
|
||||
end
|
||||
|
||||
defp replace!([], key, _value, original) when is_atom(key) do
|
||||
raise(KeyError, key: key, term: original)
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, _} -> [{key, value} | delete(keywords, key)]
|
||||
false -> raise KeyError, key: key, term: keywords
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -813,31 +817,30 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.update!([a: 1, b: 2, a: 3], :a, &(&1 * 2))
|
||||
[a: 2, b: 2]
|
||||
iex> Keyword.update!([a: 1, b: 2, c: 3], :b, &(&1 * 2))
|
||||
[a: 1, b: 4, c: 3]
|
||||
iex> Keyword.update!([a: 1], :a, &(&1 * 2))
|
||||
[a: 2]
|
||||
iex> Keyword.update!([a: 1, a: 2], :a, &(&1 * 2))
|
||||
[a: 2]
|
||||
|
||||
iex> Keyword.update!([a: 1], :b, &(&1 * 2))
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
|
||||
"""
|
||||
@spec update!(t, key, (value -> value)) :: t
|
||||
def update!(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key) and is_function(fun, 1) do
|
||||
def update!(keywords, key, fun) do
|
||||
update!(keywords, key, fun, keywords)
|
||||
end
|
||||
|
||||
defp update!([{key, value} | keywords], key, fun, _original) do
|
||||
defp update!([{key, value} | keywords], key, fun, _dict) do
|
||||
[{key, fun.(value)} | delete(keywords, key)]
|
||||
end
|
||||
|
||||
defp update!([{_, _} = e | keywords], key, fun, original) do
|
||||
[e | update!(keywords, key, fun, original)]
|
||||
defp update!([{_, _} = e | keywords], key, fun, dict) do
|
||||
[e | update!(keywords, key, fun, dict)]
|
||||
end
|
||||
|
||||
defp update!([], key, _fun, original) when is_atom(key) do
|
||||
raise(KeyError, key: key, term: original)
|
||||
defp update!([], key, _fun, dict) when is_atom(key) do
|
||||
raise(KeyError, key: key, term: dict)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -892,7 +895,7 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec split(t, [key]) :: {t, t}
|
||||
def split(keywords, keys) when is_list(keywords) and is_list(keys) do
|
||||
def split(keywords, keys) when is_list(keywords) do
|
||||
fun = fn {k, v}, {take, drop} ->
|
||||
case k in keys do
|
||||
true -> {[{k, v} | take], drop}
|
||||
@@ -920,7 +923,7 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec take(t, [key]) :: t
|
||||
def take(keywords, keys) when is_list(keywords) and is_list(keys) do
|
||||
def take(keywords, keys) when is_list(keywords) do
|
||||
:lists.filter(fn {k, _} -> k in keys end, keywords)
|
||||
end
|
||||
|
||||
@@ -938,20 +941,15 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec drop(t, [key]) :: t
|
||||
def drop(keywords, keys) when is_list(keywords) and is_list(keys) do
|
||||
def drop(keywords, keys) when is_list(keywords) do
|
||||
:lists.filter(fn {key, _} -> key not in keys end, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the first value for `key` and removes all associated entries in the keyword list.
|
||||
Returns and removes all values associated with `key` in the keyword list.
|
||||
|
||||
It returns a tuple where the first element is the first value for `key` and the
|
||||
second element is a keyword list with all entries associated with `key` removed.
|
||||
If the `key` is not present in the keyword list, `{default, keyword_list}` is
|
||||
returned.
|
||||
|
||||
If you don't want to remove all the entries associated with `key` use `pop_first/3`
|
||||
instead, that function will remove only the first entry.
|
||||
All duplicated keys are removed. See `pop_first/3` for
|
||||
removing only the first entry.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -966,75 +964,16 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec pop(t, key, value) :: {value, t}
|
||||
def pop(keywords, key, default \\ nil) when is_list(keywords) and is_atom(key) do
|
||||
def pop(keywords, key, default \\ nil) when is_list(keywords) do
|
||||
case fetch(keywords, key) do
|
||||
{:ok, value} -> {value, delete(keywords, key)}
|
||||
:error -> {default, keywords}
|
||||
{:ok, value} ->
|
||||
{value, delete(keywords, key)}
|
||||
|
||||
:error ->
|
||||
{default, keywords}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the first value for `key` and removes all associated antries in the keyword list,
|
||||
raising if `key` is not present.
|
||||
|
||||
This function behaves like `pop/3`, but raises in cases the `key` is not present in the
|
||||
given `keywords`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.pop!([a: 1], :a)
|
||||
{1, []}
|
||||
iex> Keyword.pop!([a: 1, a: 2], :a)
|
||||
{1, []}
|
||||
iex> Keyword.pop!([a: 1], :b)
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec pop!(t, key) :: {value, t}
|
||||
def pop!(keywords, key) when is_list(keywords) and is_atom(key) do
|
||||
case fetch(keywords, key) do
|
||||
{:ok, value} -> {value, delete(keywords, key)}
|
||||
:error -> raise KeyError, key: key, term: keywords
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all values for `key` and removes all associated entries in the keyword list.
|
||||
|
||||
It returns a tuple where the first element is a list of values for `key` and the
|
||||
second element is a keyword list with all entries associated with `key` removed.
|
||||
If the `key` is not present in the keyword list, `{[], keyword_list}` is
|
||||
returned.
|
||||
|
||||
If you don't want to remove all the entries associated with `key` use `pop_first/3`
|
||||
instead, that function will remove only the first entry.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.pop_values([a: 1], :a)
|
||||
{[1], []}
|
||||
iex> Keyword.pop_values([a: 1], :b)
|
||||
{[], [a: 1]}
|
||||
iex> Keyword.pop_values([a: 1, a: 2], :a)
|
||||
{[1, 2], []}
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec pop_values(t, key) :: {[value], t}
|
||||
def pop_values(keywords, key) when is_list(keywords) and is_atom(key) do
|
||||
pop_values(:lists.reverse(keywords), key, [], [])
|
||||
end
|
||||
|
||||
defp pop_values([{key, value} | tail], key, values, acc),
|
||||
do: pop_values(tail, key, [value | values], acc)
|
||||
|
||||
defp pop_values([{_, _} = pair | tail], key, values, acc),
|
||||
do: pop_values(tail, key, values, [pair | acc])
|
||||
|
||||
defp pop_values([], _key, values, acc),
|
||||
do: {values, acc}
|
||||
|
||||
@doc """
|
||||
Lazily returns and removes all values associated with `key` in the keyword list.
|
||||
|
||||
@@ -1059,7 +998,7 @@ defmodule Keyword do
|
||||
"""
|
||||
@spec pop_lazy(t, key, (() -> value)) :: {value, t}
|
||||
def pop_lazy(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key) and is_function(fun, 0) do
|
||||
when is_list(keywords) and is_function(fun, 0) do
|
||||
case fetch(keywords, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(keywords, key)}
|
||||
@@ -1087,7 +1026,7 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec pop_first(t, key, value) :: {value, t}
|
||||
def pop_first(keywords, key, default \\ nil) when is_list(keywords) and is_atom(key) do
|
||||
def pop_first(keywords, key, default \\ nil) when is_list(keywords) do
|
||||
case :lists.keytake(key, 1, keywords) do
|
||||
{:value, {^key, value}, rest} -> {value, rest}
|
||||
false -> {default, keywords}
|
||||
@@ -1109,6 +1048,7 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
@deprecated "Use Kernel.length/1 instead"
|
||||
def size(keyword) do
|
||||
length(keyword)
|
||||
|
||||
+102
-140
@@ -1,6 +1,19 @@
|
||||
defmodule List do
|
||||
@moduledoc """
|
||||
Linked lists hold zero, one, or more elements in the choosen 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:
|
||||
|
||||
@@ -50,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()
|
||||
#=> [
|
||||
@@ -115,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
|
||||
@@ -135,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
|
||||
@@ -183,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
|
||||
@@ -204,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
|
||||
@@ -269,8 +219,7 @@ defmodule List do
|
||||
1
|
||||
|
||||
"""
|
||||
@spec first([]) :: nil
|
||||
@spec first([elem, ...]) :: elem when elem: var
|
||||
@spec first([elem]) :: nil | elem when elem: var
|
||||
def first([]), do: nil
|
||||
def first([head | _]), do: head
|
||||
|
||||
@@ -289,19 +238,16 @@ defmodule List do
|
||||
3
|
||||
|
||||
"""
|
||||
@spec last([]) :: nil
|
||||
@spec last([elem, ...]) :: elem when elem: var
|
||||
@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)
|
||||
@@ -321,7 +267,7 @@ defmodule List do
|
||||
|
||||
@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
|
||||
@@ -342,7 +288,7 @@ defmodule List do
|
||||
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
|
||||
@@ -360,7 +306,7 @@ defmodule List do
|
||||
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
|
||||
@@ -378,10 +324,10 @@ defmodule List do
|
||||
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
|
||||
|
||||
@@ -399,7 +345,7 @@ defmodule List do
|
||||
|
||||
@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
|
||||
@@ -441,7 +387,7 @@ defmodule List do
|
||||
@spec keytake([tuple], any, non_neg_integer) :: {tuple, [tuple]} | nil
|
||||
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
|
||||
@@ -464,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
|
||||
@@ -540,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)
|
||||
@@ -555,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
|
||||
|
||||
@@ -573,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"
|
||||
@@ -761,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)
|
||||
@@ -774,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.
|
||||
|
||||
@@ -797,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.
|
||||
|
||||
@@ -857,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)
|
||||
@@ -887,18 +860,11 @@ 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
|
||||
|
||||
Notice that this function expects a list of integers representing
|
||||
Unicode code points. If you have a list of bytes, you must instead use
|
||||
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
|
||||
@@ -912,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
|
||||
@@ -925,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:
|
||||
|
||||
@@ -949,11 +911,11 @@ 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.
|
||||
|
||||
Notice that this function expects a list of integers representing
|
||||
Unicode code points. If you have a list of bytes, you must instead use
|
||||
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
|
||||
@@ -981,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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+85
-270
@@ -2,62 +2,16 @@ import Kernel, except: [to_string: 1]
|
||||
|
||||
defmodule Macro do
|
||||
@moduledoc ~S"""
|
||||
Macros are compile-time constructs that are invoked with Elixir's AST
|
||||
as input and a superset of Elixir's AST as output.
|
||||
|
||||
Let's see a simple example that shows the difference between functions and macros:
|
||||
|
||||
defmodule Example do
|
||||
defmacro macro_inspect(value) do
|
||||
IO.inspect(value)
|
||||
value
|
||||
end
|
||||
|
||||
def fun_inspect(value) do
|
||||
IO.inpect(value)
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
Now let's give it a try:
|
||||
|
||||
import Example
|
||||
|
||||
macro_inspect(1)
|
||||
#=> 1
|
||||
#=> 1
|
||||
|
||||
fun_inspect(1)
|
||||
#=> 1
|
||||
#=> 1
|
||||
|
||||
So far they behave the same, as we are passing an integer as argument.
|
||||
But what happens when we pass an expresion:
|
||||
|
||||
macro_inspect(1 + 2)
|
||||
#=> {:+, [line: 3], [1, 2]}
|
||||
#=> 3
|
||||
|
||||
fun_inspect(1 + 2)
|
||||
#=> 3
|
||||
#=> 3
|
||||
|
||||
The macro receives the representation of the code given as argument,
|
||||
while a function receives the result of the code given as argument.
|
||||
A macro must return a superset of the code representation. See
|
||||
`t:input/0` and `t:output/0` for more information.
|
||||
|
||||
To learn more about Elixir's AST and how to build them programmatically,
|
||||
see `quote/2`.
|
||||
Conveniences for working with macros.
|
||||
|
||||
## Custom Sigils
|
||||
|
||||
Macros are also commonly used to implement custom sigils. To create a custom
|
||||
sigil, define a function with the name `sigil_{identifier}` that takes two
|
||||
arguments. The first argument will be the string, the second will be a charlist
|
||||
containing any modifiers. If the sigil is lower case (such as `sigil_x`) then
|
||||
the string argument will allow interpolation. If the sigil is upper case
|
||||
(such as `sigil_X`) then the string will not be interpolated.
|
||||
To create a custom sigil, define a function with the name
|
||||
`sigil_{identifier}` that takes two arguments. The first argument will be
|
||||
the string, the second will be a charlist containing any modifiers. If the
|
||||
sigil is lower case (such as `sigil_x`) then the string argument will allow
|
||||
interpolation. If the sigil is upper case (such as `sigil_X`) then the string
|
||||
will not be interpolated.
|
||||
|
||||
Valid modifiers include only lower and upper case letters. Other characters
|
||||
will cause a syntax error.
|
||||
@@ -105,94 +59,13 @@ defmodule Macro do
|
||||
alias Code.Identifier
|
||||
|
||||
@typedoc "Abstract Syntax Tree (AST)"
|
||||
@type t :: input
|
||||
@type t :: expr | literal
|
||||
|
||||
@typedoc "The inputs of a macro"
|
||||
@type input ::
|
||||
input_expr
|
||||
| {input, input}
|
||||
| [input]
|
||||
| atom
|
||||
| number
|
||||
| binary
|
||||
@typedoc "Represents expressions in the AST"
|
||||
@type expr :: {expr | atom, keyword, atom | [t]}
|
||||
|
||||
@typep input_expr :: {input_expr | atom, metadata, atom | [input]}
|
||||
|
||||
@typedoc "The output of a macro"
|
||||
@type output ::
|
||||
output_expr
|
||||
| {output, output}
|
||||
| [output]
|
||||
| atom
|
||||
| number
|
||||
| binary
|
||||
| captured_remote_function
|
||||
| pid
|
||||
|
||||
@typep output_expr :: {output_expr | atom, metadata, atom | [output]}
|
||||
|
||||
@typedoc """
|
||||
A keyword list of AST metadata.
|
||||
|
||||
The metadata in Elixir AST is a keyword list of values. Any key can be used
|
||||
and different parts of the compiler may use different keys. For example,
|
||||
the AST received by a macro will always include the `:line` annotation,
|
||||
while the AST emitted by `quote/2` will only have the `:line` annotation if
|
||||
the `:line` option is provided.
|
||||
|
||||
The following metadata keys are public:
|
||||
|
||||
* `:context` - Defines the context in which the AST was generated.
|
||||
For example, `quote/2` will include the module calling `quote/2`
|
||||
as the context. This is often used to distinguish regular code from code
|
||||
generated by a macro or by `quote/2`.
|
||||
* `:counter` - The variable counter used for variable hygiene. In terms of
|
||||
the compiler, each variable is identified by the combination of either
|
||||
`name` and `metadata[:counter]`, or `name` and `context`.
|
||||
* `:generated` - Whether the code should be considered as generated by
|
||||
the compiler or not. This means the compiler and tools like Dialyzer may not
|
||||
emit certain warnings.
|
||||
* `:keep` - Used by `quote/2` with the option `location: :keep` to annotate
|
||||
the file and the line number of the quoted source.
|
||||
* `:line` - The line number of the AST node.
|
||||
|
||||
The following metadata keys are enabled by `Code.string_to_quoted/2`:
|
||||
|
||||
* `:closing` - contains metadata about the closing pair, such as a `}`
|
||||
in a tuple or in a map, or such as the closing `)` in a function call
|
||||
with parens. The `:closing` does not delimit the end of expression if
|
||||
there are `:do` and `:end` metadata (when `:token_metadata` is true)
|
||||
* `:column` - the column number of the AST node (when `:columns` is true)
|
||||
* `:delimiter` - contains the opening delimiter for sigils, strings,
|
||||
and charlists as a string (such as `"{"`, `"/"`, `"'"`, and the like)
|
||||
* `:format` - set to `:keyword` when an atom is defined as a keyword
|
||||
* `:do` - contains metadata about the `do` location in a function call with
|
||||
`do/end` blocks (when `:token_metadata` is true)
|
||||
* `:end` - contains metadata about the `end` location in a function call with
|
||||
`do/end` blocks (when `:token_metadata` is true)
|
||||
* `:end_of_expression` - denotes when the end of expression effectively
|
||||
happens. Available for all expressions except the last one inside a
|
||||
`__block__` (when `:token_metadata` is true)
|
||||
|
||||
The following metadata keys are private:
|
||||
|
||||
* `:alias` - Used for alias hygiene.
|
||||
* `:ambiguous_op` - Used for improved error messages in the compiler.
|
||||
* `:import` - Used for import hygiene.
|
||||
* `:var` - Used for improved error messages on undefined variables.
|
||||
|
||||
Do not rely on them as they may change or be fully removed in future versions
|
||||
of the language. They are often used by `quote/2` and the compiler to provide
|
||||
features like hygiene, better error messages, and so forth.
|
||||
|
||||
If you introduce custom keys into the AST metadata, please make sure to prefix
|
||||
them with the name of your library or application, so that they will not conflict
|
||||
with keys that could potentially be introduced by the compiler in the future.
|
||||
"""
|
||||
@type metadata :: keyword
|
||||
|
||||
@typedoc "A captured remote function in the format of &Mod.fun/arity"
|
||||
@type captured_remote_function :: fun
|
||||
@typedoc "Represents literals in the AST"
|
||||
@type literal :: atom | number | binary | fun | {t, t} | [t]
|
||||
|
||||
@doc """
|
||||
Breaks a pipeline expression into a list.
|
||||
@@ -265,7 +138,7 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
# {:fn, _, _} is what we get when we pipe into an anonymous function without
|
||||
# calling it, for example, `:foo |> (fn x -> x end)`.
|
||||
# calling it, e.g., `:foo |> (fn x -> x end)`.
|
||||
def pipe(expr, {:fn, _, _}, _integer) do
|
||||
raise ArgumentError,
|
||||
"cannot pipe #{to_string(expr)} into an anonymous function without" <>
|
||||
@@ -277,26 +150,13 @@ defmodule Macro do
|
||||
{call, line, List.insert_at([], integer, expr)}
|
||||
end
|
||||
|
||||
def pipe(_expr, {op, _line, [arg]}, _integer) when op == :+ or op == :- do
|
||||
raise ArgumentError,
|
||||
"piping into a unary operator is not supported, please use the qualified name: " <>
|
||||
"Kernel.#{op}(#{to_string(arg)}), instead of #{op}#{to_string(arg)}"
|
||||
end
|
||||
|
||||
def pipe(expr, {op, line, args} = op_args, integer) when is_list(args) do
|
||||
cond do
|
||||
is_atom(op) and Identifier.unary_op(op) != :error ->
|
||||
raise ArgumentError,
|
||||
"cannot pipe #{to_string(expr)} into #{to_string(op_args)}, " <>
|
||||
"the #{to_string(op)} operator can only take one argument"
|
||||
|
||||
is_atom(op) and Identifier.binary_op(op) != :error ->
|
||||
raise ArgumentError,
|
||||
"cannot pipe #{to_string(expr)} into #{to_string(op_args)}, " <>
|
||||
"the #{to_string(op)} operator can only take two arguments"
|
||||
|
||||
true ->
|
||||
{op, line, List.insert_at(args, integer, expr)}
|
||||
def pipe(expr, {call, line, args} = call_args, integer) when is_list(args) do
|
||||
if is_atom(call) and Identifier.binary_op(call) != :error do
|
||||
raise ArgumentError,
|
||||
"cannot pipe #{to_string(expr)} into #{to_string(call_args)}, " <>
|
||||
"the #{to_string(call)} operator can only take two arguments"
|
||||
else
|
||||
{call, line, List.insert_at(args, integer, expr)}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -346,11 +206,7 @@ defmodule Macro do
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec generate_arguments(0, context :: atom) :: []
|
||||
@spec generate_arguments(pos_integer, context) :: [{atom, [], context}, ...] when context: atom
|
||||
def generate_arguments(amount, context)
|
||||
|
||||
def generate_arguments(0, context) when is_atom(context), do: []
|
||||
def generate_arguments(0, _), do: []
|
||||
|
||||
def generate_arguments(amount, context)
|
||||
when is_integer(amount) and amount > 0 and is_atom(context) do
|
||||
@@ -646,7 +502,7 @@ defmodule Macro do
|
||||
|
||||
In this setup, Elixir will escape the following: `\0`, `\a`, `\b`,
|
||||
`\d`, `\e`, `\f`, `\n`, `\r`, `\s`, `\t` and `\v`. Bytes can be
|
||||
given as hexadecimals via `\xNN` and Unicode code points as
|
||||
given as hexadecimals via `\xNN` and Unicode Codepoints as
|
||||
`\uNNNN` escapes.
|
||||
|
||||
This function is commonly used on sigil implementations
|
||||
@@ -675,7 +531,7 @@ defmodule Macro do
|
||||
## Map
|
||||
|
||||
The map must be a function. The function receives an integer
|
||||
representing the code point of the character it wants to unescape.
|
||||
representing the codepoint of the character it wants to unescape.
|
||||
Here is the default mapping function implemented by Elixir:
|
||||
|
||||
def unescape_map(unicode), do: true
|
||||
@@ -696,8 +552,8 @@ defmodule Macro do
|
||||
If the `unescape_map/1` function returns `false`, the char is
|
||||
not escaped and the backslash is kept in the string.
|
||||
|
||||
Hexadecimals and Unicode code points will be escaped if the map
|
||||
function returns `true` for `?x`. Unicode code points if the map
|
||||
Hexadecimals and Unicode codepoints will be escaped if the map
|
||||
function returns `true` for `?x`. Unicode codepoints if the map
|
||||
function returns `true` for `?u`.
|
||||
|
||||
## Examples
|
||||
@@ -757,7 +613,7 @@ defmodule Macro do
|
||||
def to_string(tree, fun \\ fn _ast, string -> string end)
|
||||
|
||||
# Variables
|
||||
def to_string({var, _, context} = ast, fun) when is_atom(var) and is_atom(context) do
|
||||
def to_string({var, _, atom} = ast, fun) when is_atom(atom) do
|
||||
fun.(ast, Atom.to_string(var))
|
||||
end
|
||||
|
||||
@@ -895,16 +751,6 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
# All other calls
|
||||
def to_string({target, meta, []} = ast, fun) do
|
||||
target = call_to_string(target, fun)
|
||||
|
||||
if meta[:no_parens] do
|
||||
fun.(ast, target)
|
||||
else
|
||||
fun.(ast, target <> "()")
|
||||
end
|
||||
end
|
||||
|
||||
def to_string({target, _, args} = ast, fun) when is_list(args) do
|
||||
with :error <- unary_call(ast, fun),
|
||||
:error <- binary_call(ast, fun),
|
||||
@@ -962,10 +808,9 @@ defmodule Macro do
|
||||
Kernel.inspect(value, limit: :infinity, printable_limit: :infinity)
|
||||
end
|
||||
|
||||
defp bitpart_to_string({:"::", _, [left, right]} = ast, fun) do
|
||||
defp bitpart_to_string({:::, _, [left, right]} = ast, fun) do
|
||||
result =
|
||||
op_to_string(left, fun, :"::", :left) <>
|
||||
"::" <> bitmods_to_string(right, fun, :"::", :right)
|
||||
op_to_string(left, fun, :::, :left) <> "::" <> bitmods_to_string(right, fun, :::, :right)
|
||||
|
||||
fun.(ast, result)
|
||||
end
|
||||
@@ -998,7 +843,7 @@ defmodule Macro do
|
||||
# Check if we have an interpolated string.
|
||||
defp interpolated?({:<<>>, _, [_ | _] = parts}) do
|
||||
Enum.all?(parts, fn
|
||||
{:"::", _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
|
||||
binary when is_binary(binary) -> true
|
||||
_ -> false
|
||||
end)
|
||||
@@ -1008,33 +853,20 @@ defmodule Macro do
|
||||
false
|
||||
end
|
||||
|
||||
defp interpolate(ast, fun), do: interpolate(ast, "\"", "\"", fun)
|
||||
|
||||
defp interpolate({:<<>>, _, [parts]}, left, right, _) when left in [~s["""\n], ~s['''\n]] do
|
||||
<<left::binary, parts::binary, right::binary>>
|
||||
end
|
||||
|
||||
defp interpolate({:<<>>, _, parts}, left, right, fun) do
|
||||
defp interpolate({:<<>>, _, parts}, fun) do
|
||||
parts =
|
||||
Enum.map_join(parts, "", fn
|
||||
{:"::", _, [{{:., _, [Kernel, :to_string]}, _, [arg]}, {:binary, _, _}]} ->
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [arg]}, {:binary, _, _}]} ->
|
||||
"\#{" <> to_string(arg, fun) <> "}"
|
||||
|
||||
binary when is_binary(binary) ->
|
||||
binary = inspect_no_limit(binary)
|
||||
binary = binary_part(binary, 1, byte_size(binary) - 2)
|
||||
escape_sigil(binary, left)
|
||||
binary_part(binary, 1, byte_size(binary) - 2)
|
||||
end)
|
||||
|
||||
<<left::binary, parts::binary, right::binary>>
|
||||
<<?", parts::binary, ?">>
|
||||
end
|
||||
|
||||
defp escape_sigil(parts, "("), do: String.replace(parts, ")", ~S"\)")
|
||||
defp escape_sigil(parts, "{"), do: String.replace(parts, "}", ~S"\}")
|
||||
defp escape_sigil(parts, "["), do: String.replace(parts, "]", ~S"\]")
|
||||
defp escape_sigil(parts, "<"), do: String.replace(parts, ">", ~S"\>")
|
||||
defp escape_sigil(parts, delimiter), do: String.replace(parts, delimiter, "\\#{delimiter}")
|
||||
|
||||
defp module_to_string(atom, _fun) when is_atom(atom) do
|
||||
inspect_no_limit(atom)
|
||||
end
|
||||
@@ -1094,22 +926,25 @@ defmodule Macro do
|
||||
:error
|
||||
end
|
||||
|
||||
defp sigil_call({sigil, meta, [{:<<>>, _, _} = parts, args]} = ast, fun)
|
||||
defp sigil_call({sigil, _, [{:<<>>, _, _} = parts, args]} = ast, fun)
|
||||
when is_atom(sigil) and is_list(args) do
|
||||
delimiter = Keyword.get(meta, :delimiter, "\"")
|
||||
{left, right} = delimiter_pair(delimiter)
|
||||
|
||||
case Atom.to_string(sigil) do
|
||||
<<"sigil_", name>> when name >= ?A and name <= ?Z ->
|
||||
args = sigil_args(args, fun)
|
||||
{:<<>>, _, [binary]} = parts
|
||||
formatted = <<?~, name, left::binary, binary::binary, right::binary, args::binary>>
|
||||
|
||||
formatted =
|
||||
if :binary.last(binary) == ?\n do
|
||||
binary = String.replace(binary, ~s["""], ~s["\\""])
|
||||
<<?~, name, ~s["""\n], binary::binary, ~s["""], sigil_args(args, fun)::binary>>
|
||||
else
|
||||
{left, right} = select_sigil_container(binary)
|
||||
<<?~, name, left, binary::binary, right, sigil_args(args, fun)::binary>>
|
||||
end
|
||||
|
||||
{:ok, fun.(ast, formatted)}
|
||||
|
||||
<<"sigil_", name>> when name >= ?a and name <= ?z ->
|
||||
args = sigil_args(args, fun)
|
||||
formatted = "~" <> <<name>> <> interpolate(parts, left, right, fun) <> args
|
||||
{:ok, fun.(ast, formatted)}
|
||||
{:ok, fun.(ast, "~" <> <<name>> <> interpolate(parts, fun) <> sigil_args(args, fun))}
|
||||
|
||||
_ ->
|
||||
:error
|
||||
@@ -1120,13 +955,17 @@ defmodule Macro do
|
||||
:error
|
||||
end
|
||||
|
||||
defp delimiter_pair("["), do: {"[", "]"}
|
||||
defp delimiter_pair("{"), do: {"{", "}"}
|
||||
defp delimiter_pair("("), do: {"(", ")"}
|
||||
defp delimiter_pair("<"), do: {"<", ">"}
|
||||
defp delimiter_pair("\"\"\""), do: {"\"\"\"\n", "\"\"\""}
|
||||
defp delimiter_pair("'''"), do: {"'''\n", "'''"}
|
||||
defp delimiter_pair(str), do: {str, str}
|
||||
defp select_sigil_container(binary) do
|
||||
cond do
|
||||
:binary.match(binary, ["\""]) == :nomatch -> {?", ?"}
|
||||
:binary.match(binary, ["\'"]) == :nomatch -> {?', ?'}
|
||||
:binary.match(binary, ["(", ")"]) == :nomatch -> {?(, ?)}
|
||||
:binary.match(binary, ["[", "]"]) == :nomatch -> {?[, ?]}
|
||||
:binary.match(binary, ["{", "}"]) == :nomatch -> {?{, ?}}
|
||||
:binary.match(binary, ["<", ">"]) == :nomatch -> {?<, ?>}
|
||||
true -> {?/, ?/}
|
||||
end
|
||||
end
|
||||
|
||||
defp sigil_args([], _fun), do: ""
|
||||
defp sigil_args(args, fun), do: fun.(args, List.to_string(args))
|
||||
@@ -1298,11 +1137,9 @@ defmodule Macro do
|
||||
* Module attributes reader (`@foo`)
|
||||
|
||||
If the expression cannot be expanded, it returns the expression
|
||||
itself. This function does not traverse the AST, only the root
|
||||
node is expanded.
|
||||
|
||||
`expand_once/2` performs the expansion just once. Check `expand/2`
|
||||
to perform expansion until the node can no longer be expanded.
|
||||
itself. Notice that `expand_once/2` performs the expansion just
|
||||
once and it is not recursive. Check `expand/2` for expansion
|
||||
until the node can no longer be expanded.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1369,10 +1206,10 @@ defmodule Macro do
|
||||
elem(do_expand_once(ast, env), 0)
|
||||
end
|
||||
|
||||
defp do_expand_once({:__aliases__, meta, _} = original, env) do
|
||||
case :elixir_aliases.expand(original, env) do
|
||||
defp do_expand_once({:__aliases__, _, _} = original, env) do
|
||||
case :elixir_aliases.expand(original, env.aliases, env.macro_aliases, env.lexical_tracker) do
|
||||
receiver when is_atom(receiver) ->
|
||||
:elixir_env.trace({:alias_reference, meta, receiver}, env)
|
||||
:elixir_lexical.record_remote(receiver, env.function, env.lexical_tracker)
|
||||
{receiver, true}
|
||||
|
||||
aliases ->
|
||||
@@ -1381,7 +1218,7 @@ defmodule Macro do
|
||||
case :lists.all(&is_atom/1, aliases) do
|
||||
true ->
|
||||
receiver = :elixir_aliases.concat(aliases)
|
||||
:elixir_env.trace({:alias_reference, meta, receiver}, env)
|
||||
:elixir_lexical.record_remote(receiver, env.function, env.lexical_tracker)
|
||||
{receiver, true}
|
||||
|
||||
false ->
|
||||
@@ -1408,9 +1245,17 @@ defmodule Macro do
|
||||
end
|
||||
end
|
||||
|
||||
defp do_expand_once({atom, meta, context} = original, _env)
|
||||
# Expand possible macro import invocation
|
||||
defp do_expand_once({atom, meta, context} = original, env)
|
||||
when is_atom(atom) and is_list(meta) and is_atom(context) do
|
||||
{original, false}
|
||||
if Macro.Env.has_var?(env, {atom, Keyword.get(meta, :counter, context)}) do
|
||||
{original, false}
|
||||
else
|
||||
case do_expand_once({atom, meta, []}, env) do
|
||||
{_, true} = exp -> exp
|
||||
{_, false} -> {original, false}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp do_expand_once({atom, meta, args} = original, env)
|
||||
@@ -1495,67 +1340,37 @@ defmodule Macro do
|
||||
def operator?(name, arity) when is_atom(name) and is_integer(arity), do: false
|
||||
|
||||
@doc """
|
||||
Returns `true` if the given quoted expression represents a quoted literal.
|
||||
|
||||
Atoms, numbers, and functions are always literals. Binaries, lists, tuples,
|
||||
maps, and structs are only literals if all of their terms are also literals.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.quoted_literal?(quote(do: "foo"))
|
||||
true
|
||||
iex> Macro.quoted_literal?(quote(do: {"foo", 1}))
|
||||
true
|
||||
iex> Macro.quoted_literal?(quote(do: {"foo", 1, :baz}))
|
||||
true
|
||||
iex> Macro.quoted_literal?(quote(do: %{foo: "bar"}))
|
||||
true
|
||||
iex> Macro.quoted_literal?(quote(do: %URI{path: "/"}))
|
||||
true
|
||||
iex> Macro.quoted_literal?(quote(do: URI.parse("/")))
|
||||
false
|
||||
iex> Macro.quoted_literal?(quote(do: {foo, var}))
|
||||
false
|
||||
|
||||
Returns `true` if the given quoted expression is an AST literal.
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec quoted_literal?(t) :: boolean
|
||||
@spec quoted_literal?(literal) :: true
|
||||
@spec quoted_literal?(expr) :: false
|
||||
def quoted_literal?(term)
|
||||
|
||||
def quoted_literal?({:__aliases__, _, args}),
|
||||
do: quoted_literal?(args)
|
||||
|
||||
def quoted_literal?({:%, _, [left, right]}),
|
||||
do: quoted_literal?(left) and quoted_literal?(right)
|
||||
|
||||
def quoted_literal?({:%{}, _, args}), do: quoted_literal?(args)
|
||||
def quoted_literal?({:{}, _, args}), do: quoted_literal?(args)
|
||||
def quoted_literal?({left, right}), do: quoted_literal?(left) and quoted_literal?(right)
|
||||
def quoted_literal?(list) when is_list(list), do: Enum.all?(list, "ed_literal?/1)
|
||||
|
||||
def quoted_literal?(term),
|
||||
do: is_atom(term) or is_number(term) or is_binary(term) or is_function(term)
|
||||
def quoted_literal?(term) do
|
||||
is_atom(term) or is_number(term) or is_binary(term) or is_function(term)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives an AST node and expands it until it can no longer
|
||||
be expanded.
|
||||
|
||||
Note this function does not traverse the AST, only the root
|
||||
node is expanded.
|
||||
|
||||
This function uses `expand_once/2` under the hood. Check
|
||||
it out for more information and examples.
|
||||
"""
|
||||
def expand(ast, env) do
|
||||
expand_until({ast, true}, env)
|
||||
def expand(tree, env) do
|
||||
expand_until({tree, true}, env)
|
||||
end
|
||||
|
||||
defp expand_until({ast, true}, env) do
|
||||
expand_until(do_expand_once(ast, env), env)
|
||||
defp expand_until({tree, true}, env) do
|
||||
expand_until(do_expand_once(tree, env), env)
|
||||
end
|
||||
|
||||
defp expand_until({ast, false}, _env) do
|
||||
ast
|
||||
defp expand_until({tree, false}, _env) do
|
||||
tree
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
||||
+66
-77
@@ -21,31 +21,31 @@ defmodule Macro.Env do
|
||||
|
||||
It contains the following fields:
|
||||
|
||||
* `aliases` - a list of two-element tuples, where the first
|
||||
element is the aliased name and the second one the actual name
|
||||
* `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
|
||||
* `functions` - a list of functions imported from each module
|
||||
* `line` - the current line as an integer
|
||||
* `macro_aliases` - a list of aliases defined inside the current macro
|
||||
* `macros` - a list of macros imported from each module
|
||||
* `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`:
|
||||
|
||||
* `contextual_vars`
|
||||
* `current_vars`
|
||||
* `lexical_tracker`
|
||||
* `prematch_vars`
|
||||
* `tracers`
|
||||
* `unused_vars`
|
||||
* `prematch_vars`
|
||||
* `contextual_vars`
|
||||
|
||||
The following fields are deprecated and must not be accessed or relied on:
|
||||
|
||||
@@ -53,80 +53,69 @@ defmodule Macro.Env do
|
||||
|
||||
"""
|
||||
|
||||
@type aliases :: [{module, module}]
|
||||
@type context :: :match | :guard | nil
|
||||
@type context_modules :: [module]
|
||||
@type file :: binary
|
||||
@type functions :: [{module, [name_arity]}]
|
||||
@type lexical_tracker :: pid | nil
|
||||
@type line :: non_neg_integer
|
||||
@type macro_aliases :: [{module, {term, module}}]
|
||||
@type macros :: [{module, [name_arity]}]
|
||||
@type name_arity :: {atom, arity}
|
||||
@type file :: binary
|
||||
@type line :: non_neg_integer
|
||||
@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 contextual_vars :: [atom]
|
||||
@typep current_vars ::
|
||||
{%{optional(variable) => {var_version, var_type}},
|
||||
%{optional(variable) => {var_version, var_type}} | false}
|
||||
@typep unused_vars ::
|
||||
{%{optional({atom, var_version}) => non_neg_integer | false}, non_neg_integer}
|
||||
@typep prematch_vars ::
|
||||
{%{optional(variable) => {var_version, var_type}}, non_neg_integer}
|
||||
| :warn
|
||||
| :raise
|
||||
| :pin
|
||||
| :apply
|
||||
@typep tracers :: [module]
|
||||
@typep vars :: [variable]
|
||||
@typep var_type :: :term
|
||||
@typep var_version :: non_neg_integer
|
||||
@typep vars :: [variable]
|
||||
@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,
|
||||
contextual_vars: contextual_vars,
|
||||
current_vars: current_vars,
|
||||
file: file,
|
||||
function: name_arity | nil,
|
||||
functions: functions,
|
||||
lexical_tracker: lexical_tracker,
|
||||
line: line,
|
||||
macro_aliases: macro_aliases,
|
||||
macros: macros,
|
||||
module: atom,
|
||||
prematch_vars: prematch_vars,
|
||||
unused_vars: unused_vars,
|
||||
file: file,
|
||||
line: line,
|
||||
function: name_arity | nil,
|
||||
context: context,
|
||||
requires: requires,
|
||||
tracers: tracers,
|
||||
vars: 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
|
||||
}
|
||||
|
||||
# TODO: Remove :vars field on v2.0
|
||||
def __struct__ do
|
||||
%{
|
||||
__struct__: __MODULE__,
|
||||
aliases: [],
|
||||
context: nil,
|
||||
context_modules: [],
|
||||
contextual_vars: [],
|
||||
current_vars: {%{}, %{}},
|
||||
file: "nofile",
|
||||
function: nil,
|
||||
functions: [],
|
||||
lexical_tracker: nil,
|
||||
line: 0,
|
||||
macro_aliases: [],
|
||||
macros: [],
|
||||
module: nil,
|
||||
prematch_vars: :warn,
|
||||
file: "nofile",
|
||||
line: 0,
|
||||
function: nil,
|
||||
context: nil,
|
||||
requires: [],
|
||||
tracers: [],
|
||||
unused_vars: {%{}, 0},
|
||||
vars: []
|
||||
aliases: [],
|
||||
functions: [],
|
||||
macros: [],
|
||||
macro_aliases: [],
|
||||
context_modules: [],
|
||||
vars: [],
|
||||
unused_vars: %{},
|
||||
current_vars: %{},
|
||||
prematch_vars: :warn,
|
||||
lexical_tracker: nil,
|
||||
contextual_vars: []
|
||||
}
|
||||
end
|
||||
|
||||
@@ -146,8 +135,8 @@ defmodule Macro.Env do
|
||||
@spec vars(t) :: [variable]
|
||||
def vars(env)
|
||||
|
||||
def vars(%{__struct__: Macro.Env, current_vars: {read, _}}) do
|
||||
Map.keys(read)
|
||||
def vars(%{__struct__: Macro.Env, current_vars: current_vars}) do
|
||||
Map.keys(current_vars)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -157,8 +146,8 @@ defmodule Macro.Env do
|
||||
@spec has_var?(t, variable) :: boolean()
|
||||
def has_var?(env, var)
|
||||
|
||||
def has_var?(%{__struct__: Macro.Env, current_vars: {read, _}}, var) do
|
||||
Map.has_key?(read, var)
|
||||
def has_var?(%{__struct__: Macro.Env, current_vars: current_vars}, var) do
|
||||
Map.has_key?(current_vars, var)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -180,8 +169,8 @@ defmodule Macro.Env do
|
||||
env
|
||||
end
|
||||
|
||||
def to_match(%{__struct__: Macro.Env, current_vars: {read, _}, unused_vars: {_, counter}} = env) do
|
||||
%{env | context: :match, prematch_vars: {read, counter}}
|
||||
def to_match(%{__struct__: Macro.Env, current_vars: vars} = env) do
|
||||
%{env | context: :match, prematch_vars: vars}
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
||||
+42
-87
@@ -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> %{}
|
||||
%{}
|
||||
@@ -85,19 +91,6 @@ defmodule Map do
|
||||
iex> %{map | three: 3}
|
||||
** (KeyError) key :three not found
|
||||
|
||||
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`
|
||||
|
||||
"""
|
||||
|
||||
@type key :: any
|
||||
@@ -213,8 +206,8 @@ defmodule Map do
|
||||
|> :maps.from_list()
|
||||
end
|
||||
|
||||
defp new_transform([element | rest], fun, acc) do
|
||||
new_transform(rest, fun, [fun.(element) | acc])
|
||||
defp new_transform([item | rest], fun, acc) do
|
||||
new_transform(rest, fun, [fun.(item) | acc])
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -265,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
|
||||
@@ -341,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
|
||||
|
||||
@@ -383,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
|
||||
@@ -421,9 +409,8 @@ defmodule Map do
|
||||
Gets the value for a specific `key` in `map`.
|
||||
|
||||
If `key` is present in `map` with value `value`, then `value` is
|
||||
returned. Otherwise, `default` is returned.
|
||||
|
||||
If `default` is not provided, `nil` is used.
|
||||
returned. Otherwise, `default` is returned (which is `nil` unless
|
||||
specified otherwise).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -634,31 +621,6 @@ defmodule Map do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns and removes the value associated with `key` in `map` or 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, 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`.
|
||||
|
||||
@@ -685,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
|
||||
|
||||
@@ -702,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 """
|
||||
@@ -742,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
|
||||
@@ -938,6 +892,7 @@ 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)
|
||||
|
||||
+43
-58
@@ -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 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,15 +192,19 @@ 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 """
|
||||
@@ -241,7 +228,7 @@ defmodule MapSet do
|
||||
# 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 """
|
||||
@@ -275,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 """
|
||||
@@ -323,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 """
|
||||
@@ -388,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
|
||||
|
||||
+99
-222
@@ -8,8 +8,7 @@ defmodule Module do
|
||||
After a module is compiled, using many of the functions in
|
||||
this module will raise errors, since it is out of their scope
|
||||
to inspect runtime data. Most of the runtime data can be inspected
|
||||
via the [`__info__/1`](`c:Module.__info__/1`) function attached to
|
||||
each compiled module.
|
||||
via the `__info__/1` function attached to each compiled module.
|
||||
|
||||
## Module attributes
|
||||
|
||||
@@ -56,9 +55,6 @@ defmodule Module do
|
||||
If the behaviour changes or `URI.HTTP` does not implement
|
||||
one of the callbacks, a warning will be raised.
|
||||
|
||||
For detailed documentation, see the
|
||||
[behaviour typespec documentation](typespecs.html#behaviours).
|
||||
|
||||
### `@impl`
|
||||
|
||||
To aid in the correct implementation of behaviours, you may optionally declare
|
||||
@@ -140,7 +136,7 @@ defmodule Module do
|
||||
end
|
||||
|
||||
The Mix compiler automatically looks for calls to deprecated modules
|
||||
and emit warnings during compilation.
|
||||
and emit warnings during compilation, computed via `mix xref warnings`.
|
||||
|
||||
Using the `@deprecated` attribute will also be reflected in the
|
||||
documentation of the given function and macro. You can choose between
|
||||
@@ -178,7 +174,7 @@ defmodule Module do
|
||||
|
||||
Accepts a string (often a heredoc) or `false` where `@doc false` will
|
||||
make the entity invisible to documentation extraction tools like
|
||||
[`ExDoc`](https://hexdocs.pm/ex_doc/). For example:
|
||||
ExDoc. For example:
|
||||
|
||||
defmodule MyModule do
|
||||
@typedoc "This type"
|
||||
@@ -201,10 +197,9 @@ defmodule Module do
|
||||
|
||||
As can be seen in the example above, `@doc` and `@typedoc` also accept
|
||||
a keyword list that serves as a way to provide arbitrary metadata
|
||||
about the entity. Tools like [`ExDoc`](https://hexdocs.pm/ex_doc/) and
|
||||
`IEx` may use this information to display annotations. A common use
|
||||
case is `since` that may be used to annotate in which version the
|
||||
function was introduced.
|
||||
about the entity. Tools like ExDoc and IEx may use this information to
|
||||
display annotations. A common use case is `since` that may be used
|
||||
to annotate in which version the function was introduced.
|
||||
|
||||
As illustrated in the example, it is possible to use these attributes
|
||||
more than once before an entity. However, the compiler will warn if
|
||||
@@ -216,9 +211,6 @@ defmodule Module do
|
||||
there are a few reserved keys that will be ignored and warned if used.
|
||||
Currently these are: `:opaque` and `:defaults`.
|
||||
|
||||
Once this module is compiled, this information becomes available via
|
||||
the `Code.fetch_docs/1` function.
|
||||
|
||||
### `@dialyzer`
|
||||
|
||||
Defines warnings to request or suppress when using a version of
|
||||
@@ -277,15 +269,12 @@ defmodule Module do
|
||||
|
||||
Accepts a string (often a heredoc) or `false` where `@moduledoc false`
|
||||
will make the module invisible to documentation extraction tools like
|
||||
[`ExDoc`](https://hexdocs.pm/ex_doc/).
|
||||
ExDoc.
|
||||
|
||||
Similarly to `@doc` also accepts a keyword list to provide metadata
|
||||
about the module. For more details, see the documentation of `@doc`
|
||||
above.
|
||||
|
||||
Once this module is compiled, this information becomes available via
|
||||
the `Code.fetch_docs/1` function.
|
||||
|
||||
### `@on_definition`
|
||||
|
||||
A hook that will be invoked when each function or macro in the current
|
||||
@@ -348,9 +337,8 @@ defmodule Module do
|
||||
### Custom attributes
|
||||
|
||||
In addition to the built-in attributes outlined above, custom attributes may
|
||||
also be added. Custom attributes are expressed using the `@/1` operator followed
|
||||
by a valid variable name. The value given to the custom attribute must be a valid
|
||||
Elixir value:
|
||||
also be added. A custom attribute is any valid identifier prefixed with an
|
||||
`@` and followed by a valid Elixir value:
|
||||
|
||||
defmodule MyModule do
|
||||
@custom_attr [some: "stuff"]
|
||||
@@ -373,7 +361,7 @@ defmodule Module do
|
||||
When just a module is provided, the function is assumed to be
|
||||
`__after_compile__/2`.
|
||||
|
||||
Callbacks will run in the order they are registered.
|
||||
Callbacks registered first will run last.
|
||||
|
||||
#### Example
|
||||
|
||||
@@ -397,11 +385,10 @@ defmodule Module do
|
||||
When just a module is provided, the function/macro is assumed to be
|
||||
`__before_compile__/1`.
|
||||
|
||||
Callbacks will run in the order they are registered. Any overridable
|
||||
definition will be made concrete before the first callback runs.
|
||||
A definition may be made overridable again in another before compile
|
||||
callback and it will be made concrete one last time after all callbacks
|
||||
run.
|
||||
Callbacks registered first will run last. Any overridable definition
|
||||
will be made concrete before the first callback runs. A definition may
|
||||
be made overridable again in another before compile callback and it
|
||||
will be made concrete one last time after after all callbacks run.
|
||||
|
||||
*Note*: unlike `@after_compile`, the callback function/macro must
|
||||
be placed in a separate module (because when the callback is invoked,
|
||||
@@ -439,6 +426,10 @@ defmodule Module do
|
||||
* the list of quoted guards
|
||||
* the quoted function body
|
||||
|
||||
Note the hook receives the quoted arguments and it is invoked before
|
||||
the function is stored in the module. So `Module.defines?/2` will return
|
||||
`false` for the first clause of every function.
|
||||
|
||||
If the function/macro being defined has multiple clauses, the hook will
|
||||
be called for each clause.
|
||||
|
||||
@@ -482,10 +473,10 @@ defmodule Module do
|
||||
below:
|
||||
|
||||
* `@compile :debug_info` - includes `:debug_info` regardless of the
|
||||
corresponding setting in `Code.get_compiler_option/1`
|
||||
corresponding setting in `Code.compiler_options/1`
|
||||
|
||||
* `@compile {:debug_info, false}` - disables `:debug_info` regardless
|
||||
of the corresponding setting in `Code.get_compiler_option/1`
|
||||
of the corresponding setting in `Code.compiler_options/1`
|
||||
|
||||
* `@compile {:inline, some_fun: 2, other_fun: 3}` - inlines the given
|
||||
name/arity pairs. Inlining is applied locally, calls from another
|
||||
@@ -495,10 +486,6 @@ defmodule Module do
|
||||
modules after compilation. Instead, the module will be loaded after
|
||||
it is dispatched to
|
||||
|
||||
* `@compile {:no_warn_undefined, Mod}` or
|
||||
`@compile {:no_warn_undefined, {Mod, fun, arity}}` - does not warn if
|
||||
the given module or the given `Mod.fun/arity` are not defined
|
||||
|
||||
You can see a handful more options used by the Erlang compiler in
|
||||
the documentation for the [`:compile` module](http://www.erlang.org/doc/man/compile.html).
|
||||
'''
|
||||
@@ -506,37 +493,27 @@ defmodule Module do
|
||||
@typep definition :: {atom, arity}
|
||||
@typep def_kind :: :def | :defp | :defmacro | :defmacrop
|
||||
|
||||
@extra_error_msg_defines? "Use Kernel.function_exported?/3 and Kernel.macro_exported?/3 " <>
|
||||
"to check for public functions and macros instead"
|
||||
|
||||
@extra_error_msg_definitions_in "Use the Module.__info__/1 callback to get public functions and macros instead"
|
||||
|
||||
@doc """
|
||||
Provides runtime information about functions, macros, and other information
|
||||
defined by the module.
|
||||
Provides runtime information about functions and macros defined by the
|
||||
module, etc.
|
||||
|
||||
Each module gets an `__info__/1` function when it's compiled. The function
|
||||
takes one of the following items:
|
||||
takes one of the following atoms:
|
||||
|
||||
* `:attributes` - a keyword list with all persisted attributes
|
||||
* `:functions` - keyword list of public functions along with their arities
|
||||
|
||||
* `:compile` - a list with compiler metadata
|
||||
|
||||
* `:functions` - a keyword list of public functions and their arities
|
||||
|
||||
* `:macros` - a keyword list of public macros and their arities
|
||||
|
||||
* `:md5` - the MD5 of the module
|
||||
* `:macros` - keyword list of public macros along with their arities
|
||||
|
||||
* `:module` - the module atom name
|
||||
|
||||
* `:md5` - the MD5 of the module
|
||||
|
||||
* `:compile` - a list with compiler metadata
|
||||
|
||||
* `:attributes` - a list with all persisted attributes
|
||||
|
||||
"""
|
||||
@callback __info__(:attributes) :: keyword()
|
||||
@callback __info__(:compile) :: [term()]
|
||||
@callback __info__(:functions) :: keyword()
|
||||
@callback __info__(:macros) :: keyword()
|
||||
@callback __info__(:md5) :: binary()
|
||||
@callback __info__(:module) :: module()
|
||||
@callback __info__(:functions | :macros | :module | :md5 | :compile | :attributes) :: term()
|
||||
|
||||
@doc """
|
||||
Checks if a module is open.
|
||||
@@ -607,10 +584,10 @@ defmodule Module do
|
||||
|
||||
def eval_quoted(module, quoted, binding, opts)
|
||||
when is_atom(module) and is_list(binding) and is_list(opts) do
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
assert_not_compiled!(:eval_quoted, module)
|
||||
:elixir_def.reset_last(module)
|
||||
|
||||
{value, binding, _env} =
|
||||
{value, binding, _env, _scope} =
|
||||
:elixir.eval_quoted(quoted, binding, Keyword.put(opts, :module, module))
|
||||
|
||||
{value, binding}
|
||||
@@ -923,13 +900,7 @@ defmodule Module do
|
||||
Use `defines?/3` to assert for a specific type.
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
Use `Kernel.function_exported?/3` and `Kernel.macro_exported?/3` to check for
|
||||
public functions and macros respectively in compiled modules.
|
||||
|
||||
Note that `defines?` returns false for functions and macros that have
|
||||
been defined but then marked as overridable and no other implementation
|
||||
has been provided. You can check the overridable status by calling
|
||||
`overridable?/2`.
|
||||
Use `Kernel.function_exported?/3` to check compiled modules.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -943,7 +914,7 @@ defmodule Module do
|
||||
@spec defines?(module, definition) :: boolean
|
||||
def defines?(module, {name, arity} = tuple)
|
||||
when is_atom(module) and is_atom(name) and is_integer(arity) and arity >= 0 and arity <= 255 do
|
||||
assert_not_compiled!(__ENV__.function, module, @extra_error_msg_defines?)
|
||||
assert_not_compiled!(:defines?, module)
|
||||
{set, _bag} = data_tables_for(module)
|
||||
:ets.member(set, {:def, tuple})
|
||||
end
|
||||
@@ -955,8 +926,7 @@ defmodule Module do
|
||||
`kind` can be any of `:def`, `:defp`, `:defmacro`, or `:defmacrop`.
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
Use `Kernel.function_exported?/3` and `Kernel.macro_exported?/3` to check for
|
||||
public functions and macros respectively in compiled modules.
|
||||
Use `Kernel.function_exported?/3` to check compiled modules.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -971,8 +941,7 @@ defmodule Module do
|
||||
def defines?(module, {name, arity} = tuple, def_kind)
|
||||
when is_atom(module) and is_atom(name) and is_integer(arity) and arity >= 0 and arity <= 255 and
|
||||
def_kind in [:def, :defp, :defmacro, :defmacrop] do
|
||||
assert_not_compiled!(__ENV__.function, module, @extra_error_msg_defines?)
|
||||
|
||||
assert_not_compiled!(:defines?, module)
|
||||
{set, _bag} = data_tables_for(module)
|
||||
|
||||
case :ets.lookup(set, {:def, tuple}) do
|
||||
@@ -993,11 +962,9 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Copies the given spec as a callback.
|
||||
Converts the given spec to a callback.
|
||||
|
||||
Returns `true` if there is such a spec and it was copied as a callback.
|
||||
If the function associated to the spec has documentation defined prior to
|
||||
invoking this function, the docs are copied too.
|
||||
Returns `true` if there is such a spec and it was converted to a callback.
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec spec_to_callback(module, definition) :: boolean
|
||||
@@ -1006,27 +973,19 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all functions and macros defined in `module`.
|
||||
|
||||
It returns a list with all defined functions and macros, public and private,
|
||||
in the shape of `[{name, arity}, ...]`.
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
Use the `c:Module.__info__/1` callback to get the public functions and macros in
|
||||
compiled modules.
|
||||
Returns all functions defined in `module`.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule Example do
|
||||
def version, do: 1
|
||||
defmacrop test(arg), do: arg
|
||||
Module.definitions_in(__MODULE__) #=> [{:version, 0}, {:test, 1}]
|
||||
Module.definitions_in(__MODULE__) #=> [{:version, 0}]
|
||||
end
|
||||
|
||||
"""
|
||||
@spec definitions_in(module) :: [definition]
|
||||
def definitions_in(module) when is_atom(module) do
|
||||
assert_not_compiled!(__ENV__.function, module, @extra_error_msg_definitions_in)
|
||||
assert_not_compiled!(:definitions_in, module)
|
||||
{_, bag} = data_tables_for(module)
|
||||
bag_lookup_element(bag, :defs, 2)
|
||||
end
|
||||
@@ -1035,10 +994,6 @@ defmodule Module do
|
||||
Returns all functions defined in `module`, according
|
||||
to its kind.
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
Use the `c:Module.__info__/1` callback to get the public functions and macros in
|
||||
compiled modules.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule Example do
|
||||
@@ -1051,7 +1006,7 @@ defmodule Module do
|
||||
@spec definitions_in(module, def_kind) :: [definition]
|
||||
def definitions_in(module, def_kind)
|
||||
when is_atom(module) and def_kind in [:def, :defp, :defmacro, :defmacrop] do
|
||||
assert_not_compiled!(__ENV__.function, module, @extra_error_msg_definitions_in)
|
||||
assert_not_compiled!(:definitions_in, module)
|
||||
{set, _} = data_tables_for(module)
|
||||
:lists.concat(:ets.match(set, {{:def, :"$1"}, def_kind, :_, :_, :_, :_}))
|
||||
end
|
||||
@@ -1062,15 +1017,10 @@ defmodule Module do
|
||||
An overridable function is lazily defined, allowing a
|
||||
developer to customize it. See `Kernel.defoverridable/1` for
|
||||
more information and documentation.
|
||||
|
||||
Once a function or a macro is marked as overridable, it will
|
||||
no longer be listed under `definitions_in/1` or return true
|
||||
when given to `defines?/2` until another implementation is
|
||||
given.
|
||||
"""
|
||||
@spec make_overridable(module, [definition]) :: :ok
|
||||
def make_overridable(module, tuples) when is_atom(module) and is_list(tuples) do
|
||||
assert_not_readonly!(__ENV__.function, module)
|
||||
assert_not_compiled!(:make_overridable, module)
|
||||
|
||||
func = fn
|
||||
{function_name, arity} = tuple
|
||||
@@ -1083,7 +1033,18 @@ defmodule Module do
|
||||
|
||||
clause ->
|
||||
neighbours = :elixir_locals.yank(tuple, module)
|
||||
:elixir_overridable.record_overridable(module, tuple, clause, neighbours)
|
||||
overridable_definitions = :elixir_overridable.overridable(module)
|
||||
|
||||
count =
|
||||
case :maps.find(tuple, overridable_definitions) do
|
||||
{:ok, {count, _, _, _}} -> count + 1
|
||||
:error -> 1
|
||||
end
|
||||
|
||||
overridable_definitions =
|
||||
:maps.put(tuple, {count, clause, neighbours, false}, overridable_definitions)
|
||||
|
||||
:elixir_overridable.overridable(module, overridable_definitions)
|
||||
end
|
||||
|
||||
other ->
|
||||
@@ -1126,7 +1087,7 @@ defmodule Module do
|
||||
behaviour_definitions = bag_lookup_element(bag, {:accumulate, :behaviour}, 2)
|
||||
|
||||
cond do
|
||||
Code.ensure_compiled(behaviour) != {:module, behaviour} ->
|
||||
not Code.ensure_compiled?(behaviour) ->
|
||||
{:error, "it was not defined"}
|
||||
|
||||
not function_exported?(behaviour, :behaviour_info, 1) ->
|
||||
@@ -1168,7 +1129,7 @@ defmodule Module do
|
||||
@spec overridable?(module, definition) :: boolean
|
||||
def overridable?(module, {function_name, arity} = tuple)
|
||||
when is_atom(function_name) and is_integer(arity) and arity >= 0 and arity <= 255 do
|
||||
:elixir_overridable.overridable_for(module, tuple) != :not_overridable
|
||||
:maps.is_key(tuple, :elixir_overridable.overridable(module))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1183,7 +1144,7 @@ defmodule Module do
|
||||
"""
|
||||
@spec put_attribute(module, atom, term) :: :ok
|
||||
def put_attribute(module, key, value) when is_atom(module) and is_atom(key) do
|
||||
__put_attribute__(module, key, value, nil)
|
||||
put_attribute(module, key, value, nil)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1203,68 +1164,21 @@ defmodule Module do
|
||||
|
||||
Module.get_attribute(__MODULE__, :foo)
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
Use the `c:Module.__info__/1` callback to get all persisted attributes, or
|
||||
`Code.fetch_docs/1` to retrieve all documentation related attributes in
|
||||
compiled modules.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule Foo do
|
||||
Module.put_attribute(__MODULE__, :value, 1)
|
||||
Module.get_attribute(__MODULE__, :value) #=> 1
|
||||
|
||||
Module.get_attribute(__MODULE__, :value, :default) #=> 1
|
||||
Module.get_attribute(__MODULE__, :not_found, :default) #=> :default
|
||||
|
||||
Module.register_attribute(__MODULE__, :value, accumulate: true)
|
||||
Module.put_attribute(__MODULE__, :value, 1)
|
||||
Module.get_attribute(__MODULE__, :value) #=> [1]
|
||||
end
|
||||
|
||||
"""
|
||||
@spec get_attribute(module, atom, term) :: term
|
||||
def get_attribute(module, key, default \\ nil) when is_atom(module) and is_atom(key) do
|
||||
case __get_attribute__(module, key, nil) do
|
||||
nil -> default
|
||||
value -> value
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the given attribute has been defined.
|
||||
|
||||
An attribute is defined if it has been registered with `register_attribute/3`
|
||||
or assigned a value. If an attribute has been deleted with `delete_attribute/2`
|
||||
it is no longer considered defined.
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule MyModule do
|
||||
@value 1
|
||||
Module.register_attribute(__MODULE__, :other_value)
|
||||
Module.put_attribute(__MODULE__, :another_value, 1)
|
||||
|
||||
Module.has_attribute?(__MODULE__, :value) #=> true
|
||||
Module.has_attribute?(__MODULE__, :other_value) #=> true
|
||||
Module.has_attribute?(__MODULE__, :another_value) #=> true
|
||||
|
||||
Module.has_attribute?(__MODULE__, :undefined) #=> false
|
||||
|
||||
Module.delete_attribute(__MODULE__, :value)
|
||||
Module.has_attribute?(__MODULE__, :value) #=> false
|
||||
end
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec has_attribute?(module, atom) :: boolean
|
||||
def has_attribute?(module, key) when is_atom(module) and is_atom(key) do
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
{set, _bag} = data_tables_for(module)
|
||||
|
||||
:ets.member(set, key)
|
||||
@spec get_attribute(module, atom) :: term
|
||||
def get_attribute(module, key) when is_atom(module) and is_atom(key) do
|
||||
get_attribute(module, key, nil)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1282,7 +1196,7 @@ defmodule Module do
|
||||
"""
|
||||
@spec delete_attribute(module, atom) :: term
|
||||
def delete_attribute(module, key) when is_atom(module) and is_atom(key) do
|
||||
assert_not_readonly!(__ENV__.function, module)
|
||||
assert_not_compiled!(:delete_attribute, module)
|
||||
{set, bag} = data_tables_for(module)
|
||||
|
||||
case :ets.lookup(set, key) do
|
||||
@@ -1334,7 +1248,7 @@ defmodule Module do
|
||||
@spec register_attribute(module, atom, [{:accumulate, boolean}, {:persist, boolean}]) :: :ok
|
||||
def register_attribute(module, attribute, options)
|
||||
when is_atom(module) and is_atom(attribute) and is_list(options) do
|
||||
assert_not_readonly!(__ENV__.function, module)
|
||||
assert_not_compiled!(:register_attribute, module)
|
||||
{set, bag} = data_tables_for(module)
|
||||
|
||||
if Keyword.get(options, :persist) do
|
||||
@@ -1344,9 +1258,6 @@ defmodule Module do
|
||||
if Keyword.get(options, :accumulate) do
|
||||
:ets.insert_new(set, {attribute, [], :accumulate}) ||
|
||||
:ets.update_element(set, attribute, {3, :accumulate})
|
||||
else
|
||||
:ets.insert_new(bag, {:warn_attributes, attribute})
|
||||
:ets.insert_new(set, {attribute, nil, :unset})
|
||||
end
|
||||
|
||||
:ok
|
||||
@@ -1389,9 +1300,10 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use @doc instead"
|
||||
def add_doc(module, line, kind, {name, arity}, signature \\ [], doc) do
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
assert_not_compiled!(:add_doc, module)
|
||||
|
||||
if kind in [:defp, :defmacrop, :typep] do
|
||||
if doc, do: {:error, :private_doc}, else: :ok
|
||||
@@ -1426,7 +1338,7 @@ defmodule Module do
|
||||
"#{kind} #{name}/#{arity} is private, " <>
|
||||
"@doc attribute is always discarded for private functions/macros/types"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(%{env | line: line}))
|
||||
:elixir_errors.warn(line, env.file, message)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1526,7 +1438,7 @@ defmodule Module do
|
||||
|
||||
pending_callbacks =
|
||||
if impls != [] do
|
||||
{non_implemented_callbacks, contexts} = check_impls(env, behaviours, callbacks, impls)
|
||||
{non_implemented_callbacks, contexts} = check_impls(behaviours, callbacks, impls)
|
||||
warn_missing_impls(env, non_implemented_callbacks, contexts, all_definitions)
|
||||
non_implemented_callbacks
|
||||
else
|
||||
@@ -1537,32 +1449,34 @@ defmodule Module do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp check_behaviours(env, behaviours) do
|
||||
defp check_behaviours(%{lexical_tracker: pid} = env, behaviours) do
|
||||
Enum.reduce(behaviours, %{}, fn behaviour, acc ->
|
||||
cond do
|
||||
not is_atom(behaviour) ->
|
||||
message =
|
||||
"@behaviour #{inspect(behaviour)} must be an atom (in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
acc
|
||||
|
||||
Code.ensure_compiled(behaviour) != {:module, behaviour} ->
|
||||
not Code.ensure_compiled?(behaviour) ->
|
||||
message =
|
||||
"@behaviour #{inspect(behaviour)} does not exist (in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
|
||||
acc
|
||||
|
||||
not function_exported?(behaviour, :behaviour_info, 1) ->
|
||||
message =
|
||||
"module #{inspect(behaviour)} is not a behaviour (in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
|
||||
acc
|
||||
|
||||
true ->
|
||||
:elixir_env.trace({:require, [], behaviour, []}, env)
|
||||
:elixir_lexical.record_remote(behaviour, nil, pid)
|
||||
optional_callbacks = behaviour_info(behaviour, :optional_callbacks)
|
||||
callbacks = behaviour_info(behaviour, :callbacks)
|
||||
Enum.reduce(callbacks, acc, &add_callback(&1, behaviour, env, optional_callbacks, &2))
|
||||
@@ -1576,15 +1490,10 @@ defmodule Module do
|
||||
case acc do
|
||||
%{^callback => {_kind, conflict, _optional?}} ->
|
||||
message =
|
||||
if conflict == behaviour do
|
||||
"the behavior #{inspect(conflict)} has been declared twice " <>
|
||||
"(conflict in #{format_definition(kind, callback)} in module #{inspect(env.module)})"
|
||||
else
|
||||
"conflicting behaviours found. #{format_definition(kind, callback)} is required by " <>
|
||||
"#{inspect(conflict)} and #{inspect(behaviour)} (in module #{inspect(env.module)})"
|
||||
end
|
||||
"conflicting behaviours found. #{format_definition(kind, callback)} is required by " <>
|
||||
"#{inspect(conflict)} and #{inspect(behaviour)} (in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
|
||||
%{} ->
|
||||
:ok
|
||||
@@ -1601,7 +1510,7 @@ defmodule Module do
|
||||
format_callback(callback, kind, behaviour) <>
|
||||
" is not implemented (in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
|
||||
{_, wrong_kind, _, _} when kind != wrong_kind ->
|
||||
message =
|
||||
@@ -1609,7 +1518,7 @@ defmodule Module do
|
||||
" was implemented as \"#{wrong_kind}\" but should have been \"#{kind}\" " <>
|
||||
"(in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
|
||||
_ ->
|
||||
:ok
|
||||
@@ -1631,7 +1540,7 @@ defmodule Module do
|
||||
module.__protocol__(:module) == module
|
||||
end
|
||||
|
||||
defp check_impls(env, behaviours, callbacks, impls) do
|
||||
defp check_impls(behaviours, callbacks, impls) do
|
||||
acc = {callbacks, %{}}
|
||||
|
||||
Enum.reduce(impls, acc, fn {fa, context, defaults, kind, line, file, value}, acc ->
|
||||
@@ -1644,8 +1553,7 @@ defmodule Module do
|
||||
end)
|
||||
|
||||
{:error, message} ->
|
||||
formatted = format_impl_warning(fa, kind, message)
|
||||
IO.warn(formatted, Macro.Env.stacktrace(%{env | line: line, file: file}))
|
||||
:elixir_errors.warn(line, file, format_impl_warning(fa, kind, message))
|
||||
acc
|
||||
end
|
||||
end)
|
||||
@@ -1761,7 +1669,7 @@ defmodule Module do
|
||||
"This either means you forgot to add the \"@impl true\" annotation before the " <>
|
||||
"definition or that you are accidentally overriding this callback"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(%{env | line: :elixir_utils.get_line(meta)}))
|
||||
:elixir_errors.warn(:elixir_utils.get_line(meta), env.file, message)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1801,24 +1709,19 @@ defmodule Module do
|
||||
@doc false
|
||||
# Used internally by Kernel's @.
|
||||
# This function is private and must be used only internally.
|
||||
def __get_attribute__(module, key, line) when is_atom(key) do
|
||||
assert_not_compiled!(
|
||||
{:get_attribute, 2},
|
||||
module,
|
||||
"Use the Module.__info__/1 callback or Code.fetch_docs/1 instead"
|
||||
)
|
||||
|
||||
def get_attribute(module, key, line) when is_atom(key) do
|
||||
assert_not_compiled!(:get_attribute, module)
|
||||
{set, bag} = data_tables_for(module)
|
||||
|
||||
case :ets.lookup(set, key) do
|
||||
[{_, _, :accumulate}] ->
|
||||
:lists.reverse(bag_lookup_element(bag, {:accumulate, key}, 2))
|
||||
|
||||
[{_, val, line}] when is_integer(line) ->
|
||||
:ets.update_element(set, key, {3, :used})
|
||||
[{_, val, nil}] ->
|
||||
val
|
||||
|
||||
[{_, val, _}] ->
|
||||
:ets.update_element(set, key, {3, nil})
|
||||
val
|
||||
|
||||
[] when is_integer(line) ->
|
||||
@@ -1838,8 +1741,8 @@ defmodule Module do
|
||||
@doc false
|
||||
# Used internally by Kernel's @.
|
||||
# This function is private and must be used only internally.
|
||||
def __put_attribute__(module, key, value, line) when is_atom(key) do
|
||||
assert_not_readonly!(__ENV__.function, module)
|
||||
def put_attribute(module, key, value, line) when is_atom(key) do
|
||||
assert_not_compiled!(:put_attribute, module)
|
||||
{set, bag} = data_tables_for(module)
|
||||
value = preprocess_attribute(key, value)
|
||||
put_attribute(module, key, value, line, set, bag)
|
||||
@@ -1889,7 +1792,7 @@ defmodule Module do
|
||||
catch
|
||||
:error, :badarg ->
|
||||
:ets.insert(set, {:on_load, value, line})
|
||||
:ets.insert(bag, {:warn_attributes, :on_load})
|
||||
:ets.insert(bag, {:attributes, :on_load})
|
||||
else
|
||||
_ -> raise ArgumentError, "the @on_load attribute can only be set once per module"
|
||||
end
|
||||
@@ -1901,7 +1804,7 @@ defmodule Module do
|
||||
catch
|
||||
:error, :badarg ->
|
||||
:ets.insert(set, {key, value, line})
|
||||
:ets.insert(bag, {:warn_attributes, key})
|
||||
:ets.insert(bag, {:attributes, key})
|
||||
else
|
||||
:accumulate -> :ets.insert(bag, {{:accumulate, key}, value})
|
||||
_ -> :ets.insert(set, {key, value, line})
|
||||
@@ -1917,7 +1820,7 @@ defmodule Module do
|
||||
|
||||
defp preprocess_attribute(key, value) when key in [:moduledoc, :typedoc, :doc] do
|
||||
case value do
|
||||
{line, doc} when is_integer(line) and (is_binary(doc) or doc == false or is_nil(doc)) ->
|
||||
{line, doc} when is_integer(line) and (is_binary(doc) or is_boolean(doc) or is_nil(doc)) ->
|
||||
value
|
||||
|
||||
{line, [{key, _} | _]} when is_integer(line) and is_atom(key) ->
|
||||
@@ -1925,13 +1828,13 @@ defmodule Module do
|
||||
|
||||
{line, doc} when is_integer(line) ->
|
||||
raise ArgumentError,
|
||||
"@#{key} is a built-in module attribute for documentation. It should be either " <>
|
||||
"false, nil, a string, or a keyword list, got: #{inspect(doc)}"
|
||||
"@#{key} is a built-in module attribute for documentation. It should be " <>
|
||||
"a string, boolean, keyword list, or nil, got: #{inspect(doc)}"
|
||||
|
||||
_other ->
|
||||
raise ArgumentError,
|
||||
"@#{key} is a built-in module attribute for documentation. When set dynamically, " <>
|
||||
"it should be {line, doc} (where \"doc\" is either false, nil, a string, or a keyword list), " <>
|
||||
"it should be {line, doc} (where \"doc\" is a string, boolean, keyword list, or nil), " <>
|
||||
"got: #{inspect(value)}"
|
||||
end
|
||||
end
|
||||
@@ -2078,35 +1981,9 @@ defmodule Module do
|
||||
:error, :badarg -> []
|
||||
end
|
||||
|
||||
defp assert_not_compiled!(function_name_arity, module, extra_msg \\ "") do
|
||||
defp assert_not_compiled!(fun, module) do
|
||||
open?(module) ||
|
||||
raise ArgumentError,
|
||||
assert_not_compiled_message(function_name_arity, module, extra_msg)
|
||||
end
|
||||
|
||||
defp assert_not_readonly!({function_name, arity}, module) do
|
||||
case :elixir_module.mode(module) do
|
||||
:all ->
|
||||
:ok
|
||||
|
||||
:readonly ->
|
||||
raise ArgumentError,
|
||||
"could not call Module.#{function_name}/#{arity} because the module " <>
|
||||
"#{inspect(module)} is in read-only mode (@after_compile)"
|
||||
|
||||
:closed ->
|
||||
raise ArgumentError,
|
||||
assert_not_compiled_message({function_name, arity}, module, "")
|
||||
end
|
||||
end
|
||||
|
||||
defp assert_not_compiled_message({function_name, arity}, module, extra_msg) do
|
||||
mfa = "Module.#{function_name}/#{arity}"
|
||||
|
||||
"could not call #{mfa} because the module #{inspect(module)} is already compiled" <>
|
||||
case extra_msg do
|
||||
"" -> ""
|
||||
_ -> ". " <> extra_msg
|
||||
end
|
||||
"could not call #{fun} with argument #{inspect(module)} because the module is already compiled"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,317 +0,0 @@
|
||||
defmodule Module.Checker do
|
||||
alias Module.ParallelChecker
|
||||
|
||||
@moduledoc false
|
||||
|
||||
def verify(module, cache) do
|
||||
case prepare_module(module) do
|
||||
{:ok, map} ->
|
||||
undefined_and_deprecation_warnings = undefined_and_deprecation_warnings(map, cache)
|
||||
infer_warnings = infer_definitions(map)
|
||||
warnings = infer_warnings ++ undefined_and_deprecation_warnings
|
||||
emit_warnings(warnings)
|
||||
|
||||
:error ->
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
defp prepare_module({module, module_map}) when is_map(module_map) do
|
||||
{:ok,
|
||||
%{
|
||||
module: module,
|
||||
file: module_map.file,
|
||||
definitions: module_map.definitions,
|
||||
deprecated: module_map.deprecated,
|
||||
no_warn_undefined: no_warn_undefined(module_map.compile_opts)
|
||||
}}
|
||||
end
|
||||
|
||||
defp prepare_module({module, binary}) when is_binary(binary) do
|
||||
with {:ok, debug_info} <- debug_info(module, binary),
|
||||
{:ok, checker_info} <- checker_chunk(binary) do
|
||||
{:ok,
|
||||
%{
|
||||
module: module,
|
||||
file: debug_info.file,
|
||||
definitions: debug_info.definitions,
|
||||
deprecated: checker_info.deprecated,
|
||||
no_warn_undefined: checker_info.no_warn_undefined
|
||||
}}
|
||||
end
|
||||
end
|
||||
|
||||
defp no_warn_undefined(compile_opts) do
|
||||
for(
|
||||
{:no_warn_undefined, values} <- compile_opts,
|
||||
value <- List.wrap(values),
|
||||
do: value
|
||||
)
|
||||
end
|
||||
|
||||
defp debug_info(module, binary) do
|
||||
with {:ok, {_, [debug_info: chunk]}} <- :beam_lib.chunks(binary, [:debug_info]),
|
||||
{:debug_info_v1, backend, data} <- chunk,
|
||||
{:ok, info} <- backend.debug_info(:elixir_v1, module, data, []) do
|
||||
{:ok, %{definitions: info.definitions, file: info.relative_file}}
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
defp checker_chunk(binary) do
|
||||
with {:ok, {_, [{'ExCk', chunk}]}} <- :beam_lib.chunks(binary, ['ExCk']),
|
||||
{:elixir_checker_v1, contents} <- :erlang.binary_to_term(chunk) do
|
||||
deprecated = Enum.map(contents.exports, fn {fun, map} -> {fun, map.deprecated_reason} end)
|
||||
{:ok, %{deprecated: deprecated, no_warn_undefined: contents.no_warn_undefined}}
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
defp infer_definitions(map) do
|
||||
results = Module.Types.infer_definitions(map.file, map.module, map.definitions)
|
||||
Enum.flat_map(results, fn {_function, reasons} -> reasons end)
|
||||
end
|
||||
|
||||
defp undefined_and_deprecation_warnings(map, cache) do
|
||||
state = %{
|
||||
cache: cache,
|
||||
file: map.file,
|
||||
module: map.module,
|
||||
no_warn_undefined: merge_no_warn_undefined(map),
|
||||
function: nil,
|
||||
warnings: []
|
||||
}
|
||||
|
||||
state = check_definitions(map.definitions, state)
|
||||
|
||||
state.warnings
|
||||
|> merge_warnings()
|
||||
|> sort_warnings()
|
||||
end
|
||||
|
||||
defp merge_no_warn_undefined(map) do
|
||||
case Code.get_compiler_option(:no_warn_undefined) do
|
||||
:all ->
|
||||
:all
|
||||
|
||||
list when is_list(list) ->
|
||||
map.no_warn_undefined ++ list
|
||||
end
|
||||
end
|
||||
|
||||
defp check_definitions(definitions, state) do
|
||||
Enum.reduce(definitions, state, &check_definition/2)
|
||||
end
|
||||
|
||||
defp check_definition({function, _kind, meta, clauses}, state) do
|
||||
with_file_meta(%{state | function: function}, meta, fn state ->
|
||||
Enum.reduce(clauses, state, &check_clause/2)
|
||||
end)
|
||||
end
|
||||
|
||||
defp with_file_meta(%{file: original_file} = state, meta, fun) do
|
||||
case Keyword.fetch(meta, :file) do
|
||||
{:ok, {meta_file, _}} ->
|
||||
state = fun.(%{state | file: meta_file})
|
||||
%{state | file: original_file}
|
||||
|
||||
:error ->
|
||||
fun.(state)
|
||||
end
|
||||
end
|
||||
|
||||
defp check_clause({_meta, args, _guards, body}, state) do
|
||||
state = check_expr(args, state)
|
||||
check_expr(body, state)
|
||||
end
|
||||
|
||||
# &Mod.fun/arity
|
||||
defp check_expr({:&, meta, [{:/, _, [{{:., _, [module, fun]}, _, []}, arity]}]}, state)
|
||||
when is_atom(module) and is_atom(fun) do
|
||||
check_remote(module, fun, arity, meta, state)
|
||||
end
|
||||
|
||||
# Mod.fun(...)
|
||||
defp check_expr({{:., meta, [module, fun]}, _, args}, state)
|
||||
when is_atom(module) and is_atom(fun) do
|
||||
state = check_remote(module, fun, length(args), meta, state)
|
||||
check_expr(args, state)
|
||||
end
|
||||
|
||||
# %Module{...}
|
||||
defp check_expr({:%, meta, [module, {:%{}, _meta, args}]}, state)
|
||||
when is_atom(module) and is_list(args) do
|
||||
state = check_remote(module, :__struct__, 0, meta, state)
|
||||
check_expr(args, state)
|
||||
end
|
||||
|
||||
# Function call
|
||||
defp check_expr({left, _meta, right}, state) when is_list(right) do
|
||||
state = check_expr(right, state)
|
||||
check_expr(left, state)
|
||||
end
|
||||
|
||||
# {x, y}
|
||||
defp check_expr({left, right}, state) do
|
||||
state = check_expr(right, state)
|
||||
check_expr(left, state)
|
||||
end
|
||||
|
||||
# [...]
|
||||
defp check_expr(list, state) when is_list(list) do
|
||||
Enum.reduce(list, state, &check_expr/2)
|
||||
end
|
||||
|
||||
defp check_expr(_other, state) do
|
||||
state
|
||||
end
|
||||
|
||||
defp check_remote(module, fun, arity, meta, state) 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 state.module != module do
|
||||
state
|
||||
else
|
||||
ParallelChecker.preload_module(state.cache, module)
|
||||
check_export(module, fun, arity, meta, state)
|
||||
end
|
||||
end
|
||||
|
||||
defp check_export(module, fun, arity, meta, state) do
|
||||
case ParallelChecker.fetch_export(state.cache, module, fun, arity) do
|
||||
{:ok, :def, reason} ->
|
||||
check_deprecated(module, fun, arity, reason, meta, state)
|
||||
|
||||
{:ok, :defmacro, reason} ->
|
||||
state = warn(meta, state, {:unrequired_module, module, fun, arity})
|
||||
check_deprecated(module, fun, arity, reason, meta, state)
|
||||
|
||||
{:error, :module} ->
|
||||
if warn_undefined?(module, fun, arity, state) do
|
||||
warn(meta, state, {:undefined_module, module, fun, arity})
|
||||
else
|
||||
state
|
||||
end
|
||||
|
||||
{:error, :function} ->
|
||||
if warn_undefined?(module, fun, arity, state) do
|
||||
exports = ParallelChecker.all_exports(state.cache, module)
|
||||
warn(meta, state, {:undefined_function, module, fun, arity, exports})
|
||||
else
|
||||
state
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp check_deprecated(module, fun, arity, reason, meta, state) do
|
||||
if reason do
|
||||
warn(meta, state, {:deprecated, module, fun, arity, reason})
|
||||
else
|
||||
state
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Do not warn inside guards
|
||||
# TODO: Properly handle protocols
|
||||
defp warn_undefined?(_module, :__impl__, 1, _state), do: false
|
||||
defp warn_undefined?(_module, :module_info, 0, _state), do: false
|
||||
defp warn_undefined?(_module, :module_info, 1, _state), do: false
|
||||
defp warn_undefined?(:erlang, :orelse, 2, _state), do: false
|
||||
defp warn_undefined?(:erlang, :andalso, 2, _state), do: false
|
||||
|
||||
defp warn_undefined?(_, _, _, %{no_warn_undefined: :all}) do
|
||||
false
|
||||
end
|
||||
|
||||
defp warn_undefined?(module, fun, arity, state) do
|
||||
not Enum.any?(state.no_warn_undefined, &(&1 == module or &1 == {module, fun, arity}))
|
||||
end
|
||||
|
||||
defp warn(meta, state, warning) do
|
||||
{fun, arity} = state.function
|
||||
location = {state.file, meta[:line] || 0, {state.module, fun, arity}}
|
||||
%{state | warnings: [{__MODULE__, warning, location} | state.warnings]}
|
||||
end
|
||||
|
||||
defp merge_warnings(warnings) do
|
||||
Enum.reduce(warnings, %{}, fn {module, warning, location}, acc ->
|
||||
locations = MapSet.new([location])
|
||||
Map.update(acc, {module, warning}, locations, &MapSet.put(&1, location))
|
||||
end)
|
||||
end
|
||||
|
||||
defp sort_warnings(warnings) do
|
||||
warnings
|
||||
|> Enum.map(fn {{module, warning}, locations} -> {module, warning, Enum.sort(locations)} end)
|
||||
|> Enum.sort()
|
||||
end
|
||||
|
||||
defp 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
|
||||
|
||||
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
|
||||
|
||||
defp format_locations([location]) do
|
||||
format_location(location)
|
||||
end
|
||||
|
||||
defp format_locations(locations) do
|
||||
[
|
||||
"Found at #{length(locations)} locations:\n",
|
||||
Enum.map(locations, &format_location/1)
|
||||
]
|
||||
end
|
||||
|
||||
defp format_location({file, line, {module, fun, arity}}) do
|
||||
file = Path.relative_to_cwd(file)
|
||||
line = if line > 0, do: [Integer.to_string(line), ": "], else: []
|
||||
mfa = Exception.format_mfa(module, fun, arity)
|
||||
[" ", file, ?:, line, mfa, ?\n]
|
||||
end
|
||||
|
||||
defp print_warning(message) do
|
||||
IO.puts(:stderr, [:elixir_errors.warning_prefix(), message])
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
@@ -99,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),
|
||||
@@ -202,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)
|
||||
@@ -212,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,296 +0,0 @@
|
||||
defmodule Module.ParallelChecker do
|
||||
@moduledoc false
|
||||
|
||||
@type cache() :: {pid(), :ets.tid()}
|
||||
@type warning() :: term()
|
||||
@type kind() :: :def | :defmacro
|
||||
|
||||
@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
|
||||
binaries and a list of warnings from the verification.
|
||||
"""
|
||||
@spec verify([{map(), binary()}], [{module(), binary()}], pos_integer()) :: [warning()]
|
||||
def verify(compiled_modules, runtime_binaries, schedulers \\ nil) do
|
||||
compiled_maps = Enum.map(compiled_modules, fn {map, _binary} -> {map.module, map} end)
|
||||
check_modules = compiled_maps ++ runtime_binaries
|
||||
|
||||
schedulers = schedulers || max(:erlang.system_info(:schedulers_online), 2)
|
||||
{:ok, server} = :gen_server.start_link(__MODULE__, [check_modules, self(), schedulers], [])
|
||||
preload_cache(get_ets(server), check_modules)
|
||||
start(server)
|
||||
|
||||
collect_results(length(check_modules), [])
|
||||
end
|
||||
|
||||
defp collect_results(0, warnings) do
|
||||
warnings
|
||||
end
|
||||
|
||||
defp collect_results(count, warnings) do
|
||||
receive do
|
||||
{__MODULE__, _module, new_warnings} ->
|
||||
collect_results(count - 1, new_warnings ++ warnings)
|
||||
end
|
||||
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, kind(), binary() | nil} | {:error, :function | :module}
|
||||
def fetch_export({_server, ets}, module, fun, arity) do
|
||||
case :ets.lookup(ets, {:cached, module}) do
|
||||
[{_key, true}] ->
|
||||
case :ets.lookup(ets, {:export, {module, fun, arity}}) do
|
||||
[{_key, kind, reason}] -> {:ok, kind, reason}
|
||||
[] -> {:error, :function}
|
||||
end
|
||||
|
||||
[{_key, false}] ->
|
||||
{:error, :module}
|
||||
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
|
||||
[{_key, exports}] = :ets.lookup(ets, {:all_exports, module})
|
||||
|
||||
exports
|
||||
|> Enum.map(fn {function, _kind} -> function end)
|
||||
|> Enum.sort()
|
||||
end
|
||||
|
||||
def init([modules, send_results, schedulers]) do
|
||||
ets = :ets.new(:checker_cache, [:set, :public, {:read_concurrency, true}])
|
||||
|
||||
state = %{
|
||||
ets: ets,
|
||||
waiting: %{},
|
||||
send_results: send_results,
|
||||
modules: modules,
|
||||
spawned: 0,
|
||||
schedulers: schedulers
|
||||
}
|
||||
|
||||
{:ok, 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_call(:get_ets, _from, %{ets: ets} = state) do
|
||||
{:reply, ets, state}
|
||||
end
|
||||
|
||||
def handle_cast(:start, %{modules: []} = state) do
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
def handle_cast(:start, state) do
|
||||
{:noreply, spawn_checkers(state)}
|
||||
end
|
||||
|
||||
def handle_info({__MODULE__, :done}, state) do
|
||||
state = %{state | spawned: state.spawned - 1}
|
||||
|
||||
if state.spawned == 0 and state.modules == [] do
|
||||
{:stop, :normal, state}
|
||||
else
|
||||
state = spawn_checkers(state)
|
||||
{:noreply, state}
|
||||
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
|
||||
|
||||
defp get_ets(server) do
|
||||
:gen_server.call(server, :get_ets)
|
||||
end
|
||||
|
||||
defp start(server) do
|
||||
:gen_server.cast(server, :start)
|
||||
end
|
||||
|
||||
defp preload_cache(ets, modules) do
|
||||
Enum.each(modules, fn
|
||||
{_module, map} when is_map(map) -> cache_from_module_map(ets, map)
|
||||
{module, binary} when is_binary(binary) -> cache_from_chunk(ets, module, binary)
|
||||
end)
|
||||
end
|
||||
|
||||
defp spawn_checkers(%{modules: []} = state) do
|
||||
state
|
||||
end
|
||||
|
||||
defp spawn_checkers(%{spawned: spawned, schedulers: schedulers} = state)
|
||||
when spawned >= schedulers do
|
||||
state
|
||||
end
|
||||
|
||||
defp spawn_checkers(%{modules: [{module, _} = verify | modules]} = state) do
|
||||
parent = self()
|
||||
ets = state.ets
|
||||
send_results_pid = state.send_results
|
||||
|
||||
spawn_link(fn ->
|
||||
warnings = Module.Checker.verify(verify, {parent, ets})
|
||||
send(send_results_pid, {__MODULE__, module, warnings})
|
||||
send(parent, {__MODULE__, :done})
|
||||
end)
|
||||
|
||||
spawn_checkers(%{state | modules: modules, spawned: state.spawned + 1})
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
defp cache_from_info(ets, module) do
|
||||
if Code.ensure_loaded?(module) do
|
||||
exports = info_exports(module)
|
||||
deprecated = info_deprecated(module)
|
||||
cache_info(ets, module, exports, deprecated)
|
||||
else
|
||||
:ets.insert(ets, {{:cached, module}, false})
|
||||
end
|
||||
end
|
||||
|
||||
defp info_exports(module) do
|
||||
Map.new(
|
||||
[{{:__info__, 1}, :def}] ++
|
||||
behaviour_exports(module) ++
|
||||
Enum.map(module.__info__(:macros), &{&1, :defmacro}) ++
|
||||
Enum.map(module.__info__(:functions), &{&1, :def})
|
||||
)
|
||||
rescue
|
||||
_ -> 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) do
|
||||
exports =
|
||||
Enum.map(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, {{:all_exports, module}, exports})
|
||||
:ets.insert(ets, {{:cached, module}, true})
|
||||
end
|
||||
|
||||
defp cache_chunk(ets, module, exports) do
|
||||
exports =
|
||||
Enum.map(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})
|
||||
exports = [{{:__info__, 1}, :def} | exports]
|
||||
|
||||
:ets.insert(ets, {{:all_exports, module}, exports})
|
||||
:ets.insert(ets, {{:cached, module}, true})
|
||||
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 {:behaviour_info, 1} in module.module_info(:functions) 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
|
||||
end
|
||||
@@ -1,387 +0,0 @@
|
||||
defmodule Module.Types do
|
||||
@moduledoc false
|
||||
|
||||
import Module.Types.Helpers
|
||||
alias Module.Types.{Expr, Pattern}
|
||||
|
||||
@doc """
|
||||
Infer function definitions' types.
|
||||
"""
|
||||
def infer_definitions(file, module, defs) do
|
||||
clauses = infer_signatures(file, module, defs)
|
||||
infer_bodies(clauses)
|
||||
end
|
||||
|
||||
defp infer_signatures(file, module, defs) do
|
||||
Enum.map(defs, fn {{fun, _arity} = function, kind, meta, clauses} ->
|
||||
stack = head_stack()
|
||||
context = head_context(file, module, function)
|
||||
|
||||
clauses =
|
||||
Enum.map(clauses, fn {_meta, params, guards, body} ->
|
||||
def_expr = {kind, meta, [guards_to_expr(guards, {fun, [], params})]}
|
||||
stack = push_expr_stack(def_expr, stack)
|
||||
|
||||
case of_head(params, guards, stack, context) do
|
||||
{:ok, _signature, context} -> {:ok, {context, body}}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end)
|
||||
|
||||
{function, clauses}
|
||||
end)
|
||||
end
|
||||
|
||||
defp infer_bodies(clauses) do
|
||||
Enum.map(clauses, fn {function, clauses} ->
|
||||
errors =
|
||||
Enum.flat_map(clauses, fn
|
||||
{:ok, {head_context, body}} ->
|
||||
stack = body_stack()
|
||||
context = body_context(head_context)
|
||||
|
||||
case Expr.of_expr(body, stack, context) do
|
||||
{:ok, _type, _context} -> []
|
||||
{:error, reason} -> [reason]
|
||||
end
|
||||
|
||||
{:error, reason} ->
|
||||
[reason]
|
||||
end)
|
||||
|
||||
{function, errors}
|
||||
end)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def of_head(params, guards, stack, context) do
|
||||
with {:ok, types, context} <-
|
||||
map_reduce_ok(params, context, &Pattern.of_pattern(&1, stack, &2)),
|
||||
# TODO: Check that of_guard/3 returns boolean() | :fail
|
||||
{:ok, _, context} <- Pattern.of_guard(guards_to_or(guards), stack, context),
|
||||
do: {:ok, lift_types(types, context), context}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def head_context(file, module, function) do
|
||||
%{
|
||||
# File of module
|
||||
file: file,
|
||||
# Module of definitions
|
||||
module: module,
|
||||
# Current function
|
||||
function: function,
|
||||
# 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 infered 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)`
|
||||
# `:fail` when `elem(x, 0)`
|
||||
# `:guarded_fail` when `is_tuple and elem(x, 0)`
|
||||
guard_sources: %{}
|
||||
}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def head_stack() do
|
||||
%{
|
||||
# Stack of expression we have recursed through during inference,
|
||||
# used for tracing
|
||||
expr_stack: [],
|
||||
# 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,
|
||||
# Track if we are in a context where type guard functions should
|
||||
# affect inference
|
||||
type_guards_enabled?: true,
|
||||
# Context used to determine if unification is bi-directional, :expr
|
||||
# is directional, :pattern is bi-directional
|
||||
context: :pattern
|
||||
}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def body_context(head_context) do
|
||||
%{
|
||||
# File of module
|
||||
file: head_context.file,
|
||||
# Module of definitions
|
||||
module: head_context.module,
|
||||
# Current function
|
||||
function: head_context.function,
|
||||
# Expression variable to type variable
|
||||
vars: head_context.vars,
|
||||
# Type variable to expression variable
|
||||
types_to_vars: head_context.types_to_vars,
|
||||
# Type variable to type
|
||||
types: head_context.types,
|
||||
# Trace of all variables that have been refined to a type,
|
||||
# including the type they were refined to, why, and where
|
||||
traces: head_context.traces,
|
||||
# Counter to give type variables unique names
|
||||
counter: head_context.counter
|
||||
}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def body_stack() do
|
||||
%{
|
||||
# Stack of expression we have recursed through during inference,
|
||||
# used for tracing
|
||||
expr_stack: [],
|
||||
# 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,
|
||||
# Context used to determine if unification is bi-directional, :expr
|
||||
# is directional, :pattern is bi-directional
|
||||
context: :expr
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lifts type variables to their infered types from the context.
|
||||
"""
|
||||
def lift_types(types, context) do
|
||||
context = %{
|
||||
types: context.types,
|
||||
lifted_types: %{},
|
||||
lifted_counter: 0
|
||||
}
|
||||
|
||||
{types, _context} = Enum.map_reduce(types, context, &do_lift_type/2)
|
||||
types
|
||||
end
|
||||
|
||||
@doc false
|
||||
def lift_type(type, context) do
|
||||
context = %{
|
||||
types: context.types,
|
||||
lifted_types: %{},
|
||||
lifted_counter: 0
|
||||
}
|
||||
|
||||
{type, _context} = do_lift_type(type, context)
|
||||
type
|
||||
end
|
||||
|
||||
## GUARDS
|
||||
|
||||
# TODO: Remove this and let multiple when be treated as multiple clauses,
|
||||
# meaning they will be intersection types
|
||||
defp guards_to_or([]) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp guards_to_or(guards) do
|
||||
Enum.reduce(guards, fn guard, acc -> {{:., [], [:erlang, :orelse]}, [], [guard, acc]} 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
|
||||
|
||||
## VARIABLE LIFTING
|
||||
|
||||
# Lift type variable to its infered (hopefully concrete) types from the context
|
||||
defp do_lift_type({:var, var}, context) do
|
||||
case Map.fetch(context.lifted_types, var) do
|
||||
{:ok, lifted_var} ->
|
||||
{{:var, lifted_var}, context}
|
||||
|
||||
:error ->
|
||||
case Map.fetch(context.types, var) do
|
||||
{:ok, :unbound} ->
|
||||
new_lifted_var(var, context)
|
||||
|
||||
{:ok, type} ->
|
||||
# Remove visited types to avoid infinite loops
|
||||
# then restore after we are done recursing on vars
|
||||
types = context.types
|
||||
context = %{context | types: Map.delete(context.types, var)}
|
||||
{type, context} = do_lift_type(type, context)
|
||||
{type, %{context | types: types}}
|
||||
|
||||
:error ->
|
||||
new_lifted_var(var, context)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp do_lift_type({:tuple, types}, context) do
|
||||
{types, context} = Enum.map_reduce(types, context, &do_lift_type/2)
|
||||
{{:tuple, types}, context}
|
||||
end
|
||||
|
||||
defp do_lift_type({:map, pairs}, context) do
|
||||
{pairs, context} =
|
||||
Enum.map_reduce(pairs, context, fn {key, value}, context ->
|
||||
{key, context} = do_lift_type(key, context)
|
||||
{value, context} = do_lift_type(value, context)
|
||||
{{key, value}, context}
|
||||
end)
|
||||
|
||||
{{:map, pairs}, context}
|
||||
end
|
||||
|
||||
defp do_lift_type({:list, type}, context) do
|
||||
{type, context} = do_lift_type(type, context)
|
||||
{{:list, type}, context}
|
||||
end
|
||||
|
||||
defp do_lift_type(other, context) do
|
||||
{other, context}
|
||||
end
|
||||
|
||||
defp new_lifted_var(original_var, context) do
|
||||
types = Map.put(context.lifted_types, original_var, context.lifted_counter)
|
||||
counter = context.lifted_counter + 1
|
||||
|
||||
type = {:var, context.lifted_counter}
|
||||
context = %{context | lifted_types: types, lifted_counter: counter}
|
||||
{type, context}
|
||||
end
|
||||
|
||||
## ERROR FORMATTING
|
||||
|
||||
def format_warning({:unable_unify, left, right, expr, traces}) do
|
||||
[
|
||||
"incompatible types:\n\n ",
|
||||
format_type(left),
|
||||
" !~ ",
|
||||
format_type(right),
|
||||
"\n\n",
|
||||
format_expr(expr),
|
||||
format_traces(traces),
|
||||
"Conflict found at"
|
||||
]
|
||||
end
|
||||
|
||||
defp format_expr(nil) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp format_expr(expr) do
|
||||
[
|
||||
"in expression:\n\n ",
|
||||
expr_to_string(expr),
|
||||
"\n\n"
|
||||
]
|
||||
end
|
||||
|
||||
defp format_traces([]) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp format_traces(traces) do
|
||||
Enum.map(traces, fn
|
||||
{var, {:type, type, expr, location}} ->
|
||||
[
|
||||
"where \"",
|
||||
Macro.to_string(var),
|
||||
"\" was given the type ",
|
||||
Module.Types.format_type(type),
|
||||
" in:\n\n # ",
|
||||
format_location(location),
|
||||
" ",
|
||||
expr_to_string(expr),
|
||||
"\n\n"
|
||||
]
|
||||
|
||||
{var1, {:var, var2, expr, location}} ->
|
||||
[
|
||||
"where \"",
|
||||
Macro.to_string(var1),
|
||||
"\" was given the same type as \"",
|
||||
Macro.to_string(var2),
|
||||
"\" in:\n\n # ",
|
||||
format_location(location),
|
||||
" ",
|
||||
expr_to_string(expr),
|
||||
"\n\n"
|
||||
]
|
||||
end)
|
||||
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
|
||||
|
||||
@doc false
|
||||
def format_type({:union, types}) do
|
||||
"#{Enum.map_join(types, " | ", &format_type/1)}"
|
||||
end
|
||||
|
||||
def format_type({:tuple, types}) do
|
||||
"{#{Enum.map_join(types, ", ", &format_type/1)}}"
|
||||
end
|
||||
|
||||
def format_type({:list, type}) do
|
||||
"[#{format_type(type)}]"
|
||||
end
|
||||
|
||||
def format_type({:map, pairs}) do
|
||||
case List.keytake(pairs, :__struct__, 0) do
|
||||
{{:__struct__, struct}, pairs} ->
|
||||
"%#{inspect(struct)}{#{format_map_pairs(pairs)}}"
|
||||
|
||||
nil ->
|
||||
"%{#{format_map_pairs(pairs)}}"
|
||||
end
|
||||
end
|
||||
|
||||
def format_type({:atom, literal}) do
|
||||
inspect(literal)
|
||||
end
|
||||
|
||||
def format_type(atom) when is_atom(atom) do
|
||||
"#{atom}()"
|
||||
end
|
||||
|
||||
def format_type({:var, index}) do
|
||||
"var#{index}"
|
||||
end
|
||||
|
||||
defp format_map_pairs(pairs) do
|
||||
Enum.map_join(pairs, ", ", fn {left, right} ->
|
||||
"#{format_type(left)} => #{format_type(right)}"
|
||||
end)
|
||||
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
|
||||
{:., _, [:erlang, :orelse]} -> :or
|
||||
{:., _, [:erlang, :andalso]} -> :and
|
||||
{{:., _, [mod, fun]}, _, args} -> erl_to_ex(mod, fun, args)
|
||||
other -> other
|
||||
end)
|
||||
end
|
||||
|
||||
defp erl_to_ex(mod, fun, args) do
|
||||
case :elixir_rewrite.erl_to_ex(mod, fun, args) do
|
||||
{Kernel, fun, args} -> {fun, [], args}
|
||||
{mod, fun, args} -> {{:., [], [mod, fun]}, [], args}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,7 +0,0 @@
|
||||
defmodule Module.Types.Expr do
|
||||
@moduledoc false
|
||||
|
||||
def of_expr(_expr, _stack, context) do
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
end
|
||||
@@ -1,126 +0,0 @@
|
||||
defmodule Module.Types.Helpers do
|
||||
@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 """
|
||||
Push expression to stack.
|
||||
|
||||
The expression stack is used to give the context where a type variable
|
||||
was refined when show a type conflict error.
|
||||
"""
|
||||
def push_expr_stack(expr, stack) do
|
||||
%{stack | expr_stack: [expr | stack.expr_stack]}
|
||||
end
|
||||
|
||||
@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)
|
||||
|
||||
result when elem(result, 0) == :ok ->
|
||||
result = Tuple.delete_at(result, 0)
|
||||
do_reduce_ok(tail, result, 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)
|
||||
|
||||
result when elem(result, 0) == :ok ->
|
||||
result = Tuple.delete_at(result, 0)
|
||||
do_map_ok(tail, [result | 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 """
|
||||
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
|
||||
end
|
||||
@@ -1,356 +0,0 @@
|
||||
defmodule Module.Types.Infer do
|
||||
@moduledoc false
|
||||
|
||||
import Module.Types.Helpers
|
||||
|
||||
@doc """
|
||||
Unifies two types and returns the unified type and an updated typing context
|
||||
or an error in case of a typing conflict.
|
||||
"""
|
||||
def unify(source, target, stack, context) do
|
||||
case do_unify(source, target, stack, context) do
|
||||
{:ok, type, context} ->
|
||||
{:ok, type, context}
|
||||
|
||||
{:error, reason} ->
|
||||
if stack.context == :pattern do
|
||||
case do_unify(target, source, stack, context) do
|
||||
{:ok, type, context} ->
|
||||
{:ok, type, context}
|
||||
|
||||
{:error, _} ->
|
||||
{:error, reason}
|
||||
end
|
||||
else
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp do_unify(same, same, _stack, context) do
|
||||
{:ok, same, context}
|
||||
end
|
||||
|
||||
defp do_unify({:var, var}, type, stack, context) do
|
||||
unify_var(var, type, stack, context, _var_source = true)
|
||||
end
|
||||
|
||||
defp do_unify(type, {:var, var}, stack, context) do
|
||||
unify_var(var, type, stack, context, _var_source = false)
|
||||
end
|
||||
|
||||
defp do_unify({:tuple, sources}, {:tuple, targets}, stack, context)
|
||||
when length(sources) == length(targets) do
|
||||
result =
|
||||
map_reduce_ok(Enum.zip(sources, targets), context, fn {source, target}, context ->
|
||||
unify(source, target, stack, context)
|
||||
end)
|
||||
|
||||
case result do
|
||||
{:ok, types, context} -> {:ok, {:tuple, types}, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_unify({:list, source}, {:list, target}, stack, context) do
|
||||
case unify(source, target, stack, context) do
|
||||
{:ok, type, context} -> {:ok, {:list, type}, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_unify({:map, source_pairs}, {:map, target_pairs}, stack, context) do
|
||||
# Since maps in patterns only support literal keys (excluding maps)
|
||||
# we can do exact type match without subtype checking
|
||||
|
||||
unique_right_pairs =
|
||||
Enum.reject(target_pairs, fn {key, _value} ->
|
||||
:lists.keyfind(key, 1, source_pairs)
|
||||
end)
|
||||
|
||||
unique_pairs = source_pairs ++ unique_right_pairs
|
||||
|
||||
# Build union of all unique key-value pairs between the maps
|
||||
result =
|
||||
map_reduce_ok(unique_pairs, context, fn {source_key, source_value}, context ->
|
||||
case :lists.keyfind(source_key, 1, target_pairs) do
|
||||
{^source_key, target_value} ->
|
||||
case unify(source_value, target_value, stack, context) do
|
||||
{:ok, value, context} -> {:ok, {source_key, value}, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
|
||||
false ->
|
||||
{:ok, {source_key, source_value}, context}
|
||||
end
|
||||
end)
|
||||
|
||||
case result do
|
||||
{:ok, pairs, context} -> {:ok, {:map, pairs}, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_unify(source, :dynamic, _stack, context) do
|
||||
{:ok, source, context}
|
||||
end
|
||||
|
||||
defp do_unify(source, target, stack, context) do
|
||||
if subtype?(source, target, context) do
|
||||
{:ok, source, context}
|
||||
else
|
||||
error({:unable_unify, source, target}, stack, context)
|
||||
end
|
||||
end
|
||||
|
||||
defp unify_var(var, type, stack, context, var_source?) do
|
||||
case Map.fetch!(context.types, var) do
|
||||
:unbound ->
|
||||
context = refine_var(var, type, stack, context)
|
||||
|
||||
if recursive_type?(type, [], context) do
|
||||
if var_source? do
|
||||
error({:unable_unify, {:var, var}, type}, stack, context)
|
||||
else
|
||||
error({:unable_unify, type, {:var, var}}, stack, context)
|
||||
end
|
||||
else
|
||||
{:ok, {:var, var}, context}
|
||||
end
|
||||
|
||||
var_type ->
|
||||
context = trace_var(var, type, stack, context)
|
||||
|
||||
unify_result =
|
||||
if var_source? do
|
||||
unify(var_type, type, stack, context)
|
||||
else
|
||||
unify(type, var_type, stack, context)
|
||||
end
|
||||
|
||||
case unify_result do
|
||||
{:ok, var_type, context} ->
|
||||
context = refine_var(var, var_type, stack, context)
|
||||
{:ok, {:var, var}, context}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Adds a variable to the typing context and returns its type variables.
|
||||
If the variable has already been added, return the existing type variable.
|
||||
"""
|
||||
def new_var(var, context) do
|
||||
case Map.fetch(context.vars, var_name(var)) do
|
||||
{:ok, type} ->
|
||||
{type, context}
|
||||
|
||||
:error ->
|
||||
type = {:var, context.counter}
|
||||
vars = Map.put(context.vars, var_name(var), type)
|
||||
types_to_vars = Map.put(context.types_to_vars, context.counter, var)
|
||||
types = Map.put(context.types, context.counter, :unbound)
|
||||
traces = Map.put(context.traces, context.counter, [])
|
||||
|
||||
context = %{
|
||||
context
|
||||
| vars: vars,
|
||||
types_to_vars: types_to_vars,
|
||||
types: types,
|
||||
traces: traces,
|
||||
counter: context.counter + 1
|
||||
}
|
||||
|
||||
{type, context}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Set the type for a variable and add trace.
|
||||
"""
|
||||
def refine_var(var, type, stack, context) do
|
||||
types = Map.put(context.types, var, type)
|
||||
context = %{context | types: types}
|
||||
trace_var(var, type, stack, context)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Remove type variable and all its traces.
|
||||
"""
|
||||
def remove_var(var, context) do
|
||||
types = Map.delete(context.types, var)
|
||||
traces = Map.delete(context.traces, var)
|
||||
%{context | types: types, traces: traces}
|
||||
end
|
||||
|
||||
defp trace_var(var, type, %{trace: true, expr_stack: expr_stack} = _stack, context) do
|
||||
line = get_meta(hd(expr_stack))[:line]
|
||||
trace = {type, expr_stack, {context.file, line}}
|
||||
traces = Map.update!(context.traces, var, &[trace | &1])
|
||||
%{context | traces: traces}
|
||||
end
|
||||
|
||||
defp trace_var(_var, _type, %{trace: false} = _stack, context) do
|
||||
context
|
||||
end
|
||||
|
||||
# Check if a variable is recursive and incompatible with itself
|
||||
# Bad: `{var} = var`
|
||||
# Good: `x = y; y = z; z = x`
|
||||
defp recursive_type?({:var, var} = parent, parents, context) do
|
||||
case Map.fetch!(context.types, var) do
|
||||
:unbound ->
|
||||
false
|
||||
|
||||
type ->
|
||||
if type in parents do
|
||||
not Enum.all?(parents, &match?({:var, _}, &1))
|
||||
else
|
||||
recursive_type?(type, [parent | parents], context)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp recursive_type?({:list, type} = parent, parents, context) do
|
||||
recursive_type?(type, [parent | parents], context)
|
||||
end
|
||||
|
||||
defp recursive_type?({:tuple, types} = parent, parents, context) do
|
||||
Enum.any?(types, &recursive_type?(&1, [parent | parents], context))
|
||||
end
|
||||
|
||||
defp recursive_type?({:map, pairs} = parent, parents, context) do
|
||||
Enum.any?(pairs, fn {key, value} ->
|
||||
recursive_type?(key, [parent | parents], context) or
|
||||
recursive_type?(value, [parent | parents], context)
|
||||
end)
|
||||
end
|
||||
|
||||
defp recursive_type?(_other, _parents, _context) do
|
||||
false
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the first argument is a subtype of the second argument.
|
||||
Only checks for simple and concrete types.
|
||||
"""
|
||||
def subtype?({:atom, boolean}, :boolean, _context) when is_boolean(boolean), do: true
|
||||
def subtype?({:atom, atom}, :atom, _context) when is_atom(atom), do: true
|
||||
def subtype?(:boolean, :atom, _context), do: true
|
||||
def subtype?(:float, :number, _context), do: true
|
||||
def subtype?(:integer, :number, _context), do: true
|
||||
def subtype?({:tuple, _}, :tuple, _context), do: true
|
||||
|
||||
# TODO: Lift unions to unify/3?
|
||||
def subtype?({:union, left_types}, {:union, _} = right_union, context) do
|
||||
Enum.all?(left_types, &subtype?(&1, right_union, context))
|
||||
end
|
||||
|
||||
def subtype?(left, {:union, right_types}, context) do
|
||||
Enum.any?(right_types, &subtype?(left, &1, context))
|
||||
end
|
||||
|
||||
def subtype?(left, right, _context), do: left == right
|
||||
|
||||
@doc """
|
||||
Returns a "simplified" union using `subtype?/3` to remove redundant types.
|
||||
|
||||
Due to limitations in `subtype?/3` some overlapping types may still be
|
||||
included. For example unions with overlapping non-concrete types such as
|
||||
`{boolean()} | {atom()}` will not be merged or types with variables that
|
||||
are distinct but equivalent such as `a | b when a ~ b`.
|
||||
"""
|
||||
# TODO: Translate union of all top types to dynamic()
|
||||
def to_union(types, context) when types != [] do
|
||||
if :dynamic in types do
|
||||
:dynamic
|
||||
else
|
||||
case unique_super_types(flatten_union(types), context) do
|
||||
[type] -> type
|
||||
types -> {:union, types}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp flatten_union(types) do
|
||||
Enum.flat_map(types, fn
|
||||
{:union, types} -> flatten_union(types)
|
||||
type -> [type]
|
||||
end)
|
||||
end
|
||||
|
||||
# Filter subtypes
|
||||
# `boolean() | atom()` => `atom()`
|
||||
# `:foo | atom()` => `atom()`
|
||||
# Does not unify `true | false` => `boolean()`
|
||||
defp unique_super_types([type | types], context) do
|
||||
types = Enum.reject(types, &subtype?(&1, type, context))
|
||||
|
||||
if Enum.any?(types, &subtype?(type, &1, context)) do
|
||||
unique_super_types(types, context)
|
||||
else
|
||||
[type | unique_super_types(types, context)]
|
||||
end
|
||||
end
|
||||
|
||||
defp unique_super_types([], _context) do
|
||||
[]
|
||||
end
|
||||
|
||||
# Collect relevant information from context and traces to report error
|
||||
defp error({:unable_unify, left, right}, stack, context) do
|
||||
{fun, arity} = context.function
|
||||
line = get_meta(hd(stack.expr_stack))[:line]
|
||||
location = {context.file, line, {context.module, fun, arity}}
|
||||
|
||||
traces = type_traces(context)
|
||||
common_expr = common_super_expr(traces) || hd(stack.expr_stack)
|
||||
traces = simplify_traces(traces, context)
|
||||
|
||||
{:error, {Module.Types, {:unable_unify, left, right, common_expr, traces}, [location]}}
|
||||
end
|
||||
|
||||
defp type_traces(context) do
|
||||
Enum.flat_map(context.traces, fn {var_index, traces} ->
|
||||
expr_var = Map.fetch!(context.types_to_vars, var_index)
|
||||
Enum.map(traces, &{expr_var, &1})
|
||||
end)
|
||||
end
|
||||
|
||||
# Only use last expr from trace and tag if trace is for
|
||||
# a concrete type or type variable
|
||||
defp simplify_traces(traces, context) do
|
||||
Enum.flat_map(traces, fn {var, {type, [expr | _], location}} ->
|
||||
case type do
|
||||
{:var, var_index} ->
|
||||
var2 = Map.fetch!(context.types_to_vars, var_index)
|
||||
[{var, {:var, var2, expr, location}}]
|
||||
|
||||
_ ->
|
||||
[{var, {:type, type, expr, location}}]
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
# Find first common super expression among all traces
|
||||
defp common_super_expr([]) do
|
||||
nil
|
||||
end
|
||||
|
||||
defp common_super_expr([{_var, {_type, expr_stack, _location}} | traces]) do
|
||||
Enum.find_value(expr_stack, fn expr ->
|
||||
common? =
|
||||
Enum.all?(traces, fn {_var, {_type, expr_stack, _location}} -> expr in expr_stack end)
|
||||
|
||||
if common? do
|
||||
expr
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp get_meta({_fun, meta, _args}) when is_list(meta), do: meta
|
||||
defp get_meta(_other), do: []
|
||||
end
|
||||
@@ -1,585 +0,0 @@
|
||||
defmodule Module.Types.Pattern do
|
||||
@moduledoc false
|
||||
|
||||
import Module.Types.{Helpers, Infer}
|
||||
|
||||
@doc """
|
||||
Return the type and typing context of a pattern expression or an error
|
||||
in case of a typing conflict.
|
||||
"""
|
||||
# :atom
|
||||
def of_pattern(atom, _stack, context) when is_atom(atom) do
|
||||
{:ok, {:atom, atom}, context}
|
||||
end
|
||||
|
||||
# 12
|
||||
def of_pattern(literal, _stack, context) when is_integer(literal) do
|
||||
{:ok, :integer, context}
|
||||
end
|
||||
|
||||
# 1.2
|
||||
def of_pattern(literal, _stack, context) when is_float(literal) do
|
||||
{:ok, :float, context}
|
||||
end
|
||||
|
||||
# "..."
|
||||
def of_pattern(literal, _stack, context) when is_binary(literal) do
|
||||
{:ok, :binary, context}
|
||||
end
|
||||
|
||||
# <<...>>>
|
||||
def of_pattern({:<<>>, _meta, args} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case reduce_ok(args, context, &of_binary(&1, stack, &2)) do
|
||||
{:ok, context} -> {:ok, :binary, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# left | []
|
||||
def of_pattern({:|, _meta, [left_expr, []]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
of_pattern(left_expr, stack, context)
|
||||
end
|
||||
|
||||
# left | right
|
||||
def of_pattern({:|, _meta, [left_expr, right_expr]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case of_pattern(left_expr, stack, context) do
|
||||
{:ok, left, context} ->
|
||||
case of_pattern(right_expr, 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_pattern([], _stack, context) do
|
||||
{:ok, {:list, :dynamic}, context}
|
||||
end
|
||||
|
||||
# [expr, ...]
|
||||
def of_pattern(exprs, stack, context) when is_list(exprs) do
|
||||
stack = push_expr_stack(exprs, stack)
|
||||
|
||||
case map_reduce_ok(exprs, context, &of_pattern(&1, stack, &2)) do
|
||||
{:ok, types, context} -> {:ok, {:list, to_union(types, context)}, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# left ++ right
|
||||
def of_pattern(
|
||||
{{:., _meta1, [:erlang, :++]}, _meta2, [left_expr, right_expr]} = expr,
|
||||
stack,
|
||||
context
|
||||
) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case of_pattern(left_expr, stack, context) do
|
||||
{:ok, {:list, left}, context} ->
|
||||
case of_pattern(right_expr, stack, context) do
|
||||
{:ok, {:list, right}, context} ->
|
||||
{:ok, {:list, to_union([left, right], context)}, context}
|
||||
|
||||
{:ok, right, context} ->
|
||||
{:ok, {:list, to_union([left, right], context)}, context}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# _
|
||||
def of_pattern({:_, _meta, atom}, _stack, context) when is_atom(atom) do
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
|
||||
# var
|
||||
def of_pattern(var, _stack, context) when is_var(var) do
|
||||
{type, context} = new_var(var, context)
|
||||
{:ok, type, context}
|
||||
end
|
||||
|
||||
# {left, right}
|
||||
def of_pattern({left, right}, stack, context) do
|
||||
of_pattern({:{}, [], [left, right]}, stack, context)
|
||||
end
|
||||
|
||||
# {...}
|
||||
def of_pattern({:{}, _meta, exprs} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case map_reduce_ok(exprs, context, &of_pattern(&1, stack, &2)) do
|
||||
{:ok, types, context} -> {:ok, {:tuple, types}, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# left = right
|
||||
def of_pattern({:=, _meta, [left_expr, right_expr]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
with {:ok, left_type, context} <- of_pattern(left_expr, stack, context),
|
||||
{:ok, right_type, context} <- of_pattern(right_expr, stack, context),
|
||||
do: unify(left_type, right_type, stack, context)
|
||||
end
|
||||
|
||||
# %{...}
|
||||
def of_pattern({:%{}, _meta, args} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case of_pairs(args, stack, context) do
|
||||
{:ok, pairs, context} -> {:ok, {:map, pairs_to_unions(pairs, context)}, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# %_{...}
|
||||
def of_pattern(
|
||||
{:%, _meta1, [{:_, _meta2, var_context}, {:%{}, _meta3, args}]} = expr,
|
||||
stack,
|
||||
context
|
||||
)
|
||||
when is_atom(var_context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case of_pairs(args, stack, context) do
|
||||
{:ok, pairs, context} ->
|
||||
pairs = [{{:atom, :__struct__}, :atom} | pairs]
|
||||
{:ok, {:map, pairs}, context}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# %var{...}
|
||||
def of_pattern({:%, _meta1, [var, {:%{}, _meta2, args}]} = expr, stack, context)
|
||||
when is_var(var) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
with {:ok, pairs, context} <- of_pairs(args, stack, context),
|
||||
{var_type, context} = new_var(var, context),
|
||||
{:ok, _, context} <- unify(var_type, :atom, stack, context) do
|
||||
pairs = [{{:atom, :__struct__}, var_type} | pairs]
|
||||
{:ok, {:map, pairs}, context}
|
||||
end
|
||||
end
|
||||
|
||||
# %Struct{...}
|
||||
def of_pattern({:%, _meta1, [module, {:%{}, _meta2, args}]} = expr, stack, context)
|
||||
when is_atom(module) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case of_pairs(args, stack, context) do
|
||||
{:ok, pairs, context} ->
|
||||
pairs = [{{:atom, :__struct__}, {:atom, module}} | pairs]
|
||||
{:ok, {:map, pairs}, context}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp of_pairs(pairs, stack, context) do
|
||||
map_reduce_ok(pairs, context, fn {key, value}, context ->
|
||||
with {:ok, key_type, context} <- of_pattern(key, stack, context),
|
||||
{:ok, value_type, context} <- of_pattern(value, stack, context),
|
||||
do: {:ok, {key_type, value_type}, context}
|
||||
end)
|
||||
end
|
||||
|
||||
defp pairs_to_unions(pairs, context) do
|
||||
# Maps only allow simple literal keys in patterns so
|
||||
# we do not have to do subtype checking
|
||||
|
||||
Enum.reduce(pairs, [], fn {key, value}, pairs ->
|
||||
case :lists.keyfind(key, 1, pairs) do
|
||||
{^key, {:union, union}} ->
|
||||
:lists.keystore(key, 1, pairs, {key, to_union([value | union], context)})
|
||||
|
||||
{^key, original_value} ->
|
||||
:lists.keystore(key, 1, pairs, {key, to_union([value, original_value], context)})
|
||||
|
||||
false ->
|
||||
[{key, value} | pairs]
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
## GUARDS
|
||||
|
||||
# TODO: Some guards can be changed to intersection types or higher order types
|
||||
|
||||
@guard_functions %{
|
||||
{:is_atom, 1} => {[:atom], :boolean},
|
||||
{:is_binary, 1} => {[:binary], :boolean},
|
||||
{:is_bitstring, 1} => {[:binary], :boolean},
|
||||
{:is_boolean, 1} => {[:boolean], :boolean},
|
||||
{:is_float, 1} => {[:float], :boolean},
|
||||
{:is_function, 1} => {[:fun], :boolean},
|
||||
{:is_function, 2} => {[:fun, :integer], :boolean},
|
||||
{:is_integer, 1} => {[:integer], :boolean},
|
||||
{:is_list, 1} => {[{:list, :dynamic}], :boolean},
|
||||
{:is_map, 1} => {[{:map, []}], :boolean},
|
||||
{:is_map_key, 2} => {[:dynamic, {:map, []}], :dynamic},
|
||||
{:is_number, 1} => {[:number], :boolean},
|
||||
{:is_pid, 1} => {[:pid], :boolean},
|
||||
{:is_port, 1} => {[:port], :boolean},
|
||||
{:is_reference, 1} => {[:reference], :boolean},
|
||||
{:is_tuple, 1} => {[:tuple], :boolean},
|
||||
{:<, 2} => {[:dynamic, :dynamic], :boolean},
|
||||
{:"=<", 2} => {[:dynamic, :dynamic], :boolean},
|
||||
{:>, 2} => {[:dynamic, :dynamic], :boolean},
|
||||
{:>=, 2} => {[:dynamic, :dynamic], :boolean},
|
||||
{:"/=", 2} => {[:dynamic, :dynamic], :boolean},
|
||||
{:"=/=", 2} => {[:dynamic, :dynamic], :boolean},
|
||||
{:==, 2} => {[:dynamic, :dynamic], :boolean},
|
||||
{:"=:=", 2} => {[:dynamic, :dynamic], :boolean},
|
||||
{:*, 2} => {[:number, :number], :number},
|
||||
{:+, 1} => {[:number], :number},
|
||||
{:+, 2} => {[:number, :number], :number},
|
||||
{:-, 1} => {[:number], :number},
|
||||
{:-, 2} => {[:number, :number], :number},
|
||||
{:/, 2} => {[:number, :number], :number},
|
||||
{:abs, 1} => {[:number], :number},
|
||||
{:ceil, 1} => {[:number], :integer},
|
||||
{:floor, 1} => {[:number], :integer},
|
||||
{:round, 1} => {[:number], :integer},
|
||||
{:trunc, 1} => {[:number], :integer},
|
||||
{:element, 2} => {[:integer, :tuple], :dynamic},
|
||||
{:hd, 1} => {[{:list, :dynamic}], :dynamic},
|
||||
{:length, 1} => {[{:list, :dynamic}], :integer},
|
||||
{:map_get, 2} => {[:dynamic, {:map, []}], :dynamic},
|
||||
{:map_size, 1} => {[{:map, []}], :integer},
|
||||
{:tl, 1} => {[{:list, :dynamic}], :dynamic},
|
||||
{:tuple_size, 1} => {[:tuple], :integer},
|
||||
{:node, 1} => {[{:union, [:pid, :reference, :port]}], :atom},
|
||||
{:binary_part, 3} => {[:binary, :integer, :integer], :binary},
|
||||
{:bit_size, 1} => {[:binary], :integer},
|
||||
{:byte_size, 1} => {[:binary], :integer},
|
||||
{:size, 1} => {[{:union, [:binary, :tuple]}], :boolean},
|
||||
{:div, 2} => {[:integer, :integer], :integer},
|
||||
{:rem, 2} => {[:integer, :integer], :integer},
|
||||
{:node, 0} => {[], :atom},
|
||||
{:self, 0} => {[], :pid},
|
||||
{:bnot, 1} => {[:integer], :integer},
|
||||
{:band, 2} => {[:integer, :integer], :integer},
|
||||
{:bor, 2} => {[:integer, :integer], :integer},
|
||||
{:bxor, 2} => {[:integer, :integer], :integer},
|
||||
{:bsl, 2} => {[:integer, :integer], :integer},
|
||||
{:bsr, 2} => {[:integer, :integer], :integer},
|
||||
{:xor, 2} => {[:boolean, :boolean], :boolean},
|
||||
{:not, 1} => {[:boolean], :boolean}
|
||||
|
||||
# Following guards are matched explicitly to handle
|
||||
# type guard functions such as is_atom/1
|
||||
# {:andalso, 2} => {[:boolean, :boolean], :boolean}
|
||||
# {:orelse, 2} => {[:boolean, :boolean], :boolean}
|
||||
}
|
||||
|
||||
@type_guards [
|
||||
:is_atom,
|
||||
:is_binary,
|
||||
:is_bitstring,
|
||||
:is_boolean,
|
||||
:is_float,
|
||||
:is_function,
|
||||
:is_function,
|
||||
:is_integer,
|
||||
:is_list,
|
||||
:is_map,
|
||||
:is_number,
|
||||
:is_pid,
|
||||
:is_port,
|
||||
:is_reference,
|
||||
:is_tuple
|
||||
]
|
||||
|
||||
@doc """
|
||||
Refines the type variables in the typing context using type check guards
|
||||
such as `is_integer/1`.
|
||||
"""
|
||||
def of_guard({{:., _, [:erlang, :andalso]}, _, [left, right]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
fresh_context = fresh_context(context)
|
||||
|
||||
with {:ok, left_type, left_context} <- of_guard(left, stack, fresh_context),
|
||||
{:ok, right_type, right_context} <- of_guard(right, stack, fresh_context),
|
||||
{:ok, context} <- merge_context_and(context, stack, left_context, right_context),
|
||||
{:ok, _, context} <- unify(left_type, :boolean, stack, context),
|
||||
{:ok, _, context} <- unify(right_type, :boolean, stack, context),
|
||||
do: {:ok, :boolean, context}
|
||||
end
|
||||
|
||||
def of_guard({{:., _, [:erlang, :orelse]}, _, [left, right]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
fresh_context = fresh_context(context)
|
||||
|
||||
with {:ok, left_type, left_context} <- of_guard(left, stack, fresh_context),
|
||||
{:ok, _right_type, right_context} <- of_guard(right, stack, fresh_context),
|
||||
{:ok, context} <- merge_context_or(context, stack, left_context, right_context),
|
||||
{:ok, _, context} <- unify(left_type, :boolean, stack, context),
|
||||
do: {:ok, :boolean, context}
|
||||
end
|
||||
|
||||
def of_guard({{:., _, [:erlang, guard]}, _, args} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
{param_types, return_type} = guard_signature(guard, length(args))
|
||||
type_guard? = type_guard?(guard)
|
||||
|
||||
# Only check type guards in the context of and/or/not,
|
||||
# a type guard in the context of is_tuple(x) > :foo
|
||||
# should not affect the inference of x
|
||||
if not type_guard? or stack.type_guards_enabled? do
|
||||
arg_stack = %{stack | type_guards_enabled?: type_guard?}
|
||||
|
||||
with {:ok, arg_types, context} <-
|
||||
map_reduce_ok(args, context, &of_guard(&1, arg_stack, &2)),
|
||||
{:ok, context} <- unify_call(param_types, arg_types, stack, context) do
|
||||
{arg_types, guard_sources} =
|
||||
case arg_types do
|
||||
[{:var, index} | rest_arg_types] when type_guard? ->
|
||||
guard_sources =
|
||||
Map.update(context.guard_sources, index, [:guarded], &[:guarded | &1])
|
||||
|
||||
{rest_arg_types, guard_sources}
|
||||
|
||||
_ ->
|
||||
{arg_types, context.guard_sources}
|
||||
end
|
||||
|
||||
guard_sources =
|
||||
Enum.reduce(arg_types, guard_sources, fn
|
||||
{:var, index}, guard_sources ->
|
||||
Map.update(guard_sources, index, [:fail], &[:fail | &1])
|
||||
|
||||
_, guard_sources ->
|
||||
guard_sources
|
||||
end)
|
||||
|
||||
{:ok, return_type, %{context | guard_sources: guard_sources}}
|
||||
end
|
||||
else
|
||||
{:ok, return_type, context}
|
||||
end
|
||||
end
|
||||
|
||||
def of_guard(var, _stack, context) when is_var(var) do
|
||||
type = Map.fetch!(context.vars, var_name(var))
|
||||
{:ok, type, context}
|
||||
end
|
||||
|
||||
def of_guard(expr, stack, context) do
|
||||
# Fall back to of_pattern/3 for literals
|
||||
of_pattern(expr, stack, context)
|
||||
end
|
||||
|
||||
defp fresh_context(context) do
|
||||
types = Map.new(context.types, fn {var, _} -> {var, :unbound} end)
|
||||
traces = Map.new(context.traces, fn {var, _} -> {var, []} end)
|
||||
%{context | types: types, traces: traces}
|
||||
end
|
||||
|
||||
defp unify_call(params, args, stack, context) do
|
||||
reduce_ok(Enum.zip(params, args), context, fn {param, arg}, context ->
|
||||
case unify(param, arg, stack, context) do
|
||||
{:ok, _, context} -> {:ok, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp merge_context_and(context, stack, left, right) do
|
||||
with {:ok, context} <- unify_new_types(context, stack, left),
|
||||
{:ok, context} <- unify_new_types(context, stack, right) do
|
||||
guard_sources = and_guard_sources(left.guard_sources, right.guard_sources)
|
||||
guard_sources = merge_guard_sources([context.guard_sources, guard_sources])
|
||||
{:ok, %{context | guard_sources: guard_sources}}
|
||||
end
|
||||
end
|
||||
|
||||
defp unify_new_types(context, stack, new_context) do
|
||||
context = merge_traces(context, new_context)
|
||||
|
||||
reduce_ok(Map.to_list(new_context.types), context, fn
|
||||
{_index, :unbound}, context ->
|
||||
{:ok, context}
|
||||
|
||||
{index, new_type}, context ->
|
||||
case unify({:var, index}, new_type, %{stack | trace: false}, context) do
|
||||
{:ok, _, context} ->
|
||||
{:ok, context}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp merge_guard_sources(sources) do
|
||||
Enum.reduce(sources, fn left, right ->
|
||||
Map.merge(left, right, fn _index, left, right -> join_guard_source(left, right) end)
|
||||
end)
|
||||
end
|
||||
|
||||
defp join_guard_source(left, right) do
|
||||
sources = left ++ right
|
||||
|
||||
cond do
|
||||
:fail in sources -> [:fail]
|
||||
:guarded_fail in sources -> [:guarded_fail]
|
||||
:guarded in sources -> [:guarded]
|
||||
true -> []
|
||||
end
|
||||
end
|
||||
|
||||
defp and_guard_sources(left, right) do
|
||||
Map.merge(left, right, fn _index, left, right ->
|
||||
# When the failing guard function wont fail due to type check function before it,
|
||||
# for example: is_list(x) and length(x)
|
||||
if :guarded in left and :fail in right do
|
||||
[:guarded_fail]
|
||||
else
|
||||
join_guard_source(left, right)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp merge_traces(context, new_context) do
|
||||
traces =
|
||||
:maps.fold(
|
||||
fn index, new_traces, traces ->
|
||||
:maps.update_with(index, &(new_traces ++ &1), new_traces, traces)
|
||||
end,
|
||||
context.traces,
|
||||
new_context.traces
|
||||
)
|
||||
|
||||
%{context | traces: traces}
|
||||
end
|
||||
|
||||
defp merge_context_or(context, stack, left, right) do
|
||||
context =
|
||||
case {Map.to_list(left.types), Map.to_list(right.types)} do
|
||||
{[{index, :unbound}], [{index, type}]} ->
|
||||
refine_var(index, type, stack, context)
|
||||
|
||||
{[{index, type}], [{index, :unbound}]} ->
|
||||
refine_var(index, type, stack, context)
|
||||
|
||||
{[{index, left_type}], [{index, right_type}]} ->
|
||||
# Only include right side if left side is from type guard such as is_list(x),
|
||||
# do not refine in case of length(x)
|
||||
left_guard_sources = Map.get(left.guard_sources, index, [])
|
||||
|
||||
if :fail in left_guard_sources do
|
||||
guard_sources = Map.put(context.guard_sources, index, [:fail])
|
||||
context = %{context | guard_sources: guard_sources}
|
||||
refine_var(index, left_type, stack, context)
|
||||
else
|
||||
guard_sources =
|
||||
merge_guard_sources([
|
||||
context.guard_sources,
|
||||
left.guard_sources,
|
||||
right.guard_sources
|
||||
])
|
||||
|
||||
context = %{context | guard_sources: guard_sources}
|
||||
refine_var(index, to_union([left_type, right_type], context), stack, context)
|
||||
end
|
||||
|
||||
{left_types, _right_types} ->
|
||||
Enum.reduce(left_types, context, fn {index, left_type}, context ->
|
||||
left_guard_sources = Map.get(left.guard_sources, index, [])
|
||||
|
||||
if :fail in left_guard_sources do
|
||||
guard_sources =
|
||||
merge_guard_sources([
|
||||
context.guard_sources,
|
||||
left.guard_sources,
|
||||
right.guard_sources
|
||||
])
|
||||
|
||||
context = %{context | guard_sources: guard_sources}
|
||||
refine_var(index, left_type, stack, context)
|
||||
else
|
||||
context
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
{:ok, context}
|
||||
end
|
||||
|
||||
# binary-pattern :: specifier
|
||||
defp of_binary({:"::", _meta, [expr, specifiers]} = full_expr, stack, context) do
|
||||
{expected_type, utf?} = collect_binary_type(specifiers) || {:integer, false}
|
||||
stack = push_expr_stack(full_expr, stack)
|
||||
|
||||
# Special case utf specifiers with binary literals since they allow
|
||||
# both integer and binary literals but variables are always integer
|
||||
if is_binary(expr) and utf? do
|
||||
{:ok, context}
|
||||
else
|
||||
with {:ok, type, context} <- of_pattern(expr, stack, context),
|
||||
{:ok, _type, context} <- unify(type, expected_type, stack, context),
|
||||
do: {:ok, context}
|
||||
end
|
||||
end
|
||||
|
||||
# binary-pattern
|
||||
defp of_binary(expr, stack, context) do
|
||||
case of_pattern(expr, stack, context) do
|
||||
{:ok, type, context} when type in [:integer, :float, :binary] ->
|
||||
{:ok, context}
|
||||
|
||||
{:ok, type, _context} ->
|
||||
{:error, {:invalid_binary_type, type}}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# Collect binary type specifiers,
|
||||
# from `<<pattern::integer-size(10)>>` collect `integer`
|
||||
defp collect_binary_type({:-, _meta, [left, right]}),
|
||||
do: collect_binary_type(left) || collect_binary_type(right)
|
||||
|
||||
defp collect_binary_type({:integer, _, _}), do: {:integer, false}
|
||||
defp collect_binary_type({:float, _, _}), do: {:float, false}
|
||||
defp collect_binary_type({:bits, _, _}), do: {:binary, false}
|
||||
defp collect_binary_type({:bitstring, _, _}), do: {:binary, false}
|
||||
defp collect_binary_type({:bytes, _, _}), do: {:binary, false}
|
||||
defp collect_binary_type({:binary, _, _}), do: {:binary, false}
|
||||
defp collect_binary_type({:utf8, _, _}), do: {:integer, true}
|
||||
defp collect_binary_type({:utf16, _, _}), do: {:integer, true}
|
||||
defp collect_binary_type({:utf32, _, _}), do: {:integer, true}
|
||||
defp collect_binary_type(_), do: nil
|
||||
|
||||
defp guard_signature(name, arity) do
|
||||
Map.fetch!(@guard_functions, {name, arity})
|
||||
end
|
||||
|
||||
defp type_guard?(name) do
|
||||
name in @type_guards
|
||||
end
|
||||
end
|
||||
+2
-19
@@ -13,23 +13,8 @@ defmodule Node do
|
||||
@doc """
|
||||
Turns a non-distributed node into a distributed node.
|
||||
|
||||
This functionality starts the `:net_kernel` and other related
|
||||
processes.
|
||||
|
||||
This function is rarely invoked in practice. Instead, nodes are
|
||||
named and started via the command line by using the `--sname` and
|
||||
`--name` flags. If you need to use this function to dynamically
|
||||
name a node, please make sure the `epmd` operating system process
|
||||
is running by calling `epmd -daemon`.
|
||||
|
||||
Invoking this function when the distribution has already been started,
|
||||
either via the command line interface or dynamically, will return an
|
||||
error.
|
||||
|
||||
## Examples
|
||||
|
||||
{:ok, pid} = Node.start(:example, :shortnames, 15000)
|
||||
|
||||
This functionality starts the `:net_kernel` and other
|
||||
related processes.
|
||||
"""
|
||||
@spec start(node, :longnames | :shortnames, non_neg_integer) :: {:ok, pid} | {:error, term}
|
||||
def start(name, type \\ :longnames, tick_time \\ 15000) do
|
||||
@@ -265,7 +250,6 @@ defmodule Node do
|
||||
|
||||
This function will raise `FunctionClauseError` if the given `node` is not alive.
|
||||
"""
|
||||
@spec set_cookie(t, atom) :: true
|
||||
def set_cookie(node \\ Node.self(), cookie) when is_atom(cookie) do
|
||||
:erlang.set_cookie(node, cookie)
|
||||
end
|
||||
@@ -275,7 +259,6 @@ defmodule Node do
|
||||
|
||||
Returns the cookie if the node is alive, otherwise `:nocookie`.
|
||||
"""
|
||||
@spec get_cookie() :: atom
|
||||
def get_cookie() do
|
||||
:erlang.get_cookie()
|
||||
end
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
defmodule OptionParser do
|
||||
@moduledoc """
|
||||
Functions for parsing command line arguments.
|
||||
Functions for parsing command line options.
|
||||
|
||||
When calling a command, it's possible to pass command line options
|
||||
to modify what the command does. In this documentation, those are
|
||||
called "switches", in other situations they may be called "flags"
|
||||
or simply "options". A switch can be given a value, also called an
|
||||
"argument".
|
||||
|
||||
The main function in this module is `parse/2`, which parses a list
|
||||
of command line options and arguments into a keyword list:
|
||||
The main function in this module is `parse/2`, which allows
|
||||
developers to parse a list of arguments into options:
|
||||
|
||||
iex> OptionParser.parse(["--debug"], strict: [debug: :boolean])
|
||||
{[debug: true], [], []}
|
||||
@@ -83,11 +77,9 @@ defmodule OptionParser do
|
||||
|
||||
Switches can be specified via one of two options:
|
||||
|
||||
* `:strict` - defines strict switches and their types. Any switch
|
||||
in `argv` that is not specified in the list is returned in the
|
||||
invalid options list. This is the preferred way to parse options.
|
||||
|
||||
* `:switches` - defines switches and their types. This function
|
||||
* `:strict` - defines strict switches. Any switch in `argv` that is not
|
||||
specified in the list is returned in the invalid options list.
|
||||
* `:switches` - defines some switches and their types. This function
|
||||
still attempts to parse switches that are not in this list.
|
||||
|
||||
Both these options accept a keyword list where the key is an atom
|
||||
@@ -121,7 +113,7 @@ defmodule OptionParser do
|
||||
Switches can be specified with modifiers, which change how
|
||||
they behave. The following modifiers are supported:
|
||||
|
||||
* `:keep` - keeps duplicated elements instead of overriding them;
|
||||
* `:keep` - keeps duplicated items instead of overriding them;
|
||||
works with all types except `:count`. Specifying `switch_name: :keep`
|
||||
assumes the type of `:switch_name` will be `:string`.
|
||||
|
||||
@@ -165,17 +157,16 @@ defmodule OptionParser do
|
||||
# The :option_parser_example atom is not used anywhere below
|
||||
|
||||
However, the code below would work as long as `:option_parser_example` atom is
|
||||
used at some point later (or earlier) **in the same module**. For example:
|
||||
used at some point later (or earlier) **in the same module**:
|
||||
|
||||
{opts, _, _} = OptionParser.parse(["--option-parser-example"], switches: [debug: :boolean])
|
||||
# ... then somewhere in the same module you access it ...
|
||||
opts[:option_parser_example]
|
||||
|
||||
In other words, Elixir will only parse options that are used by the runtime,
|
||||
ignoring all others. If you would like to parse all switches, regardless if
|
||||
they exist or not, you can force creation of atoms by passing
|
||||
`allow_nonexistent_atoms: true` as option. Use this option with care. It is
|
||||
only useful when you are building command-line applications that receive
|
||||
In other words, Elixir will do the correct thing and only parse options that are
|
||||
used by the runtime, ignoring all others. If you would like to parse all switches,
|
||||
regardless if they exist or not, you can force creation of atoms by passing
|
||||
`allow_nonexistent_atoms: true` as option. Use this option with care. It is only
|
||||
useful when you are building command-line applications that receive
|
||||
dynamically-named arguments and must be avoided in long-running systems.
|
||||
|
||||
## Aliases
|
||||
@@ -452,6 +443,7 @@ defmodule OptionParser do
|
||||
option_key = config.aliases[key]
|
||||
|
||||
if key && option_key do
|
||||
# TODO: Remove this in Elixir v2.0
|
||||
IO.warn("multi-letter aliases are deprecated, got: #{inspect(key)}")
|
||||
next_tagged({:default, option_key}, value, original, rest, config)
|
||||
else
|
||||
@@ -604,6 +596,7 @@ defmodule OptionParser do
|
||||
{strict, true}
|
||||
|
||||
true ->
|
||||
# TODO: Remove this in Elixir v2.0
|
||||
IO.warn("not passing the :switches or :strict option to OptionParser is deprecated")
|
||||
{[], false}
|
||||
end
|
||||
|
||||
+14
-18
@@ -20,7 +20,7 @@ defmodule Path do
|
||||
|
||||
## Examples
|
||||
|
||||
### Unix-like operating systems
|
||||
### Unix
|
||||
|
||||
Path.absname("foo")
|
||||
#=> "/usr/local/foo"
|
||||
@@ -95,8 +95,6 @@ defmodule Path do
|
||||
absname(absname_join(name), cwd)
|
||||
end
|
||||
|
||||
@slash [?/, ?\\]
|
||||
|
||||
# Joins a list
|
||||
defp absname_join([name1, name2 | rest]), do: absname_join([absname_join(name1, name2) | rest])
|
||||
|
||||
@@ -112,11 +110,6 @@ defmodule Path do
|
||||
do_absname_join(rest, relativename, [?:, uc_letter + ?a - ?A], :win32)
|
||||
end
|
||||
|
||||
defp do_absname_join(<<c1, c2, rest::binary>>, relativename, [], :win32)
|
||||
when c1 in @slash and c2 in @slash do
|
||||
do_absname_join(rest, relativename, '//', :win32)
|
||||
end
|
||||
|
||||
defp do_absname_join(<<?\\, rest::binary>>, relativename, result, :win32),
|
||||
do: do_absname_join(<<?/, rest::binary>>, relativename, result, :win32)
|
||||
|
||||
@@ -195,7 +188,7 @@ defmodule Path do
|
||||
|
||||
## Examples
|
||||
|
||||
### Unix-like operating systems
|
||||
### Unix
|
||||
|
||||
Path.type("/") #=> :absolute
|
||||
Path.type("/usr/local/bin") #=> :absolute
|
||||
@@ -223,7 +216,7 @@ defmodule Path do
|
||||
|
||||
## Examples
|
||||
|
||||
### Unix-like operating systems
|
||||
### Unix
|
||||
|
||||
Path.relative("/usr/local/bin") #=> "usr/local/bin"
|
||||
Path.relative("usr/local/bin") #=> "usr/local/bin"
|
||||
@@ -261,6 +254,8 @@ defmodule Path do
|
||||
defp unix_pathtype([list | rest]) when is_list(list), do: unix_pathtype(list ++ rest)
|
||||
defp unix_pathtype(relative), do: {:relative, relative}
|
||||
|
||||
@slash [?/, ?\\]
|
||||
|
||||
defp win32_pathtype([list | rest]) when is_list(list), do: win32_pathtype(list ++ rest)
|
||||
|
||||
defp win32_pathtype([char, list | rest]) when is_list(list),
|
||||
@@ -406,9 +401,6 @@ defmodule Path do
|
||||
iex> Path.dirname("/foo/bar/")
|
||||
"/foo/bar"
|
||||
|
||||
iex> Path.dirname("bar.ex")
|
||||
"."
|
||||
|
||||
"""
|
||||
@spec dirname(t) :: binary
|
||||
def dirname(path) do
|
||||
@@ -522,7 +514,6 @@ defmodule Path do
|
||||
do_join(left, right, os_type) |> remove_dir_sep(os_type)
|
||||
end
|
||||
|
||||
defp do_join(left, "/", os_type), do: remove_dir_sep(left, os_type)
|
||||
defp do_join("", right, os_type), do: relative(right, os_type)
|
||||
defp do_join("/", right, os_type), do: "/" <> relative(right, os_type)
|
||||
|
||||
@@ -565,7 +556,7 @@ defmodule Path do
|
||||
"""
|
||||
@spec split(t) :: [binary]
|
||||
|
||||
# Work around a bug in Erlang on Unix-like operating systems
|
||||
# Work around a bug in Erlang on UNIX
|
||||
def split(""), do: []
|
||||
|
||||
def split(path) do
|
||||
@@ -693,9 +684,14 @@ defmodule Path do
|
||||
|
||||
defp resolve_home(rest) do
|
||||
case {rest, major_os_type()} do
|
||||
{"\\" <> _, :win32} -> System.user_home!() <> rest
|
||||
{"/" <> _, _} -> System.user_home!() <> rest
|
||||
_ -> "~" <> rest
|
||||
{"\\" <> _, :win32} ->
|
||||
System.user_home!() <> rest
|
||||
|
||||
{"/" <> _, _} ->
|
||||
System.user_home!() <> rest
|
||||
|
||||
_ ->
|
||||
rest
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+17
-40
@@ -20,7 +20,7 @@ defmodule Port do
|
||||
:ok
|
||||
|
||||
In the example above, we have created a new port that executes the
|
||||
program `cat`. `cat` is a program available on Unix-like operating systems that
|
||||
program `cat`. `cat` is a program available on UNIX systems that
|
||||
receives data from multiple inputs and concatenates them in the output.
|
||||
|
||||
After the port was created, we sent it two commands in the form of
|
||||
@@ -89,7 +89,7 @@ defmodule Port do
|
||||
{#Port<0.1444>, {:data, "hello\n"}}
|
||||
|
||||
`:spawn` will retrieve the program name from the argument and traverse your
|
||||
operating system `$PATH` environment variable looking for a matching program.
|
||||
OS `$PATH` environment variable looking for a matching program.
|
||||
|
||||
Although the above is handy, it means it is impossible to invoke an executable
|
||||
that has whitespaces on its name or in any of its arguments. For those reasons,
|
||||
@@ -117,49 +117,27 @@ defmodule Port do
|
||||
reimplementing core part of the Runtime System, such as the `:user` and
|
||||
`:shell` processes.
|
||||
|
||||
## Zombie operating system processes
|
||||
## Zombie OS processes
|
||||
|
||||
A port can be closed via the `close/1` function or by sending a `{pid, :close}`
|
||||
message. However, if the VM crashes, a long-running program started by the port
|
||||
will have its stdin and stdout channels closed but **it won't be automatically
|
||||
terminated**.
|
||||
|
||||
While most Unix command line tools will exit once its communication channels
|
||||
are closed, not all command line applications will do so. You can easily check
|
||||
this by starting the port and then shutting down the VM and inspecting your
|
||||
operating system to see if the port process is still running.
|
||||
While most UNIX command line tools will exit once its communication channels
|
||||
are closed, not all command line applications will do so. While we encourage
|
||||
graceful termination by detecting if stdin/stdout has been closed, we do not
|
||||
always have control over how third-party software terminates. In those cases,
|
||||
you can wrap the application in a script that checks for stdin. Here is such
|
||||
script in Bash:
|
||||
|
||||
While we encourage graceful termination by detecting if stdin/stdout has been
|
||||
closed, we do not always have control over how third-party software terminates.
|
||||
In those cases, you can wrap the application in a script that checks for stdin.
|
||||
Here is such script that has been verified to work on bash shells:
|
||||
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Start the program in the background
|
||||
exec "$@" &
|
||||
pid1=$!
|
||||
|
||||
# Silence warnings from here on
|
||||
exec >/dev/null 2>&1
|
||||
|
||||
# Read from stdin in the background and
|
||||
# kill running program when stdin closes
|
||||
exec 0<&0 $(
|
||||
while read; do :; done
|
||||
kill -KILL $pid1
|
||||
) &
|
||||
pid2=$!
|
||||
|
||||
# Clean up
|
||||
wait $pid1
|
||||
ret=$?
|
||||
kill -KILL $pid2
|
||||
exit $ret
|
||||
|
||||
Note the program above hijacks stdin, so you won't be able to communicate
|
||||
with the underlying software via stdin (on the positive side, software that
|
||||
reads from stdin typically terminates when stdin closes).
|
||||
#!/bin/bash
|
||||
"$@" &
|
||||
pid=$!
|
||||
while read line ; do
|
||||
:
|
||||
done
|
||||
kill -KILL $pid
|
||||
|
||||
Now instead of:
|
||||
|
||||
@@ -248,7 +226,6 @@ defmodule Port do
|
||||
|
||||
For more information, see `:erlang.port_info/1`.
|
||||
"""
|
||||
@spec info(port) :: keyword | nil
|
||||
def info(port) do
|
||||
nillify(:erlang.port_info(port))
|
||||
end
|
||||
@@ -284,7 +261,7 @@ defmodule Port do
|
||||
where:
|
||||
|
||||
* `ref` is a monitor reference returned by this function;
|
||||
* `object` is either the `port` being monitored (when monitoring by port ID)
|
||||
* `object` is either the `port` being monitored (when monitoring by port id)
|
||||
or `{name, node}` (when monitoring by a port name);
|
||||
* `reason` is the exit reason.
|
||||
|
||||
|
||||
@@ -583,7 +583,7 @@ defmodule Process do
|
||||
send(:test, :hello)
|
||||
#=> :hello
|
||||
send(:wrong_name, :hello)
|
||||
** (ArgumentError) argument error
|
||||
#=> ** (ArgumentError) argument error
|
||||
|
||||
"""
|
||||
@spec register(pid | port, atom) :: true
|
||||
@@ -620,7 +620,7 @@ defmodule Process do
|
||||
Process.unregister(:test)
|
||||
#=> true
|
||||
Process.unregister(:wrong_name)
|
||||
** (ArgumentError) argument error
|
||||
#=> ** (ArgumentError) argument error
|
||||
|
||||
"""
|
||||
@spec unregister(atom) :: true
|
||||
@@ -708,6 +708,8 @@ defmodule Process do
|
||||
@spec flag(:message_queue_data, :erlang.message_queue_data()) :: :erlang.message_queue_data()
|
||||
@spec flag(:min_bin_vheap_size, non_neg_integer) :: non_neg_integer
|
||||
@spec flag(:min_heap_size, non_neg_integer) :: non_neg_integer
|
||||
@spec flag(:monitor_nodes, term) :: term
|
||||
@spec flag({:monitor_nodes, term()}, term) :: term
|
||||
@spec flag(:priority, priority_level) :: priority_level
|
||||
@spec flag(:save_calls, 0..10000) :: 0..10000
|
||||
@spec flag(:sensitive, boolean) :: boolean
|
||||
|
||||
+32
-275
@@ -1,258 +1,15 @@
|
||||
defmodule Protocol do
|
||||
@moduledoc ~S"""
|
||||
Reference and functions for working with protocols.
|
||||
|
||||
A protocol specifies an API that should be defined by its
|
||||
implementations. A protocol is defined with `Kernel.defprotocol/2`
|
||||
and its implementations with `Kernel.defimpl/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
In Elixir, we have two verbs for checking how many items there
|
||||
are in a data structure: `length` and `size`. `length` means the
|
||||
information must be computed. For example, `length(list)` needs to
|
||||
traverse the whole list to calculate its length. On the other hand,
|
||||
`tuple_size(tuple)` and `byte_size(binary)` do not depend on the
|
||||
tuple and binary size as the size information is precomputed in
|
||||
the data structure.
|
||||
|
||||
Although Elixir includes specific functions such as `tuple_size`,
|
||||
`binary_size` and `map_size`, sometimes we want to be able to
|
||||
retrieve the size of a data structure regardless of its type.
|
||||
In Elixir we can write polymorphic code, i.e. code that works
|
||||
with different shapes/types, by using protocols. A size protocol
|
||||
could be implemented as follows:
|
||||
|
||||
defprotocol Size do
|
||||
@doc "Calculates the size (and not the length!) of a data structure"
|
||||
def size(data)
|
||||
end
|
||||
|
||||
Now that the protocol can be implemented for every data structure
|
||||
the protocol may have a compliant implementation for:
|
||||
|
||||
defimpl Size, for: BitString do
|
||||
def size(binary), do: byte_size(binary)
|
||||
end
|
||||
|
||||
defimpl Size, for: Map do
|
||||
def size(map), do: map_size(map)
|
||||
end
|
||||
|
||||
defimpl Size, for: Tuple do
|
||||
def size(tuple), do: tuple_size(tuple)
|
||||
end
|
||||
|
||||
Notice we didn't implement it for lists as we don't have the
|
||||
`size` information on lists, rather its value needs to be
|
||||
computed with `length`.
|
||||
|
||||
It is possible to implement protocols for all Elixir types:
|
||||
|
||||
* Structs (see below)
|
||||
* `Tuple`
|
||||
* `Atom`
|
||||
* `List`
|
||||
* `BitString`
|
||||
* `Integer`
|
||||
* `Float`
|
||||
* `Function`
|
||||
* `PID`
|
||||
* `Map`
|
||||
* `Port`
|
||||
* `Reference`
|
||||
* `Any` (see below)
|
||||
|
||||
## Protocols and Structs
|
||||
|
||||
The real benefit of protocols comes when mixed with structs.
|
||||
For instance, Elixir ships with many data types implemented as
|
||||
structs, like `MapSet`. We can implement the `Size` protocol
|
||||
for those types as well:
|
||||
|
||||
defimpl Size, for: MapSet do
|
||||
def size(map_set), do: MapSet.size(map_set)
|
||||
end
|
||||
|
||||
When implementing a protocol for a struct, the `:for` option can
|
||||
be omitted if the `defimpl` call is inside the module that defines
|
||||
the struct:
|
||||
|
||||
defmodule User do
|
||||
defstruct [:email, :name]
|
||||
|
||||
defimpl Size do
|
||||
# two fields
|
||||
def size(%User{}), do: 2
|
||||
end
|
||||
end
|
||||
|
||||
If a protocol implementation is not found for a given type,
|
||||
invoking the protocol will raise unless it is configured to
|
||||
fall back to `Any`. Conveniences for building implementations
|
||||
on top of existing ones are also available, look at `defstruct/1`
|
||||
for more information about deriving
|
||||
protocols.
|
||||
|
||||
## Fallback to `Any`
|
||||
|
||||
In some cases, it may be convenient to provide a default
|
||||
implementation for all types. This can be achieved by setting
|
||||
the `@fallback_to_any` attribute to `true` in the protocol
|
||||
definition:
|
||||
|
||||
defprotocol Size do
|
||||
@fallback_to_any true
|
||||
def size(data)
|
||||
end
|
||||
|
||||
The `Size` protocol can now be implemented for `Any`:
|
||||
|
||||
defimpl Size, for: Any do
|
||||
def size(_), do: 0
|
||||
end
|
||||
|
||||
Although the implementation above is arguably not a reasonable
|
||||
one. For example, it makes no sense to say a PID or an integer
|
||||
have a size of `0`. That's one of the reasons why `@fallback_to_any`
|
||||
is an opt-in behaviour. For the majority of protocols, raising
|
||||
an error when a protocol is not implemented is the proper behaviour.
|
||||
|
||||
## Multiple implementations
|
||||
|
||||
Protocols can also be implemented for multiple types at once:
|
||||
|
||||
defprotocol Reversible do
|
||||
def reverse(term)
|
||||
end
|
||||
|
||||
defimpl Reversible, for: [Map, List] do
|
||||
def reverse(term), do: Enum.reverse(term)
|
||||
end
|
||||
|
||||
Inside `defimpl/2`, you can use `@protocol` to access the protocol
|
||||
being implemented and `@for` to access the module it is being
|
||||
defined for.
|
||||
|
||||
## Types
|
||||
|
||||
Defining a protocol automatically defines a type named `t`, which
|
||||
can be used as follows:
|
||||
|
||||
@spec print_size(Size.t()) :: :ok
|
||||
def print_size(data) do
|
||||
result =
|
||||
case Size.size(data) do
|
||||
0 -> "data has no items"
|
||||
1 -> "data has one item"
|
||||
n -> "data has #{n} items"
|
||||
end
|
||||
|
||||
IO.puts(result)
|
||||
end
|
||||
|
||||
The `@spec` above expresses that all types allowed to implement the
|
||||
given protocol are valid argument types for the given function.
|
||||
|
||||
## Reflection
|
||||
|
||||
Any protocol module contains three extra functions:
|
||||
|
||||
* `__protocol__/1` - returns the protocol information. The function takes
|
||||
one of the following atoms:
|
||||
|
||||
* `:consolidated?` - returns whether the protocol is consolidated
|
||||
|
||||
* `:functions` - returns a keyword list of protocol functions and their arities
|
||||
|
||||
* `:impls` - if consolidated, returns `{:consolidated, modules}` with the list of modules
|
||||
implementing the protocol, otherwise `:not_consolidated`
|
||||
|
||||
* `:module` - the protocol module atom name
|
||||
|
||||
* `impl_for/1` - returns the module that implements the protocol for the given argument,
|
||||
`nil` otherwise
|
||||
|
||||
* `impl_for!/1` - same as above but raises an error if an implementation is
|
||||
not found
|
||||
|
||||
For example, for the `Enumerable` protocol we have:
|
||||
|
||||
iex> Enumerable.__protocol__(:functions)
|
||||
[count: 1, member?: 2, reduce: 3, slice: 1]
|
||||
|
||||
iex> Enumerable.impl_for([])
|
||||
Enumerable.List
|
||||
|
||||
iex> Enumerable.impl_for(42)
|
||||
nil
|
||||
|
||||
In addition, every protocol implementation module contains the `__impl__/1` function. The
|
||||
function takes one of the following atoms:
|
||||
|
||||
* `:for` - returns the module responsible for the data structure of the protocol implementation
|
||||
|
||||
* `:protocol` - returns the protocol module for which this implementation is provided
|
||||
|
||||
For example, the module implementing the `Enumerable` protocol for lists is `Enumerable.List`.
|
||||
Therefore, we can invoke `__impl__/1` on this module:
|
||||
|
||||
iex(1)> Enumerable.List.__impl__(:for)
|
||||
List
|
||||
|
||||
iex(2)> Enumerable.List.__impl__(:protocol)
|
||||
Enumerable
|
||||
|
||||
## Consolidation
|
||||
|
||||
In order to cope with code loading in development, protocols in
|
||||
Elixir provide a slow implementation of protocol dispatching specific
|
||||
to development.
|
||||
|
||||
In order to speed up dispatching in production environments, where
|
||||
all implementations are known up-front, Elixir provides a feature
|
||||
called *protocol consolidation*. Consolidation directly links protocols
|
||||
to their implementations in a way that invoking a function from a
|
||||
consolidated protocol is equivalent to invoking two remote functions.
|
||||
|
||||
Protocol consolidation is applied by default to all Mix projects during
|
||||
compilation. This may be an issue during test. For instance, if you want
|
||||
to implement a protocol during test, the implementation will have no
|
||||
effect, as the protocol has already been consolidated. One possible
|
||||
solution is to include compilation directories that are specific to your
|
||||
test environment in your mix.exs:
|
||||
|
||||
def project do
|
||||
...
|
||||
elixirc_paths: elixirc_paths(Mix.env())
|
||||
...
|
||||
end
|
||||
|
||||
defp elixirc_paths(:test), do: ["lib", "test/support"]
|
||||
defp elixirc_paths(_), do: ["lib"]
|
||||
|
||||
And then you can define the implementations specific to the test environment
|
||||
inside `test/support/some_file.ex`.
|
||||
|
||||
Another approach is to disable protocol consolidation during tests in your
|
||||
mix.exs:
|
||||
|
||||
def project do
|
||||
...
|
||||
consolidate_protocols: Mix.env() != :test
|
||||
...
|
||||
end
|
||||
|
||||
Although doing so is not recommended as it may affect your test suite
|
||||
performance.
|
||||
|
||||
Finally, note all protocols are compiled with `debug_info` set to `true`,
|
||||
regardless of the option set by the `elixirc` compiler. The debug info is
|
||||
used for consolidation and it is removed after consolidation unless
|
||||
globally set.
|
||||
@moduledoc """
|
||||
Functions for working with protocols.
|
||||
"""
|
||||
|
||||
@doc false
|
||||
@doc """
|
||||
Defines a new protocol function.
|
||||
|
||||
Protocols do not allow functions to be defined directly, instead, the
|
||||
regular `Kernel.def/*` macros are replaced by this macro which
|
||||
defines the protocol functions with the appropriate callbacks.
|
||||
"""
|
||||
defmacro def(signature)
|
||||
|
||||
defmacro def({_, _, args}) when args == [] or is_atom(args) do
|
||||
@@ -285,7 +42,7 @@ defmodule Protocol do
|
||||
impl_for!(term).unquote(name)(unquote_splicing(call_args))
|
||||
end
|
||||
|
||||
# Copy spec as callback if possible,
|
||||
# Convert the spec to callback if possible,
|
||||
# otherwise generate a dummy callback
|
||||
Module.spec_to_callback(__MODULE__, {name, arity}) ||
|
||||
@callback unquote(name)(unquote_splicing(type_args)) :: term
|
||||
@@ -368,7 +125,7 @@ defmodule Protocol do
|
||||
## Examples
|
||||
|
||||
defprotocol Derivable do
|
||||
def ok(arg)
|
||||
def ok(a)
|
||||
end
|
||||
|
||||
defimpl Derivable, for: Any do
|
||||
@@ -390,10 +147,13 @@ defmodule Protocol do
|
||||
defmodule ImplStruct do
|
||||
@derive [Derivable]
|
||||
defstruct a: 0, b: 0
|
||||
end
|
||||
|
||||
Derivable.ok(%ImplStruct{})
|
||||
{:ok, %ImplStruct{a: 0, b: 0}, %ImplStruct{a: 0, b: 0}, []}
|
||||
defimpl Sample do
|
||||
def ok(struct) do
|
||||
Unknown.undefined(struct)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Explicit derivations can now be called via `__deriving__`:
|
||||
|
||||
@@ -475,7 +235,6 @@ defmodule Protocol do
|
||||
|
||||
defp extract_matching_by_attribute(paths, prefix, callback) do
|
||||
for path <- paths,
|
||||
path = to_charlist(path),
|
||||
file <- list_dir(path),
|
||||
mod = extract_from_file(path, file, prefix, callback),
|
||||
do: mod
|
||||
@@ -488,6 +247,8 @@ defmodule Protocol do
|
||||
end
|
||||
end
|
||||
|
||||
defp list_dir(path), do: list_dir(to_charlist(path))
|
||||
|
||||
defp extract_from_file(path, file, prefix, callback) do
|
||||
if :lists.prefix(prefix, file) and :filename.extension(file) == '.beam' do
|
||||
extract_from_beam(:filename.join(path, file), callback)
|
||||
@@ -547,14 +308,13 @@ defmodule Protocol do
|
||||
end
|
||||
|
||||
defp beam_protocol(protocol) do
|
||||
chunk_ids = [:debug_info, 'Docs', 'ExCk']
|
||||
chunk_ids = [:debug_info, 'Docs', 'ExDp']
|
||||
opts = [:allow_missing_chunks]
|
||||
|
||||
case :beam_lib.chunks(beam_file(protocol), chunk_ids, opts) do
|
||||
{:ok, {^protocol, [{:debug_info, debug_info} | chunks]}} ->
|
||||
{:debug_info_v1, _backend, {:elixir_v1, info, specs}} = debug_info
|
||||
%{attributes: attributes, definitions: definitions} = info
|
||||
chunks = :lists.filter(fn {_name, value} -> value != :missing_chunk end, chunks)
|
||||
chunks = :lists.map(fn {name, value} -> {List.to_string(name), value} end, chunks)
|
||||
|
||||
case attributes[:protocol] do
|
||||
@@ -636,14 +396,14 @@ defmodule Protocol do
|
||||
end
|
||||
|
||||
defp built_in_clause_for(mod, guard, protocol, meta, line) do
|
||||
x = {:x, [line: line, version: -1], __MODULE__}
|
||||
x = quote(line: line, do: x)
|
||||
guard = quote(line: line, do: :erlang.unquote(guard)(unquote(x)))
|
||||
body = load_impl(protocol, mod)
|
||||
{meta, [x], [guard], body}
|
||||
end
|
||||
|
||||
defp struct_clause_for(meta, line) do
|
||||
x = {:x, [line: line, version: -1], __MODULE__}
|
||||
x = quote(line: line, do: x)
|
||||
head = quote(line: line, do: %{__struct__: unquote(x)})
|
||||
guard = quote(line: line, do: :erlang.is_atom(unquote(x)))
|
||||
body = quote(line: line, do: struct_impl_for(unquote(x)))
|
||||
@@ -715,7 +475,7 @@ defmodule Protocol do
|
||||
nil
|
||||
end
|
||||
|
||||
# Disable Dialyzer checks - before and after consolidation
|
||||
# Disable dialyzer checks - before and after consolidation
|
||||
# the types could be more strict
|
||||
@dialyzer {:nowarn_function, __protocol__: 1, impl_for: 1, impl_for!: 1}
|
||||
|
||||
@@ -737,11 +497,10 @@ defmodule Protocol do
|
||||
target = Module.concat(__MODULE__, mod)
|
||||
|
||||
Kernel.def impl_for(data) when :erlang.unquote(guard)(data) do
|
||||
try do
|
||||
unquote(target).__impl__(:target)
|
||||
rescue
|
||||
UndefinedFunctionError ->
|
||||
unquote(any_impl_for)
|
||||
case Code.ensure_compiled?(unquote(target)) and
|
||||
function_exported?(unquote(target), :__impl__, 1) do
|
||||
true -> unquote(target).__impl__(:target)
|
||||
false -> unquote(any_impl_for)
|
||||
end
|
||||
end
|
||||
end,
|
||||
@@ -774,11 +533,9 @@ defmodule Protocol do
|
||||
Kernel.defp struct_impl_for(struct) do
|
||||
target = Module.concat(__MODULE__, struct)
|
||||
|
||||
try do
|
||||
target.__impl__(:target)
|
||||
rescue
|
||||
UndefinedFunctionError ->
|
||||
unquote(any_impl_for)
|
||||
case Code.ensure_compiled?(target) and function_exported?(target, :__impl__, 1) do
|
||||
true -> target.__impl__(:target)
|
||||
false -> unquote(any_impl_for)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -917,9 +674,9 @@ defmodule Protocol do
|
||||
"the #{inspect(protocol)} protocol has already been consolidated, an " <>
|
||||
"implementation for #{inspect(for)} has no effect. If you want to " <>
|
||||
"implement protocols after compilation or during tests, check the " <>
|
||||
"\"Consolidation\" section in the Protocol module documentation"
|
||||
"\"Consolidation\" section in the documentation for Kernel.defprotocol/2"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
end
|
||||
|
||||
:ok
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
defmodule Range do
|
||||
@moduledoc """
|
||||
Ranges represent a sequence of one or many, ascending
|
||||
or descending, consecutive integers.
|
||||
Defines a range.
|
||||
|
||||
A range represents a sequence of one or many,
|
||||
ascending or descending, consecutive integers.
|
||||
|
||||
Ranges can be either increasing (`first <= last`) or
|
||||
decreasing (`first > last`). Ranges are also always
|
||||
@@ -47,12 +49,6 @@ defmodule Range do
|
||||
|
||||
@doc """
|
||||
Creates a new range.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Range.new(-100, 100)
|
||||
-100..100
|
||||
|
||||
"""
|
||||
@spec new(integer, integer) :: t
|
||||
def new(first, last) when is_integer(first) and is_integer(last) do
|
||||
@@ -82,7 +78,7 @@ defmodule Range do
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec disjoint?(t, t) :: boolean
|
||||
def disjoint?(first1..last1 = _range1, first2..last2 = _range2) do
|
||||
def disjoint?(first1..last1, first2..last2) do
|
||||
{first1, last1} = normalize(first1, last1)
|
||||
{first2, last2} = normalize(first2, last2)
|
||||
last2 < first1 or last1 < first2
|
||||
@@ -92,6 +88,7 @@ defmodule Range do
|
||||
defp normalize(first, last) when first > last, do: {last, first}
|
||||
defp normalize(first, last), do: {first, last}
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
@doc false
|
||||
@deprecated "Pattern match on first..last instead"
|
||||
def range?(term)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user