Skip to content

Commit 660c66d

Browse files
authored
Added support for tags labels and localstorage save (#55)
1 parent b3eaf63 commit 660c66d

File tree

3 files changed

+70
-14
lines changed

3 files changed

+70
-14
lines changed

src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,32 @@ fn format_pair(pair: Pair<&str>, indent_level: usize, is_newline: bool) -> Strin
131131
.collect();
132132

133133
let dash = if is_newline { "- " } else { "" };
134-
134+
let pair_tag = match pair.as_node_tag() {
135+
Some(tag) => format!("(#{}) ", tag),
136+
None => String::new(),
137+
};
135138
match len {
136139
0 => format!(
137-
"{}{}{}: {:?}",
140+
"{}{}{}{}: {:?}",
138141
indent,
139142
dash,
143+
pair_tag,
140144
pair.as_rule(),
141145
pair.as_span().as_str()
142146
),
143-
1 => format!("{}{}{} > {}", indent, dash, pair.as_rule(), children[0]),
147+
1 => format!(
148+
"{}{}{}{} > {}",
149+
indent,
150+
dash,
151+
pair_tag,
152+
pair.as_rule(),
153+
children[0]
154+
),
144155
_ => format!(
145-
"{}{}{}\n{}",
156+
"{}{}{}{}\n{}",
146157
indent,
147158
dash,
159+
pair_tag,
148160
pair.as_rule(),
149161
children.join("\n")
150162
),

static/scripts/editor.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ let loaded = false;
1111
const editorDom = document.querySelector<HTMLLinkElement>(".editor")!;
1212
const gridDom = document.querySelector<HTMLDivElement>(".editor-grid")!;
1313
const inputDom = document.querySelector<HTMLDivElement>(".editor-input")!;
14+
const inputTextDom =
15+
document.querySelector<HTMLTextAreaElement>(".editor-input-text")!;
1416
const outputDom =
1517
document.querySelector<HTMLTextAreaElement>(".editor-output")!;
1618
const modeBtn = document.querySelector<HTMLButtonElement>("#modeBtn")!;
@@ -20,23 +22,30 @@ const windowHeight = window.innerHeight;
2022

2123
CodeMirror.defineSimpleMode("pest", {
2224
start: [
25+
{ regex: /\/\/.*/, token: "comment" },
26+
{ regex: /[a-zA-Z_]\w*/, token: "constiable" },
27+
{ regex: /=/, token: "operator", next: "mod" },
28+
],
29+
mod: [
30+
{ regex: /\{/, token: "bracket", next: "inside_rule" },
31+
{ regex: /[_@!$]/, token: "operator-2" },
32+
],
33+
inside_rule: [
34+
{ regex: /\/\/.*/, token: "comment" },
2335
{ regex: /"/, token: "string", next: "string" },
2436
{
2537
regex: /'(?:[^'\\]|\\(?:[nrt0'"]|x[\da-fA-F]{2}|u\{[\da-fA-F]{6}\}))'/,
2638
token: "string",
2739
},
28-
{ regex: /\/\/.*/, token: "comment" },
40+
{ regex: /\}/, token: "bracket", next: "start" },
41+
{ regex: /#\w+/, token: "tag" },
42+
{ regex: /[a-zA-Z_]\w*/, token: "constiable-2" },
43+
{ regex: /=/, token: "operator-2" },
2944
{ regex: /\d+/, token: "number" },
3045
{ regex: /[~|*+?&!]|(\.\.)/, token: "operator" },
31-
{ regex: /[a-zA-Z_]\w*/, token: "constiable" },
32-
{ regex: /=/, next: "mod" },
33-
],
34-
mod: [
35-
{ regex: /\{/, next: "start" },
36-
{ regex: /[_@!$]/, token: "operator-2" },
3746
],
3847
string: [
39-
{ regex: /"/, token: "string", next: "start" },
48+
{ regex: /"/, token: "string", next: "inside_rule" },
4049
{ regex: /(?:[^\\"]|\\(?:.|$))*/, token: "string" },
4150
],
4251
meta: {
@@ -121,6 +130,22 @@ function makeResizable(wideMode: boolean) {
121130
}
122131
}
123132

133+
type SavedGrammar = {
134+
grammar: string;
135+
input: string;
136+
};
137+
function saveCode() {
138+
const grammar = myCodeMirror.getValue();
139+
const input = inputTextDom.value;
140+
const json = JSON.stringify({ grammar, input } satisfies SavedGrammar);
141+
localStorage.setItem("last-editor-state", json);
142+
}
143+
function getSavedCode() {
144+
const json = localStorage.getItem("last-editor-state");
145+
const parsed = JSON.parse(json || "null");
146+
return parsed || { grammar: "", input: "" };
147+
}
148+
124149
function wideMode() {
125150
modeBtn.onclick = restore;
126151
modeBtn.innerText = "Normal Mode";
@@ -148,4 +173,16 @@ function restore() {
148173
modeBtn.onclick = wideMode;
149174
formatBtn.onclick = doFormat;
150175

151-
init().then(() => (loaded = true));
176+
init().then(() => {
177+
loaded = true;
178+
const url = new URL(window.location.href);
179+
const hasUrlGrammar = url.searchParams.get("g");
180+
if (!hasUrlGrammar) {
181+
const { grammar, input } = getSavedCode();
182+
myCodeMirror.setValue(grammar);
183+
inputTextDom.value = input;
184+
}
185+
});
186+
187+
inputTextDom.addEventListener("input", saveCode);
188+
myCodeMirror.on("change", saveCode);

static/style.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,14 @@ body {
645645
color: #7ef69d;
646646
}
647647
.cm-s-pest span.cm-constiable {
648-
color: #51d3f6;
648+
color: #4ea8d8;
649+
}
650+
.cm-s-pest span.cm-constiable-2 {
651+
color: white;
652+
}
653+
654+
.cm-s-pest span.cm-tag {
655+
color: #f6dc51;
649656
}
650657
.cm-s-pest span.cm-error {
651658
background: #ac4142;

0 commit comments

Comments
 (0)