Permalink
Browse files

Parse feature with description

  • Loading branch information...
1 parent e48635c commit 3d103228132516bc52473ad9e95976e11af8141f @txus txus committed Nov 30, 2011
View
@@ -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
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
View
@@ -1,6 +1,6 @@
# require 'parslet'
require_relative 'parser/lexer'
-# require_relative 'parser/parser'
+require_relative 'parser/parser'
# module Gherkin
# class Parser < Parslet::Parser
@@ -29,7 +29,7 @@ rule
But { [:BUT, text] }
# Text
- [^#]*$ { [:TEXT, text.strip] }
+ [^#\n]* { [:TEXT, text.strip] }
inner
def run(code)
@@ -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
@@ -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

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -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.