Class: StartMatchEmpty

Inherits:
GrammarLinter show all
Defined in:
lib/ruby_grammar_builder/linters/start_match_empty.rb

Overview

Warns when a PatternRange has a start_pattern that matches the empty string

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GrammarLinter

#post_lint

Class Method Details

.display_options(indent, options) ⇒ String

Displays the state of the options

Returns:

  • (String)

    the options as a string



44
45
46
# File 'lib/ruby_grammar_builder/linters/start_match_empty.rb', line 44

def self.display_options(indent, options)
    ",\n#{indent}zeroLengthStart?: #{options[:zeroLengthStart?]}"
end

.optionsArray<Symbol>

Contributes the option :zeroLengthStart?

:zeroLengthStart? disables this linter

Returns:

  • (Array<Symbol>)

    a list of symbols that represent keys that can be read by the plugin



35
36
37
# File 'lib/ruby_grammar_builder/linters/start_match_empty.rb', line 35

def self.options
    [:zeroLengthStart?]
end

Instance Method Details

#pre_lint(pattern, options) ⇒ Boolean

Runs the linter on each pattern

Parameters:

  • pattern (PatternBase, Symbol, Hash)

    the pattern to lint

  • options (Hash)

    hash of any of the option keys provided by self.options. options will only be populated when pattern is a PatternBase

Returns:

  • (Boolean)

    the result of the lint



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_grammar_builder/linters/start_match_empty.rb', line 12

def pre_lint(pattern, options)
    return true unless pattern.is_a? PatternRange

    regexp = with_no_warnings do
        Regexp.new(pattern.start_pattern.evaluate.gsub("\\G", '\uFFFF'))
    end
    if "" =~ regexp and !options[:zeroLengthStart?]
        puts "Warning: #{pattern.start_pattern.evaluate}"
        puts "matches the zero length string (\"\").\n\n"
        puts "This means that the patternRange always matches"
        puts "You can disable this warning by setting :zeroLengthStart? to true."
        puts "The pattern is:\n#{pattern}"
    end
    true # return true for warnings
end