1 /**
2  * Temple (C) Dylan Knutson, 2013, distributed under the:
3  * Boost Software License - Version 1.0 - August 17th, 2003
4  *
5  * Permission is hereby granted, free of charge, to any person or organization
6  * obtaining a copy of the software and accompanying documentation covered by
7  * this license (the "Software") to use, reproduce, display, distribute,
8  * execute, and transmit the Software, and to prepare derivative works of the
9  * Software, and to permit third-parties to whom the Software is furnished to
10  * do so, all subject to the following:
11  *
12  * The copyright notices in the Software and this entire statement, including
13  * the above license grant, this restriction and the following disclaimer,
14  * must be included in all copies of the Software, in whole or in part, and
15  * all derivative works of the Software, unless such copies or derivative
16  * works are solely in the form of machine-executable object code generated by
17  * a source language processor.
18  */
19 
20 module temple.delims;
21 
22 import
23 	std.traits,
24 	std.typecons;
25 
26 /// Represents a delimer and the index that it is located at
27 template DelimPos(D = Delim)
28 {
29 	alias DelimPos = Tuple!(size_t, "pos", D, "delim");
30 }
31 
32 /// All of the delimer types parsed by Temple
33 enum Delim
34 {
35 	OpenShort,
36 	OpenShortStr,
37 	Open,
38 	OpenStr,
39 	CloseShort,
40 	Close
41 }
42 
43 enum Delims = [EnumMembers!Delim];
44 
45 /// Subset of Delims, only including opening delimers
46 enum OpenDelim  : Delim
47 {
48 	OpenShort       = Delim.OpenShort,
49 	Open            = Delim.Open,
50 	OpenShortStr    = Delim.OpenShortStr,
51 	OpenStr         = Delim.OpenStr
52 }
53 enum OpenDelims = [EnumMembers!OpenDelim];
54 
55 /// Subset of Delims, only including close delimers
56 enum CloseDelim : Delim
57 {
58 	CloseShort = Delim.CloseShort,
59 	Close      = Delim.Close
60 }
61 enum CloseDelims = [EnumMembers!CloseDelim];
62 
63 /// Maps an open delimer to its matching closing delimer
64 /// Formally, an onto function
65 enum OpenToClose =
66 [
67 	OpenDelim.OpenShort    : CloseDelim.CloseShort,
68 	OpenDelim.OpenShortStr : CloseDelim.CloseShort,
69 	OpenDelim.Open         : CloseDelim.Close,
70 	OpenDelim.OpenStr      : CloseDelim.Close
71 ];
72 
73 string toString(in Delim d)
74 {
75 	final switch(d) with(Delim)
76 	{
77 		case OpenShort:     return "%";
78 		case OpenShortStr:  return "%=";
79 		case Open:          return "<%";
80 		case OpenStr:       return "<%=";
81 		case CloseShort:    return "\n";
82 		case Close:         return "%>";
83 	}
84 }
85 
86 /// Is the delimer a shorthand delimer?
87 /// e.g., `%=`, or `%`
88 bool isShort(in Delim d)
89 {
90 	switch(d) with(Delim)
91 	{
92 		case OpenShortStr:
93 		case OpenShort   : return true;
94 		default: return false;
95 	}
96 }
97 
98 unittest {
99 	static assert(Delim.OpenShort.isShort() == true);
100 	static assert(Delim.Close.isShort() == false);
101 }
102 
103 /// Is the contents of the delimer evaluated and appended to
104 /// the template buffer? E.g. the content within `<%= %>` delims
105 bool isStr(in Delim d)
106 {
107 	switch(d) with(Delim)
108 	{
109 		case OpenShortStr:
110 		case OpenStr     : return true;
111 		default: return false;
112 	}
113 }
114 
115 unittest
116 {
117 	static assert(Delim.OpenShort.isStr() == false);
118 	static assert(Delim.OpenShortStr.isStr() == true);
119 }