1 // SDLang-D
2 // Written in the D programming language.
3 
4 /++
5 $(H2 SDLang-D v0.10.1)
6 
7 Library for parsing and generating SDL (Simple Declarative Language).
8 
9 Import this module to use SDLang-D as a library.
10 
11 For the list of officially supported compiler versions, see the
12 $(LINK2 https://github.com/Abscissa/SDLang-D/blob/master/.travis.yml, .travis.yml)
13 file included with your version of SDLang-D.
14 
15 Links:
16 $(UL
17 	$(LI $(LINK2 http://sdlang.org/, SDLang Language Homepage) )
18 	$(LI $(LINK2 https://github.com/Abscissa/SDLang-D, SDLang-D Homepage) )
19 	$(LI $(LINK2 http://semitwist.com/sdlang-d, SDLang-D API Reference (latest version) ) )
20 	$(LI $(LINK2 http://semitwist.com/sdlang-d-docs, SDLang-D API Reference (earlier versions) ) )
21 	$(LI $(LINK2 http://sdl.ikayzo.org/display/SDL/Language+Guide, Old Official SDL Site) [$(LINK2 http://semitwist.com/sdl-mirror/Language+Guide.html, mirror)] )
22 )
23 
24 Authors: Nick Sabalausky ("Abscissa") http://semitwist.com/contact
25 Copyright:
26 Copyright (C) 2012-2016 Nick Sabalausky.
27 
28 License: $(LINK2 https://github.com/Abscissa/SDLang-D/blob/master/LICENSE.txt, zlib/libpng)
29 +/
30 
31 module gfx.decl.sdlang;
32 
33 import std.array;
34 import std.datetime;
35 import std.file;
36 import std.stdio;
37 
38 import gfx.decl.sdlang.ast;
39 import gfx.decl.sdlang.exception;
40 import gfx.decl.sdlang.lexer;
41 import gfx.decl.sdlang.parser;
42 import gfx.decl.sdlang.symbol;
43 import gfx.decl.sdlang.token;
44 import gfx.decl.sdlang.util;
45 
46 // Expose main public API
47 public import gfx.decl.sdlang.ast       : Attribute, Tag;
48 public import gfx.decl.sdlang.exception;
49 public import gfx.decl.sdlang.parser    : parseFile, parseSource;
50 public import gfx.decl.sdlang.token     : Value, Token, DateTimeFrac, DateTimeFracUnknownZone;
51 public import gfx.decl.sdlang.util      : sdlangVersion, Location;
52 
53 version(sdlangUsingBuiltinTestRunner)
54 	void main() {}
55 
56 version(sdlangCliApp)
57 {
58 	int main(string[] args)
59 	{
60 		if(
61 			args.length != 3 ||
62 			(args[1] != "lex" && args[1] != "parse" && args[1] != "to-sdl")
63 		)
64 		{
65 			stderr.writeln("SDLang-D v", sdlangVersion);
66 			stderr.writeln("Usage: sdlang [lex|parse|to-sdl] filename.sdl");
67 			return 1;
68 		}
69 
70 		auto filename = args[2];
71 
72 		try
73 		{
74 			if(args[1] == "lex")
75 				doLex(filename);
76 			else if(args[1] == "parse")
77 				doParse(filename);
78 			else
79 				doToSDL(filename);
80 		}
81 		catch(ParseException e)
82 		{
83 			stderr.writeln(e.msg);
84 			return 1;
85 		}
86 
87 		return 0;
88 	}
89 
90 	void doLex(string filename)
91 	{
92 		auto source = cast(string)read(filename);
93 		auto lexer = new Lexer(source, filename);
94 
95 		foreach(tok; lexer)
96 		{
97 			// Value
98 			string value;
99 			if(tok.symbol == symbol!"Value")
100 				value = tok.value.hasValue? toString(tok.value.type) : "{null}";
101 
102 			value = value==""? "\t" : "("~value~":"~tok.value.toString()~") ";
103 
104 			// Data
105 			auto data = tok.data.replace("\n", "").replace("\r", "");
106 			if(data != "")
107 				data = "\t|"~tok.data~"|";
108 
109 			// Display
110 			writeln(
111 				tok.location.toString, ":\t",
112 				tok.symbol.name, value,
113 				data
114 			);
115 
116 			if(tok.symbol.name == "Error")
117 				break;
118 		}
119 	}
120 
121 	void doParse(string filename)
122 	{
123 		auto root = parseFile(filename);
124 		stdout.rawWrite(root.toDebugString());
125 		writeln();
126 	}
127 
128 	void doToSDL(string filename)
129 	{
130 		auto root = parseFile(filename);
131 		stdout.rawWrite(root.toSDLDocument());
132 	}
133 }