Grammar.cs
73 lines
| 2.4 KiB
| text/x-csharp
|
CSharpLexer
cin
|
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
|
r178 | public abstract class Grammar<TSymbol> { | ||
cin
|
r177 | |||
protected abstract IAlphabetBuilder<TSymbol> AlphabetBuilder { | ||||
get; | ||||
} | ||||
cin
|
r178 | protected SymbolToken UnclassifiedToken() { | ||
cin
|
r236 | return new SymbolToken(AutomatonConst.UnclassifiedInput); | ||
cin
|
r177 | } | ||
protected void DefineAlphabet(IEnumerable<TSymbol> alphabet) { | ||||
Safe.ArgumentNotNull(alphabet, "alphabet"); | ||||
foreach (var ch in alphabet) | ||||
AlphabetBuilder.DefineSymbol(ch); | ||||
} | ||||
cin
|
r178 | protected Token SymbolToken(TSymbol symbol) { | ||
return Token.New(TranslateOrAdd(symbol)); | ||||
cin
|
r177 | } | ||
cin
|
r178 | protected Token SymbolToken(IEnumerable<TSymbol> symbols) { | ||
cin
|
r177 | Safe.ArgumentNotNull(symbols, "symbols"); | ||
cin
|
r178 | return Token.New(TranslateOrAdd(symbols).ToArray()); | ||
cin
|
r177 | } | ||
cin
|
r178 | protected Token SymbolSetToken(params TSymbol[] set) { | ||
cin
|
r177 | return SymbolToken(set); | ||
} | ||||
int TranslateOrAdd(TSymbol ch) { | ||||
var t = AlphabetBuilder.Translate(ch); | ||||
cin
|
r236 | if (t == AutomatonConst.UnclassifiedInput) | ||
cin
|
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
|
r236 | if (t == AutomatonConst.UnclassifiedInput) | ||
cin
|
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
|
r178 | protected Token SymbolTokenExcept(IEnumerable<TSymbol> symbols) { | ||
cin
|
r177 | Safe.ArgumentNotNull(symbols, "symbols"); | ||
cin
|
r178 | return Token.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() ); | ||
cin
|
r177 | } | ||
} | ||||
} | ||||