Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/12/11 15:45:04 (13 years ago)
Author:
epitzer
Message:

#1622 Fix quote handling and simplify tokenizer in Calculator and provide better error message.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/Calculator.cs

    r6902 r6908  
    2525
    2626    private static readonly Regex TokenRegex =
    27       new Regex(@"[a-zA-Z0-9._]+|""([^""]|\\"")*""|'([^']|\\')*'|[-+*/<>^]|dup|swap|drop|log|true|false|if|==|not|isnull|null|ismatch|rename");
     27      new Regex(@"""(\\""|[^""])*""|'(\\'|[^'])*'|[^\s]+");
    2828
    2929    #endregion
     
    6161            stack.Push(d);
    6262          } else if (token.StartsWith("\"")) {
    63             stack.Push(GetVariableValue(variables, token.Substring(1, token.Length - 2).Replace(@"\""", @"""")));
     63            stack.Push(GetVariableValue(variables, token.Substring(1, token.Length - 2).Replace("\\\"", "\"")));
    6464          } else if (token.StartsWith("'")) {
    65             stack.Push(token.Substring(1, token.Length-2).Replace("\'", "'"));
     65            stack.Push(token.Substring(1, token.Length-2).Replace("\\'", "'"));
    6666          } else {
    6767            Apply(token, stack, variables);
     
    7070      } catch (Exception x) {
    7171        throw new Exception(string.Format(
    72           "Calculation Failed at token #{1}: '{2}' {0}current stack is: {0}{3}", Environment.NewLine,
    73           i, tokens[i], string.Join(Environment.NewLine, stack.Select(AsString))),
     72          "Calculation of '{1}'{0}failed at token #{2}: '{3}' {0}current stack is: {0}{4}", Environment.NewLine,
     73          Formula, i, tokens[i],
     74          string.Join(Environment.NewLine, stack.Select(AsString))),
    7475          x);
    7576      }
    76       if (stack.Count != 1) throw new Exception(string.Format("Invalid final evaluation stack size {0} (should be 1)", stack.Count));
     77      if (stack.Count != 1)
     78        throw new Exception(
     79          string.Format("Invalid final evaluation stack size {0} (should be 1) in formula '{1}'",
     80          stack.Count, Formula));
    7781      var result = stack.Pop();
    7882      if (result is string) return new StringValue((string)result);
Note: See TracChangeset for help on using the changeset viewer.