private static Assembly CompileCode(string directory, string grammarId, SyntaxParserMapAlgorithm syntaxParserMapAlgorithm) { string[] files = Directory.GetFiles(directory, "*.cs", SearchOption.TopDirectoryOnly); CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider(); CompilerParameters objCompilerParameters = new CompilerParameters(); objCompilerParameters.ReferencedAssemblies.Add( typeof(LALR1Compiler.LRSyntaxParser).Assembly.Location); objCompilerParameters.ReferencedAssemblies.Add( typeof(List <>).Assembly.Location); objCompilerParameters.ReferencedAssemblies.Add( typeof(Object).Assembly.Location); objCompilerParameters.GenerateExecutable = false; objCompilerParameters.GenerateInMemory = true; objCompilerParameters.IncludeDebugInformation = true; CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromFile( objCompilerParameters, files); if (cr.Errors.HasErrors) { Console.WriteLine("Compiling Errors:"); foreach (var item in cr.Errors) { Console.Write(" "); Console.WriteLine(item); } return(null); } else { return(cr.CompiledAssembly); } }
private static void DumpSyntaxParserCode( RegulationList grammar, LRParsingMap map, string grammarId, string LR0Directory, SyntaxParserMapAlgorithm algorithm) { var parserType = new CodeTypeDeclaration(GetParserName(grammarId, algorithm)); parserType.IsClass = true; parserType.BaseTypes.Add(typeof(LRSyntaxParser)); DumpSyntaxParserFields(grammar, parserType, grammarId, algorithm); DumpSyntaxParserMethod_GetGrammar(grammar, parserType); DumpSyntaxParserMethod_GetParsingMap(grammar, map, parserType); var parserNamespace = new CodeNamespace(GetNamespace(grammarId)); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Object).Namespace)); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Collections.Generic.List <int>).Namespace)); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(LALR1Compiler.HashCache).Namespace)); parserNamespace.Types.Add(parserType); //生成代码 string parserCodeFullname = Path.Combine(LR0Directory, GetParserName(grammarId, algorithm) + ".cs"); using (var stream = new StreamWriter(parserCodeFullname, false)) { CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CodeGeneratorOptions geneOptions = new CodeGeneratorOptions();//代码生成选项 geneOptions.BlankLinesBetweenMembers = true; geneOptions.BracingStyle = "C"; geneOptions.ElseOnClosing = false; geneOptions.IndentString = " "; geneOptions.VerbatimOrder = true; codeProvider.GenerateCodeFromNamespace(parserNamespace, stream, geneOptions); } }
private static void DumpTreeNodeTypeConstCode( RegulationList grammar, LRParsingMap map, string grammarId, string LR0Directory, SyntaxParserMapAlgorithm algorithm) { var treeNodeConstType = new CodeTypeDeclaration(GetTreeNodeConstTypeName(grammarId, algorithm)); treeNodeConstType.IsClass = true; DumpTreeNodeConstFields(grammar, treeNodeConstType); var parserNamespace = new CodeNamespace(GetNamespace(grammarId)); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Object).Namespace)); parserNamespace.Types.Add(treeNodeConstType); //生成代码 string fullname = Path.Combine(LR0Directory, GetTreeNodeConstTypeName(grammarId, algorithm) + ".cs"); using (var stream = new StreamWriter(fullname, false)) { CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CodeGeneratorOptions geneOptions = new CodeGeneratorOptions();//代码生成选项 geneOptions.BlankLinesBetweenMembers = true; geneOptions.BracingStyle = "C"; geneOptions.ElseOnClosing = false; geneOptions.IndentString = " "; geneOptions.VerbatimOrder = true; codeProvider.GenerateCodeFromNamespace(parserNamespace, stream, geneOptions); } }
private static void DumpLexicalAnalyzer_GetSymbol( RegulationList grammar, CodeTypeDeclaration lexiType, string grammarId, SyntaxParserMapAlgorithm algorithm) { List <LexiState> lexiStateList = grammar.GetLexiStateList(); DivideState commentState = null;// 为了处理注释,"/"符号要特殊对待。 foreach (var state in lexiStateList) { if (state.CharTypeList.Contains(SourceCodeCharType.Divide)) { commentState = new DivideState(state); continue; } CodeMemberMethod method = state.GetMethodDefinitionStatement(grammarId, algorithm); if (method != null) { lexiType.Members.Add(method); } } { if (commentState == null) { commentState = LexiState.GetCommentState(); } CodeMemberMethod method = commentState.GetMethodDefinitionStatement(grammarId, algorithm); if (method != null) { lexiType.Members.Add(method); } } }
private static void DumpLexicalAnalyzerCode(RegulationList grammar, string grammarId, SyntaxParserMapAlgorithm algorithm, string directory) { var lexiType = new CodeTypeDeclaration(GetLexicalAnalyzerName(grammarId)); lexiType.IsClass = true; lexiType.IsPartial = true; lexiType.BaseTypes.Add(typeof(LexicalAnalyzer)); DumpLexicalAnalyzer_TryGetToken(grammar, lexiType); DumpLexicalAnalyzer_GetSymbol(grammar, lexiType, grammarId, algorithm); DumpLexicalAnalyzer_GetKeywordList(grammar, grammarId, lexiType); var lexiNamespace = new CodeNamespace(GetNamespace(grammarId)); lexiNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Object).Namespace)); lexiNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Collections.Generic.List <int>).Namespace)); lexiNamespace.Imports.Add(new CodeNamespaceImport(typeof(LALR1Compiler.HashCache).Namespace)); lexiNamespace.Types.Add(lexiType); //生成代码 string parserCodeFullname = Path.Combine(directory, GetLexicalAnalyzerName(grammarId) + ".cs"); using (var stream = new StreamWriter(parserCodeFullname, false)) { CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CodeGeneratorOptions geneOptions = new CodeGeneratorOptions();//代码生成选项 geneOptions.BlankLinesBetweenMembers = true; geneOptions.BracingStyle = "C"; geneOptions.ElseOnClosing = false; geneOptions.IndentString = " "; geneOptions.VerbatimOrder = true; codeProvider.GenerateCodeFromNamespace(lexiNamespace, stream, geneOptions); } }
internal override CodeStatement[] DumpReadToken(string grammarId, SyntaxParserMapAlgorithm algorithm) { List <CodeStatement> result = new List <CodeStatement>(); if (this.Count > 0)// 有除了"//"和"/*"之外的项,例如可能是"/", "/=" { OtherItemsAndNote(result, grammarId, algorithm); } else { OnlyNote(result); } return(result.ToArray()); }
private static void DumpSyntaxParserFields(RegulationList grammar, CodeTypeDeclaration parserType, string grammarId, SyntaxParserMapAlgorithm algorithm) { { // protected static LRParsingMap parsingMap; CodeMemberField field = new CodeMemberField(typeof(LRParsingMap), "parsingMap"); field.Attributes = MemberAttributes.Private | MemberAttributes.Static; parserType.Members.Add(field); } { // protected static RegulationList grammar; CodeMemberField field = new CodeMemberField(typeof(RegulationList), "grammar"); field.Attributes = MemberAttributes.Private | MemberAttributes.Static; parserType.Members.Add(field); } foreach (var node in grammar.GetAllTreeNodeTypes()) { //var field = new CodeSnippetTypeMember(string.Format( // " public static readonly {0} {1} = new {0}(\"{2}\", \"{3}\", \"{4}\");", // typeof(TreeNodeType).Name, GetNodeNameInParser(node), // node.Type, node.Content, node.Nickname)); //parserType.Members.Add(field); // private static TreeNodeType NODE__Grammar = new TreeNodeType(ContextfreeGrammarSLRTreeNodeType.NODE__Grammar, "Grammar", "<Grammar>"); CodeMemberField field = new CodeMemberField(typeof(TreeNodeType), GetNodeNameInParser(node)); // field.Attributes 不支持readonly,遗憾了。 field.Attributes = MemberAttributes.Private | MemberAttributes.Static; var ctor = new CodeObjectCreateExpression(typeof(TreeNodeType), new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(GetTreeNodeConstTypeName(grammarId, algorithm)), GetNodeNameInParser(node)), new CodePrimitiveExpression(node.Content), new CodePrimitiveExpression(node.Nickname)); field.InitExpression = ctor; parserType.Members.Add(field); } { // private static readonly TreeNodeType end_of_token_listLeave__ = TreeNodeType.endOfTokenListNode; var node = TreeNodeType.endOfTokenListNode; CodeMemberField field = new CodeMemberField(typeof(TreeNodeType), GetNodeNameInParser(node)); field.Attributes = MemberAttributes.Private | MemberAttributes.Static; field.InitExpression = new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(TreeNodeType)), "endOfTokenListNode"); parserType.Members.Add(field); } }
private static string GetTreeNodeConstTypeName(string grammarId, SyntaxParserMapAlgorithm algorithm) { SourceCodeCharType charType = grammarId[0].GetCharType(); if (charType == SourceCodeCharType.Letter || charType == SourceCodeCharType.UnderLine) { return(string.Format("{0}{1}TreeNodeType", ConstString2IdentifierHelper.ConstString2Identifier(grammarId), algorithm)); } else { return(string.Format("_{0}{1}TreeNodeType", ConstString2IdentifierHelper.ConstString2Identifier(grammarId), algorithm)); } }
//public CodeGetTokenCollection() : base(Environment.NewLine) { } internal virtual CodeStatement[] DumpReadToken(string grammarId, SyntaxParserMapAlgorithm algorithm) { List <CodeStatement> result = new List <CodeStatement>(); for (int length = this.list[0].Value.Type.Length; length > 0; length--) { bool exists = false; // if (context.NextLetterIndex + {0} <= count) var ifStatement = new CodeConditionStatement( new CodeSnippetExpression(string.Format( "context.NextLetterIndex + {0} <= count", length))); { // str = context.SourceCode.Substring(context.NextLetterIndex, {0}); var str = new CodeVariableDeclarationStatement(typeof(string), "str"); str.InitExpression = new CodeSnippetExpression(string.Format( "context.SourceCode.Substring(context.NextLetterIndex, {0});", length)); ifStatement.TrueStatements.Add(str); } foreach (var item in this.list) { if (item.Value.Content.Length != length) { continue; } // if ("XXX" == str) { ... } CodeStatement[] statements = item.DumpReadToken(grammarId, algorithm); if (statements != null) { ifStatement.TrueStatements.AddRange(statements); exists = true; } } if (exists) { result.Add(ifStatement); } } return(result.ToArray()); }
public virtual CodeStatement[] DumpReadToken(string grammarId, SyntaxParserMapAlgorithm algorithm) { List <CodeStatement> list = new List <CodeStatement>(); { /// if ("XXX" == str) var ifStatement = new CodeConditionStatement( new CodeBinaryOperatorExpression( new CodePrimitiveExpression(this.Value.Content), CodeBinaryOperatorType.IdentityEquality, new CodeVariableReferenceExpression("str"))); { var convertor = new TreeNodeType2TokenType(); // result.TokenType = new TokenType("..", "..", ".."); var newTokenType = new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("result"), "TokenType"), new CodeObjectCreateExpression(typeof(TokenType), new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(Program.GetTokenConstTypeName(grammarId, algorithm)), Program.GetNodeNameInParser(this.Value)), new CodePrimitiveExpression(this.Value.Content), new CodePrimitiveExpression(this.Value.Nickname))); ifStatement.TrueStatements.Add(newTokenType); // context.NextLetterIndex = context.NextLetterIndex + {0} var pointer = new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("context"), "NextLetterIndex"); var incresePointer = new CodeAssignStatement( pointer, new CodeBinaryOperatorExpression( pointer, CodeBinaryOperatorType.Add, new CodePrimitiveExpression(this.Value.Content.Length))); ifStatement.TrueStatements.Add(incresePointer); // return true; var returnTrue = new CodeMethodReturnStatement(new CodePrimitiveExpression(true)); ifStatement.TrueStatements.Add(returnTrue); } list.Add(ifStatement); } return(list.ToArray()); }
private static void TestCode(Assembly asm, string directory, string compilerDir, string grammarId, SyntaxParserMapAlgorithm syntaxParserMapAlgorithm) { if (asm == null) { Console.WriteLine(" Get Compiled Compiler Failed..."); return; } { string conflicts = File.ReadAllText(Path.Combine(compilerDir, "Conflicts.log")); if (int.Parse(conflicts) > 0) { Console.WriteLine(" No need to Test Code with conflicts in SyntaxParser"); return; } } try { LexicalAnalyzer lexi = asm.CreateInstance( GetNamespace(grammarId) + "." + GetLexicalAnalyzerName(grammarId)) as LexicalAnalyzer; LRSyntaxParser parser = asm.CreateInstance( GetNamespace(grammarId) + "." + GetParserName(grammarId, syntaxParserMapAlgorithm)) as LRSyntaxParser; string[] sourceCodeFullnames = Directory.GetFiles( directory, "*.Code", SearchOption.TopDirectoryOnly); foreach (var fullname in sourceCodeFullnames) { try { FileInfo fileInfo = new FileInfo(fullname); string sourceCode = File.ReadAllText(fullname); TokenList tokenList = lexi.Analyze(sourceCode); tokenList.Dump(Path.Combine(compilerDir, fileInfo.Name + ".TokenList.log")); SyntaxTree tree = parser.Parse(tokenList); tree.Dump(Path.Combine(compilerDir, fileInfo.Name + ".Tree.log")); } catch (Exception e) { Console.WriteLine("Testing Error for {0}:", fullname); Console.WriteLine(e); } } } catch (Exception ex) { Console.WriteLine("Running Errors:"); Console.Write(" "); Console.WriteLine(ex); } }
public override CodeStatement[] DumpReadToken(string grammarId, SyntaxParserMapAlgorithm algorithm) { return(null); }
private void OtherItemsAndNote(List <CodeStatement> result, string grammarId, SyntaxParserMapAlgorithm algorithm) { for (int length = this[0].Value.Type.Length; length > 0; length--) { bool exists = false; // if (context.NextLetterIndex + {0} <= count) var ifStatement = new CodeConditionStatement( new CodeSnippetExpression(string.Format( "context.NextLetterIndex + {0} <= count", length))); { // str = context.SourceCode.SubString(context.NextLetterIndex, {0}); var str = new CodeVariableDeclarationStatement(typeof(string), "str"); str.InitExpression = new CodeSnippetExpression(string.Format( "context.SourceCode.Substring(context.NextLetterIndex, {0});", length)); ifStatement.TrueStatements.Add(str); } foreach (var item in this) { // if ("xxx" == str) { ... } if (item.Value.Content.Length != length) { continue; } CodeStatement[] statements = item.DumpReadToken(grammarId, algorithm); if (statements != null) { ifStatement.TrueStatements.AddRange(statements); exists = true; } } if (length == 2)// 轮到添加对注释的处理了 { SingleLine(ifStatement); MultiLine(ifStatement); exists = true; } if (exists) { result.Add(ifStatement); } } }
/// <summary> /// 获取GetXXX()的定义 /// </summary> /// <returns></returns> public CodeMemberMethod GetMethodDefinitionStatement(string grammarId, SyntaxParserMapAlgorithm algorithm) { if (this.IsContextfreeGrammarKeyword()) { return(null); } var method = new CodeMemberMethod(); method.Name = string.Format("Get{0}", this.GetTokenName()); method.Attributes = MemberAttributes.Family; method.ReturnType = new CodeTypeReference(typeof(bool)); method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Token), "result")); method.Parameters.Add(new CodeParameterDeclarationExpression("AnalyzingContext", "context")); { // var count = context.SourceCode.Length; var count = new CodeVariableDeclarationStatement(typeof(int), "count"); count.InitExpression = new CodePropertyReferenceExpression( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("context"), "SourceCode"), "Length"); method.Statements.Add(count); } { CodeStatement[] statements = this.GetTokenList.DumpReadToken(grammarId, algorithm); method.Statements.AddRange(statements); } { // var current = context.CurrentChar().ToString(); // result.TokenType = new TokenType("__error", current, current); // result.LexicalError = true; // context.NextLetterIndex++; // return true; var current = new CodeVariableDeclarationStatement(typeof(string), "current"); current.InitExpression = new CodeMethodInvokeExpression( new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("context"), "CurrentChar"), "ToString"); var tokenType = new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("result"), "TokenType"), new CodeObjectCreateExpression(typeof(TokenType), new CodePrimitiveExpression("__error"), new CodeVariableReferenceExpression("current"), new CodeVariableReferenceExpression("current"))); var lexiError = new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("result"), "LexicalError"), new CodePrimitiveExpression(true)); var increase = new CodeAssignStatement( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("context"), "NextLetterIndex"), new CodeBinaryOperatorExpression( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("context"), "NextLetterIndex"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1))); var returnTrue = new CodeMethodReturnStatement(new CodePrimitiveExpression(false)); method.Statements.Add(current); method.Statements.Add(tokenType); method.Statements.Add(lexiError); method.Statements.Add(increase); method.Statements.Add(returnTrue); } return(method); }