Skip to content

Commit 66c310b

Browse files
committed
grammar UI added
1 parent 6def2c0 commit 66c310b

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

app.css

+69
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,72 @@ ul {
213213
margin-top: 10px;
214214
color: #444;
215215
}
216+
217+
218+
219+
#grammar{
220+
position: fixed;
221+
width:100%;
222+
height:100%;
223+
right: 0;
224+
left: 0;
225+
top: 0;
226+
bottom: 0;
227+
background: rgba(0,0,0,.5);
228+
z-index: 999;
229+
display: none;
230+
}
231+
#grammar-content{
232+
position: fixed;
233+
width:calc(100% - 60px);
234+
height:calc(100% - 60px);
235+
right: 30px;
236+
left: 30px;
237+
top: 30px;
238+
bottom: 30px;
239+
background: white;
240+
z-index: 5;
241+
box-sizing: border-box;
242+
padding: 30px;
243+
display: flex;
244+
justify-content: space-between;
245+
}
246+
#grammar-close{
247+
position: relative;
248+
z-index: 10;
249+
margin:15px 0 0 15px;
250+
cursor: pointer;
251+
width:30px;
252+
height:30px;
253+
background: white;
254+
border-radius:50%;
255+
display: flex;
256+
justify-content: center;
257+
align-items: center;
258+
font-size:20px;
259+
box-shadow: 0 0 10px 0 rgba(0, 0, 0, .3);
260+
}
261+
#grammar-source, #grammar-result{
262+
width:100%;
263+
height:100%;
264+
box-sizing: border-box;
265+
padding:10px;
266+
font-size:16px;
267+
font-weight: normal;
268+
}
269+
#grammar-result{
270+
height:auto;
271+
margin-top:20px;
272+
background: rgba(0,0,0,.1);
273+
user-select: text;
274+
}
275+
.col-6{
276+
width:calc(50% - 10px);
277+
}
278+
#grammar-actions{
279+
display: flex;
280+
flex-direction: column;
281+
}
282+
#grammar-actions > *{
283+
margin-bottom:10px;
284+
}

index.html

+33
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,39 @@ <h1>Finite Automata Designer</h1>
4747
<button data-key="move" class="active">move</button>
4848
<button data-key="design">design</button>
4949
</div>
50+
<button id="grammar-view">Grammar</button>
51+
</div>
52+
53+
<div id="grammar">
54+
<div id="grammar-close">×</div>
55+
<div id="grammar-content">
56+
<div class="col-6">
57+
<textarea placeholder="Enter grammar here ..." id="grammar-source"></textarea>
58+
</div>
59+
<div class="col-6">
60+
<div id="grammar-actions">
61+
<label>
62+
<input type="radio" name="grammar-action" value="chomsky">
63+
To Chomsky
64+
</label>
65+
<label>
66+
<input type="radio" name="grammar-action" value="greibach">
67+
To Greibach
68+
</label>
69+
<label>
70+
<input type="radio" name="grammar-action" value="simplify">
71+
Simplify
72+
</label>
73+
<label>
74+
<input type="radio" name="grammar-action" value="right-linear">
75+
To Right Linear
76+
</label>
77+
</div>
78+
<div id="grammar-result">
79+
Result :
80+
</div>
81+
</div>
82+
</div>
5083
</div>
5184

5285
<div id="copyright">

js/app.js

+47
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,50 @@ cnv.oncontextmenu = function (e) {
472472
});
473473
}
474474
};
475+
476+
477+
478+
let grammarAction = '';
479+
$('#grammar-source').value = loadGrammar() ? loadGrammar() : '';
480+
481+
$('#grammar-source').onkeyup = executeGrammarAction;
482+
$$('input[name="grammar-action"]').forEach(el => el.onchange = function(){
483+
switch (this.value) {
484+
case 'simplify':
485+
grammarAction = 'simplify';
486+
break;
487+
488+
case 'right-linear':
489+
grammarAction = 'toRightLinear';
490+
break;
491+
492+
case 'chomsky':
493+
grammarAction = 'toCNF';
494+
break;
495+
496+
case 'greibach':
497+
grammarAction = 'toGNF';
498+
break;
499+
}
500+
executeGrammarAction();
501+
});
502+
$('#grammar-view').onclick = () => $('#grammar').style.display = 'block';
503+
$('#grammar-close').onclick = () => $('#grammar').style.display = 'none';
504+
505+
function executeGrammarAction(){
506+
const grammarSource = $('#grammar-source').value;
507+
508+
saveGrammar(grammarSource);
509+
510+
if(!grammarAction) return;
511+
512+
try{
513+
let grammar = new Grammar().parse(grammarSource);
514+
515+
grammar = grammar[grammarAction]();
516+
$('#grammar-result').innerHTML = 'Result : <br><br>' + grammar.stringify().replace(/\n/g, '<br>');
517+
}catch (e) {
518+
console.log(e)
519+
$('#grammar-result').innerHTML = e;
520+
}
521+
}

js/storage.js

+17
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,20 @@ function isFirstVisit() {
3737
return true;
3838
}
3939
}
40+
41+
function saveGrammar(grammar){
42+
try{
43+
localStorage.grammar = grammar;
44+
45+
return true;
46+
}catch (e) {
47+
return false;
48+
}
49+
}
50+
function loadGrammar() {
51+
try {
52+
return localStorage.grammar;
53+
}catch (e) {
54+
return undefined;
55+
}
56+
}

0 commit comments

Comments
 (0)