import { OctaveLogger } from './OctaveLogger' ; import { StackFrame , Source } from 'vscode-debugadapter' ; import { CommandInterface } from './Commands' ; export class StackFramesManager { private _frame : number = 0 ; public constructor ( ) { } public clear ( ) { this . _frame = 0 ; } public selectStackFrame ( i , runtime , callback ) { if ( i === this . _frame ) { callback ( ) ; } else if ( i < this . _frame ) { this . down ( this . _frame - i , runtime , callback ) ; } else { this . up ( i - this . _frame , runtime , callback ) ; } } private up ( n , runtime , callback ) { if ( n > 0 ) { runtime . evaluate ( `dbup ${ n }` , ( output ) => { callback ( ) ; } ) ; } else { OctaveLogger . error ( `Error: up(${ n })!` ) ; } } private down ( n , runtime , callback ) { if ( n > 0 ) { runtime . evaluate ( `dbdown ${ n }` , ( output ) => { callback ( ) ; } ) ; } else { OctaveLogger . error ( `Error: down(${ n })!` ) ; } } private static readonly STACK_REGEX = /^\s*(?:-->)?\s*(\w+)(?:>(\w+))*? at line (\d+) \[(.*)\]$/ ; public get ( startFrame , endFrame , runtime , callback ) { const stackframes = new Array < StackFrame > ( ) ; runtime . evaluate ( 'dbstack;' , ( output ) => { output . forEach ( line => { const match = line . match ( StackFramesManager . STACK_REGEX ) ; if ( match !== null && match . length > 1 ) { const functionName = match [ match . length - 3 ] ; const name = ( functionName !== undefined ? functionName : match [ 1 ] ) ; const id = stackframes . length ; const source = new Source ( name , match [ match . length - 1 ] ) ; const lineNumber = parseInt ( match [ match . length - 2 ] ) ; const frame = new StackFrame ( id , name , source , lineNumber ) ; stackframes . push ( frame ) ; } } ) ; callback ( stackframes ) ; } ) ; } }