Skip to content

Commit c7a848d

Browse files
committed
Provide actual template literal lexical components
- Mark the generated tokens with the appropriate types.
1 parent 72e1245 commit c7a848d

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

src/calmjs/parse/lexers/es2015.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33
ES2015 (ECMAScript 6th Edition/ES6) lexer.
44
"""
55

6+
import re
67
import ply
78

89
from calmjs.parse.lexers.es5 import Lexer as ES5Lexer
910

11+
template_token_types = (
12+
(re.compile(r'`.*`', re.S),
13+
'TEMPLATE_NOSUB'),
14+
(re.compile(r'`.*\${', re.S),
15+
'TEMPLATE_HEAD'),
16+
(re.compile(r'}.*\${', re.S),
17+
'TEMPLATE_MIDDLE'),
18+
(re.compile(r'}.*`', re.S),
19+
'TEMPLATE_TAIL'),
20+
)
21+
1022

1123
class Lexer(ES5Lexer):
1224
"""
@@ -25,7 +37,7 @@ class Lexer(ES5Lexer):
2537
'ARROW', 'SPREAD', # => ...
2638

2739
# ES2015 terminal types
28-
'TEMPLATE',
40+
'TEMPLATE_NOSUB', 'TEMPLATE_HEAD', 'TEMPLATE_MIDDLE', 'TEMPLATE_TAIL',
2941
)
3042

3143
template = r"""
@@ -42,8 +54,8 @@ class Lexer(ES5Lexer):
4254
""" # `
4355

4456
@ply.lex.TOKEN(template)
45-
def t_TEMPLATE(self, token):
46-
# remove escape + new line sequence used for strings
47-
# written across multiple lines of code
48-
token.value = token.value.replace('\\\n', '')
57+
def t_TEMPLATE_RAW(self, token):
58+
for patt, token_type in template_token_types:
59+
if patt.match(token.value):
60+
token.type = token_type
4961
return token

src/calmjs/parse/tests/lexer.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -464,56 +464,62 @@
464464
('const c',
465465
['CONST const', 'ID c']),
466466
), (
467-
'arrow_punctuator',
468-
('=>',
469-
['ARROW =>']),
467+
'punctuators',
468+
('=> ...',
469+
['ARROW =>', 'SPREAD ...']),
470470
), (
471471
'arrow_functions',
472472
('const c = (name) => { return name; }',
473473
['CONST const', 'ID c', 'EQ =', 'LPAREN (', 'ID name', 'RPAREN )',
474474
'ARROW =>', 'LBRACE {', 'RETURN return', 'ID name', 'SEMI ;',
475475
'RBRACE }']),
476+
), (
477+
'spread',
478+
('[...spring, ...summer]',
479+
['LBRACKET [', 'SPREAD ...', 'ID spring', 'COMMA ,', 'SPREAD ...',
480+
'ID summer', 'RBRACKET ]']),
476481
), (
477482
'template_literal',
478483
('`foo`',
479-
['TEMPLATE `foo`']),
484+
['TEMPLATE_NOSUB `foo`']),
480485
), (
481486
'template_multiline',
482487
('`foo\nbar\r\nfoo`',
483-
['TEMPLATE `foo\nbar\r\nfoo`']),
488+
['TEMPLATE_NOSUB `foo\nbar\r\nfoo`']),
484489
), (
485490
'template_other_newlines',
486491
('`foo\u2028\u2029foo`',
487-
['TEMPLATE `foo\u2028\u2029foo`']),
492+
['TEMPLATE_NOSUB `foo\u2028\u2029foo`']),
488493
), (
489494
'template_literal_with_dollar',
490495
('`foo$`',
491-
['TEMPLATE `foo$`']),
496+
['TEMPLATE_NOSUB `foo$`']),
492497
), (
493498
'template_head_tail',
494499
(r'`hello ${name} while this`',
495-
['TEMPLATE `hello ${', 'ID name', 'TEMPLATE } while this`']),
500+
['TEMPLATE_HEAD `hello ${', 'ID name', 'TEMPLATE_TAIL } while this`']),
496501
), (
497502
'template_empty_head_tail',
498503
(r'`${name}`',
499-
['TEMPLATE `${', 'ID name', 'TEMPLATE }`']),
504+
['TEMPLATE_HEAD `${', 'ID name', 'TEMPLATE_TAIL }`']),
500505
), (
501506
'template_nested',
502507
(r'`${`${a * 2}`} ${b}`',
503-
['TEMPLATE `${', 'TEMPLATE `${', 'ID a', 'MULT *', 'NUMBER 2',
504-
'TEMPLATE }`', 'TEMPLATE } ${', 'ID b', 'TEMPLATE }`']),
508+
['TEMPLATE_HEAD `${', 'TEMPLATE_HEAD `${', 'ID a', 'MULT *',
509+
'NUMBER 2', 'TEMPLATE_TAIL }`', 'TEMPLATE_MIDDLE } ${', 'ID b',
510+
'TEMPLATE_TAIL }`']),
505511
), (
506512
'template_some_keywords',
507513
(r'`this -> ${this}.`',
508-
['TEMPLATE `this -> ${', 'THIS this', 'TEMPLATE }.`']),
514+
['TEMPLATE_HEAD `this -> ${', 'THIS this', 'TEMPLATE_TAIL }.`']),
509515
), (
510516
'template_literal_escape',
511517
(r'`f\`o`',
512-
[r'TEMPLATE `f\`o`']),
518+
[r'TEMPLATE_NOSUB `f\`o`']),
513519
), (
514520
'template_literal_assignment',
515521
('s = `hello world`',
516-
['ID s', 'EQ =', 'TEMPLATE `hello world`']),
522+
['ID s', 'EQ =', 'TEMPLATE_NOSUB `hello world`']),
517523
)
518524
]
519525

src/calmjs/parse/tests/test_es2015_lexer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ def test_initial_template_character(self):
6565
)
6666

6767
LexerES5TestCase = build_equality_testcase(
68-
'LexerTestCase', partial(run_lexer, lexer_cls=Lexer), (
68+
'LexerES5TestCase', partial(run_lexer, lexer_cls=Lexer), (
6969
(label, data[0], data[1],) for label, data in es5_cases))
7070

7171
LexerES5PosTestCase = build_equality_testcase(
72-
'LexerPosTestCase', partial(
72+
'LexerES5PosTestCase', partial(
7373
run_lexer_pos, lexer_cls=Lexer), es5_pos_cases)
7474

7575
LexerES2015TestCase = build_equality_testcase(

0 commit comments

Comments
 (0)