nv-data-tree-csp-jconvert

nv-data-tree-csp-jconvert ================= - nv-data-tree-csp-jconvert - convert serveral speical json-format(used in nvlang) to a tree

Usage no npm install needed!

<script type="module">
  import nvDataTreeCspJconvert from 'https://cdn.skypack.dev/nv-data-tree-csp-jconvert';
</script>

README

nv-data-tree-csp-jconvert

  • nv-data-tree-csp-jconvert
  • convert serveral speical json-format(used in nvlang) to a tree

install

  • npm install nv-data-tree-csp-jconvert

usage

sexpr

  • shape S = [tag:String,attrib:Object,child:Array]

const {
    dfs_eng,
    dfs_sexpr,
}  = require("nv-data-tree-csp-jconvert")


//type S [tag:String,attrib:Object,child:Array<S>]

var S0 = [
    'html',{}, [
        [
            'head',{}, [
                 ['meta', {name:'viewport'},[]],
                 ['meta', {},[]]
            ],
        ],
        [
            'body',{}, [
                ['div',{id:"top_nav"},[]],
                ['div',{},[]]
            ]
        ]
    ]
]

var [rt,forest] = dfs_sexpr.tree_lize(S0)

> dfs_sexpr.show(rt)
html
    head
        meta
            -name viewport
        meta
    body
        div
            -id top_nav
        div


> console.dir(dfs_sexpr.jsonize(rt),{depth:null})
[
  'html',
  {},
  [
    [
      'head',
      {},
      [ [ 'meta', { name: 'viewport' }, [] ], [ 'meta', {}, [] ] ]
    ],
    [
      'body',
      {},
      [ [ 'div', { id: 'top_nav' }, [] ], [ 'div', {}, [] ] ]
    ]
  ]
]

tac

  • T@optional : String, if-skipped, it will be a index
  • A@optional : Object
  • C@optional : Array<...S>
  • S : [T?,A?,C?]

valid-ptrn

 [T],[A],[C],
 [T,A],[T,C],[A,C],
 [T,A,C]

const {
    wfs_eng,
    wfs_tac,
}  = require("nv-data-tree-csp-jconvert")


 var TAC0 = [
      'html',[
           'head',[
                 'meta',{charset:"utf8"},
           ],
           'body',{id:"app"},[
                'div',['',{_text:'xxxx'}],
                'br',
                'div',['',{_text:'xxxx'}],
                'br'
           ]
      ]
 ]


 var [rt,forest] = wfs_tac.tree_lize(TAC0);
 wfs_tac.show(rt)
 >
 @root@
    html
        head
            meta
                -charset utf8
        body
            -id app
            div

                    -_text xxxx
            br
            div

                    -_text xxxx
            br

 console.dir(wfs_tac.jsonize(rt),{depth:null});
  
    [
      'html',
      [
        'head',
        [ 'meta', { charset: 'utf8' } ],
        'body',
        { id: 'app' },
        [
          'div',
          [ '', { _text: 'xxxx' } ],
          'br',
          'div',
          [ '', { _text: 'xxxx' } ],
          'br'
        ]
      ]
    ]


     var TAC1 = [
          'html',[
               'head',[
                     'meta',{charset:"utf8"},
               ],
               'body',{id:"app"},[
                    'div',[{_text:'xxxx'}],
                    'br',
                    'div',[{_text:'xxxx'}],
                    'br'
               ]
          ]
     ]
     
     var [rt,forest] = wfs_tac.tree_lize(TAC1);
     console.dir(wfs_tac.jsonize(rt),{depth:null});
     [
       'html',
       [
         'head',
         [ 'meta', { charset: 'utf8' } ],
         'body',
         { id: 'app' },
         [
           'div',
           [ 0, { _text: 'xxxx' } ],
           'br',
           'div',
           [ 0, { _text: 'xxxx' } ],
           'br'
         ]
       ]
     ]

missing T/A/C

    > var [rt,forest] = wfs_tac.tree_lize([{a:100},{b:200}])
    > wfs_tac.show(rt)
    @root@
        0
            -a 100
        1
            -b 200

    > console.dir(wfs_tac.jsonize(rt),{depth:null});
    [ { a: 100 }, { b: 200 } ]


    > var [rt,forest] = wfs_tac.tree_lize(['t0','t1',{a:100},{b:200}])

    > wfs_tac.show(rt)
    @root@
        t0
        t1
            -a 100
        2
            -b 200

    > console.dir(wfs_tac.jsonize(rt),{depth:null});
    [ 't0', 't1', { a: 100 }, { b: 200 } ]


    > var [rt,forest] = wfs_tac.tree_lize(['t0','t1',{a:100},'t2',{b:200}])
    undefined
    > wfs_tac.show(rt)
    @root@
        t0
        t1
            -a 100
        t2
            -b 200
    > console.dir(wfs_tac.jsonize(rt),{depth:null});
    [ 't0', 't1', { a: 100 }, 't2', { b: 200 } ]



    > var [rt,forest] = wfs_tac.tree_lize([[1,2],{a:100}])

    > wfs_tac.show(rt)
    @root@
        0
            1
            2
        1
            -a 100

    > console.dir(wfs_tac.jsonize(rt),{depth:null});
    [ [ 1, 2 ], { a: 100 } ]

cnest

  • E: {[k?]:any,ckey@custom-defined:Array }

wfs

