Skip to content

Commit

Permalink
Merge pull request #1700 from ruby/options-on-match-last-line
Browse files Browse the repository at this point in the history
Expose options on match last line nodes
  • Loading branch information
kddnewton authored Oct 26, 2023
2 parents f6015ca + 0284b38 commit d088c0c
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions lib/prism/node_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
# Here we are reopening the prism module to provide methods on nodes that aren't
# templated and are meant as convenience methods.
module Prism
module RegularExpressionOptions
# Returns a numeric value that represents the flags that were used to create
# the regular expression.
def options
o = flags & (RegularExpressionFlags::IGNORE_CASE | RegularExpressionFlags::EXTENDED | RegularExpressionFlags::MULTI_LINE)
o |= Regexp::FIXEDENCODING if flags.anybits?(RegularExpressionFlags::EUC_JP | RegularExpressionFlags::WINDOWS_31J | RegularExpressionFlags::UTF_8)
o |= Regexp::NOENCODING if flags.anybits?(RegularExpressionFlags::ASCII_8BIT)
o
end
end

private_constant :RegularExpressionOptions

class FloatNode < Node
# Returns the value of the node as a Ruby Float.
def value
Expand All @@ -24,15 +37,16 @@ def value
end
end

class InterpolatedMatchLastLineNode < Node
include RegularExpressionOptions
end

class InterpolatedRegularExpressionNode < Node
# Returns a numeric value that represents the flags that were used to create
# the regular expression.
def options
o = flags & (RegularExpressionFlags::IGNORE_CASE | RegularExpressionFlags::EXTENDED | RegularExpressionFlags::MULTI_LINE)
o |= Regexp::FIXEDENCODING if flags.anybits?(RegularExpressionFlags::EUC_JP | RegularExpressionFlags::WINDOWS_31J | RegularExpressionFlags::UTF_8)
o |= Regexp::NOENCODING if flags.anybits?(RegularExpressionFlags::ASCII_8BIT)
o
end
include RegularExpressionOptions
end

class MatchLastLineNode < Node
include RegularExpressionOptions
end

class RationalNode < Node
Expand All @@ -43,14 +57,7 @@ def value
end

class RegularExpressionNode < Node
# Returns a numeric value that represents the flags that were used to create
# the regular expression.
def options
o = flags & (RegularExpressionFlags::IGNORE_CASE | RegularExpressionFlags::EXTENDED | RegularExpressionFlags::MULTI_LINE)
o |= Regexp::FIXEDENCODING if flags.anybits?(RegularExpressionFlags::EUC_JP | RegularExpressionFlags::WINDOWS_31J | RegularExpressionFlags::UTF_8)
o |= Regexp::NOENCODING if flags.anybits?(RegularExpressionFlags::ASCII_8BIT)
o
end
include RegularExpressionOptions
end

class ConstantReadNode < Node
Expand Down

0 comments on commit d088c0c

Please sign in to comment.