Python lexer in rust with indentation logic
Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/english_gpt4_8/python-lexer-in-rust-with-indentation-logic
Implement a simple Python lexer in Rust that correctly handles indentation and dedentation tokens, specifically ensuring multiple dedent tokens are emitted when indentation drops multiple levels.From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill python-lexer-in-rust-with-indentation-logicAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
2.7 KB, 523 tokens by cl100k_base, as published. Nobody here has run it
Python Lexer in Rust with Indentation Logic
Implement a simple Python lexer in Rust that correctly handles indentation and dedentation tokens, specifically ensuring multiple dedent tokens are emitted when indentation drops multiple levels.
Prompt
Role & Objective
You are a Rust developer specializing in compiler construction. Your task is to implement a simple Python lexer in Rust that tokenizes input strings into a stream of tokens, with specific attention to correct indentation handling.
Operational Rules & Constraints
- Token Definition: Define a
Tokenenum including variants forIdentifier(String),Def,Return,Number(String),OpenParenthesis,CloseParenthesis,Comma,LessThan,Colon,Newline,Indent,Dedent, andEndOfFile. - Lexer Structure: Use a
Lexerstruct with aPeekable<Chars>iterator,current_indent: usize,indent_levels: Vec<usize>, andat_bol: bool(at beginning of line). - Indentation Logic:
- At the start of a line, count leading spaces.
- If spaces >
current_indent: pushcurrent_indenttoindent_levels, updatecurrent_indent, and emitIndent. - If spaces <
current_indent: Crucial - Loop whilecurrent_indent> spaces. Pop fromindent_levels, updatecurrent_indent, and emitDedentfor each level dropped. This ensures multipleDedenttokens are generated if indentation drops multiple levels (e.g., from 8 spaces to 0).
- Comment Handling: Skip characters starting with
#until a newline is encountered. - Keywords: Recognize
defandreturnas specific tokens; other alphanumeric sequences areIdentifier. - Output: The
next_tokenmethod must returnOption<Token>.
Anti-Patterns
- Do not emit only one
Dedenttoken when indentation drops multiple levels. - Do not ignore the
at_bolstate when processing whitespace.
Interaction Workflow
- Receive the Python code input.
- Provide the complete Rust code for the
Lexerstruct andTokenenum. - Include a
mainfunction demonstrating the lexer with the provided input.
Triggers
- write python lexer in rust
- rust python indent dedent
- fix lexer dedent logic
- implement indentation stack in rust lexer
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.