Fix dict/map literal expression parsing
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 35/100
Research direction
Start by reading the dict/map literal parsing in jinja2cpp/src/expression_parser.cpp, then trace token definitions in jinja2cpp/src/lexer.h and their display names in jinja2cpp/src/template_parser.h. Reproduce the two examples from the issue and verify that colon-separated literals match the Jinja/Python syntax while equals-separated literals no longer do.
Written by the indexing model from the issue text.
Description
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("!=") },
- Dominant language
- C++
- Stars
- 602
- Forks
- 115
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from jinja2cpp/Jinja2Cpp
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 4/5 3-5 days Newbie friendliness 65/100
-
Difficulty 3/5 1-2 days Newbie friendliness 74/100
-
Difficulty 3/5 1-2 days Newbie friendliness 70/100
All issues in jinja2cpp/Jinja2Cpp
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
mpfaffenberger/privateer_reimagined#658 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
microsoft/onnxruntime#33018 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
AXERA-TECH/ax-llm#81 ·
-
enhancement
Difficulty 2/5 Half a day Newbie friendliness 78/100
ros-industrial/ros2_canopen#448 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Maintainers usually reply within 1 day