[m-users.] Correct use of pragma source_file and #N directives together ?

Sean Charles (emacstheviking) objitsu at gmail.com
Wed Aug 17 07:34:05 AEST 2022


Hi, I am trying to complete my literate programming tool by having it intersperse the above mentioned directives into the generated source, but having read section "19.6 Source file name" until I glazed over, I just cannot fathom my obvious error!
I have two small markdown files, which when rendered without the --mercury-lines option, an option to my program, stitch together and run just fine:

➜  mercury-mlpt git:(master) ✗ mlpt --append-duplicates mtest.md mtest2.md 
Walking trail starting from: <<*>>
! ensuring output path: .
> write:<<*>> ./test.m in progress.

:- module test.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module list.

% following is defined in the other file!
main(!IO) :-
    io.format("Yay\n", [], !IO),
    io.format("Yay once again\n", [], !IO),
    io.format("-- line counter test--\n", [], !IO),
    io.format("this is the final and\n", [], !IO),
    io.format("last word\n", [], !IO).

The two markdown files are here, apologies in advance for the length of this post but I am trying to be concise:

---- mtest.md
# Testing pragma source file

```prolog <<*>> test.m
:- module test.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module list.

% following is defined in the other file!
<<main>>
```

so now we can define the main file BUT the definition is in another file!

~~~ <<content>>
io.format("-- line counter test--\n", [], !IO),
io.format("this is the final and\n", [], !IO),
io.format("last word\n", [], !IO).
~~~

ALL DONE. THE END.
----

and the second file:

---- mtest2.md
# Main defined in here

To make sure it can pull out stuff across files

~~~{.prolog} <<main>>
main(!IO) :-
    io.format("Yay\n", [], !IO),
    io.format("Yay once again\n", [], !IO),
    <<content>>
~~~

The <<content>> section is back in the other file.
----

When I re-render with the --mercury-lines flag, the final output looks like this but it point blank compiles with lots of errors!

---- output with compiler directives ----
:- module test.
:- pragma source_file("mtest.md").
#5
:- interface.
:- pragma source_file("mtest.md").
#6
:- import_module io.
:- pragma source_file("mtest.md").
#7
:- pred main(io::di, io::uo) is det.
:- pragma source_file("mtest.md").
#8

:- pragma source_file("mtest.md").
#9
:- implementation.
:- pragma source_file("mtest.md").
#10
:- import_module list.
:- pragma source_file("mtest.md").
#11

:- pragma source_file("mtest.md").
#12
% following is defined in the other file!
:- pragma source_file("mtest2.md").
#6
main(!IO) :-
:- pragma source_file("mtest2.md").
#7
    io.format("Yay\n", [], !IO),
:- pragma source_file("mtest2.md").
#8
    io.format("Yay once again\n", [], !IO),
:- pragma source_file("mtest.md").
#19
    io.format("-- line counter test--\n", [], !IO),
:- pragma source_file("mtest.md").
#20
    io.format("this is the final and\n", [], !IO),
:- pragma source_file("mtest.md").
#21
    io.format("last word\n", [], !IO).

----

and the accompanying error output from the compiler:

➜  mercury-mlpt git:(master) ✗ mmc test.m
mtest.md:007: Error: no clauses for predicate `main'/2.
mtest2.md:007: Syntax error at token ':-': operator precedence error.
mtest2.md:008: Syntax error at token ':-': operator precedence error.
mtest2.md:009: Syntax error at token ':-': operator precedence error.
mtest2.md:020: Syntax error at token ':-': operator precedence error.
mtest2.md:021: Error: module qualifier in definition does not match preceding
mtest2.md:021:   `:- module' declaration.
mtest2.md:021: Syntax error at token ':-': operator precedence error.


I manually commented out the pragma source_file() directives in test.m, to leave only the first one, and it does compile. I don't understand why these errors are happening as the documentation says that it 'is a good idea'....' but it doesn't really say what the restrictions are on the use of the pragma and the line directive, not to the point where it was clear to me anyway. I can see that there are probably too many and they are upsetting the compilers parsing but I don't know why.

This is the last option I have to implement before initial GitHub release and I didn't think it would be this tricky!  :D The tool is working well, I am already using it for my ongoing transpiler IDE project.

Thanks,
Sean.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20220816/2a62e197/attachment.html>


More information about the users mailing list