public override object /* XmlElement */ VisitElement(XMLParser.ElementContext context) { var names = context.Name(); string name = null; foreach (var n in names.Select(t => t.GetText())) { if (name == null) { name = n; } if (n != name) { throw new Exception(); } } var element = new XmlElement(); element.NodeType = NodeConstants.ELEMENT_NODE; element.NodeName = name; element.LocalName = name; var map = new AntlrNamedNodeMap(); element.Attributes = map; var attrs = context.attribute(); foreach (var a in attrs) { var new_attr = VisitAttribute(a) as Attr; map.Add(new_attr); new_attr.ParentNode = element; } if (context.content() != null) { var content = VisitContent(context.content()) as NodeList; element.ChildNodes = content; for (int i = 0; i < content.Length; ++i) { var c = content.item(i); c.ParentNode = element; } } return(element); }
private static XmlNode BottomUpConvert(IParseTree tree, Parser parser) { if (tree is TerminalNodeImpl) { var result = new XmlElement(); result.AntlrIParseTree = tree; TerminalNodeImpl t = tree as TerminalNodeImpl; Interval interval = t.SourceInterval; result.NodeType = NodeConstants.ELEMENT_NODE; var common_token_stream = parser.InputStream as CommonTokenStream; var lexer = common_token_stream.TokenSource as Lexer; var fixed_name = parser.Vocabulary.GetSymbolicName(t.Symbol.Type); result.LocalName = fixed_name; var nl = new AntlrNodeList(); result.ChildNodes = nl; var child = new XmlText(); child.AntlrIParseTree = tree; child.NodeType = NodeConstants.TEXT_NODE; child.Data = new xpath.org.eclipse.wst.xml.xpath2.processor.@internal.OutputParseTree().PerformEscapes(/*"'" + */ tree.GetText() /*+ "'"*/); child.ParentNode = result; nl.Add(child); { var attr = new XmlAttr(); var child_count = t.ChildCount; attr.NodeType = NodeConstants.ATTRIBUTE_NODE; attr.Name = "ChildCount"; attr.Value = child_count.ToString(); attr.ParentNode = result; nl.Add(attr); } { var attr = new XmlAttr(); attr.NodeType = NodeConstants.ATTRIBUTE_NODE; var source_interval = t.SourceInterval; var a = source_interval.a; var b = source_interval.b; attr.Name = "SourceInterval"; attr.Value = "[" + a + "," + b + "]"; attr.ParentNode = result; nl.Add(attr); } return(result); } else { var result = new XmlElement(); var t = tree as ParserRuleContext; result.AntlrIParseTree = tree; result.NodeType = NodeConstants.ELEMENT_NODE; var x = tree as RuleContext; var ri = x.RuleIndex; var name = parser.RuleNames[ri]; result.LocalName = name; var nl = new AntlrNodeList(); result.ChildNodes = nl; var map = new AntlrNamedNodeMap(); result.Attributes = map; { var attr = new XmlAttr(); var child_count = t.ChildCount; attr.NodeType = NodeConstants.ATTRIBUTE_NODE; attr.Name = "ChildCount"; attr.LocalName = "ChildCount"; attr.Value = child_count.ToString(); attr.ParentNode = result; nl.Add(attr); map.Add(attr); } { var attr = new XmlAttr(); attr.NodeType = NodeConstants.ATTRIBUTE_NODE; var source_interval = t.SourceInterval; var a = source_interval.a; var b = source_interval.b; attr.Name = "Start"; attr.LocalName = "Start"; attr.Value = a.ToString(); attr.ParentNode = result; nl.Add(attr); map.Add(attr); } { var attr = new XmlAttr(); attr.NodeType = NodeConstants.ATTRIBUTE_NODE; var source_interval = t.SourceInterval; var a = source_interval.a; var b = source_interval.b; attr.Name = "End"; attr.LocalName = "End"; attr.Value = b.ToString(); attr.ParentNode = result; nl.Add(attr); map.Add(attr); } for (int i = 0; i < tree.ChildCount; ++i) { var child = tree.GetChild(i); var convert = BottomUpConvert(child, parser); nl.Add(convert); convert.ParentNode = result; } return(result); } }