author | Fabian Huch <huch@in.tum.de> |
Thu, 09 Nov 2023 15:45:51 +0100 | |
changeset 78931 | 26841d3c568c |
parent 76517 | b67c9ed2c810 |
child 80809 | 4a64fc4d1cde |
permissions | -rw-r--r-- |
76514
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/Tools/prismjs.ML |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
3 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
4 |
Support for the Prism.js syntax highlighter -- via Isabelle/Scala. |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
5 |
*) |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
6 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
7 |
signature PRISMJS = |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
8 |
sig |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
9 |
type language |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
10 |
val languages: unit -> language list |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
11 |
val language_names: language -> string list |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
12 |
val language_name: language -> string |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
13 |
val language_alias: language -> string list |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
14 |
val check_language: Proof.context -> string * Position.T -> string |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
15 |
datatype token = Token of string list * content |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
16 |
and content = Atom of string | Nested of token list |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
17 |
val decode_token: token XML.Decode.T |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
18 |
val decode_content: content XML.Decode.T |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
19 |
val token_types: token -> string list |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
20 |
val token_content: token -> content |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
21 |
val token_string: token -> string |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
22 |
val content_string: content -> string |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
23 |
val tokenize: {text: string, language: string} -> token list |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
24 |
end; |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
25 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
26 |
structure Prismjs: PRISMJS = |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
27 |
struct |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
28 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
29 |
(* languages *) |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
30 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
31 |
abstype language = Language of string list |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
32 |
with |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
33 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
34 |
fun language_names (Language names) = names; |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
35 |
val language_name = hd o language_names; |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
36 |
val language_alias = tl o language_names; |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
37 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
38 |
fun languages () = |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
39 |
\<^scala>\<open>Prismjs.languages\<close> [] |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
40 |
|> map (split_lines #> Language); |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
41 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
42 |
fun check_language ctxt arg = |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
43 |
let |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
44 |
val langs = languages () |> maps language_names |> sort_strings |> map (rpair ()); |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
45 |
val name = |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
46 |
Completion.check_item Markup.prismjs_languageN |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
47 |
(fn (name, _) => Markup.entity Markup.prismjs_languageN name) |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
48 |
langs ctxt arg; |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
49 |
in name end; |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
50 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
51 |
end; |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
52 |
|
76517 | 53 |
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o Pretty.str o quote o language_name); |
54 |
||
76514
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
55 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
56 |
(* tokenize *) |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
57 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
58 |
datatype token = Token of string list * content |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
59 |
and content = Atom of string | Nested of token list; |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
60 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
61 |
local open XML.Decode in |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
62 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
63 |
fun decode_token body = body |> pair (list string) decode_content |> Token |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
64 |
and decode_content body = |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
65 |
body |> variant |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
66 |
[fn ([a], []) => Atom a, |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
67 |
fn ([], a) => Nested (list decode_token a)] |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
68 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
69 |
end; |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
70 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
71 |
fun token_types (Token (types, _)) = types; |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
72 |
fun token_content (Token (_, content)) = content; |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
73 |
fun token_string tok = content_string (token_content tok) |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
74 |
and content_string (Atom string) = string |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
75 |
| content_string (Nested tokens) = implode (map token_string tokens); |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
76 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
77 |
fun tokenize {text, language} = |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
78 |
\<^scala>\<open>Prismjs.tokenize\<close> [text, language] |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
79 |
|> map (YXML.parse_body #> decode_token); |
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
80 |
|
2615cf68f6f4
ML support for the Prism.js syntax highlighter -- via Isabelle/Scala;
wenzelm
parents:
diff
changeset
|
81 |
end; |