From 166a1a78da9a3f710f35813ce870190ba671ba6b Mon Sep 17 00:00:00 2001 From: handa Date: Sat, 7 Nov 2009 07:05:28 +0000 Subject: [PATCH] *** empty log message *** --- XmlExpr.cs | 150 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 76 insertions(+), 74 deletions(-) diff --git a/XmlExpr.cs b/XmlExpr.cs index f9e4a86..f877772 100644 --- a/XmlExpr.cs +++ b/XmlExpr.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text; +using System.Text.RegularExpressions; using System.Xml; namespace System.Xml @@ -247,7 +248,7 @@ namespace System.Xml public abstract bool CheckValue (Term val); } - public class Int : Typed + public class IntType : Typed { private int[] range; @@ -267,7 +268,8 @@ namespace System.Xml return true; } - public Int (Domain domain, Symbol name, int n, string desc, int[] range) + public IntType (Domain domain, Symbol name, int n, string desc, + int[] range) : base (domain, name, new Term (n), desc) { if (range != null && range.Length % 2 == 1) @@ -293,13 +295,13 @@ namespace System.Xml public override Variable Clone (Domain domain) { - return new Int (domain, name, val.Intval, desc, range); + return new IntType (domain, name, val.Intval, desc, range); } public int[] Range { get { return range; } set { range = value; } } } - public class Str : Typed + public class StrType : Typed { private string[] range; @@ -319,7 +321,7 @@ namespace System.Xml return true; } - public Str (Domain domain, Symbol name, string str, string desc, + public StrType (Domain domain, Symbol name, string str, string desc, string[] range) : base (domain, name, new Term (str), desc) { @@ -333,9 +335,9 @@ namespace System.Xml { if (range == null) return true; - string str = val.Strval; - foreach (string s in range) - if (str == s) + Str str = val.Strval; + foreach (Str s in range) + if (str.Equals (s)) return true; } return false; @@ -343,13 +345,13 @@ namespace System.Xml public override Variable Clone (Domain domain) { - return new Str (domain, name, val.Strval, desc, range); + return new StrType (domain, name, val.Strval, desc, range); } public string[] Range { get { return range; } set { range = value; } } } - public class Sym : Typed + public class SymType : Typed { public Symbol[] range; @@ -369,8 +371,8 @@ namespace System.Xml return true; } - public Sym (Domain domain, Symbol name, Symbol sym, string desc, - Symbol[] range) + public SymType (Domain domain, Symbol name, Symbol sym, string desc, + Symbol[] range) : base (domain, name, new Term (sym), desc) { this.range = range; @@ -393,7 +395,7 @@ namespace System.Xml public override Variable Clone (Domain domain) { - return new Sym (domain, name, val.Symval, desc, range); + return new SymType (domain, name, val.Symval, desc, range); } public Symbol[] Range { get { return range; } set { range = value; } } @@ -867,7 +869,7 @@ namespace System.Xml if (variables.TryGetValue (name, out vari)) { - Variable.Int intvari = vari as Variable.Int; + Variable.IntType intvari = vari as Variable.IntType; if (intvari == null) throw new Error (Error.VariableTypeConflict, "Not an integer variable: {0}", name); @@ -883,7 +885,7 @@ namespace System.Xml } else { - vari = new Variable.Int (this, name, n, desc, range); + vari = new Variable.IntType (this, name, n, desc, range); variables[name] = vari; } return vari; @@ -896,7 +898,7 @@ namespace System.Xml if (variables.TryGetValue (name, out vari)) { - Variable.Str strvari = vari as Variable.Str; + Variable.StrType strvari = vari as Variable.StrType; if (strvari == null) throw new Error (Error.VariableTypeConflict, "Not a string variable: {0}", name); @@ -912,7 +914,7 @@ namespace System.Xml } else { - vari = new Variable.Str (this, name, str, desc, range); + vari = new Variable.StrType (this, name, str, desc, range); variables[name] = vari; } return vari; @@ -925,7 +927,7 @@ namespace System.Xml if (variables.TryGetValue (name, out vari)) { - Variable.Sym symvari = vari as Variable.Sym; + Variable.SymType symvari = vari as Variable.SymType; if (symvari == null) throw new Error (Error.VariableTypeConflict, "Not a symbol variable: {0}", name); @@ -941,7 +943,7 @@ namespace System.Xml } else { - vari = new Variable.Sym (this, name, sym, desc, range); + vari = new Variable.SymType (this, name, sym, desc, range); variables[name] = vari; } return vari; @@ -1318,28 +1320,19 @@ namespace System.Xml private static Term Fconcat (Domain domain, Variable vari, Term[] args) { - string str; - - if (vari == null) - str = ""; - else - str = vari.Value.Strval; + Str str = vari == null ? new Str ("") : vari.Value.Strval; foreach (Term arg in args) { if (arg.IsStr) - str += (string) arg.objval; + str.Insert (-1, arg.Strval); else if (arg.IsList) - foreach (Term term in (List) arg.objval) - str += (char) term.Intval; + foreach (Term term in arg.Listval) + str.Insert (-1, (char) term.Intval); else - str += (char) arg.Intval; + str.Insert (-1, (char) arg.Intval); } - - Term strterm = new Term (str); - if (vari != null) - vari.SetValue (strterm); - return strterm; + return vari == null ? new Term (str) : vari.Value; } private static Term Fnth (Domain domain, Variable vari, Term[] args) @@ -1347,27 +1340,16 @@ namespace System.Xml Term result; if (args[1].IsStr) - { - result.intval = ((string) args[1].objval)[args[0].Intval]; - result.objval = null; - } - else if (args[1].IsList) - { - result = ((List) args[1].objval)[args[0].Intval]; - } - else - throw new Error (Error.WrongType, - "Not a string nor a list: {0}", args[1]); - return result; + return new Term (args[1].Strval.Nth (args[0].Intval)); + if (args[1].IsList) + return args[1].Listval[args[0].Intval]; + throw new Error (Error.WrongType, + "Not a string nor a list: {0}", args[1]); } private static Term Fcopy (Domain domain, Variable vari, Term[] args) { - Term result; - - result.intval = 0; - result.objval = new List (args[0].Listval); - return result; + return args[0].Clone (); } private static Term Fins (Domain domain, Variable vari, Term[] args) @@ -1375,8 +1357,7 @@ namespace System.Xml if (vari == null) throw new Error (Error.NoVariableName, "No variable name to set"); if (vari.Value.IsStr) - vari.Value - = vari.Value.Strval.Insert (args[0].Intval, args[1].Strval); + vari.Value.Strval.Insert (args[0].Intval, args[1].Strval); else if (vari.Value.IsList) vari.Value.Listval.InsertRange (args[0].Intval, args[1].Listval); else @@ -1389,17 +1370,15 @@ namespace System.Xml { if (vari == null) throw new Error (Error.NoVariableName, "No variable name to set"); - Term term = vari.Value; - if (term.IsStr) - term.objval = term.Strval.Remove (args[0].Intval, - args[1].Intval - args[0].Intval); + if (vari.Value.IsStr) + vari.Value.Strval.Delete (args[0].Intval, args[1].Intval); else if (vari.Value.IsList) vari.Value.Listval.RemoveRange (args[0].Intval, - args[1].Intval - args[0].Intval); + args[1].Intval - args[0].Intval); else throw new Error (Error.WrongType, "Not a string nor a list: {0}", vari.Value); - return term; + return vari.Value; } private static Term Fand (Domain domain, Variable vari, Term[] args) @@ -1892,19 +1871,46 @@ namespace System.Xml } } - internal class CharSeq + public class Str { private string str; private Regex regex; - public CharSeq (string str) { this.str = str; } - public Insert (int index, string str) + public Str (string str) { this.str = str; } + + public Str Clone (Str str) { return new Str (str.str); } + + public bool Equals (Str str) { return this.str == str.str; } + + public bool Matches (Str str) { - this.str = this.str.Insert (index, str); + if (regex = null) + regex = new Regex (this.str); + return regex.IsMatch (str.str); } - public Delete (int from, int to) + + public void Insert (int index, Str str) + { + if (index < 0) + this.str = this.str + str.str; + else + this.str = this.str.Insert (index, str.str); + regex = null; + } + + public void Insert (int index, char c) + { + if (index < 0) + this.str = this.str + c; + else + this.str = this.str.Insert (index, new string (c, 1)); + regex = null; + } + + public void Delete (int from, int to) { this.str = this.str.Remove (from, to - from); + regex = null; } } @@ -1976,11 +1982,11 @@ namespace System.Xml } } - public string Strval { + public Str Strval { get { if (! IsStr) throw new Error (Error.WrongType, "{0} is not a string", this); - return (StringBuilder) objval; + return (Str) objval; } } @@ -2067,11 +2073,7 @@ namespace System.Xml if (IsSymbol) return term.IsSymbol && term.Symval == Symval; if (IsStr) - return (term.IsStr - && term.Strval.Length = Strval.Length - && (Strval.Length == 0 - || (term.Strval.Chars[0] == Strval.Chars[0] - && term.Strval.ToString () == Strval.ToString ()))); + return (term.IsStr && term.Strval.Equals (Strval)); if (IsList) { if (! term.IsList) @@ -2094,11 +2096,11 @@ namespace System.Xml public bool Matches (Term term) { if (IsInt) - return term.IsInt && term.Intval == Intval; + return term.IsInt && Intval == term.Intval; if (IsSymbol) - return term.IsSymbol && term.Symval == Symval; + return term.IsSymbol && Symval == term.Symval; if (IsStr) - return term.IsStr && term.Strval == Strval; + return term.IsStr && Strval.Matches (term.Strval); if (IsList) { if (! term.IsList) -- 1.7.10.4