fix:修复前端bug (#83)
Co-authored-by: jackfiled <xcrenchangjun@outlook.com> Reviewed-on: PostGuard/Canon#83 Co-authored-by: ichirinko <1621543655@qq.com> Co-committed-by: ichirinko <1621543655@qq.com>
This commit is contained in:
		
							
								
								
									
										19
									
								
								Canon.Core/Exceptions/CanonException.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								Canon.Core/Exceptions/CanonException.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
namespace Canon.Core.Exceptions;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// 编译器中的统一异常基类
 | 
			
		||||
/// </summary>
 | 
			
		||||
public class CanonException : Exception
 | 
			
		||||
{
 | 
			
		||||
    public CanonException()
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public CanonException(string message) : base(message)
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public CanonException(string message, Exception innerException) : base(message, innerException)
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -8,7 +8,7 @@ namespace Canon.Core.Exceptions;
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// 语法分析中引发的异常
 | 
			
		||||
/// </summary>
 | 
			
		||||
public class GrammarException : Exception
 | 
			
		||||
public class GrammarException : CanonException
 | 
			
		||||
{
 | 
			
		||||
    public override string Message { get; }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,32 +3,32 @@ using Enums;
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// 词法分析中引发的异常
 | 
			
		||||
/// </summary>
 | 
			
		||||
public class LexemeException : Exception
 | 
			
		||||
public class LexemeException : CanonException
 | 
			
		||||
{
 | 
			
		||||
    public LexemeErrorType ErrorType { get; }
 | 
			
		||||
 | 
			
		||||
    public uint Line { get; }
 | 
			
		||||
 | 
			
		||||
    public uint CharPosition { get; }
 | 
			
		||||
    public LexemeException()  { }
 | 
			
		||||
 | 
			
		||||
    public LexemeException(string message) : base(message) { }
 | 
			
		||||
 | 
			
		||||
    public LexemeException(string message, Exception innerException) :
 | 
			
		||||
        base(message, innerException) { }
 | 
			
		||||
    private readonly string _message;
 | 
			
		||||
 | 
			
		||||
    /// <param name="errorType">错误类型</param>
 | 
			
		||||
    /// <param name="line">单词的行号</param>
 | 
			
		||||
    /// <param name="charPosition">单词的列号</param>
 | 
			
		||||
    /// <param name="message">错误信息</param>
 | 
			
		||||
    public LexemeException(LexemeErrorType errorType, uint line, uint charPosition, string message) :
 | 
			
		||||
        base("line:" + line + ", charPosition:" + charPosition + " :" + message)
 | 
			
		||||
    public LexemeException(LexemeErrorType errorType, uint line, uint charPosition, string message)
 | 
			
		||||
    {
 | 
			
		||||
        ErrorType = errorType;
 | 
			
		||||
        Line = line;
 | 
			
		||||
        CharPosition = charPosition;
 | 
			
		||||
        _message = message;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public override string Message => ToString();
 | 
			
		||||
 | 
			
		||||
    public override string ToString()
 | 
			
		||||
    {
 | 
			
		||||
        return $"LexemeException: ErrorType={ErrorType}, Line={Line}, CharPosition={CharPosition}, Message={Message}\n";
 | 
			
		||||
        return $"LexemeException: ErrorType={ErrorType}, Line={Line}, CharPosition={CharPosition}, Message={_message}\n";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ using Canon.Server.Entities;
 | 
			
		||||
using Canon.Server.Services;
 | 
			
		||||
using Microsoft.AspNetCore.Mvc;
 | 
			
		||||
using Microsoft.EntityFrameworkCore;
 | 
			
		||||
using MongoDB.Bson;
 | 
			
		||||
 | 
			
		||||
namespace Canon.Server.Controllers;
 | 
			
		||||
 | 
			
		||||
@@ -35,7 +36,7 @@ public class CompilerController(CompileDbContext dbContext, CompilerService comp
 | 
			
		||||
    public async Task<ActionResult<CompileResponse>> GetResponse(string compileId)
 | 
			
		||||
    {
 | 
			
		||||
        CompileResult? result = await (from item in dbContext.CompileResults.AsNoTracking()
 | 
			
		||||
            where item.CompileId == compileId
 | 
			
		||||
            where item.Id == new ObjectId(compileId)
 | 
			
		||||
            select item).FirstOrDefaultAsync();
 | 
			
		||||
 | 
			
		||||
        if (result is null)
 | 
			
		||||
@@ -59,7 +60,7 @@ public class CompilerController(CompileDbContext dbContext, CompilerService comp
 | 
			
		||||
    public async Task<IActionResult> DeleteCompileResult(string compileId)
 | 
			
		||||
    {
 | 
			
		||||
        CompileResult? result = await (from item in dbContext.CompileResults
 | 
			
		||||
            where item.CompileId == compileId
 | 
			
		||||
            where item.Id == new ObjectId(compileId)
 | 
			
		||||
            select item).FirstOrDefaultAsync();
 | 
			
		||||
 | 
			
		||||
        if (result is null)
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,9 @@ public class CompileResponse
 | 
			
		||||
    [Required]
 | 
			
		||||
    public string Id { get; set; }
 | 
			
		||||
 | 
			
		||||
    [Required]
 | 
			
		||||
    public bool Error { get; set; }
 | 
			
		||||
 | 
			
		||||
    [Required]
 | 
			
		||||
    public string SourceCode { get; set; }
 | 
			
		||||
 | 
			
		||||
@@ -35,7 +38,8 @@ public class CompileResponse
 | 
			
		||||
 | 
			
		||||
    public CompileResponse(CompileResult result)
 | 
			
		||||
    {
 | 
			
		||||
        Id = result.CompileId;
 | 
			
		||||
        Id = result.Id.ToString();
 | 
			
		||||
        Error = result.Error;
 | 
			
		||||
        SourceCode = result.SourceCode;
 | 
			
		||||
        CompiledCode = result.CompiledCode;
 | 
			
		||||
        ImageAddress = $"/api/file/{result.SytaxTreeImageFilename}";
 | 
			
		||||
 
 | 
			
		||||
@@ -7,8 +7,7 @@ public class CompileResult
 | 
			
		||||
{
 | 
			
		||||
    public ObjectId Id { get; set; }
 | 
			
		||||
 | 
			
		||||
    [MaxLength(40)]
 | 
			
		||||
    public string CompileId { get; set; } = string.Empty;
 | 
			
		||||
    public bool Error { get; set; }
 | 
			
		||||
 | 
			
		||||
    public string SourceCode { get; set; } = string.Empty;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,6 @@ builder.Services.AddDbContext<CompileDbContext>(options =>
 | 
			
		||||
    options.UseMongoDB(connectionString, "Canon");
 | 
			
		||||
});
 | 
			
		||||
builder.Services.AddGridFs(connectionString, "Canon");
 | 
			
		||||
builder.Services.AddSingleton<ICompilerLogger, CompilerLogger>();
 | 
			
		||||
builder.Services.AddTransient<ILexer, Lexer>();
 | 
			
		||||
builder.Services.AddSingleton<IGrammarParser>(
 | 
			
		||||
    _ => GeneratedGrammarParser.Instance);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
using Canon.Core.Abstractions;
 | 
			
		||||
using Canon.Core.Exceptions;
 | 
			
		||||
using Canon.Core.LexicalParser;
 | 
			
		||||
using Canon.Core.SemanticParser;
 | 
			
		||||
using Canon.Core.SyntaxNodes;
 | 
			
		||||
@@ -6,6 +7,7 @@ using Canon.Server.DataTransferObjects;
 | 
			
		||||
using Canon.Server.Entities;
 | 
			
		||||
using Canon.Server.Models;
 | 
			
		||||
using Microsoft.EntityFrameworkCore;
 | 
			
		||||
using MongoDB.Bson;
 | 
			
		||||
 | 
			
		||||
namespace Canon.Server.Services;
 | 
			
		||||
 | 
			
		||||
@@ -13,7 +15,6 @@ public class CompilerService(
 | 
			
		||||
    ILexer lexer,
 | 
			
		||||
    IGrammarParser grammarParser,
 | 
			
		||||
    SyntaxTreeTraveller traveller,
 | 
			
		||||
    ICompilerLogger compilerLogger,
 | 
			
		||||
    CompileDbContext dbContext,
 | 
			
		||||
    GridFsService gridFsService,
 | 
			
		||||
    SyntaxTreePresentationService syntaxTreePresentationService,
 | 
			
		||||
@@ -34,19 +35,44 @@ public class CompilerService(
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        CodeReader reader = new(sourceCode);
 | 
			
		||||
 | 
			
		||||
        ProgramStruct root;
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            IEnumerable<SemanticToken> tokens = lexer.Tokenize(reader);
 | 
			
		||||
        ProgramStruct root = grammarParser.Analyse(tokens);
 | 
			
		||||
            root = grammarParser.Analyse(tokens);
 | 
			
		||||
        }
 | 
			
		||||
        catch (CanonException e)
 | 
			
		||||
        {
 | 
			
		||||
            CompileResult errorResult = new()
 | 
			
		||||
            {
 | 
			
		||||
                Id = ObjectId.GenerateNewId(),
 | 
			
		||||
                Error = true,
 | 
			
		||||
                SourceCode = sourceCode.Code,
 | 
			
		||||
                CompiledCode = string.Empty,
 | 
			
		||||
                SytaxTreeImageFilename = string.Empty,
 | 
			
		||||
                CompileTime = DateTime.Now,
 | 
			
		||||
                CompileInformation = e.Message
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            await dbContext.CompileResults.AddAsync(errorResult);
 | 
			
		||||
            await dbContext.SaveChangesAsync();
 | 
			
		||||
 | 
			
		||||
            return new CompileResponse(errorResult);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        await using Stream imageStream = syntaxTreePresentationService.Present(root);
 | 
			
		||||
        string filename = await gridFsService.UploadStream(imageStream, "image/png");
 | 
			
		||||
 | 
			
		||||
        ICompilerLogger compilerLogger = new CompilerLogger();
 | 
			
		||||
        CodeGeneratorVisitor visitor = new(compilerLogger);
 | 
			
		||||
        traveller.Travel(root, visitor);
 | 
			
		||||
 | 
			
		||||
        CompileResult result = new()
 | 
			
		||||
        {
 | 
			
		||||
            Id = ObjectId.GenerateNewId(),
 | 
			
		||||
            Error = visitor.IsError,
 | 
			
		||||
            SourceCode = sourceCode.Code,
 | 
			
		||||
            CompileId = Guid.NewGuid().ToString(),
 | 
			
		||||
            CompiledCode = visitor.Builder.Build(),
 | 
			
		||||
            SytaxTreeImageFilename = filename,
 | 
			
		||||
            CompileTime = DateTime.Now,
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,7 @@ export function Index() {
 | 
			
		||||
    const [inputValue, setInputValue] = useState('');
 | 
			
		||||
    const [outputValue, setOutputValue] = useState<openapi.components["schemas"]["CompileResponse"]>({
 | 
			
		||||
        compiledCode: "",
 | 
			
		||||
        error: false,
 | 
			
		||||
        sourceCode: "",
 | 
			
		||||
        id: "",
 | 
			
		||||
        imageAddress: "",
 | 
			
		||||
@@ -32,6 +33,7 @@ export function Index() {
 | 
			
		||||
            setInputValue("");
 | 
			
		||||
            setOutputValue({
 | 
			
		||||
                compiledCode: "",
 | 
			
		||||
                error: false,
 | 
			
		||||
                sourceCode: "",
 | 
			
		||||
                id: "",
 | 
			
		||||
                imageAddress: "pic/uncompiled.png",
 | 
			
		||||
@@ -70,17 +72,20 @@ export function Index() {
 | 
			
		||||
            body: {
 | 
			
		||||
                code: inputValue
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        if (data !== undefined) {
 | 
			
		||||
            setOutputValue(data);
 | 
			
		||||
        if (data == undefined) {
 | 
			
		||||
            enqueueSnackbar("内部错误", { variant: "error", anchorOrigin: { vertical: 'bottom', horizontal: 'right' } });
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!data.error) {
 | 
			
		||||
            enqueueSnackbar("编译成功", { variant: "success", anchorOrigin: { vertical: 'bottom', horizontal: 'right' } });
 | 
			
		||||
            navigate(`/${data.id}`, {})
 | 
			
		||||
 | 
			
		||||
        } else {
 | 
			
		||||
            // error
 | 
			
		||||
            enqueueSnackbar("编译失败", { variant: "error", anchorOrigin: { vertical: 'bottom', horizontal: 'right' } });
 | 
			
		||||
        }
 | 
			
		||||
        navigate(`/${data.id}`, {})
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function historyButtonClick() {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								Canon.Server/client-app/src/openapi.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								Canon.Server/client-app/src/openapi.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -115,6 +115,7 @@ export interface components {
 | 
			
		||||
  schemas: {
 | 
			
		||||
    CompileResponse: {
 | 
			
		||||
      id: string;
 | 
			
		||||
      error: boolean;
 | 
			
		||||
      sourceCode: string;
 | 
			
		||||
      compiledCode: string;
 | 
			
		||||
      imageAddress: string;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user