Exemple #1
0
        static void DoJsonArray(JsonTextBuffer avb, ListTerm t, bool first)
        {
            if (t.IsEmptyList)
            {
                return;
            }

            DoJsonValue(avb, null, t.Arg(0), first);
            DoJsonArray(avb, (ListTerm)t.Arg(1), false);
        }
Exemple #2
0
        static void DoJsonArray(XmlTextWriter xwr, string [] attributes, string label, ListTerm t, ref bool contentWritten)
        {
            if (t.IsEmptyList)
            {
                return;
            }

            DoJsonValue(xwr, attributes, label, t.Arg(0), ref contentWritten);
            DoJsonArray(xwr, attributes, label, (ListTerm)t.Arg(1), ref contentWritten);
        }
            public static string Format(ListTerm lt)
            {
                if (!lt.IsProperList || lt.ProperLength != 2)
                {
                    IO.Error("Format list must be a proper list of length 2");

                    return(null);
                }

                BaseTerm fstring = lt.Arg(0);
                BaseTerm args    = lt.Arg(1).Arg(0);

                return(Format(fstring, args));
            }
            public static BaseTerm FindRegexMatches(
                OperatorTable opTable, string source, string matchPattern, string[] options)
            {
                Regex        re        = null;
                RegexOptions reOptions = RegexOptions.None;

                if (options != null)
                {
                    foreach (string o in options)
                    {
                        switch (o.ToLower())
                        {
                        case "ignorecase":
                            reOptions |= RegexOptions.IgnoreCase;
                            break;

                        case "multiline":
                            reOptions |= RegexOptions.Multiline;
                            break;

                        case "singleline":
                            reOptions |= RegexOptions.Singleline;
                            break;

                        case "explicitcapture":
                            reOptions |= RegexOptions.ExplicitCapture;
                            break;

                        case "cultureinvariant":
                            reOptions |= RegexOptions.CultureInvariant;
                            break;

                        default:
                            IO.Error("match_regex -- unsupported option '{0}'", o);
                            break;
                        }
                    }
                    ;
                }

                try
                {
                    re = new Regex(matchPattern, reOptions); //, RegexOptions.Multiline);
                }
                catch (Exception x)
                {
                    IO.Error("Error in regular expression '{0}'\r\nMessage: {1}", matchPattern, x.Message);
                }

                Match m = re.Match(source);

                if (!m.Success)
                {
                    return(null);
                }

                int[]    gnums  = re.GetGroupNumbers();
                BaseTerm groups = new ListTerm();

                while (m.Success)
                {
                    for (int i = 1; i < gnums.Length; i++) // start at group 1 (0 is the fully captured match string)
                    {
                        Group    g        = m.Groups[gnums[i]];
                        BaseTerm captures = new ListTerm();
                        string   groupId  = re.GetGroupNames()[i];
                        int      groupNo;
                        BaseTerm groupIdTerm;

                        foreach (Capture c in g.Captures)
                        {
                            captures = ((ListTerm)captures).AppendElement(new StringTerm(c.ToString()));
                        }

                        if (int.TryParse(groupId, out groupNo))
                        {
                            groupIdTerm = new DecimalTerm(groupNo);
                        }
                        else
                        {
                            groupIdTerm = new StringTerm(re.GetGroupNames()[i]);
                        }

                        groups = ((ListTerm)groups).AppendElement(
                            new OperatorTerm(opTable, ":", groupIdTerm, captures));
                    }

                    m = m.NextMatch();
                }

                if (((ListTerm)groups).ProperLength == 1)
                {
                    groups = groups.Arg(0); // groups is <groupname>:[<captures>]

                    if (groups.Arg(0) is DecimalTerm)
                    {
                        groups = groups.Arg(1); // anonymous group, just return the list of captures
                    }
                }

                return(groups);
            }