[m-rev.] [for review 2/4] Write foreign code line number markers in java output.

Julien Fischer jfischer at opturion.com
Mon Apr 22 02:07:52 AEST 2013


Hi,

On Thu, 18 Apr 2013, Paul Bone wrote:

> For review by Julien.
>
> Write foreign code line number markers in java output.

s/in java output/generate Java output.

> compiler/mlds_to_java.m:
>    When we compile Mercury to Java that contains foreign code we now write
>    out line number markers making it possible to parse the .java file to
>    determine the location in the .m file of the foreign code, without
>    parsing similar markers used to identify generated code.

...

> compiler/mlds_to_java.m | 144 ++++++++++++++++++++++++++++++++++--------------
> 1 file changed, 102 insertions(+), 42 deletions(-)
>
> diff --git a/compiler/mlds_to_java.m b/compiler/mlds_to_java.m
> index 5acbe9f..fa3c20e 100644
> --- a/compiler/mlds_to_java.m
> +++ b/compiler/mlds_to_java.m

...

> @@ -5155,10 +5152,23 @@ mlds_output_data_addr(data_addr(ModuleQualifier, DataName), !IO) :-
> :- mutable(last_context, prog_context, context_init, ground,
>     [untrailed, attach_to_io_state]).
>
> -:- pred output_context(java_out_info::in, mlds_context::in,
> -    io::di, io::uo) is det.
> +:- type context_marker
> +                % The beginning of some user defined Java code whose errors

I would say Java foreign code rather than "user defined Java code".

> +                % should be remorted with Mercury line numbers.

s/remorted/reported/


> +    --->    marker_begin_block
> +
> +                % The end of such a block.
> +    ;       marker_end_block

The convention used in the rest of this module is to place the comment
describing each data constructor *under* the constructor.

>
> -output_context(Info, Context, !IO) :-
> +                % This marks mercury generated code for which Java's line
> +                % numbers should be used, it's just a comment for the
> +                % Mercury developers.
> +    ;       marker_comment.
> +
> +:- pred output_context(java_out_info::in, context_marker::in,
> +    mlds_context::in, io::di, io::uo) is det.
> +
> +output_context(Info, Marker, Context, !IO) :-
>     LineNumbers = Info ^ joi_line_numbers,
>     (
>         LineNumbers = yes,
> @@ -5167,18 +5177,25 @@ output_context(Info, Context, !IO) :-
>         term.context_file(ProgContext, File),
>         term.context_line(ProgContext, Line),
>         (
> -            ProgContext \= LastContext,
> -            Line > 0,
> -            File \= ""
> +            % It is safe to ignore marker comments when if the comment isn't

Delete "if".

> +            % useful.  All other marker types must be emitted in all cases.
> +            Marker = marker_comment
> +            =>
> +            (
> +                ProgContext \= LastContext,
> +                Line > 0,
> +                File \= ""
> +            )
>         ->
>             % Java doesn't have an equivalent of #line directives.
> +            % We use the token MER_LINE to allow us to prase these lines out

s/prase/filter/

> +            % of the file when mangling javac's output.
>             % \u is treated as a Unicode escape even with comments.
> -            io.write_string("// ", !IO),
>             string.replace_all(File, "\\u", "\\\\u", SafePath),
> -            io.write_string(SafePath, !IO),
> -            io.write_string(":", !IO),
> -            io.write_int(Line, !IO),
> -            io.nl(!IO),
> +            % Do not modify this format string without modifying
> +            % util/mfilterjavac.m
> +            io.format("// %s %s:%d\n",
> +                [s(marker_string(Marker)), s(SafePath), i(Line)], !IO),
>             set_last_context(ProgContext, !IO)
>         ;
>             true

...

> +:- func num_lines(string) = int.
> +
> +num_lines(String) = Num :-
> +    foldl2((pred(C::in, N0::in, N::out, Prev::in, C::out) is det :-
> +        (
> +            % CR or LF or CRLF increment the line count, with care to ensure
> +            % that CRLF is only counted once.
> +            (
> +                C = '\r'
> +            ;
> +                (
> +                    C = '\n',
> +                    Prev \= '\r'
> +                )
> +            )
> +        ->
> +            N = N0 + 1
> +        ;
> +            N = N0
> +        )
> +        ), String, 1, Num, 'x', _).

Use a state variable for N0, N etc.

The rest seeems okay.

Cheers,
Julien.



More information about the reviews mailing list