1 /++ 2 Wrapper for SDLite. 3 4 Noticable differences between SDLang and SDLite parser: 5 - Line breaking is not allowed ("title \", newline, " 'value'") 6 +/ 7 module sily.sdlang; 8 9 import sdl = sdlite; 10 import std.range; 11 12 import taggedalgebraic.taggedunion; 13 14 /++ 15 Representation of single sdlang node 16 Example: 17 --- 18 // Create SDLNode. SDLNode("name", SDLValue[] values, SDLAttribute[] attributes, SDLNode[] children) 19 SDLNode node = SDLNode("name", [SDLValue.text("values")], [], []); 20 // Get name 21 node.name; 22 // Get namespace 23 node.namespace; 24 // Get/Set qualified name (eq to namespace:name) 25 node.qualifiedName; 26 node.qualifiedName = "namespace:name"; 27 // Get array of values (aka 'node 1 "b" v=2' -> returns 1 "b") 28 node.values; 29 // Get array of attributes (aka 'node 1 "b" v=2' -> returns v=2) 30 node.attributes; 31 // Get array of children 32 node.children; 33 // Gets attribute by qualified name 34 node.getAttribute("email") 35 // Gets attribute by qualified name with default value 36 node.getAttribute("email", SDLValue.text("mail@mail.com")) 37 --- 38 +/ 39 alias SDLNode = sdl.SDLNode; 40 41 /++ 42 Value of sdlang node 43 Example: 44 --- 45 // Create new value 46 SDLValue val = SDLValue.double_(22.5); 47 // Get value casted to int 48 val.value!int; 49 // Check type 50 val.kind == SDLType.text; 51 --- 52 +/ 53 alias SDLValue = sdl.SDLValue; 54 55 /++ 56 Attribute of sdlang node (attr="val") 57 Example: 58 --- 59 // Create new attribute 60 SDLAttribute attr = SDLAttribute("qualifiedName", SDLValue.text("value")); 61 // Get name 62 attr.name; 63 // Get namepsace 64 attr.namespace; 65 // Get/Set qualified name (namespace:name) 66 attr.qualifiedName; 67 attr.qualifiedName = "namespace:name"; 68 // Get/Set value 69 attr.value; 70 attr.value = SDLValue.text("new value") 71 --- 72 +/ 73 alias SDLAttribute = sdl.SDLAttribute; 74 75 /++ 76 Alias to SDLValue.Kind. Represents type of SDLValue. 77 Example: 78 --- 79 node.values[0].kind == SDLType.float_; 80 --- 81 Defined types: 82 --- 83 Void null_; 84 string text; 85 immutable(ubyte)[] binary; 86 int int_; 87 long long_; 88 long[2] decimal; 89 float float_; 90 double double_; 91 bool bool_; 92 SysTime dateTime; 93 Date date; 94 Duration duration; 95 --- 96 +/ 97 alias SDLType = sdl.SDLValue.Kind; 98 99 /++ 100 Parses SDL string into SDLNode[] 101 Example: 102 --- 103 import sily.sdlang; 104 import std.file; 105 SDLNode[] arr1 = parseSDL(readText("file.sdl")); 106 SDLNode[] arr2 = parseSDL("name \"Direct SDLang parsing\" cool=true"); 107 --- 108 +/ 109 SDLNode[] parseSDL(string input) { 110 SDLNode[] result; 111 sdl.parseSDLDocument!((n) { result ~= n; })(input, ""); 112 return result; 113 } 114 115 private alias generateSDLang = sdl.generateSDLang; 116 117 /++ 118 Writes SDL data into string 119 Example: 120 --- 121 import sily.sdlang; 122 import std.file; 123 SDLNode[] arr1 = parseSDL(readText("file.sdl")); 124 string out = arr1.generateSDL(); 125 --- 126 +/ 127 string generateSDL(SDLNode[] input) { 128 auto app = appender!string; 129 app.generateSDLang(input); 130 return app.data; 131 } 132 133 /// Ditto 134 string generateSDL(SDLNode input) { 135 auto app = appender!string; 136 app.generateSDLang(input); 137 return app.data; 138 } 139 140 /// Ditto 141 string generateSDL(SDLValue input) { 142 auto app = appender!string; 143 app.generateSDLang(input); 144 return app.data; 145 } 146