예제 #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);
        }
예제 #2
0
        private void ParseNode(IDefParser parentParser, IDefParser defParser, TokenNode rootToken, List <ParsingError> errors)
        {
            if (rootToken.Data != null)
            {
                try
                {
                    defParser.ParseOptions(rootToken.Options);
                }
                catch (Exception e)
                {
                    errors.Add(new ParsingError
                    {
                        Node      = rootToken,
                        Exception = e,
                    });
                }
            }

            if (rootToken.Children == null)
            {
                throw new InvalidOperationException($"Expected keyword token to contain children: {rootToken}, parentParser: {parentParser}");
            }

            foreach (TokenNode childToken in rootToken.Children)
            {
                try
                {
                    switch (childToken.Type)
                    {
                    case TokenType.KEYWORD:
                        if (childToken.IsLeaf)
                        {
                            try
                            {
                                defParser.ParseCue(childToken.Keyword, childToken.Data);
                            }
                            catch (Exception e)
                            {
                                errors.Add(new ParsingError
                                {
                                    Node      = childToken,
                                    Exception = e,
                                });
                            }
                        }
                        else
                        {
                            if (!GetParser(childToken.Keyword, out IDefParser subParser))
                            {
                                throw new InvalidOperationException($"Unknown top-level token keyword: {childToken.Keyword}");
                            }

                            Type subtype = subParser.DefType;

                            object subdata = Activator.CreateInstance(subtype);

                            subParser.Def = subdata;
                            ParseNode(defParser, subParser, childToken, errors);
                            subParser.Def = null;

                            // object data = //n

                            // childToken.Keyword
                            try
                            {
                                defParser.ParseChildToken(subdata);
                            }
                            catch (Exception e)
                            {
                                errors.Add(new ParsingError
                                {
                                    Node      = childToken,
                                    Exception = e,
                                });
                            }
                        }
                        break;

                    case TokenType.MISC:

                        if (childToken.Content != null)
                        {
                            defParser.ParseMisc(childToken.Content);
                        }
                        break;

                    case TokenType.BREAK:

                        defParser.ParseBreak();
                        break;

                    default:
                        ZLog.Warn($"Unhandled token type: {childToken.Type}");
                        break;
                    }
                }
                catch (Exception e)
                {
                    errors.Add(new ParsingError
                    {
                        Node      = childToken,
                        Exception = e,
                    });
                }
            }
        }
예제 #3
0
 public void Register(IDefParser defParser)
 {
     ParserKeywordRegistry[defParser.Keyword] = defParser;
     ParserTypeRegistry[defParser.DefType]    = defParser;
 }