Module: LexerKit::CLI

Defined in:
lib/lexer_kit/cli.rb,
lib/lexer_kit/cli/commands.rb

Overview

Command-line interface for LexerKit

Defined Under Namespace

Modules: Commands

Constant Summary collapse

VERSION =
LexerKit::VERSION

Class Method Summary collapse

Class Method Details

.load_lexer(path) ⇒ Object

Load lexer from .lkt1 or .lkb1 file



82
83
84
# File 'lib/lexer_kit/cli.rb', line 82

def self.load_lexer(path)
  LexerKit.load_lexer(path)
end


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
# File 'lib/lexer_kit/cli.rb', line 53

def print_help
  puts <<~HELP
    Usage: lexer_kit <command> [options] [arguments]

    Commands:
      compile   Compile DSL file to .lkt1 or .lkb1
      lex       Tokenize input with a lexer
      info      Show lexer information
      disasm    Disassemble a .lkt1 or .lkb1 file
      visualize Output DFA as Graphviz DOT format

    Options:
      -h, --help     Show this help
      -v, --version  Show version

    Examples:
      lexer_kit compile json_lexer.rb
      lexer_kit compile json_lexer.rb --verbose  # show conflict warnings
      lexer_kit compile json_lexer.rb --dry-run  # check conflicts only
      lexer_kit lex json_lexer.lkt1 data.json
      lexer_kit info json_lexer.lkt1
      lexer_kit lex json_lexer.lkb1 data.json
      lexer_kit info json_lexer.lkb1
      lexer_kit visualize json_lexer.lkt1 | dot -Tpng -o dfa.png
  HELP
end

.render_error(error, color: $stderr.tty?) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/lexer_kit/cli.rb', line 45

def render_error(error, color: $stderr.tty?)
  if error.respond_to?(:render)
    warn error.render(color: color)
  else
    warn "error: #{error.message}"
  end
end

.run(argv) ⇒ Object



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
# File 'lib/lexer_kit/cli.rb', line 12

def run(argv)
  if argv.empty? || argv[0] == "-h" || argv[0] == "--help"
    print_help
    return 0
  end

  if ["-v", "--version"].include?(argv[0])
    puts "lexer_kit #{VERSION}"
    return 0
  end

  command = argv.shift
  case command
  when "compile"
    Commands::Compile.run(argv)
  when "lex"
    Commands::Lex.run(argv)
  when "info"
    Commands::Info.run(argv)
  when "disasm"
    Commands::Disasm.run(argv)
  when "visualize"
    Commands::Visualize.run(argv)
  else
    warn "Unknown command: #{command}"
    warn "Run 'lexer_kit --help' for usage."
    1
  end
rescue StandardError => e
  render_error(e)
  1
end