more.sdl

An SDL (Simple Declarative Language) parser. Supports StAX/SAX style API. See more.std.dom for DOM style API.

Members

Aliases

LineNumber
alias LineNumber = size_t
Undocumented in source.

Classes

SdlParseException
class SdlParseException

Thrown by sdl parse functions when invalid SDL is encountered.

Enums

SdlErrorType
enum SdlErrorType

Used in SdlParseException to distinguish specific sdl parse errors.

Functions

arrayRange
string arrayRange(char min, char max, string initializer)
Undocumented in source. Be warned that the author may not have intended to support it.
isID
bool isID(dchar c)
Undocumented in source. Be warned that the author may not have intended to support it.
isIDStart
bool isIDStart(dchar c)
Undocumented in source. Be warned that the author may not have intended to support it.
isSdlKeyword
bool isSdlKeyword(char[] token)
Undocumented in source. Be warned that the author may not have intended to support it.
parseOneSdlTag
void parseOneSdlTag(Tag* tag, char[] sdlText)

A convenience function to parse a single tag. Calls tag.resetForNewSdl and then calls parseSdlTag.

parseSdlTag
bool parseSdlTag(Tag* tag, char[]* sdlText)

Parses one SDL tag (not including its children) from sdlText saving slices for every name/value/attribute to the given tag struct. This function assumes that sdlText contains at least one full SDL _tag. The only time this function will allocate memory is if the value/attribute appenders in the tag struct are not large enough to hold all the values. Because of this, after the tag values/attributes are populated, it is up to the caller to copy any memory they wish to save unless sdlText is going to persist in memory. Note: this function does not handle the UTF-8 bom because it doesn't make sense to re-check for the BOM before every tag.

rangeInitializers
string rangeInitializers(string[] s)
Undocumented in source. Be warned that the author may not have intended to support it.
rangeInitializersCurrent
string rangeInitializersCurrent(string[] s)
Undocumented in source. Be warned that the author may not have intended to support it.
rangeInitializersNext
string rangeInitializersNext(string[] s)
Undocumented in source. Be warned that the author may not have intended to support it.
sdlLiteralToD
T sdlLiteralToD(const(char)[] literal)

Converts literal to the given D type T. This is a wrapper arround the sdlLiteralToD function that returns true on sucess, except this function returns the value itself and throws an SdlParseException on error.

sdlLiteralToD
bool sdlLiteralToD(const(char)[] literal, T t)

Converts literal to the given D type T. If isSomeString!T, then it will remove the surrounding quotes if they are present.

setupSdlText
char[] setupSdlText(const(char[]) sdlText, bool copySdl)
Undocumented in source. Be warned that the author may not have intended to support it.

Manifest constants

invalidBraceFmt
enum invalidBraceFmt;
Undocumented in source.
mixedValuesAndAttributesFmt
enum mixedValuesAndAttributesFmt;
Undocumented in source.
noEndingQuote
enum noEndingQuote;
Undocumented in source.
notEnoughCloseBracesFmt
enum notEnoughCloseBracesFmt;
Undocumented in source.
tooManyEndingBraces
enum tooManyEndingBraces;
Undocumented in source.

Structs

Attribute
struct Attribute

Holds three character arrays for an SDL attribute, namespace/id/value.

Dependency
struct Dependency
Undocumented in source.
Package
struct Package
Undocumented in source.
SdlBuffer2Sink
struct SdlBuffer2Sink
Undocumented in source.
SdlParser
struct SdlParser(A)
Undocumented in source.
SdlWalker
struct SdlWalker

Assists in walking an SDL tree which supports the StAX method of parsing.

Tag
struct Tag

Contains a tag's name, values and attributes. It does not contain any information about its child tags because that part of the sdl would not have been parsed yet, however, it does indicate if the tag was followed by an open brace. This struct is used directly for the StAX/SAX APIs and indirectly for the DOM or Reflection APIs.

Variables

sdlBuffer
char[2048] sdlBuffer;
Undocumented in source.
sdlBuffer2
char[sdlBuffer.length] sdlBuffer2;
Undocumented in source.
sdlIDFlag
enum ubyte sdlIDFlag;
Undocumented in source.
sdlNumberFlag
enum ubyte sdlNumberFlag;
Undocumented in source.
sdlNumberPostfixFlag
enum ubyte sdlNumberPostfixFlag;
Undocumented in source.
sdlValidAfterTagItemFlag
enum ubyte sdlValidAfterTagItemFlag;
Undocumented in source.

Examples

1 void printTags(char[] sdl) {
2     Tag tag;
3     while(parseSdlTag(&tag, &sdl)) {
4         writefln("(depth %s) tag '%s' values '%s' attributes '%s'",
5             tag.depth, tag.name, tag.values.data, tag.attributes.data);
6     }
7 }
8 
9 struct Person {
10     string name;
11     ushort age;
12     string[] nicknames;
13     auto children = appender!(Person[])();
14     void reset() {
15         name = null;
16         age = 0;
17         nicknames = null;
18         children.clear();
19     }
20     void parseFromSdl(ref SdlWalker walker) {
21         tag.enforceNoValues();
22         tag.enforceNoAttributes();
23         reset();
24         foreach(auto personWalker = walker.children();
25                 !personWalker.empty; personWalker.popFront) {
26 
27             if(tag.name == "name") {
28 
29                 tag.enforceNoAttributes();
30                 tag.enforceNoChildren();
31                 tag.getOneValue(name);
32 
33             } else if(tag.name == "age") {
34 
35                 tag.enforceNoAttributes();
36                 tag.enforceNoChildren();
37                 tag.getOneValue(age);
38 
39             } else if(tag.name == "nicknames") {
40 
41                 tag.enforceNoAttributes();
42                 tag.enforceNoChildren();
43                 tag.getValues(nicknames);
44 
45             } else if(tag.name == "child") {
46 
47 
48 
49                 // todo implement
50 
51             } else tag.throwIsUnknown();
52         }
53     }
54     void validate() {
55         if(name == null) throw new Exception("person is missing the 'name' tag");
56         if(age == 0) throw new Exception("person is missing the 'age' tag");
57     }
58 }
59 
60 void parseTags(char[] sdl) {
61     struct Person {
62         string name;
63         ushort age;
64         string[] nicknames;
65         Person[] children;
66         void reset() {
67             name = null;
68             age = 0;
69             nicknames = null;
70             children = null;
71         }
72         void validate() {
73             if(name == null) throw new Exception("person is missing the 'name' tag");
74             if(age == 0) throw new Exception("person is missing the 'age' tag");
75         }
76     }
77     auto people = appender(Person[])();
78     Person person;
79 
80     Tag tag;
81     for(auto walker = SdlWalker(&tag, sdl); !walker.empty; walker.popFront) {
82         if(tag.name == "person") {
83 
84             tag.enforceNoValues();
85             tag.enforceNoAttributes();
86             person.reset();
87             person.validate();
88             people.put(person);
89 
90         } else tag.throwIsUnknown();
91     }
92 }

TODO: implement escaped strings TODO: finish unit tests TODO: write a input-range sdl parser TODO: implement datetime/timespans

Meta

Authors

Jonathan Marler, johnnymarler@gmail.com

License

use freely for any purpose