Equivalent to C's #line directive?

Discussion in 'Java' started by M. Uli Kusterer, Sep 20, 2004.

  1. 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
     
    M. Uli Kusterer, Sep 20, 2004
    #1
    1. Advertising

  2. > 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
    --
    The Nice programming language: http://nice.sf.net
     
    Daniel Bonniot, Sep 20, 2004
    #2
    1. Advertising

  3. M. Uli Kusterer

    Chris Smith Guest

    M. Uli Kusterer wrote:
    > 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
     
    Chris Smith, Sep 20, 2004
    #3
  4. In article <cimo2h$ltj$>,
    Daniel Bonniot <> wrote:

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


    Thanks.

    I'm not sure I grok these docs, but I've DLed them and I'll look at the
    stuff. Thanks.

    Cheers,
    -- Uli
    http://www.zathras.de
     
    M. Uli Kusterer, Sep 20, 2004
    #4
  5. In article <>,
    Chris Smith <> 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).


    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
     
    M. Uli Kusterer, Sep 20, 2004
    #5
  6. > 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
    --
    The Nice programming language: http://nice.sf.net
     
    Daniel Bonniot, Sep 21, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor

    line directive

    Mantorok Redgormor, Nov 16, 2003, in forum: C Programming
    Replies:
    2
    Views:
    344
    Sidney Cadot
    Nov 16, 2003
  2. AA

    Using #line directive

    AA, Nov 30, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    557
    Michael Nemtsev
    Dec 1, 2006
  3. Pawel

    #line directive makes gdb crazy ?

    Pawel, Jan 12, 2007, in forum: C Programming
    Replies:
    3
    Views:
    636
    Kenneth Brody
    Jan 15, 2007
  4. AA

    Using #line directive

    AA, Nov 30, 2006, in forum: C++
    Replies:
    0
    Views:
    369
  5. Alf P. Steinbach

    Re: Using #line directive

    Alf P. Steinbach, Nov 30, 2006, in forum: C++
    Replies:
    1
    Views:
    331
    Richard Heathfield
    Nov 30, 2006
Loading...

Share This Page