Skip to content
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

Closed
wants to merge 3 commits into from
Closed

Line Directive Patch #911

wants to merge 3 commits into from

Conversation

gam3
Copy link

@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 mentioned this pull request May 26, 2015
@marshall-lee
Copy link

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
Copy link
Member

eregon commented May 26, 2015

@marshall-lee Agreed.

@gam3
Copy link
Author

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
Copy link
Author

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
Copy link

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
Copy link
Author

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 -*-

@k0kubun
Copy link
Member

k0kubun commented Aug 17, 2019

It seems to have a conflict now. Could you rebase this from master?

@k0kubun
Copy link
Member

k0kubun commented Aug 19, 2019

Let me close this as it has not been updated for a while. Please reopen this after resolving conflicts. Thanks.

@k0kubun k0kubun closed this Aug 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants