HumanReadableSExpressions.jl Documentation
Getting Started
HumanReadableSExpressions.jl is a Julia package for reading and writing files written in HRSE, or Human Readable S-Expressions, which are a human-readable format for representing data and configuration files that is equivalent and and interchangeable with s-expressions.
HumanReadableSExpressions.jl provides two main functions, readhrse
and writehrse
, which read and write HRSE files, respectively. Both functions support custom serialization and deserialization of types using StructTypes.jl.
API
HumanReadableSExpressions.readhrse
— Functionreadhrse(hrse::IO; options=HrseReadOptions(); type=nothing)
readhrse(hrse::String; options=HrseReadOptions(); type=nothing)
Reads an HRSE file from the given IO object or string and returns the corresponding Julia object. The options
argument can be used to configure the parser. Lists will be read as vectors, pairs as a Pair, symbols and strings as a String, and numeric types as the corresponding Julia type defined in the parser options. If type
is given, the result will be parsed as the given type using its StructTypes.StructType.
Examples
julia> using HumanReadableSExpressions
julia> hrse = """
alpha:
1 2 3 4
5 6
7 8 9
beta: (0 . 3)
gamma:
a: 1
b: 2
c: "c"
""";
julia> readhrse(hrse)
3-element Vector{Pair{String}}:
"alpha" => [[1, 2, 3, 4], [5, 6], [7, 8, 9]]
"beta" => (0 => 3)
"gamma" => Pair{String}["a" => 1, "b" => 2, "c" => "c"]
See also HrseReadOptions
.
HumanReadableSExpressions.HrseReadOptions
— TypeHrseReadOptions(kwargs...)
Stores options for parsing HRSE files.
Arguments
integertypes = [Int64, BigInt]
: A list of signed integer types to try parsing integers as. The first type that can represent the integer will be used.floattype = Float64
: The floating point type to parse floating point numbers as.readcomments = false
: Whether to read comments and store them inCommentedElement
objects; if false, comments are ignored.extensions
: A collection ofExtension
s to HRSE.
See also readhrse
.
HumanReadableSExpressions.writehrse
— Functionwritehrse(io::IO, obj, options::HrsePrintOptions)
writehrse(obj, options::HrsePrintOptions)
Writes the given Julia object to the given IO object as a HRSE file. The options
argument can be used to configure the behavior of the printer. If no IO object is given, the output is written to stdout
. Arbitrary objects are serialized using their StructTypes.StructType.
Examples
julia> using HumanReadableSExpressions
julia> hrse = [
:alpha => [
[1, 2, 3, 4],
[5, 6],
[7, 8, 9]
],
:beta => (0 => 3),
:gamma => [
:a => 1
:b => 2
:c => :c
]
];
julia> writehrse(hrse, HumanReadableSExpressions.HrsePrintOptions())
alpha:
1 2 3 4
5 6
7 8 9
beta: 0: 3
gamma:
a: 1
b: 2
c: c
See also HrsePrintOptions
.
HumanReadableSExpressions.ashrse
— Functionashrse(obj, options::HrsePrintOptions)
Returns the given Julia object as a string containing a HRSE file. The options
argument can be used to configure the behavior of the printer.
See also writehrse
, HrsePrintOptions
.
HumanReadableSExpressions.HrsePrintOptions
— TypeHrsePrintOptions(kwargs...)
Stores options for printing HRSE structures to text.
Arguments
indent = " "
: The string to use for indentation.comments = true
: Whether to print comments fromCommentedElement
objects; if false, comments are ignored.extensions
: A collection ofExtension
s to HRSE.pairmode = COLON_MODE
: ThePairMode
to use when printing pairs.inlineprimitives = 20
: The maximum string length of a list of primitives to print on a single line instead of adding a new indentation level.trailingnewline = true
: Whether to print a trailing newline at the end of the file.
HumanReadableSExpressions.CommentedElement
— TypeCommentedElement(element, comments)
Wraps an element with a list of comments which directly precede it attached to it.
HumanReadableSExpressions.Extension
— TypeAn extension to HRSE's syntax that affects parsing and/or printing.
See also DENSE
.
HumanReadableSExpressions.DENSE
— ConstantAn extension to HRSE; axpects a "dense" HRSE file with a single root-level element instead of a list of elements.
See also Extension
.
HumanReadableSExpressions.PairMode
— TypeA flag that decides how pairs are displayed while printing a HRSE structure.
See also CONDENSED_MODE
, DOT_MODE
, EQUALS_MODE
, COLON_MODE
.
HumanReadableSExpressions.CONDENSED_MODE
— ConstantA pair printing mode; pairs should all be condensed to a single line using classic s-expressions, eleminating as many spaces as possible.
See also PairMode
.
HumanReadableSExpressions.DOT_MODE
— ConstantA pair printing mode; pairs should all be displayed using classic s-expressions with dot-deliminated pairs, using indentation to show nesting.
See also PairMode
.
HumanReadableSExpressions.EQUALS_MODE
— ConstantA pair printing mode; pairs should all be displayed using equals-sign-deliminated pairs with implied parentheses, using indentation to show nesting.
See also PairMode
.
HumanReadableSExpressions.COLON_MODE
— ConstantA pair printing mode; pairs should all be displayed using colon-deliminated pairs with implied parentheses, using indentation to show nesting and avoiding parentheses around lists as possible.
See also PairMode
.