aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sdlang/symbol.d
blob: ebb2b934203036188eff08c778eb0f37f05563e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// SDLang-D
// Written in the D programming language.

module sdlang.symbol;

import std.algorithm;

static immutable validSymbolNames = [
	"Error",
	"EOF",
	"EOL",

	":",
	"=",
	"{",
	"}",

	"Ident",
	"Value",
];

/// Use this to create a Symbol. Ex: symbol!"Value" or symbol!"="
/// Invalid names (such as symbol!"FooBar") are rejected at compile-time.
template symbol(string name)
{
	static assert(validSymbolNames.find(name), "Invalid Symbol: '"~name~"'");
	immutable symbol = _symbol(name);
}

private Symbol _symbol(string name)
{
	return Symbol(name);
}

/// Symbol is essentially the "type" of a Token.
/// Token is like an instance of a Symbol.
///
/// This only represents terminals. Nonterminal tokens aren't
/// constructed since the AST is built directly during parsing.
///
/// You can't create a Symbol directly. Instead, use the `symbol`
/// template.
struct Symbol
{
	private string _name;
	@property string name()
	{
		return _name;
	}
	
	@disable this();
	private this(string name)
	{
		this._name = name;
	}

	string toString()
	{
		return _name;
	}
}