Exemplo n.º 1
0
        //Extract the string content from lexeme, adjusts the escaped and double-end symbols
        protected override bool ConvertValue(CompoundTokenDetails details)
        {
            var value         = details.Body;
            var escapeEnabled = !details.IsSet((short)HereDocOptions.NoEscapes);

            //Fix all escapes
            if (escapeEnabled && value.IndexOf(EscapeChar) >= 0)
            {
                details.Flags |= (int)StringFlagsInternal.HasEscapes;
                var arr        = value.Split(EscapeChar);
                var ignoreNext = false;
                //we skip the 0 element as it is not preceeded by "\"
                for (var i = 1; i < arr.Length; i++)
                {
                    if (ignoreNext)
                    {
                        ignoreNext = false;
                        continue;
                    }
                    var s = arr[i];
                    if (string.IsNullOrEmpty(s))
                    {
                        //it is "\\" - escaped escape symbol.
                        arr[i]     = @"\";
                        ignoreNext = true;
                        continue;
                    }
                    //The char is being escaped is the first one; replace it with char in Escapes table
                    var first = s[0];
                    if (Escapes.TryGetValue(first, out char newFirst))
                    {
                        arr[i] = newFirst + s.Substring(1);
                    }
                    else
                    {
                        arr[i] = HandleSpecialEscape(arr[i], details);
                    }
                }
                value = string.Join(string.Empty, arr);
            }

            details.TypeCodes = new[] { TypeCode.String };
            details.Value     = value;

            return(true);
        }
        }     //method

        //Extract the string content from lexeme, adjusts the escaped and double-end symbols
        protected override bool ConvertValue(CompoundTokenDetails details)
        {
            string value         = details.Body;
            bool   escapeEnabled = !details.IsSet((short)StringOptions.NoEscapes);

            //Fix all escapes
            if (escapeEnabled && value.IndexOf(EscapeChar) >= 0)
            {
                details.Flags |= (int)StringFlagsInternal.HasEscapes;
                string[] arr        = value.Split(EscapeChar);
                bool     ignoreNext = false;
                //we skip the 0 element as it is not preceeded by "\"
                for (int i = 1; i < arr.Length; i++)
                {
                    if (ignoreNext)
                    {
                        ignoreNext = false;
                        continue;
                    }
                    string s = arr[i];
                    if (string.IsNullOrEmpty(s))
                    {
                        //it is "\\" - escaped escape symbol.
                        arr[i]     = @"\";
                        ignoreNext = true;
                        continue;
                    }
                    //The char is being escaped is the first one; replace it with char in Escapes table
                    char first = s[0];
                    char newFirst;
                    if (Escapes.TryGetValue(first, out newFirst))
                    {
                        arr[i] = newFirst + s.Substring(1);
                    }
                    else
                    {
                        arr[i] = HandleSpecialEscape(arr[i], details);
                    } //else
                }     //for i
                value = string.Join(string.Empty, arr);
            }         // if EscapeEnabled

            //Check for doubled end symbol
            string endSymbol = details.EndSymbol;

            if (details.IsSet((short)StringOptions.AllowsDoubledQuote) && value.IndexOf(endSymbol) >= 0)
            {
                value = value.Replace(endSymbol + endSymbol, endSymbol);
            }

            if (details.IsSet((short)StringOptions.IsChar))
            {
                if (value.Length != 1)
                {
                    details.Error = Resources.ErrBadChar; //"Invalid length of char literal - should be a single character.";
                    return(false);
                }
                details.Value = value[0];
            }
            else
            {
                details.TypeCodes = new TypeCode[] { TypeCode.String };
                details.Value     = value;
            }
            return(true);
        }
Exemplo n.º 3
0
        }     //method

        //Extract the string content from lexeme, adjusts the escaped and double-end symbols
        protected override bool ConvertValue(ScanDetails details)
        {
            string value         = details.Body;
            bool   escapeEnabled = !details.IsSet(ScanFlags.DisableEscapes);

            //Fix all escapes
            if (escapeEnabled && value.IndexOf(EscapeChar) >= 0)
            {
                details.Flags |= ScanFlags.HasEscapes;
                string[] arr        = value.Split(EscapeChar);
                bool     ignoreNext = false;
                //we skip the 0 element as it is not preceeded by "\"
                for (int i = 1; i < arr.Length; i++)
                {
                    if (ignoreNext)
                    {
                        ignoreNext = false;
                        continue;
                    }
                    string s = arr[i];
                    if (string.IsNullOrEmpty(s))
                    {
                        //it is "\\" - escaped escape symbol.
                        arr[i]     = @"\";
                        ignoreNext = true;
                        continue;
                    }
                    //The char is being escaped is the first one; replace it with char in Escapes table
                    char first = s[0];
                    char newFirst;
                    if (Escapes.TryGetValue(first, out newFirst))
                    {
                        arr[i] = newFirst + s.Substring(1);
                    }
                    else
                    {
                        arr[i] = HandleSpecialEscape(arr[i], details);
                    } //else
                }     //for i
                value = string.Join(string.Empty, arr);
            }         // if EscapeEnabled

            //Check for doubled end symbol
            string startS = details.ControlSymbol;

            if (details.IsSet(ScanFlags.AllowDoubledQuote) && value.IndexOf(startS) >= 0)
            {
                value = value.Replace(startS + startS, startS);
            }

            if (details.IsSet(ScanFlags.IsChar))
            {
                details.TypeCodes = new TypeCode[] { TypeCode.Char }
            }
            ;
            //Check char length - must be exactly 1
            if (details.TypeCodes[0] == TypeCode.Char && value.Length != 1)
            {
                details.Error = "Invalid length of char literal - should be 1.";
                return(false);
            }

            details.Value = (details.TypeCodes[0] == TypeCode.Char ? (object)value[0] : value);
            return(true);

            //TODO: Investigate unescaped linebreak, with  Flags == BnfFlags.StringAllowLineBreak | BnfFlags.StringLineBreakEscaped
            //      also investigate what happens in this case in Windows where default linebreak is "\r\n", not "\n"
        }