diff options
author | mattco98 <matthewcolsson@gmail.com> | 2020-05-03 15:41:14 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-04 16:46:31 +0200 |
commit | adb4accab3668e60a6998c4b492d1cf7be11f9d1 (patch) | |
tree | 14dc0ee4b84d5cb7b9b4b6697d6481633c5f8a39 /Userland | |
parent | 2fdeb464f739b5aada58d1910334336c8169da5b (diff) | |
download | serenity-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 'Userland')
-rw-r--r-- | Userland/js.cpp | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/Userland/js.cpp b/Userland/js.cpp index f3048bdd55..38e0fb1623 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -500,7 +500,9 @@ int main(int argc, char** argv) stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Magenta) }); break; case JS::TokenType::StringLiteral: - case JS::TokenType::TemplateLiteral: + case JS::TokenType::TemplateLiteralStart: + case JS::TokenType::TemplateLiteralEnd: + case JS::TokenType::TemplateLiteralString: case JS::TokenType::RegexLiteral: case JS::TokenType::UnterminatedStringLiteral: stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Green), Line::Style::Bold }); @@ -571,6 +573,8 @@ int main(int argc, char** argv) case JS::TokenType::Interface: case JS::TokenType::Let: case JS::TokenType::New: + case JS::TokenType::TemplateLiteralExprStart: + case JS::TokenType::TemplateLiteralExprEnd: case JS::TokenType::Throw: case JS::TokenType::Typeof: case JS::TokenType::Var: |