CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/codemirror/mode/lean.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import * as CodeMirror from "codemirror";
7
8
/*
9
This is just a first tiny quick step. To finish this:
10
11
- Look at https://codemirror.net/demo/simplemode.html for how this works.
12
- Put everything from https://github.com/leanprover/vscode-lean/blob/master/syntaxes/lean.json in here.
13
14
playgroud to see the alternative of negative look ahead in action: https://regex101.com/r/lop9Se/1
15
16
*/
17
18
// This is redundant with the regexp's below, but we need this to do completions
19
// before the terms are ever used.
20
export const completions: string[] = "import|prelude|theory|definition|def|abbreviation|instance|renaming|hiding|exposing|parameter|parameters|begin|constant|constants|lemma|variable|variables|theorem|example|open|axiom|inductive|coinductive|with|structure|universe|universes|alias|precedence|reserve|postfix|prefix|infix|infixl|infixr|notation|end|using|namespace|section|local|set_option|extends|include|omit|class|classes|instances|raw|run_cmd|print|eval|reduce|check|help|exit|calc|have|this|match|do|suffices|show|by|in|at|let|forall|fun|exists|assume|from|Prop|Type|Sort".split(
21
"|"
22
);
23
completions.sort();
24
25
(CodeMirror as any).defineSimpleMode("lean", {
26
start: [
27
{ regex: /"(?:[^\\]|\\.)*?(?:"|$)/, token: "string" },
28
{ regex: /\/-/, token: "comment", next: "blockcomment" },
29
{
30
regex: /#(print|eval|reduce|check|help|exit)\b/,
31
token: "variable-3",
32
},
33
{ regex: /--.*/, token: "comment" },
34
{ regex: /[-+\/*=<>!]+/, token: "operator" },
35
{
36
regex: /((?!\.).{1}|^)\b(import|prelude|theory|definition|def|abbreviation|instance|renaming|hiding|exposing|parameter|parameters|begin|constant|constants|lemma|variable|variables|theorem|example|open|axiom|inductive|coinductive|with|structure|universe|universes|alias|precedence|reserve|postfix|prefix|infix|infixl|infixr|notation|end|using|namespace|section|local|set_option|extends|include|omit|class|classes|instances|raw|run_cmd)\b/,
37
token: "keyword",
38
},
39
{
40
regex: /((?!\.).{1}|^)\b(calc|have|this|match|do|suffices|show|by|in|at|let|forall|fun|exists|assume|from)\b/,
41
token: "variable-2",
42
},
43
{
44
regex: /\b(Prop|Type|Sort)\b/,
45
token: "atom",
46
},
47
{
48
regex: /0x[a-f\d]+|[-+]?(?:\.\d+|\d+\.?\d*)(?:e[-+]?\d+)?/i,
49
token: "number",
50
},
51
{ regex: /\/-.*?-\//, token: "comment" },
52
{ regex: /begin/, indent: true },
53
{ regex: /end/, dedent: true },
54
{ regex: /[a-z$][\w$]*/, token: "variable" },
55
{ regex: /b?"/, token: "string", next: "string" },
56
],
57
string: [
58
{ regex: /"/, token: "string", next: "start" },
59
{ regex: /(?:[^\\"]|\\(?:.|$))*/, token: "string" },
60
],
61
blockcomment: [
62
{ regex: /.*?-\//, token: "comment", next: "start" },
63
{ regex: /.*/, token: "comment" },
64
],
65
meta: {
66
dontIndentStates: ["comment"],
67
lineComment: "--",
68
},
69
});
70
71