Rename Binary.Inspect -> Inspect and Wadler -> Inspect.Algebra

This commit is contained in:
José Valim
2013-07-05 22:15:16 +02:00
parent f69ecfe8aa
commit b335b1663d
13 changed files with 138 additions and 137 deletions
+1 -1
View File
@@ -343,7 +343,7 @@
* enhancements
* [Behaviour] Add Behaviour with a simple callback DSL to define callbacks
* [Binary] Add a Dict binary that converts its keys to binaries on insertion
* [Binary] Optimize `Binary.Inspect` and improve inspect for floats
* [Binary] Optimize `Inspect` and improve inspect for floats
* [CLI] Support `--detached` option
* [Code] `Code.string_to_ast` supports `:existing_atoms_only` as an option in order to guarantee no new atoms is generated when parsing the code
* [EEx] Support `<%%` and `<%#` tags
-8
View File
@@ -559,11 +559,3 @@ end
defimpl Access, for: HashDict do
def access(dict, key), do: HashDict.get(dict, key, nil)
end
defimpl Binary.Inspect, for: HashDict do
import Kernel, except: [inspect: 2]
def inspect(dict, opts) do
"#HashDict<" <> Kernel.inspect(HashDict.to_list(dict), opts) <> ">"
end
end
-8
View File
@@ -490,11 +490,3 @@ defimpl Enumerable, for: HashSet do
def member?(set, v), do: HashSet.member?(set, v)
def count(set), do: HashSet.size(set)
end
defimpl Binary.Inspect, for: HashSet do
import Kernel, except: [inspect: 2]
def inspect(set, opts) do
"#HashSet<" <> Kernel.inspect(HashSet.to_list(set), opts) <> ">"
end
end
@@ -1,9 +1,12 @@
import Kernel, except: [inspect: 1]
import Wadler
import Inspect.Algebra
defprotocol Binary.Inspect do
defrecord Inspect.Opts, raw: false, limit: :infinity, depth: 0,
pretty: false, width: 80, as_doc: false
defprotocol Inspect do
@moduledoc """
The `Binary.Inspect` protocol is responsible for
The `Inspect` protocol is responsible for
converting any structure to a binary for textual
representation. All basic data structures
(tuple, list, function, pid, etc) implement the
@@ -15,12 +18,12 @@ defprotocol Binary.Inspect do
def inspect(thing, opts)
end
defmodule Binary.Inspect.Utils do
defmodule Inspect.Utils do
@moduledoc """
This module defines useful functions to be used on the
implementation of custom pretty-printers. The provided
functions use the document algebra implemented on the
`Wadler` module.
`Inspect.Algebra` module.
"""
## groups aware of depth
@@ -187,9 +190,9 @@ defmodule Binary.Inspect.Utils do
end
end
defimpl Binary.Inspect, for: Atom do
defimpl Inspect, for: Atom do
require Macro
import Binary.Inspect.Utils
import Inspect.Utils
@doc """
Represents the atom as an Elixir term. The atoms false, true
@@ -209,25 +212,30 @@ defimpl Binary.Inspect, for: Atom do
"Foo.Bar"
"""
def inspect(false, opts), do: return(text("false"), opts)
def inspect(true, opts), do: return(text("true"), opts)
def inspect(nil, opts), do: return(text("nil"), opts)
def inspect(:"", opts), do: return(text(":\"\""), opts)
def inspect(Elixir, opts), do: return(text("Elixir"), opts)
def inspect(atom, opts) do
return text(inspect_text(atom)), opts
end
# Used internally by Elixir
@doc false
def inspect_text(false), do: "false"
def inspect_text(true), do: "true"
def inspect_text(nil), do: "nil"
def inspect_text(:""), do: ":\"\""
def inspect_text(Elixir), do: "Elixir"
def inspect_text(atom) do
binary = atom_to_binary(atom)
cond do
valid_atom_identifier?(binary) ->
return text(":" <> binary), opts
":" <> binary
valid_ref_identifier?(binary) ->
return text(Module.to_binary(atom)), opts
Module.to_binary(atom)
atom in Macro.binary_ops or atom in Macro.unary_ops ->
return text(":" <> binary), opts
":" <> binary
true ->
return text(":" <> escape(binary, ?")), opts
":" <> escape(binary, ?")
end
end
@@ -270,8 +278,8 @@ defimpl Binary.Inspect, for: Atom do
defp valid_identifier?(other), do: other
end
defimpl Binary.Inspect, for: BitString do
import Binary.Inspect.Utils
defimpl Inspect, for: BitString do
import Inspect.Utils
@doc %B"""
Represents the string as itself escaping
@@ -330,8 +338,8 @@ defimpl Binary.Inspect, for: BitString do
defp decrement(counter), do: counter - 1
end
defimpl Binary.Inspect, for: List do
import Binary.Inspect.Utils
defimpl Inspect, for: List do
import Inspect.Utils
@doc %B"""
Represents a list checking if it can be printed or not.
@@ -396,7 +404,7 @@ defimpl Binary.Inspect, for: List do
end
defp key_to_binary(key, opts) do
case Binary.Inspect.Atom.inspect(key, Keyword.put(opts, :as_doc, false)) do
case Inspect.Atom.inspect(key, Keyword.put(opts, :as_doc, false)) do
":" <> right -> right
other -> other
end
@@ -413,8 +421,8 @@ defimpl Binary.Inspect, for: List do
defp keyword?(_other), do: false
end
defimpl Binary.Inspect, for: Tuple do
import Binary.Inspect.Utils
defimpl Inspect, for: Tuple do
import Inspect.Utils
@doc """
Inspect tuples. If the tuple represents a record,
@@ -462,7 +470,7 @@ defimpl Binary.Inspect, for: Tuple do
defp record_join(name, fields, tail, opts) do
opts = inc_depth(opts)
fields = lc { field, _ } inlist fields, do: field
namedoc = Binary.Inspect.Atom.inspect(name, opts)
namedoc = Inspect.Atom.inspect(name, opts)
group_maybe(
concat(
namedoc,
@@ -499,8 +507,8 @@ defimpl Binary.Inspect, for: Tuple do
end
end
defimpl Binary.Inspect, for: Number do
import Binary.Inspect.Utils
defimpl Inspect, for: Number do
import Inspect.Utils
@doc """
Represents the number as a binary.
@@ -520,8 +528,8 @@ defimpl Binary.Inspect, for: Number do
end
end
defimpl Binary.Inspect, for: Regex do
import Binary.Inspect.Utils
defimpl Inspect, for: Regex do
import Inspect.Utils
@moduledoc %B"""
Represents the Regex using the `%r""` syntax.
@@ -541,8 +549,8 @@ defimpl Binary.Inspect, for: Regex do
end
end
defimpl Binary.Inspect, for: Function do
import Binary.Inspect.Utils
defimpl Inspect, for: Function do
import Inspect.Utils
@moduledoc """
Inspect functions, when possible, in a literal form.
@@ -562,27 +570,45 @@ defimpl Binary.Inspect, for: Function do
end
end
defimpl Binary.Inspect, for: PID do
import Binary.Inspect.Utils
defimpl Inspect, for: PID do
import Inspect.Utils
def inspect(pid, opts) do
return(text("#PID" <> list_to_binary pid_to_list(pid)), opts)
end
end
defimpl Binary.Inspect, for: Port do
import Binary.Inspect.Utils
defimpl Inspect, for: Port do
import Inspect.Utils
def inspect(port, opts) do
return(text(list_to_binary :erlang.port_to_list(port)), opts)
end
end
defimpl Binary.Inspect, for: Reference do
import Binary.Inspect.Utils
defimpl Inspect, for: Reference do
import Inspect.Utils
def inspect(ref, opts) do
'#Ref' ++ rest = :erlang.ref_to_list(ref)
return text("#Reference" <> list_to_binary(rest)), opts
end
end
defimpl Inspect, for: HashDict do
def inspect(dict, opts) do
"#HashDict<" <> Kernel.inspect(HashDict.to_list(dict), opts) <> ">"
end
end
defimpl Inspect, for: HashSet do
def inspect(set, opts) do
"#HashSet<" <> Kernel.inspect(HashSet.to_list(set), opts) <> ">"
end
end
defimpl Inspect, for: Range do
def inspect(Range[first: first, last: last], opts) do
Kernel.inspect(first, opts) <> ".." <> Kernel.inspect(last, opts)
end
end
@@ -1,4 +1,4 @@
defmodule Wadler do
defmodule Inspect.Algebra do
@moduledoc """
Elixir implementation of the Wadler document algebra as described in
["Strictly Pretty" (2000) by Christian Lindig][0].
@@ -18,11 +18,10 @@ defmodule Wadler do
"""
# some shortcut functions
defp newline, do: "\n"
defp strlen(s), do: String.length(s)
defp default_nesting, do: 2
@default_nesting 2
@newline "\n"
defp strlen(s), do: String.length(s)
defp repeat(_, 0), do: ""
defp repeat(s, i), do: :lists.duplicate(i, s)
@@ -41,7 +40,7 @@ defmodule Wadler do
## Examples
iex> Wadler.empty
iex> Inspect.Algebra.empty
:doc_nil
"""
@@ -54,8 +53,8 @@ defmodule Wadler do
## Examples
iex> doc = Wadler.concat Wadler.text("Tasteless"), Wadler.text("Artosis")
iex> Wadler.pretty(doc, 80)
iex> doc = Inspect.Algebra.concat Inspect.Algebra.text("Tasteless"), Inspect.Algebra.text("Artosis")
iex> Inspect.Algebra.pretty(doc, 80)
"TastelessArtosis"
"""
@@ -69,8 +68,8 @@ defmodule Wadler do
## Examples
iex> doc = Wadler.nest(Wadler.concat(Wadler.break, Wadler.text("6")), 5)
iex> Wadler.pretty(doc, 80)
iex> doc = Inspect.Algebra.nest(Inspect.Algebra.concat(Inspect.Algebra.break, Inspect.Algebra.text("6")), 5)
iex> Inspect.Algebra.pretty(doc, 80)
" 6"
"""
@@ -83,8 +82,8 @@ defmodule Wadler do
## Examples
iex> doc = Wadler.text "Hello, World!"
iex> Wadler.pretty(doc, 80)
iex> doc = Inspect.Algebra.text "Hello, World!"
iex> Inspect.Algebra.pretty(doc, 80)
"Hello, World!"
"""
@@ -100,15 +99,15 @@ defmodule Wadler do
Let's glue two docs together with a break and then render it:
iex> doc = Wadler.glue(Wadler.text("a"), " ", Wadler.text("b"))
iex> Wadler.pretty(doc, 80)
iex> doc = Inspect.Algebra.glue(Inspect.Algebra.text("a"), " ", Inspect.Algebra.text("b"))
iex> Inspect.Algebra.pretty(doc, 80)
"a b"
Notice the break was represented as is, because we haven't reached
a line limit. Once we do, it is replaced by a new line:
iex> doc = Wadler.glue(Wadler.text(String.duplicate "a", 20), " ", Wadler.text("b"))
iex> Wadler.pretty(doc, 10)
iex> doc = Inspect.Algebra.glue(Inspect.Algebra.text(String.duplicate "a", 20), " ", Inspect.Algebra.text("b"))
iex> Inspect.Algebra.pretty(doc, 10)
"aaaaaaaaaaaaaaaaaaaa\nb"
"""
@@ -136,25 +135,25 @@ defmodule Wadler do
## Examples
iex> doc = Wadler.group(
...> Wadler.concat(
...> Wadler.group(
...> Wadler.concat(
...> Wadler.text("Hello,"),
...> Wadler.concat(
...> Wadler.break,
...> Wadler.text("A")
iex> doc = Inspect.Algebra.group(
...> Inspect.Algebra.concat(
...> Inspect.Algebra.group(
...> Inspect.Algebra.concat(
...> Inspect.Algebra.text("Hello,"),
...> Inspect.Algebra.concat(
...> Inspect.Algebra.break,
...> Inspect.Algebra.text("A")
...> )
...> )
...> ),
...> Wadler.concat(
...> Wadler.break,
...> Wadler.text("B")
...> Inspect.Algebra.concat(
...> Inspect.Algebra.break,
...> Inspect.Algebra.text("B")
...> )
...> ))
iex> Wadler.pretty(doc, 80)
iex> Inspect.Algebra.pretty(doc, 80)
"Hello, A B"
iex> Wadler.pretty(doc, 6)
iex> Inspect.Algebra.pretty(doc, 6)
"Hello,\nA\nB"
"""
@@ -167,9 +166,9 @@ defmodule Wadler do
## Examples
iex> doc = Wadler.space Wadler.text("Hughes"), Wadler.text("Wadler")
iex> Wadler.pretty(doc, 80)
"Hughes Wadler"
iex> doc = Inspect.Algebra.space Inspect.Algebra.text("Hughes"), Inspect.Algebra.text("Inspect.Algebra")
iex> Inspect.Algebra.pretty(doc, 80)
"Hughes Inspect.Algebra"
"""
@spec space(doc, doc) :: :doc_cons_t
@@ -180,13 +179,13 @@ defmodule Wadler do
## Examples
iex> doc = Wadler.line Wadler.text("Hughes"), Wadler.text("Wadler")
iex> Wadler.pretty(doc, 80)
"Hughes\nWadler"
iex> doc = Inspect.Algebra.line Inspect.Algebra.text("Hughes"), Inspect.Algebra.text("Inspect.Algebra")
iex> Inspect.Algebra.pretty(doc, 80)
"Hughes\nInspect.Algebra"
"""
@spec line(doc, doc) :: :doc_cons_t
def line(x, y), do: glue(x, newline, y)
def line(x, y), do: glue(x, @newline, y)
@doc """
Folds a list of document entities into a document entity
@@ -194,9 +193,9 @@ defmodule Wadler do
## Examples
iex> doc = [Wadler.text("A"), Wadler.text("B")]
iex> doc = Wadler.folddoc(doc, fn(x,y) -> Wadler.concat(x, Wadler.concat(Wadler.text("!"), y)) end)
iex> Wadler.pretty(doc, 80)
iex> doc = [Inspect.Algebra.text("A"), Inspect.Algebra.text("B")]
iex> doc = Inspect.Algebra.folddoc(doc, fn(x,y) -> Inspect.Algebra.concat(x, Inspect.Algebra.concat(Inspect.Algebra.text("!"), y)) end)
iex> Inspect.Algebra.pretty(doc, 80)
"A!B"
"""
@@ -210,8 +209,8 @@ defmodule Wadler do
## Examples
iex> doc = [Wadler.text("A"), Wadler.text("B")]
iex> doc |> Wadler.spread |> Wadler.pretty(80)
iex> doc = [Inspect.Algebra.text("A"), Inspect.Algebra.text("B")]
iex> doc |> Inspect.Algebra.spread |> Inspect.Algebra.pretty(80)
"A B"
"""
@@ -223,8 +222,8 @@ defmodule Wadler do
## Examples
iex> doc = [Wadler.text("A"), Wadler.text("B")]
iex> doc |> Wadler.stack |> Wadler.pretty(80)
iex> doc = [Inspect.Algebra.text("A"), Inspect.Algebra.text("B")]
iex> doc |> Inspect.Algebra.stack |> Inspect.Algebra.pretty(80)
"A\nB"
"""
@spec stack([doc]) :: doc
@@ -238,7 +237,7 @@ defmodule Wadler do
@spec surround(binary, doc, binary) :: doc
def surround(left, doc, right, sep // "") do
glue(
nest(glue(text(left), sep, doc), default_nesting), # remember that first line is not nested
nest(glue(text(left), sep, doc), @default_nesting), # remember that first line is not nested
sep,
text(right)
)
@@ -263,7 +262,7 @@ defmodule Wadler do
defp do_render(s_text(str: s, sdoc: d)), do: [s | do_render(d)]
defp do_render(s_line(indent: i, sdoc: d)) do
prefix = repeat " ", i
[newline | [prefix | do_render d]]
[@newline | [prefix | do_render d]]
end
@doc """
+3 -3
View File
@@ -2013,7 +2013,7 @@ defmodule Kernel do
end
@doc %B"""
Inspect the given arguments according to the `Binary.Inspect` protocol.
Inspect the given arguments according to the `Inspect` protocol.
## Options
@@ -2054,8 +2054,8 @@ defmodule Kernel do
"""
def inspect(arg, opts // []) do
case is_tuple(arg) and Keyword.get(opts, :raw, false) do
true -> Binary.Inspect.Tuple.inspect(arg, opts)
false -> Binary.Inspect.inspect(arg, opts)
true -> Inspect.Tuple.inspect(arg, opts)
false -> Inspect.inspect(arg, opts)
end
end
-8
View File
@@ -60,11 +60,3 @@ defimpl Range.Iterator, for: Number do
first - last + 1
end
end
defimpl Binary.Inspect, for: Range do
import Kernel, except: [inspect: 2]
def inspect(Range[first: first, last: last], opts) do
Kernel.inspect(first, opts) <> ".." <> Kernel.inspect(last, opts)
end
end
+2 -2
View File
@@ -218,8 +218,8 @@ core_main() ->
"lib/elixir/lib/code.ex",
"lib/elixir/lib/protocol.ex",
"lib/elixir/lib/enum.ex",
"lib/elixir/lib/wadler.ex",
"lib/elixir/lib/binary/inspect.ex",
"lib/elixir/lib/inspect/algebra.ex",
"lib/elixir/lib/inspect.ex",
"lib/elixir/lib/binary/chars.ex",
"lib/elixir/lib/io.ex",
"lib/elixir/lib/path.ex",
+1 -1
View File
@@ -18,7 +18,7 @@ warn(Warning) ->
%% Handle inspecting for exceptions
inspect(Atom) when is_atom(Atom) ->
'Elixir.Binary.Inspect.Atom':inspect(Atom, []).
'Elixir.Inspect.Atom':inspect_text(Atom).
%% Raised during macros translation.
+7 -7
View File
@@ -5,12 +5,12 @@ defmodule KernelTest do
doctest Access.List
doctest Binary.Chars.List
doctest Binary.Inspect.Atom
doctest Binary.Inspect.BitString
doctest Binary.Inspect.List
doctest Binary.Inspect.Number
doctest Binary.Inspect.Regex
doctest Binary.Inspect.Tuple
doctest Inspect.Atom
doctest Inspect.BitString
doctest Inspect.List
doctest Inspect.Number
doctest Inspect.Regex
doctest Inspect.Tuple
doctest Bitwise
doctest Code
doctest Dict
@@ -28,5 +28,5 @@ defmodule KernelTest do
doctest Set
doctest Stream
doctest String
doctest Wadler
doctest Inspect.Algebra
end
@@ -1,9 +1,9 @@
Code.require_file "test_helper.exs", __DIR__
Code.require_file "../test_helper.exs", __DIR__
defmodule WadlerTest do
defmodule Inspect.AlgebraTest do
use ExUnit.Case, async: true
import Wadler
import Inspect.Algebra
def helloabcd do
concat(
@@ -1,6 +1,6 @@
Code.require_file "../test_helper.exs", __DIR__
Code.require_file "test_helper.exs", __DIR__
defmodule Binary.Inspect.AtomTest do
defmodule Inspect.AtomTest do
use ExUnit.Case, async: true
test :basic do
@@ -42,11 +42,11 @@ defmodule Binary.Inspect.AtomTest do
end
test :impl do
assert Binary.Inspect.Atom.__impl__ == Binary.Inspect
assert Inspect.Atom.__impl__ == Inspect
end
end
defmodule Binary.Inspect.BitStringTest do
defmodule Inspect.BitStringTest do
use ExUnit.Case, async: true
test :bitstring do
@@ -79,7 +79,7 @@ defmodule Binary.Inspect.BitStringTest do
end
end
defmodule Binary.Inspect.NumberTest do
defmodule Inspect.NumberTest do
use ExUnit.Case, async: true
test :integer do
@@ -93,7 +93,7 @@ defmodule Binary.Inspect.NumberTest do
end
end
defmodule Binary.Inspect.TupleTest do
defmodule Inspect.TupleTest do
use ExUnit.Case, async: true
test :basic do
@@ -120,7 +120,7 @@ defmodule Binary.Inspect.TupleTest do
defrecord Config, a: 1, b: []
test :with_record do
assert inspect(Config.new) == "Binary.Inspect.TupleTest.Config[a: 1, b: []]"
assert inspect(Config.new) == "Inspect.TupleTest.Config[a: 1, b: []]"
end
test :with_tuple_matching_record_name_but_not_length do
@@ -140,7 +140,7 @@ defmodule Binary.Inspect.TupleTest do
defrecord Rec, value: 1
test :two_items_record do
assert inspect({ Rec[value: 1], 1 }) == "{Binary.Inspect.TupleTest.Rec[value: 1],1}"
assert inspect({ Rec[value: 1], 1 }) == "{Inspect.TupleTest.Rec[value: 1],1}"
end
test :empty do
@@ -152,11 +152,11 @@ defmodule Binary.Inspect.TupleTest do
end
test :with_raw do
assert inspect(Config.new, raw: true) == "{Binary.Inspect.TupleTest.Config,1,[]}"
assert inspect(Config.new, raw: true) == "{Inspect.TupleTest.Config,1,[]}"
end
end
defmodule Binary.Inspect.ListTest do
defmodule Inspect.ListTest do
use ExUnit.Case, async: true
test :basic do
@@ -204,7 +204,7 @@ defmodule Binary.Inspect.ListTest do
end
end
defmodule Binary.Inspect.OthersTest do
defmodule Inspect.OthersTest do
use ExUnit.Case, async: true
def f do
+2 -2
View File
@@ -451,7 +451,7 @@ defimpl Binary.Chars, for: Mix.Version.Schema do
end
end
defimpl Binary.Inspect, for: Mix.Version.Schema do
defimpl Inspect, for: Mix.Version.Schema do
def inspect(self, _opts) do
"#Mix.Version.Schema<" <> to_binary(self) <> ">"
end
@@ -463,7 +463,7 @@ defimpl Binary.Chars, for: Mix.Version.Requirement do
end
end
defimpl Binary.Inspect, for: Mix.Version.Requirement do
defimpl Inspect, for: Mix.Version.Requirement do
def inspect({ _, source, _ }, _opts) do
"#Mix.Version.Requirement<" <> source <> ">"
end