Repository navigation
Expand file tree
/
Copy pathformat
More file actions
executable file
·52 lines (42 loc) · 1.35 KB
/
Copy pathformat
File metadata and controls
executable file
·52 lines (42 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env ruby
# frozen_string_literal: true
def comment?(line)
line.start_with?("//") || line.start_with?("/*") || line.start_with?("*") || line.end_with?("*/\n")
end
def no_indent?(lines, index)
line = lines[index]
return false if line.nil?
line.start_with?("|") ||
(line.end_with?(":\n") && !line.start_with?("*") && !line.start_with?("//")) ||
(comment?(line) && no_indent?(lines, index + 1))
end
file_path = File.expand_path("parser.y", __dir__)
parser = File.read(file_path)
prefix, parser = parser.split("%%", 2)
indent = 0
lines = parser.lines.each { |line| line.sub!(/^[ \t]*/, '') }
new_lines = lines.map.with_index do |line, index|
open_count = line.count("{")
close_count = line.count("}")
if open_count == 0 && close_count > 0
indent -= close_count; close_count = 0
elsif open_count == 1 && close_count == 1 && line.start_with?("}") && line.end_with?("{\n") # } else {
indent -= 1
open_count = 1
close_count = 0
end
if no_indent?(lines, index)
extra_indent = 0
else
extra_indent = 1
end
texts = []
(indent + extra_indent).times { texts << " " } if line != "\n"
texts << " " if line.start_with?("*") && lines[index - 1].start_with?(%r[/\*|\*])
texts << line
indent += open_count
indent -= close_count
texts.join
end
parser = new_lines.join
File.write(file_path, "#{prefix}%%#{parser}")