[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Common / Tokenizer.cs
1 // ***********************************************************************
2 // Copyright (c) 2015 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30 using System.Text;
31
32 // Missing XML Docs
33 #pragma warning disable 1591
34
35 #if NUNIT_ENGINE
36 namespace NUnit.Engine
37 #else
38 namespace NUnit.Common
39 #endif
40 {
41     public enum TokenKind
42     {
43         Eof,
44         Word,
45         String,
46         Symbol
47     }
48
49     public class Token
50     {
51         public Token(TokenKind kind) : this(kind, string.Empty) { }
52
53         public Token(TokenKind kind, char ch) : this(kind, ch.ToString()) { }
54
55         public Token(TokenKind kind, string text)
56         {
57             Kind = kind;
58             Text = text;
59         }
60
61         public TokenKind Kind { get; private set; }
62
63         public string Text { get; private set; }
64
65         public int Pos { get; set; }
66
67         #region Equality Overrides
68
69         public override bool Equals(object obj)
70         {
71             return obj is Token && this == (Token)obj;
72         }
73
74         public override int GetHashCode()
75         {
76             return Text.GetHashCode();
77         }
78
79         public override string ToString()
80         {
81             return Text != null
82                 ? Kind.ToString() + ":" + Text
83                 : Kind.ToString();
84         }
85
86         public static bool operator ==(Token t1, Token t2)
87         {
88             bool t1Null = ReferenceEquals(t1, null);
89             bool t2Null = ReferenceEquals(t2, null);
90
91             if (t1Null && t2Null)
92                 return true;
93
94             if (t1Null || t2Null)
95                 return false;
96
97             return t1.Kind == t2.Kind && t1.Text == t2.Text;
98         }
99
100         public static bool operator !=(Token t1, Token t2)
101         {
102             return !(t1 == t2);
103         }
104
105         #endregion
106     }
107
108     /// <summary>
109     /// Tokenizer class performs lexical analysis for the TestSelectionParser.
110     /// It recognizes a very limited set of tokens: words, symbols and
111     /// quoted strings. This is sufficient for the simple DSL we use to
112     /// select which tests to run.
113     /// </summary>
114     public class Tokenizer
115     {
116         private string _input;
117         private int _index;
118
119         private const char EOF_CHAR = '\0';
120         private const string WORD_BREAK_CHARS = "=!()&|";
121         private readonly string[] DOUBLE_CHAR_SYMBOLS = new string[] { "==", "=~", "!=", "!~", "&&", "||" };
122
123         private Token _lookahead;
124
125         public Tokenizer(string input)
126         {
127             if (input == null)
128                 throw new ArgumentNullException("input");
129
130             _input = input;
131             _index = 0;
132         }
133
134         public Token LookAhead
135         {
136             get
137             {
138                 if (_lookahead == null)
139                     _lookahead = GetNextToken();
140
141                 return _lookahead;
142             }
143         }
144
145         public Token NextToken()
146         {
147             Token result = _lookahead ?? GetNextToken();
148             _lookahead = null;
149             return result;
150         }
151
152         private Token GetNextToken()
153         {
154             SkipBlanks();
155
156             var ch = NextChar;
157             int pos = _index;
158
159             switch (ch)
160             {
161                 case EOF_CHAR:
162                     return new Token(TokenKind.Eof) { Pos = pos };
163
164                 // Single char symbols
165                 case '(':
166                 case ')':
167                     GetChar();
168                     return new Token(TokenKind.Symbol, ch) { Pos = pos };
169
170                 // Possible double char symbols
171                 case '&':
172                 case '|':
173                 case '=':
174                 case '!':
175                     GetChar();
176                     foreach(string dbl in DOUBLE_CHAR_SYMBOLS)
177                         if (ch == dbl[0] && NextChar == dbl[1])
178                         {
179                             GetChar();
180                             return new Token(TokenKind.Symbol, dbl) { Pos = pos };
181                         }
182
183                     return new Token(TokenKind.Symbol, ch);
184
185                 case '"':
186                 case '\'':
187                 case '/':
188                     return GetString();
189
190                 default:
191                     return GetWord();
192             }
193         }
194
195         private bool IsWordChar(char c)
196         {
197             if (char.IsWhiteSpace(c) || c == EOF_CHAR)
198                 return false;
199
200             return WORD_BREAK_CHARS.IndexOf(c) < 0;
201         }
202
203         private Token GetWord()
204         {
205             var sb = new StringBuilder();
206             int pos = _index;
207
208             while (IsWordChar(NextChar))
209                 sb.Append(GetChar());
210
211             return new Token(TokenKind.Word, sb.ToString()) { Pos = pos };
212         }
213
214         private Token GetString()
215         {
216             var sb = new StringBuilder();
217             int pos = _index;
218
219             char quote = GetChar(); // Save the initial quote char
220
221             while (NextChar != EOF_CHAR)
222             {
223                 var ch = GetChar();
224                 if (ch == '\\')
225                     ch = GetChar();
226                 else if (ch == quote)
227                     break;
228                 sb.Append(ch);
229             }
230
231             return new Token(TokenKind.String, sb.ToString()) { Pos = pos };
232         }
233
234         /// <summary>
235         /// Get the next character in the input, consuming it.
236         /// </summary>
237         /// <returns>The next char</returns>
238         private char GetChar()
239         {
240             return _index < _input.Length ? _input[_index++] : EOF_CHAR;
241         }
242
243         /// <summary>
244         /// Peek ahead at the next character in input
245         /// </summary>
246         private char NextChar
247         {
248             get
249             {
250                 return _index < _input.Length ? _input[_index] : EOF_CHAR;
251             }
252         }
253
254         private void SkipBlanks()
255         {
256             while (char.IsWhiteSpace(NextChar))
257                 _index++;
258         }
259     }
260 }