class JsDuck::Js::Parser

A JavaScript parser implementation that uses RKelly and adapts its output to be the same as the old Esprima parser used to produce.

Constants

ADAPTER

Public Class Methods

new(input, options={}) click to toggle source
# File lib/jsduck/js/parser.rb, line 14
def initialize(input, options={})
  @input = input
end

Public Instance Methods

parse() click to toggle source

Parses JavaScript source code with RKelly, turns RKelly AST into Esprima AST, and associate comments with syntax nodes.

# File lib/jsduck/js/parser.rb, line 20
def parse
  parser = RKelly::Parser.new
  ast = parser.parse(@input)
  unless ast
    raise syntax_error(parser)
  end

  ast = ADAPTER.adapt(ast)
  # Adjust Program node range
  ast["range"] = [0, @input.length-1]
  return Js::Associator.new(@input).associate(ast)
end
syntax_error(parser) click to toggle source
# File lib/jsduck/js/parser.rb, line 33
def syntax_error(parser)
  token = parser.stopped_at
  if token
    "Invalid JavaScript syntax: Unexpected '#{token.value}' on line #{token.range.from.line}"
  else
    "Invalid JavaScript syntax: Unexpected end of file"
  end
end