Example #1
0
 public override string ToString()
 {
     if (null == Document)
     {
         return("");
     }
     if (string.IsNullOrEmpty(Document.FileOrUrl))
     {
         return("@import <<in-memory>>;");
     }
     return("@import \"" + XbnfNode.Escape(Document.FileOrUrl) + "\";");
 }
        public string ToString(string fmt)
        {
            var sb = new StringBuilder();

            for (int ic = Includes.Count, i = 0; i < ic; ++i)
            {
                sb.Append("@include ");
                sb.Append("\"" + XbnfNode.Escape(Includes[i].Document.FileOrUrl) + "\"");
                sb.AppendLine(";");
            }
            var oc = Options.Count;

            if (0 < oc)
            {
                sb.Append("@options ");
                for (var i = 0; i < oc; ++i)
                {
                    if (0 != i)
                    {
                        sb.Append(", ");
                    }
                    sb.Append(Options[i]);
                }
                sb.AppendLine(";");
            }
            if ("gnc" == fmt)
            {
                for (int ic = Productions.Count, i = 0; i < ic; ++i)
                {
                    sb.AppendLine(Productions[i].ToString("pnc"));
                }
            }
            else if ("xc" == fmt)
            {
                for (int ic = Productions.Count, i = 0; i < ic; ++i)
                {
                    sb.AppendLine(Productions[i].ToString("xc"));
                }
            }
            else
            {
                for (int ic = Productions.Count, i = 0; i < ic; ++i)
                {
                    sb.AppendLine(Productions[i].ToString());
                }
            }
            return(sb.ToString());
        }
        static string _ToRegex(XbnfDocument d, XbnfExpression e, bool first, bool gplex = false)
        {
            var le = e as XbnfLiteralExpression;

            if (null != le)
            {
                var s = _EscapeLiteral(XbnfNode.Escape(le.Value), !gplex);
                if (gplex)
                {
                    s = string.Concat("\"", s, "\"");
                }
                return(s);
            }
            var rxe = e as XbnfRegexExpression;

            if (null != rxe)
            {
                var r = rxe.Value;
                if (gplex)
                {
                    r = r.Replace("\"", "\\\"");
                }
                return(first ? r : string.Concat("(", r, ")"));
            }
            var rfe = e as XbnfRefExpression;

            if (null != rfe)
            {
                _ToRegex(d, d.Productions[rfe.Symbol].Expression, first, gplex);
            }
            var re = e as XbnfRepeatExpression;

            if (null != re)
            {
                if (re.IsOptional)
                {
                    return(string.Concat("(", _ToRegex(d, re.Expression, true, gplex), ")*"));
                }
                else
                {
                    return(string.Concat("(", _ToRegex(d, re.Expression, true, gplex), ")+"));
                }
            }
            var oe = e as XbnfOrExpression;

            if (null != oe)
            {
                if (!first)
                {
                    return(string.Concat("(", _ToRegex(d, oe.Left, false, gplex), "|", _ToRegex(d, oe.Right, false, gplex), ")"));
                }
                else
                {
                    return(string.Concat(_ToRegex(d, oe.Left, false, gplex), "|", _ToRegex(d, oe.Right, false, gplex)));
                }
            }
            var oc = e as XbnfConcatExpression;

            if (null != oc)
            {
                return(string.Concat(_ToRegex(d, oc.Left, false, gplex), _ToRegex(d, oc.Right, false, gplex)));
            }
            var ope = e as XbnfOptionalExpression;

            if (null != ope)
            {
                return(string.Concat("(", _ToRegex(d, ope.Expression, true, gplex), ")?"));
            }
            return("");
        }