Package: pre-js-py
What is this?PreJsPy is highly configurable a precedence-based parser written in both Python and JavaScript.
The default grammar is based on a subset of JavaScript but can be adapted to a lot of different scenarios.>>>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'}}The JavaScript version of this library has been adapted from the JavaScript library JSEP which is (c) 2013 Stephen Oney,http://jsep.from.so/and has been published under the terms of the MIT license. The code has been ported to Python and a lot of utility functions have been added.Grammar FeaturesSymbolic ValuesConstantssuch as`true`,`false`and`null`configurable: Arbitrary constants can be addedIdentifiersmay contain  $,_ or alphanumeric chacraters but may not start with a numbercan include property names and accessors, for examplefoo,bar.baz,foo[‘bar’].bazVarious types of literalsNumeric Literals (`42.001`)decimal notation (`-42`)exponential notation (`6.7E-10`)String Literals (`"Hello world"`)can be either double or single quotesspecial characters can be escapedArray Literals (`[1, 2, 3]`)must be surrounded by square bracketscan be nestedMultiple types of operatorsUnary operatorsby default`-`,`!`,`~`and`+`custom operators can be added, existing ones can be removedBinary operatorsprecedence basedby default contains all JavaScript operatorscustom ones can be added with custom precedencesbrackets can be used to override precedencesJavaScript Conditional operatorsingle ternary operator`a ? b : c`Call Expressionsmust use round bracketsfor example`Math.cos(x)`APIThe API in JavaScript and python are almost identical. The only differences come from the features of the languages themselves.varparser=newPreJsPy();// 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 ASTparser=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 ASTInstallThis package is published on thePython Package IndexInstallation can be done simply via pip:pipinstallpre_js_pyIt is also published onNodejs Package ManagerInstallation can be done simply via npm:npminstallpre-js-pyLicense + AcknowledgementsThis module and associated documentation is Copyright (c) Tom Wiesing 2016
and licensed under the MIT license, seelicensefor details.