Пример #1
0
        public charconst(int line, int pos, string str, BekCharModes mode = BekCharModes.BV16)
        {
            this.line = line;
            this.pos = pos;

            IFormatProvider prov = new System.Globalization.NumberFormatInfo();

            string s;
            if (str.StartsWith("'"))
            {
                var str1 = str.Substring(1, str.Length - 2);
                if (str1.Length == 1)
                    val = (int)str1[0];
                else
                {
                    var unesc = System.Text.RegularExpressions.Regex.Unescape(str1);
                    if (unesc.Length != 1)
                        throw new BekParseException(string.Format("Illeagal character constant {0}", str));
                    val = (int)unesc[0];
                }
            }
            else if (str.StartsWith("0x")) //must be hexadecimal number
            {
                s = str.Substring(2, str.Length - 2);
                if (!int.TryParse(s, NumberStyles.AllowHexSpecifier, prov, out val))
                    throw new BekException(string.Format("Invalid hexadecimal numeral {0}", str));
            }
            else
            {
                s = str;
                if (!int.TryParse(s, out val)) //first try to parse as decimal numeral
                {
                    //we know at this point that the character constant string starts and ends with a single quote
                    string buffer = s.Substring(1, s.Length - 2);

                    if (mode != BekCharModes.BV32)
                    {
                        string intermediate = "";
                        try
                        {
                            intermediate = System.Text.RegularExpressions.Regex.Unescape(buffer);
                        }
                        catch (Exception e)
                        {
                            throw new BekException(string.Format("Illeagal character constant {0}", s), e);
                        }
                        if (intermediate.Length > 1)
                        {
                            throw new BekException(string.Format("Illeagal character constant {0}", s));
                        }
                        this.val = (int)intermediate[0];
                    }
                    else
                    {
                        try
                        {
                            string intermediate = System.Text.RegularExpressions.Regex.Unescape(buffer);
                            // extract unicode code points as integers
                            var e = StringInfo.GetTextElementEnumerator(intermediate);
                            if (e.MoveNext())
                            {
                                this.val = char.ConvertToUtf32(e.GetTextElement(), 0);
                            }
                            else
                            {
                                throw new Exception();
                            }
                            if (e.MoveNext())
                            {
                                throw new Exception();
                            }
                        }
                        catch (Exception e)
                        {
                            throw new BekException(string.Format("Illeagal character constant {0}", s), e);
                        }
                    }
                }
            }
        }
Пример #2
0
 public charconst(string str, BekCharModes mode = BekCharModes.BV16)
     : this(0, 0, str)
 {
 }