Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/Token.cs @ 14929

Last change on this file since 14929 was 14929, checked in by gkronber, 7 years ago

#2520 fixed unit tests for new persistence: loading & storing all samples

File size: 2.5 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;
23using HeuristicLab.Persistence;
24
25namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
26  [StorableType("3be004ef-813b-4389-9945-c084b5f234eb")]
27  internal enum TokenSymbol { LPAR, RPAR, SYMB, NUMBER };
28  internal class Token {
29    public static readonly Token LPAR = Token.Parse("(");
30    public static readonly Token RPAR = Token.Parse(")");
31
32    public TokenSymbol Symbol { get; set; }
33    public string StringValue { get; set; }
34    public double DoubleValue { get; set; }
35    public Token() { }
36
37    public override bool Equals(object obj) {
38      Token other = (obj as Token);
39      if (other == null) return false;
40      if (other.Symbol != Symbol) return false;
41      return other.StringValue == this.StringValue;
42    }
43
44    public override int GetHashCode() {
45      return Symbol.GetHashCode() & StringValue.GetHashCode();
46    }
47
48    public static Token Parse(string strToken) {
49      strToken = strToken.Trim();
50      Token t = new Token();
51      t.StringValue = strToken.Trim();
52      double temp;
53      if (strToken == "") {
54        t = null;
55      } else if (strToken == "(") {
56        t.Symbol = TokenSymbol.LPAR;
57      } else if (strToken == ")") {
58        t.Symbol = TokenSymbol.RPAR;
59      } else if (double.TryParse(strToken, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out temp)) {
60        t.Symbol = TokenSymbol.NUMBER;
61        t.DoubleValue = double.Parse(strToken, CultureInfo.InvariantCulture.NumberFormat);
62      } else {
63        t.Symbol = TokenSymbol.SYMB;
64        t.StringValue = t.StringValue.ToUpper();
65      }
66      return t;
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.