vbcodecompiler.cs
来自「没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没」· CS 代码 · 共 1,281 行 · 第 1/3 页
CS
1,281 行
/* * VBCodeCompiler.cs - Implementation of the * System.CodeDom.Compiler.VBCodeCompiler class. * * Copyright (C) 2003 Southern Storm Software, Pty Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */namespace System.CodeDom.Compiler{#if CONFIG_CODEDOMusing System.IO;using System.Reflection;using System.Globalization;internal class VBCodeCompiler : CodeCompiler{ // List of reserved words in VB. private static readonly String[] reservedWords = { "addhandler", "addressof", "alias", "and", "andalso", "ansi", "as", "assembly", "auto", "boolean", "byref", "byte", "byval", "call", "case", "catch", "cbool", "cbyte", "cchar", "cdate", "cdec", "cdbl", "char", "cint", "class", "clng", "cobj", "const", "cshort", "csng", "cstr", "ctype", "date", "decimal", "declare", "default", "delegate", "dim", "directcast", "do", "double", "each", "else", "elseif", "end", "enum", "erase", "error", "event", "exit", "false", "finally", "for", "friend", "function", "get", "gettype", "gosub", "goto", "handles", "if", "implements", "imports", "in", "inherits", "integer", "interface", "is", "let", "lib", "like", "long", "loop", "me", "mod", "module", "mustinherit", "mustoverride", "mybase", "myclass", "namespace", "new", "next", "not", "nothing", "notinheritable", "notoverridable", "object", "on", "option", "optional", "or", "orelse", "overloads", "overridable", "overrides", "paramarray", "preserve", "private", "property", "protected", "public", "raiseevent", "readonly", "redim", "removehandler", "resume", "return", "select", "set", "shadows", "shared", "short", "single", "static", "step", "stop", "string", "structure", "sub", "synclock", "then", "throw", "to", "true", "try", "typeof", "unicode", "until", "variant", "when", "while", "with", "withevents", "writeonly", "xor" }; // Constructor. public VBCodeCompiler() {} // Get the name of the compiler. protected override String CompilerName { get { // Use the Portable.NET VB compiler. String cscc = Environment.GetEnvironmentVariable("CSCC"); if(cscc != null) { return cscc; } else { return "cscc"; } } } // Get the file extension to use for source files. protected override String FileExtension { get { return ".vb"; } } // Convert compiler parameters into compiler arguments. protected override String CmdArgsFromParameters (CompilerParameters options) { return CSharpCodeCompiler.CmdArgsFromParameters(options, "vb"); } // Process an output line from the compiler. protected override void ProcessCompilerOutputLine (CompilerResults results, String line) { CompilerError error = ProcessCompilerOutputLine(line); if(error != null) { results.Errors.Add(error); } } // Get the token for "null". protected override String NullToken { get { return "Nothing"; } } // Determine if a string is a reserved word. private static bool IsReservedWord(String value) { if(value != null) { value = value.ToLower(CultureInfo.InvariantCulture); return (Array.IndexOf(reservedWords, value) != -1); } else { return false; } } // Create an escaped identifier if "value" is a language keyword. protected override String CreateEscapedIdentifier(String value) { if(IsReservedWord(value)) { return "[" + value + "]"; } else { return value; } } // Create a valid identifier if "value" is a language keyword. protected override String CreateValidIdentifier(String value) { if(IsReservedWord(value)) { return "_" + value; } else { return value; } } // Normalize a type name to its keyword form. private String NormalizeTypeName(String type) { int index1, index2; String baseName; String suffixes; // Bail out if the type is null. if(type == null) { return null; } // Split the type into base and suffix parts. index1 = type.IndexOf('*'); index2 = type.IndexOf('['); if(index1 > index2 && index2 != -1) { index1 = index2; } if(index1 != -1) { baseName = type.Substring(0, index1); suffixes = type.Substring(index1); suffixes = suffixes.Replace('[', '('); suffixes = suffixes.Replace(']', ')'); } else { baseName = type; suffixes = null; } // Convert the base name. switch(baseName) { case "System.Boolean": baseName = "Boolean"; break; case "System.Char": baseName = "Char"; break; case "System.Byte": baseName = "Byte"; break; case "System.Int16": baseName = "Short"; break; case "System.Int32": baseName = "Integer"; break; case "System.Int64": baseName = "Long"; break; case "System.Single": baseName = "Single"; break; case "System.Double": baseName = "Double"; break; case "System.Decimal": baseName = "Decimal"; break; case "System.String": baseName = "String"; break; case "System.DateTime": baseName = "Date"; break; case "System.Object": baseName = "Object"; break; default: break; } // Return the new type to the caller. return baseName + suffixes; } // Generate various expression categories. protected override void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression e) { OutputIdentifier(e.ParameterName); } protected override void GenerateArrayCreateExpression (CodeArrayCreateExpression e) { Output.Write("New "); if(e.Initializers.Count == 0) { Output.Write(NormalizeTypeName(e.CreateType.BaseType)); Output.Write("("); if(e.SizeExpression != null) { GenerateExpression(e.SizeExpression); } else { Output.Write(e.Size); } Output.Write(")"); } else { OutputType(e.CreateType); if(e.CreateType.ArrayRank == 0) { Output.Write("()"); } Output.WriteLine(" {"); Indent += 1; OutputExpressionList(e.Initializers, true); Indent -= 1; Output.Write("}"); } } protected override void GenerateArrayIndexerExpression (CodeArrayIndexerExpression e) { GenerateExpression(e.TargetObject); Output.Write("("); OutputExpressionList(e.Indices); Output.Write(")"); } protected override void GenerateBaseReferenceExpression (CodeBaseReferenceExpression e) { Output.Write("MyBase"); } protected override void GenerateCastExpression (CodeCastExpression e) { Output.Write("CType("); GenerateExpression(e.Expression); Output.Write(", "); OutputType(e.TargetType); Output.Write(")"); } protected override void GenerateDelegateCreateExpression (CodeDelegateCreateExpression e) { Output.Write("New "); OutputType(e.DelegateType); Output.Write("("); if(e.TargetObject != null) { GenerateExpression(e.TargetObject); Output.Write("."); } OutputIdentifier(e.MethodName); Output.Write(")"); } protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression e) { if(e.TargetObject != null) { GenerateExpression(e.TargetObject); } Output.Write("("); OutputExpressionList(e.Parameters); Output.Write(")"); } protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression e) { if(e.TargetObject != null) { GenerateExpression(e.TargetObject); Output.Write("."); } OutputIdentifier(e.EventName); } protected override void GenerateFieldReferenceExpression (CodeFieldReferenceExpression e) { if(e.TargetObject != null) { GenerateExpression(e.TargetObject); Output.Write("."); } OutputIdentifier(e.FieldName); } protected override void GenerateIndexerExpression (CodeIndexerExpression e) { GenerateExpression(e.TargetObject); Output.Write("("); OutputExpressionList(e.Indices); Output.Write(")"); } protected override void GenerateMethodInvokeExpression (CodeMethodInvokeExpression e) { GenerateMethodReferenceExpression(e.Method); Output.Write("("); OutputExpressionList(e.Parameters); Output.Write(")"); } protected override void GenerateMethodReferenceExpression (CodeMethodReferenceExpression e) { if(e.TargetObject != null) { GenerateExpression(e.TargetObject); Output.Write("."); } OutputIdentifier(e.MethodName); } protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression e) { Output.Write("New "); OutputType(e.CreateType); Output.Write("("); OutputExpressionList(e.Parameters); Output.Write(")"); } protected override void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression e) { if(e.TargetObject != null) { GenerateExpression(e.TargetObject); Output.Write("."); } OutputIdentifier(e.PropertyName); } protected override void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression e) { Output.Write("value"); } protected override void GenerateSnippetExpression (CodeSnippetExpression e) { Output.Write(e.Value); } protected override void GenerateThisReferenceExpression (CodeThisReferenceExpression e) { Output.Write("Me"); } protected override void GenerateVariableReferenceExpression (CodeVariableReferenceExpression e) { OutputIdentifier(e.VariableName); } // Generate various statement categories. protected override void GenerateAssignStatement (CodeAssignStatement e) { GenerateExpression(e.Left); Output.Write(" = "); GenerateExpression(e.Right); Output.WriteLine(); } protected override void GenerateAttachEventStatement (CodeAttachEventStatement e) { Output.Write("AddHandler "); GenerateExpression(e.Event); Output.Write(", "); GenerateExpression(e.Listener); Output.WriteLine(); } protected override void GenerateConditionStatement (CodeConditionStatement e) { Output.Write("If "); GenerateExpression(e.Condition); Output.WriteLine(" Then"); ++Indent; GenerateStatements(e.TrueStatements); --Indent; CodeStatementCollection stmts = e.FalseStatements; if(stmts.Count > 0 || Options.ElseOnClosing) { Output.WriteLine("Else"); ++Indent; GenerateStatements(stmts); --Indent; } Output.WriteLine("End If"); } protected override void GenerateExpressionStatement (CodeExpressionStatement e) { GenerateExpression(e.Expression); Output.WriteLine(); } protected override void GenerateGotoStatement (CodeGotoStatement e)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?