Module: MilkTea::CLI::CommandLex

Included in:
MilkTea::CLI
Defined in:
lib/milk_tea/tooling/cli/commands/lex.rb

Instance Method Summary collapse

Instance Method Details

#lex_commandObject



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
# File 'lib/milk_tea/tooling/cli/commands/lex.rb', line 6

def lex_command
  path = nil
  sexpr = false

  args = @argv.dup
  @argv = []
  until args.empty?
    arg = args.shift
    next if arg == "--"

    if arg == "--sexpr"
      sexpr = true
      next
    end

    if path.nil?
      path = arg
    else
      @err.puts("unknown option: #{arg}")
      print_usage(@err)
      return 1
    end
  end

  unless path
    @err.puts("missing source file path")
    print_usage(@err)
    return 1
  end

  tokens = Lexer.lex(read_source_file(path), path: path)
  if sexpr
    @out.puts(SexprDumper.dump_tokens(tokens))
  else
    @out.write(PP.pp(tokens, +""))
  end
  0
end