Skip to content
Back to skills

Python Lexer In Rust With Indentation Logic

ASecurity

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.

  • 611 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 11, 2026
educationpythonrust

Security analysis

A100/100

Scanned November 11, 2026

npx -y skills add ECNU-ICALK/AutoSkill --skill python-lexer-in-rust-with-indentation-logic --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Python Lexer In Rust With Indentation Logic?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Python Lexer In Rust With Indentation Logic
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ecnu-icalk-python-lexer-in-rust-with-indentation-logic/badge)](https://www.skillsdirectory.com/skills/ecnu-icalk-python-lexer-in-rust-with-indentation-logic)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "58169f99-8457-46c6-b2e8-2eb1d9fdea39"
name: "Python Lexer in Rust with Indentation Logic"
description: "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."
version: "0.1.0"
tags:
  - "rust"
  - "python"
  - "lexer"
  - "indentation"
  - "compiler"
triggers:
  - "write python lexer in rust"
  - "rust python indent dedent"
  - "fix lexer dedent logic"
  - "implement indentation stack in rust lexer"
---

# 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
1. **Token Definition**: Define a `Token` enum including variants for `Identifier(String)`, `Def`, `Return`, `Number(String)`, `OpenParenthesis`, `CloseParenthesis`, `Comma`, `LessThan`, `Colon`, `Newline`, `Indent`, `Dedent`, and `EndOfFile`.
2. **Lexer Structure**: Use a `Lexer` struct with a `Peekable<Chars>` iterator, `current_indent: usize`, `indent_levels: Vec<usize>`, and `at_bol: bool` (at beginning of line).
3. **Indentation Logic**:
   - At the start of a line, count leading spaces.
   - If spaces > `current_indent`: push `current_indent` to `indent_levels`, update `current_indent`, and emit `Indent`.
   - If spaces < `current_indent`: **Crucial** - Loop while `current_indent` > spaces. Pop from `indent_levels`, update `current_indent`, and emit `Dedent` for each level dropped. This ensures multiple `Dedent` tokens are generated if indentation drops multiple levels (e.g., from 8 spaces to 0).
4. **Comment Handling**: Skip characters starting with `#` until a newline is encountered.
5. **Keywords**: Recognize `def` and `return` as specific tokens; other alphanumeric sequences are `Identifier`.
6. **Output**: The `next_token` method must return `Option<Token>`.

# Anti-Patterns
- Do not emit only one `Dedent` token when indentation drops multiple levels.
- Do not ignore the `at_bol` state when processing whitespace.

# Interaction Workflow
1. Receive the Python code input.
2. Provide the complete Rust code for the `Lexer` struct and `Token` enum.
3. Include a `main` function 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

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in education →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.