aboutsummaryrefslogtreecommitdiff
path: root/cli/cmd/compile.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2021-04-17 22:51:06 +0900
committerRyo Nihei <nihei.dev@gmail.com>2021-04-17 22:51:06 +0900
commit88f83624dc6d7c3b66a34c7c3f414719530e421f (patch)
tree31c0d8c966a4eaf98dd1670855298a8b4e0969c2 /cli/cmd/compile.go
parentChange the lexical specs of regexp and define concrete syntax error values (diff)
downloadtre-88f83624dc6d7c3b66a34c7c3f414719530e421f.tar.gz
tre-88f83624dc6d7c3b66a34c7c3f414719530e421f.tar.xz
Add validation of lexical specs and improve error messages
Diffstat (limited to 'cli/cmd/compile.go')
-rw-r--r--cli/cmd/compile.go42
1 files changed, 28 insertions, 14 deletions
diff --git a/cli/cmd/compile.go b/cli/cmd/compile.go
index 7815129..6ad64b6 100644
--- a/cli/cmd/compile.go
+++ b/cli/cmd/compile.go
@@ -25,23 +25,16 @@ func init() {
}
func runCompile(cmd *cobra.Command, args []string) (retErr error) {
- var lspec *spec.LexSpec
- {
- data, err := ioutil.ReadAll(os.Stdin)
- if err != nil {
- return err
- }
- lspec = &spec.LexSpec{}
- err = json.Unmarshal(data, lspec)
- if err != nil {
- return err
- }
+ lspec, err := readLexSpec()
+ if err != nil {
+ return fmt.Errorf("Cannot read a lexical specification: %w", err)
}
var w io.Writer
{
- f, err := os.OpenFile("maleeni-compile.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
+ fileName := "maleeni-compile.log"
+ f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
- return err
+ return fmt.Errorf("Cannot open the log file %s: %w", fileName, err)
}
defer f.Close()
w = f
@@ -62,11 +55,32 @@ Date time: %v
if err != nil {
return err
}
+ err = writeCompiledLexSpec(clspec)
+ if err != nil {
+ return fmt.Errorf("Cannot write a compiled lexical specification: %w", err)
+ }
+
+ return nil
+}
+
+func readLexSpec() (*spec.LexSpec, error) {
+ data, err := ioutil.ReadAll(os.Stdin)
+ if err != nil {
+ return nil, err
+ }
+ lspec := &spec.LexSpec{}
+ err = json.Unmarshal(data, lspec)
+ if err != nil {
+ return nil, err
+ }
+ return lspec, nil
+}
+
+func writeCompiledLexSpec(clspec *spec.CompiledLexSpec) error {
out, err := json.Marshal(clspec)
if err != nil {
return err
}
fmt.Fprintf(os.Stdout, "%v\n", string(out))
-
return nil
}