Skip to content

Commit

Permalink
Add #capturing? and #comment? to all exp
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynetics committed Feb 9, 2023
1 parent 72b4694 commit 64d5232
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- `Regexp::Expression::Shared#{capturing?,comment?}`
- previously only available on capturing and comment groups

## [2.7.0] - 2023-02-08 - [Janosch Müller](mailto:janosch84@gmail.com)

### Added
Expand Down
6 changes: 5 additions & 1 deletion lib/regexp_parser/expression/classes/free_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ def quantify(*_args)
end
end

class Comment < Regexp::Expression::FreeSpace; end
class Comment < Regexp::Expression::FreeSpace
def comment?
true
end
end

class WhiteSpace < Regexp::Expression::FreeSpace
def merge(exp)
Expand Down
4 changes: 0 additions & 4 deletions lib/regexp_parser/expression/classes/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ class Base < Regexp::Expression::Subexpression
def parts
[text.dup, *expressions, ')']
end

def capturing?; false end

def comment?; false end
end

class Passive < Group::Base
Expand Down
8 changes: 8 additions & 0 deletions lib/regexp_parser/expression/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ def referential?
false # overridden to be true e.g. in Expression::Backreference::Base
end

def capturing?
false # overridden to be true in Expression::Group::Capture
end

def comment?
false # overridden to be true e.g. in Expression::Group::Comment
end

def nesting_level=(lvl)
@nesting_level = lvl
quantifier && quantifier.nesting_level = lvl
Expand Down
12 changes: 6 additions & 6 deletions spec/parser/free_space_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
[c-g] + # A set
(h|i|j) # A group
/x,
[1] => [Literal, to_s: 'a?', quantified?: true],
[2] => [WhiteSpace],
[3] => [Comment, to_s: "# One letter\n"],
[7] => [Comment, to_s: "# Another one\n"],
[11] => [Comment, to_s: "# A set\n"],
[15] => [Comment, to_s: "# A group\n"]
[1] => [Literal, to_s: 'a?', quantified?: true, comment?: false],
[2] => [WhiteSpace, comment?: false],
[3] => [Comment, to_s: "# One letter\n", comment?: true],
[7] => [Comment, to_s: "# Another one\n", comment?: true],
[11] => [Comment, to_s: "# A set\n", comment?: true],
[15] => [Comment, to_s: "# A group\n", comment?: true]

include_examples 'parse', /
a
Expand Down
12 changes: 6 additions & 6 deletions spec/parser/groups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
1 => [:assertion, :nlookbehind, Assertion::NegativeLookbehind]

include_examples 'parse', /a(?# is for apple)b(?# for boy)c(?# cat)/,
1 => [:group, :comment, Group::Comment],
3 => [:group, :comment, Group::Comment],
5 => [:group, :comment, Group::Comment]
1 => [:group, :comment, Group::Comment, capturing?: false, comment?: true],
3 => [:group, :comment, Group::Comment, capturing?: false, comment?: true],
5 => [:group, :comment, Group::Comment, capturing?: false, comment?: true]

if ruby_version_at_least('2.4.1')
include_examples 'parse', 'a(?~b)c(?~d)e',
Expand All @@ -21,7 +21,7 @@
end

include_examples 'parse', /(?m:a)/,
0 => [:group, :options, Group::Options, options: { m: true }, option_changes: { m: true }]
0 => [:group, :options, Group::Options, capturing?: false, options: { m: true }, option_changes: { m: true }]

# self-defeating group option
include_examples 'parse', /(?m-m:a)/,
Expand Down Expand Up @@ -55,8 +55,8 @@

# option switch in group
include_examples 'parse', /(a(?i-m)b)c/m,
0 => [:group, :capture, Group::Capture, options: { m: true }],
[0, 0] => [:literal, :literal, Literal, text: 'a', options: { m: true }],
0 => [:group, :capture, Group::Capture, capturing?: true, options: { m: true }],
[0, 0] => [:literal, :literal, Literal, capturing?: false, text: 'a', options: { m: true }],
[0, 1] => [:group, :options_switch, Group::Options, options: { i: true }, option_changes: { i: true, m: false }],
[0, 2] => [:literal, :literal, Literal, text: 'b', options: { i: true }],
1 => [:literal, :literal, Literal, text: 'c', options: { m: true }]
Expand Down

0 comments on commit 64d5232

Please sign in to comment.