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