Example #1
0
        public CppEnum(string ns, string name, string[] usings, ParseNode content, List <ParserMessage> messages,
                       ParseNode typeDeclaration, string attributePrefix) : base(
                ns, name, usings, content, messages, typeDeclaration, attributePrefix, false)
        {
            ParseSymbols();

            void ParseSymbols()
            {
                ParseNode declarationContent = content.GetHierarchy().FirstOrDefault(n => n.RuleName == "declaration_content");

                ParseNode[] allSymbols = declarationContent.ChildrenSkipUnnamed().Where(n => n.RuleName != "comment_set").ToArray();
                CppSymbol   lastSymbol = null;

                foreach (ParseNode[] symbol in SplitSymbols())
                {
                    lastSymbol = CppSymbol.ParseSymbol(symbol, lastSymbol);
                    symbols.Add(lastSymbol);
                }

                IEnumerable <ParseNode[]> SplitSymbols()
                {
                    List <ParseNode[]> groups = new List <ParseNode[]>();
                    List <ParseNode>   nodes  = new List <ParseNode>(allSymbols);
                    ParseNode          comma  = FindFirstComma();

                    while (comma != null)
                    {
                        comma = FindFirstComma();
                        int         index = nodes.IndexOf(comma);
                        ParseNode[] group = nodes.Take(index).ToArray();
                        if (group.Any())
                        {
                            groups.Add(group);
                        }

                        nodes.RemoveRange(0, index + 1);
                    }

                    if (nodes.Any())
                    {
                        groups.Add(nodes.ToArray());
                    }

                    return(groups);

                    ParseNode FindFirstComma()
                    {
                        return(nodes.FirstOrDefault(n => n.GetHierarchy()
                                                    .FirstOrDefault(t => t.RuleName == "symbol")
                                                    ?.ToString().Contains(",") == true));
                    }
                }
            }
        }
Example #2
0
        public static CppSymbol ParseSymbol(ParseNode[] content, CppSymbol lastSymbol)
        {
            string    name          = string.Join(string.Empty, content.Select(c => c.ToString()));
            string    value         = string.Empty;
            ParseNode valueSplitter = content.FirstOrDefault(c => c.GetHierarchy()
                                                             .FirstOrDefault(n => n.RuleName == "symbol")
                                                             ?.ToString().Contains("=") == true);

            if (valueSplitter != null && Array.IndexOf(content, valueSplitter) == 1)
            {
                name  = content[0].ToString();
                value = string.Join(string.Empty, content.Skip(2).Select(c => c.ToString()));
            }
            else if (content.Length == 1)
            {
                name = content[0].ToString();
            }

            if (string.IsNullOrEmpty(value))
            {
                value = lastSymbol != null
                            ? long.TryParse(lastSymbol.Value, out long numberValue)
                                  ? (numberValue + 1).ToString("D", CultureInfo.InvariantCulture)
                                  : "0"
                            : "0";
            }

            value = value.Trim().Trim('\'', '"');
            if (!long.TryParse(value, out _))
            {
                try
                {
                    long convertedValue = Convert.ToInt64(value, 16);
                    value = convertedValue.ToString("D", CultureInfo.InvariantCulture);
                }
                catch (FormatException)
                {
                    //do nothing
                }
            }
            return(new CppSymbol(name, value));
        }
Example #3
0
        public static CppSymbol ParseSymbol(ParseNode[] content, CppSymbol lastSymbol)
        {
            string    name          = string.Join(string.Empty, content.Select(c => c.ToString()));
            string    value         = string.Empty;
            ParseNode valueSplitter = content.FirstOrDefault(c => c.GetHierarchy()
                                                             .FirstOrDefault(n => n.RuleName == "symbol")
                                                             ?.ToString().Contains("=") == true);

            if (valueSplitter != null && Array.IndexOf(content, valueSplitter) == 1)
            {
                name  = content[0].ToString();
                value = string.Join(string.Empty, content.Skip(2).Select(c => c.ToString()));
            }
            else if (content.Length == 1)
            {
                name = content[0].ToString();
            }

            if (string.IsNullOrEmpty(value))
            {
                if (lastSymbol != null)
                {
                    if (long.TryParse(lastSymbol.Value, out long numberValue))
                    {
                        value = (numberValue + 1).ToString("D", CultureInfo.InvariantCulture);
                    }
                }
                else
                {
                    value = "0";
                }
            }

            value = value.Trim().Trim('\'', '"');
            return(new CppSymbol(name, value));
        }