parse(text: string) - > Result<TOML Value, string>- 수신자
- 해당 없음
- 최소 인자 수
- 1
- 최대 인자 수
- 1
Declaration and parameters
링크사용 조건
기본 포함 · 미정
Import std.toml in language mode 5.21.
The current compiler selects std/toml.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
수신자
매개변수 제약
parse accepts a complete TOML 1.0 document. An empty document or comments with whitespace produce an empty root table. Basic and literal strings, multiline strings, dotted and quoted keys, inline tables, arrays and arrays of tables retain their TOML meanings.
Integers must fit the signed 64 bit range. TOML decimal, hexadecimal, octal and binary spellings and valid underscore separators are accepted. Floating values use IEEE binary64 rounding. The TOML inf and nan forms are valid TOML values. Finite overflow follows the TOML numeric error boundary and returns Err.
Offset date times, local date times, local dates and local times remain distinct TOML kinds. Invalid calendar fields and time fields return Err. Strings that resemble dates remain strings.
A key or table definition may not redefine an existing value under the TOML 1.0 rules. Names are compared after decoding. Malformed escapes, leading zeroes and misplaced underscore separators return Err without exposing a partial document.
A failure returns a nonempty string identifying the cause, one based line and Unicode scalar column. CRLF is a single line break. The first offending scalar is the location and an incomplete input points immediately after its last scalar. Source comments and layout are not retained in TOMLValue.
The text argument is evaluated once. Parsing consumes the complete document, performs no I/O and does not invoke JSON conversion. Syntax failures use Err rather than a runtime diagnostic.
결과
The declared result type is Result<TOML.
효과
실패
Result and effects
링크Parse a complete TOML 1.0 document and report invalid input with a cause and position.
Related entries
링크Parse complete TOML 1.0 documents
링크Parse complete TOML 1.0 documents
parse accepts a complete TOML 1.0 document. An empty document or comments with whitespace produce an empty root table. Basic and literal strings, multiline strings, dotted and quoted keys, inline tables, arrays and arrays of tables retain their TOML meanings.
Integers must fit the signed 64 bit range. TOML decimal, hexadecimal, octal and binary spellings and valid underscore separators are accepted. Floating values use IEEE binary64 rounding. The TOML inf and nan forms are valid TOML values. Finite overflow follows the TOML numeric error boundary and returns Err.
Offset date times, local date times, local dates and local times remain distinct TOML kinds. Invalid calendar fields and time fields return Err. Strings that resemble dates remain strings.
A key or table definition may not redefine an existing value under the TOML 1.0 rules. Names are compared after decoding. Malformed escapes, leading zeroes and misplaced underscore separators return Err without exposing a partial document.
A failure returns a nonempty string identifying the cause, one based line and Unicode scalar column. CRLF is a single line break. The first offending scalar is the location and an incomplete input points immediately after its last scalar. Source comments and layout are not retained in TOMLValue.
The text argument is evaluated once. Parsing consumes the complete document, performs no I/O and does not invoke JSON conversion. Syntax failures use Err rather than a runtime diagnostic.
이유
Configuration files need a named format version and predictable rejection of invalid definitions. Preserving TOML kinds lets callers choose a separate and explicit conversion into JSON.
Parse a table and write its deterministic form
링크import std.tomlimport std.testimport std.iolet value = test.assertOk(toml.parse("title = \"Topaz\"\ncount = 6\nactive = true\n[page]\nwidth = 210\n"))io.writeLine(test.assertOk(toml.stringify(value)))let empty = test.assertOk(toml.parse(" # comment\n"))test.assertEq(test.assertOk(toml.stringify(empty)), "")
active = true
count = 6
title = "Topaz"
[page]
width = 210
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
active = truecount = 6title = "Topaz"[page]width = 210
Accept literal strings, multiline values, radix integers and dates
링크import std.tomlimport std.iofor text in (["title = 'ink#paper'\n", "text = \"\"\"\nfirst\nsecond\"\"\"\n", "value = 0xff\n", "date = 1979-05-27T07:32:00Z\n", "value = inf\n", "value = nan\n"]) {match (toml.parse(text)) {case Ok(_) => io.writeLine("accepted")case Err(_) => io.writeLine("rejected")}}
accepted
accepted
accepted
accepted
accepted
accepted
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
acceptedacceptedacceptedacceptedacceptedaccepted
Reject malformed numeric spellings and repeated definitions
링크import std.tomlimport std.iofor text in (["n = 1__0\n", "n = 01\n", "[a]\nx = 1\n[a]\ny = 2\n", "x = 1\nx = 2\n"]) {match (toml.parse(text)) {case Ok(_) => io.writeLine("accepted")case Err(_) => io.writeLine("invalid")}}
invalid
invalid
invalid
invalid
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
invalidinvalidinvalidinvalid
Return line and scalar column in a TOML error
링크import std.tomlimport std.testimport std.iolet error = test.assertErr(toml.parse("x = ?"))test.assertContains(error, "line 1")test.assertContains(error, "column 5")io.writeLine("located")
located
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
located