Git Product home page Git Product logo

pynarcissus's Introduction

PyNarcissus

PyNarcissus is a Javascript parser, and not a very Pythonic one at that. Furthermore, documentation is poor.

Currently this is just a Javascript parser. Javascript evaluation might be a little more tricky since the Narcissus interpreter implements a bunch of stuff metacircularly.

Also, check out the original Narcissus: http://mxr.mozilla.org/mozilla/source/js/narcissus/

This project is interesting not only for the fact that a Javascript parser has been written in Python, but because the port was as direct a transliteration as possible, the Javascript Narcissus parser and this Python one make an interesting case study in the differences between Javascript and Python.

Perhaps I'll post a few interesting line by line comparisons, but in the interim time, jsparser.py is equivalent in function (and indeed incredibly similar at the source level) to Narcissus' jsdefs.js and jsparse.js. You'll have to blame Brendan Eich if you're unhappy with the overall structure and design.

There is also a conversion routine that turns a Javascript parse tree into rudimentary S-expressions.

New: Check out pyjon!

As of 2009-02-19, PyNarcissus has been engineered such that

str(parse("javascript code"))

in Python is identical to

parse("javascript code").toString()

in Javascript. It was this feature that allowed for robust testing of the parser in the absence of a Javascript evaluator, as the output of the Python version was compared to the output of the Javascript version over all of the Spidermonkey Javascript tests.

There are a few differences in output, as Python handles both floating point precision and non-ascii characters slightly differently.

It is expected that future versions of PyNarcissus will be more Pythonic in many ways, and will break this string-conversion compatibility with the Javascript Narcissus.

pynarcissus's People

Contributors

carlsmith avatar jtolio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pynarcissus's Issues

Tons of missing constants

flake8 generates 429 undefined names when linting this repo... How do all of those constants actually get generated?

flake8 testing of https://github.com/jtolds/pynarcissus on Python 2.7.13

$ flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics

./jsparser.py:207:49: F821 undefined name 'END'
    done = property(lambda self: self.peek() == END)
                                                ^

./jsparser.py:223:22: F821 undefined name 'NEWLINE'
                tt = NEWLINE
                     ^
__...__

request for output to JSON

it would be a great help to have the option for the parser to output to JSON

i have tried a few different methods to achieve this but all seem to output invalid JSON

i am at a loss at this point, and i only thought to put up an issue because i saw you added the python3 port, which i was previously doing myself, some two weeks ago

by requesting directly i was hoping that the effort would be a superficial addition for the creator and maintainer

if you think it would be too much work, i'd be happy to do it myself if you could offer a suggestion of how you would tackle the problem

thanks,

here are my attempts:

  • i took the return value for parse() and created a function that descends through the class structure returning values based on their type: 'str', 'int', 'bool', 'NoneType', 'list', 'dict', 'main.Node', 'main.Tokenizer', 'main.Object'; but the returned object is missing some properties in the classes, ie 'type', while retaining others like 'type_', also tokenizer always contains the same values
  • i took the output of the str function that the program prints to stdout, removed the clear and copy functions and the tokenizer: [object Object], then tried to manually add in double quotes where necessary to make the output a valid JSON object.. a few problems here, first off ignoring the tokenizer object seems like i am missing out on vital information, and the other problem was that sometimes there is "value" : "{" and sometimes there is "value" : { .. }, after completing the work the JSON was invalid
  • assuming the issue lied in the "value" : { .. } issue i resolved to add a new function identical the the str function but instead of printing just %s values, it would print "%s" where necessary.. now, i could differentiate between "value": "{" and "value" : "{ .. }" but i would have to go through and manually remove the quotes around objects.. after doing so the JSON was invalid

nested ternary operators are parsed incorrectly

What steps will reproduce the problem?
Run:
    echo a ? b ? c : d : e | python sexp.py

What is the expected output? What do you see instead?

It should be equivalent to
    echo a ? (b ? c : d) : e | python sexp.py

, i.e.:

    (SCRIPT
      (TERNARY a (TERNARY b c d) e))

however, instead I get:

    (SCRIPT
      (TERNARY a b (TERNARY c d e)))

which is the same as

    echo (a ? b : c) ? d : e | python sexp.py




Original issue reported on code.google.com by [email protected] on 24 Jan 2014 at 9:29

Support non-ascii characters

First of all I really appreciate all the work you've done so far for this port.

The problem: When passing a string containing non-ascii characters the
tokenizer throws a `UnicodeEncodeError` at line 197.

I solved that with removing the `str`-cast in this line but perhaps you
want to do an instance check for `basestring` here.

BTW: It would be great if you could manage to implement the `jsexec` part
somehow... :)

Cheers

Sebastian

Original issue reported on code.google.com by [email protected] on 13 Dec 2009 at 1:42

Fix parsing of comma expression in sexp.py

What steps will reproduce the problem?
1. Parse a comma expression with more than 2 sub-expression

What is the expected output? What do you see instead?

OtherError, 2 out of 3 subnodes checked.

What version of the product are you using? On what operating system?

r17, Windows with Python 2.7

Please provide any additional information below.

Original issue reported on code.google.com by [email protected] on 29 Dec 2013 at 6:09

Attachments:

nested functions cause failure

nested functions cause script failure on line 1058 of jsparser3.py :
while (operators and opPrecedence.get(operators[-1].type_) > opPrecedence[NEW]):

easily repeatable, here's the js file i created to get the error message below :

//fakejs.js
function isnumber(numb) {
return !isNaN(parseFloat(numb) && isFinite(numb)
}

error message :

./jsparser3.py ../fakejs.js
Traceback (most recent call last):
File "./jsparser3.py", line 1146, in
print(str(parse(open(sys.argv[1]).read(),sys.argv[1])))
File "./jsparser3.py", line 1140, in parse
n = Script(t, x)
File "./jsparser3.py", line 363, in Script
n = Statements(t, x)
File "./jsparser3.py", line 474, in Statements
n.append(Statement(t, x))
File "./jsparser3.py", line 498, in Statement
return FunctionDefinition(t, x, True, type_)
File "./jsparser3.py", line 757, in FunctionDefinition
f.body = Script(t, x2)
File "./jsparser3.py", line 363, in Script
n = Statements(t, x)
File "./jsparser3.py", line 474, in Statements
n.append(Statement(t, x))
File "./jsparser3.py", line 682, in Statement
n.value = Expression(t, x)
File "./jsparser3.py", line 1058, in Expression
opPrecedence[NEW]):
TypeError: unorderable types: NoneType() > int()

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.