diff options
| author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2024-05-12 11:59:44 +0900 |
|---|---|---|
| committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2024-05-21 13:52:29 +0900 |
| commit | 47efdae6028e39bbe8f35d325e26e6bc206251ef (patch) | |
| tree | 893107184339c2f3b7e70135adcba0d883eaffe2 /ext | |
| parent | 147134b474fc74b68537750a2f63c81a87bd0e86 (diff) | |
ripper: Preserve indentation
Diffstat (limited to 'ext')
| -rw-r--r-- | ext/ripper/tools/dsl.rb | 11 | ||||
| -rw-r--r-- | ext/ripper/tools/preproc.rb | 2 |
2 files changed, 7 insertions, 6 deletions
diff --git a/ext/ripper/tools/dsl.rb b/ext/ripper/tools/dsl.rb index 5ef1d69318..4815364c92 100644 --- a/ext/ripper/tools/dsl.rb +++ b/ext/ripper/tools/dsl.rb @@ -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 diff --git a/ext/ripper/tools/preproc.rb b/ext/ripper/tools/preproc.rb index a92be93d5b..5e8a6e0cb5 100644 --- a/ext/ripper/tools/preproc.rb +++ b/ext/ripper/tools/preproc.rb @@ -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" |
