*** 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 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 == '\"' || c == ':')
70             str += "\\";
71           str += c;
72         }
73       return str;
74     }
75
76     public MPlist Find (MSymbol key)
77     {
78       return (Plist == null ? null : Plist.Find (key));
79     }
80
81     public object Get (MSymbol key)
82     {
83       return (Plist == null ? null : Plist.Get (key));
84     }
85
86     public object Put (MSymbol key, object val)
87     {
88       if (Plist == null)
89         Plist = new MPlist ();
90       return Plist.Put (key, val);
91     }
92   }
93 }