Exemple #1
0
        private bool GetParser(string childTokenKeyword, out IDefParser defParser)
        {
            // uses parsers in place
            bool result = ParserKeywordRegistry.TryGetValue(childTokenKeyword, out IDefParser keywordParser);

            if (!result)
            {
                defParser = default;
                return(false);
            }

            defParser = keywordParser.Clone();
            return(true);
        }
Exemple #2
0
        public override IEnumerable <object> Deserialize(IEnumerable <LineMeta> lines)
        {
            List <object>    results    = new List <object>();
            List <TokenNode> rootTokens = GenerateTokens(lines);

            // Type resultType = typeof(T);

            List <ParsingError> errors = new List <ParsingError>();

            foreach (TokenNode rootToken in rootTokens)
            {
                if (rootToken.Type == TokenType.BREAK)
                {
                    continue;
                }

                if (rootToken.Type != TokenType.KEYWORD)
                {
                    throw new InvalidOperationException($"Expected keyword token, found {rootToken.ToString_Internal(false)}");
                }

                if (!ParserKeywordRegistry.TryGetValue(rootToken.Keyword, out IDefParser parser))
                {
                    throw new InvalidOperationException($"Unknown top-level token keyword: {rootToken.ToString_Internal(false)}");
                }

                Type dataType = parser.DefType;

                object data = Activator.CreateInstance(dataType);

                // if (parser.DefType != resultType)
                // {
                //     throw new InvalidOperationException($"Expected last parsed keyword: {rootToken.Keyword} to parse as {resultType}");
                // }

                parser.Def = data;
                ParseNode(null, parser, rootToken, errors);

                CheckForErrors(rootToken, errors);

                // yield return data;
                results.Add(data);
            }

            return(results);
        }
Exemple #3
0
        public override IEnumerable <T> Deserialize <T>(IEnumerable <LineMeta> lines)
        {
            List <TokenNode> rootTokens = GenerateTokens(lines);

            Type resultType = typeof(T);

            List <ParsingError> errors = new List <ParsingError>();

            foreach (TokenNode rootToken in rootTokens)
            {
                if (rootToken.Type == TokenType.BREAK)
                {
                    continue;
                }

                if (rootToken.Type != TokenType.KEYWORD)
                {
                    throw new InvalidOperationException($"Expected keyword token: {rootToken.Type}, found token, content {rootToken.Content}");
                }

                if (!ParserKeywordRegistry.TryGetValue(rootToken.Keyword, out IDefParser parser))
                {
                    throw new InvalidOperationException($"Unknown top-level token keyword: {rootToken.Keyword}");
                }

                T data = new T();

                if (parser.DefType != resultType)
                {
                    throw new InvalidOperationException($"Expected last parsed keyword: {rootToken.Keyword} to parse as {resultType}");
                }

                ZLog.Info("[ROOT]");
                ZLog.Info(rootToken.ToString());

                parser.Def = data;
                ParseNode(null, parser, rootToken, errors);

                CheckForErrors(rootToken, errors);

                yield return(data);
            }
        }