Skip to content

Commit

Permalink
Parse feature with description
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Nov 30, 2011
1 parent e48635c commit 3d10322
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ task :regenerate do

if has_rex && has_racc
`rex lib/gherkin/parser/gherkin.rex -o lib/gherkin/parser/lexer.rb`
# `racc lib/gherkin/parser/gherkin.y -o lib/gherkin/parser/parser.rb`
`racc lib/gherkin/parser/gherkin.y -o lib/gherkin/parser/parser.rb`
else
puts "You need both Rexical and Racc to do that. Install them by doing:"
puts
Expand Down
16 changes: 6 additions & 10 deletions lib/gherkin/ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def accept(visitor)
name = self.class.name.split('::').last
visitor.send("visit_#{name}".to_sym, self)
end

def pos(line, column)
@line, @column = line, column
end
end

class Feature < Node
Expand All @@ -15,11 +19,9 @@ class Feature < Node
include Enumerable

def initialize(name, scenarios=[], background=nil)
@line, @column = name.line_and_column

@name = name.to_s
@name = name
@background = background
@scenarios = scenarios
@scenarios = scenarios
end

def each
Expand Down Expand Up @@ -52,8 +54,6 @@ class Scenario < Node
include Enumerable

def initialize(name, steps=[], tags=[])
@line, @column = name.line_and_column

@name = name.to_s
@steps = steps
@tags = tags
Expand All @@ -67,8 +67,6 @@ def each
class Step < Node
attr_reader :name, :keyword
def initialize(name, keyword)
@line, @column = name.line_and_column

@name = name.to_s
@keyword = keyword.to_s
end
Expand All @@ -77,8 +75,6 @@ def initialize(name, keyword)
class Tag < Node
attr_reader :name
def initialize(name)
@line, @column = name.line_and_column

@name = name.to_s
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gherkin/parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# require 'parslet'
require_relative 'parser/lexer'
# require_relative 'parser/parser'
require_relative 'parser/parser'

# module Gherkin
# class Parser < Parslet::Parser
Expand Down
2 changes: 1 addition & 1 deletion lib/gherkin/parser/gherkin.rex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rule
But { [:BUT, text] }

# Text
[^#]*$ { [:TEXT, text.strip] }
[^#\n]* { [:TEXT, text.strip] }

inner
def run(code)
Expand Down
44 changes: 44 additions & 0 deletions lib/gherkin/parser/gherkin.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Compile with: racc gherkin.y -o parser.rb

class Gherkin::Parser

# Declare tokens produced by the lexer
token NEWLINE
token FEATURE BACKGROUND SCENARIO
token TAG
token GIVEN WHEN THEN AND BUT
token TEXT

rule

Root:
Feature { result = val[0] }
;

Feature:
FEATURE TEXT { result = AST::Feature.new(val[1]) }
| FEATURE TEXT NEWLINE
Description { result = AST::Feature.new(val[1]) }
;

Description:
TEXT NEWLINE
| Description TEXT NEWLINE
;

end

---- header
require_relative "lexer"
require_relative "../ast"

---- inner
def parse(code, show_tokens=false)
@tokens = Lexer.new.run(code)
p @tokens if show_tokens
do_parse
end

def next_token
@tokens.shift
end
2 changes: 1 addition & 1 deletion lib/gherkin/parser/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _next_token
when (text = @ss.scan(/But/))
action { [:BUT, text] }

when (text = @ss.scan(/[^#]*$/))
when (text = @ss.scan(/[^#\n]*/))
action { [:TEXT, text.strip] }

else
Expand Down
156 changes: 156 additions & 0 deletions lib/gherkin/parser/parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions test/gherkin/parser/parser_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'test_helper'

module Gherkin
describe Parser do
def parse(input)
parser = Parser.new
parser.parse(input)
end

it 'parses feature header without description' do
feature = parse(
"Feature: my feature"
)
feature.must_be_kind_of AST::Feature
feature.name.must_equal "my feature"
end

it 'parses feature header with description' do
feature = parse(
"Feature: my feature\n In order to do something\n As a user\n"
)
feature.must_be_kind_of AST::Feature
feature.name.must_equal "my feature"
end
end
end

0 comments on commit 3d10322

Please sign in to comment.