Esempio n. 1
0
        private void ReadFile(string path)
        {
            string text = File.ReadAllText(path);

            output = "";

            //Show text
            labelFileName.Text = Path.GetFileName(path);
            sourceCodeBox.Text = text.Replace("\0", "<null>"); //Prevenir que el null corte el texto

            //Analizar tokens
            Lexer.Lexer lex    = new Lexer.Lexer();
            var         tokens = lex.getTokens(text);

            foreach (var item in tokens)
            {
                string line = "";

                switch (item.tokenType)
                {
                case TokenType.Enter:
                case TokenType.WhiteSpace:
                case TokenType.Block_Comments:
                case TokenType.Comments:
                    line = "";
                    break;

                case TokenType.Error:
                    line = $"*** Error line {item.location.row}.*** Unrecognized element: {item.value}\r\n";
                    break;

                case TokenType.Error_Comment:
                    line = $"*** Error line {item.location.row}.*** Unfinished comment.\r\n";
                    break;

                case TokenType.Error_Length:
                    line = $"*** Warning: Max id length exceeded at line {item.location.row}.***\r\n";
                    break;

                case TokenType.Error_EOFComment:
                    line = $"*** Error line {item.location.row}.*** EOF in comment.\r\n";
                    break;

                case TokenType.Error_nullString:
                    line = $"*** Error line {item.location.row}.*** Null character in string.\r\n";
                    break;

                case TokenType.Error_null:
                    line = $"*** Error line {item.location.row}.*** Null character.\r\n";
                    break;

                case TokenType.Error_EOFstring:
                    line = $"*** Error line {item.location.row}.*** EOF in string.\r\n";
                    break;

                case TokenType.Error_String:
                    line = $"*** Error line {item.location.row}.*** Unfinished string.\r\n";
                    break;

                case TokenType.Error_UnpairedComment:
                    line = $"*** Error line {item.location.row}.*** Unpaired comment.\r\n";
                    break;

                default:
                    //line =
                    //$"{item.value} \t\t\t>> {item.tokenType} Line: {item.location.row}  Col: [{item.location.firstCol}:{item.location.lastCol}]\r\n";
                    break;
                }

                output += line;
            }
            Queue <Token> tokensQueue = lex.ListToQueue(tokens);

            Parser.Descendente.Parser pars1 = new Parser.Descendente.Parser(tokensQueue);
            ParserReport parserReport       = pars1.getReport();

            this.parserRep = parserReport;

            //ANALIZADOR SINTACTICO

            output += parserReport.isCorrect ? "Todo bien :)\n" : "Oh! No! Hay un error\n";

            if (!parserReport.isCorrect)
            {
                TokenLocation location = new TokenLocation(0, 0, 0);
                foreach (var item in parserReport.Errors)
                {
                    if (location != item.location)
                    {
                        if (item.errorType == ErrorType.semantico)
                        {
                            output += $"*** Error semántico en linea {item.location.row}: {item.errorMessage} ***\n";
                        }
                        else
                        {
                            output += $"*** Error sintáctico en linea {item.location.row}: Token inesperado {item.value} ***\n";
                        }
                    }
                    location = item.location;
                }
            }

            //Show output
            this.outputBox.Text       = output.Replace("\0", "<null>"); //Prevenir que el null corte el texto
            this.outputBox.Visible    = true;
            this.symbolButtom.Visible = true;

            //Color errors
            for (int i = 0; i < outputBox.Lines.Length; i++)
            {
                string line = outputBox.Lines[i];

                if (line.Contains("***"))
                {
                    outputBox.Select(outputBox.GetFirstCharIndexFromLine(i), line.Length);
                    outputBox.SelectionColor = Color.Red;
                }
            }
        }