-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
ES2015 (ECMAScript 6th Edition/ES6) lexer. | ||
""" | ||
|
||
import re | ||
import ply | ||
|
||
from calmjs.parse.lexers.es5 import Lexer as ES5Lexer | ||
|
||
template_token_types = ( | ||
(re.compile(r'`.*`', re.S), | ||
'TEMPLATE_NOSUB'), | ||
(re.compile(r'`.*\${', re.S), | ||
'TEMPLATE_HEAD'), | ||
(re.compile(r'}.*\${', re.S), | ||
'TEMPLATE_MIDDLE'), | ||
(re.compile(r'}.*`', re.S), | ||
'TEMPLATE_TAIL'), | ||
) | ||
|
||
|
||
class Lexer(ES5Lexer): | ||
""" | ||
ES2015 lexer. | ||
""" | ||
|
||
# Punctuators (ES6) | ||
# t_DOLLAR_LBRACE = r'${' | ||
# this is also a right brace punctuator... | ||
# t_RBRACE = r'}' | ||
t_ARROW = r'=>' | ||
t_SPREAD = r'\.\.\.' | ||
|
||
tokens = ES5Lexer.tokens + ( | ||
# ES2015 punctuators | ||
'ARROW', 'SPREAD', # => ... | ||
|
||
# ES2015 terminal types | ||
'TEMPLATE_NOSUB', 'TEMPLATE_HEAD', 'TEMPLATE_MIDDLE', 'TEMPLATE_TAIL', | ||
) | ||
|
||
template = r""" | ||
(?:(?:`|}) # opening ` or } | ||
(?: [^`\\] # no \, or ` | ||
| \\[a-zA-Z!-\/:-@\[-`{-~] # or escaped characters | ||
| \\(\n|\r(?!\n)|\u2028|\u2029|\r\n) # or line continuation | ||
| \\x[0-9a-fA-F]{2} # or hex_escape_sequence | ||
| \\u[0-9a-fA-F]{4} # or unicode_escape_sequence | ||
| \\(?:[1-7][0-7]{0,2}|[0-7]{2,3}) # or octal_escape_sequence | ||
| \\0 # or <NUL> (15.10.2.11) | ||
)*? # zero or many times | ||
(?:`|\${)) # closing ` or ${ | ||
""" # ` | ||
|
||
@ply.lex.TOKEN(template) | ||
def t_TEMPLATE_RAW(self, token): | ||
for patt, token_type in template_token_types: | ||
if patt.match(token.value): | ||
token.type = token_type | ||
return token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
############################################################################### | ||
# encoding: utf-8 | ||
# | ||
# Copyright (c) 2011 Ruslan Spivak | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
# | ||
############################################################################### | ||
|
||
__author__ = 'Ruslan Spivak <ruslan.spivak@gmail.com>' | ||
|
||
import unittest | ||
from functools import partial | ||
|
||
from calmjs.parse.lexers.es2015 import Lexer | ||
from calmjs.parse.exceptions import ECMASyntaxError | ||
|
||
from calmjs.parse.testing.util import build_equality_testcase | ||
from calmjs.parse.tests.lexer import ( | ||
run_lexer, | ||
run_lexer_pos, | ||
es5_cases, | ||
es5_pos_cases, | ||
es2015_cases, | ||
es2015_pos_cases, | ||
) | ||
|
||
|
||
class LexerFailureTestCase(unittest.TestCase): | ||
|
||
def test_initial_template_character(self): | ||
lexer = Lexer() | ||
lexer.input('`') | ||
with self.assertRaises(ECMASyntaxError) as e: | ||
[token for token in lexer] | ||
self.assertEqual(str(e.exception), "Illegal character '`' at 1:1") | ||
|
||
|
||
LexerKeywordTestCase = build_equality_testcase( | ||
'LexerTestCase', partial(run_lexer, lexer_cls=Lexer), ( | ||
(label, data[0], data[1],) for label, data in [( | ||
# Keywords | ||
# ('break case ...', ['BREAK break', 'CASE case', ...]) | ||
'keywords_all', | ||
(' '.join(kw.lower() for kw in Lexer.keywords), | ||
['%s %s' % (kw, kw.lower()) for kw in Lexer.keywords] | ||
), | ||
)] | ||
) | ||
) | ||
|
||
LexerES5TestCase = build_equality_testcase( | ||
'LexerES5TestCase', partial(run_lexer, lexer_cls=Lexer), ( | ||
(label, data[0], data[1],) for label, data in es5_cases)) | ||
|
||
LexerES5PosTestCase = build_equality_testcase( | ||
'LexerES5PosTestCase', partial( | ||
run_lexer_pos, lexer_cls=Lexer), es5_pos_cases) | ||
|
||
LexerES2015TestCase = build_equality_testcase( | ||
'LexerES2015TestCase', partial(run_lexer, lexer_cls=Lexer), ( | ||
(label, data[0], data[1],) for label, data in es2015_cases)) | ||
|
||
LexerES2015PosTestCase = build_equality_testcase( | ||
'LexerES2015PosTestCase', partial( | ||
run_lexer_pos, lexer_cls=Lexer), es2015_pos_cases) |