class JsDuck::Warning::Parser

Parses the warnings passed in from command line

Grammar:

<warnings> := <warning> [ “,” <warning> ]*

<warning> := [“+” | “-”] <type> [<params-block>] [<path-block>]

<type> := w+

<params-block> := “(” [<params>] “)”

<params> := <param> [ “,” <param> ]*

<param> := w+ | “”

<path-block> := “:” <path>

<path> := .*

Public Class Methods

new(string) click to toggle source
# File lib/jsduck/warning/parser.rb, line 28
def initialize(string)
  @scanner = StringScanner.new(string)
end

Public Instance Methods

parse() click to toggle source

Parses the warnings string.

For example the following string:

+tag,-nodoc(class,private):/some/path

is parsed into the following structure:

[
  {
    :type => :tag,
    :enabled => true,
    :params => [],
    :path => nil,
  },
  {
    :type => :nodoc,
    :enabled => false,
    :params => [:class, :private],
    :path => "/some/path",
  },
]

When scanning fails, raises an exception with a descriptive message.

# File lib/jsduck/warning/parser.rb, line 57
def parse
  results = []

  while !eos?
    results << warning
    match(/,/)
  end

  results
end

Private Instance Methods

enabled() click to toggle source
# File lib/jsduck/warning/parser.rb, line 79
def enabled
  if match(/\+/)
    true
  elsif match(/-/)
    false
  else
    true
  end
end
eos?() click to toggle source
# File lib/jsduck/warning/parser.rb, line 156
def eos?
  skip_ws
  @scanner.eos?
end
look(re) click to toggle source
# File lib/jsduck/warning/parser.rb, line 151
def look(re)
  skip_ws
  @scanner.check(re)
end
match(re) click to toggle source

scans a pattern, ignoring the optional whitespace before it

# File lib/jsduck/warning/parser.rb, line 146
def match(re)
  skip_ws
  @scanner.scan(re)
end
param() click to toggle source
# File lib/jsduck/warning/parser.rb, line 110
def param
  if p = match(/\w+/)
    p.to_sym
  elsif look(/,/)
    nil
  else
    unexpected_char
  end
end
params() click to toggle source
# File lib/jsduck/warning/parser.rb, line 93
def params
  if match(/\(/)
    ps = []

    while !look(/\)/)
      ps << param
      break unless match(/,/)
    end

    require(/\)/)

    ps
  else
    []
  end
end
path() click to toggle source
# File lib/jsduck/warning/parser.rb, line 120
def path
  if match(/:/)
    match(/[^,]*/).strip
  else
    nil
  end
end
require(re) click to toggle source

scans a pattern, throws error on failure

# File lib/jsduck/warning/parser.rb, line 129
def require(re)
  if m = match(re)
    m
  else
    unexpected_char
  end
end
skip_ws() click to toggle source
# File lib/jsduck/warning/parser.rb, line 161
def skip_ws
  @scanner.scan(/\s*/)
end
type() click to toggle source
# File lib/jsduck/warning/parser.rb, line 89
def type
  require(/\w+/).to_sym
end
unexpected_char() click to toggle source

Reports unexpected character

# File lib/jsduck/warning/parser.rb, line 138
def unexpected_char
  # do successful empty scan, so we can use #pre_match and #post_match
  @scanner.scan(//)
  raise WarnException, "Unexpected '#{@scanner.peek(1)}' at " +
    "--warnings='#{@scanner.pre_match}<HERE>#{@scanner.post_match}'"
end
warning() click to toggle source
# File lib/jsduck/warning/parser.rb, line 70
def warning
  return {
    :enabled => enabled,
    :type => type,
    :params => params,
    :path => path,
  }
end