Dokumentacja TreeStructInfoParserJS
Opis ogólny
TreeStructInfoParserJS
to parser formatu TreeStructInfo, służącego do reprezentacji zagnieżdżonych struktur drzewiastych z atrybutami, wartościami referencyjnymi i opcjonalnym rzutowaniem typów. Wspiera:
- nagłówki wersji,
- wieloliniowe wartości,
- atrybuty i węzły referencyjne,
- rzutowanie typów na liczby, boolean, base64 itp.
Konstruktor
new TreeStructInfoParserJS()
Tworzy nową instancję parsera.
Ustawienia
setAutoCasting(value: boolean): void
Włącza lub wyłącza automatyczne rzutowanie typów (np. "true"
na true
, "0xFF"
na 255
).
setMultilineJoinChar(char: string): void
Ustawia znak, który będzie używany do łączenia wartości wieloliniowych (domyślnie \n
).
setBoolValues(trueValues: string[], falseValues: string[]): void
Definiuje własne ciągi znaków uznawane za wartości typu boolean:
trueValues
: np.["yes", "on"]
falseValues
: np.["no", "off"]
Parsowanie
parse(input: string): { version: string, name: string|null, data: Object }
Parsuje pełny dokument TreeStructInfo
i zwraca obiekt zawierający:
version
: numer wersji (np."1.0"
)name
: opcjonalna nazwa drzewadata
: zagnieżdżona struktura obiektowa reprezentująca drzewo
Obsługiwane elementy formatu
treestructinfo "1.0" name "TreeName"
– nagłówek z wersją i nazwąattr Name "Value"
– zwykłe atrybutyref attr Name "Value"
– atrybuty globalne do ponownego użyciaref node Name ... end ref node
– definicje węzłów referencyjnychref attr Name
/ref node Name
– odwołania do zdefiniowanych wcześniej wartościnode Name ... end node
– zagnieżdżone węzły"multi-line"
– kontynuacja wartości z poprzedniego atrybutu:: Comment
– komentarze
Mechanizm rzutowania (autoCast
)
Jeśli autoCasting
jest włączone, parser automatycznie rozpoznaje:
Wzorzec wejściowy | Typ wynikowy |
---|---|
"true" / "false" |
boolean |
"123" |
number (int) |
"3.14" lub "3,14" |
number (float) |
"0x1F" |
number (hex) |
"0b1010" |
number (bin) |
"0o755" |
number (oct) |
"YWJj" (base64) |
string (decoded) |
"X,Y" np. 12,34 |
number[] (coord) |
Przechowywane dane wewnętrzne
this.lines
– główne linie drzewathis.refLines
– linie z referencjami do węzłówthis.references
– mapa wartości referencyjnychthis.nodeReferences
– mapa referencyjnych węzłówthis.autoCasting
– flaga rzutowania typówthis.trueValues
/this.falseValues
– słowa uznawane za boolean
Błędy i wyjątki
Metody parse
, parseNode
, parseTree
, resolveRefAttr
, resolveRefNode
rzucają wyjątki (Error
) w przypadku:
- nieprawidłowego formatu,
- nieznanych referencji,
- błędnej składni drzewa.
Przykład użycia
const parser = new TreeStructInfoParserJS();
parser.setAutoCasting(true);
parser.setBoolValues(["yes"], ["no"]);
const input = `
treestructinfo "1.0" name "Example"
attr Title "My Tree"
node Root
attr Flag "yes"
attr Count "0x10"
end node
end tree
`;
const result = parser.parse(input);
console.log(result);
/*
{
version: "1.0",
name: "Example",
data: {
Title: "My Tree",
Root: {
Flag: true,
Count: 16
}
}
}
*/
Licencja
Licencjonowane na warunkach licencji BSD 3-Clause
BSD-3-Clause
Сopyright (c) 2025 Dariusz Rorat
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. -
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution. - Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.