-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.js
113 lines (111 loc) · 2.82 KB
/
grammar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const baseuml = require("./grammer/baseuml");
const jsonuml = require("./grammer/jsonuml");
const yamluml = require("./grammer/yamluml");
module.exports = grammar({
name: "plantuml",
conflicts: ($) => [
// NOTE:# is also key in html mark
// also mark the color,
// so they must be conflicts
[$.colorleader, $.uniqkey],
// NOTE: * is uniqkey, also string mark
[$.block_1, $._command_unit],
// NOTE: style block is conflicts with uniqkey, also use "<" and ">" and "/"
[$.block_style, $.uniqkey],
// NOTE: it is because $
[$.message, $.uniquevar],
],
rules: {
plantuml: ($) =>
choice(
$.baseuml,
$.gannttuml,
$.saltuml,
$.mindmapuml,
$.yamluml,
$.jsonuml
),
...yamluml,
...baseuml,
...jsonuml,
command: ($) => seq(repeat1($._command_unit), "\n"),
_command_unit: ($) =>
choice(
$.message,
$.block,
$.string,
$.method,
$.uniqkey,
$.color,
$.identifier,
$.uniquevar,
$.comment
),
method: ($) => seq($.identifier, "()"),
message: ($) => seq("$", $.identifier, "(", optional($.string), ")"),
block: ($) => choice($.block_1, $._block_2, $._block_3, $.block_style),
block_1: ($) =>
seq(
optional(choice($.uniquevar, $.identifier)),
"{",
repeat($.command),
"}"
),
_block_2: ($) => seq("[", repeat($._command_unit), "]"),
_block_3: ($) => seq("(", repeat($._command_unit), ")"),
block_style: ($) =>
seq(
"<",
field("head", seq($.identifier, optional(seq(":", $.identifier)), ">")),
choice(repeat($.command), repeat($._command_unit)),
field("end", seq("</", $.identifier, ">"))
),
uniqkey: ($) =>
choice(
".",
">",
"<",
"_",
":",
"*",
"+",
"-",
"#",
"!",
"/",
",",
"|",
"=",
";"
),
string: ($) =>
seq(
'"',
optional(
repeat(choice(token.immediate(prec(1, /[^"\\]+/)), $.escape_sequence))
),
'"'
),
escape_sequence: ($) =>
token.immediate(
seq(
"\\",
choice(
/[^xu0-7]/,
/[0-7]{1,3}/,
/x[0-9a-fA-F]{2}/,
/u[0-9a-fA-F]{4}/,
/u{[0-9a-fA-F]+}/
)
)
),
color: ($) => seq($.colorleader, $.identifier),
colorleader: ($) => "#",
uniquevar: ($) => seq("$", $.identifier),
identifier: ($) => /[a-zA-Z0-9_.\/]+/,
// TODO: mutiline comment
comment: ($) => choice($._signallinecomment, $._mutilinecomment),
_signallinecomment: (_) => token(seq("'", /[^\n]+/g)),
_mutilinecomment: (_) => seq("/'", repeat(/[^\n^']+/g), "'/"),
},
});