*** 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 ((flags & MProperty.Flags.BothSticky) != MProperty.Flags.None
57           && (flags & MProperty.Flags.Sensitive) != MProperty.Flags.None)
58         throw new Exception ("Sensitive property can't be sticky");
59       if (sym.flags == null)
60         sym.flags = flags;
61       else if (sym.flags != flags)
62         throw new Exception ("Flags of PropertyKey mismatch");
63       return sym;
64     }
65
66     public override string ToString ()
67     {
68       string str = "";
69
70       foreach (char c in Name)
71         {
72           if (c == '\\' || c == ' ' || c == '\'' || c == '\"'
73               || c == '(' || c == ')')
74             str += "\\";
75           str += c;
76         }
77       return str;
78     }
79
80     public MPlist Find (MSymbol key)
81     {
82       return (Plist == null ? null : Plist.Find (key));
83     }
84
85     public object Get (MSymbol key)
86     {
87       return (Plist == null ? null : Plist.Get (key));
88     }
89
90     public object Put (MSymbol key, object val)
91     {
92       if (Plist == null)
93         Plist = new MPlist ();
94       return Plist.Put (key, val);
95     }
96   }
97 }