aboutsummaryrefslogtreecommitdiff
path: root/cli/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cli/cmd')
-rw-r--r--cli/cmd/compile.go42
-rw-r--r--cli/cmd/lex.go43
2 files changed, 52 insertions, 33 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
}
diff --git a/cli/cmd/lex.go b/cli/cmd/lex.go
index 14fbc01..2c0be27 100644
--- a/cli/cmd/lex.go
+++ b/cli/cmd/lex.go
@@ -27,28 +27,16 @@ As use ` + "`maleeni compile`" + `, you can generate the specification.`,
}
func runLex(cmd *cobra.Command, args []string) (retErr error) {
- var clspec *spec.CompiledLexSpec
- {
- clspecPath := args[0]
- f, err := os.Open(clspecPath)
- if err != nil {
- return err
- }
- data, err := ioutil.ReadAll(f)
- if err != nil {
- return err
- }
- clspec = &spec.CompiledLexSpec{}
- err = json.Unmarshal(data, clspec)
- if err != nil {
- return err
- }
+ clspec, err := readCompiledLexSpec(args[0])
+ if err != nil {
+ return fmt.Errorf("Cannot read a compiled lexical specification: %w", err)
}
var w io.Writer
{
- f, err := os.OpenFile("maleeni-lex.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
+ fileName := "maleeni-lex.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
@@ -76,7 +64,7 @@ Date time: %v
}
data, err := json.Marshal(tok)
if err != nil {
- fmt.Fprintf(os.Stderr, "failed to marshal a token; token: %v, error: %v\n", tok, err)
+ return fmt.Errorf("failed to marshal a token; token: %v, error: %v\n", tok, err)
}
fmt.Fprintf(os.Stdout, "%v\n", string(data))
if tok.EOF {
@@ -86,3 +74,20 @@ Date time: %v
return nil
}
+
+func readCompiledLexSpec(path string) (*spec.CompiledLexSpec, error) {
+ f, err := os.Open(path)
+ if err != nil {
+ return nil, err
+ }
+ data, err := ioutil.ReadAll(f)
+ if err != nil {
+ return nil, err
+ }
+ clspec := &spec.CompiledLexSpec{}
+ err = json.Unmarshal(data, clspec)
+ if err != nil {
+ return nil, err
+ }
+ return clspec, nil
+}