Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import and refactor some of convertor functions #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions esprima/character.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,15 @@
))
LINE_TERMINATOR = set(('\x0A', '\x0D', '\u2028', '\u2029'))

DECIMAL_CONV = dict((c, n) for n, c in enumerate('0123456789'))
OCTAL_CONV = dict((c, n) for n, c in enumerate('01234567'))
HEX_CONV = dict((c, n) for n, c in enumerate('0123456789abcdef'))
for n, c in enumerate('ABCDEF', 10):
HEX_CONV[c] = n
DECIMAL_DIGIT = set(DECIMAL_CONV.keys())
OCTAL_DIGIT = set(OCTAL_CONV.keys())
OCTAL_DIGIT = set('01234567')
DECIMAL_DIGIT = OCTAL_DIGIT | {"8", "9"}
HEX_DIGIT = set(HEX_CONV.keys())

del U_CATEGORIES, UNICODE_LETTER, UNICODE_COMBINING_MARK
del UNICODE_DIGIT, UNICODE_CONNECTOR_PUNCTUATION
del DECIMAL_CONV, OCTAL_CONV, HEX_CONV

class Character:
@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion esprima/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def parsePrimaryExpression(self):
self.scanner.index = self.startMarker.index
token = self.nextRegexToken()
raw = self.getTokenRaw(token)
expr = self.finalize(node, Node.RegexLiteral(token.regex, raw, token.pattern, token.flags))
expr = self.finalize(node, Node.RegexLiteral("/" + token.pattern + "/" + token.flags, raw, token.pattern, token.flags))
else:
expr = self.throwUnexpectedToken(self.nextToken())

Expand Down
21 changes: 12 additions & 9 deletions esprima/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@

from .objects import Object
from .compat import xrange, unicode, uchr, uord
from .character import Character, HEX_CONV, OCTAL_CONV
from .character import Character, HEX_CONV
from .messages import Messages
from .token import Token


def hexValue(ch):
def hexDigitValue(ch):
return HEX_CONV[ch]


def octalValue(ch):
return OCTAL_CONV[ch]
_zeroDigitCode = ord("0")


def octalDigitValue(ch):
return ord(ch) - _zeroDigitCode


class RegExp(Object):
Expand Down Expand Up @@ -382,7 +385,7 @@ def scanHexEscape(self, prefix):
if not self.eof() and Character.isHexDigit(self.source[self.index]):
ch = self.source[self.index]
self.index += 1
code = code * 16 + hexValue(ch)
code = code * 16 + hexDigitValue(ch)
else:
return None

Expand All @@ -402,7 +405,7 @@ def scanUnicodeCodePointEscape(self):
if not Character.isHexDigit(ch):
break

code = code * 16 + hexValue(ch)
code = code * 16 + hexDigitValue(ch)

if code > 0x10FFFF or ch != '}':
self.throwUnexpectedToken()
Expand Down Expand Up @@ -484,17 +487,17 @@ def getComplexIdentifier(self):
def octalToDecimal(self, ch):
# \0 is not octal escape sequence
octal = ch != '0'
code = octalValue(ch)
code = octalDigitValue(ch)

if not self.eof() and Character.isOctalDigit(self.source[self.index]):
octal = True
code = code * 8 + octalValue(self.source[self.index])
code = code * 8 + octalDigitValue(self.source[self.index])
self.index += 1

# 3 digits are only allowed when string starts
# with 0, 1, 2, 3
if ch in '0123' and not self.eof() and Character.isOctalDigit(self.source[self.index]):
code = code * 8 + octalValue(self.source[self.index])
code = code * 8 + octalDigitValue(self.source[self.index])
self.index += 1

return Octal(octal, code)
Expand Down