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 /Libraries/LibGUI | |
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 'Libraries/LibGUI')
-rw-r--r-- | Libraries/LibGUI/JSSyntaxHighlighter.cpp | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/Libraries/LibGUI/JSSyntaxHighlighter.cpp b/Libraries/LibGUI/JSSyntaxHighlighter.cpp index 1659fd4263..08769b2a56 100644 --- a/Libraries/LibGUI/JSSyntaxHighlighter.cpp +++ b/Libraries/LibGUI/JSSyntaxHighlighter.cpp @@ -42,7 +42,9 @@ static TextStyle style_for_token_type(Gfx::Palette palette, JS::TokenType type) case JS::TokenType::NumericLiteral: return { palette.syntax_number() }; 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: return { palette.syntax_string() }; @@ -55,6 +57,8 @@ static TextStyle style_for_token_type(Gfx::Palette palette, JS::TokenType type) case JS::TokenType::ParenClose: case JS::TokenType::ParenOpen: case JS::TokenType::Semicolon: + case JS::TokenType::TemplateLiteralExprStart: + case JS::TokenType::TemplateLiteralExprEnd: return { palette.syntax_punctuation() }; case JS::TokenType::Ampersand: case JS::TokenType::AmpersandEquals: |