New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line Directive Patch #911

Open
wants to merge 3 commits into
base: trunk
from

Conversation

3 participants
@gam3

gam3 commented May 26, 2015

Add a line directive to Ruby

  # -*- line: [filename] {line}  -*-

The filename is parsed by the standard magic comment code. if only one argument is passes it is assumed to be a line number. If there is a parse error or the line number evaluates to 0, the directive is ignored.

This is done by creating a array of filenames and using the upper bits of the line_number to determine the current filename. The original filename is in position 0.

An extra node is added by the parser that informs the compiler of the filenames so the backtrace code can follow it.

The LINE and FILE constants are updated and compile time warnings are also effected.

https://bugs.ruby-lang.org/issues/11181

@gam3 gam3 referenced this pull request May 26, 2015

Closed

Ld 2 2 #910

@marshall-lee

This comment has been minimized.

marshall-lee commented May 26, 2015

I don't think it's a good choice for Ruby to have a directives.

You can implement your own preprocessor for these needs (piggybacking on eval capabilities) without touching an interpreter:

eval <<-RUBY, line, filename
  # ...
RUBY

Ruby is an interpreter and virtual machine but not your literate programming framework.

@eregon

This comment has been minimized.

Member

eregon commented May 26, 2015

@marshall-lee Agreed.

@gam3

This comment has been minimized.

gam3 commented May 26, 2015

The problem is that my input file looks like

file: alice
<alice>=
   puts "'#{__FILE__} #{__LINE__}'"  # alice line 2
   <bob>
   puts "'#{__FILE__} #{__LINE__}'"  # alice line 4

file: bob
<bob>=
   puts "'#{__FILE__} #{__LINE__}'"  # bob line 2
   <charlie>
   puts "'#{__FILE__} #{__LINE__}'"  # bob line 4

file: charly
<charlie>=
   puts "'#{__FILE__} #{__LINE__}'"  # charlie line 2

Some buildy thingy is run on this that reads all three files and used ''' as the start and generate something like:

eval <<-'RUBY', nil, 'alice', 1
  puts "'#{__FILE__} #{__LINE__}' = 'alice 2'"
  eval <<-'RUBY2', nil, 'bob', 1
puts "'#{__FILE__} #{__LINE__}' = 'bob 2'"
eval <<-'RUBY3', nil, 'charlie', 1
  puts "'#{__FILE__} #{__LINE__}' = 'charlie 2'"
RUBY3
puts "'#{__FILE__} #{__LINE__}' != 'bob 4'"  # ERROR: displays as 'bob 5'
  RUBY2
  puts "'#{__FILE__} #{__LINE__}' != 'alice 4'"   # ERROR: displays as 'alice 9'
RUBY

Then there is the less likeley case where you have non-complete 'code blocks'. This would be where
we (for some odd reseason) wanted to have the start of a block in one file and the end in another:

 file a = "class Bob"
 file b = "end # class Bob"

  #line 1 "a"
  class bob
  #line 1 "b"
  end # class Bob
@gam3

This comment has been minimized.

gam3 commented May 26, 2015

It does not have to be a directive, but I think that this is the cleanest approach.
As the dirctive sets the __LINE__ and __FILE__ constants at parse time.
Other syntax I have thought about implie that they can be changed at run time.

Also note that Ruby already has a directive

# -*- coding: UTF-8 -*-
@marshall-lee

This comment has been minimized.

marshall-lee commented May 27, 2015

Then there is the less likeley case where you have non-complete 'code blocks'.

What the problem in preprocessing the code smarter? Group all these chunks by file name, concatenate them and pass to eval.

@gam3

This comment has been minimized.

gam3 commented May 28, 2015

It does not have to be a directive, but I think that this is the cleanest approach.
As the dirctive sets the LINE and FILE constants at parse time.
Other syntax I have thought about implie that they can be changed at run time.

Also note that Ruby already has a directive

# -*- coding: UTF-8 -*-

@gam3 gam3 closed this May 28, 2015

@gam3 gam3 reopened this May 28, 2015

@gam3 gam3 force-pushed the gam3:line_directive branch from 189be45 to e57ffa2 Aug 22, 2015

@gam3 gam3 force-pushed the gam3:line_directive branch 2 times, most recently from d6d0878 to 32873f5 Sep 17, 2016

@gam3 gam3 force-pushed the gam3:line_directive branch from 32873f5 to 268307b Mar 6, 2017

@gam3 gam3 force-pushed the gam3:line_directive branch from 268307b to 6c2d8d8 Mar 7, 2017

@matzbot matzbot force-pushed the ruby:trunk branch from 2677ddd to ce7ad3a Jan 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment