用python和javascript编写的高度可配置的基于优先级的解析器

pre-js-p的Python项目详细描述


Build Status

这是什么?

prejspy是一个高度可配置的基于优先级的解析器,用python和javascript编写。 默认语法基于javascript的一个子集,但可以适应许多不同的场景。

>>>fromPreJsPyimportPreJsPy>>>parser=PreJsPy()>>>parser.parse("6 * 9 == 42"){'type':'BinaryExpression','operator':'==','right':{'type':'Literal','raw':'42','value':42.0},'left':{'type':'BinaryExpression','operator':'*','right':{'type':'Literal','raw':'9','value':9.0},'left':{'type':'Literal','raw':'6','value':6.0}}}
>varPreJsPy=require('pre-js-py').PreJsPy;>parser=newPreJsPy()>parser.parse('6 * 9 == 42'){type:'BinaryExpression',operator:'==',left:{type:'BinaryExpression',operator:'*',left:{type:'Literal',value:6,raw:'6'},right:{type:'Literal',value:9,raw:'9'}},right:{type:'Literal',value:42,raw:'42'}}

这个库的javascript版本改编自javascript库jsep,它是(c)2013 stephen oney,http://jsep.from.so/,并根据mit许可证的条款发布。代码已经移植到python中,并且添加了许多实用函数。

语法功能

  • 符号值
    • 常数
      • 例如`true``false``null`
      • 可配置:可添加任意常数
    • 标识符
      • 可能包含$、\u或字母数字字符,但不能以数字开头
      • 可以包括属性名和访问器,例如foobar.bazfoo['bar'].baz
  • 各种文字类型
    • 数字文本(`42.001`
      • 十进制表示法(`-42`
      • 指数表示法(`6.7E-10`
    • 字符串文本(`"Hello world"`
      • 可以是双引号或单引号
      • 特殊字符可以转义
    • 数组文本(`[1, 2, 3]`
      • 必须用方括号括起来
      • 可以嵌套
  • 多种类型的运算符
    • 一元运算符
      • 默认情况下`-``!``~``+`
      • 可以添加自定义运算符,现有的运算符可以删除
    • 二进制运算符
      • 基于优先级
      • 默认情况下包含所有javascript运算符
      • 可以使用自定义先例添加自定义先例
      • 括号可用于替代先例
    • javascript条件运算符
      • 单三元运算符`a ? b : c`
  • 调用表达式
    • 必须使用圆括号
    • 例如`Math.cos(x)`

API

javascript和python中的api几乎相同。唯一的区别来自语言本身的特点。

varparser=newPreJsPy();// creates a new parser
parser.getConstants()// returns an object containing constants
parser.setConstants(d)// sets the current constants know to the parser
parser.getUnaryOperators()// gets an array of unary operators
parser.setUnaryOperators(ary)// sets an array of unary operators
parser.getBinaryOperators()// returns an object mapping binary operators to their precedence
parser.setBinaryOperators(ary)// sets the binary operators to precedence mapping
parser.parse(s)// parses a string into a AST
parser=PreJsPy()# creates a new parserparser.getConstants()# returns an object containing constantsparser.setConstants(d)# sets the current constants know to the parserparser.getUnaryOperators()# gets an array of unary operatorsparser.setUnaryOperators(ary)# sets an array of unary operatorsparser.getBinaryOperators()# returns an object mapping binary operators to their precedenceparser.setBinaryOperators(ary)# sets the binary operators to precedence mappingparser.parse(s)# parses a string into a AST

安装

此包发布在 Python Package Index 安装只需通过pip:

pip install pre_js_py

它也发表在 Nodejs Package Manager 只需通过NPM即可完成安装:

npm install pre-js-py

许可证+确认

本模块及相关文档版权所有(C)Tom Wiesing 2016 并根据麻省理工学院的许可证获得许可,详情请参见license

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
SpringBoot如何在Java中创建固定长度的平面文件?   java是freemarker类型。果心无法解决此问题。它是从required间接引用的。类文件   设置字符串格式时发生java FormatFlagsTransversionMismatchException异常   无法将java应用程序连接到sql server 2008 R2   在关闭事件之前,使用Java API BMC补救方法更新“分类”   java如何在使用opencsv写入csv时保持空值   java在spring中是否需要事务属性?   java Hibernate抑制信息消息   java如何在spring data cosmos的@Container中给出布尔值?   java获取旋转矩形的角   如何在java中获得对象的两个数组列表的差异   java与新接口相比,嵌套模板的优点和缺点是什么?   Java Apache Derby十进制列数据为0.001输出为0.0   用于隐藏包含特定单词的元素的javascript用户脚本   java Jackson objectMapper无法读取UTF8   java在JavaFX中创建太多事件处理程序   如何在运行Redhat Linux的amazon ec2微实例上卸载openjdk   如何使用Java swing创建一个动态列表,其中必须为每个元素指定多个参数   java如何在将面板放置在gridlayout上时消除面板之间的间隙?   java标准方法名称,为什么println中的l不是大写?