~ubuntu-branches/ubuntu/utopic/ruby-kpeg/utopic

« back to all changes in this revision

Viewing changes to bin/kpeg

  • Committer: Package Import Robot
  • Author(s): Dominique Dumont
  • Date: 2014-04-15 09:01:50 UTC
  • Revision ID: package-import@ubuntu.com-20140415090150-b3pvbpg66n78yp4x
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require 'kpeg'
 
4
require 'kpeg/code_generator'
 
5
require 'kpeg/format_parser'
 
6
require 'kpeg/grammar_renderer'
 
7
 
 
8
require 'optparse'
 
9
 
 
10
options = {}
 
11
OptionParser.new do |o|
 
12
  o.banner = "Usage: kpeg [options]"
 
13
 
 
14
  o.on("-t", "--test", "Syntax check the file only") do |v|
 
15
    options[:test] = v
 
16
  end
 
17
 
 
18
  o.on("--reformat", "Reformat your grammar and write it back out") do
 
19
    options[:reformat] = true
 
20
  end
 
21
 
 
22
  o.on("-o", "--output FILE", "Where the output should go") do |v|
 
23
    options[:output] = v
 
24
  end
 
25
 
 
26
  o.on("-n", "--name NAME", "Class name to use for the parser") do |v|
 
27
    options[:name] = v
 
28
  end
 
29
 
 
30
  o.on("-f", "--force", "Overwrite the output if it exists") do |v|
 
31
    options[:force] = v
 
32
  end
 
33
 
 
34
  o.on("-s", "--stand-alone", "Write the parser to run standalone") do |v|
 
35
    options[:standalone] = v
 
36
  end
 
37
 
 
38
  o.on("-v", "--[no-]verbose", "Run verbosely") do |v|
 
39
    options[:verbose] = v
 
40
  end
 
41
 
 
42
  o.on("-d", "--debug", "Debug parsing the file") do |v|
 
43
    options[:debug] = v
 
44
  end
 
45
end.parse!
 
46
 
 
47
file = ARGV.shift
 
48
 
 
49
unless File.exists?(file)
 
50
  puts "File '#{file}' does not exist"
 
51
  exit 1
 
52
end
 
53
 
 
54
parser = KPeg::FormatParser.new File.read(file), true
 
55
 
 
56
unless m = parser.parse
 
57
  puts "Syntax error in grammar #{file}"
 
58
  parser.show_error
 
59
  exit 1
 
60
end
 
61
 
 
62
grammar = parser.grammar
 
63
 
 
64
if options[:reformat]
 
65
  if !options[:output]
 
66
    puts "Please specify -o for where to write the new grammar"
 
67
    exit 1
 
68
  end
 
69
 
 
70
  output = options[:output]
 
71
  if File.exists?(output) and !options[:force]
 
72
    puts "Output '#{output}' already exists, not overwriting (use -f)"
 
73
    exit 1
 
74
  end
 
75
 
 
76
  rend = KPeg::GrammarRenderer.new(parser.grammar)
 
77
 
 
78
  File.open output, "w" do |f|
 
79
    rend.render(f)
 
80
  end
 
81
 
 
82
  puts "Wrote reformatted output to #{output}"
 
83
 
 
84
  exit 0
 
85
end
 
86
 
 
87
if !options[:test] and !options[:name]
 
88
  unless name = grammar.variables["name"]
 
89
    puts "Please specify -n"
 
90
    exit 1
 
91
  end
 
92
else
 
93
  name = options[:name]
 
94
end
 
95
 
 
96
 
 
97
if options[:output]
 
98
  new_path = options[:output]
 
99
else
 
100
  new_path = "#{file}.rb"
 
101
end
 
102
 
 
103
if !options[:test] and File.exists?(new_path) and !options[:force]
 
104
  puts "Path #{new_path} already exists, not overwriting\n"
 
105
  exit 1
 
106
end
 
107
 
 
108
if options[:test]
 
109
  puts "Syntax ok"
 
110
 
 
111
  if options[:debug]
 
112
    gr = KPeg::GrammarRenderer.new(grammar)
 
113
    gr.render(STDOUT)
 
114
  end
 
115
  exit 0
 
116
end
 
117
 
 
118
 
 
119
cg = KPeg::CodeGenerator.new name, grammar
 
120
cg.standalone = options[:standalone]
 
121
 
 
122
output = cg.output
 
123
 
 
124
open new_path, "w" do |io|
 
125
  io << output
 
126
end
 
127
 
 
128
puts "Wrote #{name} to #{new_path}"