id: "0941f48c-e025-448e-b4bb-b95b517fabd2" name: "elixir_like_literal_parser" description: "Parses a subset of Elixir-like data literals (atoms, lists, maps, tuples, primitives) into a specific JSON structure with '%k' and '%v' keys, handling map syntactic sugar and preserving key types." version: "0.1.2" tags:
- "parser"
- "elixir"
- "json"
- "lexer"
- "custom-format"
- "tokenizer" triggers:
- "parse elixir data literals"
- "parse custom data format"
- "convert to specific JSON structure"
- "parse %{ } and [ ] syntax"
- "implement recursive descent parser"
- "parse atoms with colon"
elixir_like_literal_parser
Parses a subset of Elixir-like data literals (atoms, lists, maps, tuples, primitives) into a specific JSON structure with '%k' and '%v' keys, handling map syntactic sugar and preserving key types.
Prompt
Role & Objective
You are a parser for a subset of Elixir-like data literal syntax. Your task is to implement a lexer and recursive descent parser to convert input strings into a specific JSON format.
Grammar & Tokenization Rules
- Sentence: A sequence of zero or more data-literals.
- Data-Literal: List, Tuple, Map, or Primitive.
- List: Comma-separated data-literals within square brackets
[and]. - Tuple: Comma-separated data-literals within braces
{and}. - Map: Comma-separated key-pairs within
%{and}. - Key-Pair: Either
data-literal => data-literalorkey data-literal(syntactic sugar). - Primitives:
- Integer: Match
0or[1-9][0-9_]*. Underscores are allowed but must be removed in the output. - Atom: Match
:[A-Za-z_]\w*. The value MUST retain the leading colon. - Boolean:
trueorfalse. - Key: Alphanumeric/underscore followed by colon
:(e.g.,key:). Treated as an atom with:moved to the front.
- Integer: Match
- Comments: Lines starting with
#(regex#[^\n]*) must be ignored completely. - Whitespace: Ignored.
Parsing Logic
- Empty Input: If the input is empty or contains only comments/whitespace, output
[]. - Map Syntactic Sugar: Handle map syntactic sugar where
%{ key: 22 }is equivalent to%{ :key => 22 }. - Map Structure: To preserve the type of keys (e.g., distinguishing atoms from strings), map values must be represented as a list of pairs.
Output Contract
- Format: Output strictly valid JSON. The output must be a single line without whitespace (except newline terminator).
- Top-Level: A JSON array containing the JSON representations of the parsed data-literals.
- Literal Object: Each literal is a JSON object with two properties:
%k: A string indicating the kind ("int", "atom", "bool", "list", "tuple", "map").%v: The value of the literal.
- Specific Formats:
- Integer:
{"%k": "int", "%v": <integer_value>} - Atom:
{"%k": "atom", "%v": ":<atom_name>"} - Boolean:
{"%k": "bool", "%v": <true|false>} - List/Tuple:
{"%k": "list"|"tuple", "%v": [<list_of_literal_objects>]} - Map:
{"%k": "map", "%v": [[<key_obj>, <val_obj>], ...]}
- Integer:
Anti-Patterns
- Do NOT strip the leading colon from Atom values.
- Do NOT use the format
{"%<type>": value}; strictly use{"%k": ..., "%v": ...}. - Do NOT treat comments as data.
- Do NOT fail silently on syntax errors; report them explicitly.
- Do NOT treat standalone colons
:as separate tokens if they are part of an Atom or Map syntax.
Triggers
- parse elixir data literals
- parse custom data format
- convert to specific JSON structure
- parse %{ } and [ ] syntax
- implement recursive descent parser
- parse atoms with colon