@@ -0,0 +1,33 | |||
|
1 | using Implab; | |
|
2 | ||
|
3 | namespace Implab.Automaton.RegularExpressions { | |
|
4 | /// <summary> | |
|
5 | /// Конечный символ расширенного регулярного выражения, при построении ДКА | |
|
6 | /// используется для определения конечных состояний. | |
|
7 | /// </summary> | |
|
8 | public class EndToken<TTag>: Token { | |
|
9 | ||
|
10 | TTag m_tag; | |
|
11 | ||
|
12 | public EndToken(TTag tag) { | |
|
13 | m_tag = tag; | |
|
14 | } | |
|
15 | ||
|
16 | public EndToken() | |
|
17 | : this(default(TTag)) { | |
|
18 | } | |
|
19 | ||
|
20 | public TTag Tag { | |
|
21 | get { return m_tag; } | |
|
22 | } | |
|
23 | ||
|
24 | public override void Accept(IVisitor visitor) { | |
|
25 | Safe.ArgumentOfType(visitor, typeof(IVisitor<TTag>), "visitor"); | |
|
26 | Safe.ArgumentNotNull(visitor, "visitor"); | |
|
27 | ((IVisitor<TTag>)visitor).Visit(this); | |
|
28 | } | |
|
29 | public override string ToString() { | |
|
30 | return "#"; | |
|
31 | } | |
|
32 | } | |
|
33 | } |
@@ -0,0 +1,8 | |||
|
1 | namespace Implab.Automaton.RegularExpressions { | |
|
2 | /// <summary> | |
|
3 | /// Интерфейс обходчика синтаксического дерева регулярного выражения | |
|
4 | /// </summary> | |
|
5 | public interface IVisitor<T> : IVisitor { | |
|
6 | void Visit(EndToken<T> token); | |
|
7 | } | |
|
8 | } |
@@ -0,0 +1,100 | |||
|
1 | using Implab; | |
|
2 | using System; | |
|
3 | using System.Collections.Generic; | |
|
4 | using System.Linq; | |
|
5 | using Implab.Automaton; | |
|
6 | using Implab.Automaton.RegularExpressions; | |
|
7 | ||
|
8 | namespace Implab.Formats { | |
|
9 | /// <summary> | |
|
10 | /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>. | |
|
11 | /// </summary> | |
|
12 | public abstract class Grammar<TSymbol, TTag> { | |
|
13 | ||
|
14 | protected abstract IAlphabetBuilder<TSymbol> AlphabetBuilder { | |
|
15 | get; | |
|
16 | } | |
|
17 | ||
|
18 | protected SymbolToken<TTag> UnclassifiedToken() { | |
|
19 | return new SymbolToken<TTag>(DFAConst.UNCLASSIFIED_INPUT); | |
|
20 | } | |
|
21 | ||
|
22 | protected void DefineAlphabet(IEnumerable<TSymbol> alphabet) { | |
|
23 | Safe.ArgumentNotNull(alphabet, "alphabet"); | |
|
24 | ||
|
25 | foreach (var ch in alphabet) | |
|
26 | AlphabetBuilder.DefineSymbol(ch); | |
|
27 | } | |
|
28 | ||
|
29 | protected Token<TTag> SymbolToken(TSymbol symbol) { | |
|
30 | return Token<TTag>.New(TranslateOrAdd(symbol)); | |
|
31 | } | |
|
32 | ||
|
33 | protected Token<TTag> SymbolToken(IEnumerable<TSymbol> symbols) { | |
|
34 | Safe.ArgumentNotNull(symbols, "symbols"); | |
|
35 | ||
|
36 | return Token<TTag>.New(TranslateOrAdd(symbols).ToArray()); | |
|
37 | } | |
|
38 | ||
|
39 | protected Token<TTag> SymbolSetToken(params TSymbol[] set) { | |
|
40 | return SymbolToken(set); | |
|
41 | } | |
|
42 | ||
|
43 | int TranslateOrAdd(TSymbol ch) { | |
|
44 | var t = AlphabetBuilder.Translate(ch); | |
|
45 | if (t == DFAConst.UNCLASSIFIED_INPUT) | |
|
46 | t = AlphabetBuilder.DefineSymbol(ch); | |
|
47 | return t; | |
|
48 | } | |
|
49 | ||
|
50 | IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) { | |
|
51 | return symbols.Distinct().Select(TranslateOrAdd); | |
|
52 | } | |
|
53 | ||
|
54 | int TranslateOrDie(TSymbol ch) { | |
|
55 | var t = AlphabetBuilder.Translate(ch); | |
|
56 | if (t == DFAConst.UNCLASSIFIED_INPUT) | |
|
57 | throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch)); | |
|
58 | return t; | |
|
59 | } | |
|
60 | ||
|
61 | IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) { | |
|
62 | return symbols.Distinct().Select(TranslateOrDie); | |
|
63 | } | |
|
64 | ||
|
65 | protected Token<TTag> SymbolTokenExcept(IEnumerable<TSymbol> symbols) { | |
|
66 | Safe.ArgumentNotNull(symbols, "symbols"); | |
|
67 | ||
|
68 | return Token<TTag>.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() ); | |
|
69 | } | |
|
70 | ||
|
71 | protected abstract IndexedAlphabetBase<TSymbol> CreateAlphabet(); | |
|
72 | ||
|
73 | protected ScannerContext<TTag> BuildScannerContext(Token<TTag> regexp) { | |
|
74 | ||
|
75 | var dfa = new RegularDFA<TSymbol, TTag>(AlphabetBuilder); | |
|
76 | ||
|
77 | var visitor = new RegularExpressionVisitor<TTag>(); | |
|
78 | regexp.Accept( visitor ); | |
|
79 | ||
|
80 | visitor.BuildDFA(dfa); | |
|
81 | ||
|
82 | if (dfa.IsFinalState(dfa.InitialState)) | |
|
83 | throw new ApplicationException("The specified language contains empty token"); | |
|
84 | ||
|
85 | var ab = CreateAlphabet(); | |
|
86 | var optimal = dfa.Optimize(ab); | |
|
87 | ||
|
88 | return new ScannerContext<TTag>( | |
|
89 | optimal.CreateTransitionTable(), | |
|
90 | optimal.CreateFinalStateTable(), | |
|
91 | optimal.CreateTagTable(), | |
|
92 | optimal.InitialState, | |
|
93 | ab.GetTranslationMap() | |
|
94 | ); | |
|
95 | } | |
|
96 | ||
|
97 | } | |
|
98 | ||
|
99 | ||
|
100 | } |
@@ -1,17 +1,17 | |||
|
1 | 1 | using System; |
|
2 | 2 | |
|
3 | 3 | namespace Implab.Automaton.RegularExpressions { |
|
4 |
public class AltToken |
|
|
5 |
public AltToken(Token |
|
|
4 | public class AltToken: BinaryToken { | |
|
5 | public AltToken(Token left, Token right) | |
|
6 | 6 | : base(left, right) { |
|
7 | 7 | } |
|
8 | 8 | |
|
9 |
public override void Accept(IVisitor |
|
|
9 | public override void Accept(IVisitor visitor) { | |
|
10 | 10 | Safe.ArgumentNotNull(visitor, "visitor"); |
|
11 | 11 | visitor.Visit(this); |
|
12 | 12 | } |
|
13 | 13 | public override string ToString() { |
|
14 |
return String.Format(Right is BinaryToken |
|
|
14 | return String.Format(Right is BinaryToken ? "{0}|({1})" : "{0}|{1}", Left, Right); | |
|
15 | 15 | } |
|
16 | 16 | } |
|
17 | 17 | } |
@@ -1,21 +1,21 | |||
|
1 | 1 | using Implab; |
|
2 | 2 | |
|
3 | 3 | namespace Implab.Automaton.RegularExpressions { |
|
4 |
public abstract class BinaryToken |
|
|
5 |
readonly Token |
|
|
6 |
readonly Token |
|
|
4 | public abstract class BinaryToken: Token { | |
|
5 | readonly Token m_left; | |
|
6 | readonly Token m_right; | |
|
7 | 7 | |
|
8 |
public Token |
|
|
8 | public Token Left { | |
|
9 | 9 | get { return m_left; } |
|
10 | 10 | } |
|
11 | 11 | |
|
12 |
public Token |
|
|
12 | public Token Right { | |
|
13 | 13 | get { return m_right; } |
|
14 | 14 | } |
|
15 | 15 | |
|
16 |
protected BinaryToken(Token |
|
|
16 | protected BinaryToken(Token left, Token right) { | |
|
17 | 17 | Safe.ArgumentNotNull(m_left = left, "left"); |
|
18 | 18 | Safe.ArgumentNotNull(m_right = right, "right"); |
|
19 | 19 | } |
|
20 | 20 | } |
|
21 | 21 | } |
@@ -1,22 +1,22 | |||
|
1 | 1 | using System; |
|
2 | 2 | |
|
3 | 3 | namespace Implab.Automaton.RegularExpressions { |
|
4 |
public class CatToken |
|
|
5 |
public CatToken(Token |
|
|
4 | public class CatToken : BinaryToken { | |
|
5 | public CatToken(Token left, Token right) | |
|
6 | 6 | : base(left, right) { |
|
7 | 7 | } |
|
8 | 8 | |
|
9 |
public override void Accept(IVisitor |
|
|
9 | public override void Accept(IVisitor visitor) { | |
|
10 | 10 | Safe.ArgumentNotNull(visitor, "visitor"); |
|
11 | 11 | visitor.Visit(this); |
|
12 | 12 | } |
|
13 | 13 | |
|
14 | 14 | public override string ToString() { |
|
15 | 15 | return String.Format("{0}{1}", FormatToken(Left), FormatToken(Right)); |
|
16 | 16 | } |
|
17 | 17 | |
|
18 |
static string FormatToken(Token |
|
|
19 |
return String.Format(token is AltToken |
|
|
18 | static string FormatToken(Token token) { | |
|
19 | return String.Format(token is AltToken ? "({0})" : "{0}", token); | |
|
20 | 20 | } |
|
21 | 21 | } |
|
22 | 22 | } |
@@ -1,13 +1,13 | |||
|
1 | 1 | using Implab; |
|
2 | 2 | |
|
3 | 3 | namespace Implab.Automaton.RegularExpressions { |
|
4 |
public class EmptyToken |
|
|
5 |
public override void Accept(IVisitor |
|
|
4 | public class EmptyToken: Token { | |
|
5 | public override void Accept(IVisitor visitor) { | |
|
6 | 6 | Safe.ArgumentNotNull(visitor, "visitor"); |
|
7 | 7 | visitor.Visit(this); |
|
8 | 8 | } |
|
9 | 9 | public override string ToString() { |
|
10 | 10 | return "$"; |
|
11 | 11 | } |
|
12 | 12 | } |
|
13 | 13 | } |
@@ -1,32 +1,18 | |||
|
1 | 1 | using Implab; |
|
2 | 2 | |
|
3 | 3 | namespace Implab.Automaton.RegularExpressions { |
|
4 | 4 | /// <summary> |
|
5 | 5 | /// Конечный символ расширенного регулярного выражения, при построении ДКА |
|
6 | 6 | /// используется для определения конечных состояний. |
|
7 | 7 | /// </summary> |
|
8 |
public class EndToken |
|
|
9 | ||
|
10 | TTag m_tag; | |
|
11 | ||
|
12 | public EndToken(TTag tag) { | |
|
13 | m_tag = tag; | |
|
14 | } | |
|
8 | public class EndToken: Token { | |
|
15 | 9 | |
|
16 | public EndToken() | |
|
17 | : this(default(TTag)) { | |
|
18 | } | |
|
19 | ||
|
20 | public TTag Tag { | |
|
21 | get { return m_tag; } | |
|
22 | } | |
|
23 | ||
|
24 | public override void Accept(IVisitor<TTag> visitor) { | |
|
10 | public override void Accept(IVisitor visitor) { | |
|
25 | 11 | Safe.ArgumentNotNull(visitor, "visitor"); |
|
26 | 12 | visitor.Visit(this); |
|
27 | 13 | } |
|
28 | 14 | public override string ToString() { |
|
29 | 15 | return "#"; |
|
30 | 16 | } |
|
31 | 17 | } |
|
32 | 18 | } |
@@ -1,8 +1,7 | |||
|
1 | using System; | |
|
2 | ||
|
1 | ||
|
3 | 2 | namespace Implab.Automaton.RegularExpressions { |
|
4 | 3 | public interface ITaggedDFABuilder<TTag> : IDFATableBuilder { |
|
5 | 4 | void SetStateTag(int s, TTag[] tags); |
|
6 | 5 | } |
|
7 | 6 | } |
|
8 | 7 |
@@ -1,13 +1,13 | |||
|
1 | 1 | namespace Implab.Automaton.RegularExpressions { |
|
2 | 2 | /// <summary> |
|
3 | 3 | /// Интерфейс обходчика синтаксического дерева регулярного выражения |
|
4 | 4 | /// </summary> |
|
5 |
public interface IVisitor |
|
|
6 |
void Visit(AltToken |
|
|
7 |
void Visit(StarToken |
|
|
8 |
void Visit(CatToken |
|
|
9 |
void Visit(EmptyToken |
|
|
10 |
void Visit(EndToken |
|
|
11 |
void Visit(SymbolToken |
|
|
5 | public interface IVisitor { | |
|
6 | void Visit(AltToken token); | |
|
7 | void Visit(StarToken token); | |
|
8 | void Visit(CatToken token); | |
|
9 | void Visit(EmptyToken token); | |
|
10 | void Visit(EndToken token); | |
|
11 | void Visit(SymbolToken token); | |
|
12 | 12 | } |
|
13 | 13 | } |
@@ -1,84 +1,83 | |||
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
1 | using System.Collections.Generic; | |
|
3 | 2 | using System.Linq; |
|
4 | 3 | |
|
5 | 4 | namespace Implab.Automaton.RegularExpressions { |
|
6 | 5 | public class RegularDFA<TInput, TTag> : DFATable, ITaggedDFABuilder<TTag> { |
|
7 | 6 | |
|
8 | 7 | readonly Dictionary<int,TTag[]> m_tags = new Dictionary<int, TTag[]>(); |
|
9 | 8 | readonly IAlphabet<TInput> m_alphabet; |
|
10 | 9 | |
|
11 | 10 | public RegularDFA(IAlphabet<TInput> alphabet) { |
|
12 | 11 | Safe.ArgumentNotNull(alphabet, "aplhabet"); |
|
13 | 12 | |
|
14 | 13 | m_alphabet = alphabet; |
|
15 | 14 | } |
|
16 | 15 | |
|
17 | 16 | |
|
18 | 17 | public IAlphabet<TInput> InputAlphabet { |
|
19 | 18 | get { |
|
20 | 19 | return m_alphabet; |
|
21 | 20 | } |
|
22 | 21 | } |
|
23 | 22 | |
|
24 | 23 | public void MarkFinalState(int s, TTag[] tags) { |
|
25 | 24 | MarkFinalState(s); |
|
26 | 25 | SetStateTag(s, tags); |
|
27 | 26 | } |
|
28 | 27 | |
|
29 | 28 | public void SetStateTag(int s, TTag[] tags) { |
|
30 | 29 | Safe.ArgumentNotNull(tags, "tags"); |
|
31 | 30 | m_tags[s] = tags; |
|
32 | 31 | } |
|
33 | 32 | |
|
34 | 33 | public TTag[] GetStateTag(int s) { |
|
35 | 34 | TTag[] tags; |
|
36 | 35 | return m_tags.TryGetValue(s, out tags) ? tags : new TTag[0]; |
|
37 | 36 | } |
|
38 | 37 | |
|
39 | 38 | public TTag[][] CreateTagTable() { |
|
40 | 39 | var table = new TTag[StateCount][]; |
|
41 | 40 | |
|
42 | 41 | foreach (var pair in m_tags) |
|
43 | 42 | table[pair.Key] = pair.Value; |
|
44 | 43 | |
|
45 | 44 | return table; |
|
46 | 45 | } |
|
47 | 46 | |
|
48 | 47 | /// <summary> |
|
49 | 48 | /// Optimize the specified alphabet. |
|
50 | 49 | /// </summary> |
|
51 | 50 | /// <param name="alphabet">Пустой алфавит, который будет зполнен в процессе оптимизации.</param> |
|
52 | 51 | public RegularDFA<TInput,TTag> Optimize(IAlphabetBuilder<TInput> alphabet) { |
|
53 | 52 | Safe.ArgumentNotNull(alphabet, "alphabet"); |
|
54 | 53 | |
|
55 | 54 | var dfa = new RegularDFA<TInput, TTag>(alphabet); |
|
56 | 55 | |
|
57 | 56 | var states = new DummyAlphabet(StateCount); |
|
58 | 57 | var alphaMap = new Dictionary<int,int>(); |
|
59 | 58 | var stateMap = new Dictionary<int,int>(); |
|
60 | 59 | |
|
61 | 60 | Optimize(dfa, alphaMap, stateMap); |
|
62 | 61 | |
|
63 | 62 | // mark tags in the new DFA |
|
64 | 63 | foreach (var g in m_tags.Where(x => x.Key < StateCount).GroupBy(x => stateMap[x.Key], x => x.Value )) |
|
65 | 64 | dfa.SetStateTag(g.Key, g.SelectMany(x => x).ToArray()); |
|
66 | 65 | |
|
67 | 66 | // make the alphabet for the new DFA |
|
68 | 67 | foreach (var pair in alphaMap) |
|
69 | 68 | alphabet.DefineClass(m_alphabet.GetSymbols(pair.Key), pair.Value); |
|
70 | 69 | |
|
71 | 70 | return dfa; |
|
72 | 71 | } |
|
73 | 72 | |
|
74 | 73 | protected override IEnumerable<HashSet<int>> GroupFinalStates() { |
|
75 | 74 | var arrayComparer = new CustomEqualityComparer<TTag[]>( |
|
76 | 75 | (x,y) => x.Length == y.Length && x.All(it => y.Contains(it)), |
|
77 | 76 | x => x.Sum(it => x.GetHashCode()) |
|
78 | 77 | ); |
|
79 | 78 | return FinalStates.GroupBy(x => m_tags[x], arrayComparer).Select(g => new HashSet<int>(g)); |
|
80 | 79 | } |
|
81 | 80 | |
|
82 | 81 | } |
|
83 | 82 | } |
|
84 | 83 |
@@ -1,184 +1,206 | |||
|
1 | 1 | using Implab; |
|
2 | 2 | using System; |
|
3 | 3 | using System.Collections.Generic; |
|
4 | 4 | using System.Diagnostics; |
|
5 | 5 | using System.Linq; |
|
6 | 6 | |
|
7 | 7 | namespace Implab.Automaton.RegularExpressions { |
|
8 | 8 | /// <summary> |
|
9 | 9 | /// Используется для построения ДКА по регулярному выражению, сначала обходит |
|
10 | 10 | /// регулярное выражение и вычисляет followpos, затем используется метод |
|
11 | 11 | /// <see cref="BuildDFA(IDFADefinition)"/> для построения автомата. |
|
12 | 12 | /// </summary> |
|
13 | 13 | public class RegularExpressionVisitor<TTag> : IVisitor<TTag> { |
|
14 | 14 | int m_idx; |
|
15 |
Token |
|
|
15 | Token m_root; | |
|
16 | 16 | HashSet<int> m_firstpos; |
|
17 | 17 | HashSet<int> m_lastpos; |
|
18 | 18 | |
|
19 | 19 | readonly Dictionary<int, HashSet<int>> m_followpos = new Dictionary<int, HashSet<int>>(); |
|
20 | 20 | readonly Dictionary<int, int> m_indexes = new Dictionary<int, int>(); |
|
21 |
readonly |
|
|
21 | readonly HashSet<int> m_ends = new HashSet<int>(); | |
|
22 | readonly Dictionary<int, TTag> m_tags = new Dictionary<int, TTag>(); | |
|
22 | 23 | |
|
23 | 24 | public Dictionary<int, HashSet<int>> FollowposMap { |
|
24 | 25 | get { return m_followpos; } |
|
25 | 26 | } |
|
26 | 27 | |
|
27 | 28 | public HashSet<int> Followpos(int pos) { |
|
28 | 29 | HashSet<int> set; |
|
29 | 30 | return m_followpos.TryGetValue(pos, out set) ? set : m_followpos[pos] = new HashSet<int>(); |
|
30 | 31 | } |
|
31 | 32 | |
|
32 | 33 | bool Nullable(object n) { |
|
33 |
if (n is EmptyToken |
|
|
34 | if (n is EmptyToken || n is StarToken) | |
|
34 | 35 | return true; |
|
35 |
var altToken = n as AltToken |
|
|
36 | var altToken = n as AltToken; | |
|
36 | 37 | if (altToken != null) |
|
37 | 38 | return Nullable(altToken.Left) || Nullable(altToken.Right); |
|
38 |
var catToken = n as CatToken |
|
|
39 | var catToken = n as CatToken; | |
|
39 | 40 | if (catToken != null) |
|
40 | 41 | return Nullable(catToken.Left) && Nullable(catToken.Right); |
|
41 | 42 | return false; |
|
42 | 43 | } |
|
43 | 44 | |
|
44 | 45 | |
|
45 |
public void Visit(AltToken |
|
|
46 | public void Visit(AltToken token) { | |
|
46 | 47 | if (m_root == null) |
|
47 | 48 | m_root = token; |
|
48 | 49 | var firtspos = new HashSet<int>(); |
|
49 | 50 | var lastpos = new HashSet<int>(); |
|
50 | 51 | |
|
51 | 52 | token.Left.Accept(this); |
|
52 | 53 | firtspos.UnionWith(m_firstpos); |
|
53 | 54 | lastpos.UnionWith(m_lastpos); |
|
54 | 55 | |
|
55 | 56 | token.Right.Accept(this); |
|
56 | 57 | firtspos.UnionWith(m_firstpos); |
|
57 | 58 | lastpos.UnionWith(m_lastpos); |
|
58 | 59 | |
|
59 | 60 | m_firstpos = firtspos; |
|
60 | 61 | m_lastpos = lastpos; |
|
61 | 62 | } |
|
62 | 63 | |
|
63 |
public void Visit(StarToken |
|
|
64 | public void Visit(StarToken token) { | |
|
64 | 65 | if (m_root == null) |
|
65 | 66 | m_root = token; |
|
66 | 67 | token.Token.Accept(this); |
|
67 | 68 | |
|
68 | 69 | foreach (var i in m_lastpos) |
|
69 | 70 | Followpos(i).UnionWith(m_firstpos); |
|
70 | 71 | } |
|
71 | 72 | |
|
72 |
public void Visit(CatToken |
|
|
73 | public void Visit(CatToken token) { | |
|
73 | 74 | if (m_root == null) |
|
74 | 75 | m_root = token; |
|
75 | 76 | |
|
76 | 77 | var firtspos = new HashSet<int>(); |
|
77 | 78 | var lastpos = new HashSet<int>(); |
|
78 | 79 | token.Left.Accept(this); |
|
79 | 80 | firtspos.UnionWith(m_firstpos); |
|
80 | 81 | var leftLastpos = m_lastpos; |
|
81 | 82 | |
|
82 | 83 | token.Right.Accept(this); |
|
83 | 84 | lastpos.UnionWith(m_lastpos); |
|
84 | 85 | var rightFirstpos = m_firstpos; |
|
85 | 86 | |
|
86 | 87 | if (Nullable(token.Left)) |
|
87 | 88 | firtspos.UnionWith(rightFirstpos); |
|
88 | 89 | |
|
89 | 90 | if (Nullable(token.Right)) |
|
90 | 91 | lastpos.UnionWith(leftLastpos); |
|
91 | 92 | |
|
92 | 93 | m_firstpos = firtspos; |
|
93 | 94 | m_lastpos = lastpos; |
|
94 | 95 | |
|
95 | 96 | foreach (var i in leftLastpos) |
|
96 | 97 | Followpos(i).UnionWith(rightFirstpos); |
|
97 | 98 | |
|
98 | 99 | } |
|
99 | 100 | |
|
100 |
public void Visit(EmptyToken |
|
|
101 | public void Visit(EmptyToken token) { | |
|
101 | 102 | if (m_root == null) |
|
102 | 103 | m_root = token; |
|
103 | 104 | } |
|
104 | 105 | |
|
105 |
public void Visit(SymbolToken |
|
|
106 | public void Visit(SymbolToken token) { | |
|
106 | 107 | if (m_root == null) |
|
107 | 108 | m_root = token; |
|
108 | 109 | m_idx++; |
|
109 | 110 | m_indexes[m_idx] = token.Value; |
|
110 | 111 | m_firstpos = new HashSet<int>(new[] { m_idx }); |
|
111 | 112 | m_lastpos = new HashSet<int>(new[] { m_idx }); |
|
112 | 113 | } |
|
113 | 114 | |
|
114 | 115 | public void Visit(EndToken<TTag> token) { |
|
115 | 116 | if (m_root == null) |
|
116 | 117 | m_root = token; |
|
117 | 118 | m_idx++; |
|
118 | 119 | m_indexes[m_idx] = DFAConst.UNCLASSIFIED_INPUT; |
|
119 | 120 | m_firstpos = new HashSet<int>(new[] { m_idx }); |
|
120 | 121 | m_lastpos = new HashSet<int>(new[] { m_idx }); |
|
121 | 122 | Followpos(m_idx); |
|
122 |
m_ends.Add(m_idx |
|
|
123 | m_ends.Add(m_idx); | |
|
124 | m_tags.Add(m_idx, token.Tag); | |
|
125 | } | |
|
126 | ||
|
127 | public void Visit(EndToken token) { | |
|
128 | if (m_root == null) | |
|
129 | m_root = token; | |
|
130 | m_idx++; | |
|
131 | m_indexes[m_idx] = DFAConst.UNCLASSIFIED_INPUT; | |
|
132 | m_firstpos = new HashSet<int>(new[] { m_idx }); | |
|
133 | m_lastpos = new HashSet<int>(new[] { m_idx }); | |
|
134 | Followpos(m_idx); | |
|
135 | m_ends.Add(m_idx); | |
|
123 | 136 | } |
|
124 | 137 | |
|
125 | 138 | public void BuildDFA(ITaggedDFABuilder<TTag> dfa) { |
|
126 | 139 | Safe.ArgumentNotNull(dfa,"dfa"); |
|
127 | 140 | |
|
128 | 141 | var states = new MapAlphabet<HashSet<int>>( |
|
129 | 142 | false, |
|
130 | 143 | new CustomEqualityComparer<HashSet<int>>( |
|
131 | 144 | (x, y) => x.SetEquals(y), |
|
132 | 145 | x => x.Sum(n => n.GetHashCode()) |
|
133 | 146 | )); |
|
134 | 147 | |
|
135 | 148 | var initialState = states.DefineSymbol(m_firstpos); |
|
136 | 149 | dfa.SetInitialState(initialState); |
|
137 | 150 | |
|
138 | 151 | var tags = GetStateTags(m_firstpos); |
|
139 | 152 | if (tags != null && tags.Length > 0) |
|
140 | 153 | dfa.MarkFinalState(initialState, tags); |
|
141 | 154 | |
|
142 | 155 | var inputMax = m_indexes.Values.Max(); |
|
143 | 156 | var queue = new Queue<HashSet<int>>(); |
|
144 | 157 | |
|
145 | 158 | queue.Enqueue(m_firstpos); |
|
146 | 159 | |
|
147 | 160 | while (queue.Count > 0) { |
|
148 | 161 | var state = queue.Dequeue(); |
|
149 | 162 | var s1 = states.Translate(state); |
|
150 | 163 | Debug.Assert(s1 != DFAConst.UNCLASSIFIED_INPUT); |
|
151 | 164 | |
|
152 | 165 | for (int a = 0; a <= inputMax; a++) { |
|
153 | 166 | var next = new HashSet<int>(); |
|
154 | 167 | foreach (var p in state) { |
|
155 | 168 | if (m_indexes[p] == a) { |
|
156 | 169 | next.UnionWith(Followpos(p)); |
|
157 | 170 | } |
|
158 | 171 | } |
|
159 | 172 | if (next.Count > 0) { |
|
160 |
int s2 |
|
|
161 |
if ( |
|
|
173 | int s2; | |
|
174 | if (states.Contains(next)) { | |
|
175 | s2 = states.Translate(next); | |
|
176 | } else { | |
|
162 | 177 | s2 = states.DefineSymbol(next); |
|
163 | 178 | |
|
179 | if (IsFinal(next)) { | |
|
180 | ||
|
181 | dfa.MarkFinalState(s2); | |
|
164 | 182 | tags = GetStateTags(next); |
|
165 |
if (tags != null && tags.Length > 0) |
|
|
166 | dfa.MarkFinalState(s2); | |
|
183 | if (tags != null && tags.Length > 0) | |
|
167 | 184 | dfa.SetStateTag(s2, tags); |
|
168 | 185 | } |
|
169 | 186 | |
|
170 | 187 | queue.Enqueue(next); |
|
171 | 188 | } |
|
172 | 189 | dfa.Add(new AutomatonTransition(s1, s2, a)); |
|
173 | 190 | } |
|
174 | 191 | } |
|
175 | 192 | } |
|
176 | 193 | } |
|
177 | 194 | |
|
195 | bool IsFinal(IEnumerable<int> state) { | |
|
196 | Debug.Assert(state != null); | |
|
197 | return state.Any(m_ends.Contains); | |
|
198 | } | |
|
199 | ||
|
178 | 200 | TTag[] GetStateTags(IEnumerable<int> state) { |
|
179 | 201 | Debug.Assert(state != null); |
|
180 |
return state.Where(m_ |
|
|
202 | return state.Where(m_tags.ContainsKey).Select(pos => m_tags[pos]).ToArray(); | |
|
181 | 203 | } |
|
182 | 204 | |
|
183 | 205 | } |
|
184 | 206 | } |
@@ -1,34 +1,31 | |||
|
1 | 1 | using Implab; |
|
2 | 2 | using System; |
|
3 | using System.Collections.Generic; | |
|
4 | using System.Linq; | |
|
5 | using System.Text; | |
|
6 | using System.Threading.Tasks; | |
|
3 | ||
|
7 | 4 | |
|
8 | 5 | namespace Implab.Automaton.RegularExpressions { |
|
9 | 6 | /// <summary> |
|
10 | 7 | /// Замыкание выражения с 0 и более повторов. |
|
11 | 8 | /// </summary> |
|
12 |
public class StarToken |
|
|
9 | public class StarToken: Token { | |
|
13 | 10 | |
|
14 |
Token |
|
|
11 | Token m_token; | |
|
15 | 12 | |
|
16 |
public Token |
|
|
13 | public Token Token { | |
|
17 | 14 | get { return m_token; } |
|
18 | 15 | } |
|
19 | 16 | |
|
20 |
public StarToken(Token |
|
|
17 | public StarToken(Token token) { | |
|
21 | 18 | Safe.ArgumentNotNull(token, "token"); |
|
22 | 19 | m_token = token; |
|
23 | 20 | } |
|
24 | 21 | |
|
25 |
public override void Accept(IVisitor |
|
|
22 | public override void Accept(IVisitor visitor) { | |
|
26 | 23 | Safe.ArgumentNotNull(visitor, "visitor"); |
|
27 | 24 | visitor.Visit(this); |
|
28 | 25 | } |
|
29 | 26 | |
|
30 | 27 | public override string ToString() { |
|
31 | 28 | return String.Format("({0})*", Token); |
|
32 | 29 | } |
|
33 | 30 | } |
|
34 | 31 | } |
@@ -1,27 +1,27 | |||
|
1 | 1 | using Implab; |
|
2 | 2 | |
|
3 | 3 | namespace Implab.Automaton.RegularExpressions { |
|
4 | 4 | /// <summary> |
|
5 | 5 | /// Выражение, соответсвующее одному символу. |
|
6 | 6 | /// </summary> |
|
7 |
public class SymbolToken |
|
|
7 | public class SymbolToken: Token { | |
|
8 | 8 | int m_value; |
|
9 | 9 | |
|
10 | 10 | public int Value { |
|
11 | 11 | get { return m_value; } |
|
12 | 12 | } |
|
13 | 13 | |
|
14 | 14 | public SymbolToken(int value) { |
|
15 | 15 | m_value = value; |
|
16 | 16 | } |
|
17 |
public override void Accept(IVisitor |
|
|
17 | public override void Accept(IVisitor visitor) { | |
|
18 | 18 | Safe.ArgumentNotNull(visitor, "visitor"); |
|
19 | 19 | |
|
20 | 20 | visitor.Visit(this); |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | 23 | public override string ToString() { |
|
24 | 24 | return Value.ToString(); |
|
25 | 25 | } |
|
26 | 26 | } |
|
27 | 27 | } |
@@ -1,63 +1,63 | |||
|
1 | 1 | using Implab; |
|
2 | 2 | using System; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | |
|
5 | 5 | namespace Implab.Automaton.RegularExpressions { |
|
6 |
public abstract class Token |
|
|
7 |
public abstract void Accept(IVisitor |
|
|
6 | public abstract class Token { | |
|
7 | public abstract void Accept(IVisitor visitor); | |
|
8 | 8 | |
|
9 |
public Token |
|
|
10 |
return Cat(new EndToken |
|
|
9 | public Token Extend() { | |
|
10 | return Cat(new EndToken()); | |
|
11 | 11 | } |
|
12 | 12 | |
|
13 |
public Token<TTag> |
|
|
13 | public Token Tag<TTag>(TTag tag) { | |
|
14 | 14 | return Cat(new EndToken<TTag>(tag)); |
|
15 | 15 | } |
|
16 | 16 | |
|
17 |
public Token |
|
|
18 |
return new CatToken |
|
|
17 | public Token Cat(Token right) { | |
|
18 | return new CatToken(this, right); | |
|
19 | 19 | } |
|
20 | 20 | |
|
21 |
public Token |
|
|
22 |
return new AltToken |
|
|
21 | public Token Or(Token right) { | |
|
22 | return new AltToken(this, right); | |
|
23 | 23 | } |
|
24 | 24 | |
|
25 |
public Token |
|
|
26 |
return Or(new EmptyToken |
|
|
25 | public Token Optional() { | |
|
26 | return Or(new EmptyToken()); | |
|
27 | 27 | } |
|
28 | 28 | |
|
29 |
public Token |
|
|
30 |
return new StarToken |
|
|
29 | public Token EClosure() { | |
|
30 | return new StarToken(this); | |
|
31 | 31 | } |
|
32 | 32 | |
|
33 |
public Token |
|
|
34 |
return Cat(new StarToken |
|
|
33 | public Token Closure() { | |
|
34 | return Cat(new StarToken(this)); | |
|
35 | 35 | } |
|
36 | 36 | |
|
37 |
public Token |
|
|
38 |
Token |
|
|
37 | public Token Repeat(int count) { | |
|
38 | Token token = null; | |
|
39 | 39 | |
|
40 | 40 | for (int i = 0; i < count; i++) |
|
41 | 41 | token = token != null ? token.Cat(this) : this; |
|
42 |
return token ?? new EmptyToken |
|
|
42 | return token ?? new EmptyToken(); | |
|
43 | 43 | } |
|
44 | 44 | |
|
45 |
public Token |
|
|
45 | public Token Repeat(int min, int max) { | |
|
46 | 46 | if (min > max || min < 1) |
|
47 | 47 | throw new ArgumentOutOfRangeException(); |
|
48 | 48 | var token = Repeat(min); |
|
49 | 49 | |
|
50 | 50 | for (int i = min; i < max; i++) |
|
51 | 51 | token = token.Cat( Optional() ); |
|
52 | 52 | return token; |
|
53 | 53 | } |
|
54 | 54 | |
|
55 |
public static Token |
|
|
55 | public static Token New(params int[] set) { | |
|
56 | 56 | Safe.ArgumentNotNull(set, "set"); |
|
57 |
Token |
|
|
57 | Token token = null; | |
|
58 | 58 | foreach(var c in set.Distinct()) |
|
59 |
token = token == null ? new SymbolToken |
|
|
59 | token = token == null ? new SymbolToken(c) : token.Or(new SymbolToken(c)); | |
|
60 | 60 | return token; |
|
61 | 61 | } |
|
62 | 62 | } |
|
63 | 63 | } |
@@ -1,25 +1,23 | |||
|
1 | 1 | using System.Collections.Generic; |
|
2 | 2 | using System.Linq; |
|
3 | 3 | using Implab.Automaton; |
|
4 | 4 | |
|
5 | 5 | namespace Implab.Formats { |
|
6 | 6 | public class ByteAlphabet : IndexedAlphabetBase<byte> { |
|
7 | public ByteAlphabet() { | |
|
8 | } | |
|
9 | 7 | |
|
10 | 8 | #region implemented abstract members of IndexedAlphabetBase |
|
11 | 9 | |
|
12 | 10 | public override int GetSymbolIndex(byte symbol) { |
|
13 | 11 | return (int)symbol; |
|
14 | 12 | } |
|
15 | 13 | |
|
16 | 14 | public IEnumerable<byte> InputSymbols { |
|
17 | 15 | get { |
|
18 | 16 | return Enumerable.Range(byte.MinValue, byte.MaxValue).Cast<byte>(); |
|
19 | 17 | } |
|
20 | 18 | } |
|
21 | 19 | |
|
22 | 20 | #endregion |
|
23 | 21 | } |
|
24 | 22 | } |
|
25 | 23 |
@@ -1,19 +1,16 | |||
|
1 | 1 | using System.Collections.Generic; |
|
2 | 2 | using System.Linq; |
|
3 | 3 | using Implab.Automaton; |
|
4 | 4 | |
|
5 | 5 | namespace Implab.Formats { |
|
6 | 6 | public class CharAlphabet: IndexedAlphabetBase<char> { |
|
7 | 7 | |
|
8 | public CharAlphabet() { | |
|
9 | } | |
|
10 | ||
|
11 | 8 | public override int GetSymbolIndex(char symbol) { |
|
12 | 9 | return symbol; |
|
13 | 10 | } |
|
14 | 11 | |
|
15 | 12 | public IEnumerable<char> InputSymbols { |
|
16 | 13 | get { return Enumerable.Range(char.MinValue, char.MaxValue).Cast<char>(); } |
|
17 | 14 | } |
|
18 | 15 | } |
|
19 | 16 | } |
@@ -1,101 +1,104 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Globalization; |
|
3 | 3 | using Implab.Automaton; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using Implab.Components; |
|
6 | 6 | using System.IO; |
|
7 | using Implab.Automaton.RegularExpressions; | |
|
8 | 7 | |
|
9 | 8 | namespace Implab.Formats.JSON { |
|
10 | 9 | /// <summary> |
|
11 | 10 | /// Сканнер (лексер), разбивающий поток символов на токены JSON. |
|
12 | 11 | /// </summary> |
|
13 | 12 | public class JSONScanner : Disposable { |
|
14 | 13 | readonly StringBuilder m_builder = new StringBuilder(); |
|
15 | 14 | |
|
16 |
readonly ScannerContext<JSONGrammar.TokenType> m_json |
|
|
17 |
readonly ScannerContext<JSONGrammar.TokenType> m_string |
|
|
15 | readonly ScannerContext<JSONGrammar.TokenType> m_jsonContext = JSONGrammar.Instance.JsonDFA; | |
|
16 | readonly ScannerContext<JSONGrammar.TokenType> m_stringContext = JSONGrammar.Instance.JsonStringDFA; | |
|
18 | 17 | |
|
19 | 18 | |
|
20 | 19 | readonly TextScanner m_scanner; |
|
21 | 20 | |
|
22 | 21 | /// <summary> |
|
23 | 22 | /// Создает новый экземпляр сканнера |
|
24 | 23 | /// </summary> |
|
25 | 24 | public JSONScanner(string text) { |
|
26 | 25 | Safe.ArgumentNotEmpty(text, "text"); |
|
27 | 26 | |
|
28 | 27 | m_scanner = new StringScanner(text); |
|
29 | 28 | } |
|
30 | 29 | |
|
31 | 30 | public JSONScanner(TextReader reader, int bufferMax, int chunkSize) { |
|
32 | 31 | Safe.ArgumentNotNull(reader, "reader"); |
|
33 | 32 | |
|
34 | m_scanner = new ReaderScanner(reader); | |
|
33 | m_scanner = new ReaderScanner(reader, bufferMax, chunkSize); | |
|
35 | 34 | } |
|
36 | 35 | |
|
37 | 36 | /// <summary> |
|
38 | 37 | /// Читает следующий лексический элемент из входных данных. |
|
39 | 38 | /// </summary> |
|
40 | 39 | /// <param name="tokenValue">Возвращает значение прочитанного токена.</param> |
|
41 | 40 | /// <param name="tokenType">Возвращает тип прочитанного токена.</param> |
|
42 | 41 | /// <returns><c>true</c> - чтение произведено успешно. <c>false</c> - достигнут конец входных данных</returns> |
|
43 | 42 | /// <remarks>В случе если токен не распознается, возникает исключение. Значения токенов обрабатываются, т.е. |
|
44 | 43 | /// в строках обрабатываются экранированные символы, числа становтся типа double.</remarks> |
|
45 | 44 | public bool ReadToken(out object tokenValue, out JsonTokenType tokenType) { |
|
46 | 45 | JSONGrammar.TokenType[] tag; |
|
47 |
if (m_json |
|
|
46 | if (m_jsonContext.Execute(m_scanner, out tag)) { | |
|
48 | 47 | switch (tag[0]) { |
|
49 | 48 | case JSONGrammar.TokenType.StringBound: |
|
50 | 49 | tokenValue = ReadString(); |
|
51 | 50 | tokenType = JsonTokenType.String; |
|
52 | 51 | break; |
|
53 | 52 | case JSONGrammar.TokenType.Number: |
|
54 | 53 | tokenValue = Double.Parse(m_scanner.GetTokenValue(), CultureInfo.InvariantCulture); |
|
55 | 54 | tokenType = JsonTokenType.Number; |
|
56 | 55 | break; |
|
57 | 56 | default: |
|
58 | 57 | tokenType = (JsonTokenType)tag[0]; |
|
59 | 58 | tokenValue = m_scanner.GetTokenValue(); |
|
60 | 59 | break; |
|
61 | 60 | } |
|
62 | 61 | return true; |
|
63 | 62 | } |
|
64 | 63 | tokenValue = null; |
|
65 | 64 | tokenType = JsonTokenType.None; |
|
66 | 65 | return false; |
|
67 | 66 | } |
|
68 | 67 | |
|
69 | 68 | string ReadString() { |
|
70 | 69 | int pos = 0; |
|
71 |
|
|
|
70 | var buf = new char[6]; // the buffer for unescaping chars | |
|
72 | 71 | |
|
73 | 72 | JSONGrammar.TokenType[] tag; |
|
74 | 73 | m_builder.Clear(); |
|
75 | 74 | |
|
76 |
while (m_string |
|
|
75 | while (m_stringContext.Execute(m_scanner, out tag)) { | |
|
77 | 76 | switch (tag[0]) { |
|
78 | 77 | case JSONGrammar.TokenType.StringBound: |
|
79 | 78 | return m_builder.ToString(); |
|
80 | 79 | case JSONGrammar.TokenType.UnescapedChar: |
|
81 | 80 | m_scanner.CopyTokenTo(m_builder); |
|
82 | 81 | break; |
|
83 | 82 | case JSONGrammar.TokenType.EscapedUnicode: // \xXXXX - unicode escape sequence |
|
84 | 83 | m_scanner.CopyTokenTo(buf, 0); |
|
85 | 84 | m_builder.Append(StringTranslator.TranslateHexUnicode(buf, 2)); |
|
86 | 85 | pos++; |
|
87 | 86 | break; |
|
88 | 87 | case JSONGrammar.TokenType.EscapedChar: // \t - escape sequence |
|
89 | 88 | m_scanner.CopyTokenTo(buf, 0); |
|
90 | 89 | m_builder.Append(StringTranslator.TranslateEscapedChar(buf[1])); |
|
91 | 90 | break; |
|
92 | default: | |
|
93 | break; | |
|
94 | 91 | } |
|
95 | 92 | |
|
96 | 93 | } |
|
97 | 94 | |
|
98 | 95 | throw new ParserException("Unexpected end of data"); |
|
99 | 96 | } |
|
97 | ||
|
98 | protected override void Dispose(bool disposing) { | |
|
99 | if (disposing) | |
|
100 | Safe.Dispose(m_scanner); | |
|
101 | base.Dispose(disposing); | |
|
100 | 102 | } |
|
101 | 103 | } |
|
104 | } |
@@ -1,24 +1,30 | |||
|
1 | using System; | |
|
2 | ||
|
3 | namespace Implab.Formats { | |
|
1 | namespace Implab.Formats { | |
|
2 | /// <summary> | |
|
3 | /// Represents a scanner configuration usefull to recongnize token, based on the DFA. | |
|
4 | /// </summary> | |
|
4 | 5 | public class ScannerContext<TTag> { |
|
6 | ||
|
5 | 7 | public int[,] Dfa { get; private set; } |
|
8 | ||
|
6 | 9 | public bool[] Final { get; private set; } |
|
10 | ||
|
7 | 11 | public TTag[][] Tags { get; private set; } |
|
12 | ||
|
8 | 13 | public int State { get; private set; } |
|
14 | ||
|
9 | 15 | public int[] Alphabet { get; private set; } |
|
10 | 16 | |
|
11 | 17 | public ScannerContext(int[,] dfa, bool[] final, TTag[][] tags, int state, int[] alphabet) { |
|
12 | 18 | Dfa = dfa; |
|
13 | 19 | Final = final; |
|
14 | 20 | Tags = tags; |
|
15 | 21 | State = state; |
|
16 | 22 | Alphabet = alphabet; |
|
17 | 23 | } |
|
18 | 24 | |
|
19 | 25 | public bool Execute(TextScanner scanner, out TTag[] tag) { |
|
20 | 26 | return scanner.ReadToken(Dfa, Final, Tags, State, Alphabet, out tag); |
|
21 | 27 | } |
|
22 | 28 | } |
|
23 | 29 | } |
|
24 | 30 |
@@ -1,149 +1,150 | |||
|
1 | 1 | using System; |
|
2 | 2 | using Implab.Components; |
|
3 | using Implab.Automaton.RegularExpressions; | |
|
4 | 3 | using System.Diagnostics; |
|
5 | 4 | using Implab.Automaton; |
|
6 | using System.IO; | |
|
7 | 5 | using System.Text; |
|
8 | 6 | |
|
9 | 7 | namespace Implab.Formats { |
|
10 | 8 | public abstract class TextScanner : Disposable { |
|
11 | 9 | readonly int m_bufferMax; |
|
12 | 10 | readonly int m_chunkSize; |
|
13 | 11 | |
|
14 | 12 | char[] m_buffer; |
|
15 | 13 | int m_bufferOffset; |
|
16 | 14 | int m_bufferSize; |
|
17 | 15 | int m_tokenOffset; |
|
18 | 16 | int m_tokenLength; |
|
19 | 17 | |
|
20 | 18 | /// <summary> |
|
21 |
/// Initializes a new instance of the <see cref="Implab.Formats.TextScanner |
|
|
19 | /// Initializes a new instance of the <see cref="Implab.Formats.TextScanner"/> class. | |
|
22 | 20 | /// </summary> |
|
23 | 21 | /// <param name="bufferMax">Buffer max.</param> |
|
24 | 22 | /// <param name="chunkSize">Chunk size.</param> |
|
25 | 23 | protected TextScanner(int bufferMax, int chunkSize) { |
|
26 | 24 | Debug.Assert(m_chunkSize <= m_bufferMax); |
|
27 | 25 | |
|
28 | 26 | m_bufferMax = bufferMax; |
|
29 | 27 | m_chunkSize = chunkSize; |
|
30 | 28 | } |
|
31 | 29 | |
|
32 | 30 | /// <summary> |
|
33 |
/// Initializes a new instance of the <see cref="Implab.Formats.TextScanner |
|
|
31 | /// Initializes a new instance of the <see cref="Implab.Formats.TextScanner"/> class. | |
|
34 | 32 | /// </summary> |
|
35 | 33 | /// <param name="buffer">Buffer.</param> |
|
36 | 34 | protected TextScanner(char[] buffer) { |
|
37 | 35 | if (buffer != null) { |
|
38 | 36 | m_buffer = buffer; |
|
39 | 37 | m_bufferSize = buffer.Length; |
|
40 | 38 | } |
|
41 | 39 | } |
|
42 | 40 | |
|
43 | 41 | /// <summary> |
|
44 | 42 | /// (hungry) Reads the next token. |
|
45 | 43 | /// </summary> |
|
46 | 44 | /// <returns><c>true</c>, if token internal was read, <c>false</c> if there is no more tokens in the stream.</returns> |
|
47 | 45 | /// <param name="dfa">The transition map for the automaton</param> |
|
48 | 46 | /// <param name="final">Final states of the automaton.</param> |
|
49 | 47 | /// <param name="tags">Tags.</param> |
|
50 | 48 | /// <param name="state">The initial state for the automaton.</param> |
|
51 | internal bool ReadToken<TTag>(int[,] dfa, int[] final, TTag[][] tags, int state, int[] alphabet, out TTag[] tag) { | |
|
49 | /// <param name="alphabet"></param> | |
|
50 | /// <param name = "tag"></param> | |
|
51 | internal bool ReadToken<TTag>(int[,] dfa, bool[] final, TTag[][] tags, int state, int[] alphabet, out TTag[] tag) { | |
|
52 | 52 | Safe.ArgumentNotNull(); |
|
53 | 53 | m_tokenLength = 0; |
|
54 | 54 | |
|
55 | 55 | var maxSymbol = alphabet.Length - 1; |
|
56 | 56 | |
|
57 | 57 | do { |
|
58 | 58 | // after the next chunk is read the offset in the buffer may change |
|
59 | 59 | int pos = m_bufferOffset + m_tokenLength; |
|
60 | 60 | |
|
61 | 61 | while(pos < m_bufferSize) { |
|
62 | 62 | var ch = m_buffer[pos]; |
|
63 | 63 | |
|
64 | 64 | state = dfa[state,ch > maxSymbol ? DFAConst.UNCLASSIFIED_INPUT : alphabet[ch]]; |
|
65 | 65 | if (state == DFAConst.UNREACHABLE_STATE) |
|
66 | 66 | break; |
|
67 | 67 | |
|
68 | 68 | pos++; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | m_tokenLength = pos - m_bufferOffset; |
|
72 | 72 | } while (state != DFAConst.UNREACHABLE_STATE && Feed()); |
|
73 | 73 | |
|
74 | 74 | m_tokenOffset = m_bufferOffset; |
|
75 | 75 | m_bufferOffset += m_tokenLength; |
|
76 | 76 | |
|
77 | 77 | if (final[state]) { |
|
78 | 78 | tag = tags[state]; |
|
79 | 79 | return true; |
|
80 |
} |
|
|
80 | } | |
|
81 | ||
|
81 | 82 |
|
|
82 | 83 |
|
|
83 | 84 | return false; |
|
84 | 85 | |
|
85 | 86 |
|
|
86 | 87 |
|
|
88 | ||
|
87 | 89 |
|
|
88 | 90 | |
|
89 | 91 |
|
|
90 | } | |
|
91 | 92 | |
|
92 | 93 | protected void Feed(char[] buffer, int offset, int length) { |
|
93 | 94 | m_buffer = buffer; |
|
94 | 95 | m_bufferOffset = offset; |
|
95 | 96 | m_bufferSize = offset + length; |
|
96 | 97 | } |
|
97 | 98 | |
|
98 | 99 | protected bool Feed() { |
|
99 | 100 | if (m_chunkSize <= 0) |
|
100 | 101 | return false; |
|
101 | 102 | |
|
102 | 103 | if (m_buffer != null) { |
|
103 | 104 | var free = m_buffer.Length - m_bufferSize; |
|
104 | 105 | |
|
105 | 106 | if (free < m_chunkSize) { |
|
106 | 107 | free += m_chunkSize; |
|
107 | 108 | var used = m_bufferSize - m_bufferOffset; |
|
108 | 109 | var size = used + free; |
|
109 | 110 | |
|
110 | 111 | if (size > m_bufferMax) |
|
111 |
throw new ParserException(String.Format("The buffer limit ({0} Kb) is reached" |
|
|
112 | throw new ParserException(String.Format("The buffer limit ({0} Kb) is reached", m_bufferMax/1024)); | |
|
112 | 113 | |
|
113 | 114 | var temp = new char[size]; |
|
114 | 115 | |
|
115 | 116 | var read = Read(temp, used, m_chunkSize); |
|
116 | 117 | if (read == 0) |
|
117 | 118 | return false; |
|
118 | 119 | |
|
119 | 120 | Array.Copy(m_buffer, m_bufferOffset, temp, 0, used); |
|
120 | 121 | |
|
121 | 122 | m_bufferOffset = 0; |
|
122 | 123 | m_bufferSize = used + read; |
|
123 | 124 | m_buffer = temp; |
|
124 | 125 | } |
|
125 | 126 | } else { |
|
126 | 127 | Debug.Assert(m_bufferOffset == 0); |
|
127 | 128 | m_buffer = new char[m_chunkSize]; |
|
128 | 129 | m_bufferSize = Read(m_buffer, 0, m_chunkSize); |
|
129 | 130 | return (m_bufferSize != 0); |
|
130 | 131 | } |
|
131 | 132 | } |
|
132 | 133 | |
|
133 | 134 | protected abstract int Read(char[] buffer, int offset, int size); |
|
134 | 135 | |
|
135 | 136 | public string GetTokenValue() { |
|
136 | 137 | return new String(m_buffer, m_tokenOffset, m_tokenLength); |
|
137 | 138 | } |
|
138 | 139 | |
|
139 | 140 | public void CopyTokenTo(char[] buffer, int offset) { |
|
140 | 141 | m_buffer.CopyTo(buffer, offset); |
|
141 | 142 | } |
|
142 | 143 | |
|
143 | 144 | public void CopyTokenTo(StringBuilder sb) { |
|
144 | 145 | sb.Append(m_buffer, m_tokenOffset, m_tokenLength); |
|
145 | 146 | } |
|
146 | 147 | |
|
147 | 148 | } |
|
148 | 149 | } |
|
149 | 150 |
@@ -1,273 +1,275 | |||
|
1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
|
2 | 2 | <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|
3 | 3 | <PropertyGroup> |
|
4 | 4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|
5 | 5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|
6 | 6 | <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid> |
|
7 | 7 | <OutputType>Library</OutputType> |
|
8 | 8 | <RootNamespace>Implab</RootNamespace> |
|
9 | 9 | <AssemblyName>Implab</AssemblyName> |
|
10 | 10 | <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> |
|
11 | 11 | <ReleaseVersion>0.2</ReleaseVersion> |
|
12 | 12 | <ProductVersion>8.0.30703</ProductVersion> |
|
13 | 13 | <SchemaVersion>2.0</SchemaVersion> |
|
14 | 14 | </PropertyGroup> |
|
15 | 15 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|
16 | 16 | <DebugSymbols>true</DebugSymbols> |
|
17 | 17 | <DebugType>full</DebugType> |
|
18 | 18 | <Optimize>false</Optimize> |
|
19 | 19 | <OutputPath>bin\Debug</OutputPath> |
|
20 | 20 | <DefineConstants>TRACE;DEBUG;</DefineConstants> |
|
21 | 21 | <ErrorReport>prompt</ErrorReport> |
|
22 | 22 | <WarningLevel>4</WarningLevel> |
|
23 | 23 | <ConsolePause>false</ConsolePause> |
|
24 | 24 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
25 | 25 | </PropertyGroup> |
|
26 | 26 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|
27 | 27 | <DebugType>full</DebugType> |
|
28 | 28 | <Optimize>true</Optimize> |
|
29 | 29 | <OutputPath>bin\Release</OutputPath> |
|
30 | 30 | <ErrorReport>prompt</ErrorReport> |
|
31 | 31 | <WarningLevel>4</WarningLevel> |
|
32 | 32 | <ConsolePause>false</ConsolePause> |
|
33 | 33 | </PropertyGroup> |
|
34 | 34 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' "> |
|
35 | 35 | <DebugSymbols>true</DebugSymbols> |
|
36 | 36 | <DebugType>full</DebugType> |
|
37 | 37 | <Optimize>false</Optimize> |
|
38 | 38 | <OutputPath>bin\Debug</OutputPath> |
|
39 | 39 | <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants> |
|
40 | 40 | <ErrorReport>prompt</ErrorReport> |
|
41 | 41 | <WarningLevel>4</WarningLevel> |
|
42 | 42 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
43 | 43 | <ConsolePause>false</ConsolePause> |
|
44 | 44 | </PropertyGroup> |
|
45 | 45 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' "> |
|
46 | 46 | <Optimize>true</Optimize> |
|
47 | 47 | <OutputPath>bin\Release</OutputPath> |
|
48 | 48 | <ErrorReport>prompt</ErrorReport> |
|
49 | 49 | <WarningLevel>4</WarningLevel> |
|
50 | 50 | <ConsolePause>false</ConsolePause> |
|
51 | 51 | <DefineConstants>NET_4_5</DefineConstants> |
|
52 | 52 | </PropertyGroup> |
|
53 | 53 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' "> |
|
54 | 54 | <DebugSymbols>true</DebugSymbols> |
|
55 | 55 | <DebugType>full</DebugType> |
|
56 | 56 | <Optimize>false</Optimize> |
|
57 | 57 | <OutputPath>bin\Debug</OutputPath> |
|
58 | 58 | <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants> |
|
59 | 59 | <ErrorReport>prompt</ErrorReport> |
|
60 | 60 | <WarningLevel>4</WarningLevel> |
|
61 | 61 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
62 | 62 | <ConsolePause>false</ConsolePause> |
|
63 | 63 | </PropertyGroup> |
|
64 | 64 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' "> |
|
65 | 65 | <Optimize>true</Optimize> |
|
66 | 66 | <OutputPath>bin\Release</OutputPath> |
|
67 | 67 | <DefineConstants>NET_4_5;MONO;</DefineConstants> |
|
68 | 68 | <ErrorReport>prompt</ErrorReport> |
|
69 | 69 | <WarningLevel>4</WarningLevel> |
|
70 | 70 | <ConsolePause>false</ConsolePause> |
|
71 | 71 | </PropertyGroup> |
|
72 | 72 | <ItemGroup> |
|
73 | 73 | <Reference Include="System" /> |
|
74 | 74 | <Reference Include="System.Xml" /> |
|
75 | 75 | <Reference Include="mscorlib" /> |
|
76 | 76 | </ItemGroup> |
|
77 | 77 | <ItemGroup> |
|
78 | 78 | <Compile Include="CustomEqualityComparer.cs" /> |
|
79 | 79 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> |
|
80 | 80 | <Compile Include="Diagnostics\EventText.cs" /> |
|
81 | 81 | <Compile Include="Diagnostics\LogChannel.cs" /> |
|
82 | 82 | <Compile Include="Diagnostics\LogicalOperation.cs" /> |
|
83 | 83 | <Compile Include="Diagnostics\TextFileListener.cs" /> |
|
84 | 84 | <Compile Include="Diagnostics\TraceLog.cs" /> |
|
85 | 85 | <Compile Include="Diagnostics\TraceEvent.cs" /> |
|
86 | 86 | <Compile Include="Diagnostics\TraceEventType.cs" /> |
|
87 | 87 | <Compile Include="ICancellable.cs" /> |
|
88 | 88 | <Compile Include="IProgressHandler.cs" /> |
|
89 | 89 | <Compile Include="IProgressNotifier.cs" /> |
|
90 | 90 | <Compile Include="IPromiseT.cs" /> |
|
91 | 91 | <Compile Include="IPromise.cs" /> |
|
92 | 92 | <Compile Include="IServiceLocator.cs" /> |
|
93 | 93 | <Compile Include="ITaskController.cs" /> |
|
94 | 94 | <Compile Include="Parallels\DispatchPool.cs" /> |
|
95 | 95 | <Compile Include="Parallels\ArrayTraits.cs" /> |
|
96 | 96 | <Compile Include="Parallels\MTQueue.cs" /> |
|
97 | 97 | <Compile Include="Parallels\WorkerPool.cs" /> |
|
98 | 98 | <Compile Include="ProgressInitEventArgs.cs" /> |
|
99 | 99 | <Compile Include="Properties\AssemblyInfo.cs" /> |
|
100 | 100 | <Compile Include="Parallels\AsyncPool.cs" /> |
|
101 | 101 | <Compile Include="Safe.cs" /> |
|
102 | 102 | <Compile Include="ValueEventArgs.cs" /> |
|
103 | 103 | <Compile Include="PromiseExtensions.cs" /> |
|
104 | 104 | <Compile Include="SyncContextPromise.cs" /> |
|
105 | 105 | <Compile Include="Diagnostics\OperationContext.cs" /> |
|
106 | 106 | <Compile Include="Diagnostics\TraceContext.cs" /> |
|
107 | 107 | <Compile Include="Diagnostics\LogEventArgs.cs" /> |
|
108 | 108 | <Compile Include="Diagnostics\LogEventArgsT.cs" /> |
|
109 | 109 | <Compile Include="Diagnostics\Extensions.cs" /> |
|
110 | 110 | <Compile Include="PromiseEventType.cs" /> |
|
111 | 111 | <Compile Include="Parallels\AsyncQueue.cs" /> |
|
112 | 112 | <Compile Include="PromiseT.cs" /> |
|
113 | 113 | <Compile Include="IDeferred.cs" /> |
|
114 | 114 | <Compile Include="IDeferredT.cs" /> |
|
115 | 115 | <Compile Include="Promise.cs" /> |
|
116 | 116 | <Compile Include="PromiseTransientException.cs" /> |
|
117 | 117 | <Compile Include="Parallels\Signal.cs" /> |
|
118 | 118 | <Compile Include="Parallels\SharedLock.cs" /> |
|
119 | 119 | <Compile Include="Diagnostics\ILogWriter.cs" /> |
|
120 | 120 | <Compile Include="Diagnostics\ListenerBase.cs" /> |
|
121 | 121 | <Compile Include="Parallels\BlockingQueue.cs" /> |
|
122 | 122 | <Compile Include="AbstractEvent.cs" /> |
|
123 | 123 | <Compile Include="AbstractPromise.cs" /> |
|
124 | 124 | <Compile Include="AbstractPromiseT.cs" /> |
|
125 | 125 | <Compile Include="FuncTask.cs" /> |
|
126 | 126 | <Compile Include="FuncTaskBase.cs" /> |
|
127 | 127 | <Compile Include="FuncTaskT.cs" /> |
|
128 | 128 | <Compile Include="ActionChainTaskBase.cs" /> |
|
129 | 129 | <Compile Include="ActionChainTask.cs" /> |
|
130 | 130 | <Compile Include="ActionChainTaskT.cs" /> |
|
131 | 131 | <Compile Include="FuncChainTaskBase.cs" /> |
|
132 | 132 | <Compile Include="FuncChainTask.cs" /> |
|
133 | 133 | <Compile Include="FuncChainTaskT.cs" /> |
|
134 | 134 | <Compile Include="ActionTaskBase.cs" /> |
|
135 | 135 | <Compile Include="ActionTask.cs" /> |
|
136 | 136 | <Compile Include="ActionTaskT.cs" /> |
|
137 | 137 | <Compile Include="ICancellationToken.cs" /> |
|
138 | 138 | <Compile Include="SuccessPromise.cs" /> |
|
139 | 139 | <Compile Include="SuccessPromiseT.cs" /> |
|
140 | 140 | <Compile Include="PromiseAwaiterT.cs" /> |
|
141 | 141 | <Compile Include="PromiseAwaiter.cs" /> |
|
142 | 142 | <Compile Include="Components\ComponentContainer.cs" /> |
|
143 | 143 | <Compile Include="Components\Disposable.cs" /> |
|
144 | 144 | <Compile Include="Components\DisposablePool.cs" /> |
|
145 | 145 | <Compile Include="Components\ObjectPool.cs" /> |
|
146 | 146 | <Compile Include="Components\ServiceLocator.cs" /> |
|
147 | 147 | <Compile Include="Components\IInitializable.cs" /> |
|
148 | 148 | <Compile Include="TaskController.cs" /> |
|
149 | 149 | <Compile Include="Components\App.cs" /> |
|
150 | 150 | <Compile Include="Components\IRunnable.cs" /> |
|
151 | 151 | <Compile Include="Components\ExecutionState.cs" /> |
|
152 | 152 | <Compile Include="Components\RunnableComponent.cs" /> |
|
153 | 153 | <Compile Include="Components\IFactory.cs" /> |
|
154 | 154 | <Compile Include="Automaton\EnumAlphabet.cs" /> |
|
155 | 155 | <Compile Include="Automaton\IAlphabet.cs" /> |
|
156 | 156 | <Compile Include="Automaton\ParserException.cs" /> |
|
157 | 157 | <Compile Include="Automaton\IndexedAlphabetBase.cs" /> |
|
158 | 158 | <Compile Include="Automaton\IAlphabetBuilder.cs" /> |
|
159 | 159 | <Compile Include="Automaton\RegularExpressions\AltToken.cs" /> |
|
160 | 160 | <Compile Include="Automaton\RegularExpressions\BinaryToken.cs" /> |
|
161 | 161 | <Compile Include="Automaton\RegularExpressions\CatToken.cs" /> |
|
162 | 162 | <Compile Include="Automaton\DFAConst.cs" /> |
|
163 | <Compile Include="Automaton\RegularExpressions\Grammar.cs" /> | |
|
164 | 163 | <Compile Include="Automaton\RegularExpressions\StarToken.cs" /> |
|
165 | 164 | <Compile Include="Automaton\RegularExpressions\SymbolToken.cs" /> |
|
166 | 165 | <Compile Include="Automaton\RegularExpressions\EmptyToken.cs" /> |
|
167 | <Compile Include="Automaton\RegularExpressions\EndToken.cs" /> | |
|
168 | 166 | <Compile Include="Automaton\RegularExpressions\Token.cs" /> |
|
169 | 167 | <Compile Include="Automaton\RegularExpressions\IVisitor.cs" /> |
|
170 | 168 | <Compile Include="Automaton\AutomatonTransition.cs" /> |
|
171 | 169 | <Compile Include="Formats\JSON\JSONElementContext.cs" /> |
|
172 | 170 | <Compile Include="Formats\JSON\JSONElementType.cs" /> |
|
173 | 171 | <Compile Include="Formats\JSON\JSONGrammar.cs" /> |
|
174 | 172 | <Compile Include="Formats\JSON\JSONParser.cs" /> |
|
175 | 173 | <Compile Include="Formats\JSON\JSONScanner.cs" /> |
|
176 | 174 | <Compile Include="Formats\JSON\JsonTokenType.cs" /> |
|
177 | 175 | <Compile Include="Formats\JSON\JSONWriter.cs" /> |
|
178 | 176 | <Compile Include="Formats\JSON\JSONXmlReader.cs" /> |
|
179 | 177 | <Compile Include="Formats\JSON\JSONXmlReaderOptions.cs" /> |
|
180 | 178 | <Compile Include="Formats\JSON\StringTranslator.cs" /> |
|
181 | 179 | <Compile Include="Automaton\MapAlphabet.cs" /> |
|
182 | 180 | <Compile Include="Automaton\DummyAlphabet.cs" /> |
|
183 | 181 | <Compile Include="Formats\CharAlphabet.cs" /> |
|
184 | 182 | <Compile Include="Formats\ByteAlphabet.cs" /> |
|
185 | 183 | <Compile Include="Automaton\IDFATable.cs" /> |
|
186 | 184 | <Compile Include="Automaton\IDFATableBuilder.cs" /> |
|
187 | 185 | <Compile Include="Automaton\DFATable.cs" /> |
|
188 | 186 | <Compile Include="Automaton\RegularExpressions\RegularDFA.cs" /> |
|
189 | 187 | <Compile Include="Automaton\RegularExpressions\RegularExpressionVisitor.cs" /> |
|
190 | 188 | <Compile Include="Automaton\RegularExpressions\ITaggedDFABuilder.cs" /> |
|
191 | 189 | <Compile Include="Formats\TextScanner.cs" /> |
|
192 | 190 | <Compile Include="Formats\StringScanner.cs" /> |
|
193 | 191 | <Compile Include="Formats\ReaderScanner.cs" /> |
|
194 | 192 | <Compile Include="Formats\ScannerContext.cs" /> |
|
193 | <Compile Include="Formats\Grammar.cs" /> | |
|
194 | <Compile Include="Automaton\RegularExpressions\EndTokenT.cs" /> | |
|
195 | <Compile Include="Automaton\RegularExpressions\EndToken.cs" /> | |
|
196 | <Compile Include="Automaton\RegularExpressions\IVisitorT.cs" /> | |
|
195 | 197 | </ItemGroup> |
|
196 | 198 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
197 | 199 | <ItemGroup /> |
|
198 | 200 | <ProjectExtensions> |
|
199 | 201 | <MonoDevelop> |
|
200 | 202 | <Properties> |
|
201 | 203 | <Policies> |
|
202 | 204 | <CSharpFormattingPolicy IndentSwitchBody="True" NamespaceBraceStyle="EndOfLine" ClassBraceStyle="EndOfLine" InterfaceBraceStyle="EndOfLine" StructBraceStyle="EndOfLine" EnumBraceStyle="EndOfLine" MethodBraceStyle="EndOfLine" ConstructorBraceStyle="EndOfLine" DestructorBraceStyle="EndOfLine" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" NewLineBeforeConstructorInitializerColon="NewLine" NewLineAfterConstructorInitializerColon="SameLine" BeforeIndexerDeclarationBracket="False" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" SpacesBeforeBrackets="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" /> |
|
203 | 205 | <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" /> |
|
204 | 206 | <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" /> |
|
205 | 207 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" /> |
|
206 | 208 | <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" /> |
|
207 | 209 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" /> |
|
208 | 210 | <NameConventionPolicy> |
|
209 | 211 | <Rules> |
|
210 | 212 | <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
211 | 213 | <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
212 | 214 | <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
213 | 215 | <RequiredPrefixes> |
|
214 | 216 | <String>I</String> |
|
215 | 217 | </RequiredPrefixes> |
|
216 | 218 | </NamingRule> |
|
217 | 219 | <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
218 | 220 | <RequiredSuffixes> |
|
219 | 221 | <String>Attribute</String> |
|
220 | 222 | </RequiredSuffixes> |
|
221 | 223 | </NamingRule> |
|
222 | 224 | <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
223 | 225 | <RequiredSuffixes> |
|
224 | 226 | <String>EventArgs</String> |
|
225 | 227 | </RequiredSuffixes> |
|
226 | 228 | </NamingRule> |
|
227 | 229 | <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
228 | 230 | <RequiredSuffixes> |
|
229 | 231 | <String>Exception</String> |
|
230 | 232 | </RequiredSuffixes> |
|
231 | 233 | </NamingRule> |
|
232 | 234 | <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
233 | 235 | <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" /> |
|
234 | 236 | <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
235 | 237 | <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" /> |
|
236 | 238 | <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
237 | 239 | <RequiredPrefixes> |
|
238 | 240 | <String>m_</String> |
|
239 | 241 | </RequiredPrefixes> |
|
240 | 242 | </NamingRule> |
|
241 | 243 | <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True"> |
|
242 | 244 | <RequiredPrefixes> |
|
243 | 245 | <String>_</String> |
|
244 | 246 | </RequiredPrefixes> |
|
245 | 247 | </NamingRule> |
|
246 | 248 | <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
247 | 249 | <RequiredPrefixes> |
|
248 | 250 | <String>m_</String> |
|
249 | 251 | </RequiredPrefixes> |
|
250 | 252 | </NamingRule> |
|
251 | 253 | <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
252 | 254 | <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
253 | 255 | <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
254 | 256 | <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
255 | 257 | <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
256 | 258 | <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
257 | 259 | <RequiredPrefixes> |
|
258 | 260 | <String>T</String> |
|
259 | 261 | </RequiredPrefixes> |
|
260 | 262 | </NamingRule> |
|
261 | 263 | </Rules> |
|
262 | 264 | </NameConventionPolicy> |
|
263 | 265 | </Policies> |
|
264 | 266 | </Properties> |
|
265 | 267 | </MonoDevelop> |
|
266 | 268 | </ProjectExtensions> |
|
267 | 269 | <ItemGroup> |
|
268 | 270 | <Folder Include="Components\" /> |
|
269 | 271 | <Folder Include="Automaton\RegularExpressions\" /> |
|
270 | 272 | <Folder Include="Formats\" /> |
|
271 | 273 | <Folder Include="Formats\JSON\" /> |
|
272 | 274 | </ItemGroup> |
|
273 | 275 | </Project> No newline at end of file |
@@ -1,123 +1,128 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Text.RegularExpressions; |
|
6 | 6 | using System.Diagnostics; |
|
7 | 7 | |
|
8 | 8 | namespace Implab |
|
9 | 9 | { |
|
10 | 10 | public static class Safe |
|
11 | 11 | { |
|
12 | 12 | public static void ArgumentAssert(bool condition, string paramName) { |
|
13 | 13 | if (!condition) |
|
14 | 14 | throw new ArgumentException("The parameter is invalid", paramName); |
|
15 | 15 | } |
|
16 | 16 | |
|
17 | 17 | public static void ArgumentMatch(string value, string paramName, Regex rx) { |
|
18 | 18 | if (rx == null) |
|
19 | 19 | throw new ArgumentNullException("rx"); |
|
20 | 20 | if (!rx.IsMatch(value)) |
|
21 | 21 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | public static void ArgumentNotEmpty(string value, string paramName) { |
|
25 | 25 | if (String.IsNullOrEmpty(value)) |
|
26 | 26 | throw new ArgumentException("The parameter can't be empty", paramName); |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | public static void ArgumentNotEmpty<T>(T[] value, string paramName) { |
|
30 | 30 | if (value == null || value.Length == 0) |
|
31 | 31 | throw new ArgumentException("The array must be not emty", paramName); |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | public static void ArgumentNotNull(object value, string paramName) { |
|
35 | 35 | if (value == null) |
|
36 | 36 | throw new ArgumentNullException(paramName); |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | public static void ArgumentInRange(int value, int min, int max, string paramName) { |
|
40 | 40 | if (value < min || value > max) |
|
41 | 41 | throw new ArgumentOutOfRangeException(paramName); |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | public static void ArgumentOfType(object value, Type type, string paramName) { | |
|
45 | if (!type.IsInstanceOfType(value)) | |
|
46 | throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName); | |
|
47 | } | |
|
48 | ||
|
44 | 49 | public static void Dispose(params IDisposable[] objects) { |
|
45 | 50 | foreach (var d in objects) |
|
46 | 51 | if (d != null) |
|
47 | 52 | d.Dispose(); |
|
48 | 53 | } |
|
49 | 54 | |
|
50 | 55 | public static void Dispose(params object[] objects) { |
|
51 | 56 | foreach (var obj in objects) { |
|
52 | 57 | var d = obj as IDisposable; |
|
53 | 58 | if (d != null) |
|
54 | 59 | d.Dispose(); |
|
55 | 60 | } |
|
56 | 61 | } |
|
57 | 62 | |
|
58 | 63 | public static void Dispose(object obj) { |
|
59 | 64 | var d = obj as IDisposable; |
|
60 | 65 | if (d != null) |
|
61 | 66 | d.Dispose(); |
|
62 | 67 | } |
|
63 | 68 | |
|
64 | 69 | [DebuggerStepThrough] |
|
65 | 70 | public static IPromise<T> WrapPromise<T>(Func<T> action) { |
|
66 | 71 | ArgumentNotNull(action, "action"); |
|
67 | 72 | |
|
68 | 73 | var p = new Promise<T>(); |
|
69 | 74 | try { |
|
70 | 75 | p.Resolve(action()); |
|
71 | 76 | } catch (Exception err) { |
|
72 | 77 | p.Reject(err); |
|
73 | 78 | } |
|
74 | 79 | |
|
75 | 80 | return p; |
|
76 | 81 | } |
|
77 | 82 | |
|
78 | 83 | [DebuggerStepThrough] |
|
79 | 84 | public static IPromise WrapPromise(Action action) { |
|
80 | 85 | ArgumentNotNull(action, "action"); |
|
81 | 86 | |
|
82 | 87 | var p = new Promise(); |
|
83 | 88 | try { |
|
84 | 89 | action(); |
|
85 | 90 | p.Resolve(); |
|
86 | 91 | } catch (Exception err) { |
|
87 | 92 | p.Reject(err); |
|
88 | 93 | } |
|
89 | 94 | |
|
90 | 95 | return p; |
|
91 | 96 | } |
|
92 | 97 | |
|
93 | 98 | [DebuggerStepThrough] |
|
94 | 99 | public static IPromise InvokePromise(Func<IPromise> action) { |
|
95 | 100 | ArgumentNotNull(action, "action"); |
|
96 | 101 | |
|
97 | 102 | try { |
|
98 | 103 | var p = action(); |
|
99 | 104 | if (p == null) { |
|
100 | 105 | var d = new Promise(); |
|
101 | 106 | d.Reject(new Exception("The action returned null")); |
|
102 | 107 | p = d; |
|
103 | 108 | } |
|
104 | 109 | return p; |
|
105 | 110 | } catch (Exception err) { |
|
106 | 111 | var p = new Promise(); |
|
107 | 112 | p.Reject(err); |
|
108 | 113 | return p; |
|
109 | 114 | } |
|
110 | 115 | } |
|
111 | 116 | |
|
112 | 117 | [DebuggerStepThrough] |
|
113 | 118 | public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { |
|
114 | 119 | ArgumentNotNull(action, "action"); |
|
115 | 120 | |
|
116 | 121 | try { |
|
117 | 122 | return action() ?? Promise<T>.FromException(new Exception("The action returned null")); |
|
118 | 123 | } catch (Exception err) { |
|
119 | 124 | return Promise<T>.FromException(err); |
|
120 | 125 | } |
|
121 | 126 | } |
|
122 | 127 | } |
|
123 | 128 | } |
|
1 | NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now