Module: EBNF::Parser

Included in:
Base
Defined in:
lib/ebnf/parser.rb

Instance Method Summary collapse

Instance Method Details

#alt(s) ⇒ Array

Parse alt

>>> alt("a | b | c")
((alt a b c) '')

Parameters:

  • s (String)

Returns:

  • (Array)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ebnf/parser.rb', line 125

def alt(s)
  debug("alt") {"(#{s.inspect})"}
  args = []
  while !s.to_s.empty?
    e, s = depth {seq(s)}
    debug {"=> seq returned #{[e, s].inspect}"}
    if e.to_s.empty?
      break unless args.empty?
      e = [:seq, []] # empty sequence
    end
    args << e
    unless s.to_s.empty?
      t, ss = depth {terminal(s)}
      break unless t[0] == :alt
      s = ss
    end
  end
  args.length > 1 ? [args.unshift(:alt), s] : [e, s]
end

#diff(s) ⇒ Object

parse diff

>>> diff("a - b")
((diff a b) '')


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ebnf/parser.rb', line 180

def diff(s)
  debug("diff") {"(#{s.inspect})"}
  e1, s = depth {postfix(s)}
  debug {"=> postfix returned #{[e1, s].inspect}"}
  unless e1.to_s.empty?
    unless s.to_s.empty?
      t, ss = depth {terminal(s)}
      debug {"diff #{[t, ss].inspect}"}
      if t.is_a?(Array) && t.first == :diff
        s = ss
        e2, s = primary(s)
        unless e2.to_s.empty?
          return [[:diff, e1, e2], s]
        else
          error("diff", "Syntax Error")
          raise "Syntax Error"
        end
      end
    end
  end
  [e1, s]
end

#eachRule(scanner) {|rule_string| ... } ⇒ Object

Iterate over rule strings. a line that starts with ‘[’ or ‘@’ starts a new rule

Parameters:

  • scanner (StringScanner)

Yields:

  • rule_string

Yield Parameters:

  • rule_string (String)


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
# File 'lib/ebnf/parser.rb', line 10

def eachRule(scanner)
  cur_lineno = 1
  r = ''
  until scanner.eos?
    case
    when s = scanner.scan(%r(\s+)m)
      # Eat whitespace
      cur_lineno += s.count("\n")
      #debug("eachRule(ws)") { "[#{cur_lineno}] #{s.inspect}" }
    when s = scanner.scan(%r(/\*([^\*]|\*[^\/])*\*/)m)
      # Eat comments
      debug("eachRule(comment)") { "[#{cur_lineno}] #{s.inspect}" }
    when s = scanner.scan(%r((#|//).*$))
      # Eat comments
      cur_lineno += s.count("\n")
      debug("eachRule(comment)") { "[#{cur_lineno}] #{s.inspect}" }
    when s = scanner.scan(%r(^@terminals))
      #debug("eachRule(@terminals)") { "[#{cur_lineno}] #{s.inspect}" }
      yield(r) unless r.empty?
      @lineno = cur_lineno
      yield(s)
      r = ''
    when s = scanner.scan(/@pass/)
      # Found rule start, if we've already collected a rule, yield it
      #debug("eachRule(@pass)") { "[#{cur_lineno}] #{s.inspect}" }
      yield r unless r.empty?
      @lineno = cur_lineno
      r = s
    when s = scanner.scan(/(?:\[[\w\.]+\])\s*[\w\.]+\s*::=/)
      # Found rule start, if we've already collected a rule, yield it
      yield r unless r.empty?
      #debug("eachRule(rule)") { "[#{cur_lineno}] #{s.inspect}" }
      @lineno = cur_lineno
      r = s
    else
      # Collect until end of line, or start of comment
      s = scanner.scan_until(%r((?:/\*)|$)m)
      cur_lineno += s.count("\n")
      #debug("eachRule(rest)") { "[#{cur_lineno}] #{s.inspect}" }
      r += s
    end
  end
  yield r unless r.empty?
end

#expression(s) ⇒ Array

Parse a string into an expression tree and a remaining string

Examples:

>>> expression("a b c")
((seq a b c) '')

>>> expression("a? b+ c*")
((seq (opt a) (plus b) (star c)) '')

>>> expression(" | x xlist")
((alt (seq) (seq x xlist)) '')

>>> expression("a | (b - c)")
((alt a (diff b c)) '')

>>> expression("a b | c d")
((alt (seq a b) (seq c d)) '')

>>> expression("a | b | c")
((alt a b c) '')

>>> expression("a) b c")
(a ' b c')

>>> expression("BaseDecl? PrefixDecl*")
((seq (opt BaseDecl) (star PrefixDecl)) '')

>>> expression("NCCHAR1 | diff | [0-9] | #x00B7 | [#x0300-#x036F] | \[#x203F-#x2040\]")
((alt NCCHAR1 diff
      (range '0-9')
      (hex '#x00B7')
      (range '#x0300-#x036F')
      (range, '#x203F-#x2040')) '')

Parameters:

  • s (String)

Returns:

  • (Array)


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ebnf/parser.rb', line 107

def expression(s)
  debug("expression") {"(#{s.inspect})"}
  e, s = depth {alt(s)}
  debug {"=> alt returned #{[e, s].inspect}"}
  unless s.to_s.empty?
    t, ss = depth {terminal(s)}
    debug {"=> terminal returned #{[t, ss].inspect}"}
    return [e, ss] if t.is_a?(Array) && t.first == :")"
  end
  [e, s]
end

#postfix(s) ⇒ Object

parse postfix

>>> postfix("a b c")
(a ' b c')

>>> postfix("a? b c")
((opt, a) ' b c')


211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/ebnf/parser.rb', line 211

def postfix(s)
  debug("postfix") {"(#{s.inspect})"}
  e, s = depth {primary(s)}
  debug {"=> primary returned #{[e, s].inspect}"}
  return ["", s] if e.to_s.empty?
  if !s.to_s.empty?
    t, ss = depth {terminal(s)}
    debug {"=> #{[t, ss].inspect}"}
    if t.is_a?(Array) && [:opt, :star, :plus].include?(t.first)
      return [[t.first, e], ss]
    end
  end
  [e, s]
end

#primary(s) ⇒ Object

parse primary

>>> primary("a b c")
(a ' b c')


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/ebnf/parser.rb', line 231

def primary(s)
  debug("primary") {"(#{s.inspect})"}
  t, s = depth {terminal(s)}
  debug {"=> terminal returned #{[t, s].inspect}"}
  if t.is_a?(Symbol) || t.is_a?(String)
    [t, s]
  elsif %w(range hex).map(&:to_sym).include?(t.first)
    [t, s]
  elsif t.first == :"("
    e, s = depth {expression(s)}
    debug {"=> expression returned #{[e, s].inspect}"}
    [e, s]
  else
    ["", s]
  end
end

#ruleParts(rule) ⇒ Rule

Parse a rule into an optional rule number, a symbol and an expression

Parameters:

  • rule (String)

Returns:



60
61
62
63
64
65
66
67
68
# File 'lib/ebnf/parser.rb', line 60

def ruleParts(rule)
  num_sym, expr = rule.split('::=', 2).map(&:strip)
  num, sym = num_sym.split(']', 2).map(&:strip)
  num, sym = "", num if sym.nil?
  num = num[1..-1]
  r = Rule.new(sym && sym.to_sym, num, expression(expr).first, :ebnf => self)
  debug("ruleParts") { r.inspect }
  r
end

#seq(s) ⇒ Object

parse seq

>>> seq("a b c")
((seq a b c) '')

>>> seq("a b? c")
((seq a (opt b) c) '')


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ebnf/parser.rb', line 153

def seq(s)
  debug("seq") {"(#{s.inspect})"}
  args = []
  while !s.to_s.empty?
    e, ss = depth {diff(s)}
    debug {"=> diff returned #{[e, ss].inspect}"}
    unless e.to_s.empty?
      args << e
      s = ss
    else
      break;
    end
  end
  if args.length > 1
    [args.unshift(:seq), s]
  elsif args.length == 1
    args + [s]
  else
    ["", s]
  end
end

#terminal(s) ⇒ Object

parse one terminal; return the terminal and the remaining string

A terminal is represented as a tuple whose 1st item gives the type; some types have additional info in the tuple.

Examples:

>>> terminal("'abc' def")
('abc' ' def')

>>> terminal("[0-9]")
((range '0-9') '')
>>> terminal("#x00B7")
((hex '#x00B7') '')
>>> terminal ("\[#x0300-#x036F\]")
((range '#x0300-#x036F') '')
>>> terminal("\[^<>'{}|^`\]-\[#x00-#x20\]")
((range "^<>'{}|^`") '-\[#x00-#x20\]')


266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/ebnf/parser.rb', line 266

def terminal(s)
  s = s.strip
  case m = s[0,1]
  when '"', "'" # STRING1 or STRING2 Terminated by line-end or whitespace
    l, s = s[1..-1].split(m.rstrip  , 2)
    [LL1::Lexer.unescape_string(l), s]
  when '[' # ENUM, RANGE, O_ENUM, or O_RANGE
    l, s = s[1..-1].split(/(?<=[^\\])\]/, 2)
    [[:range, LL1::Lexer.unescape_string(l)], s]
  when '#' # HEX
    s.match(/(#\w+)(.*)$/)
    l, s = $1, $2
    [[:hex, l], s]
  when /[\w\.]/ # SYMBOL
    s.match(/(\w+)(.*)$/)
    l, s = $1, $2
    [l.to_sym, s]
  when '@' # @pass or @terminals
    s.match(/@(#\w+)(.*)$/)
    l, s = $1, $2
    [[:"@", l], s]
  when '-'
    [[:diff], s[1..-1]]
  when '?'
    [[:opt], s[1..-1]]
  when '|'
    [[:alt], s[1..-1]]
  when '+'
    [[:plus], s[1..-1]]
  when '*'
    [[:star], s[1..-1]]
  when /[\(\)]/ # '(' or ')'
    [[m.to_sym], s[1..-1]]
  else
    error("terminal", "unrecognized terminal: #{s.inspect}")
    raise "Syntax Error, unrecognized terminal: #{s.inspect}"
  end
end