// for simplicity, we just use recursive descent internal Pick GetAlternation() { Pick.Alternation result = null; Pick last = null; int lastWeight = NO_WEIGHT; while (true) { Pick temp = GetSequence(); if (temp == null) { Error("empty alternation"); } int weight = GetWeight(); if (weight == NO_WEIGHT) { weight = 1; } if (last == null) { last = temp; lastWeight = weight; } else { if (result == null) { result = IBM.ICU.Charset.Pick.MakeAlternation().Or2(lastWeight, last); } result = result.Or2(weight, temp); } int token = t.Next(); if (token != '|') { t.Backup(); if (result != null) { return(result); } if (last != null) { return(last); } } } }
public void SetFailure(Pick pick_0, int item) { ArrayList val = (ArrayList)failures[index]; if (val == null) { val = new ArrayList(); failures[index] = val; } ILOG.J2CsMapping.Collections.ISet set = (ISet)val[item]; if (set == null) { set = new HashedSet(); val[item] = set; } ILOG.J2CsMapping.Collections.Generics.Collections.Add(set, pick_0); }
internal Pick GetSequence() { Pick.Sequence result = null; Pick last = null; while (true) { Pick item = GetCore(); if (item == null) { if (result != null) { return(result); } if (last != null) { return(last); } Error("missing item"); } // qualify it as many times as possible Pick oldItem; do { oldItem = item; item = Qualify(item); } while (item != oldItem); // add it in if (last == null) { last = item; } else { if (result == null) { result = IBM.ICU.Charset.Pick.MakeSequence().And2(last); } result = result.And2(item); } } }
private bool AddRule() { int type = t.Next(); if (type == IBM.ICU.Charset.Tokenizer.DONE) { return(false); } if (type != IBM.ICU.Charset.Tokenizer.STRING) { Error("missing weight"); } String s = t.GetString(); if (s.Length == 0 || s[0] != '$') { Error("missing $ in variable"); } if (t.Next() != '=') { Error("missing ="); } int startBody = t.index; Pick rule = GetAlternation(); if (rule == null) { Error("missing expression"); } t.AddSymbol(s, t.GetSource(), startBody, t.index); if (t.Next() != ';') { Error("missing ;"); } return(AddPick(s, rule)); }
public BNF Complete() { // check that the rules match the variables, except for $root in rules ILOG.J2CsMapping.Collections.ISet ruleSet = new ILOG.J2CsMapping.Collections.ListSet(map.Keys); // add also ILOG.J2CsMapping.Collections.Generics.Collections.Add(variables, "$root"); ILOG.J2CsMapping.Collections.Generics.Collections.AddAll(t.GetLookedUpItems(), variables); if (!ruleSet.Equals(variables)) { String msg = ShowDiff(variables, ruleSet); if (msg.Length != 0) { msg = "Error: Missing definitions for: " + msg; } String temp = ShowDiff(ruleSet, variables); if (temp.Length != 0) { temp = "Warning: Defined but not used: " + temp; } if (msg.Length == 0) { msg = temp; } else if (temp.Length != 0) { msg = msg + "; " + temp; } Error(msg); } if (!ruleSet.Equals(variables)) { String msg_0 = ShowDiff(variables, ruleSet); if (msg_0.Length != 0) { msg_0 = "Missing definitions for: " + msg_0; } String temp_1 = ShowDiff(ruleSet, variables); if (temp_1.Length != 0) { temp_1 = "Defined but not used: " + temp_1; } if (msg_0.Length == 0) { msg_0 = temp_1; } else if (temp_1.Length != 0) { msg_0 = msg_0 + "; " + temp_1; } Error(msg_0); } // replace variables by definitions IIterator it = new ILOG.J2CsMapping.Collections.IteratorAdapter(ruleSet.GetEnumerator()); while (it.HasNext()) { String key = (String)it.Next(); Pick expression = (Pick)ILOG.J2CsMapping.Collections.Collections.Get(map, key); IIterator it2 = new ILOG.J2CsMapping.Collections.IteratorAdapter(ruleSet.GetEnumerator()); if (false && key.Equals("$crlf")) { System.Console.Out.WriteLine("debug"); } while (it2.HasNext()) { Object key2 = it2.Next(); if (key.Equals(key2)) { continue; } Pick expression2 = (Pick)ILOG.J2CsMapping.Collections.Collections.Get(map, key2); expression2.Replace(key, expression); } } pick = (Pick)ILOG.J2CsMapping.Collections.Collections.Get(map, "$root"); target = IBM.ICU.Charset.Pick.Target.Make(pick, random, quoter); // TODO remove temp collections return(this); }
// for Building public Pick Replace(String toReplace, Pick replacement) { Pick.Replacer visitor = new Pick.Replacer(toReplace, replacement); return(Visit(visitor)); }
internal void SetLast(Pick newOne) { items[items.Length - 1] = newOne; }
internal ItemPick(Pick item_0) { this.item = item_0; }
internal Replacer(String toReplace_0, Pick replacement_1) { this.toReplace = toReplace_0; this.replacement = replacement_1; }
// Note: each visitor should return the Pick that will replace a (or a // itself) abstract internal Pick Handle(Pick a);
internal Quote(Pick item) : base(item) { }
public Repeat(int minCount_0, int maxCount, int[] itemWeights, Pick item) : base(item) { this.minCount = 0; weightedIndex = new Pick.WeightedIndex(minCount_0).Add(maxCount - minCount_0 + 1, itemWeights); }
public Pick.Alternation Or2(int itemWeight, Pick item) { return(Or2(itemWeight, new Pick[] { item })); // we don't care about // perf }
public Pick.Sequence And2(Pick item) { AddInternal(new Pick[] { item }); // we don't care about perf return(this); // for chaining }
/* * static public Pick.Sequence and(Object item) { return new * Sequence().and2(item); } static public Pick.Sequence and(Object[] items) * { return new Sequence().and2(items); } static public Pick.Alternation * or(int itemWeight, Object item) { return new * Alternation().or2(itemWeight, item); } static public Pick.Alternation * or(Object[] items) { return new Alternation().or2(1, items); } static * public Pick.Alternation or(int itemWeight, Object[] items) { return new * Alternation().or2(itemWeight, items); } static public Pick.Alternation * or(int[] itemWeights, Object[] items) { return new * Alternation().or2(itemWeights, items); } * * static public Pick maybe(int percent, Object item) { return new Repeat(0, * 1, new int[]{100-percent, percent}, item); //return Pick.or(1.0-percent, * NOTHING).or2(percent, item); } static public Pick repeat(int minCount, * int maxCount, int itemWeights, Object item) { return new Repeat(minCount, * maxCount, itemWeights, item); } * * static public Pick codePoint(String source) { return new CodePoint(new * UnicodeSet(source)); } */ static public Pick RepeatMthd(int minCount, int maxCount, int[] itemWeights, Pick item) { return(new Pick.Repeat(minCount, maxCount, itemWeights, item)); }