Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/Token.cs @ 15440

Last change on this file since 15440 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 2.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Globalization;
23
24namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
25  internal enum TokenSymbol { LPAR, RPAR, SYMB, NUMBER };
26  internal class Token {
27    public static readonly Token LPAR = Token.Parse("(");
28    public static readonly Token RPAR = Token.Parse(")");
29
30    public TokenSymbol Symbol { get; set; }
31    public string StringValue { get; set; }
32    public double DoubleValue { get; set; }
33    public Token() { }
34
35    public override bool Equals(object obj) {
36      Token other = (obj as Token);
37      if (other == null) return false;
38      if (other.Symbol != Symbol) return false;
39      return other.StringValue == this.StringValue;
40    }
41
42    public override int GetHashCode() {
43      return Symbol.GetHashCode() & StringValue.GetHashCode();
44    }
45
46    public static Token Parse(string strToken) {
47      strToken = strToken.Trim();
48      Token t = new Token();
49      t.StringValue = strToken.Trim();
50      double temp;
51      if (strToken == "") {
52        t = null;
53      } else if (strToken == "(") {
54        t.Symbol = TokenSymbol.LPAR;
55      } else if (strToken == ")") {
56        t.Symbol = TokenSymbol.RPAR;
57      } else if (double.TryParse(strToken, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out temp)) {
58        t.Symbol = TokenSymbol.NUMBER;
59        t.DoubleValue = double.Parse(strToken, CultureInfo.InvariantCulture.NumberFormat);
60      } else {
61        t.Symbol = TokenSymbol.SYMB;
62        t.StringValue = t.StringValue.ToUpper();
63      }
64      return t;
65    }
66  }
67}
Note: See TracBrowser for help on using the repository browser.