Use #line for user code blocks to report input grammar position for errors
This commit is contained in:
1 parent
42035c3a55
commit
7a140623ff
5 files changed
+32
-18
No files matched your search
+1
-1
@@ -33,7 +33,7 @@ class Propane
|
||||
|
||||
def run(input_file, output_file, log_file, options)
|
||||
begin
|
||||
grammar = Grammar.new(File.read(input_file))
|
||||
grammar = Grammar.new(File.read(input_file), input_file)
|
||||
generator = Generator.new(grammar, output_file, log_file, options)
|
||||
generator.generate
|
||||
rescue Error => e
|
||||
|
||||
@@ -38,7 +38,13 @@ class Propane
|
||||
output_file = @output_file
|
||||
end
|
||||
erb = ERB.new(template, trim_mode: "<>")
|
||||
result = erb.result(binding.clone)
|
||||
result = erb.result(binding.clone).lines.each_with_index.map do |line, i|
|
||||
if line == "#linereset\n"
|
||||
"#line #{i + 2}\n"
|
||||
else
|
||||
line
|
||||
end
|
||||
end.join
|
||||
File.open(output_file, "wb") do |fh|
|
||||
fh.write(result)
|
||||
end
|
||||
|
||||
+14
-6
@@ -22,7 +22,8 @@ class Propane
|
||||
attr_reader :on_token_node
|
||||
attr_reader :token_user_fields
|
||||
|
||||
def initialize(input)
|
||||
def initialize(input, filename)
|
||||
@filename = filename
|
||||
@patterns = []
|
||||
@start_rules = []
|
||||
@tokens = []
|
||||
@@ -304,8 +305,12 @@ class Propane
|
||||
def parse_code_block_statement!
|
||||
if md = consume!(/<<([a-z]*)(.*?)>>\n/m)
|
||||
name, code = md[1..2]
|
||||
code.sub!(/\A\n/, "")
|
||||
code += "\n" unless code.end_with?("\n")
|
||||
code = code.chomp
|
||||
if code.start_with?("\n")
|
||||
code = %[#line #{@line_number + 1} "#{@filename}"#{code}\n#linereset\n]
|
||||
else
|
||||
code = %[#line #{@line_number} "#{@filename}"\n#{code}\n#linereset\n]
|
||||
end
|
||||
if @code_blocks[name]
|
||||
@code_blocks[name] += code
|
||||
else
|
||||
@@ -346,9 +351,12 @@ class Propane
|
||||
|
||||
def parse_code_block!
|
||||
if md = consume!(/<<(.*?)>>\n/m)
|
||||
code = md[1]
|
||||
code.sub!(/\A\n/, "")
|
||||
code += "\n" unless code.end_with?("\n")
|
||||
code = md[1].chomp
|
||||
if code.start_with?("\n")
|
||||
code = %[#line #{@line_number + 1} "#{@filename}"#{code}\n#linereset\n]
|
||||
else
|
||||
code = %[#line #{@line_number} "#{@filename}"\n#{code}\n#linereset\n]
|
||||
end
|
||||
code
|
||||
end
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user