##// END OF EJS Templates
Working on text scanner
Working on text scanner

File last commit:

r172:92d5278d1b10 ref20160224
r173:ecfece82ca11 ref20160224
Show More
Grammar.cs
89 lines | 3.0 KiB | text/x-csharp | CSharpLexer
cin
DFA refactoring
r162 using Implab;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Implab.Automaton.RegularExpressions {
/// <summary>
/// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>.
/// </summary>
cin
DFA refactoring
r165 public abstract class Grammar<TSymbol, TTag> {
cin
DFA refactoring
r162
cin
DFA refactoring
r165 protected abstract IAlphabetBuilder<TSymbol> AlphabetBuilder {
cin
JSON moved to Formats namespace...
r163 get;
cin
DFA refactoring
r162 }
cin
DFA refactoring
r165 protected SymbolToken<TTag> UnclassifiedToken() {
cin
JSON moved to Formats namespace...
r163 return new SymbolToken<TTag>(DFAConst.UNCLASSIFIED_INPUT);
cin
DFA refactoring
r162 }
cin
DFA refactoring
r165 protected void DefineAlphabet(IEnumerable<TSymbol> alphabet) {
cin
DFA refactoring
r162 Safe.ArgumentNotNull(alphabet, "alphabet");
foreach (var ch in alphabet)
cin
DFA refactoring
r165 AlphabetBuilder.DefineSymbol(ch);
cin
DFA refactoring
r162 }
cin
JSON moved to Formats namespace...
r163
cin
DFA refactoring
r165 protected Token<TTag> SymbolToken(TSymbol symbol) {
cin
JSON moved to Formats namespace...
r163 return Token<TTag>.New(TranslateOrAdd(symbol));
cin
DFA refactoring
r162 }
cin
DFA refactoring
r165 protected Token<TTag> SymbolToken(IEnumerable<TSymbol> symbols) {
cin
JSON moved to Formats namespace...
r163 Safe.ArgumentNotNull(symbols, "symbols");
return Token<TTag>.New(TranslateOrAdd(symbols).ToArray());
cin
DFA refactoring
r162 }
cin
DFA refactoring
r165 protected Token<TTag> SymbolSetToken(params TSymbol[] set) {
cin
DFA refactoring
r162 return SymbolToken(set);
}
cin
JSON moved to Formats namespace...
r163 int TranslateOrAdd(TSymbol ch) {
cin
DFA refactoring
r165 var t = AlphabetBuilder.Translate(ch);
cin
JSON moved to Formats namespace...
r163 if (t == DFAConst.UNCLASSIFIED_INPUT)
cin
DFA refactoring
r165 t = AlphabetBuilder.DefineSymbol(ch);
cin
DFA refactoring
r162 return t;
}
cin
JSON moved to Formats namespace...
r163 IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) {
cin
DFA refactoring
r162 return symbols.Distinct().Select(TranslateOrAdd);
}
cin
JSON moved to Formats namespace...
r163 int TranslateOrDie(TSymbol ch) {
cin
DFA refactoring
r165 var t = AlphabetBuilder.Translate(ch);
cin
JSON moved to Formats namespace...
r163 if (t == DFAConst.UNCLASSIFIED_INPUT)
cin
DFA refactoring
r162 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch));
return t;
}
cin
JSON moved to Formats namespace...
r163 IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) {
cin
DFA refactoring
r162 return symbols.Distinct().Select(TranslateOrDie);
}
cin
DFA refactoring
r165 protected Token<TTag> SymbolTokenExcept(IEnumerable<TSymbol> symbols) {
cin
DFA refactoring
r162 Safe.ArgumentNotNull(symbols, "symbols");
cin
DFA refactoring
r165 return Token<TTag>.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() );
cin
DFA refactoring
r162 }
cin
Working on text scanner
r172 protected abstract IAlphabetBuilder<TSymbol> CreateAlphabet();
cin
Almost complete DFA refactoring
r164
cin
Working on text scanner
r172 protected RegularDFA<TSymbol, TTag> BuildDFA(Token<TTag> regexp) {
var dfa = new RegularDFA<TSymbol, TTag>(AlphabetBuilder);
cin
DFA refactoring
r162
cin
Working on text scanner
r172 var visitor = new RegularExpressionVisitor<TTag>();
regexp.Accept( visitor );
cin
DFA refactoring
r162
cin
Working on text scanner
r172 visitor.BuildDFA(dfa);
cin
DFA refactoring
r165
if (dfa.IsFinalState(dfa.InitialState))
cin
DFA refactoring
r162 throw new ApplicationException("The specified language contains empty token");
cin
Working on text scanner
r172 return dfa.Optimize(CreateAlphabet());
cin
DFA refactoring
r162 }
}
}