private static void DumpLexicalAnalyzer_GetKeywordList(
     RegulationList grammar, string grammarId, CodeTypeDeclaration lexiType)
 {
     {
         // private static readonly List<Keyword> keywords = new List<Keyword>();
         var field = new CodeMemberField(typeof(List <Keyword>), "keywords");
         field.Attributes     = MemberAttributes.Private | MemberAttributes.Static;
         field.InitExpression = new CodeObjectCreateExpression(typeof(List <Keyword>));
         lexiType.Members.Add(field);
     }
     {
         // protected override IEnumerable<Keyword> GetKeywords()
         var method = new CodeMemberMethod();
         method.Name       = "GetKeywords";
         method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
         method.ReturnType = new CodeTypeReference(typeof(IEnumerable <Keyword>));
         var returnKeywords = new CodeMethodReturnStatement(
             new CodeVariableReferenceExpression("keywords"));
         method.Statements.Add(returnKeywords);
         lexiType.Members.Add(method);
     }
     {
         // static DemoLexicalAnalyzer()
         var method = new CodeTypeConstructor();
         method.Name       = GetLexicalAnalyzerName(grammarId);
         method.Attributes = MemberAttributes.Static;
         //{
         //    // List<Keyword> keyword = new List<Keyword>();
         //    var keyword = new CodeVariableDeclarationStatement("List<Keyword>", "keyword");
         //    keyword.InitExpression = new CodeObjectCreateExpression("List<Keyword>");
         //    method.Statements.Add(keyword);
         //}
         var convertor = new TreeNodeType2TokenType();
         foreach (var node in grammar.GetAllTreeNodeLeaveTypes())
         {
             // keywords.Add(new Keyword("__x", "x"));
             if (node.IsIdentifier())
             {
                 TokenType tokenType = convertor.GetTokenType(node);
                 var       ctor      = new CodeObjectCreateExpression(typeof(Keyword),
                                                                      new CodePrimitiveExpression(tokenType.Type),
                                                                      new CodePrimitiveExpression(tokenType.Content));
                 var add = new CodeMethodInvokeExpression(
                     new CodeVariableReferenceExpression("keywords"),
                     "Add",
                     ctor);
                 method.Statements.Add(add);
             }
         }
         //{
         //    // DemoLexicalAnalyzer.keywords = keywords;
         //    var assign = new CodeAssignStatement(
         //        new CodeFieldReferenceExpression(
         //            new CodeSnippetExpression(GetLexicalAnalyzerName(grammarId)), "keywords"),
         //        new CodeVariableReferenceExpression("keywords"));
         //    method.Statements.Add(assign);
         //}
         lexiType.Members.Add(method);
     }
 }
Exemple #2
0
        /// <summary>
        /// 为生成词法分析器,获取该文法的所有词法状态。
        /// </summary>
        /// <param name="grammar"></param>
        /// <returns></returns>
        static List <LexiState> GetLexiStateList(this RegulationList grammar)
        {
            var result = new List <LexiState>();
            List <TreeNodeType> nodeList = grammar.GetAllTreeNodeLeaveTypes();

            foreach (var node in nodeList)
            {
                if (node.Type == "identifierLeave__")// ContextfreeGrammar的关键字
                {
                    TryInsertGetIdentifier(result);
                }
                else if (node.Type == "numberLeave__")// ContextfreeGrammar的关键字
                {
                    TryInsertGetNumber(result);
                }
                else if (node.Type == "constStringLeave__")// ContextfreeGrammar的关键字
                {
                    TryInserteGetConstString(result);
                }
                else if (node.Type == "charLeave__")// ContextfreeGrammar的关键字(暂未支持)
                {
                    TryInserteGetChar(result);
                }
                else
                {
                    CodeGetRegularToken(result, node);
                }
            }

            return(result);
        }
Exemple #3
0
        private static void DumpTokenTypeConstFields(RegulationList grammar, CodeTypeDeclaration tokenTypeConstType)
        {
            // public const string __colon_colon_equal = "__colon_colon_equal";
            var convertor = new TreeNodeType2TokenType();

            foreach (var node in grammar.GetAllTreeNodeLeaveTypes())
            {
                var field = new CodeMemberField(typeof(string), GetNodeNameInParser(node));
                field.Attributes = MemberAttributes.Public | MemberAttributes.Const;
                TokenType tokenType = convertor.GetTokenType(node);
                field.InitExpression = new CodePrimitiveExpression(tokenType.Type);
                tokenTypeConstType.Members.Add(field);
            }
        }
        private static string CheckConstStrings(RegulationList grammar, out bool error)
        {
            error = false;
            StringBuilder builder = new StringBuilder();

            foreach (var node in grammar.GetAllTreeNodeLeaveTypes())
            {
                if (contextfreeGrammarKeywordList.Contains(node.Nickname))
                {
                    continue;
                }

                bool   innderError = false;
                string lack        = CheckNode(node, out innderError);
                builder.Append(innderError);
                if (innderError)
                {
                    error = true;
                }
            }

            return(builder.ToString());
        }