mirror of
https://github.com/ruby/ruby.git
synced 2026-01-26 20:19:19 +00:00
ripper: Preserve indentation
This commit is contained in:
parent
147134b474
commit
47efdae602
@ -20,9 +20,9 @@ class DSL
|
||||
NAME_PATTERN = /(?>\$|\d+|[a-zA-Z_][a-zA-Z0-9_]*|\[[a-zA-Z_.][-a-zA-Z0-9_.]*\])(?>(?:\.|->)[a-zA-Z_][a-zA-Z0-9_]*)*/.source
|
||||
NOT_REF_PATTERN = /(?>\#.*|[^\"$@]*|"(?>\\.|[^\"])*")/.source
|
||||
|
||||
def self.line?(line, lineno = nil)
|
||||
if %r</\*% *ripper(?:\[(.*?)\])?: *(.*?) *%\*/> =~ line
|
||||
new($2, $1&.split(",") || [], lineno)
|
||||
def self.line?(line, lineno = nil, indent: nil)
|
||||
if %r<(?<space>\s*)/\*% *ripper(?:\[(?<option>.*?)\])?: *(?<code>.*?) *%\*/> =~ line
|
||||
new(code, option&.split(",") || [], lineno, indent: indent || space)
|
||||
end
|
||||
end
|
||||
|
||||
@ -38,8 +38,9 @@ class DSL
|
||||
end
|
||||
}
|
||||
|
||||
def initialize(code, options, lineno = nil)
|
||||
def initialize(code, options, lineno = nil, indent: "\t\t\t")
|
||||
@lineno = lineno
|
||||
@indent = indent
|
||||
@events = {}
|
||||
@error = options.include?("error")
|
||||
@brace = options.include?("brace")
|
||||
@ -80,7 +81,7 @@ class DSL
|
||||
s = "{VALUE #{ (1..@vars).map {|v| "v#{ v }" }.join(",") };#{ s }}" if @vars > 0
|
||||
s << "ripper_error(p);" if @error
|
||||
s = "{#{ s }}" if @brace
|
||||
"\t\t\t#{s}"
|
||||
"#{@indent}#{s}"
|
||||
end
|
||||
|
||||
def new_var
|
||||
|
||||
@ -56,7 +56,7 @@ require_relative 'dsl'
|
||||
def generate_line(f, out)
|
||||
while line = f.gets
|
||||
case
|
||||
when gen = DSL.line?(line)
|
||||
when gen = DSL.line?(line, f.lineno)
|
||||
out << gen.generate << "\n"
|
||||
when line.start_with?("%%")
|
||||
out << "%%\n"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user