aboutsummaryrefslogtreecommitdiff
path: root/cli/cmd/lex.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/lex.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/lex.go')
-rw-r--r--cli/cmd/lex.go43
1 files changed, 24 insertions, 19 deletions
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
+}