Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/Token.cs @ 18123

Last change on this file since 18123 was 18123, checked in by gkronber, 2 years ago

#3140: fixed parsing of Numbers in SymbolicExpressionImporter

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 {
26    LPAR, RPAR, SYMB, CONSTANT,
27    NUM, // num
28    EQ,  // =
29    LBRACKET, // <
30    RBRACKET  // >
31  };
32  internal class Token {
33    public static readonly Token LPAR = Token.Parse("(");
34    public static readonly Token RPAR = Token.Parse(")");
35    public static readonly Token LBRACKET = Token.Parse("<");
36    public static readonly Token RBRACKET = Token.Parse(">");
37    public static readonly Token EQ = Token.Parse("=");
38    public static readonly Token NUM = Token.Parse("num");
39
40    public TokenSymbol Symbol { get; set; }
41    public string StringValue { get; set; }
42    public double DoubleValue { get; set; }
43    public Token() { }
44
45    public override bool Equals(object obj) {
46      Token other = (obj as Token);
47      if (other == null) return false;
48      if (other.Symbol != Symbol) return false;
49      return other.StringValue == this.StringValue;
50    }
51
52    public override int GetHashCode() {
53      return Symbol.GetHashCode() & StringValue.GetHashCode();
54    }
55
56    public static Token Parse(string strToken) {
57      strToken = strToken.Trim();
58      var t = new Token();
59      t.StringValue = strToken.Trim();
60      if (strToken == "") {
61        t = null;
62      } else if (strToken == "(") {
63        t.Symbol = TokenSymbol.LPAR;
64      } else if (strToken == ")") {
65        t.Symbol = TokenSymbol.RPAR;
66      } else if (strToken == "<") {
67        t.Symbol = TokenSymbol.LBRACKET;
68      } else if (strToken == ">") {
69        t.Symbol = TokenSymbol.RBRACKET;
70      } else if (strToken == "=") {
71        t.Symbol = TokenSymbol.EQ;
72      } else if (strToken.ToLower() == "num") {
73        t.Symbol = TokenSymbol.NUM;
74      } else if (double.TryParse(strToken, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out var val)) {
75        t.Symbol = TokenSymbol.CONSTANT;
76        t.DoubleValue = val;
77      } else {
78        t.Symbol = TokenSymbol.SYMB;
79        t.StringValue = t.StringValue.ToUpper();
80      }
81      return t;
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.