示例#1
0
        /// <summary>
        /// Lexing aka Tokenizing and parsing the assembly.
        /// </summary>
        private bool FirstPass(StreamReader readStream)
        {
            bool   BuildSuccessful = true;
            int    LineNumber      = 0;
            string CurrentLine;

            Token[] currentTokens;
            Console.WriteLine("Lexical analysis and parsing...");
            while (!readStream.EndOfStream)
            {
                CurrentLine = String.Empty;
                CurrentLine = readStream.ReadLine();
                CurrentLine.Trim();
                LineNumber += 1;

                if (CurrentLine == String.Empty)
                {
                    continue;
                }

                try
                {
                    //FIRST PASS
                    currentTokens = YParser.ParseString(CurrentLine);
                    if (currentTokens == null || currentTokens.Length == 0)
                    {
                        continue;
                    }
                    YFile.AddLine(currentTokens);
                }
                catch (FoundUnexpectedToken e)
                {
                    //Console.WriteLine("ERROR : Unexpected Token " + e.token1.Text + " On Line : " + LineNumber + "|" + CurrentLine);
                    if (e.token1 != null)
                    {
                        Console.WriteLine($"ERROR : Unexpected Token {e.token1.Text} On Line : {LineNumber} | {CurrentLine}");
                    }
                    else
                    {
                        Console.WriteLine($"ERROR : Unexpected Token On Line : {LineNumber} | {CurrentLine}");
                    }
                }
                catch (AssemblerException e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("ERROR at stage : " + Enum.GetName(typeof(EnumAssemblerStages), e._stage) + " " + e.Message);
                    BuildSuccessful = false;
                }
                catch (TokenAccessException e)
                {
                    BuildSuccessful = false;

                    Console.ForegroundColor = ConsoleColor.Red;
                    if (e._Tkn != null)
                    {
                        Console.WriteLine("ERROR accessing token: " + e.Message);
                        Console.WriteLine(e._Tkn.TokenInfoString());
                    }
                    else
                    {
                        Console.WriteLine("ERROR accessing token: " + e.Message);
                    }
                }
                catch (Exception e)
                {
                    BuildSuccessful = false;

                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    Console.ResetColor();
                }
            }

            return(BuildSuccessful);
        }