Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e6d67c169 | ||
|
|
614ae049e6 | ||
|
|
b1492cb0cf | ||
|
|
237a71311c | ||
|
|
e40b280fd8 | ||
|
|
308255bda8 | ||
|
|
1e81a5b9a5 | ||
|
|
0627d7b0d4 | ||
|
|
99ec7522d3 | ||
|
|
d30c5c0185 | ||
|
|
3fc334ebc3 | ||
|
|
df8955c0dd | ||
|
|
6b87a54825 | ||
|
|
72ec902554 | ||
|
|
2c9fff0f93 | ||
|
|
c42f7af02b | ||
|
|
9c77960a26 | ||
|
|
a20f413841 | ||
|
|
ffbae20014 | ||
|
|
9c81578ab5 | ||
|
|
a9f7b8e18c | ||
|
|
61c3c34f44 | ||
|
|
d8faf5bb7b | ||
|
|
9d03032cfd | ||
|
|
3c4c68b3d5 | ||
|
|
bed0a68176 | ||
|
|
60642c340d | ||
|
|
35e4263934 | ||
|
|
43327512da | ||
|
|
32692f14ff | ||
|
|
f80bd3c478 | ||
|
|
23934b48ba | ||
|
|
c370a8a724 | ||
|
|
8c2237f80d | ||
|
|
a5e9c39023 | ||
|
|
6d918f5fb4 | ||
|
|
4347468d8a | ||
|
|
f044def5a1 | ||
|
|
05c9ccad12 | ||
|
|
7533e56c1b | ||
|
|
3f083795b6 | ||
|
|
1bbc959d1a | ||
|
|
1603f21b0a | ||
|
|
02c3957667 | ||
|
|
eeab9f5aa1 | ||
|
|
3f3e987a23 | ||
|
|
53c8b27ee2 | ||
|
|
3550dd10af | ||
|
|
7e44b88122 | ||
|
|
5c5259826f | ||
|
|
ccdefd904f | ||
|
|
ff95a59ec0 | ||
|
|
4081590742 | ||
|
|
693df5fe8c | ||
|
|
b4605374b5 | ||
|
|
36ec08e0c8 | ||
|
|
ae0ac9b3c9 | ||
|
|
e3479831ba | ||
|
|
ffbcc004ec | ||
|
|
e0d61c7875 | ||
|
|
b2059031a6 | ||
|
|
ed2829d8ea | ||
|
|
081f99cd37 | ||
|
|
7837cc5c59 | ||
|
|
b96fc39422 | ||
|
|
e1cb898198 | ||
|
|
88e2894925 | ||
|
|
f7cb5ae053 | ||
|
|
b4845e6854 | ||
|
|
0f1200f99b | ||
|
|
cec1a7190b | ||
|
|
53d1b98a05 | ||
|
|
4828f11cd2 | ||
|
|
501271cad0 | ||
|
|
fd4a6ad5cf | ||
|
|
196aab87ca | ||
|
|
59b266506a | ||
|
|
1c8e625d77 | ||
|
|
f9d7adbfdf | ||
|
|
f1721c8a58 | ||
|
|
57abca4c16 | ||
|
|
8e9ece0d62 | ||
|
|
54ba096d34 | ||
|
|
6042fd0bcc | ||
|
|
475b73b46f | ||
|
|
3cf6069ad9 | ||
|
|
48cee5aaa5 |
+3
-4
@@ -12,21 +12,20 @@ test_template: &DEFAULT_TEST_SETTINGS
|
||||
test_freebsd_task:
|
||||
<<: *DEFAULT_TEST_SETTINGS
|
||||
|
||||
name: FreeBSD 13.0
|
||||
name: FreeBSD 12.1
|
||||
alias: FreeBSD Stable
|
||||
|
||||
freebsd_instance:
|
||||
image_family: freebsd-13-0
|
||||
image_family: freebsd-12-1
|
||||
cpu: 8
|
||||
memory: 7424Mi
|
||||
|
||||
env:
|
||||
CHECK_REPRODUCIBLE: true
|
||||
LC_ALL: en_US.UTF-8
|
||||
PATH: $PATH:/usr/local/lib/erlang22/bin
|
||||
|
||||
install_script:
|
||||
- pkg install -y erlang-runtime22 git gmake
|
||||
- pkg install -y erlang git gmake
|
||||
- rm -rf .git
|
||||
- gmake compile
|
||||
|
||||
|
||||
+2
-3
@@ -1,10 +1,9 @@
|
||||
[
|
||||
inputs: [
|
||||
"lib/*/{lib,unicode,test}/**/*.{ex,exs}",
|
||||
"lib/*/*.exs",
|
||||
"lib/ex_unit/examples/*.exs",
|
||||
".formatter.exs"
|
||||
"lib/*/mix.exs"
|
||||
],
|
||||
|
||||
locals_without_parens: [
|
||||
# Formatter tests
|
||||
assert_format: 2,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
### Precheck
|
||||
|
||||
* Do not use the issue tracker for help or support (try Elixir Forum, Stack Overflow, IRC, etc.)
|
||||
* For proposing a new feature, please start a discussion on the Elixir Core mailing list: https://groups.google.com/group/elixir-lang-core
|
||||
* For bugs, do a quick search and make sure the bug has not yet been reported
|
||||
* Please disclose security vulnerabilities privately at elixir-security@googlegroups.com
|
||||
* Do not use the issues tracker for guidance, questions or support (try Elixir Forum, Stack Overflow, Slack, etc. instead)
|
||||
* Finally, be nice and have fun!
|
||||
|
||||
### Environment
|
||||
@@ -14,7 +14,6 @@
|
||||
### Current behavior
|
||||
|
||||
Include code samples, errors and stacktraces if appropriate.
|
||||
If reporting a bug, please include the reproducing steps.
|
||||
|
||||
### Expected behavior
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
name: builds.hex.pm
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- v*.*
|
||||
tags:
|
||||
- v*
|
||||
|
||||
env:
|
||||
ELIXIR_OPTS: "--warnings-as-errors"
|
||||
ERLC_OPTS: "warnings_as_errors"
|
||||
LANG: C.UTF-8
|
||||
|
||||
concurrency: builds_txt
|
||||
|
||||
jobs:
|
||||
release_pre_built:
|
||||
strategy:
|
||||
fail-fast: true
|
||||
max-parallel: 1
|
||||
matrix:
|
||||
include:
|
||||
- otp: 23
|
||||
otp_version: '23.3'
|
||||
build_docs: build_docs
|
||||
runs-on: ubuntu-20.04
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 50
|
||||
- name: Get tags
|
||||
run: git fetch --tags origin
|
||||
- uses: ./.github/workflows/release_pre_built
|
||||
with:
|
||||
otp_version: ${{ matrix.otp_version }}
|
||||
otp: ${{ matrix.otp }}
|
||||
build_docs: ${{ matrix.build_docs }}
|
||||
- name: Utils.sh
|
||||
run: |
|
||||
cat << 'EOF' > utils.sh
|
||||
function purge_key() {
|
||||
curl \
|
||||
-X POST \
|
||||
-H "Fastly-Key: ${FASTLY_KEY}" \
|
||||
-H "Accept: application/json" \
|
||||
-H "Content-Length: 0" \
|
||||
"https://api.fastly.com/service/$1/purge/$2"
|
||||
}
|
||||
function purge() {
|
||||
purge_key ${FASTLY_REPO_SERVICE_ID} $1
|
||||
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
|
||||
sleep 2
|
||||
purge_key ${FASTLY_REPO_SERVICE_ID} $1
|
||||
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
|
||||
sleep 2
|
||||
purge_key ${FASTLY_REPO_SERVICE_ID} $1
|
||||
purge_key ${FASTLY_BUILDS_SERVICE_ID} $1
|
||||
}
|
||||
EOF
|
||||
chmod +x utils.sh
|
||||
- name: Upload Docs to S3
|
||||
if: ${{ matrix.build_docs }}
|
||||
env:
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.HEX_AWS_ACCESS_KEY_ID }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.HEX_AWS_SECRET_ACCESS_KEY }}
|
||||
AWS_REGION: ${{ secrets.HEX_AWS_REGION }}
|
||||
AWS_S3_BUCKET: ${{ secrets.HEX_AWS_S3_BUCKET }}
|
||||
FASTLY_REPO_SERVICE_ID: ${{ secrets.HEX_FASTLY_REPO_SERVICE_ID }}
|
||||
FASTLY_BUILDS_SERVICE_ID: ${{ secrets.HEX_FASTLY_BUILDS_SERVICE_ID }}
|
||||
FASTLY_KEY: ${{ secrets.HEX_FASTLY_KEY }}
|
||||
run: |
|
||||
source utils.sh
|
||||
version=$(echo ${{ github.ref_name }} | sed -e 's/^v//g')
|
||||
for f in doc/*; do
|
||||
if [ -d "$f" ]; then
|
||||
app=`echo $f | sed s/"doc\/"//`
|
||||
tarball="${app}-${version}.tar.gz"
|
||||
surrogate_key="docs/${app}-${version}"
|
||||
tar -czf "${tarball}" -C "doc/${app}" .
|
||||
aws s3 cp "${tarball}" "s3://${{ env.AWS_S3_BUCKET }}/docs/${tarball}" \
|
||||
--cache-control "public,max-age=3600" \
|
||||
--metadata "{\"surrogate-key\":\"${surrogate_key}\",\"surrogate-control\":\"public,max-age=604800\"}"
|
||||
purge "${surrogate_key}"
|
||||
fi
|
||||
done
|
||||
@@ -10,18 +10,19 @@ env:
|
||||
|
||||
jobs:
|
||||
test_linux:
|
||||
name: Linux, ${{ matrix.otp_release }}, Ubuntu 18.04
|
||||
name: Linux, ${{ matrix.otp_release }}, Ubuntu 16.04
|
||||
continue-on-error: ${{ matrix.development }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
otp_release: ['OTP-24.0', 'OTP-23.3', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0']
|
||||
otp_release: ['OTP-24.0', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0', 'OTP-21.3.8', 'OTP-21.0']
|
||||
development: [false]
|
||||
include:
|
||||
- otp_release: master
|
||||
development: true
|
||||
- otp_release: maint
|
||||
development: true
|
||||
runs-on: ubuntu-18.04
|
||||
runs-on: ubuntu-16.04
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
@@ -29,7 +30,7 @@ jobs:
|
||||
- name: Install Erlang/OTP
|
||||
run: |
|
||||
cd $RUNNER_TEMP
|
||||
wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-18.04/${{ matrix.otp_release }}.tar.gz
|
||||
wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-16.04/${{ matrix.otp_release }}.tar.gz
|
||||
mkdir -p otp
|
||||
tar zxf otp.tar.gz -C otp --strip-components=1
|
||||
otp/Install -minimal $(pwd)/otp
|
||||
@@ -46,19 +47,17 @@ jobs:
|
||||
run: dialyzer -pa lib/elixir/ebin --build_plt --output_plt elixir.plt --apps lib/elixir/ebin/elixir.beam lib/elixir/ebin/Elixir.Kernel.beam
|
||||
- name: Erlang test suite
|
||||
run: make test_erlang
|
||||
continue-on-error: ${{ matrix.development }}
|
||||
- name: Elixir test suite
|
||||
run: make test_elixir
|
||||
continue-on-error: ${{ matrix.development }}
|
||||
- name: Check reproducible builds
|
||||
run: taskset 1 make check_reproducible
|
||||
if: matrix.otp_release == 'OTP-24.0'
|
||||
if: matrix.otp_release == 'OTP-23.0'
|
||||
|
||||
test_windows:
|
||||
name: Windows, OTP-${{ matrix.otp_release }}, Windows Server 2019
|
||||
strategy:
|
||||
matrix:
|
||||
otp_release: ['22.3']
|
||||
otp_release: ['22.0']
|
||||
runs-on: windows-2019
|
||||
steps:
|
||||
- name: Configure Git
|
||||
@@ -84,13 +83,11 @@ jobs:
|
||||
- name: Erlang test suite
|
||||
run: make --keep-going test_erlang
|
||||
- name: Elixir test suite
|
||||
run: |
|
||||
del c:/Windows/System32/drivers/etc/hosts
|
||||
make --keep-going test_elixir
|
||||
run: make --keep-going test_elixir
|
||||
|
||||
check_posix_compliant:
|
||||
name: Check POSIX-compliant
|
||||
runs-on: ubuntu-18.04
|
||||
runs-on: ubuntu-16.04
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
name: "Release pre built"
|
||||
description: "Builds elixir release, ExDoc and generates docs"
|
||||
inputs:
|
||||
otp:
|
||||
description: "The major OTP version"
|
||||
otp_version:
|
||||
description: "The exact OTP version (major.minor[.patch])"
|
||||
build_docs:
|
||||
description: "If docs have to be built or not"
|
||||
runs:
|
||||
using: "composite"
|
||||
steps:
|
||||
- uses: erlef/setup-beam@v1
|
||||
with:
|
||||
otp-version: ${{ inputs.otp_version }}
|
||||
version-type: strict
|
||||
- name: Build Elixir Release
|
||||
shell: bash
|
||||
run: |
|
||||
make Precompiled.zip
|
||||
mv Precompiled.zip elixir-otp-${{ inputs.otp }}.zip
|
||||
shasum -a 1 elixir-otp-${{ inputs.otp }}.zip > elixir-otp-${{ inputs.otp }}.zip.sha1sum
|
||||
shasum -a 256 elixir-otp-${{ inputs.otp }}.zip > elixir-otp-${{ inputs.otp }}.zip.sha256sum
|
||||
echo "$PWD/bin" >> $GITHUB_PATH
|
||||
- name: Get latest stable ExDoc version
|
||||
if: ${{ inputs.build_docs }}
|
||||
shell: bash
|
||||
run: |
|
||||
EX_DOC_LATEST_STABLE_VERSION=$(curl -s https://hex.pm/api/packages/ex_doc | jq --raw-output '.latest_stable_version')
|
||||
echo "EX_DOC_LATEST_STABLE_VERSION=${EX_DOC_LATEST_STABLE_VERSION}" >> $GITHUB_ENV
|
||||
- uses: actions/checkout@v3
|
||||
if: ${{ inputs.build_docs }}
|
||||
with:
|
||||
repository: elixir-lang/ex_doc
|
||||
ref: v${{ env.EX_DOC_LATEST_STABLE_VERSION }}
|
||||
path: ex_doc
|
||||
- name: Build ex_doc
|
||||
if: ${{ inputs.build_docs }}
|
||||
shell: bash
|
||||
run: |
|
||||
mv ex_doc ../ex_doc
|
||||
cd ../ex_doc
|
||||
../elixir/bin/mix do local.rebar --force, local.hex --force, deps.get, compile
|
||||
cd ../elixir
|
||||
- name: Build Docs
|
||||
if: ${{ inputs.build_docs }}
|
||||
shell: bash
|
||||
run: |
|
||||
make Docs.zip
|
||||
shasum -a 1 Docs.zip > Docs.zip.sha1sum
|
||||
shasum -a 256 Docs.zip > Docs.zip.sha256sum
|
||||
+390
-253
@@ -1,359 +1,496 @@
|
||||
# Changelog for Elixir v1.13
|
||||
# Changelog for Elixir v1.11
|
||||
|
||||
The focus behind Elixir v1.13 has been on tooling, mainly tooling related to code formatting, code fragments, code reflection, and code recompilation. A lot of this functionality will directly impact developers working on large codebases and provide meaningful quality of life improvements for those working on Elixir tooling and environments, such as IDEs, notebooks, etc.
|
||||
Over the last releases, the Elixir team has been focusing on the compiler, both in terms of catching more mistakes at compilation time and making it faster. Elixir v1.11 has made excellent progress on both fronts. This release also includes many other goodies, such as tighter Erlang integration, support for more guard expressions, built-in datetime formatting, and other calendar enhancements.
|
||||
|
||||
## Semantic recompilation
|
||||
## Tighter Erlang integration
|
||||
|
||||
Elixir v1.13 comes with many improvements to the compiler, so it recompiles your files less frequently. In particular:
|
||||
Following Elixir v1.10, we have further integrated with Erlang's new logger by adding four new log levels: `notice`, `critical`, `alert`, and `emergency`, matching all log levels found in the Syslog standard. The `Logger` module now supports structured logging by passing maps and keyword lists to its various functions. It is also possible to specify the log level per module, via the `Logger.put_module_level/2` function. Log levels per application will be added in future releases.
|
||||
|
||||
* The digest of the files are considered in addition to their size. This avoids recompiling many files when switching or rebasing branches.
|
||||
IEx also has been improved to show the documentation for Erlang modules directly from your Elixir terminal. This works with Erlang/OTP 23+ and requires Erlang modules to have been compiled with documentation chunks.
|
||||
|
||||
* Changing your `mix.exs` will no longer trigger a full recompilation, unless you specifically change the configurations used by the Elixir compiler (`:elixirc_paths` and `:elixirc_options`).
|
||||
## Compiler checks: application boundaries
|
||||
|
||||
* Changing compile-time configuration files (`config/config.exs` and any other file imported from it) now only recompiles the project files that depend on the reconfigured applications, instead of a full recompilation. However, if you change the configuration of your application itself, the whole project is still recompiled.
|
||||
Elixir v1.11 builds on top of the recently added compilation tracers to track application boundaries. From this release, Elixir will warn if you invoke a function from an existing module but this module does not belong to any of your listed dependencies.
|
||||
|
||||
* Adding, updating or removing a dependency now only recompiles the project files that depend on the modified a dependency.
|
||||
These two conditions may seem contradictory. After all, if a module is available, it must have come from a dependency. This is not true in two scenarios:
|
||||
|
||||
* If your project has both Erlang and Elixir files, changing an Erlang file will now recompile only the Elixir files that depend on it.
|
||||
* Modules from Elixir and Erlang/OTP are always available - even if their applications are not listed as a dependency
|
||||
|
||||
In a nutshell, Elixir went from triggering full recompilations whenever any of `mix.exs`, `config/config.exs`, `src/*`, and `mix.lock` changed on disk to semantic recompilations. Now it only fully recompiles when:
|
||||
* In an umbrella project, because all child applications are compiled within the same VM, you may have a module from a sibling project available, even if you don't depend on said sibling
|
||||
|
||||
* you change the compilation options in `mix.exs`
|
||||
* you change the configuration for the current project in `config/config.exs`
|
||||
This new compiler check makes sure that all modules that you invoke are listed as part of your dependencies, emitting a warning like below otherwise:
|
||||
|
||||
## mix xref
|
||||
```text
|
||||
:ssl.connect/2 defined in application :ssl is used by the current
|
||||
application but the current application does not directly depend
|
||||
on :ssl. To fix this, you must do one of:
|
||||
|
||||
`mix xref` is a tool that analyzes relationships between files. By analyzing the compile-time and runtime dependencies between files, it allows developers to understand what files have to be recompiled whenever a file changes.
|
||||
1. If :ssl is part of Erlang/Elixir, you must include it under
|
||||
:extra_applications inside "def application" in your mix.exs
|
||||
|
||||
Elixir v1.13 comes with many improvements to `mix xref`, such as:
|
||||
|
||||
* `mix xref graph` now supports `--label` to be set to "compile-connected", which returns all compile-time dependencies that lead to additional transitive dependencies.
|
||||
|
||||
* A new `mix xref trace FILE` subcommand receives a file and returns all dependencies in said file, including the line and what caused said dependency (a function/macro call, an alias, a struct, etc).
|
||||
|
||||
* All `mix xref` subcommands support the `--fail-above` flag, which allows you to enforce your project has at most a certain number of compile-time cycles, transitive compile-time dependencies, etc.
|
||||
|
||||
* `mix xref graph` now supports multiple `--sink` and `--source` to be given.
|
||||
|
||||
With these improvements, it has become simpler to understand the impact code recompilation has in our codebases and how to limit it.
|
||||
|
||||
## Code fragments
|
||||
|
||||
The `Code` module got a companion module called `Code.Fragment`, which hosts functions that work on incomplete code, as is often the scenario in editors, interactive shells, etc. The module contains different heuristics to analyze the source code and return context informational.
|
||||
|
||||
Thanks to these improvements, `IEx`' autocomplete got several quality of life improvements, such as the autocompletion of sigils, structs, and paths. For example, typing `~<TAB>` now shows:
|
||||
|
||||
```iex
|
||||
iex(1)> ~
|
||||
~C (sigil_C) ~D (sigil_D) ~N (sigil_N) ~R (sigil_R)
|
||||
~S (sigil_S) ~T (sigil_T) ~U (sigil_U) ~W (sigil_W)
|
||||
~c (sigil_c) ~r (sigil_r) ~s (sigil_s) ~w (sigil_w)
|
||||
2. If :ssl is a dependency, make sure it is listed under "def deps"
|
||||
in your mix.exs
|
||||
|
||||
3. In case you don't want to add a requirement to :ssl, you may
|
||||
optionally skip this warning by adding [xref: [exclude: :ssl]
|
||||
to your "def project" in mix.exs
|
||||
```
|
||||
|
||||
Adding the sigil letter and pressing tab then shows the available delimiters:
|
||||
This comes with extra benefits in umbrella projects, as it requires applications to depend on the siblings they depend on, which will fail if there are any cyclic dependencies.
|
||||
|
||||
```iex
|
||||
iex(1)> ~r
|
||||
" """ ' ''' ( / < [ { |
|
||||
## Compiler checks: data constructors
|
||||
|
||||
In Elixir v1.11, the compiler also tracks structs and maps fields across a function body. For example, imagine you wanted to write this code:
|
||||
|
||||
def drive?(%User{age: age}), do: age >= 18
|
||||
|
||||
If there is either a typo on the `:age` field or the `:age` field was not yet defined, the compiler will fail accordingly. However, if you wrote this code:
|
||||
|
||||
def drive?(%User{} = user), do: user.age >= 18
|
||||
|
||||
The compiler would not catch the missing field and an error would only be raised at runtime. With v1.11, Elixir will track the usage of all maps and struct fields within the same function, emitting warnings for cases like above:
|
||||
|
||||
```text
|
||||
warning: undefined field `age` in expression:
|
||||
|
||||
# example.exs:7
|
||||
user.age
|
||||
|
||||
expected one of the following fields: name, address
|
||||
|
||||
where "user" was given the type %User{} in:
|
||||
|
||||
# example.exs:7
|
||||
%User{} = user
|
||||
|
||||
Conflict found at
|
||||
example.exs:7: Check.drive?/1
|
||||
```
|
||||
|
||||
Similarly, `%<TAB>` now shows only the available structs (exceptions excluded), instead of all modules:
|
||||
The compiler also checks binary constructors. Consider you have to send a string over the wire with length-based encoding, where the string is prefixed by its length, up to 4MBs. Your initial attempt may be this:
|
||||
|
||||
def run_length(string) when is_binary(string) do
|
||||
<<byte_size(string)::32, string>>
|
||||
end
|
||||
|
||||
However, the code above has a bug. Each segment given between `<<>>` must be an integer, unless specified otherwise. With Elixir v1.11, the compiler will let you know so:
|
||||
|
||||
```text
|
||||
warning: incompatible types:
|
||||
|
||||
binary() !~ integer()
|
||||
|
||||
in expression:
|
||||
|
||||
<<byte_size(string)::integer()-size(32), string>>
|
||||
|
||||
where "string" was given the type integer() in:
|
||||
|
||||
# foo.exs:4
|
||||
<<byte_size(string)::integer()-size(32), string>>
|
||||
|
||||
where "string" was given the type binary() in:
|
||||
|
||||
# foo.exs:3
|
||||
is_binary(string)
|
||||
|
||||
HINT: all expressions given to binaries are assumed to be of type integer()
|
||||
unless said otherwise. For example, <<expr>> assumes "expr" is an integer.
|
||||
Pass a modifier, such as <<expr::float>> or <<expr::binary>>, to change the
|
||||
default behaviour.
|
||||
|
||||
Conflict found at
|
||||
foo.exs:4: Check.run_length/1
|
||||
```
|
||||
|
||||
Which can be fixed by adding `::binary` to the second component:
|
||||
|
||||
def run_length(string) when is_binary(string) do
|
||||
<<byte_size(string)::32, string::binary>>
|
||||
end
|
||||
|
||||
While some of those warnings could be automatically fixed by the compiler, future versions will also perform those checks across functions and potentially across modules, where automatic fixes wouldn't be desired (nor possible).
|
||||
|
||||
## Compilation time improvements
|
||||
|
||||
Elixir v1.11 features many improvements to how the compiler tracks file dependencies, such that touching one file causes less files to be recompiled. In previous versions, Elixir tracked three types of dependencies:
|
||||
|
||||
* compile time dependencies - if A depends on B at compile time, such as by using a macro, whenever B changes, A is recompiled
|
||||
* struct dependencies - if A depends on B's struct, whenever B's struct definition changed, A is recompiled
|
||||
* runtime dependencies - if A depends on B at runtime, A is never recompiled
|
||||
|
||||
However, because dependencies are transitive, if A depends on B at compile time and B depends on C at runtime, A would depend on C at compile time. Therefore, it is very important to reduce the amount of compile time dependencies.
|
||||
|
||||
Elixir v1.11 replaces "struct dependencies" by "exports dependencies". In other words, if A depends on B, whenever B public's interface changes, A is recompiled. B's public interface is made by its struct definition and all of its public functions and macros.
|
||||
|
||||
This change allows us to mark `import`s and `require`s as "exports dependencies" instead of "compile time" dependencies. This simplifies the dependency graph considerably. For example, [in the Hex.pm project](https://github.com/hexpm/hexpm), changing the `user.ex` file in Elixir v1.10 would emit this:
|
||||
|
||||
```text
|
||||
$ touch lib/hexpm/accounts/user.ex && mix compile
|
||||
Compiling 90 files (.ex)
|
||||
```
|
||||
|
||||
In Elixir v1.11, we now get:
|
||||
|
||||
```text
|
||||
$ touch lib/hexpm/accounts/user.ex && mix compile
|
||||
Compiling 16 files (.ex)
|
||||
```
|
||||
|
||||
To make things even better, Elixir v1.11 also introduces a more granular file tracking for path dependencies. In previous versions, a module from a path dependency would always be treated as a compile time dependency. This often meant that if you have an umbrella project, changing an application would cause many modules in sibling applications to recompile. Fortunately, Elixir v1.11 will tag modules from dependencies as exports if appropriate, yielding dramatic improvements to those using path dependencies.
|
||||
|
||||
To round up the list of compiler enhancements, the `--profile=time` option added in Elixir v1.10 now also includes the time to compile each individual file. For example, in the Plug project, one can now get:
|
||||
|
||||
```text
|
||||
[profile] lib/plug/conn.ex compiled in 935ms
|
||||
[profile] lib/plug/ssl.ex compiled in 147ms (plus 744ms waiting)
|
||||
[profile] lib/plug/static.ex compiled in 238ms (plus 654ms waiting)
|
||||
[profile] lib/plug/csrf_protection.ex compiled in 237ms (plus 790ms waiting)
|
||||
[profile] lib/plug/debugger.ex compiled in 719ms (plus 947ms waiting)
|
||||
[profile] Finished compilation cycle of 60 modules in 1802ms
|
||||
[profile] Finished group pass check of 60 modules in 75ms
|
||||
```
|
||||
|
||||
While implementing those features, we have also made the `--long-compilation-threshold` flag more precise. In previous versions, `--long-compilation-threshold` would consider both the time a file spent to compile and the time spent waiting on other files. In Elixir v1.11, it considers only the compilation time. This means less false positives and you can now effectively get all files that take longer than 2s to compile by passing `--long-compilation-threshold 2`.
|
||||
|
||||
## `mix xref graph` improvements
|
||||
|
||||
To bring visibility to the compiler tracking improvements described in the previous section, we have also added new features to `mix xref`. `mix xref` is a task that describes cross-references between files in your projects. The `mix xref graph` subsection focuses on the dependency graph between them.
|
||||
|
||||
First we have made the existing `--label` flag to consider transitive dependencies. Using `--sink FILE` and `--label compile` can be a powerful combo to find out which files will change whenever the given `FILE` changes. For example, in the Hex.pm project, we get:
|
||||
|
||||
```text
|
||||
$ mix xref graph --sink lib/hexpm/accounts/user.ex --label compile
|
||||
lib/hexpm/billing/hexpm.ex
|
||||
└── lib/hexpm/billing/billing.ex (compile)
|
||||
lib/hexpm/billing/local.ex
|
||||
└── lib/hexpm/billing/billing.ex (compile)
|
||||
lib/hexpm/emails/bamboo.ex
|
||||
├── lib/hexpm/accounts/email.ex (compile)
|
||||
└── lib/hexpm/accounts/user.ex (compile)
|
||||
lib/hexpm/emails/emails.ex
|
||||
└── lib/hexpm_web/views/email_view.ex (compile)
|
||||
lib/hexpm_web/controllers/api/docs_controller.ex
|
||||
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
|
||||
lib/hexpm_web/controllers/api/key_controller.ex
|
||||
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
|
||||
lib/hexpm_web/controllers/api/organization_controller.ex
|
||||
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
|
||||
lib/hexpm_web/controllers/api/organization_user_controller.ex
|
||||
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
|
||||
lib/hexpm_web/controllers/api/owner_controller.ex
|
||||
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
|
||||
lib/hexpm_web/controllers/api/package_controller.ex
|
||||
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
|
||||
lib/hexpm_web/controllers/api/release_controller.ex
|
||||
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
|
||||
lib/hexpm_web/controllers/api/repository_controller.ex
|
||||
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
|
||||
lib/hexpm_web/controllers/api/retirement_controller.ex
|
||||
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
|
||||
lib/hexpm_web/controllers/blog_controller.ex
|
||||
└── lib/hexpm_web/views/blog_view.ex (compile)
|
||||
lib/hexpm_web/endpoint.ex
|
||||
├── lib/hexpm_web/plug_parser.ex (compile)
|
||||
└── lib/hexpm_web/session.ex (compile)
|
||||
```
|
||||
|
||||
All the files at the root will recompile if `lib/hexpm/accounts/user.ex` changes. Their children describe the *why*. For example, the `repository_controller.ex` file will recompile if user changes because it has a compile time dependency on `auth_helpers.ex`, which depends on `user.ex`. This indirect compile time dependency is often the source of recompilations and Elixir v1.11 now makes it trivial to spot them, so they can be eventually addressed.
|
||||
|
||||
Another improvement to `mix xref graph` is the addition of `--format cycles`, which will print all cycles in your compilation dependency graph. A `--min-cycle-size` flag can be used if you want to discard short cycles.
|
||||
|
||||
## `config/runtime.exs` and `mix app.config`
|
||||
|
||||
Elixir v1.9 introduced a new configuration file, specific to releases, called `config/releases.exs`. A release is a self-contained artifact with the Erlang VM, Elixir and your application, ready to run in production.
|
||||
|
||||
The addition of `config/releases.exs` has been a very useful one but, unfortunately, it applies only to releases. Developers not using releases must use the `config/config.exs` file, which often loaded too early at compilation time. For any dynamic configuration, developers had to resort to third-party tools or workarounds to achieve the desired results.
|
||||
|
||||
Elixir v1.11 addresses this issue by introducing a new configuration file, called `config/runtime.exs`. This new configuration file is loaded exactly before your application starts, when the code is already fully compiled. It is loaded in development, test, and production, regardless if you are using Mix or releases. Therefore it provides a unified API for runtime configuration in Elixir.
|
||||
|
||||
`config/runtime.exs` works the same as any other configuration file. However, given `config/runtime.exs` is meant to run with or without Mix, developers must not use `Mix.env()` or `Mix.target()` in `config/runtime.exs`. Instead, they must use the new `config_env()` and `config_target()`, which have been added to the `Config` module.
|
||||
|
||||
While `config/releases.exs` will continue to be supported, developers can migrate to `config/runtime.exs` without loss of functionality. For example, a `config/releases.exs` file such as this one
|
||||
|
||||
```elixir
|
||||
iex(1)> %File.St
|
||||
File.Stat File.Stream
|
||||
# config/releases.exs
|
||||
import Config
|
||||
|
||||
config :foo, ...
|
||||
config :bar, ...
|
||||
```
|
||||
|
||||
Once you define the struct, you can hit `tab` to show all struct fields available:
|
||||
could run as is as `config/runtime.exs`. However, given `config/runtime.exs` runs in all environments, you may want to restrict part of your configuration to the `:prod` environment:
|
||||
|
||||
```elixir
|
||||
iex(1)> %URI{
|
||||
authority: fragment: host: path: port:
|
||||
query: scheme: userinfo:
|
||||
```
|
||||
# config/runtime.exs
|
||||
import Config
|
||||
|
||||
As you fill a field in, the already filled fields no longer show up:
|
||||
|
||||
```elixir
|
||||
iex(1)> %URI{path: "/example",
|
||||
authority: fragment: host: port: query:
|
||||
scheme: userinfo:
|
||||
```
|
||||
|
||||
Along the same lines, `SyntaxError` and `TokenMissingError` were improved to show a code snippet whenever possible:
|
||||
|
||||
```elixir
|
||||
$ elixir -e "hello + * world"
|
||||
** (SyntaxError) nofile:1:9: syntax error before: '*'
|
||||
|
|
||||
1 | hello + * world
|
||||
| ^
|
||||
```
|
||||
|
||||
Finally, new compilation tracers have been added, alongside a handful of functions in `Module` to retrieve module metadata, which can be used to enrich suggestions in programming environments.
|
||||
|
||||
## Extended code formatting
|
||||
|
||||
The `mix format` task has been augmented with the notion of plugins. Plugins can teach the formatter how to format new files and how to format sigils, via the `Mix.Tasks.Format` behaviour.
|
||||
|
||||
For example, imagine that your project uses Markdown in two distinct ways: via a custom `~M` sigil and via files with the `.md` and `.markdown` extensions. A custom plugin would look like this:
|
||||
|
||||
```elixir
|
||||
defmodule MixMarkdownFormatter do
|
||||
@behaviour Mix.Tasks.Format
|
||||
|
||||
def features(_opts) do
|
||||
[sigils: [:M], extensions: [".md", ".markdown"]]
|
||||
end
|
||||
|
||||
def format(contents, opts) do
|
||||
# logic that formats markdown
|
||||
end
|
||||
if config_env() == :prod do
|
||||
config :foo, ...
|
||||
config :bar, ...
|
||||
end
|
||||
```
|
||||
|
||||
Now any application can use your formatter as follows:
|
||||
If both files are available, releases will pick the now preferred `config/runtime.exs` instead of `config/releases.exs`.
|
||||
|
||||
```elixir
|
||||
# .formatter.exs
|
||||
[
|
||||
# Define the desired plugins
|
||||
plugins: [MixMarkdownFormatter],
|
||||
# Remember to update the inputs list to include the new extensions
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}", "posts/*.{md,markdown}"]
|
||||
]
|
||||
```
|
||||
To wrap it all up, `Mix` also includes a new task called `mix app.config`. This task loads all applications and configures them, without starting them. Whenever you write your own Mix tasks, you will typically want to invoke either `mix app.start` or `mix app.config` before running your own code. Which one is better depends if you want your applications running or only configured.
|
||||
|
||||
Finally, the `Code` module has also been augmented with two functions: `Code.string_to_quoted_with_comments/2` and `Code.quoted_to_algebra/2`. Those functions allow someone to retrieve the Elixir AST with their original source code comments, and then convert this AST to formatted code. In other words, those functions provide a wrapper around the Elixir Code Formatter, supporting developers who wish to create tools that directly manipulate and custom format Elixir source code.
|
||||
## Other improvements
|
||||
|
||||
## v1.13.2 (2022-01-13)
|
||||
Elixir v1.11 adds the `is_struct/2`, `is_exception/1`, and `is_exception/2` guards. It also adds support for the `map.field` syntax in guards.
|
||||
|
||||
The Calendar module ships with a new `Calendar.strftime/3` function, which provides datetime formatting based on the `strftime` format. The `Date` module got new functions for working with weeks and months, such as `Date.beginning_of_month/1` and `Date.end_of_week/2`. Finally, all calendar types got conversion functions from and to gregorian timestamps, such as `Date.from_gregorian_days/2` and `NaiveDateTime.to_gregorian_seconds/1`.
|
||||
|
||||
Mix also includes two new tasks: `mix app.config`, for application runtime configuration, and `mix test.coverage`, which generates aggregated coverage reports for umbrella projects and for test suites partitioned across processes.
|
||||
|
||||
## v1.11.4 (2021-03-16)
|
||||
|
||||
This release introduces fixes for better support of Erlang/OTP 24+.
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Kernel] Update formatting when printing warnings and errors from Erlang/OTP 24+
|
||||
* [Kernel] Support float-16 on bitstrings
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix format] Allow plugins to also format `.ex` and `.exs` files
|
||||
* [mix release] Allow bypassing application mode validation in release spec
|
||||
* [mix test] Print a message when the suite fails due to the coverage threshold
|
||||
* [mix local.rebar] This task will now install rebar3 version 3.14.4, compiled with Erlang/OTP 21
|
||||
|
||||
## v1.11.3 (2021-01-04)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Macro] Add `Macro.unique_var/2` and `Macro.generate_unique_arguments/2`
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] Do not emit warnings on `Code.Fragment.container_cursor_to_quoted/2`
|
||||
* [Kernel] Fix a crash when a for-comprehension with `:uniq` was used inside another comprehension with `:uniq`
|
||||
* [Kernel] Ensure `env.context_modules` is properly set inside optimized `defmodule`
|
||||
* [Keyword] Deprecate the recently added `Keyword.map/2` as it is equivalent to `Keyword.new/2`
|
||||
* [Map] Deprecate the recently added `Map.map/2` as it is equivalent to `Map.new/2`
|
||||
* [Protocol] Warn on zero arity callbacks inside protocols
|
||||
* [Code] Do not raise when checking for operator ambiguity when `:static_atoms_encoder` is set in `Code.string_to_quoted`
|
||||
* [Kernel] Emit undefined function warnings from `with`
|
||||
* [Kernel] Do not fail type checking when literals are used in guards
|
||||
* [Module] Do not warn for attributes used in `@after_compile`
|
||||
* [Record] Make sure nested record names do not clobber each other
|
||||
|
||||
## v1.13.1 (2021-12-14)
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit.Assertions] Do not crash if there are macros and module attributes on the left side of `++`
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.Helpers] Do not use Unicode chars if ANSI is disabled
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix deps.compile] Fix compatibility with rebar v3.14
|
||||
* [mix release] Do not use private `ram_file:compress/1`
|
||||
* [mix xref] Do not crash when retrieving calls for modules in memory
|
||||
|
||||
## v1.11.2 (2020-11-03)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] Do not show code snippets in `SyntaxError` and `TokenMissingError` if line is empty
|
||||
* [Exception] Do not fail blaming `ArgumentError` for improper lists on `apply/3`
|
||||
* [Macro] Set a max `line_length` for `Macro.to_string/1`
|
||||
* [Macro] Fix formatting of lists on module attributes for `Macro.to_string/1`
|
||||
* [String] Fix incorrect codepoint byte counting in `slice` with negative positions in ranges
|
||||
* [Task] Ensure async streams can be consumed from another process than the one that creates them
|
||||
* [URI] Undeprecate `URI.parse/1` as `URI.new/1` is too strict in many common cases
|
||||
* [URI] Make sure `URI.new/1` returns nil for empty paths
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx] Make sure the `--version` flag halts IEx
|
||||
* [Code] Do not crash when getting docs for missing `erts` appdir
|
||||
* [Kernel] Raise meaningful error if `:erlang.is_record` is used in guards
|
||||
* [Kernel] Prune tracers when fetching `__ENV__` inside functions
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Make protocol consolidation part of the `Mix.install/2` cache
|
||||
* [mix] Fix regression where aliases could not call themselves recursively
|
||||
* [mix compile] Do not discard tracers that are set programatically
|
||||
|
||||
## v1.13.0 (2021-12-03)
|
||||
## v1.11.1 (2020-10-16)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] Ignore tracers if lexical tracker is dead or explicitly nil when evaling code with an environment
|
||||
* [GenServer] Do not show warning when using `super` in `GenServer.child_spec/1`
|
||||
* [Kernel] Do not crash when :reduce is set to `nil` in comprehensions
|
||||
* [Kernel] Fix a scenario where undefined function warnings were not being emitted
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.Helpers] Properly handle tags inside typespec when showing Erlang docs
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Do not deadlock Logger if handler crashes on sync mode
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Add inet6 fallback to Mix usage of httpc
|
||||
* [mix compile.app] Do not list apps that do not match the current target
|
||||
|
||||
## v1.11.0 (2020-10-06)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx] Add `:parser_options` to EEx functions
|
||||
* [EEx] Track column information in EEx templates when enabled in the compiler
|
||||
* [EEx] Show column information in EEx error messages
|
||||
* [EEx] Support `:indentation` option when compiling EEx templates for proper column tracking
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Calendar] Add `c:Calendar.year_of_era/3` to support calendars where the beginning of a new era does not align with the beginning of a new year
|
||||
* [CLI] Support `--short-version` on the CLI that does not boot the VM
|
||||
* [Code] Add `Code.string_to_quoted_with_comments/2` and `Code.quoted_to_algebra/2`
|
||||
* [Code] Add more `:token_metadata` to aliases and remote calls when parsing strings
|
||||
* [Code] Add `Code.Fragment` module to provide best-effort information from code fragments. The module currently provides an updated `Code.Fragment.cursor_context/2` with operator support and `Code.Fragment.surround_context/2` which looks at a given position in a fragment and find its surrounding delimiters
|
||||
* [Code] Allow custom sigil formatting on `Code.format_string!/2`
|
||||
* [Code] Add `{:on_module, bytecode, :none}` trace to compilation tracers
|
||||
* [Enum] Optimize `Enum.concat/1` for lists of lists
|
||||
* [Enum] Add `Enum.slide/3`
|
||||
* [Exception] Better format Elixir exceptions in Erlang
|
||||
* [Inspect] Allow default inspect fun to be set globally with `Inspect.Opts.default_inspect_fun/1`
|
||||
* [IO] Allow `:eof` to be given as limit to `IO.getn/2`
|
||||
* [Kernel] Support the `:sigils` option in `import Mod, only: :sigils` and allow the sigil modifiers to be also digits
|
||||
* [Kernel] Make `get_in` consistently abort and return `nil` when `nil` values are found (previously Elixir would raise an error in this case). This allows a user to use `get_in` as a safe navigation operator.
|
||||
* [Kernel] Improve compilation times by reducing the amount of copies of the AST across compiler processes
|
||||
* [Kernel] Raise if trying to define a module with a slash in its name
|
||||
* [Kernel] Warn when `?\` is used and there is no need for a escape character
|
||||
* [Kernel] Track structs in typespecs as export deps instead of compile-time deps
|
||||
* [Kernel] Add power operator (`**/2`)
|
||||
* [Keyword] Add `Keyword.validate/2`
|
||||
* [Keyword] Implement `Keyword.filter/2` and `Keyword.map/2`
|
||||
* [List] Add `List.keyfind!/3`
|
||||
* [Macro] Add `Macro.prewalker/1` and `Macro.postwalker/1`
|
||||
* [Macro.Env] Add the following reflection functions: `required?/2`, `lookup_import/2`, `fetch_alias/2`, and `fetch_macro_alias/2`
|
||||
* [Map] Implement `Map.filter/2` and `Map.map/2`
|
||||
* [Module] Support `:nillify_clauses` in `Module.get_definition/3`
|
||||
* [Module] Add `Module.attributes_in/1` and `Module.overridables_in/1`
|
||||
* [OptionParser] Add "did you mean?" suggestions to `OptionParser.ParseError` messages
|
||||
* [Record] Add record reflection via `@__records__`
|
||||
* [Task] Add `Task.completed/1`
|
||||
* [Task] Add `Task.ignore/1` to keep a task running but ignoring all of its results
|
||||
* [Task] Reduce the amount of copying `Task.async*` functions
|
||||
* [URI] Add `URI.new/1` and `URI.new!/1`
|
||||
* [Access] Add `Access.at!/1`
|
||||
* [Calendar] Add `Calendar.strftime/3` for datetime formatting
|
||||
* [Calendar] Add linear integer representations to Calendar modules: `Date.from_gregorian_days/2`, `Date.to_gregorian_days/1`, `NaiveDateTime.from_gregorian_seconds/3`, `NaiveDateTime.to_gregorian_seconds/1`, `Time.from_seconds_after_midnight/1`, and `Time.to_seconds_after_midnight/1`
|
||||
* [Calendar] Add `new!` to Date/Time/NaiveDateTime/DateTime (`new` has also been added to `DateTime` for completeness)
|
||||
* [Calendar] Support custom starting day of the week in `Date.day_of_week/2`
|
||||
* [Calendar] Add `Date.beginning_of_month/1` and `Date.end_of_month/1`
|
||||
* [Calendar] Add `Date.beginning_of_week/2` and `Date.end_of_week/2`
|
||||
* [Code] Add `:column` to `Code.string_to_quoted*/2`
|
||||
* [Code] Add `Code.can_await_module_compilation?/0` to check if the parallel compiler is enabled and it can await for other modules to be compiled
|
||||
* [Config] Support `config_env/0` and `config_target/0` in `config` files
|
||||
* [Config] Allow `import_config` to be disabled for some configuration files
|
||||
* [Enum] Allow a sorting function on `Enum.min_max_by/3,4`, including the new `compare/2` conventions
|
||||
* [Kernel] Add `is_struct/2` guard
|
||||
* [Kernel] Add `is_exception/1` and `is_exception/2` guards
|
||||
* [Kernel] Support `map.field` syntax in guards
|
||||
* [Kernel] Add `+++` and `---` with right associativity to the list of custom operators
|
||||
* [Kernel] Warn if a variable that looks like a compiler variable (such as `__MODULE__`) is unused
|
||||
* [Kernel.ParallelCompiler] Report individual file compilation times when `profile: :time` is given
|
||||
* [Kernel.ParallelCompiler] Improve precision of `:long_compilation_threshold` so it takes only compilation times into account (and not waiting times)
|
||||
* [Registry] Add `Registry.delete_meta/2`
|
||||
* [Task] Add `Task.await_many/2`
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Show hint if comparing different but equivalent strings
|
||||
* [ExUnit.CaptureIO] Add `with_io/3` to return result with captured io
|
||||
* [ExUnit.CaptureLog] Add `with_log/2` to return result with captured logs
|
||||
* [ExUnit] Add support for coloring on Windows 10 consoles/shells
|
||||
* [ExUnit] Add `ExUnit.fetch_test_supervisor/0`
|
||||
* [ExUnit] Add `@tag :tmp_dir` support to ExUnit. The temporary directory is automatically created and pruned before each test
|
||||
* [ExUnit] Add file and line to ExUnit's `--trace`
|
||||
* [ExUnit.Assertion] Allow receive timeouts to be computed at runtime
|
||||
* [ExUnit.Case] Add `register_test/6` to speed up compilation of custom tests
|
||||
* [ExUnit.Doctest] Allow users to add tags to doctests
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.Autocomplete] Add path autocompletion whenever when the cursor follows `"./` or `"/` or `"DRIVER:` where `DRIVER` is a single letter
|
||||
* [IEx.Autocomplete] Add autocompletion for sigils, struct names, and struct fields
|
||||
* [IEx.Helpers] Allow multiple modules to be given to `r/1`
|
||||
* [IEx] Add support for coloring on Windows 10 consoles/shells
|
||||
* [IEx.Helpers] Show docs from Erlang modules that have been compiled with the docs chunk
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Add `Logger.put_application_level/2`
|
||||
* [Logger] Print all log levels in accordance to Erlang/OTP. This also means `[warn]` is now shown as `[warning]`
|
||||
* [Logger] Add `notice`, `critical`, `alert`, and `emergency` log levels
|
||||
* [Logger] Support structured logging by logging maps or keyword lists
|
||||
* [Logger] Allow level to be set per module with `Logger.put_module_level/2`
|
||||
* [Logger] Include `erl_level` in Logger's metadata
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Add `MIX_INSTALL_FORCE` environment variable support
|
||||
* [Mix] Support `:config` and `:system_env` in `Mix.install/2`
|
||||
* [Mix] Add `Mix.installed?/0`
|
||||
* [Mix.Shell] Add `:default` option to `Mix.Shell.yes?`
|
||||
* [mix archive.install] Run `loadconfig` before building archive
|
||||
* [mix compile] Move Elixir version check to before deps are compiled, in order to give feedback earlier
|
||||
* [mix compile.elixir] Do not recompile files if their modification time change but their contents are still the same and the .beam files are still on disk
|
||||
* [mix compile.elixir] Do not recompile all Elixir sources when Erlang modules change, only dependent ones
|
||||
* [mix compile.elixir] Do not recompile Elixir files if `mix.exs` changes, instead recompile only files using `Mix.Project` or trigger a recompilation if a compiler option changes
|
||||
* [mix compile.elixir] Only recompile needed files when a dependency is added, updated or removed
|
||||
* [mix compile.elixir] Only recompile needed files when a dependency is configured
|
||||
* [mix deps] Add `:subdir` option to git deps
|
||||
* [mix escript.install] Run `loadconfig` before building escript
|
||||
* [mix format] Support `:plugins` in `mix format` that can hook into custom extensions and sigils
|
||||
* [mix format] Add `Mix.Tasks.Format.formatter_for_file/2`
|
||||
* [mix local.rebar] No longer support `sub_dirs` in Rebar 2 to help migration towards Rebar 3
|
||||
* [mix local.rebar] Support `--if-missing` option when installing Rebar
|
||||
* [mix local.rebar] Set `REBAR_PROFILE=prod` when compiling Rebar dependencies
|
||||
* [mix test] Support `--profile-require=time` to profile the time loading test files themselves
|
||||
* [mix test] Allow filtering modules from coverage using regex
|
||||
* [mix test] Allow the exit status of ExUnit to be configured and set the default to 2
|
||||
* [mix test] Exit with a status of 3 when coverage falls below threshold
|
||||
* [mix test] Write failed manifest when suite fails due to --warnings-as-errors
|
||||
* [mix test] Ignore `MIX_TEST_PARTITION` when partitions set to 1
|
||||
* [mix xref] Support multiple sinks and sources in `mix xref graph`
|
||||
* [mix xref] Add `trace` subcommand to print compilation dependencies between files
|
||||
* [mix xref] Add `--fail-above` option to `mix xref`
|
||||
* [mix xref] Add `--label compile-connected` to `mix xref`
|
||||
* [mix] Add `MIX_BUILD_ROOT` to config `_build` dir
|
||||
* [mix] Introduce `MIX_XDG` as a simpler mechanism to opt-in to the XDG specification
|
||||
* [mix] Allow requirements for a Mix task to be listed via the `@requirements` module attribute
|
||||
* [mix] Allow optional dependencies to be defined in `:extra_applications` and `:applications`
|
||||
* [mix app.config] Add new `mix app.config` task that compiles applications and loads runtime configuration
|
||||
* [mix archive.install] Support `--repo` option on Hex packages
|
||||
* [mix compile] Support the `__mix_recompile__?/0` callback for custom behaviour on when Mix should recompile a given module
|
||||
* [mix compile.elixir] Mark modules for path dependencies as "Export dependencies" if they changed but their public interface is the same
|
||||
* [mix compile.elixir] Track application boundaries in the Elixir compiler. If you invoke code from Erlang or Elixir standard libraries and you don't depend on the proper applications, a warning will be emitted. A warning will also be emitted if you invoke code from an umbrella sibling that you don't depend on - effectively forbidding cyclic dependencies between apps
|
||||
* [mix deps] Sort the dependencies alphabetically before printing
|
||||
* [mix deps] Use `origin/HEAD` as the default Git ref in dependencies
|
||||
* [mix deps] Redact Git `username`/`password` in output log
|
||||
* [mix deps] Support rebar3's `git_subdir` resource type
|
||||
* [mix deps.compile] Allow local deps to be skipped on `mix deps.compile`
|
||||
* [mix deps.unlock] Print which dependencies get unlocked when using the `--unused` flag
|
||||
* [mix escript.install] Support `--repo` option on Hex packages
|
||||
* [mix new] Add `@impl` to application generated by `mix new --sup`
|
||||
* [mix release] Enable overriding `sys.config` location via `RELEASE_SYS_CONFIG` env var
|
||||
* [mix release] Boot a release under configuration in interactive mode and then swap to embedded mode (if running on Erlang/OTP 23+)
|
||||
* [mix release] Add `rel_templates_path` to configure the source of template files such as "env.sh.eex", "vm.args.eex" and "overlays"
|
||||
* [mix release] Allow some chunks to be kept in the `:strip_beams` config
|
||||
* [mix test] Allow `:ignore_modules` inside `:test_coverage` option
|
||||
* [mix test.coverage] Add `mix test.coverage` that aggregates coverage results from umbrellas and OS partitioning
|
||||
* [mix xref] Make the `--label` option for `mix xref graph` transitive by default and add `--only-direct` for only direct dependencies
|
||||
* [mix xref] Add `--format cycles` support for `mix xref graph`
|
||||
* [mix xref] Add support to `mix xref graph` for using `--source` and `--sink` at the same time
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx] Accept comments in EEx between do and the first clause
|
||||
* [EEx] Accept EEx expressions where `->` is followed by newline
|
||||
* [EEx] Make trimming behaviour via the `:trim` option more consistent
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Application] Allow any expression as first argument of `compile_env`
|
||||
* [Application] Warn if `Application.compile_env` or `Application.compile_env!` are called without a require
|
||||
* [Code] Make sure `:static_atoms_encoder` in `Code.string_to_quoted/2` also applies to quoted keyword keys
|
||||
* [Code] Ensure bindings with no context are returned as atoms instead of `{binding, nil}` in eval operations
|
||||
* [Inspect] Fix a bug when inspecting a non-binary bitstring with colors
|
||||
* [Kernel] Reject bidirectional formatting characters in strings and comments
|
||||
* [Kernel] Support escaping of terminators in uppercase sigils heredocs for consistency
|
||||
* [Kernel] Raise if `__CALLER__` or `__ENV__` or `__STACKTRACE__` are used in match
|
||||
* [Kernel] Improve error message on invalid argument for `byte_size` from binary concat
|
||||
* [Kernel] Raise when aliasing non-Elixir modules without `:as`
|
||||
* [Kernel] Allow `unquote_splicing` inside `%{...}` without parens
|
||||
* [Kernel] Ensure that waiting on a struct expansion inside a typespec is correctly tracked as waiting time in the compiler
|
||||
* [Kernel] Correctly parse the atom `.` as a keyword list key
|
||||
* [Kernel] Do not leak variables from the first generator in `with` and `for` special forms
|
||||
* [Kernel] Fix column number on strings with NFD characters
|
||||
* [Kernel] Fix a bug where a combination of dynamic line in `quote` with `unquote` of remote calls would emit invalid AST metadata
|
||||
* [OptionParser] Validate switch types/modifiers early on to give more precise feedback
|
||||
* [Protocol] Add `defdelegate` to the list of unallowed macros inside protocols as protocols do not allow function definitions
|
||||
* [Protocol] Warn if `@callback`, `@macrocallback` and `@optional_callbacks` are defined inside protocol
|
||||
* [Protocol] Ensure protocol metadata is deterministic on consolidation
|
||||
* [Range] Always show step when range is descending
|
||||
* [String] Update Unicode database to version 14.0
|
||||
* [URI] Only percent decode if followed by hex digits (according to https://url.spec.whatwg.org/#percent-decode)
|
||||
* [Version] Ensure proper precedence of `and`/`or` in version requirements
|
||||
* [Application] Warn if non-atom keys are given to `put_env`, `get_env`, `fetch_env`, and `delete_env`
|
||||
* [Code] Do not send language keyword through the `:static_atoms_encoder` in `Code.string_to_quoted`
|
||||
* [Kernel] Validate values given to `:line` in quote to avoid emitting invalid ASTs
|
||||
* [Kernel] Report the correct line number when raising inside a macro
|
||||
* [Kernel] Fix an issue where `elixirc` would not accept paths with backslash (`\`) separators on Windows
|
||||
* [Kernel] Properly parse `&//2` (i.e. the capture of the division operator)
|
||||
* [Kernel] Raise `CompileError` when trying to define reserved types
|
||||
* [Kernel] Improve compiler error message when using `|` in a `def` signature
|
||||
* [Kernel] Improve error message when trying to use invalid list operators in guards
|
||||
* [Kernel.SpecialForms] Add `|/2` to the list of special forms to avoid inconsistent behaviour on overrides
|
||||
* [Keyword] Enforce keys to be atoms in `Keyword.keys/1`
|
||||
* [Record] Keep lexical ordering when creating records
|
||||
* [Registry] Do not crash when a process with key-value has been registered using `:via` and it fails to start on `init`
|
||||
* [URI] `URI.decode_query/2` emits an empty string for parameters without values, according to [URL's living standard](https://url.spec.whatwg.org/#application/x-www-form-urlencoded) - note this behaviour is not specified in the spec implemented by the URI module, so the living standard was chosen
|
||||
* [Version] Add defaults and enforce keys in `Version` struct
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Fix formatter and counters from `ExUnit.run/0` to consider all tests in a module whenever if a module's `setup_all` fails
|
||||
* [ExUnit] Allow doctests newlines to be terminated by CRLF
|
||||
* [ExUnit.CaptureIO] Fix race condition where a dead capture would still be considered as active
|
||||
* [ExUnit.Diff] Do not crash when failing to eval/inspect struct
|
||||
* [ExUnit.Diff] Properly diff numbers in respect to `==` and `===` operators
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx] Fix the loss of `.iex.exs` context after a pry session
|
||||
* [IEx] Stop evaluator before exiting IEx server to avoid evaluators leaking
|
||||
* [IEx] Fix tokenizer emitting repeated warnings in the REPL
|
||||
* [IEx] Ensure `--dot-iex` is preserved when restarting the evaluator and after shell respawn
|
||||
* [IEx.Pry] Ensure `IEx.pry` can be triggered more than twice when invoked from the same process
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix cmd] Fix a bug where only the first --app option would be executed
|
||||
* [mix compile] Fix an issue where new protocol implementations would not propagate when running `mix compile` from an umbrella root
|
||||
* [mix deps.compile] Use `gmake` instead of `make` when compiling deps on NetBSD/DragonFlyBSD
|
||||
* [mix release] Load `.app` from dependencies path when it is a project dependency
|
||||
* [mix release] Always include "rel/overlays" in the list of overlays directories if available
|
||||
* [mix release] Change `erts/bin/erl` binary mode to `0o755`
|
||||
* [mix test] Compare to test coverage threshold inclusively
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Raise clear error message for invalid `:compile_time_purge_matching` configuration
|
||||
* [Logger] Fix a bug where Logger would not reset its discard counter under some scenarios
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix compile.elixir] Track transitive runtime dependencies coming from local/path dependencies
|
||||
* [mix compile.elixir] Recompile file if `@external_resource` is deleted
|
||||
* [mix compile.elixir] Print number of compiling files on all compiler cycles. This will make the `Compiling N files (.ex)` show up multiple times if necessary
|
||||
* [mix deps] Raise if local dep is unavailable while compiling
|
||||
* [mix deps.unlock] Fix blank output when unlocking a dependency that is not locked
|
||||
* [mix local.install] Do not respect `MIX_DEPS_PATH` for install commands
|
||||
* [mix release] Improve release scripts by making sure shell errors cascade (this is done by avoiding exporting and defining variables in a single step)
|
||||
* [mix release] Do not boot release if `RELEASE_COOKIE` is empty
|
||||
* [mix release] Allow releases running as a daemon to be restarted
|
||||
* [mix release] Raise proper error message when non-serializable values are in configs
|
||||
* [mix test] Fix coverage engine to also tag `case`, `cond`, and `receive` branches where the right side is a literal
|
||||
* [Logger] Print metadata for all types that implement String.Chars
|
||||
|
||||
### 3. Soft-deprecations (no warnings emitted)
|
||||
|
||||
#### Elixir
|
||||
### Elixir
|
||||
|
||||
* [Code] Environment options in `Code.eval_quoted/3` and `Code.eval_string/3`, such as `:aliases` and `:tracers`, have been deprecated in favor of passing an environment
|
||||
* [IO] `:all` on `IO.getn` is deprecated in favor of `:eof`
|
||||
* [URI] `URI.parse/1` is deprecated in favor of `URI.new/1` and `URI.new!/1`
|
||||
* [Exception] `Exception.exception?/1` is deprecated in favor of `Kernel.is_exception/1`
|
||||
* [Regex] `Regex.regex?/1` is deprecated in favor of `Kernel.is_struct/2`
|
||||
|
||||
#### Mix
|
||||
### Logger
|
||||
|
||||
* [mix format] `Mix.Tasks.Format.formatter_opts_for_file/2` is deprecated in favor of `Mix.Tasks.Format.formatter_for_file/2`
|
||||
* [Logger] `warn` log level is deprecated in favor of `warning`
|
||||
|
||||
### Mix
|
||||
|
||||
* [mix release] `config/releases.exs` is deprecated in favor of a more general purpose `config/runtime.exs`
|
||||
|
||||
### 4. Hard-deprecations
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] `Code.cursor_context/2` is deprecated, use `Code.Fragment.cursor_context/2` instead
|
||||
* [Macro] `Macro.to_string/2` is deprecated, use `Macro.to_string/1` instead
|
||||
* [System] `System.get_pid/0` is deprecated, use `System.pid/0` instead
|
||||
* [Version] Using `!` or `!=` in version requirements is deprecated, use `~>` or `>=` instead
|
||||
* [Supervisor] Deprecate `Supervisor.start_child/2` and `Supervisor.terminate_child/2` in favor of `DynamicSupervisor`
|
||||
* [Supervisor.Spec] Deprecate `Supervisor.Spec.worker/3` and `Supervisor.Spec.supervisor/3` in favor of the new typespecs
|
||||
* [System] Deprecate `System.stacktrace/0` in favor of `__STACKTRACE__`
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix escript.build] `:strip_beam` option is deprecated in favor of `:strip_beams`
|
||||
* [Mix] `:exit_code` in `Mix.raise/2` has been deprecated in favor of `:exit_status`
|
||||
* [Mix.Config] `Mix.Config` is deprecated in favor of `Config` module
|
||||
* [Mix.Project] Deprecate `Mix.Project.compile/2` in favor of `Mix.Task.run("compile", args)`
|
||||
|
||||
## v1.12
|
||||
## v1.10
|
||||
|
||||
The CHANGELOG for v1.12 releases can be found [in the v1.12 branch](https://github.com/elixir-lang/elixir/blob/v1.12/CHANGELOG.md).
|
||||
The CHANGELOG for v1.10 releases can be found [in the v1.10 branch](https://github.com/elixir-lang/elixir/blob/v1.10/CHANGELOG.md).
|
||||
|
||||
+10
-16
@@ -13,23 +13,17 @@ The goal of the Code of Conduct is to specify a baseline standard of behavior so
|
||||
These are the values Elixir developers should aspire to:
|
||||
|
||||
* Be friendly and welcoming
|
||||
* Be kind
|
||||
* Be patient
|
||||
* Remember that people have varying communication styles and that not everyone is using their native language. (Meaning and tone can be lost in translation.)
|
||||
* Interpret the arguments of others in good faith, do not seek to disagree.
|
||||
* When we do disagree, try to understand why.
|
||||
* Be thoughtful
|
||||
* Productive communication requires effort. Think about how your words will be interpreted.
|
||||
* Remember that sometimes it is best to refrain entirely from commenting.
|
||||
* Be respectful
|
||||
* In particular, respect differences of opinion. It is important that we resolve disagreements and differing views constructively.
|
||||
* Be constructive
|
||||
* Avoid derailing: stay on topic; if you want to talk about something else, start a new conversation.
|
||||
* Avoid unconstructive criticism: don't merely decry the current state of affairs; offer — or at least solicit — suggestions as to how things may be improved.
|
||||
* Avoid harsh words and stern tone: we are all aligned towards the well-being of the community and the progress of the ecosystem. Harsh words exclude, demotivate, and lead to unnecessary conflict.
|
||||
* Avoid snarking (pithy, unproductive, sniping comments).
|
||||
* Avoid microaggressions (brief and commonplace verbal, behavioral and environmental indignities that communicate hostile, derogatory or negative slights and insults towards a project, person or group).
|
||||
* Be responsible
|
||||
* What you say and do matters. Take responsibility for your words and actions, including their consequences, whether intended or otherwise.
|
||||
* Avoid destructive behavior
|
||||
* Derailing: stay on topic; if you want to talk about something else, start a new conversation.
|
||||
* Unconstructive criticism: don't merely decry the current state of affairs; offer (or at least solicit) suggestions as to how things may be improved.
|
||||
* Snarking (pithy, unproductive, sniping comments).
|
||||
|
||||
The following actions are explicitly forbidden:
|
||||
|
||||
@@ -47,11 +41,11 @@ Explicit enforcement of the Code of Conduct applies to the official mediums oper
|
||||
|
||||
* The [official GitHub projects][1] and code reviews.
|
||||
* The official elixir-lang mailing lists.
|
||||
* The **[#elixir][2]** IRC channel on [Libera.Chat][3].
|
||||
* The **[#elixir-lang][2]** IRC channel on [Freenode][3].
|
||||
|
||||
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.
|
||||
|
||||
Project maintainers may block, remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
|
||||
Project maintainers may remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by emailing: elixir-lang-conduct@googlegroups.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. **All reports will be kept confidential**.
|
||||
|
||||
@@ -59,8 +53,8 @@ Instances of abusive, harassing, or otherwise unacceptable behavior may be repor
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
This document was based on the Code of Conduct from the Go project (dated Sep/2021) and the Contributor Covenant (v1.4).
|
||||
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://web.libera.chat/#elixir
|
||||
[3]: https://libera.chat/
|
||||
[2]: https://webchat.freenode.net/?channels=#elixir-lang
|
||||
[3]: https://www.freenode.net
|
||||
|
||||
@@ -2,8 +2,7 @@ PREFIX ?= /usr/local
|
||||
TEST_FILES ?= "*_test.exs"
|
||||
SHARE_PREFIX ?= $(PREFIX)/share
|
||||
MAN_PREFIX ?= $(SHARE_PREFIX)/man
|
||||
CANONICAL := 1.13/
|
||||
CANONICAL ?= master/
|
||||
CANONICAL := v1.11/
|
||||
ELIXIRC := bin/elixirc --ignore-module-conflict $(ELIXIRC_OPTS)
|
||||
ERLC := erlc -I lib/elixir/include
|
||||
ERL_MAKE := if [ -n "$(ERLC_OPTS)" ]; then ERL_COMPILER_OPTIONS=$(ERLC_OPTS) erl -make; else erl -make; fi
|
||||
@@ -28,9 +27,9 @@ SOURCE_DATE_EPOCH_FILE = $(SOURCE_DATE_EPOCH_PATH)/SOURCE_DATE_EPOCH
|
||||
#==> Functions
|
||||
|
||||
define CHECK_ERLANG_RELEASE
|
||||
erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 22)])' -s erlang halt | grep -q '^true'; \
|
||||
erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 21)])' -s erlang halt | grep -q '^true'; \
|
||||
if [ $$? != 0 ]; then \
|
||||
echo "At least Erlang/OTP 22.0 is required to build Elixir"; \
|
||||
echo "At least Erlang/OTP 21.0 is required to build Elixir"; \
|
||||
exit 1; \
|
||||
fi
|
||||
endef
|
||||
@@ -83,7 +82,7 @@ $(PARSER): lib/elixir/src/elixir_parser.yrl
|
||||
|
||||
# Since Mix depends on EEx and EEx depends on Mix,
|
||||
# we first compile EEx without the .app file,
|
||||
# then Mix, and then compile EEx fully
|
||||
# then Mix and then compile EEx fully
|
||||
elixir: stdlib lib/eex/ebin/Elixir.EEx.beam mix ex_unit logger eex iex
|
||||
|
||||
stdlib: $(KERNEL) VERSION
|
||||
@@ -105,6 +104,7 @@ unicode: $(UNICODE)
|
||||
$(UNICODE): lib/elixir/unicode/*
|
||||
@ echo "==> unicode (compile)";
|
||||
$(Q) $(ELIXIRC) lib/elixir/unicode/unicode.ex -o lib/elixir/ebin;
|
||||
$(Q) $(ELIXIRC) lib/elixir/unicode/properties.ex -o lib/elixir/ebin;
|
||||
$(Q) $(ELIXIRC) lib/elixir/unicode/tokenizer.ex -o lib/elixir/ebin;
|
||||
|
||||
$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
|
||||
@@ -178,40 +178,48 @@ clean_residual_files:
|
||||
|
||||
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}")
|
||||
|
||||
DOCS_FORMAT = html
|
||||
COMPILE_DOCS = CANONICAL=$(CANONICAL) bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" --main "$(3)" --source-url "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) --output doc/$(2) --canonical "https://hexdocs.pm/$(2)/$(CANONICAL)" --homepage-url "https://elixir-lang.org/docs.html" --formatter "$(DOCS_FORMAT)" $(4)
|
||||
DOCS_COMPILE = CANONICAL=$(CANONICAL) bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" --main "$(3)" --source-url "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) --output doc/$(2) --canonical "https://hexdocs.pm/$(2)/$(CANONICAL)" --homepage-url "https://elixir-lang.org/docs.html" --formatter "$(DOCS_FORMAT)" $(4)
|
||||
DOCS_CONFIG = bin/elixir lib/elixir/scripts/docs_config.exs "$(1)"
|
||||
|
||||
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 DOCS_COMPILE,Elixir,elixir,Kernel,--config "lib/elixir/scripts/elixir_docs.exs")
|
||||
$(call DOCS_CONFIG,elixir)
|
||||
|
||||
docs_eex: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (eex)"
|
||||
$(Q) rm -rf doc/eex
|
||||
$(call COMPILE_DOCS,EEx,eex,EEx,--config "lib/mix/docs.exs")
|
||||
$(call DOCS_COMPILE,EEx,eex,EEx,--config "lib/elixir/scripts/mix_docs.exs")
|
||||
$(call DOCS_CONFIG,eex)
|
||||
|
||||
docs_mix: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (mix)"
|
||||
$(Q) rm -rf doc/mix
|
||||
$(call COMPILE_DOCS,Mix,mix,Mix,--config "lib/mix/docs.exs")
|
||||
$(call DOCS_COMPILE,Mix,mix,Mix,--config "lib/elixir/scripts/mix_docs.exs")
|
||||
$(call DOCS_CONFIG,mix)
|
||||
|
||||
docs_iex: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (iex)"
|
||||
$(Q) rm -rf doc/iex
|
||||
$(call COMPILE_DOCS,IEx,iex,IEx,--config "lib/mix/docs.exs")
|
||||
$(call DOCS_COMPILE,IEx,iex,IEx,--config "lib/elixir/scripts/mix_docs.exs")
|
||||
$(call DOCS_CONFIG,iex)
|
||||
|
||||
docs_ex_unit: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (ex_unit)"
|
||||
$(Q) rm -rf doc/ex_unit
|
||||
$(call COMPILE_DOCS,ExUnit,ex_unit,ExUnit,--config "lib/mix/docs.exs")
|
||||
$(call DOCS_COMPILE,ExUnit,ex_unit,ExUnit,--config "lib/elixir/scripts/mix_docs.exs")
|
||||
$(call DOCS_CONFIG,ex_unit)
|
||||
|
||||
docs_logger: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (logger)"
|
||||
$(Q) rm -rf doc/logger
|
||||
$(call COMPILE_DOCS,Logger,logger,Logger,--config "lib/mix/docs.exs")
|
||||
$(call DOCS_COMPILE,Logger,logger,Logger,--config "lib/elixir/scripts/mix_docs.exs")
|
||||
$(call DOCS_CONFIG,logger)
|
||||
|
||||
../ex_doc/bin/ex_doc:
|
||||
@ echo "ex_doc is not found in ../ex_doc as expected. See README for more information."
|
||||
@@ -220,23 +228,23 @@ docs_logger: compile ../ex_doc/bin/ex_doc
|
||||
#==> Zip tasks
|
||||
|
||||
Docs.zip: docs
|
||||
rm -f Docs-v$(VERSION).zip
|
||||
zip -9 -r Docs-v$(VERSION).zip CHANGELOG.md doc NOTICE LICENSE README.md
|
||||
@ echo "Docs file created $(CURDIR)/Docs-v$(VERSION).zip"
|
||||
rm -f Docs.zip
|
||||
zip -9 -r Docs.zip CHANGELOG.md doc NOTICE LICENSE README.md
|
||||
@ echo "Docs file created $(CURDIR)/Docs.zip"
|
||||
|
||||
Precompiled.zip: build_man compile
|
||||
rm -f Precompiled-v$(VERSION).zip
|
||||
zip -9 -r Precompiled-v$(VERSION).zip bin CHANGELOG.md lib/*/ebin lib/*/lib LICENSE man NOTICE README.md VERSION
|
||||
@ echo "Precompiled file created $(CURDIR)/Precompiled-v$(VERSION).zip"
|
||||
rm -f Precompiled.zip
|
||||
zip -9 -r Precompiled.zip bin CHANGELOG.md lib/*/ebin lib/*/lib LICENSE man NOTICE README.md VERSION
|
||||
@ echo "Precompiled file created $(CURDIR)/Precompiled.zip"
|
||||
|
||||
zips: Precompiled.zip Docs.zip
|
||||
@ echo ""
|
||||
@ 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:"
|
||||
@ shasum -a 1 < Docs-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA1:"
|
||||
@ shasum -a 512 < Docs-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA512:"
|
||||
@ shasum -a 1 < Precompiled.zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA1:"
|
||||
@ shasum -a 512 < Precompiled.zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA512:"
|
||||
@ shasum -a 1 < Docs.zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA1:"
|
||||
@ shasum -a 512 < Docs.zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA512:"
|
||||
@ echo ""
|
||||
|
||||
#==> Test tasks
|
||||
@@ -277,7 +285,7 @@ $(TEST_EBIN)/%.beam: $(TEST_ERL)/%.erl
|
||||
$(Q) mkdir -p $(TEST_EBIN)
|
||||
$(Q) $(ERLC) -o $(TEST_EBIN) $<
|
||||
|
||||
test_elixir: test_stdlib test_ex_unit test_logger test_eex test_iex test_mix
|
||||
test_elixir: test_stdlib test_ex_unit test_logger test_mix test_eex test_iex
|
||||
|
||||
test_stdlib: compile
|
||||
@ echo "==> elixir (ex_unit)"
|
||||
|
||||
@@ -22,7 +22,6 @@ limitations under the License.
|
||||
== All other files
|
||||
|
||||
Copyright 2012 Plataformatec
|
||||
Copyright 2021 The Elixir Team
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<img src="https://github.com/elixir-lang/elixir-lang.github.com/raw/master/images/logo/logo.png" width="200" alt="Elixir">
|
||||
# 
|
||||
|
||||
[](https://github.com/elixir-lang/elixir/actions?query=branch%3Amaster+workflow%3ACI) [](https://cirrus-ci.com/github/elixir-lang/elixir)
|
||||
|
||||
@@ -11,14 +11,11 @@ For more about Elixir, installation and documentation,
|
||||
## Policies
|
||||
|
||||
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.
|
||||
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 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].
|
||||
All interactions in our official communication channels follow our [Code of Conduct][1].
|
||||
|
||||
## Bug reports
|
||||
|
||||
@@ -26,56 +23,13 @@ For reporting bugs, [visit our issue tracker][2] and follow the steps
|
||||
for reporting a new issue. **Please disclose security vulnerabilities
|
||||
privately at elixir-security@googlegroups.com**.
|
||||
|
||||
## Issues tracker management
|
||||
|
||||
All currently open bugs related to the Elixir repository are listed
|
||||
in the issues tracker. The Elixir team uses the issues tracker to focus
|
||||
on *actionable items*, including planned enhancements in the short- and
|
||||
medium-term. We also do our best to label entries for clarity and to ease
|
||||
collaboration.
|
||||
|
||||
Our *actionable item policy* has some important consequences, such as:
|
||||
|
||||
* Proposing new features as well as request for support, help, and
|
||||
guidance must be done in their own spaces, detailed next.
|
||||
|
||||
* Issues where we have identified to be outside of Elixir scope,
|
||||
such as a bug upstream, will be closed (and requested to be moved
|
||||
elsewhere if appropriate).
|
||||
|
||||
* We actively close unrelated and non-actionable issues to keep the
|
||||
issues tracker tidy. However, we may get things wrong from time to
|
||||
time, so we are glad to revisit issues and reopen if necessary.
|
||||
|
||||
Keep the tone positive and be kind! For more information, see the
|
||||
[Code of Conduct][1].
|
||||
|
||||
### Proposing new features
|
||||
|
||||
For proposing new features, please start a discussion in the
|
||||
[Elixir Core mailing list][3]. Keep in mind that it is your responsibility
|
||||
to argue and explain why a feature is useful and how it will impact the
|
||||
codebase and the community.
|
||||
|
||||
Once a proposal is accepted, it will be added to [the issue tracker][2].
|
||||
Features and bug fixes that have already been merged and will be included
|
||||
in the next release are then "closed" and added to the [changelog][7].
|
||||
|
||||
### Discussions, support, and help
|
||||
|
||||
For general discussions, support, and help, please use many of the community
|
||||
spaces [listed on the sidebar of the Elixir website](https://elixir-lang.org/),
|
||||
such as forums, chat platforms, etc, where the wider community will be available
|
||||
to help you.
|
||||
|
||||
## Compiling from source
|
||||
|
||||
For the many different ways to install Elixir,
|
||||
[see our installation instructions on the website](https://elixir-lang.org/install.html).
|
||||
However, if you want to contribute to Elixir, you will need to compile from source.
|
||||
To compile from source, you can follow the steps below.
|
||||
|
||||
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang).
|
||||
After that, clone this repository to your machine, compile and test it:
|
||||
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang). Then clone this repository to your machine, compile and test it:
|
||||
|
||||
```sh
|
||||
git clone https://github.com/elixir-lang/elixir.git
|
||||
@@ -87,13 +41,37 @@ make clean test
|
||||
[this article includes important notes for compiling Elixir from source
|
||||
on Windows](https://github.com/elixir-lang/elixir/wiki/Windows).
|
||||
|
||||
In case you want to use this Elixir version as your system version,
|
||||
you need to add the `bin` directory to [your PATH environment variable](https://elixir-lang.org/install.html#setting-path-environment-variable).
|
||||
|
||||
If Elixir fails to build (specifically when pulling in a new version via
|
||||
`git`), be sure to remove any previous build artifacts by running
|
||||
`make clean`, then `make test`.
|
||||
|
||||
If tests pass, you can use 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:
|
||||
|
||||
Erlang/OTP 21 [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.
|
||||
|
||||
## Proposing new features
|
||||
|
||||
For proposing new features, please start a discussion in the
|
||||
[Elixir Core mailing list][3]. Keep in mind that it is your responsibility
|
||||
to argue and explain why a feature is useful and how it will impact the
|
||||
codebase and the community.
|
||||
|
||||
Once a proposal is accepted, it will be added to [the issue tracker][2].
|
||||
The issue 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].
|
||||
|
||||
## Contributing
|
||||
|
||||
We welcome everyone to contribute to Elixir. To do so, there are a few
|
||||
@@ -145,7 +123,7 @@ make clean_elixir compile
|
||||
|
||||
Similarly, if you can't get Elixir to compile or the tests to pass after
|
||||
updating an existing checkout, run `make clean compile`. You can check
|
||||
[the official build status](https://github.com/elixir-lang/elixir/actions/workflows/ci.yml).
|
||||
[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).
|
||||
|
||||
With tests running and passing, you are ready to contribute to Elixir and
|
||||
@@ -153,9 +131,9 @@ With tests running and passing, you are ready to contribute to Elixir and
|
||||
We have saved some excellent pull requests we have received in the past in
|
||||
case you are looking for some examples:
|
||||
|
||||
* [Implement Enum.member? - Pull request](https://github.com/elixir-lang/elixir/pull/992)
|
||||
* [Add String.valid? - Pull request](https://github.com/elixir-lang/elixir/pull/1058)
|
||||
* [Implement capture_io for ExUnit - Pull request](https://github.com/elixir-lang/elixir/pull/1059)
|
||||
* [Implement Enum.member? - Pull Request](https://github.com/elixir-lang/elixir/pull/992)
|
||||
* [Add String.valid? - Pull Request](https://github.com/elixir-lang/elixir/pull/1058)
|
||||
* [Implement capture_io for ExUnit - Pull Request](https://github.com/elixir-lang/elixir/pull/1059)
|
||||
|
||||
### Reviewing changes
|
||||
|
||||
@@ -212,13 +190,13 @@ and `mix` under the `doc` directory. If you are planning to contribute documenta
|
||||
* [Issue tracker][2]
|
||||
* [Changelog][7]
|
||||
* [Security Policy][9]
|
||||
* **[#elixir][4]** on [Libera.Chat][5] IRC
|
||||
* **[#elixir-lang][4]** on [Freenode][5] IRC
|
||||
|
||||
[1]: CODE_OF_CONDUCT.md
|
||||
[2]: https://github.com/elixir-lang/elixir/issues
|
||||
[3]: https://groups.google.com/group/elixir-lang-core
|
||||
[4]: https://web.libera.chat/#elixir
|
||||
[5]: https://libera.chat
|
||||
[4]: https://webchat.freenode.net/?channels=#elixir-lang
|
||||
[5]: https://www.freenode.net
|
||||
[6]: https://elixir-lang.org/docs.html
|
||||
[7]: CHANGELOG.md
|
||||
[8]: https://groups.google.com/group/elixir-lang-ann
|
||||
@@ -227,7 +205,7 @@ and `mix` under the `doc` directory. If you are planning to contribute documenta
|
||||
|
||||
## License
|
||||
|
||||
"Elixir" and the Elixir logo are registered trademarks of The Elixir Team.
|
||||
"Elixir" and the Elixir logo are copyright (c) 2012 Plataformatec.
|
||||
|
||||
Elixir source code is released under Apache License 2.0.
|
||||
|
||||
|
||||
+3
-3
@@ -4,7 +4,7 @@
|
||||
|
||||
1. Ensure you are running on the oldest supported Erlang version
|
||||
|
||||
2. Update version in /VERSION, bin/elixir and bin/elixir.bat
|
||||
2. Update version in /VERSION
|
||||
|
||||
3. Ensure /CHANGELOG.md is updated, versioned and add the current date
|
||||
|
||||
@@ -32,11 +32,11 @@
|
||||
|
||||
2. Update tables in /SECURITY.md and "Compatibility and Deprecations"
|
||||
|
||||
3. Commit "Branch out vMAJOR.MINOR"
|
||||
3. Commit "Prepare vMAJOR.MINOR for release"
|
||||
|
||||
### Back in master
|
||||
|
||||
1. Bump /VERSION file, bin/elixir and bin/elixir.bat
|
||||
1. Bump /VERSION file
|
||||
|
||||
2. Start new /CHANGELOG.md
|
||||
|
||||
|
||||
+7
-7
@@ -4,13 +4,13 @@
|
||||
|
||||
Elixir applies bug fixes only to the latest minor branch. Security patches are available for the last 5 minor branches:
|
||||
|
||||
Elixir version | Support
|
||||
:------------- | :-----------------------------
|
||||
1.13 | Bug fixes and security patches
|
||||
1.12 | Security patches only
|
||||
1.11 | Security patches only
|
||||
1.10 | Security patches only
|
||||
1.9 | Security patches only
|
||||
| Elixir version | Support
|
||||
| -------------- | ------------------------------
|
||||
| 1.11 | Bug fixes and security patches
|
||||
| 1.10 | Security patches only
|
||||
| 1.9 | Security patches only
|
||||
| 1.8 | Security patches only
|
||||
| 1.7 | Security patches only
|
||||
|
||||
## Announcements
|
||||
|
||||
|
||||
+18
-31
@@ -1,22 +1,20 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
ELIXIR_VERSION=1.13.2
|
||||
|
||||
if [ $# -eq 0 ] || { [ $# -eq 1 ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; }; then
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
cat <<USAGE >&2
|
||||
Usage: $(basename "$0") [options] [.exs file] [data]
|
||||
|
||||
## General options
|
||||
|
||||
-e "COMMAND" Evaluates the given command (*)
|
||||
-h, --help Prints this message (standalone)
|
||||
-h, --help Prints this message and exits
|
||||
-r "FILE" Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in \$PATH
|
||||
-S SCRIPT Finds and executes the given script in \$PATH
|
||||
-pr "FILE" Requires the given files/patterns in parallel (*)
|
||||
-pa "PATH" Prepends the given path to Erlang code path (*)
|
||||
-pz "PATH" Appends the given path to Erlang code path (*)
|
||||
-v, --version Prints Erlang/OTP and Elixir versions (standalone)
|
||||
-v, --version Prints Elixir version and exits
|
||||
|
||||
--app APP Starts the given app and its dependencies (*)
|
||||
--erl "SWITCHES" Switches to be passed down to Erlang (*)
|
||||
@@ -24,7 +22,6 @@ Usage: $(basename "$0") [options] [.exs file] [data]
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--short-version Prints Elixir version (standalone)
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
|
||||
Options given after the .exs file or -- are passed down to the executed code.
|
||||
@@ -55,7 +52,6 @@ It will attempt to create PIPEDIR and LOGDIR if they don't exist.
|
||||
See run_erl to learn more. To reattach, run: to_erl PIPEDIR.
|
||||
|
||||
** Options marked with (*) can be given more than once.
|
||||
** Standalone options can't be combined with other options.
|
||||
USAGE
|
||||
exit 1
|
||||
fi
|
||||
@@ -70,16 +66,11 @@ readlink_f () {
|
||||
fi
|
||||
}
|
||||
|
||||
if [ $# -eq 1 ] && [ "$1" = "--short-version" ]; then
|
||||
echo "$ELIXIR_VERSION"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Stores static Erlang arguments and --erl (which is passed as is)
|
||||
ERL=""
|
||||
|
||||
# Stores erl arguments preserving spaces/quotes (mimics an array)
|
||||
erl_set () {
|
||||
erl () {
|
||||
eval "E${E}=\$1"
|
||||
E=$((E + 1))
|
||||
}
|
||||
@@ -146,34 +137,34 @@ while [ $I -le $LENGTH ]; do
|
||||
;;
|
||||
--cookie)
|
||||
S=2
|
||||
erl_set "-setcookie"
|
||||
erl_set "$2"
|
||||
erl "-setcookie"
|
||||
erl "$2"
|
||||
;;
|
||||
--sname|--name)
|
||||
S=2
|
||||
erl_set "$(echo "$1" | cut -c 2-)"
|
||||
erl_set "$2"
|
||||
erl "$(echo "$1" | cut -c 2-)"
|
||||
erl "$2"
|
||||
;;
|
||||
--erl-config)
|
||||
S=2
|
||||
erl_set "-config"
|
||||
erl_set "$2"
|
||||
erl "-config"
|
||||
erl "$2"
|
||||
;;
|
||||
--vm-args)
|
||||
S=2
|
||||
erl_set "-args_file"
|
||||
erl_set "$2"
|
||||
erl "-args_file"
|
||||
erl "$2"
|
||||
;;
|
||||
--boot)
|
||||
S=2
|
||||
erl_set "-boot"
|
||||
erl_set "$2"
|
||||
erl "-boot"
|
||||
erl "$2"
|
||||
;;
|
||||
--boot-var)
|
||||
S=3
|
||||
erl_set "-boot_var"
|
||||
erl_set "$2"
|
||||
erl_set "$3"
|
||||
erl "-boot_var"
|
||||
erl "$2"
|
||||
erl "$3"
|
||||
;;
|
||||
--pipe-to)
|
||||
S=3
|
||||
@@ -219,11 +210,7 @@ if [ "$OS" != "Windows_NT" ] && [ -z "$NO_COLOR" ]; then
|
||||
if test -t 1 -a -t 2; then ERL="-elixir ansi_enabled true $ERL"; fi
|
||||
fi
|
||||
|
||||
# One MAY change ERTS_BIN= but you MUST NOT change
|
||||
# ERTS_BIN=$ERTS_BIN as it is handled by Elixir releases.
|
||||
ERTS_BIN=
|
||||
ERTS_BIN="$ERTS_BIN"
|
||||
|
||||
set -- "$ERTS_BIN$ERL_EXEC" -pa "$SCRIPT_PATH"/../lib/*/ebin $ELIXIR_ERL_OPTIONS $ERL "$@"
|
||||
|
||||
if [ -n "$RUN_ERL_PIPE" ]; then
|
||||
|
||||
+8
-23
@@ -1,14 +1,10 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
|
||||
set ELIXIR_VERSION=1.13.2
|
||||
|
||||
setlocal enabledelayedexpansion
|
||||
if ""%1""=="""" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""--help"" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""-h"" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""/h"" if ""%2""=="""" goto documentation
|
||||
if ""%1""==""/?"" if ""%2""=="""" goto documentation
|
||||
if /I ""%1""==""--short-version"" if ""%2""=="""" goto shortversion
|
||||
if ""%1""=="""" goto documentation
|
||||
if /I ""%1""==""--help"" goto documentation
|
||||
if /I ""%1""==""-h"" goto documentation
|
||||
if /I ""%1""==""/h"" goto documentation
|
||||
if ""%1""==""/?"" goto documentation
|
||||
goto parseopts
|
||||
|
||||
:documentation
|
||||
@@ -17,13 +13,13 @@ echo.
|
||||
echo ## General options
|
||||
echo.
|
||||
echo -e "COMMAND" Evaluates the given command (*)
|
||||
echo -h, --help Prints this message (standalone)
|
||||
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 Erlang/OTP and Elixir versions (standalone)
|
||||
echo -v, --version Prints Elixir version and exits
|
||||
echo.
|
||||
echo --app APP Starts the given app and its dependencies (*)
|
||||
echo --erl "SWITCHES" Switches to be passed down to Erlang (*)
|
||||
@@ -31,7 +27,6 @@ echo --eval "COMMAND" Evaluates the given command, same as -e (*)
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --short-version Prints Elixir version (standalone)
|
||||
echo --werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
echo.
|
||||
echo Options given after the .exs file or -- are passed down to the executed code.
|
||||
@@ -59,11 +54,6 @@ echo.
|
||||
echo --pipe-to is not supported on Windows. If set, Elixir won't boot.
|
||||
echo.
|
||||
echo ** Options marked with (*) can be given more than once.
|
||||
echo ** Standalone options can't be combined with other options.
|
||||
goto end
|
||||
|
||||
:shortversion
|
||||
echo !ELIXIR_VERSION!
|
||||
goto end
|
||||
|
||||
:parseopts
|
||||
@@ -88,7 +78,6 @@ set SCRIPT_PATH=%~dp0
|
||||
|
||||
rem Designates the path to the ERTS system
|
||||
set ERTS_BIN=
|
||||
set ERTS_BIN=!ERTS_BIN!
|
||||
|
||||
rem Recursive loop called for each parameter that parses the cmd line parameters
|
||||
:startloop
|
||||
@@ -110,21 +99,18 @@ if !par!=="+elixirc" (set parsElixir=!parsElixir! +elixirc && set runMode="elixi
|
||||
rem ******* EVAL PARAMETERS ************************
|
||||
if ""==!par:-e=! (
|
||||
set "VAR=%~1"
|
||||
if not defined VAR (set VAR= )
|
||||
set parsElixir=!parsElixir! -e "!VAR:"=\"!"
|
||||
shift
|
||||
goto startloop
|
||||
)
|
||||
if ""==!par:--eval=! (
|
||||
set "VAR=%~1"
|
||||
if not defined VAR (set VAR= )
|
||||
set parsElixir=!parsElixir! --eval "!VAR:"=\"!"
|
||||
shift
|
||||
goto startloop
|
||||
)
|
||||
if ""==!par:--rpc-eval=! (
|
||||
set "VAR=%~2"
|
||||
if not defined VAR (set VAR= )
|
||||
set parsElixir=!parsElixir! --rpc-eval %1 "!VAR:"=\"!"
|
||||
shift
|
||||
shift
|
||||
@@ -136,7 +122,6 @@ if ""==!par:-pr=! (set "parsElixir=!parsElixir! -pr %1" && shift && goto
|
||||
if ""==!par:-pa=! (set "parsElixir=!parsElixir! -pa %1" && shift && goto startloop)
|
||||
if ""==!par:-pz=! (set "parsElixir=!parsElixir! -pz %1" && shift && goto startloop)
|
||||
if ""==!par:-v=! (set "parsElixir=!parsElixir! -v" && goto startloop)
|
||||
if ""==!par:--version=! (set "parsElixir=!parsElixir! --version" && goto startloop)
|
||||
if ""==!par:--app=! (set "parsElixir=!parsElixir! --app %1" && shift && goto startloop)
|
||||
if ""==!par:--no-halt=! (set "parsElixir=!parsElixir! --no-halt" && goto startloop)
|
||||
if ""==!par:--remsh=! (set "parsElixir=!parsElixir! --remsh %1" && shift && goto startloop)
|
||||
@@ -180,4 +165,4 @@ if defined useWerl (
|
||||
"!ERTS_BIN!erl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
|
||||
)
|
||||
:end
|
||||
endlocal
|
||||
endlocal
|
||||
+2
-3
@@ -7,14 +7,13 @@ Usage: $(basename "$0") [elixir switches] [compiler switches] [.ex files]
|
||||
|
||||
-h, --help Prints this message and exits
|
||||
-o The directory to output compiled files
|
||||
-v, --version Prints Elixir version and exits (standalone)
|
||||
-v, --version Prints Elixir version and exits
|
||||
|
||||
--ignore-module-conflict Does not emit warnings if a module was previously defined
|
||||
--no-debug-info Does not attach debug info to compiled modules
|
||||
--no-docs Does not attach documentation to compiled modules
|
||||
--profile time Profile the time to compile modules
|
||||
--verbose Prints compilation status
|
||||
--warnings-as-errors Treats warnings as errors and return non-zero exit status
|
||||
--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.
|
||||
|
||||
+3
-4
@@ -14,16 +14,15 @@ goto run
|
||||
:documentation
|
||||
echo Usage: %~nx0 [elixir switches] [compiler switches] [.ex files]
|
||||
echo.
|
||||
echo -h, --help Prints this message and exits
|
||||
echo -o The directory to output compiled files
|
||||
echo -v, --version Prints Elixir version and exits (standalone)
|
||||
echo.
|
||||
echo --help, -h Prints this message and exits
|
||||
echo --ignore-module-conflict Does not emit warnings if a module was previously defined
|
||||
echo --no-debug-info Does not attach debug info to compiled modules
|
||||
echo --no-docs Does not attach documentation to compiled modules
|
||||
echo --profile time Profile the time to compile modules
|
||||
echo --verbose Prints compilation status
|
||||
echo --warnings-as-errors Treats warnings as errors and returns non-zero exit status
|
||||
echo --version, -v Prints Elixir version and exits
|
||||
echo --warnings-as-errors Treats warnings as errors and returns non-zero exit code
|
||||
echo.
|
||||
echo ** Options given after -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
|
||||
+25
-76
@@ -17,11 +17,11 @@ defmodule EEx do
|
||||
|
||||
## API
|
||||
|
||||
This module provides three main APIs for you to use:
|
||||
This module provides 3 main APIs for you to use:
|
||||
|
||||
1. Evaluate a string (`eval_string/3`) or a file (`eval_file/3`)
|
||||
directly. This is the simplest API to use but also the
|
||||
slowest, since the code is evaluated at runtime and not precompiled.
|
||||
slowest, since the code is evaluated and not compiled before.
|
||||
|
||||
2. Define a function from a string (`function_from_string/5`)
|
||||
or a file (`function_from_file/5`). This allows you to embed
|
||||
@@ -40,18 +40,13 @@ defmodule EEx do
|
||||
They are:
|
||||
|
||||
* `:file` - the file to be used in the template. Defaults to the given
|
||||
file the template is read from or to `"nofile"` when compiling from a string.
|
||||
* `:line` - the line to be used as the template start. Defaults to `1`.
|
||||
file the template is read from or to "nofile" when compiling from a string.
|
||||
* `:line` - the line to be used as the template start. Defaults to 1.
|
||||
* `:indentation` - (since v1.11.0) an integer added to the column after every
|
||||
new line. Defaults to `0`.
|
||||
new line. Defaults to 0.
|
||||
* `:engine` - the EEx engine to be used for compilation.
|
||||
* `:trim` - if `true`, trims whitespace left and right of quotation as
|
||||
long as at least one newline is present. All subsequent newlines and
|
||||
spaces are removed but one newline is retained. Defaults to `false`.
|
||||
* `:parser_options` - (since: 1.13.0) allow customizing the parsed code that is generated.
|
||||
See `Code.string_to_quoted/2` for available options. Note that the options
|
||||
`:file`, `:line` and `:column` are ignored if passed in.
|
||||
Defaults to `Code.get_compiler_option(:parser_options)` (which defaults to `[]` if not set).
|
||||
* `:trim` - if true, trims whitespace left/right of quotation tags up until
|
||||
newlines. At least one newline is retained. Defaults to false.
|
||||
|
||||
## Engine
|
||||
|
||||
@@ -110,13 +105,10 @@ defmodule EEx do
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Generates a function definition from the given string.
|
||||
Generates a function definition from the string.
|
||||
|
||||
The first argument is the kind of the generated function (`:def` or `:defp`).
|
||||
The `name` argument is the name that the generated function will have.
|
||||
`template` is the string containing the EEx template. `args` is a list of arguments
|
||||
that the generated function will accept. They will be available inside the EEx
|
||||
template. `options` is a list of EEx compilation options (see the module documentation).
|
||||
The kind (`:def` or `:defp`) must be given, the
|
||||
function name, its arguments and the compilation options.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -128,11 +120,11 @@ defmodule EEx do
|
||||
"3"
|
||||
|
||||
"""
|
||||
defmacro function_from_string(kind, name, template, args \\ [], options \\ []) do
|
||||
defmacro function_from_string(kind, name, source, args \\ [], options \\ []) do
|
||||
quote bind_quoted: binding() do
|
||||
info = Keyword.merge([file: __ENV__.file, line: __ENV__.line], options)
|
||||
args = Enum.map(args, fn arg -> {arg, [line: info[:line]], nil} end)
|
||||
compiled = EEx.compile_string(template, info)
|
||||
compiled = EEx.compile_string(source, info)
|
||||
|
||||
case kind do
|
||||
:def -> def unquote(name)(unquote_splicing(args)), do: unquote(compiled)
|
||||
@@ -144,11 +136,8 @@ defmodule EEx do
|
||||
@doc """
|
||||
Generates a function definition from the file contents.
|
||||
|
||||
The first argument is the kind of the generated function (`:def` or `:defp`).
|
||||
The `name` argument is the name that the generated function will have.
|
||||
`file` is the path to the EEx template file. `args` is a list of arguments
|
||||
that the generated function will accept. They will be available inside the EEx
|
||||
template. `options` is a list of EEx compilation options (see the module documentation).
|
||||
The kind (`:def` or `:defp`) must be given, the
|
||||
function name, its arguments and the compilation options.
|
||||
|
||||
This function is useful in case you have templates but
|
||||
you want to precompile inside a module for speed.
|
||||
@@ -171,7 +160,7 @@ defmodule EEx do
|
||||
"""
|
||||
defmacro function_from_file(kind, name, file, args \\ [], options \\ []) do
|
||||
quote bind_quoted: binding() do
|
||||
info = Keyword.merge([file: IO.chardata_to_string(file), line: 1], options)
|
||||
info = Keyword.merge(options, file: file, line: 1)
|
||||
args = Enum.map(args, fn arg -> {arg, [line: 1], nil} end)
|
||||
compiled = EEx.compile_file(file, info)
|
||||
|
||||
@@ -185,25 +174,8 @@ defmodule EEx do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a string `source` and generates a quoted expression
|
||||
Gets a string `source` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
|
||||
This is useful if you want to compile a EEx template into code and inject
|
||||
that code somewhere or evaluate it at runtime.
|
||||
|
||||
The generated quoted code will use variables defined in the template that
|
||||
will be taken from the context where the code is evaluated. If you
|
||||
have a template such as `<%= a + b %>`, then the returned quoted code
|
||||
will use the `a` and `b` variables in the context where it's evaluated. See
|
||||
examples below.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> quoted = EEx.compile_string("<%= a + b %>")
|
||||
iex> {result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
|
||||
iex> result
|
||||
"3"
|
||||
|
||||
"""
|
||||
@spec compile_string(String.t(), keyword) :: Macro.t()
|
||||
def compile_string(source, options \\ []) when is_binary(source) and is_list(options) do
|
||||
@@ -211,34 +183,12 @@ defmodule EEx do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a `filename` and generates a quoted expression
|
||||
Gets a `filename` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
|
||||
This is useful if you want to compile a EEx template into code and inject
|
||||
that code somewhere or evaluate it at runtime.
|
||||
|
||||
The generated quoted code will use variables defined in the template that
|
||||
will be taken from the context where the code is evaluated. If you
|
||||
have a template such as `<%= a + b %>`, then the returned quoted code
|
||||
will use the `a` and `b` variables in the context where it's evaluated. See
|
||||
examples below.
|
||||
|
||||
## Examples
|
||||
|
||||
# sample.eex
|
||||
<%= a + b %>
|
||||
|
||||
# In code:
|
||||
quoted = EEx.compile_file("sample.eex")
|
||||
{result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
|
||||
result
|
||||
#=> "3"
|
||||
|
||||
"""
|
||||
@spec compile_file(Path.t(), keyword) :: Macro.t()
|
||||
def compile_file(filename, options \\ []) when is_list(options) do
|
||||
filename = IO.chardata_to_string(filename)
|
||||
options = Keyword.merge([file: filename, line: 1], options)
|
||||
@spec compile_file(String.t(), keyword) :: Macro.t()
|
||||
def compile_file(filename, options \\ []) when is_binary(filename) and is_list(options) do
|
||||
options = Keyword.merge(options, file: filename, line: 1)
|
||||
compile_string(File.read!(filename), options)
|
||||
end
|
||||
|
||||
@@ -251,7 +201,7 @@ defmodule EEx do
|
||||
"foo baz"
|
||||
|
||||
"""
|
||||
@spec eval_string(String.t(), keyword, keyword) :: String.t()
|
||||
@spec eval_string(String.t(), keyword, keyword) :: any
|
||||
def eval_string(source, bindings \\ [], options \\ [])
|
||||
when is_binary(source) and is_list(bindings) and is_list(options) do
|
||||
compiled = compile_string(source, options)
|
||||
@@ -266,16 +216,15 @@ defmodule EEx do
|
||||
# sample.eex
|
||||
foo <%= bar %>
|
||||
|
||||
# IEx
|
||||
# iex
|
||||
EEx.eval_file("sample.eex", bar: "baz")
|
||||
#=> "foo baz"
|
||||
|
||||
"""
|
||||
@spec eval_file(Path.t(), keyword, keyword) :: String.t()
|
||||
@spec eval_file(String.t(), keyword, keyword) :: any
|
||||
def eval_file(filename, bindings \\ [], options \\ [])
|
||||
when is_list(bindings) and is_list(options) do
|
||||
filename = IO.chardata_to_string(filename)
|
||||
options = Keyword.put_new(options, :file, filename)
|
||||
when is_binary(filename) and is_list(bindings) and is_list(options) do
|
||||
options = Keyword.put(options, :file, filename)
|
||||
compiled = compile_file(filename, options)
|
||||
do_eval(compiled, bindings, options)
|
||||
end
|
||||
|
||||
+16
-29
@@ -16,7 +16,6 @@ defmodule EEx.Compiler do
|
||||
column = 1
|
||||
indentation = opts[:indentation] || 0
|
||||
trim = opts[:trim] || false
|
||||
parser_options = opts[:parser_options] || Code.get_compiler_option(:parser_options)
|
||||
tokenizer_options = %{trim: trim, indentation: indentation}
|
||||
|
||||
case EEx.Tokenizer.tokenize(source, line, column, tokenizer_options) do
|
||||
@@ -28,7 +27,7 @@ defmodule EEx.Compiler do
|
||||
quoted: [],
|
||||
start_line: nil,
|
||||
start_column: nil,
|
||||
parser_options: parser_options
|
||||
parser_options: Code.get_compiler_option(:parser_options)
|
||||
}
|
||||
|
||||
init = state.engine.init(opts)
|
||||
@@ -42,16 +41,8 @@ defmodule EEx.Compiler do
|
||||
# Generates the buffers by handling each expression from the tokenizer.
|
||||
# It returns Macro.t/0 or it raises.
|
||||
|
||||
defp generate_buffer([{:text, line, column, chars} | rest], buffer, scope, state) do
|
||||
buffer =
|
||||
if function_exported?(state.engine, :handle_text, 3) do
|
||||
meta = [line: line, column: column]
|
||||
state.engine.handle_text(buffer, meta, IO.chardata_to_string(chars))
|
||||
else
|
||||
# TODO: Remove this branch on Elixir v2.0
|
||||
state.engine.handle_text(buffer, IO.chardata_to_string(chars))
|
||||
end
|
||||
|
||||
defp generate_buffer([{:text, _line, _column, chars} | rest], buffer, scope, state) do
|
||||
buffer = state.engine.handle_text(buffer, IO.chardata_to_string(chars))
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
@@ -68,15 +59,7 @@ defmodule EEx.Compiler do
|
||||
scope,
|
||||
state
|
||||
) do
|
||||
if mark != '=' do
|
||||
message =
|
||||
"the contents of this expression won't be output unless the EEx block starts with \"<%=\""
|
||||
|
||||
:elixir_errors.erl_warn({start_line, start_column}, state.file, message)
|
||||
end
|
||||
|
||||
{rest, line, contents} =
|
||||
look_ahead_middle(rest, start_line, chars) || {rest, start_line, chars}
|
||||
{contents, line, rest} = look_ahead_middle(rest, start_line, chars)
|
||||
|
||||
{contents, rest} =
|
||||
generate_buffer(
|
||||
@@ -117,7 +100,7 @@ defmodule EEx.Compiler do
|
||||
"unexpected beginning of EEx tag \"<%#{modifier}\" on \"<%#{modifier}#{chars}%>\", " <>
|
||||
"please remove \"#{modifier}\" accordingly"
|
||||
|
||||
:elixir_errors.erl_warn({line, column}, state.file, message)
|
||||
:elixir_errors.erl_warn(line, state.file, message)
|
||||
generate_buffer([{:middle_expr, line, column, '', 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
|
||||
@@ -155,7 +138,7 @@ defmodule EEx.Compiler do
|
||||
"unexpected beginning of EEx tag \"<%#{modifier}\" on end of " <>
|
||||
"expression \"<%#{modifier}#{chars}%>\", please remove \"#{modifier}\" accordingly"
|
||||
|
||||
:elixir_errors.erl_warn({line, column}, state.file, message)
|
||||
:elixir_errors.erl_warn(line, state.file, message)
|
||||
generate_buffer([{:end_expr, line, column, '', chars} | t], buffer, scope, state)
|
||||
# TODO: Make this an error on Elixir v2.0 since it accidentally worked previously.
|
||||
# raise EEx.SyntaxError, message: message, file: state.file, line: line, column: column
|
||||
@@ -195,20 +178,24 @@ defmodule EEx.Compiler do
|
||||
|
||||
# Look middle expressions that immediately follow a start_expr
|
||||
|
||||
defp look_ahead_middle([{:text, _, _, text} | rest], start, contents) do
|
||||
defp look_ahead_middle(
|
||||
[{:text, _, _, text}, {:middle_expr, line, _, _, chars} | rest] = tokens,
|
||||
start,
|
||||
contents
|
||||
) do
|
||||
if only_spaces?(text) do
|
||||
look_ahead_middle(rest, start, contents ++ text)
|
||||
{contents ++ text ++ chars, line, rest}
|
||||
else
|
||||
nil
|
||||
{contents, start, tokens}
|
||||
end
|
||||
end
|
||||
|
||||
defp look_ahead_middle([{:middle_expr, line, _column, _, chars} | rest], _start, contents) do
|
||||
{rest, line, contents ++ chars}
|
||||
{contents ++ chars, line, rest}
|
||||
end
|
||||
|
||||
defp look_ahead_middle(_tokens, _start, _contents) do
|
||||
nil
|
||||
defp look_ahead_middle(tokens, start, contents) do
|
||||
{contents, start, tokens}
|
||||
end
|
||||
|
||||
defp only_spaces?(chars) do
|
||||
|
||||
+13
-15
@@ -4,8 +4,10 @@ defmodule EEx.Engine do
|
||||
|
||||
An engine needs to implement all callbacks below.
|
||||
|
||||
This module also ships with a default engine implementation
|
||||
you can delegate to. See `EEx.SmartEngine` as an example.
|
||||
An engine may also `use EEx.Engine` to get the default behaviour
|
||||
but this is not advised. In such cases, if any of the callbacks
|
||||
are overridden, they must call `super()` to delegate to the
|
||||
underlying `EEx.Engine`.
|
||||
"""
|
||||
|
||||
@type state :: term
|
||||
@@ -29,8 +31,7 @@ defmodule EEx.Engine do
|
||||
|
||||
It must return the updated state.
|
||||
"""
|
||||
@callback handle_text(state, [line: pos_integer, column: pos_integer], text :: String.t()) ::
|
||||
state
|
||||
@callback handle_text(state, text :: String.t()) :: state
|
||||
|
||||
@doc """
|
||||
Called for the dynamic/code parts of a template.
|
||||
@@ -68,7 +69,6 @@ defmodule EEx.Engine do
|
||||
@callback handle_end(state) :: Macro.t()
|
||||
|
||||
@doc false
|
||||
@deprecated "Use explicit delegation to EEx.Engine instead"
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
@behaviour EEx.Engine
|
||||
@@ -90,7 +90,7 @@ defmodule EEx.Engine do
|
||||
end
|
||||
|
||||
def handle_text(state, text) do
|
||||
EEx.Engine.handle_text(state, [], text)
|
||||
EEx.Engine.handle_text(state, text)
|
||||
end
|
||||
|
||||
def handle_expr(state, marker, expr) do
|
||||
@@ -147,7 +147,7 @@ defmodule EEx.Engine do
|
||||
end
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:init/1`."
|
||||
@doc false
|
||||
def init(_opts) do
|
||||
%{
|
||||
binary: [],
|
||||
@@ -156,18 +156,18 @@ defmodule EEx.Engine do
|
||||
}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_begin/1`."
|
||||
@doc false
|
||||
def handle_begin(state) do
|
||||
check_state!(state)
|
||||
%{state | binary: [], dynamic: []}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_end/1`."
|
||||
@doc false
|
||||
def handle_end(quoted) do
|
||||
handle_body(quoted)
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_body/1`."
|
||||
@doc false
|
||||
def handle_body(state) do
|
||||
check_state!(state)
|
||||
%{binary: binary, dynamic: dynamic} = state
|
||||
@@ -176,16 +176,14 @@ defmodule EEx.Engine do
|
||||
{:__block__, [], Enum.reverse(dynamic)}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_text/3`."
|
||||
def handle_text(state, _meta, text) do
|
||||
check_state!(state)
|
||||
@doc false
|
||||
def handle_text(state, text) do
|
||||
%{binary: binary} = state
|
||||
%{state | binary: [text | binary]}
|
||||
end
|
||||
|
||||
@doc "Default implementation for `c:handle_expr/3`."
|
||||
@doc false
|
||||
def handle_expr(state, "=", ast) do
|
||||
check_state!(state)
|
||||
%{binary: binary, dynamic: dynamic, vars_count: vars_count} = state
|
||||
var = Macro.var(:"arg#{vars_count}", __MODULE__)
|
||||
|
||||
|
||||
@@ -33,26 +33,10 @@ defmodule EEx.SmartEngine do
|
||||
|
||||
"""
|
||||
|
||||
@behaviour EEx.Engine
|
||||
use EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate init(opts), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_body(state), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_begin(state), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_end(state), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
defdelegate handle_text(state, meta, text), to: EEx.Engine
|
||||
|
||||
@impl true
|
||||
def handle_expr(state, marker, expr) do
|
||||
def handle_expr(buffer, mark, expr) do
|
||||
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
|
||||
EEx.Engine.handle_expr(state, marker, expr)
|
||||
super(buffer, mark, expr)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -71,15 +71,8 @@ defmodule EEx.Tokenizer do
|
||||
{:ok, expr, new_line, new_column, rest} ->
|
||||
{key, expr} =
|
||||
case :elixir_tokenizer.tokenize(expr, 1, file: "eex", check_terminators: false) do
|
||||
{:ok, _line, _column, warnings, tokens} ->
|
||||
Enum.each(Enum.reverse(warnings), fn {location, file, msg} ->
|
||||
:elixir_errors.erl_warn(location, file, msg)
|
||||
end)
|
||||
|
||||
token_key(tokens, expr)
|
||||
|
||||
{:error, _, _, _, _} ->
|
||||
{:expr, expr}
|
||||
{:ok, tokens} -> token_key(tokens, expr)
|
||||
{:error, _, _, _} -> {:expr, expr}
|
||||
end
|
||||
|
||||
{rest, new_line, new_column, buffer} =
|
||||
@@ -134,7 +127,7 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
# Receives tokens and check if it is a start, middle or an end token.
|
||||
defp token_key(tokens, expr) do
|
||||
case {tokens, tokens |> Enum.reverse() |> drop_eol()} do
|
||||
case {tokens, Enum.reverse(tokens)} do
|
||||
{[{:end, _} | _], [{:do, _} | _]} ->
|
||||
{:middle_expr, expr}
|
||||
|
||||
@@ -165,9 +158,6 @@ defmodule EEx.Tokenizer do
|
||||
end
|
||||
end
|
||||
|
||||
defp drop_eol([{:eol, _} | rest]), do: drop_eol(rest)
|
||||
defp drop_eol(rest), do: rest
|
||||
|
||||
defp maybe_append_space([?\s]), do: [?\s]
|
||||
defp maybe_append_space([h]), do: [h, ?\s]
|
||||
defp maybe_append_space([h | t]), do: [h | maybe_append_space(t)]
|
||||
|
||||
@@ -43,15 +43,6 @@ defmodule EEx.SmartEngineTest do
|
||||
assert_received :found
|
||||
end
|
||||
|
||||
test "error with unused \"do\" block without \"<%=\" modifier" do
|
||||
stderr =
|
||||
ExUnit.CaptureIO.capture_io(:stderr, fn ->
|
||||
assert_eval("", "<% if true do %>I'm invisible!<% end %>", assigns: %{})
|
||||
end)
|
||||
|
||||
assert stderr =~ "the contents of this expression won't be output"
|
||||
end
|
||||
|
||||
defp assert_eval(expected, actual, binding \\ []) do
|
||||
result = EEx.eval_string(actual, binding, file: __ENV__.file, engine: EEx.SmartEngine)
|
||||
assert result == expected
|
||||
|
||||
@@ -64,7 +64,7 @@ defmodule EEx.TokenizerTest do
|
||||
{:ok, [{:text, 1, 1, 'foo <% true %>'}, {:eof, 1, 16}]}
|
||||
end
|
||||
|
||||
test "quotation with do-end" do
|
||||
test "quotation with do/end" do
|
||||
assert T.tokenize('foo <%% true do %>bar<%% end %>', 1, 1, @opts) ==
|
||||
{:ok, [{:text, 1, 1, 'foo <% true do %>bar<% end %>'}, {:eof, 1, 32}]}
|
||||
end
|
||||
@@ -91,7 +91,7 @@ defmodule EEx.TokenizerTest do
|
||||
assert T.tokenize('<%%% a <%%= b %> c %>', 1, 1, @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "EEx comments" do
|
||||
test "eex comments" do
|
||||
exprs = [
|
||||
{:text, 1, 1, 'foo '},
|
||||
{:eof, 1, 16}
|
||||
@@ -100,7 +100,7 @@ defmodule EEx.TokenizerTest do
|
||||
assert T.tokenize('foo <%# true %>', 1, 1, @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "EEx comments with do-end" do
|
||||
test "eex comments with do/end" do
|
||||
exprs = [
|
||||
{:text, 1, 1, 'foo '},
|
||||
{:text, 1, 19, 'bar'},
|
||||
@@ -110,29 +110,7 @@ defmodule EEx.TokenizerTest do
|
||||
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1, 1, @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "EEx comments inside do-end" do
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, '', ' if true do '},
|
||||
{:text, 1, 31, 'bar'},
|
||||
{:end_expr, 1, 34, [], ' end '},
|
||||
{:eof, 1, 43}
|
||||
]
|
||||
|
||||
assert T.tokenize('<% if true do %><%# comment %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
|
||||
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, [], ' case true do '},
|
||||
{:middle_expr, 1, 33, '', ' true -> '},
|
||||
{:text, 1, 46, 'bar'},
|
||||
{:end_expr, 1, 49, [], ' end '},
|
||||
{:eof, 1, 58}
|
||||
]
|
||||
|
||||
assert T.tokenize('<% case true do %><%# comment %><% true -> %>bar<% end %>', 1, 1, @opts) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "Elixir comments" do
|
||||
test "elixir comments" do
|
||||
exprs = [
|
||||
{:text, 1, 1, 'foo '},
|
||||
{:expr, 1, 5, [], ' true # this is a boolean '},
|
||||
@@ -142,7 +120,7 @@ defmodule EEx.TokenizerTest do
|
||||
assert T.tokenize('foo <% true # this is a boolean %>', 1, 1, @opts) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "Elixir comments with do-end" do
|
||||
test "elixir comments with do/end" do
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, [], ' if true do # startif '},
|
||||
{:text, 1, 27, 'text'},
|
||||
@@ -182,19 +160,7 @@ defmodule EEx.TokenizerTest do
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with fn-end with newline" do
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, '=', ' a fn ->\n'},
|
||||
{:text, 2, 3, 'foo'},
|
||||
{:end_expr, 2, 6, [], ' end '},
|
||||
{:eof, 2, 15}
|
||||
]
|
||||
|
||||
assert T.tokenize('<%= a fn ->\n%>foo<% end %>', 1, 1, @opts) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with multiple fn-end" do
|
||||
test "strings with multiple callbacks" do
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, '=', ' a fn -> '},
|
||||
{:text, 1, 15, 'foo'},
|
||||
@@ -208,7 +174,7 @@ defmodule EEx.TokenizerTest do
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with fn-end followed by do block" do
|
||||
test "strings with callback followed by do block" do
|
||||
exprs = [
|
||||
{:start_expr, 1, 1, '=', ' a fn -> '},
|
||||
{:text, 1, 15, 'foo'},
|
||||
|
||||
+24
-58
@@ -87,11 +87,7 @@ defmodule EExTest do
|
||||
expected = "123\n456\n789"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
|
||||
string = "<%= 123 %> \n\n456\n\n <%= 789 %>"
|
||||
expected = "123\n456\n789"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
|
||||
string = "<%= 123 %> \n \n456\n \n <%= 789 %>"
|
||||
string = "<%= 123 %> \n <%= 456 %> \n <%= 789 %>"
|
||||
expected = "123\n456\n789"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
|
||||
@@ -198,15 +194,6 @@ defmodule EExTest do
|
||||
assert_eval("foo baz", "foo <%= if false do %>bar<% else %>baz<% end %>")
|
||||
end
|
||||
|
||||
test "embedded code with comments in do end" do
|
||||
assert_eval("foo bar", "foo <%= case true do %><%# comment %><% true -> %>bar<% end %>")
|
||||
|
||||
assert_eval(
|
||||
"foo\n\nbar\n",
|
||||
"foo\n<%= case true do %>\n<%# comment %>\n<% true -> %>\nbar\n<% end %>"
|
||||
)
|
||||
end
|
||||
|
||||
test "embedded code with nested do end" do
|
||||
assert_eval("foo bar", "foo <%= if true do %><%= if true do %>bar<% end %><% end %>")
|
||||
end
|
||||
@@ -224,11 +211,6 @@ defmodule EExTest do
|
||||
"<%= Enum.map([1, 2, 3], fn x -> %> <%= 100 + x %> <% end) %>"
|
||||
)
|
||||
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= Enum.map([1, 2, 3], fn x ->\n%> <%= 100 + x %> <% end) %>"
|
||||
)
|
||||
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= apply Enum, :map, [[1, 2, 3], fn x -> %> <%= 100 + x %> <% end] %>"
|
||||
@@ -274,7 +256,7 @@ defmodule EExTest do
|
||||
assert_raise EEx.SyntaxError,
|
||||
"nofile:1:18: unexpected middle of expression <% else %>",
|
||||
fn ->
|
||||
EEx.compile_string("<%= if true %>foo<% else %>bar<% end %>")
|
||||
EEx.compile_string("<% if true %> foo<% else %>bar<% end %>")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -285,16 +267,16 @@ defmodule EExTest do
|
||||
end
|
||||
|
||||
test "when start expression is found without an end expression" do
|
||||
msg = "nofile:2:18: unexpected end of string, expected a closing '<% end %>'"
|
||||
msg = "nofile:2:17: unexpected end of string, expected a closing '<% end %>'"
|
||||
|
||||
assert_raise EEx.SyntaxError, msg, fn ->
|
||||
EEx.compile_string("foo\n<%= if true do %>")
|
||||
EEx.compile_string("foo\n<% if true do %>")
|
||||
end
|
||||
end
|
||||
|
||||
test "when nested end expression is found without a start expression" do
|
||||
assert_raise EEx.SyntaxError, "nofile:1:31: unexpected end of expression <% end %>", fn ->
|
||||
EEx.compile_string("foo <%= if true do %><% end %><% end %>")
|
||||
assert_raise EEx.SyntaxError, "nofile:1:30: unexpected end of expression <% end %>", fn ->
|
||||
EEx.compile_string("foo <% if true do %><% end %><% end %>")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -542,7 +524,7 @@ defmodule EExTest do
|
||||
<% "a" in y -> %>
|
||||
Good
|
||||
<% true -> %>
|
||||
<%= if true do %>true<% else %>false<% end %>
|
||||
<% if true do %>true<% else %>false<% end %>
|
||||
Bad
|
||||
<% end %>
|
||||
"""
|
||||
@@ -587,6 +569,18 @@ defmodule EExTest do
|
||||
end
|
||||
|
||||
describe "buffers" do
|
||||
test "unused buffers are kept out" do
|
||||
string = """
|
||||
<%= 123 %>
|
||||
<% if true do %>
|
||||
<%= 456 %>
|
||||
<% end %>
|
||||
<%= 789 %>
|
||||
"""
|
||||
|
||||
assert_eval("123\n\n789\n", string)
|
||||
end
|
||||
|
||||
test "inside comprehensions" do
|
||||
string = """
|
||||
<%= for _name <- packages || [] do %>
|
||||
@@ -624,24 +618,6 @@ defmodule EExTest do
|
||||
assert EExTest.Compiled.__info__(:attributes)[:external_resource] ==
|
||||
[Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex")]
|
||||
end
|
||||
|
||||
test "supports t:Path.t() paths" do
|
||||
filename = to_charlist(Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex"))
|
||||
result = EEx.eval_file(filename, bar: 1)
|
||||
assert_normalized_newline_equal("foo 1\n", result)
|
||||
end
|
||||
|
||||
assert_raise EEx.SyntaxError, "my_file.eex:1:12: missing token '%>'", fn ->
|
||||
EEx.compile_string("foo <%= bar", file: "my_file.eex")
|
||||
end
|
||||
|
||||
test "supports overriding file and line through options" do
|
||||
filename = Path.join(__DIR__, "fixtures/eex_template_with_syntax_error.eex")
|
||||
|
||||
assert_raise EEx.SyntaxError, "my_file.eex:11:1: missing token '%>'", fn ->
|
||||
EEx.eval_file(filename, _bindings = [], file: "my_file.eex", line: 10)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "precompiled" do
|
||||
@@ -687,8 +663,8 @@ defmodule EExTest do
|
||||
buffer <> ":END"
|
||||
end
|
||||
|
||||
def handle_text(buffer, meta, text) do
|
||||
buffer <> ":TEXT-#{meta[:line]}-#{meta[:column]}(#{String.trim(text)})"
|
||||
def handle_text(buffer, text) do
|
||||
buffer <> ":TEXT(#{String.trim(text)})"
|
||||
end
|
||||
|
||||
def handle_expr(buffer, "/", expr) do
|
||||
@@ -706,16 +682,16 @@ defmodule EExTest do
|
||||
|
||||
describe "custom engines" do
|
||||
test "text" do
|
||||
assert_eval("BODY(INIT:TEXT-1-1(foo))", "foo", [], engine: TestEngine)
|
||||
assert_eval("BODY(INIT:TEXT(foo))", "foo", [], engine: TestEngine)
|
||||
end
|
||||
|
||||
test "custom marker" do
|
||||
assert_eval("BODY(INIT:TEXT-1-1(foo):DIV(:bar))", "foo <%/ :bar %>", [], engine: TestEngine)
|
||||
assert_eval("BODY(INIT:TEXT(foo):DIV(:bar))", "foo <%/ :bar %>", [], engine: TestEngine)
|
||||
end
|
||||
|
||||
test "begin/end" do
|
||||
assert_eval(
|
||||
~s[BODY(INIT:TEXT-1-1(foo):EQUAL(if do\n "BEGIN:TEXT-1-17(this):END"\nelse\n "BEGIN:TEXT-1-31(that):END"\nend))],
|
||||
~s[BODY(INIT:TEXT(foo):EQUAL(if do\n "BEGIN:TEXT(this):END"\nelse\n "BEGIN:TEXT(that):END"\nend))],
|
||||
"foo <%= if do %>this<% else %>that<% end %>",
|
||||
[],
|
||||
engine: TestEngine
|
||||
@@ -732,16 +708,6 @@ defmodule EExTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "parser options" do
|
||||
test "customizes parsed code" do
|
||||
atoms_encoder = fn "not_jose", _ -> {:ok, :jose} end
|
||||
|
||||
assert_eval("valid", "<%= not_jose %>", [jose: "valid"],
|
||||
parser_options: [static_atoms_encoder: atoms_encoder]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp assert_eval(expected, actual, binding \\ [], opts \\ []) do
|
||||
opts = Keyword.merge([file: __ENV__.file, engine: opts[:engine] || EEx.Engine], opts)
|
||||
result = EEx.eval_string(actual, binding, opts)
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
foo <%= bar
|
||||
+38
-36
@@ -7,7 +7,7 @@ defmodule Access do
|
||||
|
||||
`Access` supports keyword lists (`Keyword`) and maps (`Map`) out
|
||||
of the box. Keywords supports only atoms keys, keys for maps can
|
||||
be of any type. Both return `nil` if the key does not exist:
|
||||
be of any type. Both returns `nil` if the key does not exist:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:a]
|
||||
@@ -47,11 +47,12 @@ defmodule Access do
|
||||
> `map[key]`, if your map is made of predefined atom keys,
|
||||
> you should prefer to access those atom keys with `map.key`
|
||||
> instead of `map[key]`, as `map.key` will raise if the key
|
||||
> is missing (which is not supposed to happen if the keys are
|
||||
> predefined). Similarly, since structs are maps and structs
|
||||
> have predefined keys, they only allow the `struct.key`
|
||||
> syntax and they do not allow the `struct[key]` access syntax.
|
||||
> See the `Map` module for more information.
|
||||
> 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.
|
||||
|
||||
## Nested data structures
|
||||
|
||||
@@ -99,15 +100,16 @@ defmodule Access do
|
||||
@type key :: any
|
||||
@type value :: any
|
||||
|
||||
@type get_fun(data) ::
|
||||
(:get, data, (term -> term) -> new_data :: container)
|
||||
@type get_fun(data, get_value) ::
|
||||
(:get, data, (term -> term) ->
|
||||
{get_value, new_data :: container})
|
||||
|
||||
@type get_and_update_fun(data, current_value) ::
|
||||
@type get_and_update_fun(data, get_value) ::
|
||||
(:get_and_update, data, (term -> term) ->
|
||||
{current_value, new_data :: container} | :pop)
|
||||
{get_value, new_data :: container} | :pop)
|
||||
|
||||
@type access_fun(data, current_value) ::
|
||||
get_fun(data) | get_and_update_fun(data, current_value)
|
||||
@type access_fun(data, get_value) ::
|
||||
get_fun(data, get_value) | get_and_update_fun(data, get_value)
|
||||
|
||||
@doc """
|
||||
Invoked in order to access the value stored under `key` in the given term `term`.
|
||||
@@ -133,16 +135,16 @@ defmodule Access do
|
||||
|
||||
The implementation of this callback should invoke `fun` with the value under
|
||||
`key` in the passed structure `data`, or with `nil` if `key` is not present in it.
|
||||
This function must return either `{current_value, new_value}` or `:pop`.
|
||||
This function must return either `{get_value, update_value}` or `:pop`.
|
||||
|
||||
If the passed function returns `{current_value, new_value}`,
|
||||
the return value of this callback should be `{current_value, new_data}`, where:
|
||||
If the passed function returns `{get_value, update_value}`,
|
||||
the return value of this callback should be `{get_value, new_data}`, where:
|
||||
|
||||
* `current_value` is the retrieved value (which can be operated on before being returned)
|
||||
* `get_value` is the retrieved value (which can be operated on before being returned)
|
||||
|
||||
* `new_value` is the new value to be stored under `key`
|
||||
* `update_value` is the new value to be stored under `key`
|
||||
|
||||
* `new_data` is `data` after updating the value of `key` with `new_value`.
|
||||
* `new_data` is `data` after updating the value of `key` with `update_value`.
|
||||
|
||||
If the passed function returns `:pop`, the return value of this callback
|
||||
must be `{value, new_data}` where `value` is the value under `key`
|
||||
@@ -151,9 +153,8 @@ defmodule Access do
|
||||
See the implementations of `Map.get_and_update/3` or `Keyword.get_and_update/3`
|
||||
for more examples.
|
||||
"""
|
||||
@callback get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, new_data :: data}
|
||||
when current_value: value, data: container | any_container
|
||||
@callback get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
|
||||
when get_value: var, data: container | any_container
|
||||
|
||||
@doc """
|
||||
Invoked to "pop" the value under `key` out of the given data structure.
|
||||
@@ -320,9 +321,9 @@ defmodule Access do
|
||||
a struct that implements the `Access` behaviour).
|
||||
|
||||
The `fun` argument receives the value of `key` (or `nil` if `key` is not
|
||||
present in `container`) and must return a two-element tuple `{current_value, new_value}`:
|
||||
the "get" value `current_value` (the retrieved value, which can be operated on before
|
||||
being returned) and the new value to be stored under `key` (`new_value`).
|
||||
present in `container`) and must return a two-element tuple `{get_value, update_value}`:
|
||||
the "get" value `get_value` (the retrieved value, which can be operated on before
|
||||
being returned) and the new value to be stored under `key` (`update_value`).
|
||||
`fun` may also return `:pop`, which means the current value
|
||||
should be removed from the container and returned.
|
||||
|
||||
@@ -337,9 +338,8 @@ defmodule Access do
|
||||
{1, [a: 2]}
|
||||
|
||||
"""
|
||||
@spec get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, new_data :: data}
|
||||
when current_value: var, data: container
|
||||
@spec get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
|
||||
when get_value: var, data: container
|
||||
def get_and_update(container, key, fun)
|
||||
|
||||
def get_and_update(%module{} = container, key, fun) do
|
||||
@@ -422,7 +422,7 @@ defmodule Access do
|
||||
The returned function uses the default value if the key does not exist.
|
||||
This can be used to specify defaults and safely traverse missing keys:
|
||||
|
||||
iex> get_in(%{}, [Access.key(:user, %{}), Access.key(:name, "meg")])
|
||||
iex> get_in(%{}, [Access.key(:user, %{name: "meg"}), Access.key(:name)])
|
||||
"meg"
|
||||
|
||||
Such is also useful when using update functions, allowing us to introduce
|
||||
@@ -445,11 +445,14 @@ defmodule Access do
|
||||
|
||||
An error is raised if the accessed structure is not a map or a struct:
|
||||
|
||||
iex> get_in(nil, [Access.key(:foo)])
|
||||
** (BadMapError) expected a map, got: nil
|
||||
|
||||
iex> get_in([], [Access.key(:foo)])
|
||||
** (BadMapError) expected a map, got: []
|
||||
|
||||
"""
|
||||
@spec key(key, term) :: access_fun(data :: struct | map, current_value :: term)
|
||||
@spec key(key, term) :: access_fun(data :: struct | map, get_value :: term)
|
||||
def key(key, default \\ nil) do
|
||||
fn
|
||||
:get, data, next ->
|
||||
@@ -493,7 +496,7 @@ defmodule Access do
|
||||
** (RuntimeError) Access.key!/1 expected a map/struct, got: []
|
||||
|
||||
"""
|
||||
@spec key!(key) :: access_fun(data :: struct | map, current_value :: term)
|
||||
@spec key!(key) :: access_fun(data :: struct | map, get_value :: term)
|
||||
def key!(key) do
|
||||
fn
|
||||
:get, %{} = data, next ->
|
||||
@@ -541,7 +544,7 @@ defmodule Access do
|
||||
** (RuntimeError) Access.elem/1 expected a tuple, got: %{}
|
||||
|
||||
"""
|
||||
@spec elem(non_neg_integer) :: access_fun(data :: tuple, current_value :: term)
|
||||
@spec elem(non_neg_integer) :: access_fun(data :: tuple, get_value :: term)
|
||||
def elem(index) when is_integer(index) and index >= 0 do
|
||||
pos = index + 1
|
||||
|
||||
@@ -595,7 +598,7 @@ defmodule Access do
|
||||
** (RuntimeError) Access.all/0 expected a list, got: %{}
|
||||
|
||||
"""
|
||||
@spec all() :: access_fun(data :: list, current_value :: list)
|
||||
@spec all() :: access_fun(data :: list, get_value :: list)
|
||||
def all() do
|
||||
&all/3
|
||||
end
|
||||
@@ -670,7 +673,7 @@ defmodule Access do
|
||||
** (RuntimeError) Access.at/1 expected a list, got: %{}
|
||||
|
||||
"""
|
||||
@spec at(integer) :: access_fun(data :: list, current_value :: term)
|
||||
@spec at(integer) :: access_fun(data :: list, get_value :: term)
|
||||
def at(index) when is_integer(index) do
|
||||
fn op, data, next -> at(op, data, index, next) end
|
||||
end
|
||||
@@ -724,8 +727,7 @@ defmodule Access do
|
||||
** (Enum.OutOfBoundsError) out of bounds error
|
||||
|
||||
"""
|
||||
@doc since: "1.11.0"
|
||||
@spec at!(integer) :: access_fun(data :: list, current_value :: term)
|
||||
@spec at!(integer) :: access_fun(data :: list, get_value :: term)
|
||||
def at!(index) when is_integer(index) do
|
||||
fn op, data, next -> at!(op, data, index, next) end
|
||||
end
|
||||
@@ -792,7 +794,7 @@ defmodule Access do
|
||||
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec filter((term -> boolean)) :: access_fun(data :: list, current_value :: list)
|
||||
@spec filter((term -> boolean)) :: access_fun(data :: list, get_value :: list)
|
||||
def filter(func) when is_function(func) do
|
||||
fn op, data, next -> filter(op, data, func, next) end
|
||||
end
|
||||
|
||||
@@ -241,7 +241,7 @@ defmodule Agent do
|
||||
and the start function will return `{:error, :timeout}`.
|
||||
|
||||
If the `:debug` option is present, the corresponding function in the
|
||||
[`:sys` module](`:sys`) will be invoked.
|
||||
[`:sys` module](http://www.erlang.org/doc/man/sys.html) will be invoked.
|
||||
|
||||
If the `:spawn_opt` option is present, its value will be passed as options
|
||||
to the underlying process as in `Process.spawn/4`.
|
||||
|
||||
@@ -42,16 +42,12 @@ defmodule Application do
|
||||
end
|
||||
|
||||
In Mix projects, the environment of the application and its dependencies can
|
||||
be overridden via the `config/config.exs` and `config/runtime.exs` files. The
|
||||
former is loaded at build-time, before your code compiles, and the latter at
|
||||
runtime, just before your app starts. For example, someone using your application
|
||||
can override its `:db_host` environment variable as follows:
|
||||
be overridden via the `config/config.exs` file. For example, someone using
|
||||
your application can override its `:db_host` environment variable as follows:
|
||||
|
||||
import Config
|
||||
config :my_app, :db_host, "db.local"
|
||||
|
||||
See the "Configuration" section in the `Mix` module for more information.
|
||||
|
||||
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
|
||||
@@ -100,7 +96,6 @@ defmodule Application do
|
||||
first choice. However, if you really have to read the application environment
|
||||
during compilation, we recommend you to use `compile_env/3` instead:
|
||||
|
||||
require Application
|
||||
@db_host Application.compile_env(:my_app, :db_host, "db.local")
|
||||
|
||||
By using `compile_env/3`, tools like Mix will store the values used during
|
||||
@@ -168,7 +163,7 @@ defmodule Application do
|
||||
In the sections above, we have configured an application in the
|
||||
`application/0` section of the `mix.exs` file. Ultimately, Mix will use
|
||||
this configuration to create an [*application resource
|
||||
file*](https://erlang.org/doc/man/application.html), which is a file called
|
||||
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`.
|
||||
|
||||
@@ -272,10 +267,11 @@ defmodule Application do
|
||||
## Further information
|
||||
|
||||
For further details on applications please check the documentation of the
|
||||
[`:application` Erlang module](`:application`), and the
|
||||
[Applications](https://erlang.org/doc/design_principles/applications.html)
|
||||
[`application`](http://www.erlang.org/doc/man/application.html) Erlang module,
|
||||
and the
|
||||
[Applications](http://www.erlang.org/doc/design_principles/applications.html)
|
||||
section of the [OTP Design Principles User's
|
||||
Guide](https://erlang.org/doc/design_principles/users_guide.html).
|
||||
Guide](http://erlang.org/doc/design_principles/users_guide.html).
|
||||
"""
|
||||
|
||||
@doc """
|
||||
@@ -390,7 +386,6 @@ defmodule Application do
|
||||
:maxT,
|
||||
:registered,
|
||||
:included_applications,
|
||||
:optional_applications,
|
||||
:applications,
|
||||
:mod,
|
||||
:start_phases
|
||||
@@ -504,31 +499,19 @@ defmodule Application do
|
||||
Giving a path is useful to let Elixir know that only certain paths
|
||||
in a large configuration are compile time dependent.
|
||||
"""
|
||||
# TODO: Warn on v1.14 if get_env/fetch_env/fetch_env! is used at
|
||||
# compile time instead of compile_env
|
||||
# TODO: Warn on v1.14 if get_env/fetch_env/fetch_env! is used at compile time instead of compile_env
|
||||
@doc since: "1.10.0"
|
||||
@spec compile_env(app, key | list, value) :: value
|
||||
defmacro compile_env(app, key_or_path, default \\ nil) do
|
||||
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
|
||||
|
||||
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
|
||||
|
||||
quote do
|
||||
Application.__compile_env__(unquote(app), unquote(key_or_path), unquote(default), __ENV__)
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_key_or_path({:__aliases__, _, _} = alias, env),
|
||||
do: Macro.expand(alias, %{env | function: {:__info__, 1}})
|
||||
|
||||
defp expand_key_or_path(list, env) when is_list(list),
|
||||
do: Enum.map(list, &expand_key_or_path(&1, env))
|
||||
|
||||
defp expand_key_or_path(other, _env),
|
||||
do: other
|
||||
|
||||
@doc false
|
||||
def __compile_env__(app, key_or_path, default, env) do
|
||||
case fetch_compile_env(app, key_or_path, env) do
|
||||
@@ -545,13 +528,11 @@ defmodule Application do
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec compile_env!(app, key | list) :: value
|
||||
defmacro compile_env!(app, key_or_path) do
|
||||
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
|
||||
|
||||
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
|
||||
|
||||
quote do
|
||||
Application.__compile_env__!(unquote(app), unquote(key_or_path), __ENV__)
|
||||
end
|
||||
@@ -630,18 +611,10 @@ defmodule Application do
|
||||
|
||||
Our database engine used by `:my_app` needs to know what databases exist, and
|
||||
what the database configurations are. The database engine can make a call to
|
||||
`Application.get_env(:my_app, :my_app_databases, [])` to retrieve the list of
|
||||
databases (specified by module names).
|
||||
|
||||
The engine can then traverse each repository in the list and call
|
||||
`Application.get_env(:my_app, Databases.RepoOne)` and so forth to retrieve the
|
||||
configuration of each one. In this case, each configuration will be a keyword
|
||||
list, so you can use the functions in the `Keyword` module or even the `Access`
|
||||
module to traverse it, for example:
|
||||
|
||||
config = Application.get_env(:my_app, Databases.RepoOne)
|
||||
config[:ip]
|
||||
|
||||
`get_env(:my_app, :my_app_databases)` to retrieve the list of databases (specified
|
||||
by module names). Our database engine can then traverse each repository in the
|
||||
list and then call `get_env(:my_app, Databases.RepoOne)` and so forth to retrieve
|
||||
the configuration of each one.
|
||||
"""
|
||||
@spec get_env(app, key, value) :: value
|
||||
def get_env(app, key, default \\ nil) when is_atom(app) do
|
||||
@@ -721,6 +694,9 @@ 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.
|
||||
|
||||
@@ -729,14 +705,28 @@ defmodule Application do
|
||||
* have the same application listed more than once
|
||||
* have the same key inside the same application listed more than once
|
||||
|
||||
If those conditions are not met, it will raise.
|
||||
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
|
||||
:application.set_env(config, opts)
|
||||
# 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 """
|
||||
@@ -753,7 +743,6 @@ defmodule Application do
|
||||
defp maybe_warn_on_app_env_key(_app, key) when is_atom(key),
|
||||
do: :ok
|
||||
|
||||
# TODO: Remove this deprecation warning on 2.0+ and allow list lookups as in compile_env.
|
||||
defp maybe_warn_on_app_env_key(app, key) do
|
||||
message = "passing non-atom as application env key is deprecated, got: #{inspect(key)}"
|
||||
IO.warn_once({Application, :key, app, key}, message, _stacktrace_drop_levels = 2)
|
||||
@@ -898,7 +887,7 @@ defmodule Application do
|
||||
#=> "bar-123"
|
||||
|
||||
For more information on code paths, check the `Code` module in
|
||||
Elixir and also Erlang's [`:code` module](`:code`).
|
||||
Elixir and also Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html).
|
||||
"""
|
||||
@spec app_dir(app) :: String.t()
|
||||
def app_dir(app) when is_atom(app) do
|
||||
|
||||
+15
-18
@@ -92,9 +92,6 @@ defmodule Base do
|
||||
|
||||
"""
|
||||
|
||||
@type encode_case :: :upper | :lower
|
||||
@type decode_case :: :upper | :lower | :mixed
|
||||
|
||||
b16_alphabet = '0123456789ABCDEF'
|
||||
b64_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
||||
b64url_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
|
||||
@@ -270,7 +267,7 @@ defmodule Base do
|
||||
"666f6f626172"
|
||||
|
||||
"""
|
||||
@spec encode16(binary, case: encode_case) :: binary
|
||||
@spec encode16(binary, keyword) :: binary
|
||||
def encode16(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
do_encode16(case, data)
|
||||
@@ -303,7 +300,7 @@ defmodule Base do
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec decode16(binary, case: decode_case) :: {:ok, binary} | :error
|
||||
@spec decode16(binary, keyword) :: {:ok, binary} | :error
|
||||
def decode16(string, opts \\ []) do
|
||||
{:ok, decode16!(string, opts)}
|
||||
rescue
|
||||
@@ -340,7 +337,7 @@ defmodule Base do
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec decode16!(binary, case: encode_case) :: binary
|
||||
@spec decode16!(binary, keyword) :: binary
|
||||
def decode16!(string, opts \\ [])
|
||||
|
||||
def decode16!(string, opts) when is_binary(string) and rem(byte_size(string), 2) == 0 do
|
||||
@@ -370,7 +367,7 @@ defmodule Base do
|
||||
"Zm9vYg"
|
||||
|
||||
"""
|
||||
@spec encode64(binary, padding: boolean) :: binary
|
||||
@spec encode64(binary, keyword) :: binary
|
||||
def encode64(data, opts \\ []) when is_binary(data) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_encode64(data, pad?)
|
||||
@@ -400,7 +397,7 @@ defmodule Base do
|
||||
{:ok, "foob"}
|
||||
|
||||
"""
|
||||
@spec decode64(binary, ignore: :whitespace, padding: boolean) :: {:ok, binary} | :error
|
||||
@spec decode64(binary, keyword) :: {:ok, binary} | :error
|
||||
def decode64(string, opts \\ []) when is_binary(string) do
|
||||
{:ok, decode64!(string, opts)}
|
||||
rescue
|
||||
@@ -434,7 +431,7 @@ defmodule Base do
|
||||
"foob"
|
||||
|
||||
"""
|
||||
@spec decode64!(binary, ignore: :whitespace, padding: boolean) :: binary
|
||||
@spec decode64!(binary, keyword) :: binary
|
||||
def decode64!(string, opts \\ []) when is_binary(string) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
string |> remove_ignored(opts[:ignore]) |> do_decode64(pad?)
|
||||
@@ -456,7 +453,7 @@ defmodule Base do
|
||||
"_3_-_A"
|
||||
|
||||
"""
|
||||
@spec url_encode64(binary, padding: boolean) :: binary
|
||||
@spec url_encode64(binary, keyword) :: binary
|
||||
def url_encode64(data, opts \\ []) when is_binary(data) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_encode64url(data, pad?)
|
||||
@@ -484,7 +481,7 @@ defmodule Base do
|
||||
{:ok, <<255, 127, 254, 252>>}
|
||||
|
||||
"""
|
||||
@spec url_decode64(binary, ignore: :whitespace, padding: boolean) :: {:ok, binary} | :error
|
||||
@spec url_decode64(binary, keyword) :: {:ok, binary} | :error
|
||||
def url_decode64(string, opts \\ []) when is_binary(string) do
|
||||
{:ok, url_decode64!(string, opts)}
|
||||
rescue
|
||||
@@ -516,7 +513,7 @@ defmodule Base do
|
||||
<<255, 127, 254, 252>>
|
||||
|
||||
"""
|
||||
@spec url_decode64!(binary, ignore: :whitespace, padding: boolean) :: binary
|
||||
@spec url_decode64!(binary, keyword) :: binary
|
||||
def url_decode64!(string, opts \\ []) when is_binary(string) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
string |> remove_ignored(opts[:ignore]) |> do_decode64url(pad?)
|
||||
@@ -554,7 +551,7 @@ defmodule Base do
|
||||
"MZXW6YTBOI"
|
||||
|
||||
"""
|
||||
@spec encode32(binary, case: encode_case, padding: boolean) :: binary
|
||||
@spec encode32(binary, keyword) :: binary
|
||||
def encode32(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
@@ -597,7 +594,7 @@ defmodule Base do
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec decode32(binary, case: decode_case, padding: boolean) :: {:ok, binary} | :error
|
||||
@spec decode32(binary, keyword) :: {:ok, binary} | :error
|
||||
def decode32(string, opts \\ []) do
|
||||
{:ok, decode32!(string, opts)}
|
||||
rescue
|
||||
@@ -643,7 +640,7 @@ defmodule Base do
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec decode32!(binary, case: decode_case, padding: boolean) :: binary
|
||||
@spec decode32!(binary, keyword) :: binary
|
||||
def decode32!(string, opts \\ []) when is_binary(string) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
@@ -683,7 +680,7 @@ defmodule Base do
|
||||
"CPNMUOJ1E8"
|
||||
|
||||
"""
|
||||
@spec hex_encode32(binary, case: encode_case, padding: boolean) :: binary
|
||||
@spec hex_encode32(binary, keyword) :: binary
|
||||
def hex_encode32(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
@@ -727,7 +724,7 @@ defmodule Base do
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec hex_decode32(binary, case: decode_case, padding: boolean) :: {:ok, binary} | :error
|
||||
@spec hex_decode32(binary, keyword) :: {:ok, binary} | :error
|
||||
def hex_decode32(string, opts \\ []) do
|
||||
{:ok, hex_decode32!(string, opts)}
|
||||
rescue
|
||||
@@ -774,7 +771,7 @@ defmodule Base do
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec hex_decode32!(binary, case: decode_case, padding: boolean) :: binary
|
||||
@spec hex_decode32!(binary, keyword) :: binary
|
||||
def hex_decode32!(string, opts \\ []) when is_binary(string) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
|
||||
@@ -4,7 +4,7 @@ defmodule Behaviour do
|
||||
|
||||
This module is deprecated. Instead of `defcallback/1` and
|
||||
`defmacrocallback/1`, the `@callback` and `@macrocallback`
|
||||
module attributes can be used respectively. See the
|
||||
module attributes can be used (respectively). See the
|
||||
documentation for `Module` for more information on these
|
||||
attributes.
|
||||
|
||||
@@ -17,7 +17,6 @@ defmodule Behaviour do
|
||||
@doc """
|
||||
Defines a function callback according to the given type specification.
|
||||
"""
|
||||
@deprecated "Use the @callback module attribute instead"
|
||||
defmacro defcallback(spec) do
|
||||
do_defcallback(:def, split_spec(spec, quote(do: term)))
|
||||
end
|
||||
@@ -25,7 +24,6 @@ defmodule Behaviour do
|
||||
@doc """
|
||||
Defines a macro callback according to the given type specification.
|
||||
"""
|
||||
@deprecated "Use the @macrocallback module attribute instead"
|
||||
defmacro defmacrocallback(spec) do
|
||||
do_defcallback(:defmacro, split_spec(spec, quote(do: Macro.t())))
|
||||
end
|
||||
|
||||
@@ -191,8 +191,22 @@ defmodule Bitwise do
|
||||
:erlang.bxor(left, right)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def unquote(:^^^)(left, right) do
|
||||
@doc """
|
||||
Bitwise XOR 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)
|
||||
end
|
||||
|
||||
|
||||
@@ -189,7 +189,7 @@ defmodule Calendar do
|
||||
@doc """
|
||||
Calculates the year and era from the given `year`.
|
||||
"""
|
||||
@callback year_of_era(year, month, day) :: {year, era}
|
||||
@callback year_of_era(year) :: {year, era}
|
||||
|
||||
@doc """
|
||||
Calculates the day and era from the given `year`, `month`, and `day`.
|
||||
@@ -453,7 +453,7 @@ defmodule Calendar do
|
||||
b | Abbreviated month name | Jan
|
||||
B | Full month name | January
|
||||
c | Preferred date+time representation | 2018-10-17 12:34:56
|
||||
d | Day of the month | 01, 31
|
||||
d | Day of the month | 01, 12
|
||||
f | Microseconds *(does not support width and padding modifiers)* | 000000, 999999, 0123
|
||||
H | Hour using a 24-hour clock | 00, 23
|
||||
I | Hour using a 12-hour clock | 01, 12
|
||||
@@ -485,9 +485,6 @@ defmodule Calendar do
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%a, %B %d %Y")
|
||||
"Mon, August 26 2019"
|
||||
|
||||
iex> Calendar.strftime(~U[2020-04-02 13:52:06.0Z], "%B %-d, %Y")
|
||||
"April 2, 2020"
|
||||
|
||||
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%c")
|
||||
"2019-08-26 13:52:06"
|
||||
|
||||
|
||||
@@ -83,8 +83,8 @@ defmodule Date do
|
||||
366
|
||||
iex> Enum.member?(range, ~D[2001-02-01])
|
||||
true
|
||||
iex> Enum.take(range, 3)
|
||||
[~D[2001-01-01], ~D[2001-01-02], ~D[2001-01-03]]
|
||||
iex> Enum.reduce(range, 0, fn _date, acc -> acc - 1 end)
|
||||
-366
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@@ -92,61 +92,19 @@ defmodule Date do
|
||||
def range(%{calendar: calendar} = first, %{calendar: calendar} = last) do
|
||||
{first_days, _} = to_iso_days(first)
|
||||
{last_days, _} = to_iso_days(last)
|
||||
# TODO: Deprecate inferring a range with a step of -1 on Elixir v1.16
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
range(first, first_days, last, last_days, calendar, step)
|
||||
end
|
||||
|
||||
def range(%{calendar: _, year: _, month: _, day: _}, %{calendar: _, year: _, month: _, day: _}) do
|
||||
raise ArgumentError, "both dates must have matching calendars"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a range of dates with a step.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> range = Date.range(~D[2001-01-01], ~D[2002-01-01], 2)
|
||||
iex> range
|
||||
#DateRange<~D[2001-01-01], ~D[2002-01-01], 2>
|
||||
iex> Enum.count(range)
|
||||
183
|
||||
iex> Enum.member?(range, ~D[2001-01-03])
|
||||
true
|
||||
iex> Enum.take(range, 3)
|
||||
[~D[2001-01-01], ~D[2001-01-03], ~D[2001-01-05]]
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec range(Calendar.date(), Calendar.date(), step :: pos_integer | neg_integer) ::
|
||||
Date.Range.t()
|
||||
def range(%{calendar: calendar} = first, %{calendar: calendar} = last, step)
|
||||
when is_integer(step) and step != 0 do
|
||||
{first_days, _} = to_iso_days(first)
|
||||
{last_days, _} = to_iso_days(last)
|
||||
range(first, first_days, last, last_days, calendar, step)
|
||||
end
|
||||
|
||||
def range(
|
||||
%{calendar: _, year: _, month: _, day: _} = first,
|
||||
%{calendar: _, year: _, month: _, day: _} = last,
|
||||
step
|
||||
) do
|
||||
raise ArgumentError,
|
||||
"both dates must have matching calendar and the step must be a " <>
|
||||
"non-zero integer, got: #{inspect(first)}, #{inspect(last)}, #{step}"
|
||||
end
|
||||
|
||||
defp range(first, first_days, last, last_days, calendar, step) do
|
||||
%Date.Range{
|
||||
first: %Date{calendar: calendar, year: first.year, month: first.month, day: first.day},
|
||||
last: %Date{calendar: calendar, year: last.year, month: last.month, day: last.day},
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
step: step
|
||||
last_in_iso_days: last_days
|
||||
}
|
||||
end
|
||||
|
||||
def range(%{calendar: _, year: _, month: _, day: _}, %{calendar: _, year: _, month: _, day: _}) do
|
||||
raise ArgumentError, "both dates must have matching calendars"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current date in UTC.
|
||||
|
||||
@@ -315,7 +273,7 @@ defmodule Date do
|
||||
|
||||
@doc """
|
||||
Parses the extended "Dates" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
The year parsed by this function is limited to four digits.
|
||||
|
||||
@@ -340,7 +298,7 @@ defmodule Date do
|
||||
|
||||
@doc """
|
||||
Parses the extended "Dates" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Raises if the format is invalid.
|
||||
|
||||
@@ -365,7 +323,7 @@ defmodule Date do
|
||||
|
||||
@doc """
|
||||
Converts the given `date` to
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
By default, `Date.to_iso8601/2` returns dates formatted in the "extended"
|
||||
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
|
||||
@@ -542,8 +500,8 @@ defmodule Date do
|
||||
end
|
||||
end
|
||||
|
||||
def compare(%{calendar: calendar1} = date1, %{calendar: calendar2} = date2) do
|
||||
if Calendar.compatible_calendars?(calendar1, calendar2) do
|
||||
def compare(date1, date2) do
|
||||
if Calendar.compatible_calendars?(date1.calendar, date2.calendar) do
|
||||
case {to_iso_days(date1), to_iso_days(date2)} do
|
||||
{first, second} when first > second -> :gt
|
||||
{first, second} when first < second -> :lt
|
||||
@@ -701,12 +659,11 @@ defmodule Date do
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def to_iso_days(%{calendar: Calendar.ISO, year: year, month: month, day: day}) do
|
||||
defp to_iso_days(%{calendar: Calendar.ISO, year: year, month: month, day: day}) do
|
||||
{Calendar.ISO.date_to_iso_days(year, month, day), {0, 86_400_000_000}}
|
||||
end
|
||||
|
||||
def to_iso_days(%{calendar: calendar, year: year, month: month, day: day}) do
|
||||
defp to_iso_days(%{calendar: calendar, year: year, month: month, day: day}) do
|
||||
calendar.naive_datetime_to_iso_days(year, month, day, 0, 0, 0, {0, 0})
|
||||
end
|
||||
|
||||
@@ -943,14 +900,8 @@ defmodule Date do
|
||||
@spec year_of_era(Calendar.date()) :: {Calendar.year(), non_neg_integer()}
|
||||
def year_of_era(date)
|
||||
|
||||
def year_of_era(%{calendar: calendar, year: year, month: month, day: day}) do
|
||||
# TODO: Remove me on 1.17
|
||||
# The behaviour implementation already warns on missing callback.
|
||||
if function_exported?(calendar, :year_of_era, 3) do
|
||||
calendar.year_of_era(year, month, day)
|
||||
else
|
||||
calendar.year_of_era(year)
|
||||
end
|
||||
def year_of_era(%{calendar: calendar, year: year}) do
|
||||
calendar.year_of_era(year)
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
||||
@@ -2,13 +2,12 @@ defmodule Date.Range do
|
||||
@moduledoc """
|
||||
Returns an inclusive range between dates.
|
||||
|
||||
Ranges must be created with the `Date.range/2` or `Date.range/3` function.
|
||||
Ranges must be created with the `Date.range/2` function.
|
||||
|
||||
The following fields are public:
|
||||
|
||||
* `:first` - the initial date on the range
|
||||
* `:last` - the last date on the range
|
||||
* `:step` - (since v1.12.0) the step
|
||||
|
||||
The remaining fields are private and should not be accessed.
|
||||
"""
|
||||
@@ -17,133 +16,98 @@ defmodule Date.Range do
|
||||
first: Date.t(),
|
||||
last: Date.t(),
|
||||
first_in_iso_days: iso_days(),
|
||||
last_in_iso_days: iso_days(),
|
||||
step: pos_integer | neg_integer
|
||||
last_in_iso_days: iso_days()
|
||||
}
|
||||
|
||||
@typep iso_days() :: Calendar.iso_days()
|
||||
|
||||
@enforce_keys [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
|
||||
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
|
||||
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days]
|
||||
|
||||
defimpl Enumerable do
|
||||
def member?(
|
||||
%Date.Range{
|
||||
first: %{calendar: calendar},
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
step: step
|
||||
} = range,
|
||||
%Date{calendar: calendar} = date
|
||||
) do
|
||||
{days, _} = Date.to_iso_days(date)
|
||||
def member?(%{first: %{calendar: calendar}} = range, %Date{calendar: calendar} = date) do
|
||||
%{
|
||||
first: first,
|
||||
last: last,
|
||||
first_in_iso_days: first_in_iso_days,
|
||||
last_in_iso_days: last_in_iso_days
|
||||
} = range
|
||||
|
||||
cond do
|
||||
empty?(range) ->
|
||||
{:ok, false}
|
||||
%{year: first_year, month: first_month, day: first_day} = first
|
||||
%{year: last_year, month: last_month, day: last_day} = last
|
||||
%{year: year, month: month, day: day} = date
|
||||
first = {first_year, first_month, first_day}
|
||||
last = {last_year, last_month, last_day}
|
||||
date = {year, month, day}
|
||||
|
||||
first_days <= last_days ->
|
||||
{:ok, first_days <= days and days <= last_days and rem(days - first_days, step) == 0}
|
||||
|
||||
true ->
|
||||
{:ok, last_days <= days and days <= first_days and rem(days - first_days, step) == 0}
|
||||
if first_in_iso_days <= last_in_iso_days do
|
||||
{:ok, date >= first and date <= last}
|
||||
else
|
||||
{:ok, date >= last and date <= first}
|
||||
end
|
||||
end
|
||||
|
||||
def member?(%Date.Range{step: _}, _) do
|
||||
def member?(_, _) do
|
||||
{:ok, false}
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def member?(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range,
|
||||
date
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
member?(Map.put(date_range, :step, step), date)
|
||||
def count(%{first_in_iso_days: first, last_in_iso_days: last}) do
|
||||
{:ok, abs(first - last) + 1}
|
||||
end
|
||||
|
||||
def count(range) do
|
||||
{:ok, size(range)}
|
||||
def slice(range) do
|
||||
%{
|
||||
first_in_iso_days: first,
|
||||
last_in_iso_days: last,
|
||||
first: %{calendar: calendar}
|
||||
} = range
|
||||
|
||||
if first <= last do
|
||||
{:ok, last - first + 1, &slice_asc(first + &1, &2, calendar)}
|
||||
else
|
||||
{:ok, first - last + 1, &slice_desc(first - &1, &2, calendar)}
|
||||
end
|
||||
end
|
||||
|
||||
def slice(
|
||||
%Date.Range{
|
||||
first_in_iso_days: first,
|
||||
first: %{calendar: calendar},
|
||||
step: step
|
||||
} = range
|
||||
) do
|
||||
{:ok, size(range), &slice(first + &1 * step, step, &2, calendar)}
|
||||
defp slice_asc(current, 1, calendar), do: [date_from_iso_days(current, calendar)]
|
||||
|
||||
defp slice_asc(current, remaining, calendar) do
|
||||
[date_from_iso_days(current, calendar) | slice_asc(current + 1, remaining - 1, calendar)]
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def slice(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
slice(Map.put(date_range, :step, step))
|
||||
defp slice_desc(current, 1, calendar), do: [date_from_iso_days(current, calendar)]
|
||||
|
||||
defp slice_desc(current, remaining, calendar) do
|
||||
[date_from_iso_days(current, calendar) | slice_desc(current - 1, remaining - 1, calendar)]
|
||||
end
|
||||
|
||||
defp slice(current, _step, 1, calendar) do
|
||||
[date_from_iso_days(current, calendar)]
|
||||
def reduce(range, acc, fun) do
|
||||
%{
|
||||
first_in_iso_days: first_in_iso_days,
|
||||
last_in_iso_days: last_in_iso_days,
|
||||
first: %{calendar: calendar}
|
||||
} = range
|
||||
|
||||
up? = first_in_iso_days <= last_in_iso_days
|
||||
reduce(first_in_iso_days, last_in_iso_days, acc, fun, calendar, up?)
|
||||
end
|
||||
|
||||
defp slice(current, step, remaining, calendar) do
|
||||
[
|
||||
date_from_iso_days(current, calendar)
|
||||
| slice(current + step, step, remaining - 1, calendar)
|
||||
]
|
||||
end
|
||||
|
||||
def reduce(
|
||||
%Date.Range{
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
first: %{calendar: calendar},
|
||||
step: step
|
||||
},
|
||||
acc,
|
||||
fun
|
||||
) do
|
||||
reduce(first_days, last_days, acc, fun, step, calendar)
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def reduce(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range,
|
||||
acc,
|
||||
fun
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
reduce(Map.put(date_range, :step, step), acc, fun)
|
||||
end
|
||||
|
||||
defp reduce(_first_days, _last_days, {:halt, acc}, _fun, _step, _calendar) do
|
||||
defp reduce(_x, _y, {:halt, acc}, _fun, _calendar, _up?) do
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp reduce(first_days, last_days, {:suspend, acc}, fun, step, calendar) do
|
||||
{:suspended, acc, &reduce(first_days, last_days, &1, fun, step, calendar)}
|
||||
defp reduce(x, y, {:suspend, acc}, fun, calendar, up?) do
|
||||
{:suspended, acc, &reduce(x, y, &1, fun, calendar, up?)}
|
||||
end
|
||||
|
||||
defp reduce(first_days, last_days, {:cont, acc}, fun, step, calendar)
|
||||
when step > 0 and first_days <= last_days
|
||||
when step < 0 and first_days >= last_days do
|
||||
reduce(
|
||||
first_days + step,
|
||||
last_days,
|
||||
fun.(date_from_iso_days(first_days, calendar), acc),
|
||||
fun,
|
||||
step,
|
||||
calendar
|
||||
)
|
||||
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = true) when x <= y do
|
||||
reduce(x + 1, y, fun.(date_from_iso_days(x, calendar), acc), fun, calendar, up?)
|
||||
end
|
||||
|
||||
defp reduce(_, _, {:cont, acc}, _fun, _step, _calendar) do
|
||||
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = false) when x >= y do
|
||||
reduce(x - 1, y, fun.(date_from_iso_days(x, calendar), acc), fun, calendar, up?)
|
||||
end
|
||||
|
||||
defp reduce(_, _, {:cont, acc}, _fun, _calendar, _up) do
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
@@ -158,70 +122,11 @@ defmodule Date.Range do
|
||||
|
||||
%Date{year: year, month: month, day: day, calendar: calendar}
|
||||
end
|
||||
|
||||
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
|
||||
when step > 0 and first_days > last_days,
|
||||
do: 0
|
||||
|
||||
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
|
||||
when step < 0 and first_days < last_days,
|
||||
do: 0
|
||||
|
||||
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step}),
|
||||
do: abs(div(last_days - first_days, step)) + 1
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
defp size(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
size(Map.put(date_range, :step, step))
|
||||
end
|
||||
|
||||
defp empty?(%Date.Range{
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
step: step
|
||||
})
|
||||
when step > 0 and first_days > last_days,
|
||||
do: true
|
||||
|
||||
defp empty?(%Date.Range{
|
||||
first_in_iso_days: first_days,
|
||||
last_in_iso_days: last_days,
|
||||
step: step
|
||||
})
|
||||
when step < 0 and first_days < last_days,
|
||||
do: true
|
||||
|
||||
defp empty?(%Date.Range{step: _}), do: false
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
defp empty?(
|
||||
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
|
||||
date_range
|
||||
) do
|
||||
step = if first_days <= last_days, do: 1, else: -1
|
||||
empty?(Map.put(date_range, :step, step))
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect do
|
||||
import Kernel, except: [inspect: 2]
|
||||
|
||||
def inspect(%Date.Range{first: first, last: last, step: 1}, _) do
|
||||
def inspect(%Date.Range{first: first, last: last}, _) do
|
||||
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ">"
|
||||
end
|
||||
|
||||
def inspect(%Date.Range{first: first, last: last, step: step}, _) do
|
||||
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ", #{step}>"
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def inspect(%{__struct__: Date.Range, first: first, last: last} = date_range, opts) do
|
||||
step = if first <= last, do: 1, else: -1
|
||||
inspect(Map.put(date_range, :step, step), opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,14 +2,11 @@ defmodule DateTime do
|
||||
@moduledoc """
|
||||
A datetime implementation with a time zone.
|
||||
|
||||
This datetime can be seen as a snapshot of a date and time
|
||||
at a given time zone. For such purposes, it also includes both
|
||||
UTC and Standard offsets, as well as the zone abbreviation
|
||||
field used exclusively for formatting purposes. Note future
|
||||
datetimes are not necessarily guaranteed to exist, as time
|
||||
zones may change any time in the future due to geopolitical
|
||||
reasons. See the "Datetimes as snapshots" section for more
|
||||
information.
|
||||
This datetime can be seen as an ephemeral snapshot
|
||||
of a datetime at a given time zone. For such purposes,
|
||||
it also includes both UTC and Standard offsets, as
|
||||
well as the zone abbreviation field used exclusively
|
||||
for formatting purposes.
|
||||
|
||||
Remember, comparisons in Elixir using `==/2`, `>/2`, `</2` and friends
|
||||
are structural and based on the DateTime struct fields. For proper
|
||||
@@ -44,56 +41,6 @@ defmodule DateTime do
|
||||
Calendar.put_time_zone_database(Tzdata.TimeZoneDatabase)
|
||||
|
||||
See the proper names in the library installation instructions.
|
||||
|
||||
## Datetimes as snapshots
|
||||
|
||||
In the first section, we described datetimes as a "snapshot of
|
||||
a date and time at a given time zone". To understand precisely
|
||||
what we mean, let's see an example.
|
||||
|
||||
Imagine someone in Poland wants to schedule a meeting with someone
|
||||
in Brazil in the next year. The meeting will happen at 2:30 AM
|
||||
in the Polish time zone. At what time will the meeting happen in
|
||||
Brazil?
|
||||
|
||||
You can consult the time zone database today, one year before,
|
||||
using the API in this module and it will give you an answer that
|
||||
is valid right now. However, this answer may not be valid in the
|
||||
future. Why? Because both Brazil and Poland may change their timezone
|
||||
rules, ultimately affecting the result. For example, a country may
|
||||
choose to enter or abandon "Daylight Saving Time", which is a
|
||||
process where we adjust the clock one hour forward or one hour
|
||||
back once per year. Whenener the rules change, the exact instant
|
||||
that 2:30 AM in Polish time will be in Brazil may change.
|
||||
|
||||
In other words, whenever working with future DateTimes, there is
|
||||
no guarantee the results you get will always be correct, until
|
||||
the event actually happens. Therefore, when you ask for a future
|
||||
time, the answers you get are a snapshot that reflects the current
|
||||
state of the time zone rules. For datetimes in the past, this is
|
||||
not a problem, because time zone rules do not change for past
|
||||
events.
|
||||
|
||||
To make matters worse, it may be that the 2:30 AM in Polish time
|
||||
does not actually even exist or it is ambiguous. If a certain
|
||||
time zone observes "Daylight Saving Time", they will move their
|
||||
clock forward once a year. When this happens, there is a whole
|
||||
hour that does not exist. Then, when they move the clock back,
|
||||
there is a certain hour that will happen twice. So if you want
|
||||
to schedule a meeting when this shift back happens, you would
|
||||
need to explicitly say which of the 2:30 AM you precisely mean.
|
||||
Applications that are date and time sensitive, need to take
|
||||
these scenarios into account and correctly communicate them to
|
||||
users.
|
||||
|
||||
The good news is: Elixir contains all of the building blocks
|
||||
necessary to tackle those problems. The default timezone database
|
||||
used by Elixir, `Calendar.UTCOnlyTimeZoneDatabase`, only works
|
||||
with UTC, which does not observe those issues. Once you bring
|
||||
a proper time zone database, the functions in this module will
|
||||
query the database and return the relevant information. For
|
||||
example, look at how `DateTime.new/4` returns different results
|
||||
based on the scenarios described in this section.
|
||||
"""
|
||||
|
||||
@enforce_keys [:year, :month, :day, :hour, :minute, :second] ++
|
||||
@@ -162,9 +109,8 @@ defmodule DateTime do
|
||||
{:ok, ~U[2016-05-24 13:26:08.003Z]}
|
||||
|
||||
When the datetime is ambiguous - for instance during changing from summer
|
||||
to winter time - the two possible valid datetimes are returned in a tuple.
|
||||
The first datetime is also the one which comes first chronologically, while
|
||||
the second one comes last.
|
||||
to winter time - the two possible valid datetimes are returned. First the one
|
||||
that happens first, then the one that happens after.
|
||||
|
||||
iex> {:ambiguous, first_dt, second_dt} = DateTime.new(~D[2018-10-28], ~T[02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> first_dt
|
||||
@@ -193,7 +139,7 @@ defmodule DateTime do
|
||||
@doc since: "1.11.0"
|
||||
@spec new(Date.t(), Time.t(), Calendar.time_zone(), Calendar.time_zone_database()) ::
|
||||
{:ok, t}
|
||||
| {:ambiguous, first_datetime :: t, second_datetime :: t}
|
||||
| {:ambiguous, t, t}
|
||||
| {:gap, t, t}
|
||||
| {:error,
|
||||
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
|
||||
@@ -294,7 +240,9 @@ defmodule DateTime do
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError,
|
||||
"cannot build datetime with #{inspect(date)} and #{inspect(time)}, reason: #{inspect(reason)}"
|
||||
"cannot build datetime with #{inspect(date)} and #{inspect(time)}, reason: #{
|
||||
inspect(reason)
|
||||
}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -418,9 +366,8 @@ defmodule DateTime do
|
||||
{:ok, ~U[2016-05-24 13:26:08.003Z]}
|
||||
|
||||
When the datetime is ambiguous - for instance during changing from summer
|
||||
to winter time - the two possible valid datetimes are returned in a tuple.
|
||||
The first datetime is also the one which comes first chronologically, while
|
||||
the second one comes last.
|
||||
to winter time - the two possible valid datetimes are returned. First the one
|
||||
that happens first, then the one that happens after.
|
||||
|
||||
iex> {:ambiguous, first_dt, second_dt} = DateTime.from_naive(~N[2018-10-28 02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> first_dt
|
||||
@@ -471,7 +418,7 @@ defmodule DateTime do
|
||||
Calendar.time_zone_database()
|
||||
) ::
|
||||
{:ok, t}
|
||||
| {:ambiguous, first_datetime :: t, second_datetime :: t}
|
||||
| {:ambiguous, t, t}
|
||||
| {:gap, t, t}
|
||||
| {:error,
|
||||
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
|
||||
@@ -503,7 +450,7 @@ defmodule DateTime do
|
||||
# we get the last microsecond just before.
|
||||
before_naive =
|
||||
first_period_until_wall
|
||||
|> Map.replace!(:microsecond, {999_999, 6})
|
||||
|> Map.put(:microsecond, {999_999, 6})
|
||||
|> NaiveDateTime.add(-1)
|
||||
|
||||
after_naive = second_period_from_wall
|
||||
@@ -945,14 +892,13 @@ defmodule DateTime do
|
||||
|
||||
@doc """
|
||||
Converts the given datetime to
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601) format.
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601) format.
|
||||
|
||||
By default, `DateTime.to_iso8601/2` returns datetimes formatted in the "extended"
|
||||
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
|
||||
|
||||
Only supports converting datetimes which are in the ISO calendar,
|
||||
attempting to convert datetimes from other calendars will raise.
|
||||
You can also optionally specify an offset for the formatted string.
|
||||
|
||||
WARNING: the ISO 8601 datetime format does not contain the time zone nor
|
||||
its abbreviation, which means information is lost when converting to such
|
||||
@@ -984,31 +930,11 @@ defmodule DateTime do
|
||||
iex> DateTime.to_iso8601(dt, :basic)
|
||||
"20000229T230007-0400"
|
||||
|
||||
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
|
||||
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
|
||||
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
|
||||
iex> DateTime.to_iso8601(dt, :extended, 3600)
|
||||
"2000-03-01T04:00:07+01:00"
|
||||
|
||||
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
|
||||
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
|
||||
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
|
||||
iex> DateTime.to_iso8601(dt, :extended, 0)
|
||||
"2000-03-01T03:00:07+00:00"
|
||||
|
||||
iex> dt = %DateTime{year: 2000, month: 3, day: 01, zone_abbr: "UTC",
|
||||
...> hour: 03, minute: 0, second: 7, microsecond: {0, 0},
|
||||
...> utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"}
|
||||
iex> DateTime.to_iso8601(dt, :extended, 0)
|
||||
"2000-03-01T03:00:07Z"
|
||||
|
||||
iex> {:ok, dt, offset} = DateTime.from_iso8601("2000-03-01T03:00:07Z")
|
||||
iex> "2000-03-01T03:00:07Z" = DateTime.to_iso8601(dt, :extended, offset)
|
||||
"""
|
||||
@spec to_iso8601(Calendar.datetime(), :basic | :extended, nil | integer()) :: String.t()
|
||||
def to_iso8601(datetime, format \\ :extended, offset \\ nil)
|
||||
@spec to_iso8601(Calendar.datetime(), :extended | :basic) :: String.t()
|
||||
def to_iso8601(datetime, format \\ :extended)
|
||||
|
||||
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, nil)
|
||||
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format)
|
||||
when format in [:extended, :basic] do
|
||||
%{
|
||||
year: year,
|
||||
@@ -1023,56 +949,21 @@ defmodule DateTime do
|
||||
std_offset: std_offset
|
||||
} = datetime
|
||||
|
||||
datetime_to_string(year, month, day, hour, minute, second, microsecond, format) <>
|
||||
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)
|
||||
end
|
||||
|
||||
def to_iso8601(
|
||||
%{calendar: Calendar.ISO, microsecond: {_, precision}, time_zone: "Etc/UTC"} = datetime,
|
||||
format,
|
||||
0
|
||||
)
|
||||
when format in [:extended, :basic] do
|
||||
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, 0)
|
||||
|
||||
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
|
||||
"Z"
|
||||
end
|
||||
|
||||
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, offset)
|
||||
when format in [:extended, :basic] do
|
||||
{_, precision} = datetime.microsecond
|
||||
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, offset)
|
||||
|
||||
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
|
||||
Calendar.ISO.offset_to_string(offset, 0, nil, format)
|
||||
end
|
||||
|
||||
def to_iso8601(%{calendar: _} = datetime, format, offset) when format in [:extended, :basic] do
|
||||
def to_iso8601(%{calendar: _} = datetime, format) when format in [:extended, :basic] do
|
||||
datetime
|
||||
|> convert!(Calendar.ISO)
|
||||
|> to_iso8601(format, offset)
|
||||
end
|
||||
|
||||
defp shift_by_offset(%{calendar: calendar} = datetime, offset) do
|
||||
total_offset = datetime.utc_offset + datetime.std_offset
|
||||
|
||||
datetime
|
||||
|> to_iso_days()
|
||||
# Subtract total original offset in order to get UTC and add the new offset
|
||||
|> Calendar.ISO.add_day_fraction_to_iso_days(offset - total_offset, 86400)
|
||||
|> calendar.naive_datetime_from_iso_days()
|
||||
end
|
||||
|
||||
defp datetime_to_string(year, month, day, hour, minute, second, microsecond, format) do
|
||||
Calendar.ISO.date_to_string(year, month, day, format) <>
|
||||
"T" <>
|
||||
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
|
||||
|> to_iso8601(format)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses the extended "Date and time of day" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Since ISO 8601 does not include the proper time zone, the given
|
||||
string will be converted to UTC and its offset in seconds will be
|
||||
@@ -1082,6 +973,9 @@ defmodule DateTime do
|
||||
As specified in the standard, the separator "T" may be omitted if
|
||||
desired as there is no ambiguity within this function.
|
||||
|
||||
The year parsed by this function is limited to four digits and,
|
||||
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
|
||||
hour of the next day, this notation is not supported by Elixir.
|
||||
Note leap seconds are not supported by the built-in Calendar.ISO.
|
||||
|
||||
## Examples
|
||||
|
||||
+149
-417
@@ -1,127 +1,22 @@
|
||||
defmodule Calendar.ISO do
|
||||
@moduledoc """
|
||||
The default calendar implementation, a Gregorian calendar following ISO 8601.
|
||||
A calendar implementation that follows to ISO 8601.
|
||||
|
||||
This calendar implements a proleptic Gregorian calendar and
|
||||
This calendar implements the proleptic Gregorian calendar and
|
||||
is therefore compatible with the calendar used in most countries
|
||||
today. The proleptic means the Gregorian rules for leap years are
|
||||
applied for all time, consequently the dates give different results
|
||||
before the year 1583 from when the Gregorian calendar was adopted.
|
||||
|
||||
## ISO 8601 compliance
|
||||
|
||||
The ISO 8601 specification is feature-rich, but allows applications
|
||||
to selectively implement most parts of it. The choices Elixir makes
|
||||
are catalogued below.
|
||||
|
||||
### Features
|
||||
|
||||
The standard library supports a minimal set of possible ISO 8601 features.
|
||||
Specifically, the parser only supports calendar dates and does not support
|
||||
ordinal and week formats.
|
||||
|
||||
By default Elixir only parses extended-formatted date/times. You can opt-in
|
||||
to parse basic-formatted date/times.
|
||||
|
||||
`NaiveDateTime.to_iso8601/2` and `DateTime.to_iso8601/2` allow you to produce
|
||||
either basic or extended formatted strings, and `Calendar.strftime/2` allows
|
||||
you to format datetimes however else you desire.
|
||||
|
||||
Elixir does not support reduced accuracy formats (for example, a date without
|
||||
the day component) nor decimal precisions in the lowest component (such as
|
||||
`10:01:25,5`). No functions exist to parse ISO 8601 durations or time intervals.
|
||||
|
||||
#### Examples
|
||||
|
||||
Elixir expects the extended format by default when parsing:
|
||||
|
||||
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("20150123T235007")
|
||||
{:error, :invalid_format}
|
||||
|
||||
Parsing can be restricted to basic if desired:
|
||||
|
||||
iex> Calendar.ISO.parse_naive_datetime("20150123T235007Z", :basic)
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
|
||||
iex> Calendar.ISO.parse_naive_datetime("20150123T235007Z", :extended)
|
||||
{:error, :invalid_format}
|
||||
|
||||
Only calendar dates are supported in parsing; ordinal and week dates are not.
|
||||
|
||||
iex> Calendar.ISO.parse_date("2015-04-15")
|
||||
{:ok, {2015, 4, 15}}
|
||||
iex> Calendar.ISO.parse_date("2015-105")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_date("2015-W16")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_date("2015-W016-3")
|
||||
{:error, :invalid_format}
|
||||
|
||||
Years, months, days, hours, minutes, and seconds must be fully specified:
|
||||
|
||||
iex> Calendar.ISO.parse_date("2015-04-15")
|
||||
{:ok, {2015, 4, 15}}
|
||||
iex> Calendar.ISO.parse_date("2015-04")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_date("2015")
|
||||
{:error, :invalid_format}
|
||||
|
||||
iex> Calendar.ISO.parse_time("23:50:07.0123456")
|
||||
{:ok, {23, 50, 7, {12345, 6}}}
|
||||
iex> Calendar.ISO.parse_time("23:50:07")
|
||||
{:ok, {23, 50, 7, {0, 0}}}
|
||||
iex> Calendar.ISO.parse_time("23:50")
|
||||
{:error, :invalid_format}
|
||||
iex> Calendar.ISO.parse_time("23")
|
||||
{:error, :invalid_format}
|
||||
|
||||
### Extensions
|
||||
|
||||
The parser and formatter adopt one ISO 8601 extension: extended year notation.
|
||||
|
||||
This allows dates to be prefixed with a `+` or `-` sign, extending the range of
|
||||
expressible years from the default (`0000..9999`) to `-9999..9999`. Elixir still
|
||||
restricts years in this format to four digits.
|
||||
|
||||
#### 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_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-23 23:50:07")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
|
||||
|
||||
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23 23:50:07Z")
|
||||
{:ok, {-2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
|
||||
iex> Calendar.ISO.parse_utc_datetime("+2015-01-23 23:50:07Z")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
|
||||
|
||||
### Additions
|
||||
|
||||
ISO 8601 does not allow a whitespace instead of `T` as a separator
|
||||
between date and times, both when parsing and formatting.
|
||||
This is a common enough representation, Elixir allows it during parsing.
|
||||
|
||||
The formatting of dates in `NaiveDateTime.to_iso8601/1` and `DateTime.to_iso8601/1`
|
||||
do produce specification-compliant string representations using the `T` separator.
|
||||
|
||||
#### Examples
|
||||
|
||||
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.0123456")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
|
||||
|
||||
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07.0123456Z")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}, 0}
|
||||
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.0123456Z")
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}, 0}
|
||||
Given this is the default calendar used by Elixir, it has one
|
||||
difference compared to the ISO8601 specification in that it allows
|
||||
a whitespace instead of `T` as a seperator between date and times
|
||||
both when parsing and formatting. Strict formatting can be done
|
||||
by using the `to_iso8601` found in `NaiveDateTime` and `DateTime`.
|
||||
|
||||
Note that while ISO 8601 allows times and datetimes to specify
|
||||
24:00:00 as the zero hour of the next day, this notation is not
|
||||
supported by Elixir.
|
||||
"""
|
||||
|
||||
@behaviour Calendar
|
||||
@@ -131,8 +26,6 @@ defmodule Calendar.ISO do
|
||||
unix_end = 315_569_519_999_999_999 - @unix_epoch * 1_000_000
|
||||
@unix_range_microseconds unix_start..unix_end
|
||||
|
||||
defguardp is_format(term) when term in [:basic, :extended]
|
||||
|
||||
@typedoc """
|
||||
"Before the Current Era" or "Before the Common Era" (BCE), for those years less than `1`.
|
||||
"""
|
||||
@@ -158,8 +51,6 @@ defmodule Calendar.ISO do
|
||||
@type minute :: 0..59
|
||||
@type second :: 0..59
|
||||
@type weekday :: :monday | :tuesday | :wednesday | :thursday | :friday | :saturday | :sunday
|
||||
@type utc_offset :: integer
|
||||
@type format :: :basic | :extended
|
||||
|
||||
@typedoc """
|
||||
Microseconds with stored precision.
|
||||
@@ -187,10 +78,7 @@ defmodule Calendar.ISO do
|
||||
@microseconds_per_second 1_000_000
|
||||
@parts_per_day @seconds_per_day * @microseconds_per_second
|
||||
|
||||
@datetime_seps [?\s, ?T]
|
||||
@ext_date_sep ?-
|
||||
@ext_time_sep ?:
|
||||
|
||||
@sep [?\s, ?T]
|
||||
@days_per_nonleap_year 365
|
||||
@days_per_leap_year 366
|
||||
|
||||
@@ -199,11 +87,10 @@ defmodule Calendar.ISO do
|
||||
# on ~D[0001-01-01] which is 366 days later.
|
||||
@iso_epoch 366
|
||||
|
||||
[match_basic_date, match_ext_date, guard_date, read_date] =
|
||||
[match_date, guard_date, read_date] =
|
||||
quote do
|
||||
[
|
||||
<<y1, y2, y3, y4, m1, m2, d1, d2>>,
|
||||
<<y1, y2, y3, y4, @ext_date_sep, m1, m2, @ext_date_sep, d1, d2>>,
|
||||
<<y1, y2, y3, y4, ?-, m1, m2, ?-, d1, d2>>,
|
||||
y1 >= ?0 and y1 <= ?9 and y2 >= ?0 and y2 <= ?9 and y3 >= ?0 and y3 <= ?9 and y4 >= ?0 and
|
||||
y4 <= ?9 and m1 >= ?0 and m1 <= ?9 and m2 >= ?0 and m2 <= ?9 and d1 >= ?0 and d1 <= ?9 and
|
||||
d2 >= ?0 and d2 <= ?9,
|
||||
@@ -215,11 +102,10 @@ defmodule Calendar.ISO do
|
||||
]
|
||||
end
|
||||
|
||||
[match_basic_time, match_ext_time, guard_time, read_time] =
|
||||
[match_time, guard_time, read_time] =
|
||||
quote do
|
||||
[
|
||||
<<h1, h2, i1, i2, s1, s2>>,
|
||||
<<h1, h2, @ext_time_sep, i1, i2, @ext_time_sep, s1, s2>>,
|
||||
<<h1, h2, ?:, i1, i2, ?:, s1, s2>>,
|
||||
h1 >= ?0 and h1 <= ?9 and h2 >= ?0 and h2 <= ?9 and i1 >= ?0 and i1 <= ?9 and i2 >= ?0 and
|
||||
i2 <= ?9 and s1 >= ?0 and s1 <= ?9 and s2 >= ?0 and s2 <= ?9,
|
||||
{
|
||||
@@ -248,76 +134,49 @@ defmodule Calendar.ISO do
|
||||
defguardp is_std_offset(offset) when is_integer(offset)
|
||||
|
||||
@doc """
|
||||
Parses a time `string` in the `:extended` format.
|
||||
|
||||
For more information on supported strings, see how this
|
||||
module implements [ISO 8601](#module-iso-8601-compliance).
|
||||
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
|
||||
@spec parse_time(String.t()) ::
|
||||
{:ok, {hour, minute, second, microsecond}}
|
||||
| {:error, atom}
|
||||
def parse_time("T" <> string) when is_binary(string),
|
||||
do: do_parse_time(string)
|
||||
|
||||
def parse_time(string) when is_binary(string),
|
||||
do: parse_time(string, :extended)
|
||||
do: do_parse_time(string)
|
||||
|
||||
@doc """
|
||||
Parses a time `string` according to a given `format`.
|
||||
|
||||
The `format` can either be `:basic` or `:extended`.
|
||||
|
||||
For more information on supported strings, see how this
|
||||
module implements [ISO 8601](#module-iso-8601-compliance).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.parse_time("235007", :basic)
|
||||
{:ok, {23, 50, 7, {0, 0}}}
|
||||
iex> Calendar.ISO.parse_time("235007", :extended)
|
||||
{:error, :invalid_format}
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec parse_time(String.t(), format) ::
|
||||
{:ok, {hour, minute, second, microsecond}}
|
||||
| {:error, atom}
|
||||
def parse_time(string, format) when is_binary(string) and is_format(format) do
|
||||
case string do
|
||||
"T" <> rest -> do_parse_time(rest, format)
|
||||
_ -> do_parse_time(string, format)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_parse_time(<<unquote(match_basic_time), rest::binary>>, :basic)
|
||||
when unquote(guard_time) do
|
||||
{hour, minute, second} = unquote(read_time)
|
||||
parse_formatted_time(hour, minute, second, rest)
|
||||
end
|
||||
|
||||
defp do_parse_time(<<unquote(match_ext_time), rest::binary>>, :extended)
|
||||
when unquote(guard_time) do
|
||||
{hour, minute, second} = unquote(read_time)
|
||||
parse_formatted_time(hour, minute, second, rest)
|
||||
end
|
||||
|
||||
defp do_parse_time(_, _) do
|
||||
{:error, :invalid_format}
|
||||
end
|
||||
|
||||
defp parse_formatted_time(hour, minute, second, rest) do
|
||||
with {microsecond, rest} <- parse_microsecond(rest),
|
||||
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
|
||||
@@ -329,16 +188,14 @@ defmodule Calendar.ISO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a date `string` in the `:extended` format.
|
||||
|
||||
For more information on supported strings, see how this
|
||||
module implements [ISO 8601](#module-iso-8601-compliance).
|
||||
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")
|
||||
@@ -347,160 +204,91 @@ defmodule Calendar.ISO do
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@impl true
|
||||
@spec parse_date(String.t()) ::
|
||||
{:ok, {year, month, day}}
|
||||
| {:error, atom}
|
||||
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, :extended)
|
||||
do: parse_date(string, 1)
|
||||
|
||||
@doc """
|
||||
Parses a date `string` according to a given `format`.
|
||||
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
|
||||
|
||||
The `format` can either be `:basic` or `:extended`.
|
||||
|
||||
For more information on supported strings, see how this
|
||||
module implements [ISO 8601](#module-iso-8601-compliance).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.parse_date("20150123", :basic)
|
||||
{:ok, {2015, 1, 23}}
|
||||
iex> Calendar.ISO.parse_date("20150123", :extended)
|
||||
{:error, :invalid_format}
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec parse_date(String.t(), format) ::
|
||||
{:ok, {year, month, day}}
|
||||
| {:error, atom}
|
||||
def parse_date(string, format) when is_binary(string) and is_format(format),
|
||||
do: parse_date_guarded(string, format)
|
||||
|
||||
defp parse_date_guarded("-" <> string, format),
|
||||
do: do_parse_date(string, -1, format)
|
||||
|
||||
defp parse_date_guarded("+" <> string, format),
|
||||
do: do_parse_date(string, 1, format)
|
||||
|
||||
defp parse_date_guarded(string, format),
|
||||
do: do_parse_date(string, 1, format)
|
||||
|
||||
defp do_parse_date(unquote(match_basic_date), multiplier, :basic) when unquote(guard_date) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
parse_formatted_date(year, month, day, multiplier)
|
||||
end
|
||||
|
||||
defp do_parse_date(unquote(match_ext_date), multiplier, :extended) when unquote(guard_date) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
parse_formatted_date(year, month, day, multiplier)
|
||||
end
|
||||
|
||||
defp do_parse_date(_, _, _) do
|
||||
{:error, :invalid_format}
|
||||
end
|
||||
|
||||
defp parse_formatted_date(year, month, day, multiplier) do
|
||||
year = multiplier * year
|
||||
|
||||
if valid_date?(year, month, day) do
|
||||
{:ok, {year, month, day}}
|
||||
if valid_date?(year, month, day) do
|
||||
{:ok, {year, month, day}}
|
||||
else
|
||||
{:error, :invalid_date}
|
||||
end
|
||||
else
|
||||
{:error, :invalid_date}
|
||||
_ ->
|
||||
{:error, :invalid_format}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a naive datetime `string` in the `:extended` format.
|
||||
|
||||
For more information on supported strings, see how this
|
||||
module implements [ISO 8601](#module-iso-8601-compliance).
|
||||
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-23 23:50:07Z")
|
||||
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-23 23:50:07-02:30")
|
||||
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
|
||||
@spec parse_naive_datetime(String.t()) ::
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}}
|
||||
| {:error, atom}
|
||||
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, :extended)
|
||||
do: parse_naive_datetime(string, 1)
|
||||
|
||||
@doc """
|
||||
Parses a naive datetime `string` according to a given `format`.
|
||||
|
||||
The `format` can either be `:basic` or `:extended`.
|
||||
|
||||
For more information on supported strings, see how this
|
||||
module implements [ISO 8601](#module-iso-8601-compliance).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.parse_naive_datetime("20150123 235007", :basic)
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
|
||||
iex> Calendar.ISO.parse_naive_datetime("20150123 235007", :extended)
|
||||
{:error, :invalid_format}
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec parse_naive_datetime(String.t(), format) ::
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}}
|
||||
| {:error, atom}
|
||||
def parse_naive_datetime(string, format) when is_binary(string) and is_format(format),
|
||||
do: parse_naive_datetime_guarded(string, format)
|
||||
|
||||
defp parse_naive_datetime_guarded("-" <> string, format),
|
||||
do: do_parse_naive_datetime(string, -1, format)
|
||||
|
||||
defp parse_naive_datetime_guarded("+" <> string, format),
|
||||
do: do_parse_naive_datetime(string, 1, format)
|
||||
|
||||
defp parse_naive_datetime_guarded(string, format),
|
||||
do: do_parse_naive_datetime(string, 1, format)
|
||||
|
||||
defp do_parse_naive_datetime(
|
||||
<<unquote(match_basic_date), datetime_sep, unquote(match_basic_time), rest::binary>>,
|
||||
multiplier,
|
||||
:basic
|
||||
)
|
||||
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
{hour, minute, second} = unquote(read_time)
|
||||
parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier)
|
||||
end
|
||||
|
||||
defp do_parse_naive_datetime(
|
||||
<<unquote(match_ext_date), datetime_sep, unquote(match_ext_time), rest::binary>>,
|
||||
multiplier,
|
||||
:extended
|
||||
)
|
||||
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
{hour, minute, second} = unquote(read_time)
|
||||
parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier)
|
||||
end
|
||||
|
||||
defp do_parse_naive_datetime(_, _, _) do
|
||||
{:error, :invalid_format}
|
||||
end
|
||||
|
||||
defp parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier) do
|
||||
year = multiplier * year
|
||||
|
||||
with {microsecond, rest} <- parse_microsecond(rest),
|
||||
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}
|
||||
@@ -517,94 +305,54 @@ defmodule Calendar.ISO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a UTC datetime `string` in the `:extended` format.
|
||||
|
||||
For more information on supported strings, see how this
|
||||
module implements [ISO 8601](#module-iso-8601-compliance).
|
||||
Parses a UTC datetime string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07Z")
|
||||
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-23 23:50:07+02:30")
|
||||
{:ok, {2015, 1, 23, 21, 20, 7, {0, 0}}, 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-23 23:50:07")
|
||||
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
|
||||
@spec parse_utc_datetime(String.t()) ::
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}, utc_offset}
|
||||
| {:error, atom}
|
||||
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, :extended)
|
||||
do: parse_utc_datetime(string, 1)
|
||||
|
||||
@doc """
|
||||
Parses a UTC datetime `string` according to a given `format`.
|
||||
|
||||
The `format` can either be `:basic` or `:extended`.
|
||||
|
||||
For more information on supported strings, see how this
|
||||
module implements [ISO 8601](#module-iso-8601-compliance).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.parse_utc_datetime("20150123 235007Z", :basic)
|
||||
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
|
||||
iex> Calendar.ISO.parse_utc_datetime("20150123 235007Z", :extended)
|
||||
{:error, :invalid_format}
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec parse_utc_datetime(String.t(), format) ::
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}, utc_offset}
|
||||
| {:error, atom}
|
||||
def parse_utc_datetime(string, format) when is_binary(string) and is_format(format),
|
||||
do: parse_utc_datetime_guarded(string, format)
|
||||
|
||||
defp parse_utc_datetime_guarded("-" <> string, format),
|
||||
do: do_parse_utc_datetime(string, -1, format)
|
||||
|
||||
defp parse_utc_datetime_guarded("+" <> string, format),
|
||||
do: do_parse_utc_datetime(string, 1, format)
|
||||
|
||||
defp parse_utc_datetime_guarded(string, format),
|
||||
do: do_parse_utc_datetime(string, 1, format)
|
||||
|
||||
defp do_parse_utc_datetime(
|
||||
<<unquote(match_basic_date), datetime_sep, unquote(match_basic_time), rest::binary>>,
|
||||
multiplier,
|
||||
:basic
|
||||
)
|
||||
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
{hour, minute, second} = unquote(read_time)
|
||||
parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier)
|
||||
end
|
||||
|
||||
defp do_parse_utc_datetime(
|
||||
<<unquote(match_ext_date), datetime_sep, unquote(match_ext_time), rest::binary>>,
|
||||
multiplier,
|
||||
:extended
|
||||
)
|
||||
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
{hour, minute, second} = unquote(read_time)
|
||||
parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier)
|
||||
end
|
||||
|
||||
defp do_parse_utc_datetime(_, _, _) do
|
||||
{:error, :invalid_format}
|
||||
end
|
||||
|
||||
defp parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier) do
|
||||
year = multiplier * year
|
||||
|
||||
with {microsecond, rest} <- parse_microsecond(rest),
|
||||
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}
|
||||
@@ -634,7 +382,8 @@ defmodule Calendar.ISO do
|
||||
{:ok, {year, month, day, hour, minute, second, microsecond}, offset}
|
||||
end
|
||||
else
|
||||
_ -> {:error, :invalid_format}
|
||||
_ ->
|
||||
{:error, :invalid_format}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1023,33 +772,14 @@ defmodule Calendar.ISO do
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec year_of_era(year) :: {1..10000, era}
|
||||
def year_of_era(year) when is_year_CE(year), do: {year, 1}
|
||||
def year_of_era(year) when is_year_BCE(year), do: {abs(year) + 1, 0}
|
||||
|
||||
@doc """
|
||||
Calendar callback to compute the year and era from the
|
||||
given `year`, `month` and `day`.
|
||||
|
||||
In the ISO calendar, the new year coincides with the new era,
|
||||
so the `month` and `day` arguments are discarded. If you only
|
||||
have the year available, you can `year_of_era/1` instead.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.year_of_era(1, 1, 1)
|
||||
{1, 1}
|
||||
iex> Calendar.ISO.year_of_era(2018, 12, 1)
|
||||
{2018, 1}
|
||||
iex> Calendar.ISO.year_of_era(0, 1, 1)
|
||||
{1, 0}
|
||||
iex> Calendar.ISO.year_of_era(-1, 12, 1)
|
||||
{2, 0}
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@impl true
|
||||
@spec year_of_era(year, month, day) :: {1..10000, era}
|
||||
def year_of_era(year, _month, _day), do: year_of_era(year)
|
||||
def year_of_era(year) when is_year_CE(year) do
|
||||
{year, 1}
|
||||
end
|
||||
|
||||
def year_of_era(year) when is_year_BCE(year) do
|
||||
{abs(year) + 1, 0}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the day and era from the given `year`, `month`, and `day`.
|
||||
@@ -1345,7 +1075,9 @@ defmodule Calendar.ISO do
|
||||
@doc """
|
||||
Determines if the date given is valid according to the proleptic Gregorian calendar.
|
||||
|
||||
Leap seconds are not supported by the built-in Calendar.ISO.
|
||||
Note that while ISO 8601 allows times to specify 24:00:00 as the
|
||||
zero hour of the next day, this notation is not supported by Elixir.
|
||||
Leap seconds are not supported as well by the built-in Calendar.ISO.
|
||||
|
||||
## Examples
|
||||
|
||||
|
||||
@@ -598,7 +598,7 @@ defmodule NaiveDateTime do
|
||||
|
||||
@doc """
|
||||
Parses the extended "Date and time of day" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Time zone offset may be included in the string but they will be
|
||||
simply discarded as such information is not included in naive date
|
||||
@@ -607,6 +607,9 @@ defmodule NaiveDateTime do
|
||||
As specified in the standard, the separator "T" may be omitted if
|
||||
desired as there is no ambiguity within this function.
|
||||
|
||||
The year parsed by this function is limited to four digits and,
|
||||
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
|
||||
hour of the next day, this notation is not supported by Elixir.
|
||||
Note leap seconds are not supported by the built-in Calendar.ISO.
|
||||
|
||||
## Examples
|
||||
@@ -673,7 +676,7 @@ defmodule NaiveDateTime do
|
||||
|
||||
@doc """
|
||||
Parses the extended "Date and time of day" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Raises if the format is invalid.
|
||||
|
||||
@@ -701,7 +704,7 @@ defmodule NaiveDateTime do
|
||||
|
||||
@doc """
|
||||
Converts the given naive datetime to
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
By default, `NaiveDateTime.to_iso8601/2` returns naive datetimes formatted in the "extended"
|
||||
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
|
||||
|
||||
@@ -211,7 +211,7 @@ defmodule Time do
|
||||
|
||||
@doc """
|
||||
Parses the extended "Local time" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Time zone offset may be included in the string but they will be
|
||||
simply discarded as such information is not included in times.
|
||||
@@ -219,6 +219,12 @@ defmodule Time do
|
||||
As specified in the standard, the separator "T" may be omitted if
|
||||
desired as there is no ambiguity within this function.
|
||||
|
||||
Time representations with reduced accuracy are not supported.
|
||||
|
||||
Note that while ISO 8601 allows times to specify 24:00:00 as the
|
||||
zero hour of the next day, this notation is not supported by Elixir.
|
||||
Leap seconds are not supported as well by the built-in Calendar.ISO.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Time.from_iso8601("23:50:07")
|
||||
@@ -257,7 +263,7 @@ defmodule Time do
|
||||
|
||||
@doc """
|
||||
Parses the extended "Local time" format described by
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
Raises if the format is invalid.
|
||||
|
||||
@@ -284,7 +290,7 @@ defmodule Time do
|
||||
|
||||
@doc """
|
||||
Converts the given time to
|
||||
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
|
||||
By default, `Time.to_iso8601/2` returns times formatted in the "extended"
|
||||
format, for human readability. It also supports the "basic" format through
|
||||
|
||||
+200
-467
@@ -2,7 +2,7 @@ defmodule Code do
|
||||
@moduledoc ~S"""
|
||||
Utilities for managing code compilation, code evaluation, and code loading.
|
||||
|
||||
This module complements Erlang's [`:code` module](`:code`)
|
||||
This module complements Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html)
|
||||
to add behaviour which is specific to Elixir. Almost all of the functions in this module
|
||||
have global side effects on the behaviour of Elixir.
|
||||
|
||||
@@ -30,61 +30,6 @@ defmodule Code do
|
||||
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.
|
||||
|
||||
The functions above work with Elixir source. If you want to work
|
||||
with modules compiled to bytecode, which have the `.beam` extension
|
||||
and are typically found below the _build directory of a Mix project,
|
||||
see the functions in Erlang's [`:code`](`:code`) module.
|
||||
|
||||
## Code loading on the Erlang VM
|
||||
|
||||
Erlang has two modes to load code: interactive and embedded.
|
||||
|
||||
By default, the Erlang VM runs in interactive mode, where modules
|
||||
are loaded as needed. In embedded mode the opposite happens, as all
|
||||
modules need to be loaded upfront or explicitly.
|
||||
|
||||
You can use `ensure_loaded/1` (as well as `ensure_loaded?/1` and
|
||||
`ensure_loaded!/1`) to check if a module is loaded before using it and
|
||||
act.
|
||||
|
||||
## `ensure_compiled/1` and `ensure_compiled!/1`
|
||||
|
||||
Elixir also includes `ensure_compiled/1` and `ensure_compiled!/1`
|
||||
functions that are a superset of `ensure_loaded/1`.
|
||||
|
||||
Since Elixir's compilation happens in parallel, in some situations
|
||||
you may need to use a module that was not yet compiled, therefore
|
||||
it can't even be loaded.
|
||||
|
||||
When invoked, `ensure_compiled/1` and `ensure_compiled!/1` halt the
|
||||
compilation of the caller until the module becomes available. Note
|
||||
the distinction between `ensure_compiled/1` and `ensure_compiled!/1`
|
||||
is important: if you are using `ensure_compiled!/1`, you are
|
||||
indicating to the compiler that you can only continue if said module
|
||||
is available.
|
||||
|
||||
If you are using `Code.ensure_compiled/1`, you are implying you may
|
||||
continue without the module and therefore Elixir may return
|
||||
`{:error, :unavailable}` for cases where the module is not yet available
|
||||
(but may be available later on).
|
||||
|
||||
For those reasons, developers must typically use `Code.ensure_compiled!/1`.
|
||||
In particular, do not do this:
|
||||
|
||||
case Code.ensure_compiled(module) do
|
||||
{:module, _} -> module
|
||||
{:error, _} -> raise ...
|
||||
end
|
||||
|
||||
Finally, note you only need `ensure_compiled!/1` to check for modules
|
||||
being defined within the same project. It does not apply to modules from
|
||||
dependencies as dependencies are always compiled upfront.
|
||||
|
||||
In most cases, `ensure_loaded/1` is enough. `ensure_compiled!/1`
|
||||
must be used in rare cases, usually involving macros that need to
|
||||
invoke a module for callback information. The use of `ensure_compiled/1`
|
||||
is even less likely.
|
||||
|
||||
## Compilation tracers
|
||||
|
||||
Elixir supports compilation tracers, which allows modules to observe constructs
|
||||
@@ -149,12 +94,6 @@ defmodule Code do
|
||||
of keys to traverse in the application environment and `return` is either
|
||||
`{:ok, value}` or `:error`.
|
||||
|
||||
* `{:on_module, bytecode, :none}` - (since v1.11.0) traced whenever a module
|
||||
is defined. This is equivalent to the `@after_compile` callback and invoked
|
||||
after any `@after_compile` in the given module. The third element is currently
|
||||
`:none` but it may provide more metadata in the future. It is best to ignore
|
||||
it at the moment.
|
||||
|
||||
The `:tracers` compiler option can be combined with the `:parser_options`
|
||||
compiler option to enrich the metadata of the traced events above.
|
||||
|
||||
@@ -217,12 +156,6 @@ defmodule Code do
|
||||
required_files()
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Code.Fragment.cursor_context/2 instead"
|
||||
def cursor_context(code, options \\ []) do
|
||||
Code.Fragment.cursor_context(code, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes files from the required files list.
|
||||
|
||||
@@ -343,63 +276,67 @@ defmodule Code do
|
||||
|
||||
* `:line` - the line on which the script starts
|
||||
|
||||
Additionally, you may also pass an environment as second argument,
|
||||
so the evaluation happens within that environment. However, if the evaluated
|
||||
code requires or compiles another file, the environment given to this function
|
||||
will not apply to said files.
|
||||
Additionally, the following scope values can be configured:
|
||||
|
||||
Returns a tuple of the form `{value, binding}`, where `value` is the value
|
||||
returned from evaluating `string`. If an error occurs while evaluating
|
||||
`string` an exception will be raised.
|
||||
* `:aliases` - a list of tuples with the alias and its target
|
||||
|
||||
`binding` is a list with all variable bindings after evaluating `string`.
|
||||
The binding keys are usually atoms, but they may be a tuple for variables
|
||||
defined in a different context.
|
||||
* `:requires` - a list of modules required
|
||||
|
||||
* `:functions` - a list of tuples where the first element is a module
|
||||
and the second a list of imported function names and arity; the list
|
||||
of function names and arity must be sorted
|
||||
|
||||
* `:macros` - a list of tuples where the first element is a module
|
||||
and the second a list of imported macro names and arity; the list
|
||||
of function names and arity must be sorted
|
||||
|
||||
Note that setting any of the values above overrides Elixir's default
|
||||
values. For example, setting `:requires` to `[]` will no longer
|
||||
automatically require the `Kernel` module. In the same way setting
|
||||
`:macros` will no longer auto-import `Kernel` macros like `Kernel.if/2`,
|
||||
`Kernel.SpecialForms.case/2`, and so on.
|
||||
|
||||
Returns a tuple of the form `{value, binding}`,
|
||||
where `value` is the value returned from evaluating `string`.
|
||||
If an error occurs while evaluating `string` an exception will be raised.
|
||||
|
||||
`binding` is a list with all variable bindings
|
||||
after evaluating `string`. The binding keys are usually atoms, but they
|
||||
may be a tuple for variables defined in a different context.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {result, binding} = Code.eval_string("a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
|
||||
iex> result
|
||||
3
|
||||
iex> Enum.sort(binding)
|
||||
[a: 1, b: 2]
|
||||
iex> Code.eval_string("a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
|
||||
{3, [a: 1, b: 2]}
|
||||
|
||||
iex> {result, binding} = Code.eval_string("c = a + b", [a: 1, b: 2], __ENV__)
|
||||
iex> result
|
||||
3
|
||||
iex> Enum.sort(binding)
|
||||
[a: 1, b: 2, c: 3]
|
||||
iex> Code.eval_string("c = a + b", [a: 1, b: 2], __ENV__)
|
||||
{3, [a: 1, b: 2, c: 3]}
|
||||
|
||||
iex> {result, binding} = Code.eval_string("a = a + b", [a: 1, b: 2])
|
||||
iex> result
|
||||
3
|
||||
iex> Enum.sort(binding)
|
||||
[a: 3, b: 2]
|
||||
iex> Code.eval_string("a = a + b", [a: 1, b: 2])
|
||||
{3, [a: 3, b: 2]}
|
||||
|
||||
For convenience, you can pass `__ENV__/0` as the `opts` argument and
|
||||
all imports, requires and aliases defined in the current environment
|
||||
will be automatically carried over:
|
||||
|
||||
iex> {result, binding} = Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
|
||||
iex> result
|
||||
3
|
||||
iex> Enum.sort(binding)
|
||||
[a: 1, b: 2]
|
||||
iex> Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
|
||||
{3, [a: 1, b: 2]}
|
||||
|
||||
"""
|
||||
@spec eval_string(List.Chars.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
|
||||
def eval_string(string, binding \\ [], opts \\ [])
|
||||
|
||||
def eval_string(string, binding, %Macro.Env{} = env) do
|
||||
validated_eval_string(string, binding, env)
|
||||
eval_string_with_error_handling(string, binding, Map.to_list(env))
|
||||
end
|
||||
|
||||
def eval_string(string, binding, opts) when is_list(opts) do
|
||||
validated_eval_string(string, binding, opts)
|
||||
validate_eval_opts(opts)
|
||||
eval_string_with_error_handling(string, binding, opts)
|
||||
end
|
||||
|
||||
defp validated_eval_string(string, binding, opts_or_env) do
|
||||
%{line: line, file: file} = env = :elixir.env_for_eval(opts_or_env)
|
||||
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, 1, file, [])
|
||||
{value, binding, _env} = :elixir.eval_forms(forms, binding, env)
|
||||
{value, binding}
|
||||
@@ -421,8 +358,9 @@ defmodule Code do
|
||||
|
||||
* `:line_length` - the line length to aim for when formatting
|
||||
the document. Defaults to 98. Note this value is used as
|
||||
guideline but there are situations where it is not enforced.
|
||||
See the "Line length" section below for more information
|
||||
reference but it is not enforced by the formatter as sometimes
|
||||
user intervention is required. See "Running the formatter"
|
||||
section
|
||||
|
||||
* `:locals_without_parens` - a keyword list of name and arity
|
||||
pairs that should be kept without parens whenever possible.
|
||||
@@ -430,12 +368,17 @@ defmodule Code do
|
||||
that name. The formatter already includes a list of functions
|
||||
and this option augments this list.
|
||||
|
||||
* `:rename_deprecated_at` - rename all known deprecated functions
|
||||
at the given version to their non-deprecated equivalent. It
|
||||
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`
|
||||
inline usages of `do: ...`, `else: ...` and friends into `do/end`
|
||||
blocks. Defaults to `false`. Note that 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.
|
||||
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
|
||||
|
||||
@@ -443,6 +386,8 @@ defmodule Code do
|
||||
|
||||
First, the formatter never changes the semantics of the code by
|
||||
default. This means the input AST and the output AST are equivalent.
|
||||
Optional behaviour, such as `:rename_deprecated_at`, is allowed to
|
||||
break this guarantee.
|
||||
|
||||
The second principle is to provide as little configuration as possible.
|
||||
This eases the formatter adoption by removing contention points while
|
||||
@@ -468,8 +413,29 @@ defmodule Code do
|
||||
do not recommend to run the formatter blindly in an existing codebase.
|
||||
Instead you should format and sanity check each formatted file.
|
||||
|
||||
For example, the formatter may break a long function definition over
|
||||
multiple clauses:
|
||||
Let's see some examples. The code below:
|
||||
|
||||
"this is a very long string ... #{inspect(some_value)}"
|
||||
|
||||
may be formatted as:
|
||||
|
||||
"this is a very long string ... #{
|
||||
inspect(some_value)
|
||||
}"
|
||||
|
||||
This happens because the only place the formatter can introduce a
|
||||
new line without changing the code semantics is in the interpolation.
|
||||
In those scenarios, we recommend developers to directly adjust the
|
||||
code. Here we can use the binary concatenation operator `<>/2`:
|
||||
|
||||
"this is a very long string " <>
|
||||
"... #{inspect(some_value)}"
|
||||
|
||||
The string concatenation makes the code fit on a single line and also
|
||||
gives more options to the formatter.
|
||||
|
||||
A similar example is when the formatter breaks a function definition
|
||||
over multiple clauses:
|
||||
|
||||
def my_function(
|
||||
%User{name: name, age: age, ...},
|
||||
@@ -534,42 +500,6 @@ defmodule Code do
|
||||
we describe in the next sections the cases where the formatter keeps the
|
||||
user encoding and how to control multiline expressions.
|
||||
|
||||
## Line length
|
||||
|
||||
Another point about the formatter is that the `:line_length` configuration
|
||||
is a guideline. In many cases, it is not possible for the formatter to break
|
||||
your code apart, which means it will go over the line length. For example,
|
||||
if you have a long string:
|
||||
|
||||
"this is a very long string that will go over the line length"
|
||||
|
||||
The formatter doesn't know how to break it apart without changing the
|
||||
code underlying syntax representation, so it is up to you to step in:
|
||||
|
||||
"this is a very long string " <>
|
||||
"that will go over the line length"
|
||||
|
||||
The string concatenation makes the code fit on a single line and also
|
||||
gives more options to the formatter.
|
||||
|
||||
This may also appear in do/end blocks, where the `do` keyword (or `->`)
|
||||
may go over the line length because there is no opportunity for the
|
||||
formatter to introduce a line break in a readable way. For example,
|
||||
if you do:
|
||||
|
||||
case very_long_expression() do
|
||||
end
|
||||
|
||||
And only the `do` keyword is above the line length, Elixir **will not**
|
||||
emit this:
|
||||
|
||||
case very_long_expression()
|
||||
do
|
||||
end
|
||||
|
||||
So it prefers to not touch the line at all and leave `do` above the
|
||||
line limit.
|
||||
|
||||
## Keeping user's formatting
|
||||
|
||||
The formatter respects the input format in some cases. Those are
|
||||
@@ -588,7 +518,7 @@ defmodule Code do
|
||||
line before and after and 2) empty lines are always squeezed
|
||||
together into a single empty line
|
||||
|
||||
* The choice between `:do` keyword and `do`-`end` blocks is left
|
||||
* The choice between `:do` keyword and `do/end` blocks is left
|
||||
to the user
|
||||
|
||||
* Lists, tuples, bitstrings, maps, structs and function calls will be
|
||||
@@ -637,7 +567,7 @@ defmodule Code do
|
||||
Elixir has two syntaxes for function calls. With parens and no parens.
|
||||
By default, Elixir will add parens to all calls except for:
|
||||
|
||||
1. calls that have `do`-`end` blocks
|
||||
1. calls that have do/end blocks
|
||||
2. local calls without parens where the name and arity of the local
|
||||
call is also listed under `:locals_without_parens` (except for
|
||||
calls with arity 0, where the compiler always require parens)
|
||||
@@ -716,34 +646,13 @@ defmodule Code do
|
||||
are considered equivalent (the nesting is discarded alongside most of
|
||||
user formatting). In such cases, the code formatter will always format to
|
||||
the latter.
|
||||
|
||||
## Newlines
|
||||
|
||||
The formatter converts all newlines in code from `\r\n` to `\n`.
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec format_string!(binary, keyword) :: iodata
|
||||
def format_string!(string, opts \\ []) when is_binary(string) and is_list(opts) do
|
||||
line_length = Keyword.get(opts, :line_length, 98)
|
||||
|
||||
to_quoted_opts =
|
||||
[
|
||||
unescape: false,
|
||||
warn_on_unnecessary_quotes: false,
|
||||
literal_encoder: &{:ok, {:__block__, &2, [&1]}},
|
||||
token_metadata: true
|
||||
] ++ opts
|
||||
|
||||
{forms, comments} = string_to_quoted_with_comments!(string, to_quoted_opts)
|
||||
|
||||
to_algebra_opts =
|
||||
[
|
||||
comments: comments
|
||||
] ++ opts
|
||||
|
||||
doc = Code.Formatter.to_algebra(forms, to_algebra_opts)
|
||||
|
||||
Inspect.Algebra.format(doc, line_length)
|
||||
algebra = Code.Formatter.to_algebra!(string, opts)
|
||||
Inspect.Algebra.format(algebra, line_length)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -768,51 +677,84 @@ 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 `opts`.
|
||||
See `eval_string/3` for a description of `binding` and options.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> contents = quote(do: var!(a) + var!(b))
|
||||
iex> {result, binding} = Code.eval_quoted(contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
|
||||
iex> result
|
||||
3
|
||||
iex> Enum.sort(binding)
|
||||
[a: 1, b: 2]
|
||||
iex> Code.eval_quoted(contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
|
||||
{3, [a: 1, b: 2]}
|
||||
|
||||
For convenience, you can pass `__ENV__/0` as the `opts` argument and
|
||||
all options will be automatically extracted from the current environment:
|
||||
|
||||
iex> contents = quote(do: var!(a) + var!(b))
|
||||
iex> {result, binding} = Code.eval_quoted(contents, [a: 1, b: 2], __ENV__)
|
||||
iex> result
|
||||
3
|
||||
iex> Enum.sort(binding)
|
||||
[a: 1, b: 2]
|
||||
iex> Code.eval_quoted(contents, [a: 1, b: 2], __ENV__)
|
||||
{3, [a: 1, b: 2]}
|
||||
|
||||
"""
|
||||
@spec eval_quoted(Macro.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
|
||||
def eval_quoted(quoted, binding \\ [], opts \\ [])
|
||||
|
||||
def eval_quoted(quoted, binding, %Macro.Env{} = env) do
|
||||
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, :elixir.env_for_eval(env))
|
||||
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, Map.to_list(env))
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
def eval_quoted(quoted, binding, opts) when is_list(opts) do
|
||||
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, :elixir.env_for_eval(opts))
|
||||
validate_eval_opts(opts)
|
||||
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, opts)
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
defp validate_eval_opts(opts) do
|
||||
if f = opts[:functions], do: validate_imports(:functions, f)
|
||||
if m = opts[:macros], do: validate_imports(:macros, m)
|
||||
if a = opts[:aliases], do: validate_aliases(:aliases, a)
|
||||
if r = opts[:requires], do: validate_requires(:requires, r)
|
||||
end
|
||||
|
||||
defp validate_requires(kind, requires) do
|
||||
valid = is_list(requires) and Enum.all?(requires, &is_atom(&1))
|
||||
|
||||
unless valid do
|
||||
raise ArgumentError, "expected :#{kind} option given to eval in the format: [module]"
|
||||
end
|
||||
end
|
||||
|
||||
defp validate_aliases(kind, aliases) do
|
||||
valid = is_list(aliases) and Enum.all?(aliases, fn {k, v} -> is_atom(k) and is_atom(v) end)
|
||||
|
||||
unless valid do
|
||||
raise ArgumentError,
|
||||
"expected :#{kind} option given to eval in the format: [{module, module}]"
|
||||
end
|
||||
end
|
||||
|
||||
defp validate_imports(kind, imports) do
|
||||
valid =
|
||||
is_list(imports) and
|
||||
Enum.all?(imports, fn {k, v} ->
|
||||
is_atom(k) and is_list(v) and
|
||||
Enum.all?(v, fn {name, arity} -> is_atom(name) and is_integer(arity) end)
|
||||
end)
|
||||
|
||||
unless valid do
|
||||
raise ArgumentError,
|
||||
"expected :#{kind} option given to eval in the format: [{module, [{name, arity}]}]"
|
||||
end
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Converts the given string to its quoted form.
|
||||
|
||||
Returns `{:ok, quoted_form}` if it succeeds,
|
||||
`{:error, {meta, message_info, token}}` otherwise.
|
||||
`{:error, {line, error, token}}` otherwise.
|
||||
|
||||
## Options
|
||||
|
||||
* `:file` - the filename to be reported in case of parsing errors.
|
||||
Defaults to `"nofile"`.
|
||||
Defaults to "nofile".
|
||||
|
||||
* `:line` - the starting line of the string being parsed.
|
||||
Defaults to 1.
|
||||
@@ -823,13 +765,6 @@ defmodule Code do
|
||||
* `:columns` - when `true`, attach a `:column` key to the quoted
|
||||
metadata. Defaults to `false`.
|
||||
|
||||
* `:unescape` (since v1.10.0) - when `false`, preserves escaped sequences.
|
||||
For example, `"null byte\\t\\x00"` will be kept as is instead of being
|
||||
converted to a bitstring literal. Note if you set this option to false, the
|
||||
resulting AST is no longer valid, but it can be useful to analyze/transform
|
||||
source code, typically in in combination with `quoted_to_algebra/2`.
|
||||
Defaults to `true`.
|
||||
|
||||
* `:existing_atoms_only` - when `true`, raises an error
|
||||
when non-existing atoms are found by the tokenizer.
|
||||
Defaults to `false`.
|
||||
@@ -894,7 +829,7 @@ defmodule Code do
|
||||
|
||||
"""
|
||||
@spec string_to_quoted(List.Chars.t(), keyword) ::
|
||||
{:ok, Macro.t()} | {:error, {location :: keyword, binary | {binary, binary}, binary}}
|
||||
{:ok, Macro.t()} | {:error, {location :: keyword, term, term}}
|
||||
def string_to_quoted(string, opts \\ []) when is_list(opts) do
|
||||
file = Keyword.get(opts, :file, "nofile")
|
||||
line = Keyword.get(opts, :line, 1)
|
||||
@@ -912,7 +847,7 @@ defmodule Code do
|
||||
@doc """
|
||||
Converts the given string to its quoted form.
|
||||
|
||||
It returns the AST if it succeeds,
|
||||
It returns the ast if it succeeds,
|
||||
raises an exception otherwise. The exception is a `TokenMissingError`
|
||||
in case a token is missing (usually because the expression is incomplete),
|
||||
`SyntaxError` otherwise.
|
||||
@@ -928,188 +863,7 @@ defmodule Code do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given string to its quoted form and a list of comments.
|
||||
|
||||
This function is useful when performing textual changes to the source code,
|
||||
while preserving information like comments and literals position.
|
||||
|
||||
Returns `{:ok, quoted_form, comments}` if it succeeds,
|
||||
`{:error, {line, error, token}}` otherwise.
|
||||
|
||||
Comments are maps with the following fields:
|
||||
|
||||
* `:line` - The line number the source code
|
||||
|
||||
* `:text` - The full text of the comment, including the leading `#`
|
||||
|
||||
* `:previous_eol_count` - How many end of lines there are between the comment and the previous AST node or comment
|
||||
|
||||
* `:next_eol_count` - How many end of lines there are between the comment and the next AST node or comment
|
||||
|
||||
Check `string_to_quoted/2` for options information.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.string_to_quoted_with_comments("\""
|
||||
...> :foo
|
||||
...>
|
||||
...> # Hello, world!
|
||||
...>
|
||||
...>
|
||||
...> # Some more comments!
|
||||
...> "\"")
|
||||
{:ok, :foo, [
|
||||
%{line: 3, column: 1, previous_eol_count: 2, next_eol_count: 3, text: "\# Hello, world!"},
|
||||
%{line: 6, column: 1, previous_eol_count: 3, next_eol_count: 1, text: "\# Some more comments!"},
|
||||
]}
|
||||
|
||||
iex> Code.string_to_quoted_with_comments(":foo # :bar")
|
||||
{:ok, :foo, [
|
||||
%{line: 1, column: 6, previous_eol_count: 0, next_eol_count: 0, text: "\# :bar"}
|
||||
]}
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec string_to_quoted_with_comments(List.Chars.t(), keyword) ::
|
||||
{:ok, Macro.t(), list(map())} | {:error, {location :: keyword, term, term}}
|
||||
def string_to_quoted_with_comments(string, opts \\ []) when is_list(opts) do
|
||||
charlist = to_charlist(string)
|
||||
file = Keyword.get(opts, :file, "nofile")
|
||||
line = Keyword.get(opts, :line, 1)
|
||||
column = Keyword.get(opts, :column, 1)
|
||||
|
||||
Process.put(:code_formatter_comments, [])
|
||||
opts = [preserve_comments: &preserve_comments/5] ++ opts
|
||||
|
||||
with {:ok, tokens} <- :elixir.string_to_tokens(charlist, line, column, file, opts),
|
||||
{:ok, forms} <- :elixir.tokens_to_quoted(tokens, file, opts) do
|
||||
comments = Enum.reverse(Process.get(:code_formatter_comments))
|
||||
{:ok, forms, comments}
|
||||
end
|
||||
after
|
||||
Process.delete(:code_formatter_comments)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given string to its quoted form and a list of comments.
|
||||
|
||||
Returns the AST and a list of comments if it succeeds, raises an exception
|
||||
otherwise. The exception is a `TokenMissingError` in case a token is missing
|
||||
(usually because the expression is incomplete), `SyntaxError` otherwise.
|
||||
|
||||
Check `string_to_quoted/2` for options information.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec string_to_quoted_with_comments!(List.Chars.t(), keyword) :: {Macro.t(), list(map())}
|
||||
def string_to_quoted_with_comments!(string, opts \\ []) do
|
||||
charlist = to_charlist(string)
|
||||
|
||||
case string_to_quoted_with_comments(charlist, opts) do
|
||||
{:ok, forms, comments} ->
|
||||
{forms, comments}
|
||||
|
||||
{:error, {location, error, token}} ->
|
||||
:elixir_errors.parse_error(
|
||||
location,
|
||||
Keyword.get(opts, :file, "nofile"),
|
||||
error,
|
||||
token,
|
||||
{charlist, Keyword.get(opts, :line, 1), Keyword.get(opts, :column, 1)}
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp preserve_comments(line, column, tokens, comment, rest) do
|
||||
comments = Process.get(:code_formatter_comments)
|
||||
|
||||
comment = %{
|
||||
line: line,
|
||||
column: column,
|
||||
previous_eol_count: previous_eol_count(tokens),
|
||||
next_eol_count: next_eol_count(rest, 0),
|
||||
text: List.to_string(comment)
|
||||
}
|
||||
|
||||
Process.put(:code_formatter_comments, [comment | comments])
|
||||
end
|
||||
|
||||
defp next_eol_count('\s' ++ rest, count), do: next_eol_count(rest, count)
|
||||
defp next_eol_count('\t' ++ rest, count), do: next_eol_count(rest, count)
|
||||
defp next_eol_count('\n' ++ rest, count), do: next_eol_count(rest, count + 1)
|
||||
defp next_eol_count('\r\n' ++ rest, count), do: next_eol_count(rest, count + 1)
|
||||
defp next_eol_count(_, count), do: count
|
||||
|
||||
defp previous_eol_count([{token, {_, _, count}} | _])
|
||||
when token in [:eol, :",", :";"] and count > 0 do
|
||||
count
|
||||
end
|
||||
|
||||
defp previous_eol_count([]), do: 1
|
||||
defp previous_eol_count(_), do: 0
|
||||
|
||||
@doc ~S"""
|
||||
Converts a quoted expression to an algebra document using Elixir's formatter rules.
|
||||
|
||||
The algebra document can be converted into a string by calling:
|
||||
|
||||
doc
|
||||
|> Inspect.Algebra.format(:infinity)
|
||||
|> IO.iodata_to_binary()
|
||||
|
||||
For a high-level function that does the same, see `Macro.to_string/1`.
|
||||
|
||||
## Formatting considerations
|
||||
|
||||
The Elixir AST does not contain metadata for literals like strings, lists, or
|
||||
tuples with two elements, which means that the produced algebra document will
|
||||
not respect all of the user preferences and comments may be misplaced.
|
||||
To get better results, you can use the `:token_metadata`, `:unescape` and
|
||||
`:literal_encoder` options to `string_to_quoted/2` to provide additional
|
||||
information to the formatter:
|
||||
|
||||
[
|
||||
literal_encoder: &{:ok, {:__block__, &2, [&1]}},
|
||||
token_metadata: true,
|
||||
unescape: false
|
||||
]
|
||||
|
||||
This will produce an AST that contains information such as `do` blocks start
|
||||
and end lines or sigil delimiters, and by wrapping literals in blocks they can
|
||||
now hold metadata like line number, string delimiter and escaped sequences, or
|
||||
integer formatting (such as `0x2a` instead of `47`). However, **note this AST is
|
||||
not valid**. If you evaluate it, it won't have the same semantics as the regular
|
||||
Elixir AST due to the `:unescape` and `:literal_encoder` options. However,
|
||||
those options are useful if you're doing source code manipulation, where it's
|
||||
important to preserve user choices and comments placing.
|
||||
|
||||
## Options
|
||||
|
||||
* `:comments` - the list of comments associated with the quoted expression.
|
||||
Defaults to `[]`. It is recommended that both `:token_metadata` and
|
||||
`:literal_encoder` options are given to `string_to_quoted_with_comments/2`
|
||||
in order to get proper placement for comments
|
||||
|
||||
* `:escape` - when `true`, escaped sequences like `\n` will be escaped into
|
||||
`\\n`. If the `:unescape` option was set to `false` when using
|
||||
`string_to_quoted/2`, setting this option to `false` will prevent it from
|
||||
escaping the sequences twice. Defaults to `true`.
|
||||
|
||||
* `:locals_without_parens` - a keyword list of name and arity
|
||||
pairs that should be kept without parens whenever possible.
|
||||
The arity may be the atom `:*`, which implies all arities of
|
||||
that name. The formatter already includes a list of functions
|
||||
and this option augments this list.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec quoted_to_algebra(Macro.t(), keyword) :: Inspect.Algebra.t()
|
||||
def quoted_to_algebra(quoted, opts \\ []) do
|
||||
quoted
|
||||
|> Code.Normalizer.normalize(opts)
|
||||
|> Code.Formatter.to_algebra(opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates the given file.
|
||||
Evals the given file.
|
||||
|
||||
Accepts `relative_to` as an argument to tell where the file is located.
|
||||
|
||||
@@ -1128,14 +882,9 @@ defmodule Code do
|
||||
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 =
|
||||
Module.ParallelChecker.verify(fn ->
|
||||
:elixir_compiler.file(file, fn _, _ -> :ok end)
|
||||
end)
|
||||
|
||||
loaded = :elixir_compiler.file(file, fn _, _ -> :ok end)
|
||||
:elixir_code_server.cast({:required, file})
|
||||
loaded
|
||||
verify_loaded(loaded)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1179,13 +928,9 @@ defmodule Code do
|
||||
nil
|
||||
|
||||
:proceed ->
|
||||
loaded =
|
||||
Module.ParallelChecker.verify(fn ->
|
||||
:elixir_compiler.file(file, fn _, _ -> :ok end)
|
||||
end)
|
||||
|
||||
loaded = :elixir_compiler.file(file, fn _, _ -> :ok end)
|
||||
:elixir_code_server.cast({:required, file})
|
||||
loaded
|
||||
verify_loaded(loaded)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1211,8 +956,8 @@ defmodule Code do
|
||||
@doc """
|
||||
Stores all given compilation options.
|
||||
|
||||
To store individual options and for a description of all
|
||||
options, see `put_compiler_option/2`.
|
||||
To store individual options, see `put_compiler_option/2`.
|
||||
For a description of all options, see `put_compiler_option/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1390,7 +1135,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, &elem(&1, 0))
|
||||
Enum.map(loaded, fn {module, _map, binary} -> {module, binary} end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1404,7 +1149,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, &elem(&1, 0))
|
||||
Enum.map(loaded, fn {module, _map, binary} -> {module, binary} end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1424,9 +1169,8 @@ defmodule Code do
|
||||
@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
|
||||
Module.ParallelChecker.verify(fn ->
|
||||
:elixir_compiler.file(find_file(file, relative_to), fn _, _ -> :ok end)
|
||||
end)
|
||||
loaded = :elixir_compiler.file(find_file(file, relative_to), fn _, _ -> :ok end)
|
||||
verify_loaded(loaded)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1438,7 +1182,39 @@ defmodule Code do
|
||||
If it succeeds in loading the module, it returns `{:module, module}`.
|
||||
If not, returns `{:error, reason}` with the error reason.
|
||||
|
||||
See the module documentation for more information on code loading.
|
||||
## Code loading on the Erlang VM
|
||||
|
||||
Erlang has two modes to load code: interactive and embedded.
|
||||
|
||||
By default, the Erlang VM runs in interactive mode, where modules
|
||||
are loaded as needed. In embedded mode the opposite happens, as all
|
||||
modules need to be loaded upfront or explicitly.
|
||||
|
||||
Therefore, this function is used to check if a module is loaded
|
||||
before using it and allows one to react accordingly. For example, the `URI`
|
||||
module uses this function to check if a specific parser exists for a given
|
||||
URI scheme.
|
||||
|
||||
## `ensure_compiled/1`
|
||||
|
||||
Elixir also contains an `ensure_compiled/1` function that is a
|
||||
superset of `ensure_loaded/1`.
|
||||
|
||||
Since Elixir's compilation happens in parallel, in some situations
|
||||
you may need to use a module that was not yet compiled, therefore
|
||||
it can't even be loaded.
|
||||
|
||||
When invoked, `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.
|
||||
|
||||
`ensure_compiled/1` does not apply to dependencies, as dependencies
|
||||
must be compiled upfront.
|
||||
|
||||
In most cases, `ensure_loaded/1` is enough. `ensure_compiled/1`
|
||||
must be used in rare cases, usually involving macros that need to
|
||||
invoke a module for callback information.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1473,62 +1249,14 @@ defmodule Code do
|
||||
match?({:module, ^module}, ensure_loaded(module))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Same as `ensure_loaded/1` but raises if the module cannot be loaded.
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec ensure_loaded!(module) :: module
|
||||
def ensure_loaded!(module) do
|
||||
case ensure_loaded(module) do
|
||||
{:module, module} ->
|
||||
module
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError,
|
||||
"could not load module #{inspect(module)} due to reason #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Similar to `ensure_compiled!/1` but indicates you can continue without said module.
|
||||
|
||||
While `ensure_compiled!/1` indicates to the Elixir compiler you can
|
||||
only continue when said module is available, this function indicates
|
||||
you may continue compilation without said module.
|
||||
|
||||
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.
|
||||
|
||||
Therefore, if you can only continue if the module is available, use
|
||||
`ensure_compiled!/1` instead. In particular, do not do this:
|
||||
|
||||
case Code.ensure_compiled(module) do
|
||||
{:module, _} -> module
|
||||
{:error, _} -> raise ...
|
||||
end
|
||||
|
||||
See the module documentation for more information on code loading.
|
||||
"""
|
||||
@spec ensure_compiled(module) ::
|
||||
{:module, module}
|
||||
| {:error, :embedded | :badfile | :nofile | :on_load_failure | :unavailable}
|
||||
def ensure_compiled(module) when is_atom(module) do
|
||||
ensure_compiled(module, :soft)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the given module is compiled and loaded.
|
||||
|
||||
If the module is already loaded, it works as no-op. If the module was
|
||||
not compiled yet, `ensure_compiled!/1` halts the compilation of the caller
|
||||
until the module given to `ensure_compiled!/1` becomes available or
|
||||
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 or is in a deadlock, an error
|
||||
is raised.
|
||||
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
|
||||
@@ -1536,26 +1264,25 @@ defmodule Code do
|
||||
if the other is compiled. This returns a specific unavailable error code,
|
||||
where we cannot successfully verify a module is available or not.
|
||||
|
||||
See the module documentation for more information on code loading.
|
||||
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`.
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec ensure_compiled!(module) :: module
|
||||
def ensure_compiled!(module) do
|
||||
case ensure_compiled(module, :hard) do
|
||||
{:module, module} ->
|
||||
module
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError,
|
||||
"could not load module #{inspect(module)} due to reason #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp ensure_compiled(module, mode) do
|
||||
@spec ensure_compiled(module) ::
|
||||
{:module, module}
|
||||
| {:error, :embedded | :badfile | :nofile | :on_load_failure | :unavailable}
|
||||
def ensure_compiled(module) when is_atom(module) do
|
||||
case :code.ensure_loaded(module) do
|
||||
{:error, :nofile} = error ->
|
||||
if can_await_module_compilation?() do
|
||||
case Kernel.ErrorHandler.ensure_compiled(module, :module, mode) do
|
||||
case Kernel.ErrorHandler.ensure_compiled(module, :module, :soft) do
|
||||
:found -> {:module, module}
|
||||
:deadlock -> {:error, :unavailable}
|
||||
:not_found -> {:error, :nofile}
|
||||
@@ -1599,7 +1326,7 @@ defmodule Code do
|
||||
file.
|
||||
|
||||
It returns the term stored in the documentation chunk in the format defined by
|
||||
[EEP 48](https://www.erlang.org/eeps/eep-0048.html) or `{:error, reason}` if
|
||||
[EEP 48](http://erlang.org/eep/eeps/eep-0048.html) or `{:error, reason}` if
|
||||
the chunk is not available.
|
||||
|
||||
## Examples
|
||||
@@ -1702,7 +1429,7 @@ defmodule Code do
|
||||
@doc ~S"""
|
||||
Deprecated function to retrieve old documentation format.
|
||||
|
||||
Elixir v1.7 adopts [EEP 48](https://www.erlang.org/eeps/eep-0048.html)
|
||||
Elixir v1.7 adopts [EEP 48](http://erlang.org/eep/eeps/eep-0048.html)
|
||||
which is a new documentation format meant to be shared across all
|
||||
BEAM languages. The old format, used by `Code.get_docs/2`, is no
|
||||
longer available, and therefore this function always returns `nil`.
|
||||
@@ -1733,4 +1460,10 @@ 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
|
||||
|
||||
+293
-246
@@ -12,11 +12,8 @@ defmodule Code.Formatter do
|
||||
@empty empty()
|
||||
@ampersand_prec Code.Identifier.unary_op(:&) |> elem(1)
|
||||
|
||||
# Operators that are composed of multiple binary operators
|
||||
@multi_binary_operators [:"..//"]
|
||||
|
||||
# Operators that do not have space between operands
|
||||
@no_space_binary_operators [:.., :"//"]
|
||||
@no_space_binary_operators [:..]
|
||||
|
||||
# Operators that do not have newline between operands (as well as => and keywords)
|
||||
@no_newline_binary_operators [:\\, :in]
|
||||
@@ -142,90 +139,183 @@ defmodule Code.Formatter do
|
||||
@do_end_keywords [:rescue, :catch, :else, :after]
|
||||
|
||||
@doc """
|
||||
Converts the quoted expression into an algebra document.
|
||||
Checks if two strings are equivalent.
|
||||
"""
|
||||
def to_algebra(quoted, opts \\ []) do
|
||||
comments = Keyword.get(opts, :comments, [])
|
||||
def equivalent(string1, string2) when is_binary(string1) and is_binary(string2) do
|
||||
quoted1 = :elixir.string_to_quoted!(to_charlist(string1), 1, 1, "nofile", [])
|
||||
quoted2 = :elixir.string_to_quoted!(to_charlist(string2), 1, 1, "nofile", [])
|
||||
|
||||
state =
|
||||
comments
|
||||
|> Enum.map(&format_comment/1)
|
||||
|> gather_comments()
|
||||
|> state(opts)
|
||||
case not_equivalent(quoted1, quoted2) do
|
||||
{left, right} -> {:error, left, right}
|
||||
nil -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
{doc, _} = block_to_algebra(quoted, @min_line, @max_line, state)
|
||||
doc
|
||||
defp not_equivalent({:__block__, _, [left]}, right) do
|
||||
not_equivalent(left, right)
|
||||
end
|
||||
|
||||
defp not_equivalent(left, {:__block__, _, [right]}) do
|
||||
not_equivalent(left, right)
|
||||
end
|
||||
|
||||
defp not_equivalent({:__block__, _, []}, nil) do
|
||||
nil
|
||||
end
|
||||
|
||||
defp not_equivalent(nil, {:__block__, _, []}) do
|
||||
nil
|
||||
end
|
||||
|
||||
defp not_equivalent([left | lefties], [right | righties]) do
|
||||
not_equivalent(left, right) || not_equivalent(lefties, righties)
|
||||
end
|
||||
|
||||
defp not_equivalent({left_name, _, left_args}, {right_name, _, right_args}) do
|
||||
not_equivalent(left_name, right_name) || not_equivalent(left_args, right_args)
|
||||
end
|
||||
|
||||
defp not_equivalent({left1, left2}, {right1, right2}) do
|
||||
not_equivalent(left1, right1) || not_equivalent(left2, right2)
|
||||
end
|
||||
|
||||
defp not_equivalent(side, side) do
|
||||
nil
|
||||
end
|
||||
|
||||
defp not_equivalent(left, right) do
|
||||
{left, right}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lists all default locals without parens.
|
||||
Converts `string` to an algebra document.
|
||||
|
||||
Returns `{:ok, doc}` or `{:error, parser_error}`.
|
||||
|
||||
See `Code.format_string!/2` for the list of options.
|
||||
"""
|
||||
def locals_without_parens do
|
||||
@locals_without_parens
|
||||
def to_algebra(string, opts \\ []) when is_binary(string) and is_list(opts) do
|
||||
file = Keyword.get(opts, :file, "nofile")
|
||||
line = Keyword.get(opts, :line, 1)
|
||||
charlist = String.to_charlist(string)
|
||||
|
||||
Process.put(:code_formatter_comments, [])
|
||||
|
||||
tokenizer_options = [
|
||||
unescape: false,
|
||||
preserve_comments: &preserve_comments/5,
|
||||
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, 1, file, tokenizer_options),
|
||||
{:ok, forms} <- :elixir.tokens_to_quoted(tokens, file, parser_options) do
|
||||
state =
|
||||
Process.get(:code_formatter_comments)
|
||||
|> Enum.reverse()
|
||||
|> gather_comments()
|
||||
|> state(opts)
|
||||
|
||||
{doc, _} = block_to_algebra(forms, @min_line, @max_line, state)
|
||||
{:ok, doc}
|
||||
end
|
||||
after
|
||||
Process.delete(:code_formatter_comments)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if a function is a local without parens.
|
||||
Converts `string` to an algebra document.
|
||||
|
||||
Raises if the `string` cannot be parsed.
|
||||
|
||||
See `Code.format_string!/2` for the list of options.
|
||||
"""
|
||||
def local_without_parens?(fun, arity, locals_without_parens) do
|
||||
arity > 0 and
|
||||
Enum.any?(locals_without_parens, fn {key, val} ->
|
||||
key == fun and (val == :* or val == arity)
|
||||
end)
|
||||
def to_algebra!(string, opts \\ []) do
|
||||
case to_algebra(string, opts) do
|
||||
{:ok, doc} ->
|
||||
doc
|
||||
|
||||
{:error, {location, error, token}} ->
|
||||
:elixir_errors.parse_error(location, Keyword.get(opts, :file, "nofile"), error, token)
|
||||
end
|
||||
end
|
||||
|
||||
defp state(comments, opts) do
|
||||
force_do_end_blocks = Keyword.get(opts, :force_do_end_blocks, false)
|
||||
locals_without_parens = Keyword.get(opts, :locals_without_parens, [])
|
||||
sigils = Keyword.get(opts, :sigils, [])
|
||||
|
||||
sigils =
|
||||
Map.new(sigils, fn {key, value} ->
|
||||
with true <- is_atom(key) and is_function(value, 2),
|
||||
[char] <- Atom.to_charlist(key),
|
||||
true <- char in ?A..?Z do
|
||||
{char, value}
|
||||
else
|
||||
_ ->
|
||||
rename_deprecated_at =
|
||||
if version = opts[:rename_deprecated_at] do
|
||||
case Version.parse(version) do
|
||||
{:ok, parsed} ->
|
||||
parsed
|
||||
|
||||
:error ->
|
||||
raise ArgumentError,
|
||||
":sigils must be a keyword list with a single uppercased letter as key and an " <>
|
||||
"anonymous function expecting two arguments as value, got: #{inspect(sigils)}"
|
||||
"invalid version #{inspect(version)} given to :rename_deprecated_at"
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
locals_without_parens =
|
||||
Keyword.get(opts, :locals_without_parens, []) ++ @locals_without_parens
|
||||
|
||||
%{
|
||||
force_do_end_blocks: force_do_end_blocks,
|
||||
locals_without_parens: locals_without_parens ++ locals_without_parens(),
|
||||
locals_without_parens: locals_without_parens,
|
||||
operand_nesting: 2,
|
||||
skip_eol: false,
|
||||
comments: comments,
|
||||
sigils: sigils
|
||||
rename_deprecated_at: rename_deprecated_at,
|
||||
comments: comments
|
||||
}
|
||||
end
|
||||
|
||||
defp format_comment(%{text: text} = comment) do
|
||||
%{comment | text: format_comment_text(text)}
|
||||
# Code comment handling
|
||||
|
||||
defp preserve_comments(line, _column, tokens, comment, rest) do
|
||||
comments = Process.get(:code_formatter_comments)
|
||||
comment = {line, {previous_eol(tokens), next_eol(rest, 0)}, format_comment(comment, [])}
|
||||
Process.put(:code_formatter_comments, [comment | comments])
|
||||
end
|
||||
|
||||
defp format_comment_text("#"), do: "#"
|
||||
defp format_comment_text("#!" <> rest), do: "#!" <> rest
|
||||
defp format_comment_text("##" <> rest), do: "#" <> format_comment_text("#" <> rest)
|
||||
defp format_comment_text("# " <> rest), do: "# " <> rest
|
||||
defp format_comment_text("#" <> rest), do: "# " <> rest
|
||||
defp next_eol('\s' ++ rest, count), do: next_eol(rest, count)
|
||||
defp next_eol('\t' ++ rest, count), do: next_eol(rest, count)
|
||||
defp next_eol('\n' ++ rest, count), do: next_eol(rest, count + 1)
|
||||
defp next_eol('\r\n' ++ rest, count), do: next_eol(rest, count + 1)
|
||||
defp next_eol(_, count), do: count
|
||||
|
||||
defp previous_eol([{token, {_, _, count}} | _])
|
||||
when token in [:eol, :",", :";"] and count > 0 do
|
||||
count
|
||||
end
|
||||
|
||||
defp previous_eol([]), do: 1
|
||||
defp previous_eol(_), do: nil
|
||||
|
||||
defp format_comment('##' ++ rest, acc), do: format_comment([?# | rest], [?# | acc])
|
||||
|
||||
defp format_comment('#!', acc), do: reverse_to_string(acc, '#!')
|
||||
defp format_comment('#! ' ++ _ = rest, acc), do: reverse_to_string(acc, rest)
|
||||
defp format_comment('#!' ++ rest, acc), do: reverse_to_string(acc, [?#, ?!, ?\s, rest])
|
||||
|
||||
defp format_comment('#', acc), do: reverse_to_string(acc, '#')
|
||||
defp format_comment('# ' ++ _ = rest, acc), do: reverse_to_string(acc, rest)
|
||||
defp format_comment('#' ++ rest, acc), do: reverse_to_string(acc, [?#, ?\s, rest])
|
||||
|
||||
defp reverse_to_string(acc, prefix) do
|
||||
acc |> Enum.reverse(prefix) |> List.to_string()
|
||||
end
|
||||
|
||||
# If there is a no new line before, we can't gather all followup comments.
|
||||
defp gather_comments([%{previous_eol_count: 0} = comment | comments]) do
|
||||
comment = %{comment | previous_eol_count: @newlines}
|
||||
defp gather_comments([{line, {nil, next_eol}, doc} | comments]) do
|
||||
comment = {line, {@newlines, next_eol}, doc}
|
||||
[comment | gather_comments(comments)]
|
||||
end
|
||||
|
||||
defp gather_comments([comment | comments]) do
|
||||
%{line: line, next_eol_count: next_eol_count, text: doc} = comment
|
||||
|
||||
{next_eol_count, comments, doc} =
|
||||
gather_followup_comments(line + 1, next_eol_count, comments, doc)
|
||||
|
||||
comment = %{comment | next_eol_count: next_eol_count, text: doc}
|
||||
defp gather_comments([{line, {previous_eol, next_eol}, doc} | comments]) do
|
||||
{next_eol, comments, doc} = gather_followup_comments(line + 1, next_eol, comments, doc)
|
||||
comment = {line, {previous_eol, next_eol}, doc}
|
||||
[comment | gather_comments(comments)]
|
||||
end
|
||||
|
||||
@@ -233,17 +323,16 @@ defmodule Code.Formatter do
|
||||
[]
|
||||
end
|
||||
|
||||
defp gather_followup_comments(line, _, [%{line: line} = comment | comments], doc)
|
||||
when comment.previous_eol_count != 0 do
|
||||
%{next_eol_count: next_eol_count, text: text} = comment
|
||||
gather_followup_comments(line + 1, next_eol_count, comments, line(doc, text))
|
||||
defp gather_followup_comments(line, _, [{line, {previous_eol, next_eol}, text} | comments], doc)
|
||||
when previous_eol != nil do
|
||||
gather_followup_comments(line + 1, next_eol, comments, line(doc, text))
|
||||
end
|
||||
|
||||
defp gather_followup_comments(_line, next_eol_count, comments, doc) do
|
||||
{next_eol_count, comments, doc}
|
||||
defp gather_followup_comments(_line, next_eol, comments, doc) do
|
||||
{next_eol, comments, doc}
|
||||
end
|
||||
|
||||
# Special AST nodes from compiler feedback
|
||||
# Special AST nodes from compiler feedback.
|
||||
|
||||
defp quoted_to_algebra({{:special, :clause_args}, _meta, [args]}, _context, state) do
|
||||
{doc, state} = clause_args_to_algebra(args, state)
|
||||
@@ -270,7 +359,7 @@ defmodule Code.Formatter do
|
||||
{doc, state} =
|
||||
entries
|
||||
|> prepend_heredoc_line()
|
||||
|> interpolation_to_algebra(~s["""], state, @double_heredoc, @double_heredoc)
|
||||
|> interpolation_to_algebra(:heredoc, state, @double_heredoc, @double_heredoc)
|
||||
|
||||
{force_unfit(doc), state}
|
||||
|
||||
@@ -292,7 +381,7 @@ defmodule Code.Formatter do
|
||||
{doc, state} =
|
||||
entries
|
||||
|> prepend_heredoc_line()
|
||||
|> list_interpolation_to_algebra(~s['''], state, @single_heredoc, @single_heredoc)
|
||||
|> list_interpolation_to_algebra(:heredoc, state, @single_heredoc, @single_heredoc)
|
||||
|
||||
{force_unfit(doc), state}
|
||||
|
||||
@@ -314,17 +403,10 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
# foo[bar]
|
||||
defp quoted_to_algebra({{:., _, [Access, :get]}, meta, [target, arg]}, _context, state) do
|
||||
defp quoted_to_algebra({{:., _, [Access, :get]}, meta, [target | args]}, _context, state) do
|
||||
{target_doc, state} = remote_target_to_algebra(target, state)
|
||||
|
||||
{access_doc, state} =
|
||||
if keyword?(arg) do
|
||||
list_to_algebra(meta, arg, state)
|
||||
else
|
||||
list_to_algebra(meta, [arg], state)
|
||||
end
|
||||
|
||||
{concat(target_doc, access_doc), state}
|
||||
{call_doc, state} = list_to_algebra(meta, args, state)
|
||||
{concat(target_doc, call_doc), state}
|
||||
end
|
||||
|
||||
# %Foo{}
|
||||
@@ -355,7 +437,7 @@ defmodule Code.Formatter do
|
||||
defp quoted_to_algebra({:__block__, meta, [list]}, _context, state) when is_list(list) do
|
||||
case meta[:delimiter] do
|
||||
~s['''] ->
|
||||
string = list |> List.to_string() |> escape_heredoc(~s['''])
|
||||
string = list |> List.to_string() |> escape_heredoc()
|
||||
{@single_heredoc |> concat(string) |> concat(@single_heredoc) |> force_unfit(), state}
|
||||
|
||||
~s['] ->
|
||||
@@ -369,7 +451,7 @@ defmodule Code.Formatter do
|
||||
|
||||
defp quoted_to_algebra({:__block__, meta, [string]}, _context, state) when is_binary(string) do
|
||||
if meta[:delimiter] == ~s["""] do
|
||||
string = escape_heredoc(string, ~s["""])
|
||||
string = escape_heredoc(string)
|
||||
{@double_heredoc |> concat(string) |> concat(@double_heredoc) |> force_unfit(), state}
|
||||
else
|
||||
string = escape_string(string, @double_quote)
|
||||
@@ -439,17 +521,19 @@ defmodule Code.Formatter do
|
||||
|
||||
# not(left in right)
|
||||
# left not in right
|
||||
defp quoted_to_algebra({:not, meta, [{:in, _, [left, right]}]}, context, state) do
|
||||
binary_op_to_algebra(:in, "not in", meta, left, right, context, state)
|
||||
end
|
||||
defp quoted_to_algebra({:not, meta, [{:in, _, [left, right]} = arg]}, context, state) do
|
||||
%{rename_deprecated_at: since} = state
|
||||
|
||||
# 1..2//3
|
||||
defp quoted_to_algebra({:"..//", meta, [left, middle, right]}, context, state) do
|
||||
quoted_to_algebra({:"//", meta, [{:.., meta, [left, middle]}, right]}, context, state)
|
||||
# TODO: Remove metadata and always rewrite to left not in right in Elixir v2.0.
|
||||
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
|
||||
unary_op_to_algebra(:not, meta, arg, context, state)
|
||||
end
|
||||
end
|
||||
|
||||
defp quoted_to_algebra({:fn, meta, [_ | _] = clauses}, _context, state) do
|
||||
anon_fun_to_algebra(clauses, line(meta), closing_line(meta), state, eol?(meta, state))
|
||||
anon_fun_to_algebra(clauses, line(meta), closing_line(meta), state, eol?(meta))
|
||||
end
|
||||
|
||||
defp quoted_to_algebra({fun, meta, args}, context, state) when is_atom(fun) and is_list(args) do
|
||||
@@ -481,10 +565,6 @@ defmodule Code.Formatter do
|
||||
if keyword_key?(left_arg) do
|
||||
{left, state} =
|
||||
case left_arg do
|
||||
# TODO: Remove this clause in v1.16 when we no longer quote operator :..//
|
||||
{:__block__, _, [:"..//"]} ->
|
||||
{string(~S{"..//":}), state}
|
||||
|
||||
{:__block__, _, [atom]} when is_atom(atom) ->
|
||||
key =
|
||||
case Code.Identifier.classify(atom) do
|
||||
@@ -518,11 +598,6 @@ defmodule Code.Formatter do
|
||||
{doc, state}
|
||||
end
|
||||
|
||||
# #PID's and #Ref's may appear on regular AST
|
||||
defp quoted_to_algebra(unknown, _context, state) do
|
||||
{inspect(unknown), state}
|
||||
end
|
||||
|
||||
## Blocks
|
||||
|
||||
defp block_to_algebra([{:->, _, _} | _] = type_fun, min_line, max_line, state) do
|
||||
@@ -636,7 +711,7 @@ defmodule Code.Formatter do
|
||||
{operands, max_line} =
|
||||
unwrap_right(right_arg, op, meta, right_context, [{{:root, left_context}, left_arg}])
|
||||
|
||||
fun = fn
|
||||
operand_to_algebra = fn
|
||||
{{:root, context}, arg}, _args, state ->
|
||||
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :left, 2)
|
||||
{{doc, @empty, 1}, state}
|
||||
@@ -648,7 +723,14 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
{doc, state} =
|
||||
operand_to_algebra_with_comments(operands, meta, min_line, max_line, context, state, fun)
|
||||
operand_to_algebra_with_comments(
|
||||
operands,
|
||||
meta,
|
||||
min_line,
|
||||
max_line,
|
||||
state,
|
||||
operand_to_algebra
|
||||
)
|
||||
|
||||
if keyword?(right_arg) and context in [:parens_arg, :no_parens_arg] do
|
||||
{wrap_in_parens(doc), state}
|
||||
@@ -667,7 +749,7 @@ defmodule Code.Formatter do
|
||||
{pipes, min_line} =
|
||||
unwrap_pipes(left_arg, meta, left_context, [{{op, right_context}, right_arg}])
|
||||
|
||||
fun = fn
|
||||
operand_to_algebra = fn
|
||||
{{:root, context}, arg}, _args, state ->
|
||||
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :left, 2)
|
||||
{{doc, @empty, 1}, state}
|
||||
@@ -679,7 +761,7 @@ defmodule Code.Formatter do
|
||||
{{concat(op_string, doc), @empty, 1}, state}
|
||||
end
|
||||
|
||||
operand_to_algebra_with_comments(pipes, meta, min_line, max_line, context, state, fun)
|
||||
operand_to_algebra_with_comments(pipes, meta, min_line, max_line, state, operand_to_algebra)
|
||||
end
|
||||
|
||||
defp binary_op_to_algebra(op, op_string, meta, left_arg, right_arg, context, state, nesting) do
|
||||
@@ -703,7 +785,7 @@ defmodule Code.Formatter do
|
||||
concat(concat(group(left), op_string), group(right))
|
||||
|
||||
true ->
|
||||
eol? = eol?(meta, state)
|
||||
eol? = eol?(meta)
|
||||
|
||||
next_break_fits? =
|
||||
op in @next_break_fits_operators and next_break_fits?(right_arg, state) and not eol?
|
||||
@@ -807,45 +889,11 @@ defmodule Code.Formatter do
|
||||
{Enum.reverse(acc), line(meta)}
|
||||
end
|
||||
|
||||
defp operand_to_algebra_with_comments(operands, meta, min_line, max_line, context, state, fun) do
|
||||
# If we are in a no_parens_one_arg expression, we actually cannot
|
||||
# extract comments from the first operand, because it would rewrite:
|
||||
#
|
||||
# @spec function(x) ::
|
||||
# # Comment
|
||||
# any
|
||||
# when x: any
|
||||
#
|
||||
# to:
|
||||
#
|
||||
# @spec # Comment
|
||||
# function(x) ::
|
||||
# any
|
||||
# when x: any
|
||||
#
|
||||
# Instead we get:
|
||||
#
|
||||
# @spec function(x) ::
|
||||
# any
|
||||
# # Comment
|
||||
# when x: any
|
||||
#
|
||||
# Which may look counter-intuitive but it actually makes sense,
|
||||
# as the closest possible location for the comment is the when
|
||||
# operator.
|
||||
{operands, acc, state} =
|
||||
if context == :no_parens_one_arg do
|
||||
[operand | operands] = operands
|
||||
{doc_triplet, state} = fun.(operand, :unused, state)
|
||||
{operands, [doc_triplet], state}
|
||||
else
|
||||
{operands, [], state}
|
||||
end
|
||||
|
||||
defp operand_to_algebra_with_comments(operands, meta, min_line, max_line, state, fun) do
|
||||
{docs, comments?, state} =
|
||||
quoted_to_algebra_with_comments(operands, acc, min_line, max_line, state, fun)
|
||||
quoted_to_algebra_with_comments(operands, [], min_line, max_line, state, fun)
|
||||
|
||||
if comments? or eol?(meta, state) do
|
||||
if comments? or eol?(meta) do
|
||||
{docs |> Enum.reduce(&line(&2, &1)) |> force_unfit(), state}
|
||||
else
|
||||
{docs |> Enum.reduce(&glue(&2, &1)), state}
|
||||
@@ -910,7 +958,7 @@ defmodule Code.Formatter do
|
||||
)
|
||||
when is_atom(fun) and is_integer(arity) do
|
||||
{target_doc, state} = remote_target_to_algebra(target, state)
|
||||
fun = Code.Identifier.inspect_as_function(fun)
|
||||
fun = remote_fun_to_algebra(target, fun, arity, state)
|
||||
{target_doc |> nest(1) |> concat(string(".#{fun}/#{arity}")), state}
|
||||
end
|
||||
|
||||
@@ -955,7 +1003,7 @@ defmodule Code.Formatter do
|
||||
defp remote_to_algebra({{:., _, [target, fun]}, meta, args}, context, state)
|
||||
when is_atom(fun) do
|
||||
{target_doc, state} = remote_target_to_algebra(target, state)
|
||||
fun = Code.Identifier.inspect_as_function(fun)
|
||||
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
|
||||
@@ -991,6 +1039,38 @@ defmodule Code.Formatter do
|
||||
end
|
||||
end
|
||||
|
||||
defp remote_fun_to_algebra(target, fun, arity, state) do
|
||||
%{rename_deprecated_at: since} = state
|
||||
|
||||
atom_target =
|
||||
case since && target do
|
||||
{:__aliases__, _, [alias | _] = aliases} when is_atom(alias) ->
|
||||
Module.concat(aliases)
|
||||
|
||||
{:__block__, _, [atom]} when is_atom(atom) ->
|
||||
atom
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
|
||||
with {fun, requirement} <- deprecated(atom_target, fun, arity),
|
||||
true <- Version.match?(since, requirement) do
|
||||
fun
|
||||
else
|
||||
_ -> Code.Identifier.inspect_as_function(fun)
|
||||
end
|
||||
end
|
||||
|
||||
# We can only rename functions in the same module because
|
||||
# introducing a new module may be 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"}
|
||||
defp deprecated(Kernel.ParallelCompiler, :files, 2), do: {"compile", "~> 1.6"}
|
||||
defp deprecated(Kernel.ParallelCompiler, :files_to_path, 2), do: {"compile_to_path", "~> 1.6"}
|
||||
defp deprecated(_, _, _), do: :error
|
||||
|
||||
defp remote_target_to_algebra({:fn, _, [_ | _]} = quoted, state) do
|
||||
# This change is not semantically required but for beautification.
|
||||
{doc, state} = quoted_to_algebra(quoted, :no_parens_arg, state)
|
||||
@@ -1005,14 +1085,9 @@ 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
|
||||
|
||||
local_without_parens?(fun, length(args), state.locals_without_parens) ->
|
||||
:skip_unless_many_args
|
||||
|
||||
true ->
|
||||
:skip_if_do_end
|
||||
meta?(meta, :closing) -> :skip_if_only_do_end
|
||||
local_without_parens?(fun, args, state) -> :skip_unless_many_args
|
||||
true -> :skip_if_do_end
|
||||
end
|
||||
|
||||
{{call_doc, state}, wrap_in_parens?} =
|
||||
@@ -1086,7 +1161,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
args = if keyword?, do: left ++ right, else: left ++ [right]
|
||||
many_eol? = match?([_, _ | _], args) and eol?(meta, state)
|
||||
many_eol? = match?([_, _ | _], args) and eol?(meta)
|
||||
no_generators? = no_generators?(args)
|
||||
to_algebra_fun = "ed_to_algebra(&1, context, &2)
|
||||
|
||||
@@ -1191,6 +1266,15 @@ defmodule Code.Formatter do
|
||||
end
|
||||
end
|
||||
|
||||
defp local_without_parens?(fun, args, %{locals_without_parens: locals_without_parens}) do
|
||||
length = length(args)
|
||||
|
||||
length > 0 and
|
||||
Enum.any?(locals_without_parens, fn {key, val} ->
|
||||
key == fun and (val == :* or val == length)
|
||||
end)
|
||||
end
|
||||
|
||||
defp no_generators?(args) do
|
||||
not Enum.any?(args, &match?({:<-, _, [_, _]}, &1))
|
||||
end
|
||||
@@ -1267,8 +1351,9 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp list_interpolation_to_algebra([entry | entries], escape, state, acc, last) do
|
||||
{{:., _, [Kernel, :to_string]}, _meta, [quoted]} = entry
|
||||
{doc, state} = interpolation_to_string(quoted, state)
|
||||
{{:., _, [Kernel, :to_string]}, meta, [quoted]} = entry
|
||||
{doc, state} = block_to_algebra(quoted, line(meta), closing_line(meta), state)
|
||||
doc = surround("\#{", doc, "}")
|
||||
list_interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
|
||||
end
|
||||
|
||||
@@ -1283,8 +1368,9 @@ 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} = interpolation_to_string(quoted, state)
|
||||
{:"::", _, [{{:., _, [Kernel, :to_string]}, meta, [quoted]}, {:binary, _, _}]} = entry
|
||||
{doc, state} = block_to_algebra(quoted, line(meta), closing_line(meta), state)
|
||||
doc = surround("\#{", doc, "}")
|
||||
interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
|
||||
end
|
||||
|
||||
@@ -1292,23 +1378,6 @@ defmodule Code.Formatter do
|
||||
{concat(acc, last), state}
|
||||
end
|
||||
|
||||
defp interpolation_to_string(quoted, %{skip_eol: skip_eol} = state) do
|
||||
{doc, state} = block_to_algebra(quoted, @max_line, @min_line, %{state | skip_eol: true})
|
||||
doc = interpolation_to_string(surround("\#{", doc, "}"))
|
||||
{doc, %{state | skip_eol: skip_eol}}
|
||||
end
|
||||
|
||||
defp interpolation_to_string(doc) do
|
||||
[head | tail] =
|
||||
doc
|
||||
|> format_to_string()
|
||||
|> String.split("\n")
|
||||
|
||||
Enum.reduce(tail, string(head), fn line, acc ->
|
||||
concat([acc, line(), string(line)])
|
||||
end)
|
||||
end
|
||||
|
||||
## Sigils
|
||||
|
||||
defp maybe_sigil_to_algebra(fun, meta, args, state) do
|
||||
@@ -1317,29 +1386,13 @@ defmodule Code.Formatter do
|
||||
opening_delimiter when not is_nil(opening_delimiter) <- meta[:delimiter] do
|
||||
doc = <<?~, name, opening_delimiter::binary>>
|
||||
|
||||
entries =
|
||||
case state.sigils do
|
||||
%{^name => callback} ->
|
||||
case callback.(hd(entries), sigil: List.to_atom([name]), modifiers: modifiers) do
|
||||
binary when is_binary(binary) ->
|
||||
[binary]
|
||||
|
||||
other ->
|
||||
raise ArgumentError,
|
||||
"expected sigil callback to return a binary, got: #{inspect(other)}"
|
||||
end
|
||||
|
||||
%{} ->
|
||||
entries
|
||||
end
|
||||
|
||||
if opening_delimiter in [@double_heredoc, @single_heredoc] do
|
||||
closing_delimiter = concat(opening_delimiter, List.to_string(modifiers))
|
||||
|
||||
{doc, state} =
|
||||
entries
|
||||
|> prepend_heredoc_line()
|
||||
|> interpolation_to_algebra(opening_delimiter, state, doc, closing_delimiter)
|
||||
|> interpolation_to_algebra(:heredoc, state, doc, closing_delimiter)
|
||||
|
||||
{force_unfit(doc), state}
|
||||
else
|
||||
@@ -1363,7 +1416,7 @@ defmodule Code.Formatter do
|
||||
|
||||
defp bitstring_to_algebra(meta, args, state) do
|
||||
last = length(args) - 1
|
||||
join = if eol?(meta, state), do: :line, else: :flex_break
|
||||
join = if eol?(meta), do: :line, else: :flex_break
|
||||
to_algebra_fun = &bitstring_segment_to_algebra(&1, &2, last)
|
||||
|
||||
{args_doc, join, state} =
|
||||
@@ -1431,7 +1484,7 @@ defmodule Code.Formatter do
|
||||
## Literals
|
||||
|
||||
defp list_to_algebra(meta, args, state) do
|
||||
join = if eol?(meta, state), do: :line, else: :break
|
||||
join = if eol?(meta), do: :line, else: :break
|
||||
fun = "ed_to_algebra(&1, :parens_arg, &2)
|
||||
|
||||
{args_doc, _join, state} =
|
||||
@@ -1441,7 +1494,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp map_to_algebra(meta, name_doc, [{:|, _, [left, right]}], state) do
|
||||
join = if eol?(meta, state), do: :line, else: :break
|
||||
join = if eol?(meta), do: :line, else: :break
|
||||
fun = "ed_to_algebra(&1, :parens_arg, &2)
|
||||
{left_doc, state} = fun.(left, state)
|
||||
|
||||
@@ -1458,7 +1511,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp map_to_algebra(meta, name_doc, args, state) do
|
||||
join = if eol?(meta, state), do: :line, else: :break
|
||||
join = if eol?(meta), do: :line, else: :break
|
||||
fun = "ed_to_algebra(&1, :parens_arg, &2)
|
||||
|
||||
{args_doc, _join, state} =
|
||||
@@ -1469,7 +1522,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp tuple_to_algebra(meta, args, join, state) do
|
||||
join = if eol?(meta, state), do: :line, else: join
|
||||
join = if eol?(meta), do: :line, else: join
|
||||
fun = "ed_to_algebra(&1, :parens_arg, &2)
|
||||
|
||||
{args_doc, join, state} =
|
||||
@@ -1486,11 +1539,6 @@ defmodule Code.Formatter do
|
||||
Atom.to_string(atom)
|
||||
end
|
||||
|
||||
# TODO: Remove this clause in v1.16 when we no longer quote operator :..//
|
||||
defp atom_to_algebra(:"..//") do
|
||||
string(":\"..//\"")
|
||||
end
|
||||
|
||||
defp atom_to_algebra(atom) do
|
||||
string = Atom.to_string(atom)
|
||||
|
||||
@@ -1548,13 +1596,11 @@ defmodule Code.Formatter do
|
||||
end
|
||||
end
|
||||
|
||||
defp escape_heredoc(string, escape) do
|
||||
string = String.replace(string, escape, "\\" <> escape)
|
||||
defp escape_heredoc(string) do
|
||||
heredoc_to_algebra(["" | String.split(string, "\n")])
|
||||
end
|
||||
|
||||
defp escape_string(string, <<_, _, _>> = escape) do
|
||||
string = String.replace(string, escape, "\\" <> escape)
|
||||
defp escape_string(string, :heredoc) do
|
||||
heredoc_to_algebra(String.split(string, "\n"))
|
||||
end
|
||||
|
||||
@@ -1653,7 +1699,7 @@ defmodule Code.Formatter do
|
||||
|> glue(body_doc)
|
||||
|> nest(2)
|
||||
|> glue("end")
|
||||
|> maybe_force_clauses(clauses, state)
|
||||
|> maybe_force_clauses(clauses)
|
||||
|> group()
|
||||
|
||||
{doc, state}
|
||||
@@ -1687,7 +1733,7 @@ defmodule Code.Formatter do
|
||||
|> glue(body_doc)
|
||||
|> nest(2)
|
||||
|> glue("end")
|
||||
|> maybe_force_clauses(clauses, state)
|
||||
|> maybe_force_clauses(clauses)
|
||||
|> group()
|
||||
|
||||
{doc, state}
|
||||
@@ -1715,7 +1761,7 @@ defmodule Code.Formatter do
|
||||
"(() -> "
|
||||
|> concat(nest(body_doc, :cursor))
|
||||
|> concat(")")
|
||||
|> maybe_force_clauses(clauses, state)
|
||||
|> maybe_force_clauses(clauses)
|
||||
|> group()
|
||||
|
||||
{doc, state}
|
||||
@@ -1736,7 +1782,7 @@ defmodule Code.Formatter do
|
||||
|> group()
|
||||
|> concat(break() |> concat(body_doc) |> nest(2))
|
||||
|> wrap_in_parens()
|
||||
|> maybe_force_clauses(clauses, state)
|
||||
|> maybe_force_clauses(clauses)
|
||||
|> group()
|
||||
|
||||
{doc, state}
|
||||
@@ -1755,8 +1801,8 @@ defmodule Code.Formatter do
|
||||
|
||||
## Clauses
|
||||
|
||||
defp maybe_force_clauses(doc, clauses, state) do
|
||||
if Enum.any?(clauses, fn {:->, meta, _} -> eol?(meta, state) end) do
|
||||
defp maybe_force_clauses(doc, clauses) do
|
||||
if Enum.any?(clauses, fn {:->, meta, _} -> eol?(meta) end) do
|
||||
force_unfit(doc)
|
||||
else
|
||||
doc
|
||||
@@ -1779,7 +1825,7 @@ defmodule Code.Formatter do
|
||||
{doc_acc, state_acc}
|
||||
end)
|
||||
|
||||
{clauses_doc |> maybe_force_clauses([clause | clauses], state) |> group(), state}
|
||||
{clauses_doc |> maybe_force_clauses([clause | clauses]) |> group(), state}
|
||||
end
|
||||
|
||||
defp clauses_to_algebra(other, min_line, max_line, state) do
|
||||
@@ -1859,7 +1905,7 @@ defmodule Code.Formatter do
|
||||
defp quoted_to_algebra_with_comments(args, acc, min_line, max_line, state, fun) do
|
||||
{pre_comments, state} =
|
||||
get_and_update_in(state.comments, fn comments ->
|
||||
Enum.split_while(comments, fn %{line: line} -> line <= min_line end)
|
||||
Enum.split_while(comments, fn {line, _, _} -> line <= min_line end)
|
||||
end)
|
||||
|
||||
{docs, comments?, state} =
|
||||
@@ -1875,29 +1921,23 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp each_quoted_to_algebra_with_comments([arg | args], acc, max_line, state, comments?, fun) do
|
||||
case traverse_line(arg, {@max_line, @min_line}) do
|
||||
{@max_line, @min_line} ->
|
||||
{doc_triplet, state} = fun.(arg, args, state)
|
||||
acc = [doc_triplet | acc]
|
||||
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
|
||||
{doc_start, doc_end} = traverse_line(arg, {@max_line, @min_line})
|
||||
|
||||
{doc_start, doc_end} ->
|
||||
{acc, comments, comments?} =
|
||||
extract_comments_before(doc_start, acc, state.comments, comments?)
|
||||
{acc, comments, comments?} =
|
||||
extract_comments_before(doc_start, acc, state.comments, comments?)
|
||||
|
||||
{doc_triplet, state} = fun.(arg, args, %{state | comments: comments})
|
||||
{doc_triplet, state} = fun.(arg, args, %{state | comments: comments})
|
||||
|
||||
{acc, comments, comments?} =
|
||||
extract_comments_trailing(doc_start, doc_end, acc, state.comments, comments?)
|
||||
{acc, comments, comments?} =
|
||||
extract_comments_trailing(doc_start, doc_end, acc, state.comments, comments?)
|
||||
|
||||
acc = [adjust_trailing_newlines(doc_triplet, doc_end, comments) | acc]
|
||||
state = %{state | comments: comments}
|
||||
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
|
||||
end
|
||||
acc = [adjust_trailing_newlines(doc_triplet, doc_end, comments) | acc]
|
||||
state = %{state | comments: comments}
|
||||
each_quoted_to_algebra_with_comments(args, acc, max_line, state, comments?, fun)
|
||||
end
|
||||
|
||||
defp extract_comments_before(max, acc, [%{line: line} = comment | rest], _) when line < max do
|
||||
%{previous_eol_count: previous, next_eol_count: next, text: doc} = comment
|
||||
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)
|
||||
end
|
||||
@@ -1912,7 +1952,7 @@ defmodule Code.Formatter do
|
||||
defp add_previous_to_acc(acc, _previous),
|
||||
do: acc
|
||||
|
||||
defp extract_comments_trailing(min, max, acc, [%{line: line, text: doc_comment} | rest], _)
|
||||
defp extract_comments_trailing(min, max, 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)
|
||||
@@ -1924,7 +1964,7 @@ defmodule Code.Formatter do
|
||||
|
||||
# If the document is immediately followed by comment which is followed by newlines,
|
||||
# its newlines wouldn't have considered the comment, so we need to adjust it.
|
||||
defp adjust_trailing_newlines({doc, next_line, newlines}, doc_end, [%{line: line} | _])
|
||||
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
|
||||
@@ -2001,6 +2041,8 @@ defmodule Code.Formatter do
|
||||
{wrap_in_parens_if_operator(doc, ast), state}
|
||||
end
|
||||
|
||||
# TODO: We can remove this workaround once we remove
|
||||
# ?rearrange_uop from the parser on v2.0.
|
||||
defp wrap_in_parens_if_operator(doc, {:__block__, _, [expr]}) do
|
||||
wrap_in_parens_if_operator(doc, expr)
|
||||
end
|
||||
@@ -2056,16 +2098,21 @@ defmodule Code.Formatter do
|
||||
|
||||
defp binary_operator?(quoted) do
|
||||
case quoted do
|
||||
{op, _, [_, _, _]} when op in @multi_binary_operators -> true
|
||||
{op, _, [_, _]} when is_atom(op) -> Code.Identifier.binary_op(op) != :error
|
||||
_ -> false
|
||||
{op, _, [_, _]} when is_atom(op) ->
|
||||
Code.Identifier.binary_op(op) != :error
|
||||
|
||||
_ ->
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
defp unary_operator?(quoted) do
|
||||
case quoted do
|
||||
{op, _, [_]} when is_atom(op) -> Code.Identifier.unary_op(op) != :error
|
||||
_ -> false
|
||||
{op, _, [_]} when is_atom(op) ->
|
||||
Code.Identifier.unary_op(op) != :error
|
||||
|
||||
_ ->
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2126,12 +2173,12 @@ defmodule Code.Formatter do
|
||||
false
|
||||
end
|
||||
|
||||
defp eol_or_comments?(meta, %{comments: comments} = state) do
|
||||
eol?(meta, state) or
|
||||
defp eol_or_comments?(meta, %{comments: comments}) do
|
||||
eol?(meta) or
|
||||
(
|
||||
min_line = line(meta)
|
||||
max_line = closing_line(meta)
|
||||
Enum.any?(comments, fn %{line: line} -> line > min_line and line < max_line end)
|
||||
Enum.any?(comments, fn {line, _, _} -> line > min_line and line < max_line end)
|
||||
)
|
||||
end
|
||||
|
||||
@@ -2156,8 +2203,8 @@ defmodule Code.Formatter do
|
||||
{{_, arg_meta, _}, _} = hd(arg)
|
||||
first_line = line(arg_meta)
|
||||
|
||||
case Enum.drop_while(comments, fn %{line: line} -> line <= block_line end) do
|
||||
[%{line: line} | _] when line <= first_line ->
|
||||
case Enum.drop_while(comments, fn {line, _, _} -> line <= block_line end) do
|
||||
[{line, _, _} | _] when line <= first_line ->
|
||||
{false, block}
|
||||
|
||||
_ ->
|
||||
@@ -2175,7 +2222,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp force_args?(args) do
|
||||
match?([_ | _], args) and force_args?(args, %{})
|
||||
match?([_, _ | _], args) and force_args?(args, MapSet.new())
|
||||
end
|
||||
|
||||
defp force_args?([[arg | _] | args], lines) do
|
||||
@@ -2185,19 +2232,18 @@ defmodule Code.Formatter do
|
||||
defp force_args?([arg | args], lines) do
|
||||
line =
|
||||
case arg do
|
||||
{{_, meta, _}, _} -> meta[:line]
|
||||
{_, meta, _} -> meta[:line]
|
||||
{{_, meta, _}, _} -> line(meta)
|
||||
{_, meta, _} -> line(meta)
|
||||
end
|
||||
|
||||
cond do
|
||||
# Line may be missing from non-formatter AST
|
||||
is_nil(line) -> force_args?(args, lines)
|
||||
Map.has_key?(lines, line) -> false
|
||||
true -> force_args?(args, Map.put(lines, line, true))
|
||||
if MapSet.member?(lines, line) do
|
||||
false
|
||||
else
|
||||
force_args?(args, MapSet.put(lines, line))
|
||||
end
|
||||
end
|
||||
|
||||
defp force_args?([], lines), do: map_size(lines) >= 2
|
||||
defp force_args?([], _lines), do: true
|
||||
|
||||
defp force_keyword(doc, arg) do
|
||||
if force_args?(arg), do: force_unfit(doc), else: doc
|
||||
@@ -2215,8 +2261,9 @@ defmodule Code.Formatter do
|
||||
defp keyword_key?(_),
|
||||
do: false
|
||||
|
||||
defp eol?(_meta, %{skip_eol: true}), do: false
|
||||
defp eol?(meta, _state), do: Keyword.get(meta, :newlines, 0) > 0
|
||||
defp eol?(meta) do
|
||||
Keyword.get(meta, :newlines, 0) > 0
|
||||
end
|
||||
|
||||
defp meta?(meta, key) do
|
||||
is_list(meta[key])
|
||||
|
||||
@@ -1,841 +0,0 @@
|
||||
defmodule Code.Fragment do
|
||||
@moduledoc """
|
||||
This module provides conveniences for analyzing fragments of
|
||||
textual code and extract available information whenever possible.
|
||||
|
||||
Most of the functions in this module provide a best-effort
|
||||
and may not be accurate under all circumstances. Read each
|
||||
documentation for more information.
|
||||
|
||||
This module should be considered experimental.
|
||||
"""
|
||||
|
||||
@type position :: {line :: pos_integer(), column :: pos_integer()}
|
||||
|
||||
@doc """
|
||||
Receives a string and returns the cursor context.
|
||||
|
||||
This function receives a string with an Elixir code fragment,
|
||||
representing a cursor position, and based on the string, it
|
||||
provides contextual information about said position. The
|
||||
return of this function can then be used to provide tips,
|
||||
suggestions, and autocompletion functionality.
|
||||
|
||||
This function provides a best-effort detection and may not be
|
||||
accurate under all circumstances. See the "Limitations"
|
||||
section below.
|
||||
|
||||
Consider adding a catch-all clause when handling the return
|
||||
type of this function as new cursor information may be added
|
||||
in future releases.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.Fragment.cursor_context("")
|
||||
:expr
|
||||
|
||||
iex> Code.Fragment.cursor_context("hello_wor")
|
||||
{:local_or_var, 'hello_wor'}
|
||||
|
||||
## Return values
|
||||
|
||||
* `{:alias, charlist}` - the context is an alias, potentially
|
||||
a nested one, such as `Hello.Wor` or `HelloWor`
|
||||
|
||||
* `{:dot, inside_dot, charlist}` - the context is a dot
|
||||
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
|
||||
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
|
||||
itself. If a var is given, this may either be a remote call or a map
|
||||
field access. Examples are `Hello.wor`, `:hello.wor`, `hello.wor`,
|
||||
`Hello.nested.wor`, `hello.nested.wor`, and `@hello.world`
|
||||
|
||||
* `{:dot_arity, inside_dot, charlist}` - the context is a dot arity
|
||||
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
|
||||
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
|
||||
itself. If a var is given, it must be a remote arity. Examples are
|
||||
`Hello.world/`, `:hello.world/`, `hello.world/2`, and `@hello.world/2`
|
||||
|
||||
* `{:dot_call, inside_dot, charlist}` - the context is a dot
|
||||
call. This means parentheses or space have been added after the expression.
|
||||
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
|
||||
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
|
||||
itself. If a var is given, it must be a remote call. Examples are
|
||||
`Hello.world(`, `:hello.world(`, `Hello.world `, `hello.world(`, `hello.world `,
|
||||
and `@hello.world(`
|
||||
|
||||
* `:expr` - may be any expression. Autocompletion may suggest an alias,
|
||||
local or var
|
||||
|
||||
* `{:local_or_var, charlist}` - the context is a variable or a local
|
||||
(import or local) call, such as `hello_wor`
|
||||
|
||||
* `{:local_arity, charlist}` - the context is a local (import or local)
|
||||
arity, such as `hello_world/`
|
||||
|
||||
* `{:local_call, charlist}` - the context is a local (import or local)
|
||||
call, such as `hello_world(` and `hello_world `
|
||||
|
||||
* `{:module_attribute, charlist}` - the context is a module attribute,
|
||||
such as `@hello_wor`
|
||||
|
||||
* `{:operator, charlist}` - the context is an operator, such as `+` or
|
||||
`==`. Note textual operators, such as `when` do not appear as operators
|
||||
but rather as `:local_or_var`. `@` is never an `:operator` and always a
|
||||
`:module_attribute`
|
||||
|
||||
* `{:operator_arity, charlist}` - the context is an operator arity, which
|
||||
is an operator followed by /, such as `+/`, `not/` or `when/`
|
||||
|
||||
* `{:operator_call, charlist}` - the context is an operator call, which is
|
||||
an operator followed by space, such as `left + `, `not ` or `x when `
|
||||
|
||||
* `:none` - no context possible
|
||||
|
||||
* `{:sigil, charlist}` - the context is a sigil. It may be either the beginning
|
||||
of a sigil, such as `~` or `~s`, or an operator starting with `~`, such as
|
||||
`~>` and `~>>`
|
||||
|
||||
* `{:struct, charlist}` - the context is a struct, such as `%`, `%UR` or `%URI`
|
||||
|
||||
* `{:unquoted_atom, charlist}` - the context is an unquoted atom. This
|
||||
can be any atom or an atom representing a module
|
||||
|
||||
## Limitations
|
||||
|
||||
The current algorithm only considers the last line of the input. This means
|
||||
it will also show suggestions inside strings, heredocs, etc, which is
|
||||
intentional as it helps with doctests, references, and more.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec cursor_context(List.Chars.t(), keyword()) ::
|
||||
{:alias, charlist}
|
||||
| {:dot, inside_dot, charlist}
|
||||
| {:dot_arity, inside_dot, charlist}
|
||||
| {:dot_call, inside_dot, charlist}
|
||||
| :expr
|
||||
| {:local_or_var, charlist}
|
||||
| {:local_arity, charlist}
|
||||
| {:local_call, charlist}
|
||||
| {:module_attribute, charlist}
|
||||
| {:operator, charlist}
|
||||
| {:operator_arity, charlist}
|
||||
| {:operator_call, charlist}
|
||||
| :none
|
||||
| {:sigil, charlist}
|
||||
| {:struct, charlist}
|
||||
| {:unquoted_atom, charlist}
|
||||
when inside_dot:
|
||||
{:alias, charlist}
|
||||
| {:dot, inside_dot, charlist}
|
||||
| {:module_attribute, charlist}
|
||||
| {:unquoted_atom, charlist}
|
||||
| {:var, charlist}
|
||||
def cursor_context(fragment, opts \\ [])
|
||||
|
||||
def cursor_context(binary, opts) when is_binary(binary) and is_list(opts) do
|
||||
binary =
|
||||
case :binary.matches(binary, "\n") do
|
||||
[] ->
|
||||
binary
|
||||
|
||||
matches ->
|
||||
{position, _} = List.last(matches)
|
||||
binary_part(binary, position + 1, byte_size(binary) - position - 1)
|
||||
end
|
||||
|
||||
binary
|
||||
|> String.to_charlist()
|
||||
|> :lists.reverse()
|
||||
|> codepoint_cursor_context(opts)
|
||||
|> elem(0)
|
||||
end
|
||||
|
||||
def cursor_context(charlist, opts) when is_list(charlist) and is_list(opts) do
|
||||
charlist =
|
||||
case charlist |> Enum.chunk_by(&(&1 == ?\n)) |> List.last([]) do
|
||||
[?\n | _] -> []
|
||||
rest -> rest
|
||||
end
|
||||
|
||||
charlist
|
||||
|> :lists.reverse()
|
||||
|> codepoint_cursor_context(opts)
|
||||
|> elem(0)
|
||||
end
|
||||
|
||||
def cursor_context(other, opts) when is_list(opts) do
|
||||
cursor_context(to_charlist(other), opts)
|
||||
end
|
||||
|
||||
@operators '\\<>+-*/:=|&~^%!'
|
||||
@starter_punctuation ',([{;'
|
||||
@non_starter_punctuation ')]}"\'.$'
|
||||
@space '\t\s'
|
||||
@trailing_identifier '?!'
|
||||
@tilde_op_prefix '<=~'
|
||||
|
||||
@non_identifier @trailing_identifier ++
|
||||
@operators ++ @starter_punctuation ++ @non_starter_punctuation ++ @space
|
||||
|
||||
@textual_operators ~w(when not and or in)c
|
||||
@incomplete_operators ~w(^^ ~~ ~)c
|
||||
|
||||
defp codepoint_cursor_context(reverse, _opts) do
|
||||
{stripped, spaces} = strip_spaces(reverse, 0)
|
||||
|
||||
case stripped do
|
||||
# It is empty
|
||||
[] -> {:expr, 0}
|
||||
# Structs
|
||||
[?%, ?:, ?: | _] -> {{:struct, ''}, 1}
|
||||
[?%, ?: | _] -> {{:unquoted_atom, '%'}, 2}
|
||||
[?% | _] -> {{:struct, ''}, 1}
|
||||
# Token/AST only operators
|
||||
[?>, ?= | rest] when rest == [] or hd(rest) != ?: -> {:expr, 0}
|
||||
[?>, ?- | rest] when rest == [] or hd(rest) != ?: -> {:expr, 0}
|
||||
# Two-digit containers
|
||||
[?<, ?< | rest] when rest == [] or hd(rest) != ?< -> {:expr, 0}
|
||||
# Ambiguity around :
|
||||
[?: | rest] when rest == [] or hd(rest) != ?: -> unquoted_atom_or_expr(spaces)
|
||||
# Dots
|
||||
[?.] -> {:none, 0}
|
||||
[?. | rest] when hd(rest) not in '.:' -> dot(rest, spaces + 1, '')
|
||||
# It is a local or remote call with parens
|
||||
[?( | rest] -> call_to_cursor_context(strip_spaces(rest, spaces + 1))
|
||||
# A local arity definition
|
||||
[?/ | rest] -> arity_to_cursor_context(strip_spaces(rest, spaces + 1))
|
||||
# Starting a new expression
|
||||
[h | _] when h in @starter_punctuation -> {:expr, 0}
|
||||
# It is a local or remote call without parens
|
||||
rest when spaces > 0 -> call_to_cursor_context({rest, spaces})
|
||||
# It is an identifier
|
||||
_ -> identifier_to_cursor_context(reverse, 0, false)
|
||||
end
|
||||
end
|
||||
|
||||
defp strip_spaces([h | rest], count) when h in @space, do: strip_spaces(rest, count + 1)
|
||||
defp strip_spaces(rest, count), do: {rest, count}
|
||||
|
||||
defp unquoted_atom_or_expr(0), do: {{:unquoted_atom, ''}, 1}
|
||||
defp unquoted_atom_or_expr(_), do: {:expr, 0}
|
||||
|
||||
defp arity_to_cursor_context({reverse, spaces}) do
|
||||
case identifier_to_cursor_context(reverse, spaces, true) do
|
||||
{{:local_or_var, acc}, count} -> {{:local_arity, acc}, count}
|
||||
{{:dot, base, acc}, count} -> {{:dot_arity, base, acc}, count}
|
||||
{{:operator, acc}, count} -> {{:operator_arity, acc}, count}
|
||||
{_, _} -> {:none, 0}
|
||||
end
|
||||
end
|
||||
|
||||
defp call_to_cursor_context({reverse, spaces}) do
|
||||
case identifier_to_cursor_context(reverse, spaces, true) do
|
||||
{{:local_or_var, acc}, count} -> {{:local_call, acc}, count}
|
||||
{{:dot, base, acc}, count} -> {{:dot_call, base, acc}, count}
|
||||
{{:operator, acc}, count} -> {{:operator_call, acc}, count}
|
||||
{_, _} -> {:none, 0}
|
||||
end
|
||||
end
|
||||
|
||||
defp identifier_to_cursor_context([?., ?., ?: | _], n, _), do: {{:unquoted_atom, '..'}, n + 3}
|
||||
defp identifier_to_cursor_context([?., ?., ?. | _], n, _), do: {{:local_or_var, '...'}, n + 3}
|
||||
defp identifier_to_cursor_context([?., ?: | _], n, _), do: {{:unquoted_atom, '.'}, n + 2}
|
||||
defp identifier_to_cursor_context([?., ?. | _], n, _), do: {{:operator, '..'}, n + 2}
|
||||
|
||||
defp identifier_to_cursor_context(reverse, count, call_op?) do
|
||||
case identifier(reverse, count) do
|
||||
:none ->
|
||||
{:none, 0}
|
||||
|
||||
:operator ->
|
||||
operator(reverse, count, [], call_op?)
|
||||
|
||||
{:module_attribute, acc, count} ->
|
||||
{{:module_attribute, acc}, count}
|
||||
|
||||
{:sigil, acc, count} ->
|
||||
{{:sigil, acc}, count}
|
||||
|
||||
{:unquoted_atom, acc, count} ->
|
||||
{{:unquoted_atom, acc}, count}
|
||||
|
||||
{:alias, rest, acc, count} ->
|
||||
case strip_spaces(rest, count) do
|
||||
{'.' ++ rest, count} when rest == [] or hd(rest) != ?. ->
|
||||
nested_alias(rest, count + 1, acc)
|
||||
|
||||
{'%' ++ _, count} ->
|
||||
{{:struct, acc}, count + 1}
|
||||
|
||||
_ ->
|
||||
{{:alias, acc}, count}
|
||||
end
|
||||
|
||||
{:identifier, _, acc, count} when call_op? and acc in @textual_operators ->
|
||||
{{:operator, acc}, count}
|
||||
|
||||
{:identifier, rest, acc, count} ->
|
||||
case strip_spaces(rest, count) do
|
||||
{'.' ++ rest, count} when rest == [] or hd(rest) != ?. ->
|
||||
dot(rest, count + 1, acc)
|
||||
|
||||
_ ->
|
||||
{{:local_or_var, acc}, count}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp identifier([?? | rest], count), do: check_identifier(rest, count + 1, [??])
|
||||
defp identifier([?! | rest], count), do: check_identifier(rest, count + 1, [?!])
|
||||
defp identifier(rest, count), do: check_identifier(rest, count, [])
|
||||
|
||||
defp check_identifier([h | t], count, acc) when h not in @non_identifier,
|
||||
do: rest_identifier(t, count + 1, [h | acc])
|
||||
|
||||
defp check_identifier(_, _, _), do: :operator
|
||||
|
||||
defp rest_identifier([h | rest], count, acc) when h not in @non_identifier do
|
||||
rest_identifier(rest, count + 1, [h | acc])
|
||||
end
|
||||
|
||||
defp rest_identifier(rest, count, [?@ | acc]) do
|
||||
case tokenize_identifier(rest, count, acc) do
|
||||
{:identifier, _rest, acc, count} -> {:module_attribute, acc, count}
|
||||
:none when acc == [] -> {:module_attribute, '', count}
|
||||
_ -> :none
|
||||
end
|
||||
end
|
||||
|
||||
defp rest_identifier([?~ | rest], count, [letter])
|
||||
when (letter in ?A..?Z or letter in ?a..?z) and
|
||||
(rest == [] or hd(rest) not in @tilde_op_prefix) do
|
||||
{:sigil, [letter], count + 1}
|
||||
end
|
||||
|
||||
defp rest_identifier([?: | rest], count, acc) when rest == [] or hd(rest) != ?: do
|
||||
case String.Tokenizer.tokenize(acc) do
|
||||
{_, _, [], _, _, _} -> {:unquoted_atom, acc, count + 1}
|
||||
_ -> :none
|
||||
end
|
||||
end
|
||||
|
||||
defp rest_identifier([?? | _], _count, _acc) do
|
||||
:none
|
||||
end
|
||||
|
||||
defp rest_identifier(rest, count, acc) do
|
||||
tokenize_identifier(rest, count, acc)
|
||||
end
|
||||
|
||||
defp tokenize_identifier(rest, count, acc) do
|
||||
case String.Tokenizer.tokenize(acc) do
|
||||
# Not actually an atom cause rest is not a :
|
||||
{:atom, _, _, _, _, _} ->
|
||||
:none
|
||||
|
||||
# Aliases must be ascii only
|
||||
{:alias, _, _, _, false, _} ->
|
||||
:none
|
||||
|
||||
{kind, _, [], _, _, extra} ->
|
||||
if ?@ in extra do
|
||||
:none
|
||||
else
|
||||
{kind, rest, acc, count}
|
||||
end
|
||||
|
||||
_ ->
|
||||
:none
|
||||
end
|
||||
end
|
||||
|
||||
defp nested_alias(rest, count, acc) do
|
||||
{rest, count} = strip_spaces(rest, count)
|
||||
|
||||
case identifier_to_cursor_context(rest, count, true) do
|
||||
{{:struct, prev}, count} -> {{:struct, prev ++ '.' ++ acc}, count}
|
||||
{{:alias, prev}, count} -> {{:alias, prev ++ '.' ++ acc}, count}
|
||||
_ -> {:none, 0}
|
||||
end
|
||||
end
|
||||
|
||||
defp dot(rest, count, acc) do
|
||||
{rest, count} = strip_spaces(rest, count)
|
||||
|
||||
case identifier_to_cursor_context(rest, count, true) do
|
||||
{{:local_or_var, var}, count} -> {{:dot, {:var, var}, acc}, count}
|
||||
{{:unquoted_atom, _} = prev, count} -> {{:dot, prev, acc}, count}
|
||||
{{:alias, _} = prev, count} -> {{:dot, prev, acc}, count}
|
||||
{{:dot, _, _} = prev, count} -> {{:dot, prev, acc}, count}
|
||||
{{:module_attribute, _} = prev, count} -> {{:dot, prev, acc}, count}
|
||||
{{:struct, acc}, count} -> {{:struct, acc ++ '.'}, count}
|
||||
{_, _} -> {:none, 0}
|
||||
end
|
||||
end
|
||||
|
||||
defp operator([h | rest], count, acc, call_op?) when h in @operators do
|
||||
operator(rest, count + 1, [h | acc], call_op?)
|
||||
end
|
||||
|
||||
defp operator(rest, count, acc, call_op?) when acc in @incomplete_operators do
|
||||
{rest, dot_count} = strip_spaces(rest, count)
|
||||
|
||||
cond do
|
||||
call_op? ->
|
||||
{:none, 0}
|
||||
|
||||
match?([?. | rest] when rest == [] or hd(rest) != ?., rest) ->
|
||||
dot(tl(rest), dot_count + 1, acc)
|
||||
|
||||
acc == '~' ->
|
||||
{{:sigil, ''}, count}
|
||||
|
||||
true ->
|
||||
{{:operator, acc}, count}
|
||||
end
|
||||
end
|
||||
|
||||
# If we are opening a sigil, ignore the operator.
|
||||
defp operator([letter, ?~ | rest], _count, [op], _call_op?)
|
||||
when op in '<|/' and (letter in ?A..?Z or letter in ?a..?z) and
|
||||
(rest == [] or hd(rest) not in @tilde_op_prefix) do
|
||||
{:none, 0}
|
||||
end
|
||||
|
||||
defp operator(rest, count, acc, _call_op?) do
|
||||
case :elixir_tokenizer.tokenize(acc, 1, 1, []) do
|
||||
{:ok, _, _, _, [{:atom, _, _}]} ->
|
||||
{{:unquoted_atom, tl(acc)}, count}
|
||||
|
||||
{:ok, _, _, _, [{_, _, op}]} ->
|
||||
{rest, dot_count} = strip_spaces(rest, count)
|
||||
|
||||
cond do
|
||||
Code.Identifier.unary_op(op) == :error and Code.Identifier.binary_op(op) == :error ->
|
||||
:none
|
||||
|
||||
match?([?. | rest] when rest == [] or hd(rest) != ?., rest) ->
|
||||
dot(tl(rest), dot_count + 1, acc)
|
||||
|
||||
true ->
|
||||
{{:operator, acc}, count}
|
||||
end
|
||||
|
||||
_ ->
|
||||
{:none, 0}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a string and returns the surround context.
|
||||
|
||||
This function receives a string with an Elixir code fragment
|
||||
and a `position`. It returns a map containing the beginning
|
||||
and ending of the identifier alongside its context, or `:none`
|
||||
if there is nothing with a known context.
|
||||
|
||||
The difference between `cursor_context/2` and `surround_context/3`
|
||||
is that the former assumes the expression in the code fragment
|
||||
is incomplete. For example, `do` in `cursor_context/2` may be
|
||||
a keyword or a variable or a local call, while `surround_context/3`
|
||||
assumes the expression in the code fragment is complete, therefore
|
||||
`do` would always be a keyword.
|
||||
|
||||
The `position` contains both the `line` and `column`, both starting
|
||||
with the index of 1. The column must precede the surrounding expression.
|
||||
For example, the expression `foo`, will return something for the columns
|
||||
1, 2, and 3, but not 4:
|
||||
|
||||
foo
|
||||
^ column 1
|
||||
|
||||
foo
|
||||
^ column 2
|
||||
|
||||
foo
|
||||
^ column 3
|
||||
|
||||
foo
|
||||
^ column 4
|
||||
|
||||
The returned map contains the column the expression starts and the
|
||||
first column after the expression ends.
|
||||
|
||||
Similar to `cursor_context/2`, this function also provides a best-effort
|
||||
detection and may not be accurate under all circumstances. See the
|
||||
"Return values" and "Limitations" section under `cursor_context/2` for
|
||||
more information.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.Fragment.surround_context("foo", {1, 1})
|
||||
%{begin: {1, 1}, context: {:local_or_var, 'foo'}, end: {1, 4}}
|
||||
|
||||
## Differences to `cursor_context/2`
|
||||
|
||||
Because `surround_context/3` deals with complete code, it has some
|
||||
difference to `cursor_context/2`:
|
||||
|
||||
* `dot_call`/`dot_arity` and `operator_call`/`operator_arity`
|
||||
are collapsed into `dot` and `operator` contexts respectively
|
||||
as there aren't any meaningful distinctions between them
|
||||
|
||||
* On the other hand, this function still makes a distinction between
|
||||
`local_call`/`local_arity` and `local_or_var`, since the latter can
|
||||
be a local or variable
|
||||
|
||||
* `@` when not followed by any identifier is returned as `{:operator, '@'}`
|
||||
(in contrast to `{:module_attribute, ''}` in `cursor_context/2`
|
||||
|
||||
* This function never returns empty sigils `{:sigil, ''}` or empty structs
|
||||
`{:struct, ''}` as context
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec surround_context(List.Chars.t(), position(), keyword()) ::
|
||||
%{begin: position, end: position, context: context} | :none
|
||||
when context:
|
||||
{:alias, charlist}
|
||||
| {:dot, inside_dot, charlist}
|
||||
| {:local_or_var, charlist}
|
||||
| {:local_arity, charlist}
|
||||
| {:local_call, charlist}
|
||||
| {:module_attribute, charlist}
|
||||
| {:operator, charlist}
|
||||
| {:unquoted_atom, charlist},
|
||||
inside_dot:
|
||||
{:alias, charlist}
|
||||
| {:dot, inside_dot, charlist}
|
||||
| {:module_attribute, charlist}
|
||||
| {:unquoted_atom, charlist}
|
||||
| {:var, charlist}
|
||||
def surround_context(fragment, position, options \\ [])
|
||||
|
||||
def surround_context(binary, {line, column}, opts) when is_binary(binary) do
|
||||
binary
|
||||
|> String.split("\n")
|
||||
|> Enum.at(line - 1, '')
|
||||
|> String.to_charlist()
|
||||
|> position_surround_context(line, column, opts)
|
||||
end
|
||||
|
||||
def surround_context(charlist, {line, column}, opts) when is_list(charlist) do
|
||||
charlist
|
||||
|> :string.split('\n', :all)
|
||||
|> Enum.at(line - 1, '')
|
||||
|> position_surround_context(line, column, opts)
|
||||
end
|
||||
|
||||
def surround_context(other, {_, _} = position, opts) do
|
||||
surround_context(to_charlist(other), position, opts)
|
||||
end
|
||||
|
||||
defp position_surround_context(charlist, line, column, opts)
|
||||
when is_integer(line) and line >= 1 and is_integer(column) and column >= 1 do
|
||||
{reversed_pre, post} = string_reverse_at(charlist, column - 1, [])
|
||||
{reversed_pre, post} = adjust_position(reversed_pre, post)
|
||||
|
||||
case take_identifier(post, []) do
|
||||
{_, [], _} ->
|
||||
maybe_operator(reversed_pre, post, line, opts)
|
||||
|
||||
{:identifier, reversed_post, rest} ->
|
||||
{rest, _} = strip_spaces(rest, 0)
|
||||
reversed = reversed_post ++ reversed_pre
|
||||
|
||||
case codepoint_cursor_context(reversed, opts) do
|
||||
{{:struct, acc}, offset} ->
|
||||
build_surround({:struct, acc}, reversed, line, offset)
|
||||
|
||||
{{:alias, acc}, offset} ->
|
||||
build_surround({:alias, acc}, reversed, line, offset)
|
||||
|
||||
{{:dot, _, [_ | _]} = dot, offset} ->
|
||||
build_surround(dot, reversed, line, offset)
|
||||
|
||||
{{:local_or_var, acc}, offset} when hd(rest) == ?( ->
|
||||
build_surround({:local_call, acc}, reversed, line, offset)
|
||||
|
||||
{{:local_or_var, acc}, offset} when hd(rest) == ?/ ->
|
||||
build_surround({:local_arity, acc}, reversed, line, offset)
|
||||
|
||||
{{:local_or_var, acc}, offset} when acc in @textual_operators ->
|
||||
build_surround({:operator, acc}, reversed, line, offset)
|
||||
|
||||
{{:local_or_var, acc}, offset} when acc not in ~w(do end after else catch rescue)c ->
|
||||
build_surround({:local_or_var, acc}, reversed, line, offset)
|
||||
|
||||
{{:module_attribute, ''}, offset} ->
|
||||
build_surround({:operator, '@'}, reversed, line, offset)
|
||||
|
||||
{{:module_attribute, acc}, offset} ->
|
||||
build_surround({:module_attribute, acc}, reversed, line, offset)
|
||||
|
||||
{{:sigil, acc}, offset} ->
|
||||
build_surround({:sigil, acc}, reversed, line, offset)
|
||||
|
||||
{{:unquoted_atom, acc}, offset} ->
|
||||
build_surround({:unquoted_atom, acc}, reversed, line, offset)
|
||||
|
||||
_ ->
|
||||
maybe_operator(reversed_pre, post, line, opts)
|
||||
end
|
||||
|
||||
{:alias, reversed_post, _rest} ->
|
||||
reversed = reversed_post ++ reversed_pre
|
||||
|
||||
case codepoint_cursor_context(reversed, opts) do
|
||||
{{:alias, acc}, offset} ->
|
||||
build_surround({:alias, acc}, reversed, line, offset)
|
||||
|
||||
{{:struct, acc}, offset} ->
|
||||
build_surround({:struct, acc}, reversed, line, offset)
|
||||
|
||||
_ ->
|
||||
:none
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp maybe_operator(reversed_pre, post, line, opts) do
|
||||
case take_operator(post, []) do
|
||||
{[], _rest} ->
|
||||
:none
|
||||
|
||||
{reversed_post, rest} ->
|
||||
reversed = reversed_post ++ reversed_pre
|
||||
|
||||
case codepoint_cursor_context(reversed, opts) do
|
||||
{{:operator, acc}, offset} when acc not in @incomplete_operators ->
|
||||
build_surround({:operator, acc}, reversed, line, offset)
|
||||
|
||||
{{:sigil, ''}, offset} when hd(rest) in ?A..?Z or hd(rest) in ?a..?z ->
|
||||
build_surround({:sigil, [hd(rest)]}, [hd(rest) | reversed], line, offset + 1)
|
||||
|
||||
{{:dot, _, [_ | _]} = dot, offset} ->
|
||||
build_surround(dot, reversed, line, offset)
|
||||
|
||||
_ ->
|
||||
:none
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp build_surround(context, reversed, line, offset) do
|
||||
{post, reversed_pre} = enum_reverse_at(reversed, offset, [])
|
||||
pre = :lists.reverse(reversed_pre)
|
||||
pre_length = :string.length(pre) + 1
|
||||
|
||||
%{
|
||||
context: context,
|
||||
begin: {line, pre_length},
|
||||
end: {line, pre_length + :string.length(post)}
|
||||
}
|
||||
end
|
||||
|
||||
defp take_identifier([h | t], acc) when h in @trailing_identifier,
|
||||
do: {:identifier, [h | acc], t}
|
||||
|
||||
defp take_identifier([h | t], acc) when h not in @non_identifier,
|
||||
do: take_identifier(t, [h | acc])
|
||||
|
||||
defp take_identifier(rest, acc) do
|
||||
with {[?. | t], _} <- strip_spaces(rest, 0),
|
||||
{[h | _], _} when h in ?A..?Z <- strip_spaces(t, 0) do
|
||||
take_alias(rest, acc)
|
||||
else
|
||||
_ -> {:identifier, acc, rest}
|
||||
end
|
||||
end
|
||||
|
||||
defp take_alias([h | t], acc) when h not in @non_identifier,
|
||||
do: take_alias(t, [h | acc])
|
||||
|
||||
defp take_alias(rest, acc) do
|
||||
with {[?. | t], acc} <- move_spaces(rest, acc),
|
||||
{[h | t], acc} when h in ?A..?Z <- move_spaces(t, [?. | acc]) do
|
||||
take_alias(t, [h | acc])
|
||||
else
|
||||
_ -> {:alias, acc, rest}
|
||||
end
|
||||
end
|
||||
|
||||
defp take_operator([h | t], acc) when h in @operators, do: take_operator(t, [h | acc])
|
||||
defp take_operator([h | t], acc) when h == ?., do: take_operator(t, [h | acc])
|
||||
defp take_operator(rest, acc), do: {acc, rest}
|
||||
|
||||
# Unquoted atom handling
|
||||
defp adjust_position(reversed_pre, [?: | post])
|
||||
when hd(post) != ?: and (reversed_pre == [] or hd(reversed_pre) != ?:) do
|
||||
{[?: | reversed_pre], post}
|
||||
end
|
||||
|
||||
defp adjust_position(reversed_pre, [?% | post]) do
|
||||
adjust_position([?% | reversed_pre], post)
|
||||
end
|
||||
|
||||
# Dot/struct handling
|
||||
defp adjust_position(reversed_pre, post) do
|
||||
case move_spaces(post, reversed_pre) do
|
||||
# If we are between spaces and a dot, move past the dot
|
||||
{[?. | post], reversed_pre} when hd(post) != ?. and hd(reversed_pre) != ?. ->
|
||||
{post, reversed_pre} = move_spaces(post, [?. | reversed_pre])
|
||||
{reversed_pre, post}
|
||||
|
||||
_ ->
|
||||
case strip_spaces(reversed_pre, 0) do
|
||||
# If there is a dot to our left, make sure to move to the first character
|
||||
{[?. | rest], _} when rest == [] or hd(rest) not in '.:' ->
|
||||
{post, reversed_pre} = move_spaces(post, reversed_pre)
|
||||
{reversed_pre, post}
|
||||
|
||||
# If there is a % to our left, make sure to move to the first character
|
||||
{[?% | _], _} ->
|
||||
case move_spaces(post, reversed_pre) do
|
||||
{[h | _] = post, reversed_pre} when h in ?A..?Z ->
|
||||
{reversed_pre, post}
|
||||
|
||||
_ ->
|
||||
{reversed_pre, post}
|
||||
end
|
||||
|
||||
_ ->
|
||||
{reversed_pre, post}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp move_spaces([h | t], acc) when h in @space, do: move_spaces(t, [h | acc])
|
||||
defp move_spaces(t, acc), do: {t, acc}
|
||||
|
||||
defp string_reverse_at(charlist, 0, acc), do: {acc, charlist}
|
||||
|
||||
defp string_reverse_at(charlist, n, acc) do
|
||||
case :unicode_util.gc(charlist) do
|
||||
[gc | cont] when is_integer(gc) -> string_reverse_at(cont, n - 1, [gc | acc])
|
||||
[gc | cont] when is_list(gc) -> string_reverse_at(cont, n - 1, :lists.reverse(gc, acc))
|
||||
[] -> {acc, []}
|
||||
end
|
||||
end
|
||||
|
||||
defp enum_reverse_at([h | t], n, acc) when n > 0, do: enum_reverse_at(t, n - 1, [h | acc])
|
||||
defp enum_reverse_at(rest, _, acc), do: {acc, rest}
|
||||
|
||||
@doc """
|
||||
Receives a code fragment and returns a quoted expression
|
||||
with a cursor at the nearest argument position.
|
||||
|
||||
A container is any Elixir expression starting with `(`,
|
||||
`{`, and `[`. This includes function calls, tuples, lists,
|
||||
maps, and so on. For example, take this code, which would
|
||||
be given as input:
|
||||
|
||||
max(some_value,
|
||||
|
||||
This function will return the AST equivalent to:
|
||||
|
||||
max(some_value, __cursor__())
|
||||
|
||||
In other words, this function is capable of closing any open
|
||||
brackets and insert the cursor position. Any content at the
|
||||
cursor position that is after a comma or an opening bracket
|
||||
is discarded. For example, if this is given as input:
|
||||
|
||||
max(some_value, another_val
|
||||
|
||||
It will return the same AST:
|
||||
|
||||
max(some_value, __cursor__())
|
||||
|
||||
Similarly, if only this is given:
|
||||
|
||||
max(some_va
|
||||
|
||||
Then it returns:
|
||||
|
||||
max(__cursor__())
|
||||
|
||||
Calls without parenthesis are also supported, as we assume the
|
||||
brackets are implicit.
|
||||
|
||||
Operators and anonymous functions are not containers, and therefore
|
||||
will be discarded. The following will all return the same AST:
|
||||
|
||||
max(some_value,
|
||||
max(some_value, fn x -> x end
|
||||
max(some_value, 1 + another_val
|
||||
max(some_value, 1 |> some_fun() |> another_fun
|
||||
|
||||
On the other hand, tuples, lists, maps, etc all retain the
|
||||
cursor position:
|
||||
|
||||
max(some_value, [1, 2,
|
||||
|
||||
Returns the following AST:
|
||||
|
||||
max(some_value, [1, 2, __cursor__()])
|
||||
|
||||
Keyword lists (and do-end blocks) are also retained. The following:
|
||||
|
||||
if(some_value, do:
|
||||
if(some_value, do: :token
|
||||
if(some_value, do: 1 + val
|
||||
|
||||
all return:
|
||||
|
||||
if(some_value, do: __cursor__())
|
||||
|
||||
The AST returned by this function is not safe to evaluate but
|
||||
it can be analyzed and expanded.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.Fragment.container_cursor_to_quoted("max(some_value, ")
|
||||
{:ok, {:max, [line: 1], [{:some_value, [line: 1], nil}, {:__cursor__, [line: 1], []}]}}
|
||||
|
||||
## Options
|
||||
|
||||
* `:file` - the filename to be reported in case of parsing errors.
|
||||
Defaults to `"nofile"`.
|
||||
|
||||
* `:line` - the starting line of the string being parsed.
|
||||
Defaults to 1.
|
||||
|
||||
* `:column` - the starting column of the string being parsed.
|
||||
Defaults to 1.
|
||||
|
||||
* `:columns` - when `true`, attach a `:column` key to the quoted
|
||||
metadata. Defaults to `false`.
|
||||
|
||||
* `:token_metadata` - when `true`, includes token-related
|
||||
metadata in the expression AST, such as metadata for `do` and `end`
|
||||
tokens, for closing tokens, end of expressions, as well as delimiters
|
||||
for sigils. See `t:Macro.metadata/0`. Defaults to `false`.
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec container_cursor_to_quoted(List.Chars.t(), keyword()) ::
|
||||
{:ok, Macro.t()} | {:error, {location :: keyword, binary | {binary, binary}, binary}}
|
||||
def container_cursor_to_quoted(fragment, opts \\ []) do
|
||||
file = Keyword.get(opts, :file, "nofile")
|
||||
line = Keyword.get(opts, :line, 1)
|
||||
column = Keyword.get(opts, :column, 1)
|
||||
columns = Keyword.get(opts, :columns, false)
|
||||
token_metadata = Keyword.get(opts, :token_metadata, false)
|
||||
|
||||
fragment = to_charlist(fragment)
|
||||
tokenizer_opts = [file: file, cursor_completion: true, columns: columns]
|
||||
|
||||
case :elixir_tokenizer.tokenize(fragment, line, column, tokenizer_opts) do
|
||||
{:ok, _, _, _warnings, tokens} ->
|
||||
:elixir.tokens_to_quoted(tokens, nil, columns: columns, token_metadata: token_metadata)
|
||||
|
||||
{:error, {line, column, {prefix, suffix}, token}, _rest, _warnings, _so_far} ->
|
||||
location = [line: line, column: column]
|
||||
{:error, {location, {to_string(prefix), to_string(suffix)}, to_string(token)}}
|
||||
|
||||
{:error, {line, column, error, token}, _rest, _warnings, _so_far} ->
|
||||
location = [line: line, column: column]
|
||||
{:error, {location, to_string(error), to_string(token)}}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -37,18 +37,16 @@ defmodule Code.Identifier do
|
||||
op in [:"::"] -> {:right, 60}
|
||||
op in [:|] -> {:right, 70}
|
||||
op in [:=] -> {:right, 100}
|
||||
op in [:||, :|||, :or] -> {:left, 120}
|
||||
op in [:&&, :&&&, :and] -> {:left, 130}
|
||||
op in [:==, :!=, :=~, :===, :!==] -> {:left, 140}
|
||||
op in [:<, :<=, :>=, :>] -> {:left, 150}
|
||||
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>] -> {:left, 160}
|
||||
op in [:in] -> {:left, 170}
|
||||
op in [:^^^] -> {:left, 180}
|
||||
op in [:"//"] -> {:right, 190}
|
||||
op in [:||, :|||, :or] -> {:left, 130}
|
||||
op in [:&&, :&&&, :and] -> {:left, 140}
|
||||
op in [:==, :!=, :=~, :===, :!==] -> {:left, 150}
|
||||
op in [:<, :<=, :>=, :>] -> {:left, 160}
|
||||
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>] -> {:left, 170}
|
||||
op in [:in] -> {:left, 180}
|
||||
op in [:^^^] -> {:left, 190}
|
||||
op in [:++, :--, :.., :<>, :+++, :---] -> {:right, 200}
|
||||
op in [:+, :-] -> {:left, 210}
|
||||
op in [:*, :/] -> {:left, 220}
|
||||
op in [:**] -> {:left, 230}
|
||||
op in [:.] -> {:left, 310}
|
||||
true -> :error
|
||||
end
|
||||
@@ -70,11 +68,10 @@ defmodule Code.Identifier do
|
||||
the ambiguity between the atom and the keyword identifier
|
||||
|
||||
* `:not_callable` - an atom that cannot be used as a function call after the
|
||||
`.` operator. Those are typically AST nodes that are special forms (such as
|
||||
`:%{}` and `:<<>>>`) as well as nodes that are ambiguous in calls (such as
|
||||
`:..` and `:...`). This category also includes atoms like `:Foo`, since
|
||||
they are valid identifiers but they need quotes to be used in function
|
||||
calls (`Foo."Bar"`)
|
||||
`.` operator (for example, `:<<>>` is not callable because `Foo.<<>>` is a
|
||||
syntax error); this category includes atoms like `:Foo`, since they are
|
||||
valid identifiers but they need quotes to be used in function calls
|
||||
(`Foo."Bar"`)
|
||||
|
||||
* `:other` - any other atom (these are usually escaped when inspected, like
|
||||
`:"foo and bar"`)
|
||||
@@ -84,10 +81,10 @@ defmodule Code.Identifier do
|
||||
charlist = Atom.to_charlist(atom)
|
||||
|
||||
cond do
|
||||
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :"..//", :->] ->
|
||||
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :->] ->
|
||||
:not_callable
|
||||
|
||||
atom in [:"::", :"//"] ->
|
||||
atom in [:"::"] ->
|
||||
:not_atomable
|
||||
|
||||
unary_op(atom) != :error or binary_op(atom) != :error ->
|
||||
@@ -97,7 +94,7 @@ defmodule Code.Identifier do
|
||||
:alias
|
||||
|
||||
true ->
|
||||
case :elixir_config.identifier_tokenizer().tokenize(charlist) do
|
||||
case :elixir_config.get(:identifier_tokenizer, String.Tokenizer).tokenize(charlist) do
|
||||
{kind, _acc, [], _, _, special} ->
|
||||
if kind == :identifier and not :lists.member(?@, special) do
|
||||
:callable_local
|
||||
|
||||
@@ -1,581 +0,0 @@
|
||||
defmodule Code.Normalizer do
|
||||
@moduledoc false
|
||||
|
||||
defguard is_literal(x)
|
||||
when is_integer(x) or
|
||||
is_float(x) or
|
||||
is_binary(x) or
|
||||
is_atom(x)
|
||||
|
||||
@doc """
|
||||
Wraps literals in the quoted expression to conform to the AST format expected
|
||||
by the formatter.
|
||||
"""
|
||||
def normalize(quoted, opts \\ []) do
|
||||
line = Keyword.get(opts, :line, nil)
|
||||
escape = Keyword.get(opts, :escape, true)
|
||||
locals_without_parens = Keyword.get(opts, :locals_without_parens, [])
|
||||
|
||||
state = %{
|
||||
escape: escape,
|
||||
parent_meta: [line: line],
|
||||
locals_without_parens: locals_without_parens ++ Code.Formatter.locals_without_parens()
|
||||
}
|
||||
|
||||
do_normalize(quoted, state)
|
||||
end
|
||||
|
||||
# Wrapped literals should receive the block meta
|
||||
defp do_normalize({:__block__, meta, [literal]}, state)
|
||||
when not is_tuple(literal) or tuple_size(literal) == 2 do
|
||||
normalize_literal(literal, meta, state)
|
||||
end
|
||||
|
||||
# Only normalize the first argument of an alias if it's not an atom
|
||||
defp do_normalize({:__aliases__, meta, [first | rest]}, state) when not is_atom(first) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
first = do_normalize(first, %{state | parent_meta: meta})
|
||||
{:__aliases__, meta, [first | rest]}
|
||||
end
|
||||
|
||||
defp do_normalize({:__aliases__, _, _} = quoted, _state) do
|
||||
quoted
|
||||
end
|
||||
|
||||
# Skip captured arguments like &1
|
||||
defp do_normalize({:&, meta, [term]}, state) when is_integer(term) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
{:&, meta, [term]}
|
||||
end
|
||||
|
||||
# Ranges
|
||||
defp do_normalize(left..right//step, state) do
|
||||
left = do_normalize(left, state)
|
||||
right = do_normalize(right, state)
|
||||
meta = meta_line(state)
|
||||
|
||||
if step == 1 do
|
||||
{:.., meta, [left, right]}
|
||||
else
|
||||
step = do_normalize(step, state)
|
||||
{:"..//", meta, [left, right, step]}
|
||||
end
|
||||
end
|
||||
|
||||
# Bit containers
|
||||
defp do_normalize({:<<>>, _, args} = quoted, state) when is_list(args) do
|
||||
normalize_bitstring(quoted, state)
|
||||
end
|
||||
|
||||
# Atoms with interpolations
|
||||
defp do_normalize(
|
||||
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta,
|
||||
[{:<<>>, _, args} = string, :utf8]},
|
||||
state
|
||||
)
|
||||
when is_list(args) do
|
||||
dot_meta = patch_meta_line(dot_meta, state.parent_meta)
|
||||
call_meta = patch_meta_line(call_meta, dot_meta)
|
||||
|
||||
string =
|
||||
if state.escape do
|
||||
normalize_bitstring(string, state, true)
|
||||
else
|
||||
normalize_bitstring(string, state)
|
||||
end
|
||||
|
||||
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta, [string, :utf8]}
|
||||
end
|
||||
|
||||
# Charlists with interpolations
|
||||
defp do_normalize({{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]}, state) do
|
||||
parts =
|
||||
Enum.map(parts, fn
|
||||
{{:., part_dot_meta, [Kernel, :to_string]}, part_call_meta, args} ->
|
||||
args = normalize_args(args, state)
|
||||
|
||||
{{:., part_dot_meta, [Kernel, :to_string]}, part_call_meta, args}
|
||||
|
||||
part ->
|
||||
if state.escape do
|
||||
maybe_escape_literal(part, state)
|
||||
else
|
||||
part
|
||||
end
|
||||
end)
|
||||
|
||||
{{:., dot_meta, [List, :to_charlist]}, call_meta, [parts]}
|
||||
end
|
||||
|
||||
# Don't normalize the `Access` atom in access syntax
|
||||
defp do_normalize({:., meta, [Access, :get]}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
{:., meta, [Access, :get]}
|
||||
end
|
||||
|
||||
# Only normalize the left side of the dot operator
|
||||
# The right hand side is an atom in the AST but it's not an atom literal, so
|
||||
# it should not be wrapped
|
||||
defp do_normalize({:., meta, [left, right]}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
|
||||
left = do_normalize(left, %{state | parent_meta: meta})
|
||||
|
||||
{:., meta, [left, right]}
|
||||
end
|
||||
|
||||
# A list of left to right arrows is not considered as a list literal, so it's not wrapped
|
||||
defp do_normalize([{:->, _, [_ | _]} | _] = quoted, state) do
|
||||
normalize_args(quoted, state)
|
||||
end
|
||||
|
||||
# left -> right
|
||||
defp do_normalize({:->, meta, [left, right]}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
|
||||
left = normalize_args(left, %{state | parent_meta: meta})
|
||||
right = do_normalize(right, %{state | parent_meta: meta})
|
||||
{:->, meta, [left, right]}
|
||||
end
|
||||
|
||||
# Maps
|
||||
defp do_normalize({:%{}, meta, args}, state) when is_list(args) do
|
||||
meta =
|
||||
if meta == [] do
|
||||
line = state.parent_meta[:line]
|
||||
[line: line, closing: [line: line]]
|
||||
else
|
||||
meta
|
||||
end
|
||||
|
||||
state = %{state | parent_meta: meta}
|
||||
|
||||
args =
|
||||
case args do
|
||||
[{:|, pipe_meta, [left, right]}] ->
|
||||
left = do_normalize(left, state)
|
||||
right = normalize_map_args(right, state)
|
||||
[{:|, pipe_meta, [left, right]}]
|
||||
|
||||
[{_, _, _} = call] ->
|
||||
[do_normalize(call, state)]
|
||||
|
||||
args ->
|
||||
normalize_map_args(args, state)
|
||||
end
|
||||
|
||||
{:%{}, meta, args}
|
||||
end
|
||||
|
||||
# Sigils
|
||||
defp do_normalize({sigil, meta, [{:<<>>, _, args} = string, modifiers]} = quoted, state)
|
||||
when is_list(args) and is_atom(sigil) do
|
||||
case Atom.to_string(sigil) do
|
||||
<<"sigil_", _name>> ->
|
||||
meta =
|
||||
meta
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
|> Keyword.put_new(:delimiter, "\"")
|
||||
|
||||
{sigil, meta, [do_normalize(string, %{state | parent_meta: meta}), modifiers]}
|
||||
|
||||
_ ->
|
||||
normalize_call(quoted, state)
|
||||
end
|
||||
end
|
||||
|
||||
# Tuples
|
||||
defp do_normalize({:{}, meta, args} = quoted, state) when is_list(args) do
|
||||
{last_arg, args} = List.pop_at(args, -1)
|
||||
|
||||
if args != [] and match?([_ | _], last_arg) and keyword?(last_arg) do
|
||||
args = normalize_args(args, state)
|
||||
kw_list = normalize_kw_args(last_arg, state, true)
|
||||
{:{}, meta, args ++ kw_list}
|
||||
else
|
||||
normalize_call(quoted, state)
|
||||
end
|
||||
end
|
||||
|
||||
# Module attributes
|
||||
defp do_normalize({:@, meta, [{name, name_meta, [value]}]}, state) do
|
||||
value =
|
||||
cond do
|
||||
keyword?(value) ->
|
||||
normalize_kw_args(value, state, true)
|
||||
|
||||
is_list(value) ->
|
||||
normalize_literal(value, meta, state)
|
||||
|
||||
true ->
|
||||
do_normalize(value, state)
|
||||
end
|
||||
|
||||
{:@, meta, [{name, name_meta, [value]}]}
|
||||
end
|
||||
|
||||
# Regular blocks
|
||||
defp do_normalize({:__block__, meta, args}, state) when is_list(args) do
|
||||
{:__block__, meta, normalize_args(args, state)}
|
||||
end
|
||||
|
||||
# Calls
|
||||
defp do_normalize({_, _, args} = quoted, state) when is_list(args) do
|
||||
normalize_call(quoted, state)
|
||||
end
|
||||
|
||||
# Vars
|
||||
defp do_normalize({_, _, context} = quoted, _state) when is_atom(context) do
|
||||
quoted
|
||||
end
|
||||
|
||||
# Literals
|
||||
defp do_normalize(quoted, state) do
|
||||
normalize_literal(quoted, [], state)
|
||||
end
|
||||
|
||||
# Numbers
|
||||
defp normalize_literal(number, meta, state) when is_number(number) do
|
||||
meta =
|
||||
meta
|
||||
|> Keyword.put_new(:token, inspect(number))
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
|
||||
{:__block__, meta, [number]}
|
||||
end
|
||||
|
||||
# Atom, Strings
|
||||
defp normalize_literal(literal, meta, state) when is_atom(literal) or is_binary(literal) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
literal = maybe_escape_literal(literal, state)
|
||||
|
||||
if is_atom(literal) and Code.Identifier.classify(literal) == :alias and
|
||||
is_nil(meta[:delimiter]) do
|
||||
"Elixir." <> segments = Atom.to_string(literal)
|
||||
|
||||
segments =
|
||||
segments
|
||||
|> String.split(".")
|
||||
|> Enum.map(&String.to_atom/1)
|
||||
|
||||
{:__aliases__, meta, segments}
|
||||
else
|
||||
{:__block__, meta, [literal]}
|
||||
end
|
||||
end
|
||||
|
||||
# 2-tuples
|
||||
defp normalize_literal({left, right}, meta, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
state = %{state | parent_meta: meta}
|
||||
|
||||
if match?([_ | _], right) and keyword?(right) do
|
||||
{:__block__, meta, [{do_normalize(left, state), normalize_kw_args(right, state, true)}]}
|
||||
else
|
||||
{:__block__, meta, [{do_normalize(left, state), do_normalize(right, state)}]}
|
||||
end
|
||||
end
|
||||
|
||||
# Lists
|
||||
defp normalize_literal(list, meta, state) when is_list(list) do
|
||||
if list != [] and List.ascii_printable?(list) do
|
||||
# It's a charlist
|
||||
list =
|
||||
if state.escape do
|
||||
{string, _} = Code.Identifier.escape(IO.chardata_to_string(list), -1)
|
||||
IO.iodata_to_binary(string) |> to_charlist()
|
||||
else
|
||||
list
|
||||
end
|
||||
|
||||
meta =
|
||||
meta
|
||||
|> Keyword.put_new(:delimiter, "'")
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
|
||||
{:__block__, meta, [list]}
|
||||
else
|
||||
meta =
|
||||
if line = state.parent_meta[:line] do
|
||||
meta
|
||||
|> Keyword.put_new(:closing, line: line)
|
||||
|> patch_meta_line(state.parent_meta)
|
||||
else
|
||||
meta
|
||||
end
|
||||
|
||||
{:__block__, meta, [normalize_kw_args(list, state, false)]}
|
||||
end
|
||||
end
|
||||
|
||||
# Probably an invalid value, wrap it and send it upstream
|
||||
defp normalize_literal(quoted, meta, _state) do
|
||||
{:__block__, meta, [quoted]}
|
||||
end
|
||||
|
||||
defp normalize_call({form, meta, args}, state) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
arity = length(args)
|
||||
|
||||
# Only normalize the form if it's a qualified call
|
||||
form =
|
||||
if is_atom(form) do
|
||||
form
|
||||
else
|
||||
do_normalize(form, %{state | parent_meta: meta})
|
||||
end
|
||||
|
||||
meta =
|
||||
if is_nil(meta[:no_parens]) and is_nil(meta[:closing]) and is_nil(meta[:do]) and
|
||||
not Code.Formatter.local_without_parens?(form, arity, state.locals_without_parens) do
|
||||
[closing: [line: meta[:line]]] ++ meta
|
||||
else
|
||||
meta
|
||||
end
|
||||
|
||||
cond do
|
||||
Keyword.has_key?(meta, :do) or match?([{{:__block__, _, [:do]}, _} | _], List.last(args)) ->
|
||||
# def foo do :ok end
|
||||
# def foo, do: :ok
|
||||
normalize_kw_blocks(form, meta, args, state)
|
||||
|
||||
match?([{:do, _} | _], List.last(args)) ->
|
||||
# Non normalized kw blocks
|
||||
line = state.parent_meta[:line]
|
||||
meta = meta ++ [do: [line: line], end: [line: line]]
|
||||
normalize_kw_blocks(form, meta, args, state)
|
||||
|
||||
allow_keyword?(form, arity) ->
|
||||
args = normalize_args(args, %{state | parent_meta: state.parent_meta})
|
||||
{last_arg, leading_args} = List.pop_at(args, -1, [])
|
||||
|
||||
last_args =
|
||||
case last_arg do
|
||||
{:__block__, _, [[{{:__block__, key_meta, _}, _} | _]] = last_args} ->
|
||||
if key_meta[:format] == :keyword do
|
||||
last_args
|
||||
else
|
||||
[last_arg]
|
||||
end
|
||||
|
||||
[] ->
|
||||
[]
|
||||
|
||||
_ ->
|
||||
[last_arg]
|
||||
end
|
||||
|
||||
{form, meta, leading_args ++ last_args}
|
||||
|
||||
true ->
|
||||
args = normalize_args(args, %{state | parent_meta: state.parent_meta})
|
||||
{form, meta, args}
|
||||
end
|
||||
end
|
||||
|
||||
defp allow_keyword?(:when, 2), do: true
|
||||
defp allow_keyword?(:{}, _), do: false
|
||||
defp allow_keyword?(op, arity), do: not is_atom(op) or not Macro.operator?(op, arity)
|
||||
|
||||
defp normalize_bitstring({:<<>>, meta, parts} = quoted, state, escape_interpolation \\ false) do
|
||||
meta = patch_meta_line(meta, state.parent_meta)
|
||||
|
||||
parts =
|
||||
if interpolated?(quoted) do
|
||||
normalize_interpolation_parts(parts, %{state | parent_meta: meta}, escape_interpolation)
|
||||
else
|
||||
state = %{state | parent_meta: meta}
|
||||
|
||||
Enum.map(parts, fn part ->
|
||||
with {:"::", meta, [left, _]} <- part,
|
||||
true <- meta[:inferred_bitstring_spec] do
|
||||
do_normalize(left, state)
|
||||
else
|
||||
_ -> do_normalize(part, state)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
{:<<>>, meta, parts}
|
||||
end
|
||||
|
||||
defp normalize_interpolation_parts(parts, state, escape_interpolation) do
|
||||
Enum.map(parts, fn
|
||||
{:"::", interpolation_meta,
|
||||
[
|
||||
{{:., dot_meta, [Kernel, :to_string]}, middle_meta, [middle]},
|
||||
{:binary, binary_meta, context}
|
||||
]} ->
|
||||
middle = do_normalize(middle, %{state | parent_meta: dot_meta})
|
||||
|
||||
{:"::", interpolation_meta,
|
||||
[
|
||||
{{:., dot_meta, [Kernel, :to_string]}, middle_meta, [middle]},
|
||||
{:binary, binary_meta, context}
|
||||
]}
|
||||
|
||||
part ->
|
||||
if escape_interpolation do
|
||||
maybe_escape_literal(part, state)
|
||||
else
|
||||
part
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp normalize_map_args(args, state) do
|
||||
Enum.map(normalize_kw_args(args, state, false), fn
|
||||
{:__block__, _, [{_, _} = pair]} -> pair
|
||||
pair -> pair
|
||||
end)
|
||||
end
|
||||
|
||||
defp normalize_kw_blocks(form, meta, args, state) do
|
||||
{kw_blocks, leading_args} = List.pop_at(args, -1)
|
||||
|
||||
kw_blocks =
|
||||
Enum.map(kw_blocks, fn {tag, block} ->
|
||||
block = do_normalize(block, %{state | parent_meta: meta})
|
||||
|
||||
block =
|
||||
case block do
|
||||
{_, _, [[{:->, _, _} | _] = block]} -> block
|
||||
block -> block
|
||||
end
|
||||
|
||||
# Only wrap the tag if it isn't already wrapped
|
||||
tag =
|
||||
case tag do
|
||||
{:__block__, _, _} -> tag
|
||||
_ -> {:__block__, [line: meta[:line]], [tag]}
|
||||
end
|
||||
|
||||
{tag, block}
|
||||
end)
|
||||
|
||||
leading_args = normalize_args(leading_args, %{state | parent_meta: meta})
|
||||
{form, meta, leading_args ++ [kw_blocks]}
|
||||
end
|
||||
|
||||
defp normalize_kw_args(elems, state, keyword?)
|
||||
|
||||
defp normalize_kw_args(
|
||||
[{{:__block__, key_meta, [key]}, value} = first | rest] = current,
|
||||
state,
|
||||
keyword?
|
||||
)
|
||||
when is_atom(key) do
|
||||
keyword? = keyword? or keyword?(current)
|
||||
|
||||
first =
|
||||
if key_meta[:format] == :keyword and not keyword? do
|
||||
key_meta = Keyword.delete(key_meta, :format)
|
||||
line = key_meta[:line] || meta_line(state)
|
||||
{:__block__, [line: line], [{{:__block__, key_meta, [key]}, value}]}
|
||||
else
|
||||
first
|
||||
end
|
||||
|
||||
[first | normalize_kw_args(rest, state, keyword?)]
|
||||
end
|
||||
|
||||
defp normalize_kw_args([{left, right} | rest] = current, state, keyword?) do
|
||||
keyword? = keyword? or keyword?(current)
|
||||
|
||||
left =
|
||||
if keyword? do
|
||||
meta = [format: :keyword] ++ meta_line(state)
|
||||
{:__block__, meta, [maybe_escape_literal(left, state)]}
|
||||
else
|
||||
do_normalize(left, state)
|
||||
end
|
||||
|
||||
right = do_normalize(right, state)
|
||||
|
||||
pair =
|
||||
with {:__block__, meta, _} <- left,
|
||||
:keyword <- meta[:format] do
|
||||
{left, right}
|
||||
else
|
||||
_ -> {:__block__, meta_line(state), [{left, right}]}
|
||||
end
|
||||
|
||||
[pair | normalize_kw_args(rest, state, keyword?)]
|
||||
end
|
||||
|
||||
defp normalize_kw_args([first | rest], state, keyword?) do
|
||||
[do_normalize(first, state) | normalize_kw_args(rest, state, keyword?)]
|
||||
end
|
||||
|
||||
defp normalize_kw_args([], _state, _keyword?) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp normalize_args(args, state) do
|
||||
Enum.map(args, &do_normalize(&1, state))
|
||||
end
|
||||
|
||||
defp maybe_escape_literal(string, %{escape: true}) when is_binary(string) do
|
||||
{string, _} = Code.Identifier.escape(string, -1)
|
||||
IO.iodata_to_binary(string)
|
||||
end
|
||||
|
||||
defp maybe_escape_literal(atom, %{escape: true} = state) when is_atom(atom) do
|
||||
atom
|
||||
|> Atom.to_string()
|
||||
|> maybe_escape_literal(state)
|
||||
|> String.to_atom()
|
||||
end
|
||||
|
||||
defp maybe_escape_literal(term, _) do
|
||||
term
|
||||
end
|
||||
|
||||
# Check if we have an interpolated string.
|
||||
defp interpolated?({:<<>>, _, [_ | _] = parts}) do
|
||||
Enum.all?(parts, fn
|
||||
{:"::", _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
|
||||
binary when is_binary(binary) -> true
|
||||
_ -> false
|
||||
end)
|
||||
end
|
||||
|
||||
defp interpolated?(_) do
|
||||
false
|
||||
end
|
||||
|
||||
defp patch_meta_line(meta, parent_meta) do
|
||||
with nil <- meta[:line],
|
||||
line when is_integer(line) <- parent_meta[:line] do
|
||||
[line: line] ++ meta
|
||||
else
|
||||
_ -> meta
|
||||
end
|
||||
end
|
||||
|
||||
defp meta_line(state) do
|
||||
if line = state.parent_meta[:line] do
|
||||
[line: line]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
defp keyword?([{{:__block__, key_meta, [key]}, _} | rest]) when is_atom(key) do
|
||||
if key_meta[:format] == :keyword do
|
||||
keyword?(rest)
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
defp keyword?([{key, _value} | rest]) when is_atom(key) do
|
||||
case Atom.to_charlist(key) do
|
||||
'Elixir.' ++ _ -> false
|
||||
_ -> keyword?(rest)
|
||||
end
|
||||
end
|
||||
|
||||
defp keyword?([]), do: true
|
||||
defp keyword?(_other), do: false
|
||||
end
|
||||
@@ -7,9 +7,9 @@ defmodule Code.Typespec do
|
||||
@spec spec_to_quoted(atom, tuple) :: {atom, keyword, [Macro.t()]}
|
||||
def spec_to_quoted(name, spec)
|
||||
|
||||
def spec_to_quoted(name, {:type, anno, :fun, [{:type, _, :product, args}, result]})
|
||||
def spec_to_quoted(name, {:type, line, :fun, [{:type, _, :product, args}, result]})
|
||||
when is_atom(name) do
|
||||
meta = meta(anno)
|
||||
meta = [line: line]
|
||||
body = {name, meta, Enum.map(args, &typespec_to_quoted/1)}
|
||||
|
||||
vars =
|
||||
@@ -27,13 +27,11 @@ defmodule Code.Typespec do
|
||||
end
|
||||
end
|
||||
|
||||
def spec_to_quoted(name, {:type, anno, :fun, []}) when is_atom(name) do
|
||||
meta = meta(anno)
|
||||
{:"::", meta, [{name, meta, []}, quote(do: term)]}
|
||||
def spec_to_quoted(name, {:type, line, :fun, []}) when is_atom(name) do
|
||||
{:"::", [line: line], [{name, [line: line], []}, quote(do: term)]}
|
||||
end
|
||||
|
||||
def spec_to_quoted(name, {:type, anno, :bounded_fun, [type, constrs]}) when is_atom(name) do
|
||||
meta = meta(anno)
|
||||
def spec_to_quoted(name, {:type, line, :bounded_fun, [type, constrs]}) when is_atom(name) do
|
||||
{:type, _, :fun, [{:type, _, :product, args}, result]} = type
|
||||
|
||||
guards =
|
||||
@@ -41,6 +39,7 @@ defmodule Code.Typespec do
|
||||
{erl_to_ex_var(var), typespec_to_quoted(type)}
|
||||
end
|
||||
|
||||
meta = [line: line]
|
||||
ignore_vars = Keyword.keys(guards)
|
||||
|
||||
vars =
|
||||
@@ -53,7 +52,7 @@ defmodule Code.Typespec do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
|
||||
when_args = [
|
||||
{:"::", meta, [{name, meta, args}, typespec_to_quoted(result)]},
|
||||
{:"::", meta, [{name, [line: line], args}, typespec_to_quoted(result)]},
|
||||
guards ++ vars
|
||||
]
|
||||
|
||||
@@ -189,27 +188,27 @@ defmodule Code.Typespec do
|
||||
|
||||
## To AST conversion
|
||||
|
||||
defp collect_vars({:ann_type, _anno, args}) when is_list(args) do
|
||||
defp collect_vars({:ann_type, _line, args}) when is_list(args) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp collect_vars({:type, _anno, _kind, args}) when is_list(args) do
|
||||
defp collect_vars({:type, _line, _kind, args}) when is_list(args) do
|
||||
Enum.flat_map(args, &collect_vars/1)
|
||||
end
|
||||
|
||||
defp collect_vars({:remote_type, _anno, args}) when is_list(args) do
|
||||
defp collect_vars({:remote_type, _line, args}) when is_list(args) do
|
||||
Enum.flat_map(args, &collect_vars/1)
|
||||
end
|
||||
|
||||
defp collect_vars({:typed_record_field, _anno, type}) do
|
||||
defp collect_vars({:typed_record_field, _line, type}) do
|
||||
collect_vars(type)
|
||||
end
|
||||
|
||||
defp collect_vars({:paren_type, _anno, [type]}) do
|
||||
defp collect_vars({:paren_type, _line, [type]}) do
|
||||
collect_vars(type)
|
||||
end
|
||||
|
||||
defp collect_vars({:var, _anno, var}) do
|
||||
defp collect_vars({:var, _line, var}) do
|
||||
[erl_to_ex_var(var)]
|
||||
end
|
||||
|
||||
@@ -217,48 +216,48 @@ defmodule Code.Typespec do
|
||||
[]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:user_type, anno, name, args}) do
|
||||
defp typespec_to_quoted({:user_type, line, name, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
{name, meta(anno), args}
|
||||
{name, [line: line], args}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :tuple, :any}) do
|
||||
{:tuple, meta(anno), []}
|
||||
defp typespec_to_quoted({:type, line, :tuple, :any}) do
|
||||
{:tuple, [line: line], []}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :tuple, args}) do
|
||||
defp typespec_to_quoted({:type, line, :tuple, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
{:{}, meta(anno), args}
|
||||
{:{}, [line: line], args}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _anno, :list, [{:type, _, :union, unions} = arg]}) do
|
||||
defp typespec_to_quoted({:type, _line, :list, [{:type, _, :union, unions} = arg]}) do
|
||||
case unpack_typespec_kw(unions, []) do
|
||||
{:ok, ast} -> ast
|
||||
:error -> [typespec_to_quoted(arg)]
|
||||
end
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :list, []}) do
|
||||
{:list, meta(anno), []}
|
||||
defp typespec_to_quoted({:type, line, :list, []}) do
|
||||
{:list, [line: line], []}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _anno, :list, [arg]}) do
|
||||
defp typespec_to_quoted({:type, _line, :list, [arg]}) do
|
||||
[typespec_to_quoted(arg)]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :nonempty_list, []}) do
|
||||
[{:..., meta(anno), nil}]
|
||||
defp typespec_to_quoted({:type, line, :nonempty_list, []}) do
|
||||
[{:..., [line: line], nil}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :nonempty_list, [arg]}) do
|
||||
[typespec_to_quoted(arg), {:..., meta(anno), nil}]
|
||||
defp typespec_to_quoted({:type, line, :nonempty_list, [arg]}) do
|
||||
[typespec_to_quoted(arg), {:..., [line: line], nil}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :map, :any}) do
|
||||
{:map, meta(anno), []}
|
||||
defp typespec_to_quoted({:type, line, :map, :any}) do
|
||||
{:map, [line: line], []}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :map, fields}) do
|
||||
defp typespec_to_quoted({:type, line, :map, fields}) do
|
||||
fields =
|
||||
Enum.map(fields, fn
|
||||
{:type, _, :map_field_assoc, :any} ->
|
||||
@@ -276,17 +275,16 @@ defmodule Code.Typespec do
|
||||
|
||||
case List.keytake(fields, :__struct__, 0) do
|
||||
{{:__struct__, struct}, fields_pruned} when is_atom(struct) and struct != nil ->
|
||||
map_pruned = {:%{}, meta(anno), fields_pruned}
|
||||
{:%, meta(anno), [struct, map_pruned]}
|
||||
map_pruned = {:%{}, [line: line], fields_pruned}
|
||||
{:%, [line: line], [struct, map_pruned]}
|
||||
|
||||
_ ->
|
||||
{:%{}, meta(anno), fields}
|
||||
{:%{}, [line: line], fields}
|
||||
end
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :binary, [arg1, arg2]}) do
|
||||
defp typespec_to_quoted({:type, line, :binary, [arg1, arg2]}) do
|
||||
[arg1, arg2] = for arg <- [arg1, arg2], do: typespec_to_quoted(arg)
|
||||
line = meta(anno)[:line]
|
||||
|
||||
case {typespec_to_quoted(arg1), typespec_to_quoted(arg2)} do
|
||||
{arg1, 0} ->
|
||||
@@ -300,57 +298,57 @@ defmodule Code.Typespec do
|
||||
end
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :union, args}) do
|
||||
defp typespec_to_quoted({:type, line, :union, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
Enum.reduce(Enum.reverse(args), fn arg, expr -> {:|, meta(anno), [arg, expr]} end)
|
||||
Enum.reduce(Enum.reverse(args), fn arg, expr -> {:|, [line: line], [arg, expr]} end)
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :fun, [{:type, _, :product, args}, result]}) do
|
||||
defp typespec_to_quoted({:type, line, :fun, [{:type, _, :product, args}, result]}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
[{:->, meta(anno), [args, typespec_to_quoted(result)]}]
|
||||
[{:->, [line: line], [args, typespec_to_quoted(result)]}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :fun, [args, result]}) do
|
||||
[{:->, meta(anno), [[typespec_to_quoted(args)], typespec_to_quoted(result)]}]
|
||||
defp typespec_to_quoted({:type, line, :fun, [args, result]}) do
|
||||
[{:->, [line: line], [[typespec_to_quoted(args)], typespec_to_quoted(result)]}]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :fun, []}) do
|
||||
typespec_to_quoted({:type, anno, :fun, [{:type, anno, :any}, {:type, anno, :any, []}]})
|
||||
defp typespec_to_quoted({:type, line, :fun, []}) do
|
||||
typespec_to_quoted({:type, line, :fun, [{:type, line, :any}, {:type, line, :any, []}]})
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, :range, [left, right]}) do
|
||||
{:.., meta(anno), [typespec_to_quoted(left), typespec_to_quoted(right)]}
|
||||
defp typespec_to_quoted({:type, line, :range, [left, right]}) do
|
||||
{:.., [line: line], [typespec_to_quoted(left), typespec_to_quoted(right)]}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _anno, nil, []}) do
|
||||
defp typespec_to_quoted({:type, _line, nil, []}) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, anno, name, args}) do
|
||||
defp typespec_to_quoted({:type, line, name, args}) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
{name, meta(anno), args}
|
||||
{name, [line: line], args}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:var, anno, var}) do
|
||||
{erl_to_ex_var(var), meta(anno), nil}
|
||||
defp typespec_to_quoted({:var, line, var}) do
|
||||
{erl_to_ex_var(var), [line: line], nil}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:op, anno, op, arg}) do
|
||||
{op, meta(anno), [typespec_to_quoted(arg)]}
|
||||
defp typespec_to_quoted({:op, line, op, arg}) do
|
||||
{op, [line: line], [typespec_to_quoted(arg)]}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:remote_type, anno, [mod, name, args]}) do
|
||||
remote_type(anno, mod, name, args)
|
||||
defp typespec_to_quoted({:remote_type, line, [mod, name, args]}) do
|
||||
remote_type(line, mod, name, args)
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:ann_type, anno, [var, type]}) do
|
||||
{:"::", meta(anno), [typespec_to_quoted(var), typespec_to_quoted(type)]}
|
||||
defp typespec_to_quoted({:ann_type, line, [var, type]}) do
|
||||
{:"::", [line: line], [typespec_to_quoted(var), typespec_to_quoted(type)]}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted(
|
||||
{:typed_record_field, {:record_field, anno1, {:atom, anno2, name}}, type}
|
||||
{:typed_record_field, {:record_field, line, {:atom, line1, name}}, type}
|
||||
) do
|
||||
typespec_to_quoted({:ann_type, anno1, [{:var, anno2, name}, type]})
|
||||
typespec_to_quoted({:ann_type, line, [{:var, line1, name}, type]})
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:type, _, :any}) do
|
||||
@@ -361,7 +359,7 @@ defmodule Code.Typespec do
|
||||
typespec_to_quoted(type)
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({type, _anno, atom}) when is_atom(type) do
|
||||
defp typespec_to_quoted({type, _line, atom}) when is_atom(type) do
|
||||
atom
|
||||
end
|
||||
|
||||
@@ -369,30 +367,30 @@ defmodule Code.Typespec do
|
||||
|
||||
## Helpers
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :charlist}, []) do
|
||||
typespec_to_quoted({:type, anno, :charlist, []})
|
||||
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :charlist}, []) do
|
||||
typespec_to_quoted({:type, line, :charlist, []})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :nonempty_charlist}, []) do
|
||||
typespec_to_quoted({:type, anno, :nonempty_charlist, []})
|
||||
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :nonempty_charlist}, []) do
|
||||
typespec_to_quoted({:type, line, :nonempty_charlist, []})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :struct}, []) do
|
||||
typespec_to_quoted({:type, anno, :struct, []})
|
||||
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :struct}, []) do
|
||||
typespec_to_quoted({:type, line, :struct, []})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]) do
|
||||
typespec_to_quoted({:type, anno, :as_boolean, [arg]})
|
||||
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]) do
|
||||
typespec_to_quoted({:type, line, :as_boolean, [arg]})
|
||||
end
|
||||
|
||||
defp remote_type(anno, {:atom, _, :elixir}, {:atom, _, :keyword}, args) do
|
||||
typespec_to_quoted({:type, anno, :keyword, args})
|
||||
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :keyword}, args) do
|
||||
typespec_to_quoted({:type, line, :keyword, args})
|
||||
end
|
||||
|
||||
defp remote_type(anno, mod, name, args) do
|
||||
defp remote_type(line, mod, name, args) do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
dot = {:., meta(anno), [typespec_to_quoted(mod), typespec_to_quoted(name)]}
|
||||
{dot, meta(anno), args}
|
||||
dot = {:., [line: line], [typespec_to_quoted(mod), typespec_to_quoted(name)]}
|
||||
{dot, [line: line], args}
|
||||
end
|
||||
|
||||
defp erl_to_ex_var(var) do
|
||||
@@ -416,6 +414,4 @@ defmodule Code.Typespec do
|
||||
defp unpack_typespec_kw(_, _acc) do
|
||||
:error
|
||||
end
|
||||
|
||||
defp meta(anno), do: [line: :erl_anno.line(anno)]
|
||||
end
|
||||
|
||||
@@ -17,8 +17,8 @@ defprotocol Collectable do
|
||||
|
||||
This design is intentional. `Enumerable` was designed to support infinite
|
||||
collections, resources and other structures with fixed shape. For example,
|
||||
it doesn't make sense to insert values into a `Range`, as it has a
|
||||
fixed shape where only the range limits and step are stored.
|
||||
it doesn't make sense to insert values into a range, as it has a fixed
|
||||
shape where just the range limits are stored.
|
||||
|
||||
The `Collectable` module was designed to fill the gap left by the
|
||||
`Enumerable` protocol. `Collectable.into/1` can be seen as the opposite of
|
||||
@@ -27,8 +27,8 @@ defprotocol Collectable do
|
||||
|
||||
## Examples
|
||||
|
||||
To show how to manually use the `Collectable` protocol, let's play with a
|
||||
simplified implementation for `MapSet`.
|
||||
To show how to manually use the `Collectable` protocol, let's play with its
|
||||
implementation for `MapSet`.
|
||||
|
||||
iex> {initial_acc, collector_fun} = Collectable.into(MapSet.new())
|
||||
iex> updated_acc = Enum.reduce([1, 2, 3], initial_acc, fn elem, acc ->
|
||||
@@ -38,33 +38,21 @@ defprotocol Collectable do
|
||||
#MapSet<[1, 2, 3]>
|
||||
|
||||
To show how the protocol can be implemented, we can again look at the
|
||||
simplified implementation for `MapSet`. In this implementation "collecting" elements
|
||||
implementation for `MapSet`. In this implementation "collecting" elements
|
||||
simply means inserting them in the set through `MapSet.put/2`.
|
||||
|
||||
defimpl Collectable, for: MapSet do
|
||||
def into(map_set) do
|
||||
def into(original) do
|
||||
collector_fun = fn
|
||||
map_set_acc, {:cont, elem} ->
|
||||
MapSet.put(map_set_acc, elem)
|
||||
|
||||
map_set_acc, :done ->
|
||||
map_set_acc
|
||||
|
||||
_map_set_acc, :halt ->
|
||||
:ok
|
||||
set, {:cont, elem} -> MapSet.put(set, elem)
|
||||
set, :done -> set
|
||||
_set, :halt -> :ok
|
||||
end
|
||||
|
||||
initial_acc = map_set
|
||||
|
||||
{initial_acc, collector_fun}
|
||||
{original, collector_fun}
|
||||
end
|
||||
end
|
||||
|
||||
So now we can call `Enum.into/2`:
|
||||
|
||||
iex> Enum.into([1, 2, 3], MapSet.new())
|
||||
#MapSet<[1, 2, 3]>
|
||||
|
||||
"""
|
||||
|
||||
@type command :: {:cont, term} | :done | :halt
|
||||
@@ -72,11 +60,8 @@ defprotocol Collectable do
|
||||
@doc """
|
||||
Returns an initial accumulator and a "collector" function.
|
||||
|
||||
Receives a `collectable` which can be used as the initial accumulator that will
|
||||
be passed to the function.
|
||||
|
||||
The collector function receives a term and a command and injects the term into
|
||||
the collectable accumulator on every `{:cont, term}` command.
|
||||
The returned function receives a term and a command and injects the term into
|
||||
the collectable on every `{:cont, term}` command.
|
||||
|
||||
`:done` is passed as a command when no further values will be injected. This
|
||||
is useful when there's a need to close resources or normalizing values. A
|
||||
@@ -88,17 +73,16 @@ defprotocol Collectable do
|
||||
For examples on how to use the `Collectable` protocol and `into/1` see the
|
||||
module documentation.
|
||||
"""
|
||||
@spec into(t) :: {initial_acc :: term, collector :: (term, command -> t | term)}
|
||||
@spec into(t) :: {term, (term, command -> t | term)}
|
||||
def into(collectable)
|
||||
end
|
||||
|
||||
defimpl Collectable, for: List do
|
||||
def into(list) do
|
||||
# TODO: Change the behaviour so the into always comes last on Elixir v2.0
|
||||
if list != [] do
|
||||
def into(original) do
|
||||
if original != [] do
|
||||
IO.warn(
|
||||
"the Collectable protocol is deprecated for non-empty lists. The behaviour of " <>
|
||||
"Enum.into/2 and \"for\" comprehensions with an :into option is incorrect " <>
|
||||
"things like Enum.into/2 or \"for\" comprehensions with an :into option is incorrect " <>
|
||||
"when collecting into non-empty lists. If you're collecting into a non-empty keyword " <>
|
||||
"list, consider using Keyword.merge/2 instead. If you're collecting into a non-empty " <>
|
||||
"list, consider concatenating the two lists with the ++ operator."
|
||||
@@ -106,14 +90,9 @@ defimpl Collectable, for: List do
|
||||
end
|
||||
|
||||
fun = fn
|
||||
list_acc, {:cont, elem} ->
|
||||
[elem | list_acc]
|
||||
|
||||
list_acc, :done ->
|
||||
list ++ :lists.reverse(list_acc)
|
||||
|
||||
_list_acc, :halt ->
|
||||
:ok
|
||||
list, {:cont, x} -> [x | list]
|
||||
list, :done -> original ++ :lists.reverse(list)
|
||||
_, :halt -> :ok
|
||||
end
|
||||
|
||||
{[], fun}
|
||||
@@ -121,7 +100,7 @@ defimpl Collectable, for: List do
|
||||
end
|
||||
|
||||
defimpl Collectable, for: BitString do
|
||||
def into(binary) when is_binary(binary) do
|
||||
def into(original) when is_binary(original) do
|
||||
fun = fn
|
||||
acc, {:cont, x} when is_binary(x) and is_list(acc) ->
|
||||
[acc | x]
|
||||
@@ -138,14 +117,14 @@ defimpl Collectable, for: BitString do
|
||||
acc, :done ->
|
||||
IO.iodata_to_binary(acc)
|
||||
|
||||
__acc, :halt ->
|
||||
_, :halt ->
|
||||
:ok
|
||||
end
|
||||
|
||||
{[binary], fun}
|
||||
{[original], fun}
|
||||
end
|
||||
|
||||
def into(bitstring) do
|
||||
def into(original) when is_bitstring(original) do
|
||||
fun = fn
|
||||
acc, {:cont, x} when is_bitstring(x) ->
|
||||
<<acc::bitstring, x::bitstring>>
|
||||
@@ -153,27 +132,22 @@ defimpl Collectable, for: BitString do
|
||||
acc, :done ->
|
||||
acc
|
||||
|
||||
_acc, :halt ->
|
||||
_, :halt ->
|
||||
:ok
|
||||
end
|
||||
|
||||
{bitstring, fun}
|
||||
{original, fun}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Collectable, for: Map do
|
||||
def into(map) do
|
||||
def into(original) do
|
||||
fun = fn
|
||||
map_acc, {:cont, {key, value}} ->
|
||||
Map.put(map_acc, key, value)
|
||||
|
||||
map_acc, :done ->
|
||||
map_acc
|
||||
|
||||
_map_acc, :halt ->
|
||||
:ok
|
||||
map, {:cont, {k, v}} -> Map.put(map, k, v)
|
||||
map, :done -> map
|
||||
_, :halt -> :ok
|
||||
end
|
||||
|
||||
{map, fun}
|
||||
{original, fun}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -16,8 +16,7 @@ defmodule Config do
|
||||
import_config "#{config_env()}.exs"
|
||||
|
||||
`import Config` will import the functions `config/2`, `config/3`
|
||||
`config_env/0`, `config_target/0`, and `import_config/1`
|
||||
to help you manage your configuration.
|
||||
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
|
||||
@@ -36,10 +35,8 @@ defmodule Config do
|
||||
|
||||
**Important:** if you are writing a library to be used by other developers,
|
||||
it is generally recommended to avoid the application environment, as the
|
||||
application environment is effectively a global storage. Also note that
|
||||
the `config/config.exs` of a library is not evaluated when the library is
|
||||
used as a dependency, as configuration is always meant to configure the
|
||||
current project. For more information, read our [library guidelines](library-guidelines.md).
|
||||
application environment is effectively a global storage. For more information,
|
||||
read our [library guidelines](library-guidelines.md).
|
||||
|
||||
## Migrating from `use Mix.Config`
|
||||
|
||||
@@ -75,7 +72,7 @@ defmodule Config do
|
||||
@config_key {__MODULE__, :config}
|
||||
@imports_key {__MODULE__, :imports}
|
||||
|
||||
defp get_opts!(), do: Process.get(@opts_key) || raise_improper_use!()
|
||||
defp get_opts!(), do: Process.get(@opts_key)
|
||||
defp put_opts(value), do: Process.put(@opts_key, value)
|
||||
defp delete_opts(), do: Process.delete(@opts_key)
|
||||
|
||||
@@ -102,9 +99,8 @@ defmodule Config do
|
||||
|
||||
The given `opts` are merged into the existing configuration
|
||||
for the given `root_key`. Conflicting keys are overridden by the
|
||||
ones specified in `opts`, unless they are keywords, which are
|
||||
deep merged recursively. For example, the application configuration
|
||||
below
|
||||
ones specified in `opts`. For example, the application
|
||||
configuration below
|
||||
|
||||
config :logger,
|
||||
level: :warn,
|
||||
@@ -139,9 +135,8 @@ defmodule Config do
|
||||
|
||||
The given `opts` are merged into the existing values for `key`
|
||||
in the given `root_key`. Conflicting keys are overridden by the
|
||||
ones specified in `opts`, unless they are keywords, which are
|
||||
deep merged recursively. For example, the application configuration
|
||||
below
|
||||
ones specified in `opts`. For example, the application
|
||||
configuration below
|
||||
|
||||
config :ecto, Repo,
|
||||
log_level: :warn,
|
||||
|
||||
@@ -11,55 +11,18 @@ defmodule Config.Provider do
|
||||
the file system. For more information on runtime configuration,
|
||||
see `mix release`.
|
||||
|
||||
## Multiple config files
|
||||
## Sample config provider
|
||||
|
||||
One common use of config providers is to specify multiple
|
||||
configuration files in a release. Elixir ships with one provider,
|
||||
called `Config.Reader`, which is capable of handling Elixir's
|
||||
built-in config files.
|
||||
|
||||
For example, imagine you want to list some basic configuration
|
||||
on Mix's built-in `config/runtime.exs` file, but you also want
|
||||
to support additional configuration files. To do so, you can add
|
||||
this inside the `def project` portion of your `mix.exs`:
|
||||
|
||||
releases: [
|
||||
demo: [
|
||||
config_providers: [
|
||||
{Config.Reader, {:system, "RELEASE_ROOT", "/extra_config.exs"}}
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
You can place this `extra_config.exs` file in your release in
|
||||
multiple ways:
|
||||
|
||||
1. If it is available on the host when assembling the release,
|
||||
you can place it on "rel/overlays/extra_config.exs" and it
|
||||
will be automatically copied to the release root
|
||||
|
||||
2. If it is available on the target during deployment, you can
|
||||
simply copy it to the release root as a step in your deployment
|
||||
|
||||
Now once the system boots, it will load both `config/runtime.exs`
|
||||
and `extra_config.exs` early in the boot process. You can learn
|
||||
more options on `Config.Reader`.
|
||||
|
||||
## Custom config provider
|
||||
|
||||
You can also implement custom config providers, similar to how
|
||||
`Config.Reader` works. For example, imagine you need to load
|
||||
some configuration from a JSON file and load that into the system.
|
||||
Said configuration provider would look like:
|
||||
For example, imagine you need to load some configuration from
|
||||
a JSON file and load that into the system. Said configuration
|
||||
provider would look like:
|
||||
|
||||
defmodule JSONConfigProvider do
|
||||
@behaviour Config.Provider
|
||||
|
||||
# Let's pass the path to the JSON file as config
|
||||
@impl true
|
||||
def init(path) when is_binary(path), do: path
|
||||
|
||||
@impl true
|
||||
def load(config, path) do
|
||||
# We need to start any app we may depend on.
|
||||
{:ok, _} = Application.ensure_all_started(:jason)
|
||||
@@ -76,17 +39,19 @@ defmodule Config.Provider do
|
||||
end
|
||||
end
|
||||
|
||||
Then, when specifying your release, you can specify the provider in
|
||||
Then when specifying your release, you can specify the provider in
|
||||
the release configuration:
|
||||
|
||||
releases: [
|
||||
demo: [
|
||||
config_providers: [
|
||||
{JSONConfigProvider, "/etc/config.json"}
|
||||
]
|
||||
# ...,
|
||||
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
|
||||
@@ -128,7 +93,7 @@ defmodule Config.Provider do
|
||||
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
|
||||
`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.
|
||||
@@ -230,6 +195,7 @@ defmodule Config.Provider do
|
||||
end
|
||||
|
||||
defp boot_providers(path, provider, reboot_config, reboot_fun) do
|
||||
validate_no_cyclic_boot!(path)
|
||||
original_config = read_config!(path)
|
||||
|
||||
config =
|
||||
@@ -354,6 +320,14 @@ defmodule Config.Provider do
|
||||
defp booted_value(%{prune_runtime_sys_config_after_boot: true}, path), do: {:booted, path}
|
||||
defp booted_value(%{prune_runtime_sys_config_after_boot: false}, _path), do: {:booted, nil}
|
||||
|
||||
defp 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]} ->
|
||||
|
||||
@@ -4,27 +4,19 @@ defmodule Config.Reader do
|
||||
|
||||
## As a provider
|
||||
|
||||
`Config.Reader` can also be used as a `Config.Provider`. A config
|
||||
provider is used during releases to customize how applications are
|
||||
configured. When used as a provider, it expects a single argument:
|
||||
the configuration path (as outlined in `t:Config.Provider.config_path/0`)
|
||||
for the file to be read and loaded during the system boot.
|
||||
`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 add this inside the `def project` portion
|
||||
of your `mix.exs`:
|
||||
in an absolute path, you can configure your `mix release` as:
|
||||
|
||||
releases: [
|
||||
demo: [
|
||||
config_providers: [
|
||||
{Config.Reader, "/etc/config.exs"}
|
||||
]
|
||||
]
|
||||
]
|
||||
config_providers: [{Config.Reader, "/etc/config.exs"}]
|
||||
|
||||
Or if you want to read a custom path inside the release:
|
||||
|
||||
config_providers: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
|
||||
config_provider: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
|
||||
|
||||
You can also pass a keyword list of options to the reader,
|
||||
where the `:path` is a required key:
|
||||
@@ -36,8 +28,10 @@ defmodule Config.Reader do
|
||||
imports: :disabled}
|
||||
]
|
||||
|
||||
Remember Mix already loads `config/runtime.exs` by default.
|
||||
For more examples and scenarios, see the `Config.Providers` module.
|
||||
Note by default Mix releases supports runtime configuration via
|
||||
a `config/runtime.exs`. If a `config/runtime.exs` exists in your
|
||||
application, it is automatically copied inside the release and
|
||||
automatically set as a config provider.
|
||||
"""
|
||||
|
||||
@behaviour Config.Provider
|
||||
|
||||
@@ -147,10 +147,10 @@ defmodule DynamicSupervisor do
|
||||
extra_arguments: [term()]
|
||||
}
|
||||
|
||||
@typedoc "Options given to `start_link` functions"
|
||||
@typedoc "Option values used by the `start*` functions"
|
||||
@type option :: GenServer.option()
|
||||
|
||||
@typedoc "Options given to `start_link` and `init/1` functions"
|
||||
@typedoc "Options given to `start_link/1` and `init/1`"
|
||||
@type init_option ::
|
||||
{:strategy, strategy()}
|
||||
| {:max_restarts, non_neg_integer()}
|
||||
@@ -419,7 +419,7 @@ defmodule DynamicSupervisor do
|
||||
@doc since: "1.6.0"
|
||||
@spec which_children(Supervisor.supervisor()) :: [
|
||||
# module() | :dynamic here because :supervisor.modules() is not exported
|
||||
{:undefined, pid | :restarting, :worker | :supervisor, [module()] | :dynamic}
|
||||
{:undefined, pid | :restarting, :worker | :supervisor, module() | :dynamic}
|
||||
]
|
||||
def which_children(supervisor) do
|
||||
call(supervisor, :which_children)
|
||||
|
||||
+171
-870
File diff suppressed because it is too large
Load Diff
+49
-178
@@ -43,16 +43,10 @@ defmodule Exception do
|
||||
@callback blame(t, stacktrace) :: {t, stacktrace}
|
||||
@optional_callbacks [blame: 2]
|
||||
|
||||
@doc false
|
||||
# Callback for formatting Erlang exceptions
|
||||
def format_error(%struct{} = exception, _stacktrace) do
|
||||
%{general: message(exception), reason: "#" <> Atom.to_string(struct)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the given `term` is an exception.
|
||||
"""
|
||||
# TODO: Deprecate this on Elixir v1.15
|
||||
# TODO: Remove this on Elixir v1.15
|
||||
@doc deprecated: "Use Kernel.is_exception/1 instead"
|
||||
def exception?(term)
|
||||
def exception?(%_{__exception__: true}), do: true
|
||||
@@ -68,7 +62,7 @@ defmodule Exception do
|
||||
caught_exception ->
|
||||
"got #{inspect(caught_exception.__struct__)} with message " <>
|
||||
"#{inspect(message(caught_exception))} while retrieving Exception.message/1 " <>
|
||||
"for #{inspect(exception)}. Stacktrace:\n#{format_stacktrace(__STACKTRACE__)}"
|
||||
"for #{inspect(exception)}"
|
||||
else
|
||||
result when is_binary(result) ->
|
||||
result
|
||||
@@ -217,17 +211,16 @@ defmodule Exception do
|
||||
clauses =
|
||||
for {meta, ex_args, guards, _block} <- clauses do
|
||||
scope = :elixir_erl.scope(meta, true)
|
||||
ann = :elixir_erl.get_ann(meta)
|
||||
|
||||
{erl_args, scope} =
|
||||
:elixir_erl_clauses.match(ann, &:elixir_erl_pass.translate_args/3, ex_args, scope)
|
||||
:elixir_erl_clauses.match(&:elixir_erl_pass.translate_args/2, ex_args, scope)
|
||||
|
||||
{args, binding} =
|
||||
[call_args, ex_args, erl_args]
|
||||
|> Enum.zip()
|
||||
|> Enum.map_reduce([], &blame_arg/2)
|
||||
|
||||
guards = Enum.map(guards, &blame_guard(&1, ann, scope, binding))
|
||||
guards = Enum.map(guards, &blame_guard(&1, scope, binding))
|
||||
{args, guards}
|
||||
end
|
||||
|
||||
@@ -259,19 +252,19 @@ defmodule Exception do
|
||||
|
||||
defp rewrite_arg(arg) do
|
||||
Macro.prewalk(arg, fn
|
||||
{:%{}, meta, [__struct__: Range, first: first, last: last, step: step]} ->
|
||||
{:"..//", meta, [first, last, step]}
|
||||
{:%{}, meta, [__struct__: Range, first: first, last: last]} ->
|
||||
{:.., meta, [first, last]}
|
||||
|
||||
other ->
|
||||
other
|
||||
end)
|
||||
end
|
||||
|
||||
defp blame_guard({{:., _, [:erlang, op]}, meta, [left, right]}, ann, scope, binding)
|
||||
defp blame_guard({{:., _, [:erlang, op]}, meta, [left, right]}, scope, binding)
|
||||
when op == :andalso or op == :orelse do
|
||||
guards = [
|
||||
blame_guard(left, ann, scope, binding),
|
||||
blame_guard(right, ann, scope, binding)
|
||||
blame_guard(left, scope, binding),
|
||||
blame_guard(right, scope, binding)
|
||||
]
|
||||
|
||||
kernel_op =
|
||||
@@ -283,8 +276,8 @@ defmodule Exception do
|
||||
{kernel_op, meta, guards}
|
||||
end
|
||||
|
||||
defp blame_guard(ex_guard, ann, scope, binding) do
|
||||
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, ann, scope)
|
||||
defp blame_guard(ex_guard, scope, binding) do
|
||||
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, scope)
|
||||
|
||||
match? =
|
||||
try do
|
||||
@@ -697,17 +690,6 @@ defmodule Exception do
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def format_snippet(snippet, error_line) do
|
||||
line_digits = error_line |> Integer.to_string() |> byte_size()
|
||||
placeholder = String.duplicate(" ", max(line_digits, 2))
|
||||
padding = if line_digits < 2, do: " "
|
||||
|
||||
" #{placeholder} |\n" <>
|
||||
" #{padding}#{error_line} | #{snippet.content}\n" <>
|
||||
" #{placeholder} | #{String.duplicate(" ", snippet.offset)}^"
|
||||
end
|
||||
|
||||
defp format_location(opts) when is_list(opts) do
|
||||
format_file_line(Keyword.get(opts, :file), Keyword.get(opts, :line), " ")
|
||||
end
|
||||
@@ -727,19 +709,15 @@ defmodule ArgumentError do
|
||||
|
||||
@impl true
|
||||
def blame(
|
||||
exception,
|
||||
%{message: "argument error"} = exception,
|
||||
[{:erlang, :apply, [module, function, args], _} | _] = stacktrace
|
||||
) do
|
||||
message =
|
||||
cond do
|
||||
not proper_list?(args) ->
|
||||
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)} " <>
|
||||
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a proper list"
|
||||
|
||||
# Note that args may be an empty list even if they were supplied
|
||||
not is_atom(module) and is_atom(function) and args == [] ->
|
||||
"you attempted to apply a function named #{inspect(function)} on #{inspect(module)}. " <>
|
||||
"If you are using Kernel.apply/3, make sure the module is an atom. " <>
|
||||
"you attempted to apply #{inspect(function)} on #{inspect(module)}. " <>
|
||||
"If you are using apply/3, make sure the module is an atom. " <>
|
||||
"If you are using the dot syntax, such as map.field or module.function(), " <>
|
||||
"make sure the left side of the dot is an atom or a map"
|
||||
|
||||
@@ -748,9 +726,12 @@ defmodule ArgumentError do
|
||||
"Modules (the first argument of apply) must always be an atom"
|
||||
|
||||
not is_atom(function) ->
|
||||
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)}. " <>
|
||||
"However #{inspect(function)} is not a valid function name. Function names (the second argument " <>
|
||||
"of apply) must always be an atom"
|
||||
"you attempted to apply #{inspect(function)} on module #{inspect(module)}. " <>
|
||||
"Functions (the second argument of apply) must always be an atom"
|
||||
|
||||
not is_list(args) ->
|
||||
"you attempted to apply #{inspect(function)} on module #{inspect(module)} " <>
|
||||
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a list"
|
||||
end
|
||||
|
||||
{%{exception | message: message}, stacktrace}
|
||||
@@ -759,9 +740,6 @@ defmodule ArgumentError do
|
||||
def blame(exception, stacktrace) do
|
||||
{exception, stacktrace}
|
||||
end
|
||||
|
||||
defp proper_list?(list) when length(list) >= 0, do: true
|
||||
defp proper_list?(_), do: false
|
||||
end
|
||||
|
||||
defmodule ArithmeticError do
|
||||
@@ -805,60 +783,29 @@ defmodule ArithmeticError do
|
||||
end
|
||||
|
||||
defmodule SystemLimitError do
|
||||
defexception message: "a system limit has been reached"
|
||||
defexception []
|
||||
|
||||
@impl true
|
||||
def message(_) do
|
||||
"a system limit has been reached"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule SyntaxError do
|
||||
defexception [:file, :line, :column, :snippet, description: "syntax error"]
|
||||
defexception [:file, :line, :column, description: "syntax error"]
|
||||
|
||||
@impl true
|
||||
def message(%{
|
||||
file: file,
|
||||
line: line,
|
||||
column: column,
|
||||
description: description,
|
||||
snippet: snippet
|
||||
})
|
||||
when not is_nil(snippet) and not is_nil(column) do
|
||||
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
|
||||
" " <> description <> "\n" <> Exception.format_snippet(snippet, line)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def message(%{
|
||||
file: file,
|
||||
line: line,
|
||||
column: column,
|
||||
description: description
|
||||
}) do
|
||||
def message(%{file: file, line: line, column: column, description: description}) do
|
||||
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
|
||||
" " <> description
|
||||
end
|
||||
end
|
||||
|
||||
defmodule TokenMissingError do
|
||||
defexception [:file, :line, :snippet, :column, description: "expression is incomplete"]
|
||||
defexception [:file, :line, :column, description: "expression is incomplete"]
|
||||
|
||||
@impl true
|
||||
def message(%{
|
||||
file: file,
|
||||
line: line,
|
||||
column: column,
|
||||
description: description,
|
||||
snippet: snippet
|
||||
})
|
||||
when not is_nil(snippet) and not is_nil(column) do
|
||||
Exception.format_file_line_column(Path.relative_to_cwd(file), line, column) <>
|
||||
" " <> description <> "\n" <> Exception.format_snippet(snippet, line)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def message(%{
|
||||
file: file,
|
||||
line: line,
|
||||
column: column,
|
||||
description: description
|
||||
}) do
|
||||
def message(%{file: file, line: line, column: column, description: description}) do
|
||||
Exception.format_file_line_column(file && Path.relative_to_cwd(file), line, column) <>
|
||||
" " <> description
|
||||
end
|
||||
@@ -1045,23 +992,11 @@ defmodule UndefinedFunctionError do
|
||||
|
||||
@doc false
|
||||
def hint_for_loaded_module(module, function, arity, exports) do
|
||||
cond do
|
||||
macro_exported?(module, function, arity) ->
|
||||
". However there is a macro with the same name and arity. " <>
|
||||
"Be sure to require #{inspect(module)} if you intend to invoke this macro"
|
||||
|
||||
message = otp_obsolete(module, function, arity) ->
|
||||
", #{message}"
|
||||
|
||||
true ->
|
||||
IO.iodata_to_binary(did_you_mean(module, function, exports))
|
||||
end
|
||||
end
|
||||
|
||||
defp otp_obsolete(module, function, arity) do
|
||||
case :otp_internal.obsolete(module, function, arity) do
|
||||
{:removed, [_ | _] = string} -> string
|
||||
_ -> nil
|
||||
if macro_exported?(module, function, arity) do
|
||||
". However there is a macro with the same name and arity. " <>
|
||||
"Be sure to require #{inspect(module)} if you intend to invoke this macro"
|
||||
else
|
||||
IO.iodata_to_binary(did_you_mean(module, function, exports))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1091,7 +1026,7 @@ defmodule UndefinedFunctionError do
|
||||
|
||||
case result do
|
||||
[] -> []
|
||||
suggestions -> [". Did you mean:\n\n" | Enum.map(suggestions, &format_fa/1)]
|
||||
suggestions -> [". Did you mean one of:\n\n" | Enum.map(suggestions, &format_fa/1)]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1122,9 +1057,7 @@ defmodule UndefinedFunctionError do
|
||||
end
|
||||
|
||||
defp expects_callback?(behaviour, function, arity) do
|
||||
callbacks =
|
||||
behaviour.behaviour_info(:callbacks) -- behaviour.behaviour_info(:optional_callbacks)
|
||||
|
||||
callbacks = behaviour.behaviour_info(:callbacks)
|
||||
Enum.member?(callbacks, {function, arity})
|
||||
end
|
||||
|
||||
@@ -1166,7 +1099,7 @@ defmodule FunctionClauseError do
|
||||
|
||||
%{module: module, function: function, arity: arity} ->
|
||||
formatted = Exception.format_mfa(module, function, arity)
|
||||
blamed = blame(exception, &inspect/1, &blame_match/1)
|
||||
blamed = blame(exception, &inspect/1, &blame_match/2)
|
||||
"no function clause matching in #{formatted}" <> blamed
|
||||
end
|
||||
end
|
||||
@@ -1188,24 +1121,24 @@ defmodule FunctionClauseError do
|
||||
end
|
||||
end
|
||||
|
||||
defp blame_match(%{match?: true, node: node}), do: Macro.to_string(node)
|
||||
defp blame_match(%{match?: false, node: node}), do: "-" <> Macro.to_string(node) <> "-"
|
||||
defp blame_match(%{match?: true, node: node}, _), do: Macro.to_string(node)
|
||||
defp blame_match(%{match?: false, node: node}, _), do: "-" <> Macro.to_string(node) <> "-"
|
||||
defp blame_match(_, string), do: string
|
||||
|
||||
@doc false
|
||||
def blame(%{args: nil}, _, _) do
|
||||
""
|
||||
end
|
||||
|
||||
def blame(exception, inspect_fun, fun) do
|
||||
def blame(exception, inspect_fun, ast_fun) do
|
||||
%{module: module, function: function, arity: arity, kind: kind, args: args, clauses: clauses} =
|
||||
exception
|
||||
|
||||
mfa = Exception.format_mfa(module, function, arity)
|
||||
|
||||
format_clause_fun = fn {args, guards} ->
|
||||
args = Enum.map_join(args, ", ", fun)
|
||||
base = " #{kind} #{function}(#{args})"
|
||||
Enum.reduce(guards, base, &"#{&2} when #{clause_to_string(&1, fun)}") <> "\n"
|
||||
code = Enum.reduce(guards, {function, [], args}, &{:when, [], [&2, &1]})
|
||||
" #{kind} " <> Macro.to_string(code, ast_fun) <> "\n"
|
||||
end
|
||||
|
||||
"\n\nThe following arguments were given to #{mfa}:\n" <>
|
||||
@@ -1213,11 +1146,6 @@ defmodule FunctionClauseError do
|
||||
"#{format_clauses(clauses, format_clause_fun, @clause_limit)}"
|
||||
end
|
||||
|
||||
defp clause_to_string({op, _, [left, right]}, fun),
|
||||
do: clause_to_string(left, fun) <> " #{op} " <> clause_to_string(right, fun)
|
||||
|
||||
defp clause_to_string(node, fun), do: fun.(node)
|
||||
|
||||
defp format_args(args, inspect_fun) do
|
||||
args
|
||||
|> Enum.with_index(1)
|
||||
@@ -1320,10 +1248,6 @@ defmodule KeyError do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def blame(exception = %{message: message}, stacktrace) when is_binary(message) do
|
||||
{exception, stacktrace}
|
||||
end
|
||||
|
||||
def blame(exception = %{term: nil}, stacktrace) do
|
||||
message = message(exception.key, exception.term)
|
||||
{%{exception | message: message}, stacktrace}
|
||||
@@ -1362,7 +1286,7 @@ defmodule KeyError do
|
||||
|
||||
case suggestions do
|
||||
[] -> []
|
||||
suggestions -> [". Did you mean:\n\n" | format_suggestions(suggestions)]
|
||||
suggestions -> [". Did you mean one of:\n\n" | format_suggestions(suggestions)]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1481,32 +1405,16 @@ defmodule ErlangError do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def normalize(:badarg, stacktrace) do
|
||||
case error_info(:badarg, stacktrace) do
|
||||
{:ok, args} ->
|
||||
message = "errors were found at the given arguments:\n\n#{args}"
|
||||
%ArgumentError{message: message}
|
||||
|
||||
:error ->
|
||||
%ArgumentError{}
|
||||
end
|
||||
def normalize(:badarg, _stacktrace) do
|
||||
%ArgumentError{}
|
||||
end
|
||||
|
||||
def normalize(:badarith, _stacktrace) do
|
||||
%ArithmeticError{}
|
||||
end
|
||||
|
||||
def normalize(:system_limit, stacktrace) do
|
||||
case error_info(:system_limit, stacktrace) do
|
||||
{:ok, args} ->
|
||||
message =
|
||||
"a system limit has been reached due to errors at the given arguments:\n\n#{args}"
|
||||
|
||||
%SystemLimitError{message: message}
|
||||
|
||||
:error ->
|
||||
%SystemLimitError{}
|
||||
end
|
||||
def normalize(:system_limit, _stacktrace) do
|
||||
%SystemLimitError{}
|
||||
end
|
||||
|
||||
def normalize(:cond_clause, _stacktrace) do
|
||||
@@ -1596,41 +1504,4 @@ defmodule ErlangError do
|
||||
defp from_stacktrace(_) do
|
||||
{nil, nil, nil}
|
||||
end
|
||||
|
||||
defp error_info(:badarg, [{:erlang, fun, _, _} | _]) when fun in [:byte_size, :bit_size] do
|
||||
{:ok,
|
||||
"""
|
||||
* 1st argument: not a bitstring
|
||||
|
||||
This typically happens when calling Kernel.#{fun}/1 with an invalid argument \
|
||||
or when performing binary construction or binary concatenation with <> and \
|
||||
one of the arguments is not a binary\
|
||||
"""}
|
||||
end
|
||||
|
||||
defp error_info(erl_exception, stacktrace) do
|
||||
with [{module, _, args_or_arity, opts} | _] <- stacktrace,
|
||||
%{} = error_info <- opts[:error_info] do
|
||||
module = Map.get(error_info, :module, module)
|
||||
function = Map.get(error_info, :function, :format_error)
|
||||
arity = if is_integer(args_or_arity), do: args_or_arity, else: length(args_or_arity)
|
||||
extra = apply(module, function, [erl_exception, stacktrace])
|
||||
args_errors = Map.take(extra, Enum.to_list(1..arity//1))
|
||||
|
||||
if map_size(args_errors) > 0 do
|
||||
{:ok, IO.iodata_to_binary(Enum.map(args_errors, &arg_error/1))}
|
||||
else
|
||||
:error
|
||||
end
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
defp arg_error({n, message}), do: " * #{nth(n)} argument: #{message}\n"
|
||||
|
||||
defp nth(1), do: "1st"
|
||||
defp nth(2), do: "2nd"
|
||||
defp nth(3), do: "3rd"
|
||||
defp nth(n), do: "#{n}th"
|
||||
end
|
||||
|
||||
+8
-17
@@ -764,16 +764,15 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Copies the contents of `source_file` to `destination_file` preserving its modes.
|
||||
Copies the contents in `source_file` to `destination_file` preserving its modes.
|
||||
|
||||
`source_file` must be a file or a symbolic link to one. `destination_file` must
|
||||
be a path to a non-existent file. If either is a directory, `{:error, :eisdir}`
|
||||
will be returned.
|
||||
`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.
|
||||
|
||||
The `callback` function is invoked if the `destination_file` already exists.
|
||||
The function receives arguments for `source_file` and `destination_file`;
|
||||
it should return `true` if the existing file should be overwritten, `false` if
|
||||
otherwise. The default callback returns `true`.
|
||||
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}`.
|
||||
@@ -1542,12 +1541,6 @@ defmodule File do
|
||||
executes the given function and then reverts back
|
||||
to the previous path regardless of whether there is an exception.
|
||||
|
||||
The current working directory is temporarily set for the BEAM globally. This
|
||||
can lead to race conditions if multiple processes are changing the current
|
||||
working directory concurrently. To run an external command in a given
|
||||
directory without changing the global current working directory, use the
|
||||
`:cd` option of `System.cmd/3` and `Port.open/2`.
|
||||
|
||||
Raises an error if retrieving or changing the current
|
||||
directory fails.
|
||||
"""
|
||||
@@ -1614,9 +1607,7 @@ defmodule File do
|
||||
which means it can be used both for read and write.
|
||||
|
||||
The `line_or_bytes` argument configures how the file is read when
|
||||
streaming, by `:line` (default) or by a given number of bytes. When
|
||||
using the `:line` option, CRLF line breaks (`"\r\n"`) are normalized
|
||||
to LF (`"\n"`).
|
||||
streaming, by `:line` (default) or by a given number of bytes.
|
||||
|
||||
Operating the stream can fail on open for the same reasons as
|
||||
`File.open!/2`. Note that the file is automatically opened each time streaming
|
||||
|
||||
+40
-96
@@ -46,46 +46,6 @@ defmodule Float do
|
||||
@precision_range 0..15
|
||||
@type precision_range :: 0..15
|
||||
|
||||
@doc """
|
||||
Computes `base` raised to power of `exponent`.
|
||||
|
||||
`base` must be a float and `exponent` can be any number.
|
||||
However, if a negative base and a fractional exponent
|
||||
are given, it raises `ArithmeticError`.
|
||||
|
||||
It always returns a float. See `Integer.pow/2` for
|
||||
exponentiation that returns integers.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.pow(2.0, 0)
|
||||
1.0
|
||||
iex> Float.pow(2.0, 1)
|
||||
2.0
|
||||
iex> Float.pow(2.0, 10)
|
||||
1024.0
|
||||
iex> Float.pow(2.0, -1)
|
||||
0.5
|
||||
iex> Float.pow(2.0, -3)
|
||||
0.125
|
||||
|
||||
iex> Float.pow(3.0, 1.5)
|
||||
5.196152422706632
|
||||
|
||||
iex> Float.pow(-2.0, 3)
|
||||
-8.0
|
||||
iex> Float.pow(-2.0, 4)
|
||||
16.0
|
||||
|
||||
iex> Float.pow(-1.0, 0.5)
|
||||
** (ArithmeticError) bad argument in arithmetic expression
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec pow(float, number) :: float
|
||||
def pow(base, exponent) when is_float(base) and is_number(exponent),
|
||||
do: :math.pow(base, exponent)
|
||||
|
||||
@doc """
|
||||
Parses a binary into a float.
|
||||
|
||||
@@ -308,11 +268,11 @@ defmodule Float do
|
||||
raise ArgumentError, invalid_precision_message(precision)
|
||||
end
|
||||
|
||||
defp round(0.0 = num, _precision, _rounding), do: num
|
||||
defp round(0.0, _precision, _rounding), do: 0.0
|
||||
|
||||
defp round(float, precision, rounding) do
|
||||
<<sign::1, exp::11, significant::52-bitstring>> = <<float::float>>
|
||||
{num, count} = decompose(significant, 1)
|
||||
{num, count, _} = decompose(significant, 1)
|
||||
count = count - exp + 1023
|
||||
|
||||
cond do
|
||||
@@ -364,22 +324,6 @@ defmodule Float do
|
||||
end
|
||||
end
|
||||
|
||||
defp decompose(significant, initial) do
|
||||
decompose(significant, 1, 0, initial)
|
||||
end
|
||||
|
||||
defp decompose(<<1::1, bits::bitstring>>, count, last_count, acc) do
|
||||
decompose(bits, count + 1, count, (acc <<< (count - last_count)) + 1)
|
||||
end
|
||||
|
||||
defp decompose(<<0::1, bits::bitstring>>, count, last_count, acc) do
|
||||
decompose(bits, count + 1, last_count, acc)
|
||||
end
|
||||
|
||||
defp decompose(<<>>, _count, last_count, acc) do
|
||||
{acc, last_count}
|
||||
end
|
||||
|
||||
defp scale_up(num, boundary, exp) when num >= boundary, do: {num, exp}
|
||||
defp scale_up(num, boundary, exp), do: scale_up(num <<< 1, boundary, exp - 1)
|
||||
|
||||
@@ -459,55 +403,55 @@ defmodule Float do
|
||||
def ratio(0.0), do: {0, 1}
|
||||
|
||||
def ratio(float) when is_float(float) do
|
||||
<<sign::1, exp::11, mantissa::52>> = <<float::float>>
|
||||
case <<float::float>> do
|
||||
<<sign::1, 0::11, significant::52-bitstring>> ->
|
||||
{num, _, den} = decompose(significant, 0)
|
||||
{sign(sign, num), shift_left(den, 1022)}
|
||||
|
||||
{num, den_exp} =
|
||||
if exp != 0 do
|
||||
# Floats are expressed like this:
|
||||
# (2**52 + mantissa) * 2**(-52 + exp - 1023)
|
||||
#
|
||||
# We compute the root factors of the mantissa so we have this:
|
||||
# (2**52 + mantissa * 2**count) * 2**(-52 + exp - 1023)
|
||||
{mantissa, count} = root_factors(mantissa, 0)
|
||||
<<sign::1, exp::11, significant::52-bitstring>> ->
|
||||
{num, _, den} = decompose(significant, 1)
|
||||
num = sign(sign, num)
|
||||
|
||||
# Now we can move the count around so we have this:
|
||||
# (2**(52-count) + mantissa) * 2**(count + -52 + exp - 1023)
|
||||
if mantissa == 0 do
|
||||
{1, exp - 1023}
|
||||
else
|
||||
num = (1 <<< (52 - count)) + mantissa
|
||||
den_exp = count - 52 + exp - 1023
|
||||
{num, den_exp}
|
||||
case exp - 1023 do
|
||||
exp when exp > 0 ->
|
||||
{den, exp} = shift_right(den, exp)
|
||||
{shift_left(num, exp), den}
|
||||
|
||||
exp when exp < 0 ->
|
||||
{num, shift_left(den, -exp)}
|
||||
|
||||
0 ->
|
||||
{num, den}
|
||||
end
|
||||
else
|
||||
# Subnormals are expressed like this:
|
||||
# (mantissa) * 2**(-52 + 1 - 1023)
|
||||
#
|
||||
# So we compute it to this:
|
||||
# (mantissa * 2**(count)) * 2**(-52 + 1 - 1023)
|
||||
#
|
||||
# Which becomes:
|
||||
# mantissa * 2**(count-1074)
|
||||
root_factors(mantissa, -1074)
|
||||
end
|
||||
|
||||
if den_exp > 0 do
|
||||
{sign(sign, num <<< den_exp), 1}
|
||||
else
|
||||
{sign(sign, num), 1 <<< -den_exp}
|
||||
end
|
||||
end
|
||||
|
||||
defp root_factors(mantissa, count) when mantissa != 0 and (mantissa &&& 1) == 0,
|
||||
do: root_factors(mantissa >>> 1, count + 1)
|
||||
defp decompose(significant, initial) do
|
||||
decompose(significant, 1, 0, 2, 1, initial)
|
||||
end
|
||||
|
||||
defp root_factors(mantissa, count),
|
||||
do: {mantissa, count}
|
||||
defp decompose(<<1::1, bits::bitstring>>, count, last_count, power, _last_power, acc) do
|
||||
decompose(bits, count + 1, count, power <<< 1, power, shift_left(acc, count - last_count) + 1)
|
||||
end
|
||||
|
||||
@compile {:inline, sign: 2}
|
||||
defp decompose(<<0::1, bits::bitstring>>, count, last_count, power, last_power, acc) do
|
||||
decompose(bits, count + 1, last_count, power <<< 1, last_power, acc)
|
||||
end
|
||||
|
||||
defp decompose(<<>>, _count, last_count, _power, last_power, acc) do
|
||||
{acc, last_count, last_power}
|
||||
end
|
||||
|
||||
@compile {:inline, sign: 2, shift_left: 2}
|
||||
defp sign(0, num), do: num
|
||||
defp sign(1, num), do: -num
|
||||
|
||||
defp shift_left(num, times), do: num <<< times
|
||||
|
||||
defp shift_right(num, 0), do: {num, 0}
|
||||
defp shift_right(1, times), do: {1, times}
|
||||
defp shift_right(num, times), do: shift_right(num >>> 1, times - 1)
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation
|
||||
of the given float.
|
||||
|
||||
@@ -7,7 +7,7 @@ defmodule GenEvent do
|
||||
If you are interested in implementing an event manager, please read the
|
||||
"Alternatives" section below. If you have to implement an event handler to
|
||||
integrate with an existing system, such as Elixir's Logger, please use
|
||||
[`:gen_event`](`:gen_event`) instead.
|
||||
[`:gen_event`](https://erlang.org/doc/man/gen_event.html) instead.
|
||||
|
||||
## Alternatives
|
||||
|
||||
@@ -38,7 +38,7 @@ defmodule GenEvent do
|
||||
|
||||
If your use case requires exactly what GenEvent provided, or you have to
|
||||
integrate with an existing `:gen_event`-based system, you can still use the
|
||||
[`:gen_event`](`:gen_event`) Erlang module.
|
||||
[`:gen_event`](http://erlang.org/doc/man/gen_event.html) Erlang module.
|
||||
"""
|
||||
|
||||
@moduledoc deprecated: "Use Erlang/OTP's :gen_event module instead"
|
||||
|
||||
@@ -61,7 +61,7 @@ defmodule GenServer do
|
||||
|
||||
Every time you do a `GenServer.call/3`, the client will send a message
|
||||
that must be handled by the `c:handle_call/3` callback in the GenServer.
|
||||
A `cast/2` message must be handled by `c:handle_cast/2`. There are 8 possible
|
||||
A `cast/2` message must be handled by `c:handle_cast/2`. There are 7 possible
|
||||
callbacks to be implemented when you use a `GenServer`. The only required
|
||||
callback is `c:init/1`.
|
||||
|
||||
@@ -159,16 +159,16 @@ defmodule GenServer do
|
||||
a name on start via the `:name` option. Registered names are also
|
||||
automatically cleaned up on termination. The supported values are:
|
||||
|
||||
* an atom - the GenServer is registered locally (to the current node)
|
||||
with the given name using `Process.register/2`.
|
||||
* an atom - the GenServer is registered locally with the given name
|
||||
using `Process.register/2`.
|
||||
|
||||
* `{:global, term}` - the GenServer is registered globally with the given
|
||||
term using the functions in the [`:global` module](`:global`).
|
||||
term using the functions in the [`:global` module](http://www.erlang.org/doc/man/global.html).
|
||||
|
||||
* `{:via, module, term}` - the GenServer is registered with the given
|
||||
mechanism and name. The `:via` option expects a module that exports
|
||||
`register_name/2`, `unregister_name/1`, `whereis_name/1` and `send/2`.
|
||||
One such example is the [`:global` module](`:global`) which uses these functions
|
||||
One such example is the [`:global` module](http://www.erlang.org/doc/man/global.html) which uses these functions
|
||||
for keeping the list of names of processes and their associated PIDs
|
||||
that are available globally for a network of Elixir nodes. Elixir also
|
||||
ships with a local, decentralized and scalable registry called `Registry`
|
||||
@@ -242,8 +242,7 @@ defmodule GenServer do
|
||||
end
|
||||
|
||||
defp schedule_work do
|
||||
# We schedule the work to happen in 2 hours (written in milliseconds).
|
||||
# Alternatively, one might write :timer.hours(2)
|
||||
# In 2 hours
|
||||
Process.send_after(self(), :work, 2 * 60 * 60 * 1000)
|
||||
end
|
||||
end
|
||||
@@ -254,20 +253,15 @@ defmodule GenServer do
|
||||
a timeout value in milliseconds; if not, `:infinity` is assumed.
|
||||
The timeout can be used to detect a lull in incoming messages.
|
||||
|
||||
The `timeout()` value is used as follows:
|
||||
If the process has 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.
|
||||
|
||||
* If the process has any message already waiting when the `timeout()` value
|
||||
is returned, the timeout is ignored and the waiting message is handled as
|
||||
usual. This means that even a timeout of `0` milliseconds is not guaranteed
|
||||
to execute (if you want to take another action immediately and unconditionally,
|
||||
use a `:continue` instruction instead).
|
||||
|
||||
* If any message arrives before the specified number of milliseconds
|
||||
elapse, the timeout is cleared and that message is handled as usual.
|
||||
|
||||
* Otherwise, when the specified number of milliseconds have elapsed with no
|
||||
message arriving, `handle_info/2` is called with `:timeout` as the first
|
||||
argument.
|
||||
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
|
||||
|
||||
@@ -317,14 +311,14 @@ defmodule GenServer do
|
||||
|
||||
## Debugging with the :sys module
|
||||
|
||||
GenServers, as [special processes](https://erlang.org/doc/design_principles/spec_proc.html),
|
||||
can be debugged using the [`:sys` module](`:sys`).
|
||||
GenServers, as [special processes](http://erlang.org/doc/design_principles/spec_proc.html),
|
||||
can be debugged using the [`:sys` module](http://www.erlang.org/doc/man/sys.html).
|
||||
Through various hooks, this module allows developers to introspect the state of
|
||||
the process and trace system events that happen during its execution, such as
|
||||
received messages, sent replies and state changes.
|
||||
|
||||
Let's explore the basic functions from the
|
||||
[`:sys` module](`:sys`) used for debugging:
|
||||
[`:sys` module](http://www.erlang.org/doc/man/sys.html) used for debugging:
|
||||
|
||||
* `:sys.get_state/2` - allows retrieval of the state of the process.
|
||||
In the case of a GenServer process, it will be the callback module state,
|
||||
@@ -406,8 +400,8 @@ defmodule GenServer do
|
||||
in Erlang can also provide extra insight.
|
||||
|
||||
* [GenServer - Elixir's Getting Started Guide](https://elixir-lang.org/getting-started/mix-otp/genserver.html)
|
||||
* [`:gen_server` module documentation](`:gen_server`)
|
||||
* [gen_server Behaviour - OTP Design Principles](https://erlang.org/doc/design_principles/gen_server_concepts.html)
|
||||
* [`:gen_server` module documentation](http://www.erlang.org/doc/man/gen_server.html)
|
||||
* [gen_server Behaviour - OTP Design Principles](http://www.erlang.org/doc/design_principles/gen_server_concepts.html)
|
||||
* [Clients and Servers - Learn You Some Erlang for Great Good!](http://learnyousomeerlang.com/clients-and-servers)
|
||||
|
||||
"""
|
||||
@@ -430,7 +424,7 @@ defmodule GenServer do
|
||||
`c:handle_call/3` for more information on hibernation.
|
||||
|
||||
Returning `{:ok, state, {:continue, continue}}` is similar to
|
||||
`{:ok, state}` except that immediately after entering the loop,
|
||||
`{:ok, state}` except that immediately after entering the loop
|
||||
the `c:handle_continue/2` callback will be invoked with the value
|
||||
`continue` as first argument.
|
||||
|
||||
@@ -477,8 +471,8 @@ defmodule GenServer do
|
||||
|
||||
Returning `{:reply, reply, new_state, :hibernate}` is similar to
|
||||
`{:reply, reply, new_state}` except the process is hibernated and will
|
||||
continue the loop once a message is in its message queue. However, if a message is
|
||||
already in the message queue, the process will continue the loop immediately. Hibernating a
|
||||
continue the loop once a message is in its message queue. If a message is
|
||||
already in the message queue this will be immediately. Hibernating a
|
||||
`GenServer` causes garbage collection and leaves a continuous heap that
|
||||
minimises the memory used by the process.
|
||||
|
||||
@@ -512,7 +506,7 @@ defmodule GenServer do
|
||||
occurs as with a `:reply` tuple.
|
||||
|
||||
Returning `{:stop, reason, reply, new_state}` stops the loop and `c:terminate/2`
|
||||
is called with reason `reason` and state `new_state`. Then, the `reply` is sent
|
||||
is called with reason `reason` and state `new_state`. Then the `reply` is sent
|
||||
as the response to call and the process exits with reason `reason`.
|
||||
|
||||
Returning `{:stop, reason, new_state}` is similar to
|
||||
@@ -590,6 +584,8 @@ defmodule GenServer do
|
||||
|
||||
This callback is optional. If one is not implemented, the server will fail
|
||||
if a continue instruction is used.
|
||||
|
||||
This callback is only supported on Erlang/OTP 21+.
|
||||
"""
|
||||
@callback handle_continue(continue :: term, state :: term) ::
|
||||
{:noreply, new_state}
|
||||
@@ -603,13 +599,15 @@ defmodule GenServer do
|
||||
`reason` is exit reason and `state` is the current state of the `GenServer`.
|
||||
The return value is ignored.
|
||||
|
||||
`c:terminate/2` is called if the `GenServer` traps exits (using `Process.flag/2`)
|
||||
*and* the parent process sends an exit signal, or a callback (except `c:init/1`)
|
||||
does one of the following:
|
||||
`c:terminate/2` is called if a callback (except `c:init/1`) does one of the
|
||||
following:
|
||||
|
||||
* returns a `:stop` tuple
|
||||
* raises (via `Kernel.raise/2`) or exits (via `Kernel.exit/1`)
|
||||
* raises
|
||||
* calls `Kernel.exit/1`
|
||||
* returns an invalid value
|
||||
* the `GenServer` traps exits (using `Process.flag/2`) *and* the parent
|
||||
process sends an exit signal
|
||||
|
||||
If part of a supervision tree, a `GenServer` will receive an exit
|
||||
signal when the tree is shutting down. The exit signal is based on
|
||||
@@ -716,7 +714,7 @@ defmodule GenServer do
|
||||
{:debug, debug}
|
||||
| {:name, name}
|
||||
| {:timeout, timeout}
|
||||
| {:spawn_opt, [Process.spawn_opt()]}
|
||||
| {:spawn_opt, Process.spawn_opt()}
|
||||
| {:hibernate_after, timeout}
|
||||
|
||||
@typedoc "Debug options supported by the `start*` functions"
|
||||
@@ -895,7 +893,7 @@ defmodule GenServer do
|
||||
milliseconds initializing or it will be terminated and the start function
|
||||
will return `{:error, :timeout}`
|
||||
|
||||
* `:debug` - if present, the corresponding function in the [`:sys` module](`:sys`) is invoked
|
||||
* `:debug` - if present, the corresponding function in the [`:sys` module](http://www.erlang.org/doc/man/sys.html) is invoked
|
||||
|
||||
* `:spawn_opt` - if present, its value is passed as options to the
|
||||
underlying process as in `Process.spawn/4`
|
||||
@@ -1018,7 +1016,6 @@ defmodule GenServer do
|
||||
nil ->
|
||||
exit({:noproc, {__MODULE__, :call, [server, request, timeout]}})
|
||||
|
||||
# TODO: remove this clause when we require Erlang/OTP 25+
|
||||
pid when pid == self() ->
|
||||
exit({:calling_self, {__MODULE__, :call, [server, request, timeout]}})
|
||||
|
||||
@@ -1042,8 +1039,18 @@ defmodule GenServer do
|
||||
is unknown whether the destination `server` successfully
|
||||
handled the message.
|
||||
|
||||
`c:handle_cast/2` will be called on the server to handle
|
||||
the request. In case the `server` is on a node which is
|
||||
not yet connected to the caller one, the semantics differ
|
||||
depending on the used Erlang/OTP version.
|
||||
|
||||
`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
|
||||
not block the call.
|
||||
"""
|
||||
@spec cast(server, term) :: :ok
|
||||
def cast(server, request)
|
||||
|
||||
@@ -26,8 +26,8 @@ defprotocol Inspect do
|
||||
defimpl Inspect, for: MapSet do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(map_set, opts) do
|
||||
concat(["#MapSet<", to_doc(MapSet.to_list(map_set), opts), ">"])
|
||||
def inspect(dict, opts) do
|
||||
concat(["#MapSet<", to_doc(MapSet.to_list(dict), opts), ">"])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -157,7 +157,7 @@ defimpl Inspect, for: BitString do
|
||||
defp each_bit(bitstring, _counter, opts) do
|
||||
size = bit_size(bitstring)
|
||||
<<h::size(size)>> = bitstring
|
||||
concat(Inspect.Integer.inspect(h, opts), "::size(" <> Integer.to_string(size) <> ")")
|
||||
Inspect.Integer.inspect(h, opts) <> "::size(" <> Integer.to_string(size) <> ")"
|
||||
end
|
||||
|
||||
@compile {:inline, decrement: 1}
|
||||
@@ -454,17 +454,16 @@ defimpl Inspect, for: Any do
|
||||
end
|
||||
|
||||
def inspect(map, name, opts) do
|
||||
map = Map.to_list(map) ++ [:...]
|
||||
# Use the :limit option and an extra element to force
|
||||
# `container_doc/6` to append "...".
|
||||
opts = %{opts | limit: min(opts.limit, map_size(map))}
|
||||
map = Map.to_list(map) ++ ["..."]
|
||||
|
||||
open = color("#" <> name <> "<", :map, opts)
|
||||
sep = color(",", :map, opts)
|
||||
close = color(">", :map, opts)
|
||||
|
||||
fun = fn
|
||||
{key, value}, opts -> Inspect.List.keyword({key, value}, opts)
|
||||
:..., _opts -> "..."
|
||||
end
|
||||
|
||||
container_doc(open, map, close, opts, fun, separator: sep, break: :strict)
|
||||
container_doc(open, map, close, opts, &Inspect.List.keyword/2, separator: sep, break: :strict)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ defmodule Inspect.Opts do
|
||||
to pass the custom options through.
|
||||
|
||||
* `:inspect_fun` (since v1.9.0) - a function to build algebra documents.
|
||||
Defaults to `Inspect.Opts.default_inspect_fun/0`.
|
||||
Defaults to `Inspect.inspect/2`.
|
||||
|
||||
* `:limit` - limits the number of items that are inspected for tuples,
|
||||
bitstrings, maps, lists and any other collection of items, with the exception of
|
||||
@@ -105,62 +105,6 @@ defmodule Inspect.Opts do
|
||||
syntax_colors: [{color_key, IO.ANSI.ansidata()}],
|
||||
width: non_neg_integer | :infinity
|
||||
}
|
||||
|
||||
@doc """
|
||||
Builds an `Inspect.Opts` struct.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec new(keyword()) :: t
|
||||
def new(opts) do
|
||||
struct(%Inspect.Opts{inspect_fun: default_inspect_fun()}, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the default inspect function.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec default_inspect_fun() :: (term, t -> Inspect.Algebra.t())
|
||||
def default_inspect_fun do
|
||||
:persistent_term.get({__MODULE__, :inspect_fun}, &Inspect.inspect/2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sets the default inspect function.
|
||||
|
||||
Set this option with care as it will change how all values
|
||||
in the system are inspected. The main use of this functionality
|
||||
is to provide an entry point to filter inspected values,
|
||||
in order for entities to comply with rules and legislations
|
||||
on data security and data privacy.
|
||||
|
||||
It is **extremely discouraged** for libraries to set their own
|
||||
function as this must be controlled by applications. Libraries
|
||||
should instead define their own structs with custom inspect
|
||||
implementations. If a library must change the default inspect
|
||||
function, then it is best to define to ask users of your library
|
||||
to explicitly call `default_inspect_fun/1` with your function of
|
||||
choice.
|
||||
|
||||
The default is `Inspect.inspect/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
previous_fun = Inspect.Opts.default_inspect_fun()
|
||||
|
||||
Inspect.Opts.default_inspect_fun(fn
|
||||
%{address: _} = map, opts ->
|
||||
previous_fun.(%{map | address: "[REDACTED]"}, opts)
|
||||
|
||||
value, opts ->
|
||||
previous_fun.(value, opts)
|
||||
end)
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec default_inspect_fun((term, t -> Inspect.Algebra.t())) :: :ok
|
||||
def default_inspect_fun(fun) when is_function(fun, 2) do
|
||||
:persistent_term.put({__MODULE__, :inspect_fun}, fun)
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Inspect.Error do
|
||||
@@ -354,13 +298,7 @@ defmodule Inspect.Algebra do
|
||||
try do
|
||||
Process.put(:inspect_trap, true)
|
||||
|
||||
res =
|
||||
Inspect.Map.inspect(struct, %{
|
||||
opts
|
||||
| syntax_colors: [],
|
||||
inspect_fun: Inspect.Opts.default_inspect_fun()
|
||||
})
|
||||
|
||||
res = Inspect.Map.inspect(struct, %{opts | syntax_colors: []})
|
||||
res = IO.iodata_to_binary(format(res, :infinity))
|
||||
|
||||
message =
|
||||
@@ -370,10 +308,7 @@ defmodule Inspect.Algebra do
|
||||
exception = Inspect.Error.exception(message: message)
|
||||
|
||||
if opts.safe do
|
||||
Inspect.inspect(exception, %{
|
||||
opts
|
||||
| inspect_fun: Inspect.Opts.default_inspect_fun()
|
||||
})
|
||||
Inspect.inspect(exception, opts)
|
||||
else
|
||||
reraise(exception, __STACKTRACE__)
|
||||
end
|
||||
@@ -601,7 +536,7 @@ defmodule Inspect.Algebra do
|
||||
Colors a document if the `color_key` has a color in the options.
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec color(t, Inspect.Opts.color_key(), Inspect.Opts.t()) :: t
|
||||
@spec color(t, Inspect.Opts.color_key(), Inspect.Opts.t()) :: doc_color
|
||||
def color(doc, color_key, %Inspect.Opts{syntax_colors: syntax_colors}) when is_doc(doc) do
|
||||
if precolor = Keyword.get(syntax_colors, color_key) do
|
||||
postcolor = Keyword.get(syntax_colors, :reset, :reset)
|
||||
@@ -959,12 +894,12 @@ defmodule Inspect.Algebra do
|
||||
format(width, 0, [{0, :flat, doc}])
|
||||
end
|
||||
|
||||
# Type representing the document mode to be rendered:
|
||||
# Type representing the document mode to be rendered
|
||||
#
|
||||
# * flat - represents a document with breaks as flats (a break may fit, as it may break)
|
||||
# * break - represents a document with breaks as breaks (a break always fits, since it breaks)
|
||||
#
|
||||
# The following modes are exclusive to fitting:
|
||||
# The following modes are exclusive to fitting
|
||||
#
|
||||
# * flat_no_break - represents a document with breaks as flat not allowed to enter in break mode
|
||||
# * break_no_flat - represents a document with breaks as breaks not allowed to enter in flat mode
|
||||
|
||||
+45
-110
@@ -64,53 +64,10 @@ defmodule Integer do
|
||||
"""
|
||||
defguard is_even(integer) when is_integer(integer) and (integer &&& 1) == 0
|
||||
|
||||
@doc """
|
||||
Computes `base` raised to power of `exponent`.
|
||||
|
||||
Both `base` and `exponent` must be integers.
|
||||
The exponent must be zero or positive.
|
||||
|
||||
See `Float.pow/2` for exponentiation of negative
|
||||
exponents as well as floats.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.pow(2, 0)
|
||||
1
|
||||
iex> Integer.pow(2, 1)
|
||||
2
|
||||
iex> Integer.pow(2, 10)
|
||||
1024
|
||||
iex> Integer.pow(2, 11)
|
||||
2048
|
||||
iex> Integer.pow(2, 64)
|
||||
0x10000000000000000
|
||||
|
||||
iex> Integer.pow(3, 4)
|
||||
81
|
||||
iex> Integer.pow(4, 3)
|
||||
64
|
||||
|
||||
iex> Integer.pow(-2, 3)
|
||||
-8
|
||||
iex> Integer.pow(-2, 4)
|
||||
16
|
||||
|
||||
iex> Integer.pow(2, -2)
|
||||
** (ArithmeticError) bad argument in arithmetic expression
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec pow(integer, non_neg_integer) :: integer
|
||||
def pow(base, exponent) when is_integer(base) and is_integer(exponent) do
|
||||
if exponent < 0, do: :erlang.error(:badarith, [base, exponent])
|
||||
base ** exponent
|
||||
end
|
||||
|
||||
@doc """
|
||||
Computes the modulo remainder of an integer division.
|
||||
|
||||
This function performs a [floored division](`floor_div/2`), which means that
|
||||
`Integer.mod/2` uses floored division, which means that
|
||||
the result will always have the sign of the `divisor`.
|
||||
|
||||
Raises an `ArithmeticError` exception if one of the arguments is not an
|
||||
@@ -142,8 +99,8 @@ defmodule Integer do
|
||||
Raises an `ArithmeticError` exception if one of the arguments is not an
|
||||
integer, or when the `divisor` is `0`.
|
||||
|
||||
This function performs a *floored* integer division, which means that
|
||||
the result will always be rounded towards negative infinity.
|
||||
`Integer.floor_div/2` performs *floored* integer division. This means that
|
||||
the result is always rounded towards negative infinity.
|
||||
|
||||
If you want to perform truncated integer division (rounding towards zero),
|
||||
use `Kernel.div/2` instead.
|
||||
@@ -269,7 +226,7 @@ defmodule Integer do
|
||||
** (ArgumentError) invalid base 38
|
||||
|
||||
"""
|
||||
@spec parse(binary, 2..36) :: {integer, remainder_of_binary :: binary} | :error
|
||||
@spec parse(binary, 2..36) :: {integer, binary} | :error
|
||||
def parse(binary, base \\ 10)
|
||||
|
||||
def parse(_binary, base) when base not in 2..36 do
|
||||
@@ -312,12 +269,12 @@ defmodule Integer do
|
||||
|
||||
defp count_digits_nosign(<<_::bits>>, _, 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` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36. If no `base` is given,
|
||||
it defaults to `10`.
|
||||
of `integer`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -335,6 +292,22 @@ defmodule Integer do
|
||||
iex> Integer.to_string(0123)
|
||||
"123"
|
||||
|
||||
"""
|
||||
@spec to_string(integer) :: String.t()
|
||||
def to_string(integer) do
|
||||
:erlang.integer_to_binary(integer)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_string(100, 16)
|
||||
"64"
|
||||
|
||||
@@ -346,16 +319,15 @@ defmodule Integer do
|
||||
|
||||
"""
|
||||
@spec to_string(integer, 2..36) :: String.t()
|
||||
def to_string(integer, base \\ 10) do
|
||||
def to_string(integer, base) do
|
||||
:erlang.integer_to_binary(integer, base)
|
||||
end
|
||||
|
||||
# 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 `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36. If no `base` is given,
|
||||
it defaults to `10`.
|
||||
Returns a charlist which corresponds to the text representation of the given `integer`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -373,6 +345,21 @@ defmodule Integer do
|
||||
iex> Integer.to_charlist(0123)
|
||||
'123'
|
||||
|
||||
"""
|
||||
@spec to_charlist(integer) :: charlist
|
||||
def to_charlist(integer) do
|
||||
:erlang.integer_to_list(integer)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_charlist(100, 16)
|
||||
'64'
|
||||
|
||||
@@ -384,7 +371,7 @@ defmodule Integer do
|
||||
|
||||
"""
|
||||
@spec to_charlist(integer, 2..36) :: charlist
|
||||
def to_charlist(integer, base \\ 10) do
|
||||
def to_charlist(integer, base) do
|
||||
:erlang.integer_to_list(integer, base)
|
||||
end
|
||||
|
||||
@@ -427,58 +414,6 @@ defmodule Integer do
|
||||
defp gcd_positive(integer1, 0), do: integer1
|
||||
defp gcd_positive(integer1, integer2), do: gcd_positive(integer2, rem(integer1, integer2))
|
||||
|
||||
@doc """
|
||||
Returns the extended greatest common divisor of the two given integers.
|
||||
|
||||
This function uses the extended Euclidean algorithm to return a three-element tuple with the `gcd`
|
||||
and the coefficients `m` and `n` of Bézout's identity such that:
|
||||
|
||||
gcd(a, b) = m*a + n*b
|
||||
|
||||
By convention, `extended_gcd(0, 0)` returns `{0, 0, 0}`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.extended_gcd(240, 46)
|
||||
{2, -9, 47}
|
||||
iex> Integer.extended_gcd(46, 240)
|
||||
{2, 47, -9}
|
||||
iex> Integer.extended_gcd(-46, 240)
|
||||
{2, -47, -9}
|
||||
iex> Integer.extended_gcd(-46, -240)
|
||||
{2, -47, 9}
|
||||
|
||||
iex> Integer.extended_gcd(14, 21)
|
||||
{7, -1, 1}
|
||||
|
||||
iex> Integer.extended_gcd(10, 0)
|
||||
{10, 1, 0}
|
||||
iex> Integer.extended_gcd(0, 10)
|
||||
{10, 0, 1}
|
||||
iex> Integer.extended_gcd(0, 0)
|
||||
{0, 0, 0}
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec extended_gcd(integer, integer) :: {non_neg_integer, integer, integer}
|
||||
def extended_gcd(0, 0), do: {0, 0, 0}
|
||||
def extended_gcd(0, b), do: {b, 0, 1}
|
||||
def extended_gcd(a, 0), do: {a, 1, 0}
|
||||
|
||||
def extended_gcd(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
|
||||
extended_gcd(integer2, integer1, 0, 1, 1, 0)
|
||||
end
|
||||
|
||||
defp extended_gcd(r1, r0, s1, s0, t1, t0) do
|
||||
div = div(r0, r1)
|
||||
|
||||
case r0 - div * r1 do
|
||||
0 when r1 > 0 -> {r1, s1, t1}
|
||||
0 when r1 < 0 -> {-r1, -s1, -t1}
|
||||
r2 -> extended_gcd(r2, r1, s0 - div * s1, s1, t0 - div * t1, t1)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Integer.to_charlist/1 instead"
|
||||
def to_char_list(integer), do: Integer.to_charlist(integer)
|
||||
|
||||
+58
-96
@@ -34,7 +34,7 @@ defmodule IO do
|
||||
IO data is a data type that can be used as a more efficient alternative to binaries
|
||||
in certain situations.
|
||||
|
||||
A term of type **IO data** is a binary or a list containing bytes (integers within the `0..255` range)
|
||||
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"`:
|
||||
|
||||
@@ -99,8 +99,8 @@ defmodule IO do
|
||||
Erlang and Elixir also have the idea of `t:chardata/0`. Chardata is very
|
||||
similar to IO data: the only difference is that integers in IO data represent
|
||||
bytes while integers in chardata represent Unicode code points. Bytes
|
||||
(`t:byte/0`) are integers within the `0..255` range, while Unicode code points
|
||||
(`t:char/0`) are integers within the `0..0x10FFFF` range. The `IO` module provides
|
||||
(`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.
|
||||
|
||||
@@ -108,8 +108,8 @@ defmodule IO do
|
||||
argument error. For example, let's try to put a code point that is not
|
||||
representable with one byte, like `?π`, inside IO data:
|
||||
|
||||
IO.iodata_to_binary(["The symbol for pi is: ", ?π])
|
||||
#=> ** (ArgumentError) argument error
|
||||
iex> IO.iodata_to_binary(["The symbol for pi is: ", ?π])
|
||||
** (ArgumentError) argument error
|
||||
|
||||
If we use chardata instead, it will work as expected:
|
||||
|
||||
@@ -122,14 +122,14 @@ defmodule IO do
|
||||
@type nodata :: {:error, term} | :eof
|
||||
@type chardata :: String.t() | maybe_improper_list(char | chardata, String.t() | [])
|
||||
|
||||
defguardp is_device(term) when is_atom(term) or is_pid(term)
|
||||
defguardp is_iodata(data) when is_list(data) or is_binary(data)
|
||||
|
||||
@doc """
|
||||
Reads from the IO `device`.
|
||||
|
||||
The `device` is iterated by the given number of characters, line by line if
|
||||
`:line` is given, or until `:eof`.
|
||||
The `device` is iterated by the given number of characters or line by line if
|
||||
`:line` is given.
|
||||
Alternatively, if `:all` is given, then whole `device` is returned.
|
||||
|
||||
It returns:
|
||||
|
||||
@@ -141,24 +141,14 @@ defmodule IO do
|
||||
for instance, `{:error, :estale}` if reading from an
|
||||
NFS volume
|
||||
|
||||
If `:all` is given, `:eof` is never returned, but an
|
||||
empty string in case the device has reached EOF.
|
||||
"""
|
||||
@spec read(device, :eof | :line | non_neg_integer) :: chardata | nodata
|
||||
@spec read(device, :all | :line | non_neg_integer) :: chardata | nodata
|
||||
def read(device \\ :stdio, line_or_chars)
|
||||
|
||||
# TODO: Deprecate me on v1.17
|
||||
def read(device, :all) do
|
||||
with :eof <- read(device, :eof) do
|
||||
with [_ | _] = opts <- :io.getopts(device),
|
||||
false <- Keyword.get(opts, :binary, true) do
|
||||
''
|
||||
else
|
||||
_ -> ""
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def read(device, :eof) do
|
||||
getn(device, '', :eof)
|
||||
do_read_all(map_dev(device), :empty)
|
||||
end
|
||||
|
||||
def read(device, :line) do
|
||||
@@ -169,11 +159,35 @@ defmodule IO do
|
||||
:io.get_chars(map_dev(device), '', count)
|
||||
end
|
||||
|
||||
defp do_read_all(mapped_dev, acc) do
|
||||
case :io.get_line(mapped_dev, "") do
|
||||
line when is_binary(line) or is_list(line) -> do_read_all(mapped_dev, concat(acc, line))
|
||||
:eof -> read_eof(mapped_dev, 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.
|
||||
|
||||
The `device` is iterated by the given number of bytes, line by line if
|
||||
`:line` is given, or until `:eof`.
|
||||
The `device` is iterated by the given number of bytes or line by line if
|
||||
`:line` is given.
|
||||
Alternatively, if `:all` is given, then whole `device` is returned.
|
||||
|
||||
It returns:
|
||||
|
||||
@@ -185,19 +199,17 @@ defmodule IO do
|
||||
for instance, `{:error, :estale}` if reading from an
|
||||
NFS volume
|
||||
|
||||
If `:all` is given, `:eof` is never returned, but an
|
||||
empty string in case the device has reached EOF.
|
||||
|
||||
Note: do not use this function on IO devices in Unicode mode
|
||||
as it will return the wrong result.
|
||||
"""
|
||||
@spec binread(device, :eof | :line | non_neg_integer) :: iodata | nodata
|
||||
@spec binread(device, :all | :line | non_neg_integer) :: iodata | nodata
|
||||
def binread(device \\ :stdio, line_or_chars)
|
||||
|
||||
# TODO: Deprecate me on v1.17
|
||||
def binread(device, :all) do
|
||||
with :eof <- binread(device, :eof), do: ""
|
||||
end
|
||||
|
||||
def binread(device, :eof) do
|
||||
binread_eof(map_dev(device), "")
|
||||
do_binread_all(map_dev(device), "")
|
||||
end
|
||||
|
||||
def binread(device, :line) do
|
||||
@@ -215,10 +227,10 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@read_all_size 4096
|
||||
defp binread_eof(mapped_dev, acc) do
|
||||
defp do_binread_all(mapped_dev, acc) do
|
||||
case :file.read(mapped_dev, @read_all_size) do
|
||||
{:ok, data} -> binread_eof(mapped_dev, acc <> data)
|
||||
:eof -> if acc == "", do: :eof, else: acc
|
||||
{:ok, data} -> do_binread_all(mapped_dev, acc <> data)
|
||||
:eof -> acc
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
@@ -281,7 +293,7 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec puts(device, chardata | String.Chars.t()) :: :ok
|
||||
def puts(device \\ :stdio, item) when is_device(device) do
|
||||
def puts(device \\ :stdio, item) do
|
||||
:io.put_chars(map_dev(device), [to_chardata(item), ?\n])
|
||||
end
|
||||
|
||||
@@ -305,7 +317,7 @@ defmodule IO do
|
||||
@spec warn(chardata | String.Chars.t(), Exception.stacktrace()) :: :ok
|
||||
def warn(message, []) do
|
||||
message = [to_chardata(message), ?\n]
|
||||
:elixir_errors.log_and_print_warning(0, nil, message, message)
|
||||
:elixir_errors.io_warn(0, nil, message, message)
|
||||
end
|
||||
|
||||
def warn(message, [{_, _, _, opts} | _] = stacktrace) do
|
||||
@@ -314,7 +326,7 @@ defmodule IO do
|
||||
line = opts[:line]
|
||||
file = opts[:file]
|
||||
|
||||
:elixir_errors.log_and_print_warning(
|
||||
:elixir_errors.io_warn(
|
||||
line || 0,
|
||||
file && List.to_string(file),
|
||||
message,
|
||||
@@ -416,9 +428,9 @@ defmodule IO do
|
||||
See `inspect/2` for a full list of options.
|
||||
"""
|
||||
@spec inspect(device, item, keyword) :: item when item: var
|
||||
def inspect(device, item, opts) when is_device(device) and is_list(opts) do
|
||||
def inspect(device, item, opts) when is_list(opts) do
|
||||
label = if label = opts[:label], do: [to_chardata(label), ": "], else: []
|
||||
opts = Inspect.Opts.new(opts)
|
||||
opts = struct(Inspect.Opts, opts)
|
||||
doc = Inspect.Algebra.group(Inspect.Algebra.to_doc(item, opts))
|
||||
chardata = Inspect.Algebra.format(doc, opts.width)
|
||||
puts(device, [label, chardata])
|
||||
@@ -433,18 +445,12 @@ defmodule IO do
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
|
||||
See `IO.getn/3` for a description of return values.
|
||||
|
||||
"""
|
||||
@spec getn(
|
||||
device | chardata | String.Chars.t(),
|
||||
pos_integer | :eof | chardata | String.Chars.t()
|
||||
) ::
|
||||
@spec getn(device | chardata | String.Chars.t(), pos_integer | chardata | String.Chars.t()) ::
|
||||
chardata | nodata
|
||||
def getn(prompt, count \\ 1)
|
||||
|
||||
def getn(prompt, :eof) do
|
||||
getn(:stdio, prompt, :eof)
|
||||
end
|
||||
|
||||
def getn(prompt, count) when is_integer(count) and count > 0 do
|
||||
getn(:stdio, prompt, count)
|
||||
end
|
||||
@@ -471,27 +477,11 @@ defmodule IO do
|
||||
NFS volume
|
||||
|
||||
"""
|
||||
@spec getn(device, chardata | String.Chars.t(), pos_integer | :eof) :: chardata | nodata
|
||||
def getn(device, prompt, :eof) do
|
||||
getn_eof(map_dev(device), to_chardata(prompt), [])
|
||||
end
|
||||
|
||||
@spec getn(device, chardata | String.Chars.t(), pos_integer) :: chardata | nodata
|
||||
def getn(device, prompt, count) when is_integer(count) and count > 0 do
|
||||
:io.get_chars(map_dev(device), to_chardata(prompt), count)
|
||||
end
|
||||
|
||||
defp getn_eof(device, prompt, acc) do
|
||||
case :io.get_line(device, prompt) do
|
||||
line when is_binary(line) or is_list(line) -> getn_eof(device, '', [line | acc])
|
||||
:eof -> wrap_eof(:lists.reverse(acc))
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
defp wrap_eof([h | _] = acc) when is_binary(h), do: IO.iodata_to_binary(acc)
|
||||
defp wrap_eof([h | _] = acc) when is_list(h), do: :lists.flatten(acc)
|
||||
defp wrap_eof([]), do: :eof
|
||||
|
||||
@doc ~S"""
|
||||
Reads a line from the IO `device`.
|
||||
|
||||
@@ -518,17 +508,6 @@ defmodule IO do
|
||||
:io.get_line(map_dev(device), to_chardata(prompt))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a line-based `IO.Stream` on `:stdio`.
|
||||
|
||||
This is equivalent to:
|
||||
|
||||
IO.stream(:stdio, :line)
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
def stream, do: stream(:stdio, :line)
|
||||
|
||||
@doc """
|
||||
Converts the IO `device` into an `IO.Stream`.
|
||||
|
||||
@@ -545,9 +524,6 @@ defmodule IO do
|
||||
Note that an IO stream has side effects and every time
|
||||
you go over the stream you may get different results.
|
||||
|
||||
`stream/1` has been introduced in Elixir v1.12.0,
|
||||
while `stream/2` has been available since v1.0.0.
|
||||
|
||||
## Examples
|
||||
|
||||
Here is an example on how we mimic an echo server
|
||||
@@ -557,23 +533,12 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec stream(device, :line | pos_integer) :: Enumerable.t()
|
||||
def stream(device \\ :stdio, line_or_codepoints)
|
||||
def stream(device, line_or_codepoints)
|
||||
when line_or_codepoints == :line
|
||||
when is_integer(line_or_codepoints) and line_or_codepoints > 0 do
|
||||
IO.Stream.__build__(map_dev(device), false, line_or_codepoints)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a raw, line-based `IO.Stream` on `:stdio`. The operation is Unicode unsafe.
|
||||
|
||||
This is equivalent to:
|
||||
|
||||
IO.binstream(:stdio, :line)
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
def binstream, do: binstream(:stdio, :line)
|
||||
|
||||
@doc """
|
||||
Converts the IO `device` into an `IO.Stream`. The operation is Unicode unsafe.
|
||||
|
||||
@@ -582,7 +547,8 @@ defmodule IO do
|
||||
and write.
|
||||
|
||||
The `device` is iterated by the given number of bytes or line by line if
|
||||
`:line` is given. This reads from the IO device as a raw binary.
|
||||
`:line` is given.
|
||||
This reads from the IO device as a raw binary.
|
||||
|
||||
Note that an IO stream has side effects and every time
|
||||
you go over the stream you may get different results.
|
||||
@@ -590,11 +556,9 @@ defmodule IO do
|
||||
Finally, do not use this function on IO devices in Unicode
|
||||
mode as it will return the wrong result.
|
||||
|
||||
`binstream/1` has been introduced in Elixir v1.12.0,
|
||||
while `binstream/2` has been available since v1.0.0.
|
||||
"""
|
||||
@spec binstream(device, :line | pos_integer) :: Enumerable.t()
|
||||
def binstream(device \\ :stdio, line_or_bytes)
|
||||
def binstream(device, line_or_bytes)
|
||||
when line_or_bytes == :line
|
||||
when is_integer(line_or_bytes) and line_or_bytes > 0 do
|
||||
IO.Stream.__build__(map_dev(device), true, line_or_bytes)
|
||||
@@ -622,8 +586,6 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec chardata_to_string(chardata) :: String.t()
|
||||
def chardata_to_string(chardata)
|
||||
|
||||
def chardata_to_string(string) when is_binary(string) do
|
||||
string
|
||||
end
|
||||
|
||||
@@ -242,7 +242,7 @@ defmodule IO.ANSI do
|
||||
performed. If you don't want this behaviour, use `format_fragment/2`.
|
||||
|
||||
An optional boolean parameter can be passed to enable or disable
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will be emitted.
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
|
||||
By default checks if ANSI is enabled using the `enabled?/0` function.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -194,10 +194,6 @@ defmodule IO.ANSI.Docs do
|
||||
inline_text("*", traverse_erlang_html(entries, indent, options), options)
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({tag, _, entries}, indent, options) when tag in [:strong, :b] do
|
||||
inline_text("**", traverse_erlang_html(entries, indent, options), options)
|
||||
end
|
||||
|
||||
defp traverse_erlang_html({:code, _, entries}, indent, options) do
|
||||
inline_text("`", traverse_erlang_html(entries, indent, options), options)
|
||||
end
|
||||
@@ -299,7 +295,7 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp inline_html?(binary) when is_binary(binary), do: true
|
||||
defp inline_html?({tag, _, _}) when tag in [:a, :code, :em, :i, :strong, :b, :br], do: true
|
||||
defp inline_html?({tag, _, _}) when tag in [:a, :code, :em, :i, :br], do: true
|
||||
defp inline_html?(_), do: false
|
||||
|
||||
## Markdown
|
||||
@@ -802,14 +798,14 @@ defmodule IO.ANSI.Docs do
|
||||
Regex.replace(~r{\[([^\]]*?)\]\((.*?)\)}, text, "\\1 (\\2)")
|
||||
end
|
||||
|
||||
# We have four entries: **, __, *, _ and `.
|
||||
# We have four entries: **, *, _ and `.
|
||||
#
|
||||
# The first four behave the same while the last one is simpler
|
||||
# The first three behave the same while the last one is simpler
|
||||
# when it comes to delimiters as it ignores spaces and escape
|
||||
# characters. But, since the first two has two characters,
|
||||
# we need to handle 3 cases:
|
||||
# characters. But, since the first has two characters, we need to
|
||||
# handle 3 cases:
|
||||
#
|
||||
# 1. __ and **
|
||||
# 1. **
|
||||
# 2. _ and *
|
||||
# 3. `
|
||||
#
|
||||
@@ -823,8 +819,8 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
### Inline start
|
||||
|
||||
defp handle_inline(<<mark, mark, rest::binary>>, options) when mark in @single do
|
||||
handle_inline(rest, [mark | mark], [<<mark, mark>>], [], options)
|
||||
defp handle_inline(<<?*, ?*, rest::binary>>, options) do
|
||||
handle_inline(rest, ?d, ["**"], [], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, rest::binary>>, options) when mark in @single do
|
||||
@@ -837,10 +833,9 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
### Inline delimiters
|
||||
|
||||
defp handle_inline(<<delimiter, mark, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters and mark in @single do
|
||||
acc = [delimiter, Enum.reverse(buffer) | acc]
|
||||
handle_inline(rest, [mark | mark], [<<mark, mark>>], acc, options)
|
||||
defp handle_inline(<<delimiter, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters do
|
||||
handle_inline(rest, ?d, ["**"], [delimiter, Enum.reverse(buffer) | acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<delimiter, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
@@ -855,10 +850,9 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
### Clauses for handling escape
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, mark, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and mark in @single do
|
||||
acc = [?\\, Enum.reverse(buffer) | acc]
|
||||
handle_inline(rest, [mark | mark], [<<mark, mark>>], acc, options)
|
||||
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer) | acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
@@ -877,8 +871,8 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
### Inline end
|
||||
|
||||
defp handle_inline(<<mark, mark, delimiter, rest::binary>>, [mark | mark], buffer, acc, options)
|
||||
when delimiter in @delimiters and mark in @single do
|
||||
defp handle_inline(<<?*, ?*, delimiter, rest::binary>>, ?d, buffer, acc, options)
|
||||
when delimiter in @delimiters do
|
||||
inline_buffer = inline_buffer(buffer, options)
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer | acc], options)
|
||||
end
|
||||
@@ -889,8 +883,8 @@ defmodule IO.ANSI.Docs do
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer | acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, mark, rest::binary>>, [mark | mark], buffer, acc, options)
|
||||
when rest == "" and mark in @single do
|
||||
defp handle_inline(<<?*, ?*, rest::binary>>, ?d, buffer, acc, options)
|
||||
when rest == "" do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
end
|
||||
|
||||
@@ -936,11 +930,10 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp color_for(mark, colors) do
|
||||
case mark do
|
||||
"__" -> color(:doc_bold, colors)
|
||||
"**" -> color(:doc_bold, colors)
|
||||
"_" -> color(:doc_underline, colors)
|
||||
"*" -> color(:doc_underline, colors)
|
||||
"`" -> color(:doc_inline_code, colors)
|
||||
"_" -> color(:doc_underline, colors)
|
||||
"*" -> color(:doc_bold, colors)
|
||||
"**" -> color(:doc_bold, colors)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+308
-778
File diff suppressed because it is too large
Load Diff
@@ -16,8 +16,6 @@ defmodule Kernel.CLI do
|
||||
profile: nil
|
||||
}
|
||||
|
||||
@standalone_opts ["-h", "--help", "--short-version"]
|
||||
|
||||
@doc """
|
||||
This is the API invoked by Elixir boot process.
|
||||
"""
|
||||
@@ -90,7 +88,7 @@ defmodule Kernel.CLI do
|
||||
case blamed do
|
||||
%FunctionClauseError{} ->
|
||||
formatted = Exception.format_banner(kind, reason, stacktrace)
|
||||
padded_blame = pad(FunctionClauseError.blame(blamed, &inspect/1, &blame_match/1))
|
||||
padded_blame = pad(FunctionClauseError.blame(blamed, &inspect/1, &blame_match/2))
|
||||
[formatted, padded_blame]
|
||||
|
||||
_ ->
|
||||
@@ -178,8 +176,9 @@ defmodule Kernel.CLI do
|
||||
IO.write(:stderr, format_error(kind, reason, stacktrace))
|
||||
end
|
||||
|
||||
defp blame_match(%{match?: true, node: node}), do: blame_ansi(:normal, "+", node)
|
||||
defp blame_match(%{match?: false, node: node}), do: blame_ansi(:red, "-", node)
|
||||
defp blame_match(%{match?: true, node: node}, _), do: blame_ansi(:normal, "+", node)
|
||||
defp blame_match(%{match?: false, node: node}, _), do: blame_ansi(:red, "-", node)
|
||||
defp blame_match(_, string), do: string
|
||||
|
||||
defp blame_ansi(color, no_ansi, node) do
|
||||
if IO.ANSI.enabled?() do
|
||||
@@ -196,9 +195,8 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
|
||||
@elixir_internals [:elixir, :elixir_aliases, :elixir_expand, :elixir_compiler, :elixir_module] ++
|
||||
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map, :elixir_locals] ++
|
||||
[:elixir_erl, :elixir_erl_clauses, :elixir_erl_compiler, :elixir_erl_pass] ++
|
||||
[Kernel.ErrorHandler, Module.ParallelChecker]
|
||||
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map] ++
|
||||
[:elixir_erl, :elixir_erl_clauses, :elixir_erl_pass, Kernel.ErrorHandler]
|
||||
|
||||
defp prune_stacktrace([{mod, _, _, _} | t]) when mod in @elixir_internals do
|
||||
prune_stacktrace(t)
|
||||
@@ -218,16 +216,7 @@ defmodule Kernel.CLI do
|
||||
|
||||
# Parse shared options
|
||||
|
||||
defp warn_standalone(opt) do
|
||||
IO.puts(:stderr, "#{opt} : Standalone options can't be combined with other options")
|
||||
end
|
||||
|
||||
defp parse_shared([opt | _], _config) when opt in @standalone_opts do
|
||||
warn_standalone(opt)
|
||||
System.halt(1)
|
||||
end
|
||||
|
||||
defp parse_shared([opt | t], _config) when opt in ["-v", "--version"] do
|
||||
defp parse_shared([opt | _t], _config) when opt in ["-v", "--version"] do
|
||||
if function_exported?(IEx, :started?, 0) and IEx.started?() do
|
||||
IO.puts("IEx " <> System.build_info()[:build])
|
||||
else
|
||||
@@ -235,7 +224,6 @@ defmodule Kernel.CLI do
|
||||
IO.puts("Elixir " <> System.build_info()[:build])
|
||||
end
|
||||
|
||||
t != [] && warn_standalone(opt)
|
||||
System.halt(0)
|
||||
end
|
||||
|
||||
|
||||
@@ -30,10 +30,10 @@ defmodule Kernel.ErrorHandler do
|
||||
end
|
||||
|
||||
def ensure_compiled(module, kind, deadlock) do
|
||||
{compiler_pid, file_pid} = :erlang.get(:elixir_compiler_info)
|
||||
parent = :erlang.get(:elixir_compiler_pid)
|
||||
ref = :erlang.make_ref()
|
||||
modules = :elixir_module.compiler_modules()
|
||||
send(compiler_pid, {:waiting, kind, self(), ref, file_pid, module, modules, deadlock})
|
||||
send(parent, {:waiting, kind, self(), ref, module, modules, deadlock})
|
||||
:erlang.garbage_collect(self())
|
||||
|
||||
receive do
|
||||
|
||||
@@ -173,11 +173,10 @@ defmodule Kernel.LexicalTracker do
|
||||
end
|
||||
|
||||
def handle_cast({:add_import, module, fas, line, warn}, state) do
|
||||
to_remove = for {{:import, {^module, _, _}} = key, _} <- state.directives, do: key
|
||||
|
||||
directives =
|
||||
state.directives
|
||||
|> Map.drop(to_remove)
|
||||
|> Enum.reject(&match?({{:import, {^module, _, _}}, _}, &1))
|
||||
|> Map.new()
|
||||
|> add_directive(module, line, warn, :import)
|
||||
|
||||
directives =
|
||||
|
||||
@@ -3,12 +3,6 @@ defmodule Kernel.ParallelCompiler do
|
||||
A module responsible for compiling and requiring files in parallel.
|
||||
"""
|
||||
|
||||
@typedoc "The line. 0 indicates no line."
|
||||
@type line() :: non_neg_integer()
|
||||
@type location() :: line() | {line(), column :: non_neg_integer}
|
||||
@type warning() :: {file :: Path.t(), location(), message :: String.t()}
|
||||
@type error() :: {file :: Path.t(), line(), message :: String.t()}
|
||||
|
||||
@doc """
|
||||
Starts a task for parallel compilation.
|
||||
|
||||
@@ -22,28 +16,23 @@ defmodule Kernel.ParallelCompiler do
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
def async(fun) when is_function(fun, 0) do
|
||||
case :erlang.get(:elixir_compiler_info) do
|
||||
{compiler, _} ->
|
||||
file = :erlang.get(:elixir_compiler_file)
|
||||
dest = :erlang.get(:elixir_compiler_dest)
|
||||
if parent = :erlang.get(:elixir_compiler_pid) do
|
||||
file = :erlang.get(:elixir_compiler_file)
|
||||
dest = :erlang.get(:elixir_compiler_dest)
|
||||
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
|
||||
|
||||
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
|
||||
checker = Module.ParallelChecker.get()
|
||||
|
||||
Task.async(fn ->
|
||||
send(compiler, {:async, self()})
|
||||
Module.ParallelChecker.put(compiler, checker)
|
||||
:erlang.put(:elixir_compiler_info, {compiler, self()})
|
||||
:erlang.put(:elixir_compiler_file, file)
|
||||
dest != :undefined and :erlang.put(:elixir_compiler_dest, dest)
|
||||
:erlang.process_flag(:error_handler, error_handler)
|
||||
fun.()
|
||||
end)
|
||||
|
||||
:undefined ->
|
||||
raise ArgumentError,
|
||||
"cannot spawn parallel compiler task because " <>
|
||||
"the current file is not being compiled/required"
|
||||
Task.async(fn ->
|
||||
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)
|
||||
:erlang.process_flag(:error_handler, error_handler)
|
||||
fun.()
|
||||
end)
|
||||
else
|
||||
raise ArgumentError,
|
||||
"cannot spawn parallel compiler task because " <>
|
||||
"the current file is not being compiled/required"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -97,19 +86,11 @@ defmodule Kernel.ParallelCompiler do
|
||||
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec compile([Path.t()], keyword()) :: {:ok, [atom], [warning]} | {:error, [error], [warning]}
|
||||
def compile(files, options \\ []) when is_list(options) do
|
||||
spawn_workers(files, :compile, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Compiles the given files and writes resulting BEAM files into path.
|
||||
|
||||
See `compile/2` for more information.
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec compile_to_path([Path.t()], Path.t(), keyword()) ::
|
||||
{:ok, [atom], [warning]} | {:error, [error], [warning]}
|
||||
def compile_to_path(files, path, options \\ []) when is_binary(path) and is_list(options) do
|
||||
spawn_workers(files, {:compile, path}, options)
|
||||
end
|
||||
@@ -135,21 +116,10 @@ defmodule Kernel.ParallelCompiler do
|
||||
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec require([Path.t()], keyword()) ::
|
||||
{:ok, [atom], [warning]} | {:error, [error], [warning]}
|
||||
def require(files, options \\ []) when is_list(options) do
|
||||
spawn_workers(files, :require, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prints a warning returned by the compiler.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec print_warning(warning) :: :ok
|
||||
def print_warning({file, location, warning}) do
|
||||
:elixir_errors.print_warning(location, file, warning)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Kernel.ParallelCompiler.compile/2 instead"
|
||||
def files(files, options \\ []) when is_list(options) do
|
||||
@@ -171,32 +141,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
defp spawn_workers(files, output, options) do
|
||||
{:module, _} = :code.ensure_loaded(Kernel.ErrorHandler)
|
||||
schedulers = max(:erlang.system_info(:schedulers_online), 2)
|
||||
{:ok, checker} = Module.ParallelChecker.start_link(schedulers)
|
||||
|
||||
try do
|
||||
outcome = spawn_workers(schedulers, checker, files, output, options)
|
||||
{outcome, Code.get_compiler_option(:warnings_as_errors)}
|
||||
else
|
||||
{{:ok, _, [_ | _] = warnings}, true} ->
|
||||
message = "Compilation failed due to warnings while using the --warnings-as-errors option"
|
||||
IO.puts(:stderr, message)
|
||||
{:error, warnings, []}
|
||||
|
||||
{{:ok, outcome, warnings}, _} ->
|
||||
beam_timestamp = Keyword.get(options, :beam_timestamp)
|
||||
{:ok, write_module_binaries(outcome, output, beam_timestamp), warnings}
|
||||
|
||||
{{:error, errors, warnings}, true} ->
|
||||
{:error, errors ++ warnings, []}
|
||||
|
||||
{{:error, errors, warnings}, _} ->
|
||||
{:error, errors, warnings}
|
||||
after
|
||||
Module.ParallelChecker.stop(checker)
|
||||
end
|
||||
end
|
||||
|
||||
defp spawn_workers(schedulers, checker, files, output, options) do
|
||||
beam_timestamp = Keyword.get(options, :beam_timestamp)
|
||||
threshold = Keyword.get(options, :long_compilation_threshold, 10) * 1000
|
||||
timer_ref = Process.send_after(self(), :threshold_check, threshold)
|
||||
|
||||
@@ -211,8 +156,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
output: output,
|
||||
timer_ref: timer_ref,
|
||||
long_compilation_threshold: threshold,
|
||||
schedulers: schedulers,
|
||||
checker: checker
|
||||
schedulers: schedulers
|
||||
})
|
||||
|
||||
Process.cancel_timer(state.timer_ref)
|
||||
@@ -223,7 +167,21 @@ defmodule Kernel.ParallelCompiler do
|
||||
0 -> :ok
|
||||
end
|
||||
|
||||
outcome
|
||||
case {outcome, Code.get_compiler_option(:warnings_as_errors)} do
|
||||
{{:ok, _, [_ | _] = warnings}, true} ->
|
||||
message = "Compilation failed due to warnings while using the --warnings-as-errors option"
|
||||
IO.puts(:stderr, message)
|
||||
{:error, warnings, []}
|
||||
|
||||
{{:ok, outcome, warnings}, _} ->
|
||||
{:ok, write_module_binaries(outcome, output, beam_timestamp), warnings}
|
||||
|
||||
{{:error, errors, warnings}, true} ->
|
||||
{:error, errors ++ warnings, []}
|
||||
|
||||
{{:error, errors, warnings}, _} ->
|
||||
{:error, errors, warnings}
|
||||
end
|
||||
end
|
||||
|
||||
defp each_file(fun) when is_function(fun, 1), do: fn file, _ -> fun.(file) end
|
||||
@@ -265,21 +223,32 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
|
||||
defp maybe_check_modules(result, runtime_modules, state) do
|
||||
%{profile: profile, checker: checker} = state
|
||||
%{schedulers: schedulers, profile: profile} = state
|
||||
|
||||
compiled_modules =
|
||||
for {{:module, _module}, {_binary, info}} <- result,
|
||||
do: info
|
||||
if :elixir_config.get(:bootstrap) do
|
||||
[]
|
||||
else
|
||||
compiled_modules = checker_compiled_modules(result)
|
||||
runtime_modules = checker_runtime_modules(runtime_modules)
|
||||
|
||||
runtime_modules =
|
||||
for module <- runtime_modules,
|
||||
path = :code.which(module),
|
||||
is_list(path) and path != [],
|
||||
do: {module, path}
|
||||
profile_checker(profile, compiled_modules, runtime_modules, fn ->
|
||||
Module.ParallelChecker.verify(compiled_modules, runtime_modules, schedulers)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
profile_checker(profile, compiled_modules, runtime_modules, fn ->
|
||||
Module.ParallelChecker.verify(checker, compiled_modules, runtime_modules)
|
||||
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_init(:time), do: {:time, System.monotonic_time(), 0}
|
||||
@@ -317,9 +286,9 @@ defmodule Kernel.ParallelCompiler do
|
||||
defp spawn_workers([{ref, found} | t], spawned, waiting, files, result, warnings, state) do
|
||||
{files, waiting} =
|
||||
case List.keytake(waiting, ref, 2) do
|
||||
{{_kind, pid, ^ref, file_pid, _on, _defining, _deadlock}, waiting} ->
|
||||
{{_kind, pid, ^ref, _on, _defining, _deadlock}, waiting} ->
|
||||
send(pid, {ref, found})
|
||||
{update_timing(files, file_pid, :waiting), waiting}
|
||||
{update_timing(files, pid, :waiting), waiting}
|
||||
|
||||
nil ->
|
||||
# In case the waiting process died (for example, it was an async process),
|
||||
@@ -331,14 +300,13 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
|
||||
defp spawn_workers([file | queue], spawned, waiting, files, result, warnings, state) do
|
||||
%{output: output, dest: dest, checker: checker} = state
|
||||
%{output: output, dest: dest} = state
|
||||
parent = self()
|
||||
file = Path.expand(file)
|
||||
|
||||
{pid, ref} =
|
||||
:erlang.spawn_monitor(fn ->
|
||||
Module.ParallelChecker.put(parent, checker)
|
||||
:erlang.put(:elixir_compiler_info, {parent, self()})
|
||||
:erlang.put(:elixir_compiler_pid, parent)
|
||||
:erlang.put(:elixir_compiler_file, file)
|
||||
|
||||
try do
|
||||
@@ -392,7 +360,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
defp spawn_workers(
|
||||
[],
|
||||
1,
|
||||
[{_, pid, ref, _, _, _, _}] = waiting,
|
||||
[{_, pid, ref, _, _, _}] = waiting,
|
||||
[%{pid: pid}] = files,
|
||||
result,
|
||||
warnings,
|
||||
@@ -407,31 +375,24 @@ defmodule Kernel.ParallelCompiler do
|
||||
# There is potentially a deadlock. We will release modules with
|
||||
# the following order:
|
||||
#
|
||||
# 1. Code.ensure_compiled/1 checks without a known definition (deadlock = soft)
|
||||
# 2. Code.ensure_compiled/1 checks with a known definition (deadlock = soft)
|
||||
# 3. Struct/import/require/ensure_compiled! checks without a known definition (deadlock = hard)
|
||||
# 4. Modules without a known definition
|
||||
# 5. Code invocation (deadlock = raise)
|
||||
# 1. Code.ensure_compiled/1 checks (deadlock = soft)
|
||||
# 2. Struct checks (deadlock = hard)
|
||||
# 3. Modules without a known definition
|
||||
# 4. Code invocation (deadlock = raise)
|
||||
#
|
||||
# The reason for step 3 and 4 is to not treat typos as deadlocks and
|
||||
# help developers handle those sooner. However, this can have false
|
||||
# positives in case multiple modules are defined in the same file
|
||||
# and the module we are waiting for is defined later on.
|
||||
#
|
||||
# Finally, note there is no difference between hard and raise, the
|
||||
# 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)
|
||||
|
||||
deadlocked =
|
||||
deadlocked(waiting, :soft, false) ||
|
||||
deadlocked(waiting, :soft, true) || deadlocked(waiting, :hard, false) ||
|
||||
without_definition(waiting, files)
|
||||
without_definition = without_definition(waiting, files) ->
|
||||
spawn_workers(without_definition, spawned, waiting, files, result, warnings, state)
|
||||
|
||||
if deadlocked do
|
||||
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, state)
|
||||
else
|
||||
errors = handle_deadlock(waiting, files)
|
||||
{{:error, errors, warnings}, state}
|
||||
true ->
|
||||
errors = handle_deadlock(waiting, files)
|
||||
{{:error, errors, warnings}, state}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -486,26 +447,18 @@ defmodule Kernel.ParallelCompiler 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.
|
||||
# Note that not all files have been compiled yet, so they may not be in waiting.
|
||||
# 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: pid} <- files,
|
||||
{_, _, ref, ^pid, on, _, _} <- waiting,
|
||||
not defining?(on, waiting),
|
||||
{_, ^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, defining?) do
|
||||
nillify_empty(
|
||||
for {_, _, ref, _, on, _, ^type} <- waiting,
|
||||
defining?(on, waiting) == defining?,
|
||||
do: {ref, :deadlock}
|
||||
)
|
||||
end
|
||||
|
||||
defp defining?(on, waiting) do
|
||||
Enum.any?(waiting, fn {_, _, _, _, _, defining, _} -> on in defining end)
|
||||
defp deadlocked(waiting, type) do
|
||||
nillify_empty(for {_, _, ref, _, _, ^type} <- waiting, do: {ref, :deadlock})
|
||||
end
|
||||
|
||||
defp nillify_empty([]), do: nil
|
||||
@@ -522,41 +475,41 @@ defmodule Kernel.ParallelCompiler do
|
||||
|
||||
{:available, kind, module} ->
|
||||
available =
|
||||
for {^kind, _, ref, _, ^module, _defining, _deadlock} <- waiting,
|
||||
for {^kind, _, ref, ^module, _defining, _deadlock} <- waiting,
|
||||
do: {ref, :found}
|
||||
|
||||
result = Map.put(result, {kind, module}, true)
|
||||
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
|
||||
|
||||
{:module_available, child, ref, file, module, binary, checker_info} ->
|
||||
{:module_available, child, ref, file, module, binary, module_map} ->
|
||||
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, ^module, _defining, _deadlock} <- waiting,
|
||||
do: {ref, :found}
|
||||
|
||||
result = Map.put(result, {:module, module}, {binary, checker_info})
|
||||
result = Map.put(result, {:module, module}, {binary, module_map})
|
||||
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
|
||||
|
||||
# If we are simply requiring files, we do not add to waiting.
|
||||
{:waiting, _kind, child, ref, _file_pid, _on, _defining, _deadlock} when output == :require ->
|
||||
{:waiting, _kind, child, ref, _on, _defining, _deadlock} when output == :require ->
|
||||
send(child, {ref, :not_found})
|
||||
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
|
||||
|
||||
{:waiting, kind, child_pid, ref, file_pid, on, defining, deadlock?} ->
|
||||
{:waiting, kind, child, ref, on, defining, deadlock?} ->
|
||||
# If we already got what we were waiting for, do not put it on waiting.
|
||||
# If we're waiting on ourselves, send :found so that we can crash with
|
||||
# a better error.
|
||||
{files, waiting} =
|
||||
if Map.has_key?(result, {kind, on}) or on in defining do
|
||||
send(child_pid, {ref, :found})
|
||||
send(child, {ref, :found})
|
||||
{files, waiting}
|
||||
else
|
||||
files = update_timing(files, file_pid, :compiling)
|
||||
{files, [{kind, child_pid, ref, file_pid, on, defining, deadlock?} | waiting]}
|
||||
files = update_timing(files, child, :compiling)
|
||||
{files, [{kind, child, ref, on, defining, deadlock?} | waiting]}
|
||||
end
|
||||
|
||||
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
|
||||
@@ -577,10 +530,10 @@ defmodule Kernel.ParallelCompiler do
|
||||
state = %{state | timer_ref: timer_ref}
|
||||
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
|
||||
|
||||
{:warning, file, location, message} ->
|
||||
{:warning, file, line, message} ->
|
||||
file = file && Path.absname(file)
|
||||
message = :unicode.characters_to_binary(message)
|
||||
warning = {file, location, message}
|
||||
warning = {file, line, message}
|
||||
wait_for_messages(queue, spawned, waiting, files, result, [warning | warnings], state)
|
||||
|
||||
{:file_ok, child_pid, ref, file, lexical} ->
|
||||
@@ -650,13 +603,13 @@ defmodule Kernel.ParallelCompiler do
|
||||
data = maybe_warn_long_compilation(data, state)
|
||||
|
||||
if state.profile != :none do
|
||||
compiling = to_padded_ms(data.compiling)
|
||||
waiting = to_padded_ms(data.waiting)
|
||||
relative = Path.relative_to_cwd(data.file)
|
||||
compiling = System.convert_time_unit(data.compiling, :native, :millisecond)
|
||||
waiting = System.convert_time_unit(data.waiting, :native, :millisecond)
|
||||
extra = if waiting > 0, do: " (plus #{waiting}ms waiting)", else: ""
|
||||
|
||||
IO.puts(
|
||||
:stderr,
|
||||
"[profile] #{compiling}ms compiling + #{waiting}ms waiting for #{relative}"
|
||||
"[profile] #{Path.relative_to_cwd(data.file)} compiled in #{compiling}ms" <> extra
|
||||
)
|
||||
end
|
||||
|
||||
@@ -667,13 +620,6 @@ defmodule Kernel.ParallelCompiler do
|
||||
end)
|
||||
end
|
||||
|
||||
defp to_padded_ms(time) do
|
||||
time
|
||||
|> System.convert_time_unit(:native, :millisecond)
|
||||
|> Integer.to_string()
|
||||
|> String.pad_leading(6, " ")
|
||||
end
|
||||
|
||||
defp discard_down(pid) do
|
||||
receive do
|
||||
{:DOWN, _, :process, ^pid, _} -> :ok
|
||||
@@ -702,7 +648,7 @@ defmodule Kernel.ParallelCompiler 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)
|
||||
@@ -749,7 +695,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
line = get_line(file, reason, stack)
|
||||
file = Path.absname(file)
|
||||
message = :unicode.characters_to_binary(Kernel.CLI.format_error(kind, reason, stack))
|
||||
{file, line || 0, message}
|
||||
{file, line, message}
|
||||
end
|
||||
|
||||
defp get_line(_file, %{line: line}, _stack) when is_integer(line) and line > 0 do
|
||||
|
||||
@@ -201,7 +201,7 @@ defmodule Kernel.SpecialForms do
|
||||
iex> <<"foo"::utf32>>
|
||||
<<0, 0, 0, 102, 0, 0, 0, 111, 0, 0, 0, 111>>
|
||||
|
||||
Otherwise we get an `ArgumentError` when constructing the binary:
|
||||
Otherwise we get an `ArgumentError` when construcing the binary:
|
||||
|
||||
rest = "oo"
|
||||
<<102, rest>>
|
||||
@@ -365,8 +365,8 @@ defmodule Kernel.SpecialForms do
|
||||
ERL_COMPILER_OPTIONS=bin_opt_info mix compile
|
||||
|
||||
To learn more about specific optimizations and performance considerations,
|
||||
check out the
|
||||
["Constructing and matching binaries" chapter of the Erlang's Efficiency Guide](https://erlang.org/doc/efficiency_guide/binaryhandling.html).
|
||||
check out
|
||||
[Erlang's Efficiency Guide on handling binaries](http://www.erlang.org/doc/efficiency_guide/binaryhandling.html).
|
||||
"""
|
||||
defmacro unquote(:<<>>)(args), do: error!([args])
|
||||
|
||||
@@ -617,11 +617,9 @@ defmodule Kernel.SpecialForms do
|
||||
import List, only: [flatten: 1]
|
||||
import String, except: [split: 2]
|
||||
|
||||
Importing the same module again will erase the previous imports,
|
||||
except when the `except` option is used, which is always exclusive
|
||||
on a previously declared `import/2`. If there is no previous import,
|
||||
then it applies to all functions and macros in the module. For
|
||||
example:
|
||||
Note that calling `except` is always exclusive on a previously
|
||||
declared `import/2`. If there is no previous import, then it applies
|
||||
to all functions and macros in the module. For example:
|
||||
|
||||
import List, only: [flatten: 1, keyfind: 4]
|
||||
import List, except: [flatten: 1]
|
||||
@@ -1211,12 +1209,6 @@ defmodule Kernel.SpecialForms do
|
||||
reported to where `defadd` was invoked. `location: :keep` affects
|
||||
only definitions inside the quote.
|
||||
|
||||
> **Important:** do not use location: :keep if the function definition
|
||||
> also `unquote`s some of the macro arguments. If you do so, Elixir
|
||||
> will store the file definition of the current location but the
|
||||
> unquoted arguments may contain line information of the macro caller,
|
||||
> leading to erroneous stacktraces.
|
||||
|
||||
## Binding and unquote fragments
|
||||
|
||||
Elixir quote/unquote mechanisms provide a functionality called
|
||||
@@ -1422,7 +1414,7 @@ defmodule Kernel.SpecialForms do
|
||||
The `IO` module provides streams, that are both `Enumerable` and
|
||||
`Collectable`, here is an upcase echo server using comprehensions:
|
||||
|
||||
for line <- IO.stream(), into: IO.stream() do
|
||||
for line <- IO.stream(:stdio, :line), into: IO.stream(:stdio, :line) do
|
||||
String.upcase(line)
|
||||
end
|
||||
|
||||
@@ -1525,15 +1517,15 @@ defmodule Kernel.SpecialForms do
|
||||
iex> width
|
||||
nil
|
||||
|
||||
The behaviour of any expression in a clause is the same as if it was
|
||||
written outside of `with`. For example, `=` will raise a `MatchError`
|
||||
instead of returning the non-matched value:
|
||||
The behaviour of any expression in a clause is the same as outside.
|
||||
For example, `=` will raise a `MatchError` instead of returning the
|
||||
non-matched value:
|
||||
|
||||
with :foo = :bar, do: :ok
|
||||
** (MatchError) no match of right hand side value: :bar
|
||||
|
||||
As with any other function or macro call in Elixir, explicit parens can
|
||||
also be used around the arguments before the `do`-`end` block:
|
||||
also be used around the arguments before the `do`/`end` block:
|
||||
|
||||
iex> opts = %{width: 10, height: 15}
|
||||
iex> with(
|
||||
@@ -1546,8 +1538,6 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
The choice between parens and no parens is a matter of preference.
|
||||
|
||||
## Else clauses
|
||||
|
||||
An `else` option can be given to modify what is being returned from
|
||||
`with` in the case of a failed match:
|
||||
|
||||
@@ -1558,60 +1548,11 @@ defmodule Kernel.SpecialForms do
|
||||
...> else
|
||||
...> :error ->
|
||||
...> {:error, :wrong_data}
|
||||
...>
|
||||
...> _other_error ->
|
||||
...> :unexpected_error
|
||||
...> end
|
||||
{:error, :wrong_data}
|
||||
|
||||
The `else` block works like a `case` clause: it can have multiple clauses,
|
||||
and the first match will be used. Variables bound inside `with` (such as
|
||||
`width` in this example) are not available in the `else` block.
|
||||
|
||||
If an `else` block is used and there are no matching clauses, a `WithClauseError`
|
||||
exception is raised.
|
||||
|
||||
### Beware!
|
||||
|
||||
Keep in mind that, one of potential drawback of `with` is that all
|
||||
failure clauses are flattened into a single `else` block. For example,
|
||||
take this code that checks if a given path points to an Elixir file
|
||||
and that it exists before creating a backup copy:
|
||||
|
||||
with ".ex" <- Path.extname(path),
|
||||
true <- File.exists?(path) do
|
||||
backup_path = path <> ".backup"
|
||||
File.cp!(path, backup_path)
|
||||
{:ok, backup_path}
|
||||
else
|
||||
binary when is_binary(binary) ->
|
||||
{:error, :invalid_extension}
|
||||
|
||||
false ->
|
||||
{:error, :missing_file}
|
||||
end
|
||||
|
||||
Note how we are having to reconstruct the result types of `Path.extname/1`
|
||||
and `File.exists?/1` to build error messages. In this case, it is better
|
||||
to change the with clauses to already return the desired format, like this:
|
||||
|
||||
with :ok <- validate_extension(path),
|
||||
:ok <- validate_exists(path) do
|
||||
backup_path = path <> ".backup"
|
||||
File.cp!(path, backup_path)
|
||||
{:ok, backup_path}
|
||||
end
|
||||
|
||||
defp validate_extname(path) do
|
||||
if Path.extname(path) == ".ex", do: :ok, else: {:error, :invalid_extension}
|
||||
end
|
||||
|
||||
defp validate_exists(path) do
|
||||
if File.exists?(path), do: :ok, else: {:error, :missing_file}
|
||||
end
|
||||
|
||||
Note how the code above is better organized and clearer once we
|
||||
make sure each clause in `with` returns a normalize format.
|
||||
"""
|
||||
defmacro with(args), do: error!([args])
|
||||
|
||||
@@ -1657,7 +1598,7 @@ defmodule Kernel.SpecialForms do
|
||||
defmacro unquote(:__block__)(args), do: error!([args])
|
||||
|
||||
@doc """
|
||||
Capture operator. Captures or creates an anonymous function.
|
||||
Caputure operator. Captures or creates an anonymous function.
|
||||
|
||||
## Capture
|
||||
|
||||
@@ -1780,21 +1721,19 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
## Examples
|
||||
|
||||
case File.read(file) do
|
||||
{:ok, contents} when is_binary(contents) ->
|
||||
String.split(contents, "\n")
|
||||
|
||||
{:error, _reason} ->
|
||||
Logger.warning "could not find #{file}, assuming empty..."
|
||||
[]
|
||||
case thing do
|
||||
{:selector, i, value} when is_integer(i) ->
|
||||
value
|
||||
value ->
|
||||
value
|
||||
end
|
||||
|
||||
In the example above, we match the result of `File.read/1`
|
||||
against each clause "head" and execute the clause "body"
|
||||
corresponding to the first clause that matches.
|
||||
In the example above, we match `thing` against each clause "head"
|
||||
and execute the clause "body" corresponding to the first clause
|
||||
that matches.
|
||||
|
||||
If no clause matches, an error is raised. For this reason,
|
||||
it may be necessary to add a final catch-all clause (like `_`)
|
||||
If no clause matches, an error is raised.
|
||||
For this reason, it may be necessary to add a final catch-all clause (like `_`)
|
||||
which will always match.
|
||||
|
||||
x = 10
|
||||
@@ -1802,7 +1741,6 @@ defmodule Kernel.SpecialForms do
|
||||
case x do
|
||||
0 ->
|
||||
"This clause won't match"
|
||||
|
||||
_ ->
|
||||
"This clause would match any value (x = #{x})"
|
||||
end
|
||||
@@ -1820,7 +1758,8 @@ defmodule Kernel.SpecialForms do
|
||||
value
|
||||
#=> unbound variable value
|
||||
|
||||
Variables in the outer context cannot be overridden either:
|
||||
When binding variables with the same names as variables in the outer context,
|
||||
the variables in the outer context are not affected.
|
||||
|
||||
value = 7
|
||||
|
||||
@@ -1847,19 +1786,6 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
#=> "Will match"
|
||||
|
||||
## Using guards to match against multiple values
|
||||
|
||||
While it is not possible to match against multiple patterns in a single
|
||||
clause, it's possible to match against multiple values by using guards:
|
||||
|
||||
case data do
|
||||
value when value in [:one, :two] ->
|
||||
"#{value} has been matched"
|
||||
|
||||
:three ->
|
||||
"three has been matched"
|
||||
end
|
||||
|
||||
"""
|
||||
defmacro case(condition, clauses), do: error!([condition, clauses])
|
||||
|
||||
|
||||
@@ -553,7 +553,9 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec({:%, _, [name, {:%{}, meta, fields}]}, vars, caller, state) do
|
||||
module = Macro.expand(name, %{caller | function: {:__info__, 1}})
|
||||
# We cannot set a function name to avoid tracking
|
||||
# as a compile time dependency, because for structs it actually is one.
|
||||
module = Macro.expand(name, caller)
|
||||
|
||||
struct =
|
||||
module
|
||||
@@ -575,7 +577,7 @@ defmodule Kernel.Typespec do
|
||||
unless Keyword.has_key?(struct, field) do
|
||||
compile_error(
|
||||
caller,
|
||||
"undefined field #{inspect(field)} on struct #{inspect(module)}"
|
||||
"undefined field #{inspect(field)} on struct #{Macro.to_string(name)}"
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -641,7 +643,10 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec({:__aliases__, _, _} = alias, vars, caller, state) do
|
||||
typespec(expand_remote(alias, caller), vars, caller, state)
|
||||
# We set a function name to avoid tracking
|
||||
# aliases in typespecs as compile time dependencies.
|
||||
atom = Macro.expand(alias, %{caller | function: {:typespec, 0}})
|
||||
typespec(atom, vars, caller, state)
|
||||
end
|
||||
|
||||
# Handle funs
|
||||
@@ -729,7 +734,9 @@ defmodule Kernel.Typespec do
|
||||
|
||||
# Handle remote calls
|
||||
defp typespec({{:., meta, [remote, name]}, _, args} = orig, vars, caller, state) do
|
||||
remote = expand_remote(remote, caller)
|
||||
# We set a function name to avoid tracking
|
||||
# aliases in typespecs as compile time dependencies.
|
||||
remote = Macro.expand(remote, %{caller | function: {:typespec, 0}})
|
||||
|
||||
cond do
|
||||
not is_atom(remote) ->
|
||||
@@ -900,25 +907,6 @@ defmodule Kernel.Typespec do
|
||||
|
||||
## Helpers
|
||||
|
||||
# This is a backport of Macro.expand/2 because we want to expand
|
||||
# aliases but we don't them to become compile-time references.
|
||||
defp expand_remote({:__aliases__, _, _} = alias, env) do
|
||||
case :elixir_aliases.expand(alias, env) do
|
||||
receiver when is_atom(receiver) ->
|
||||
receiver
|
||||
|
||||
aliases ->
|
||||
aliases = :lists.map(&Macro.expand_once(&1, env), aliases)
|
||||
|
||||
case :lists.all(&is_atom/1, aliases) do
|
||||
true -> :elixir_aliases.concat(aliases)
|
||||
false -> alias
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_remote(other, env), do: Macro.expand(other, env)
|
||||
|
||||
defp compile_error(caller, desc) do
|
||||
raise CompileError, file: caller.file, line: caller.line, description: desc
|
||||
end
|
||||
@@ -1020,11 +1008,7 @@ defmodule Kernel.Typespec do
|
||||
:elixir_errors.erl_warn(caller.line, caller.file, warning)
|
||||
|
||||
{_, :used_once} ->
|
||||
compile_error(
|
||||
caller,
|
||||
"type variable #{name} is used only once. Type variables in typespecs " <>
|
||||
"must be referenced at least twice, otherwise it is equivalent to term()"
|
||||
)
|
||||
compile_error(caller, "type variable #{name} is unused")
|
||||
|
||||
_ ->
|
||||
:ok
|
||||
|
||||
@@ -29,16 +29,9 @@ defmodule Kernel.Utils do
|
||||
append_first? = Keyword.get(opts, :append_first, false)
|
||||
|
||||
{name, args} =
|
||||
case fun do
|
||||
{:when, _, [_left, right]} ->
|
||||
raise ArgumentError,
|
||||
"guards are not allowed in defdelegate/2, got: when #{Macro.to_string(right)}"
|
||||
|
||||
_ ->
|
||||
case Macro.decompose_call(fun) do
|
||||
{_, _} = pair -> pair
|
||||
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
|
||||
end
|
||||
case Macro.decompose_call(fun) do
|
||||
{_, _} = pair -> pair
|
||||
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
|
||||
end
|
||||
|
||||
as = Keyword.get(opts, :as, name)
|
||||
@@ -115,16 +108,7 @@ defmodule Kernel.Utils do
|
||||
:lists.foreach(foreach, enforce_keys)
|
||||
|
||||
struct = :maps.put(:__struct__, module, :maps.from_list(fields))
|
||||
|
||||
case enforce_keys -- :maps.keys(struct) do
|
||||
[] ->
|
||||
{struct, enforce_keys, Module.get_attribute(module, :derive)}
|
||||
|
||||
error_keys ->
|
||||
raise ArgumentError,
|
||||
"@enforce_keys required keys (#{inspect(error_keys)}) that are not defined in defstruct: " <>
|
||||
"#{inspect(fields)}"
|
||||
end
|
||||
{struct, enforce_keys, Module.get_attribute(module, :derive)}
|
||||
end
|
||||
|
||||
defp warn_on_duplicate_struct_key([]) do
|
||||
@@ -144,9 +128,9 @@ defmodule Kernel.Utils do
|
||||
Announcing callback for defstruct.
|
||||
"""
|
||||
def announce_struct(module) do
|
||||
case :erlang.get(:elixir_compiler_info) do
|
||||
case :erlang.get(:elixir_compiler_pid) do
|
||||
:undefined -> :ok
|
||||
{pid, _} -> send(pid, {:available, :struct, module})
|
||||
pid -> send(pid, {:available, :struct, module})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -226,7 +210,7 @@ defmodule Kernel.Utils do
|
||||
def defguard(args, expr, env) do
|
||||
{^args, vars} = extract_refs_from_args(args)
|
||||
env = :elixir_env.with_vars(%{env | context: :guard}, vars)
|
||||
{expr, _, _} = :elixir_expand.expand(expr, :elixir_env.env_to_ex(env), env)
|
||||
{expr, _scope} = :elixir_expand.expand(expr, env)
|
||||
|
||||
quote do
|
||||
case Macro.Env.in_guard?(__CALLER__) do
|
||||
@@ -274,7 +258,7 @@ defmodule Kernel.Utils do
|
||||
{new_var, acc}
|
||||
|
||||
%{} ->
|
||||
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc) + 1))
|
||||
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc)))
|
||||
new_var = Macro.var(generated, Elixir)
|
||||
{new_var, Map.put(acc, pair, {new_var, var})}
|
||||
end
|
||||
|
||||
+139
-340
@@ -13,43 +13,45 @@ defmodule Keyword do
|
||||
|
||||
[{:exit_on_close, true}, {:active, :once}, {:packet_size, 1024}]
|
||||
|
||||
Elixir provides a special and more concise syntax for keyword lists:
|
||||
Elixir provides a special and more concise syntax for keyword lists
|
||||
that looks like this:
|
||||
|
||||
[exit_on_close: true, active: :once, packet_size: 1024]
|
||||
|
||||
The two syntaxes return the exact same value.
|
||||
|
||||
A *key* can be any atom, consisting of Unicode letters, numbers,
|
||||
an underscore or the `@` sign. If the *key* should have any other
|
||||
characters, such as spaces, you can wrap it in quotes:
|
||||
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:
|
||||
|
||||
iex> ["exit on close": true]
|
||||
["exit on close": true]
|
||||
|
||||
Wrapping an atom in quotes does not make it a string. Keyword list
|
||||
*keys* are always atoms. Quotes should only be used when necessary
|
||||
or Elixir will issue a warning.
|
||||
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.
|
||||
|
||||
## Duplicate keys and ordering
|
||||
|
||||
A keyword may have duplicate keys so it is not strictly a key-value
|
||||
data type. However most of the functions in this module work on a
|
||||
key-value structure and behave similar 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 duplicate entries exist.
|
||||
Similarly, `Keyword.put/3` and `Keyword.delete/2` ensure all duplicate
|
||||
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 whole list in order to find
|
||||
keys, so these operations are slower than their map counterparts.
|
||||
|
||||
A handful of functions exist to handle duplicate keys, for example,
|
||||
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 the first entry of the existing ones.
|
||||
deletes just one of the existing entries.
|
||||
|
||||
Even though lists preserve the existing order, the functions in
|
||||
Even though lists preserve the user ordering, the functions in
|
||||
`Keyword` do not guarantee any ordering. For example, if you invoke
|
||||
`Keyword.put(opts, new_key, new_value)`, there is no guarantee for
|
||||
where `new_key` will be added to (the front, the end or anywhere else).
|
||||
`Keyword.put(opts, new_key, new_value)`, there is no guarantee to
|
||||
where `new_key` will be added (to the front, to the end, or
|
||||
anywhere else).
|
||||
|
||||
Given ordering is not guaranteed, it is not recommended to pattern
|
||||
match on keyword lists either. For example, a function such as:
|
||||
@@ -65,7 +67,7 @@ defmodule Keyword do
|
||||
my_function([another_key: :bar, some_key: :foo])
|
||||
|
||||
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
|
||||
that, the time it takes to perform an operation grows at the same
|
||||
rate as the length of the list.
|
||||
|
||||
## Call syntax
|
||||
@@ -81,8 +83,8 @@ defmodule Keyword do
|
||||
|
||||
String.split("1-0", "-", trim: true, parts: 2)
|
||||
|
||||
Since tuples, lists and maps are treated similarly to function
|
||||
arguments in Elixir syntax, this property is also available to them:
|
||||
Since tuples, lists, maps, and others are treated the same as function
|
||||
calls in Elixir syntax, this property is also available to them:
|
||||
|
||||
iex> {1, 2, foo: :bar}
|
||||
{1, 2, [{:foo, :bar}]}
|
||||
@@ -103,9 +105,7 @@ defmodule Keyword do
|
||||
@type t(value) :: [{key, value}]
|
||||
|
||||
@doc """
|
||||
Returns `true` if `term` is a keyword list, otherwise `false`.
|
||||
|
||||
When `term` is a list it is traversed to the end.
|
||||
Returns `true` if `term` is a keyword list; otherwise returns `false`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -145,7 +145,7 @@ defmodule Keyword do
|
||||
@doc """
|
||||
Creates a keyword list from an enumerable.
|
||||
|
||||
Removes duplicate entries and the last one prevails.
|
||||
Duplicated entries are removed, the latest one prevails.
|
||||
Unlike `Enum.into(enumerable, [])`, `Keyword.new(enumerable)`
|
||||
guarantees the keys are unique.
|
||||
|
||||
@@ -166,7 +166,7 @@ defmodule Keyword do
|
||||
@doc """
|
||||
Creates a keyword list from an enumerable via the transformation function.
|
||||
|
||||
Removes duplicate entries and the last one prevails.
|
||||
Duplicated entries are removed, the latest one prevails.
|
||||
Unlike `Enum.into(enumerable, [], fun)`,
|
||||
`Keyword.new(enumerable, fun)` guarantees the keys are unique.
|
||||
|
||||
@@ -187,146 +187,12 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the first argument is a `keyword` with the given
|
||||
keys and default values.
|
||||
Gets the value for a specific `key`.
|
||||
|
||||
The second argument must be a list of atoms, specifying
|
||||
a given key, or tuples specifying a key and a default value.
|
||||
If `key` does not exist, return the default value
|
||||
(`nil` if no default value).
|
||||
|
||||
If the keyword list has only the given keys, it returns
|
||||
`{:ok, keyword}` with default values applied. Otherwise it
|
||||
returns `{:error, invalid_keys}` with invalid keys.
|
||||
|
||||
See also: `validate!/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, result} = Keyword.validate([], [one: 1, two: 2])
|
||||
iex> Enum.sort(result)
|
||||
[one: 1, two: 2]
|
||||
|
||||
iex> {:ok, result} = Keyword.validate([two: 3], [one: 1, two: 2])
|
||||
iex> Enum.sort(result)
|
||||
[one: 1, two: 3]
|
||||
|
||||
If atoms are given, they are supported as keys but do not
|
||||
provide a default value:
|
||||
|
||||
iex> {:ok, result} = Keyword.validate([], [:one, two: 2])
|
||||
iex> Enum.sort(result)
|
||||
[two: 2]
|
||||
|
||||
iex> {:ok, result} = Keyword.validate([one: 1], [:one, two: 2])
|
||||
iex> Enum.sort(result)
|
||||
[one: 1, two: 2]
|
||||
|
||||
Passing unknown keys returns an error:
|
||||
|
||||
iex> Keyword.validate([three: 3, four: 4], [one: 1, two: 2])
|
||||
{:error, [:four, :three]}
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec validate(keyword(), values :: [atom() | {atom(), term()}]) ::
|
||||
{:ok, keyword()} | {:error, [atom]}
|
||||
def validate(keyword, values) when is_list(keyword) and is_list(values) do
|
||||
validate(keyword, values, [], [], [])
|
||||
end
|
||||
|
||||
defp validate([{key, _} = pair | keyword], values1, values2, acc, bad_keys) when is_atom(key) do
|
||||
case find_key!(key, values1, values2) do
|
||||
{values1, values2} ->
|
||||
validate(keyword, values1, values2, [pair | acc], bad_keys)
|
||||
|
||||
:error ->
|
||||
case find_key!(key, values2, values1) do
|
||||
{values1, values2} ->
|
||||
validate(keyword, values1, values2, [pair | acc], bad_keys)
|
||||
|
||||
:error ->
|
||||
validate(keyword, values1, values2, acc, [key | bad_keys])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp validate([], values1, values2, acc, []) do
|
||||
{:ok, move_pairs!(values1, move_pairs!(values2, acc))}
|
||||
end
|
||||
|
||||
defp validate([], _values1, _values2, _acc, bad_keys) do
|
||||
{:error, bad_keys}
|
||||
end
|
||||
|
||||
defp validate([pair | _], _values1, _values2, _acc, []) do
|
||||
raise ArgumentError,
|
||||
"expected a keyword list as first argument, got invalid entry: #{inspect(pair)}"
|
||||
end
|
||||
|
||||
defp find_key!(key, [key | rest], acc), do: {rest, acc}
|
||||
defp find_key!(key, [{key, _} | rest], acc), do: {rest, acc}
|
||||
defp find_key!(key, [head | tail], acc), do: find_key!(key, tail, [head | acc])
|
||||
defp find_key!(_key, [], _acc), do: :error
|
||||
|
||||
defp move_pairs!([key | rest], acc) when is_atom(key),
|
||||
do: move_pairs!(rest, acc)
|
||||
|
||||
defp move_pairs!([{key, _} = pair | rest], acc) when is_atom(key),
|
||||
do: move_pairs!(rest, [pair | acc])
|
||||
|
||||
defp move_pairs!([], acc),
|
||||
do: acc
|
||||
|
||||
defp move_pairs!([other | _], _) do
|
||||
raise ArgumentError,
|
||||
"expected the second argument to be a list of atoms or tuples, got: #{inspect(other)}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Similar to `validate/2` but returns the keyword or raises an error.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.validate!([], [one: 1, two: 2]) |> Enum.sort()
|
||||
[one: 1, two: 2]
|
||||
iex> Keyword.validate!([two: 3], [one: 1, two: 2]) |> Enum.sort()
|
||||
[one: 1, two: 3]
|
||||
|
||||
If atoms are given, they are supported as keys but do not
|
||||
provide a default value:
|
||||
|
||||
iex> Keyword.validate!([], [:one, two: 2]) |> Enum.sort()
|
||||
[two: 2]
|
||||
iex> Keyword.validate!([one: 1], [:one, two: 2]) |> Enum.sort()
|
||||
[one: 1, two: 2]
|
||||
|
||||
Passing unknown keys raises an error:
|
||||
|
||||
iex> Keyword.validate!([three: 3], [one: 1, two: 2])
|
||||
** (ArgumentError) unknown keys [:three] in [three: 3], the allowed keys are: [:one, :two]
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec validate!(keyword(), values :: [atom() | {atom(), term()}]) :: keyword()
|
||||
def validate!(keyword, values) do
|
||||
case validate(keyword, values) do
|
||||
{:ok, kw} ->
|
||||
kw
|
||||
|
||||
{:error, invalid_keys} ->
|
||||
keys =
|
||||
for value <- values,
|
||||
do: if(is_atom(value), do: value, else: elem(value, 0))
|
||||
|
||||
raise ArgumentError,
|
||||
"unknown keys #{inspect(invalid_keys)} in #{inspect(keyword)}, the allowed keys are: #{inspect(keys)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value under the given `key`.
|
||||
|
||||
Returns the default value if `key` does not exist
|
||||
(`nil` if no default value is provided).
|
||||
|
||||
If duplicate entries exist, it returns the first one.
|
||||
If duplicated entries exist, the first one is returned.
|
||||
Use `get_values/2` to retrieve all entries.
|
||||
|
||||
## Examples
|
||||
@@ -340,7 +206,7 @@ defmodule Keyword do
|
||||
iex> Keyword.get([a: 1], :b, 3)
|
||||
3
|
||||
|
||||
With duplicate keys:
|
||||
With duplicated keys:
|
||||
|
||||
iex> Keyword.get([a: 1, a: 2], :a, 3)
|
||||
1
|
||||
@@ -357,14 +223,14 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value under the given `key`.
|
||||
Gets the value for a specific `key`.
|
||||
|
||||
If `key` does not exist, lazily evaluates `fun` and returns its result.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to set up and tear down again.
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
If duplicate entries exist, it returns the first one.
|
||||
If duplicated entries exist, the first one is returned.
|
||||
Use `get_values/2` to retrieve all entries.
|
||||
|
||||
## Examples
|
||||
@@ -392,14 +258,14 @@ defmodule Keyword do
|
||||
@doc """
|
||||
Gets the value from `key` and updates it, all in one pass.
|
||||
|
||||
The `fun` argument receives the value of `key` (or `nil` if `key`
|
||||
This `fun` argument receives the value of `key` (or `nil` if `key`
|
||||
is not present) and must return a two-element tuple: the current value
|
||||
(the retrieved value, which can be operated on before being returned)
|
||||
and the new value to be stored under `key`. The `fun` may also
|
||||
return `:pop`, implying the current value shall be removed from the
|
||||
keyword list and returned.
|
||||
|
||||
Returns a tuple that contains the current value returned by
|
||||
The returned value is a tuple with the current value returned by
|
||||
`fun` and a new keyword list with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
@@ -414,11 +280,6 @@ defmodule Keyword do
|
||||
...> end)
|
||||
{nil, [b: "new value!", a: 1]}
|
||||
|
||||
iex> Keyword.get_and_update([a: 2], :a, fn number ->
|
||||
...> {2 * number, 3 * number}
|
||||
...> end)
|
||||
{4, [a: 6]}
|
||||
|
||||
iex> Keyword.get_and_update([a: 1], :a, fn _ -> :pop end)
|
||||
{1, []}
|
||||
|
||||
@@ -426,8 +287,8 @@ defmodule Keyword do
|
||||
{nil, [a: 1]}
|
||||
|
||||
"""
|
||||
@spec get_and_update(t, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, new_keywords :: t}
|
||||
@spec get_and_update(t, key, (value -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, value}
|
||||
when current_value: value
|
||||
def get_and_update(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key),
|
||||
@@ -462,15 +323,15 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value under `key` and updates it. Raises if there is no `key`.
|
||||
Gets the value from `key` and updates it. Raises if there is no `key`.
|
||||
|
||||
The `fun` argument receives the value under `key` and must return a
|
||||
This `fun` argument receives the value of `key` and must return a
|
||||
two-element tuple: the current value (the retrieved value, which can be
|
||||
operated on before being returned) and the new value to be stored under
|
||||
`key`.
|
||||
|
||||
Returns a tuple that contains the current value returned by
|
||||
`fun` and a new keyword list with the updated value under `key`.
|
||||
The returned value is a tuple with the current value returned by `fun` and a new
|
||||
keyword list with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -490,8 +351,8 @@ defmodule Keyword do
|
||||
{1, []}
|
||||
|
||||
"""
|
||||
@spec get_and_update!(t, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, new_keywords :: t}
|
||||
@spec get_and_update!(t, key, (value -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, t}
|
||||
when current_value: value
|
||||
def get_and_update!(keywords, key, fun) do
|
||||
get_and_update!(keywords, key, fun, [])
|
||||
@@ -515,13 +376,13 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
defp get_and_update!([], key, _fun, acc) when is_atom(key) do
|
||||
raise KeyError, key: key, term: acc
|
||||
raise(KeyError, key: key, term: acc)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Fetches the value for a specific `key` and returns it in a tuple.
|
||||
|
||||
If the `key` does not exist, it returns `:error`.
|
||||
If the `key` does not exist, returns `:error`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -542,7 +403,7 @@ defmodule Keyword do
|
||||
@doc """
|
||||
Fetches the value for specific `key`.
|
||||
|
||||
If the `key` does not exist, it raises a `KeyError`.
|
||||
If `key` does not exist, a `KeyError` is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -556,12 +417,12 @@ defmodule Keyword do
|
||||
def fetch!(keywords, key) when is_list(keywords) and is_atom(key) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, value} -> value
|
||||
false -> raise KeyError, key: key, term: keywords
|
||||
false -> raise(KeyError, key: key, term: keywords)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets all values under a specific `key`.
|
||||
Gets all values for a specific `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -585,7 +446,7 @@ defmodule Keyword do
|
||||
@doc """
|
||||
Returns all keys from the keyword list.
|
||||
|
||||
Keeps duplicate keys in the resulting list of keys.
|
||||
Duplicated keys appear duplicated in the final list of keys.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -601,24 +462,26 @@ defmodule Keyword do
|
||||
"""
|
||||
@spec keys(t) :: [key]
|
||||
def keys(keywords) when is_list(keywords) do
|
||||
:lists.map(
|
||||
fn
|
||||
{key, _} when is_atom(key) -> key
|
||||
element -> throw(element)
|
||||
end,
|
||||
keywords
|
||||
)
|
||||
catch
|
||||
element ->
|
||||
raise ArgumentError,
|
||||
"expected a keyword list, but an entry in the list is not a two-element tuple " <>
|
||||
"with an atom as its first element, got: #{inspect(element)}"
|
||||
try do
|
||||
:lists.map(
|
||||
fn
|
||||
{key, _} when is_atom(key) -> key
|
||||
element -> throw(element)
|
||||
end,
|
||||
keywords
|
||||
)
|
||||
catch
|
||||
element ->
|
||||
raise ArgumentError,
|
||||
"expected a keyword list, but an entry in the list is not a two-element tuple with an atom as its first element, " <>
|
||||
"got: #{inspect(element)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all values from the keyword list.
|
||||
|
||||
Keeps values from duplicate keys in the resulting list of values.
|
||||
Values from duplicated keys will be kept in the final list of values.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -655,11 +518,11 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the entries in the keyword list under a specific `key`.
|
||||
Deletes the entries in the keyword list for a specific `key`.
|
||||
|
||||
If the `key` does not exist, it returns the keyword list unchanged.
|
||||
If the `key` does not exist, returns the keyword list unchanged.
|
||||
Use `delete_first/2` to delete just the first entry in case of
|
||||
duplicate keys.
|
||||
duplicated keys.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -685,9 +548,9 @@ defmodule Keyword do
|
||||
defp delete_key([], _key), do: []
|
||||
|
||||
@doc """
|
||||
Deletes the first entry in the keyword list under a specific `key`.
|
||||
Deletes the first entry in the keyword list for a specific `key`.
|
||||
|
||||
If the `key` does not exist, it returns the keyword list unchanged.
|
||||
If the `key` does not exist, returns the keyword list unchanged.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -718,10 +581,10 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under the specified `key`.
|
||||
Puts the given `value` under `key`.
|
||||
|
||||
If a value under `key` already exists, it overrides the value
|
||||
and removes all duplicate entries.
|
||||
If a previous value is already stored, all entries are
|
||||
removed and the value is overridden.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -743,19 +606,19 @@ defmodule Keyword do
|
||||
in keyword list unless `key` is already present.
|
||||
|
||||
This is useful if the value is very expensive to calculate or
|
||||
generally difficult to set up and tear down again.
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> keyword = [a: 1]
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 13
|
||||
...> 3
|
||||
...> end
|
||||
iex> Keyword.put_new_lazy(keyword, :a, fun)
|
||||
[a: 1]
|
||||
iex> Keyword.put_new_lazy(keyword, :b, fun)
|
||||
[b: 13, a: 1]
|
||||
[b: 3, a: 1]
|
||||
|
||||
"""
|
||||
@spec put_new_lazy(t, key, (() -> value)) :: t
|
||||
@@ -768,7 +631,8 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under `key`, unless the entry `key` already exists.
|
||||
Puts the given `value` under `key` unless the entry `key`
|
||||
already exists.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -789,8 +653,8 @@ defmodule Keyword do
|
||||
@doc """
|
||||
Puts a value under `key` only if the `key` already exists in `keywords`.
|
||||
|
||||
In case a key exists multiple times in the keyword list,
|
||||
it removes later occurrences.
|
||||
In the case a value is stored multiple times in the keyword list,
|
||||
later occurrences are removed.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -822,7 +686,7 @@ defmodule Keyword do
|
||||
@doc """
|
||||
Puts a value under `key` only if the `key` already exists in `keywords`.
|
||||
|
||||
If `key` is not present in `keywords`, it raises a `KeyError`.
|
||||
If `key` is not present in `keywords`, a `KeyError` exception is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -850,13 +714,13 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
defp replace!([], key, _value, original) do
|
||||
raise KeyError, key: key, term: original
|
||||
raise(KeyError, key: key, term: original)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two keywords are equal.
|
||||
|
||||
Considers two keywords to be equal if they contain
|
||||
Two keywords are considered to be equal if they contain
|
||||
the same keys and those keys contain the same values.
|
||||
|
||||
## Examples
|
||||
@@ -868,25 +732,19 @@ defmodule Keyword do
|
||||
iex> Keyword.equal?([a: 1, b: 2, a: 3], [b: 2, a: 3, a: 1])
|
||||
true
|
||||
|
||||
Comparison between values is done with `===/3`,
|
||||
which means integers are not equivalent to floats:
|
||||
|
||||
iex> Keyword.equal?([a: 1.0], [a: 1])
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(t, t) :: boolean
|
||||
def equal?(left, right) when is_list(left) and is_list(right) do
|
||||
:lists.sort(left) === :lists.sort(right)
|
||||
:lists.sort(left) == :lists.sort(right)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Merges two keyword lists into one.
|
||||
|
||||
Adds all keys, including duplicate keys, given in `keywords2`
|
||||
to `keywords1`, overriding any existing ones.
|
||||
All keys, including duplicated keys, given in `keywords2` will be added
|
||||
to `keywords1`, overriding any existing one.
|
||||
|
||||
There are no guarantees about the order of the keys in the returned keyword.
|
||||
There are no guarantees about the order of keys in the returned keyword.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -927,13 +785,13 @@ defmodule Keyword do
|
||||
@doc """
|
||||
Merges two keyword lists into one.
|
||||
|
||||
Adds all keys, including duplicate keys, given in `keywords2`
|
||||
to `keywords1`. Invokes the given function to solve conflicts.
|
||||
All keys, including duplicated keys, given in `keywords2` will be added
|
||||
to `keywords1`. The given function will be invoked to solve conflicts.
|
||||
|
||||
If `keywords2` has duplicate keys, it invokes the given function
|
||||
If `keywords2` has duplicate keys, the given function will be invoked
|
||||
for each matching pair in `keywords1`.
|
||||
|
||||
There are no guarantees about the order of the keys in the returned keyword.
|
||||
There are no guarantees about the order of keys in the returned keyword.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1007,11 +865,12 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the value under `key` using the given function.
|
||||
Updates the `key` with the given function.
|
||||
|
||||
Raises `KeyError` if the `key` does not exist.
|
||||
If the `key` does not exist, raises `KeyError`.
|
||||
|
||||
Removes all duplicate keys and only updates the first one.
|
||||
If there are duplicated keys, they are all removed and only the first one
|
||||
is updated.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1034,21 +893,23 @@ defmodule Keyword do
|
||||
[{key, fun.(value)} | delete(keywords, key)]
|
||||
end
|
||||
|
||||
defp update!([{_, _} = pair | keywords], key, fun, original) do
|
||||
[pair | update!(keywords, key, fun, original)]
|
||||
defp update!([{_, _} = e | keywords], key, fun, original) do
|
||||
[e | update!(keywords, key, fun, original)]
|
||||
end
|
||||
|
||||
defp update!([], key, _fun, original) do
|
||||
raise KeyError, key: key, term: original
|
||||
defp update!([], key, _fun, original) when is_atom(key) do
|
||||
raise(KeyError, key: key, term: original)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the value under `key` in `keywords` using the given function.
|
||||
Updates the `key` in `keywords` with the given function.
|
||||
|
||||
If the `key` does not exist, it inserts the given `default` value.
|
||||
Does not pass the `default` value through the update function.
|
||||
|
||||
Removes all duplicate keys and only updates the first one.
|
||||
If there are duplicated keys, they are all removed and only the first one
|
||||
is updated.
|
||||
|
||||
The default value will not be passed through the update function.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1062,33 +923,30 @@ defmodule Keyword do
|
||||
[a: 1, b: 11]
|
||||
|
||||
"""
|
||||
@spec update(t, key, default :: value, (existing_value :: value -> new_value :: value)) :: t
|
||||
@spec update(t, key, default :: value, (existing_value :: value -> updated_value :: value)) :: t
|
||||
def update(keywords, key, default, fun)
|
||||
when is_list(keywords) and is_atom(key) and is_function(fun, 1) do
|
||||
update_guarded(keywords, key, default, fun)
|
||||
end
|
||||
|
||||
defp update_guarded([{key, value} | keywords], key, _default, fun) do
|
||||
def update([{key, value} | keywords], key, _default, fun) do
|
||||
[{key, fun.(value)} | delete(keywords, key)]
|
||||
end
|
||||
|
||||
defp update_guarded([{_, _} = pair | keywords], key, default, fun) do
|
||||
[pair | update_guarded(keywords, key, default, fun)]
|
||||
def update([{_, _} = e | keywords], key, default, fun) do
|
||||
[e | update(keywords, key, default, fun)]
|
||||
end
|
||||
|
||||
defp update_guarded([], key, default, _fun) do
|
||||
def update([], key, default, _fun) when is_atom(key) do
|
||||
[{key, default}]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Takes all entries corresponding to the given `keys` and extracts them into a
|
||||
Takes all entries corresponding to the given keys and extracts them into a
|
||||
separate keyword list.
|
||||
|
||||
Returns a tuple with the new list and the old list with removed keys.
|
||||
|
||||
Ignores keys for which there are no entries in the keyword list.
|
||||
Keys for which there are no entries in the keyword list are ignored.
|
||||
|
||||
Entries with duplicate keys end up in the same keyword list.
|
||||
Entries with duplicated keys end up in the same keyword list.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1113,10 +971,10 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Takes all entries corresponding to the given `keys` and returns them as a new
|
||||
Takes all entries corresponding to the given keys and returns them in a new
|
||||
keyword list.
|
||||
|
||||
Preserves duplicate keys in the new keyword list.
|
||||
Duplicated keys are preserved in the new keyword list.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1132,14 +990,12 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Drops the given `keys` from the keyword list.
|
||||
Drops the given keys from the keyword list.
|
||||
|
||||
Removes duplicate keys from the new keyword list.
|
||||
Duplicated keys are preserved in the new keyword list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.drop([a: 1, a: 2], [:a])
|
||||
[]
|
||||
iex> Keyword.drop([a: 1, b: 2, c: 3], [:b, :d])
|
||||
[a: 1, c: 3]
|
||||
iex> Keyword.drop([a: 1, b: 2, b: 3, c: 3, a: 5], [:b, :d])
|
||||
@@ -1148,7 +1004,7 @@ defmodule Keyword do
|
||||
"""
|
||||
@spec drop(t, [key]) :: t
|
||||
def drop(keywords, keys) when is_list(keywords) and is_list(keys) do
|
||||
:lists.filter(fn {k, _} -> k not in keys end, keywords)
|
||||
:lists.filter(fn {key, _} -> key not in keys end, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1156,10 +1012,11 @@ defmodule Keyword do
|
||||
|
||||
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, it returns `{default, keyword_list}`.
|
||||
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, which will remove only the first entry.
|
||||
instead, that function will remove only the first entry.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1185,7 +1042,7 @@ defmodule Keyword do
|
||||
Returns the first value for `key` and removes all associated entries in the keyword list,
|
||||
raising if `key` is not present.
|
||||
|
||||
This function behaves like `pop/3`, but raises in case the `key` is not present in the
|
||||
This function behaves like `pop/3`, but raises in cases the `key` is not present in the
|
||||
given `keywords`.
|
||||
|
||||
## Examples
|
||||
@@ -1212,10 +1069,11 @@ defmodule Keyword do
|
||||
|
||||
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, it returns `{[], keyword_list}`.
|
||||
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, which will remove only the first entry.
|
||||
instead, that function will remove only the first entry.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1246,9 +1104,10 @@ defmodule Keyword do
|
||||
Lazily returns and removes all values associated with `key` in the keyword list.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to set up and tear down again.
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
Removes all duplicate keys. See `pop_first/3` for removing only the first entry.
|
||||
All duplicated keys are removed. See `pop_first/3` for
|
||||
removing only the first entry.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1267,15 +1126,18 @@ defmodule Keyword do
|
||||
def pop_lazy(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key) and is_function(fun, 0) do
|
||||
case fetch(keywords, key) do
|
||||
{:ok, value} -> {value, delete(keywords, key)}
|
||||
:error -> {fun.(), keywords}
|
||||
{:ok, value} ->
|
||||
{value, delete(keywords, key)}
|
||||
|
||||
:error ->
|
||||
{fun.(), keywords}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns and removes the first value associated with `key` in the keyword list.
|
||||
|
||||
Keeps duplicate keys in the resulting keyword list.
|
||||
Duplicated keys are not removed.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1307,76 +1169,13 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec to_list(t) :: t
|
||||
def to_list(keywords) when is_list(keywords) do
|
||||
keywords
|
||||
def to_list(keyword) when is_list(keyword) do
|
||||
keyword
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Kernel.length/1 instead"
|
||||
def size(keywords) do
|
||||
length(keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a keyword list containing only the entries from `keywords`
|
||||
for which the function `fun` returns a truthy value.
|
||||
|
||||
See also `reject/2` which discards all entries where the function
|
||||
returns a truthy value.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.filter([one: 1, two: 2, three: 3], fn {_key, val} -> rem(val, 2) == 1 end)
|
||||
[one: 1, three: 3]
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec filter(t, ({key, value} -> as_boolean(term))) :: t
|
||||
def filter(keywords, fun) when is_list(keywords) and is_function(fun, 1) do
|
||||
do_filter(keywords, fun)
|
||||
end
|
||||
|
||||
defp do_filter([], _fun), do: []
|
||||
|
||||
defp do_filter([{_, _} = entry | entries], fun) do
|
||||
if fun.(entry) do
|
||||
[entry | do_filter(entries, fun)]
|
||||
else
|
||||
do_filter(entries, fun)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a keyword list excluding the entries from `keywords`
|
||||
for which the function `fun` returns a truthy value.
|
||||
|
||||
See also `filter/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.reject([one: 1, two: 2, three: 3], fn {_key, val} -> rem(val, 2) == 1 end)
|
||||
[two: 2]
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec reject(t, ({key, value} -> as_boolean(term))) :: t
|
||||
def reject(keywords, fun) when is_list(keywords) and is_function(fun, 1) do
|
||||
do_reject(keywords, fun)
|
||||
end
|
||||
|
||||
defp do_reject([], _fun), do: []
|
||||
|
||||
defp do_reject([{_, _} = entry | entries], fun) do
|
||||
if fun.(entry) do
|
||||
do_reject(entries, fun)
|
||||
else
|
||||
[entry | do_reject(entries, fun)]
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Keyword.new/2 instead"
|
||||
def map(keywords, fun) when is_list(keywords) do
|
||||
Enum.map(keywords, fn {k, v} -> {k, fun.({k, v})} end)
|
||||
def size(keyword) do
|
||||
length(keyword)
|
||||
end
|
||||
end
|
||||
|
||||
+25
-78
@@ -15,13 +15,6 @@ defmodule List do
|
||||
iex> [1, true, 2, false, 3, true] -- [true, false]
|
||||
[1, 2, 3, true]
|
||||
|
||||
An element can be prepended to a list using `|`:
|
||||
|
||||
iex> new = 0
|
||||
iex> list = [1, 2, 3]
|
||||
iex> [new | list]
|
||||
[0, 1, 2, 3]
|
||||
|
||||
Lists in Elixir are effectively linked lists, which means
|
||||
they are internally represented in pairs containing the
|
||||
head and the tail of a list:
|
||||
@@ -230,7 +223,7 @@ defmodule List do
|
||||
|
||||
@doc """
|
||||
Folds (reduces) the given list from the left with
|
||||
a function. Requires an accumulator, which can be any value.
|
||||
a function. Requires an accumulator.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -239,9 +232,6 @@ defmodule List do
|
||||
|
||||
iex> List.foldl([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
|
||||
2
|
||||
|
||||
iex> List.foldl([1, 2, 3], {0, 0}, fn x, {a1, a2} -> {a1 + x, a2 - x} end)
|
||||
{6, -6}
|
||||
|
||||
"""
|
||||
@spec foldl([elem], acc, (elem, acc -> acc)) :: acc when elem: var, acc: var
|
||||
@@ -251,15 +241,12 @@ defmodule List do
|
||||
|
||||
@doc """
|
||||
Folds (reduces) the given list from the right with
|
||||
a function. Requires an accumulator, which can be any value.
|
||||
a function. Requires an accumulator.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.foldr([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
|
||||
-2
|
||||
|
||||
iex> List.foldr([1, 2, 3, 4], %{sum: 0, product: 1}, fn x, %{sum: a1, product: a2} -> %{sum: a1 + x, product: a2 * x} end)
|
||||
%{product: 24, sum: 10}
|
||||
|
||||
"""
|
||||
@spec foldr([elem], acc, (elem, acc -> acc)) :: acc when elem: var, acc: var
|
||||
@@ -268,18 +255,13 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the first element in `list` or `default` if `list` is empty.
|
||||
|
||||
`first/2` has been introduced in Elixir v1.12.0, while `first/1` has been available since v1.0.0.
|
||||
Returns the first element in `list` or `nil` if `list` is empty.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.first([])
|
||||
nil
|
||||
|
||||
iex> List.first([], 1)
|
||||
1
|
||||
|
||||
iex> List.first([1])
|
||||
1
|
||||
|
||||
@@ -287,25 +269,19 @@ defmodule List do
|
||||
1
|
||||
|
||||
"""
|
||||
@spec first([], any) :: any
|
||||
@spec first([elem, ...], any) :: elem when elem: var
|
||||
def first(list, default \\ nil)
|
||||
def first([], default), do: default
|
||||
def first([head | _], _default), do: head
|
||||
@spec first([]) :: nil
|
||||
@spec first([elem, ...]) :: elem when elem: var
|
||||
def first([]), do: nil
|
||||
def first([head | _]), do: head
|
||||
|
||||
@doc """
|
||||
Returns the last element in `list` or `default` if `list` is empty.
|
||||
|
||||
`last/2` has been introduced in Elixir v1.12.0, while `last/1` has been available since v1.0.0.
|
||||
Returns the last element in `list` or `nil` if `list` is empty.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.last([])
|
||||
nil
|
||||
|
||||
iex> List.last([], 1)
|
||||
1
|
||||
|
||||
iex> List.last([1])
|
||||
1
|
||||
|
||||
@@ -313,13 +289,11 @@ defmodule List do
|
||||
3
|
||||
|
||||
"""
|
||||
@spec last([], any) :: any
|
||||
@spec last([elem, ...], any) :: elem when elem: var
|
||||
@compile {:inline, last: 2}
|
||||
def last(list, default \\ nil)
|
||||
def last([], default), do: default
|
||||
def last([head], _default), do: head
|
||||
def last([_ | tail], default), do: last(tail, default)
|
||||
@spec last([]) :: nil
|
||||
@spec last([elem, ...]) :: 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
|
||||
@@ -341,40 +315,10 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keyfind([tuple], any, non_neg_integer, any) :: any
|
||||
def keyfind(list, key, position, default \\ nil) when is_integer(position) do
|
||||
def keyfind(list, key, position, default \\ nil) do
|
||||
:lists.keyfind(key, position + 1, list) || default
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and returns the first tuple
|
||||
where the element at `position` in the tuple matches the
|
||||
given `key`.
|
||||
|
||||
If no matching tuple is found, an error is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.keyfind!([a: 1, b: 2], :a, 0)
|
||||
{:a, 1}
|
||||
|
||||
iex> List.keyfind!([a: 1, b: 2], 2, 1)
|
||||
{:b, 2}
|
||||
|
||||
iex> List.keyfind!([a: 1, b: 2], :c, 0)
|
||||
** (KeyError) key :c at position 0 not found in: [a: 1, b: 2]
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec keyfind!([tuple], any, non_neg_integer) :: any
|
||||
def keyfind!(list, key, position) when is_integer(position) do
|
||||
:lists.keyfind(key, position + 1, list) ||
|
||||
raise KeyError,
|
||||
key: key,
|
||||
term: list,
|
||||
message:
|
||||
"key #{inspect(key)} at position #{inspect(position)} not found in: #{inspect(list)}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and returns `true` if there is
|
||||
a tuple where the element at `position` in the tuple matches
|
||||
@@ -393,7 +337,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keymember?([tuple], any, non_neg_integer) :: boolean
|
||||
def keymember?(list, key, position) when is_integer(position) do
|
||||
def keymember?(list, key, position) do
|
||||
:lists.keymember(key, position + 1, list)
|
||||
end
|
||||
|
||||
@@ -411,7 +355,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keyreplace([tuple], any, non_neg_integer, tuple) :: [tuple]
|
||||
def keyreplace(list, key, position, new_tuple) when is_integer(position) do
|
||||
def keyreplace(list, key, position, new_tuple) do
|
||||
:lists.keyreplace(key, position + 1, list, new_tuple)
|
||||
end
|
||||
|
||||
@@ -429,7 +373,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keysort([tuple], non_neg_integer) :: [tuple]
|
||||
def keysort(list, position) when is_integer(position) do
|
||||
def keysort(list, position) do
|
||||
:lists.keysort(position + 1, list)
|
||||
end
|
||||
|
||||
@@ -449,7 +393,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keystore([tuple], any, non_neg_integer, tuple) :: [tuple, ...]
|
||||
def keystore(list, key, position, new_tuple) when is_integer(position) do
|
||||
def keystore(list, key, position, new_tuple) do
|
||||
:lists.keystore(key, position + 1, list, new_tuple)
|
||||
end
|
||||
|
||||
@@ -471,7 +415,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keydelete([tuple], any, non_neg_integer) :: [tuple]
|
||||
def keydelete(list, key, position) when is_integer(position) do
|
||||
def keydelete(list, key, position) do
|
||||
:lists.keydelete(key, position + 1, list)
|
||||
end
|
||||
|
||||
@@ -495,7 +439,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec keytake([tuple], any, non_neg_integer) :: {tuple, [tuple]} | nil
|
||||
def keytake(list, key, position) when is_integer(position) do
|
||||
def keytake(list, key, position) do
|
||||
case :lists.keytake(key, position + 1, list) do
|
||||
{:value, element, list} -> {element, list}
|
||||
false -> nil
|
||||
@@ -867,6 +811,9 @@ defmodule List do
|
||||
iex> List.to_existing_atom('🌢 Elixir')
|
||||
:"🌢 Elixir"
|
||||
|
||||
iex> List.to_existing_atom('this_atom_will_never_exist')
|
||||
** (ArgumentError) argument error
|
||||
|
||||
"""
|
||||
@spec to_existing_atom(charlist) :: atom
|
||||
def to_existing_atom(charlist) do
|
||||
@@ -952,7 +899,7 @@ defmodule List do
|
||||
|
||||
Note that this function expects a list of integers representing
|
||||
Unicode code points. If you have a list of bytes, you must instead use
|
||||
the [`:binary` module](`:binary`).
|
||||
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1007,7 +954,7 @@ defmodule List do
|
||||
|
||||
Note that this function expects a list of integers representing
|
||||
Unicode code points. If you have a list of bytes, you must instead use
|
||||
the [`:binary` module](`:binary`).
|
||||
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
|
||||
|
||||
## Examples
|
||||
|
||||
|
||||
+56
-243
@@ -53,7 +53,7 @@ defmodule Macro do
|
||||
## Custom Sigils
|
||||
|
||||
Macros are also commonly used to implement custom sigils. To create a custom
|
||||
sigil, define a macro with the name `sigil_{identifier}` that takes two
|
||||
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
|
||||
@@ -167,9 +167,9 @@ defmodule Macro do
|
||||
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)
|
||||
`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)
|
||||
`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)
|
||||
@@ -270,7 +270,7 @@ defmodule Macro do
|
||||
def pipe(expr, {:fn, _, _}, _integer) do
|
||||
raise ArgumentError,
|
||||
"cannot pipe #{to_string(expr)} into an anonymous function without" <>
|
||||
" calling the function; use Kernel.then/2 instead or" <>
|
||||
" calling the function; use something like (fn ... end).() or" <>
|
||||
" define the anonymous function as a regular private function"
|
||||
end
|
||||
|
||||
@@ -341,7 +341,7 @@ defmodule Macro do
|
||||
variables using `Macro.var/2`.
|
||||
|
||||
Note the arguments are not unique. If you later on want
|
||||
to access the same variables, you can invoke this function
|
||||
to access this same varibles, you can invoke this function
|
||||
with the same inputs. Use `generate_unique_arguments/2` to
|
||||
generate a unique arguments that can't be overridden.
|
||||
|
||||
@@ -391,8 +391,8 @@ defmodule Macro do
|
||||
by the atoms `var` and `context`.
|
||||
|
||||
Note this variable is not unique. If you later on want
|
||||
to access this same variable, you can invoke `var/2`
|
||||
again with the same arguments. Use `unique_var/2` to
|
||||
to access this same varible, you can invoke `var/2`
|
||||
again with the same argument. Use `unique_var/2` to
|
||||
generate a unique variable that can't be overridden.
|
||||
|
||||
## Examples
|
||||
@@ -478,35 +478,14 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
defp do_traverse_args(args, acc, pre, post) when is_list(args) do
|
||||
:lists.mapfoldl(
|
||||
fn x, acc ->
|
||||
{x, acc} = pre.(x, acc)
|
||||
do_traverse(x, acc, pre, post)
|
||||
end,
|
||||
acc,
|
||||
args
|
||||
)
|
||||
Enum.map_reduce(args, acc, fn x, acc ->
|
||||
{x, acc} = pre.(x, acc)
|
||||
do_traverse(x, acc, pre, post)
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a depth-first, pre-order traversal of quoted expressions.
|
||||
|
||||
Returns a new ast where each node is the result of invoking `fun` on each
|
||||
corresponding node of `ast`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> ast = quote do: 5 + 3 * 7
|
||||
iex> new_ast = Macro.prewalk(ast, fn
|
||||
...> {:+, meta, children} -> {:*, meta, children}
|
||||
...> {:*, meta, children} -> {:+, meta, children}
|
||||
...> other -> other
|
||||
...> end)
|
||||
iex> Code.eval_quoted(ast)
|
||||
{26, []}
|
||||
iex> Code.eval_quoted(new_ast)
|
||||
{50, []}
|
||||
|
||||
"""
|
||||
@spec prewalk(t, (t -> t)) :: t
|
||||
def prewalk(ast, fun) when is_function(fun, 1) do
|
||||
@@ -562,15 +541,10 @@ defmodule Macro do
|
||||
iex> Macro.decompose_call(quote(do: 42))
|
||||
:error
|
||||
|
||||
iex> Macro.decompose_call(quote(do: {:foo, [], []}))
|
||||
:error
|
||||
|
||||
"""
|
||||
@spec decompose_call(t()) :: {atom, [t()]} | {t(), atom, [t()]} | :error
|
||||
def decompose_call(ast)
|
||||
|
||||
def decompose_call({:{}, _, args}) when is_list(args), do: :error
|
||||
|
||||
def decompose_call({{:., _, [remote, function]}, _, args})
|
||||
when is_tuple(remote) or is_atom(remote),
|
||||
do: {remote, function, args}
|
||||
@@ -604,7 +578,8 @@ defmodule Macro do
|
||||
|
||||
* `:prune_metadata` - when true, removes metadata from escaped AST
|
||||
nodes. Note this option changes the semantics of escaped code and
|
||||
it should only be used when escaping ASTs. Defaults to false.
|
||||
it should only be used when escaping ASTs, never values. Defaults
|
||||
to false.
|
||||
|
||||
As an example, `ExUnit` stores the AST of every assertion, so when
|
||||
an assertion fails we can show code snippets to users. Without this
|
||||
@@ -612,8 +587,7 @@ defmodule Macro do
|
||||
MD5 of the module bytecode, because the AST contains metadata,
|
||||
such as counters, specific to the compilation environment. By pruning
|
||||
the metadata, we ensure that the module is deterministic and reduce
|
||||
the amount of data `ExUnit` needs to keep around. Only the minimal
|
||||
amount of metadata is kept, such as `:line` and `:no_parens`.
|
||||
the amount of data `ExUnit` needs to keep around.
|
||||
|
||||
## Comparison to `Kernel.SpecialForms.quote/2`
|
||||
|
||||
@@ -644,7 +618,7 @@ defmodule Macro do
|
||||
@spec escape(term, keyword) :: t()
|
||||
def escape(expr, opts \\ []) do
|
||||
unquote = Keyword.get(opts, :unquote, false)
|
||||
kind = if Keyword.get(opts, :prune_metadata, false), do: :prune_metadata, else: :none
|
||||
kind = if Keyword.get(opts, :prune_metadata, false), do: :prune_metadata, else: :default
|
||||
:elixir_quote.escape(expr, kind, unquote)
|
||||
end
|
||||
|
||||
@@ -657,17 +631,13 @@ defmodule Macro do
|
||||
expanding structs defined under the module being compiled.
|
||||
|
||||
It will raise `CompileError` if the struct is not available.
|
||||
From Elixir v1.12, calling this function also adds an export
|
||||
dependency on the given struct.
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec struct!(module, Macro.Env.t()) ::
|
||||
%{required(:__struct__) => module, optional(atom) => any}
|
||||
when module: module()
|
||||
@spec struct!(module, Macro.Env.t()) :: %{__struct__: module} when module: module()
|
||||
def struct!(module, env) when is_atom(module) do
|
||||
if module == env.module do
|
||||
Module.get_attribute(module, :__struct__)
|
||||
end || :elixir_map.load_struct([line: env.line], module, [], [], env)
|
||||
Module.get_attribute(module, :struct)
|
||||
end || :elixir_map.load_struct([line: env.line], module, [], env)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -719,125 +689,6 @@ defmodule Macro do
|
||||
|
||||
defp find_invalid(other), do: {:error, other}
|
||||
|
||||
@doc """
|
||||
Returns an enumerable that traverses the `ast` in depth-first,
|
||||
pre-order traversal.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> ast = quote do: foo(1, "abc")
|
||||
iex> Enum.map(Macro.prewalker(ast), & &1)
|
||||
[{:foo, [], [1, "abc"]}, 1, "abc"]
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
def prewalker(ast) do
|
||||
&prewalker([ast], &1, &2)
|
||||
end
|
||||
|
||||
defp prewalker(_buffer, {:halt, acc}, _fun) do
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp prewalker(buffer, {:suspend, acc}, fun) do
|
||||
{:suspended, acc, &prewalker(buffer, &1, fun)}
|
||||
end
|
||||
|
||||
defp prewalker([], {:cont, acc}, _fun) do
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
defp prewalker([{left, right} = node | tail], {:cont, acc}, fun) do
|
||||
prewalker([left, right | tail], fun.(node, acc), fun)
|
||||
end
|
||||
|
||||
defp prewalker([{left, meta, right} = node | tail], {:cont, acc}, fun)
|
||||
when is_atom(left) and is_list(meta) do
|
||||
if is_atom(right) do
|
||||
prewalker(tail, fun.(node, acc), fun)
|
||||
else
|
||||
prewalker(right ++ tail, fun.(node, acc), fun)
|
||||
end
|
||||
end
|
||||
|
||||
defp prewalker([{left, meta, right} = node | tail], {:cont, acc}, fun) when is_list(meta) do
|
||||
if is_atom(right) do
|
||||
prewalker([left | tail], fun.(node, acc), fun)
|
||||
else
|
||||
prewalker([left | right] ++ tail, fun.(node, acc), fun)
|
||||
end
|
||||
end
|
||||
|
||||
defp prewalker([list | tail], {:cont, acc}, fun) when is_list(list) do
|
||||
prewalker(list ++ tail, fun.(list, acc), fun)
|
||||
end
|
||||
|
||||
defp prewalker([head | tail], {:cont, acc}, fun) do
|
||||
prewalker(tail, fun.(head, acc), fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an enumerable that traverses the `ast` in depth-first,
|
||||
post-order traversal.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> ast = quote do: foo(1, "abc")
|
||||
iex> Enum.map(Macro.postwalker(ast), & &1)
|
||||
[1, "abc", {:foo, [], [1, "abc"]}]
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
def postwalker(ast) do
|
||||
&postwalker([ast], make_ref(), &1, &2)
|
||||
end
|
||||
|
||||
defp postwalker(_buffer, _ref, {:halt, acc}, _fun) do
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp postwalker(buffer, ref, {:suspend, acc}, fun) do
|
||||
{:suspended, acc, &postwalker(buffer, ref, &1, fun)}
|
||||
end
|
||||
|
||||
defp postwalker([], _ref, {:cont, acc}, _fun) do
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
defp postwalker([{ref, head} | tail], ref, {:cont, acc}, fun) do
|
||||
postwalker(tail, ref, fun.(head, acc), fun)
|
||||
end
|
||||
|
||||
defp postwalker([{left, right} = node | tail], ref, {:cont, acc}, fun) do
|
||||
postwalker([right, {ref, node} | tail], ref, fun.(left, acc), fun)
|
||||
end
|
||||
|
||||
defp postwalker([{left, meta, right} = node | tail], ref, {:cont, acc}, fun)
|
||||
when is_atom(left) and is_list(meta) do
|
||||
if is_atom(right) do
|
||||
postwalker(tail, ref, fun.(node, acc), fun)
|
||||
else
|
||||
postwalker(right ++ [{ref, node} | tail], ref, {:cont, acc}, fun)
|
||||
end
|
||||
end
|
||||
|
||||
defp postwalker([{left, meta, right} = node | tail], ref, cont_acc, fun)
|
||||
when is_list(meta) do
|
||||
if is_atom(right) do
|
||||
postwalker([left, {ref, node} | tail], ref, cont_acc, fun)
|
||||
else
|
||||
postwalker([left | right] ++ [{ref, node} | tail], ref, cont_acc, fun)
|
||||
end
|
||||
end
|
||||
|
||||
defp postwalker([list | tail], ref, cont_acc, fun) when is_list(list) do
|
||||
postwalker(list ++ [{ref, list} | tail], ref, cont_acc, fun)
|
||||
end
|
||||
|
||||
defp postwalker([head | tail], ref, {:cont, acc}, fun) do
|
||||
postwalker(tail, ref, fun.(head, acc), fun)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Unescapes the given chars.
|
||||
|
||||
@@ -863,8 +714,8 @@ defmodule Macro do
|
||||
and return a version with it unescaped.
|
||||
"""
|
||||
@spec unescape_string(String.t()) :: String.t()
|
||||
def unescape_string(string) do
|
||||
:elixir_interpolation.unescape_string(string)
|
||||
def unescape_string(chars) do
|
||||
:elixir_interpolation.unescape_chars(chars)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -879,9 +730,8 @@ defmodule Macro do
|
||||
representing the code point of the character it wants to unescape.
|
||||
Here is the default mapping function implemented by Elixir:
|
||||
|
||||
def unescape_map(:newline), do: true
|
||||
def unescape_map(:unicode), do: true
|
||||
def unescape_map(:hex), do: true
|
||||
def unescape_map(unicode), do: true
|
||||
def unescape_map(hex), do: true
|
||||
def unescape_map(?0), do: ?0
|
||||
def unescape_map(?a), do: ?\a
|
||||
def unescape_map(?b), do: ?\b
|
||||
@@ -898,9 +748,9 @@ defmodule Macro do
|
||||
If the `unescape_map/1` function returns `false`, the char is
|
||||
not escaped and the backslash is kept in the string.
|
||||
|
||||
Newlines, Unicode, and hexadecimals code points will be escaped if
|
||||
the map returns `true` respectively for `:newline`, `:unicode`, and
|
||||
`:hex`.
|
||||
Hexadecimals and Unicode code points will be escaped if the map
|
||||
function returns `true` for `?x`. Unicode code points if the map
|
||||
function returns `true` for `?u`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -910,48 +760,31 @@ defmodule Macro do
|
||||
|
||||
"""
|
||||
@spec unescape_string(String.t(), (non_neg_integer -> non_neg_integer | false)) :: String.t()
|
||||
def unescape_string(string, map) do
|
||||
:elixir_interpolation.unescape_string(string, map)
|
||||
def unescape_string(chars, map) do
|
||||
:elixir_interpolation.unescape_chars(chars, map)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Traverse over the arguments using Enum.map/2 instead"
|
||||
def unescape_tokens(tokens) do
|
||||
for token <- tokens do
|
||||
if is_binary(token), do: unescape_string(token), else: token
|
||||
case :elixir_interpolation.unescape_tokens(tokens) do
|
||||
{:ok, unescaped_tokens} -> unescaped_tokens
|
||||
{:error, reason} -> raise ArgumentError, to_string(reason)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Traverse over the arguments using Enum.map/2 instead"
|
||||
def unescape_tokens(tokens, map) do
|
||||
for token <- tokens do
|
||||
if is_binary(token), do: unescape_string(token, map), else: token
|
||||
case :elixir_interpolation.unescape_tokens(tokens, map) do
|
||||
{:ok, unescaped_tokens} -> unescaped_tokens
|
||||
{:error, reason} -> raise ArgumentError, to_string(reason)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given expression AST to a string.
|
||||
|
||||
This function discards all formatting of the original code.
|
||||
See `Code.quoted_to_algebra/2` as a lower level function
|
||||
with more control around formatting.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.to_string(quote(do: foo.bar(1, 2, 3)))
|
||||
"foo.bar(1, 2, 3)"
|
||||
|
||||
"""
|
||||
@spec to_string(t()) :: String.t()
|
||||
def to_string(tree) do
|
||||
doc = Inspect.Algebra.format(Code.quoted_to_algebra(tree), 98)
|
||||
IO.iodata_to_binary(doc)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given expression AST to a string.
|
||||
|
||||
The given `fun` is called for every node in the AST with two arguments: the
|
||||
AST of the node being printed and the string representation of that same
|
||||
node. The return value of this function is used as the final string
|
||||
@@ -961,17 +794,19 @@ defmodule Macro do
|
||||
|
||||
## Examples
|
||||
|
||||
Macro.to_string(quote(do: 1 + 2), fn
|
||||
1, _string -> "one"
|
||||
2, _string -> "two"
|
||||
_ast, string -> string
|
||||
end)
|
||||
#=> "one + two"
|
||||
iex> Macro.to_string(quote(do: foo.bar(1, 2, 3)))
|
||||
"foo.bar(1, 2, 3)"
|
||||
|
||||
iex> Macro.to_string(quote(do: 1 + 2), fn
|
||||
...> 1, _string -> "one"
|
||||
...> 2, _string -> "two"
|
||||
...> _ast, string -> string
|
||||
...> end)
|
||||
"one + two"
|
||||
|
||||
"""
|
||||
@deprecated "Use Macro.to_string/1 instead"
|
||||
@spec to_string(t(), (t(), String.t() -> String.t())) :: String.t()
|
||||
def to_string(tree, fun)
|
||||
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
|
||||
@@ -1124,7 +959,7 @@ defmodule Macro do
|
||||
|
||||
def to_string({target, _, args} = ast, fun) when is_list(args) do
|
||||
with :error <- unary_call(ast, fun),
|
||||
:error <- op_call(ast, fun),
|
||||
:error <- binary_call(ast, fun),
|
||||
:error <- sigil_call(ast, fun) do
|
||||
{list, last} = split_last(args)
|
||||
|
||||
@@ -1242,6 +1077,8 @@ defmodule Macro do
|
||||
"\#{" <> 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)
|
||||
end)
|
||||
|
||||
@@ -1296,14 +1133,7 @@ defmodule Macro do
|
||||
:error
|
||||
end
|
||||
|
||||
defp op_call({:"..//", _, [left, middle, right]} = ast, fun) do
|
||||
left = op_to_string(left, fun, :.., :left)
|
||||
middle = op_to_string(middle, fun, :.., :right)
|
||||
right = op_to_string(right, fun, :"//", :right)
|
||||
{:ok, fun.(ast, left <> ".." <> middle <> "//" <> right)}
|
||||
end
|
||||
|
||||
defp op_call({op, _, [left, right]} = ast, fun) when is_atom(op) do
|
||||
defp binary_call({op, _, [left, right]} = ast, fun) when is_atom(op) do
|
||||
case Identifier.binary_op(op) do
|
||||
{_, _} ->
|
||||
left = op_to_string(left, fun, op, :left)
|
||||
@@ -1316,7 +1146,7 @@ defmodule Macro do
|
||||
end
|
||||
end
|
||||
|
||||
defp op_call(_, _) do
|
||||
defp binary_call(_, _) do
|
||||
:error
|
||||
end
|
||||
|
||||
@@ -1596,7 +1426,7 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
defp do_expand_once({:__aliases__, meta, _} = original, env) do
|
||||
case :elixir_aliases.expand_or_concat(original, env) do
|
||||
case :elixir_aliases.expand(original, env) do
|
||||
receiver when is_atom(receiver) ->
|
||||
:elixir_env.trace({:alias_reference, meta, receiver}, env)
|
||||
{receiver, true}
|
||||
@@ -1655,10 +1485,7 @@ defmodule Macro do
|
||||
[]
|
||||
end
|
||||
|
||||
s = :elixir_env.env_to_ex(env)
|
||||
|
||||
expand =
|
||||
:elixir_dispatch.expand_import(meta, {atom, length(args)}, args, s, env, extra, true)
|
||||
expand = :elixir_dispatch.expand_import(meta, {atom, length(args)}, args, env, extra, true)
|
||||
|
||||
case expand do
|
||||
{:ok, receiver, quoted} ->
|
||||
@@ -1689,9 +1516,7 @@ defmodule Macro do
|
||||
{original, false}
|
||||
|
||||
true ->
|
||||
s = :elixir_env.env_to_ex(env)
|
||||
name_arity = {right, length(args)}
|
||||
expand = :elixir_dispatch.expand_require(meta, receiver, name_arity, args, s, env)
|
||||
expand = :elixir_dispatch.expand_require(meta, receiver, {right, length(args)}, args, env)
|
||||
|
||||
case expand do
|
||||
{:ok, receiver, quoted} ->
|
||||
@@ -1718,22 +1543,9 @@ defmodule Macro do
|
||||
|
||||
@doc """
|
||||
Returns `true` if the given name and arity is an operator.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.operator?(:not_an_operator, 3)
|
||||
false
|
||||
iex> Macro.operator?(:+, 1)
|
||||
true
|
||||
iex> Macro.operator?(:++, 2)
|
||||
true
|
||||
iex> Macro.operator?(:..//, 3)
|
||||
true
|
||||
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec operator?(name :: atom(), arity()) :: boolean()
|
||||
def operator?(:"..//", 3), do: true
|
||||
def operator?(name, 2) when is_atom(name), do: Identifier.binary_op(name) != :error
|
||||
def operator?(name, 1) when is_atom(name), do: Identifier.unary_op(name) != :error
|
||||
def operator?(name, arity) when is_atom(name) and is_integer(arity), do: false
|
||||
@@ -1741,7 +1553,7 @@ defmodule Macro do
|
||||
@doc """
|
||||
Returns `true` if the given quoted expression represents a quoted literal.
|
||||
|
||||
Atoms and numbers are always literals. Binaries, lists, tuples,
|
||||
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
|
||||
@@ -1776,7 +1588,9 @@ defmodule Macro do
|
||||
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)
|
||||
|
||||
def quoted_literal?(term),
|
||||
do: is_atom(term) or is_number(term) or is_binary(term) or is_function(term)
|
||||
|
||||
@doc """
|
||||
Receives an AST node and expands it until it can no longer
|
||||
@@ -1850,8 +1664,7 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
defp do_underscore(<<h, t, rest::binary>>, _)
|
||||
when h >= ?A and h <= ?Z and not (t >= ?A and t <= ?Z) and not (t >= ?0 and t <= ?9) and
|
||||
t != ?. and t != ?_ do
|
||||
when h >= ?A and h <= ?Z and not (t >= ?A and t <= ?Z) and t != ?. and t != ?_ do
|
||||
<<?_, to_lower_char(h), t>> <> do_underscore(rest, t)
|
||||
end
|
||||
|
||||
|
||||
+57
-141
@@ -21,6 +21,8 @@ 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
|
||||
@@ -28,44 +30,66 @@ defmodule Macro.Env do
|
||||
* `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
|
||||
* `requires` - the list of required modules
|
||||
|
||||
The following fields are private to Elixir's macro expansion mechanism and
|
||||
must not be accessed directly:
|
||||
|
||||
* `aliases`
|
||||
* `functions`
|
||||
* `macro_aliases`
|
||||
* `macros`
|
||||
* `contextual_vars`
|
||||
* `current_vars`
|
||||
* `lexical_tracker`
|
||||
* `requires`
|
||||
* `prematch_vars`
|
||||
* `tracers`
|
||||
* `versioned_vars`
|
||||
* `unused_vars`
|
||||
|
||||
The following fields are deprecated and must not be accessed or relied on:
|
||||
|
||||
* `vars` - a list keeping all defined variables as `{var, context}`
|
||||
|
||||
"""
|
||||
|
||||
@type 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 requires :: [module]
|
||||
@type variable :: {atom, atom | term}
|
||||
|
||||
@typep aliases :: [{module, module}]
|
||||
@typep functions :: [{module, [name_arity]}]
|
||||
@typep lexical_tracker :: pid | nil
|
||||
@typep macro_aliases :: [{module, {term, module}}]
|
||||
@typep macros :: [{module, [name_arity]}]
|
||||
@typep requires :: [module]
|
||||
@typep 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 versioned_vars :: %{optional(variable) => var_version :: non_neg_integer}
|
||||
@typep var_type :: :term
|
||||
@typep var_version :: non_neg_integer
|
||||
@typep vars :: [variable]
|
||||
|
||||
@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,
|
||||
@@ -73,20 +97,23 @@ defmodule Macro.Env do
|
||||
line: line,
|
||||
macro_aliases: macro_aliases,
|
||||
macros: macros,
|
||||
module: module,
|
||||
module: atom,
|
||||
prematch_vars: prematch_vars,
|
||||
unused_vars: unused_vars,
|
||||
requires: requires,
|
||||
tracers: tracers,
|
||||
versioned_vars: versioned_vars
|
||||
vars: vars
|
||||
}
|
||||
|
||||
# Define the __struct__ callbacks by hand for bootstrap reasons.
|
||||
@doc false
|
||||
# TODO: Remove :vars field on v2.0
|
||||
def __struct__ do
|
||||
%{
|
||||
__struct__: __MODULE__,
|
||||
aliases: [],
|
||||
context: nil,
|
||||
context_modules: [],
|
||||
contextual_vars: [],
|
||||
current_vars: {%{}, %{}},
|
||||
file: "nofile",
|
||||
function: nil,
|
||||
functions: [],
|
||||
@@ -95,13 +122,14 @@ defmodule Macro.Env do
|
||||
macro_aliases: [],
|
||||
macros: [],
|
||||
module: nil,
|
||||
prematch_vars: :warn,
|
||||
requires: [],
|
||||
tracers: [],
|
||||
versioned_vars: %{}
|
||||
unused_vars: {%{}, 0},
|
||||
vars: []
|
||||
}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __struct__(kv) do
|
||||
Enum.reduce(kv, __struct__(), fn {k, v}, acc -> :maps.update(k, v, acc) end)
|
||||
end
|
||||
@@ -118,30 +146,19 @@ defmodule Macro.Env do
|
||||
@spec vars(t) :: [variable]
|
||||
def vars(env)
|
||||
|
||||
def vars(%{__struct__: Macro.Env, versioned_vars: vars}) do
|
||||
Map.keys(vars)
|
||||
def vars(%{__struct__: Macro.Env, current_vars: {read, _}}) do
|
||||
Map.keys(read)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if a variable belongs to the environment.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> x = 13
|
||||
iex> x
|
||||
13
|
||||
iex> Macro.Env.has_var?(__ENV__, {:x, nil})
|
||||
true
|
||||
iex> Macro.Env.has_var?(__ENV__, {:unknown, nil})
|
||||
false
|
||||
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec has_var?(t, variable) :: boolean()
|
||||
def has_var?(env, var)
|
||||
|
||||
def has_var?(%{__struct__: Macro.Env, versioned_vars: vars}, var) do
|
||||
Map.has_key?(vars, var)
|
||||
def has_var?(%{__struct__: Macro.Env, current_vars: {read, _}}, var) do
|
||||
Map.has_key?(read, var)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -155,117 +172,16 @@ defmodule Macro.Env do
|
||||
[file: file, line: line]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Fetches the alias for the given atom.
|
||||
|
||||
Returns `{:ok, alias}` if the alias exists, `:error`
|
||||
otherwise.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> alias Foo.Bar, as: Baz
|
||||
iex> Baz
|
||||
Foo.Bar
|
||||
iex> Macro.Env.fetch_alias(__ENV__, :Baz)
|
||||
{:ok, Foo.Bar}
|
||||
iex> Macro.Env.fetch_alias(__ENV__, :Unknown)
|
||||
:error
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec fetch_alias(t, atom) :: {:ok, atom} | :error
|
||||
def fetch_alias(%{__struct__: Macro.Env, aliases: aliases}, atom) when is_atom(atom),
|
||||
do: Keyword.fetch(aliases, :"Elixir.#{atom}")
|
||||
|
||||
@doc """
|
||||
Fetches the macro alias for the given atom.
|
||||
|
||||
Returns `{:ok, macro_alias}` if the alias exists, `:error`
|
||||
otherwise.
|
||||
|
||||
A macro alias is only used inside quoted expansion. See
|
||||
`fetch_alias/2` for a more general example.
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec fetch_macro_alias(t, atom) :: {:ok, atom} | :error
|
||||
def fetch_macro_alias(%{__struct__: Macro.Env, macro_aliases: aliases}, atom)
|
||||
when is_atom(atom),
|
||||
do: Keyword.fetch(aliases, :"Elixir.#{atom}")
|
||||
|
||||
@doc """
|
||||
Returns the modules from which the given `{name, arity}` was
|
||||
imported.
|
||||
|
||||
It returns a list of two element tuples in the shape of
|
||||
`{:function | :macro, module}`. The elements in the list
|
||||
are in no particular order and the order is not guaranteed.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
|
||||
[]
|
||||
iex> import Tuple, only: [duplicate: 2], warn: false
|
||||
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
|
||||
[{:function, Tuple}]
|
||||
iex> import List, only: [duplicate: 2], warn: false
|
||||
iex> Macro.Env.lookup_import(__ENV__, {:duplicate, 2})
|
||||
[{:function, List}, {:function, Tuple}]
|
||||
|
||||
iex> Macro.Env.lookup_import(__ENV__, {:def, 1})
|
||||
[{:macro, Kernel}]
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec lookup_import(t, name_arity) :: [{:function | :macro, module}]
|
||||
def lookup_import(
|
||||
%{__struct__: Macro.Env, functions: functions, macros: macros},
|
||||
{name, arity} = pair
|
||||
)
|
||||
when is_atom(name) and is_integer(arity) do
|
||||
f = for {mod, pairs} <- functions, :ordsets.is_element(pair, pairs), do: {:function, mod}
|
||||
m = for {mod, pairs} <- macros, :ordsets.is_element(pair, pairs), do: {:macro, mod}
|
||||
f ++ m
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns true if the given module has been required.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.Env.required?(__ENV__, Integer)
|
||||
false
|
||||
iex> require Integer
|
||||
iex> Macro.Env.required?(__ENV__, Integer)
|
||||
true
|
||||
|
||||
iex> Macro.Env.required?(__ENV__, Kernel)
|
||||
true
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec required?(t, module) :: boolean
|
||||
def required?(%{__struct__: Macro.Env, requires: requires}, mod) when is_atom(mod),
|
||||
do: mod in requires
|
||||
|
||||
@doc """
|
||||
Prepend a tracer to the list of tracers in the environment.
|
||||
|
||||
## Examples
|
||||
|
||||
Macro.Env.prepend_tracer(__ENV__, MyCustomTracer)
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec prepend_tracer(t, module) :: t
|
||||
def prepend_tracer(%{__struct__: Macro.Env, tracers: tracers} = env, tracer) do
|
||||
%{env | tracers: [tracer | tracers]}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a `Macro.Env` in the match context.
|
||||
"""
|
||||
@spec to_match(t) :: t
|
||||
def to_match(%{__struct__: Macro.Env} = env) do
|
||||
%{env | context: :match}
|
||||
def to_match(%{__struct__: Macro.Env, context: :match} = env) do
|
||||
env
|
||||
end
|
||||
|
||||
def to_match(%{__struct__: Macro.Env, current_vars: {read, _}, unused_vars: {_, counter}} = env) do
|
||||
%{env | context: :match, prematch_vars: {read, counter}}
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
||||
+32
-113
@@ -19,13 +19,8 @@ defmodule Map do
|
||||
in a map literal, the last one prevails.
|
||||
|
||||
When the key in a key-value pair is an atom, the `key: value` shorthand syntax
|
||||
can be used (as in many other special forms):
|
||||
|
||||
iex> %{a: 1, b: 2}
|
||||
%{a: 1, b: 2}
|
||||
|
||||
If you want to mix the shorthand syntax with `=>`, the shorthand syntax must come
|
||||
at the end:
|
||||
can be used (as in many other special forms), provided key-value pairs are put at
|
||||
the end:
|
||||
|
||||
iex> %{"hello" => "world", a: 1, b: 2}
|
||||
%{:a => 1, :b => 2, "hello" => "world"}
|
||||
@@ -50,11 +45,11 @@ defmodule Map do
|
||||
map.foo
|
||||
#=> "bar"
|
||||
map.non_existing_key
|
||||
** (KeyError) key :non_existing_key not found in: %{baz: "bong", foo: "bar"}
|
||||
#=> ** (KeyError) key :non_existing_key not found in: %{baz: "bong", foo: "bar"}
|
||||
|
||||
> Note: do not add parens when accessing fields, such as in `data.key()`.
|
||||
> If parenthesis are used, Elixir will expect `data` to be an atom representing
|
||||
> a module and attempt to call the *function* `key/0` in it.
|
||||
> If parenthesis are used, Elixir will consider it to be a function call
|
||||
> on `data`, which would be expected to be an atom.
|
||||
|
||||
The two syntaxes for accessing keys reveal the dual nature of maps. The `map[key]`
|
||||
syntax is used for dynamically created maps that may have any key, of any type.
|
||||
@@ -216,10 +211,20 @@ defmodule Map do
|
||||
@spec new(Enumerable.t(), (term -> {key, value})) :: map
|
||||
def new(enumerable, transform) when is_function(transform, 1) do
|
||||
enumerable
|
||||
|> Enum.map(transform)
|
||||
|> Enum.to_list()
|
||||
|> new_transform(transform, [])
|
||||
end
|
||||
|
||||
defp new_transform([], _fun, acc) do
|
||||
acc
|
||||
|> :lists.reverse()
|
||||
|> :maps.from_list()
|
||||
end
|
||||
|
||||
defp new_transform([element | rest], fun, acc) do
|
||||
new_transform(rest, fun, [fun.(element) | acc])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns whether the given `key` exists in the given `map`.
|
||||
|
||||
@@ -318,7 +323,7 @@ defmodule Map do
|
||||
def replace(map, key, value) do
|
||||
case map do
|
||||
%{^key => _value} ->
|
||||
%{map | key => value}
|
||||
put(map, key, value)
|
||||
|
||||
%{} ->
|
||||
map
|
||||
@@ -609,12 +614,12 @@ defmodule Map do
|
||||
%{a: 1, b: 11}
|
||||
|
||||
"""
|
||||
@spec update(map, key, default :: value, (existing_value :: value -> new_value :: value)) ::
|
||||
@spec update(map, key, default :: value, (existing_value :: value -> updated_value :: value)) ::
|
||||
map
|
||||
def update(map, key, default, fun) when is_function(fun, 1) do
|
||||
case map do
|
||||
%{^key => value} ->
|
||||
%{map | key => fun.(value)}
|
||||
put(map, key, fun.(value))
|
||||
|
||||
%{} ->
|
||||
put(map, key, default)
|
||||
@@ -627,8 +632,8 @@ defmodule Map do
|
||||
@doc """
|
||||
Removes the value associated with `key` in `map` and returns the value and the updated map.
|
||||
|
||||
If `key` is present in `map`, it returns `{value, updated_map}` where `value` is the value of
|
||||
the key and `updated_map` is the result of removing `key` from `map`. If `key`
|
||||
If `key` is present in `map`, it returns `{value, new_map}` where `value` is the value of
|
||||
the key and `new_map` is the result of removing `key` from `map`. If `key`
|
||||
is not present in `map`, `{default, map}` is returned.
|
||||
|
||||
## Examples
|
||||
@@ -641,7 +646,7 @@ defmodule Map do
|
||||
{3, %{a: 1}}
|
||||
|
||||
"""
|
||||
@spec pop(map, key, default) :: {value, updated_map :: map} | {default, map} when default: value
|
||||
@spec pop(map, key, value) :: {value, new_map :: map}
|
||||
def pop(map, key, default \\ nil) do
|
||||
case :maps.take(key, map) do
|
||||
{_, _} = tuple -> tuple
|
||||
@@ -650,8 +655,8 @@ defmodule Map do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes the value associated with `key` in `map` and returns the value
|
||||
and the updated map, or it raises if `key` is not present.
|
||||
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`.
|
||||
|
||||
@@ -666,7 +671,7 @@ defmodule Map do
|
||||
|
||||
"""
|
||||
@doc since: "1.10.0"
|
||||
@spec pop!(map, key) :: {value, updated_map :: map}
|
||||
@spec pop!(map, key) :: {value, map}
|
||||
def pop!(map, key) do
|
||||
case :maps.take(key, map) do
|
||||
{_, _} = tuple -> tuple
|
||||
@@ -807,10 +812,10 @@ defmodule Map do
|
||||
** (KeyError) key :b not found in: %{a: 1}
|
||||
|
||||
"""
|
||||
@spec update!(map, key, (existing_value :: value -> new_value :: value)) :: map
|
||||
@spec update!(map, key, (existing_value :: value -> updated_value :: value)) :: map
|
||||
def update!(map, key, fun) when is_function(fun, 1) do
|
||||
value = fetch!(map, key)
|
||||
%{map | key => fun.(value)}
|
||||
put(map, key, fun.(value))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -836,7 +841,7 @@ defmodule Map do
|
||||
iex> Map.get_and_update(%{a: 1}, :b, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{nil, %{a: 1, b: "new value!"}}
|
||||
{nil, %{b: "new value!", a: 1}}
|
||||
|
||||
iex> Map.get_and_update(%{a: 1}, :a, fn _ -> :pop end)
|
||||
{1, %{}}
|
||||
@@ -845,8 +850,8 @@ defmodule Map do
|
||||
{nil, %{a: 1}}
|
||||
|
||||
"""
|
||||
@spec get_and_update(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, new_map :: map}
|
||||
@spec get_and_update(map, key, (value -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, map}
|
||||
when current_value: value
|
||||
def get_and_update(map, key, fun) when is_function(fun, 1) do
|
||||
current = get(map, key)
|
||||
@@ -887,7 +892,7 @@ defmodule Map do
|
||||
{1, %{}}
|
||||
|
||||
"""
|
||||
@spec get_and_update!(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
|
||||
@spec get_and_update!(map, key, (value -> {current_value, new_value :: value} | :pop)) ::
|
||||
{current_value, map}
|
||||
when current_value: value
|
||||
def get_and_update!(map, key, fun) when is_function(fun, 1) do
|
||||
@@ -895,7 +900,7 @@ defmodule Map do
|
||||
|
||||
case fun.(value) do
|
||||
{get, update} ->
|
||||
{get, %{map | key => update}}
|
||||
{get, put(map, key, update)}
|
||||
|
||||
:pop ->
|
||||
{value, delete(map, key)}
|
||||
@@ -940,11 +945,6 @@ defmodule Map do
|
||||
Two maps are considered to be equal if they contain
|
||||
the same keys and those keys contain the same values.
|
||||
|
||||
Note this function exists for completeness so the `Map`
|
||||
and `Keyword` modules provide similar APIs. In practice,
|
||||
developers often compare maps using `==/2` or `===/2`
|
||||
directly.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.equal?(%{a: 1, b: 2}, %{b: 2, a: 1})
|
||||
@@ -952,12 +952,6 @@ defmodule Map do
|
||||
iex> Map.equal?(%{a: 1, b: 2}, %{b: 1, a: 2})
|
||||
false
|
||||
|
||||
Comparison between keys and values is done with `===/3`,
|
||||
which means integers are not equivalent to floats:
|
||||
|
||||
iex> Map.equal?(%{a: 1.0}, %{a: 1})
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(map, map) :: boolean
|
||||
def equal?(map1, map2)
|
||||
@@ -971,79 +965,4 @@ defmodule Map do
|
||||
def size(map) do
|
||||
map_size(map)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a map containing only those pairs from `map`
|
||||
for which `fun` returns a truthy value.
|
||||
|
||||
`fun` receives the key and value of each of the
|
||||
elements in the map as a key-value pair.
|
||||
|
||||
See also `reject/2` which discards all elements where the
|
||||
function returns a truthy value.
|
||||
|
||||
> Note: if you find yourself doing multiple calls to `Map.filter/2`
|
||||
> and `Map.reject/2` in a pipeline, it is likely more efficient
|
||||
> to use `Enum.map/2` and `Enum.filter/2` instead and convert to
|
||||
> a map at the end using `Map.new/1`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.filter(%{one: 1, two: 2, three: 3}, fn {_key, val} -> rem(val, 2) == 1 end)
|
||||
%{one: 1, three: 3}
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec filter(map, ({key, value} -> as_boolean(term))) :: map
|
||||
def filter(map, fun) when is_map(map) and is_function(fun, 1) do
|
||||
iter = :maps.iterator(map)
|
||||
next = :maps.next(iter)
|
||||
:maps.from_list(do_filter(next, fun))
|
||||
end
|
||||
|
||||
defp do_filter(:none, _fun), do: []
|
||||
|
||||
defp do_filter({key, value, iter}, fun) do
|
||||
if fun.({key, value}) do
|
||||
[{key, value} | do_filter(:maps.next(iter), fun)]
|
||||
else
|
||||
do_filter(:maps.next(iter), fun)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns map excluding the pairs from `map` for which `fun` returns
|
||||
a truthy value.
|
||||
|
||||
See also `filter/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.reject(%{one: 1, two: 2, three: 3}, fn {_key, val} -> rem(val, 2) == 1 end)
|
||||
%{two: 2}
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec reject(map, ({key, value} -> as_boolean(term))) :: map
|
||||
def reject(map, fun) when is_map(map) and is_function(fun, 1) do
|
||||
iter = :maps.iterator(map)
|
||||
next = :maps.next(iter)
|
||||
:maps.from_list(do_reject(next, fun))
|
||||
end
|
||||
|
||||
defp do_reject(:none, _fun), do: []
|
||||
|
||||
defp do_reject({key, value, iter}, fun) do
|
||||
if fun.({key, value}) do
|
||||
do_reject(:maps.next(iter), fun)
|
||||
else
|
||||
[{key, value} | do_reject(:maps.next(iter), fun)]
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Map.new/2 instead"
|
||||
def map(map, fun) when is_map(map) do
|
||||
new(map, fn {k, v} -> {k, fun.({k, v})} end)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -57,7 +57,7 @@ defmodule MapSet do
|
||||
@opaque t(value) :: %__MODULE__{map: %{optional(value) => []}}
|
||||
@type t :: t(term)
|
||||
|
||||
# TODO: Remove version key on Elixir v2.0
|
||||
# TODO: Remove version key on v2.0
|
||||
defstruct map: %{}, version: 2
|
||||
|
||||
@doc """
|
||||
@@ -223,9 +223,7 @@ defmodule MapSet do
|
||||
@doc """
|
||||
Checks if two sets are equal.
|
||||
|
||||
The comparison between elements is done using `===/2`,
|
||||
which a set with `1` is not equivalent to a set with
|
||||
`1.0`.
|
||||
The comparison between elements must be done using `===/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -233,13 +231,11 @@ defmodule MapSet do
|
||||
true
|
||||
iex> MapSet.equal?(MapSet.new([1, 2]), MapSet.new([3, 4]))
|
||||
false
|
||||
iex> MapSet.equal?(MapSet.new([1]), MapSet.new([1.0]))
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(t, t) :: boolean
|
||||
def equal?(%MapSet{map: map1, version: version}, %MapSet{map: map2, version: version}) do
|
||||
map1 === map2
|
||||
Map.equal?(map1, map2)
|
||||
end
|
||||
|
||||
# Elixir v1.5 changed the map representation, so on
|
||||
@@ -402,7 +398,6 @@ defmodule MapSet do
|
||||
end
|
||||
|
||||
defimpl Collectable do
|
||||
# TODO: Optimize into an empty mapset by using :maps.from_keys/2 on Erlang/OTP 24+
|
||||
def into(map_set) do
|
||||
fun = fn
|
||||
list, {:cont, x} -> [{x, []} | list]
|
||||
|
||||
+48
-315
@@ -235,7 +235,7 @@ defmodule Module do
|
||||
end
|
||||
|
||||
For the list of supported warnings, see
|
||||
[`:dialyzer` module](`:dialyzer`).
|
||||
[`:dialyzer` module](http://www.erlang.org/doc/man/dialyzer.html).
|
||||
|
||||
Multiple uses of `@dialyzer` will accumulate instead of overriding
|
||||
previous ones.
|
||||
@@ -253,7 +253,7 @@ defmodule Module do
|
||||
[`mix compile.elixir`](https://hexdocs.pm/mix/Mix.Tasks.Compile.Elixir.html).
|
||||
|
||||
If the external resource does not exist, the module still has
|
||||
a dependency on it, causing the module to be recompiled as soon
|
||||
a dependency on it, causing the module be recompiled as soon
|
||||
as the file is added.
|
||||
|
||||
### `@file`
|
||||
@@ -305,9 +305,9 @@ defmodule Module do
|
||||
|
||||
Accepts the function name (as an atom) of a function in the current module or
|
||||
`{function_name, 0}` tuple where `function_name` is the name of a function in
|
||||
the current module. The function must have an arity of 0 (no arguments). If
|
||||
the function does not return `:ok`, the loading of the module will be aborted.
|
||||
For example:
|
||||
the current module. The function must be public and have an arity of 0 (no
|
||||
arguments). If the function does not return `:ok`, the loading of the module
|
||||
will be aborted. For example:
|
||||
|
||||
defmodule MyModule do
|
||||
@on_load :load_check
|
||||
@@ -335,16 +335,6 @@ defmodule Module do
|
||||
@vsn "1.0"
|
||||
end
|
||||
|
||||
### Struct attributes
|
||||
|
||||
* `@derive` - derives an implementation for the given protocol for the
|
||||
struct defined in the current module
|
||||
|
||||
* `@enforce_keys` - ensures the given keys are always set when building
|
||||
the struct defined in the current module
|
||||
|
||||
See `Kernel.defstruct/1` for more information on building and using structs.
|
||||
|
||||
### Typespec attributes
|
||||
|
||||
The following attributes are part of typespecs and are also built-in in
|
||||
@@ -553,103 +543,6 @@ defmodule Module do
|
||||
@callback __info__(:md5) :: binary()
|
||||
@callback __info__(:module) :: module()
|
||||
|
||||
@doc """
|
||||
Returns information about module attributes used by Elixir.
|
||||
|
||||
See the "Module attributes" section in the module documentation for more
|
||||
information on each attribute.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = Module.reserved_attributes()
|
||||
iex> Map.has_key?(map, :moduledoc)
|
||||
true
|
||||
iex> Map.has_key?(map, :doc)
|
||||
true
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
def reserved_attributes() do
|
||||
%{
|
||||
after_compile: %{
|
||||
doc: "A hook that will be invoked right after the current module is compiled."
|
||||
},
|
||||
before_compile: %{
|
||||
doc: "A hook that will be invoked before the module is compiled."
|
||||
},
|
||||
behaviour: %{
|
||||
doc: "Specifies that the current module implements a given behaviour."
|
||||
},
|
||||
on_definition: %{
|
||||
doc:
|
||||
"A hook that will be invoked when each function or macro in the current module is defined."
|
||||
},
|
||||
impl: %{
|
||||
doc: "Declares an implementation of a callback function or macro."
|
||||
},
|
||||
compile: %{
|
||||
doc: "Defines options for module compilation."
|
||||
},
|
||||
deprecated: %{
|
||||
doc: "Provides the deprecation reason for a function."
|
||||
},
|
||||
moduledoc: %{
|
||||
doc: "Provides documentation for the current module."
|
||||
},
|
||||
doc: %{
|
||||
doc: "Provides documentation for a function/macro/callback."
|
||||
},
|
||||
typedoc: %{
|
||||
doc: "Provides documentation for a type."
|
||||
},
|
||||
dialyzer: %{
|
||||
doc: "Defines Dialyzer warnings to request or suppress."
|
||||
},
|
||||
external_resource: %{
|
||||
doc: "Specifies an external resource for the current module."
|
||||
},
|
||||
file: %{
|
||||
doc:
|
||||
"Changes the filename used in stacktraces for the function or macro that follows the attribute."
|
||||
},
|
||||
on_load: %{
|
||||
doc: "A hook that will be invoked whenever the module is loaded."
|
||||
},
|
||||
vsn: %{
|
||||
doc: "Specify the module version."
|
||||
},
|
||||
type: %{
|
||||
doc: "Defines a type to be used in `@spec`."
|
||||
},
|
||||
typep: %{
|
||||
doc: "Defines a private type to be used in `@spec`."
|
||||
},
|
||||
opaque: %{
|
||||
doc: "Defines an opaque type to be used in `@spec`."
|
||||
},
|
||||
spec: %{
|
||||
doc: "Provides a specification for a function."
|
||||
},
|
||||
callback: %{
|
||||
doc: "Provides a specification for a behaviour callback."
|
||||
},
|
||||
macrocallback: %{
|
||||
doc: "Provides a specification for a macro behaviour callback."
|
||||
},
|
||||
optional_callbacks: %{
|
||||
doc: "Specifies which behaviour callbacks and macro behaviour callbacks are optional."
|
||||
},
|
||||
derive: %{
|
||||
doc:
|
||||
"Derives an implementation for the given protocol for the struct defined in the current module."
|
||||
},
|
||||
enforce_keys: %{
|
||||
doc:
|
||||
"Ensures the given keys are always set when building the struct defined in the current module."
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if a module is open.
|
||||
|
||||
@@ -709,24 +602,22 @@ defmodule Module do
|
||||
|
||||
def eval_quoted(%Macro.Env{} = env, quoted, binding, opts)
|
||||
when is_list(binding) and is_list(opts) do
|
||||
validated_eval_quoted(env.module, quoted, binding, struct!(env, opts))
|
||||
eval_quoted(env.module, quoted, binding, Keyword.merge(Map.to_list(env), opts))
|
||||
end
|
||||
|
||||
def eval_quoted(module, quoted, binding, %Macro.Env{} = env)
|
||||
when is_atom(module) and is_list(binding) do
|
||||
validated_eval_quoted(module, quoted, binding, env)
|
||||
eval_quoted(module, quoted, binding, Map.to_list(env))
|
||||
end
|
||||
|
||||
def eval_quoted(module, quoted, binding, opts)
|
||||
when is_atom(module) and is_list(binding) and is_list(opts) do
|
||||
validated_eval_quoted(module, quoted, binding, opts)
|
||||
end
|
||||
|
||||
defp validated_eval_quoted(module, quoted, binding, env_or_opts) do
|
||||
assert_not_compiled!({:eval_quoted, 4}, module)
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
:elixir_def.reset_last(module)
|
||||
env = :elixir.env_for_eval(env_or_opts)
|
||||
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, %{env | module: module})
|
||||
|
||||
{value, binding, _env} =
|
||||
:elixir.eval_quoted(quoted, binding, Keyword.put(opts, :module, module))
|
||||
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
@@ -792,8 +683,6 @@ defmodule Module do
|
||||
@doc """
|
||||
Concatenates a list of aliases and returns a new alias.
|
||||
|
||||
It handles binaries and atoms.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Module.concat([Foo, Bar])
|
||||
@@ -811,8 +700,6 @@ defmodule Module do
|
||||
@doc """
|
||||
Concatenates two aliases and returns a new alias.
|
||||
|
||||
It handles binaries and atoms.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Module.concat(Foo, Bar)
|
||||
@@ -833,10 +720,13 @@ defmodule Module do
|
||||
was already referenced.
|
||||
|
||||
If the alias was not referenced yet, fails with `ArgumentError`.
|
||||
It handles binaries and atoms.
|
||||
It handles charlists, binaries and atoms.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Module.safe_concat([Module, Unknown])
|
||||
** (ArgumentError) argument error
|
||||
|
||||
iex> Module.safe_concat([List, Chars])
|
||||
List.Chars
|
||||
|
||||
@@ -851,10 +741,13 @@ defmodule Module do
|
||||
already referenced.
|
||||
|
||||
If the alias was not referenced yet, fails with `ArgumentError`.
|
||||
It handles binaries and atoms.
|
||||
It handles charlists, binaries and atoms.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Module.safe_concat(Module, Unknown)
|
||||
** (ArgumentError) argument error
|
||||
|
||||
iex> Module.safe_concat(List, Chars)
|
||||
List.Chars
|
||||
|
||||
@@ -967,8 +860,8 @@ defmodule Module do
|
||||
|
||||
defp autogenerated_key(counters, key) do
|
||||
case counters do
|
||||
%{^key => :once} -> {key, %{counters | key => 2}}
|
||||
%{^key => value} -> {key, %{counters | key => value + 1}}
|
||||
%{^key => :once} -> {key, Map.put(counters, key, 2)}
|
||||
%{^key => value} -> {key, Map.put(counters, key, value + 1)}
|
||||
%{} -> {key, Map.put(counters, key, :once)}
|
||||
end
|
||||
end
|
||||
@@ -1117,61 +1010,6 @@ defmodule Module do
|
||||
Kernel.Typespec.spec_to_callback(module, definition)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all module attributes names defined in `module`.
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule Example do
|
||||
@foo 1
|
||||
Module.register_attribute(__MODULE__, :bar, accumulate: true)
|
||||
|
||||
:foo in Module.attributes_in(__MODULE__)
|
||||
#=> true
|
||||
|
||||
:bar in Module.attributes_in(__MODULE__)
|
||||
#=> true
|
||||
end
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec attributes_in(module) :: [atom]
|
||||
def attributes_in(module) when is_atom(module) do
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
{set, _} = data_tables_for(module)
|
||||
:ets.select(set, [{{:"$1", :_, :_}, [{:is_atom, :"$1"}], [:"$1"]}])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all overridable definitions in `module`.
|
||||
|
||||
Note a definition is included even if it was was already overridden.
|
||||
You can use `defines?/2` to see if a definition exists or one is pending.
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule Example do
|
||||
def foo, do: 1
|
||||
def bar, do: 2
|
||||
|
||||
defoverridable foo: 1, bar: 1
|
||||
def foo, do: 3
|
||||
|
||||
[:bar, :foo] = Module.overridables_in(__MODULE__) |> Enum.sort()
|
||||
end
|
||||
|
||||
"""
|
||||
@doc since: "1.13.0"
|
||||
@spec overridables_in(module) :: [atom]
|
||||
def overridables_in(module) when is_atom(module) do
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
:elixir_overridable.overridables_for(module)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all functions and macros defined in `module`.
|
||||
|
||||
@@ -1223,74 +1061,6 @@ defmodule Module do
|
||||
:ets.select(set, [{{{:def, :"$1"}, kind, :_, :_, :_, :_}, [], [:"$1"]}])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the definition for the given name-arity pair.
|
||||
|
||||
It returns a tuple with the `version`, the `kind`,
|
||||
the definition `metadata`, and a list with each clause.
|
||||
Each clause is a four-element tuple with metadata,
|
||||
the arguments, the guards, and the clause AST.
|
||||
|
||||
The clauses are returned in the Elixir AST but a subset
|
||||
that has already been expanded and normalized. This makes
|
||||
it useful for analyzing code but it cannot be reinjected
|
||||
into the module as it will have lost some of its original
|
||||
context. Given this AST representation is mostly internal,
|
||||
it is versioned and it may change at any time. Therefore,
|
||||
**use this API with caution**.
|
||||
|
||||
## Options
|
||||
|
||||
* `:nillify_clauses` (since v1.13.0) - returns `nil` instead
|
||||
of returning the clauses. This is useful when there is
|
||||
only an interest in fetching the kind and metadata
|
||||
|
||||
"""
|
||||
@spec get_definition(module, definition, keyword) ::
|
||||
{:v1, def_kind, meta :: keyword,
|
||||
[{meta :: keyword, arguments :: [Macro.t()], guards :: [Macro.t()], Macro.t()}] | nil}
|
||||
@doc since: "1.12.0"
|
||||
def get_definition(module, {name, arity}, options \\ [])
|
||||
when is_atom(module) and is_atom(name) and is_integer(arity) and is_list(options) do
|
||||
assert_not_compiled!(__ENV__.function, module, "")
|
||||
{set, bag} = data_tables_for(module)
|
||||
|
||||
case :ets.lookup(set, {:def, {name, arity}}) do
|
||||
[{_key, kind, meta, _, _, _}] ->
|
||||
clauses =
|
||||
if options[:nillify_clauses],
|
||||
do: nil,
|
||||
else: bag_lookup_element(bag, {:clauses, {name, arity}}, 2)
|
||||
|
||||
{:v1, kind, meta, clauses}
|
||||
|
||||
[] ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a definition from a module.
|
||||
|
||||
It returns true if the definition exists and it was removed,
|
||||
otherwise it returns false.
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec delete_definition(module, definition) :: boolean()
|
||||
def delete_definition(module, {name, arity})
|
||||
when is_atom(module) and is_atom(name) and is_integer(arity) do
|
||||
assert_not_readonly!(__ENV__.function, module)
|
||||
|
||||
case :elixir_def.take_definition(module, {name, arity}) do
|
||||
false ->
|
||||
false
|
||||
|
||||
_ ->
|
||||
:elixir_locals.yank({name, arity}, module)
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Makes the given functions in `module` overridable.
|
||||
|
||||
@@ -1361,7 +1131,7 @@ defmodule Module do
|
||||
behaviour_definitions = bag_lookup_element(bag, {:accumulate, :behaviour}, 2)
|
||||
|
||||
cond do
|
||||
not Code.ensure_loaded?(behaviour) ->
|
||||
Code.ensure_compiled(behaviour) != {:module, behaviour} ->
|
||||
{:error, "it was not defined"}
|
||||
|
||||
not function_exported?(behaviour, :behaviour_info, 1) ->
|
||||
@@ -1398,12 +1168,7 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if `tuple` in `module` was marked as overridable
|
||||
at some point.
|
||||
|
||||
Note `overridable?/2` returns true even if the definition was
|
||||
already overridden. You can use `defines?/2` to see if a definition
|
||||
exists or one is pending.
|
||||
Returns `true` if `tuple` in `module` is marked as overridable.
|
||||
"""
|
||||
@spec overridable?(module, definition) :: boolean
|
||||
def overridable?(module, {function_name, arity} = tuple)
|
||||
@@ -1686,8 +1451,7 @@ defmodule Module do
|
||||
redefining @doc attribute previously set at line #{current_line}.
|
||||
|
||||
Please remove the duplicate docs. If instead you want to override a \
|
||||
previously defined @doc, attach the @doc attribute to a function head \
|
||||
(the function signature not followed by any do-block). For example:
|
||||
previously defined @doc, attach the @doc attribute to a function head:
|
||||
|
||||
@doc """
|
||||
new docs
|
||||
@@ -1796,7 +1560,14 @@ defmodule Module do
|
||||
defp check_behaviours(env, behaviours) do
|
||||
Enum.reduce(behaviours, %{}, fn behaviour, acc ->
|
||||
cond do
|
||||
not Code.ensure_loaded?(behaviour) ->
|
||||
not is_atom(behaviour) ->
|
||||
message =
|
||||
"@behaviour #{inspect(behaviour)} must be an atom (in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
acc
|
||||
|
||||
Code.ensure_compiled(behaviour) != {:module, behaviour} ->
|
||||
message =
|
||||
"@behaviour #{inspect(behaviour)} does not exist (in module #{inspect(env.module)})"
|
||||
|
||||
@@ -2185,15 +1956,6 @@ defmodule Module do
|
||||
end
|
||||
end
|
||||
|
||||
defp preprocess_attribute(:behaviour, value) do
|
||||
if is_atom(value) do
|
||||
Code.ensure_compiled(value)
|
||||
value
|
||||
else
|
||||
raise ArgumentError, "@behaviour expects a module, got: #{inspect(value)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp preprocess_attribute(:on_load, value) do
|
||||
case value do
|
||||
_ when is_atom(value) ->
|
||||
@@ -2211,13 +1973,20 @@ defmodule Module do
|
||||
end
|
||||
|
||||
defp preprocess_attribute(:impl, value) do
|
||||
if is_boolean(value) or (is_atom(value) and value != nil) do
|
||||
value
|
||||
else
|
||||
raise ArgumentError,
|
||||
"@impl is a built-in module attribute that marks the next definition " <>
|
||||
"as a callback implementation. It should be a module or a boolean, " <>
|
||||
"got: #{inspect(value)}"
|
||||
case value do
|
||||
_ when is_boolean(value) ->
|
||||
value
|
||||
|
||||
module when is_atom(module) and module != nil ->
|
||||
# Attempt to compile behaviour but ignore failure (will warn later)
|
||||
_ = Code.ensure_compiled(module)
|
||||
value
|
||||
|
||||
_ ->
|
||||
raise ArgumentError,
|
||||
"@impl is a built-in module attribute that marks the next definition " <>
|
||||
"as a callback implementation. It should be a module or a boolean, " <>
|
||||
"got: #{inspect(value)}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2240,7 +2009,7 @@ defmodule Module do
|
||||
defp preprocess_attribute(:external_resource, value) when not is_binary(value) do
|
||||
raise ArgumentError,
|
||||
"@external_resource is a built-in module attribute used for specifying file " <>
|
||||
"dependencies. It should be a string path to a file, got: #{inspect(value)}"
|
||||
"dependencies. It should be a string the path to a file, got: #{inspect(value)}"
|
||||
end
|
||||
|
||||
defp preprocess_attribute(:deprecated, value) when not is_binary(value) do
|
||||
@@ -2265,46 +2034,10 @@ defmodule Module do
|
||||
end
|
||||
end
|
||||
|
||||
defp preprocess_attribute(:dialyzer, value) do
|
||||
# From https://github.com/erlang/otp/blob/master/lib/stdlib/src/erl_lint.erl
|
||||
:lists.foreach(
|
||||
fn attr ->
|
||||
if not valid_dialyzer_attribute?(attr) do
|
||||
raise ArgumentError, "invalid value for @dialyzer attribute: #{inspect(attr)}"
|
||||
end
|
||||
end,
|
||||
List.wrap(value)
|
||||
)
|
||||
|
||||
value
|
||||
end
|
||||
|
||||
defp preprocess_attribute(_key, value) do
|
||||
value
|
||||
end
|
||||
|
||||
defp valid_dialyzer_attribute?({key, fun_arities}) when is_atom(key) do
|
||||
(key == :nowarn_function or valid_dialyzer_attribute?(key)) and
|
||||
:lists.all(
|
||||
fn
|
||||
{fun, arity} when is_atom(fun) and is_integer(arity) -> true
|
||||
_ -> false
|
||||
end,
|
||||
List.wrap(fun_arities)
|
||||
)
|
||||
end
|
||||
|
||||
defp valid_dialyzer_attribute?(attr) do
|
||||
:lists.member(
|
||||
attr,
|
||||
[:no_return, :no_unused, :no_improper_lists, :no_fun_app] ++
|
||||
[:no_match, :no_opaque, :no_fail_call, :no_contracts] ++
|
||||
[:no_behaviours, :no_undefined_callbacks, :unmatched_returns] ++
|
||||
[:error_handling, :race_conditions, :no_missing_calls] ++
|
||||
[:specdiffs, :overspecs, :underspecs, :unknown, :no_underspecs]
|
||||
)
|
||||
end
|
||||
|
||||
defp preprocess_doc_meta([], _module, _line, map), do: map
|
||||
|
||||
defp preprocess_doc_meta([{key, _} | tail], module, line, map)
|
||||
|
||||
@@ -71,9 +71,8 @@ defmodule Module.LocalsTracker do
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Collect all conflicting imports with the given functions
|
||||
"""
|
||||
# Collecting all conflicting imports with the given functions
|
||||
@doc false
|
||||
def collect_imports_conflicts({set, _bag}, all_defined) do
|
||||
for {pair, _, meta, _} <- all_defined, n = out_neighbour(set, {:import, pair}) do
|
||||
{meta, {n, pair}}
|
||||
|
||||
@@ -1,141 +1,43 @@
|
||||
defmodule Module.ParallelChecker do
|
||||
@moduledoc false
|
||||
|
||||
import Kernel, except: [spawn: 3]
|
||||
|
||||
@type cache() :: {pid(), :ets.tid()}
|
||||
@type warning() :: term()
|
||||
@type kind() :: :def | :defmacro
|
||||
@type mode() :: :elixir | :erlang
|
||||
|
||||
@doc """
|
||||
Initializes the parallel checker process.
|
||||
"""
|
||||
def start_link(schedulers \\ nil) do
|
||||
:gen_server.start_link(__MODULE__, schedulers, [])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the parallel checker process.
|
||||
"""
|
||||
def stop(checker) do
|
||||
send(checker, {__MODULE__, :stop})
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the parallel checker data from pdict.
|
||||
"""
|
||||
def get do
|
||||
{_, checker} = :erlang.get(:elixir_checker_info)
|
||||
checker
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stores the parallel checker information.
|
||||
"""
|
||||
def put(pid, checker) do
|
||||
:erlang.put(:elixir_checker_info, {pid, checker})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Spawns a process that runs the parallel checker.
|
||||
"""
|
||||
def spawn({pid, checker}, module, info) do
|
||||
ref = make_ref()
|
||||
|
||||
spawned =
|
||||
spawn(fn ->
|
||||
Process.link(pid)
|
||||
mon_ref = Process.monitor(pid)
|
||||
|
||||
receive do
|
||||
{^ref, :cache, ets} ->
|
||||
loaded_info =
|
||||
if is_map(info) do
|
||||
cache_from_module_map(ets, info)
|
||||
info
|
||||
else
|
||||
info = File.read!(info)
|
||||
cache_from_chunk(ets, module, info)
|
||||
info
|
||||
end
|
||||
|
||||
send(checker, {ref, :cached})
|
||||
|
||||
receive do
|
||||
{^ref, :check} ->
|
||||
warnings = check_module(module, loaded_info, {checker, ets})
|
||||
send(pid, {__MODULE__, module, warnings})
|
||||
send(checker, {__MODULE__, :done})
|
||||
end
|
||||
|
||||
{:DOWN, ^mon_ref, _, _, _} ->
|
||||
:ok
|
||||
end
|
||||
end)
|
||||
|
||||
{spawned, ref}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Verifies the given compilation function
|
||||
by starting a checker if one does not exist.
|
||||
See `verify/3`.
|
||||
"""
|
||||
def verify(fun) do
|
||||
case :erlang.get(:elixir_compiler_info) do
|
||||
:undefined ->
|
||||
previous = :erlang.get(:elixir_checker_info)
|
||||
{:ok, checker} = start_link()
|
||||
put(self(), checker)
|
||||
|
||||
try do
|
||||
{result, compile_info} = Enum.unzip(fun.())
|
||||
_ = verify(checker, compile_info, [])
|
||||
result
|
||||
after
|
||||
if previous != :undefined do
|
||||
:erlang.put(:elixir_checker_info, previous)
|
||||
else
|
||||
:erlang.erase(:elixir_checker_info)
|
||||
end
|
||||
|
||||
stop(checker)
|
||||
end
|
||||
|
||||
_ ->
|
||||
# If we are during compilation, then they will be
|
||||
# reported to the compiler, which will validate them.
|
||||
Enum.map(fun.(), &elem(&1, 0))
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives pairs of module maps and BEAM binaries. In parallel it verifies
|
||||
the modules and adds the ExCk chunk to the binaries. Returns the updated
|
||||
list of warnings from the verification.
|
||||
binaries and a list of warnings from the verification.
|
||||
"""
|
||||
@spec verify(pid(), [{pid(), reference()}], [{module(), binary()}]) :: [warning()]
|
||||
def verify(checker, compiled_info, runtime_files) do
|
||||
runtime_info =
|
||||
for {module, file} <- runtime_files do
|
||||
spawn({self(), checker}, module, file)
|
||||
end
|
||||
@spec verify([{map(), binary()}], [{module(), binary()}], pos_integer() | nil) :: [warning()]
|
||||
def verify(compiled_modules, runtime_binaries, schedulers \\ nil) do
|
||||
compiled_maps = Enum.map(compiled_modules, fn {map, _binary} -> {map.module, map} end)
|
||||
|
||||
modules = compiled_info ++ runtime_info
|
||||
:gen_server.cast(checker, {:start, modules})
|
||||
collect_results(modules, [])
|
||||
case compiled_maps ++ runtime_binaries do
|
||||
[] ->
|
||||
[]
|
||||
|
||||
check_modules ->
|
||||
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
|
||||
end
|
||||
|
||||
defp collect_results([], warnings) do
|
||||
defp collect_results(0, warnings) do
|
||||
warnings
|
||||
end
|
||||
|
||||
defp collect_results([_ | modules], warnings) do
|
||||
defp collect_results(count, warnings) do
|
||||
receive do
|
||||
{__MODULE__, _module, new_warnings} ->
|
||||
collect_results(modules, new_warnings ++ warnings)
|
||||
collect_results(count - 1, new_warnings ++ warnings)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -143,8 +45,8 @@ defmodule Module.ParallelChecker do
|
||||
Test cache.
|
||||
"""
|
||||
def test_cache do
|
||||
{:ok, checker} = start_link()
|
||||
{checker, :gen_server.call(checker, :ets)}
|
||||
{:ok, pid} = :gen_server.start_link(__MODULE__, [[], self(), 1], [])
|
||||
{pid, get_ets(pid)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -165,17 +67,17 @@ defmodule Module.ParallelChecker do
|
||||
or if the function does not exist return `{:error, :function}`.
|
||||
"""
|
||||
@spec fetch_export(cache(), module(), atom(), arity()) ::
|
||||
{:ok, mode(), kind(), binary() | nil} | {:error, :function | :module}
|
||||
{:ok, kind(), binary() | nil} | {:error, :function | :module}
|
||||
def fetch_export({_server, ets}, module, fun, arity) do
|
||||
case :ets.lookup(ets, {:cached, module}) do
|
||||
[{_key, false}] ->
|
||||
{:error, :module}
|
||||
|
||||
[{_key, mode}] ->
|
||||
case :ets.lookup(ets, {:export, module, {fun, arity}}) do
|
||||
[{_key, kind, reason}] -> {:ok, mode, kind, reason}
|
||||
[{_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
|
||||
|
||||
@@ -187,16 +89,17 @@ defmodule Module.ParallelChecker do
|
||||
def all_exports({_server, ets}, module) do
|
||||
# This is only called after we get a deprecation notice
|
||||
# so we can assume it's a cached module
|
||||
ets
|
||||
|> :ets.match({{:export, module, :"$1"}, :_, :_})
|
||||
|> Enum.flat_map(& &1)
|
||||
[{_key, exports}] = :ets.lookup(ets, {:all_exports, module})
|
||||
|
||||
exports
|
||||
|> Enum.map(fn {function, _kind} -> function end)
|
||||
|> Enum.sort()
|
||||
end
|
||||
|
||||
## Module checking
|
||||
|
||||
defp check_module(module, info, cache) do
|
||||
case extract_definitions(module, info) do
|
||||
defp check_module(module, cache) do
|
||||
case extract_definitions(module) do
|
||||
{:ok, module, file, definitions, no_warn_undefined} ->
|
||||
Module.Types.warnings(module, file, definitions, no_warn_undefined, cache)
|
||||
|> group_warnings()
|
||||
@@ -207,7 +110,7 @@ defmodule Module.ParallelChecker do
|
||||
end
|
||||
end
|
||||
|
||||
defp extract_definitions(module, module_map) when is_map(module_map) do
|
||||
defp extract_definitions({module, module_map}) when is_map(module_map) do
|
||||
no_warn_undefined =
|
||||
module_map.compile_opts
|
||||
|> extract_no_warn_undefined()
|
||||
@@ -216,11 +119,11 @@ defmodule Module.ParallelChecker do
|
||||
{:ok, module, module_map.file, module_map.definitions, no_warn_undefined}
|
||||
end
|
||||
|
||||
defp extract_definitions(module, binary) when is_binary(binary) do
|
||||
defp extract_definitions({module, binary}) when is_binary(binary) do
|
||||
with {:ok, {_, [debug_info: chunk]}} <- :beam_lib.chunks(binary, [:debug_info]),
|
||||
{:debug_info_v1, backend, data} <- chunk,
|
||||
{:ok, module_map} <- backend.debug_info(:elixir_v1, module, data, []) do
|
||||
extract_definitions(module, module_map)
|
||||
extract_definitions({module, module_map})
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
@@ -273,7 +176,7 @@ defmodule Module.ParallelChecker do
|
||||
|
||||
defp format_locations(locations) do
|
||||
[
|
||||
"Invalid call found at #{length(locations)} locations:\n",
|
||||
"Found at #{length(locations)} locations:\n",
|
||||
Enum.map(locations, &format_location/1)
|
||||
]
|
||||
end
|
||||
@@ -301,7 +204,106 @@ defmodule Module.ParallelChecker do
|
||||
IO.puts(:stderr, [:elixir_errors.warning_prefix(), message])
|
||||
end
|
||||
|
||||
## Cache
|
||||
## Server callbacks
|
||||
|
||||
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, 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 = check_module(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
|
||||
@@ -334,31 +336,28 @@ defmodule Module.ParallelChecker do
|
||||
definitions_to_exports(map.definitions)
|
||||
|
||||
deprecated = Map.new(map.deprecated)
|
||||
cache_info(ets, map.module, exports, deprecated, :elixir)
|
||||
cache_info(ets, map.module, exports, deprecated)
|
||||
end
|
||||
|
||||
defp cache_from_info(ets, module) do
|
||||
if Code.ensure_loaded?(module) do
|
||||
{mode, exports} = info_exports(module)
|
||||
exports = info_exports(module)
|
||||
deprecated = info_deprecated(module)
|
||||
cache_info(ets, module, exports, deprecated, mode)
|
||||
cache_info(ets, module, exports, deprecated)
|
||||
else
|
||||
:ets.insert(ets, {{:cached, module}, false})
|
||||
end
|
||||
end
|
||||
|
||||
defp info_exports(module) do
|
||||
map =
|
||||
Map.new(
|
||||
[{{:__info__, 1}, :def}] ++
|
||||
behaviour_exports(module) ++
|
||||
Enum.map(module.__info__(:macros), &{&1, :defmacro}) ++
|
||||
Enum.map(module.__info__(:functions), &{&1, :def})
|
||||
)
|
||||
|
||||
{:elixir, map}
|
||||
Map.new(
|
||||
[{{:__info__, 1}, :def}] ++
|
||||
behaviour_exports(module) ++
|
||||
Enum.map(module.__info__(:macros), &{&1, :defmacro}) ++
|
||||
Enum.map(module.__info__(:functions), &{&1, :def})
|
||||
)
|
||||
rescue
|
||||
_ -> {:erlang, Map.new(Enum.map(module.module_info(:exports), &{&1, :def}))}
|
||||
_ -> Map.new(Enum.map(module.module_info(:exports), &{&1, :def}))
|
||||
end
|
||||
|
||||
defp info_deprecated(module) do
|
||||
@@ -367,32 +366,39 @@ defmodule Module.ParallelChecker do
|
||||
_ -> %{}
|
||||
end
|
||||
|
||||
defp cache_info(ets, module, exports, deprecated, mode) do
|
||||
Enum.each(exports, fn {{fun, arity}, kind} ->
|
||||
reason = Map.get(deprecated, {fun, arity})
|
||||
:ets.insert(ets, {{:export, module, {fun, arity}}, kind, reason})
|
||||
{{fun, arity}, kind}
|
||||
end)
|
||||
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})
|
||||
|
||||
:ets.insert(ets, {{:cached, module}, mode})
|
||||
{{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
|
||||
Enum.each(exports, fn {{fun, arity}, %{kind: kind, deprecated_reason: reason}} ->
|
||||
:ets.insert(ets, {{:export, module, {fun, arity}}, kind, reason})
|
||||
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)
|
||||
{{fun, arity}, kind}
|
||||
end)
|
||||
|
||||
:ets.insert(ets, {{:export, module, {:__info__, 1}}, :def, nil})
|
||||
:ets.insert(ets, {{:cached, module}, :elixir})
|
||||
: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 function_exported?(module, :behaviour_info, 1) do
|
||||
if {:behaviour_info, 1} in module.module_info(:functions) do
|
||||
[{{:behaviour_info, 1}, :def}]
|
||||
else
|
||||
[]
|
||||
@@ -408,88 +414,4 @@ defmodule Module.ParallelChecker do
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp lock(server, module) do
|
||||
:gen_server.call(server, {:lock, module}, :infinity)
|
||||
end
|
||||
|
||||
defp unlock(server, module) do
|
||||
:gen_server.call(server, {:unlock, module})
|
||||
end
|
||||
|
||||
## Server callbacks
|
||||
|
||||
def init(schedulers) do
|
||||
ets = :ets.new(__MODULE__, [:set, :public, {:read_concurrency, true}])
|
||||
|
||||
state = %{
|
||||
ets: ets,
|
||||
waiting: %{},
|
||||
modules: [],
|
||||
spawned: 0,
|
||||
schedulers: schedulers || max(:erlang.system_info(:schedulers_online), 2)
|
||||
}
|
||||
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
def handle_call(:ets, _from, state) do
|
||||
{:reply, state.ets, state}
|
||||
end
|
||||
|
||||
def handle_call({:lock, module}, from, %{waiting: waiting} = state) do
|
||||
case waiting do
|
||||
%{^module => froms} ->
|
||||
waiting = Map.put(state.waiting, module, [from | froms])
|
||||
{:noreply, %{state | waiting: waiting}}
|
||||
|
||||
%{} ->
|
||||
waiting = Map.put(state.waiting, module, [])
|
||||
{:reply, true, %{state | waiting: waiting}}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_call({:unlock, module}, _from, %{waiting: waiting} = state) do
|
||||
froms = Map.fetch!(waiting, module)
|
||||
Enum.each(froms, &:gen_server.reply(&1, false))
|
||||
waiting = Map.delete(waiting, module)
|
||||
{:reply, :ok, %{state | waiting: waiting}}
|
||||
end
|
||||
|
||||
def handle_info({__MODULE__, :done}, state) do
|
||||
state = %{state | spawned: state.spawned - 1}
|
||||
{:noreply, run_checkers(state)}
|
||||
end
|
||||
|
||||
def handle_info({__MODULE__, :stop}, state) do
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
def handle_cast({:start, modules}, %{ets: ets} = state) do
|
||||
for {pid, ref} <- modules do
|
||||
send(pid, {ref, :cache, ets})
|
||||
end
|
||||
|
||||
for {_pid, ref} <- modules do
|
||||
receive do
|
||||
{^ref, :cached} -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
{:noreply, run_checkers(%{state | modules: modules})}
|
||||
end
|
||||
|
||||
defp run_checkers(%{modules: []} = state) do
|
||||
state
|
||||
end
|
||||
|
||||
defp run_checkers(%{spawned: spawned, schedulers: schedulers} = state)
|
||||
when spawned >= schedulers do
|
||||
state
|
||||
end
|
||||
|
||||
defp run_checkers(%{modules: [{pid, ref} | modules]} = state) do
|
||||
send(pid, {ref, :check})
|
||||
run_checkers(%{state | modules: modules, spawned: state.spawned + 1})
|
||||
end
|
||||
end
|
||||
|
||||
+152
-147
@@ -60,7 +60,7 @@ defmodule Module.Types do
|
||||
head_stack = Unify.push_expr_stack(def_expr, stack)
|
||||
|
||||
with {:ok, _types, context} <- Pattern.of_head(args, guards, head_stack, context),
|
||||
{:ok, _type, context} <- Expr.of_expr(body, :dynamic, stack, context) do
|
||||
{:ok, _type, context} <- Expr.of_expr(body, stack, context) do
|
||||
context.warnings
|
||||
else
|
||||
{:error, {type, error, context}} ->
|
||||
@@ -92,7 +92,7 @@ defmodule Module.Types do
|
||||
traces: %{},
|
||||
# Counter to give type variables unique names
|
||||
counter: 0,
|
||||
# Track if a variable was inferred from a type guard function such is_tuple/1
|
||||
# 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)`
|
||||
# `:guarded` when `is_tuple and elem(x, 0)`
|
||||
@@ -115,42 +115,115 @@ defmodule Module.Types do
|
||||
# When false do not add a trace when a type variable is refined,
|
||||
# useful when merging contexts where the variables already have traces
|
||||
trace: true,
|
||||
# There are two factors that control how we track guards.
|
||||
#
|
||||
# * consider_type_guards?: if type guards should be considered.
|
||||
# This applies only at the root and root-based "and" and "or" nodes.
|
||||
#
|
||||
# * keep_guarded? - if a guarded clause should remain as guarded
|
||||
# even on failure. Used on the right side of and.
|
||||
#
|
||||
type_guards: {_consider_type_guards? = true, _keep_guarded? = false},
|
||||
# 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: nil
|
||||
}
|
||||
end
|
||||
|
||||
## VARIABLE LIFTING
|
||||
|
||||
@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 """
|
||||
Lifts a single type to its infered type from the context.
|
||||
"""
|
||||
def lift_type(type, context) do
|
||||
context = %{
|
||||
types: context.types,
|
||||
lifted_types: %{},
|
||||
lifted_counter: 0
|
||||
}
|
||||
|
||||
{type, _context} = do_lift_type(type, context)
|
||||
type
|
||||
end
|
||||
|
||||
# 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, n, types}, context) do
|
||||
{types, context} = Enum.map_reduce(types, context, &do_lift_type/2)
|
||||
{{:tuple, n, types}, context}
|
||||
end
|
||||
|
||||
defp do_lift_type({:map, pairs}, context) do
|
||||
{pairs, context} =
|
||||
Enum.map_reduce(pairs, context, fn {kind, key, value}, context ->
|
||||
{key, context} = do_lift_type(key, context)
|
||||
{value, context} = do_lift_type(value, context)
|
||||
{{kind, 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 TO WARNING
|
||||
|
||||
# Collect relevant information from context and traces to report error
|
||||
def error_to_warning(:unable_apply, {mfa, args, expected, signature, stack}, context) do
|
||||
{fun, arity} = context.function
|
||||
line = get_meta(stack.last_expr)[:line]
|
||||
location = {context.file, line, {context.module, fun, arity}}
|
||||
|
||||
traces = type_traces(stack, context)
|
||||
{[signature | args], traces} = lift_all_types([signature | args], traces, context)
|
||||
error = {:unable_apply, mfa, args, expected, signature, {location, stack.last_expr, traces}}
|
||||
{Module.Types, error, location}
|
||||
end
|
||||
|
||||
def error_to_warning(:unable_unify, {left, right, stack}, context) do
|
||||
{fun, arity} = context.function
|
||||
line = get_meta(stack.last_expr)[:line]
|
||||
location = {context.file, line, {context.module, fun, arity}}
|
||||
|
||||
traces = type_traces(stack, context)
|
||||
{[left, right], traces} = lift_all_types([left, right], traces, context)
|
||||
traces = tag_traces(traces, context)
|
||||
|
||||
error = {:unable_unify, left, right, {location, stack.last_expr, traces}}
|
||||
{Module.Types, error, location}
|
||||
end
|
||||
@@ -161,13 +234,14 @@ defmodule Module.Types do
|
||||
# in the stack since we get related variables anyway?
|
||||
stack =
|
||||
stack.unify_stack
|
||||
|> Enum.uniq()
|
||||
|> Enum.flat_map(&[&1 | related_variables(&1, context.types)])
|
||||
|> Enum.uniq()
|
||||
|
||||
Enum.flat_map(stack, fn var_index ->
|
||||
with %{^var_index => traces} <- context.traces,
|
||||
%{^var_index => expr_var} <- context.types_to_vars do
|
||||
Enum.map(traces, &tag_trace(expr_var, &1, context))
|
||||
Enum.map(traces, &{expr_var, &1})
|
||||
else
|
||||
_other -> []
|
||||
end
|
||||
@@ -185,92 +259,56 @@ defmodule Module.Types do
|
||||
end
|
||||
|
||||
# Tag if trace is for a concrete type or type variable
|
||||
defp tag_trace(var, {type, expr, location}, context) do
|
||||
with {:var, var_index} <- type,
|
||||
%{^var_index => expr_var} <- context.types_to_vars do
|
||||
{:var, var, expr_var, expr, location}
|
||||
else
|
||||
_ -> {:type, var, type, expr, location}
|
||||
end
|
||||
end
|
||||
|
||||
defp lift_all_types(types, traces, context) do
|
||||
trace_types = for({:type, _, type, _, _} <- traces, do: type)
|
||||
{types, lift_context} = Unify.lift_types(types, context)
|
||||
{trace_types, _lift_context} = Unify.lift_types(trace_types, lift_context)
|
||||
|
||||
{traces, []} =
|
||||
Enum.map_reduce(traces, trace_types, fn
|
||||
{:type, var, _, expr, location}, [type | acc] -> {{:type, var, type, expr, location}, acc}
|
||||
other, acc -> {other, acc}
|
||||
end)
|
||||
|
||||
{types, traces}
|
||||
defp tag_traces(traces, context) do
|
||||
Enum.flat_map(traces, fn {var, {type, expr, location}} ->
|
||||
with {:var, var_index} <- type,
|
||||
%{^var_index => expr_var} <- context.types_to_vars do
|
||||
[{var, {:var, expr_var, expr, location}}]
|
||||
else
|
||||
_ -> [{var, {:type, type, expr, location}}]
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
## FORMAT WARNINGS
|
||||
|
||||
def format_warning({:unable_apply, mfa, args, expected, signature, {location, expr, traces}}) do
|
||||
{module, function, arity} = mfa
|
||||
mfa_args = Macro.generate_arguments(arity, __MODULE__)
|
||||
{module, function, ^arity} = call_to_mfa(erl_to_ex(module, function, mfa_args, []))
|
||||
format_mfa = Exception.format_mfa(module, function, arity)
|
||||
{traces, [] = _hints} = format_traces(traces, [], false)
|
||||
|
||||
clauses =
|
||||
Enum.map(
|
||||
signature,
|
||||
&String.slice(IO.iodata_to_binary(Unify.format_type({:fun, [&1]}, false)), 1..-2)
|
||||
)
|
||||
|
||||
[
|
||||
"expected #{format_mfa} to have signature:\n\n ",
|
||||
Enum.map_join(args, ", ", &Unify.format_type(&1, false)),
|
||||
" -> #{Unify.format_type(expected, false)}",
|
||||
"\n\nbut it has signature:\n\n ",
|
||||
indent(Enum.join(clauses, "\n")),
|
||||
"\n\n",
|
||||
format_expr(expr, location),
|
||||
traces,
|
||||
"Conflict found at"
|
||||
]
|
||||
end
|
||||
|
||||
def format_warning({:unable_unify, left, right, {location, expr, traces}}) do
|
||||
if map_type?(left) and map_type?(right) and match?({:ok, _, _}, missing_field(left, right)) do
|
||||
{:ok, atom, known_atoms} = missing_field(left, right)
|
||||
cond do
|
||||
map_type?(left) and map_type?(right) and match?({:ok, _, _}, missing_field(left, right)) ->
|
||||
{:ok, atom, known_atoms} = missing_field(left, right)
|
||||
|
||||
# Drop the last trace which is the expression map.foo
|
||||
traces = Enum.drop(traces, 1)
|
||||
{traces, hints} = format_traces(traces, [left, right], true)
|
||||
# Drop the last trace which is the expression map.foo
|
||||
traces = Enum.drop(traces, 1)
|
||||
{traces, hints} = format_traces(traces, true)
|
||||
|
||||
[
|
||||
"undefined field \"#{atom}\" ",
|
||||
format_expr(expr, location),
|
||||
"expected one of the following fields: ",
|
||||
Enum.map_join(Enum.sort(known_atoms), ", ", & &1),
|
||||
"\n\n",
|
||||
traces,
|
||||
format_message_hints(hints),
|
||||
"Conflict found at"
|
||||
]
|
||||
else
|
||||
simplify_left? = simplify_type?(left, right)
|
||||
simplify_right? = simplify_type?(right, left)
|
||||
[
|
||||
"undefined field \"#{atom}\" ",
|
||||
format_expr(expr, location),
|
||||
"expected one of the following fields: ",
|
||||
Enum.map_join(Enum.sort(known_atoms), ", ", & &1),
|
||||
"\n\n",
|
||||
traces,
|
||||
format_message_hints(hints),
|
||||
"Conflict found at"
|
||||
]
|
||||
|
||||
{traces, hints} = format_traces(traces, [left, right], simplify_left? or simplify_right?)
|
||||
true ->
|
||||
simplify_left? = simplify_type?(left, right)
|
||||
simplify_right? = simplify_type?(right, left)
|
||||
|
||||
[
|
||||
"incompatible types:\n\n ",
|
||||
Unify.format_type(left, simplify_left?),
|
||||
" !~ ",
|
||||
Unify.format_type(right, simplify_right?),
|
||||
"\n\n",
|
||||
format_expr(expr, location),
|
||||
traces,
|
||||
format_message_hints(hints),
|
||||
"Conflict found at"
|
||||
]
|
||||
{traces, hints} = format_traces(traces, simplify_left? or simplify_right?)
|
||||
|
||||
[
|
||||
"incompatible types:\n\n ",
|
||||
Unify.format_type(left, simplify_left?),
|
||||
" !~ ",
|
||||
Unify.format_type(right, simplify_right?),
|
||||
"\n\n",
|
||||
format_expr(expr, location),
|
||||
traces,
|
||||
format_message_hints(hints),
|
||||
"Conflict found at"
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -299,17 +337,16 @@ defmodule Module.Types do
|
||||
end
|
||||
end
|
||||
|
||||
defp format_traces([], _types, _simplify?) do
|
||||
defp format_traces([], _simplify?) do
|
||||
{[], []}
|
||||
end
|
||||
|
||||
defp format_traces(traces, types, simplify?) do
|
||||
defp format_traces(traces, simplify?) do
|
||||
traces
|
||||
|> Enum.uniq()
|
||||
|> Enum.reverse()
|
||||
|> Enum.map_reduce([], fn
|
||||
{:type, var, type, expr, location}, hints ->
|
||||
{hint, hints} = format_type_hint(type, types, expr, hints)
|
||||
{var, {:type, type, expr, location}}, hints ->
|
||||
{hint, hints} = format_type_hint(type, expr, hints)
|
||||
|
||||
trace = [
|
||||
"where \"",
|
||||
@@ -326,7 +363,7 @@ defmodule Module.Types do
|
||||
|
||||
{trace, hints}
|
||||
|
||||
{:var, var1, var2, expr, location}, hints ->
|
||||
{var1, {:var, var2, expr, location}}, hints ->
|
||||
trace = [
|
||||
"where \"",
|
||||
Macro.to_string(var1),
|
||||
@@ -354,7 +391,7 @@ defmodule Module.Types do
|
||||
end
|
||||
|
||||
defp simplify_type?(type, other) do
|
||||
map_like_type?(type) and not map_like_type?(other)
|
||||
map_type?(type) and not map_type?(other)
|
||||
end
|
||||
|
||||
## EXPRESSION FORMATTING
|
||||
@@ -397,16 +434,14 @@ defmodule Module.Types do
|
||||
## Hints
|
||||
|
||||
defp format_message_hints(hints) do
|
||||
hints
|
||||
|> Enum.uniq()
|
||||
|> Enum.reverse()
|
||||
|> Enum.map(&[format_message_hint(&1), "\n"])
|
||||
hints |> Enum.uniq() |> Enum.reverse() |> Enum.map(&format_message_hint/1)
|
||||
end
|
||||
|
||||
defp format_message_hint(:inferred_dot) do
|
||||
"""
|
||||
HINT: "var.field" (without parentheses) implies "var" is a map() while \
|
||||
"var.fun()" (with parentheses) implies "var" is an atom()
|
||||
|
||||
"""
|
||||
end
|
||||
|
||||
@@ -416,24 +451,18 @@ defmodule Module.Types do
|
||||
integer() unless said otherwise. For example, <<expr>> assumes "expr" \
|
||||
is an integer. Pass a modifier, such as <<expr::float>> or <<expr::binary>>, \
|
||||
to change the default behaviour.
|
||||
|
||||
"""
|
||||
end
|
||||
|
||||
defp format_message_hint({:sized_and_unsize_tuples, {size, var}}) do
|
||||
"""
|
||||
HINT: use pattern matching or "is_tuple(#{Macro.to_string(var)}) and \
|
||||
tuple_size(#{Macro.to_string(var)}) == #{size}" to guard a sized tuple.
|
||||
"""
|
||||
end
|
||||
|
||||
defp format_type_hint(type, types, expr, hints) do
|
||||
case format_type_hint(type, types, expr) do
|
||||
defp format_type_hint(type, expr, hints) do
|
||||
case format_type_hint(type, expr) do
|
||||
{message, hint} -> {message, [hint | hints]}
|
||||
:error -> {[], hints}
|
||||
end
|
||||
end
|
||||
|
||||
defp format_type_hint(type, types, expr) do
|
||||
defp format_type_hint(type, expr) do
|
||||
cond do
|
||||
dynamic_map_dot?(type, expr) ->
|
||||
{" (due to calling var.field)", :inferred_dot}
|
||||
@@ -444,9 +473,6 @@ defmodule Module.Types do
|
||||
inferred_bitstring_spec?(type, expr) ->
|
||||
{[], :inferred_bitstring_spec}
|
||||
|
||||
message = sized_and_unsize_tuples(expr, types) ->
|
||||
{[], {:sized_and_unsize_tuples, message}}
|
||||
|
||||
true ->
|
||||
:error
|
||||
end
|
||||
@@ -482,33 +508,15 @@ defmodule Module.Types do
|
||||
end
|
||||
end
|
||||
|
||||
defp sized_and_unsize_tuples({{:., _, [:erlang, :is_tuple]}, _, [var]}, types) do
|
||||
case Enum.find(types, &match?({:tuple, _, _}, &1)) do
|
||||
{:tuple, size, _} ->
|
||||
{size, var}
|
||||
|
||||
nil ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp sized_and_unsize_tuples(_expr, _types) do
|
||||
nil
|
||||
end
|
||||
|
||||
## Formatting helpers
|
||||
|
||||
defp indent(string) do
|
||||
String.replace(string, "\n", "\n ")
|
||||
String.replace(string, "\n", " \n")
|
||||
end
|
||||
|
||||
defp map_type?({:map, _}), do: true
|
||||
defp map_type?(_other), do: false
|
||||
|
||||
defp map_like_type?({:map, _}), do: true
|
||||
defp map_like_type?({:union, union}), do: Enum.any?(union, &map_like_type?/1)
|
||||
defp map_like_type?(_other), do: false
|
||||
|
||||
defp atom_type?(:atom), do: true
|
||||
defp atom_type?({:atom, _}), do: false
|
||||
defp atom_type?({:union, union}), do: Enum.all?(union, &atom_type?/1)
|
||||
@@ -516,7 +524,4 @@ defmodule Module.Types do
|
||||
|
||||
defp integer_type?(:integer), do: true
|
||||
defp integer_type?(_other), do: false
|
||||
|
||||
defp call_to_mfa({{:., _, [mod, fun]}, _, args}), do: {mod, fun, length(args)}
|
||||
defp call_to_mfa({fun, _, args}) when is_atom(fun), do: {Kernel, fun, length(args)}
|
||||
end
|
||||
|
||||
@@ -4,57 +4,58 @@ defmodule Module.Types.Expr do
|
||||
alias Module.Types.{Of, Pattern}
|
||||
import Module.Types.{Helpers, Unify}
|
||||
|
||||
def of_expr(expr, expected, %{context: stack_context} = stack, context)
|
||||
when stack_context != :expr do
|
||||
of_expr(expr, expected, %{stack | context: :expr}, context)
|
||||
def of_expr(expr, %{context: stack_context} = stack, context) when stack_context != :expr do
|
||||
of_expr(expr, %{stack | context: :expr}, context)
|
||||
end
|
||||
|
||||
# :atom
|
||||
def of_expr(atom, _expected, _stack, context) when is_atom(atom) do
|
||||
def of_expr(atom, _stack, context) when is_atom(atom) do
|
||||
{:ok, {:atom, atom}, context}
|
||||
end
|
||||
|
||||
# 12
|
||||
def of_expr(literal, _expected, _stack, context) when is_integer(literal) do
|
||||
def of_expr(literal, _stack, context) when is_integer(literal) do
|
||||
{:ok, :integer, context}
|
||||
end
|
||||
|
||||
# 1.2
|
||||
def of_expr(literal, _expected, _stack, context) when is_float(literal) do
|
||||
def of_expr(literal, _stack, context) when is_float(literal) do
|
||||
{:ok, :float, context}
|
||||
end
|
||||
|
||||
# "..."
|
||||
def of_expr(literal, _expected, _stack, context) when is_binary(literal) do
|
||||
def of_expr(literal, _stack, context) when is_binary(literal) do
|
||||
{:ok, :binary, context}
|
||||
end
|
||||
|
||||
# #PID<...>
|
||||
def of_expr(literal, _expected, _stack, context) when is_pid(literal) do
|
||||
def of_expr(literal, _stack, context) when is_pid(literal) do
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
|
||||
# <<...>>>
|
||||
def of_expr({:<<>>, _meta, args}, _expected, stack, context) do
|
||||
case Of.binary(args, stack, context, &of_expr/4) do
|
||||
def of_expr({:<<>>, _meta, args}, stack, context) do
|
||||
result = Of.binary(args, stack, context, &of_expr/3)
|
||||
|
||||
case result do
|
||||
{:ok, context} -> {:ok, :binary, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# left | []
|
||||
def of_expr({:|, _meta, [left_expr, []]} = expr, _expected, stack, context) do
|
||||
def of_expr({:|, _meta, [left_expr, []]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
of_expr(left_expr, :dynamic, stack, context)
|
||||
of_expr(left_expr, stack, context)
|
||||
end
|
||||
|
||||
# left | right
|
||||
def of_expr({:|, _meta, [left_expr, right_expr]} = expr, _expected, stack, context) do
|
||||
def of_expr({:|, _meta, [left_expr, right_expr]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case of_expr(left_expr, :dynamic, stack, context) do
|
||||
case of_expr(left_expr, stack, context) do
|
||||
{:ok, left, context} ->
|
||||
case of_expr(right_expr, :dynamic, stack, context) do
|
||||
case of_expr(right_expr, stack, context) do
|
||||
{:ok, {:list, right}, context} ->
|
||||
{:ok, to_union([left, right], context), context}
|
||||
|
||||
@@ -71,23 +72,22 @@ defmodule Module.Types.Expr do
|
||||
end
|
||||
|
||||
# []
|
||||
def of_expr([], _expected, _stack, context) do
|
||||
def of_expr([], _stack, context) do
|
||||
{:ok, {:list, :dynamic}, context}
|
||||
end
|
||||
|
||||
# [expr, ...]
|
||||
def of_expr(exprs, _expected, stack, context) when is_list(exprs) do
|
||||
def of_expr(exprs, stack, context) when is_list(exprs) do
|
||||
stack = push_expr_stack(exprs, stack)
|
||||
|
||||
case map_reduce_ok(exprs, context, &of_expr(&1, :dynamic, stack, &2)) do
|
||||
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
|
||||
{:ok, types, context} -> {:ok, {:list, to_union(types, context)}, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# __CALLER__
|
||||
def of_expr({:__CALLER__, _meta, var_context}, _expected, _stack, context)
|
||||
when is_atom(var_context) do
|
||||
def of_expr({:__CALLER__, _meta, var_context}, _stack, context) when is_atom(var_context) do
|
||||
struct_pair = {:required, {:atom, :__struct__}, {:atom, Macro.Env}}
|
||||
|
||||
pairs =
|
||||
@@ -99,8 +99,7 @@ defmodule Module.Types.Expr do
|
||||
end
|
||||
|
||||
# __STACKTRACE__
|
||||
def of_expr({:__STACKTRACE__, _meta, var_context}, _expected, _stack, context)
|
||||
when is_atom(var_context) do
|
||||
def of_expr({:__STACKTRACE__, _meta, var_context}, _stack, context) when is_atom(var_context) do
|
||||
file = {:tuple, 2, [{:atom, :file}, {:list, :integer}]}
|
||||
line = {:tuple, 2, [{:atom, :line}, :integer]}
|
||||
file_line = {:list, {:union, [file, line]}}
|
||||
@@ -109,45 +108,41 @@ defmodule Module.Types.Expr do
|
||||
end
|
||||
|
||||
# var
|
||||
def of_expr(var, _expected, _stack, context) when is_var(var) do
|
||||
def of_expr(var, _stack, context) when is_var(var) do
|
||||
{:ok, get_var!(var, context), context}
|
||||
end
|
||||
|
||||
# {left, right}
|
||||
def of_expr({left, right}, expected, stack, context) do
|
||||
of_expr({:{}, [], [left, right]}, expected, stack, context)
|
||||
def of_expr({left, right}, stack, context) do
|
||||
of_expr({:{}, [], [left, right]}, stack, context)
|
||||
end
|
||||
|
||||
# {...}
|
||||
def of_expr({:{}, _meta, exprs} = expr, _expected, stack, context) do
|
||||
def of_expr({:{}, _meta, exprs} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case map_reduce_ok(exprs, context, &of_expr(&1, :dynamic, stack, &2)) do
|
||||
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
|
||||
{:ok, types, context} -> {:ok, {:tuple, length(types), types}, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# left = right
|
||||
def of_expr({:=, _meta, [left_expr, right_expr]} = expr, _expected, stack, context) do
|
||||
# TODO: We might want to bring the expected type forward in case the type of this
|
||||
# pattern is not useful. For example: 1 = _ = expr
|
||||
|
||||
def of_expr({:=, _meta, [left_expr, right_expr]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
with {:ok, left_type, context} <-
|
||||
Pattern.of_pattern(left_expr, stack, context),
|
||||
{:ok, right_type, context} <- of_expr(right_expr, left_type, stack, context),
|
||||
do: unify(right_type, left_type, stack, context)
|
||||
{:ok, right_type, context} <- of_expr(right_expr, stack, context),
|
||||
do: unify(right_type, left_type, %{stack | context: :pattern}, context)
|
||||
end
|
||||
|
||||
# %{map | ...}
|
||||
def of_expr({:%{}, _, [{:|, _, [map, args]}]} = expr, _expected, stack, context) do
|
||||
def of_expr({:%{}, _, [{:|, _, [map, args]}]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
map_type = {:map, [{:optional, :dynamic, :dynamic}]}
|
||||
|
||||
with {:ok, map_type, context} <- of_expr(map, map_type, stack, context),
|
||||
{:ok, {:map, arg_pairs}, context} <- Of.closed_map(args, stack, context, &of_expr/4),
|
||||
with {:ok, map_type, context} <- of_expr(map, stack, context),
|
||||
{:ok, {:map, arg_pairs}, context} <- Of.closed_map(args, stack, context, &of_expr/3),
|
||||
dynamic_value_pairs =
|
||||
Enum.map(arg_pairs, fn {:required, key, _value} -> {:required, key, :dynamic} end),
|
||||
args_type = {:map, dynamic_value_pairs ++ [{:optional, :dynamic, :dynamic}]},
|
||||
@@ -165,68 +160,55 @@ defmodule Module.Types.Expr do
|
||||
end
|
||||
|
||||
# %Struct{map | ...}
|
||||
def of_expr(
|
||||
{:%, meta, [module, {:%{}, _, [{:|, _, [_, _]}]} = update]} = expr,
|
||||
_expected,
|
||||
stack,
|
||||
context
|
||||
) do
|
||||
def of_expr({:%, meta, [module, {:%{}, _, [{:|, _, [_, _]}]} = update]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
map_type = {:map, [{:optional, :dynamic, :dynamic}]}
|
||||
|
||||
with {:ok, struct, context} <- Of.struct(module, meta, context),
|
||||
{:ok, update, context} <- of_expr(update, map_type, stack, context) do
|
||||
{:ok, update, context} <- of_expr(update, stack, context) do
|
||||
unify(update, struct, stack, context)
|
||||
end
|
||||
end
|
||||
|
||||
# %{...}
|
||||
def of_expr({:%{}, _meta, args} = expr, _expected, stack, context) do
|
||||
def of_expr({:%{}, _meta, args} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
Of.closed_map(args, stack, context, &of_expr/4)
|
||||
Of.closed_map(args, stack, context, &of_expr/3)
|
||||
end
|
||||
|
||||
# %Struct{...}
|
||||
def of_expr({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, _expected, stack, context) do
|
||||
def of_expr({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
with {:ok, struct, context} <- Of.struct(module, meta1, context),
|
||||
{:ok, map, context} <- Of.open_map(args, stack, context, &of_expr/4) do
|
||||
{:ok, map, context} <- Of.open_map(args, stack, context, &of_expr/3) do
|
||||
unify(map, struct, stack, context)
|
||||
end
|
||||
end
|
||||
|
||||
# ()
|
||||
def of_expr({:__block__, _meta, []}, _expected, _stack, context) do
|
||||
def of_expr({:__block__, _meta, []}, _stack, context) do
|
||||
{:ok, {:atom, nil}, context}
|
||||
end
|
||||
|
||||
# (expr; expr)
|
||||
def of_expr({:__block__, _meta, exprs}, expected, stack, context) do
|
||||
expected_types = List.duplicate(:dynamic, length(exprs) - 1) ++ [expected]
|
||||
|
||||
result =
|
||||
map_reduce_ok(Enum.zip(exprs, expected_types), context, fn {expr, expected}, context ->
|
||||
of_expr(expr, expected, stack, context)
|
||||
end)
|
||||
|
||||
case result do
|
||||
def of_expr({:__block__, _meta, exprs}, stack, context) do
|
||||
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
|
||||
{:ok, expr_types, context} -> {:ok, Enum.at(expr_types, -1), context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# case expr do pat -> expr end
|
||||
def of_expr({:case, _meta, [case_expr, [{:do, clauses}]]} = expr, _expected, stack, context) do
|
||||
def of_expr({:case, _meta, [case_expr, [{:do, clauses}]]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
with {:ok, _expr_type, context} <- of_expr(case_expr, :dynamic, stack, context),
|
||||
with {:ok, _expr_type, context} <- of_expr(case_expr, stack, context),
|
||||
{:ok, context} <- of_clauses(clauses, stack, context),
|
||||
do: {:ok, :dynamic, context}
|
||||
end
|
||||
|
||||
# fn pat -> expr end
|
||||
def of_expr({:fn, _meta, clauses} = expr, _expected, stack, context) do
|
||||
def of_expr({:fn, _meta, clauses} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case of_clauses(clauses, stack, context) do
|
||||
@@ -239,7 +221,7 @@ defmodule Module.Types.Expr do
|
||||
@try_clause_blocks [:catch, :else, :after]
|
||||
|
||||
# try do expr end
|
||||
def of_expr({:try, _meta, [blocks]} = expr, _expected, stack, context) do
|
||||
def of_expr({:try, _meta, [blocks]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
{result, context} =
|
||||
@@ -249,20 +231,20 @@ defmodule Module.Types.Expr do
|
||||
{:->, _, [[{:in, _, [var, _exceptions]}], body]}, context = acc ->
|
||||
{_type, context} = new_pattern_var(var, context)
|
||||
|
||||
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
|
||||
with {:ok, context} <- of_expr_context(body, stack, context) do
|
||||
{:ok, keep_warnings(acc, context)}
|
||||
end
|
||||
|
||||
{:->, _, [[var], body]}, context = acc ->
|
||||
{_type, context} = new_pattern_var(var, context)
|
||||
|
||||
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
|
||||
with {:ok, context} <- of_expr_context(body, stack, context) do
|
||||
{:ok, keep_warnings(acc, context)}
|
||||
end
|
||||
end)
|
||||
|
||||
{block, body}, context = acc when block in @try_blocks ->
|
||||
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
|
||||
with {:ok, context} <- of_expr_context(body, stack, context) do
|
||||
{:ok, keep_warnings(acc, context)}
|
||||
end
|
||||
|
||||
@@ -277,7 +259,7 @@ defmodule Module.Types.Expr do
|
||||
end
|
||||
|
||||
# receive do pat -> expr end
|
||||
def of_expr({:receive, _meta, [blocks]} = expr, _expected, stack, context) do
|
||||
def of_expr({:receive, _meta, [blocks]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
{result, context} =
|
||||
@@ -289,8 +271,8 @@ defmodule Module.Types.Expr do
|
||||
of_clauses(clauses, stack, context)
|
||||
|
||||
{:after, [{:->, _meta, [head, body]}]}, context = acc ->
|
||||
with {:ok, _type, context} <- of_expr(head, :dynamic, stack, context),
|
||||
{:ok, _type, context} <- of_expr(body, :dynamic, stack, context),
|
||||
with {:ok, _type, context} <- of_expr(head, stack, context),
|
||||
{:ok, _type, context} <- of_expr(body, stack, context),
|
||||
do: {:ok, keep_warnings(acc, context)}
|
||||
end)
|
||||
|
||||
@@ -301,7 +283,7 @@ defmodule Module.Types.Expr do
|
||||
end
|
||||
|
||||
# for pat <- expr do expr end
|
||||
def of_expr({:for, _meta, args} = expr, _expected, stack, context) do
|
||||
def of_expr({:for, _meta, args} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
{clauses, [[{:do, block} | opts]]} = Enum.split(args, -1)
|
||||
|
||||
@@ -312,7 +294,7 @@ defmodule Module.Types.Expr do
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
else
|
||||
with {:ok, _type, context} <- of_expr(block, :dynamic, stack, context) do
|
||||
with {:ok, _type, context} <- of_expr(block, stack, context) do
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
end
|
||||
@@ -320,7 +302,7 @@ defmodule Module.Types.Expr do
|
||||
end
|
||||
|
||||
# with pat <- expr do expr end
|
||||
def of_expr({:with, _meta, clauses} = expr, _expected, stack, context) do
|
||||
def of_expr({:with, _meta, clauses} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case reduce_ok(clauses, context, &with_clause(&1, stack, &2)) do
|
||||
@@ -329,25 +311,29 @@ defmodule Module.Types.Expr do
|
||||
end
|
||||
end
|
||||
|
||||
# fun.(args)
|
||||
def of_expr({{:., _meta1, [fun]}, _meta2, args} = expr, _expected, stack, context) do
|
||||
# TODO: Use expected type to infer intersection return type
|
||||
# fun.(arg)
|
||||
def of_expr({{:., _meta1, [fun]}, _meta2, args} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
with {:ok, _fun_type, context} <- of_expr(fun, :dynamic, stack, context),
|
||||
{:ok, _arg_types, context} <-
|
||||
map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
|
||||
{:ok, :dynamic, context}
|
||||
case of_expr(fun, stack, context) do
|
||||
{:ok, _fun_type, context} ->
|
||||
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
|
||||
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
# expr.key_or_fun
|
||||
def of_expr({{:., _meta1, [expr1, key_or_fun]}, meta2, []} = expr2, _expected, stack, context)
|
||||
def of_expr({{:., _meta1, [expr1, key_or_fun]}, meta2, []} = expr2, stack, context)
|
||||
when not is_atom(expr1) do
|
||||
stack = push_expr_stack(expr2, stack)
|
||||
|
||||
if Keyword.get(meta2, :no_parens, false) do
|
||||
with {:ok, expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
|
||||
with {:ok, expr_type, context} <- of_expr(expr1, stack, context),
|
||||
{value_var, context} = add_var(context),
|
||||
pair_type = {:required, {:atom, key_or_fun}, value_var},
|
||||
optional_type = {:optional, :dynamic, :dynamic},
|
||||
@@ -355,35 +341,28 @@ defmodule Module.Types.Expr do
|
||||
{:ok, _map_type, context} <- unify(map_field_type, expr_type, stack, context),
|
||||
do: {:ok, value_var, context}
|
||||
else
|
||||
# TODO: Use expected type to infer intersection return type
|
||||
with {:ok, expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
|
||||
with {:ok, expr_type, context} <- of_expr(expr1, stack, context),
|
||||
{:ok, _map_type, context} <- unify(expr_type, :atom, stack, context),
|
||||
do: {:ok, :dynamic, context}
|
||||
end
|
||||
end
|
||||
|
||||
# expr.fun(arg)
|
||||
def of_expr({{:., meta1, [expr1, fun]}, _meta2, args} = expr2, _expected, stack, context) do
|
||||
# TODO: Use expected type to infer intersection return type
|
||||
|
||||
def of_expr({{:., meta1, [expr1, fun]}, _meta2, args} = expr2, stack, context) do
|
||||
context = Of.remote(expr1, fun, length(args), meta1, context)
|
||||
stack = push_expr_stack(expr2, stack)
|
||||
|
||||
with {:ok, _expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
|
||||
{:ok, _fun_type, context} <- of_expr(fun, :dynamic, stack, context),
|
||||
{:ok, _arg_types, context} <-
|
||||
map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
|
||||
{:ok, :dynamic, context}
|
||||
with {:ok, _expr_type, context} <- of_expr(expr1, stack, context),
|
||||
{:ok, _fun_type, context} <- of_expr(fun, stack, context) do
|
||||
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
|
||||
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# &Foo.bar/1
|
||||
def of_expr(
|
||||
{:&, meta, [{:/, _, [{{:., _, [module, fun]}, _, []}, arity]}]},
|
||||
_expected,
|
||||
_stack,
|
||||
context
|
||||
)
|
||||
def of_expr({:&, meta, [{:/, _, [{{:., _, [module, fun]}, _, []}, arity]}]}, _stack, context)
|
||||
when is_atom(module) and is_atom(fun) do
|
||||
context = Of.remote(module, fun, arity, meta, context)
|
||||
{:ok, :dynamic, context}
|
||||
@@ -391,19 +370,16 @@ defmodule Module.Types.Expr do
|
||||
|
||||
# &foo/1
|
||||
# & &1
|
||||
def of_expr({:&, _meta, _arg}, _expected, _stack, context) do
|
||||
def of_expr({:&, _meta, _arg}, _stack, context) do
|
||||
# TODO: Function type
|
||||
{:ok, :dynamic, context}
|
||||
end
|
||||
|
||||
# fun(arg)
|
||||
def of_expr({fun, _meta, args} = expr, _expected, stack, context)
|
||||
when is_atom(fun) and is_list(args) do
|
||||
# TODO: Use expected type to infer intersection return type
|
||||
|
||||
def of_expr({fun, _meta, args} = expr, stack, context) when is_atom(fun) and is_list(args) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
|
||||
case map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
|
||||
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
|
||||
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
@@ -412,15 +388,17 @@ defmodule Module.Types.Expr do
|
||||
defp for_clause({:<-, _, [left, expr]}, stack, context) do
|
||||
{pattern, guards} = extract_head([left])
|
||||
|
||||
with {:ok, _pattern_type, context} <- Pattern.of_head([pattern], guards, stack, context),
|
||||
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
|
||||
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
|
||||
# TODO: Check that of_guard/3 returns a boolean
|
||||
{:ok, _guard_type, context} <- Pattern.of_guard(guards_to_or(guards), stack, context),
|
||||
{:ok, _expr_type, context} <- of_expr(expr, stack, context),
|
||||
do: {:ok, context}
|
||||
end
|
||||
|
||||
defp for_clause({:<<>>, _, [{:<-, _, [pattern, expr]}]}, stack, context) do
|
||||
# TODO: the compiler guarantees pattern is a binary but we need to check expr is a binary
|
||||
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
|
||||
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
|
||||
{:ok, _expr_type, context} <- of_expr(expr, stack, context),
|
||||
do: {:ok, context}
|
||||
end
|
||||
|
||||
@@ -429,15 +407,15 @@ defmodule Module.Types.Expr do
|
||||
end
|
||||
|
||||
defp for_clause(expr, stack, context) do
|
||||
of_expr_context(expr, :dynamic, stack, context)
|
||||
of_expr_context(expr, stack, context)
|
||||
end
|
||||
|
||||
defp for_option({:into, expr}, stack, context) do
|
||||
of_expr_context(expr, :dynamic, stack, context)
|
||||
of_expr_context(expr, stack, context)
|
||||
end
|
||||
|
||||
defp for_option({:reduce, expr}, stack, context) do
|
||||
of_expr_context(expr, :dynamic, stack, context)
|
||||
of_expr_context(expr, stack, context)
|
||||
end
|
||||
|
||||
defp for_option({:uniq, _}, _stack, context) do
|
||||
@@ -447,8 +425,10 @@ defmodule Module.Types.Expr do
|
||||
defp with_clause({:<-, _, [left, expr]}, stack, context) do
|
||||
{pattern, guards} = extract_head([left])
|
||||
|
||||
with {:ok, _pattern_type, context} <- Pattern.of_head([pattern], guards, stack, context),
|
||||
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
|
||||
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
|
||||
# TODO: Check that of_guard/3 returns a boolean
|
||||
{:ok, _guard_type, context} <- Pattern.of_guard(guards_to_or(guards), stack, context),
|
||||
{:ok, _expr_type, context} <- of_expr(expr, stack, context),
|
||||
do: {:ok, context}
|
||||
end
|
||||
|
||||
@@ -457,11 +437,11 @@ defmodule Module.Types.Expr do
|
||||
end
|
||||
|
||||
defp with_clause(expr, stack, context) do
|
||||
of_expr_context(expr, :dynamic, stack, context)
|
||||
of_expr_context(expr, stack, context)
|
||||
end
|
||||
|
||||
defp with_option({:do, body}, stack, context) do
|
||||
of_expr_context(body, :dynamic, stack, context)
|
||||
of_expr_context(body, stack, context)
|
||||
end
|
||||
|
||||
defp with_option({:else, clauses}, stack, context) do
|
||||
@@ -469,19 +449,12 @@ defmodule Module.Types.Expr do
|
||||
end
|
||||
|
||||
defp of_clauses(clauses, stack, context) do
|
||||
reduce_ok(clauses, context, fn {:->, meta, [head, body]}, context = acc ->
|
||||
reduce_ok(clauses, context, fn {:->, _meta, [head, body]}, context = acc ->
|
||||
{patterns, guards} = extract_head(head)
|
||||
|
||||
case Pattern.of_head(patterns, guards, stack, context) do
|
||||
{:ok, _, context} ->
|
||||
with {:ok, _expr_type, context} <- of_expr(body, :dynamic, stack, context) do
|
||||
{:ok, keep_warnings(acc, context)}
|
||||
end
|
||||
|
||||
error ->
|
||||
# Skip the clause if it the head has an error
|
||||
if meta[:generated], do: {:ok, acc}, else: error
|
||||
end
|
||||
with {:ok, _, context} <- Pattern.of_head(patterns, guards, stack, context),
|
||||
{:ok, _expr_type, context} <- of_expr(body, stack, context),
|
||||
do: {:ok, keep_warnings(acc, context)}
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -508,8 +481,8 @@ defmodule Module.Types.Expr do
|
||||
[other]
|
||||
end
|
||||
|
||||
defp of_expr_context(expr, expected, stack, context) do
|
||||
case of_expr(expr, expected, stack, context) do
|
||||
defp of_expr_context(expr, stack, context) do
|
||||
case of_expr(expr, stack, context) do
|
||||
{:ok, _type, context} -> {:ok, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
|
||||
@@ -101,30 +101,6 @@ defmodule Module.Types.Helpers do
|
||||
|
||||
defp do_map_reduce_ok([], {list, acc}, _fun), do: {:ok, Enum.reverse(list), acc}
|
||||
|
||||
@doc """
|
||||
Like `Enum.flat_map/2` but only continues while `fun` returns `{:ok, list}`
|
||||
and stops on `{:error, reason}`.
|
||||
"""
|
||||
def flat_map_ok(list, fun) do
|
||||
do_flat_map_ok(list, [], fun)
|
||||
end
|
||||
|
||||
defp do_flat_map_ok([head | tail], acc, fun) do
|
||||
case fun.(head) do
|
||||
{:ok, elem} ->
|
||||
do_flat_map_ok(tail, [elem | acc], fun)
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_flat_map_ok([], acc, _fun), do: {:ok, Enum.reverse(Enum.concat(acc))}
|
||||
|
||||
@doc """
|
||||
Like `Enum.flat_map_reduce/3` but only continues while `fun` returns `{:ok, list, acc}`
|
||||
and stops on `{:error, reason}`.
|
||||
"""
|
||||
def flat_map_reduce_ok(list, acc, fun) do
|
||||
do_flat_map_reduce_ok(list, {[], acc}, fun)
|
||||
end
|
||||
@@ -154,10 +130,6 @@ defmodule Module.Types.Helpers do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Combines a list of guard expressions `when x when y when z` to an expression
|
||||
combined with `or`, `x or y or z`.
|
||||
"""
|
||||
# TODO: Remove this and let multiple when be treated as multiple clauses,
|
||||
# meaning they will be intersection types
|
||||
def guards_to_or([]) do
|
||||
@@ -167,30 +139,4 @@ defmodule Module.Types.Helpers do
|
||||
def guards_to_or(guards) do
|
||||
Enum.reduce(guards, fn guard, acc -> {{:., [], [:erlang, :orelse]}, [], [guard, acc]} end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Like `Enum.zip/1` but will zip multiple lists together instead of only two.
|
||||
"""
|
||||
def zip_many(lists) do
|
||||
zip_many(lists, [], [[]])
|
||||
end
|
||||
|
||||
defp zip_many([], [], [[] | acc]) do
|
||||
map_reverse(acc, [], &Enum.reverse/1)
|
||||
end
|
||||
|
||||
defp zip_many([], remain, [last | acc]) do
|
||||
zip_many(Enum.reverse(remain), [], [[] | [last | acc]])
|
||||
end
|
||||
|
||||
defp zip_many([[] | _], remain, [last | acc]) do
|
||||
zip_many(Enum.reverse(remain), [], [last | acc])
|
||||
end
|
||||
|
||||
defp zip_many([[elem | list1] | list2], remain, [last | acc]) do
|
||||
zip_many(list2, [list1 | remain], [[elem | last] | acc])
|
||||
end
|
||||
|
||||
defp map_reverse([], acc, _fun), do: acc
|
||||
defp map_reverse([head | tail], acc, fun), do: map_reverse(tail, [fun.(head) | acc], fun)
|
||||
end
|
||||
|
||||
@@ -11,44 +11,14 @@ defmodule Module.Types.Of do
|
||||
import Module.Types.Helpers
|
||||
import Module.Types.Unify
|
||||
|
||||
# There are important assumptions on how we work with maps.
|
||||
#
|
||||
# First, the keys in the map must be ordered by subtyping.
|
||||
#
|
||||
# Second, optional keys must be a superset of the required
|
||||
# keys, i.e. %{required(atom) => integer, optional(:foo) => :bar}
|
||||
# is forbidden.
|
||||
#
|
||||
# Third, in order to preserve co/contra-variance, a supertype
|
||||
# must satisfy its subtypes. I.e. %{foo: :bar, atom() => :baz}
|
||||
# is forbidden, it must be %{foo: :bar, atom() => :baz | :bar}.
|
||||
#
|
||||
# Once we support user declared maps, we need to validate these
|
||||
# assumptions.
|
||||
|
||||
@doc """
|
||||
Handles open maps (with dynamic => dynamic).
|
||||
"""
|
||||
def open_map(args, stack, context, of_fun) do
|
||||
with {:ok, pairs, context} <- map_pairs(args, stack, context, of_fun) do
|
||||
# If we match on a map such as %{"foo" => "bar"}, we cannot
|
||||
# assert that %{binary() => binary()}, since we are matching
|
||||
# only a single binary of infinite possible values. Therefore,
|
||||
# the correct would be to match it to %{binary() => binary() | var}.
|
||||
#
|
||||
# We can skip this in two cases:
|
||||
#
|
||||
# 1. If the key is a singleton, then we know that it has no
|
||||
# other value than the current one
|
||||
#
|
||||
# 2. If the value is a variable, then there is no benefit in
|
||||
# creating another variable, so we can skip it
|
||||
#
|
||||
# For now, we skip generating the var itself and introduce
|
||||
# :dynamic instead.
|
||||
def open_map(args, stack, context, fun) do
|
||||
with {:ok, pairs, context} <- map_pairs(args, stack, context, fun) do
|
||||
pairs =
|
||||
for {key, value} <- pairs, not has_unbound_var?(key, context) do
|
||||
if singleton?(key, context) or match?({:var, _}, value) do
|
||||
if singleton?(key, context) do
|
||||
{key, value}
|
||||
else
|
||||
{key, to_union([value, :dynamic], context)}
|
||||
@@ -63,16 +33,16 @@ defmodule Module.Types.Of do
|
||||
@doc """
|
||||
Handles closed maps (without dynamic => dynamic).
|
||||
"""
|
||||
def closed_map(args, stack, context, of_fun) do
|
||||
with {:ok, pairs, context} <- map_pairs(args, stack, context, of_fun) do
|
||||
def closed_map(args, stack, context, fun) do
|
||||
with {:ok, pairs, context} <- map_pairs(args, stack, context, fun) do
|
||||
{:ok, {:map, closed_to_unions(pairs, context)}, context}
|
||||
end
|
||||
end
|
||||
|
||||
defp map_pairs(pairs, stack, context, of_fun) do
|
||||
defp map_pairs(pairs, stack, context, fun) do
|
||||
map_reduce_ok(pairs, context, fn {key, value}, context ->
|
||||
with {:ok, key_type, context} <- of_fun.(key, :dynamic, stack, context),
|
||||
{:ok, value_type, context} <- of_fun.(value, :dynamic, stack, context),
|
||||
with {:ok, key_type, context} <- fun.(key, stack, context),
|
||||
{:ok, value_type, context} <- fun.(value, stack, context),
|
||||
do: {:ok, {key_type, value_type}, context}
|
||||
end)
|
||||
end
|
||||
@@ -143,39 +113,39 @@ defmodule Module.Types.Of do
|
||||
In the stack, we add nodes such as <<expr>>, <<..., expr>>, etc,
|
||||
based on the position of the expression within the binary.
|
||||
"""
|
||||
def binary([], _stack, context, _of_fun) do
|
||||
def binary([], _stack, context, _fun) do
|
||||
{:ok, context}
|
||||
end
|
||||
|
||||
def binary([head], stack, context, of_fun) do
|
||||
def binary([head], stack, context, fun) do
|
||||
head_stack = push_expr_stack({:<<>>, get_meta(head), [head]}, stack)
|
||||
binary_segment(head, head_stack, context, of_fun)
|
||||
binary_segment(head, head_stack, context, fun)
|
||||
end
|
||||
|
||||
def binary([head | tail], stack, context, of_fun) do
|
||||
def binary([head | tail], stack, context, fun) do
|
||||
head_stack = push_expr_stack({:<<>>, get_meta(head), [head, @suffix]}, stack)
|
||||
|
||||
case binary_segment(head, head_stack, context, of_fun) do
|
||||
{:ok, context} -> binary_many(tail, stack, context, of_fun)
|
||||
case binary_segment(head, head_stack, context, fun) do
|
||||
{:ok, context} -> binary_many(tail, stack, context, fun)
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp binary_many([last], stack, context, of_fun) do
|
||||
defp binary_many([last], stack, context, fun) do
|
||||
last_stack = push_expr_stack({:<<>>, get_meta(last), [@prefix, last]}, stack)
|
||||
binary_segment(last, last_stack, context, of_fun)
|
||||
binary_segment(last, last_stack, context, fun)
|
||||
end
|
||||
|
||||
defp binary_many([head | tail], stack, context, of_fun) do
|
||||
defp binary_many([head | tail], stack, context, fun) do
|
||||
head_stack = push_expr_stack({:<<>>, get_meta(head), [@prefix, head, @suffix]}, stack)
|
||||
|
||||
case binary_segment(head, head_stack, context, of_fun) do
|
||||
{:ok, context} -> binary_many(tail, stack, context, of_fun)
|
||||
case binary_segment(head, head_stack, context, fun) do
|
||||
{:ok, context} -> binary_many(tail, stack, context, fun)
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp binary_segment({:"::", _meta, [expr, specifiers]}, stack, context, of_fun) do
|
||||
defp binary_segment({:"::", _meta, [expr, specifiers]}, stack, context, fun) do
|
||||
expected_type =
|
||||
collect_binary_specifier(specifiers, &binary_type(stack.context, &1)) || :integer
|
||||
|
||||
@@ -192,7 +162,7 @@ defmodule Module.Types.Of do
|
||||
{:ok, context}
|
||||
|
||||
true ->
|
||||
with {:ok, type, context} <- of_fun.(expr, expected_type, stack, context),
|
||||
with {:ok, type, context} <- fun.(expr, stack, context),
|
||||
{:ok, _type, context} <- unify(type, expected_type, stack, context),
|
||||
do: {:ok, context}
|
||||
end
|
||||
@@ -249,12 +219,12 @@ defmodule Module.Types.Of do
|
||||
|
||||
defp check_export(module, fun, arity, meta, context) do
|
||||
case ParallelChecker.fetch_export(context.cache, module, fun, arity) do
|
||||
{:ok, mode, :def, reason} ->
|
||||
check_deprecated(mode, module, fun, arity, reason, meta, context)
|
||||
{:ok, :def, reason} ->
|
||||
check_deprecated(module, fun, arity, reason, meta, context)
|
||||
|
||||
{:ok, mode, :defmacro, reason} ->
|
||||
{:ok, :defmacro, reason} ->
|
||||
context = warn(meta, context, {:unrequired_module, module, fun, arity})
|
||||
check_deprecated(mode, module, fun, arity, reason, meta, context)
|
||||
check_deprecated(module, fun, arity, reason, meta, context)
|
||||
|
||||
{:error, :module} ->
|
||||
if warn_undefined?(module, fun, arity, context) do
|
||||
@@ -273,7 +243,7 @@ defmodule Module.Types.Of do
|
||||
end
|
||||
end
|
||||
|
||||
defp check_deprecated(:elixir, module, fun, arity, reason, meta, context) do
|
||||
defp check_deprecated(module, fun, arity, reason, meta, context) do
|
||||
if reason do
|
||||
warn(meta, context, {:deprecated, module, fun, arity, reason})
|
||||
else
|
||||
@@ -281,22 +251,6 @@ defmodule Module.Types.Of do
|
||||
end
|
||||
end
|
||||
|
||||
defp check_deprecated(:erlang, module, fun, arity, _reason, meta, context) do
|
||||
case :otp_internal.obsolete(module, fun, arity) do
|
||||
{:deprecated, string} when is_list(string) ->
|
||||
reason = string |> List.to_string() |> String.capitalize()
|
||||
warn(meta, context, {:deprecated, module, fun, arity, reason})
|
||||
|
||||
{:deprecated, string, removal} when is_list(string) and is_list(removal) ->
|
||||
reason = string |> List.to_string() |> String.capitalize()
|
||||
reason = "It will be removed in #{removal}. #{reason}"
|
||||
warn(meta, context, {:deprecated, module, fun, arity, reason})
|
||||
|
||||
_ ->
|
||||
context
|
||||
end
|
||||
end
|
||||
|
||||
# The protocol code dispatches to unknown modules, so we ignore them here.
|
||||
#
|
||||
# try do
|
||||
@@ -327,7 +281,7 @@ defmodule Module.Types.Of do
|
||||
%{context | warnings: [{__MODULE__, warning, location} | context.warnings]}
|
||||
end
|
||||
|
||||
## Warning formatting
|
||||
## Warning formating
|
||||
|
||||
def format_warning({:undefined_module, module, fun, arity}) do
|
||||
[
|
||||
|
||||
@@ -10,8 +10,8 @@ defmodule Module.Types.Pattern do
|
||||
def of_head(patterns, guards, stack, context) do
|
||||
with {:ok, types, context} <-
|
||||
map_reduce_ok(patterns, context, &of_pattern(&1, stack, &2)),
|
||||
# TODO: Check that of_guard/4 returns boolean() | :fail
|
||||
{:ok, _, context} <- of_guard(guards_to_or(guards), :dynamic, stack, context),
|
||||
# TODO: Check that of_guard/3 returns boolean() | :fail
|
||||
{:ok, _, context} <- of_guard(guards_to_or(guards), stack, context),
|
||||
do: {:ok, types, context}
|
||||
end
|
||||
|
||||
@@ -57,9 +57,8 @@ defmodule Module.Types.Pattern do
|
||||
)
|
||||
when is_atom(var_context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
expected_fun = fn arg, _expected, stack, context -> of_pattern(arg, stack, context) end
|
||||
|
||||
with {:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, expected_fun) do
|
||||
with {:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, &of_pattern/3) do
|
||||
{:ok, {:map, [{:required, {:atom, :__struct__}, :atom} | pairs]}, context}
|
||||
end
|
||||
end
|
||||
@@ -68,11 +67,10 @@ defmodule Module.Types.Pattern do
|
||||
def of_pattern({:%, _meta1, [var, {:%{}, _meta2, args}]} = expr, stack, context)
|
||||
when not is_atom(var) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
expected_fun = fn arg, _expected, stack, context -> of_pattern(arg, stack, context) end
|
||||
|
||||
with {:ok, var_type, context} = of_pattern(var, stack, context),
|
||||
{:ok, _, context} <- unify(var_type, :atom, stack, context),
|
||||
{:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, expected_fun) do
|
||||
{:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, &of_pattern/3) do
|
||||
{:ok, {:map, [{:required, {:atom, :__struct__}, var_type} | pairs]}, context}
|
||||
end
|
||||
end
|
||||
@@ -81,80 +79,78 @@ defmodule Module.Types.Pattern do
|
||||
of_shared(expr, stack, context, &of_pattern/3)
|
||||
end
|
||||
|
||||
def unify_kinds(:required, _), do: :required
|
||||
def unify_kinds(_, :required), do: :required
|
||||
def unify_kinds(:optional, :optional), do: :optional
|
||||
|
||||
## GUARDS
|
||||
|
||||
# TODO: Some guards can be changed to intersection types or higher order types
|
||||
@boolean {:union, [{:atom, true}, {:atom, false}]}
|
||||
@number {:union, [:integer, :float]}
|
||||
@unary_number_fun [{[:integer], :integer}, {[@number], :float}]
|
||||
@binary_number_fun [
|
||||
{[:integer, :integer], :integer},
|
||||
{[:float, @number], :float},
|
||||
{[@number, :float], :float}
|
||||
]
|
||||
|
||||
@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, [{:optional, :dynamic, :dynamic}]}], @boolean}],
|
||||
{:is_map_key, 2} => [{[:dynamic, {:map, [{:optional, :dynamic, :dynamic}]}], :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} => @binary_number_fun,
|
||||
{:+, 1} => @unary_number_fun,
|
||||
{:+, 2} => @binary_number_fun,
|
||||
{:-, 1} => @unary_number_fun,
|
||||
{:-, 2} => @binary_number_fun,
|
||||
{:/, 2} => @binary_number_fun,
|
||||
{:abs, 1} => @unary_number_fun,
|
||||
{: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, [{:optional, :dynamic, :dynamic}]}], :dynamic}],
|
||||
{:map_size, 1} => [{[{:map, [{:optional, :dynamic, :dynamic}]}], :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}],
|
||||
{:or, 2} => [{[@boolean, @boolean], @boolean}],
|
||||
{:and, 2} => [{[@boolean, @boolean], @boolean}],
|
||||
{:xor, 2} => [{[@boolean, @boolean], @boolean}],
|
||||
{:not, 1} => [{[@boolean], @boolean}]
|
||||
{: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, [{:optional, :dynamic, :dynamic}]}], @boolean},
|
||||
{:is_map_key, 2} => {[:dynamic, {:map, [{:optional, :dynamic, :dynamic}]}], :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, [{:optional, :dynamic, :dynamic}]}], :dynamic},
|
||||
{:map_size, 1} => {[{:map, [{:optional, :dynamic, :dynamic}]}], :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},
|
||||
{:or, 2} => {[@boolean, @boolean], @boolean},
|
||||
{:and, 2} => {[@boolean, @boolean], @boolean},
|
||||
{:xor, 2} => {[@boolean, @boolean], @boolean},
|
||||
{:not, 1} => {[@boolean], @boolean}
|
||||
|
||||
# Following guards are matched explicitly to handle
|
||||
# type guard functions such as is_atom/1
|
||||
@@ -169,6 +165,7 @@ defmodule Module.Types.Pattern do
|
||||
:is_boolean,
|
||||
:is_float,
|
||||
:is_function,
|
||||
:is_function,
|
||||
:is_integer,
|
||||
:is_list,
|
||||
:is_map,
|
||||
@@ -183,36 +180,29 @@ defmodule Module.Types.Pattern do
|
||||
Refines the type variables in the typing context using type check guards
|
||||
such as `is_integer/1`.
|
||||
"""
|
||||
def of_guard(expr, expected, %{context: stack_context} = stack, context)
|
||||
when stack_context != :pattern do
|
||||
of_guard(expr, expected, %{stack | context: :pattern}, context)
|
||||
def of_guard(expr, %{context: stack_context} = stack, context) when stack_context != :pattern do
|
||||
of_guard(expr, %{stack | context: :pattern}, context)
|
||||
end
|
||||
|
||||
def of_guard({{:., _, [:erlang, :andalso]}, _, [left, right]} = expr, _expected, stack, context) do
|
||||
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, context} <- of_guard(left, @boolean, stack, 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, right_type, context} <- of_guard(right, :dynamic, keep_guarded(stack), context),
|
||||
do: {:ok, to_union([@boolean, right_type], context), context}
|
||||
{:ok, _, context} <- unify(right_type, @boolean, stack, context),
|
||||
do: {:ok, @boolean, context}
|
||||
end
|
||||
|
||||
def of_guard({{:., _, [:erlang, :orelse]}, _, [left, right]} = expr, _expected, stack, context) do
|
||||
def of_guard({{:., _, [:erlang, :orelse]}, _, [left, right]} = expr, stack, context) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
left_indexes = collect_var_indexes_from_expr(left, context)
|
||||
right_indexes = collect_var_indexes_from_expr(right, context)
|
||||
fresh_context = fresh_context(context)
|
||||
|
||||
with {:ok, left_type, left_context} <- of_guard(left, @boolean, stack, context),
|
||||
{:ok, _right_type, right_context} <- of_guard(right, :dynamic, stack, context),
|
||||
context =
|
||||
merge_context_or(
|
||||
left_indexes,
|
||||
right_indexes,
|
||||
context,
|
||||
stack,
|
||||
left_context,
|
||||
right_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
|
||||
@@ -220,402 +210,218 @@ defmodule Module.Types.Pattern do
|
||||
# The unary operators + and - are special cased to avoid common warnings until
|
||||
# we add support for intersection types for the guard functions
|
||||
# -integer / +integer
|
||||
def of_guard({{:., _, [:erlang, guard]}, _, [integer]}, _expected, _stack, context)
|
||||
def of_guard({{:., _, [:erlang, guard]}, _, [integer]}, _stack, context)
|
||||
when guard in [:+, :-] and is_integer(integer) do
|
||||
{:ok, :integer, context}
|
||||
end
|
||||
|
||||
# -float / +float
|
||||
def of_guard({{:., _, [:erlang, guard]}, _, [float]}, _expected, _stack, context)
|
||||
def of_guard({{:., _, [:erlang, guard]}, _, [float]}, _stack, context)
|
||||
when guard in [:+, :-] and is_float(float) do
|
||||
{:ok, :float, context}
|
||||
end
|
||||
|
||||
# tuple_size(arg) == integer
|
||||
def of_guard(
|
||||
{{:., _, [:erlang, :==]}, _, [{{:., _, [:erlang, :tuple_size]}, _, [var]}, size]} = expr,
|
||||
expected,
|
||||
stack,
|
||||
context
|
||||
)
|
||||
when is_var(var) and is_integer(size) do
|
||||
of_tuple_size(var, size, expr, expected, stack, context)
|
||||
end
|
||||
|
||||
# integer == tuple_size(arg)
|
||||
def of_guard(
|
||||
{{:., _, [:erlang, :==]}, _, [size, {{:., _, [:erlang, :tuple_size]}, _, [var]}]} = expr,
|
||||
expected,
|
||||
stack,
|
||||
context
|
||||
)
|
||||
when is_var(var) and is_integer(size) do
|
||||
of_tuple_size(var, size, expr, expected, stack, context)
|
||||
end
|
||||
|
||||
# fun(args)
|
||||
def of_guard({{:., _, [:erlang, guard]}, _, args} = expr, expected, stack, context) do
|
||||
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)
|
||||
{consider_type_guards?, keep_guarded?} = stack.type_guards
|
||||
signature = guard_signature(guard, length(args))
|
||||
|
||||
# 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 consider_type_guards? do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
expected_clauses = filter_clauses(signature, expected, stack, context)
|
||||
param_unions = signature_to_param_unions(expected_clauses, context)
|
||||
arg_stack = %{stack | type_guards: {false, keep_guarded?}}
|
||||
mfa = {:erlang, guard, length(args)}
|
||||
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(Enum.zip(args, param_unions), context, fn {arg, param}, context ->
|
||||
of_guard(arg, param, arg_stack, context)
|
||||
end),
|
||||
{:ok, return_type, context} <-
|
||||
unify_call(
|
||||
arg_types,
|
||||
expected_clauses,
|
||||
expected,
|
||||
mfa,
|
||||
signature,
|
||||
stack,
|
||||
context,
|
||||
type_guard?
|
||||
) do
|
||||
guard_sources = guard_sources(arg_types, type_guard?, keep_guarded?, context)
|
||||
map_reduce_ok(args, context, &of_guard(&1, arg_stack, &2)),
|
||||
{:ok, context} <- unify_call(arg_types, param_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
|
||||
# Assume that type guards always return boolean
|
||||
boolean = {:union, [atom: true, atom: false]}
|
||||
[{_params, ^boolean}] = signature
|
||||
{:ok, boolean, context}
|
||||
{:ok, return_type, context}
|
||||
end
|
||||
end
|
||||
|
||||
# map.field
|
||||
def of_guard({{:., meta1, [map, field]}, meta2, []}, expected, stack, context) do
|
||||
of_guard({{:., meta1, [:erlang, :map_get]}, meta2, [field, map]}, expected, stack, context)
|
||||
def of_guard({{:., meta1, [map, field]}, meta2, []}, stack, context) do
|
||||
of_guard({{:., meta1, [:erlang, :map_get]}, meta2, [field, map]}, stack, context)
|
||||
end
|
||||
|
||||
# var
|
||||
def of_guard(var, _expected, _stack, context) when is_var(var) do
|
||||
{:ok, get_var!(var, context), context}
|
||||
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, _expected, stack, context) do
|
||||
of_shared(expr, stack, context, &of_guard(&1, :dynamic, &2, &3))
|
||||
def of_guard(expr, stack, context) do
|
||||
of_shared(expr, stack, context, &of_guard/3)
|
||||
end
|
||||
|
||||
defp of_tuple_size(var, size, expr, _expected, stack, context) do
|
||||
{consider_type_guards?, _keep_guarded?} = stack.type_guards
|
||||
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
|
||||
|
||||
result =
|
||||
if consider_type_guards? do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
tuple_elems = Enum.map(1..size//1, fn _ -> :dynamic end)
|
||||
defp unify_call(args, params, stack, context) do
|
||||
reduce_ok(Enum.zip(args, params), context, fn {arg, param}, context ->
|
||||
case unify(arg, param, stack, context) do
|
||||
{:ok, _, context} -> {:ok, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
with {:ok, type, context} <- of_guard(var, :dynamic, stack, context),
|
||||
{:ok, _type, context} <- unify({:tuple, size, tuple_elems}, type, stack, context),
|
||||
do: {:ok, context}
|
||||
else
|
||||
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}
|
||||
end
|
||||
|
||||
case result do
|
||||
{:ok, context} ->
|
||||
boolean = {:union, [atom: true, atom: false]}
|
||||
{:ok, boolean, 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
|
||||
|
||||
defp signature_to_param_unions(signature, context) do
|
||||
signature
|
||||
|> Enum.map(fn {params, _return} -> params end)
|
||||
|> zip_many()
|
||||
|> Enum.map(&to_union(&1, context))
|
||||
end
|
||||
|
||||
# Collect guard sources from argument types, see type context documentation
|
||||
# for more information
|
||||
defp guard_sources(arg_types, type_guard?, keep_guarded?, context) do
|
||||
{arg_types, guard_sources} =
|
||||
case arg_types do
|
||||
[{:var, index} | rest_arg_types] when type_guard? ->
|
||||
guard_sources = Map.put_new(context.guard_sources, index, :guarded)
|
||||
{rest_arg_types, guard_sources}
|
||||
|
||||
_ ->
|
||||
{arg_types, context.guard_sources}
|
||||
end
|
||||
|
||||
Enum.reduce(arg_types, guard_sources, fn
|
||||
{:var, index}, guard_sources ->
|
||||
Map.update(guard_sources, index, :fail, &guarded_if_keep_guarded(&1, keep_guarded?))
|
||||
|
||||
_, guard_sources ->
|
||||
guard_sources
|
||||
end)
|
||||
end
|
||||
|
||||
defp collect_var_indexes_from_expr(expr, context) do
|
||||
{_, vars} =
|
||||
Macro.prewalk(expr, %{}, fn
|
||||
var, acc when is_var(var) ->
|
||||
var_name = var_name(var)
|
||||
%{^var_name => type} = context.vars
|
||||
{var, collect_var_indexes(type, context, acc)}
|
||||
|
||||
other, acc ->
|
||||
{other, acc}
|
||||
end)
|
||||
|
||||
Map.keys(vars)
|
||||
end
|
||||
|
||||
defp unify_call(args, clauses, _expected, _mfa, _signature, stack, context, true = _type_guard?) do
|
||||
unify_type_guard_call(args, clauses, stack, context)
|
||||
end
|
||||
|
||||
defp unify_call(args, clauses, expected, mfa, signature, stack, context, false = _type_guard?) do
|
||||
unify_call(args, clauses, expected, mfa, signature, stack, context)
|
||||
end
|
||||
|
||||
defp unify_call([], [{[], return}], _expected, _mfa, _signature, _stack, context) do
|
||||
{:ok, return, context}
|
||||
end
|
||||
|
||||
defp unify_call(args, clauses, expected, mfa, signature, stack, context) do
|
||||
# Given the arguments:
|
||||
# foo | bar, {:ok, baz | bat}
|
||||
|
||||
# Expand unions in arguments:
|
||||
# foo | bar, {:ok, baz} | {:ok, bat}
|
||||
|
||||
# Permute arguments:
|
||||
# foo, {:ok, baz}
|
||||
# foo, {:ok, bat}
|
||||
# bar, {:ok, baz}
|
||||
# bar, {:ok, bat}
|
||||
|
||||
expanded_args =
|
||||
args
|
||||
|> Enum.map(&flatten_union(&1, context))
|
||||
|> cartesian_product()
|
||||
|
||||
# Remove clauses that do not match the expected type
|
||||
# Ignore type variables in parameters by changing them to dynamic
|
||||
|
||||
clauses =
|
||||
clauses
|
||||
|> filter_clauses(expected, stack, context)
|
||||
|> Enum.map(fn {params, return} ->
|
||||
{Enum.map(params, &var_to_dynamic/1), return}
|
||||
end)
|
||||
|
||||
# For each permuted argument find the clauses they match
|
||||
# All arguments must match at least one clause, but all clauses
|
||||
# do not need to match
|
||||
# Collect the return values from clauses that matched and collect
|
||||
# the type contexts from unifying argument and parameter to
|
||||
# infer type variables in arguments
|
||||
result =
|
||||
flat_map_ok(expanded_args, fn expanded_args ->
|
||||
result =
|
||||
Enum.flat_map(clauses, fn {params, return} ->
|
||||
result =
|
||||
map_ok(Enum.zip(expanded_args, params), fn {arg, param} ->
|
||||
case unify(arg, param, stack, context) do
|
||||
{:ok, _type, context} -> {:ok, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end)
|
||||
|
||||
case result do
|
||||
{:ok, contexts} -> [{return, contexts}]
|
||||
{:error, _reason} -> []
|
||||
end
|
||||
end)
|
||||
|
||||
if result != [] do
|
||||
{:ok, result}
|
||||
else
|
||||
{:error, args}
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end)
|
||||
|
||||
case result do
|
||||
{:ok, returns_contexts} ->
|
||||
{success_returns, contexts} = Enum.unzip(returns_contexts)
|
||||
contexts = Enum.concat(contexts)
|
||||
indexes = Enum.uniq(Enum.flat_map(args, &collect_var_indexes_from_type/1))
|
||||
|
||||
# Build unions from collected type contexts to unify with
|
||||
# type variables from arguments
|
||||
result =
|
||||
map_reduce_ok(indexes, context, fn index, context ->
|
||||
union =
|
||||
contexts
|
||||
|> Enum.map(&Map.fetch!(&1.types, index))
|
||||
|> Enum.reject(&(&1 == :unbound))
|
||||
|
||||
if union == [] do
|
||||
{:ok, {:var, index}, context}
|
||||
else
|
||||
unify({:var, index}, to_union(union, context), stack, context)
|
||||
end
|
||||
end)
|
||||
|
||||
case result do
|
||||
{:ok, _types, context} -> {:ok, to_union(success_returns, context), context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
|
||||
{:error, args} ->
|
||||
error(:unable_apply, {mfa, args, expected, signature, stack}, context)
|
||||
end
|
||||
end
|
||||
|
||||
defp unify_type_guard_call(args, [{params, return}], stack, context) do
|
||||
result =
|
||||
reduce_ok(Enum.zip(args, params), context, fn {arg, param}, context ->
|
||||
case unify(arg, param, stack, context) do
|
||||
{:ok, _, context} -> {:ok, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end)
|
||||
|
||||
case result do
|
||||
{:ok, context} -> {:ok, return, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp cartesian_product(lists) do
|
||||
List.foldr(lists, [[]], fn list, acc ->
|
||||
for elem_list <- list,
|
||||
list_acc <- acc,
|
||||
do: [elem_list | list_acc]
|
||||
end)
|
||||
end
|
||||
|
||||
defp var_to_dynamic(type) do
|
||||
{type, _acc} =
|
||||
walk(type, :ok, fn
|
||||
{:var, _index}, :ok ->
|
||||
{:dynamic, :ok}
|
||||
|
||||
other, :ok ->
|
||||
{other, :ok}
|
||||
end)
|
||||
|
||||
type
|
||||
end
|
||||
|
||||
defp collect_var_indexes_from_type(type) do
|
||||
{_type, indexes} =
|
||||
walk(type, [], fn
|
||||
{:var, index}, indexes ->
|
||||
{{:var, index}, [index | indexes]}
|
||||
|
||||
other, indexes ->
|
||||
{other, indexes}
|
||||
end)
|
||||
|
||||
indexes
|
||||
end
|
||||
|
||||
defp merge_context_or(left_indexes, right_indexes, context, stack, left, right) do
|
||||
left_different = filter_different_indexes(left_indexes, left, right)
|
||||
right_different = filter_different_indexes(right_indexes, left, right)
|
||||
|
||||
case {left_different, right_different} do
|
||||
{[index], [index]} -> merge_context_or_equal(index, stack, left, right)
|
||||
{_, _} -> merge_context_or_diff(left_different, context, left)
|
||||
end
|
||||
end
|
||||
|
||||
defp filter_different_indexes(indexes, left, right) do
|
||||
Enum.filter(indexes, fn index ->
|
||||
%{^index => left_type} = left.types
|
||||
%{^index => right_type} = right.types
|
||||
left_type != right_type
|
||||
end)
|
||||
end
|
||||
|
||||
defp merge_context_or_equal(index, stack, left, right) do
|
||||
%{^index => left_type} = left.types
|
||||
%{^index => right_type} = right.types
|
||||
|
||||
cond do
|
||||
left_type == :unbound ->
|
||||
refine_var!(index, right_type, stack, left)
|
||||
|
||||
right_type == :unbound ->
|
||||
left
|
||||
|
||||
true ->
|
||||
# Only include right side if left side is from type guard such as is_list(x),
|
||||
# do not refine in case of length(x)
|
||||
if left.guard_sources[index] == :fail do
|
||||
guard_sources = Map.put(left.guard_sources, index, :fail)
|
||||
left = %{left | guard_sources: guard_sources}
|
||||
refine_var!(index, left_type, stack, left)
|
||||
else
|
||||
guard_sources = merge_guard_sources([left.guard_sources, right.guard_sources])
|
||||
left = %{left | guard_sources: guard_sources}
|
||||
refine_var!(index, to_union([left_type, right_type], left), stack, left)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# If the variable failed, we can keep them from the left side as is.
|
||||
# If they didn't fail, then we need to restore them to their original value.
|
||||
defp merge_context_or_diff(indexes, old_context, new_context) do
|
||||
Enum.reduce(indexes, new_context, fn index, context ->
|
||||
if new_context.guard_sources[index] == :fail do
|
||||
context
|
||||
else
|
||||
restore_var!(index, new_context, old_context)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp merge_guard_sources(sources) do
|
||||
Enum.reduce(sources, fn left, right ->
|
||||
Map.merge(left, right, fn
|
||||
_index, :guarded, :guarded -> :guarded
|
||||
_index, _, _ -> :fail
|
||||
end)
|
||||
Map.merge(left, right, fn _index, left, right -> join_guard_source(left, right) end)
|
||||
end)
|
||||
end
|
||||
|
||||
defp guarded_if_keep_guarded(:guarded, true), do: :guarded
|
||||
defp guarded_if_keep_guarded(_, _), do: :fail
|
||||
defp join_guard_source(left, right) do
|
||||
sources = left ++ right
|
||||
|
||||
defp keep_guarded(%{type_guards: {consider?, _}} = stack),
|
||||
do: %{stack | type_guards: {consider?, true}}
|
||||
cond do
|
||||
:fail in sources -> [:fail]
|
||||
:guarded in sources -> [:guarded]
|
||||
true -> []
|
||||
end
|
||||
end
|
||||
|
||||
defp filter_clauses(signature, expected, stack, context) do
|
||||
Enum.filter(signature, fn {_params, return} ->
|
||||
match?({:ok, _type, _context}, unify(return, expected, stack, context))
|
||||
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]
|
||||
else
|
||||
join_guard_source(left, right)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
Enum.each(@guard_functions, fn {{name, arity}, signature} ->
|
||||
defp guard_signature(unquote(name), unquote(arity)), do: unquote(Macro.escape(signature))
|
||||
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
|
||||
)
|
||||
|
||||
Enum.each(@type_guards, fn name ->
|
||||
defp type_guard?(unquote(name)), do: true
|
||||
end)
|
||||
%{context | traces: traces}
|
||||
end
|
||||
|
||||
defp type_guard?(name) when is_atom(name), do: false
|
||||
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
|
||||
|
||||
defp guard_signature(name, arity) do
|
||||
Map.fetch!(@guard_functions, {name, arity})
|
||||
end
|
||||
|
||||
defp type_guard?(name) do
|
||||
name in @type_guards
|
||||
end
|
||||
|
||||
## Shared
|
||||
|
||||
@@ -641,9 +447,7 @@ defmodule Module.Types.Pattern do
|
||||
|
||||
# <<...>>>
|
||||
defp of_shared({:<<>>, _meta, args}, stack, context, fun) do
|
||||
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
|
||||
|
||||
case Of.binary(args, stack, context, expected_fun) do
|
||||
case Of.binary(args, stack, context, fun) do
|
||||
{:ok, context} -> {:ok, :binary, context}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
@@ -737,18 +541,16 @@ defmodule Module.Types.Pattern do
|
||||
# %{...}
|
||||
defp of_shared({:%{}, _meta, args} = expr, stack, context, fun) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
|
||||
Of.open_map(args, stack, context, expected_fun)
|
||||
Of.open_map(args, stack, context, fun)
|
||||
end
|
||||
|
||||
# %Struct{...}
|
||||
defp of_shared({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, stack, context, fun)
|
||||
when is_atom(module) do
|
||||
stack = push_expr_stack(expr, stack)
|
||||
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
|
||||
|
||||
with {:ok, struct, context} <- Of.struct(module, meta1, context),
|
||||
{:ok, map, context} <- Of.open_map(args, stack, context, expected_fun) do
|
||||
{:ok, map, context} <- Of.open_map(args, stack, context, fun) do
|
||||
unify(map, struct, stack, context)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,7 +10,6 @@ defmodule Module.Types.Unify do
|
||||
# {:atom, atom} < :atom
|
||||
# :integer
|
||||
# :float
|
||||
# :binary
|
||||
# :pid
|
||||
# :port
|
||||
# :reference
|
||||
@@ -21,37 +20,65 @@ defmodule Module.Types.Unify do
|
||||
# {:tuple, size, [type]} < :tuple
|
||||
# {:union, [type]}
|
||||
# {:map, [{:required | :optional, key_type, value_type}]}
|
||||
# {:fun, [{params, return}]}
|
||||
#
|
||||
# Once new types are added, they should be considered in:
|
||||
#
|
||||
# * unify (all)
|
||||
# * format_type (all)
|
||||
# * subtype? (subtypes only)
|
||||
# * has_unbound_var? (composite only)
|
||||
# * recursive_type? (composite only)
|
||||
# * collect_var_indexes (composite only)
|
||||
# * lift_types (composite only)
|
||||
# * flatten_union (composite only)
|
||||
# * walk (composite only)
|
||||
#
|
||||
|
||||
@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(same, same, _stack, context) do
|
||||
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
|
||||
|
||||
def unify(type, {:var, var}, stack, context) do
|
||||
unify_var(var, type, stack, context, _var_source = false)
|
||||
defp do_unify(type, {:var, var}, stack, context) do
|
||||
case context.types do
|
||||
%{^var => {:var, var_type}} ->
|
||||
do_unify(type, {:var, var_type}, stack, context)
|
||||
|
||||
%{} ->
|
||||
unify_var(var, type, stack, context, _var_source = false)
|
||||
end
|
||||
end
|
||||
|
||||
def unify({:var, var}, type, stack, context) do
|
||||
unify_var(var, type, stack, context, _var_source = true)
|
||||
defp do_unify({:var, var}, type, stack, context) do
|
||||
case context.types do
|
||||
%{^var => {:var, var_type}} ->
|
||||
do_unify({:var, var_type}, type, stack, context)
|
||||
|
||||
%{} ->
|
||||
unify_var(var, type, stack, context, _var_source = true)
|
||||
end
|
||||
end
|
||||
|
||||
def unify({:tuple, n, sources}, {:tuple, n, targets}, stack, context) do
|
||||
defp do_unify({:tuple, n, sources}, {:tuple, n, targets}, stack, context) do
|
||||
result =
|
||||
map_reduce_ok(Enum.zip(sources, targets), context, fn {source, target}, context ->
|
||||
unify(source, target, stack, context)
|
||||
@@ -63,41 +90,33 @@ defmodule Module.Types.Unify do
|
||||
end
|
||||
end
|
||||
|
||||
def unify({:list, source}, {:list, target}, stack, context) do
|
||||
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
|
||||
|
||||
def unify({:map, source_pairs}, {:map, target_pairs}, stack, context) do
|
||||
defp do_unify({:map, source_pairs}, {:map, target_pairs}, stack, context) do
|
||||
unify_maps(source_pairs, target_pairs, stack, context)
|
||||
end
|
||||
|
||||
def unify(source, :dynamic, _stack, context) do
|
||||
defp do_unify(source, :dynamic, _stack, context) do
|
||||
{:ok, source, context}
|
||||
end
|
||||
|
||||
def unify(:dynamic, target, _stack, context) do
|
||||
defp do_unify(:dynamic, target, _stack, context) do
|
||||
{:ok, target, context}
|
||||
end
|
||||
|
||||
def unify({:union, types}, target, stack, context) do
|
||||
unify_result =
|
||||
map_reduce_ok(types, context, fn type, context ->
|
||||
unify(type, target, stack, context)
|
||||
end)
|
||||
|
||||
case unify_result do
|
||||
{:ok, types, context} -> {:ok, to_union(types, context), context}
|
||||
{:error, context} -> {:error, context}
|
||||
end
|
||||
end
|
||||
|
||||
def unify(source, target, stack, context) do
|
||||
defp do_unify(source, target, stack, context) do
|
||||
cond do
|
||||
# TODO: This condition exists to handle unions with unbound vars.
|
||||
match?({:union, _}, target) and has_unbound_var?(target, context) ->
|
||||
# This condition exists to handle unions with unbound vars.
|
||||
# TODO: handle unions properly. Note we can easily unify
|
||||
# "union < type" even if union has vars as the vars must be
|
||||
# type
|
||||
(match?({:union, _}, source) and has_unbound_var?(source, context)) or
|
||||
(match?({:union, _}, target) and has_unbound_var?(target, context)) ->
|
||||
{:ok, source, context}
|
||||
|
||||
subtype?(source, target, context) ->
|
||||
@@ -108,14 +127,14 @@ defmodule Module.Types.Unify do
|
||||
end
|
||||
end
|
||||
|
||||
def unify_var(var, :dynamic, _stack, context, _var_source?) do
|
||||
defp unify_var(var, :dynamic, _stack, context, _var_source?) do
|
||||
{:ok, {:var, var}, context}
|
||||
end
|
||||
|
||||
def unify_var(var, type, stack, context, var_source?) do
|
||||
defp unify_var(var, type, stack, context, var_source?) do
|
||||
case context.types do
|
||||
%{^var => :unbound} ->
|
||||
context = refine_var!(var, type, stack, context)
|
||||
context = refine_var(var, type, stack, context)
|
||||
stack = push_unify_stack(var, stack)
|
||||
|
||||
if recursive_type?(type, [], context) do
|
||||
@@ -128,31 +147,6 @@ defmodule Module.Types.Unify do
|
||||
{:ok, {:var, var}, context}
|
||||
end
|
||||
|
||||
%{^var => {:var, new_var} = var_type} ->
|
||||
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, type, context} ->
|
||||
{:ok, type, context}
|
||||
|
||||
{:error, {type, reason, %{traces: error_traces} = error_context}} ->
|
||||
old_var_traces = Map.get(context.traces, new_var, [])
|
||||
new_var_traces = Map.get(error_traces, new_var, [])
|
||||
add_var_traces = Enum.drop(new_var_traces, -length(old_var_traces))
|
||||
|
||||
error_traces =
|
||||
error_traces
|
||||
|> Map.update(var, add_var_traces, &(add_var_traces ++ &1))
|
||||
|> Map.put(new_var, old_var_traces)
|
||||
|
||||
{:error, {type, reason, %{error_context | traces: error_traces}}}
|
||||
end
|
||||
|
||||
%{^var => var_type} ->
|
||||
# Only add trace if the variable wasn't already "expanded"
|
||||
context =
|
||||
@@ -176,7 +170,7 @@ defmodule Module.Types.Unify do
|
||||
{:ok, {:var, var}, context}
|
||||
|
||||
{:ok, res_type, context} ->
|
||||
context = refine_var!(var, res_type, stack, context)
|
||||
context = refine_var(var, res_type, stack, context)
|
||||
{:ok, {:var, var}, context}
|
||||
|
||||
{:error, reason} ->
|
||||
@@ -188,7 +182,7 @@ defmodule Module.Types.Unify do
|
||||
# * All required keys on each side need to match to the other side.
|
||||
# * All optional keys on each side that do not match must be discarded.
|
||||
|
||||
def unify_maps(source_pairs, target_pairs, stack, context) do
|
||||
defp unify_maps(source_pairs, target_pairs, stack, context) do
|
||||
{source_required, source_optional} = split_pairs(source_pairs)
|
||||
{target_required, target_optional} = split_pairs(target_pairs)
|
||||
|
||||
@@ -219,7 +213,7 @@ defmodule Module.Types.Unify do
|
||||
end
|
||||
end
|
||||
|
||||
def unify_source_required(source_required, target_pairs, stack, context) do
|
||||
defp unify_source_required(source_required, target_pairs, stack, context) do
|
||||
map_reduce_ok(source_required, context, fn {source_key, source_value}, context ->
|
||||
Enum.find_value(target_pairs, fn {target_kind, target_key, target_value} ->
|
||||
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
|
||||
@@ -239,7 +233,7 @@ defmodule Module.Types.Unify do
|
||||
end)
|
||||
end
|
||||
|
||||
def unify_target_required(target_required, source_pairs, stack, context) do
|
||||
defp unify_target_required(target_required, source_pairs, stack, context) do
|
||||
map_reduce_ok(target_required, context, fn {target_key, target_value}, context ->
|
||||
Enum.find_value(source_pairs, fn {source_kind, source_key, source_value} ->
|
||||
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
|
||||
@@ -259,7 +253,7 @@ defmodule Module.Types.Unify do
|
||||
end)
|
||||
end
|
||||
|
||||
def unify_source_optional(source_optional, target_optional, stack, context) do
|
||||
defp unify_source_optional(source_optional, target_optional, stack, context) do
|
||||
flat_map_reduce_ok(source_optional, context, fn {source_key, source_value}, context ->
|
||||
Enum.find_value(target_optional, fn {target_key, target_value} ->
|
||||
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
|
||||
@@ -279,7 +273,7 @@ defmodule Module.Types.Unify do
|
||||
end)
|
||||
end
|
||||
|
||||
def unify_target_optional(target_optional, source_optional, stack, context) do
|
||||
defp unify_target_optional(target_optional, source_optional, stack, context) do
|
||||
flat_map_reduce_ok(target_optional, context, fn {target_key, target_value}, context ->
|
||||
Enum.find_value(source_optional, fn {source_key, source_value} ->
|
||||
with {:ok, key, context} <- unify(source_key, target_key, stack, context) do
|
||||
@@ -308,7 +302,7 @@ defmodule Module.Types.Unify do
|
||||
{required, optional}
|
||||
end
|
||||
|
||||
def error(type, reason, context), do: {:error, {type, reason, context}}
|
||||
defp error(type, reason, context), do: {:error, {type, reason, context}}
|
||||
|
||||
@doc """
|
||||
Push expression to stack.
|
||||
@@ -412,21 +406,10 @@ defmodule Module.Types.Unify do
|
||||
%{stack | unify_stack: [var | stack.unify_stack]}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Restores the variable information from the old context into new context.
|
||||
"""
|
||||
def restore_var!(var, new_context, old_context) do
|
||||
%{^var => type} = old_context.types
|
||||
%{^var => trace} = old_context.traces
|
||||
types = Map.put(new_context.types, var, type)
|
||||
traces = Map.put(new_context.traces, var, trace)
|
||||
%{new_context | types: types, traces: traces}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Set the type for a variable and add trace.
|
||||
"""
|
||||
def refine_var!(var, type, stack, context) do
|
||||
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)
|
||||
@@ -488,68 +471,37 @@ defmodule Module.Types.Unify do
|
||||
end)
|
||||
end
|
||||
|
||||
defp recursive_type?({:fun, clauses}, parents, context) do
|
||||
Enum.any?(clauses, fn {args, return} ->
|
||||
Enum.any?([return | args], &recursive_type?(&1, [clauses | parents], context))
|
||||
end)
|
||||
end
|
||||
|
||||
defp recursive_type?(_other, _parents, _context) do
|
||||
false
|
||||
end
|
||||
|
||||
@doc """
|
||||
Collects all type vars recursively.
|
||||
"""
|
||||
def collect_var_indexes(type, context, acc \\ %{}) do
|
||||
{_type, indexes} =
|
||||
walk(type, acc, fn
|
||||
{:var, var}, acc ->
|
||||
case acc do
|
||||
%{^var => _} ->
|
||||
{{:var, var}, acc}
|
||||
|
||||
%{} ->
|
||||
case context.types do
|
||||
%{^var => :unbound} ->
|
||||
{{:var, var}, Map.put(acc, var, true)}
|
||||
|
||||
%{^var => type} ->
|
||||
{{:var, var}, collect_var_indexes(type, context, Map.put(acc, var, true))}
|
||||
end
|
||||
end
|
||||
|
||||
other, acc ->
|
||||
{other, acc}
|
||||
end)
|
||||
|
||||
indexes
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the type has a type var.
|
||||
"""
|
||||
def has_unbound_var?(type, context) do
|
||||
walk(type, :ok, fn
|
||||
{:var, var}, acc ->
|
||||
case context.types do
|
||||
%{^var => :unbound} ->
|
||||
throw(:has_unbound_var?)
|
||||
|
||||
%{^var => type} ->
|
||||
has_unbound_var?(type, context)
|
||||
{{:var, var}, acc}
|
||||
end
|
||||
|
||||
other, acc ->
|
||||
{other, acc}
|
||||
end)
|
||||
|
||||
false
|
||||
catch
|
||||
:throw, :has_unbound_var? -> true
|
||||
def has_unbound_var?({:var, var}, context) do
|
||||
case context.types do
|
||||
%{^var => :unbound} -> true
|
||||
%{^var => type} -> has_unbound_var?(type, context)
|
||||
end
|
||||
end
|
||||
|
||||
def has_unbound_var?({:tuple, _, args}, context),
|
||||
do: Enum.any?(args, &has_unbound_var?(&1, context))
|
||||
|
||||
def has_unbound_var?({:union, args}, context),
|
||||
do: Enum.any?(args, &has_unbound_var?(&1, context))
|
||||
|
||||
def has_unbound_var?({:list, arg}, context),
|
||||
do: has_unbound_var?(arg, context)
|
||||
|
||||
def has_unbound_var?({:map, pairs}, context) do
|
||||
Enum.any?(pairs, fn {_, key, value} ->
|
||||
has_unbound_var?(key, context) or has_unbound_var?(value, context)
|
||||
end)
|
||||
end
|
||||
|
||||
def has_unbound_var?(_type, _context), do: false
|
||||
|
||||
@doc """
|
||||
Returns true if it is a singleton type.
|
||||
|
||||
@@ -604,7 +556,7 @@ defmodule Module.Types.Unify do
|
||||
def subtype?({:tuple, n, left_types}, {:tuple, n, right_types}, context) do
|
||||
left_types
|
||||
|> Enum.zip(right_types)
|
||||
|> Enum.all?(fn {left, right} -> subtype?(left, right, context) end)
|
||||
|> Enum.any?(fn {left, right} -> subtype?(left, right, context) end)
|
||||
end
|
||||
|
||||
def subtype?({:map, left_pairs}, {:map, right_pairs}, context) do
|
||||
@@ -648,15 +600,17 @@ defmodule Module.Types.Unify do
|
||||
def to_union([type], _context), do: type
|
||||
|
||||
def to_union(types, context) when types != [] do
|
||||
case unique_super_types(unnest_unions(types), context) do
|
||||
flat_types = flatten_union(types)
|
||||
|
||||
case unique_super_types(flat_types, context) do
|
||||
[type] -> type
|
||||
types -> {:union, types}
|
||||
end
|
||||
end
|
||||
|
||||
defp unnest_unions(types) do
|
||||
defp flatten_union(types) do
|
||||
Enum.flat_map(types, fn
|
||||
{:union, types} -> unnest_unions(types)
|
||||
{:union, types} -> flatten_union(types)
|
||||
type -> [type]
|
||||
end)
|
||||
end
|
||||
@@ -681,204 +635,6 @@ defmodule Module.Types.Unify do
|
||||
[]
|
||||
end
|
||||
|
||||
## Type lifting
|
||||
|
||||
@doc """
|
||||
Lifts type variables to their inferred types from the context.
|
||||
"""
|
||||
def lift_types(types, %{lifted_types: _} = context) do
|
||||
Enum.map_reduce(types, context, &lift_type/2)
|
||||
end
|
||||
|
||||
def lift_types(types, context) do
|
||||
context = %{
|
||||
types: context.types,
|
||||
lifted_types: %{},
|
||||
lifted_counter: 0
|
||||
}
|
||||
|
||||
Enum.map_reduce(types, context, &lift_type/2)
|
||||
end
|
||||
|
||||
# Lift type variable to its inferred (hopefully concrete) types from the context
|
||||
defp lift_type({:var, var}, context) do
|
||||
case context.lifted_types do
|
||||
%{^var => lifted_var} ->
|
||||
{{:var, lifted_var}, context}
|
||||
|
||||
%{} ->
|
||||
case context.types do
|
||||
%{^var => :unbound} ->
|
||||
new_lifted_var(var, context)
|
||||
|
||||
%{^var => type} ->
|
||||
if recursive_type?(type, [], context) do
|
||||
new_lifted_var(var, context)
|
||||
else
|
||||
# Remove visited types to avoid infinite loops
|
||||
# then restore after we are done recursing on vars
|
||||
types = context.types
|
||||
context = put_in(context.types[var], :unbound)
|
||||
{type, context} = lift_type(type, context)
|
||||
{type, %{context | types: types}}
|
||||
end
|
||||
|
||||
%{} ->
|
||||
new_lifted_var(var, context)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp lift_type({:union, types}, context) do
|
||||
{types, context} = Enum.map_reduce(types, context, &lift_type/2)
|
||||
{{:union, types}, context}
|
||||
end
|
||||
|
||||
defp lift_type({:tuple, n, types}, context) do
|
||||
{types, context} = Enum.map_reduce(types, context, &lift_type/2)
|
||||
{{:tuple, n, types}, context}
|
||||
end
|
||||
|
||||
defp lift_type({:map, pairs}, context) do
|
||||
{pairs, context} =
|
||||
Enum.map_reduce(pairs, context, fn {kind, key, value}, context ->
|
||||
{key, context} = lift_type(key, context)
|
||||
{value, context} = lift_type(value, context)
|
||||
{{kind, key, value}, context}
|
||||
end)
|
||||
|
||||
{{:map, pairs}, context}
|
||||
end
|
||||
|
||||
defp lift_type({:list, type}, context) do
|
||||
{type, context} = lift_type(type, context)
|
||||
{{:list, type}, context}
|
||||
end
|
||||
|
||||
defp lift_type({:fun, clauses}, context) do
|
||||
clauses =
|
||||
Enum.map_reduce(clauses, context, fn {args, return}, context ->
|
||||
{[return | args], context} = Enum.map_reduce([return | args], context, &lift_type/2)
|
||||
{{args, return}, context}
|
||||
end)
|
||||
|
||||
{{:fun, clauses}, context}
|
||||
end
|
||||
|
||||
defp 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
|
||||
|
||||
# TODO: Figure out function expansion
|
||||
|
||||
@doc """
|
||||
Expand unions so that all unions are at the top level.
|
||||
|
||||
{integer() | float()} => {integer()} | {float()}
|
||||
"""
|
||||
def flatten_union({:union, types}, context) do
|
||||
Enum.flat_map(types, &flatten_union(&1, context))
|
||||
end
|
||||
|
||||
def flatten_union(type, context) do
|
||||
List.wrap(do_flatten_union(type, context))
|
||||
end
|
||||
|
||||
def do_flatten_union({:tuple, num, types}, context) do
|
||||
flatten_union_tuple(types, num, context, [])
|
||||
end
|
||||
|
||||
def do_flatten_union({:list, type}, context) do
|
||||
case do_flatten_union(type, context) do
|
||||
{:union, union_types} -> Enum.map(union_types, &{:list, &1})
|
||||
_type -> [{:list, type}]
|
||||
end
|
||||
end
|
||||
|
||||
def do_flatten_union({:map, pairs}, context) do
|
||||
flatten_union_map(pairs, context, [])
|
||||
end
|
||||
|
||||
def do_flatten_union({:var, var}, context) do
|
||||
if looping_var?(var, context, []) do
|
||||
{:var, var}
|
||||
else
|
||||
case context.types do
|
||||
%{^var => :unbound} -> {:var, var}
|
||||
%{^var => {:union, types}} -> Enum.map(types, &do_flatten_union(&1, context))
|
||||
%{^var => type} -> do_flatten_union(type, context)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def do_flatten_union(type, _context) do
|
||||
type
|
||||
end
|
||||
|
||||
defp flatten_union_tuple([type | types], num, context, acc) do
|
||||
case do_flatten_union(type, context) do
|
||||
{:union, union_types} ->
|
||||
Enum.flat_map(union_types, &flatten_union_tuple(types, num, context, [&1 | acc]))
|
||||
|
||||
type ->
|
||||
flatten_union_tuple(types, num, context, [type | acc])
|
||||
end
|
||||
end
|
||||
|
||||
defp flatten_union_tuple([], num, _context, acc) do
|
||||
[{:tuple, num, Enum.reverse(acc)}]
|
||||
end
|
||||
|
||||
defp flatten_union_map([{kind, key, value} | pairs], context, acc) do
|
||||
case do_flatten_union(key, context) do
|
||||
{:union, union_types} ->
|
||||
Enum.flat_map(union_types, &flatten_union_map_value(kind, &1, value, pairs, context, acc))
|
||||
|
||||
type ->
|
||||
flatten_union_map_value(kind, type, value, pairs, context, acc)
|
||||
end
|
||||
end
|
||||
|
||||
defp flatten_union_map([], _context, acc) do
|
||||
[{:map, Enum.reverse(acc)}]
|
||||
end
|
||||
|
||||
defp flatten_union_map_value(kind, key, value, pairs, context, acc) do
|
||||
case do_flatten_union(value, context) do
|
||||
{:union, union_types} ->
|
||||
Enum.flat_map(union_types, &flatten_union_map(pairs, context, [{kind, key, &1} | acc]))
|
||||
|
||||
value ->
|
||||
flatten_union_map(pairs, context, [{kind, key, value} | acc])
|
||||
end
|
||||
end
|
||||
|
||||
defp looping_var?(var, context, parents) do
|
||||
case context.types do
|
||||
%{^var => :unbound} ->
|
||||
false
|
||||
|
||||
%{^var => {:var, type}} ->
|
||||
if var in parents do
|
||||
true
|
||||
else
|
||||
looping_var?(type, context, [var | parents])
|
||||
end
|
||||
|
||||
%{^var => _type} ->
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats types.
|
||||
|
||||
@@ -888,7 +644,7 @@ defmodule Module.Types.Unify do
|
||||
def format_type({:map, pairs}, true) do
|
||||
case List.keyfind(pairs, {:atom, :__struct__}, 1) do
|
||||
{:required, {:atom, :__struct__}, {:atom, struct}} ->
|
||||
["%", inspect(struct), "{}"]
|
||||
"%#{inspect(struct)}{}"
|
||||
|
||||
_ ->
|
||||
"map()"
|
||||
@@ -896,31 +652,24 @@ defmodule Module.Types.Unify do
|
||||
end
|
||||
|
||||
def format_type({:union, types}, simplify?) do
|
||||
types
|
||||
|> Enum.map(&format_type(&1, simplify?))
|
||||
|> Enum.intersperse(" | ")
|
||||
"#{Enum.map_join(types, " | ", &format_type(&1, simplify?))}"
|
||||
end
|
||||
|
||||
def format_type({:tuple, _, types}, simplify?) do
|
||||
format =
|
||||
types
|
||||
|> Enum.map(&format_type(&1, simplify?))
|
||||
|> Enum.intersperse(", ")
|
||||
|
||||
["{", format, "}"]
|
||||
"{#{Enum.map_join(types, ", ", &format_type(&1, simplify?))}}"
|
||||
end
|
||||
|
||||
def format_type({:list, type}, simplify?) do
|
||||
["[", format_type(type, simplify?), "]"]
|
||||
"[#{format_type(type, simplify?)}]"
|
||||
end
|
||||
|
||||
def format_type({:map, pairs}, false) do
|
||||
case List.keytake(pairs, {:atom, :__struct__}, 1) do
|
||||
{{:required, {:atom, :__struct__}, {:atom, struct}}, pairs} ->
|
||||
["%", inspect(struct), "{", format_map_pairs(pairs), "}"]
|
||||
"%#{inspect(struct)}{#{format_map_pairs(pairs)}}"
|
||||
|
||||
_ ->
|
||||
["%{", format_map_pairs(pairs), "}"]
|
||||
"%{#{format_map_pairs(pairs)}}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -929,84 +678,26 @@ defmodule Module.Types.Unify do
|
||||
end
|
||||
|
||||
def format_type({:var, index}, _simplify?) do
|
||||
["var", Integer.to_string(index + 1)]
|
||||
end
|
||||
|
||||
def format_type({:fun, clauses}, simplify?) do
|
||||
format =
|
||||
Enum.map(clauses, fn {params, return} ->
|
||||
params = Enum.intersperse(Enum.map(params, &format_type(&1, simplify?)), ", ")
|
||||
params = if params == [], do: params, else: [params, " "]
|
||||
return = format_type(return, simplify?)
|
||||
[params, "-> ", return]
|
||||
end)
|
||||
|
||||
["(", Enum.intersperse(format, "; "), ")"]
|
||||
"var#{index}"
|
||||
end
|
||||
|
||||
def format_type(atom, _simplify?) when is_atom(atom) do
|
||||
[Atom.to_string(atom), "()"]
|
||||
"#{atom}()"
|
||||
end
|
||||
|
||||
defp format_map_pairs(pairs) do
|
||||
{atoms, others} = Enum.split_with(pairs, &match?({:required, {:atom, _}, _}, &1))
|
||||
{required, optional} = Enum.split_with(others, &match?({:required, _, _}, &1))
|
||||
|
||||
(atoms ++ required ++ optional)
|
||||
|> Enum.map(fn
|
||||
Enum.map_join(atoms ++ required ++ optional, ", ", fn
|
||||
{:required, {:atom, atom}, right} ->
|
||||
[Atom.to_string(atom), ": ", format_type(right, false)]
|
||||
"#{atom}: #{format_type(right, false)}"
|
||||
|
||||
{:required, left, right} ->
|
||||
[format_type(left, false), " => ", format_type(right, false)]
|
||||
"#{format_type(left, false)} => #{format_type(right, false)}"
|
||||
|
||||
{:optional, left, right} ->
|
||||
["optional(", format_type(left, false), ") => ", format_type(right, false)]
|
||||
"optional(#{format_type(left, false)}) => #{format_type(right, false)}"
|
||||
end)
|
||||
|> Enum.intersperse(", ")
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a depth-first, pre-order traversal of the type tree using an accumulator.
|
||||
"""
|
||||
def walk({:map, pairs}, acc, fun) do
|
||||
{pairs, acc} =
|
||||
Enum.map_reduce(pairs, acc, fn {kind, key, value}, acc ->
|
||||
{key, acc} = walk(key, acc, fun)
|
||||
{value, acc} = walk(value, acc, fun)
|
||||
{{kind, key, value}, acc}
|
||||
end)
|
||||
|
||||
fun.({:map, pairs}, acc)
|
||||
end
|
||||
|
||||
def walk({:union, types}, acc, fun) do
|
||||
{types, acc} = Enum.map_reduce(types, acc, &walk(&1, &2, fun))
|
||||
fun.({:union, types}, acc)
|
||||
end
|
||||
|
||||
def walk({:tuple, num, types}, acc, fun) do
|
||||
{types, acc} = Enum.map_reduce(types, acc, &walk(&1, &2, fun))
|
||||
fun.({:tuple, num, types}, acc)
|
||||
end
|
||||
|
||||
def walk({:list, type}, acc, fun) do
|
||||
{type, acc} = walk(type, acc, fun)
|
||||
fun.({:list, type}, acc)
|
||||
end
|
||||
|
||||
def walk({:fun, clauses}, acc, fun) do
|
||||
{clauses, acc} =
|
||||
Enum.map_reduce(clauses, acc, fn {params, return}, acc ->
|
||||
{params, acc} = Enum.map_reduce(params, acc, &walk(&1, &2, fun))
|
||||
{return, acc} = walk(return, acc, fun)
|
||||
{{params, return}, acc}
|
||||
end)
|
||||
|
||||
fun.({:fun, clauses}, acc)
|
||||
end
|
||||
|
||||
def walk(type, acc, fun) do
|
||||
fun.(type, acc)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -428,32 +428,34 @@ defmodule OptionParser do
|
||||
end
|
||||
end
|
||||
|
||||
# Handles -a, -abc, -abc=something, -n2
|
||||
# Handles -a, -abc, -abc=something
|
||||
defp next_with_config(["-" <> option | rest] = argv, config) do
|
||||
%{allow_nonexistent_atoms?: allow_nonexistent_atoms?} = config
|
||||
{option, value} = split_option(option)
|
||||
original = "-" <> option
|
||||
letters = String.graphemes(option)
|
||||
|
||||
cond do
|
||||
is_nil(value) and starts_with_number?(option) ->
|
||||
is_nil(value) and negative_number?(original) ->
|
||||
{:error, argv}
|
||||
|
||||
String.contains?(option, ["-", "_"]) ->
|
||||
{:undefined, original, value, rest}
|
||||
|
||||
String.length(option) == 1 ->
|
||||
tl(letters) == [] ->
|
||||
# We have a regular one-letter alias here
|
||||
tagged = tag_oneletter_alias(option, config)
|
||||
next_tagged(tagged, value, original, rest, config)
|
||||
|
||||
true ->
|
||||
key = get_option_key(option, config.allow_nonexistent_atoms?)
|
||||
key = get_option_key(option, allow_nonexistent_atoms?)
|
||||
option_key = config.aliases[key]
|
||||
|
||||
if key && option_key do
|
||||
IO.warn("multi-letter aliases are deprecated, got: #{inspect(key)}")
|
||||
next_tagged({:default, option_key}, value, original, rest, config)
|
||||
else
|
||||
next_with_config(expand_multiletter_alias(option, value) ++ rest, config)
|
||||
next_with_config(expand_multiletter_alias(letters, value) ++ rest, config)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -596,11 +598,9 @@ defmodule OptionParser do
|
||||
raise ArgumentError, ":switches and :strict cannot be given together"
|
||||
|
||||
switches = opts[:switches] ->
|
||||
validate_switches(switches)
|
||||
{switches, false}
|
||||
|
||||
strict = opts[:strict] ->
|
||||
validate_switches(strict)
|
||||
{strict, true}
|
||||
|
||||
true ->
|
||||
@@ -616,20 +616,6 @@ defmodule OptionParser do
|
||||
}
|
||||
end
|
||||
|
||||
defp validate_switches(switches) do
|
||||
Enum.map(switches, &validate_switch/1)
|
||||
end
|
||||
|
||||
defp validate_switch({_name, type_or_type_and_modifiers}) do
|
||||
valid = [:boolean, :count, :integer, :float, :string, :keep]
|
||||
invalid = List.wrap(type_or_type_and_modifiers) -- valid
|
||||
|
||||
if invalid != [] do
|
||||
raise ArgumentError,
|
||||
"invalid switch types/modifiers: " <> Enum.map_join(invalid, ", ", &inspect/1)
|
||||
end
|
||||
end
|
||||
|
||||
defp validate_option(value, kinds) do
|
||||
{invalid?, value} =
|
||||
cond do
|
||||
@@ -721,25 +707,13 @@ defmodule OptionParser do
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_multiletter_alias(options, value) do
|
||||
{options, maybe_integer} =
|
||||
options
|
||||
|> String.to_charlist()
|
||||
|> Enum.split_while(&(&1 not in ?0..?9))
|
||||
|
||||
defp expand_multiletter_alias(letters, value) do
|
||||
{last, expanded} =
|
||||
options
|
||||
|> List.to_string()
|
||||
|> String.graphemes()
|
||||
letters
|
||||
|> Enum.map(&("-" <> &1))
|
||||
|> List.pop_at(-1)
|
||||
|
||||
expanded ++
|
||||
[
|
||||
last <>
|
||||
if(maybe_integer != [], do: "=#{maybe_integer}", else: "") <>
|
||||
if(value, do: "=#{value}", else: "")
|
||||
]
|
||||
expanded ++ [last <> if(value, do: "=" <> value, else: "")]
|
||||
end
|
||||
|
||||
defp normalize_tag(:negated, option, value, switches) do
|
||||
@@ -780,7 +754,7 @@ defmodule OptionParser do
|
||||
|
||||
defp value_in_tail?(["-" | _]), do: true
|
||||
defp value_in_tail?(["- " <> _ | _]), do: true
|
||||
defp value_in_tail?(["-" <> arg | _]), do: starts_with_number?(arg)
|
||||
defp value_in_tail?(["-" <> arg | _]), do: negative_number?("-" <> arg)
|
||||
defp value_in_tail?([]), do: false
|
||||
defp value_in_tail?(_), do: true
|
||||
|
||||
@@ -812,8 +786,9 @@ defmodule OptionParser do
|
||||
end
|
||||
end
|
||||
|
||||
defp starts_with_number?(<<char, _::binary>>) when char in ?0..?9, do: true
|
||||
defp starts_with_number?(_), do: false
|
||||
defp negative_number?(arg) do
|
||||
match?({_, ""}, Float.parse(arg))
|
||||
end
|
||||
|
||||
defp format_errors([_ | _] = errors, opts) do
|
||||
types = opts[:switches] || opts[:strict]
|
||||
@@ -828,15 +803,7 @@ defmodule OptionParser do
|
||||
if type = get_type(option, opts, types) do
|
||||
"#{option} : Missing argument of type #{type}"
|
||||
else
|
||||
msg = "#{option} : Unknown option"
|
||||
|
||||
case did_you_mean(option, types) do
|
||||
{similar, score} when score > 0.8 ->
|
||||
msg <> ". Did you mean --#{similar}?"
|
||||
|
||||
_ ->
|
||||
msg
|
||||
end
|
||||
"#{option} : Unknown option"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -855,17 +822,4 @@ defmodule OptionParser do
|
||||
types[key]
|
||||
end
|
||||
end
|
||||
|
||||
defp did_you_mean(option, types) do
|
||||
key = option |> String.trim_leading("-") |> String.replace("-", "_")
|
||||
Enum.reduce(types, {nil, 0}, &max_similar(&1, key, &2))
|
||||
end
|
||||
|
||||
defp max_similar({source, _}, target, {_, current} = best) do
|
||||
source = Atom.to_string(source)
|
||||
|
||||
score = String.jaro_distance(source, target)
|
||||
option = String.replace(source, "_", "-")
|
||||
if score < current, do: best, else: {option, score}
|
||||
end
|
||||
end
|
||||
|
||||
+17
-35
@@ -5,8 +5,7 @@ defmodule Path do
|
||||
|
||||
The functions in this module may receive a chardata as
|
||||
argument (i.e. a string or a list of characters / string)
|
||||
and will always return a string (encoded in UTF-8). If a binary
|
||||
is given, in whatever encoding, its encoding will be kept.
|
||||
and will always return a string (encoded in UTF-8).
|
||||
|
||||
The majority of the functions in this module do not
|
||||
interact with the file system, except for a few functions
|
||||
@@ -67,7 +66,7 @@ defmodule Path do
|
||||
|
||||
case type(path) do
|
||||
:relative ->
|
||||
absname_join([relative_to, path])
|
||||
absname_join(relative_to, path)
|
||||
|
||||
:absolute ->
|
||||
absname_join([path])
|
||||
@@ -81,11 +80,11 @@ defmodule Path do
|
||||
# Absolute path on current drive
|
||||
defp absname_vr(["/" | rest], [volume | _], _relative), do: absname_join([volume | rest])
|
||||
|
||||
# Relative to current directory on current drive
|
||||
# Relative to current directory on current drive.
|
||||
defp absname_vr([<<x, ?:>> | rest], [<<x, _::binary>> | _], relative),
|
||||
do: absname(absname_join(rest), relative)
|
||||
|
||||
# Relative to current directory on another drive
|
||||
# Relative to current directory on another drive.
|
||||
defp absname_vr([<<x, ?:>> | name], _, _relative) do
|
||||
cwd =
|
||||
case :file.get_cwd([x, ?:]) do
|
||||
@@ -98,25 +97,25 @@ defmodule Path do
|
||||
|
||||
@slash [?/, ?\\]
|
||||
|
||||
defp absname_join([]), do: ""
|
||||
defp absname_join(list), do: absname_join(list, major_os_type())
|
||||
# Joins a list
|
||||
defp absname_join([name1, name2 | rest]), do: absname_join([absname_join(name1, name2) | rest])
|
||||
|
||||
defp absname_join([name1, name2 | rest], os_type) do
|
||||
joined = do_absname_join(IO.chardata_to_string(name1), relative(name2), [], os_type)
|
||||
absname_join([joined | rest], os_type)
|
||||
end
|
||||
defp absname_join([name]),
|
||||
do: do_absname_join(IO.chardata_to_string(name), <<>>, [], major_os_type())
|
||||
|
||||
defp absname_join([name], os_type) do
|
||||
do_absname_join(IO.chardata_to_string(name), <<>>, [], os_type)
|
||||
end
|
||||
# Joins two paths
|
||||
defp absname_join(left, right),
|
||||
do: do_absname_join(IO.chardata_to_string(left), relative(right), [], major_os_type())
|
||||
|
||||
defp do_absname_join(<<uc_letter, ?:, rest::binary>>, relativename, [], :win32)
|
||||
when uc_letter in ?A..?Z,
|
||||
do: do_absname_join(rest, relativename, [?:, uc_letter + ?a - ?A], :win32)
|
||||
when uc_letter in ?A..?Z 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)
|
||||
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)
|
||||
@@ -314,9 +313,6 @@ defmodule Path do
|
||||
iex> Path.relative_to("/usr/local/foo", "/etc")
|
||||
"/usr/local/foo"
|
||||
|
||||
iex> Path.relative_to("/usr/local/foo", "/usr/local/foo")
|
||||
"."
|
||||
|
||||
"""
|
||||
@spec relative_to(t, t) :: binary
|
||||
def relative_to(path, from) do
|
||||
@@ -324,10 +320,6 @@ defmodule Path do
|
||||
relative_to(split(path), split(from), path)
|
||||
end
|
||||
|
||||
defp relative_to(path, path, _original) do
|
||||
"."
|
||||
end
|
||||
|
||||
defp relative_to([h | t1], [h | t2], original) do
|
||||
relative_to(t1, t2, original)
|
||||
end
|
||||
@@ -426,16 +418,6 @@ defmodule Path do
|
||||
@doc """
|
||||
Returns the extension of the last component of `path`.
|
||||
|
||||
The behaviour of this function changed in Erlang/OTP 24 for filenames
|
||||
starting with a dot and without an extension. For example, for a file
|
||||
named ".gitignore", `extname/1` now returns an empty string, while it
|
||||
would return ".gitignore" in previous Erlang/OTP versions. This was
|
||||
done to match the behaviour of `rootname/1`, which would return
|
||||
".gitignore" as its name (and therefore it cannot also be an extension).
|
||||
|
||||
See `basename/1` and `rootname/1` for related functions to extract
|
||||
information from paths.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.extname("foo.erl")
|
||||
|
||||
@@ -222,7 +222,7 @@ defmodule Port do
|
||||
@doc """
|
||||
Sends `data` to the port driver `port`.
|
||||
|
||||
For more information, see `:erlang.port_command/3`.
|
||||
For more information, see `:erlang.port_command/2`.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
|
||||
@@ -383,19 +383,12 @@ defmodule Process do
|
||||
@type spawn_opt ::
|
||||
:link
|
||||
| :monitor
|
||||
| {:monitor, monitor_option()}
|
||||
| {:priority, :low | :normal | :high}
|
||||
| {:fullsweep_after, non_neg_integer}
|
||||
| {:min_heap_size, non_neg_integer}
|
||||
| {:min_bin_vheap_size, non_neg_integer}
|
||||
| {:max_heap_size, heap_size}
|
||||
| {:message_queue_data, :off_heap | :on_heap}
|
||||
@type spawn_opts :: [spawn_opt]
|
||||
|
||||
# TODO: Use :erlang.monitor_option() on Erlang/OTP 24+
|
||||
@typep monitor_option ::
|
||||
[alias: :explicit_unalias | :demonitor | :reply_demonitor, tag: term()]
|
||||
|
||||
@doc """
|
||||
Spawns the given function according to the given options.
|
||||
|
||||
@@ -455,7 +448,7 @@ defmodule Process do
|
||||
If the process is already dead when calling `Process.monitor/1`, a
|
||||
`:DOWN` message is delivered immediately.
|
||||
|
||||
See ["The need for monitoring"](https://elixir-lang.org/getting-started/mix-otp/genserver.html#the-need-for-monitoring)
|
||||
See [the need for monitoring](https://elixir-lang.org/getting-started/mix-otp/genserver.html#the-need-for-monitoring)
|
||||
for an example. See `:erlang.monitor/2` for more information.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
+58
-148
@@ -4,11 +4,11 @@ defmodule Protocol do
|
||||
|
||||
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/3`.
|
||||
and its implementations with `Kernel.defimpl/2`.
|
||||
|
||||
## A real case
|
||||
## Examples
|
||||
|
||||
In Elixir, we have two nouns for checking how many items there
|
||||
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,
|
||||
@@ -53,7 +53,7 @@ defmodule Protocol do
|
||||
|
||||
It is possible to implement protocols for all Elixir types:
|
||||
|
||||
* Structs (see the "Protocols and Structs" section below)
|
||||
* Structs (see below)
|
||||
* `Tuple`
|
||||
* `Atom`
|
||||
* `List`
|
||||
@@ -65,7 +65,7 @@ defmodule Protocol do
|
||||
* `Map`
|
||||
* `Port`
|
||||
* `Reference`
|
||||
* `Any` (see the "Fallback to `Any`" section below)
|
||||
* `Any` (see below)
|
||||
|
||||
## Protocols and Structs
|
||||
|
||||
@@ -79,7 +79,7 @@ defmodule Protocol do
|
||||
end
|
||||
|
||||
When implementing a protocol for a struct, the `:for` option can
|
||||
be omitted if the `defimpl/3` call is inside the module that defines
|
||||
be omitted if the `defimpl` call is inside the module that defines
|
||||
the struct:
|
||||
|
||||
defmodule User do
|
||||
@@ -134,13 +134,13 @@ defmodule Protocol do
|
||||
def reverse(term), do: Enum.reverse(term)
|
||||
end
|
||||
|
||||
Inside `defimpl/3`, you can use `@protocol` to access the protocol
|
||||
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 zero-arity type named `t`, which
|
||||
Defining a protocol automatically defines a type named `t`, which
|
||||
can be used as follows:
|
||||
|
||||
@spec print_size(Size.t()) :: :ok
|
||||
@@ -177,7 +177,7 @@ defmodule Protocol do
|
||||
* `impl_for/1` - returns the module that implements the protocol for the given argument,
|
||||
`nil` otherwise
|
||||
|
||||
* `impl_for!/1` - same as above but raises `Protocol.UndefinedError` if an implementation is
|
||||
* `impl_for!/1` - same as above but raises an error if an implementation is
|
||||
not found
|
||||
|
||||
For example, for the `Enumerable` protocol we have:
|
||||
@@ -191,17 +191,15 @@ defmodule Protocol do
|
||||
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:
|
||||
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
|
||||
* `:for` - returns the module responsible for the data structure of the protocol implementation
|
||||
|
||||
* `:protocol` - returns the protocol module for which this implementation
|
||||
is provided
|
||||
* `: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:
|
||||
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
|
||||
@@ -211,17 +209,22 @@ defmodule Protocol do
|
||||
|
||||
## Consolidation
|
||||
|
||||
In order to speed up protocol dispatching, whenever all protocol implementations
|
||||
are known up-front, typically after all Elixir code in a project is compiled,
|
||||
Elixir provides a feature called *protocol consolidation*. Consolidation directly
|
||||
links protocols to their implementations in a way that invoking a function from a
|
||||
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:
|
||||
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
|
||||
...
|
||||
@@ -275,7 +278,7 @@ defmodule Protocol do
|
||||
name = unquote(name)
|
||||
arity = unquote(arity)
|
||||
|
||||
@__functions__ [{name, arity} | @__functions__]
|
||||
@functions [{name, arity} | @functions]
|
||||
|
||||
# Generate a fake definition with the user
|
||||
# signature that will be used by docs
|
||||
@@ -308,11 +311,9 @@ defmodule Protocol do
|
||||
end
|
||||
|
||||
defp assert_protocol!(module, extra) do
|
||||
try do
|
||||
Code.ensure_compiled!(module)
|
||||
rescue
|
||||
e in ArgumentError ->
|
||||
raise ArgumentError, e.message <> extra
|
||||
case Code.ensure_compiled(module) do
|
||||
{:module, ^module} -> :ok
|
||||
_ -> raise ArgumentError, "#{inspect(module)} is not available" <> extra
|
||||
end
|
||||
|
||||
try do
|
||||
@@ -339,11 +340,9 @@ defmodule Protocol do
|
||||
defp assert_impl!(protocol, base, extra) do
|
||||
impl = Module.concat(protocol, base)
|
||||
|
||||
try do
|
||||
Code.ensure_compiled!(impl)
|
||||
rescue
|
||||
e in ArgumentError ->
|
||||
raise ArgumentError, e.message <> extra
|
||||
case Code.ensure_compiled(impl) do
|
||||
{:module, ^impl} -> :ok
|
||||
_ -> raise ArgumentError, "#{inspect(impl)} is not available" <> extra
|
||||
end
|
||||
|
||||
try do
|
||||
@@ -398,19 +397,17 @@ defmodule Protocol do
|
||||
end
|
||||
|
||||
Derivable.ok(%ImplStruct{})
|
||||
#=> {:ok, %ImplStruct{a: 0, b: 0}, %ImplStruct{a: 0, b: 0}, []}
|
||||
{:ok, %ImplStruct{a: 0, b: 0}, %ImplStruct{a: 0, b: 0}, []}
|
||||
|
||||
Explicit derivations can now be called via `__deriving__/3`:
|
||||
Explicit derivations can now be called via `__deriving__`:
|
||||
|
||||
# Explicitly derived via `__deriving__/3`
|
||||
# Explicitly derived via `__deriving__`
|
||||
Derivable.ok(%ImplStruct{a: 1, b: 1})
|
||||
#=> {:ok, %ImplStruct{a: 1, b: 1}, %ImplStruct{a: 0, b: 0}, []}
|
||||
|
||||
# Explicitly derived by API via `__deriving__/3`
|
||||
# Explicitly derived by API via `__deriving__`
|
||||
require Protocol
|
||||
Protocol.derive(Derivable, ImplStruct, :oops)
|
||||
Derivable.ok(%ImplStruct{a: 1, b: 1})
|
||||
#=> {:ok, %ImplStruct{a: 1, b: 1}, %ImplStruct{a: 0, b: 0}, :oops}
|
||||
|
||||
"""
|
||||
defmacro derive(protocol, module, options \\ []) do
|
||||
@@ -442,7 +439,7 @@ defmodule Protocol do
|
||||
@spec extract_protocols([charlist | String.t()]) :: [atom]
|
||||
def extract_protocols(paths) do
|
||||
extract_matching_by_attribute(paths, 'Elixir.', fn module, attributes ->
|
||||
case attributes[:__protocol__] do
|
||||
case attributes[:protocol] do
|
||||
[fallback_to_any: _] -> module
|
||||
_ -> nil
|
||||
end
|
||||
@@ -473,7 +470,7 @@ defmodule Protocol do
|
||||
prefix = Atom.to_charlist(protocol) ++ '.'
|
||||
|
||||
extract_matching_by_attribute(paths, prefix, fn _mod, attributes ->
|
||||
case attributes[:__impl__] do
|
||||
case attributes[:protocol_impl] do
|
||||
[protocol: ^protocol, for: for] -> for
|
||||
_ -> nil
|
||||
end
|
||||
@@ -548,9 +545,6 @@ defmodule Protocol do
|
||||
| {:error, :not_a_protocol}
|
||||
| {:error, :no_beam_info}
|
||||
def consolidate(protocol, types) when is_atom(protocol) do
|
||||
# Ensure the types are sorted so the compiled beam is deterministic
|
||||
types = Enum.sort(types)
|
||||
|
||||
with {:ok, ast_info, specs, compile_info} <- beam_protocol(protocol),
|
||||
{:ok, definitions} <- change_debug_info(protocol, ast_info, types),
|
||||
do: compile(definitions, specs, compile_info)
|
||||
@@ -567,7 +561,7 @@ defmodule Protocol do
|
||||
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
|
||||
case attributes[:protocol] do
|
||||
[fallback_to_any: any] ->
|
||||
{:ok, {any, definitions}, specs, {info, chunks}}
|
||||
|
||||
@@ -684,22 +678,17 @@ defmodule Protocol do
|
||||
def __protocol__(name, do: block) do
|
||||
quote do
|
||||
defmodule unquote(name) do
|
||||
@before_compile Protocol
|
||||
|
||||
# We don't allow function definition inside protocols
|
||||
import Kernel,
|
||||
except: [
|
||||
def: 1,
|
||||
def: 2,
|
||||
defp: 1,
|
||||
defp: 2,
|
||||
defdelegate: 2,
|
||||
defguard: 1,
|
||||
defguardp: 1,
|
||||
defmacrop: 1,
|
||||
defmacrop: 2,
|
||||
defmacro: 1,
|
||||
defmacro: 2,
|
||||
defmacrop: 1,
|
||||
defmacrop: 2
|
||||
defp: 1,
|
||||
defp: 2,
|
||||
def: 1,
|
||||
def: 2
|
||||
]
|
||||
|
||||
# Import the new dsl that holds the new def
|
||||
@@ -709,7 +698,7 @@ defmodule Protocol do
|
||||
@compile :debug_info
|
||||
|
||||
# Set up a clear slate to store defined functions
|
||||
@__functions__ []
|
||||
@functions []
|
||||
@fallback_to_any false
|
||||
|
||||
# Invoke the user given block
|
||||
@@ -721,85 +710,6 @@ defmodule Protocol do
|
||||
end
|
||||
end
|
||||
|
||||
defp callback_ast_to_fa({kind, {:"::", meta, [{name, _, args}, _return]}, _pos})
|
||||
when kind in [:callback, :macrocallback] do
|
||||
[{{name, length(List.wrap(args))}, meta}]
|
||||
end
|
||||
|
||||
defp callback_ast_to_fa(
|
||||
{kind, {:when, _, [{:"::", meta, [{name, _, args}, _return]}, _vars]}, _pos}
|
||||
)
|
||||
when kind in [:callback, :macrocallback] do
|
||||
[{{name, length(List.wrap(args))}, meta}]
|
||||
end
|
||||
|
||||
defp callback_ast_to_fa({kind, _, _pos}) when kind in [:callback, :macrocallback] do
|
||||
[]
|
||||
end
|
||||
|
||||
defp callback_metas(module, kind)
|
||||
when kind in [:callback, :macrocallback] do
|
||||
:lists.flatmap(&callback_ast_to_fa/1, Module.get_attribute(module, kind))
|
||||
|> :maps.from_list()
|
||||
end
|
||||
|
||||
defp get_callback_line(fa, metas),
|
||||
do: :maps.get(fa, metas, [])[:line]
|
||||
|
||||
defp warn(message, env, nil) do
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
end
|
||||
|
||||
defp warn(message, env, line) when is_integer(line) do
|
||||
stacktrace = :maps.update(:line, line, env) |> Macro.Env.stacktrace()
|
||||
IO.warn(message, stacktrace)
|
||||
end
|
||||
|
||||
# TODO: Convert the following warnings into errors future Elixir versions
|
||||
def __before_compile__(env) do
|
||||
# Callbacks
|
||||
callback_metas = callback_metas(env.module, :callback)
|
||||
callbacks = :maps.keys(callback_metas)
|
||||
functions = Module.get_attribute(env.module, :__functions__)
|
||||
|
||||
:lists.map(
|
||||
fn {name, arity} = fa ->
|
||||
warn(
|
||||
"cannot define @callback #{name}/#{arity} inside protocol, use def/1 to outline your protocol definition",
|
||||
env,
|
||||
get_callback_line(fa, callback_metas)
|
||||
)
|
||||
end,
|
||||
callbacks -- functions
|
||||
)
|
||||
|
||||
# Macro Callbacks
|
||||
macrocallback_metas = callback_metas(env.module, :macrocallback)
|
||||
macrocallbacks = :maps.keys(macrocallback_metas)
|
||||
|
||||
:lists.map(
|
||||
fn {name, arity} = fa ->
|
||||
warn(
|
||||
"cannot define @macrocallback #{name}/#{arity} inside protocol, use def/1 to outline your protocol definition",
|
||||
env,
|
||||
get_callback_line(fa, macrocallback_metas)
|
||||
)
|
||||
end,
|
||||
macrocallbacks
|
||||
)
|
||||
|
||||
# Optional Callbacks
|
||||
optional_callbacks = Module.get_attribute(env.module, :optional_callbacks)
|
||||
|
||||
if length(optional_callbacks) > 0 do
|
||||
warn(
|
||||
"cannot define @optional_callbacks inside protocol, all of the protocol definitions are required",
|
||||
env,
|
||||
nil
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp after_defprotocol do
|
||||
quote bind_quoted: [built_in: __built_in__()] do
|
||||
any_impl_for =
|
||||
@@ -885,16 +795,16 @@ defmodule Protocol do
|
||||
|
||||
# Store information as an attribute so it
|
||||
# can be read without loading the module.
|
||||
Module.register_attribute(__MODULE__, :__protocol__, persist: true)
|
||||
@__protocol__ [fallback_to_any: !!@fallback_to_any]
|
||||
Module.register_attribute(__MODULE__, :protocol, persist: true)
|
||||
@protocol [fallback_to_any: !!@fallback_to_any]
|
||||
|
||||
@doc false
|
||||
@spec __protocol__(:module) :: __MODULE__
|
||||
@spec __protocol__(:functions) :: unquote(Protocol.__functions_spec__(@__functions__))
|
||||
@spec __protocol__(:functions) :: unquote(Protocol.__functions_spec__(@functions))
|
||||
@spec __protocol__(:consolidated?) :: boolean
|
||||
@spec __protocol__(:impls) :: :not_consolidated | {:consolidated, [module]}
|
||||
Kernel.def(__protocol__(:module), do: __MODULE__)
|
||||
Kernel.def(__protocol__(:functions), do: unquote(:lists.sort(@__functions__)))
|
||||
Kernel.def(__protocol__(:functions), do: unquote(:lists.sort(@functions)))
|
||||
Kernel.def(__protocol__(:consolidated?), do: false)
|
||||
Kernel.def(__protocol__(:impls), do: :not_consolidated)
|
||||
end
|
||||
@@ -945,8 +855,8 @@ defmodule Protocol do
|
||||
|
||||
unquote(block)
|
||||
|
||||
Module.register_attribute(__MODULE__, :__impl__, persist: true)
|
||||
@__impl__ [protocol: @protocol, for: @for]
|
||||
Module.register_attribute(__MODULE__, :protocol_impl, persist: true)
|
||||
@protocol_impl [protocol: @protocol, for: @for]
|
||||
|
||||
unquote(impl)
|
||||
end
|
||||
@@ -987,8 +897,8 @@ defmodule Protocol do
|
||||
else
|
||||
quoted =
|
||||
quote do
|
||||
Module.register_attribute(__MODULE__, :__impl__, persist: true)
|
||||
@__impl__ [protocol: unquote(protocol), for: unquote(for)]
|
||||
Module.register_attribute(__MODULE__, :protocol_impl, persist: true)
|
||||
@protocol_impl [protocol: unquote(protocol), for: unquote(for)]
|
||||
|
||||
@doc false
|
||||
@spec __impl__(:target) :: unquote(impl)
|
||||
|
||||
+59
-256
@@ -1,75 +1,23 @@
|
||||
defmodule Range do
|
||||
@moduledoc """
|
||||
Ranges represent a sequence of zero, one or many, ascending
|
||||
or descending integers with a common difference called step.
|
||||
Ranges represent a sequence of one or many, ascending
|
||||
or descending, consecutive integers.
|
||||
|
||||
Ranges are always inclusive and they may have custom steps.
|
||||
The most common form of creating and matching on ranges is
|
||||
via the [`first..last`](`../2`) and [`first..last//step`](`..///3`)
|
||||
notations, auto-imported from `Kernel`:
|
||||
Ranges can be either increasing (`first <= last`) or
|
||||
decreasing (`first > last`). Ranges are also always
|
||||
inclusive.
|
||||
|
||||
iex> Enum.to_list(1..3)
|
||||
[1, 2, 3]
|
||||
iex> Enum.to_list(1..3//2)
|
||||
[1, 3]
|
||||
iex> Enum.to_list(3..1//-1)
|
||||
[3, 2, 1]
|
||||
A range is represented internally as a struct. However,
|
||||
the most common form of creating and matching on ranges
|
||||
is via the `../2` macro, auto-imported from `Kernel`:
|
||||
|
||||
Ranges may also have a single element:
|
||||
|
||||
iex> Enum.to_list(1..1)
|
||||
[1]
|
||||
iex> Enum.to_list(1..1//2)
|
||||
[1]
|
||||
|
||||
Or even no elements at all:
|
||||
|
||||
iex> Enum.to_list(10..0//1)
|
||||
[]
|
||||
iex> Enum.to_list(0..10//-1)
|
||||
[]
|
||||
|
||||
When defining a range without a step, the step will be
|
||||
defined based on the first and last position of the
|
||||
range, If `first >= last`, it will be an increasing range
|
||||
with a step of 1. Otherwise, it is a decreasing range.
|
||||
Note however implicitly decreasing ranges are deprecated.
|
||||
Therefore, if you need a decreasing range from `3` to `1`,
|
||||
prefer to write `3..1//-1` instead.
|
||||
|
||||
## Definition
|
||||
|
||||
An increasing range `first..last//step` is a range from
|
||||
`first` to `last` increasing by `step` where `step` must be a positive
|
||||
integer and all values `v` must be `first <= v and v <= last`. Therefore, a range
|
||||
`10..0//1` is an empty range because there is no value `v`
|
||||
that is `10 <= v and v <= 0`.
|
||||
|
||||
Similarly, a decreasing range `first..last//step` is a range
|
||||
from `first` to `last` decreasing by `step` where `step` must be a negative
|
||||
integer and values `v` must be `first >= v and v >= last`. Therefore, a range
|
||||
`0..10//-1` is an empty range because there is no value `v`
|
||||
that is `0 >= v and v >= 10`.
|
||||
|
||||
## Representation
|
||||
|
||||
Internally, ranges are represented as structs:
|
||||
|
||||
iex> range = 1..9//2
|
||||
1..9//2
|
||||
iex> first..last//step = range
|
||||
iex> range = 1..3
|
||||
1..3
|
||||
iex> first..last = range
|
||||
iex> first
|
||||
1
|
||||
iex> last
|
||||
9
|
||||
iex> step
|
||||
2
|
||||
iex> range.step
|
||||
2
|
||||
|
||||
You can access the range fields (`first`, `last`, and `step`)
|
||||
directly but you should not modify nor create ranges by hand.
|
||||
Instead use the proper operators or `new/2` and `new/3`.
|
||||
3
|
||||
|
||||
A range implements the `Enumerable` protocol, which means
|
||||
functions in the `Enum` module can be used to work with
|
||||
@@ -92,37 +40,23 @@ defmodule Range do
|
||||
not materialize the whole list of integers.
|
||||
"""
|
||||
|
||||
@enforce_keys [:first, :last, :step]
|
||||
defstruct first: nil, last: nil, step: nil
|
||||
defstruct first: nil, last: nil
|
||||
|
||||
@type limit :: integer
|
||||
@type step :: pos_integer | neg_integer
|
||||
@type t :: %__MODULE__{first: limit, last: limit, step: step}
|
||||
@type t(first, last) :: %__MODULE__{first: first, last: last, step: step}
|
||||
@type t :: %__MODULE__{first: integer, last: integer}
|
||||
@type t(first, last) :: %__MODULE__{first: first, last: last}
|
||||
|
||||
@doc """
|
||||
Creates a new range.
|
||||
|
||||
If `first` is less than `last`, the range will be increasing from
|
||||
`first` to `last`. If `first` is equal to `last`, the range will contain
|
||||
one element, which is the number itself.
|
||||
|
||||
If `first` is greater than `last`, the range will be decreasing from `first`
|
||||
to `last`, albeit this behaviour is deprecated. Therefore, it is advised to
|
||||
explicitly list the step with `new/3`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Range.new(-100, 100)
|
||||
-100..100
|
||||
|
||||
"""
|
||||
|
||||
@spec new(limit, limit) :: t
|
||||
@spec new(integer, integer) :: t
|
||||
def new(first, last) when is_integer(first) and is_integer(last) do
|
||||
# TODO: Deprecate inferring a range with a step of -1 on Elixir v1.17
|
||||
step = if first <= last, do: 1, else: -1
|
||||
%Range{first: first, last: last, step: step}
|
||||
%Range{first: first, last: last}
|
||||
end
|
||||
|
||||
def new(first, last) do
|
||||
@@ -131,66 +65,6 @@ defmodule Range do
|
||||
"got: #{inspect(first)}..#{inspect(last)}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a new range with `step`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Range.new(-100, 100, 2)
|
||||
-100..100//2
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec new(limit, limit, step) :: t
|
||||
def new(first, last, step)
|
||||
when is_integer(first) and is_integer(last) and is_integer(step) and step != 0 do
|
||||
%Range{first: first, last: last, step: step}
|
||||
end
|
||||
|
||||
def new(first, last, step) do
|
||||
raise ArgumentError,
|
||||
"ranges (first..last//step) expect both sides to be integers and the step to be a " <>
|
||||
"non-zero integer, got: #{inspect(first)}..#{inspect(last)}//#{inspect(step)}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the size of `range`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Range.size(1..10)
|
||||
10
|
||||
iex> Range.size(1..10//2)
|
||||
5
|
||||
iex> Range.size(1..10//3)
|
||||
4
|
||||
iex> Range.size(1..10//-1)
|
||||
0
|
||||
|
||||
iex> Range.size(10..1)
|
||||
10
|
||||
iex> Range.size(10..1//-1)
|
||||
10
|
||||
iex> Range.size(10..1//-2)
|
||||
5
|
||||
iex> Range.size(10..1//-3)
|
||||
4
|
||||
iex> Range.size(10..1//1)
|
||||
0
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
def size(range)
|
||||
def size(first..last//step) when step > 0 and first > last, do: 0
|
||||
def size(first..last//step) when step < 0 and first < last, do: 0
|
||||
def size(first..last//step), do: abs(div(last - first, step)) + 1
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def size(%{__struct__: Range, first: first, last: last} = range) do
|
||||
step = if first <= last, do: 1, else: -1
|
||||
size(Map.put(range, :step, step))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two ranges are disjoint.
|
||||
|
||||
@@ -205,161 +79,90 @@ defmodule Range do
|
||||
iex> Range.disjoint?(1..5, 2..7)
|
||||
false
|
||||
|
||||
Steps are also considered when computing the ranges to be disjoint:
|
||||
|
||||
iex> Range.disjoint?(1..10//2, 2..10//2)
|
||||
true
|
||||
|
||||
# First element in common in all below is 29
|
||||
iex> Range.disjoint?(2..100//3, 9..100//5)
|
||||
false
|
||||
iex> Range.disjoint?(101..2//-3, 99..9//-5)
|
||||
false
|
||||
iex> Range.disjoint?(1..100//14, 8..100//21)
|
||||
false
|
||||
iex> Range.disjoint?(57..-1//-14, 8..100//21)
|
||||
false
|
||||
iex> Range.disjoint?(1..100//14, 51..8//-21)
|
||||
false
|
||||
|
||||
# If 29 is out of range
|
||||
iex> Range.disjoint?(1..28//14, 8..28//21)
|
||||
true
|
||||
iex> Range.disjoint?(2..28//3, 9..28//5)
|
||||
true
|
||||
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec disjoint?(t, t) :: boolean
|
||||
def disjoint?(first1..last1//step1 = range1, first2..last2//step2 = range2) do
|
||||
if size(range1) == 0 or size(range2) == 0 do
|
||||
true
|
||||
else
|
||||
{first1, last1, step1} = normalize(first1, last1, step1)
|
||||
{first2, last2, step2} = normalize(first2, last2, step2)
|
||||
|
||||
cond do
|
||||
last2 < first1 or last1 < first2 ->
|
||||
true
|
||||
|
||||
abs(step1) == 1 and abs(step2) == 1 ->
|
||||
false
|
||||
|
||||
true ->
|
||||
# We need to find the first intersection of two arithmetic
|
||||
# progressions and see if they belong within the ranges
|
||||
# https://math.stackexchange.com/questions/1656120/formula-to-find-the-first-intersection-of-two-arithmetic-progressions
|
||||
{gcd, u, v} = Integer.extended_gcd(-step1, step2)
|
||||
c = first1 - first2 + step2 - step1
|
||||
t1 = -c / step1 * u
|
||||
t2 = -c / step2 * v
|
||||
t = max(floor(t1) + 1, floor(t2) + 1)
|
||||
x = div(c * u + t * step2, gcd) - 1
|
||||
y = div(c * v + t * step1, gcd) - 1
|
||||
|
||||
x < 0 or first1 + x * step1 > last1 or
|
||||
y < 0 or first2 + y * step2 > last2
|
||||
end
|
||||
end
|
||||
def disjoint?(first1..last1 = _range1, first2..last2 = _range2) do
|
||||
{first1, last1} = normalize(first1, last1)
|
||||
{first2, last2} = normalize(first2, last2)
|
||||
last2 < first1 or last1 < first2
|
||||
end
|
||||
|
||||
@compile inline: [normalize: 3]
|
||||
defp normalize(first, last, step) when first > last, do: {last, first, -step}
|
||||
defp normalize(first, last, step), do: {first, last, step}
|
||||
@compile inline: [normalize: 2]
|
||||
defp normalize(first, last) when first > last, do: {last, first}
|
||||
defp normalize(first, last), do: {first, last}
|
||||
|
||||
@doc false
|
||||
@deprecated "Pattern match on first..last//step instead"
|
||||
@deprecated "Pattern match on first..last instead"
|
||||
def range?(term)
|
||||
def range?(first..last) when is_integer(first) and is_integer(last), do: true
|
||||
def range?(_), do: false
|
||||
end
|
||||
|
||||
defimpl Enumerable, for: Range do
|
||||
def reduce(first..last//step, acc, fun) do
|
||||
reduce(first, last, acc, fun, step)
|
||||
def reduce(first..last, acc, fun) do
|
||||
reduce(first, last, acc, fun, _up? = last >= first)
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def reduce(%{__struct__: Range, first: first, last: last} = range, acc, fun) do
|
||||
step = if first <= last, do: 1, else: -1
|
||||
reduce(Map.put(range, :step, step), acc, fun)
|
||||
end
|
||||
|
||||
defp reduce(_first, _last, {:halt, acc}, _fun, _step) do
|
||||
defp reduce(_first, _last, {:halt, acc}, _fun, _up?) do
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp reduce(first, last, {:suspend, acc}, fun, step) do
|
||||
{:suspended, acc, &reduce(first, last, &1, fun, step)}
|
||||
defp reduce(first, last, {:suspend, acc}, fun, up?) do
|
||||
{:suspended, acc, &reduce(first, last, &1, fun, up?)}
|
||||
end
|
||||
|
||||
defp reduce(first, last, {:cont, acc}, fun, step)
|
||||
when step > 0 and first <= last
|
||||
when step < 0 and first >= last do
|
||||
reduce(first + step, last, fun.(first, acc), fun, step)
|
||||
defp reduce(first, last, {:cont, acc}, fun, _up? = true) when first <= last do
|
||||
reduce(first + 1, last, fun.(first, acc), fun, _up? = true)
|
||||
end
|
||||
|
||||
defp reduce(first, last, {:cont, acc}, fun, _up? = false) when first >= last do
|
||||
reduce(first - 1, last, fun.(first, acc), fun, _up? = false)
|
||||
end
|
||||
|
||||
defp reduce(_, _, {:cont, acc}, _fun, _up) do
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
def member?(first..last//step = range, value) when is_integer(value) do
|
||||
cond do
|
||||
Range.size(range) == 0 ->
|
||||
{:ok, false}
|
||||
|
||||
first <= last ->
|
||||
{:ok, first <= value and value <= last and rem(value - first, step) == 0}
|
||||
|
||||
true ->
|
||||
{:ok, last <= value and value <= first and rem(value - first, step) == 0}
|
||||
def member?(first..last, value) when is_integer(value) do
|
||||
if first <= last do
|
||||
{:ok, first <= value and value <= last}
|
||||
else
|
||||
{:ok, last <= value and value <= first}
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def member?(%{__struct__: Range, first: first, last: last} = range, value)
|
||||
when is_integer(value) do
|
||||
step = if first <= last, do: 1, else: -1
|
||||
member?(Map.put(range, :step, step), value)
|
||||
end
|
||||
|
||||
def member?(_, _value) do
|
||||
def member?(_.._, _value) do
|
||||
{:ok, false}
|
||||
end
|
||||
|
||||
def count(range) do
|
||||
{:ok, Range.size(range)}
|
||||
def count(first..last) do
|
||||
if first <= last do
|
||||
{:ok, last - first + 1}
|
||||
else
|
||||
{:ok, first - last + 1}
|
||||
end
|
||||
end
|
||||
|
||||
def slice(first.._//step = range) do
|
||||
{:ok, Range.size(range), &slice(first + &1 * step, step, &2)}
|
||||
def slice(first..last) do
|
||||
if first <= last do
|
||||
{:ok, last - first + 1, &slice_asc(first + &1, &2)}
|
||||
else
|
||||
{:ok, first - last + 1, &slice_desc(first - &1, &2)}
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def slice(%{__struct__: Range, first: first, last: last} = range) do
|
||||
step = if first <= last, do: 1, else: -1
|
||||
slice(Map.put(range, :step, step))
|
||||
end
|
||||
defp slice_asc(current, 1), do: [current]
|
||||
defp slice_asc(current, remaining), do: [current | slice_asc(current + 1, remaining - 1)]
|
||||
|
||||
defp slice(current, _step, 1), do: [current]
|
||||
defp slice(current, step, remaining), do: [current | slice(current + step, step, remaining - 1)]
|
||||
defp slice_desc(current, 1), do: [current]
|
||||
defp slice_desc(current, remaining), do: [current | slice_desc(current - 1, remaining - 1)]
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Range do
|
||||
import Inspect.Algebra
|
||||
import Kernel, except: [inspect: 2]
|
||||
|
||||
def inspect(first..last//1, opts) when last >= first do
|
||||
def inspect(first..last, opts) do
|
||||
concat([to_doc(first, opts), "..", to_doc(last, opts)])
|
||||
end
|
||||
|
||||
def inspect(first..last//step, opts) do
|
||||
concat([to_doc(first, opts), "..", to_doc(last, opts), "//", to_doc(step, opts)])
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def inspect(%{__struct__: Range, first: first, last: last} = range, opts) do
|
||||
step = if first <= last, do: 1, else: -1
|
||||
inspect(Map.put(range, :step, step), opts)
|
||||
end
|
||||
end
|
||||
|
||||
+32
-35
@@ -34,12 +34,6 @@ defmodule Record do
|
||||
# expands to: "@type user :: {:user, String.t(), integer}"
|
||||
end
|
||||
|
||||
## Reflection
|
||||
|
||||
A list of all records in a module, if any, can be retrieved by reading the
|
||||
`@__records__` module attribute. It returns a list of maps with the record
|
||||
kind, name, tag, and fields. The attribute is only available inside the
|
||||
module definition.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
@@ -53,8 +47,8 @@ defmodule Record do
|
||||
|
||||
## Options
|
||||
|
||||
This function requires one of the following options, which are exclusive to each
|
||||
other (i.e., only one of them can be used in the same call):
|
||||
This function accepts the following options, which are exclusive to each other
|
||||
(i.e., only one of them can be used in the same call):
|
||||
|
||||
* `:from` - (binary representing a path to a file) path to the Erlang file
|
||||
that contains the record definition to extract; with this option, this
|
||||
@@ -66,8 +60,6 @@ defmodule Record do
|
||||
this function uses the same path lookup used by the `-include_lib`
|
||||
attribute used in Erlang modules.
|
||||
|
||||
It additionally accepts the following optional, non-exclusive options:
|
||||
|
||||
* `:includes` - (a list of directories as binaries) if the record being
|
||||
extracted depends on relative includes, this option allows developers
|
||||
to specify the directory where those relative includes exist.
|
||||
@@ -113,8 +105,21 @@ defmodule Record do
|
||||
|
||||
## Options
|
||||
|
||||
Accepts the same options as listed for `Record.extract/2`.
|
||||
This function accepts the following options, which are exclusive to each other
|
||||
(i.e., only one of them can be used in the same call):
|
||||
|
||||
* `:from` - (binary representing a path to a file) path to the Erlang file
|
||||
that contains the record definitions to extract; with this option, this
|
||||
function uses the same path lookup used by the `-include` attribute used in
|
||||
Erlang modules.
|
||||
|
||||
* `:from_lib` - (binary representing a path to a file) path to the Erlang
|
||||
file that contains the record definitions to extract; with this option,
|
||||
this function uses the same path lookup used by the `-include_lib`
|
||||
attribute used in Erlang modules.
|
||||
|
||||
These options are expected to be literals (including the binary values) at
|
||||
compile time.
|
||||
"""
|
||||
@spec extract_all(keyword) :: [{name :: atom, keyword}]
|
||||
def extract_all(opts) when is_list(opts) do
|
||||
@@ -250,8 +255,10 @@ defmodule Record do
|
||||
"""
|
||||
defmacro defrecord(name, tag \\ nil, kv) do
|
||||
quote bind_quoted: [name: name, tag: tag, kv: kv] do
|
||||
fields = Record.__fields__(:defrecord, kv)
|
||||
Record.__validate__(__MODULE__, name, fields)
|
||||
|
||||
tag = tag || name
|
||||
fields = Record.__record__(__MODULE__, :defrecord, name, tag, kv)
|
||||
|
||||
defmacro unquote(name)(args \\ []) do
|
||||
Record.__access__(unquote(tag), unquote(fields), args, __CALLER__)
|
||||
@@ -268,8 +275,10 @@ defmodule Record do
|
||||
"""
|
||||
defmacro defrecordp(name, tag \\ nil, kv) do
|
||||
quote bind_quoted: [name: name, tag: tag, kv: kv] do
|
||||
fields = Record.__fields__(:defrecordp, kv)
|
||||
Record.__validate__(__MODULE__, name, fields)
|
||||
|
||||
tag = tag || name
|
||||
fields = Record.__record__(__MODULE__, :defrecordp, name, tag, kv)
|
||||
|
||||
defmacrop unquote(name)(args \\ []) do
|
||||
Record.__access__(unquote(tag), unquote(fields), args, __CALLER__)
|
||||
@@ -281,6 +290,13 @@ defmodule Record do
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __validate__(module, name, fields) do
|
||||
error_on_duplicate_record(module, name)
|
||||
# TODO: Make it raise on v2.0
|
||||
warn_on_duplicate_key(:lists.keysort(1, fields))
|
||||
end
|
||||
|
||||
defp error_on_duplicate_record(module, name) do
|
||||
defined_arity =
|
||||
Enum.find(0..2, fn arity ->
|
||||
@@ -306,28 +322,9 @@ defmodule Record do
|
||||
warn_on_duplicate_key(rest)
|
||||
end
|
||||
|
||||
# Callback invoked from the record/2 macro.
|
||||
@doc false
|
||||
def __record__(module, kind, name, tag, kv) do
|
||||
error_on_duplicate_record(module, name)
|
||||
|
||||
fields = fields(kind, kv)
|
||||
Module.register_attribute(module, :__records__, accumulate: true)
|
||||
|
||||
Module.put_attribute(module, :__records__, %{
|
||||
kind: kind,
|
||||
name: name,
|
||||
tag: tag,
|
||||
fields: :lists.map(&elem(&1, 0), fields)
|
||||
})
|
||||
|
||||
# TODO: Make it raise on v2.0
|
||||
warn_on_duplicate_key(:lists.keysort(1, fields))
|
||||
fields
|
||||
end
|
||||
|
||||
# Normalizes of record fields to have default values.
|
||||
defp fields(kind, fields) do
|
||||
@doc false
|
||||
def __fields__(type, fields) do
|
||||
normalizer_fun = fn
|
||||
{key, value} when is_atom(key) ->
|
||||
try do
|
||||
@@ -343,7 +340,7 @@ defmodule Record do
|
||||
{key, nil}
|
||||
|
||||
other ->
|
||||
raise ArgumentError, "#{kind} fields must be atoms, got: #{inspect(other)}"
|
||||
raise ArgumentError, "#{type} fields must be atoms, got: #{inspect(other)}"
|
||||
end
|
||||
|
||||
:lists.map(normalizer_fun, fields)
|
||||
|
||||
+23
-32
@@ -4,7 +4,7 @@ defmodule Regex do
|
||||
|
||||
Regex is based on PCRE (Perl Compatible Regular Expressions) and
|
||||
built on top of Erlang's `:re` module. More information can be found
|
||||
in the [`:re` module documentation](`:re`).
|
||||
in the [`:re` module documentation](http://www.erlang.org/doc/man/re.html).
|
||||
|
||||
Regular expressions in Elixir can be created using the sigils
|
||||
`~r` (see `Kernel.sigil_r/2`) or `~R` (see `Kernel.sigil_R/2`):
|
||||
@@ -107,6 +107,7 @@ defmodule Regex do
|
||||
|
||||
* alnum - Letters and digits
|
||||
* alpha - Letters
|
||||
* ascii - Character codes 0-127
|
||||
* blank - Space or tab only
|
||||
* cntrl - Control characters
|
||||
* digit - Decimal digits (same as \\d)
|
||||
@@ -119,11 +120,6 @@ defmodule Regex do
|
||||
* word - "Word" characters (same as \w)
|
||||
* xdigit - Hexadecimal digits
|
||||
|
||||
There is another character class, `ascii`, that erroneously matches
|
||||
Latin-1 characters instead of the 0-127 range specified by POSIX. This
|
||||
cannot be fixed without altering the behaviour of other classes, so we
|
||||
recommend matching the range with `[\\0-\x7f]` instead.
|
||||
|
||||
Note the behaviour of those classes may change according to the Unicode
|
||||
and other modifiers:
|
||||
|
||||
@@ -274,9 +270,6 @@ defmodule Regex do
|
||||
iex> Regex.match?(~r/foo/, "bar")
|
||||
false
|
||||
|
||||
Elixir also provides `Kernel.=~/2` and `String.match?/2` as
|
||||
an alternative to test strings against regular expressions and
|
||||
strings.
|
||||
"""
|
||||
@spec match?(t, String.t()) :: boolean
|
||||
def match?(%Regex{} = regex, string) when is_binary(string) do
|
||||
@@ -303,8 +296,6 @@ defmodule Regex do
|
||||
Defaults to `:binary`.
|
||||
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
|
||||
to see the possible capture values.
|
||||
* `:offset` - (since v1.12.0) specifies the starting offset to match in the given string.
|
||||
Defaults to zero.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -324,9 +315,8 @@ defmodule Regex do
|
||||
def run(%Regex{} = regex, string, options) when is_binary(string) do
|
||||
return = Keyword.get(options, :return, :binary)
|
||||
captures = Keyword.get(options, :capture, :all)
|
||||
offset = Keyword.get(options, :offset, 0)
|
||||
|
||||
case safe_run(regex, string, [{:capture, captures, return}, {:offset, offset}]) do
|
||||
case safe_run(regex, string, [{:capture, captures, return}]) do
|
||||
:nomatch -> nil
|
||||
:match -> []
|
||||
{:match, results} -> results
|
||||
@@ -374,7 +364,7 @@ defmodule Regex do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Regex.source(~r/foo/)
|
||||
iex> Regex.source(~r(foo))
|
||||
"foo"
|
||||
|
||||
"""
|
||||
@@ -388,7 +378,7 @@ defmodule Regex do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Regex.opts(~r/foo/m)
|
||||
iex> Regex.opts(~r(foo)m)
|
||||
"m"
|
||||
|
||||
"""
|
||||
@@ -435,8 +425,6 @@ defmodule Regex do
|
||||
Defaults to `:binary`.
|
||||
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
|
||||
to see the possible capture values.
|
||||
* `:offset` - (since v1.12.0) specifies the starting offset to match in the given string.
|
||||
Defaults to zero.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -456,14 +444,13 @@ defmodule Regex do
|
||||
[[{0, 1}], [{9, 3}]]
|
||||
|
||||
"""
|
||||
@spec scan(t(), String.t(), [term()]) :: [[String.t()]] | [[{integer(), integer()}]]
|
||||
@spec scan(t, String.t(), [term]) :: [[String.t()]]
|
||||
def scan(regex, string, options \\ [])
|
||||
|
||||
def scan(%Regex{} = regex, string, options) when is_binary(string) do
|
||||
return = Keyword.get(options, :return, :binary)
|
||||
captures = Keyword.get(options, :capture, :all)
|
||||
offset = Keyword.get(options, :offset, 0)
|
||||
options = [{:capture, captures, return}, :global, {:offset, offset}]
|
||||
options = [{:capture, captures, return}, :global]
|
||||
|
||||
case safe_run(regex, string, options) do
|
||||
:match -> []
|
||||
@@ -656,8 +643,20 @@ defmodule Regex do
|
||||
|
||||
"""
|
||||
@spec replace(t, String.t(), String.t() | (... -> String.t()), [term]) :: String.t()
|
||||
def replace(%Regex{} = regex, string, replacement, options \\ [])
|
||||
when is_binary(string) and is_list(options) do
|
||||
def replace(regex, string, replacement, options \\ [])
|
||||
|
||||
def replace(regex, string, replacement, options)
|
||||
when is_binary(string) and is_binary(replacement) and is_list(options) do
|
||||
do_replace(regex, string, precompile_replacement(replacement), options)
|
||||
end
|
||||
|
||||
def replace(regex, string, replacement, options)
|
||||
when is_binary(string) and is_function(replacement) and is_list(options) do
|
||||
{:arity, arity} = Function.info(replacement, :arity)
|
||||
do_replace(regex, string, {replacement, arity}, options)
|
||||
end
|
||||
|
||||
defp do_replace(%Regex{} = regex, string, replacement, options) do
|
||||
opts = if Keyword.get(options, :global) != false, do: [:global], else: []
|
||||
opts = [{:capture, :all, :index} | opts]
|
||||
|
||||
@@ -666,20 +665,13 @@ defmodule Regex do
|
||||
string
|
||||
|
||||
{:match, [mlist | t]} when is_list(mlist) ->
|
||||
apply_list(string, precompile_replacement(replacement), [mlist | t])
|
||||
|> IO.iodata_to_binary()
|
||||
apply_list(string, replacement, [mlist | t]) |> IO.iodata_to_binary()
|
||||
|
||||
{:match, slist} ->
|
||||
apply_list(string, precompile_replacement(replacement), [slist])
|
||||
|> IO.iodata_to_binary()
|
||||
apply_list(string, replacement, [slist]) |> IO.iodata_to_binary()
|
||||
end
|
||||
end
|
||||
|
||||
defp precompile_replacement(replacement) when is_function(replacement) do
|
||||
{:arity, arity} = Function.info(replacement, :arity)
|
||||
{replacement, arity}
|
||||
end
|
||||
|
||||
defp precompile_replacement(""), do: []
|
||||
|
||||
defp precompile_replacement(<<?\\, ?g, ?{, rest::binary>>) when byte_size(rest) > 0 do
|
||||
@@ -831,7 +823,6 @@ defmodule Regex do
|
||||
|
||||
@doc false
|
||||
# Unescape map function used by Macro.unescape_string.
|
||||
def unescape_map(:newline), do: true
|
||||
def unescape_map(?f), do: ?\f
|
||||
def unescape_map(?n), do: ?\n
|
||||
def unescape_map(?r), do: ?\r
|
||||
|
||||
+17
-85
@@ -354,20 +354,13 @@ defmodule Registry do
|
||||
"expected :listeners to be a list of named processes, got: #{inspect(listeners)}"
|
||||
end
|
||||
|
||||
compressed = Keyword.get(options, :compressed, false)
|
||||
|
||||
unless is_boolean(compressed) do
|
||||
raise ArgumentError,
|
||||
"expected :compressed to be a boolean, got: #{inspect(compressed)}"
|
||||
end
|
||||
|
||||
# The @info format must be kept in sync with Registry.Partition optimization.
|
||||
entries = [
|
||||
{@all_info, {keys, partitions, nil, nil, listeners}},
|
||||
{@key_info, {keys, partitions, nil}} | meta
|
||||
]
|
||||
|
||||
Registry.Supervisor.start_link(keys, name, partitions, listeners, entries, compressed)
|
||||
Registry.Supervisor.start_link(keys, name, partitions, listeners, entries)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -731,64 +724,6 @@ defmodule Registry do
|
||||
acc
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads the values for the given `key` for `pid` in `registry`.
|
||||
|
||||
For unique registries, it is either an empty list or a list
|
||||
with a single element. For duplicate registries, it is a list
|
||||
with zero, one, or multiple elements.
|
||||
|
||||
## Examples
|
||||
|
||||
In the example below we register the current process and look it up
|
||||
both from itself and other processes:
|
||||
|
||||
iex> Registry.start_link(keys: :unique, name: Registry.UniqueLookupTest)
|
||||
iex> Registry.values(Registry.UniqueLookupTest, "hello", self())
|
||||
[]
|
||||
iex> {:ok, _} = Registry.register(Registry.UniqueLookupTest, "hello", :world)
|
||||
iex> Registry.values(Registry.UniqueLookupTest, "hello", self())
|
||||
[:world]
|
||||
iex> Task.async(fn -> Registry.values(Registry.UniqueLookupTest, "hello", self()) end) |> Task.await()
|
||||
[]
|
||||
iex> parent = self()
|
||||
iex> Task.async(fn -> Registry.values(Registry.UniqueLookupTest, "hello", parent) end) |> Task.await()
|
||||
[:world]
|
||||
|
||||
The same applies to duplicate registries:
|
||||
|
||||
iex> Registry.start_link(keys: :duplicate, name: Registry.DuplicateLookupTest)
|
||||
iex> Registry.values(Registry.DuplicateLookupTest, "hello", self())
|
||||
[]
|
||||
iex> {:ok, _} = Registry.register(Registry.DuplicateLookupTest, "hello", :world)
|
||||
iex> Registry.values(Registry.DuplicateLookupTest, "hello", self())
|
||||
[:world]
|
||||
iex> {:ok, _} = Registry.register(Registry.DuplicateLookupTest, "hello", :another)
|
||||
iex> Enum.sort(Registry.values(Registry.DuplicateLookupTest, "hello", self()))
|
||||
[:another, :world]
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec values(registry, key, pid) :: [value]
|
||||
def values(registry, key, pid) when is_atom(registry) do
|
||||
case key_info!(registry) do
|
||||
{:unique, partitions, key_ets} ->
|
||||
key_ets = key_ets || key_ets!(registry, key, partitions)
|
||||
|
||||
case safe_lookup_second(key_ets, key) do
|
||||
{^pid, value} ->
|
||||
[value]
|
||||
|
||||
_ ->
|
||||
[]
|
||||
end
|
||||
|
||||
{:duplicate, partitions, key_ets} ->
|
||||
key_ets = key_ets || key_ets!(registry, pid, partitions)
|
||||
for {^pid, value} <- safe_lookup_second(key_ets, key), do: value
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Unregisters all entries for the given `key` associated to the current
|
||||
process in `registry`.
|
||||
@@ -1061,9 +996,7 @@ defmodule Registry do
|
||||
:ets.lookup(registry, key)
|
||||
catch
|
||||
:error, :badarg ->
|
||||
raise ArgumentError,
|
||||
"unknown registry: #{inspect(registry)}. Either the registry name is invalid or " <>
|
||||
"the registry is not running, possibly because its application isn't started"
|
||||
raise ArgumentError, "unknown registry: #{inspect(registry)}"
|
||||
else
|
||||
[{^key, value}] -> {:ok, value}
|
||||
_ -> :error
|
||||
@@ -1406,12 +1339,12 @@ defmodule Registry.Supervisor do
|
||||
@moduledoc false
|
||||
use Supervisor
|
||||
|
||||
def start_link(kind, registry, partitions, listeners, entries, compressed) do
|
||||
arg = {kind, registry, partitions, listeners, entries, compressed}
|
||||
def start_link(kind, registry, partitions, listeners, entries) do
|
||||
arg = {kind, registry, partitions, listeners, entries}
|
||||
Supervisor.start_link(__MODULE__, arg, name: registry)
|
||||
end
|
||||
|
||||
def init({kind, registry, partitions, listeners, entries, compressed}) do
|
||||
def init({kind, registry, partitions, listeners, entries}) do
|
||||
^registry = :ets.new(registry, [:set, :public, :named_table, read_concurrency: true])
|
||||
true = :ets.insert(registry, entries)
|
||||
|
||||
@@ -1419,7 +1352,7 @@ defmodule Registry.Supervisor do
|
||||
for i <- 0..(partitions - 1) do
|
||||
key_partition = Registry.Partition.key_name(registry, i)
|
||||
pid_partition = Registry.Partition.pid_name(registry, i)
|
||||
arg = {kind, registry, i, partitions, key_partition, pid_partition, listeners, compressed}
|
||||
arg = {kind, registry, i, partitions, key_partition, pid_partition, listeners}
|
||||
|
||||
%{
|
||||
id: pid_partition,
|
||||
@@ -1479,9 +1412,9 @@ defmodule Registry.Partition do
|
||||
|
||||
## Callbacks
|
||||
|
||||
def init({kind, registry, i, partitions, key_partition, pid_partition, listeners, compressed}) do
|
||||
def init({kind, registry, i, partitions, key_partition, pid_partition, listeners}) do
|
||||
Process.flag(:trap_exit, true)
|
||||
key_ets = init_key_ets(kind, key_partition, compressed)
|
||||
key_ets = init_key_ets(kind, key_partition)
|
||||
pid_ets = init_pid_ets(kind, pid_partition)
|
||||
|
||||
# If we have only one partition, we do an optimization which
|
||||
@@ -1502,18 +1435,17 @@ defmodule Registry.Partition do
|
||||
|
||||
# The key partition is a set for unique keys,
|
||||
# duplicate bag for duplicate ones.
|
||||
defp init_key_ets(:unique, key_partition, compressed) do
|
||||
opts = [:set, :public, read_concurrency: true, write_concurrency: true]
|
||||
:ets.new(key_partition, compression_opt(opts, compressed))
|
||||
defp init_key_ets(:unique, key_partition) do
|
||||
:ets.new(key_partition, [:set, :public, read_concurrency: true, write_concurrency: true])
|
||||
end
|
||||
|
||||
defp init_key_ets(:duplicate, key_partition, compressed) do
|
||||
opts = [:duplicate_bag, :public, read_concurrency: true, write_concurrency: true]
|
||||
:ets.new(key_partition, compression_opt(opts, compressed))
|
||||
end
|
||||
|
||||
defp compression_opt(opts, compressed) do
|
||||
if compressed, do: [:compressed] ++ opts, else: opts
|
||||
defp init_key_ets(:duplicate, key_partition) do
|
||||
:ets.new(key_partition, [
|
||||
:duplicate_bag,
|
||||
:public,
|
||||
read_concurrency: true,
|
||||
write_concurrency: true
|
||||
])
|
||||
end
|
||||
|
||||
# A process can always have multiple keys, so the
|
||||
|
||||
+41
-151
@@ -1105,9 +1105,9 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Zips two enumerables together, lazily.
|
||||
Zips two collections together, lazily.
|
||||
|
||||
The zipping finishes as soon as either enumerable completes.
|
||||
The zipping finishes as soon as any enumerable completes.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1118,9 +1118,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec zip(Enumerable.t(), Enumerable.t()) :: Enumerable.t()
|
||||
def zip(enumerable1, enumerable2) do
|
||||
zip_with(enumerable1, enumerable2, fn left, right -> {left, right} end)
|
||||
end
|
||||
def zip(left, right), do: zip([left, right])
|
||||
|
||||
@doc """
|
||||
Zips corresponding elements from a finite collection of enumerables
|
||||
@@ -1139,175 +1137,69 @@ defmodule Stream do
|
||||
@doc since: "1.4.0"
|
||||
@spec zip(enumerables) :: Enumerable.t() when enumerables: [Enumerable.t()] | Enumerable.t()
|
||||
def zip(enumerables) do
|
||||
zip_with(enumerables, &List.to_tuple(&1))
|
||||
&prepare_zip(enumerables, &1, &2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily zips corresponding elements from two enumerables into a new one, transforming them with
|
||||
the `zip_fun` function as it goes.
|
||||
|
||||
The `zip_fun` will be called with the first element from `enumerable1` and the first
|
||||
element from `enumerable2`, then with the second element from each, and so on until
|
||||
either one of the enumerables completes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> concat = Stream.concat(1..3, 4..6)
|
||||
iex> Stream.zip_with(concat, concat, fn a, b -> a + b end) |> Enum.to_list()
|
||||
[2, 4, 6, 8, 10, 12]
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec zip_with(Enumerable.t(), Enumerable.t(), (term, term -> term)) :: Enumerable.t()
|
||||
def zip_with(enumerable1, enumerable2, zip_fun)
|
||||
when is_list(enumerable1) and is_list(enumerable2) and is_function(zip_fun, 2) do
|
||||
&zip_pair(enumerable1, enumerable2, &1, &2, zip_fun)
|
||||
end
|
||||
|
||||
def zip_with(enumerable1, enumerable2, zip_fun) when is_function(zip_fun, 2) do
|
||||
zip_with([enumerable1, enumerable2], fn [left, right] -> zip_fun.(left, right) end)
|
||||
end
|
||||
|
||||
defp zip_pair(_list1, _list2, {:halt, acc}, _fun, _zip_fun) do
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp zip_pair(list1, list2, {:suspend, acc}, fun, zip_fun) do
|
||||
{:suspended, acc, &zip_pair(list1, list2, &1, fun, zip_fun)}
|
||||
end
|
||||
|
||||
defp zip_pair([], _list2, {:cont, acc}, _fun, _zip_fun), do: {:done, acc}
|
||||
defp zip_pair(_list1, [], {:cont, acc}, _fun, _zip_fun), do: {:done, acc}
|
||||
|
||||
defp zip_pair([head1 | tail1], [head2 | tail2], {:cont, acc}, fun, zip_fun) do
|
||||
zip_pair(tail1, tail2, fun.(zip_fun.(head1, head2), acc), fun, zip_fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily zips corresponding elements from a finite collection of enumerables into a new
|
||||
enumerable, transforming them with the `zip_fun` function as it goes.
|
||||
|
||||
The first element from each of the enums in `enumerables` will be put into a list which is then passed to
|
||||
the 1-arity `zip_fun` function. Then, the second elements from each of the enums are put into a list and passed to
|
||||
`zip_fun`, and so on until any one of the enums in `enumerables` completes.
|
||||
|
||||
Returns a new enumerable with the results of calling `zip_fun`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> concat = Stream.concat(1..3, 4..6)
|
||||
iex> Stream.zip_with([concat, concat], fn [a, b] -> a + b end) |> Enum.to_list()
|
||||
[2, 4, 6, 8, 10, 12]
|
||||
|
||||
iex> concat = Stream.concat(1..3, 4..6)
|
||||
iex> Stream.zip_with([concat, concat, 1..3], fn [a, b, c] -> a + b + c end) |> Enum.to_list()
|
||||
[3, 6, 9]
|
||||
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec zip_with(enumerables, (Enumerable.t() -> term)) :: Enumerable.t()
|
||||
when enumerables: [Enumerable.t()] | Enumerable.t()
|
||||
def zip_with(enumerables, zip_fun) when is_function(zip_fun, 1) do
|
||||
if is_list(enumerables) and :lists.all(&is_list/1, enumerables) do
|
||||
&zip_list(enumerables, &1, &2, zip_fun)
|
||||
else
|
||||
&zip_enum(enumerables, &1, &2, zip_fun)
|
||||
end
|
||||
end
|
||||
|
||||
defp zip_list(_enumerables, {:halt, acc}, _fun, _zip_fun) do
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp zip_list(enumerables, {:suspend, acc}, fun, zip_fun) do
|
||||
{:suspended, acc, &zip_list(enumerables, &1, fun, zip_fun)}
|
||||
end
|
||||
|
||||
defp zip_list(enumerables, {:cont, acc}, fun, zip_fun) do
|
||||
case zip_list_heads_tails(enumerables, [], []) do
|
||||
{heads, tails} -> zip_list(tails, fun.(zip_fun.(heads), acc), fun, zip_fun)
|
||||
:error -> {:done, acc}
|
||||
end
|
||||
end
|
||||
|
||||
defp zip_list_heads_tails([[head | tail] | rest], heads, tails) do
|
||||
zip_list_heads_tails(rest, [head | heads], [tail | tails])
|
||||
end
|
||||
|
||||
defp zip_list_heads_tails([[] | _rest], _heads, _tails) do
|
||||
:error
|
||||
end
|
||||
|
||||
defp zip_list_heads_tails([], heads, tails) do
|
||||
{:lists.reverse(heads), :lists.reverse(tails)}
|
||||
end
|
||||
|
||||
defp zip_enum(enumerables, acc, fun, zip_fun) do
|
||||
step = fn x, acc ->
|
||||
{:suspend, :lists.reverse([x | acc])}
|
||||
end
|
||||
defp prepare_zip(enumerables, acc, fun) do
|
||||
step = &do_zip_step(&1, &2)
|
||||
|
||||
enum_funs =
|
||||
Enum.map(enumerables, fn enum ->
|
||||
{&Enumerable.reduce(enum, &1, step), [], :cont}
|
||||
end)
|
||||
|
||||
do_zip_enum(enum_funs, acc, fun, zip_fun)
|
||||
do_zip(enum_funs, acc, fun)
|
||||
end
|
||||
|
||||
# This implementation of do_zip_enum/4 works for any number of streams to zip
|
||||
defp do_zip_enum(zips, {:halt, acc}, _fun, _zip_fun) do
|
||||
# This implementation of do_zip/3 works for any number of
|
||||
# streams to zip, even if right now zip/2 only zips two streams.
|
||||
|
||||
defp do_zip(zips, {:halt, acc}, _fun) do
|
||||
do_zip_close(zips)
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp do_zip_enum(zips, {:suspend, acc}, fun, zip_fun) do
|
||||
{:suspended, acc, &do_zip_enum(zips, &1, fun, zip_fun)}
|
||||
defp do_zip(zips, {:suspend, acc}, fun) do
|
||||
{:suspended, acc, &do_zip(zips, &1, fun)}
|
||||
end
|
||||
|
||||
defp do_zip_enum([], {:cont, acc}, _callback, _zip_fun) do
|
||||
defp do_zip([], {:cont, acc}, _callback) do
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
defp do_zip_enum(zips, {:cont, acc}, callback, zip_fun) do
|
||||
defp do_zip(zips, {:cont, acc}, callback) do
|
||||
try do
|
||||
do_zip_next(zips, acc, callback, [], [], zip_fun)
|
||||
do_zip_next_tuple(zips, acc, callback, [], [])
|
||||
catch
|
||||
kind, reason ->
|
||||
do_zip_close(zips)
|
||||
:erlang.raise(kind, reason, __STACKTRACE__)
|
||||
else
|
||||
{:next, buffer, acc} ->
|
||||
do_zip_enum(buffer, acc, callback, zip_fun)
|
||||
do_zip(buffer, acc, callback)
|
||||
|
||||
{:done, _acc} = other ->
|
||||
other
|
||||
end
|
||||
end
|
||||
|
||||
# do_zip_next/6 computes the next tuple formed by
|
||||
# do_zip_next_tuple/5 computes the next tuple formed by
|
||||
# the next element of each zipped stream.
|
||||
defp do_zip_next(
|
||||
[{_, [], :halt} | zips],
|
||||
acc,
|
||||
_callback,
|
||||
_yielded_elems,
|
||||
buffer,
|
||||
_zip_fun
|
||||
) do
|
||||
|
||||
defp do_zip_next_tuple([{_, [], :halt} | zips], acc, _callback, _yielded_elems, buffer) do
|
||||
do_zip_close(:lists.reverse(buffer, zips))
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
defp do_zip_next([{fun, [], :cont} | zips], acc, callback, yielded_elems, buffer, zip_fun) do
|
||||
defp do_zip_next_tuple([{fun, [], :cont} | zips], acc, callback, yielded_elems, buffer) do
|
||||
case fun.({:cont, []}) do
|
||||
{:suspended, [elem | next_acc], fun} ->
|
||||
next_buffer = [{fun, next_acc, :cont} | buffer]
|
||||
do_zip_next(zips, acc, callback, [elem | yielded_elems], next_buffer, zip_fun)
|
||||
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
|
||||
|
||||
{_, [elem | next_acc]} ->
|
||||
next_buffer = [{fun, next_acc, :halt} | buffer]
|
||||
do_zip_next(zips, acc, callback, [elem | yielded_elems], next_buffer, zip_fun)
|
||||
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
|
||||
|
||||
{_, []} ->
|
||||
# The current zipped stream terminated, so we close all the streams
|
||||
@@ -1317,30 +1209,28 @@ defmodule Stream do
|
||||
end
|
||||
end
|
||||
|
||||
defp do_zip_next(
|
||||
[{fun, zip_acc, zip_op} | zips],
|
||||
acc,
|
||||
callback,
|
||||
yielded_elems,
|
||||
buffer,
|
||||
zip_fun
|
||||
) do
|
||||
defp do_zip_next_tuple([{fun, zip_acc, zip_op} | zips], acc, callback, yielded_elems, buffer) do
|
||||
[elem | rest] = zip_acc
|
||||
next_buffer = [{fun, rest, zip_op} | buffer]
|
||||
do_zip_next(zips, acc, callback, [elem | yielded_elems], next_buffer, zip_fun)
|
||||
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
|
||||
end
|
||||
|
||||
defp do_zip_next([] = _zips, acc, callback, yielded_elems, buffer, zip_fun) do
|
||||
defp do_zip_next_tuple([] = _zips, acc, callback, yielded_elems, buffer) do
|
||||
# "yielded_elems" is a reversed list of results for the current iteration of
|
||||
# zipping. That is to say, the nth element from each of the enums being zipped.
|
||||
# It needs to be reversed and passed to the zipping function so it can do it's thing.
|
||||
{:next, :lists.reverse(buffer), callback.(zip_fun.(:lists.reverse(yielded_elems)), acc)}
|
||||
# zipping: it needs to be reversed and converted to a tuple to have the next
|
||||
# tuple in the list resulting from zipping.
|
||||
zipped = List.to_tuple(:lists.reverse(yielded_elems))
|
||||
{:next, :lists.reverse(buffer), callback.(zipped, acc)}
|
||||
end
|
||||
|
||||
defp do_zip_close(zips) do
|
||||
:lists.foreach(fn {fun, _, _} -> fun.({:halt, []}) end, zips)
|
||||
end
|
||||
|
||||
defp do_zip_step(x, acc) do
|
||||
{:suspend, :lists.reverse([x | acc])}
|
||||
end
|
||||
|
||||
## Sources
|
||||
|
||||
@doc """
|
||||
@@ -1358,7 +1248,7 @@ defmodule Stream do
|
||||
def cycle(enumerable)
|
||||
|
||||
def cycle([]) do
|
||||
raise ArgumentError, "cannot cycle over an empty enumerable"
|
||||
raise ArgumentError, "cannot cycle over empty enumerable"
|
||||
end
|
||||
|
||||
def cycle(enumerable) when is_list(enumerable) do
|
||||
@@ -1407,7 +1297,7 @@ defmodule Stream do
|
||||
fn acc ->
|
||||
case reduce.(acc) do
|
||||
{state, []} when state in [:done, :halted] ->
|
||||
raise ArgumentError, "cannot cycle over an empty enumerable"
|
||||
raise ArgumentError, "cannot cycle over empty enumerable"
|
||||
|
||||
other ->
|
||||
other
|
||||
@@ -1443,9 +1333,9 @@ defmodule Stream do
|
||||
## Examples
|
||||
|
||||
# Although not necessary, let's seed the random algorithm
|
||||
iex> :rand.seed(:exsss, {1, 2, 3})
|
||||
iex> :rand.seed(:exrop, {1, 2, 3})
|
||||
iex> Stream.repeatedly(&:rand.uniform/0) |> Enum.take(3)
|
||||
[0.5455598952593053, 0.6039309974353404, 0.6684893034823949]
|
||||
[0.7498295129076106, 0.06161655489244533, 0.7924073127680873]
|
||||
|
||||
"""
|
||||
@spec repeatedly((() -> element)) :: Enumerable.t()
|
||||
@@ -1694,7 +1584,7 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
defimpl Enumerable, for: Stream do
|
||||
@compile :inline_list_funcs
|
||||
@compile :inline_list_funs
|
||||
|
||||
def count(_lazy), do: {:error, __MODULE__}
|
||||
|
||||
@@ -1731,7 +1621,7 @@ defimpl Enumerable, for: Stream do
|
||||
defp do_done({reason, [acc | _]}, nil), do: {reason, acc}
|
||||
|
||||
defp do_done({reason, [acc | t]}, {done, fun}) do
|
||||
[h | _] = :lists.reverse(t)
|
||||
[h | _] = Enum.reverse(t)
|
||||
|
||||
case done.([acc, h], fun) do
|
||||
{:cont, [acc | _]} -> {reason, acc}
|
||||
@@ -1745,7 +1635,7 @@ defimpl Inspect, for: Stream do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(%{enum: enum, funs: funs}, opts) do
|
||||
inner = [enum: enum, funs: :lists.reverse(funs)]
|
||||
inner = [enum: enum, funs: Enum.reverse(funs)]
|
||||
concat(["#Stream<", to_doc(inner, opts), ">"])
|
||||
end
|
||||
end
|
||||
|
||||
+188
-344
@@ -68,8 +68,8 @@ defmodule String do
|
||||
|
||||
## Code points and grapheme cluster
|
||||
|
||||
The functions in this module act according to
|
||||
[The Unicode Standard, Version 14.0.0](http://www.unicode.org/versions/Unicode14.0.0/).
|
||||
The functions in this module act according to the Unicode
|
||||
Standard, version 12.1.0.
|
||||
|
||||
As per the standard, a code point is a single Unicode Character,
|
||||
which may be represented by one or more bytes.
|
||||
@@ -133,7 +133,7 @@ defmodule String do
|
||||
* `Kernel.bit_size/1` and `Kernel.byte_size/1` - size related functions
|
||||
* `Kernel.is_bitstring/1` and `Kernel.is_binary/1` - type-check function
|
||||
* Plus a number of functions for working with binaries (bytes)
|
||||
in the [`:binary` module](`:binary`)
|
||||
in the [`:binary` module](http://www.erlang.org/doc/man/binary.html)
|
||||
|
||||
There are many situations where using the `String` module can
|
||||
be avoided in favor of binary functions or pattern matching.
|
||||
@@ -286,7 +286,7 @@ defmodule String do
|
||||
@typedoc "Pattern used in functions like `replace/4` and `split/3`"
|
||||
@type pattern :: t | [t] | :binary.cp()
|
||||
|
||||
@conditional_mappings [:greek, :turkic]
|
||||
@conditional_mappings [:greek]
|
||||
|
||||
@doc """
|
||||
Checks if a string contains only printable characters up to `character_limit`.
|
||||
@@ -470,11 +470,11 @@ defmodule String do
|
||||
@spec split(t, pattern | Regex.t(), keyword) :: [t]
|
||||
def split(string, pattern, options \\ [])
|
||||
|
||||
def split(string, %Regex{} = pattern, options) when is_binary(string) and is_list(options) do
|
||||
def split(string, %Regex{} = pattern, options) when is_binary(string) do
|
||||
Regex.split(pattern, string, options)
|
||||
end
|
||||
|
||||
def split(string, "", options) when is_binary(string) and is_list(options) do
|
||||
def split(string, "", options) when is_binary(string) do
|
||||
parts = Keyword.get(options, :parts, :infinity)
|
||||
index = parts_to_index(parts)
|
||||
trim = Keyword.get(options, :trim, false)
|
||||
@@ -486,7 +486,7 @@ defmodule String do
|
||||
end
|
||||
end
|
||||
|
||||
def split(string, pattern, options) when is_binary(string) and is_list(options) do
|
||||
def split(string, pattern, options) when is_binary(string) do
|
||||
parts = Keyword.get(options, :parts, :infinity)
|
||||
trim = Keyword.get(options, :trim, false)
|
||||
|
||||
@@ -494,12 +494,6 @@ defmodule String do
|
||||
{:infinity, false} ->
|
||||
:binary.split(string, pattern, [:global])
|
||||
|
||||
{:infinity, true} ->
|
||||
:binary.split(string, pattern, [:global, :trim_all])
|
||||
|
||||
{2, false} ->
|
||||
:binary.split(string, pattern)
|
||||
|
||||
_ ->
|
||||
pattern = maybe_compile_pattern(pattern)
|
||||
split_each(string, pattern, trim, parts_to_index(parts))
|
||||
@@ -513,11 +507,9 @@ defmodule String do
|
||||
defp split_empty(string, _, 1), do: [string]
|
||||
|
||||
defp split_empty(string, trim, count) do
|
||||
case :unicode_util.gc(string) do
|
||||
[gc] -> [grapheme_to_binary(gc) | split_empty(<<>>, trim, count - 1)]
|
||||
[gc | rest] -> [grapheme_to_binary(gc) | split_empty(rest, trim, count - 1)]
|
||||
[] -> split_empty("", trim, 1)
|
||||
{:error, <<byte, rest::bits>>} -> [<<byte>> | split_empty(rest, trim, count - 1)]
|
||||
case next_grapheme(string) do
|
||||
{h, t} -> [h | split_empty(t, trim, count - 1)]
|
||||
nil -> split_empty("", trim, 1)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -567,7 +559,7 @@ defmodule String do
|
||||
@spec splitter(t, pattern, keyword) :: Enumerable.t()
|
||||
def splitter(string, pattern, options \\ [])
|
||||
|
||||
def splitter(string, "", options) when is_binary(string) and is_list(options) do
|
||||
def splitter(string, "", options) do
|
||||
if Keyword.get(options, :trim, false) do
|
||||
Stream.unfold(string, &next_grapheme/1)
|
||||
else
|
||||
@@ -575,7 +567,7 @@ defmodule String do
|
||||
end
|
||||
end
|
||||
|
||||
def splitter(string, pattern, options) when is_binary(string) and is_list(options) do
|
||||
def splitter(string, pattern, options) do
|
||||
pattern = maybe_compile_pattern(pattern)
|
||||
trim = Keyword.get(options, :trim, false)
|
||||
Stream.unfold(string, &do_splitter(&1, pattern, trim))
|
||||
@@ -634,13 +626,11 @@ defmodule String do
|
||||
@spec split_at(t, integer) :: {t, t}
|
||||
def split_at(string, position)
|
||||
|
||||
def split_at(string, position)
|
||||
when is_binary(string) and is_integer(position) and position >= 0 do
|
||||
def split_at(string, position) when is_integer(position) and position >= 0 do
|
||||
do_split_at(string, position)
|
||||
end
|
||||
|
||||
def split_at(string, position)
|
||||
when is_binary(string) and is_integer(position) and position < 0 do
|
||||
def split_at(string, position) when is_integer(position) and position < 0 do
|
||||
position = length(string) + position
|
||||
|
||||
case position >= 0 do
|
||||
@@ -650,14 +640,12 @@ defmodule String do
|
||||
end
|
||||
|
||||
defp do_split_at(string, position) do
|
||||
remaining = byte_size_remaining_at(string, position)
|
||||
start = byte_size(string) - remaining
|
||||
<<left::size(start)-binary, right::size(remaining)-binary>> = string
|
||||
{left, right}
|
||||
{byte_size, rest} = String.Unicode.split_at(string, position)
|
||||
{binary_part(string, 0, byte_size), rest || ""}
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Returns `true` if `string1` is canonically equivalent to `string2`.
|
||||
Returns `true` if `string1` is canonically equivalent to 'string2'.
|
||||
|
||||
It performs Normalization Form Canonical Decomposition (NFD) on the
|
||||
strings before comparing them. This function is equivalent to:
|
||||
@@ -684,7 +672,7 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec equivalent?(t, t) :: boolean
|
||||
def equivalent?(string1, string2) when is_binary(string1) and is_binary(string2) do
|
||||
def equivalent?(string1, string2) do
|
||||
normalize(string1, :nfd) == normalize(string2, :nfd)
|
||||
end
|
||||
|
||||
@@ -740,28 +728,28 @@ defmodule String do
|
||||
"""
|
||||
def normalize(string, form)
|
||||
|
||||
def normalize(string, :nfd) when is_binary(string) do
|
||||
def normalize(string, :nfd) do
|
||||
case :unicode.characters_to_nfd_binary(string) do
|
||||
string when is_binary(string) -> string
|
||||
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfd)
|
||||
end
|
||||
end
|
||||
|
||||
def normalize(string, :nfc) when is_binary(string) do
|
||||
def normalize(string, :nfc) do
|
||||
case :unicode.characters_to_nfc_binary(string) do
|
||||
string when is_binary(string) -> string
|
||||
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfc)
|
||||
end
|
||||
end
|
||||
|
||||
def normalize(string, :nfkd) when is_binary(string) do
|
||||
def normalize(string, :nfkd) do
|
||||
case :unicode.characters_to_nfkd_binary(string) do
|
||||
string when is_binary(string) -> string
|
||||
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfkd)
|
||||
end
|
||||
end
|
||||
|
||||
def normalize(string, :nfkc) when is_binary(string) do
|
||||
def normalize(string, :nfkc) do
|
||||
case :unicode.characters_to_nfkc_binary(string) do
|
||||
string when is_binary(string) -> string
|
||||
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfkc)
|
||||
@@ -771,10 +759,10 @@ defmodule String do
|
||||
@doc """
|
||||
Converts all characters in the given string to uppercase according to `mode`.
|
||||
|
||||
`mode` may be `:default`, `:ascii`, `:greek` or `:turkic`. The `:default` mode considers
|
||||
`mode` may be `:default`, `:ascii` or `:greek`. The `:default` mode considers
|
||||
all non-conditional transformations outlined in the Unicode standard. `:ascii`
|
||||
uppercases only the letters a to z. `:greek` includes the context sensitive
|
||||
mappings found in Greek. `:turkic` properly handles the letter i with the dotless variant.
|
||||
mappings found in Greek.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -794,16 +782,8 @@ defmodule String do
|
||||
iex> String.upcase("olá", :ascii)
|
||||
"OLá"
|
||||
|
||||
And `:turkic` properly handles the letter i with the dotless variant:
|
||||
|
||||
iex> String.upcase("ıi")
|
||||
"II"
|
||||
|
||||
iex> String.upcase("ıi", :turkic)
|
||||
"Iİ"
|
||||
|
||||
"""
|
||||
@spec upcase(t, :default | :ascii | :greek | :turkic) :: t
|
||||
@spec upcase(t, :default | :ascii | :greek) :: t
|
||||
def upcase(string, mode \\ :default)
|
||||
|
||||
def upcase("", _mode) do
|
||||
@@ -811,15 +791,15 @@ defmodule String do
|
||||
end
|
||||
|
||||
def upcase(string, :default) when is_binary(string) do
|
||||
String.Unicode.upcase(string, [], :default)
|
||||
String.Casing.upcase(string, [], :default)
|
||||
end
|
||||
|
||||
def upcase(string, :ascii) when is_binary(string) do
|
||||
IO.iodata_to_binary(upcase_ascii(string))
|
||||
end
|
||||
|
||||
def upcase(string, mode) when is_binary(string) and mode in @conditional_mappings do
|
||||
String.Unicode.upcase(string, [], mode)
|
||||
def upcase(string, mode) when mode in @conditional_mappings do
|
||||
String.Casing.upcase(string, [], mode)
|
||||
end
|
||||
|
||||
defp upcase_ascii(<<char, rest::bits>>) when char >= ?a and char <= ?z,
|
||||
@@ -831,10 +811,10 @@ defmodule String do
|
||||
@doc """
|
||||
Converts all characters in the given string to lowercase according to `mode`.
|
||||
|
||||
`mode` may be `:default`, `:ascii`, `:greek` or `:turkic`. The `:default` mode considers
|
||||
`mode` may be `:default`, `:ascii` or `:greek`. The `:default` mode considers
|
||||
all non-conditional transformations outlined in the Unicode standard. `:ascii`
|
||||
lowercases only the letters A to Z. `:greek` includes the context sensitive
|
||||
mappings found in Greek. `:turkic` properly handles the letter i with the dotless variant.
|
||||
mappings found in Greek.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -854,7 +834,7 @@ defmodule String do
|
||||
iex> String.downcase("OLÁ", :ascii)
|
||||
"olÁ"
|
||||
|
||||
The `:greek` mode properly handles the context sensitive sigma in Greek:
|
||||
And `:greek` properly handles the context sensitive sigma in Greek:
|
||||
|
||||
iex> String.downcase("ΣΣ")
|
||||
"σσ"
|
||||
@@ -862,16 +842,8 @@ defmodule String do
|
||||
iex> String.downcase("ΣΣ", :greek)
|
||||
"σς"
|
||||
|
||||
And `:turkic` properly handles the letter i with the dotless variant:
|
||||
|
||||
iex> String.downcase("Iİ")
|
||||
"ii̇"
|
||||
|
||||
iex> String.downcase("Iİ", :turkic)
|
||||
"ıi"
|
||||
|
||||
"""
|
||||
@spec downcase(t, :default | :ascii | :greek | :turkic) :: t
|
||||
@spec downcase(t, :default | :ascii | :greek) :: t
|
||||
def downcase(string, mode \\ :default)
|
||||
|
||||
def downcase("", _mode) do
|
||||
@@ -879,15 +851,15 @@ defmodule String do
|
||||
end
|
||||
|
||||
def downcase(string, :default) when is_binary(string) do
|
||||
String.Unicode.downcase(string, [], :default)
|
||||
String.Casing.downcase(string, [], :default)
|
||||
end
|
||||
|
||||
def downcase(string, :ascii) when is_binary(string) do
|
||||
IO.iodata_to_binary(downcase_ascii(string))
|
||||
end
|
||||
|
||||
def downcase(string, mode) when is_binary(string) and mode in @conditional_mappings do
|
||||
String.Unicode.downcase(string, [], mode)
|
||||
def downcase(string, mode) when mode in @conditional_mappings do
|
||||
String.Casing.downcase(string, [], mode)
|
||||
end
|
||||
|
||||
defp downcase_ascii(<<char, rest::bits>>) when char >= ?A and char <= ?Z,
|
||||
@@ -900,10 +872,10 @@ defmodule String do
|
||||
Converts the first character in the given string to
|
||||
uppercase and the remainder to lowercase according to `mode`.
|
||||
|
||||
`mode` may be `:default`, `:ascii`, `:greek` or `:turkic`. The `:default` mode considers
|
||||
`mode` may be `:default`, `:ascii` or `:greek`. The `:default` mode considers
|
||||
all non-conditional transformations outlined in the Unicode standard. `:ascii`
|
||||
capitalizes only the letters A to Z. `:greek` includes the context sensitive
|
||||
mappings found in Greek. `:turkic` properly handles the letter i with the dotless variant.
|
||||
lowercases only the letters A to Z. `:greek` includes the context sensitive
|
||||
mappings found in Greek.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -917,7 +889,7 @@ defmodule String do
|
||||
"Olá"
|
||||
|
||||
"""
|
||||
@spec capitalize(t, :default | :ascii | :greek | :turkic) :: t
|
||||
@spec capitalize(t, :default | :ascii | :greek) :: t
|
||||
def capitalize(string, mode \\ :default)
|
||||
|
||||
def capitalize(<<char, rest::binary>>, :ascii) do
|
||||
@@ -926,7 +898,7 @@ defmodule String do
|
||||
end
|
||||
|
||||
def capitalize(string, mode) when is_binary(string) do
|
||||
{char, rest} = String.Unicode.titlecase_once(string, mode)
|
||||
{char, rest} = String.Casing.titlecase_once(string, mode)
|
||||
char <> downcase(rest, mode)
|
||||
end
|
||||
|
||||
@@ -1191,8 +1163,7 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec trim_leading(t, t) :: t
|
||||
def trim_leading(string, to_trim)
|
||||
when is_binary(string) and is_binary(to_trim) do
|
||||
def trim_leading(string, to_trim) do
|
||||
replace_leading(string, to_trim, "")
|
||||
end
|
||||
|
||||
@@ -1222,8 +1193,7 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec trim_trailing(t, t) :: t
|
||||
def trim_trailing(string, to_trim)
|
||||
when is_binary(string) and is_binary(to_trim) do
|
||||
def trim_trailing(string, to_trim) do
|
||||
replace_trailing(string, to_trim, "")
|
||||
end
|
||||
|
||||
@@ -1238,7 +1208,7 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec trim(t) :: t
|
||||
def trim(string) when is_binary(string) do
|
||||
def trim(string) do
|
||||
string
|
||||
|> trim_leading()
|
||||
|> trim_trailing()
|
||||
@@ -1255,7 +1225,7 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec trim(t, t) :: t
|
||||
def trim(string, to_trim) when is_binary(string) and is_binary(to_trim) do
|
||||
def trim(string, to_trim) do
|
||||
string
|
||||
|> trim_leading(to_trim)
|
||||
|> trim_trailing(to_trim)
|
||||
@@ -1492,7 +1462,7 @@ defmodule String do
|
||||
defp replace_guarded(subject, "", replacement_binary, options)
|
||||
when is_binary(replacement_binary) do
|
||||
if Keyword.get(options, :global, true) do
|
||||
intersperse_bin(subject, replacement_binary, [replacement_binary])
|
||||
IO.iodata_to_binary([replacement_binary | intersperse_bin(subject, replacement_binary)])
|
||||
else
|
||||
replacement_binary <> subject
|
||||
end
|
||||
@@ -1500,7 +1470,7 @@ defmodule String do
|
||||
|
||||
defp replace_guarded(subject, "", replacement_fun, options) do
|
||||
if Keyword.get(options, :global, true) do
|
||||
intersperse_fun(subject, replacement_fun, [replacement_fun.("")])
|
||||
IO.iodata_to_binary([replacement_fun.("") | intersperse_fun(subject, replacement_fun)])
|
||||
else
|
||||
IO.iodata_to_binary([replacement_fun.("") | subject])
|
||||
end
|
||||
@@ -1530,31 +1500,17 @@ defmodule String do
|
||||
end
|
||||
end
|
||||
|
||||
defp intersperse_bin(subject, replacement, acc) do
|
||||
case :unicode_util.gc(subject) do
|
||||
[current | rest] ->
|
||||
intersperse_bin(rest, replacement, [replacement, current | acc])
|
||||
|
||||
[] ->
|
||||
reverse_characters_to_binary(acc)
|
||||
|
||||
{:error, <<byte, rest::bits>>} ->
|
||||
reverse_characters_to_binary(acc) <>
|
||||
<<byte>> <> intersperse_bin(rest, replacement, [replacement])
|
||||
defp intersperse_bin(subject, replacement) do
|
||||
case next_grapheme(subject) do
|
||||
{current, rest} -> [current, replacement | intersperse_bin(rest, replacement)]
|
||||
nil -> []
|
||||
end
|
||||
end
|
||||
|
||||
defp intersperse_fun(subject, replacement, acc) do
|
||||
case :unicode_util.gc(subject) do
|
||||
[current | rest] ->
|
||||
intersperse_fun(rest, replacement, [replacement.(""), current | acc])
|
||||
|
||||
[] ->
|
||||
reverse_characters_to_binary(acc)
|
||||
|
||||
{:error, <<byte, rest::bits>>} ->
|
||||
reverse_characters_to_binary(acc) <>
|
||||
<<byte>> <> intersperse_fun(rest, replacement, [replacement.("")])
|
||||
defp intersperse_fun(subject, replacement) do
|
||||
case next_grapheme(subject) do
|
||||
{current, rest} -> [current, replacement.("") | intersperse_fun(rest, replacement)]
|
||||
nil -> []
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1606,21 +1562,20 @@ defmodule String do
|
||||
one single grapheme.
|
||||
"""
|
||||
@spec reverse(t) :: t
|
||||
def reverse(string) when is_binary(string) do
|
||||
do_reverse(:unicode_util.gc(string), [])
|
||||
def reverse(string) do
|
||||
do_reverse(next_grapheme(string), [])
|
||||
end
|
||||
|
||||
defp do_reverse([grapheme | rest], acc),
|
||||
do: do_reverse(:unicode_util.gc(rest), [grapheme | acc])
|
||||
defp do_reverse({grapheme, rest}, acc) do
|
||||
do_reverse(next_grapheme(rest), [grapheme | acc])
|
||||
end
|
||||
|
||||
defp do_reverse([], acc),
|
||||
do: :unicode.characters_to_binary(acc)
|
||||
defp do_reverse(nil, acc), do: IO.iodata_to_binary(acc)
|
||||
|
||||
defp do_reverse({:error, <<byte, rest::bits>>}, acc),
|
||||
do: :unicode.characters_to_binary(acc) <> <<byte>> <> do_reverse(:unicode_util.gc(rest), [])
|
||||
@compile {:inline, duplicate: 2}
|
||||
|
||||
@doc """
|
||||
Returns a string `subject` repeated `n` times.
|
||||
Returns a string `subject` duplicated `n` times.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -1636,9 +1591,8 @@ defmodule String do
|
||||
"abcabc"
|
||||
|
||||
"""
|
||||
@compile {:inline, duplicate: 2}
|
||||
@spec duplicate(t, non_neg_integer) :: t
|
||||
def duplicate(subject, n) when is_binary(subject) and is_integer(n) and n >= 0 do
|
||||
def duplicate(subject, n) do
|
||||
:binary.copy(subject, n)
|
||||
end
|
||||
|
||||
@@ -1669,19 +1623,7 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec codepoints(t) :: [codepoint]
|
||||
def codepoints(string) when is_binary(string) do
|
||||
do_codepoints(string)
|
||||
end
|
||||
|
||||
defp do_codepoints(<<codepoint::utf8, rest::bits>>) do
|
||||
[<<codepoint::utf8>> | do_codepoints(rest)]
|
||||
end
|
||||
|
||||
defp do_codepoints(<<byte, rest::bits>>) do
|
||||
[<<byte>> | do_codepoints(rest)]
|
||||
end
|
||||
|
||||
defp do_codepoints(<<>>), do: []
|
||||
defdelegate codepoints(string), to: String.Unicode
|
||||
|
||||
@doc ~S"""
|
||||
Returns the next code point in a string.
|
||||
@@ -1724,10 +1666,9 @@ defmodule String do
|
||||
Binary pattern matching, however, is simpler and more efficient,
|
||||
so pick the option that better suits your use case.
|
||||
"""
|
||||
@compile {:inline, next_codepoint: 1}
|
||||
@spec next_codepoint(t) :: {codepoint, t} | nil
|
||||
def next_codepoint(<<cp::utf8, rest::binary>>), do: {<<cp::utf8>>, rest}
|
||||
def next_codepoint(<<byte, rest::binary>>), do: {<<byte>>, rest}
|
||||
def next_codepoint(<<>>), do: nil
|
||||
defdelegate next_codepoint(string), to: String.Unicode
|
||||
|
||||
@doc ~S"""
|
||||
Checks whether `string` contains only valid characters.
|
||||
@@ -1753,13 +1694,10 @@ defmodule String do
|
||||
@spec valid?(t) :: boolean
|
||||
def valid?(string)
|
||||
|
||||
def valid?(<<string::binary>>), do: valid_utf8?(string)
|
||||
def valid?(<<_::utf8, t::binary>>), do: valid?(t)
|
||||
def valid?(<<>>), do: true
|
||||
def valid?(_), do: false
|
||||
|
||||
defp valid_utf8?(<<_::utf8, rest::bits>>), do: valid_utf8?(rest)
|
||||
defp valid_utf8?(<<>>), do: true
|
||||
defp valid_utf8?(_), do: false
|
||||
|
||||
@doc false
|
||||
@deprecated "Use String.valid?/1 instead"
|
||||
def valid_character?(string) do
|
||||
@@ -1803,7 +1741,7 @@ defmodule String do
|
||||
|
||||
def chunk("", _), do: []
|
||||
|
||||
def chunk(string, trait) when is_binary(string) and trait in [:valid, :printable] do
|
||||
def chunk(string, trait) when trait in [:valid, :printable] do
|
||||
{cp, _} = next_codepoint(string)
|
||||
pred_fn = make_chunk_pred(trait)
|
||||
do_chunk(string, pred_fn.(cp), pred_fn)
|
||||
@@ -1849,17 +1787,10 @@ defmodule String do
|
||||
["é"]
|
||||
|
||||
"""
|
||||
@compile {:inline, graphemes: 1}
|
||||
@spec graphemes(t) :: [grapheme]
|
||||
def graphemes(string) when is_binary(string), do: do_graphemes(string)
|
||||
defdelegate graphemes(string), to: String.Unicode
|
||||
|
||||
defp do_graphemes(gcs) do
|
||||
case :unicode_util.gc(gcs) do
|
||||
[gc | rest] -> [grapheme_to_binary(gc) | do_graphemes(rest)]
|
||||
[] -> []
|
||||
{:error, <<byte, rest::bits>>} -> [<<byte>> | do_graphemes(rest)]
|
||||
end
|
||||
end
|
||||
@compile {:inline, next_grapheme: 1, next_grapheme_size: 1}
|
||||
|
||||
@doc """
|
||||
Returns the next grapheme in a string.
|
||||
@@ -1877,28 +1808,32 @@ defmodule String do
|
||||
nil
|
||||
|
||||
"""
|
||||
@compile {:inline, next_grapheme: 1}
|
||||
@spec next_grapheme(t) :: {grapheme, t} | nil
|
||||
def next_grapheme(string) when is_binary(string) do
|
||||
case :unicode_util.gc(string) do
|
||||
[gc] -> {grapheme_to_binary(gc), <<>>}
|
||||
[gc | rest] -> {grapheme_to_binary(gc), rest}
|
||||
[] -> nil
|
||||
{:error, <<byte, rest::bits>>} -> {<<byte>>, rest}
|
||||
def next_grapheme(binary) do
|
||||
case next_grapheme_size(binary) do
|
||||
{size, rest} -> {binary_part(binary, 0, size), rest}
|
||||
nil -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use String.next_grapheme/1 instead"
|
||||
@doc """
|
||||
Returns the size (in bytes) of the next grapheme.
|
||||
|
||||
The result is a tuple with the next grapheme size in bytes and
|
||||
the remainder of the string or `nil` in case the string
|
||||
reached its end.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> String.next_grapheme_size("olá")
|
||||
{1, "lá"}
|
||||
|
||||
iex> String.next_grapheme_size("")
|
||||
nil
|
||||
|
||||
"""
|
||||
@spec next_grapheme_size(t) :: {pos_integer, t} | nil
|
||||
def next_grapheme_size(string) when is_binary(string) do
|
||||
case :unicode_util.gc(string) do
|
||||
[gc] -> {grapheme_byte_size(gc), <<>>}
|
||||
[gc | rest] -> {grapheme_byte_size(gc), rest}
|
||||
[] -> nil
|
||||
{:error, <<_, rest::bits>>} -> {1, rest}
|
||||
end
|
||||
end
|
||||
defdelegate next_grapheme_size(string), to: String.Unicode
|
||||
|
||||
@doc """
|
||||
Returns the first grapheme from a UTF-8 string,
|
||||
@@ -1917,11 +1852,10 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec first(t) :: grapheme | nil
|
||||
def first(string) when is_binary(string) do
|
||||
case :unicode_util.gc(string) do
|
||||
[gc | _] -> grapheme_to_binary(gc)
|
||||
[] -> nil
|
||||
{:error, <<byte, _::bits>>} -> <<byte>>
|
||||
def first(string) do
|
||||
case next_grapheme(string) do
|
||||
{char, _} -> char
|
||||
nil -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1929,13 +1863,8 @@ defmodule String do
|
||||
Returns the last grapheme from a UTF-8 string,
|
||||
`nil` if the string is empty.
|
||||
|
||||
It traverses the whole string to find its last grapheme.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> String.last("")
|
||||
nil
|
||||
|
||||
iex> String.last("elixir")
|
||||
"r"
|
||||
|
||||
@@ -1944,13 +1873,15 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec last(t) :: grapheme | nil
|
||||
def last(""), do: nil
|
||||
def last(string) when is_binary(string), do: do_last(:unicode_util.gc(string), nil)
|
||||
def last(string) do
|
||||
do_last(next_grapheme(string), nil)
|
||||
end
|
||||
|
||||
defp do_last([gc | rest], _), do: do_last(:unicode_util.gc(rest), gc)
|
||||
defp do_last([], acc) when is_binary(acc), do: acc
|
||||
defp do_last([], acc), do: :unicode.characters_to_binary([acc])
|
||||
defp do_last({:error, <<byte, rest::bits>>}, _), do: do_last(:unicode_util.gc(rest), <<byte>>)
|
||||
defp do_last({char, rest}, _) do
|
||||
do_last(next_grapheme(rest), char)
|
||||
end
|
||||
|
||||
defp do_last(nil, last_char), do: last_char
|
||||
|
||||
@doc """
|
||||
Returns the number of Unicode graphemes in a UTF-8 string.
|
||||
@@ -1965,15 +1896,7 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec length(t) :: non_neg_integer
|
||||
def length(string) when is_binary(string), do: length(string, 0)
|
||||
|
||||
defp length(gcs, acc) do
|
||||
case :unicode_util.gc(gcs) do
|
||||
[_ | rest] -> length(rest, acc + 1)
|
||||
[] -> acc
|
||||
{:error, <<_, rest::bits>>} -> length(rest, acc + 1)
|
||||
end
|
||||
end
|
||||
defdelegate length(string), to: String.Unicode
|
||||
|
||||
@doc """
|
||||
Returns the grapheme at the `position` of the given UTF-8 `string`.
|
||||
@@ -1999,11 +1922,11 @@ defmodule String do
|
||||
"""
|
||||
@spec at(t, integer) :: grapheme | nil
|
||||
|
||||
def at(string, position) when is_binary(string) and is_integer(position) and position >= 0 do
|
||||
def at(string, position) when is_integer(position) and position >= 0 do
|
||||
do_at(string, position)
|
||||
end
|
||||
|
||||
def at(string, position) when is_binary(string) and is_integer(position) and position < 0 do
|
||||
def at(string, position) when is_integer(position) and position < 0 do
|
||||
position = length(string) + position
|
||||
|
||||
case position >= 0 do
|
||||
@@ -2013,11 +1936,10 @@ defmodule String do
|
||||
end
|
||||
|
||||
defp do_at(string, position) do
|
||||
left = byte_size_remaining_at(string, position)
|
||||
|
||||
string
|
||||
|> binary_part(byte_size(string) - left, left)
|
||||
|> first()
|
||||
case String.Unicode.split_at(string, position) do
|
||||
{_, nil} -> nil
|
||||
{_, rest} -> first(rest)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -2062,29 +1984,24 @@ defmodule String do
|
||||
""
|
||||
end
|
||||
|
||||
def slice(string, start, length)
|
||||
when is_binary(string) and is_integer(start) and is_integer(length) and start >= 0 and
|
||||
length >= 0 do
|
||||
do_slice(string, start, length)
|
||||
end
|
||||
def slice(string, start, length) when start >= 0 and length >= 0 do
|
||||
case String.Unicode.split_at(string, start) do
|
||||
{_, nil} ->
|
||||
""
|
||||
|
||||
def slice(string, start, length)
|
||||
when is_binary(string) and is_integer(start) and is_integer(length) and start < 0 and
|
||||
length >= 0 do
|
||||
start = length(string) + start
|
||||
|
||||
case start >= 0 do
|
||||
true -> do_slice(string, start, length)
|
||||
false -> ""
|
||||
{start_bytes, rest} ->
|
||||
{len_bytes, _} = String.Unicode.split_at(rest, length)
|
||||
binary_part(string, start_bytes, len_bytes)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_slice(string, start, length) do
|
||||
from_start = byte_size_remaining_at(string, start)
|
||||
rest = binary_part(string, byte_size(string) - from_start, from_start)
|
||||
def slice(string, start, length) when start < 0 and length >= 0 do
|
||||
start = length(string) + start
|
||||
|
||||
from_end = byte_size_remaining_at(rest, length)
|
||||
binary_part(rest, 0, from_start - from_end)
|
||||
case start >= 0 do
|
||||
true -> slice(string, start, length)
|
||||
false -> ""
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -2110,24 +2027,19 @@ defmodule String do
|
||||
iex> String.slice("elixir", 1..10)
|
||||
"lixir"
|
||||
|
||||
iex> String.slice("elixir", 10..3)
|
||||
""
|
||||
|
||||
iex> String.slice("elixir", -4..-1)
|
||||
"ixir"
|
||||
|
||||
iex> String.slice("elixir", 2..-1)
|
||||
"ixir"
|
||||
|
||||
iex> String.slice("elixir", -4..6)
|
||||
"ixir"
|
||||
|
||||
For ranges where `start > stop`, you need to explicitly
|
||||
mark them as increasing:
|
||||
|
||||
iex> String.slice("elixir", 2..-1//1)
|
||||
"ixir"
|
||||
|
||||
iex> String.slice("elixir", 1..-2//1)
|
||||
"lixi"
|
||||
|
||||
If values are out of bounds, it returns an empty string:
|
||||
|
||||
iex> String.slice("elixir", 10..3)
|
||||
iex> String.slice("elixir", -1..-4)
|
||||
""
|
||||
|
||||
iex> String.slice("elixir", -10..-7)
|
||||
@@ -2141,36 +2053,22 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec slice(t, Range.t()) :: t
|
||||
def slice(string, first..last//step = range) when is_binary(string) do
|
||||
# TODO: Deprecate negative steps on Elixir v1.16
|
||||
# TODO: There are two features we can add to slicing ranges:
|
||||
# 1. We can allow the step to be any positive number
|
||||
# 2. We can allow slice and reverse at the same time. However, we can't
|
||||
# implement so right now. First we will have to raise if a decreasing
|
||||
# range is given on Elixir v2.0.
|
||||
if step == 1 or (step == -1 and first > last) do
|
||||
slice_range(string, first, last)
|
||||
else
|
||||
raise ArgumentError,
|
||||
"String.slice/2 does not accept ranges with custom steps, got: #{inspect(range)}"
|
||||
|
||||
def slice(string, range)
|
||||
|
||||
def slice("", _.._), do: ""
|
||||
|
||||
def slice(string, first..-1) when first >= 0 do
|
||||
case String.Unicode.split_at(string, first) do
|
||||
{_, nil} ->
|
||||
""
|
||||
|
||||
{start_bytes, _} ->
|
||||
binary_part(string, start_bytes, byte_size(string) - start_bytes)
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Remove me on v2.0
|
||||
def slice(string, %{__struct__: Range, first: first, last: last} = range)
|
||||
when is_binary(string) do
|
||||
step = if first <= last, do: 1, else: -1
|
||||
slice(string, Map.put(range, :step, step))
|
||||
end
|
||||
|
||||
defp slice_range("", _, _), do: ""
|
||||
|
||||
defp slice_range(string, first, -1) when first >= 0 do
|
||||
left = byte_size_remaining_at(string, first)
|
||||
binary_part(string, byte_size(string) - left, left)
|
||||
end
|
||||
|
||||
defp slice_range(string, first, last) when first >= 0 and last >= 0 do
|
||||
def slice(string, first..last) when first >= 0 and last >= 0 do
|
||||
if last >= first do
|
||||
slice(string, first, last - first + 1)
|
||||
else
|
||||
@@ -2178,8 +2076,9 @@ defmodule String do
|
||||
end
|
||||
end
|
||||
|
||||
defp slice_range(string, first, last) do
|
||||
{bytes, length} = acc_bytes(:unicode_util.gc(string), [], 0)
|
||||
def slice(string, first..last) do
|
||||
{bytes, length} = do_acc_bytes(next_grapheme_size(string), [], 0)
|
||||
|
||||
first = add_if_negative(first, length)
|
||||
last = add_if_negative(last, length)
|
||||
|
||||
@@ -2189,26 +2088,21 @@ defmodule String do
|
||||
last = min(last + 1, length)
|
||||
bytes = Enum.drop(bytes, length - last)
|
||||
first = last - first
|
||||
{length_bytes, start_bytes} = split_bytes(bytes, 0, first)
|
||||
binary_part(string, start_bytes, length_bytes)
|
||||
{length_bytes, start_bytes} = Enum.split(bytes, first)
|
||||
binary_part(string, Enum.sum(start_bytes), Enum.sum(length_bytes))
|
||||
end
|
||||
end
|
||||
|
||||
defp acc_bytes([gc | rest], bytes, length),
|
||||
do: acc_bytes(:unicode_util.gc(rest), [grapheme_byte_size(gc) | bytes], length + 1)
|
||||
|
||||
defp acc_bytes([], bytes, length),
|
||||
do: {bytes, length}
|
||||
|
||||
defp acc_bytes({:error, <<_, rest::bits>>}, bytes, length),
|
||||
do: acc_bytes(:unicode_util.gc(rest), [1 | bytes], length + 1)
|
||||
|
||||
defp add_if_negative(value, to_add) when value < 0, do: value + to_add
|
||||
defp add_if_negative(value, _to_add), do: value
|
||||
|
||||
defp split_bytes(rest, acc, 0), do: {acc, Enum.sum(rest)}
|
||||
defp split_bytes([], acc, _), do: {acc, 0}
|
||||
defp split_bytes([head | tail], acc, count), do: split_bytes(tail, head + acc, count - 1)
|
||||
defp do_acc_bytes({size, rest}, bytes, length) do
|
||||
do_acc_bytes(next_grapheme_size(rest), [size | bytes], length + 1)
|
||||
end
|
||||
|
||||
defp do_acc_bytes(nil, bytes, length) do
|
||||
{bytes, length}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if `string` starts with any of the prefixes given.
|
||||
@@ -2318,11 +2212,9 @@ defmodule String do
|
||||
iex> String.match?("bar", ~r/foo/)
|
||||
false
|
||||
|
||||
Elixir also provides `Kernel.=~/2` and `Regex.match?/2` as
|
||||
alternatives to test strings against regular expressions.
|
||||
"""
|
||||
@spec match?(t, Regex.t()) :: boolean
|
||||
def match?(string, regex) when is_binary(string) do
|
||||
def match?(string, regex) do
|
||||
Regex.match?(regex, string)
|
||||
end
|
||||
|
||||
@@ -2389,7 +2281,7 @@ defmodule String do
|
||||
strings.
|
||||
|
||||
In case you need to work with bytes, take a look at the
|
||||
[`:binary` module](`:binary`).
|
||||
[`:binary` module](http://www.erlang.org/doc/man/binary.html).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -2433,7 +2325,7 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec to_atom(String.t()) :: atom
|
||||
def to_atom(string) when is_binary(string) do
|
||||
def to_atom(string) do
|
||||
:erlang.binary_to_atom(string, :utf8)
|
||||
end
|
||||
|
||||
@@ -2450,9 +2342,12 @@ defmodule String do
|
||||
iex> String.to_existing_atom("my_atom")
|
||||
:my_atom
|
||||
|
||||
iex> String.to_existing_atom("this_atom_will_never_exist")
|
||||
** (ArgumentError) argument error
|
||||
|
||||
"""
|
||||
@spec to_existing_atom(String.t()) :: atom
|
||||
def to_existing_atom(string) when is_binary(string) do
|
||||
def to_existing_atom(string) do
|
||||
:erlang.binary_to_existing_atom(string, :utf8)
|
||||
end
|
||||
|
||||
@@ -2478,7 +2373,7 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec to_integer(String.t()) :: integer
|
||||
def to_integer(string) when is_binary(string) do
|
||||
def to_integer(string) do
|
||||
:erlang.binary_to_integer(string)
|
||||
end
|
||||
|
||||
@@ -2494,7 +2389,7 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec to_integer(String.t(), 2..36) :: integer
|
||||
def to_integer(string, base) when is_binary(string) and is_integer(base) do
|
||||
def to_integer(string, base) do
|
||||
:erlang.binary_to_integer(string, base)
|
||||
end
|
||||
|
||||
@@ -2520,7 +2415,7 @@ defmodule String do
|
||||
|
||||
"""
|
||||
@spec to_float(String.t()) :: float
|
||||
def to_float(string) when is_binary(string) do
|
||||
def to_float(string) do
|
||||
:erlang.binary_to_float(string)
|
||||
end
|
||||
|
||||
@@ -2557,7 +2452,7 @@ defmodule String do
|
||||
def bag_distance(_string, ""), do: 0.0
|
||||
def bag_distance("", _string), do: 0.0
|
||||
|
||||
def bag_distance(string1, string2) when is_binary(string1) and is_binary(string2) do
|
||||
def bag_distance(string1, string2) do
|
||||
{bag1, length1} = string_to_bag(string1, %{}, 0)
|
||||
{bag2, length2} = string_to_bag(string2, %{}, 0)
|
||||
|
||||
@@ -2568,17 +2463,18 @@ defmodule String do
|
||||
end
|
||||
|
||||
defp string_to_bag(string, bag, length) do
|
||||
case :unicode_util.gc(string) do
|
||||
[gc | rest] -> string_to_bag(rest, bag_store(bag, gc), length + 1)
|
||||
[] -> {bag, length}
|
||||
{:error, <<byte, rest::bits>>} -> string_to_bag(rest, bag_store(bag, <<byte>>), length + 1)
|
||||
end
|
||||
end
|
||||
case next_grapheme(string) do
|
||||
{char, rest} ->
|
||||
bag =
|
||||
case bag do
|
||||
%{^char => current} -> %{bag | char => current + 1}
|
||||
%{} -> Map.put(bag, char, 1)
|
||||
end
|
||||
|
||||
defp bag_store(bag, gc) do
|
||||
case bag do
|
||||
%{^gc => current} -> %{bag | gc => current + 1}
|
||||
%{} -> Map.put(bag, gc, 1)
|
||||
string_to_bag(rest, bag, length + 1)
|
||||
|
||||
nil ->
|
||||
{bag, length}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2622,9 +2518,9 @@ defmodule String do
|
||||
def jaro_distance(_string, ""), do: 0.0
|
||||
def jaro_distance("", _string), do: 0.0
|
||||
|
||||
def jaro_distance(string1, string2) when is_binary(string1) and is_binary(string2) do
|
||||
{chars1, len1} = graphemes_and_length(string1)
|
||||
{chars2, len2} = graphemes_and_length(string2)
|
||||
def jaro_distance(string1, string2) do
|
||||
{chars1, len1} = chars_and_length(string1)
|
||||
{chars2, len2} = chars_and_length(string2)
|
||||
|
||||
case match(chars1, len1, chars2, len2) do
|
||||
{0, _trans} ->
|
||||
@@ -2635,6 +2531,12 @@ defmodule String do
|
||||
end
|
||||
end
|
||||
|
||||
@compile {:inline, chars_and_length: 1}
|
||||
defp chars_and_length(string) do
|
||||
chars = graphemes(string)
|
||||
{chars, Kernel.length(chars)}
|
||||
end
|
||||
|
||||
defp match(chars1, len1, chars2, len2) do
|
||||
if len1 < len2 do
|
||||
match(chars1, chars2, div(len2, 2) - 1)
|
||||
@@ -2703,7 +2605,7 @@ defmodule String do
|
||||
"""
|
||||
@doc since: "1.3.0"
|
||||
@spec myers_difference(t, t) :: [{:eq | :ins | :del, t}]
|
||||
def myers_difference(string1, string2) when is_binary(string1) and is_binary(string2) do
|
||||
def myers_difference(string1, string2) do
|
||||
graphemes(string1)
|
||||
|> List.myers_difference(graphemes(string2))
|
||||
|> Enum.map(fn {kind, chars} -> {kind, IO.iodata_to_binary(chars)} end)
|
||||
@@ -2713,62 +2615,4 @@ defmodule String do
|
||||
@deprecated "Use String.to_charlist/1 instead"
|
||||
@spec to_char_list(t) :: charlist
|
||||
def to_char_list(string), do: String.to_charlist(string)
|
||||
|
||||
## Helpers
|
||||
|
||||
@compile {:inline,
|
||||
codepoint_byte_size: 1,
|
||||
grapheme_byte_size: 1,
|
||||
grapheme_to_binary: 1,
|
||||
graphemes_and_length: 1,
|
||||
reverse_characters_to_binary: 1}
|
||||
|
||||
defp byte_size_remaining_at(binary, 0) do
|
||||
byte_size(binary)
|
||||
end
|
||||
|
||||
defp byte_size_remaining_at(binary, n) do
|
||||
case :unicode_util.gc(binary) do
|
||||
[_] -> 0
|
||||
[_ | rest] -> byte_size_remaining_at(rest, n - 1)
|
||||
[] -> 0
|
||||
{:error, <<_, bin::bits>>} -> byte_size_remaining_at(bin, n - 1)
|
||||
end
|
||||
end
|
||||
|
||||
defp codepoint_byte_size(cp) when cp <= 0x007F, do: 1
|
||||
defp codepoint_byte_size(cp) when cp <= 0x07FF, do: 2
|
||||
defp codepoint_byte_size(cp) when cp <= 0xFFFF, do: 3
|
||||
defp codepoint_byte_size(_), do: 4
|
||||
|
||||
defp grapheme_to_binary(cp) when is_integer(cp), do: <<cp::utf8>>
|
||||
defp grapheme_to_binary(gc), do: :unicode.characters_to_binary(gc)
|
||||
|
||||
defp grapheme_byte_size(cp) when is_integer(cp), do: codepoint_byte_size(cp)
|
||||
defp grapheme_byte_size(cps), do: grapheme_byte_size(cps, 0)
|
||||
|
||||
defp grapheme_byte_size([cp | cps], acc),
|
||||
do: grapheme_byte_size(cps, acc + codepoint_byte_size(cp))
|
||||
|
||||
defp grapheme_byte_size([], acc),
|
||||
do: acc
|
||||
|
||||
defp graphemes_and_length(string),
|
||||
do: graphemes_and_length(string, [], 0)
|
||||
|
||||
defp graphemes_and_length(string, acc, length) do
|
||||
case :unicode_util.gc(string) do
|
||||
[gc | rest] ->
|
||||
graphemes_and_length(rest, [gc | acc], length + 1)
|
||||
|
||||
[] ->
|
||||
{:lists.reverse(acc), length}
|
||||
|
||||
{:error, <<byte, rest::bits>>} ->
|
||||
graphemes_and_length(rest, [<<byte>> | acc], length + 1)
|
||||
end
|
||||
end
|
||||
|
||||
defp reverse_characters_to_binary(acc),
|
||||
do: acc |> :lists.reverse() |> :unicode.characters_to_binary()
|
||||
end
|
||||
|
||||
@@ -415,7 +415,7 @@ defmodule StringIO do
|
||||
defp list_to_binary(data, :unicode) when is_list(data), do: List.to_string(data)
|
||||
defp list_to_binary(data, :latin1) when is_list(data), do: :erlang.list_to_binary(data)
|
||||
|
||||
# From https://erlang.org/doc/apps/stdlib/io_protocol.html: result can be any
|
||||
# From http://erlang.org/doc/apps/stdlib/io_protocol.html: result can be any
|
||||
# Erlang term, but if it is a list(), the I/O server can convert it to a binary().
|
||||
defp get_until_result(data, encoding) when is_list(data), do: list_to_binary(data, encoding)
|
||||
defp get_until_result(data, _), do: data
|
||||
|
||||
@@ -284,7 +284,7 @@ defmodule Supervisor do
|
||||
supervisor gives you more direct control over how the supervisor
|
||||
is initialized. Instead of calling `Supervisor.start_link/2` with
|
||||
a list of children that are automatically initialized, we manually
|
||||
initialize the children by calling `Supervisor.init/2` inside its
|
||||
initialized the children by calling `Supervisor.init/2` inside its
|
||||
`c:init/1` callback.
|
||||
|
||||
`use Supervisor` also defines a `child_spec/1` function which allows
|
||||
@@ -930,7 +930,7 @@ defmodule Supervisor do
|
||||
"""
|
||||
@spec which_children(supervisor) :: [
|
||||
# inlining module() | :dynamic here because :supervisor.modules() is not exported
|
||||
{term() | :undefined, child | :restarting, :worker | :supervisor, [module()] | :dynamic}
|
||||
{term() | :undefined, child | :restarting, :worker | :supervisor, module() | :dynamic}
|
||||
]
|
||||
def which_children(supervisor) do
|
||||
call(supervisor, :which_children)
|
||||
|
||||
+37
-265
@@ -43,7 +43,7 @@ defmodule System do
|
||||
system time may not match in case of time warps although the VM works towards
|
||||
aligning them. This time is not monotonic (i.e., it may decrease)
|
||||
as its behaviour is configured [by the VM time warp
|
||||
mode](https://erlang.org/doc/apps/erts/time_correction.html#Time_Warp_Modes);
|
||||
mode](http://www.erlang.org/doc/apps/erts/time_correction.html#Time_Warp_Modes);
|
||||
|
||||
* `monotonic_time/0` - a monotonically increasing time provided
|
||||
by the Erlang VM.
|
||||
@@ -58,7 +58,7 @@ defmodule System do
|
||||
|
||||
For a more complete rundown on the VM support for different
|
||||
times, see the [chapter on time and time
|
||||
correction](https://erlang.org/doc/apps/erts/time_correction.html)
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html)
|
||||
in the Erlang docs.
|
||||
"""
|
||||
|
||||
@@ -81,22 +81,6 @@ defmodule System do
|
||||
| :nanosecond
|
||||
| pos_integer
|
||||
|
||||
@type signal ::
|
||||
:sigabrt
|
||||
| :sigalrm
|
||||
| :sigchld
|
||||
| :sighup
|
||||
| :sigquit
|
||||
| :sigstop
|
||||
| :sigterm
|
||||
| :sigtstp
|
||||
| :sigusr1
|
||||
| :sigusr2
|
||||
|
||||
@vm_signals [:sigquit, :sigterm, :sigusr1]
|
||||
@os_signals [:sighup, :sigabrt, :sigalrm, :sigusr2, :sigchld, :sigstop, :sigtstp]
|
||||
@signals @vm_signals ++ @os_signals
|
||||
|
||||
@base_dir :filename.join(__DIR__, "../../..")
|
||||
@version_file :filename.join(@base_dir, "VERSION")
|
||||
|
||||
@@ -425,8 +409,8 @@ defmodule System do
|
||||
|
||||
The function must receive the exit status code as an argument.
|
||||
|
||||
If the VM terminates programmatically, via `System.stop/1`, `System.halt/1`,
|
||||
or exit signals, the `at_exit/1` callbacks are not executed.
|
||||
If the VM terminates programmatically, via `System.stop/1` or `System.halt/1`,
|
||||
the `at_exit/1` callbacks are not executed.
|
||||
"""
|
||||
@spec at_exit((non_neg_integer -> any)) :: :ok
|
||||
def at_exit(fun) when is_function(fun, 1) do
|
||||
@@ -434,150 +418,6 @@ defmodule System do
|
||||
:ok
|
||||
end
|
||||
|
||||
defmodule SignalHandler do
|
||||
@moduledoc false
|
||||
@behaviour :gen_event
|
||||
|
||||
@impl true
|
||||
def init({event, fun}) do
|
||||
{:ok, {event, fun}}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_call(_message, state) do
|
||||
{:ok, :ok, state}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(signal, {event, fun}) do
|
||||
if signal == event, do: :ok = fun.()
|
||||
{:ok, {event, fun}}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info(_, {event, fun}) do
|
||||
{:ok, {event, fun}}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Traps the given `signal` to execute the `fun`.
|
||||
|
||||
**Important**: Trapping signals may have strong implications
|
||||
on how a system shuts down and behave in production and
|
||||
therefore it is extremely discouraged for libraries to
|
||||
set their own traps. Instead, they should redirect users
|
||||
to configure them themselves. The only cases where it is
|
||||
acceptable for libraries to set their own traps is when
|
||||
using Elixir in script mode, such as in `.exs` files and
|
||||
via Mix tasks.
|
||||
|
||||
An optional `id` that uniquely identifies the function
|
||||
can be given, otherwise a unique one is automatically
|
||||
generated. If a previously registered `id` is given,
|
||||
this function returns an error tuple. The `id` can be
|
||||
used to remove a registered signal by calling
|
||||
`untrap_signal/2`.
|
||||
|
||||
The given `fun` receives no arguments and it must return
|
||||
`:ok`.
|
||||
|
||||
It returns `{:ok, id}` in case of success,
|
||||
`{:error, :already_registered}` in case the id has already
|
||||
been registered for the given signal, or `{:error, :not_sup}`
|
||||
in case trapping exists is not supported by the current OS.
|
||||
|
||||
The first time a signal is trapped, it will override the
|
||||
default behaviour from the operating system. If the same
|
||||
signal is trapped multiple times, subsequent functions
|
||||
given to `trap_signal` will execute *first*. In other
|
||||
words, you can consider each function is prepended to
|
||||
the signal handler.
|
||||
|
||||
By default, the Erlang VM register traps to the three
|
||||
signals:
|
||||
|
||||
* `:sigstop` - gracefully shuts down the VM with `stop/0`
|
||||
* `:sigquit` - halts the VM via `halt/0`
|
||||
* `:sigusr1` - halts the VM via status code of 1
|
||||
|
||||
Therefore, if you add traps to the signals above, the
|
||||
default behaviour above will be executed after all user
|
||||
signals.
|
||||
|
||||
## Implementation notes
|
||||
|
||||
All signals run from a single process. Therefore, blocking the
|
||||
`fun` will block subsequent traps. It is also not possible to add
|
||||
or remove traps from within a trap itself.
|
||||
|
||||
Internally, this functionality is built on top of `:os.set_signal/2`.
|
||||
When you register a trap, Elixir automatically sets it to `:handle`
|
||||
and it reverts it back to `:default` once all traps are removed
|
||||
(except for `:sigquit`, `:sigterm`, and `:sigusr1` which are always
|
||||
handled). If you or a library call `:os.set_signal/2` directly,
|
||||
it may disable Elixir traps (or Elixir may override your configuration).
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec trap_signal(signal, (() -> :ok)) :: {:ok, reference()} | {:error, :not_sup}
|
||||
@spec trap_signal(signal, id, (() -> :ok)) ::
|
||||
{:ok, id} | {:error, :already_registered} | {:error, :not_sup}
|
||||
when id: term()
|
||||
def trap_signal(signal, id \\ make_ref(), fun)
|
||||
when signal in @signals and is_function(fun, 0) do
|
||||
:elixir_config.serial(fn ->
|
||||
gen_id = {signal, id}
|
||||
|
||||
if {SignalHandler, gen_id} in signal_handlers() do
|
||||
{:error, :already_registered}
|
||||
else
|
||||
try do
|
||||
:os.set_signal(signal, :handle)
|
||||
rescue
|
||||
_ -> {:error, :not_sup}
|
||||
else
|
||||
:ok ->
|
||||
:ok =
|
||||
:gen_event.add_handler(:erl_signal_server, {SignalHandler, gen_id}, {signal, fun})
|
||||
|
||||
{:ok, id}
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes a previously registered `signal` with `id`.
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec untrap_signal(signal, id) :: :ok | {:error, :not_found} when id: term
|
||||
def untrap_signal(signal, id) when signal in @signals do
|
||||
:elixir_config.serial(fn ->
|
||||
gen_id = {signal, id}
|
||||
|
||||
case :gen_event.delete_handler(:erl_signal_server, {SignalHandler, gen_id}, :delete) do
|
||||
:ok ->
|
||||
if not trapping?(signal) do
|
||||
:os.set_signal(signal, :default)
|
||||
end
|
||||
|
||||
:ok
|
||||
|
||||
{:error, :module_not_found} ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp trapping?(signal) do
|
||||
signal in @vm_signals or
|
||||
Enum.any?(signal_handlers(), &match?({_, {^signal, _}}, &1))
|
||||
end
|
||||
|
||||
defp signal_handlers do
|
||||
:gen_event.which_handlers(:erl_signal_server)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Locates an executable on the system.
|
||||
|
||||
@@ -597,9 +437,6 @@ defmodule System do
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Remove this once we require Erlang/OTP 24+
|
||||
@compile {:no_warn_undefined, {:os, :env, 0}}
|
||||
|
||||
@doc """
|
||||
Returns all system environment variables.
|
||||
|
||||
@@ -608,17 +445,11 @@ defmodule System do
|
||||
"""
|
||||
@spec get_env() :: %{optional(String.t()) => String.t()}
|
||||
def get_env do
|
||||
if function_exported?(:os, :env, 0) do
|
||||
Map.new(:os.env(), fn {k, v} ->
|
||||
{IO.chardata_to_string(k), IO.chardata_to_string(v)}
|
||||
end)
|
||||
else
|
||||
Enum.into(:os.getenv(), %{}, fn var ->
|
||||
var = IO.chardata_to_string(var)
|
||||
[k, v] = String.split(var, "=", parts: 2)
|
||||
{k, v}
|
||||
end)
|
||||
end
|
||||
Enum.into(:os.getenv(), %{}, fn var ->
|
||||
var = IO.chardata_to_string(var)
|
||||
[k, v] = String.split(var, "=", parts: 2)
|
||||
{k, v}
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -707,7 +538,8 @@ defmodule System do
|
||||
|
||||
For more information, see `:os.getpid/0`.
|
||||
"""
|
||||
@deprecated "Use System.pid/0 instead"
|
||||
# TODO: deprecate permanently on v1.13
|
||||
@doc deprecated: "Use System.pid/0 instead"
|
||||
@spec get_pid() :: binary
|
||||
def get_pid, do: IO.iodata_to_binary(:os.getpid())
|
||||
|
||||
@@ -754,13 +586,23 @@ defmodule System do
|
||||
@doc """
|
||||
Deprecated mechanism to retrieve the last exception stacktrace.
|
||||
|
||||
Starting from Erlang/OTP 23, this function will always return an
|
||||
empty list.
|
||||
Accessing the stacktrace outside of a rescue/catch is deprecated.
|
||||
If you want to support only Elixir v1.7+, you must access
|
||||
`__STACKTRACE__/0` inside a rescue/catch. If you want to support
|
||||
earlier Elixir versions, move `System.stacktrace/0` inside a rescue/catch.
|
||||
|
||||
Starting from Erlang/OTP 23, this function will always return an empty list.
|
||||
|
||||
Note that the Erlang VM (and therefore this function) does not
|
||||
return the current stacktrace but rather the stacktrace of the
|
||||
latest exception. To retrieve the stacktrace of the current process,
|
||||
use `Process.info(self(), :current_stacktrace)` instead.
|
||||
"""
|
||||
# TODO: Remove conditional on Erlang/OTP 23+.
|
||||
# TODO: Once Erlang/OTP 23 is required, remove conditional, and update @doc accordingly.
|
||||
# The warning is emitted by the compiler - so a @doc annotation is enough
|
||||
# Note Elixir may be compiled in an earlier Erlang version but runs on a
|
||||
# newer one, so we need the check at compilation time and runtime.
|
||||
@deprecated "Use __STACKTRACE__ instead"
|
||||
@doc deprecated: "Use __STACKTRACE__ instead"
|
||||
if function_exported?(:erlang, :get_stacktrace, 0) do
|
||||
def stacktrace do
|
||||
if function_exported?(:erlang, :get_stacktrace, 0) do
|
||||
@@ -855,13 +697,8 @@ defmodule System do
|
||||
All applications are taken down smoothly, all code is unloaded, and all ports
|
||||
are closed before the system terminates by calling `halt/1`.
|
||||
|
||||
`status` must be a non-negative integer or a binary.
|
||||
|
||||
* If an integer, the runtime system exits with the integer value which is
|
||||
returned to the operating system.
|
||||
|
||||
* If a binary, an Erlang crash dump is produced with status as slogan, and
|
||||
then the runtime system exits with status code 1.
|
||||
`status` must be a non-negative integer value which is returned by the
|
||||
runtime system to the operating system.
|
||||
|
||||
Note that on many platforms, only the status codes 0-255 are supported
|
||||
by the operating system.
|
||||
@@ -884,62 +721,6 @@ defmodule System do
|
||||
:init.stop(String.to_charlist(status))
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Executes the given `command` in the OS shell.
|
||||
|
||||
It uses `sh` for Unix-like systems and `cmd` for Windows.
|
||||
|
||||
**Important**: Use this function with care. In particular, **never
|
||||
pass untrusted user input to this function**, as the user would be
|
||||
able to perform "command injection attacks" by executing any code
|
||||
directly on the machine. Generally speaking, prefer to use `cmd/3`
|
||||
over this function.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> System.shell("echo hello")
|
||||
{"hello\n", 0}
|
||||
|
||||
If you want to stream the output to Standard IO as it arrives:
|
||||
|
||||
iex> System.shell("echo hello", into: IO.stream())
|
||||
hello
|
||||
{%IO.Stream{}, 0}
|
||||
|
||||
## Options
|
||||
|
||||
It accepts the same options as `cmd/3`, except for `arg0`.
|
||||
"""
|
||||
@doc since: "1.12.0"
|
||||
@spec shell(binary, keyword) :: {Collectable.t(), exit_status :: non_neg_integer}
|
||||
def shell(command, opts \\ []) when is_binary(command) do
|
||||
assert_no_null_byte!(command, "System.shell/2")
|
||||
|
||||
# Finding shell command logic from :os.cmd in OTP
|
||||
# https://github.com/erlang/otp/blob/8deb96fb1d017307e22d2ab88968b9ef9f1b71d0/lib/kernel/src/os.erl#L184
|
||||
command =
|
||||
case :os.type() do
|
||||
{:unix, _} ->
|
||||
command =
|
||||
command
|
||||
|> String.replace("\"", "\\\"")
|
||||
|> String.to_charlist()
|
||||
|
||||
'sh -c "' ++ command ++ '"'
|
||||
|
||||
{:win32, osname} ->
|
||||
command = String.to_charlist(command)
|
||||
|
||||
case {System.get_env("COMSPEC"), osname} do
|
||||
{nil, :windows} -> 'command.com /s /c ' ++ command
|
||||
{nil, _} -> 'cmd /s /c ' ++ command
|
||||
{cmd, _} -> '#{cmd} /s /c ' ++ command
|
||||
end
|
||||
end
|
||||
|
||||
do_cmd({:spawn, command}, [], opts)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Executes the given `command` with `args`.
|
||||
|
||||
@@ -972,9 +753,7 @@ defmodule System do
|
||||
iex> System.cmd("echo", ["hello"], env: [{"MIX_ENV", "test"}])
|
||||
{"hello\n", 0}
|
||||
|
||||
If you want to stream the output to Standard IO as it arrives:
|
||||
|
||||
iex> System.cmd("echo", ["hello"], into: IO.stream())
|
||||
iex> System.cmd("echo", ["hello"], into: IO.stream(:stdio, :line))
|
||||
hello
|
||||
{%IO.Stream{}, 0}
|
||||
|
||||
@@ -1025,7 +804,7 @@ defmodule System do
|
||||
## Shell commands
|
||||
|
||||
If you desire to execute a trusted command inside a shell, with pipes,
|
||||
redirecting and so on, please check `shell/2`.
|
||||
redirecting and so on, please check `:os.cmd/1`.
|
||||
"""
|
||||
@spec cmd(binary, [binary], keyword) :: {Collectable.t(), exit_status :: non_neg_integer}
|
||||
def cmd(command, args, opts \\ []) when is_binary(command) and is_list(args) do
|
||||
@@ -1044,15 +823,11 @@ defmodule System do
|
||||
:os.find_executable(cmd) || :erlang.error(:enoent, [command, args, opts])
|
||||
end
|
||||
|
||||
do_cmd({:spawn_executable, cmd}, [args: args], opts)
|
||||
end
|
||||
|
||||
defp do_cmd(port_init, base_opts, opts) do
|
||||
{into, opts} = cmd_opts(opts, [:use_stdio, :exit_status, :binary, :hide] ++ base_opts, "")
|
||||
{into, opts} = cmd_opts(opts, [:use_stdio, :exit_status, :binary, :hide, args: args], "")
|
||||
{initial, fun} = Collectable.into(into)
|
||||
|
||||
try do
|
||||
do_port(Port.open(port_init, opts), initial, fun)
|
||||
do_cmd(Port.open({:spawn_executable, cmd}, opts), initial, fun)
|
||||
catch
|
||||
kind, reason ->
|
||||
fun.(initial, :halt)
|
||||
@@ -1062,18 +837,17 @@ defmodule System do
|
||||
end
|
||||
end
|
||||
|
||||
defp do_port(port, acc, fun) do
|
||||
defp do_cmd(port, acc, fun) do
|
||||
receive do
|
||||
{^port, {:data, data}} ->
|
||||
do_port(port, fun.(acc, {:cont, data}), fun)
|
||||
do_cmd(port, fun.(acc, {:cont, data}), fun)
|
||||
|
||||
{^port, {:exit_status, status}} ->
|
||||
{acc, status}
|
||||
end
|
||||
end
|
||||
|
||||
defp cmd_opts([{:into, any} | t], opts, _into),
|
||||
do: cmd_opts(t, opts, any)
|
||||
defp cmd_opts([{:into, any} | t], opts, _into), do: cmd_opts(t, opts, any)
|
||||
|
||||
defp cmd_opts([{:cd, bin} | t], opts, into) when is_binary(bin),
|
||||
do: cmd_opts(t, [{:cd, bin} | opts], into)
|
||||
@@ -1084,8 +858,7 @@ defmodule System do
|
||||
defp cmd_opts([{:stderr_to_stdout, true} | t], opts, into),
|
||||
do: cmd_opts(t, [:stderr_to_stdout | opts], into)
|
||||
|
||||
defp cmd_opts([{:stderr_to_stdout, false} | t], opts, into),
|
||||
do: cmd_opts(t, opts, into)
|
||||
defp cmd_opts([{:stderr_to_stdout, false} | t], opts, into), do: cmd_opts(t, opts, into)
|
||||
|
||||
defp cmd_opts([{:parallelism, bool} | t], opts, into) when is_boolean(bool),
|
||||
do: cmd_opts(t, [{:parallelism, bool} | opts], into)
|
||||
@@ -1096,8 +869,7 @@ defmodule System do
|
||||
defp cmd_opts([{key, val} | _], _opts, _into),
|
||||
do: raise(ArgumentError, "invalid option #{inspect(key)} with value #{inspect(val)}")
|
||||
|
||||
defp cmd_opts([], opts, into),
|
||||
do: {into, opts}
|
||||
defp cmd_opts([], opts, into), do: {into, opts}
|
||||
|
||||
defp validate_env(enum) do
|
||||
Enum.map(enum, fn
|
||||
@@ -1176,7 +948,7 @@ defmodule System do
|
||||
unit before you display them to humans.
|
||||
|
||||
To determine how many seconds the `:native` unit represents in your current
|
||||
runtime, you can call this function to convert 1 second to the `:native`
|
||||
runtime, you can can call this function to convert 1 second to the `:native`
|
||||
time unit: `System.convert_time_unit(1, :second, :native)`.
|
||||
"""
|
||||
@spec convert_time_unit(integer, time_unit | :native, time_unit | :native) :: integer
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user