-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaValue.cs
More file actions
367 lines (308 loc) · 11.4 KB
/
LuaValue.cs
File metadata and controls
367 lines (308 loc) · 11.4 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
public class LuaConstantsCounter
{
private HashSet<String> StringsEncountered; private UInt32 NumStrings = 0;
private HashSet<Int64> IntegersEncountered; private UInt32 NumIntegers = 0;
private HashSet<Double> RealsEncountered; private UInt32 NumReals = 0;
// We don't bother counting booleans or nils at this point
public UInt32 TotalConstants { get { return NumStrings + NumIntegers + NumReals; } }
public void Add ( LuaNil Val ) { }
public void Add ( LuaBoolean Val ) { }
public void Add ( LuaInteger Val ) { NumIntegers += IntegersEncountered.Add( Val.Value ) ? 1U : 0U; }
public void Add ( LuaReal Val ) { NumReals += RealsEncountered.Add( Val.Value ) ? 1U : 0U; }
public void Add ( LuaString Val ) { NumStrings += StringsEncountered.Add( Val.Value ) ? 1U : 0U; }
//This is a travesty. Value of 2 chosen based on an average of the number of aliases each character has.
public void Add ( LuaMixedTable Val ) { NumStrings += 2U; }
public LuaConstantsCounter ( )
{
StringsEncountered = new HashSet<String>();
IntegersEncountered = new HashSet<Int64>();
RealsEncountered = new HashSet<Double>();
}
}
public class LuaKV
{
private readonly LuaValue _Key, _Value;
public LuaValue Key { get { return _Key; } }
public LuaValue Value { get { return _Value; } }
public override String ToString ( )
{
return String.Format( "{0} = {1};", Key.ToStringAsKey(), Value.ToStringAsValue() );
}
public LuaKV ( LuaValue Key, LuaValue Value ) { _Key = Key; _Value = Value; }
public LuaKV ( String Key, LuaValue Value ) :this( new LuaString( Key, LuaStringStyle.Identifier ), Value ) { }
}
public abstract class LuaValue
{
public abstract String ToStringAsValue ( );
public virtual String ToStringAsKey ( Int32 padding )
{
String V = AsKey();
if ( padding < 0 )
V = V.PadRight( Math.Abs( padding ) );
else if ( padding > 0 )
V = V.PadLeft( padding );
return String.Concat( '[', V, ']' );
}
public String ToStringAsKey ( ) { return ToStringAsKey( 0 ); }
protected virtual String AsKey ( )
{
return ToStringAsValue();
}
public override String ToString ( )
{
return String.Concat( this.GetType().Name, ':', this.ToStringAsValue() );
}
}
public class LuaNil : LuaValue
{
public override String ToStringAsValue ( ) { return "nil"; }
public LuaNil ( ) { }
}
public class LuaBoolean : LuaValue
{
private readonly Boolean _Value;
public Boolean Value { get { return _Value; } }
public override String ToStringAsValue ( )
{
return _Value ? "true" : "false";
}
public LuaBoolean ( Boolean v )
{
_Value = v;
}
}
// Does nothing, but we may add stuff here later
public abstract class LuaNumber : LuaValue
{
}
public enum LuaIntegerStyle {
Decimal, Hexadecimal, CodePoint }
public class LuaInteger : LuaNumber
{
private readonly Int64 _Value;
private readonly LuaIntegerStyle Style = LuaIntegerStyle.Decimal;
public Int64 Value { get { return _Value; } }
public override String ToStringAsValue ( )
{
switch ( Style )
{
case LuaIntegerStyle.Decimal :
return _Value.ToString("D");
case LuaIntegerStyle.Hexadecimal :
return "0x" + _Value.ToString("X");
case LuaIntegerStyle.CodePoint :
return "0x" + _Value.ToString("X4");
}
throw new NotSupportedException( String.Format( "Unsupported Style value '{0}'", Style ) );
}
protected override String AsKey ( )
{
String K = ToStringAsValue();
if ( Style == LuaIntegerStyle.CodePoint )
K = K.PadLeft(8);
return K;
}
public LuaInteger ( Int64 V ) { _Value = V; }
public LuaInteger ( Int64 V, LuaIntegerStyle S ) :this( V ) { Style = S; }
}
public class LuaReal : LuaNumber
{
private readonly Double _Value;
public Double Value { get { return _Value; } }
public override String ToStringAsValue ( )
{
return _Value.ToString("R");
}
public LuaReal ( Double V ) { _Value = V; }
}
public class LuaRational : LuaNumber
{
private readonly Int64 Numerator, Denominator;
public Double Value { get { return (Double)Numerator / (Double)Denominator; } }
public override String ToStringAsValue ( )
{
return String.Format( "{0:F1}/{1:F1}", (Double)Numerator, (Double)Denominator );
}
public LuaRational ( Int64 Num, Int64 Den )
{
Numerator = Num;
Denominator = Den;
}
}
// TODO: Needs a bit of work on validation and such.
// Identifier falls back to Long when it can't be used, Long falls back to DoubleQuote when it can't be used
public enum LuaStringStyle {
DoubleQuote, /*SingleQuote,*/ Long, Identifier }
public class LuaString : LuaValue
{
private static readonly Dictionary<Char,String> Escapes;
private static readonly Regex PrintableTest, IdentifierTest, LongStringProblemPattern;
private readonly LuaStringStyle Style = LuaStringStyle.DoubleQuote;
private readonly String _Value;
public String Value { get { return _Value; } }
public override String ToStringAsValue ( )
{
switch ( Style )
{
case LuaStringStyle.DoubleQuote :
return FormatDoubleQuote( Value );
case LuaStringStyle.Long :
case LuaStringStyle.Identifier :
return FormatLong( Value );
default :
throw new NotSupportedException( String.Format( "Unsupported Style value '{0}'", Style ) );
}
}
// TODO
public override String ToStringAsKey ( Int32 Padding )
{
switch ( Style )
{
case LuaStringStyle.Identifier :
return Value;
case LuaStringStyle.Long :
return String.Concat( "[ ", ToStringAsValue(), " ]" );
case LuaStringStyle.DoubleQuote :
return String.Concat( "[", ToStringAsValue(), "]" );
default :
throw new NotSupportedException( String.Format( "Unsupported Style value '{0}'", Style ) );
}
}
private static String FormatDoubleQuote ( String S )
{
StringBuilder Builder = new StringBuilder( );
Byte[] Bytes = Encoding.UTF8.GetBytes( S );
for ( UInt16 Idx = 0; Idx < Bytes.Length; Idx++ )
{
Byte B = Bytes[ Idx ];
String Out = null;
if ( Escapes.TryGetValue( (Char)B, out Out ) )
Builder.Append( Out );
else if ( (Byte)' ' <= B && B <= (Byte)'~' ) // Is within ASCII, not otherwise escaped
Builder.Append( (Char)B );
else if ( Idx < Bytes.Length-1 && '0' <= Bytes[Idx+1] && Bytes[Idx+1] <= '9' ) // Non-printable, a digit follows this one
Builder.Append( "\\" + B.ToString( "D3" ) );
else
Builder.Append( "\\" + B.ToString( "D" ) );
}
return String.Concat( "\"", Builder.ToString(), "\"" );
}
private static String FormatLong ( String S )
{
if ( !IsPrintableAscii(S) )
throw new ArgumentException();
MatchCollection Problems = LongStringProblemPattern.Matches( S );
Int32[] NumEqs = Problems.Cast<Match>()
.Select<Match,Int32>( (Match) => {return Match.Length-2;} )
.Distinct()
.ToArray();
Array.Sort( NumEqs );
Int32 Counter = 0;
while ( Counter < NumEqs.Length && Counter == NumEqs[ Counter ] )
Counter++;
return String.Format( "[{0}[{1}]{0}]", new String( '=', Counter ), S );
}
// "Printable" does include the space character, in this context
private static Boolean IsPrintableAscii ( String Input )
{
return PrintableTest.IsMatch( Input );
}
// Does it qualify for Lua's identifier rules?
private static Boolean CanBeIdentifier ( String Input )
{
return IdentifierTest.IsMatch( Input );
}
static LuaString ( )
{
PrintableTest = new Regex( @"^[\x20-\x7E]*$" );
IdentifierTest = new Regex( @"^[_a-zA-Z][_a-zA-Z0-9]*$" );
LongStringProblemPattern = new Regex( @"\]=*\]" );
// The only ones supported are those used by Lua 5.1
Escapes = new Dictionary<Char,String> {
{ '\u0007', @"\a" },
{ '\b', @"\b" },
{ '\f', @"\f" },
{ '\n', @"\n" },
{ '\r', @"\r" },
{ '\t', @"\t" },
{ '\v', @"\v" },
{ '\\', @"\\" },
{ '"', "\\\"" } // Done differently because NP++ doesn't understand @-strings
};
}
public LuaString ( String Value ) { _Value = Value; }
public LuaString ( String Value, LuaStringStyle Style )
:this( Value )
{
if ( Style == LuaStringStyle.Identifier && !CanBeIdentifier( Value ) )
throw new ArgumentException( String.Format( "String '{0}' is of wrong format to be an identifier.", Value ) );
if ( Style == LuaStringStyle.Long && !IsPrintableAscii( Value ) )
throw new ArgumentException( String.Format( "String '{0}' is of wrong format to be a long string.", Value ) );
this.Style = Style;
}
}
public class LuaArray : LuaValue
{
private readonly List<LuaValue> _Values;
public IList<LuaValue> Values { get { return _Values.AsReadOnly(); } }
public override String ToStringAsValue ( )
{
return "{ " + String.Join( ", ", _Values.Select<LuaValue,String>( v => v.ToStringAsValue() ).ToArray() ) + " }";
}
public LuaArray ( params LuaValue[] vs )
{
_Values = new List<LuaValue>( vs );
}
public LuaArray ( IEnumerable<LuaValue> vs )
{
_Values = new List<LuaValue>( vs );
}
}
public class LuaUString : LuaValue
{
private readonly LuaArray _Array;
public IList<LuaValue> Values { get { return _Array.Values; } }
public override String ToStringAsValue ( )
{
return "U" + _Array.ToStringAsValue();
}
public LuaUString ( String s )
{
List<LuaValue> Points = new List<LuaValue>();
for ( Int32 idx = 0; idx < s.Length ; idx++ )
{
Int32 CP = Char.ConvertToUtf32( s, idx );
Points.Add( new LuaInteger( CP, LuaIntegerStyle.Hexadecimal ) );
if ( Char.IsSurrogatePair( s, idx ) )
idx++;
}
_Array = new LuaArray( Points );
}
}
// Specialty type, used only for constructing Name_Alias entries for now
// TODO: This whole deal with Name_Alias should be redesigned
public class LuaMixedTable : LuaValue
{
private readonly List<String> Items;
public override String ToStringAsValue ( )
{
return "{ " + String.Join( " ", Items.ToArray() ) + " }";
}
public void Add ( LuaKV KV )
{
Items.Add( KV.ToString() );
}
public void Add ( LuaValue Val )
{
Items.Add( Val.ToStringAsValue() + "," );
}
public LuaMixedTable ( )
{
Items = new List<String>();
}
}