*** empty log message ***
[m17n/m17n-lib-cs.git] / MSymbol.cs
1 using System;
2 using System.Collections.Generic;
3 using M17N.Core;
4
5 namespace M17N.Core
6 {
7   public sealed class MSymbol
8   {
9     private static Dictionary<string, MSymbol> pool
10       = new Dictionary<string, MSymbol> ();
11
12     public readonly string Name;
13     private MPlist Plist;
14     internal MProperty.Flags? flags;
15
16     public static MSymbol nil = MSymbol.Of ("nil");
17     public static MSymbol t = MSymbol.Of ("t");
18     public static MSymbol symbol = MSymbol.Of ("symbol");
19     public static MSymbol mtext = MSymbol.Of ("mtext");
20     public static MSymbol mstring = MSymbol.Of ("string");
21     public static MSymbol plist = MSymbol.Of ("plist");
22     public static MSymbol integer = MSymbol.Of ("integer");
23
24     private MSymbol (string name)
25     {
26       Name = name;
27     }
28
29     public static MSymbol Of (string name)
30     {
31       lock (pool)
32         {
33           MSymbol sym;
34
35           if (! pool.TryGetValue (name, out sym))
36             {
37               sym = new MSymbol (name);
38               pool[name] = sym;
39             }
40           return sym;
41         }
42     }
43
44     public static MSymbol PropertyKey (string name)
45     {
46       MSymbol sym = MSymbol.Of (name);
47
48       if (sym.flags == null)
49         sym.flags = MProperty.Flags.None;;
50       return sym;
51     }
52
53     public static MSymbol PropertyKey (string name, MProperty.Flags flags)
54     {
55       MSymbol sym = MSymbol.Of (name);
56
57       if ((flags & MProperty.Flags.BothSticky) != MProperty.Flags.None
58           && (flags & MProperty.Flags.Sensitive) != MProperty.Flags.None)
59         throw new Exception ("A property can't be both sticky and sensitve");
60       if (sym.flags == null)
61         sym.flags = flags;
62       else if (sym.flags != flags)
63         throw new Exception ("Flags of PropertyKey mismatch");
64       return sym;
65     }
66
67     public override string ToString ()
68     {
69       string str = "";
70
71       foreach (char c in Name)
72         {
73           if (c == '\\' || c == ' ' || c == '\'' || c == '\"'
74               || c == '(' || c == ')')
75             str += "\\";
76           str += c;
77         }
78       return str;
79     }
80
81     public MPlist Find (MSymbol key)
82     {
83       return (Plist == null ? null : Plist.Find (key));
84     }
85
86     public object Get (MSymbol key)
87     {
88       return (Plist == null ? null : Plist.Get (key));
89     }
90
91     public object Put (MSymbol key, object val)
92     {
93       if (Plist == null)
94         Plist = new MPlist ();
95       return Plist.Put (key, val);
96     }
97   }
98 }