コード例 #1
0
        /// <summary>
        /// 파일을 전달하여 파일을 파싱합니다.
        /// </summary>
        /// <param name="fileFullPath">파싱할 파일 전체 경로.</param>
        /// <returns>파싱 성공 여부.</returns>
        public void FileParse(string fileFullPath)
        {
            // 언어 타입 찾기.
            LanguageType type = LanguageType.GetLanguageType(new FileInfo(fileFullPath).Extension);

            if (type == LanguageType.None)
            {
                return;
            }

            // 코드 파싱.
            IParseTree currentParseTree = null;

            try
            {
                var lexer = Bootstrapper.Instance.CreateContainer <Lexer>(type.Keyword, new ResolverOverride[]
                {
                    new ParameterOverride("input", new AntlrInputStream(File.ReadAllText(@fileFullPath)))
                });

                var parser = Bootstrapper.Instance.CreateContainer <Parser>(type.Keyword, new ResolverOverride[]
                {
                    new ParameterOverride("input", new CommonTokenStream(lexer))
                });
                parser.BuildParseTree = true;

                currentParseTree = ParseTreeUtility.GetNode(Bootstrapper.Instance.CreateContainer <IVisitorFactory>(type.Keyword).RootName, parser);
            }
            catch (Exception ex)
            {
                LogManager.GetCurrentClassLogger().Error(ex.Message);
            }

            // 파싱 실패시.
            if (currentParseTree == null)
            {
                return;
            }

            ParseTreeMap.Add(fileFullPath, currentParseTree);
            LogManager.GetCurrentClassLogger().Debug("Success to parse : Path({0})", @fileFullPath);

            // 트리로 부터 데이터 생성.
            //         var funcDeclareVisitor = Bootstrapper.Instance.CreateContainer<IVisitorFactory>(type.Keyword).FunctionVisitor;
            //foreach (var pair in funcDeclareVisitor.Visit(currentParseTree))
            //{
            //	FunctionBodyMap.Add(pair.Key, pair.Value);
            //             LogManager.GetCurrentClassLogger().Debug("Success to extract : Name({0}), ParseTree({1})",
            //                 pair.Key,
            //                 pair.Value.ToString()); ;
            //}

            // 트리로부터 기본적인 AST 생성.
            var astVisitor = Bootstrapper.Instance.CreateContainer <IVisitorFactory>(type.Keyword).BaseNodeVisitor;

            if (astVisitor != null)
            {
                AstTreeMap.Add(fileFullPath, astVisitor.Visit(currentParseTree));
            }
        }
コード例 #2
0
ファイル: FunctionVisitor.cs プロジェクト: wiseants/Sast
        public override Dictionary <string, IRuleNode> VisitChildren([NotNull] IRuleNode node)
        {
            if (ParseTreeUtility.IsMatchedContext("method_declaration", node) == true)
            {
                string    functionName = string.Empty;
                IRuleNode functionBody = null;

                if (ParseTreeUtility.TryChildContext("method_member_name", node, out IParseTree nameNode) == true)
                {
                    functionName = ParseTreeUtility.GetTerminals("identifier", nameNode).FirstOrDefault().GetText();
                }

                if (ParseTreeUtility.TryChildContext("method_body", node, out IParseTree bodynode) == true)
                {
                    functionBody = ParseTreeUtility.GetChildren("block", bodynode).FirstOrDefault();
                }

                return(string.IsNullOrEmpty(functionName) == false && functionBody != null ? new Dictionary <string, IRuleNode>()
                {
                    { functionName, functionBody }
                } : null);
            }

            return(base.VisitChildren(node));
        }
コード例 #3
0
ファイル: RuleChildrenVisitor.cs プロジェクト: wiseants/Sast
        public override List <IRuleNode> VisitChildren([NotNull] IRuleNode node)
        {
            if (ParseTreeUtility.IsMatchedContext(RuleName, node) == true)
            {
                return(new List <IRuleNode>()
                {
                    node
                });
            }

            return(base.VisitChildren(node));
        }
コード例 #4
0
        public override List <ITerminalNode> VisitChildren([NotNull] IRuleNode node)
        {
            if (string.IsNullOrEmpty(RuleName) == false && ParseTreeUtility.IsMatchedContext(RuleName, node) == true)
            {
                return(new List <ITerminalNode>()
                {
                    new TerminalVisitor().Visit(node).FirstOrDefault()
                });
            }

            return(base.VisitChildren(node));
        }