64738
|
1 |
/* Title: Tools/VSCode/src/grammar.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Generate static TextMate grammar for VSCode editor.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle.vscode
|
|
8 |
|
|
9 |
|
|
10 |
import isabelle._
|
|
11 |
|
|
12 |
|
|
13 |
object Grammar
|
|
14 |
{
|
|
15 |
/* generate grammar */
|
|
16 |
|
65138
|
17 |
lazy val default_logic = Isabelle_System.getenv("ISABELLE_LOGIC")
|
64738
|
18 |
|
65138
|
19 |
def default_output(logic: String = ""): String =
|
64741
|
20 |
if (logic == "" || logic == default_logic) "isabelle-grammar.json"
|
64738
|
21 |
else "isabelle-" + logic + "-grammar.json"
|
|
22 |
|
64741
|
23 |
def generate(keywords: Keyword.Keywords): JSON.S =
|
64738
|
24 |
{
|
|
25 |
val (minor_keywords, operators) =
|
|
26 |
keywords.minor.iterator.toList.partition(Symbol.is_ascii_identifier(_))
|
|
27 |
|
|
28 |
def major_keywords(pred: String => Boolean): List[String] =
|
|
29 |
(for {
|
|
30 |
k <- keywords.major.iterator
|
|
31 |
kind <- keywords.kinds.get(k)
|
|
32 |
if pred(kind)
|
|
33 |
} yield k).toList
|
|
34 |
|
|
35 |
val keywords1 =
|
|
36 |
major_keywords(k => k != Keyword.THY_END && k != Keyword.PRF_ASM && k != Keyword.PRF_ASM_GOAL)
|
64742
|
37 |
val keywords2 = minor_keywords ::: major_keywords(Set(Keyword.THY_END))
|
64738
|
38 |
val keywords3 = major_keywords(Set(Keyword.PRF_ASM, Keyword.PRF_ASM_GOAL))
|
|
39 |
|
|
40 |
|
|
41 |
def grouped_names(as: List[String]): String =
|
64871
|
42 |
JSON.Format("\\b(" + as.sorted.map(Library.escape_regex(_)).mkString("|") + ")\\b")
|
64738
|
43 |
|
64741
|
44 |
"""{
|
64738
|
45 |
"name": "Isabelle",
|
|
46 |
"scopeName": "source.isabelle",
|
|
47 |
"fileTypes": ["thy"],
|
66351
|
48 |
"uuid": """ + JSON.Format(Library.UUID()) + """,
|
64738
|
49 |
"repository": {
|
64745
|
50 |
"comment": {
|
64738
|
51 |
"patterns": [
|
|
52 |
{
|
|
53 |
"name": "comment.block.isabelle",
|
|
54 |
"begin": "\\(\\*",
|
64745
|
55 |
"patterns": [{ "include": "#comment" }],
|
64746
|
56 |
"end": "\\*\\)"
|
64738
|
57 |
}
|
|
58 |
]
|
64745
|
59 |
},
|
|
60 |
"cartouche": {
|
|
61 |
"patterns": [
|
|
62 |
{
|
|
63 |
"name": "string.quoted.other.multiline.isabelle",
|
|
64 |
"begin": "(?:\\\\<open>|‹)",
|
|
65 |
"patterns": [{ "include": "#cartouche" }],
|
64746
|
66 |
"end": "(?:\\\\<close>|›)"
|
64745
|
67 |
}
|
|
68 |
]
|
64738
|
69 |
}
|
|
70 |
},
|
|
71 |
"patterns": [
|
|
72 |
{
|
64745
|
73 |
"include": "#comment"
|
|
74 |
},
|
|
75 |
{
|
|
76 |
"include": "#cartouche"
|
64738
|
77 |
},
|
|
78 |
{
|
|
79 |
"name": "keyword.control.isabelle",
|
|
80 |
"match": """ + grouped_names(keywords1) + """
|
|
81 |
},
|
|
82 |
{
|
65175
|
83 |
"name": "keyword.other.unit.isabelle",
|
64738
|
84 |
"match": """ + grouped_names(keywords2) + """
|
|
85 |
},
|
|
86 |
{
|
|
87 |
"name": "keyword.operator.isabelle",
|
|
88 |
"match": """ + grouped_names(operators) + """
|
|
89 |
},
|
|
90 |
{
|
65175
|
91 |
"name": "entity.name.type.isabelle",
|
64738
|
92 |
"match": """ + grouped_names(keywords3) + """
|
|
93 |
},
|
|
94 |
{
|
64746
|
95 |
"name": "constant.numeric.isabelle",
|
|
96 |
"match": "\\b\\d*\\.?\\d+\\b"
|
64738
|
97 |
},
|
|
98 |
{
|
|
99 |
"name": "string.quoted.double.isabelle",
|
|
100 |
"begin": "\"",
|
|
101 |
"patterns": [
|
|
102 |
{
|
64746
|
103 |
"name": "constant.character.escape.isabelle",
|
|
104 |
"match": """ + JSON.Format("""\\[\"]|\\\d\d\d""") + """
|
64738
|
105 |
}
|
|
106 |
],
|
64746
|
107 |
"end": "\""
|
64745
|
108 |
},
|
|
109 |
{
|
|
110 |
"name": "string.quoted.backtick.isabelle",
|
|
111 |
"begin": "`",
|
|
112 |
"patterns": [
|
|
113 |
{
|
64746
|
114 |
"name": "constant.character.escape.isabelle",
|
|
115 |
"match": """ + JSON.Format("""\\[\`]|\\\d\d\d""") + """
|
64745
|
116 |
}
|
|
117 |
],
|
64746
|
118 |
"end": "`"
|
|
119 |
},
|
|
120 |
{
|
|
121 |
"name": "string.quoted.verbatim.isabelle",
|
|
122 |
"begin": """ + JSON.Format("""\{\*""") + """,
|
|
123 |
"patterns": [
|
|
124 |
{ "match": """ + JSON.Format("""[^*]+|\*(?!\})""") + """ }
|
|
125 |
],
|
|
126 |
"end": """ + JSON.Format("""\*\}""") + """
|
64738
|
127 |
}
|
|
128 |
]
|
|
129 |
}
|
64741
|
130 |
"""
|
64738
|
131 |
}
|
|
132 |
|
|
133 |
|
|
134 |
/* Isabelle tool wrapper */
|
|
135 |
|
|
136 |
val isabelle_tool = Isabelle_Tool("vscode_grammar",
|
|
137 |
"generate static TextMate grammar for VSCode editor", args =>
|
|
138 |
{
|
|
139 |
var dirs: List[Path] = Nil
|
|
140 |
var logic = default_logic
|
|
141 |
var output: Option[Path] = None
|
|
142 |
|
|
143 |
val getopts = Getopts("""
|
|
144 |
Usage: isabelle vscode_grammar [OPTIONS]
|
|
145 |
|
|
146 |
Options are:
|
|
147 |
-d DIR include session directory
|
|
148 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(default_logic) + """)
|
|
149 |
-o FILE output file name (default """ + default_output() + """)
|
|
150 |
|
|
151 |
Generate static TextMate grammar for VSCode editor.
|
|
152 |
""",
|
|
153 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
|
|
154 |
"l:" -> (arg => logic = arg),
|
|
155 |
"o:" -> (arg => output = Some(Path.explode(arg))))
|
|
156 |
|
|
157 |
val more_args = getopts(args)
|
|
158 |
if (more_args.nonEmpty) getopts.usage()
|
|
159 |
|
66963
|
160 |
val keywords =
|
66976
|
161 |
Sessions.base_info(Options.init(), logic, dirs).check_base.overall_syntax.keywords
|
64738
|
162 |
val output_path = output getOrElse Path.explode(default_output(logic))
|
64741
|
163 |
|
64738
|
164 |
Output.writeln(output_path.implode)
|
64744
|
165 |
File.write_backup(output_path, generate(keywords))
|
64738
|
166 |
})
|
|
167 |
}
|