diff options
-rw-r--r-- | cli/cmd/compile.go | 71 | ||||
-rw-r--r-- | cli/cmd/lex.go | 66 | ||||
-rw-r--r-- | compiler/compiler.go | 6 | ||||
-rw-r--r-- | driver/lexer.go | 30 |
4 files changed, 117 insertions, 56 deletions
diff --git a/cli/cmd/compile.go b/cli/cmd/compile.go index 6ad64b6..f2d56fa 100644 --- a/cli/cmd/compile.go +++ b/cli/cmd/compile.go @@ -3,7 +3,6 @@ package cmd import ( "encoding/json" "fmt" - "io" "io/ioutil" "os" "time" @@ -13,6 +12,12 @@ import ( "github.com/spf13/cobra" ) +var compileFlags = struct { + debug *bool + lexSpec *string + output *string +}{} + func init() { cmd := &cobra.Command{ Use: "compile", @@ -21,41 +26,47 @@ func init() { Example: ` cat lexspec.json | maleeni compile > clexspec.json`, RunE: runCompile, } + compileFlags.debug = cmd.Flags().BoolP("debug", "d", false, "enable logging") + compileFlags.lexSpec = cmd.Flags().StringP("lex-spec", "l", "", "lexical specification file path (default: stdin)") + 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() + lspec, err := readLexSpec(*compileFlags.lexSpec) if err != nil { return fmt.Errorf("Cannot read a lexical specification: %w", err) } - var w io.Writer - { + + var opts []compiler.CompilerOption + if *compileFlags.debug { fileName := "maleeni-compile.log" f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { return fmt.Errorf("Cannot open the log file %s: %w", fileName, err) } defer f.Close() - w = f - } - fmt.Fprintf(w, `maleeni compile starts. + fmt.Fprintf(f, `maleeni compile starts. Date time: %v --- `, time.Now().Format(time.RFC3339)) - defer func() { - fmt.Fprintf(w, "---\n") - if retErr != nil { - fmt.Fprintf(w, "maleeni compile failed: %v\n", retErr) - } else { - fmt.Fprintf(w, "maleeni compile succeeded.\n") - } - }() - clspec, err := compiler.Compile(lspec, compiler.EnableLogging(w)) + defer func() { + fmt.Fprintf(f, "---\n") + if retErr != nil { + fmt.Fprintf(f, "maleeni compile failed: %v\n", retErr) + } else { + fmt.Fprintf(f, "maleeni compile succeeded.\n") + } + }() + + opts = append(opts, compiler.EnableLogging(f)) + } + + clspec, err := compiler.Compile(lspec, opts...) if err != nil { return err } - err = writeCompiledLexSpec(clspec) + err = writeCompiledLexSpec(clspec, *compileFlags.output) if err != nil { return fmt.Errorf("Cannot write a compiled lexical specification: %w", err) } @@ -63,8 +74,17 @@ Date time: %v return nil } -func readLexSpec() (*spec.LexSpec, error) { - data, err := ioutil.ReadAll(os.Stdin) +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 } @@ -76,11 +96,20 @@ func readLexSpec() (*spec.LexSpec, error) { return lspec, nil } -func writeCompiledLexSpec(clspec *spec.CompiledLexSpec) error { +func writeCompiledLexSpec(clspec *spec.CompiledLexSpec, path string) error { out, err := json.Marshal(clspec) if err != nil { return err } - fmt.Fprintf(os.Stdout, "%v\n", string(out)) + 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 } diff --git a/cli/cmd/lex.go b/cli/cmd/lex.go index 2c0be27..f47f503 100644 --- a/cli/cmd/lex.go +++ b/cli/cmd/lex.go @@ -3,7 +3,6 @@ package cmd import ( "encoding/json" "fmt" - "io" "io/ioutil" "os" "time" @@ -13,6 +12,12 @@ import ( "github.com/spf13/cobra" ) +var lexFlags = struct { + debug *bool + source *string + output *string +}{} + func init() { cmd := &cobra.Command{ Use: "lex clexspec", @@ -23,6 +28,9 @@ As use ` + "`maleeni compile`" + `, you can generate the specification.`, Args: cobra.ExactArgs(1), RunE: runLex, } + lexFlags.debug = cmd.Flags().BoolP("debug", "d", false, "enable logging") + lexFlags.source = cmd.Flags().StringP("source", "s", "", "source file path (default: stdin)") + lexFlags.output = cmd.Flags().StringP("output", "o", "", "output file path (default: stdout)") rootCmd.AddCommand(cmd) } @@ -31,31 +39,55 @@ func runLex(cmd *cobra.Command, args []string) (retErr error) { if err != nil { return fmt.Errorf("Cannot read a compiled lexical specification: %w", err) } - var w io.Writer - { + + var opts []driver.LexerOption + if *lexFlags.debug { fileName := "maleeni-lex.log" f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { return fmt.Errorf("Cannot open the log file %s: %w", fileName, err) } defer f.Close() - w = f - } - fmt.Fprintf(w, `maleeni lex starts. + fmt.Fprintf(f, `maleeni lex starts. Date time: %v --- `, time.Now().Format(time.RFC3339)) - defer func() { - fmt.Fprintf(w, "---\n") - if retErr != nil { - fmt.Fprintf(w, "maleeni lex failed: %v\n", retErr) - } else { - fmt.Fprintf(w, "maleeni lex succeeded.\n") + defer func() { + fmt.Fprintf(f, "---\n") + if retErr != nil { + fmt.Fprintf(f, "maleeni lex failed: %v\n", retErr) + } else { + fmt.Fprintf(f, "maleeni lex succeeded.\n") + } + }() + + opts = append(opts, driver.EnableLogging(f)) + } + + var lex *driver.Lexer + { + src := os.Stdin + if *lexFlags.source != "" { + f, err := os.Open(*lexFlags.source) + if err != nil { + return fmt.Errorf("Cannot open the source file %s: %w", *lexFlags.source, err) + } + defer f.Close() + src = f } - }() - lex, err := driver.NewLexer(clspec, os.Stdin, driver.EnableLogging(w)) - if err != nil { - return err + lex, err = driver.NewLexer(clspec, src, opts...) + if err != nil { + return err + } + } + w := os.Stdout + if *lexFlags.output != "" { + f, err := os.OpenFile(*lexFlags.output, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return fmt.Errorf("Cannot open the output file %s: %w", *lexFlags.output, err) + } + defer f.Close() + w = f } for { tok, err := lex.Next() @@ -66,7 +98,7 @@ Date time: %v if err != nil { return fmt.Errorf("failed to marshal a token; token: %v, error: %v\n", tok, err) } - fmt.Fprintf(os.Stdout, "%v\n", string(data)) + fmt.Fprintf(w, "%v\n", string(data)) if tok.EOF { break } diff --git a/compiler/compiler.go b/compiler/compiler.go index ffe0511..d1720de 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -10,9 +10,9 @@ import ( "github.com/nihei9/maleeni/spec" ) -type compilerOption func(c *compilerConfig) error +type CompilerOption func(c *compilerConfig) error -func EnableLogging(w io.Writer) compilerOption { +func EnableLogging(w io.Writer) CompilerOption { return func(c *compilerConfig) error { logger, err := log.NewLogger(w) if err != nil { @@ -27,7 +27,7 @@ type compilerConfig struct { logger log.Logger } -func Compile(lexspec *spec.LexSpec, opts ...compilerOption) (*spec.CompiledLexSpec, error) { +func Compile(lexspec *spec.LexSpec, opts ...CompilerOption) (*spec.CompiledLexSpec, error) { err := lexspec.Validate() if err != nil { return nil, fmt.Errorf("invalid lexical specification:\n%w", err) diff --git a/driver/lexer.go b/driver/lexer.go index 7886720..95540f4 100644 --- a/driver/lexer.go +++ b/driver/lexer.go @@ -104,10 +104,10 @@ func (t *Token) String() string { return fmt.Sprintf("{mode: %v, mode name: %v, id: %v, kind: %v, text: %v, byte: %v}", t.Mode, t.ModeName, t.ID, t.Kind, t.Text, t.Match) } -type lexerOption func(l *lexer) error +type LexerOption func(l *Lexer) error -func EnableLogging(w io.Writer) lexerOption { - return func(l *lexer) error { +func EnableLogging(w io.Writer) LexerOption { + return func(l *Lexer) error { logger, err := log.NewLogger(w) if err != nil { return err @@ -117,7 +117,7 @@ func EnableLogging(w io.Writer) lexerOption { } } -type lexer struct { +type Lexer struct { clspec *spec.CompiledLexSpec src []byte srcPtr int @@ -126,12 +126,12 @@ type lexer struct { logger log.Logger } -func NewLexer(clspec *spec.CompiledLexSpec, src io.Reader, opts ...lexerOption) (*lexer, error) { +func NewLexer(clspec *spec.CompiledLexSpec, src io.Reader, opts ...LexerOption) (*Lexer, error) { b, err := ioutil.ReadAll(src) if err != nil { return nil, err } - l := &lexer{ + l := &Lexer{ clspec: clspec, src: b, srcPtr: 0, @@ -151,7 +151,7 @@ func NewLexer(clspec *spec.CompiledLexSpec, src io.Reader, opts ...lexerOption) return l, nil } -func (l *lexer) Next() (*Token, error) { +func (l *Lexer) Next() (*Token, error) { l.logger.Log(`lexer#Next(): State: mode: #%v %v @@ -201,7 +201,7 @@ func (l *lexer) Next() (*Token, error) { return errTok, nil } -func (l *lexer) nextAndTranMode() (*Token, error) { +func (l *Lexer) nextAndTranMode() (*Token, error) { tok, err := l.next() if err != nil { return nil, err @@ -231,7 +231,7 @@ func (l *lexer) nextAndTranMode() (*Token, error) { return tok, nil } -func (l *lexer) next() (*Token, error) { +func (l *Lexer) next() (*Token, error) { mode := l.mode() modeName := l.clspec.Modes[mode] spec := l.clspec.Specs[mode] @@ -272,7 +272,7 @@ func (l *lexer) next() (*Token, error) { } } -func (l *lexer) lookupNextState(mode spec.LexModeNum, state int, v int) (int, bool) { +func (l *Lexer) lookupNextState(mode spec.LexModeNum, state int, v int) (int, bool) { tab := l.clspec.Specs[mode].DFA.Transition rowNum := tab.RowNums[state] d := tab.UniqueEntries.RowDisplacement[rowNum] @@ -282,15 +282,15 @@ func (l *lexer) lookupNextState(mode spec.LexModeNum, state int, v int) (int, bo return tab.UniqueEntries.Entries[d+v], true } -func (l *lexer) mode() spec.LexModeNum { +func (l *Lexer) mode() spec.LexModeNum { return l.modeStack[len(l.modeStack)-1] } -func (l *lexer) pushMode(mode spec.LexModeNum) { +func (l *Lexer) pushMode(mode spec.LexModeNum) { l.modeStack = append(l.modeStack, mode) } -func (l *lexer) popMode() error { +func (l *Lexer) popMode() error { sLen := len(l.modeStack) if sLen == 0 { return fmt.Errorf("cannot pop a lex mode from a lex mode stack any more") @@ -299,7 +299,7 @@ func (l *lexer) popMode() error { return nil } -func (l *lexer) read() (byte, bool) { +func (l *Lexer) read() (byte, bool) { if l.srcPtr >= len(l.src) { return 0, true } @@ -308,6 +308,6 @@ func (l *lexer) read() (byte, bool) { return b, false } -func (l *lexer) unread(n int) { +func (l *Lexer) unread(n int) { l.srcPtr -= n } |