From: handa Date: Thu, 22 Oct 2009 08:15:58 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2191c6e2bd1fd2f13744a6a42f7004b43f38b5d2;p=m17n%2Fm17n-lib-cs.git *** empty log message *** --- diff --git a/MInputMethod.cs b/MInputMethod.cs index 8bd03c4..0a41c18 100644 --- a/MInputMethod.cs +++ b/MInputMethod.cs @@ -352,6 +352,9 @@ namespace M17N.Input internal class KeySeq : Xex.TermValue { + private static Xex.Symbol name = "keyseq"; + public static Xex.Symbol Name { get { return name; } } + public List keyseq = new List (); public override Xex.TermValue Clone () @@ -401,7 +404,7 @@ namespace M17N.Input } } - public static Xex.TermValue parser (Xex.Domain domain, XmlNode node) + public static Xex.TermValue Parser (Xex.Domain domain, XmlNode node) { Xex.Term term = new Xex.Term (domain, node.FirstChild).Eval (domain); return (term.IsStr ? new KeySeq ((MText) term.Strval) @@ -468,7 +471,7 @@ namespace M17N.Input if (node.Name == "description") description = parse_description (node); else if (node.Name == "keyseq") - keys.Add ((KeySeq) KeySeq.parser (null, node)); + keys.Add ((KeySeq) KeySeq.Parser (null, node)); } } @@ -544,18 +547,21 @@ namespace M17N.Input internal abstract class Marker : Xex.TermValue { - private MSymbol name; + private static Xex.Symbol name = "marker"; + public static Xex.Symbol Name { get { return name; } } + + private MSymbol mname; - private Marker (MSymbol name) + private Marker (MSymbol mname) { - this.name = name; + this.mname = mname; } public abstract int Position (Context ic); public virtual void Mark (Context ic) { - throw new Exception ("Can't set predefined marker: " + name); + throw new Exception ("Can't set predefined marker: " + mname); } public virtual int CharAt (Context ic) { @@ -567,27 +573,27 @@ namespace M17N.Input public override string ToString () { - return "" + name.Name + ""; + return "" + mname + ""; } - public static Xex.TermValue parser (Xex.Domain domain, XmlNode node) + public static Xex.TermValue Parser (Xex.Domain domain, XmlNode node) { return Get ((MSymbol) node.InnerText); } public class Named : Marker { - public Named (MSymbol name) : base (name) { } + public Named (MSymbol mname) : base (mname) { } public override int Position (Context ic) { - MPlist p = ic.marker_positions.Find (name); + MPlist p = ic.marker_positions.Find (mname); return (p == null ? 0 : p.Integer); } public override void Mark (Context ic) { - ic.marker_positions.Put (name, ic.cursor_pos); + ic.marker_positions.Put (mname, ic.cursor_pos); } } @@ -632,10 +638,10 @@ namespace M17N.Input { private int pos; - public PredefinedAbsolute (MSymbol name) : base (name) + public PredefinedAbsolute (MSymbol mname) : base (mname) { - if (! int.TryParse (((string) name).Substring (1), out pos)) - throw new Exception ("Invalid marker name: " + name); + if (! int.TryParse (((string) mname).Substring (1), out pos)) + throw new Exception ("Invalid marker name: " + mname); } public override int Position (Context ic) @@ -648,10 +654,10 @@ namespace M17N.Input { private int distance; - public PredefinedSurround (MSymbol name) : base (name) + public PredefinedSurround (MSymbol mname) : base (mname) { if (! int.TryParse (((string) name).Substring (1), out distance)) - throw new Exception ("Invalid marker name: " + name); + throw new Exception ("Invalid marker name: " + mname); if (distance > 0) distance--; } @@ -687,24 +693,24 @@ namespace M17N.Input = new Predefined (']'); } - public static Marker Get (MSymbol name) + public static Marker Get (MSymbol mname) { - string str = name.Name; + string str = mname.Name; if (str[0] == '@') { Predefined pred; - if (predefineds.TryGetValue (name, out pred)) + if (predefineds.TryGetValue (mname, out pred)) return pred; if (str.Length == 1) - throw new Exception ("Invalid marker name: " + name); + throw new Exception ("Invalid marker name: " + mname); if (Char.IsDigit (str[1])) - return new PredefinedAbsolute (name); - if (str.Length == 2 || name == Mat_minus_zero + return new PredefinedAbsolute (mname); + if (str.Length == 2 || mname == Mat_minus_zero || ! (str[1] == '-' || str[1] == '+')) - throw new Exception ("Invalid marker name: " + name); - return new PredefinedSurround (name); + throw new Exception ("Invalid marker name: " + mname); + return new PredefinedSurround (mname); } - return new Named (name); + return new Named (mname); } } @@ -992,6 +998,9 @@ namespace M17N.Input internal class Selector : Xex.TermValue { + private static Xex.Symbol name = "selector"; + public static Xex.Symbol Name { get { return name; } } + static new Dictionary selectors; static Selector () @@ -1014,7 +1023,7 @@ namespace M17N.Input private Selector (char tag) { this.tag = tag; } - public static Xex.TermValue parser (Xex.Domain domain, XmlNode node) + public static Xex.TermValue Parser (Xex.Domain domain, XmlNode node) { return Get ((MSymbol) node.InnerText); } @@ -1246,9 +1255,9 @@ namespace M17N.Input static MInputMethod () { - im_domain.DefTerm ("keyseq", KeySeq.parser); - im_domain.DefTerm ("marker", Marker.parser); - im_domain.DefTerm ("selector", Selector.parser); + im_domain.DefType (typeof (KeySeq)); + im_domain.DefType (typeof (Marker)); + im_domain.DefType (typeof (Selector)); im_domain.DefSubr (Finsert, "insert", false, 1, 1); im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, -1); @@ -1768,7 +1777,7 @@ namespace M17N.Input XmlNode n = nd.FirstChild; if (n.Name != Qkeyseq) continue; - KeySeq keyseq = (KeySeq) KeySeq.parser (domain, n); + KeySeq keyseq = (KeySeq) KeySeq.Parser (domain, n); Xex.Term[] actions = Xex.ParseTerms (domain, n.NextSibling); map.entries.Add (new Map.Entry (domain, keyseq, actions)); } diff --git a/XmlExpr.cs b/XmlExpr.cs index d21a630..2bcf476 100644 --- a/XmlExpr.cs +++ b/XmlExpr.cs @@ -667,8 +667,8 @@ namespace System.Xml public object context; public int depth = 0; - internal Dictionary termtypes - = new Dictionary (); + internal Dictionary termtypes + = new Dictionary (); internal Dictionary functions = new Dictionary (); internal Dictionary variables @@ -684,7 +684,7 @@ namespace System.Xml public Domain (Symbol name, Domain parent, object context) : this (name) { - termtypes = new Dictionary (parent.termtypes); + termtypes = new Dictionary (parent.termtypes); functions = new Dictionary (parent.functions); variables = new Dictionary (parent.variables); this.context = context; @@ -745,27 +745,23 @@ namespace System.Xml } } - public void DefTerm (Symbol name, TermParser parser) - { - if (termtypes.ContainsKey (name) - || functions.ContainsKey (name)) - throw new Exception ("already defined: " + name); - termtypes[name] = new TermType (name, parser); - } - public void DefType (Type type) { if (! type.IsSubclassOf (typeof (TermValue))) throw new Exception ("Not a subclass of TermValue: " + type); BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic; FieldInfo finfo = type.GetField ("name", flags); - if (finfo == null) - throw new Exception ("No \"name\" field: " + type); - Type[] types = new Type[] { typeof (Domain), typeof (XmlNode), - typeof (TermValue) }; - MethodInfo minfo = type.GetMethod ("Parser"); - if (minfo == null) - throw new Exception ("No \"Parser\" method: " + type); + if (finfo == null || finfo.FieldType != typeof (Symbol)) + throw new Exception ("No \"name\" field of string type: " + type); + Symbol name = (Symbol) finfo.GetValue (null); + if (termtypes.ContainsKey (name) + || functions.ContainsKey (name)) + throw new Exception ("already defined: " + name); + Type[] types = new Type[] { typeof (Domain), typeof (XmlNode) }; + MethodInfo minfo = type.GetMethod ("Parser", types); + if (minfo == null || minfo.ReturnType != typeof (TermValue)) + throw new Exception ("No \"Parser\" method of correct type: " + type); + termtypes[name] = minfo; } public void DefSubr (Builtin builtin, string str, bool setvar, @@ -1124,8 +1120,7 @@ namespace System.Xml static Xexpression () { - basic.DefTerm ("funcall", Funcall.parser); - basic.DefTerm ("varref", Varref.parser); + basic.DefType (typeof (Varref)); basic.DefType (typeof (Funcall)); basic.DefSubr (Fset, "set", true, 1, 1, "="); @@ -1664,21 +1659,6 @@ namespace System.Xml public delegate TermValue TermParser (Domain domain, XmlNode node); - public class TermType - { - private readonly Symbol name; - private readonly TermParser parser; - - public TermType (Symbol name, TermParser parser) - { - this.name = name; - this.parser = parser; - } - - public Symbol Name { get { return name; } } - internal TermParser Parser { get { return parser; } } - } - public abstract class TermValue { public virtual Term Eval (Domain domain) { return new Term (this); } @@ -1687,6 +1667,9 @@ namespace System.Xml private class Varref : TermValue { + private static Symbol name = "varref"; + public static Symbol Name { get { return name; } } + private Symbol vname; private Variable vari; @@ -1699,7 +1682,7 @@ namespace System.Xml return vari.Value; } - internal static TermValue parser (Domain domain, XmlNode node) + public static TermValue Parser (Domain domain, XmlNode node) { return new Varref ((Symbol) node.Attributes[Qvname].Value); } @@ -1737,27 +1720,22 @@ namespace System.Xml public static TermValue Parser (Domain domain, XmlNode node) { - return null; - } + Symbol fname = node.Name; + XmlAttribute attr; - internal static TermValue parser (Domain domain, XmlNode node) - { - Symbol fname = node.Name; - XmlAttribute attr; - - if (fname == Qfuncall) - fname = node.Attributes[Qfname].Value; - Function func = domain.GetFunc (fname); - Variable vari; - attr = node.Attributes[Qvname]; - vari = attr == null ? null : domain.GetVar (attr.Value, true); - XmlNodeList nlist = node.ChildNodes; - int nargs = nlist.Count; - Term[] args = new Term[nargs]; - for (int i = 0; i < nargs; i++) - args[i] = new Term (domain, nlist[i]); - return new Funcall (func, vari, args); - } + if (fname == Qfuncall) + fname = node.Attributes[Qfname].Value; + Function func = domain.GetFunc (fname); + Variable vari; + attr = node.Attributes[Qvname]; + vari = attr == null ? null : domain.GetVar (attr.Value, true); + XmlNodeList nlist = node.ChildNodes; + int nargs = nlist.Count; + Term[] args = new Term[nargs]; + for (int i = 0; i < nargs; i++) + args[i] = new Term (domain, nlist[i]); + return new Funcall (func, vari, args); + } public override Term Eval (Domain domain) { @@ -1837,13 +1815,13 @@ namespace System.Xml } else { - TermType term_type; + MethodInfo minfo; - if (domain.termtypes.TryGetValue (name, out term_type)) - objval = term_type.Parser (domain, node); + if (domain.termtypes.TryGetValue (name, out minfo)) + objval = (TermValue) minfo.Invoke (null, new Object[] { domain, node}); else { - Funcall funcall = (Funcall) Funcall.parser (domain, node); + Funcall funcall = (Funcall) Funcall.Parser (domain, node); if (funcall.func is Function.Macro) { Term result = funcall.Eval (domain);