public static BabyElement CreateAssignment(string name, string exact)
        {
            BabyAttribute[] attributes = new BabyAttribute[]
            {
                new BabyAttribute("name", name),
                new BabyAttribute("exact", exact)
            };

            BabyElement retVal = new BabyElement("set_value", attributes, null);

            retVal.IsAssignment = true;
            return(retVal);
        }
Example #2
0
        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();
        }