const {
    wfs_eng,
    wfs_tac,
}  = require("nv-data-tree-csp-jconvert")

 var CNEST = 
    {
       tag:'html',
       children: [
          {
              tag:'head',
              children: [
                  { tag:'meta',attribs:{charset:"utf8"}}
              ]
          },
          {
              tag:'body',
              attribs:{id:"app"},
              children: [
                  { tag:'div',children:[{tag:'',attribs:{_text:'xxxx'}}]},
                  { tag:'br'},
                  { tag:'div',children:[{tag:'',attribs:{_text:'xxxx'}}]},
                  { tag:'br'},
              ]
          },
       ]
   }


var [rt,forest] = wfs_cnest.tree_lize(CNEST)
wfs_cnest.show(rt)
>
html
    head
        meta
            -charset utf8
    body
        -id app
        div

                -_text xxxx
        br
        div

                -_text xxxx
        br

 
console.dir(wfs_cnest.jsonize(rt),{depth:null})
{
  tag: 'html',
  children: [
    {
      tag: 'head',
      children: [ { tag: 'meta', attribs: { charset: 'utf8' } } ]
    },
    {
      tag: 'body',
      attribs: { id: 'app' },
      children: [
        {
          tag: 'div',
          children: [ { tag: '', attribs: { _text: 'xxxx' } } ]
        },
        { tag: 'br' },
        {
          tag: 'div',
          children: [ { tag: '', attribs: { _text: 'xxxx' } } ]
        },
        { tag: 'br' }
      ]
    }
  ]
}

dfs

const {
    dfs_cnest,
}  = require("nv-data-tree-csp-jconvert")



 var CNEST = 
    {
       tag:'html',
       children: [
          {
              tag:'head',
              children: [
                  { tag:'meta',attribs:{charset:"utf8"}}
              ]
          },
          {
              tag:'body',
              attribs:{id:"app"},
              children: [
                  { tag:'div',children:[{tag:'',attribs:{_text:'xxxx'}}]},
                  { tag:'br'},
                  { tag:'div',children:[{tag:'',attribs:{_text:'yyyy'}}]},
                  { tag:'br'},
              ]
          },
       ]
   }


var [rt,forest] = dfs_cnest.tree_lize(CNEST)
dfs_cnest.show(rt)
html
    head
        meta
            -charset utf8
    body
        -id app
        div

                -_text xxxx
        br
        div

                -_text xxxx
        br

    console.dir(dfs_cnest.jsonize(rt),{depth:null})
    {
      children: [
        {
          tag: 'html',
          children: [
            {
              tag: 'head',
              children: [
                { tag: 'meta', attribs: { charset: 'utf8' }, children: [] }
              ]
            },
            {
              tag: 'body',
              attribs: { id: 'app' },
              children: [
                {
                  tag: 'div',
                  children: [ { tag: '', attribs: { _text: 'xxxx' }, children: [] } ]
                },
                { tag: 'br', children: [] },
                {
                  tag: 'div',
                  children: [ { tag: '', attribs: { _text: 'yyyy' }, children: [] } ]
                },
                { tag: 'br', children: [] }
              ]
            }
          ]
        }
      ]
    }

array

  • O = {[N:String]:Object}
  • e = [op,Array<String|O>?]
  • E = [op,Array<E|e>]

const {
    dfs_ary
}  = require("nv-data-tree-csp-jconvert"); 


    var ary = [
       {'%program':{target:'js'}},
       ['%let','a'],                             //执行时 %let 也有返回值 {'a':{type:'notation'}}
       ['%let',{'b':{type:'%$i32'}}],
       ['%let','c'],
       ['%+','5','3'],                     //存入当前节点 可以被 %sdfs_prev% 获取
       ['%asgn','a','%sdfs_prev%'],
       ['%-',['%+','5','3']/*存入当前节点*/,['%-','5','3']/*存入当前节点*/] //存入当前节点,
       ['%def','fname',
           ['%params','A','B','C'],             //%params 会在 当前ctx.%params 上 添加 一组 notation
           ['%add','A','B','C'],
           ['%asgn','rslt','%sdfs_prev%'],
           ['%ret%','%rslt%']
       ],
       ['%call','fname',['%args','a','b','c']]
    ]

    var [rt,forest] = dfs_ary.tree_lize(ary)
    dfs_ary.show(rt)

    %program
        -target js
        %let
            a
        %let
            b
                -type %$i32
        %let
            c
        %+
            5
            3
        %asgn
            a
            %sdfs_prev%
        %-
            %+
                5
                3
            %-
                5
                3
        %def
            fname
            %params
                A
                B
                C
            %add
                A
                B
                C
            %asgn
                rslt
                %sdfs_prev%
            %ret%
                %rslt%
        %call
            fname
            %args
                a
                b
                c


    > dfs_ary.jsonize(rt)
    [
      { '%program': { target: 'js' } },
      [ '%let', 'a' ],
      [ '%let', { b: [Object] } ],
      [ '%let', 'c' ],
      [ '%+', '5', '3' ],
      [ '%asgn', 'a', '%sdfs_prev%' ],
      [ '%-', [ '%+', '5', '3' ], [ '%-', '5', '3' ] ],
      [
        '%def',
        'fname',
        [ '%params', 'A', 'B', 'C' ],
        [ '%add', 'A', 'B', 'C' ],
        [ '%asgn', 'rslt', '%sdfs_prev%' ],
        [ '%ret%', '%rslt%' ]
      ],
      [ '%call', 'fname', [ '%args', 'a', 'b', 'c' ] ]
    ]
    >

LICENSE

  • ISC