-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
TOML.jl
117 lines (94 loc) · 3.94 KB
/
TOML.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
module TOML
module Internals
# The parser is defined in Base
using Base.TOML: Parser, parse, tryparse, ParserError, isvalid_barekey_char, reinit!
# Put the error instances in this module
for errtype in instances(Base.TOML.ErrorType)
@eval using Base.TOML: $(Symbol(errtype))
end
# We put the printing functionality in a separate module since It
# defines a function `print` and we don't want that to collide with normal
# usage of `(Base.)print` in other files
module Printer
include("print.jl")
end
end
"""
Parser()
Constructor for a TOML `Parser`. Note that in most cases one does not need to
explicitly create a `Parser` but instead one directly use use
[`TOML.parsefile`](@ref) or [`TOML.parse`](@ref). Using an explicit parser
will however reuse some internal data structures which can be beneficial for
performance if a larger number of small files are parsed.
"""
const Parser = Internals.Parser
"""
parsefile(f::AbstractString)
parsefile(p::Parser, f::AbstractString)
Parse file `f` and return the resulting table (dictionary). Throw a
[`ParserError`](@ref) upon failure.
See also: [`TOML.tryparsefile`](@ref)
"""
parsefile(f::AbstractString) =
Internals.parse(Parser(read(f, String); filepath=abspath(f)))
parsefile(p::Parser, f::AbstractString) =
Internals.parse(Internals.reinit!(p, read(f, String); filepath=abspath(f)))
"""
tryparsefile(f::AbstractString)
tryparsefile(p::Parser, f::AbstractString)
Parse file `f` and return the resulting table (dictionary). Return a
[`ParserError`](@ref) upon failure.
See also: [`TOML.parsefile`](@ref)
"""
tryparsefile(f::AbstractString) =
Internals.tryparse(Parser(read(f, String); filepath=abspath(f)))
tryparsefile(p::Parser, f::AbstractString) =
Internals.tryparse(Internals.reinit!(p, read(f, String); filepath=abspath(f)))
"""
parse(x::Union{AbstractString, IO})
parse(p::Parser, x::Union{AbstractString, IO})
Parse the string or stream `x`, and return the resulting table (dictionary).
Throw a [`ParserError`](@ref) upon failure.
See also: [`TOML.tryparse`](@ref)
"""
parse(str::AbstractString) =
Internals.parse(Parser(String(str)))
parse(p::Parser, str::AbstractString) =
Internals.parse(Internals.reinit!(p, String(str)))
parse(io::IO) = parse(read(io, String))
parse(p::Parser, io::IO) = parse(p, read(io, String))
"""
tryparse(x::Union{AbstractString, IO})
tryparse(p::Parser, x::Union{AbstractString, IO})
Parse the string or stream `x`, and return the resulting table (dictionary).
Return a [`ParserError`](@ref) upon failure.
See also: [`TOML.parse`](@ref)
"""
tryparse(str::AbstractString) =
Internals.tryparse(Parser(String(str)))
tryparse(p::Parser, str::AbstractString) =
Internals.tryparse(Internals.reinit!(p, String(str)))
tryparse(io::IO) = tryparse(read(io, String))
tryparse(p::Parser, io::IO) = tryparse(p, read(io, String))
"""
ParserError
Type that is returned from [`tryparse`](@ref) and [`tryparsefile`](@ref)
when parsing fails. It contains (among others) the following fields:
- `pos`, the position in the string when the error happened
- `table`, the result that so far was successfully parsed
- `type`, an error type, different for different types of errors
"""
const ParserError = Internals.ParserError
"""
print([to_toml::Function], io::IO [=stdout], data::AbstractDict; sort=false, by=identity)
Writes `data` as TOML syntax to the stream `io`. The keyword argument `sort`
sorts the output on the keys of the tables with the top level tables are
sorted according to the keyword argument `by`.
The following data types are supported: `AbstractDict`, `Integer`, `AbstractFloat`, `Bool`,
`Dates.DateTime`, `Dates.Time`, `Dates.Date`. Note that the integers and floats
need to be convertible to `Float64` and `Int64` respectively. For other data types,
pass the function `to_toml` that takes the data types and returns a value of a
supported type.
"""
const print = Internals.Printer.print
end