UNPKG

v-regexp

Version:

JavaScript Regular Expression Parser and Visualizer.

73 lines (65 loc) 2.24 kB
export default NFA; /** A Naive NFA Implementation Start state is always named 'start' a type NFAConfig = {compact:false,accepts:StateSet,trans:[Transition]} type State = String type StateSet = [State] type Tranisition = {from:StateSet,to:StateSet,charset:Charset,action:Action,assert:Assert} type Charset = String|[Range] Charset is similar to regex charset,supports negation and range but metacharacters Examples: includes: 'abc0-9','[^]' excludes: '^c-z0-9','^a^' //excluded 'a' and '^' two chars any char: '\0-\uffff' Or set charset to processed disjoint ranges:['ac','d','eh'] Set `charset` to empty string to enable empty move(ε-moves). Action: Function(stack:Array,c:String,i:Int,state:String,inputs:String):Array stack: storage stack c: current char i: current index state: current state inputs: whole input string Optional return new stack Only eMove transition allow `assert` Actions and Asserts of eMove transition always execute before non-eMove transitions on current path. Assert: Function(stack:Array,c:String,i:Int,state:String,inputs:String):Boolean Return True if assertion just success,if fail return false If success and need skip num chars, return the Int count to increase `i`,this feature is designed for backref. Stack modifications in action only allow shift,unshift and return new stack. NFAConfig example used to recognize numbers:{ compact:false,accepts:'start'. trans:[{from:'start',to:'start',charset:'0-9'}] } CompactNFAConfig example,see `structure` function. An automaton used to recognize triples:{ compact:true,accepts:'start', trans:[ ['start>start','0369'],['start>q1','147'],['start>q2','258'], ['q1>q1','0369'],['q1>q2','147'],['q1>start','258'], ['q2>q2','0369'],['q2>q1','258'],['q2>start','147'], ] }; */ declare function NFA(a: any): { accepts: {}; router: {}; input: typeof input; assertDFA: typeof assertDFA; accept: typeof accept; }; /** return { stack:Array, acceptable:Boolean, lastIndex:Int, lastState:String } */ declare function input(s: any, startIndex: any, _debug: any): any; declare function assertDFA(): boolean; declare function accept(state: any): any;