Mercurial > pub > ImplabNet
comparison Implab/Parsing/Grammar.cs @ 158:130781364799 v2
refactoring, code cleanup
author | cin |
---|---|
date | Thu, 18 Feb 2016 14:34:02 +0300 |
parents | c0bf853aa04f |
children |
comparison
equal
deleted
inserted
replaced
157:948c015a9011 | 158:130781364799 |
---|---|
9 /// <summary> | 9 /// <summary> |
10 /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>. | 10 /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>. |
11 /// </summary> | 11 /// </summary> |
12 /// <typeparam name="TGrammar"></typeparam> | 12 /// <typeparam name="TGrammar"></typeparam> |
13 public abstract class Grammar<TGrammar> where TGrammar: Grammar<TGrammar>, new() { | 13 public abstract class Grammar<TGrammar> where TGrammar: Grammar<TGrammar>, new() { |
14 Alphabet m_alphabet = new Alphabet(); | |
15 static TGrammar _instance; | 14 static TGrammar _instance; |
16 | 15 |
17 public static TGrammar Instance{ | 16 public static TGrammar Instance{ |
18 get { | 17 get { |
19 if (_instance == null) | 18 if (_instance == null) |
20 _instance = new TGrammar(); | 19 _instance = new TGrammar(); |
21 return _instance; | 20 return _instance; |
22 } | 21 } |
23 } | 22 } |
24 | 23 |
24 readonly CharAlphabet m_alphabet = new CharAlphabet(); | |
25 | |
26 public CharAlphabet Alphabet { | |
27 get { return m_alphabet; } | |
28 } | |
29 | |
25 public SymbolToken UnclassifiedToken() { | 30 public SymbolToken UnclassifiedToken() { |
26 return new SymbolToken(Alphabet.UNCLASSIFIED); | 31 return new SymbolToken(CharAlphabet.UNCLASSIFIED); |
27 } | 32 } |
28 | 33 |
29 public void DefineAlphabet(IEnumerable<char> alphabet) { | 34 public void DefineAlphabet(IEnumerable<char> alphabet) { |
30 Safe.ArgumentNotNull(alphabet, "alphabet"); | 35 Safe.ArgumentNotNull(alphabet, "alphabet"); |
31 | 36 |
50 return SymbolToken(set); | 55 return SymbolToken(set); |
51 } | 56 } |
52 | 57 |
53 int TranslateOrAdd(char ch) { | 58 int TranslateOrAdd(char ch) { |
54 var t = m_alphabet.Translate(ch); | 59 var t = m_alphabet.Translate(ch); |
55 if (t == Alphabet.UNCLASSIFIED) | 60 if (t == CharAlphabet.UNCLASSIFIED) |
56 t = m_alphabet.DefineSymbol(ch); | 61 t = m_alphabet.DefineSymbol(ch); |
57 return t; | 62 return t; |
58 } | 63 } |
59 | 64 |
60 IEnumerable<int> TranslateOrAdd(IEnumerable<char> symbols) { | 65 IEnumerable<int> TranslateOrAdd(IEnumerable<char> symbols) { |
61 return symbols.Distinct().Select(TranslateOrAdd); | 66 return symbols.Distinct().Select(TranslateOrAdd); |
62 } | 67 } |
63 | 68 |
64 int TranslateOrDie(char ch) { | 69 int TranslateOrDie(char ch) { |
65 var t = m_alphabet.Translate(ch); | 70 var t = m_alphabet.Translate(ch); |
66 if (t == Alphabet.UNCLASSIFIED) | 71 if (t == CharAlphabet.UNCLASSIFIED) |
67 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch)); | 72 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch)); |
68 return t; | 73 return t; |
69 } | 74 } |
70 | 75 |
71 IEnumerable<int> TranslateOrDie(IEnumerable<char> symbols) { | 76 IEnumerable<int> TranslateOrDie(IEnumerable<char> symbols) { |