From d595194791483a71c5afaff2aa3f4b575a9d22b7 Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Thu, 9 Dec 2021 02:38:12 +0900 Subject: Use new parser and DFA compiler --- cmd/maleeni/compile.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'cmd') diff --git a/cmd/maleeni/compile.go b/cmd/maleeni/compile.go index c7a8b59..21e5058 100644 --- a/cmd/maleeni/compile.go +++ b/cmd/maleeni/compile.go @@ -3,8 +3,10 @@ package main import ( "encoding/json" "fmt" + "io" "io/ioutil" "os" + "strings" "github.com/nihei9/maleeni/compiler" "github.com/nihei9/maleeni/spec" @@ -38,8 +40,17 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) { return fmt.Errorf("Cannot read a lexical specification: %w", err) } - clspec, err := compiler.Compile(lspec, compiler.CompressionLevel(*compileFlags.compLv)) + clspec, err, cerrs := compiler.Compile(lspec, compiler.CompressionLevel(*compileFlags.compLv)) 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 fmt.Errorf(b.String()) + } return err } err = writeCompiledLexSpec(clspec, *compileFlags.output) @@ -50,6 +61,16 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) { return nil } +func writeCompileError(w io.Writer, cerr *compiler.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) + } +} + func readLexSpec(path string) (*spec.LexSpec, error) { r := os.Stdin if path != "" { -- cgit v1.2.3