forked from kyordhel/GPSRCmdGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Token.cs
130 lines (109 loc) · 4.27 KB
/
Token.cs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
namespace GPSRCmdGen
{
/// <summary>
/// Represents a Token: an element of a Task
/// A token is a substring extracted from a taks prototype string after analysis,
/// which may correspond to either a literal string or a wildcard (value != null).
/// </summary>
public class Token : IMetadatable
{
#region Variables
/// <summary>
/// Stores the original substring in the taks prototype string.
/// </summary>
private string key;
/// <summary>
/// Stores the replacement object for the wildcard represented by this
/// Token in the taks prototype string.
/// </summary>
private INameable value;
/// <summary>
/// Stores he metadata contained in this Token, fetched from both,
/// taks prototype string (metadata stored in grammar) and the
/// metadata asociated to the Token's value.
/// </summary>
private List<string> metadata;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GPSRCmdGen.Token"/> class.
/// </summary>
/// <param name="key">The original substring in the taks prototype string.</param>
public Token (string key) : this(key, null, null){}
/// <summary>
/// Initializes a new instance of the <see cref="GPSRCmdGen.Token"/> class.
/// </summary>
/// <param name="key">The original substring in the taks prototype string.</param>
/// <param name="value">The replacement object for the wildcard represented by this Token.</param>
public Token (string key, INameable value) : this(key, value, null){}
/// <summary>
/// Initializes a new instance of the <see cref="GPSRCmdGen.Token"/> class.
/// </summary>
/// <param name="key">The original substring in the taks prototype string.</param>
/// <param name="value">The replacement object for the wildcard represented by this Token.</param>
/// <param name="metadata">Additional metadata to add (e.g. from the grammar
/// or the taks prototype string).</param>
public Token (string key, INameable value, IEnumerable<string> metadata)
{
this.key = key;
this.value = value;
this.metadata = new List<string>();
IMetadatable imvalue = value as IMetadatable;
if(imvalue != null)
this.metadata.AddRange (imvalue.Metadata);
if(metadata != null)
this.metadata.AddRange(metadata);
}
#endregion
#region Properties
/// <summary>
/// Gets the original substring in the taks prototype string.
/// </summary>
public string Key { get { return this.key; } }
/// <summary>
/// Gets the replacement object for the wildcard represented by this
/// Token in the taks prototype string, if the token is a wildcard.
/// If the Token is a literal string, it returns null.
/// </summary>
public INameable Value { get { return value; } }
/// <summary>
/// Gets a value indicating if the Token is a wildcard
/// </summary>
public bool IsWildcard{ get { return this.value != null; } }
/// <summary>
/// Gets the INameable.Name. of the Token.
/// When Value property is not null, it returns the name of the Toklen's value.
/// When Value property is null, it returns the Toklen's key,
/// </summary>
public string Name { get { return value == null ? this.key : this.value.Name; } }
/// <summary>
/// Gets the metadata contained in this Token, fetched from both,
/// taks prototype string (metadata stored in grammar) and the
/// metadata asociated to the Token's value.
/// </summary>
/// <value>The metadata.</value>
public List<string> Metadata { get { return this.metadata; } }
/// <summary>
/// Gets the metadata contained in this Token, fetched from both,
/// taks prototype string (metadata stored in grammar) and the
/// metadata asociated to the Token's value.
/// </summary>
/// <value>The metadata.</value>
string[] IMetadatable.Metadata { get { return this.metadata.ToArray(); } }
#endregion
#region Methods
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="GPSRCmdGen.Token"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="GPSRCmdGen.Token"/>.</returns>
public override string ToString()
{
if(value == null)
return this.key;
return String.Format ("{0} => {1}", this.key, this.value.Name);
}
#endregion
}
}