aboutsummaryrefslogtreecommitdiff
path: root/cmd/maleeni/compile.go
blob: 21e5058e59101ee159df0733ed5d1af27561539f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main

import (
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"

	"github.com/nihei9/maleeni/compiler"
	"github.com/nihei9/maleeni/spec"
	"github.com/spf13/cobra"
)

var compileFlags = struct {
	debug   *bool
	lexSpec *string
	compLv  *int
	output  *string
}{}

func init() {
	cmd := &cobra.Command{
		Use:     "compile",
		Short:   "Compile a lexical specification into a DFA",
		Long:    `compile takes a lexical specification and generates a DFA accepting the tokens described in the specification.`,
		Example: `  cat lexspec.json | maleeni compile > clexspec.json`,
		RunE:    runCompile,
	}
	compileFlags.lexSpec = cmd.Flags().StringP("lex-spec", "l", "", "lexical specification file path (default stdin)")
	compileFlags.compLv = cmd.Flags().Int("compression-level", compiler.CompressionLevelMax, "compression level")
	compileFlags.output = cmd.Flags().StringP("output", "o", "", "output file path (default stdout)")
	rootCmd.AddCommand(cmd)
}

func runCompile(cmd *cobra.Command, args []string) (retErr error) {
	lspec, err := readLexSpec(*compileFlags.lexSpec)
	if err != nil {
		return fmt.Errorf("Cannot read a lexical specification: %w", err)
	}

	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)
	if err != nil {
		return fmt.Errorf("Cannot write a compiled lexical specification: %w", err)
	}

	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 != "" {
		f, err := os.Open(path)
		if err != nil {
			return nil, fmt.Errorf("Cannot open the lexical specification file %s: %w", path, err)
		}
		defer f.Close()
		r = f
	}
	data, err := ioutil.ReadAll(r)
	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, path string) error {
	out, err := json.Marshal(clspec)
	if err != nil {
		return err
	}
	w := os.Stdout
	if path != "" {
		f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
		if err != nil {
			return fmt.Errorf("Cannot open the output file %s: %w", path, err)
		}
		defer f.Close()
		w = f
	}
	fmt.Fprintf(w, "%v\n", string(out))
	return nil
}