📄 main.cs
字号:
s_eQuitAfterStage = EStage.cParser;
} else if (stOption == "Resolve")
{
s_eQuitAfterStage = EStage.cSymbolResolution;
} else {
throw new Blue.Utilities.OptionErrorException("Expected 'Lexer', 'Parser', or 'Resolve'");
}
}
private static bool s_fIsQuitAfterStage;
private static EStage s_eQuitAfterStage;
private static bool ShouldDebugQuit(EStage eCurrentStage)
{
return s_fIsQuitAfterStage && (s_eQuitAfterStage == eCurrentStage);
}
private enum EStage {
cLexer,
cParser,
cSymbolResolution,
cCodeGen,
}
#endregion
#region Errors
//-----------------------------------------------------------------------------
// Error handling
//-----------------------------------------------------------------------------
internal enum ErrorCodes
{
cInternalError, // Last resort; something bad happened...
cNoSourceFiles, // no source specified on command line
cMissingAssembly, // can't load an assembly
cMissingSourceFile, // can't load a source file
cUnrecognizedOption, // Specified option is unrecognized
cCantOpenResponseFile, // The response file can't be found
cOptionError, // General option error
}
internal class GeneralErrorException : ErrorException
{
internal GeneralErrorException(ErrorCodes c, FileRange location, string s) :
base (c, location, s)
{
// All General errors will come through this body.
}
}
// Internal error at some unknown phase. This is really just a top-level
// catch right before an exception would go unhandled.
// Pass in the culprit exception.
protected static void PrintError_InternalError(System.Exception e)
{
m_ErrorLog.PrintError(new GeneralErrorException(
ErrorCodes.cInternalError, null,
"Internal error:" + e.GetType().ToString() + "," + e.Message
));
string stStackTrace = e.StackTrace;
Console.WriteLine("Stack Trace:\n{0}", stStackTrace);
Debug.Assert(false, "Should never get an Internal Error");
}
// Each sub-system should protect itself with this.
public static void PrintError_InternalError(System.Exception e, string stPhase)
{
m_ErrorLog.PrintError(new GeneralErrorException(
ErrorCodes.cInternalError, null,
"Internal error during '"+stPhase+"' phase:" + e.GetType().ToString() + "," + e.Message
));
string stStackTrace = e.StackTrace;
Console.WriteLine("Stack Trace for internal error:\n{0}", stStackTrace);
Debug.Assert(false, "Should never get an Internal Error");
}
public static void PrintError_NoSourceFiles()
{
m_ErrorLog.PrintError(new GeneralErrorException(
ErrorCodes.cNoSourceFiles, null, "No source files"
));
}
public static void PrintError_CantFindAssembly(string stName)
{
m_ErrorLog.PrintError(new GeneralErrorException(
ErrorCodes.cMissingAssembly, null,
"Can't find assembly '"+stName + "'"
));
}
public static void PrintError_CantFindSourceFile(string stFilename, System.Exception e)
{
m_ErrorLog.PrintError(new GeneralErrorException(
ErrorCodes.cInternalError, null,
"Can't open source file '" + stFilename + "'." + e.Message
));
}
#endregion
} // end class Driver
#endregion Driver
#region Logging Facility
//-----------------------------------------------------------------------------
// Error logging
//
//-----------------------------------------------------------------------------
/// <summary>
/// The <c>Log</c> class allows all other subsystems to emit log messages.
/// This is mostly a debugging feature.
/// </summary>
/// <remarks>
/// Activated by command line arguments (independent of the IOption system).
/// See <see cref="InitLogging"/> for details.
/// </remarks>
public class Log
{
/// <summary> Control what gets logged. </summary>
/// <remarks>
/// Each member get log to is own <see cref="System.IO.TextWriter"/>.
/// The buffer may go to the console, a file, or nothing.
/// </remarks>
public enum LF
{
/// <summary>Verbose information of use to an end-user.</summary>
Verbose,
/// <summary> Information from the parser </summary>
Parser,
/// <summary> Information during resolution </summary>
Resolve,
/// <summary> Information during codegen </summary>
CodeGen,
/// <summary> All information </summary>
All,
Invalid
}
//static System.IO.TextWriter m_out;
static System.IO.TextWriter [] m_out;
static System.IO.TextWriter GetStream(LF e)
{
return m_out[(int) e];
}
static void SetStream(LF e, System.IO.TextWriter w)
{
if (w == null)
{
w = System.IO.TextWriter.Null;
}
m_out[(int) e] = w;
}
// Create the 'all' stream
static void CreateAllStream()
{
// @todo - create a Multi-cast stream of all the others
SetStream(LF.All, GetStream(LF.CodeGen));
}
/// <summary> Initialize the logging system. </summary>
/// <param name="stArgs">The command line arguments.</param>
/// <remarks>
/// Do a prelim sweep of command line args to set streams.
/// We can't use the ErrorHandling subsystem or the Options Subsystem because
/// logging is a more fundamental subsystem that the others depend on.
/// <para>Activate via:
/// '-l:XXXXX' where XXXXX is {Codegen, Verbose, Parser, Resolve }
/// Can specify multiple switches</para>
/// </remarks>
public static void InitLogging(string [] stArgs)
{
//m_out = new System.IO.StreamWriter("blue.log");
// Set all streams to null by default
m_out = new System.IO.TextWriter[((int) LF.All) + 1];
for(int i = 0; i < m_out.Length; i++)
m_out[i] = System.IO.TextWriter.Null;
// Check command line switches
for(int i = 0; i < stArgs.Length; i++)
{
string st = stArgs[i];
if (st.Length < 3)
continue;
if (st[0] != '-' && st[0] != '/')
continue;
if (st[1] != 'l')
continue;
if (st[2] != ':')
continue;
string stSub = st.Substring(3);
LF lf = LF.Invalid;
if (stSub == "Codegen") lf = LF.CodeGen;
else if (stSub == "Verbose") lf = LF.Verbose;
else if (stSub == "Parser") lf = LF.Parser;
else if (stSub == "Resolve") lf = LF.Resolve;
if (lf != LF.Invalid)
SetStream(lf, Console.Out);
// Clear option so that it doesn't get confused with a real option
stArgs[i] = "";
}
//SetStream(LF.Verbose, Console.Out);
//SetStream(LF.CodeGen, Console.Out);
CreateAllStream();
WriteLine(LF.All, "Logging services initialized");
}
public static void TerminateLogging()
{
WriteLine(LF.All, "Terminating Logging services");
for(int i = 0; i < m_out.Length; i++)
{
m_out[i].Close();
m_out[i] = null;
}
}
// Helper to flatten an array into a single string
public static string F(params object [] args)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append('{');
for(int i = 0; i < args.Length; i++)
{
if (i != 0)
sb.Append(',');
sb.Append(args[i]);
}
sb.Append('}');
return sb.ToString();
}
#region WriteLines
public static void WriteLine(LF e, string stFormat)
{
GetStream(e).WriteLine(stFormat);
}
public static void WriteLine(LF e, string stFormat, params object [] arg)
{
GetStream(e).WriteLine(stFormat, arg);
}
#endregion
}
#endregion Logging Facility
} // end namespace Blue
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -