Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / Token.cs
1 //
2 // TokenType.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Jaroslaw Kowalski <jaak@jkowalski.net>
7 // 
8 // (C) 2006 Marek Sieradzki
9 // (C) 2004-2006 Jaroslaw Kowalski
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 using System;
31
32 namespace Microsoft.Build.BuildEngine {
33
34         internal class Token {
35         
36                 string          tokenValue;
37                 TokenType       tokenType;
38         
39                 public Token (string tokenValue, TokenType tokenType, int position)
40                 {
41                         this.tokenValue = tokenValue;
42                         this.tokenType = tokenType;
43                         this.Position = position + 1;
44                 }
45                 
46                 public string Value {
47                         get { return tokenValue; }
48                 }
49                 
50                 public TokenType Type {
51                         get { return tokenType; }
52                 }
53
54                 // this is 1-based
55                 public int Position {
56                         get; private set;
57                 }
58
59                 public static string TypeAsString (TokenType tokenType)
60                 {
61                         switch (tokenType) {
62                                 case TokenType.Item:return "@";
63                                 case TokenType.Property:return "$";
64                                 case TokenType.Metadata:return "%";
65                                 case TokenType.Transform:return "->";
66                                 case TokenType.Less:return "<";
67                                 case TokenType.Greater:return ">";
68                                 case TokenType.LessOrEqual:return "<=";
69                                 case TokenType.GreaterOrEqual:return ">=";
70                                 case TokenType.Equal:return "=";
71                                 case TokenType.NotEqual:return "!=";
72                                 case TokenType.LeftParen:return "(";
73                                 case TokenType.RightParen:return ")";
74                                 case TokenType.Dot:return ".";
75                                 case TokenType.Comma:return ",";
76                                 case TokenType.Not:return "!";
77                                 case TokenType.And:return "and";
78                                 case TokenType.Or:return "or";
79                                 case TokenType.Apostrophe:return "'";
80                                 default: return tokenType.ToString ();
81                         }
82                 }
83
84                 public override string ToString ()
85                 {
86                         if (tokenType == TokenType.EOF || tokenType == TokenType.BOF)
87                                 return String.Format ("{0} at character position {1}", tokenType.ToString (), Position);
88
89                         return String.Format ("\"{0}\" at character position {1}", tokenValue, Position);
90                 }
91         }
92         
93         internal enum TokenType {
94                 EOF,
95                 BOF,
96                 Number,
97                 String,
98                 //Keyword,
99                 Punct,
100                 WhiteSpace,
101                 Item,
102                 Property,
103                 Metadata,
104                 FunctionName,
105                 Transform,
106 //              LiteralSubExpression,
107
108                 FirstPunct,
109
110                 Less,
111                 Greater,
112                 LessOrEqual,
113                 GreaterOrEqual,
114                 Equal,
115                 NotEqual,
116                 LeftParen,
117                 RightParen,
118                 Dot,
119                 Comma,
120                 Not,
121                 And,
122                 Or,
123                 Apostrophe,
124                 
125                 LastPunct,
126                 Invalid,
127         }
128 }