Class: SyntaxTree::RegexpLiteral
- Inherits:
-
Object
- Object
- SyntaxTree::RegexpLiteral
- Defined in:
- lib/syntax_tree.rb
Overview
RegexpLiteral represents a regular expression literal.
/.+/
Instance Attribute Summary collapse
-
#beginning ⇒ Object
readonly
- String
-
the beginning of the regular expression literal.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#ending ⇒ Object
readonly
- String
-
the ending of the regular expression literal.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#parts ⇒ Object
readonly
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the regular expression literal.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(beginning:, ending:, parts:, location:, comments: []) ⇒ RegexpLiteral
constructor
A new instance of RegexpLiteral.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(beginning:, ending:, parts:, location:, comments: []) ⇒ RegexpLiteral
Returns a new instance of RegexpLiteral.
9965 9966 9967 9968 9969 9970 9971 |
# File 'lib/syntax_tree.rb', line 9965 def initialize(beginning:, ending:, parts:, location:, comments: []) @beginning = beginning @ending = ending @parts = parts @location = location @comments = comments end |
Instance Attribute Details
#beginning ⇒ Object (readonly)
- String
-
the beginning of the regular expression literal
9950 9951 9952 |
# File 'lib/syntax_tree.rb', line 9950 def beginning @beginning end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
9963 9964 9965 |
# File 'lib/syntax_tree.rb', line 9963 def comments @comments end |
#ending ⇒ Object (readonly)
- String
-
the ending of the regular expression literal
9953 9954 9955 |
# File 'lib/syntax_tree.rb', line 9953 def ending @ending end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
9960 9961 9962 |
# File 'lib/syntax_tree.rb', line 9960 def location @location end |
#parts ⇒ Object (readonly)
- Array[ StringEmbExpr | StringDVar | TStringContent ]
-
the parts of the
regular expression literal
9957 9958 9959 |
# File 'lib/syntax_tree.rb', line 9957 def parts @parts end |
Instance Method Details
#child_nodes ⇒ Object
9973 9974 9975 |
# File 'lib/syntax_tree.rb', line 9973 def child_nodes parts end |
#format(q) ⇒ Object
9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 |
# File 'lib/syntax_tree.rb', line 9977 def format(q) braces = ambiguous?(q) || include?(%r{\/}) if braces && include?(/[{}]/) q.group do q.text(beginning) q.format_each(parts) q.text(ending) end elsif braces q.group do q.text("%r{") if beginning == "/" # If we're changing from a forward slash to a %r{, then we can # replace any escaped forward slashes with regular forward slashes. parts.each do |part| if part.is_a?(TStringContent) q.text(part.value.gsub("\\/", "/")) else q.format(part) end end else q.format_each(parts) end q.text("}") q.text(ending[1..-1]) end else q.group do q.text("/") q.format_each(parts) q.text("/") q.text(ending[1..-1]) end end end |
#pretty_print(q) ⇒ Object
10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 |
# File 'lib/syntax_tree.rb', line 10017 def pretty_print(q) q.group(2, "(", ")") do q.text("regexp_literal") q.breakable q.group(2, "(", ")") { q.seplist(parts) { |part| q.pp(part) } } q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 |
# File 'lib/syntax_tree.rb', line 10028 def to_json(*opts) { type: :regexp_literal, beging: beginning, ending: ending, parts: parts, loc: location, cmts: comments }.to_json(*opts) end |