Skip to content

Commit 5866352

Browse files
committed
chore: move existing decode files to combine backend folder
1 parent 9ffdba6 commit 5866352

File tree

11 files changed

+91
-69
lines changed

11 files changed

+91
-69
lines changed

lib/json5/decode.ex

+3-31
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,12 @@ defmodule Json5.Decode do
22
@moduledoc """
33
Decode Json5 string to Elixir term
44
"""
5-
import Combine.Parsers.Base
6-
import Json5.Decode.Helper
7-
8-
alias Json5.Decode
95

106
def parse(input, config \\ %{}) do
11-
case Combine.parse(input, parser(config)) do
12-
{:error, error} -> {:error, error}
13-
[val] -> {:ok, val}
14-
end
15-
end
16-
17-
def parser(config) do
18-
ignore_whitespace()
19-
|> json5_value(config)
20-
|> ignore_whitespace()
21-
|> eof()
7+
backend().parse(input, config)
228
end
239

24-
def json5_value(prev \\ nil, config \\ %{}) do
25-
choice(prev, [
26-
json5_null(),
27-
json5_boolean(),
28-
json5_string(),
29-
json5_number(),
30-
json5_array(),
31-
json5_object(config)
32-
])
10+
defp backend do
11+
Json5.Decode.Backend.Combine
3312
end
34-
35-
defp json5_null, do: Decode.Null.null()
36-
defp json5_boolean, do: Decode.Boolean.boolean()
37-
defp json5_string, do: Decode.String.string()
38-
defp json5_number, do: Decode.Number.number()
39-
defp json5_array, do: Decode.Array.array()
40-
defp json5_object(config), do: Decode.Object.object(config)
4113
end

lib/json5/decode/backend/combine.ex

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
defmodule Json5.Decode.Backend.Combine do
2+
@moduledoc """
3+
Decode Json5 string to Elixir term
4+
"""
5+
import Combine.Parsers.Base
6+
import Json5.Decode.Backend.Combine.Helper
7+
8+
alias Json5.Decode.Backend.Combine.Array
9+
alias Json5.Decode.Backend.Combine.Boolean
10+
alias Json5.Decode.Backend.Combine.Null
11+
alias Json5.Decode.Backend.Combine.Number
12+
alias Json5.Decode.Backend.Combine.Object
13+
alias Json5.Decode.Backend.Combine.String
14+
15+
def parse(input, config \\ %{}) do
16+
case Combine.parse(input, parser(config)) do
17+
{:error, error} -> {:error, error}
18+
[val] -> {:ok, val}
19+
end
20+
end
21+
22+
def parser(config) do
23+
ignore_whitespace()
24+
|> json5_value(config)
25+
|> ignore_whitespace()
26+
|> eof()
27+
end
28+
29+
def json5_value(prev \\ nil, config \\ %{}) do
30+
choice(prev, [
31+
json5_null(),
32+
json5_boolean(),
33+
json5_string(),
34+
json5_number(),
35+
json5_array(),
36+
json5_object(config)
37+
])
38+
end
39+
40+
defp json5_null, do: Null.null()
41+
defp json5_boolean, do: Boolean.boolean()
42+
defp json5_string, do: String.string()
43+
defp json5_number, do: Number.number()
44+
defp json5_array, do: Array.array()
45+
defp json5_object(config), do: Object.object(config)
46+
end

lib/json5/decode/array.ex lib/json5/decode/backend/combine/array.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
defmodule Json5.Decode.Array do
1+
defmodule Json5.Decode.Backend.Combine.Array do
22
@moduledoc """
33
Documentation for `Json5`.
44
"""
55
import Combine.Parsers.Base
66
import Combine.Parsers.Text
7-
import Json5.Decode.Helper
7+
import Json5.Decode.Backend.Combine.Helper
88

