mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 08:27:46 +00:00
DevTools: Add StateMachineGenerator utility
This program turns a description of a state machine that takes its input byte-by-byte into C++ code. The state machine is described in a custom format as specified below: ``` // Comments are started by two slashes, and cause the rest of the line // to be ignored @name ExampleStateMachine // sets the name of the generated class @namespace Test // sets the namespace (optional) @begin Begin // sets the state the parser will start in // The rest of the file contains one or more states and an optional // @anywhere directive. Each of these is a curly bracket delimited set // of state transitions. State transitions contain a selector, the // literal "=>" and a (new_state, action) tuple. Examples: // 0x0a => (Begin, PrintLine) // [0x00..0x1f] => (_, Warn) // '_' means no change // [0x41..0x5a] => (BeginWord, _) // '_' means no action // Rules common to all states. These take precedence over rules in the // specific states. @anywhere { 0x0a => (Begin, PrintLine) [0x00..0x1f] => (_, Warn) } Begin { [0x41..0x5a] => (Word, _) [0x61..0x7a] => (Word, _) // For missing values, the transition (_, _) is implied } Word { // The entry action is run when we transition to this state from a // *different* state. @anywhere can't have this @entry IncreaseWordCount 0x09 => (Begin, _) 0x20 => (Begin, _) // The exit action is run before we transition to any *other* state // from here. @anywhere can't have this @exit EndOfWord } ``` The generated code consists of a single class which takes a `Function<Action, u8>` as a parameter in its constructor. This gets called whenever an action is to be done. This is because some input might not produce an action, but others might produce up to 3 (exit, state transition, entry). The actions allow us to build a more advanced parser over the simple state machine. The sole public method, `void advance(u8)`, handles the input byte-by-byte, managing the state changes and requesting the appropriate Action from the handler. Internally, the state transitions are resolved via a lookup table. This is a bit wasteful for more complex state machines, therefore the generator is designed to be easily extendable with a switch-based resolver; only the private `lookup_state_transition` method needs to be re-implemented. My goal for this tool is to use it for implementing a standard-compliant ANSI escape sequence parser for LibVT, as described on <https://vt100.net/emu/dec_ansi_parser>
This commit is contained in:
parent
aa4d41fe2c
commit
22195d965f
3 changed files with 502 additions and 0 deletions
6
Userland/DevTools/StateMachineGenerator/CMakeLists.txt
Normal file
6
Userland/DevTools/StateMachineGenerator/CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(StateMachineGenerator ${SOURCES})
|
||||
target_link_libraries(StateMachineGenerator LagomCore)
|
Loading…
Add table
Add a link
Reference in a new issue