From: handa Date: Mon, 7 Dec 2009 04:01:05 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ad9688b36b476f477e9a9aa6f2b3407b2c6b47bd;p=m17n%2Fm17n-lib-cs.git *** empty log message *** --- diff --git a/XmlExpr.cs b/XmlExpr.cs index 08954e8..18de2ec 100644 --- a/XmlExpr.cs +++ b/XmlExpr.cs @@ -9,15 +9,14 @@ using System.Xml; namespace System.Xml { + /// + /// Static class to provide XmlExpression loader and evaluator. + /// public static class Xexpression { - private static int trace_depth = 0; - - public static int TraceDepth { - get { return trace_depth; } - set { trace_depth = value; } - } - + /// + /// Exception thrown while loading and evaluating XmlExpression. + /// public class Error : Exception { private readonly Symbol name; @@ -1971,37 +1970,93 @@ namespace System.Xml public override string ToString () { return str; } } + /// Structure of term object. public struct Term { - public int intval; - public object objval; + internal int intval; + internal object objval; - // ... + /// Create an integer term. + /// Integer value of the term. + /// An integer term. + /// Create an integer term that has the integer value + /// specified by . This is an constant + /// term; i.e. the integer value never changes. public Term (int i) { intval = i; objval = null; } - // ... + + /// Create a symbol term. + /// Symbol value of the term. + /// A symbol term. + /// Create a symbol term that has the symbol value + /// specified by . This is an constant + /// term; i.e. the symbol value never changes. It is evaluated + /// to itself. public Term (Symbol name) { intval = 0; objval = name; } - // ... + + /// Create a string term. + /// String value of the term. + /// A string term. + /// Create a string term that has the string value + /// specified by . It is evaluated to + /// itself. The string value can be modified by "ins", "del", + /// and "concat" functions. public Term (string str) { intval = 0; objval = new Str (str); } // ... + + /// Create a list term. + /// List value of the term. + /// A list term. + /// Create a list term that has the list value + /// specified by . It is evaluated to + /// itself. The list value can be modified by "ins", "del", and + /// "append" functions. public Term (List list) { intval = 0; objval = list; } // ERROR-MESSASGE + + /// Create an error term. + /// Name of the error. + /// Error message. + /// An error term. + /// Create an error term whose error name is and error message is . It is evaluated to itself. public Term (Symbol name, string message) { intval = 0; objval = new ErrorTerm (name, message); } - public Term (Str str) { intval = 0; objval = str; } - public Term (TermValue obj) { intval = 0; objval = obj; } + /// Create a term of a specific value + /// Value of the term. + /// A term. + /// Create a term whose value is . + /// It is evaluated to a term that is returned by "Eval" method + /// of . + public Term (TermValue val) { intval = 0; objval = val; } - // + internal Term (Str str) { intval = 0; objval = str; } + + /// Create a varref term. + /// Domain to create the term in. + /// Name of the referred variable. + /// A varref term. + /// Create a varref term that is evaluated to the value + /// of the referring variable. public Term (Domain domain, Symbol vname) { intval = 0; objval = new Varref (vname); } - // ... + /// Create a funcall term. + /// Domain to create the term in. + /// Name of the calling function. + /// Array of terms that are given to the + /// function as arguments. + /// A funcall term. + /// Create a funcall term that is evaluated to a term + /// returned by the function when called + /// with . public Term (Domain domain, Symbol fname, Term[] args) : this (domain, fname, Qnull, args) { } @@ -2377,7 +2432,7 @@ namespace System.Xml return args; } - public static Symbol parse_defun_head (Domain domain, XmlNode node) + private static Symbol parse_defun_head (Domain domain, XmlNode node) { Symbol name = node.Attributes[Qfname].Value; int min_args, max_args; @@ -2391,7 +2446,7 @@ namespace System.Xml return name; } - public static void parse_defun_body (Domain domain, XmlNode node) + private static void parse_defun_body (Domain domain, XmlNode node) { bool is_defun = node.Name == Qdefun; Symbol name = node.Attributes[Qfname].Value; @@ -2459,17 +2514,17 @@ namespace System.Xml } } - private static bool default_stop (XmlNode n) { return n == null; } - - public delegate bool ParseStop (XmlNode node); + /// Parse a node and the following siblings as "term"s in a + /// specific domain. + /// + /// + /// + /// Array of terms. - public static Term[] Parse (Domain domain, XmlNode node, ParseStop stop) + public static Term[] Parse (Domain domain, XmlNode start, XmlNode stop) { - if (stop == null) - stop = default_stop; - XmlNode n; - for (n = node; ! stop (n); n = n.NextSibling) + for (n = start; n != stop && n != null; n = n.NextSibling) if (n.NodeType == XmlNodeType.Element && (n.Name == Qdefun || n.Name == Qdefmacro)) { @@ -2477,30 +2532,30 @@ namespace System.Xml parse_defun_head (domain, n); } catch (Error e) { if (e.Node == null) - e.Node = node; + e.Node = n; throw e; } catch (Exception e) { - throw new Error (Error.UnknownError, node, e, "Parsing error"); + throw new Error (Error.UnknownError, n, e, "Parsing error"); } } List terms = new List (); - for (; node != n; node = node.NextSibling) - if (node.NodeType == XmlNodeType.Element) + for (; start != n; start = start.NextSibling) + if (start.NodeType == XmlNodeType.Element) { try { - if (node.Name == Qdefun || node.Name == Qdefmacro) - parse_defun_body (domain, node); - else if (node.Name == Qdefvar) - parse_defvar (domain, node); + if (start.Name == Qdefun || start.Name == Qdefmacro) + parse_defun_body (domain, start); + else if (start.Name == Qdefvar) + parse_defvar (domain, start); else - terms.Add (Parse (domain, node)); + terms.Add (Parse (domain, start)); } catch (Error e) { if (e.Node == null) - e.Node = node; + e.Node = start; throw e; } catch (Exception e) { - throw new Error (Error.UnknownError, node, e, "Parsing error"); + throw new Error (Error.UnknownError, start, e, "Parsing error"); } } return terms.ToArray (); @@ -2526,11 +2581,21 @@ namespace System.Xml return Parse (domain, node.FirstChild, null); } + /// Evaluate a term in a specific domain. + /// Domain to evaluate the term in. + /// The term to evaluate. + /// The result of evaluating the . public static Term Eval (Domain domain, Term term) { return Eval (domain, new Term[] { term }); } + /// Evaluate terms in a specific domain. + /// Domain to evaluate terms in. + /// Array of terms to evaluate. + /// The result of evaluating the last term of . public static Term Eval (Domain domain, Term[] terms) { Term result = Zero; @@ -2549,5 +2614,16 @@ namespace System.Xml throw new Error (Error.UnknownError, null, e, "Runtime error"); } } + + // TRACING + + private static int trace_depth = 0; + + /// TraceDepth specifies the minimum depth of call stack + /// to suppress tracing. 0 means no tracing. + public static int TraceDepth { + get { return trace_depth; } + set { trace_depth = value; } + } } }