Skip to content

Commit

Permalink
Added setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Kronuz committed Jul 15, 2017
1 parent edfa88f commit cc445ea
Show file tree
Hide file tree
Showing 23 changed files with 239 additions and 25 deletions.
1 change: 1 addition & 0 deletions README
6 changes: 5 additions & 1 deletion __init__.py → esprima/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

from __future__ import absolute_import

version = '4.0.0-dev'
__version__ = (4, 0, 0)

from .esprima import parse, parseModule, parseScript, tokenize, Error, Syntax # NOQA
from .objects import toDict # NOQA
from .esprima import parse, parseModule, parseScript, tokenize # NOQA
from .error_handler import Error # NOQA
from .syntax import Syntax # NOQA
8 changes: 3 additions & 5 deletions __main__.py → esprima/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@

import sys

from .compat import unicode
from .esprima import parse, tokenize, Error
from . import __version__
from . import version


def main(argv):
def main():
import json
import time
import optparse

usage = "usage: %prog [options] [file.js]"
version = ".".join(unicode(n) for n in __version__)
parser = optparse.OptionParser(usage=usage, version=version)
parser.add_option("--comment", dest="comment",
action="store_true", default=False,
Expand Down Expand Up @@ -100,5 +98,5 @@ def main(argv):


if __name__ == '__main__':
retval = main(sys.argv)
retval = main()
sys.exit(retval)
6 changes: 3 additions & 3 deletions character.py → esprima/character.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
import unicodedata
from collections import defaultdict

from .compat import unichr, xrange
from .compat import uchr, xrange

# http://stackoverflow.com/questions/14245893/efficiently-list-all-characters-in-a-given-unicode-category
U_CATEGORIES = defaultdict(list)
for c in map(unichr, xrange(sys.maxunicode + 1)):
for c in map(uchr, xrange(sys.maxunicode + 1)):
U_CATEGORIES[unicodedata.category(c)].append(c)
UNICODE_LETTER = set(
U_CATEGORIES['Lu'] + U_CATEGORIES['Ll'] +
Expand Down Expand Up @@ -86,7 +86,7 @@
class Character:
@staticmethod
def fromCodePoint(code):
return unichr(code)
return uchr(code)

# https://tc39.github.io/ecma262/#sec-white-space

Expand Down
176 changes: 176 additions & 0 deletions esprima/comment_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# -*- coding: utf-8 -*-
# Copyright JS Foundation and other contributors, https://js.foundation/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, self.list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, self.list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# self.SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES
# LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# self.SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from __future__ import absolute_import, unicode_literals

from .objects import Object
from .scanner import SourceLocation
from .syntax import Syntax


class Comment(Object):
type
value
range?
loc?


class Entry(Object):
comment
start


class NodeInfo(Object):
node: any
start


class CommentHandler {
attach
comments
stack
leading
trailing

constructor(:
self.attach = false
self.comments = []
self.stack = []
self.leading = []
self.trailing = []

insertInnerComments(node, metadata:
# innnerComments for properties empty block
# `function a(:/** comments **\/}`
if node.type == Syntax.BlockStatement && node.body.length == 0:
innerComments = []
for i = self.leading.length - 1 i >= 0 --i:
entry = self.leading[i]
if metadata.end.offset >= entry.start:
innerComments.unshift(entry.comment)
self.leading.splice(i, 1)
self.trailing.splice(i, 1)
if innerComments.length:
node.innerComments = innerComments

findTrailingComments(metadata:
trailingComments = []

if self.trailing.length > 0:
for i = self.trailing.length - 1 i >= 0 --i:
entry = self.trailing[i]
if entry.start >= metadata.end.offset:
trailingComments.unshift(entry.comment)
self.trailing.length = 0
return trailingComments

entry = self.stack[self.stack.length - 1]
if entry && entry.node.trailingComments:
firstComment = entry.node.trailingComments[0]
if firstComment && firstComment.range[0] >= metadata.end.offset:
trailingComments = entry.node.trailingComments
delete entry.node.trailingComments
return trailingComments

findLeadingComments(metadata:
leadingComments = []

target
while self.stack.length > 0:
entry = self.stack[self.stack.length - 1]
if entry && entry.start >= metadata.start.offset:
target = entry.node
self.stack.pop()
else:
break

if target:
count = target.leadingComments ? target.leadingComments.length : 0
for (i = count - 1 i >= 0 --i:
comment = target.leadingComments[i]
if comment.range[1] <= metadata.start.offset:
leadingComments.unshift(comment)
target.leadingComments.splice(i, 1)
if target.leadingComments && target.leadingComments.length == 0:
delete target.leadingComments
return leadingComments

for (i = self.leading.length - 1 i >= 0 --i:
entry = self.leading[i]
for entry in self.leading:
if entry.start <= metadata.start.offset:
leadingComments.unshift(entry.comment)
self.leading.splice(i, 1)
return leadingComments

visitNode(node, metadata:
if node.type == Syntax.Program && node.body.length > 0:
return

self.insertInnerComments(node, metadata)
trailingComments = self.findTrailingComments(metadata)
leadingComments = self.findLeadingComments(metadata)
if leadingComments.length > 0:
node.leadingComments = leadingComments
if trailingComments.length > 0:
node.trailingComments = trailingComments

self.stack.push({
node: node,
start: metadata.start.offset
})

visitComment(node, metadata:
type = 'Line' if node.type[0] == 'L' else 'Block'
comment = {
type: type,
value: node.value
}
if node.range:
comment.range = node.range
if node.loc:
comment.loc = node.loc
self.comments.push(comment)

if self.attach:
entry: Entry = {
comment: {
type: type,
value: node.value,
range: [metadata.start.offset, metadata.end.offset]
},
start: metadata.start.offset
}
if node.loc:
entry.comment.loc = node.loc
node.type = type
self.leading.push(entry)
self.trailing.push(entry)

visit(node, metadata:
if node.type == 'LineComment':
self.visitComment(node, metadata)
elif node.type == 'BlockComment':
self.visitComment(node, metadata)
elif self.attach:
self.visitNode(node, metadata)
12 changes: 6 additions & 6 deletions compat.py → esprima/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
long = int
xrange = range
unicode = str
unichr = chr
uchr = chr

def uniord(ch):
def uord(ch):
return ord(ch[0])

else:
Expand All @@ -47,22 +47,22 @@ def uniord(ch):
try:
# Python 2 UCS4:
unichr(0x10000)
unichr = unichr
uchr = unichr

def uniord(ch):
def uord(ch):
return ord(ch[0])

except ValueError:
# Python 2 UCS2:
def unichr(code):
def uchr(code):
# UTF-16 Encoding
if code <= 0xFFFF:
return unichr(code)
cu1 = ((code - 0x10000) >> 10) + 0xD800
cu2 = ((code - 0x10000) & 1023) + 0xDC00
return unichr(cu1) + unichr(cu2)

def uniord(ch):
def uord(ch):
cp = ord(ch[0])
if cp >= 0xD800 and cp <= 0xDBFF:
second = ord(ch[1])
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions jsx_parser.py → esprima/jsx_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from __future__ import absolute_import, unicode_literals

from .compat import unichr
from .compat import uchr
from .character import Character
from . import jsx_nodes as JSXNode
from .jsx_syntax import JSXSyntax
Expand Down Expand Up @@ -151,9 +151,9 @@ def scanXHTMLEntity(self, quote):
# e.g. '&#x41;' becomes just '#x41'
st = result[1:-1]
if numeric and len(st) > 1:
result = unichr(int(st[1:], 10))
result = uchr(int(st[1:], 10))
elif hex and len(st) > 2:
result = unichr(int(st[2:], 16))
result = uchr(int(st[2:], 16))
elif not numeric and not hex and st in XHTMLEntities:
result = XHTMLEntities[st]

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions scanner.py → esprima/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import re

from .objects import Object
from .compat import xrange, unicode, unichr, uniord, PY3
from .compat import xrange, unicode, uchr, uord
from .character import Character, HEX_CONV, OCTAL_CONV
from .messages import Messages
from .token import Token
Expand Down Expand Up @@ -370,7 +370,7 @@ def isKeyword(self, id):
))

def codePointAt(self, i):
return uniord(self.source[i:i + 2])
return uord(self.source[i:i + 2])

def scanHexEscape(self, prefix):
length = 4 if prefix == 'u' else 2
Expand All @@ -384,7 +384,7 @@ def scanHexEscape(self, prefix):
else:
return None

return unichr(code)
return uchr(code)

def scanUnicodeCodePointEscape(self):
ch = self.source[self.index]
Expand Down Expand Up @@ -857,7 +857,7 @@ def scanStringLiteral(self):
octToDec = self.octalToDecimal(ch)

octal = octToDec.octal or octal
str += unichr(octToDec.code)
str += uchr(octToDec.code)
else:
str += ch

Expand Down Expand Up @@ -1023,7 +1023,7 @@ def astralSub(m):
if codePoint > 0x10FFFF:
self.throwUnexpectedToken(Messages.InvalidRegExp)
if codePoint <= 0xFFFF:
return unichr(codePoint)
return uchr(codePoint)
return astralSubstitute
tmp = re.sub(r'\\u\{([0-9a-fA-F]+)\}|\\u([a-fA-F0-9]{4})', astralSub, tmp)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
try:
from setuptools import setup
except ImportError:
from distutils.core import setup

import os

from esprima import version


def read(fname):
try:
return open(os.path.join(os.path.dirname(__file__), fname)).read().strip()
except IOError:
return ''


setup(
name='esprima',
version=version,
author='German M. Bravo (Kronuz)',
author_email='[email protected]',
packages=[
'esprima',
],
url='http://github.com/Kronuz/exprima-python',
license='BSD',
keywords='esprima ecmascript javascript parser',
description="ECMAScript parsing infrastructure for multipurpose analysis in Python",
long_description=read('README.md'),
entry_points={
'console_scripts': [
'esprima = esprima.__main__:main',
]
},
)
3 changes: 1 addition & 2 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import fnmatch
import unittest

from ..objects import toDict
from ..esprima import parse, tokenize, Error
from esprima import parse, tokenize, Error, toDict

BASE_DIR = os.path.dirname(__file__)

Expand Down

0 comments on commit cc445ea

Please sign in to comment.