-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: refactor, highlight & animation
- Loading branch information
1 parent
50b4f39
commit 7c0b10f
Showing
8 changed files
with
134 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const Loader = () => ( | ||
<div | ||
className="w-screen h-screen bg-slate-900 | ||
flex items-center justify-center" | ||
> | ||
<div | ||
className="absolute right-1/2 bottom-1/2 | ||
transform translate-x-1/2 translate-y-1/2 " | ||
> | ||
<div | ||
className="border-solid border-cyan-300 border-4 | ||
border-t-transparent animate-spin rounded-full h-20 w-20" | ||
></div> | ||
</div> | ||
</div> | ||
); | ||
|
||
export default Loader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
const NfaVisualizer = ({ svg }: { svg?: SVGSVGElement }) => { | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
if (svg) { | ||
containerRef.current?.replaceChildren(svg); | ||
} | ||
}, [svg]); | ||
|
||
return ( | ||
<div | ||
ref={containerRef} | ||
className="pt-12 pb-8 w-full overflow-scroll | ||
rounded-md border-[1px] border-slate-800" | ||
></div> | ||
); | ||
}; | ||
|
||
export default NfaVisualizer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,53 @@ | ||
import { Graph } from "@viz-js/viz"; | ||
import { RegexEngine } from "regex-potata"; | ||
|
||
function dotFromRegex(regex: RegexEngine) { | ||
function graphFromRegex(regex: RegexEngine) { | ||
const states = regex.nfaStates(); | ||
const endState = states.length - 1; | ||
const transitions = Array.from(states) | ||
.flatMap((state) => | ||
regex | ||
.nfaTransition(state) | ||
?.map((t) => `${state} -> ${t.end} [label="${t.toString()}"]\n`) | ||
) | ||
.join("\n"); | ||
const dot = ` | ||
digraph { | ||
bgcolor=none; | ||
graph [rankdir=LR]; | ||
node [shape=circle, color=white, penwidth=2, fontcolor=white, fontname="Arial"]; | ||
edge [color="#67e8f9", fontcolor=white, fontname="Arial"]; | ||
${endState} [shape=doublecircle, color="#67e8f9"]; | ||
"" [shape=none] | ||
"" -> 0 | ||
${transitions} | ||
}`; | ||
|
||
return dot; | ||
const config: Graph = { | ||
graphAttributes: { | ||
bgcolor: "none", | ||
rankdir: "LR", | ||
}, | ||
nodeAttributes: { | ||
shape: "circle", | ||
color: "white", | ||
penwidth: 2, | ||
fontcolor: "white", | ||
fontname: "Arial", | ||
}, | ||
edgeAttributes: { | ||
color: "#67e8f9", | ||
fontcolor: "white", | ||
fontname: "Arial", | ||
}, | ||
nodes: [ | ||
{ name: "", attributes: { shape: "none" } }, | ||
{ | ||
name: endState.toString(), | ||
attributes: { shape: "doublecircle", color: "#67e8f9" }, | ||
}, | ||
], | ||
edges: [{ tail: "", head: "0" }], | ||
subgraphs: [], | ||
}; | ||
|
||
for (const state of states) { | ||
const transitions = regex.nfaTransition(state); | ||
|
||
if (transitions) { | ||
for (const transition of transitions) { | ||
config.edges!.push({ | ||
tail: state.toString(), | ||
head: transition.end.toString(), | ||
attributes: { label: transition.toString() }, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return config; | ||
} | ||
|
||
export { dotFromRegex }; | ||
export { graphFromRegex }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters