Beispiel #1
0
 public RCToken(string text, RCTokenType type, int start, int index, int line, int lines)
 {
     Text  = text;
     Type  = type;
     Start = start;
     Index = index;
     Line  = line;
     Lines = lines;
 }
Beispiel #2
0
 protected static string MakeMessage(RCToken token, string details)
 {
     if (details != null && details != "")
     {
         return(string.Format("Invalid syntax around line {0} near the text '{1}'. {2}",
                              token.Line,
                              RCTokenType.EscapeControlChars(token.Text, '"'),
                              details));
     }
     else
     {
         return(string.Format("Invalid syntax around line {0} near the text '{1}'.",
                              token.Line,
                              RCTokenType.EscapeControlChars(token.Text, '"')));
     }
 }
Beispiel #3
0
        public override string IdShorthand(object scalar)
        {
            string id = scalar.ToString();

            if (id.Length > 0)
            {
                if (id[0] == '\'')
                {
                    if (id[id.Length - 1] == '\'')
                    {
                        return(id);
                    }
                    else
                    {
                        throw new Exception("Invalid id: " + id);
                    }
                }
            }
            else
            {
                return(id);
            }

            if ((id[0] >= '0') && (id[0] <= '9'))
            {
                return("'" + id + "'");
            }
            else
            {
                for (int i = 0; i < id.Length; ++i)
                {
                    if (!RCTokenType.IsIdentifierChar(id[i]))
                    {
                        return("'" + id + "'");
                    }
                }
            }
            return(id);
        }
Beispiel #4
0
        public static RCName GetName(string text)
        {
            if (text == null)
            {
                text = "";
            }
            string name    = null;
            bool   escaped = false;
            RCName result;

            lock (_lock)
            {
                if (!_names.TryGetValue(text, out result))
                {
                    if (text[0] == '\'')
                    {
                        if (text.Length == 1 || text[text.Length - 1] != '\'')
                        {
                            throw new Exception("Unmatched single quote in name: " + text);
                        }
                        // Remove quotes if not necessary
                        // They are necessary when the name begins with a number
                        if (text.Length > 1 && text[1] >= '0' && text[1] <= '9')
                        {
                            name    = text;
                            escaped = true;
                        }
                        for (int i = 1; i < text.Length - 1; ++i)
                        {
                            if (!RCTokenType.IsIdentifierChar(text[i]))
                            {
                                name    = text;
                                escaped = true;
                                break;
                            }
                        }
                        if (name == null)
                        {
                            name = text.Substring(1, text.Length - 2);
                        }
                    }
                    else if (text[0] >= '0' && text[0] <= '9')
                    {
                        name    = "'" + text + "'";
                        escaped = true;
                    }
                    else
                    {
                        for (int i = 0; i < text.Length; ++i)
                        {
                            // add quotes if necessary
                            if (!RCTokenType.IsIdentifierChar(text[i]))
                            {
                                name    = "'" + text + "'";
                                escaped = true;
                                break;
                            }
                        }
                        if (name == null)
                        {
                            name = text;
                        }
                    }
                    if (_names.TryGetValue(name, out result))
                    {
                        // this makes it a synonym for next time
                        _names.Add(text, result);
                        return(result);
                    }
                    else
                    {
                        result = new RCName(name, _names.Count, escaped);
                        _names.Add(result.Text, result);
                        _index.Write(result);
                        return(result);
                    }
                }
                else
                {
                    return(result);
                }
            }
        }
Beispiel #5
0
 public static string FormatScalar(string format, string scalar)
 {
     return("\"" + RCTokenType.EscapeControlChars(scalar.ToString(), '"') + "\"");
 }
Beispiel #6
0
 public override void ScalarToString(StringBuilder builder, string scalar)
 {
     builder.Append("\"");
     builder.Append(RCTokenType.EscapeControlChars(scalar.ToString(), '"'));
     builder.Append("\"");
 }