From: handa Date: Thu, 1 Oct 2009 00:12:01 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=23cc9b572f2d4a5e1fe693a6b58978647e6191d4;p=m17n%2Fm17n-lib-cs.git *** empty log message *** --- diff --git a/MInputMethod.cs b/MInputMethod.cs index ee54f6d..918fea5 100644 --- a/MInputMethod.cs +++ b/MInputMethod.cs @@ -163,10 +163,8 @@ namespace M17N.Input return c | (uint) modifiers; } - public Key (uint c) - { - key = c; - } + public Key (uint c) { key = c; } + public Key (int c) { key = (uint) c; } public Key (uint c, KeyModifier modifiers) { @@ -350,35 +348,37 @@ namespace M17N.Input return 0; } - public KeySeq (XmlNode node) + public KeySeq (Xex.Term arg) { - XmlAttributeCollection acol = node.Attributes; - XmlNode n; - - if (acol != null && (n = acol["keys"]) != null) + if (arg.IsStr) { - foreach (char c in n.Value) - keyseq.Add (new Key ((uint) c)); - } + string str = arg.Strval; - for (node = node.FirstChild; node != null; node = node.NextSibling) + for (int i = 0; i < str.Length; i++) + keyseq.Add (new Key (str[i])); + } + else if (arg.IsList) { - if (node.Name == "key-event") - keyseq.Add (new Key ((MSymbol) node.InnerText)); - else - keyseq.Add (new Key (parse_integer (node.InnerText))); + List list = arg.Listval; + int len = list.Count; + + for (int i = 0; i < len; i++) + { + if (list[i].IsInt) + keyseq.Add (new Key (list[i].Intval)); + else if (list[i].IsStr) + keyseq.Add (new Key (list[i].Strval)); + else if (list[i].IsSymbol) + keyseq.Add (new Key ((string) list[i].Symval)); + else + throw new Exception ("Invalid key: " + list[i]); + } } } - public KeySeq (Xex.Term[] args) + public static Xex.TermValue New (Xex.Domain domain, XmlNode node) { - foreach (Xex.Term term in args) - { - if (term.IsSymbol) - keyseq.Add (new Key ((MSymbol) ((string) term.Symval))); - else - keyseq.Add (new Key (term.Strval)); - } + return new KeySeq (new Xex.Term (domain, node.FirstChild)); } public override string ToString () @@ -525,7 +525,10 @@ namespace M17N.Input if (node.Name == "description") description = parse_description (node); else if (node.Name == "keyseq") - keys.Add (new KeySeq (node)); + { + KeySeq ks = MInputMethod.KeySeq.New (domain, node); + keys.Add (ks); + } } } @@ -811,6 +814,8 @@ namespace M17N.Input static MInputMethod () { + im_domain.DefType ("keyseq", KeySeq.New); + im_domain.DefSubr (Finsert, "insert", true, 1, 1); im_domain.DefSubr (Finsert_candidates, "candidates", true, 1, -1); im_domain.DefSubr (Fdelete, "delete", true, 1, 1); @@ -827,7 +832,6 @@ namespace M17N.Input im_domain.DefSubr (Fshift, "shift", true, 1, 1); im_domain.DefSubr (Fmarker, "marker", true, 1, 1); im_domain.DefSubr (Fchar_at, "char-at", true, 1, 1); - im_domain.DefSubr (Fkeyseq, "keyseq", true, 1, -1); MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*"); List list = MDatabase.List (tag); @@ -1746,12 +1750,6 @@ namespace M17N.Input return args[0]; } - private static Xex.Term Fkeyseq (Xex.Domain domain, Xex.Variable vari, - Xex.Term[] args) - { - return new Xex.Term (new KeySeq (args)); - } - private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari, Xex.Term[] args) { diff --git a/XmlExpr.cs b/XmlExpr.cs index 6337449..4646dec 100644 --- a/XmlExpr.cs +++ b/XmlExpr.cs @@ -83,6 +83,7 @@ namespace System.Xml.Expression private static Symbol Nexpr = "expr"; + private static Symbol Nnull = ""; private static Symbol Nfuncall = "funcall"; private static Symbol Nvarref = "varref"; private static Symbol Ninteger = "integer"; @@ -264,7 +265,7 @@ namespace System.Xml.Expression { body = new Term[nterms]; for (nterms = 0; node != null; node = node.NextSibling, nterms++) - body[nterms] = new Term (node, domain); + body[nterms] = new Term (domain, node); } } @@ -616,6 +617,8 @@ namespace System.Xml.Expression public object context; public int depth = 0; + internal Dictionary termtypes + = new Dictionary (); internal Dictionary functions = new Dictionary (); internal Dictionary variables @@ -690,6 +693,11 @@ namespace System.Xml.Expression } } + public void DefType (Symbol name, NewObject new_object) + { + termtypes[name] = new TermType (name, new_object); + } + public void DefSubr (Builtin builtin, string str, bool setvar, int min_arg, int max_arg, params string[] aliases) { @@ -767,15 +775,10 @@ namespace System.Xml.Expression desc = null; if (node != null) { - XmlNode nn; - if (node.Name == "value") - nn = node.FirstChild; - else - nn = node; - Symbol type = nn.Name; + Symbol type = node.Name; XmlNodeList range_list = null; int nranges = 0; - string val = nn.InnerText; + string val = node.InnerText; node = node.NextSibling; if (node != null) @@ -989,6 +992,8 @@ namespace System.Xml.Expression static Xexpression () { + basic.DefType ("funcall", Funcall.New); + basic.DefSubr (Fset, "set", true, 1, 1, "="); basic.DefSubr (Fnot, "not", false, 1, 1, "!"); basic.DefSubr (Fadd, "add", true, 1, -1, "+"); @@ -1015,6 +1020,7 @@ namespace System.Xml.Expression basic.DefSubr (Feval, "eval", false, 1, 1); basic.DefSubr (Fbreak, "break", false, 0, 1); basic.DefSubr (Freturn, "return", false, 0, 1); + basic.DefSpecial (Fand, "and", 1, -1, "&&"); basic.DefSpecial (For, "or", 1, -1, "||"); basic.DefSpecial (Fprogn, "progn", 0, -1, "expr"); @@ -1533,6 +1539,20 @@ namespace System.Xml.Expression return (args.Length == 1 ? Zero : args[1]); } + public delegate TermValue NewObject (Domain domain, XmlNode node); + + public class TermType + { + public readonly Symbol type; + internal readonly NewObject New; + + public TermType (Symbol type, NewObject new_object) + { + this.type = type; + New = new_object; + } + } + public abstract class TermValue { public virtual Term Eval (Domain domain) { return new Term (this); } @@ -1552,28 +1572,6 @@ namespace System.Xml.Expression this.args = args; } - public Funcall (XmlNode node, Domain domain) - { - vari = null; - Symbol name = node.Name; - - if (name == Nfuncall) - name = node.Attributes[0].Value; - else if (node.Attributes[Nvname] != null) - vari = domain.GetVar (node.Attributes[Nvname].Value, true); - func = domain.GetFunc (name); - XmlNodeList nlist = node.ChildNodes; - int nargs = nlist.Count; - - if (nargs < func.min_arg - || (func.max_arg >= 0 && nargs > func.max_arg)) - throw new Exception ("Invalid number of arguments to: " - + name + " " + nargs); - args = new Term[nargs]; - for (int i = 0; i < nlist.Count; i++) - args[i] = new Term (nlist[i], domain); - } - public Funcall (Domain domain, Symbol fname, Term[] args) { func = domain.GetFunc (fname); @@ -1583,8 +1581,34 @@ namespace System.Xml.Expression public Funcall (Domain domain, Symbol fname, Symbol vname, Term[] args) { func = domain.GetFunc (fname); - vari = domain.GetVar (vname, true); + int nargs = args.Length; + if (nargs < func.min_arg + || (func.max_arg >= 0 && nargs > func.max_arg)) + throw new Exception ("Invalid number of arguments to: " + + fname + " " + nargs); this.args = args; + if (vname != Nnull) + vari = domain.GetVar (vname, true); + } + + internal static TermValue New (Domain domain, XmlNode node) + { + Symbol fname = node.Name; + Symbol vname = Nnull; + XmlAttribute attr; + + if (fname == Nfuncall) + fname = node.Attributes[Nfname].Value; + attr = node.Attributes[Nvname]; + if (attr != null) + vname = attr.Value; + + 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 (domain, fname, vname, args); } public override Term Eval (Domain domain) @@ -1629,7 +1653,7 @@ namespace System.Xml.Expression public Term (Term term) { intval = term.intval; objval = term.objval; } public Term (TermValue obj) { intval = 0; objval = obj; } - public Term (XmlNode node, Domain domain) + public Term (Domain domain, XmlNode node) { Symbol name = node.Name; @@ -1652,11 +1676,18 @@ namespace System.Xml.Expression List list = new List (); for (node = node.FirstChild; node != null; node = node.NextSibling) - list.Add (new Term (node, domain)); + list.Add (new Term (domain, node)); objval = list; } else - objval = new Funcall (node, domain); + { + TermType term_type; + + if (domain.termtypes.TryGetValue (name, out term_type)) + objval = term_type.New (domain, node); + else + objval = Funcall.New (domain, node); + } } } @@ -1864,7 +1895,7 @@ namespace System.Xml.Expression if (n.Name == Ndefun) domain.Defun (n, false); else if (n.Name != Ndefvar) - terms[i++]= new Term (n, domain); + terms[i++]= new Term (domain, n); } } diff --git a/input.txt b/input.txt index 628c73a..a1ba964 100644 --- a/input.txt +++ b/input.txt @@ -55,10 +55,10 @@ PREDEFINED += MIM-PREDEFEIND MIM-TERM = KEYSEQ | MARKER | SELECTOR -KEYSEQ = '' [ INTTERM | STRTERM | SYMTERM ] + '' +KEYSEQ = '' [ STRTERM | LISTTERM ] '' KEYSEQTERM = KEYSEQ | VAR (value is KEYSEQ) | FUNCALL (return KEYSEQ) -MARKER = '' SYMTERM '' +MARKER = '' SYMBOL '' MARKERTERM = MARKER | VAR (value is MARKER) | FUNCALL (return MARKER) SELECTOR = '' SYMTERM '' diff --git a/xex.xml b/xex.xml index 476c1ed..4462666 100644 --- a/xex.xml +++ b/xex.xml @@ -56,7 +56,7 @@ 1011 - + @@ -68,13 +68,4 @@ ?a?b 1 - 0 - - k - a - 1 - 4 - broken - -