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 JavaScript */
/*************************************************************/
/**
* colorize_file(A, B):
* The predicate succeeds. As side effect it colorizes the JavaScript
* text A into the file B. An already existing file B is silently
* overwritten.
*/
% colorize_file(+Atom, +Atom)
colorize_file(InName, OutName) :-
colorize_file(InName, OutName, []).
% colorize_file(+Atom, +Atom, +List)
colorize_file(InName, OutName, Opts) :-
sys_fancy_opts(Opts, Res),
setup_once_cleanup(
(open(OutName, write, OutStream), dom_output_new(OutStream, DomWriter)),
sys_colorize_dest(InName, DomWriter, Res),
close(DomWriter)).
% sys_colorize_dest(+Atom, +Stream, +Term)
sys_colorize_dest(InName, OutStream, Res) :-
sys_html_header(OutStream, Res),
sys_fancy_begin(OutStream),
setup_once_cleanup(
open(InName, read, InStream),
sys_colorize_stream(InStream, OutStream),
close(InStream)),
sys_fancy_end(OutStream),
sys_html_footer(OutStream).
% sys_colorize_stream(+Stream, +Stream)
sys_colorize_stream(InStream, OutStream) :-
get_code(InStream, C),
sys_colorize_get(C, H, InStream, D),
sys_colorize_rest(H, D, InStream, OutStream).
% sys_colorize_rest(+Term, +Integer, +Stream, +Stream)
sys_colorize_rest(end_of_file, _, _, _) :- !.
sys_colorize_rest(T, C, InStream, OutStream) :-
sys_colorize_put(T, OutStream),
sys_colorize_get(C, H, InStream, D),
sys_colorize_rest(H, D, InStream, OutStream).
/****************************************************************/
/* Colorize Helper JavaScript */
/****************************************************************/
% sys_colorize_put(+Term, +Stream)
sys_colorize_put(atom(A), S) :- sys_reserved_js(A), !,
tag(S, '<span class="gr">'), put_atom(S, A), tag(S, '</span>').
sys_colorize_put(T, S) :-
sys_fancy_put(T, S).
% sys_colorize_get(+Integer, -Term, +Stream, -Integer)
sys_colorize_get(C, A, S, D) :-
sys_get_comment_js(C, A, S, D), !.
sys_colorize_get(C, A, S, D) :-
sys_get_token_js(A, S-C, _-D).
% sys_get_comment_js(+Integer, -Term, +Stream, -Integer)
sys_get_comment_js(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_js(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_js(C, filler([C|L]), S, D) :- sys_is_white(C), !,
get_code(S, H),
sys_get_filler(H, L, S, D).
% sys_get_token_js(-Token, +Pair, -Pair)
sys_get_token_js(T) --> sys_current_code(C),
{sys_code_class(C, D)}, sys_get_class_js(D, C, T).
% sys_get_class_js(+Atom, +Integer, -Token, +Pair, -Pair)
sys_get_class_js(is_solo, C, T) --> !,
sys_get_line_or_solo_js(C, T).
sys_get_class_js(A, C, T) -->
sys_get_class(A, C, T).
/***************************************************************/
/* String Tokens JavaScript */
/***************************************************************/
% sys_get_line_or_solo_js(+Integer, -Token, +Pair, -Pair)
sys_get_line_or_solo_js(0'\', single(R)) --> !,
sys_next_code, sys_get_quote_js(R, 0'\').
sys_get_line_or_solo_js(0'\", codes(R)) --> !,
sys_next_code, sys_get_quote_js(R, 0'\").
sys_get_line_or_solo_js(C, atom(A)) -->
sys_next_code, {char_code(A, C)}.
% sys_get_quote_js(-List, +Code, +Pair, -Pair)
sys_get_quote_js([], Q) --> sys_current_code(Q), !,
sys_next_code.
sys_get_quote_js(L, Q) --> sys_current_code(0'\\), !,
sys_next_code, sys_get_cont_js(L, Q).
sys_get_quote_js([C|L], Q) -->
sys_get_char_js(C), sys_get_quote_js(L, Q).
% sys_get_char_js(-Integer, +Pair, -Pair)
sys_get_char_js(_) --> sys_current_code(0'\n),
{throw(error(syntax_error(end_of_line_as_character),_))}.
sys_get_char_js(_) --> sys_current_code(0'\r),
{throw(error(syntax_error(end_of_line_as_character),_))}.
sys_get_char_js(_) --> sys_current_code(-1),
{throw(error(syntax_error(unbalanced_quoted),_))}.
sys_get_char_js(C) --> sys_current_code(C),
sys_next_code.
/***************************************************************/
/* Char Escape */
/***************************************************************/
% sys_get_cont_js(+Code, -List, +Code, +Pair, -Pair)
sys_get_cont_js([0'\\|L], Q) -->
sys_get_escape_js(L, R), sys_get_quote_js(R, Q).
% sys_get_escape_js(-List, +List, +Pair, -Pair)
sys_get_escape_js([0'x|L], R) --> sys_current_code(0'x), !,
sys_next_code, sys_get_hex_js(L, R, 2).
sys_get_escape_js([0'u|L], R) --> sys_current_code(0'x), !,
sys_next_code, sys_get_hex_js(L, R, 4).
sys_get_escape_js(_, _) --> sys_current_code(-1),
{throw(error(syntax_error(illegal_escape),_))}.
sys_get_escape_js([C|L], L) --> sys_current_code(C),
sys_next_code.
% sys_get_hex_js(-List, +List, +Integer, +Pair, -Pair)
sys_get_hex_js(L, L, 0) --> !.
sys_get_hex_js([C|L], R, B) --> sys_current_code(C), {sys_is_hex_js(C)}, !,
sys_next_code, {D is B-1}, sys_get_hex_js(L, R, D).
sys_get_hex_js(_, _, _) -->
{throw(error(syntax_error(illegal_escape),_))}.
% sys_is_hex_js(+Code)
sys_is_hex_js(C) :- 0'0 =< C, C =< 0'9, !.
sys_is_hex_js(C) :- 0'a =< C, C =< 0'f, !.
sys_is_hex_js(C) :- 0'A =< C, C =< 0'F.
/****************************************************************/
/* Reserved Words JavaScript */
/****************************************************************/
% sys_reserved_js(+Atom)
sys_reserved_js('break').
sys_reserved_js('case').
sys_reserved_js('catch').
sys_reserved_js('class').
sys_reserved_js('const').
sys_reserved_js('continue').
sys_reserved_js('debugger').
sys_reserved_js('default').
sys_reserved_js('delete').
sys_reserved_js('do').
sys_reserved_js('else').
sys_reserved_js('export').
sys_reserved_js('extends').
sys_reserved_js('false').
sys_reserved_js('finally').
sys_reserved_js('for').
sys_reserved_js('function').
sys_reserved_js('if').
sys_reserved_js('import').
sys_reserved_js('in').
sys_reserved_js('instanceof').
sys_reserved_js('new').
sys_reserved_js('null').
sys_reserved_js('return').
sys_reserved_js('super').
sys_reserved_js('switch').
sys_reserved_js('this').
sys_reserved_js('throw').
sys_reserved_js('true').
sys_reserved_js('try').
sys_reserved_js('typeof').
sys_reserved_js('var').
sys_reserved_js('void').
sys_reserved_js('while').
sys_reserved_js('with').
sys_reserved_js('let').
sys_reserved_js('static').
sys_reserved_js('yield').
sys_reserved_js('await').