annotate Implab/Parsing/Grammar.cs @ 158:130781364799 v2

refactoring, code cleanup
author cin
date Thu, 18 Feb 2016 14:34:02 +0300
parents c0bf853aa04f
children
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.Linq;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
5 using System.Text;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
6 using System.Threading.Tasks;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
7
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
8 namespace Implab.Parsing {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
9 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
10 /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>.
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
11 /// </summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
12 /// <typeparam name="TGrammar"></typeparam>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
13 public abstract class Grammar<TGrammar> where TGrammar: Grammar<TGrammar>, new() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
14 static TGrammar _instance;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
15
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
16 public static TGrammar Instance{
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
17 get {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
18 if (_instance == null)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
19 _instance = new TGrammar();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
20 return _instance;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
21 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
22 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
23
158
130781364799 refactoring, code cleanup
cin
parents: 55
diff changeset
24 readonly CharAlphabet m_alphabet = new CharAlphabet();
130781364799 refactoring, code cleanup
cin
parents: 55
diff changeset
25
130781364799 refactoring, code cleanup
cin
parents: 55
diff changeset
26 public CharAlphabet Alphabet {
130781364799 refactoring, code cleanup
cin
parents: 55
diff changeset
27 get { return m_alphabet; }
130781364799 refactoring, code cleanup
cin
parents: 55
diff changeset
28 }
130781364799 refactoring, code cleanup
cin
parents: 55
diff changeset
29
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
30 public SymbolToken UnclassifiedToken() {
158
130781364799 refactoring, code cleanup
cin
parents: 55
diff changeset
31 return new SymbolToken(CharAlphabet.UNCLASSIFIED);
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
32 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
33
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
34 public void DefineAlphabet(IEnumerable<char> alphabet) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
35 Safe.ArgumentNotNull(alphabet, "alphabet");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
36
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
37 foreach (var ch in alphabet)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
38 m_alphabet.DefineSymbol(ch);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
39 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
40 public Token SymbolRangeToken(char start, char end) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
41 return SymbolToken(Enumerable.Range(start, end - start + 1).Select(x => (char)x));
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
42 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
43
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
44 public Token SymbolToken(char symbol) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
45 return Token.New(TranslateOrAdd(symbol));
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
46 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
47
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
48 public Token SymbolToken(IEnumerable<char> symbols) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
49 Safe.ArgumentNotNull(symbols, "symbols");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
50
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
51 return Token.New(TranslateOrAdd(symbols).ToArray());
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
52 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
53
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
54 public Token SymbolSetToken(params char[] set) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
55 return SymbolToken(set);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
56 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
57
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
58 int TranslateOrAdd(char ch) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
59 var t = m_alphabet.Translate(ch);
158
130781364799 refactoring, code cleanup
cin
parents: 55
diff changeset
60 if (t == CharAlphabet.UNCLASSIFIED)
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
61 t = m_alphabet.DefineSymbol(ch);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
62 return t;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
63 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
64
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
65 IEnumerable<int> TranslateOrAdd(IEnumerable<char> symbols) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
66 return symbols.Distinct().Select(TranslateOrAdd);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
67 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
68
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
69 int TranslateOrDie(char ch) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
70 var t = m_alphabet.Translate(ch);
158
130781364799 refactoring, code cleanup
cin
parents: 55
diff changeset
71 if (t == CharAlphabet.UNCLASSIFIED)
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
72 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch));
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
73 return t;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
74 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
75
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
76 IEnumerable<int> TranslateOrDie(IEnumerable<char> symbols) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
77 return symbols.Distinct().Select(TranslateOrDie);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
78 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
79
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
80 public Token SymbolTokenExcept(IEnumerable<char> symbols) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
81 Safe.ArgumentNotNull(symbols, "symbols");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
82
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
83 return Token.New( Enumerable.Range(0, m_alphabet.Count).Except(TranslateOrDie(symbols)).ToArray());
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
84 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
85
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
86 protected CDFADefinition BuildDFA(Token lang) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
87 Safe.ArgumentNotNull(lang, "lang");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
88
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
89 var dfa = new CDFADefinition(m_alphabet);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
90
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
91 var builder = new DFABuilder();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
92
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
93 lang.Accept( builder );
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
94
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
95 builder.BuildDFA(dfa);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
96 if (dfa.InitialStateIsFinal)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
97 throw new ApplicationException("The specified language contains empty token");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
98
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
99 return dfa.Optimize();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
100 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
101
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
102
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
103
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
104 //protected abstract TGrammar CreateInstance();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
105 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
106
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
107
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
108 }