Skip to content

Instantly share code, notes, and snippets.

@burtlo
Created October 19, 2010 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burtlo/633369 to your computer and use it in GitHub Desktop.
Save burtlo/633369 to your computer and use it in GitHub Desktop.
Registering a parser with YARD
module Cucumber
module Parser
class CityBuilder
include Gherkin::Rubify
def initialize(file)
@file = file
end
def ast
@feature
end
def feature(feature)
log.debug "FEATURE: #{feature.name} #{feature.line} #{feature.keyword} #{feature.description}"
end
def background(background)
log.debug "BACKGROUND #{background.keyword} #{background.name} #{background.line} #{background.description}"
end
def scenario(statement)
log.debug "SCENARIO"
end
def scenario_outline(statement)
scenario(statement)
end
def examples(examples)
log.debug "EXAMPLES"
end
def step(step)
log.debug "STEP #{step.multiline_arg}"
end
def eof
end
def syntax_error(state, event, legal_events, line)
# raise "SYNTAX ERROR"
end
end
end
end
module YARD::Parser::Cucumber
class FeatureParser < YARD::Parser::Base
def initialize(source, file = '(stdin)')
@builder = Cucumber::Parser::CityBuilder.new(file)
@tag_counts = {}
@tag_formatter = Gherkin::Formatter::TagCountFormatter.new(@builder, @tag_counts)
@parser = Gherkin::Parser::Parser.new(@tag_formatter, true, "root", false)
@source = source
@file = file
@feature = nil
end
# This method should be implemented to parse the source and return itself.
# @abstract
# @return [Base] this method should return itself
def parse
begin
@parser.parse(@source, @file, 0)
@feature = @builder.ast
return nil if @feature.nil? # Nothing matched
@feature.language = @parser.i18n_language
rescue Gherkin::Lexer::LexingError, Gherkin::Parser::ParseError => e
e.message.insert(0, "#{@file}: ")
raise e
end
self
end
# This method should be implemented to tokenize given source
# @abstract
# @return [Array] a list/tree of lexical tokens
def tokenize
end
# This method should be implemented to return a list of semantic tokens
# representing the source code to be post-processed. Otherwise the method
# should return nil.
#
# @abstract
# @return [Array] a list of semantic tokens representing the source code
# to be post-processed
# @return [nil] if no post-processing should be done
def enumerator
[@feature]
end
end
end
module YARD::Parser
# Registers a new parser type.
#
# @example Registering a parser for "java" files
# SourceParser.register_parser_type :java, JavaParser, 'java'
# @param [Symbol] type a symbolic name for the parser type
# @param [Base] parser_klass a class that implements parsing and tokenization
# @param [Array<String>, String, Regexp] extensions a list of extensions or a
# regex to match against the file extension
# @return [void]
# @see Parser::Base
SourceParser.register_parser_type :feature, YARD::Parser::Cucumber::FeatureParser, 'feature'
end
@saxenadeepakkumar
Copy link

saxenadeepakkumar commented Oct 13, 2017

How to register java parser? Is there any java parser available or something we need to create?
Also please let me know that steps to add parser in yard as I am new for ruby.
Please help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment