@@ -11,6 +11,8 @@ let loaded = false;
1111const editorDom = document . querySelector < HTMLLinkElement > ( ".editor" ) ! ;
1212const gridDom = document . querySelector < HTMLDivElement > ( ".editor-grid" ) ! ;
1313const inputDom = document . querySelector < HTMLDivElement > ( ".editor-input" ) ! ;
14+ const inputTextDom =
15+ document . querySelector < HTMLTextAreaElement > ( ".editor-input-text" ) ! ;
1416const outputDom =
1517 document . querySelector < HTMLTextAreaElement > ( ".editor-output" ) ! ;
1618const modeBtn = document . querySelector < HTMLButtonElement > ( "#modeBtn" ) ! ;
@@ -20,23 +22,30 @@ const windowHeight = window.innerHeight;
2022
2123CodeMirror . defineSimpleMode ( "pest" , {
2224 start : [
25+ { regex : / \/ \/ .* / , token : "comment" } ,
26+ { regex : / [ a - z A - 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 : / ' (?: [ ^ ' \\ ] | \\ (?: [ n r t 0 ' " ] | x [ \d a - f A - F ] { 2 } | u \{ [ \d a - f A - F ] { 6 } \} ) ) ' / ,
2638 token : "string" ,
2739 } ,
28- { regex : / \/ \/ .* / , token : "comment" } ,
40+ { regex : / \} / , token : "bracket" , next : "start" } ,
41+ { regex : / # \w + / , token : "tag" } ,
42+ { regex : / [ a - z A - Z _ ] \w * / , token : "constiable-2" } ,
43+ { regex : / = / , token : "operator-2" } ,
2944 { regex : / \d + / , token : "number" } ,
3045 { regex : / [ ~ | * + ? & ! ] | ( \. \. ) / , token : "operator" } ,
31- { regex : / [ a - z A - 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+
124149function wideMode ( ) {
125150 modeBtn . onclick = restore ;
126151 modeBtn . innerText = "Normal Mode" ;
@@ -148,4 +173,16 @@ function restore() {
148173modeBtn . onclick = wideMode ;
149174formatBtn . 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 ) ;
0 commit comments