Exemplo n.º 1
0
        public static bool Ends(this ITokenStream stream, string type, ILogger logger, out Token[] row, out string token0)
        {
            row = stream.PeekNonEmpty();
            if (row == null)
            {
                token0 = null;
                return(true);
            }

            token0 = row[0].Value.ToLower();
            if (token0 == "end")
            {
                if (row.Length > 1)
                {
                    if (!string.Equals(row[1].Value, type, StringComparison.OrdinalIgnoreCase))
                    {
                        logger?.WriteLine($"Warning: Expected 'end {type}' at {stream.Path}:{stream.Line}.");
                    }
                }

                stream.Move();
                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        public static bool Starts(this ITokenStream stream, string type, ILogger logger, out string name)
        {
            var row = stream.PeekNonEmpty();

            if (row == null)
            {
                name = null;
                return(false);
            }

            if (string.Equals(row[0].Value, type, StringComparison.OrdinalIgnoreCase))
            {
                if (row.Length > 1)
                {
                    name = row[1].Value;
                }
                else
                {
                    name = null;
                    logger?.WriteLine($"Warning: No item name at {stream.Path}:{stream.Line}. Items without a name may get ignored.");
                }

                stream.Move();
                return(true);
            }

            name = null;
            return(false);
        }
Exemplo n.º 3
0
        public static bool Starts(this ITokenStream stream, string type)
        {
            var row = stream.PeekNonEmpty();

            if (row == null)
            {
                return(false);
            }

            if (string.Equals(row[0].Value, type, StringComparison.OrdinalIgnoreCase))
            {
                stream.Move();
                return(true);
            }

            return(false);
        }
Exemplo n.º 4
0
        public static RootConfiguration Parse(ITokenStream stream, ILogger logger)
        {
            var config = new RootConfiguration();

            while (true)
            {
                var tokens = stream.PeekNonEmpty();
                if (tokens == null)
                {
                    break;
                }

                var token0 = tokens[0].Value.ToLower();
                var token1 = tokens.Length > 1 ? tokens[1].Value : null;
                var token2 = tokens.Length > 2 ? tokens[2].Value : null;
                switch (token0)
                {
                case "bind":
                    var binding = ParseCommandBinding(stream, logger);
                    if (binding != null)
                    {
                        config.Bindings.Add(binding);
                    }

                    stream.Move();
                    break;

                case "profile":
                    var profile = ParseProfile(stream, logger);
                    if (profile != null)
                    {
                        config.Profiles.Add(profile);
                    }

                    break;

                case "menu":
                    var menu = ParseMenuConfiguration(stream, logger);
                    if (menu != null)
                    {
                        config.Menus.Add(menu);
                    }

                    break;

                default:
                    if (tokens.Length < 3 || token1 != "=")
                    {
                        logger?.WriteLine($"Error: Malformed line at {stream.Path}:{stream.Line}.");
                        stream.Move();
                        break;
                    }

                    if (token0 == "fps")
                    {
                        if (InvariantDouble.TryParse(token2, out var fps) && fps >= 1d && fps <= 300d)
                        {
                            config.Fps = fps;
                        }
                        else
                        {
                            logger?.WriteLine(
                                $"Warning: Invalid FPS value '{token2}' at '{token0}' at {stream.Path}:{stream.Line}.");
                        }
                    }
                    else if (config.Descriptors.TryGetValue(token0, out var descriptor))
                    {
                        if (!descriptor.isValidValue(token2))
                        {
                            logger?.WriteLine(
                                $"Warning: Value '{token2}' is invalid or out of range for key '{token0}' at {stream.Path}:{stream.Line}. Default value is assigned.");
                        }

                        descriptor.SetValue(token2);
                    }
                    else
                    {
                        logger?.WriteLine($"Error: Unknown key '{token0}' at {stream.Path}:{stream.Line}.");
                    }

                    stream.Move();
                    break;
                }
            }

            config.InitializeProfiles();
            return(config);
        }