Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix dict/map literal expression parsing #262

Open
tjsmith-meta opened this issue Sep 23, 2024 · 0 comments
Open

Fix dict/map literal expression parsing #262

tjsmith-meta opened this issue Sep 23, 2024 · 0 comments

Comments

@tjsmith-meta
Copy link

Looks like jinja2cpp just doesn't match the spec here on parsing dict/map literals.
https://jinja.palletsprojects.com/en/2.10.x/templates/#literals

Here's what works in Python (but doesn't work with jinja2cpp).

{% set foo = {"bar":"baz"} %}

Here's what currently works in jinja2cpp (but doesn't work with Python).

{% set foo = {"bar"="baz"} %}

Here's the local code changes I made to make jinja2cpp behave the same as Python and match the docs/spec.

diff --git a/jinja2cpp/src/expression_parser.cpp b/jinja2cpp/src/expression_parser.cpp
--- a/jinja2cpp/src/expression_parser.cpp
+++ b/jinja2cpp/src/expression_parser.cpp
@@ -395,11 +395,11 @@
         if (key != Token::String)
             return MakeParseError(ErrorCode::ExpectedStringLiteral, key);

-        if (!lexer.EatIfEqual('='))
+        if (!lexer.EatIfEqual(':'))
         {
             auto tok = lexer.PeekNextToken();
             auto tok1 = tok;
-            tok1.type = Token::Assign;
+            tok1.type = Token::Colon;
             return MakeParseError(ErrorCode::ExpectedToken, tok, {tok1});
         }

diff --git a/jinja2cpp/src/lexer.h b/jinja2cpp/src/lexer.h
--- a/jinja2cpp/src/lexer.h
+++ b/jinja2cpp/src/lexer.h
@@ -37,6 +37,7 @@
         RCrlBracket = '}',
         Assign = '=',
         Comma = ',',
+        Colon = ':',
         Eof = 256,

         // General
diff --git a/jinja2cpp/src/template_parser.h b/jinja2cpp/src/template_parser.h
--- a/jinja2cpp/src/template_parser.h
+++ b/jinja2cpp/src/template_parser.h
@@ -1017,6 +1017,7 @@
     { Token::RCrlBracket, UNIVERSAL_STR("}") },
     { Token::Assign, UNIVERSAL_STR("=") },
     { Token::Comma, UNIVERSAL_STR(",") },
+    { Token::Colon, UNIVERSAL_STR(":") },
     { Token::Eof, UNIVERSAL_STR("<<End of block>>") },
     { Token::Equal, UNIVERSAL_STR("==") },
     { Token::NotEqual, UNIVERSAL_STR("!=") },
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant