Prolog "colorize"

Admin User, erstellt 15. Feb. 2024
         
/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
:- ensure_loaded(library(tester/beautify)).
:- ensure_loaded(library(misc/markup)).
/*************************************************************/
/* Colorize Utility Python */
/*************************************************************/
/**
* colorizepy_file(A, B):
* The predicate succeeds. As side effect it colorizes the Python
* text A into the file B. An already existing file B is silently
* overwritten.
*/
% colorizepy_file(+Atom, +Atom)
colorizepy_file(InName, OutName) :-
colorizepy_file(InName, OutName, []).
% colorizepy_file(+Atom, +Atom, +List)
colorizepy_file(InName, OutName, Opts) :-
sys_fancy_opts(Opts, Res),
setup_once_cleanup(
(open(OutName, write, OutStream), dom_output_new(OutStream, DomWriter)),
sys_colorizepy_dest(InName, DomWriter, Res),
close(DomWriter)).
% sys_colorizepy_dest(+Atom, +Stream, +Term)
sys_colorizepy_dest(InName, OutStream, Res) :-
sys_html_header(OutStream, Res),
sys_fancy_begin(OutStream),
setup_once_cleanup(
open(InName, read, InStream),
sys_colorizepy_stream(InStream, OutStream),
close(InStream)),
sys_fancy_end(OutStream),
sys_html_footer(OutStream).
% sys_colorizepy_stream(+Stream, +Stream)
sys_colorizepy_stream(InStream, OutStream) :-
get_code(InStream, C),
sys_colorizepy_get(C, H, InStream, D),
sys_colorizepy_rest(H, D, InStream, OutStream).
% sys_colorizepy_rest(+Term, +Integer, +Stream, +Stream)
sys_colorizepy_rest(end_of_file, _, _, _) :- !.
sys_colorizepy_rest(T, C, InStream, OutStream) :-
sys_colorizepy_put(T, OutStream),
sys_colorizepy_get(C, H, InStream, D),
sys_colorizepy_rest(H, D, InStream, OutStream).
/****************************************************************/
/* Colorize Helper Python */
/****************************************************************/
% sys_colorizepy_put(+Term, +Stream)
sys_colorizepy_put(atom(A), S) :- sys_reserved_py(A), !,
tag(S, '<span class="gr">'), put_atom(S, A), tag(S, '</span>').
sys_colorizepy_put(T, S) :-
sys_fancy_put(T, S).
% sys_colorizepy_get(+Integer, -Term, +Stream, -Integer)
sys_colorizepy_get(C, A, S, D) :-
sys_get_comment_py(C, A, S, D), !.
sys_colorizepy_get(C, A, S, D) :-
sys_get_token_py(A, S-C, _-D).
% sys_get_comment_py(+Integer, -Term, +Stream, -Integer)
sys_get_comment_py(0'#, line([0'#|L]), S, D) :- !,
get_code(S, C),
sys_get_line(C, L, S, D).
sys_get_comment_py(C, filler([C|L]), S, D) :- sys_is_white(C), !,
get_code(S, H),
sys_get_filler(H, L, S, D).
% sys_get_token_py(-Token, +Pair, -Pair)
sys_get_token_py(T) --> sys_current_code(C),
{sys_code_class(C, D)}, sys_get_class_py(D, C, T).
% sys_get_class_py(+Atom, +Integer, -Token, +Pair, -Pair)
sys_get_class_py(is_solo, C, T) --> !,
sys_get_line_or_solo_py(C, T).
sys_get_class_py(A, C, T) -->
sys_get_class(A, C, T).
/***************************************************************/
/* String Tokens Python */
/***************************************************************/
% sys_get_line_or_solo_py(+Integer, -Token, +Pair, -Pair)
sys_get_line_or_solo_py(0'\', single(R)) --> !,
sys_next_code, sys_get_quote_py(R, 0'\').
sys_get_line_or_solo_py(0'\", codes(R)) --> !,
sys_next_code, sys_get_quote_py(R, 0'\").
sys_get_line_or_solo_py(C, atom(A)) -->
sys_next_code, {char_code(A, C)}.
% sys_get_quote_py(-List, +Code, +Pair, -Pair)
sys_get_quote_py([], Q) --> sys_current_code(Q), !,
sys_next_code.
sys_get_quote_py(L, Q) --> sys_current_code(0'\\), !,
sys_next_code, sys_get_cont_py(L, Q).
sys_get_quote_py([C|L], Q) -->
sys_get_char_py(C), sys_get_quote_py(L, Q).
% sys_get_char_py(-Integer, +Pair, -Pair)
sys_get_char_py(_) --> sys_current_code(0'\n),
{throw(error(syntax_error(end_of_line_as_character),_))}.
sys_get_char_py(_) --> sys_current_code(0'\r),
{throw(error(syntax_error(end_of_line_as_character),_))}.
sys_get_char_py(_) --> sys_current_code(-1),
{throw(error(syntax_error(unbalanced_quoted),_))}.
sys_get_char_py(C) --> sys_current_code(C),
sys_next_code.
/***************************************************************/
/* Char Escape */
/***************************************************************/
% sys_get_cont_py(+Code, -List, +Code, +Pair, -Pair)
sys_get_cont_py([0'\\|L], Q) -->
sys_get_escape_py(L, R), sys_get_quote_py(R, Q).
% sys_get_escape_py(-List, +List, +Pair, -Pair)
sys_get_escape_py([0'x|L], R) --> sys_current_code(0'x), !,
sys_next_code, sys_get_hex_py(L, R, 2).
sys_get_escape_py([0'u|L], R) --> sys_current_code(0'x), !,
sys_next_code, sys_get_hex_py(L, R, 4).
sys_get_escape_py([0'U|L], R) --> sys_current_code(0'x), !,
sys_next_code, sys_get_hex_py(L, R, 8).
sys_get_escape_py(_, _) --> sys_current_code(-1),
{throw(error(syntax_error(illegal_escape),_))}.
sys_get_escape_py([C|L], L) --> sys_current_code(C),
sys_next_code.
% sys_get_hex_py(-List, +List, +Integer, +Pair, -Pair)
sys_get_hex_py(L, L, 0) --> !.
sys_get_hex_py([C|L], R, B) --> sys_current_code(C), {sys_is_hex_py(C)}, !,
sys_next_code, {D is B-1}, sys_get_hex_py(L, R, D).
sys_get_hex_py(_, _, _) -->
{throw(error(syntax_error(illegal_escape),_))}.
% sys_is_hex_py(+Code)
sys_is_hex_py(C) :- 0'0 =< C, C =< 0'9, !.
sys_is_hex_py(C) :- 0'a =< C, C =< 0'f, !.
sys_is_hex_py(C) :- 0'A =< C, C =< 0'F.
/****************************************************************/
/* Reserved Words Python */
/****************************************************************/
% sys_reserved_py(+Atom)
sys_reserved_py('and').
sys_reserved_py('as').
sys_reserved_py('assert').
sys_reserved_py('async').
sys_reserved_py('await').
sys_reserved_py('break').
sys_reserved_py('class').
sys_reserved_py('continue').
sys_reserved_py('def').
sys_reserved_py('del').
sys_reserved_py('elif').
sys_reserved_py('else').
sys_reserved_py('except').
sys_reserved_py('finally').
sys_reserved_py('for').
sys_reserved_py('from').
sys_reserved_py('global').
sys_reserved_py('if').
sys_reserved_py('import').
sys_reserved_py('in').
sys_reserved_py('is').
sys_reserved_py('lambda').
sys_reserved_py('nonlocal').
sys_reserved_py('not').
sys_reserved_py('or').
sys_reserved_py('pass').
sys_reserved_py('raise').
sys_reserved_py('return').
sys_reserved_py('try').
sys_reserved_py('while').
sys_reserved_py('with').
sys_reserved_py('yield').