comparison Implab/Formats/Grammar.cs @ 178:d5c5db0335ee ref20160224

working on JSON parser
author cin
date Wed, 23 Mar 2016 19:51:45 +0300
parents a0ff6a0e9c44
children 6fa235c5a760
comparison
equal deleted inserted replaced
177:a0ff6a0e9c44 178:d5c5db0335ee
7 7
8 namespace Implab.Formats { 8 namespace Implab.Formats {
9 /// <summary> 9 /// <summary>
10 /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>. 10 /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>.
11 /// </summary> 11 /// </summary>
12 public abstract class Grammar<TSymbol, TTag> { 12 public abstract class Grammar<TSymbol> {
13 13
14 protected abstract IAlphabetBuilder<TSymbol> AlphabetBuilder { 14 protected abstract IAlphabetBuilder<TSymbol> AlphabetBuilder {
15 get; 15 get;
16 } 16 }
17 17
18 protected SymbolToken<TTag> UnclassifiedToken() { 18 protected SymbolToken UnclassifiedToken() {
19 return new SymbolToken<TTag>(DFAConst.UNCLASSIFIED_INPUT); 19 return new SymbolToken(AutomatonConst.UNCLASSIFIED_INPUT);
20 } 20 }
21 21
22 protected void DefineAlphabet(IEnumerable<TSymbol> alphabet) { 22 protected void DefineAlphabet(IEnumerable<TSymbol> alphabet) {
23 Safe.ArgumentNotNull(alphabet, "alphabet"); 23 Safe.ArgumentNotNull(alphabet, "alphabet");
24 24
25 foreach (var ch in alphabet) 25 foreach (var ch in alphabet)
26 AlphabetBuilder.DefineSymbol(ch); 26 AlphabetBuilder.DefineSymbol(ch);
27 } 27 }
28 28
29 protected Token<TTag> SymbolToken(TSymbol symbol) { 29 protected Token SymbolToken(TSymbol symbol) {
30 return Token<TTag>.New(TranslateOrAdd(symbol)); 30 return Token.New(TranslateOrAdd(symbol));
31 } 31 }
32 32
33 protected Token<TTag> SymbolToken(IEnumerable<TSymbol> symbols) { 33 protected Token SymbolToken(IEnumerable<TSymbol> symbols) {
34 Safe.ArgumentNotNull(symbols, "symbols"); 34 Safe.ArgumentNotNull(symbols, "symbols");
35 35
36 return Token<TTag>.New(TranslateOrAdd(symbols).ToArray()); 36 return Token.New(TranslateOrAdd(symbols).ToArray());
37 } 37 }
38 38
39 protected Token<TTag> SymbolSetToken(params TSymbol[] set) { 39 protected Token SymbolSetToken(params TSymbol[] set) {
40 return SymbolToken(set); 40 return SymbolToken(set);
41 } 41 }
42 42
43 int TranslateOrAdd(TSymbol ch) { 43 int TranslateOrAdd(TSymbol ch) {
44 var t = AlphabetBuilder.Translate(ch); 44 var t = AlphabetBuilder.Translate(ch);
45 if (t == DFAConst.UNCLASSIFIED_INPUT) 45 if (t == AutomatonConst.UNCLASSIFIED_INPUT)
46 t = AlphabetBuilder.DefineSymbol(ch); 46 t = AlphabetBuilder.DefineSymbol(ch);
47 return t; 47 return t;
48 } 48 }
49 49
50 IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) { 50 IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) {
51 return symbols.Distinct().Select(TranslateOrAdd); 51 return symbols.Distinct().Select(TranslateOrAdd);
52 } 52 }
53 53
54 int TranslateOrDie(TSymbol ch) { 54 int TranslateOrDie(TSymbol ch) {
55 var t = AlphabetBuilder.Translate(ch); 55 var t = AlphabetBuilder.Translate(ch);
56 if (t == DFAConst.UNCLASSIFIED_INPUT) 56 if (t == AutomatonConst.UNCLASSIFIED_INPUT)
57 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch)); 57 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch));
58 return t; 58 return t;
59 } 59 }
60 60
61 IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) { 61 IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) {
62 return symbols.Distinct().Select(TranslateOrDie); 62 return symbols.Distinct().Select(TranslateOrDie);
63 } 63 }
64 64
65 protected Token<TTag> SymbolTokenExcept(IEnumerable<TSymbol> symbols) { 65 protected Token SymbolTokenExcept(IEnumerable<TSymbol> symbols) {
66 Safe.ArgumentNotNull(symbols, "symbols"); 66 Safe.ArgumentNotNull(symbols, "symbols");
67 67
68 return Token<TTag>.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() ); 68 return Token.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() );
69 } 69 }
70 70
71 protected abstract IndexedAlphabetBase<TSymbol> CreateAlphabet(); 71 protected abstract IndexedAlphabetBase<TSymbol> CreateAlphabet();
72 72
73 protected ScannerContext<TTag> BuildScannerContext(Token<TTag> regexp) { 73 protected ScannerContext<TTag> BuildScannerContext<TTag>(Token regexp) {
74 74
75 var dfa = new RegularDFA<TSymbol, TTag>(AlphabetBuilder); 75 var dfa = new RegularDFA<TSymbol, TTag>(AlphabetBuilder);
76 76
77 var visitor = new RegularExpressionVisitor<TTag>(); 77 var visitor = new RegularExpressionVisitor<TTag>(dfa);
78 regexp.Accept( visitor ); 78 regexp.Accept(visitor);
79 79 visitor.BuildDFA();
80 visitor.BuildDFA(dfa);
81 80
82 if (dfa.IsFinalState(dfa.InitialState)) 81 if (dfa.IsFinalState(dfa.InitialState))
83 throw new ApplicationException("The specified language contains empty token"); 82 throw new ApplicationException("The specified language contains empty token");
84 83
85 var ab = CreateAlphabet(); 84 var ab = CreateAlphabet();