public WomElement(WomElement other) { if (other == null) { throw new ArgumentNullException("other"); } this._name = other._name; WomPropertyCollection attrs = other._properties; if (attrs != null) { this._properties = new WomPropertyCollection(attrs); } if (other._content is string) { this._content = other._content; } else { WomElementCollection otherElementList = other._content as WomElementCollection; if (otherElementList != null) { WomElementCollection elementList = ElementList; for (int i = 0; i < otherElementList.Count; i++) { elementList.Add(new WomElement(otherElementList[i])); } } } }
private void Initialize(WomElement element) { _start = ProcessElement(element); AddSuccess(); }
public override bool MoveToPrevious() { if (_propertyIndex < 0) { int index = GetElementIndex(); if (index >= 0 && --index >= 0) { this._element = this._element._parent.ElementList[index]; this._elementIndex = index; return true; } } return false; }
public override bool MoveToNext() { if (_propertyIndex < 0) { int index = GetElementIndex(); if (index >= 0 && ++index < this._element._parent.ElementList.Count) { this._element = this._element._parent.ElementList[index]; this._elementIndex = index; return true; } } return false; }
public override bool MoveTo(XPathNavigator other) { WomXPathNavigator womNavigator = other as WomXPathNavigator; if (womNavigator != null) { _element = womNavigator._element; _propertyIndex = womNavigator._propertyIndex; return true; } return false; }
// Constructors public ParserEngineBuilder(WomElement element) { Initialize(element); }
private int ProcessSetUnion(WomElement element) { ParserCharSet set = new ParserCharSet(); WomElementCollection list = element.ElementList; for (int i = 0; i < list.Count; i++) { set = set + _charSets[ProcessElement(list[i])]; } _charSets.Add(set); return _charSets.Count - 1; }
private int ProcessSequence(WomElement element) { WomElementCollection sequence = element.ElementList; return ProcessElementList(sequence); }
private int ProcessChoice(int choice1, WomElement element) { // Create first choice last instruction int choice1End = AddEmpty(); // Create a choice node to point to the two defined choices int result = AddChoice(c_unused, c_unused); // Create second choice int choice2 = ProcessElement(element); // Point the choice node to its alternatives SetArguments(result, choice1, choice2); // Point end of first choice to the end of second choice SetArguments(choice1End, _instructions.Count); return result; }
private int ProcessChoice(WomElement element) { WomElementCollection elementList = element.ElementList; int result = ProcessElement(elementList[0]); for (int i = 1; i < elementList.Count; i++) { result = ProcessChoice(result, elementList[i]); } return result; }
private int ProcessCharSetString(WomElement element) { char[] chars = element.Properties["Text"].ToCharArray(); ParserCharSet charSet = new ParserCharSet(chars); return AddCharSet(charSet); }
private int ProcessCharSetRange(WomElement element) { ParserCharSet set = ParserCharSet.FromRange( Convert.ToChar(element.Properties["Start"], CultureInfo.CurrentCulture), Convert.ToChar(element.Properties["End"], CultureInfo.CurrentCulture)); _charSets.Add(set); return _charSets.Count - 1; }
private int ProcessCharSetDefinition(WomElement element) { int index = _charSetIndex[element.Properties["Name"]]; _charSets[index] = _charSets[ProcessElement(element.ElementList[0])]; return index; }
private int ProcessCallRule(WomElement element) { string ruleName = element.Properties["Name"]; int ruleIndex = _ruleIndex[ruleName]; return AddCallRule(ruleIndex); }
private int ProcessAttribute(WomElement element) { int nameIndex = AddString(element.Properties["Name"]); int result = _instructions.Count; int nextIndex = _instructions.Count + 1; _instructions.Add(new ParserInstruction(ParserInstructionCode.StartAttribute, nextIndex, nameIndex)); ProcessElementList(element.ElementList); nextIndex = _instructions.Count + 1; _instructions.Add(new ParserInstruction(ParserInstructionCode.EndAttribute, nextIndex, nameIndex)); return result; }
private int ProcessOptional(WomElement element) { int contentStart = ProcessElementList(element.ElementList); int contentEnd = AddEmpty(); int result = AddChoice(contentStart, contentEnd); SetArguments(contentEnd, _instructions.Count); return result; }
private int ProcessRule(WomElement element) { int rule = AddRule(element.Properties["Name"]); _rules[rule].ElementName = element.Properties["ElementName"]; SetRuleStart(rule, ProcessElementList(element.ElementList)); AddRuleSuccess(); return rule; }
private int ProcessElement(WomElement element) { switch (element.Name) { case "AnyChar": return ProcessAnyChar(); case "Attribute": return ProcessAttribute(element); case "CallRule": return ProcessCallRule(element); case "CharSetDefinition": return ProcessCharSetDefinition(element); case "CharSetRange": return ProcessCharSetRange(element); case "CharSetString": return ProcessCharSetString(element); case "Choice": return ProcessChoice(element); case "Grammar": return ProcessGrammar(element); case "LineEnd": return ProcessLineEnd(element); case "LineStart": return ProcessLineStart(element); case "Match": return ProcessMatch(element); case "MatchAny": return ProcessMatchAny(element); case "MatchRange": return ProcessMatchRange(element); case "MatchSet": return ProcessMatchSet(element); case "OneOrMore": return ProcessOneOrMore(element); case "Optional": return ProcessOptional(element); case "Rule": return ProcessRule(element); case "Sequence": return ProcessSequence(element); case "SetSubtraction": return ProcessSetSubtraction(element); case "SetUnion": return ProcessSetUnion(element); case "ZeroOrMore": return ProcessZeroOrMore(element); default: throw new InvalidOperationException("Unknown element name - " + element.Name); } }
private int ProcessSetSubtraction(WomElement element) { ParserCharSet charSet1 = _charSets[ProcessElement(element.ElementList[0])]; ParserCharSet charSet2 = _charSets[ProcessElement(element.ElementList[1])]; return AddCharSet(charSet1 - charSet2); }
private int ProcessGrammar(WomElement element) { // Create rule and char set index int ruleIndex = 0; int charSetIndex = 0; for (int i = 0; i < element.ElementList.Count; i++) { WomElement item = element.ElementList[i]; if (item.Name == "Rule") { _rules.Add(null); _ruleIndex.Add(item.Properties["Name"], ruleIndex++); } else if (item.Name == "CharSetDefinition") { _charSets.Add(null); _charSetIndex.Add(item.Properties["Name"], charSetIndex++); } } int startRule = ProcessElementList(element.ElementList); _start = AddCallRule(startRule); return _start; }
private int ProcessZeroOrMore(WomElement element) { int contentStart = ProcessElementList(element.ElementList); return AddChoice(contentStart, c_nextInstruction); }
private int ProcessLineStart(WomElement element) { int nextIndex = _instructions.Count + 1; _instructions.Add(new ParserInstruction(ParserInstructionCode.LineStart, nextIndex)); return _instructions.Count - 1; }
private void ReadRule() { WomElement rule = new WomElement("Rule"); if (_reader.HasAttributes) { rule.Properties["Name"] = _reader.GetAttribute("Name"); string expr = _reader.GetAttribute("Match"); ParserExpressionReader exprReader = new ParserExpressionReader(); exprReader.Parse(expr, rule); _reader.MoveToElement(); _document.Root.Add(rule); } }
private int ProcessMatch(WomElement element) { string text = element.Properties["Text"]; if (text.Length == 1) { return AddMatchChar(text[0]); } return AddMatchString(text); }
public override bool MoveToFirstChild() { if (_propertyIndex < 0) { WomElement element = this._element.FirstChild; if (element != null) { this._element = element; this._elementIndex = 0; return true; } } return false; }
private int ProcessMatchAny(WomElement element) { int nextIndex = _instructions.Count + 1; _instructions.Add(new ParserInstruction(ParserInstructionCode.MatchAny, nextIndex)); return _instructions.Count - 1; }
public override bool MoveToParent() { if (_propertyIndex >= 0) { _propertyIndex = -1; return false; } WomElement element = this._element.Parent; if (element != null) { this._element = element; this._elementIndex = -1; return true; } return false; }
private int ProcessMatchRange(WomElement element) { return AddMatchSet(ParserCharSet.FromRange( Convert.ToChar(element.Properties["Start"], CultureInfo.CurrentCulture), Convert.ToChar(element.Properties["End"], CultureInfo.CurrentCulture))); }
// Constructors public WomXPathNavigator(WomElement element) { this._element = element; this._elementIndex = -1; this._propertyIndex = -1; }
private int ProcessMatchSet(WomElement element) { return AddMatchSet(element.Properties["Name"]); }