public bool Convert() { BabyScriptLexer lex = new BabyScriptLexer(new AntlrInputStream(inputStream)); CommonTokenStream tokenStream = new CommonTokenStream(lex); BabyScriptParser parser = new BabyScriptParser(tokenStream); parser.AddErrorListener(new MyErrorListener()); BabyScriptParser.DocumentContext doc = parser.document(); if (parser.NumberOfSyntaxErrors > 0) { Console.Error.WriteLine(string.Format(Utils.FileMessageFormat, path, "Aborting write due to syntax error(s)")); return(false); } XmlWritingListener listener = new XmlWritingListener(this, parser); new ParseTreeWalker().Walk(listener, doc); listener.Flush(); if (listener.Error) { Console.Error.WriteLine(string.Format(Utils.FileMessageFormat, path, "Aborting write due to semantic error(s)")); return(false); } return(true); }
public XmlWritingListener(BabyCompile owner, BabyScriptParser parser) { Owner = owner; Parser = parser; Writer = XmlWriter.Create(Owner.outputStream, new XmlWriterSettings { Indent = true, IndentChars = " " }); Error = false; }
private void WriteAttributes() { if (!reader.HasAttributes) { return; } string[] impliedNames = attrConfig.GetAnonAttributes(reader.Name); List <BabyAttribute> allAttributes = new List <BabyAttribute>(); List <BabyAttribute> namedAttributes = new List <BabyAttribute>(); List <BabyAttribute> anonAttributes = new List <BabyAttribute>(); //just get a list of all the attributes, in our own data structure while (reader.MoveToNextAttribute()) { if (reader.Name == "comment") { curElementComment = reader.Value; continue; } BabyAttribute newAttribute = new BabyAttribute(reader.Name.Replace(":", ""), reader.Value); allAttributes.Add(newAttribute); } //if there are some implied attributes, try to match them in order with what we get until we can't //since the order matters, we can't match the second implied one without having the first if (impliedNames != null) { foreach (string name in impliedNames) { bool found = false; foreach (BabyAttribute attrib in allAttributes) { if (attrib.Name == name) { attrib.IsAnonymous = true; anonAttributes.Add(attrib); found = true; } } if (!found) { break; } } } //and now, a named attribute is just an attribute we didn't find to be anonymous foreach (BabyAttribute attribute in allAttributes) { if (!attribute.IsAnonymous) { namedAttributes.Add(attribute); } } //things to write is all the nameless ones followed by the named ones IEnumerable <BabyAttribute> thingsToWrite = anonAttributes.Concat <BabyAttribute>(namedAttributes); int attrNumber = 0; foreach (BabyAttribute attribute in thingsToWrite) { if (!attribute.IsAnonymous) { writer.Write(attribute.Name); writer.Write(":"); } BabyScriptLexer lexer = new BabyScriptLexer(new AntlrInputStream(attribute.Value)); CommonTokenStream tokens = new CommonTokenStream(lexer); BabyScriptParser parser = new BabyScriptParser(tokens); parser.exprEof(); if (parser.NumberOfSyntaxErrors > 0) { Console.Error.WriteLine("Line {0}: \"{1}\" isn't a valid expression and will be wrapped in doublequotes", ((IXmlLineInfo)reader).LineNumber, attribute.Value); writer.Write("\"" + attribute.Value + "\""); } else { writer.Write(attribute.Value); } if (allAttributes.Count > 1 && attrNumber < allAttributes.Count - 1) { writer.Write(", "); } attrNumber++; } reader.MoveToElement(); }