Syntax colouring / editor woes ...any solutions?

I’m using: SWI-Prolog version 8.0.3 on amd64 on linux mint.

I got fed up with Emacs today because it constantly breaks the syntax colouring when I use the single character form 0’_ … the single quote confuses it meaning that all the lines I have those on I usually have to add the comment: %’ …just to restore order.

token_type(0'(, bracket). %'
token_type(0'), bracket). %'
token_type(0'], bracket). %'
token_type(0'{, bracket). %'
token_type(0'}, bracket). %'
token_type(0';, com1).    %'
token_type(_, tok).       %'

to_bracket(0'(, o_paren). %'
to_bracket(0'), c_paren). %'
to_bracket(0'[, o_list).  %'
to_bracket(0'], c_list).  %'
to_bracket(0'{, o_map).   %'
to_bracket(0'}, c_map).   %'
to_bracket(X, huh) :- throw(tokfail(X)).

So…again, I thought I would go back to using the very capable built in emacs editor, but again I find it has issues, this time with indenting rather than the colouring. So which ever editor I use I am stuck with faulty colours or faulty indenting.

Here’s the indenting issue from PceEmacs,


tok_end(C) :-
        (   token_type(C, bracket)
        ;   isws(C)
        ;   C = 0';
).

Because the final check ends with the semicolon character, the indent engine seems not to want to put the closing paren out where I know it ought to be!!

How does everybody else manage? Is there another way of dealing with character constants that is more readable / less prone to bugging editors ? I know it’s a small thing but sometimes it really stops me getting into “the zone” with what I am doing… a simple tokeniser to learn some more.

Thanks chaps,
Sean.

Emacs doesn’t support regular expressions look-ahead assertions, which is what I use in other text editors that I support to properly highlight numbers using the 0'Char (or 0'\Char) syntax. But I was under the impression that some Emacs Prolog modes have a workaround for this case? It may help if you tells us which Prolog mode are you using.

Yes, in the excitement I forgot the details.

This is GNU Emacs 25.2.2 (x86_64-pc-linux-gnu, GTK+ Version 3.22.21)
 of 2017-09-22, modified by Debian
;; Authors: Emil Åström <emil_astrom(at)hotmail(dot)com>
;;          Milan Zamazal <pdm(at)freesoft(dot)cz>
;;          Stefan Bruda <stefan(at)bruda(dot)ca>  (current maintainer)
;;          * See below for more details
;; Keywords: prolog major mode sicstus swi mercury
(defvar prolog-mode-version "1.25"

I hacked up the version of prolog.el that comes with Emacs to fix exactly this issue (plus indenting forms with dictionary access). You can see that here.

1 Like

Can you direct me to the commit here you fixed the issue? I’m browsing your commits but have not located it yet. I’m wondering if I could back port your fix to Emacs support file I distribute with Logtalk.

I believe it was the combination of this to teach it the syntax group of the escapes and this to not make the previous change break indentation.

1 Like

Thanks. Unfortunately, the back porting would not be possible without bringing along a big chunk of the file as my logtalk.el is rather simplistic and not derived from existing prolog.el mode files. It would likely be far easier to merge the all contents of the logtalk.el into your prolog.el file.

Yeah, the prolog.el that I started off with is quite complex…I guess logtalk.el is mainly setting keywords to highlight? Does it handle indentation? That was the big thing I missed using prolog-mode for Logtalk files, having to manually indent things inside :- object(...)./:-end_object.

No handling of indentation, sorry. I’m not an Emacs user and thus never had the motivation to write a better mode file. Contributions welcome :slightly_smiling_face:

Supporting all the text editors that you see on the coding directory was/is quite exhausting due to the many variations of regular expression engines and all the different ideas of how to add support for a new programming language (for several of them, there was no Prolog mode to start with).

1 Like

Dude! Thank you so much. My version was from 2003 would you believe, so I have just caught up on 15 years of development by Stefan and you hack too…excellent!

1 Like

9.0.3 on linux mint, but I think this is more Emacs but I post it here to continue this thread!
I think I found another example of the syntax colouring not being quite right and again messing up the indenting… note the trailing comment I had to add to fix the test case such that the next test started in coloumn zero!

test(simple_comment_n) :-
    fsmexp(`/* test*/`, [comn(0,`/* test*/`)]),
    fsmexp(`/* test`, [comn(0,`/* test`)]). % */))   <-- fix sequence

test(simple_comment_1) :-  ...blah...

at least there are workaround!

You will keep finding more :frowning: The way Emacs auto-indent works is really hard to get completely reliable. When I designed its Prolog counterpart I copied these ideas from GNU-Emacs, so it is just as bad :frowning:

I wrote the auto-indenter for SWISH, which is based on CodeMirror. Here we see a different approach where you use a real parser that must be restartable using some state object associated with each linebreak. On and edit the editor will re-run this parser line-by-line from the first changed line until it finds `no change’ or is past the current window. The parser reliably deals with Prolog tokenization and keeps track of parenthesis and a couple of other things to drive auto-indenting.

The advantage of this approach is that as long as the syntax is correct it works reliable (or, at least can be made to run reliable), The disadvantage is that it can make a real mess if the syntax is illegal. Well, that can also be seen as an advantage as disfunctioning auto-indent means you have a syntax error.

1 Like

Hi Jan,

Yes, I am sure there are lots of edge cases…and I had to laugh when you mentioned about the disfunctioning auto-indent…that’s a BIG alarm bell for me in any of the languages I work with, if Emacs isn’t happy…

Never mind. II have a workaround and the positive of course is it is still Prolog! I mean, I could be being forced to use JavaScript right know… or PHP…

Diving deeper into Prolog is certainly keeping my busy in these locked down days in the UK, for which I am grateful!

All the best, stay safe!
Sean.

I’ve also started looking into a system called tree-sitter that aims to be a more robust way of having a restartable parser that one can use for this sort of thing. There’s an Emacs lib for it here. I started working on a Prolog grammar for it but haven’t tried actually using it with the editor yet.

2 Likes

Very interesting. I’ve bookmarked that for a read later this evening. Thanks for that! :smiley: