summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Token.h
diff options
context:
space:
mode:
authormattco98 <matthewcolsson@gmail.com>2020-05-03 15:41:14 -0700
committerAndreas Kling <kling@serenityos.org>2020-05-04 16:46:31 +0200
commitadb4accab3668e60a6998c4b492d1cf7be11f9d1 (patch)
tree14dc0ee4b84d5cb7b9b4b6697d6481633c5f8a39 /Libraries/LibJS/Token.h
parent2fdeb464f739b5aada58d1910334336c8169da5b (diff)
downloadserenity-adb4accab3668e60a6998c4b492d1cf7be11f9d1.zip
LibJS: Add template literals
Adds fully functioning template literals. Because template literals contain expressions, most of the work has to be done in the Lexer rather than the Parser. And because of the complexity of template literals (expressions, nesting, escapes, etc), the Lexer needs to have some template-related state. When entering a new template literal, a TemplateLiteralStart token is emitted. When inside a literal, all text will be parsed up until a '${' or '`' (or EOF, but that's a syntax error) is seen, and then a TemplateLiteralExprStart token is emitted. At this point, the Lexer proceeds as normal, however it keeps track of the number of opening and closing curly braces it has seen in order to determine the close of the expression. Once it finds a matching curly brace for the '${', a TemplateLiteralExprEnd token is emitted and the state is updated accordingly. When the Lexer is inside of a template literal, but not an expression, and sees a '`', this must be the closing grave: a TemplateLiteralEnd token is emitted. The state required to correctly parse template strings consists of a vector (for nesting) of two pieces of information: whether or not we are in a template expression (as opposed to a template string); and the count of the number of unmatched open curly braces we have seen (only applicable if the Lexer is currently in a template expression). TODO: Add support for template literal newlines in the JS REPL (this will cause a syntax error currently): > `foo > bar` 'foo bar'
Diffstat (limited to 'Libraries/LibJS/Token.h')
-rw-r--r--Libraries/LibJS/Token.h7
1 files changed, 6 insertions, 1 deletions
diff --git a/Libraries/LibJS/Token.h b/Libraries/LibJS/Token.h
index e272eff804..b3242703d8 100644
--- a/Libraries/LibJS/Token.h
+++ b/Libraries/LibJS/Token.h
@@ -112,7 +112,11 @@ namespace JS {
__ENUMERATE_JS_TOKEN(SlashEquals) \
__ENUMERATE_JS_TOKEN(StringLiteral) \
__ENUMERATE_JS_TOKEN(Switch) \
- __ENUMERATE_JS_TOKEN(TemplateLiteral) \
+ __ENUMERATE_JS_TOKEN(TemplateLiteralEnd) \
+ __ENUMERATE_JS_TOKEN(TemplateLiteralExprEnd) \
+ __ENUMERATE_JS_TOKEN(TemplateLiteralExprStart) \
+ __ENUMERATE_JS_TOKEN(TemplateLiteralStart) \
+ __ENUMERATE_JS_TOKEN(TemplateLiteralString) \
__ENUMERATE_JS_TOKEN(This) \
__ENUMERATE_JS_TOKEN(Throw) \
__ENUMERATE_JS_TOKEN(Tilde) \
@@ -122,6 +126,7 @@ namespace JS {
__ENUMERATE_JS_TOKEN(UnsignedShiftRight) \
__ENUMERATE_JS_TOKEN(UnsignedShiftRightEquals) \
__ENUMERATE_JS_TOKEN(UnterminatedStringLiteral) \
+ __ENUMERATE_JS_TOKEN(UnterminatedTemplateLiteral) \
__ENUMERATE_JS_TOKEN(Var) \
__ENUMERATE_JS_TOKEN(Void) \
__ENUMERATE_JS_TOKEN(While) \