public static IReadOnlyDictionary <string, Map> Read(
            Stream stream,
            IResourceProvider resourceProvider)
        {
            using var reader = new StreamReader(stream, Encoding.ASCII, leaveOpen: true);
            var tokenSource = new TokenSource(MapInfoLexer.Create(reader).Scan(), resourceProvider, MapInfoLexer.Create);

            using var tokenStream = tokenSource.GetEnumerator();
            return(MapDeclarationParser.ReadMapDeclarations(tokenStream));
        }
예제 #2
0
        public static MapTranslation Parse(
            IEnumerable <Token> tokens,
            IResourceProvider resourceProvider)
        {
            List <TileMappings> tileMappings  = new();
            List <IMapping>     thingMappings = new();
            List <FlatMappings> flatMappings  = new();

            var tokenSource = new TokenSource(tokens, resourceProvider, XlatLexer.Create);

            using var tokenStream = tokenSource.GetEnumerator();

            while (tokenStream.MoveNext())
            {
                var id = tokenStream.Current as IdentifierToken;
                if (id == null)
                {
                    throw new ParsingException($"Unexpected token: {tokenStream.Current}");
                }

                switch (id.Id.ToLower())
                {
                case "enable":
                case "disable":
                    // global flag, ignore
                    tokenStream.ExpectNext <IdentifierToken>();
                    tokenStream.ExpectNext <SemicolonToken>();
                    break;

                case "music":
                    throw new ParsingException("This should be ignored");

                case "tiles":
                    tileMappings.Add(ParseTileMappings(tokenStream));
                    break;

                case "things":
                    thingMappings.AddRange(ParseThingMappings(tokenStream));
                    break;

                case "flats":
                    flatMappings.Add(ParseFlatMappings(tokenStream));
                    break;

                default:
                    throw new ParsingException($"Unexpected identifier: {id}");
                }
            }

            return(new MapTranslation(
                       Merge(tileMappings),
                       thingMappings,
                       flatMappings.LastOrDefault()));
        }