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 Java */
/*************************************************************/
/**
* colorizej_file(A, B):
* The predicate succeeds. As side effect it colorizes the Java
* text A into the file B. An already existing file B is silently
* overwritten.
*/
% colorizej_file(+Atom, +Atom)
colorizej_file(InName, OutName) :-
colorizej_file(InName, OutName, []).
% colorizej_file(+Atom, +Atom, +List)
colorizej_file(InName, OutName, Opts) :-
sys_fancy_opts(Opts, Res),
setup_once_cleanup(
(open(OutName, write, OutStream), dom_output_new(OutStream, DomWriter)),
sys_colorizej_dest(InName, DomWriter, Res),
close(DomWriter)).
% sys_colorizej_dest(+Atom, +Stream, +Term)
sys_colorizej_dest(InName, OutStream, Res) :-
sys_html_header(OutStream, Res),
sys_fancy_begin(OutStream),
setup_once_cleanup(
open(InName, read, InStream),
sys_colorizej_stream(InStream, OutStream),
close(InStream)),
sys_fancy_end(OutStream),
sys_html_footer(OutStream).
% sys_colorizej_stream(+Stream, +Stream)
sys_colorizej_stream(InStream, OutStream) :-
get_code(InStream, C),
sys_colorizej_get(C, H, InStream, D),
sys_colorizej_rest(H, D, InStream, OutStream).
% sys_colorizej_rest(+Term, +Integer, +Stream, +Stream)
sys_colorizej_rest(end_of_file, _, _, _) :- !.
sys_colorizej_rest(T, C, InStream, OutStream) :-
sys_colorizej_put(T, OutStream),
sys_colorizej_get(C, H, InStream, D),
sys_colorizej_rest(H, D, InStream, OutStream).
/****************************************************************/
/* Colorize Helper Java */
/****************************************************************/
% sys_colorizej_put(+Term, +Stream)
sys_colorizej_put(atom(A), S) :- sys_reserved_j(A), !,
tag(S, '<span class="gr">'), put_atom(S, A), tag(S, '</span>').
sys_colorizej_put(T, S) :-
sys_fancy_put(T, S).
% sys_colorizej_get(+Integer, -Term, +Stream, -Integer)
sys_colorizej_get(C, A, S, D) :-
sys_get_comment_j(C, A, S, D), !.
sys_colorizej_get(C, A, S, D) :-
sys_get_token_j(A, S-C, _-D).
% sys_get_comment_j(+Integer, -Term, +Stream, -Integer)
sys_get_comment_j(0'/, line([0'/, 0'/|L]), S, D) :- peek_code(S, 0'/), !,
get_code(S, _),
get_code(S, C),
sys_get_line(C, L, S, D).
sys_get_comment_j(0'/, block([0'/, 0'*|L]), S, D) :- peek_code(S, 0'*), !,
get_code(S, _),
get_code(S, C),
sys_get_block(C, L, S, D).
sys_get_comment_j(C, filler([C|L]), S, D) :- sys_is_white(C), !,
get_code(S, H),
sys_get_filler(H, L, S, D).
% sys_get_token_j(-Token, +Pair, -Pair)
sys_get_token_j(T) --> sys_current_code(C),
{sys_code_class(C, D)}, sys_get_class_j(D, C, T).
% sys_get_class_j(+Atom, +Integer, -Token, +Pair, -Pair)
sys_get_class_j(is_solo, C, T) --> !,
sys_get_line_or_solo_j(C, T).
sys_get_class_j(A, C, T) -->
sys_get_class(A, C, T).
/***************************************************************/
/* String Tokens Java */
/***************************************************************/
% sys_get_line_or_solo_j(+Integer, -Token, +Pair, -Pair)
sys_get_line_or_solo_j(0'\', single(R)) --> !,
sys_next_code, sys_get_quote_j(R, 0'\').
sys_get_line_or_solo_j(0'\", codes(R)) --> !,
sys_next_code, sys_get_quote_j(R, 0'\").
sys_get_line_or_solo_j(C, atom(A)) -->
sys_next_code, {char_code(A, C)}.
% sys_get_quote_j(-List, +Code, +Pair, -Pair)
sys_get_quote_j([], Q) --> sys_current_code(Q), !,
sys_next_code.
sys_get_quote_j(L, Q) --> sys_current_code(0'\\), !,
sys_next_code, sys_get_cont_j(L, Q).
sys_get_quote_j([C|L], Q) -->
sys_get_char_j(C), sys_get_quote_j(L, Q).
% sys_get_char_j(-Integer, +Pair, -Pair)
sys_get_char_j(_) --> sys_current_code(0'\n),
{throw(error(syntax_error(end_of_line_as_character),_))}.
sys_get_char_j(_) --> sys_current_code(0'\r),
{throw(error(syntax_error(end_of_line_as_character),_))}.
sys_get_char_j(_) --> sys_current_code(-1),
{throw(error(syntax_error(unbalanced_quoted),_))}.
sys_get_char_j(C) --> sys_current_code(C),
sys_next_code.
/***************************************************************/
/* Char Escape */
/***************************************************************/
% sys_get_cont_j(+Code, -List, +Code, +Pair, -Pair)
sys_get_cont_j([0'\\|L], Q) -->
sys_get_escape_j(L, R), sys_get_quote_j(R, Q).
% sys_get_escape_j(-List, +List, +Pair, -Pair)
sys_get_escape_j([0'u|L], R) --> sys_current_code(0'x), !,
sys_next_code, sys_get_hex_j(L, R, 4).
sys_get_escape_j(_, _) --> sys_current_code(0'\n),
{throw(error(syntax_error(illegal_escape),_))}.
sys_get_escape_j(_, _) --> sys_current_code(0'\r),
{throw(error(syntax_error(illegal_escape),_))}.
sys_get_escape_j(_, _) --> sys_current_code(-1),
{throw(error(syntax_error(illegal_escape),_))}.
sys_get_escape_j([C|L], L) --> sys_current_code(C),
sys_next_code.
% sys_get_hex_j(-List, +List, +Integer, +Pair, -Pair)
sys_get_hex_j(L, L, 0) --> !.
sys_get_hex_j([C|L], R, B) --> sys_current_code(C), {sys_is_hex_j(C)}, !,
sys_next_code, {D is B-1}, sys_get_hex_j(L, R, D).
sys_get_hex_j(_, _, _) -->
{throw(error(syntax_error(illegal_escape),_))}.
% sys_is_hex_j(+Code)
sys_is_hex_j(C) :- 0'0 =< C, C =< 0'9, !.
sys_is_hex_j(C) :- 0'a =< C, C =< 0'f, !.
sys_is_hex_j(C) :- 0'A =< C, C =< 0'F.
/****************************************************************/
/* Reserved Words Java */
/****************************************************************/
% sys_reserved_j(+Atom)
sys_reserved_j('abstract').
sys_reserved_j('continue').
sys_reserved_j('for').
sys_reserved_j('new').
sys_reserved_j('switch').
sys_reserved_j('assert').
sys_reserved_j('default').
sys_reserved_j('if').
sys_reserved_j('package').
sys_reserved_j('synchronized').
sys_reserved_j('boolean').
sys_reserved_j('do').
sys_reserved_j('goto').
sys_reserved_j('private').
sys_reserved_j('this').
sys_reserved_j('break').
sys_reserved_j('double').
sys_reserved_j('implements').
sys_reserved_j('protected').
sys_reserved_j('throw').
sys_reserved_j('byte').
sys_reserved_j('else').
sys_reserved_j('import').
sys_reserved_j('public').
sys_reserved_j('throws').
sys_reserved_j('case').
sys_reserved_j('enum').
sys_reserved_j('instanceof').
sys_reserved_j('return').
sys_reserved_j('transient').
sys_reserved_j('catch').
sys_reserved_j('extends').
sys_reserved_j('int').
sys_reserved_j('short').
sys_reserved_j('try').
sys_reserved_j('char').
sys_reserved_j('final').
sys_reserved_j('interface').
sys_reserved_j('static').
sys_reserved_j('void').
sys_reserved_j('class').
sys_reserved_j('finally').
sys_reserved_j('long').
sys_reserved_j('strictfp').
sys_reserved_j('volatile').
sys_reserved_j('const').
sys_reserved_j('float').
sys_reserved_j('native').
sys_reserved_j('super').
sys_reserved_j('while').