Equivalent to C's #line directive?

M

M. Uli Kusterer

Hi,

I'm writing a program that generates Java code from source code written
in another language. Now, when an error occurs, I'd like to have Java
report the file name and line number in the source file the user wrote.

C has a #line directive for that. Does Java have something similar?

In case I'm not clear, an example:

UserCode.p:

1: PROCEDURE Main
2: begin
4: logFilePathsStartingAt( "/dev" );
5: end


GeneratedCode.java

1: public static class UserCode
2: {
3: public static void main( String[] argv )
4: {
5: CreateLogWindow();
6:
7: PUtilityCode.LogFilePathsStartingAt( "/dev" );
8: }
9: }


Now, when an error occurs in line 7 of GeneratedCode.java, I would like
Java to tell the user "there was an error on line 4 in UserCode.p".
C has a "#line" preprocessor directive which I could write on line 6 as:

6: #line 4

and from then on any errors would be reported to be in line 4.

Thanks for any clues,
-- Uli
http://www.zathras.de
 
D

Daniel Bonniot

I'm writing a program that generates Java code from source code written
in another language. Now, when an error occurs, I'd like to have Java
report the file name and line number in the source file the user wrote.

C has a #line directive for that. Does Java have something similar?

Yes, this is covered by jsr 45. See
http://jcp.org/aboutJava/communityprocess/final/jsr045/index.html

From memory, you can generate an auxiliary file with the mappings between the
line numbers in your source language and the Java source.

Cheers,

Daniel
 
C

Chris Smith

M. Uli Kusterer said:
I'm writing a program that generates Java code from source code written
in another language. Now, when an error occurs, I'd like to have Java
report the file name and line number in the source file the user wrote.

C has a #line directive for that. Does Java have something similar?

There is no such thing in the Java language. It would be more typical
to translate your other language directly to bytecode (since bytecode is
perfectly portable), in which case you can include debug information for
whatever line numbering scheme you desire (and JSR 45 may help).

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

M. Uli Kusterer

C has a #line directive for that. Does Java have something similar?

There is no such thing in the Java language. It would be more typical
to translate your other language directly to bytecode (since bytecode is
perfectly portable), in which case you can include debug information for
whatever line numbering scheme you desire (and JSR 45 may help).[/QUOTE]

Well, I'd love to translate to bytecode directly, but I doubt I could
pull that off. At least not without a "Java Bytecode for dummies" guide.
:)

JSR 45 only works for byte code, does it? The docs look way too complex
and weird to be for Java source code. I guess I'll chicken out for now
and just leave out the line numbers and implement the parser that
compiles to Java source, and then I can always swap out the code
generator object and generate something else.

Thanks for the pointer,
-- Uli
http://www.zathras.de
 
D

Daniel Bonniot

JSR 45 only works for byte code, does it?

No, I definitely remember there are two ways to feed the numbers, one when you
generate bytecode (SourceDebugExtension), one when you generate source Java code.

Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top