• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dryruby / ebnf / 12041343242

27 Nov 2024 12:23AM UTC coverage: 93.533% (-0.5%) from 94.066%
12041343242

push

github

gkellogg
Revise logging to use standard RDF log facility.

6 of 17 new or added lines in 3 files covered. (35.29%)

12 existing lines in 4 files now uncovered.

2126 of 2273 relevant lines covered (93.53%)

25774.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/lib/ebnf/parser.rb
1
require_relative 'ebnf/meta'
2✔
2
require 'logger'
2✔
3

4
module EBNF
2✔
5
  class Parser
2✔
6
    include EBNF::PEG::Parser
2✔
7
    include EBNF::Terminals
2✔
8

9
    # Abstract syntax tree from parse
10
    #
11
    # @return [Array<EBNF::Rule>]
12
    attr_reader :ast
2✔
13

14
    # ## Terminals
15
    # Define rules for Terminals, placing results on the input stack, making them available to upstream non-Terminal rules.
16
    #
17
    # Terminals are defined with a symbol matching the associated rule name, and an optional (although strongly encouraged) regular expression used to match the head of the input stream.
18
    #
19
    # The result of the terminal block is the semantic value of that terminal, which if often a string, but may be any instance which reflects the semantic interpretation of that terminal.
20
    #
21
    # The `value` parameter is the value matched by the regexp, if defined, or by the sub-terminal rules otherwise.
22
    #
23
    # The `prod` parameter is the name of the parent rule for which this terminal is matched, which may have a bearing in some circumstances, although not used in this example.
24
    #
25
    # If no block is provided, then the value which would have been passed to the block is used as the result directly.
26

27
    # Match the Left hand side of a rule or terminal
28
    #
29
    #     [11] LHS        ::= ('[' SYMBOL+ ']' ' '+)? <? SYMBOL >? ' '* '::='
30
    terminal(:LHS, LHS) do |value, prod|
2✔
31
      value.to_s.scan(/(?:\[([^\]]+)\])?\s*<?(\w+)>?\s*::=/).first
5,202✔
32
    end
33

34
    # Match `SYMBOL` terminal
35
    #
36
    #     [12] SYMBOL     ::= '<' O_SYMBOL '>' | O_SYMBOL
37
    #     [12a] O_SYMBOL  ::= ([a-z] | [A-Z] | [0-9] | '_' | '.')+
38
    terminal(:SYMBOL, SYMBOL) do |value|
2✔
39
      value = value[1..-2] if value.start_with?('<') && value.end_with?('>')
10,536✔
40
      value.to_sym
10,536✔
41
    end
42

43
    # Match `HEX` terminal
44
    #
45
    #     [13] HEX        ::= #x' ([a-f] | [A-F] | [0-9])+
46
    terminal(:HEX, HEX) do |value|
2✔
47
      [:hex, value]
316✔
48
    end
49

50
    # Terminal for `RANGE` is matched as part of a `primary` rule.
51
    #
52
    #     [14] RANGE      ::= '[' ((R_CHAR '-' R_CHAR) | (HEX '-' HEX) | R_CHAR | HEX)+ '-'? ']' - LHS
53
    terminal(:RANGE, RANGE) do |value|
2✔
54
      [:range, value[1..-2]]
2,692✔
55
    end
56

57
    # Terminal for `O_RANGE` is matched as part of a `primary` rule.
58
    #
59
    #     [15] O_RANGE    ::= '[^' ((R_CHAR '-' R_CHAR) | (HEX '-' HEX) | R_CHAR | HEX)+ '-'? ']'
60
    terminal(:O_RANGE, O_RANGE) do |value|
2✔
61
      [:range, value[1..-2]]
488✔
62
    end
63

64
    # Match double quote string
65
    #
66
    #     [16] STRING1    ::= '"' (CHAR - '"')* '"'
67
    terminal(:STRING1, STRING1) do |value|
2✔
68
      using ::EBNF
862✔
69
      value[1..-2].tap {|s| s.quote_style = :dquote}
1,724✔
70
    end
71

72
    # Match single quote string
73
    #
74
    #     [17] STRING2    ::= "'" (CHAR - "'")* "'"
75
    terminal(:STRING2, STRING2) do |value|
2✔
76
      using ::EBNF
7,778✔
77
      value[1..-2].tap {|s| s.quote_style = :squote}
15,556✔
78
    end
79

80
    # The `CHAR` and `R_CHAR` productions are not used explicitly
81

82
    # Match `POSTFIX` terminal
83
    #
84
    #     [20] POSTFIX    ::= [?*+]
85
    terminal(:POSTFIX, POSTFIX)
2✔
86

87
    # The `PASS` productions is not used explicitly
88

89
    # ## Non-terminal productions
90
    # Define productions for non-Termainals. This can include `start_production` as well as `production` to hook into rule start and end. In some cases, we need to use sub-productions as generated when turning EBNF into PEG.
91
    #
92
    # Productions are defined with a symbol matching the associated rule name.
93
    #
94
    # The result of the productions is typically the abstract syntax tree matched by the rule, so far, but could be a specific semantic value, or could be ignored with the result being returned via the `callback`.
95
    #
96
    # The `value` parameter is the result returned from child productions
97
    #
98
    # The `data` parameter other data which may be returned by child productions placing information onto their input (unused in this example).
99
    #
100
    # The `callback` parameter provides access to a callback defined in the call to `parse`).
101

102
    # Production for end of `declaration` non-terminal.
103
    #
104
    # Look for `@terminals` to change parser state to parsing terminals.
105
    #
106
    # Clears the packrat parser when called.
107
    #
108
    # `@pass` is ignored here.
109
    #
110
    #     [2] declaration ::= '@terminals' | pass
111
    production(:declaration, clear_packrat: true) do |value, data, callback|
2✔
112
      # value contains a declaration.
113
      # Invoke callback
114
      callback.call(:terminals) if value == '@terminals'
166✔
115
      nil
166✔
116
    end
117

118
    # Production for end of `rule` non-terminal.
119
    #
120
    # By setting `as_hash: true` in the `start_production`, the `value` parameter will be in the form `{LHS: "v", expression: "v"}`. Otherwise, it would be expressed using an array of hashes of the form `[{LHS: "v"}, {expression: "v"}]`.
121
    #
122
    # Clears the packrat parser when called.
123
    #
124
    # Create rule from expression value and pass to callback
125
    #
126
    #     [3] rule        ::= LHS expression
127
    start_production(:rule, as_hash: true)
2✔
128
    production(:rule, clear_packrat: true) do |value, data, callback|
2✔
129
      # value contains an expression.
130
      # Invoke callback
131
      id, sym = value[:LHS]
5,192✔
132
      expression = value[:expression]
5,192✔
133
      callback.call(:rule, EBNF::Rule.new(sym.to_sym, id, expression))
5,192✔
134
      nil
5,192✔
135
    end
136

137
    # Production for end of `expression` non-terminal.
138
    # Passes through the optimized value of the alt production as follows:
139
    #
140
    # The `value` parameter, is of the form `[{alt: "v"}]`.
141
    #
142
    #     [:alt foo] => foo
143
    #     [:alt foo bar] => [:alt foo bar]
144
    #
145
    #     [4] expression  ::= alt
146
    production(:expression) do |value|
2✔
147
      value.first[:alt]
8,638✔
148
    end
149

150
    # Production for end of `alt` non-terminal.
151
    # Passes through the optimized value of the seq production as follows:
152
    #
153
    # The `value` parameter, is of the form `{seq: "v", _alt_1: "v"}`.
154
    #
155
    #     [:seq foo] => foo
156
    #     [:seq foo bar] => [:seq foo bar]
157
    #
158
    # Note that this also may just pass through from `_alt_1`
159
    #
160
    #     [5] alt         ::= seq ('|' seq)*
161
    start_production(:alt, as_hash: true)
2✔
162
    production(:alt) do |value|
2✔
163
      if value[:_alt_1].length > 0
8,638✔
164
        [:alt, value[:seq]] + value[:_alt_1]
3,120✔
165
      else
166
        value[:seq]
5,518✔
167
      end
168
    end
169

170
    # Production for end of `_alt_1` non-terminal.
171
    # Used to collect the `('|' seq)*` portion of the `alt` non-terminal:
172
    #
173
    # The `value` parameter, is of the form `[{seq: ["v"]}]`.
174
    #
175
    #     [5] _alt_1         ::= ('|' seq)*
176
    production(:_alt_1) do |value|
2✔
177
      value.map {|a1| a1.last[:seq]}.compact # Get rid of '|'
16,532✔
178
    end
179

180
    # Production for end of `seq` non-terminal.
181
    # Passes through the optimized value of the `diff` production as follows:
182
    #
183
    # The `value` parameter, is an array of values, which cannot be empty.
184
    #
185
    #     [:diff foo] => foo
186
    #     [:diff foo bar] => [:diff foo bar]
187
    #
188
    # Note that this also may just pass through from `_seq_1`
189
    #
190
    #     [6] seq         ::= diff+
191
    production(:seq) do |value|
2✔
192
      value.length == 1 ? value.first : ([:seq] + value)
16,532✔
193
    end
194

195
    # `Diff` production returns concatenated postfix values
196
    #
197
    # The `value` parameter, is of the form `{postfix: "v", _diff_1: "v"}`.
198
    #
199
    #     [7] diff        ::= postfix ('-' postfix)?
200
    start_production(:diff, as_hash: true)
