mirror of
https://github.com/ParkerTenBroeck/automata.git
synced 2026-06-06 21:24:06 -04:00
changed how machines are parsed/represented
This commit is contained in:
parent
132380e777
commit
34b20ec1fe
23 changed files with 1577 additions and 206 deletions
|
|
@ -10,7 +10,7 @@ crate-type = ["cdylib", "rlib"]
|
|||
serde_json = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
||||
automata = {path=".."}
|
||||
automata = {path="..", features = ["serde"]}
|
||||
console_error_panic_hook = "0.1.7"
|
||||
wasm-bindgen = "*"
|
||||
web-sys = { version = "0.3.83", features = ["Window", "Document", "HtmlElement", "Text"] }
|
||||
|
|
|
|||
|
|
@ -78,7 +78,11 @@ function buildAnalysis(text: string, doc: Text) {
|
|||
const { log, log_formatted, graph } = compile(text);
|
||||
|
||||
if (graph){
|
||||
setAutomaton(JSON.parse(graph))
|
||||
try{
|
||||
setAutomaton(JSON.parse(graph))
|
||||
}catch(e){
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Build ONE Decoration set: syntax + diagnostics
|
||||
|
|
|
|||
|
|
@ -132,6 +132,7 @@ export function clearAutomaton() {
|
|||
}
|
||||
|
||||
export function setAutomaton(auto: GraphDef) {
|
||||
console.log(auto);
|
||||
automaton = auto;
|
||||
automaton.final_states = new Set(automaton.final_states)
|
||||
automaton.states = new Set(automaton.states)
|
||||
|
|
|
|||
|
|
@ -149,7 +149,6 @@ pub struct CompileLog {
|
|||
pub end: Option<usize>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct Graph<'a> {
|
||||
initial: &'a str,
|
||||
|
|
@ -171,50 +170,11 @@ pub fn compile(input: &str) -> CompileResult {
|
|||
let result = automata::loader::parse_universal(&mut ctx);
|
||||
|
||||
let graph = if let Some(result) = result {
|
||||
match result {
|
||||
loader::Machine::Npda(npda) => {
|
||||
let mut transitions = HashMap::new();
|
||||
for ((from, symbol), to_transitions) in npda.transitions().entries(){
|
||||
let from = npda.get_state_name(from).unwrap_or("<INVALID>");
|
||||
let symbol = npda.get_symbol_name(symbol).unwrap_or("<INVALID>");
|
||||
for (char, to) in to_transitions.entries(){
|
||||
for to in to{
|
||||
let to_state = npda.get_state_name(to.state()).unwrap_or("<INVALID>");
|
||||
let string: &mut String = transitions.entry(format!("{from}#{to_state}")).or_default();
|
||||
if !string.is_empty(){
|
||||
string.push('\n');
|
||||
}
|
||||
let char = char.unwrap_or('ε');
|
||||
let stack = to.stack().iter().map(|s|npda.get_symbol_name(*s).unwrap_or("<INVALID>")).fold(String::new(), |mut s, b|{
|
||||
if !s.is_empty(){
|
||||
s.push_str(", ");
|
||||
}
|
||||
s.push_str(b);
|
||||
s
|
||||
});
|
||||
write!(string, "{char}, {symbol} -> [{stack}]").unwrap();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
let graph = Graph {
|
||||
states: npda.states().map(|(_, n)| n).collect(),
|
||||
initial: npda
|
||||
.get_state_name(npda.initial_state())
|
||||
.unwrap_or("<INVALID>"),
|
||||
final_states: npda
|
||||
.final_states()
|
||||
.map(|i| {
|
||||
i.map(|s| npda.get_state_name(s).unwrap_or("<INVALID>"))
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
transitions
|
||||
};
|
||||
|
||||
Some(serde_json::to_string(&graph).unwrap())
|
||||
}
|
||||
}
|
||||
Some(match result {
|
||||
loader::Machine::Fa(fa) => serde_json::to_string(&fa).unwrap(),
|
||||
loader::Machine::Pda(pda) => serde_json::to_string(&pda).unwrap(),
|
||||
loader::Machine::Tm(tm) => serde_json::to_string(&tm).unwrap(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue