Escape chars in string literals (ExpressionEvaluator)

Dec 3, 2014 at 1:17 PM
Edited Dec 3, 2014 at 1:17 PM
When escaping characters in string literals in the Expression Evaluator, it seems to be "swallowing" too many characters. Furthermore, I can't fix this by changing StringOptions for the StringLiteral.

I am referring to the ExpressionEvaluator that is part of the dec. 2013 source code download.

  1. Why does "\d" evaluate to "d"? I would expect it to either fail or keep the backslash since d is not a valid character to escape.
  2. When setting StringOptions for the StringLiteral to only AllowsDoubledQuote, it still allows escaping of characters using backslash. I would have expected it to ignore backslashes in the string literal and instead only allow end quotes to be escaped by doubling them.
Is this a bug? Or is there a simple way to change this behaviour?
Dec 8, 2014 at 8:33 PM
It should, debug into StringLiteral.HandleSpecialEscape code - if there's no match for control char, it should post an error into error details of the token; see why it does not show up
Dec 9, 2014 at 2:42 PM
Edited Dec 9, 2014 at 3:56 PM
You are right; it identifies the invalid escape sequence. It reaches the following line:
details.Error = string.Format(Resources.ErrInvEscape, segment);
But then it just returns, and doesn't seem to use the error details anywhere.
If I add a line that examines the Error property and returns false in ConvertValue, it seems to fail as expected:
      if (Escapes.TryGetValue(first, out newFirst))
        arr[i] = newFirst + s.Substring(1);
      else {
        arr[i] = HandleSpecialEscape(arr[i], details);
        if (!string.IsNullOrEmpty(details.Error)) return false; // <--- ADDED THIS LINE
    }//for i
    value = string.Join(string.Empty, arr);
  }// if EscapeEnabled 
Would you consider this a correct fix?
Dec 9, 2014 at 3:16 PM
Edited Dec 9, 2014 at 3:16 PM
Also, I see now that I misunderstood the use of StringOptions. I can disable backslash-escape sequences by setting NoEscapes. Further, I can enable doubled end quotes by setting AllowsDoubledQuote:
var stringLit = new StringLiteral("string", "\"",
    StringOptions.NoEscapes | StringOptions.AllowsDoubledQuote);
This solves my problem in my second question, and the expression "\123" now correctly evaluates to \123.
Dec 9, 2014 at 3:59 PM
I have opened an issue (#9897) regarding the invalid escape sequence instead of continuing the discussion here.