robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d9f93e5642
master
1" vimexpect.vim is a small object-oriented library that simplifies the task of 2" scripting communication with jobs or any interactive program. The name 3" `expect` comes from the famous tcl extension that has the same purpose. 4" 5" This library is built upon two simple concepts: Parsers and States. 6" 7" A State represents a program state and associates a set of regular 8" expressions(to parse program output) with method names(to deal with parsed 9" output). States are created with the vimexpect#State(patterns) function. 10" 11" A Parser manages data received from the program. It also manages State 12" objects by storing them into a stack, where the top of the stack is the 13" current State. Parsers are created with the vimexpect#Parser(initial_state, 14" target) function 15" 16" The State methods are defined by the user, and are always called with `self` 17" set as the Parser target. Advanced control flow is achieved by changing the 18" current state with the `push`/`pop`/`switch` parser methods. 19" 20" An example of this library in action can be found in Neovim source 21" code(contrib/neovim_gdb subdirectory) 22 23let s: State = {} 24 25 26" Create a new State instance with a list where each item is a [regexp, name] 27" pair. A method named `name` must be defined in the created instance. 28function s: State . create ( patterns ) 29let this = copy ( self ) 30let this . _patterns = a: patterns 31return this 32endfunction 33 34 35let s: Parser = {} 36let s: Parser . LINE_BUFFER_MAX_LEN = 100 37 38 39" Create a new Parser instance with the initial state and a target. The target 40" is a dictionary that will be the `self` of every State method call associated 41" with the parser, and may contain options normally passed to 42" `jobstart`(on_stdout/on_stderr will be overridden). Returns the target so it 43" can be called directly as the second argument of `jobstart`: 44" 45" call jobstart(prog_argv, vimexpect#Parser(initial_state, {'pty': 1})) 46function s: Parser . create ( initial_state , target ) 47let parser = copy ( self ) 48let parser . _line_buffer = [] 49let parser . _stack = [ a: initial_state ] 50let parser . _target = a: target 51let parser . _target . on_stdout = function ( 's:JobOutput' ) 52let parser . _target . on_stderr = function ( 's:JobOutput' ) 53let parser . _target . _parser = parser 54return parser . _target 55endfunction 56 57 58" Push a state to the state stack 59function s: Parser . push ( state ) 60call add ( self . _stack , a: state ) 61endfunction 62 63 64" Pop a state from the state stack. Fails if there's only one state remaining. 65function s: Parser . pop () 66if len ( self . _stack ) == 1 67throw 'vimexpect:emptystack:State stack cannot be empty' 68endif 69return remove ( self . _stack , -1 ) 70endfunction 71 72 73" Replace the state currently in the top of the stack. 74function s: Parser . switch ( state ) 75let old_state = self . _stack [ -1 ] 76let self . _stack [ -1 ] = a: state 77return old_state 78endfunction 79 80 81" Append a list of lines to the parser line buffer and try to match it the 82" current state. This will shift old lines if the buffer crosses its 83" limit(defined by the LINE_BUFFER_MAX_LEN field). During normal operation, 84" this function is called by the job handler provided by this module, but it 85" may be called directly by the user for other purposes(testing for example) 86function s: Parser . feed ( lines ) 87if empty ( a: lines ) 88return 89endif 90let lines = a: lines 91let linebuf = self . _line_buffer 92if lines [ 0 ] != "\n" && ! empty ( linebuf ) 93" continue the previous line 94let linebuf [ -1 ] .= lines [ 0 ] 95call remove ( lines , 0 ) 96endif 97" append the newly received lines to the line buffer 98let linebuf += lines 99" keep trying to match handlers while the line isnt empty 100while ! empty ( linebuf ) 101let match_idx = self . parse ( linebuf ) 102if match_idx == -1 103break 104endif 105let linebuf = linebuf [ match_idx + 1 : ] 106endwhile 107" shift excess lines from the buffer 108while len ( linebuf ) > self . LINE_BUFFER_MAX_LEN 109call remove ( linebuf , 0 ) 110endwhile 111let self . _line_buffer = linebuf 112endfunction 113 114 115" Try to match a list of lines with the current state and call the handler if 116" the match succeeds. Return the index in `lines` of the first match. 117function s: Parser . parse ( lines ) 118let lines = a: lines 119if empty ( lines ) 120return -1 121endif 122let state = self . state () 123" search for a match using the list of patterns 124for [ pattern , handler ] in state . _patterns 125let matches = matchlist ( lines , pattern ) 126if empty ( matches ) 127continue 128endif 129let match_idx = match ( lines , pattern ) 130call call ( state [ handler ], matches [ 1 :], self . _target ) 131return match_idx 132endfor 133endfunction 134 135 136" Return the current state 137function s: Parser . state () 138return self . _stack [ -1 ] 139endfunction 140 141 142" Job handler that simply forwards lines to the parser. 143function ! s: JobOutput ( _id , lines , _event ) dict 144call self . _parser . feed ( a: lines ) 145endfunction 146 147function vimexpect#Parser ( initial_state , target ) 148return s: Parser . create ( a: initial_state , a: target ) 149endfunction 150 151 152function vimexpect#State ( patterns ) 153return s: State . create ( a: patterns ) 154endfunction