Skip to content

Commit

Permalink
Remove --debug option from compile command
Browse files Browse the repository at this point in the history
  • Loading branch information
nihei9 committed Sep 22, 2021
1 parent cf4f533 commit 9f3a334
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 107 deletions.
30 changes: 1 addition & 29 deletions cmd/maleeni/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/nihei9/maleeni/compiler"
"github.com/nihei9/maleeni/spec"
Expand All @@ -27,7 +26,6 @@ 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.compLv = cmd.Flags().Int("compression-level", compiler.CompressionLevelMax, "compression level")
compileFlags.output = cmd.Flags().StringP("output", "o", "", "output file path (default stdout)")
Expand All @@ -40,33 +38,7 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) {
return fmt.Errorf("Cannot read a lexical specification: %w", err)
}

opts := []compiler.CompilerOption{
compiler.CompressionLevel(*compileFlags.compLv),
}
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()
fmt.Fprintf(f, `maleeni compile starts.
Date time: %v
---
`, time.Now().Format(time.RFC3339))
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...)
clspec, err := compiler.Compile(lspec, compiler.CompressionLevel(*compileFlags.compLv))
if err != nil {
return err
}
Expand Down
37 changes: 1 addition & 36 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,13 @@ package compiler

import (
"fmt"
"io"
"strings"

"github.com/nihei9/maleeni/compressor"
"github.com/nihei9/maleeni/log"
"github.com/nihei9/maleeni/spec"
)

type CompilerOption func(c *compilerConfig) error

func EnableLogging(w io.Writer) CompilerOption {
return func(c *compilerConfig) error {
logger, err := log.NewLogger(w)
if err != nil {
return err
}
c.logger = logger
return nil
}
}

func CompressionLevel(lv int) CompilerOption {
return func(c *compilerConfig) error {
if lv < CompressionLevelMin || lv > CompressionLevelMax {
Expand All @@ -34,7 +20,6 @@ func CompressionLevel(lv int) CompilerOption {
}

type compilerConfig struct {
logger log.Logger
compLv int
}

Expand All @@ -44,9 +29,7 @@ func Compile(lexspec *spec.LexSpec, opts ...CompilerOption) (*spec.CompiledLexSp
return nil, fmt.Errorf("invalid lexical specification:\n%w", err)
}

config := &compilerConfig{
logger: log.NewNopLogger(),
}
config := &compilerConfig{}
for _, opt := range opts {
err := opt(config)
if err != nil {
Expand All @@ -61,7 +44,6 @@ func Compile(lexspec *spec.LexSpec, opts ...CompilerOption) (*spec.CompiledLexSp
}
for i, es := range modeEntries[1:] {
modeName := modeNames[i+1]
config.logger.Log("Compile %v mode:", modeName)
modeSpec, err := compile(es, modeName2ID, fragmetns, config)
if err != nil {
return nil, fmt.Errorf("failed to compile in %v mode: %w", modeName, err)
Expand Down Expand Up @@ -167,11 +149,6 @@ func compile(entries []*spec.LexEntry, modeName2ID map[spec.LexModeName]spec.Lex
kindNames = append(kindNames, e.Kind)
patterns[spec.LexModeKindID(i+1)] = []byte(e.Pattern)
}

config.logger.Log("Patterns:")
for i, p := range patterns {
config.logger.Log(" #%v %v", i, string(p))
}
}

push := []spec.LexModeID{
Expand Down Expand Up @@ -217,10 +194,6 @@ func compile(entries []*spec.LexEntry, modeName2ID map[spec.LexModeName]spec.Lex
if err != nil {
return nil, err
}

var b strings.Builder
printAST(&b, root, "", "", false)
config.logger.Log("AST:\n%v", b.String())
}

var tranTab *spec.TransitionTable
Expand All @@ -231,14 +204,6 @@ func compile(entries []*spec.LexEntry, modeName2ID map[spec.LexModeName]spec.Lex
if err != nil {
return nil, err
}

config.logger.Log(`DFA:
States: %v states (%v entries)
Initial State ID: %v`, tranTab.RowCount, tranTab.RowCount*tranTab.ColCount, tranTab.InitialStateID)
config.logger.Log(" Accepting States:")
for state, symbol := range tranTab.AcceptingStates {
config.logger.Log(" %v: %v", state, symbol)
}
}

var err error
Expand Down
42 changes: 0 additions & 42 deletions log/logger.go

This file was deleted.

0 comments on commit 9f3a334

Please sign in to comment.