Class: KPeg::GrammarRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/kpeg/grammar_renderer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gram) ⇒ GrammarRenderer

Returns a new instance of GrammarRenderer.



5
6
7
# File 'lib/kpeg/grammar_renderer.rb', line 5

def initialize(gram)
  @grammar = gram
end

Class Method Details

.escape(str, embed = false) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kpeg/grammar_renderer.rb', line 66

def self.escape(str, embed=false)
  parc = StringEscape.new(str)

  rule = (embed ? "embed" : nil)

  unless parc.parse(rule)
    parc.raise_error
  end

  return parc.text
end

Instance Method Details

#parens?(op) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
# File 'lib/kpeg/grammar_renderer.rb', line 57

def parens?(op)
  case op
  when Sequence, AndPredicate, NotPredicate
    return true
  end

  false
end

#render(io) ⇒ Object



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
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kpeg/grammar_renderer.rb', line 9

def render(io)
  widest = @grammar.rules.keys.sort { |a,b| a.size <=> b.size }.last
  indent = widest.size

  @grammar.variables.sort.each do |name, value|
    io.print "%% #{name} = #{value}\n"
  end

  unless @grammar.variables.empty?
    io.print "\n"
  end

  @grammar.directives.sort_by { |name,| name }.each do |name, act|
    io.print "%% #{name} {"
    io.print act.action
    io.print "}\n\n"
  end

  @grammar.setup_actions.each do |act|
    io.print "%% {"
    io.print act.action
    io.print "}\n\n"
  end

  @grammar.rule_order.each do |name|
    rule = @grammar.find(name)

    io.print(' ' * (indent - name.size))
    io.print "#{name} = "

    op = rule.op

    if op.kind_of? Choice
      op.ops.each_with_index do |r,idx|
        unless idx == 0
          io.print "\n#{' ' * (indent+1)}| "
        end

        render_op io, r
      end
    else
      render_op io, op
    end

    io.puts
  end
end

#render_op(io, op) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/kpeg/grammar_renderer.rb', line 78

def render_op(io, op)
  case op
  when Dot
    io.print "."
  when LiteralString
    esc = GrammarRenderer.escape op.string
    io.print '"'
    io.print esc
    io.print '"'
  when LiteralRegexp
    io.print op.regexp.inspect
  when CharRange
    io.print "[#{op.start}-#{op.fin}]"
  when Sequence
    op.ops.each_with_index do |r,idx|
      unless idx == 0
        io.print " "
      end
      render_op io, r
    end
  when Choice
    io.print "("
    op.ops.each_with_index do |r,idx|
      unless idx == 0
        io.print " | "
      end

      render_op io, r
    end
    io.print ")"
  when Multiple
    if parens?(op.op)
      io.print "("
      render_op io, op.op
      io.print ")"
    else
      render_op io, op.op
    end

    if op.max
      if op.min == 0 and op.max == 1
        io.print "?"
      else
        io.print "[#{op.min}, #{op.max}]"
      end
    elsif op.min == 0
      io.print "*"
    elsif op.min == 1
      io.print "+"
    else
      io.print "[#{op.min},*]"
    end
  when AndPredicate
    io.print "&"
    if parens?(op.op)
      io.print "("
      render_op io, op.op
      io.print ")"
    else
      render_op io, op.op
    end
  when NotPredicate
    io.print "!"
    if parens?(op.op)
      io.print "("
      render_op io, op.op
      io.print ")"
    else
      render_op io, op.op
    end
  when RuleReference
    if op.arguments
      io.print "#{op.rule_name}#{op.arguments}"
    else
      io.print "#{op.rule_name}"
    end
  when InvokeRule
    if op.arguments
      io.print "@#{op.rule_name}#{op.arguments}"
    else
      io.print "@#{op.rule_name}"
    end
  when ForeignInvokeRule
    if op.arguments
      io.print "%#{op.grammar_name}.#{op.rule_name}#{op.arguments}"
    else
      io.print "%#{op.grammar_name}.#{op.rule_name}"
    end
  when Tag
    if parens?(op.op)
      io.print "("
      render_op io, op.op
      io.print ")"
    else
      render_op io, op.op
    end

    if op.tag_name
      io.print ":#{op.tag_name}"
    end
  when Action
    io.print "{#{op.action}}"
  when Collect
    io.print "< "
    render_op io, op.op
    io.print " >"
  when Bounds
    io.print "@< "
    render_op io, op.op
    io.print " >"
  else
    raise "Unknown op type - #{op.class}"
  end
end