Giter Site home page Giter Site logo

naive_str_parser's Introduction

简单的js模版引擎

1.全局安装jison

npm npm install jison -g 

2.编写jison文件,写法类似lex/yacc语法,可参见github的例子,然后生成js文件

jison three.jison //得到three.js 

3.在入口文件里面引入parser并使用

require("jison")
let pas = require("./three.js").parser

pas.yy = { //可以通过给yy属性赋值来注入环境
  a: 1,
  b: 2,
  aasd: "123",
  fu: (aa) => {
    return aa * 2
  }
}
let a = "simple_add=>{{1+3+4}}ternery=>{{1>0?\"true\":\"false\"}}func_call=>{{fu(b)}}"
k = /(?<=\{\{)(.*?)(?=\}\})/g
console.log(a.replace(k, (str) => { return pas.parse(str) })
//simple_add=>{{8}}ternery=>{{true}}func_call=>{{4}}

支持的表达式种类

  1. 数字,字符串字面量
  2. 简单的四则运算,判断(+-*/><)
  3. 三元表达式,可以使用()进行嵌套
  4. 变量名引用
  5. 函数调用引用

naive_str_parser's People

Contributors

jiuniangyz avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.