2✔
201
    production(:diff) do |value|
2✔
202
      if value[:_diff_1]
25,810✔
203
        [:diff, value[:postfix], value[:_diff_1]]
260✔
204
      else
205
        value[:postfix]
25,550✔
206
      end
207
    end
208

209
    production(:_diff_1) do |value|
2✔
210
      value.last[:postfix] if value
25,810✔
211
    end
212

213
    # Production for end of `postfix` non-terminal.
214
    # Either returns the `primary` production value, or as modified by the `postfix`.
215
    #
216
    # The `value` parameter, is of the form `{primary: "v", _postfix_1: "v"}`.
217
    #
218
    #     [:primary] => [:primary]
219
    #     [:primary, '*'] => [:star, :primary]
220
    #     [:primary, '+'] => [:plus, :primary]
221
    #     [:primary, '?'] => [:opt, :primary]
222
    #
223
    #     [8] postfix     ::= primary POSTFIX?
224
    start_production(:postfix, as_hash: true)
2✔
225
    production(:postfix) do |value|
2✔
226
      # Push result onto input stack, as the `diff` production can have some number of `postfix` values that are applied recursively
227
      case value[:_postfix_1]
26,070✔
228
      when "*" then [:star, value[:primary]]
1,596✔
229
      when "+" then [:plus, value[:primary]]
852✔
230
      when "?" then [:opt, value[:primary]]
1,472✔
231
      else value[:primary]
22,150✔
232
      end
233
    end
234

235
    # Production for end of `primary` non-terminal.
236
    # Places `:primary` on the stack
237
    #
238
    # The `value` parameter, is either a string (for a terminal) or an array of the form `['(': '(', expression: "v", ')', ')']`.
239
    #
240
    # This may either be a terminal, or the result of an `expression`.
241
    #
242
    #     [9] primary     ::= HEX
243
    #                     |   SYMBOL
244
    #                     |   RANGE
245
    #                     |   O_RANGE
246
    #                     |   STRING1
247
    #                     |   STRING2
248
    #                     |   '(' expression ')'
249
    production(:primary) do |value|
2✔
250
      Array(value).length > 2 ? value[1][:expression] : value
26,070✔
251
    end
252

253
    # Production for end of pass non-terminal.
254
    #
255
    #     [10] pass       ::= '@pass' expression
256
    production(:pass) do |value, data, callback|
2✔
257
      # Invoke callback
258
      callback.call(:pass, value.last[:expression])
48✔
259
    end
260

261
    # ## Parser invocation.
262
    # On start, yield ourselves if a block is given, otherwise, return this parser instance
263
    #
264
    # @param  [#read, #to_s]          input
265
    # @param  [Hash{Symbol => Object}] options
266
    # @option options [Boolean] :level
267
    #   Trace level. 0(debug), 1(info), 2(warn), 3(error).
268
    # @return [EBNFParser]
269
    def initialize(input, **options, &block)
2✔
270
      # If the `level` option is set, instantiate a logger for collecting trace information.
271
      if options.key?(:level)
354✔
272
        options[:logger] ||= Logger.new(STDERR).
×
UNCOV
273
          tap {|x| x.level = options[:level]}.
×
UNCOV
274
          tap {|x| x.formatter = lambda {|severity, datetime, progname, msg| "#{severity} #{msg}\n"}}
×
275
      end
276

277
      # Read input, if necessary, which will be used in a Scanner.
278
      @input = input.respond_to?(:read) ? input.read : input.to_s
354✔
279

280
      parsing_terminals = false
354✔
281
      @ast = []
354✔
282
      parse(@input, :ebnf, EBNFMeta::RULES,
354✔
283
                           # Use an optimized Regexp for whitespace
284
                           whitespace: EBNF::Terminals::PASS,
285
                           **options
286
      ) do |context, *data|
287
        rule = case context
5,358✔
288
        when :terminals
289
          # After parsing `@terminals`
290
          # This changes the state of the parser to treat subsequent rules as terminals.
291
          parsing_terminals = true
118✔
292
          rule = EBNF::Rule.new(nil, nil, data.first, kind: :terminals)
118✔
293
        when :pass
294
          # After parsing `@pass`
295
          # This defines a specific rule for whitespace.
296
          rule = EBNF::Rule.new(nil, nil, data.first, kind: :pass)
48✔
297
        when :rule
298
          # A rule which has already been turned into a `Rule` object.
299
          rule = data.first
5,192✔
300
          rule.kind = :terminal if parsing_terminals
5,192✔
301
          rule
5,192✔
302
        end
303
        @ast << rule if rule
5,358✔
304
      end
305
    rescue EBNF::PEG::Parser::Error => e
306
      raise SyntaxError, e.message
16✔
307
    end
308
  end
309
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc