Repository navigation
Closed
Description
Error:
File "/home/chrobinson.com/pierdavpa/.local/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 126, in feed_token
action, arg = states[state][token.type]
~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: '__ANON_3'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/home/chrobinson.com/pierdavpa/.local/lib/python3.11/site-packages/hcl2/api.py", line 14, in load
return loads(file.read(), with_meta=with_meta)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/chrobinson.com/pierdavpa/.local/lib/python3.11/site-packages/hcl2/api.py", line 27, in loads
tree = hcl2.parse(text + "\n")
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/chrobinson.com/pierdavpa/.local/lib/python3.11/site-packages/lark/lark.py", line 652, in parse
return self.parser.parse(text, start=start, on_error=on_error)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/chrobinson.com/pierdavpa/.local/lib/python3.11/site-packages/lark/parser_frontends.py", line 101, in parse
return self.parser.parse(stream, chosen_start, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/chrobinson.com/pierdavpa/.local/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 41, in parse
return self.parser.parse(lexer, start)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/chrobinson.com/pierdavpa/.local/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 171, in parse
return self.parse_from_state(parser_state)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/chrobinson.com/pierdavpa/.local/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 193, in parse_from_state
raise e
File "/home/chrobinson.com/pierdavpa/.local/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 184, in parse_from_state
state.feed_token(token)
File "/home/chrobinson.com/pierdavpa/.local/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 129, in feed_token
raise UnexpectedToken(token, expected, state=self, interactive_parser=None)
lark.exceptions.UnexpectedToken: Unexpected token Token('__ANON_3', 'secondary_read_write_connection_string_id') at line 70, column 5.
Expected one of:
* STAR
* COMMA
* PERCENT
* MINUS
* __ANON_1
* SLASH
* __ANON_8
* RBRACE
* __ANON_5
* __ANON_2
* __ANON_4
* __ANON_9
* __ANON_7
* LESSTHAN
* PLUS
* MORETHAN
* __ANON_6
* QMARK
* __ANON_0
When parsing a file that has a conditional assignment in it. key = value == null ? "" : someothervalue
Any idea how I might fix or work around this? Or if a library fix is needed (or can even address this?)
Reacted by purajit, Hamish Durkin, Oliver, Yevhenii Terentiev, Jeff and Trevor Wood
Metadata
Metadata
Assignees
Labels
No labels
Activity
purajit commentedon Feb 16, 2024
I have this issue too, it seems like it's expecting parentheses.
kkozik-amplify commentedon Mar 7, 2024
Hello, thanks for the report
I'm not able to reproduce. Does the bug still occur? If so, could you provide some piece of config that's causing it?
grahamhar commentedon Mar 13, 2024
Hi @kkozik-amplify ,
I just hit the same and have managed to update the test case to show the failure (see this branch). I have been unable to find a solution so far as I am not that familiar with lark.
kkozik-amplify commentedon Mar 13, 2024
@grahamhar Hi, thanks for the test case.
It looks like the issue is caused by incorrect definition in the grammar file - we will look into this.
As a workaround, wrapping conditional expression into parentheses should help:
operator = (local.assertion == 1 ? local.yes : local.no)grahamhar commentedon Mar 13, 2024
It does appear that the files parse OK when we downgrade to
4.3.0I have been doing a little more testing and we have some conditionals that have embedded conditionals too.
operator = local.assertion == 1 ? local.some_logic == 2 ? local.yes : "custom" : local.noAwlie commentedon Apr 10, 2024
Hi there,
we also had this issue.
We currently downgraded to
4.3.0. Any higher version (4.3.1, 4.3.2, 4.3.3) didn't workWe could only reproduce this error this way in our unit test:
and (basically the same)
which would fail with
lark.exceptions.UnexpectedToken: Unexpected token Token('__ANON_3', 'nested_after_conditional_object')other conditionals wouldn't cause issues.
The workaround with parentheses also worked, but is not a viable option for us
These didn't fail:
almost the same as above, but without the attribute after the conditional (
nested_after_conditional_object)some other conditionals we tested (we also tested the failing one with parentheses, which passes)
(this module syntax is of course not correct, but it doesn't matter for the parser)
This is pretty much the test-case that grahamhar added.
ariker commentedon Apr 29, 2024
Here is another file where this happens:
https://github.com/Azure/terraform-azurerm-naming/blob/0.4.1/main.tf
kadigergo commentedon May 22, 2024
here is also an example:
parsing fails only if it is nested and we have two with the ? : syntax
RPitt commentedon Jul 5, 2024
This seemingly is related to / the same root cause as
#163
#140
In short parsing of expressions seems to be problematic in 4.3.1+
fix parsing of objects elements;
fix parsing of objects elements; (#197)
kkozik-amplify commentedon Mar 27, 2025
Fixed in version 7.0.0
ian-nicholls2-NHSD commentedon Oct 17, 2025
I've also just come across this with latest version - it was quite a complex bit of terraform - but it works with terraform and fails in tflocal - its where we need to add a tag to disable AWS Backup to an S3 bucket thats automatically provisioned by the AWS Admins so need to import the bucket and JUST add the new tag - ofc the data object doesn't give you the tags so needed to get those via aws cli :( then ignore all the global tags that Key and Value are the same and only set the ones where its diff + the new tag.
I had to use
{ "${tag.Key}" = tag.Value }which is a linting no-no so need an exception for terraform_deprecated_interpolationIn
terraformonly{ tostring(tag.Key) = tag.Value }works andtflintis happy buttflocalgivesBelow works but needs a tflint disablement to pass the checks:
tflocal fails but terraform works: