class JsDuck::Doc::DelimitedParser

Helper in parsing the default values and type definitions where we take into account correctly nested parenthesis and strings. But at the same time we don't care much about the actual contents.

Public Class Methods

new(doc_scanner) click to toggle source

Initialized with Doc::Scanner instance

# File lib/jsduck/doc/delimited_parser.rb, line 10
def initialize(doc_scanner)
  @ds = doc_scanner
end

Public Instance Methods

parse_until_close_curly() click to toggle source

Parses until a closing “}”.

# File lib/jsduck/doc/delimited_parser.rb, line 15
def parse_until_close_curly
  parse_until_close_paren(/\}/, /[^\}]*/)
end
parse_until_close_square() click to toggle source

Parses until a closing “]”.

# File lib/jsduck/doc/delimited_parser.rb, line 20
def parse_until_close_square
  parse_until_close_paren(/\]/, /[^\]]*/)
end
parse_until_space() click to toggle source

Parses until a closing parenthesis or space.

# File lib/jsduck/doc/delimited_parser.rb, line 25
def parse_until_space
  begin
    parse_while(/[^\[\]\{\}\(\)'"\s]/)
  rescue
    nil
  end
end

Private Instance Methods

look(re) click to toggle source

Forward these calls to Doc::Scanner

# File lib/jsduck/doc/delimited_parser.rb, line 95
def look(re)
  @ds.look(re)
end
match(re) click to toggle source
# File lib/jsduck/doc/delimited_parser.rb, line 99
def match(re)
  @ds.match(re)
end
parse_balanced(re_open, re_close) click to toggle source
# File lib/jsduck/doc/delimited_parser.rb, line 74
def parse_balanced(re_open, re_close)
  result = match(re_open)

  result += parse_while(/[^\[\]\{\}\(\)'"]/)

  if r = match(re_close)
    result + r
  else
    throw "Unbalanced parenthesis"
  end
end
parse_compound() click to toggle source
# File lib/jsduck/doc/delimited_parser.rb, line 60
def parse_compound
  if look(/\[/)
    parse_balanced(/\[/, /\]/)
  elsif look(/\{/)
    parse_balanced(/\{/, /\}/)
  elsif look(/\(/)
    parse_balanced(/\(/, /\)/)
  elsif look(/"/)
    parse_string('"')
  elsif look(/'/)
    parse_string("'")
  end
end
parse_string(quote) click to toggle source

Parses “…” or '…' including the escape sequence ' or '"

# File lib/jsduck/doc/delimited_parser.rb, line 87
def parse_string(quote)
  re_quote = Regexp.new(quote)
  re_rest = Regexp.new("(?:[^"+quote+"\\\\]|\\\\.)*")
  match(re_quote) + match(re_rest) + (match(re_quote) || "")
end
parse_until_close_paren(re_paren, re_simple) click to toggle source

Parses until a given closing parenthesis. When unsuccessful, try simple parsing.

# File lib/jsduck/doc/delimited_parser.rb, line 37
def parse_until_close_paren(re_paren, re_simple)
  begin
    start_pos = @ds.input.pos
    result = parse_while(/[^\[\]\{\}\(\)'"]/)
    if look(re_paren)
      result
    else
      throw "Closing brace expected"
    end
  rescue
    @ds.input.pos = start_pos
    match(re_simple)
  end
end
parse_while(regex) click to toggle source
# File lib/jsduck/doc/delimited_parser.rb, line 52
def parse_while(regex)
  result = ""
  while r = parse_compound || match(regex)
    result += r
  end
  result
end