1 module squelch.common;
2 
3 import std.sumtype;
4 
5 // Dbt interpolation:
6 
7 struct QuotingContext
8 {
9 	char quote; // 0 if no quoting
10 	bool raw;
11 	bool triple;
12 }
13 
14 /// Fragment of DbtString containing a DbtExpression
15 struct DbtExpression
16 {
17 	string expr;
18 	QuotingContext quoting;
19 }
20 
21 alias DbtStringElem = SumType!(
22 	dchar, // literal character
23 	DbtExpression, // {{ ... }}
24 );
25 
26 alias DbtString = DbtStringElem[];
27 
28 string tryToString(const DbtString s)
29 {
30 	string result = "";
31 	foreach (e; s)
32 		if (!e.match!(
33 			(dchar c) { result ~= c; return true; },
34 			(DbtExpression _) { return false; },
35 		))
36 			return null;
37 	return result;
38 }
39 
40 // Tokens:
41 
42 struct TokenWhiteSpace
43 {
44 	string text;
45 }
46 
47 struct TokenComment
48 {
49 	string text;
50 }
51 
52 struct TokenKeyword
53 {
54 	string kind; // Group synonyms and lexically similar keywords
55 	string text; // Actual text
56 
57 	this(string kind, string text = null)
58 	{
59 		if (!text) text = kind;
60 		this.kind = kind;
61 		this.text = text;
62 	}
63 }
64 
65 struct TokenIdentifier
66 {
67 	DbtString text;
68 }
69 
70 struct TokenNamedParameter
71 {
72 	string text;
73 }
74 
75 struct TokenOperator
76 {
77 	string text;
78 }
79 
80 /// Distinguish < / > from the binary operator.
81 struct TokenAngleBracket
82 {
83 	string text;
84 }
85 
86 struct TokenString
87 {
88 	DbtString text;
89 	bool bytes;
90 }
91 
92 struct TokenNumber
93 {
94 	string text;
95 }
96 
97 struct TokenDbtStatement
98 {
99 	string text;
100 	string kind;
101 }
102 
103 // struct TokenDbtExpression
104 // {
105 // 	string text;
106 // }
107 
108 struct TokenDbtComment
109 {
110 	string text;
111 }
112 
113 alias Token = SumType!(
114 	TokenWhiteSpace,
115 	TokenComment,
116 	TokenKeyword,
117 	TokenIdentifier,
118 	TokenNamedParameter,
119 	TokenOperator,
120 	TokenAngleBracket,
121 	TokenString,
122 	TokenNumber,
123 	TokenDbtStatement,
124 	// TokenDbtExpression,
125 	TokenDbtComment,
126 );