Function pullParseSource

Parses an SDL document using StAX/Pull-style. Returns an InputRange with element type ParserEvent.

The pullParseFile version reads a file and parses it, while pullParseSource parses a string passed in. The optional 'filename' parameter in pullParseSource can be included so that the SDL document's filename (if any) can be displayed with any syntax error messages.

Warning! The FileStartEvent and FileEndEvent events *might* be removed later. See https://github.com/Abscissa/SDLang-D/issues/17

Prototype

auto libInputVisitor.InputVisitor!(sdlang.parser.PullParser,std.variant.VariantN!(104L,sdlang.parser.FileStartEvent,sdlang.parser.FileEndEvent,sdlang.parser.TagStartEvent,sdlang.parser.TagEndEvent,sdlang.parser.ValueEvent,sdlang.parser.AttributeEvent).VariantN) pullParseSource(
  string source,
  string filename = null
);

Example

parent 12 attr="q" {
	childA 34
	childB 56
}
lastTag

The ParserEvent sequence emitted for that SDL document would be as follows (indented for readability):

FileStartEvent
	TagStartEvent (parent)
		ValueEvent (12)
		AttributeEvent (attr, "q")
		TagStartEvent (childA)
			ValueEvent (34)
		TagEndEvent
		TagStartEvent (childB)
			ValueEvent (56)
		TagEndEvent
	TagEndEvent
	TagStartEvent  (lastTag)
	TagEndEvent
FileEndEvent

Example

foreach(event; pullParseFile("stuff.sdl"))
{
	import std.stdio;

	if(event.peek!FileStartEvent())
		writeln("FileStartEvent, starting! ");

	else if(event.peek!FileEndEvent())
		writeln("FileEndEvent, done! ");

	else if(auto e = event.peek!TagStartEvent())
		writeln("TagStartEvent: ", e.namespace, ":", e.name, " @ ", e.location);

	else if(event.peek!TagEndEvent())
		writeln("TagEndEvent");

	else if(auto e = event.peek!ValueEvent())
		writeln("ValueEvent: ", e.value);

	else if(auto e = event.peek!AttributeEvent())
		writeln("AttributeEvent: ", e.namespace, ":", e.name, "=", e.value);

	else // Shouldn't happen
		throw new Exception("Received unknown parser event");
}

Authors

Copyright

License