annotate Implab/Parsing/EnumAlphabet.cs @ 55:c0bf853aa04f

Added initial JSON support +JSONParser +JSONWriter
author cin
date Sun, 15 Jun 2014 19:39:11 +0400
parents
children 21611344d366
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
1 using Implab;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
2 using System;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
3 using System.Collections.Generic;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
4 using System.Globalization;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
5 using System.Linq;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
6 using System.Text;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
7 using System.Threading.Tasks;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
8
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
9 namespace Implab.Parsing {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
10 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
11 /// Алфавит символами которого являются элементы перечислений.
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
12 /// </summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
13 /// <typeparam name="T">Тип перечислений</typeparam>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
14 public class EnumAlphabet<T> : AlphabetBase<T> where T : struct, IConvertible {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
15 static readonly T[] _symbols;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
16 static readonly EnumAlphabet<T> _fullAlphabet;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
17
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
18 static EnumAlphabet() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
19 if (!typeof(T).IsEnum)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
20 throw new InvalidOperationException("Invalid generic parameter, enumeration is required");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
21
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
22 if (Enum.GetUnderlyingType(typeof(T)) != typeof(Int32))
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
23 throw new InvalidOperationException("Only enums based on Int32 are supported");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
24
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
25 _symbols = ((T[])Enum.GetValues(typeof(T)))
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
26 .OrderBy(x => x.ToInt32(CultureInfo.InvariantCulture))
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
27 .ToArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
28
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
29 if (
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
30 _symbols[_symbols.Length - 1].ToInt32(CultureInfo.InvariantCulture) >= _symbols.Length
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
31 || _symbols[0].ToInt32(CultureInfo.InvariantCulture) != 0
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
32 )
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
33 throw new InvalidOperationException("The specified enumeration must be zero-based and continuously numbered");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
34
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
35 _fullAlphabet = new EnumAlphabet<T>(_symbols.Select(x => x.ToInt32(CultureInfo.InvariantCulture)).ToArray());
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
36 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
37
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
38
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
39
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
40 public static EnumAlphabet<T> FullAlphabet {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
41 get {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
42 return _fullAlphabet;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
43 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
44 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
45
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
46
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
47 public EnumAlphabet()
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
48 : base() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
49 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
50
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
51 public EnumAlphabet(int[] map)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
52 : base(map) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
53 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
54
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
56 public override int GetSymbolIndex(T symbol) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
57 return symbol.ToInt32(CultureInfo.InvariantCulture);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
58 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
59
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
60 public override IEnumerable<T> InputSymbols {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
61 get { return _symbols; }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
62 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
63
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
64 protected override int MapSize {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
65 get { return _symbols.Length; }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
66 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
67 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
68 }