nv-lex-kw

nv-lex-kw =============== - nv-lex-kw - to match keywords char-by-char ,used in char-stream-parser

Usage no npm install needed!

<script type="module">
  import nvLexKw from 'https://cdn.skypack.dev/nv-lex-kw';
</script>

README

nv-lex-kw

  • nv-lex-kw
  • to match keywords char-by-char ,used in char-stream-parser

install

  • npm install nv-lex-kw

usage

const {KeyWords} = require("nv-lex-kw");

example

char-by-char

longest match

/*
> KeyWords.TYPE
{ longest_match: 0, non_longest_match: 1 }
>
> KeyWords.POLICY
{ longest_match: 0, fst_match: 1 }
>
*/

//new KeyWords(sarr,policy=KeyWords.POLICY.longest_match,esc="\\",forest_size=10000)

var keywords = new KeyWords(['abc','ab','defg'])

/*
> keywords
d
de
def
defg <longest match>
a
ab <match>
abc <longest match>
>
*/


var rslt = keywords.next('a')
/*
> keywords.branches_
a
ab <match>
abc <longest match>
>


> rslt.possible
true
>
*/

var rslt = keywords.next('b')
/*
> rslt.is_pending()
true
>

> keywords.branches_
ab <match>
abc <longest match>
>


*/

var rslt = keywords.next('c')

/*
> rslt.is_longest_match()
true
>

*/

match as early as soon

var keywords = new KeyWords(['abc','ab','defg'],KeyWords.POLICY.fst_match)
var rslt = keywords.next('a')
var rslt = keywords.next('b')

/*
> rslt.is_non_longest_match()
true
>

*/
var keywords = new KeyWords(
   ['abc','ab','defg'],
   KeyWords.POLICY.fst_match,
   KeyWords.DFLT_ESC
)
var rslt = keywords.next('\\')
/*
> keywords.esced_
true
>
*/
var rslt = keywords.next('a')
/*
> rslt.is_impossible()
false
>
> keywords
d
de
def
defg <longest match>
a
ab <match>
abc <longest match>
>
*/
var rslt = keywords.next('b')
/*
> rslt.is_longest_match()
false
> rslt.is_non_longest_match()
false
>
*/

ignore escape

var keywords = new KeyWords(
   ['abc','ab','defg'],
   KeyWords.POLICY.fst_match,
   KeyWords.DFLT_ESC
)
var rslt = keywords.next('\\')
var rslt = keywords.next('a')
var rslt = keywords.next('a')
var rslt = keywords.next('b')
/*
> rslt.is_non_longest_match()
true
>
*/

whole match

longest

var keywords = new KeyWords(
   ['abc','ab','defg'],
   KeyWords.POLICY.longest_match
)

var rslt = keywords.match('abc')

/*
> rslt.is_longest_match()
true
>
*/

var rslt = keywords.match('ab')
/*
> rslt.is_outof_policy()
true

> rslt.reason_
{ policy: 'longest_match', type: 'non_longest_match' }
>


*/


var rslt = keywords.match('def')

/*
> rslt
Rslt {
  match: false,
  possible: true,
  impossible: false,
  type: undefined,
  policy: 0,
  pending: undefined,
  branches: [ 'defg' ]
}
>

*/

var rslt = keywords.match('xyz')

/*
> rslt.reason_
{ impossible: true }
>

*/

match as early as soon

var keywords = new KeyWords(
    ['abc','ab','defg'],
    KeyWords.POLICY.fst_match
)

var rslt = keywords.match('ab')

/*

> rslt.is_non_longest_match()
true
>


> rslt
Rslt {
  match: true,
  possible: false,
  impossible: false,
  type: 1,
  policy: 1,
  pending: undefined
}
>
*/

var keywords = new KeyWords(
   ['abc','ab','defg'],
   KeyWords.POLICY.fst_match,
   KeyWords.DFLT_ESC
)

rm match

> var keywords = new KeyWords(['abc','a','defg'])
undefined
> keywords
d
de
def
defg <longest match>
a <match>
ab
abc <longest match>
>
> keywords.rm_match(3)
undefined
> keywords
d
de
def
defg <longest match>
a <longest match>
>


> var keywords = new KeyWords(['abc','a','defg'])
undefined
>
> keywords
d
de
def
defg <longest match>
a <match>
ab
abc <longest match>
>
> keywords.rm_match()
undefined
>
> keywords
a <match>
ab
abc <longest match>
>

LICENSE

  • ISC