diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2022-03-30 21:25:40 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2022-03-30 21:35:12 +0900 |
commit | b565c7ddb7cbbf2ccfb8653c9a77140d83e02c55 (patch) | |
tree | 32d16c248f238407df3a063f708fbc4a5d7f811c /grammar/grammar.go | |
parent | Allow an alternative to have multiple directives (diff) | |
download | urubu-b565c7ddb7cbbf2ccfb8653c9a77140d83e02c55.tar.gz urubu-b565c7ddb7cbbf2ccfb8653c9a77140d83e02c55.tar.xz |
Upgrade maleeni to v0.6.0
Diffstat (limited to 'grammar/grammar.go')
-rw-r--r-- | grammar/grammar.go | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/grammar/grammar.go b/grammar/grammar.go index 2cdf2b3..1a09a22 100644 --- a/grammar/grammar.go +++ b/grammar/grammar.go @@ -3,6 +3,7 @@ package grammar import ( "encoding/json" "fmt" + "io" "os" "strings" @@ -1074,8 +1075,17 @@ func Compile(gram *Grammar, opts ...CompileOption) (*spec.CompiledGrammar, error opt(config) } - lexSpec, err := mlcompiler.Compile(gram.lexSpec, mlcompiler.CompressionLevel(mlcompiler.CompressionLevelMax)) + lexSpec, err, cErrs := mlcompiler.Compile(gram.lexSpec, mlcompiler.CompressionLevel(mlcompiler.CompressionLevelMax)) if err != nil { + if len(cErrs) > 0 { + var b strings.Builder + writeCompileError(&b, cErrs[0]) + for _, cerr := range cErrs[1:] { + fmt.Fprintf(&b, "\n") + writeCompileError(&b, cerr) + } + return nil, fmt.Errorf(b.String()) + } return nil, err } @@ -1274,3 +1284,13 @@ func Compile(gram *Grammar, opts ...CompileOption) (*spec.CompiledGrammar, error }, }, nil } + +func writeCompileError(w io.Writer, cErr *mlcompiler.CompileError) { + if cErr.Fragment { + fmt.Fprintf(w, "fragment ") + } + fmt.Fprintf(w, "%v: %v", cErr.Kind, cErr.Cause) + if cErr.Detail != "" { + fmt.Fprintf(w, ": %v", cErr.Detail) + } +} |