@@ -68,32 +68,52 @@ class Config:
6868 }
6969 }
7070
71- @classmethod
72- def toFSA (cls , data : dict ) -> FSA :
73- """
74- Convert frontend FSA payload (with transitions as "from|symbol|to")
75- into the FSABackend model with proper Transition objects.
76- """
77- states = data .get ("states" , [])
78- alphabet = data .get ("alphabet" , [])
79- initial_state = data .get ("initial_state" , "q0" )
80- accept_states = data .get ("accept_states" , [])
81-
82- flat_transitions = data .get ("transitions" , [])
71+ def toFSA (self ) -> FSA :
8372 transitions : List [Transition ] = []
84- for t in flat_transitions :
85- try :
86- from_state , symbol , to_state = t .split ("|" )
87- transitions .append (
88- Transition (from_state = from_state , symbol = symbol , to_state = to_state )
73+
74+ for t in self .transitions :
75+ parts = t .split ("|" )
76+
77+ # allow trailing delimiter but enforce structure
78+ if len (parts ) == 4 and parts [- 1 ] == "" :
79+ parts = parts [:- 1 ]
80+
81+ if len (parts ) != 3 :
82+ raise ValueError (
83+ f"Invalid transition format '{ t } '. "
84+ "Expected 'from|symbol|to'"
8985 )
90- except ValueError :
91- raise ValueError (f"Invalid transition format: '{ t } '" )
86+
87+ from_state , symbol , to_state = parts
88+
89+ if from_state not in self .states :
90+ raise ValueError (f"Unknown from_state '{ from_state } '" )
91+
92+ if to_state not in self .states :
93+ raise ValueError (f"Unknown to_state '{ to_state } '" )
94+
95+ if symbol not in self .alphabet :
96+ raise ValueError (f"Symbol '{ symbol } ' not in alphabet" )
97+
98+ transitions .append (
99+ Transition (
100+ from_state = from_state ,
101+ symbol = symbol ,
102+ to_state = to_state ,
103+ )
104+ )
105+
106+ if self .initial_state not in self .states :
107+ raise ValueError ("initial_state must be in states" )
108+
109+ for s in self .accept_states :
110+ if s not in self .states :
111+ raise ValueError (f"Accept state '{ s } ' not in states" )
92112
93113 return FSA (
94- states = states ,
95- alphabet = alphabet ,
114+ states = self . states ,
115+ alphabet = self . alphabet ,
96116 transitions = transitions ,
97- initial_state = initial_state ,
98- accept_states = accept_states ,
99- )
117+ initial_state = self . initial_state ,
118+ accept_states = self . accept_states ,
119+ )
0 commit comments