Parse functions¶
In this section we provide a description of the functions in the Parsetoml library that read a textual representation of a TOML file and return a tree of nodes.
Data types¶
-
object
TomlTableRef
¶ A reference to a
TomlTable
. This is the default return value for all the functions that parse text in TOML format.
-
object
TomlTable
¶ This data type is used by Parsetoml to associate key names with TOML values. The library uses a
OrderedTable
instead of aTable
, so that the order of declaration of the keys in the TOML file is preserved.
-
object
TomlValue
¶ The value associated with a key in a TOML file. It is a parametric type defined by the following code:
TomlValue* = object
case kind* : TomlValueKind
of TomlValueKind.None: nil
of TomlValueKind.Int: intVal* : int64
of TomlValueKind.Float: floatVal* : float64
of TomlValueKind.Bool: boolVal* : bool
of TomlValueKind.Datetime: dateTimeVal* : TomlDateTime
of TomlValueKind.String: stringVal* : string
of TomlValueKind.Array: arrayVal* : seq[TomlValueRef]
of TomlValueKind.Table: tableVal* : TomlTableRef
-
object
TomlError
¶ This exception object is used by Parsetoml to signal errors happened during the parser of text. It has just one field,
location
, which is of type ParserState and has the following public fields:
Field | Type | Description |
---|---|---|
fileName |
string |
Name of the file being parsed (might be “”) |
line |
int |
Number of the line where the error was detected |
column |
int |
Number of the column where the error was detected |
stream |
streams.Stream |
Input stream |
The following example shows how to properly signal an error during the parsing of a TOML file to the user:
try:
# Parse some TOML file here
except parsetoml.TomlError:
# Retrieve information about the location of the error
let loc = (ref parsetoml.TomlError)(getCurrentException()).location
# Print a nicely formatted string explaining what went wrong
echo(loc.fileName & ":" & $(loc.line) & ":" & $(loc.column)
& ": " & getCurrentExceptionMsg())
Procedures¶
The Parsetoml library provides several functions to parse text in TOML
format. Here is an example of application of the
parseString()
procedure:
import parsetoml
# We define a "Parameters" tuple which is initialized using data
# from a TOML file.
type
Parameters = tuple
foo : string
bar : int64
proc parseParams(tree : TomlTableRef) : Parameters =
result.foo = tree.getString("input.foo")
result.bar = tree.getInt("input.bar")
let tree = parsetoml.parseString("""
[input]
foo = "a"
bar = 14
""")
let params = parseParams(tree)
assert params.foo == "a"
assert params.bar == 14
-
proc
parseString
(tomlStr : string, fileName : string = "") → TomlTableRef¶ Assuming that tomlStr is a string containing text in TOML format, this function parses it and returns a reference to a newly created
TomlTable
object.Errors in tomlStr are signaled by raising exceptions of type
TomlError
. Thelocation.fileName
field of the exception itself will be set to fileName.
-
proc
parseStream
(inputStream : streams.Stream, fileName : string = "") → TomlTableRef¶ This function is similar to
parseString()
, but it reads data from inputStream. The stream is parsed while it is being read (i.e., the parsing does not have to wait till the whole file has been read in memory).
-
proc
parseFile
(f : File, fileName : string = "") → TomlTableRef¶ The same as
parseStream()
, but this procedure accepts aFile
instead of astreams.Stream
.
-
proc
parseFile
(fileName : string) → TomlTableRef This is a wrapper to the previous implementation of
parseFile
: it handles the opening/closing of the file named fileName automatically.