File: CategoryExpression.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (152 lines) | stat: -rw-r--r-- 2,932 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections;
using NUnit.Core;
using NUnit.Core.Filters;

namespace NUnit.Util
{
	/// <summary>
	/// CategoryExpression parses strings representing boolean
	/// combinations of categories according to the following
	/// grammar:
	///   CategoryName ::= string not containing any of ',', '&', '+', '-'
	///   CategoryFilter ::= CategoryName | CategoryFilter ',' CategoryName
	///   CategoryPrimitive ::= CategoryFilter | '-' CategoryPrimitive
	///   CategoryTerm ::= CategoryPrimitive | CategoryTerm '&' CategoryPrimitive
	/// </summary>
	public class CategoryExpression
	{
		static readonly char[] ops = new char[] { ',', ';', '-', '|', '+', '(', ')' };

		private string text;
		private int next;
		private string token;

		private TestFilter filter;

		public CategoryExpression(string text) 
		{
			this.text =  text;
			this.next = 0;
		}

		public TestFilter Filter
		{
			get
			{
				if( filter == null )
				{
					filter = GetToken() == null
						? TestFilter.Empty
						: GetExpression();
				}

				return filter;
			}
		}

		private TestFilter GetExpression()
		{
			TestFilter term = GetTerm();
			if ( token != "|" )
				return term;

			OrFilter filter = new OrFilter( term );
			
			while ( token == "|" )
			{
				GetToken();
				filter.Add( GetTerm() );
			}

			return filter;
		}

		private TestFilter GetTerm()
		{
			TestFilter prim = GetPrimitive();
			if ( token != "+" && token != "-" )
				return prim;

			AndFilter filter = new AndFilter( prim );
			
			while ( token == "+"|| token == "-" )
			{
				string tok = token;
				GetToken();
				prim = GetPrimitive();
				filter.Add( tok == "-" ? new NotFilter( prim ) : prim );
			}

			return filter;
		}

		private TestFilter GetPrimitive()
		{
			if( token == "-" )
			{
				GetToken();
				return new NotFilter( GetPrimitive() );
			}
			else if( token == "(" )
			{
				GetToken();
				TestFilter expr = GetExpression();
				GetToken(); // Skip ')'
				return expr;
			}

			return GetCategoryFilter();
		}

		private CategoryFilter GetCategoryFilter()
		{
			CategoryFilter filter = new CategoryFilter( token );

			while( GetToken() == "," || token == ";" )
				filter.AddCategory( GetToken() );

			return filter;
		}

		public string GetToken()
		{
			SkipWhiteSpace();

			if ( EndOfText() ) 
				token = null;
			else if ( NextIsOperator() )
				token = text.Substring(next++, 1);
			else
			{
				int index2 = text.IndexOfAny( ops, next );
				if ( index2 < 0 ) index2 = text.Length;

				token = text.Substring( next, index2 - next ).TrimEnd();
				next = index2;
			}

			return token;
		}

		private void SkipWhiteSpace()
		{
			while( next < text.Length && Char.IsWhiteSpace( text[next] ) )
				++next;
		}

		private bool EndOfText()
		{
			return next >= text.Length;
		}

		private bool NextIsOperator()
		{
			foreach( char op in ops )
				if( op == text[next] )
					return true;

			return false;
		}
	}
}