##// END OF EJS Templates
Bound promise to CancellationToken...
Bound promise to CancellationToken Added new states to ExecutionSate enum. Added Safe.Guard() method to handle cleanup of the result of the promise

File last commit:

r178:d5c5db0335ee ref20160224
r209:a867536c68fc v2
Show More
Grammar.cs
99 lines | 3.3 KiB | text/x-csharp | CSharpLexer
cin
refactoring
r177 using Implab;
using System;
using System.Collections.Generic;
using System.Linq;
using Implab.Automaton;
using Implab.Automaton.RegularExpressions;
namespace Implab.Formats {
/// <summary>
/// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>.
/// </summary>
cin
working on JSON parser
r178 public abstract class Grammar<TSymbol> {
cin
refactoring
r177
protected abstract IAlphabetBuilder<TSymbol> AlphabetBuilder {
get;
}
cin
working on JSON parser
r178 protected SymbolToken UnclassifiedToken() {
return new SymbolToken(AutomatonConst.UNCLASSIFIED_INPUT);
cin
refactoring
r177 }
protected void DefineAlphabet(IEnumerable<TSymbol> alphabet) {
Safe.ArgumentNotNull(alphabet, "alphabet");
foreach (var ch in alphabet)
AlphabetBuilder.DefineSymbol(ch);
}
cin
working on JSON parser
r178 protected Token SymbolToken(TSymbol symbol) {
return Token.New(TranslateOrAdd(symbol));
cin
refactoring
r177 }
cin
working on JSON parser
r178 protected Token SymbolToken(IEnumerable<TSymbol> symbols) {
cin
refactoring
r177 Safe.ArgumentNotNull(symbols, "symbols");
cin
working on JSON parser
r178 return Token.New(TranslateOrAdd(symbols).ToArray());
cin
refactoring
r177 }
cin
working on JSON parser
r178 protected Token SymbolSetToken(params TSymbol[] set) {
cin
refactoring
r177 return SymbolToken(set);
}
int TranslateOrAdd(TSymbol ch) {
var t = AlphabetBuilder.Translate(ch);
cin
working on JSON parser
r178 if (t == AutomatonConst.UNCLASSIFIED_INPUT)
cin
refactoring
r177 t = AlphabetBuilder.DefineSymbol(ch);
return t;
}
IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) {
return symbols.Distinct().Select(TranslateOrAdd);
}
int TranslateOrDie(TSymbol ch) {
var t = AlphabetBuilder.Translate(ch);
cin
working on JSON parser
r178 if (t == AutomatonConst.UNCLASSIFIED_INPUT)
cin
refactoring
r177 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch));
return t;
}
IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) {
return symbols.Distinct().Select(TranslateOrDie);
}
cin
working on JSON parser
r178 protected Token SymbolTokenExcept(IEnumerable<TSymbol> symbols) {
cin
refactoring
r177 Safe.ArgumentNotNull(symbols, "symbols");
cin
working on JSON parser
r178 return Token.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() );
cin
refactoring
r177 }
protected abstract IndexedAlphabetBase<TSymbol> CreateAlphabet();
cin
working on JSON parser
r178 protected ScannerContext<TTag> BuildScannerContext<TTag>(Token regexp) {
cin
refactoring
r177
var dfa = new RegularDFA<TSymbol, TTag>(AlphabetBuilder);
cin
working on JSON parser
r178 var visitor = new RegularExpressionVisitor<TTag>(dfa);
regexp.Accept(visitor);
visitor.BuildDFA();
cin
refactoring
r177
if (dfa.IsFinalState(dfa.InitialState))
throw new ApplicationException("The specified language contains empty token");
var ab = CreateAlphabet();
var optimal = dfa.Optimize(ab);
return new ScannerContext<TTag>(
optimal.CreateTransitionTable(),
optimal.CreateFinalStateTable(),
optimal.CreateTagTable(),
optimal.InitialState,
ab.GetTranslationMap()
);
}
}
}