lib/cucumber/cucumber_expressions/ast.rb in cucumber-cucumber-expressions-16.1.2 vs lib/cucumber/cucumber_expressions/ast.rb in cucumber-cucumber-expressions-17.0.0

- old
+ new

@@ -1,64 +1,42 @@ +# frozen_string_literal: true + module Cucumber module CucumberExpressions ESCAPE_CHARACTER = '\\' ALTERNATION_CHARACTER = '/' BEGIN_PARAMETER_CHARACTER = '{' END_PARAMETER_CHARACTER = '}' BEGIN_OPTIONAL_CHARACTER = '(' END_OPTIONAL_CHARACTER = ')' class Node - def initialize(type, nodes, token, start, _end) - if nodes.nil? && token.nil? - raise 'Either nodes or token must be defined' - end + attr_reader :type, :nodes, :token, :start, :end + + def initialize(type, nodes, token, start, ending) + raise 'Either nodes or token must be defined' if nodes.nil? && token.nil? + @type = type @nodes = nodes @token = token @start = start - @end = _end + @end = ending end - def type - @type - end + def text + return @nodes.map { |value| value.text }.join('') if @token.nil? - def nodes - @nodes - end - - def token @token end - def start - @start - end - - def end - @end - end - - def text - if @token.nil? - return @nodes.map { |value| value.text }.join('') - end - @token - end - def to_hash hash = Hash.new - hash["type"] = @type - unless @nodes.nil? - hash["nodes"] = @nodes.map { |node| node.to_hash } - end - unless @token.nil? - hash["token"] = @token - end - hash["start"] = @start - hash["end"] = @end + hash['type'] = @type + hash['nodes'] = @nodes.map { |node| node.to_hash } unless @nodes.nil? + hash['token'] = @token unless @token.nil? + hash['start'] = @start + hash['end'] = @end hash end end module NodeType @@ -68,42 +46,28 @@ ALTERNATIVE = 'ALTERNATIVE_NODE' PARAMETER = 'PARAMETER_NODE' EXPRESSION = 'EXPRESSION_NODE' end - class Token - def initialize(type, text, start, _end) - @type, @text, @start, @end = type, text, start, _end - end + attr_reader :type, :text, :start, :end - def type - @type + def initialize(type, text, start, ending) + @type, @text, @start, @end = type, text, start, ending end - def text - @text - end - - def start - @start - end - - def end - @end - end - def self.is_escape_character(codepoint) codepoint.chr(Encoding::UTF_8) == ESCAPE_CHARACTER end def self.can_escape(codepoint) c = codepoint.chr(Encoding::UTF_8) if c == ' ' # TODO: Unicode whitespace? return true end + case c when ESCAPE_CHARACTER true when ALTERNATION_CHARACTER true @@ -124,10 +88,11 @@ c = codepoint.chr(Encoding::UTF_8) if c == ' ' # TODO: Unicode whitespace? return TokenType::WHITE_SPACE end + case c when ALTERNATION_CHARACTER TokenType::ALTERNATION when BEGIN_PARAMETER_CHARACTER TokenType::BEGIN_PARAMETER @@ -176,13 +141,13 @@ end end def to_hash { - "type" => @type, - "text" => @text, - "start" => @start, - "end" => @end + 'type' => @type, + 'text' => @text, + 'start' => @start, + 'end' => @end } end end module TokenType