Created
October 19, 2010 00:46
-
-
Save burtlo/633369 to your computer and use it in GitHub Desktop.
Registering a parser with YARD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.