c41fda97d7cf1accfe9a67a8e253ef5efa9ce965
[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 plist = MSymbol.Of ("plist");
21     public static MSymbol integer = MSymbol.Of ("integer");
22
23     private MSymbol (string name)
24     {
25       Name = name;
26     }
27
28     public static MSymbol Of (string name)
29     {
30       lock (pool)
31         {
32           MSymbol sym;
33
34           if (! pool.TryGetValue (name, out sym))
35             {
36               sym = new MSymbol (name);
37               pool[name] = sym;
38             }
39           return sym;
40         }
41     }
42
43     public static MSymbol PropertyKey (string name)
44     {
45       MSymbol sym = MSymbol.Of (name);
46
47       if (sym.flags == null)
48         sym.flags = MProperty.Flags.None;;
49       return sym;
50     }
51
52     public static MSymbol PropertyKey (string name, MProperty.Flags flags)
53     {
54       MSymbol sym = MSymbol.Of (name);
55
56       if (sym.flags == null)
57         sym.flags = flags;
58       else if (sym.flags != flags)
59         throw new Exception ("Flags of PropertyKey mismatch");
60       return sym;
61     }
62
63     public override string ToString ()
64     {
65       string str = "";
66
67       foreach (char c in Name)
68         {
69           if (c == '\\' || c == ' ' || c == '\'' || c == '\"'
70               || c == '(' || c == ')')
71             str += "\\";
72           str += c;
73         }
74       return str;
75     }
76
77     public MPlist Find (MSymbol key)
78     {
79       return (Plist == null ? null : Plist.Find (key));
80     }
81
82     public object Get (MSymbol key)
83     {
84       return (Plist == null ? null : Plist.Get (key));
85     }
86
87     public object Put (MSymbol key, object val)
88     {
89       if (Plist == null)
90         Plist = new MPlist ();
91       return Plist.Put (key, val);
92     }
93   }
94 }