*** 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 implicit operator MSymbol (string name)
30     {
31       return Of (name);
32     }
33
34     public static explicit operator string (MSymbol sym)
35     {
36       return sym.Name;
37     }
38
39     public static MSymbol Of (string name)
40     {
41       lock (pool)
42         {
43           MSymbol sym;
44
45           if (! pool.TryGetValue (name, out sym))
46             {
47               sym = new MSymbol (name);
48               pool[name] = sym;
49             }
50           return sym;
51         }
52     }
53
54     public static MSymbol PropertyKey (string name)
55     {
56       MSymbol sym = MSymbol.Of (name);
57
58       if (sym.flags == null)
59         sym.flags = MProperty.Flags.None;;
60       return sym;
61     }
62
63     public static MSymbol PropertyKey (string name, MProperty.Flags flags)
64     {
65       MSymbol sym = MSymbol.Of (name);
66
67       if ((flags & MProperty.Flags.BothSticky) != MProperty.Flags.None
68           && (flags & MProperty.Flags.Sensitive) != MProperty.Flags.None)
69         throw new Exception ("A property can't be both sticky and sensitve");
70       if (sym.flags == null)
71         sym.flags = flags;
72       else if (sym.flags != flags)
73         throw new Exception ("Flags of PropertyKey mismatch");
74       return sym;
75     }
76
77     public override string ToString ()
78     {
79       string str = "";
80
81       foreach (char c in Name)
82         {
83           if (c == '\\' || c == ' ' || c == '\'' || c == '\"'
84               || c == '(' || c == ')')
85             str += "\\";
86           str += c;
87         }
88       return str;
89     }
90
91     public MPlist Find (MSymbol key)
92     {
93       return (Plist == null ? null : Plist.Find (key));
94     }
95
96     public object Get (MSymbol key)
97     {
98       return (Plist == null ? null : Plist.Get (key));
99     }
100
101     public object Put (MSymbol key, object val)
102     {
103       if (Plist == null)
104         Plist = new MPlist ();
105       return Plist.Put (key, val);
106     }
107   }
108 }