99
def array do
1010
either(

lib/json5/decode/boolean.ex lib/json5/decode/backend/combine/boolean.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
defmodule Json5.Decode.Boolean do
1+
defmodule Json5.Decode.Backend.Combine.Boolean do
22
@moduledoc """
33
Documentation for `Json5`.
44
"""
55
import Combine.Parsers.Base
66
import Combine.Parsers.Text
77

8-
def boolean(), do: either(boolean_true(), boolean_false())
8+
def boolean, do: either(boolean_true(), boolean_false())
99

10-
defp boolean_true() do
10+
defp boolean_true do
1111
"true"
1212
|> string()
1313
|> label("true")
1414
|> map(fn _ -> true end)
1515
end
1616

17-
defp boolean_false() do
17+
defp boolean_false do
1818
"false"
1919
|> string()
2020
|> label("false")

lib/json5/decode/helper.ex lib/json5/decode/backend/combine/helper.ex

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
defmodule Json5.Decode.Helper do
1+
defmodule Json5.Decode.Backend.Combine.Helper do
2+
@moduledoc """
3+
General helper functions
4+
"""
25
import Combine.Helpers
36
import Combine.Parsers.Base
47
import Combine.Parsers.Text
58

69
alias Combine.ParserState
7-
alias Json5.Decode
10+
alias Json5.Decode.Backend.Combine, as: Decode
811

912
@elements [
1013
:remove_white_space,
@@ -36,7 +39,7 @@ defmodule Json5.Decode.Helper do
3639

3740
defguard is_line_terminator(ch) when ch in @line_terminator_chars
3841

39-
def lazy_json5_value() do
42+
def lazy_json5_value do
4043
lazy(fn -> Decode.json5_value() end)
4144
end
4245

lib/json5/decode/null.ex lib/json5/decode/backend/combine/null.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
defmodule Json5.Decode.Null do
1+
defmodule Json5.Decode.Backend.Combine.Null do
22
@moduledoc """
33
Documentation for `Json5`.
44
"""
55
import Combine.Parsers.Base
66
import Combine.Parsers.Text
77

8-
def null() do
8+
def null do
99
"null"
1010
|> string()
1111
|> label("null")

lib/json5/decode/number.ex lib/json5/decode/backend/combine/number.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
defmodule Json5.Decode.Number do
1+
defmodule Json5.Decode.Backend.Combine.Number do
22
@moduledoc """
33
Documentation for `Json5`.
44
"""
55
import Combine.Parsers.Base
66
import Combine.Parsers.Text
77

8-
def number() do
8+
def number do
99
either(ecma_hex_integer_literal(), ecma_decimal_literal())
1010
end
1111

12-
defp ecma_hex_integer_literal() do
12+
defp ecma_hex_integer_literal do
1313
pipe(
1414
[
1515
ignore(either(string("0x"), string("0X"))),
@@ -68,7 +68,7 @@ defmodule Json5.Decode.Number do
6868
prev |> many1(digit()) |> map(&Enum.join/1)
6969
end
7070

71-
defp ecma_exponent_part() do
71+
defp ecma_exponent_part do
7272
both(either(char("e"), char("E")), ecma_signed_integer(), &"#{&1}#{&2}")
7373
end
7474

lib/json5/decode/object.ex lib/json5/decode/backend/combine/object.ex

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
defmodule Json5.Decode.Object do
1+
defmodule Json5.Decode.Backend.Combine.Object do
22
@moduledoc """
33
Documentation for `Json5`.
44
"""
55
import Combine.Parsers.Base
66
import Combine.Parsers.Text
7-
import Json5.Decode.Helper
7+
import Json5.Decode.Backend.Combine.Helper
88

9-
alias Json5.Decode
9+
alias Json5.Decode.Backend.Combine.String, as: Json5String
1010
alias Json5.ECMA
1111

1212
require Json5.ECMA
@@ -63,19 +63,19 @@ defmodule Json5.Decode.Object do
6363
)
6464
end
6565

66-
defp json5_member_name() do
66+
defp json5_member_name do
6767
either(
6868
ecma_identifier(),
69-
Decode.String.string()
69+
Json5String.string()
7070
)
7171
end
7272

73-
defp ecma_identifier() do
73+
defp ecma_identifier do
7474
if_not(ecma_reserved_word(), ECMA.ecma_identifier_name())
7575
end
7676

77-
defp ecma_reserved_word() do
78-
choice(Enum.map(Json5.ECMA.reserved_words(), &string/1))
77+
defp ecma_reserved_word do
78+
choice(Enum.map(ECMA.reserved_words(), &string/1))
7979
end
8080

8181
defp cast_json5_member([key, value], %{object_key_function: func})

lib/json5/decode/string.ex lib/json5/decode/backend/combine/string.ex

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
defmodule Json5.Decode.String do
1+
defmodule Json5.Decode.Backend.Combine.String do
22
@moduledoc """
33
Documentation for `Json5`.
44
"""
55
import Combine.Parsers.Base
66
import Combine.Parsers.Text
7-
alias Json5.Decode.Helper
7+
alias Json5.Decode.Backend.Combine.Helper
88

9-
def string() do
9+
def string do
1010
either(
1111
between(char("'"), json5_single_string_characters(), char("'")),
1212
between(char("\""), json5_double_string_characters(), char("\""))
@@ -19,7 +19,7 @@ defmodule Json5.Decode.String do
1919
|> map(&:erlang.iolist_to_binary/1)
2020
end
2121

22-
defp json5_single_string_character() do
22+
defp json5_single_string_character do
2323
if_not(char("'"), escape_new_line_char())
2424
end
2525

@@ -29,11 +29,11 @@ defmodule Json5.Decode.String do
2929
|> map(&:erlang.iolist_to_binary/1)
3030
end
3131

32-
defp json5_double_string_character() do
32+
defp json5_double_string_character do
3333
if_not(char("\""), escape_new_line_char())
3434
end
3535

36-
defp escape_new_line_char() do
36+
defp escape_new_line_char do
3737
either(
3838
ignore(sequence([char("\\"), Helper.ecma_line_terminator()])),
3939
char()

lib/json5/ecma.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ defmodule Json5.ECMA do
7979
end
8080
end
8181

82-
def ecma_identifier_name() do
82+
def ecma_identifier_name do
8383
pipe(
8484
[
8585
ecma_identifier_start(),
@@ -89,7 +89,7 @@ defmodule Json5.ECMA do
8989
)
9090
end
9191

92-
defp ecma_identifier_start() do
92+
defp ecma_identifier_start do
9393
choice([
9494
char("$"),
9595
char("_"),
@@ -98,14 +98,14 @@ defmodule Json5.ECMA do
9898
])
9999
end
100100

101-
defp ecma_identifier_part() do
101+
defp ecma_identifier_part do
102102
either(
103103
ecma_identifier_start(),
104104
satisfy(char(), &is_unicode_identifier_letter/1)
105105
)
106106
end
107107

108-
defp ecma_unicode_letter() do
108+
defp ecma_unicode_letter do
109109
satisfy(char(), fn
110110
<<ch>> when is_unicode_letter(ch) -> true
111111
_ -> false

lib/json5/encode.ex

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ defmodule Json5.Encode do
55
require Decimal
66
require Json5.ECMA
77

8-
alias Json5.Encode
8+
alias Json5.Encode.Array
9+
alias Json5.Encode.Error
10+
alias Json5.Encode.Object
911

1012
defguardp is_to_string(input)
1113
when input in [true, false] or is_float(input) or is_integer(input) or
@@ -35,15 +37,14 @@ defmodule Json5.Encode do
3537
end
3638

3739
def do_dump(input, config) when is_list(input) do
38-
Encode.Array.dump(input, config)
40+
Array.dump(input, config)
3941
end
4042

4143
def do_dump(input, config) when is_map(input) and not is_struct(input) do
42-
Encode.Object.dump(input, config)
44+
Object.dump(input, config)
4345
end
4446

4547
def do_dump(input, _) do
46-
{:error,
47-
Json5.Encode.Error.exception(%{type: :invalid_input, input: input})}
48+
{:error, Error.exception(%{type: :invalid_input, input: input})}
4849
end
4950
end

0 commit comments

Comments
 (0)