<!DOCTYPE HTML>
<html lang="en" class="navy sidebar-visible" dir="ltr">
<head>
<meta charset="UTF-8">
<title>Parser - towl Documentation</title>
<meta name="description" content="Documentation for towl - a fast CLI tool to scan codebases for TODO comments">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<link rel="stylesheet" id="highlight-css" href="../highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="../tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="../ayu-highlight.css">
<script>
const path_to_root = "../";
const default_light_theme = "navy";
const default_dark_theme = "navy";
window.path_to_searchindex_js = "../searchindex.js";
</script>
<script src="../toc.js"></script>
</head>
<body>
<div id="mdbook-help-container">
<div id="mdbook-help-popup">
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
<div>
<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
<p>Press <kbd>?</kbd> to show this help</p>
<p>Press <kbd>Esc</kbd> to hide this help</p>
</div>
</div>
</div>
<div id="body-container">
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('navy')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
sidebar_toggle.checked = false;
}
if (sidebar === 'visible') {
sidebar_toggle.checked = true;
} else {
html.classList.remove('sidebar-visible');
}
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">towl Documentation</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/glottologist/towl" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/glottologist/towl/edit/main/docs/src/src/api/parser.md" title="Suggest an edit" aria-label="Suggest an edit" rel="edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<div class="search-wrapper">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
<div class="spinner-wrapper">
<i class="fa fa-spinner fa-spin"></i>
</div>
</div>
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="parser"><a class="header" href="#parser">Parser</a></h1>
<p>The parser reads file content, identifies comment lines using regex patterns, extracts TODO items, and captures surrounding context.</p>
<h2 id="parser-1"><a class="header" href="#parser-1"><code>Parser</code></a></h2>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub struct Parser {
comment_patterns: Vec<Regex>,
patterns: Vec<Pattern>,
function_patterns: Vec<Regex>,
context_lines: usize,
}
<span class="boring">}</span></code></pre></pre>
<p>The parser is <code>pub(crate)</code> -- it is used internally by <code>Scanner</code> and not exposed in the public API. The public interface is through the module-level functions.</p>
<h3 id="construction"><a class="header" href="#construction">Construction</a></h3>
<p>Created internally by <code>Scanner::new()</code> using <code>Parser::new(config)</code>. All regex patterns are compiled once during construction.</p>
<h2 id="public-functions"><a class="header" href="#public-functions">Public Functions</a></h2>
<h3 id="validate_patterns"><a class="header" href="#validate_patterns"><code>validate_patterns</code></a></h3>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub fn validate_patterns(config: &ParsingConfig) -> Result<(), TowlParserError>
<span class="boring">}</span></code></pre></pre>
<p>Validates all regex patterns in the config without creating a parser. Useful for checking configuration before starting a scan.</p>
<p><strong>Checks:</strong></p>
<ul>
<li>Each pattern is valid regex</li>
<li>Each pattern is within <code>MAX_PATTERN_LENGTH</code> (256 characters)</li>
<li>Compiled regex is within <code>REGEX_SIZE_LIMIT</code> (256 KB)</li>
</ul>
<h3 id="parse_content"><a class="header" href="#parse_content"><code>parse_content</code></a></h3>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub fn parse_content(
config: &ParsingConfig,
path: &Path,
content: &str,
) -> Result<Vec<TodoComment>, TowlParserError>
<span class="boring">}</span></code></pre></pre>
<p>Parses file content for TODO comments. Creates a temporary parser, runs extraction, and returns the results.</p>
<h2 id="parsing-pipeline"><a class="header" href="#parsing-pipeline">Parsing Pipeline</a></h2>
<p>For each line in the file:</p>
<ol>
<li><strong>Comment detection</strong> -- Check if the line matches any <code>comment_prefixes</code> pattern</li>
<li><strong>TODO matching</strong> -- Check if the comment matches any <code>todo_patterns</code> pattern</li>
<li><strong>Type classification</strong> -- Determine the <code>TodoType</code> from the matched pattern</li>
<li><strong>Description extraction</strong> -- Extract the description via the first capture group <code>(.*)</code></li>
<li><strong>Context capture</strong> -- Grab <code>include_context_lines</code> lines above and below</li>
<li><strong>Function detection</strong> -- Search upward (within 3 lines) for a <code>function_patterns</code> match</li>
</ol>
<h2 id="pattern-types"><a class="header" href="#pattern-types">Pattern Types</a></h2>
<h3 id="comment-prefixes"><a class="header" href="#comment-prefixes">Comment Prefixes</a></h3>
<p>Regex patterns that identify comment lines:</p>
<div class="table-wrapper"><table><thead><tr><th>Default pattern</th><th>Matches</th></tr></thead><tbody>
<tr><td><code>//</code></td><td>C-style line comments</td></tr>
<tr><td><code>^\s*#</code></td><td>Shell/Python comments</td></tr>
<tr><td><code>/\*</code></td><td>C-style block comment start</td></tr>
<tr><td><code>^\s*\*</code></td><td>C-style block comment continuation</td></tr>
</tbody></table>
</div>
<h3 id="todo-patterns"><a class="header" href="#todo-patterns">TODO Patterns</a></h3>
<p>Regex patterns with a capture group for the description:</p>
<div class="table-wrapper"><table><thead><tr><th>Default pattern</th><th>Matches</th></tr></thead><tbody>
<tr><td><code>(?i)\bTODO:\s*(.*)</code></td><td>TODO comments</td></tr>
<tr><td><code>(?i)\bFIXME:\s*(.*)</code></td><td>FIXME comments</td></tr>
<tr><td><code>(?i)\bHACK:\s*(.*)</code></td><td>HACK comments</td></tr>
<tr><td><code>(?i)\bNOTE:\s*(.*)</code></td><td>NOTE comments</td></tr>
<tr><td><code>(?i)\bBUG:\s*(.*)</code></td><td>BUG comments</td></tr>
</tbody></table>
</div>
<p>All default patterns are case-insensitive (<code>(?i)</code>).</p>
<h3 id="function-patterns"><a class="header" href="#function-patterns">Function Patterns</a></h3>
<p>Regex patterns to detect enclosing function names:</p>
<div class="table-wrapper"><table><thead><tr><th>Default pattern</th><th>Language</th></tr></thead><tbody>
<tr><td><code>^\s*(pub\s+)?fn\s+(\w+)</code></td><td>Rust</td></tr>
<tr><td><code>^\s*def\s+(\w+)</code></td><td>Python</td></tr>
<tr><td><code>^\s*(async\s+)?function\s+(\w+)</code></td><td>JavaScript</td></tr>
<tr><td><code>^\s*(public|private|protected)?\s*(static\s+)?\w+\s+(\w+)\s*\(</code></td><td>Java/C#</td></tr>
<tr><td><code>^\s*func\s+(\w+)</code></td><td>Go/Swift</td></tr>
</tbody></table>
</div>
<h2 id="constants"><a class="header" href="#constants">Constants</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Constant</th><th>Value</th><th>Purpose</th></tr></thead><tbody>
<tr><td><code>MIN_CONTEXT_LINES</code></td><td>1</td><td>Minimum context window</td></tr>
<tr><td><code>MAX_CONTEXT_LINES</code></td><td>50</td><td>Maximum context window</td></tr>
<tr><td><code>FORWARD_SEARCH_LINES</code></td><td>3</td><td>Lines searched upward for function context</td></tr>
<tr><td><code>MAX_PATTERN_LENGTH</code></td><td>256</td><td>Maximum regex pattern string length</td></tr>
<tr><td><code>REGEX_SIZE_LIMIT</code></td><td>262,144</td><td>Maximum compiled regex size (256 KB)</td></tr>
<tr><td><code>MAX_TOTAL_PATTERNS</code></td><td>50</td><td>Maximum total patterns across all categories</td></tr>
</tbody></table>
</div>
<h2 id="errors"><a class="header" href="#errors">Errors</a></h2>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub enum TowlParserError {
InvalidRegexPattern(String, regex::Error),
UnknownConfigPattern(TowlCommentError),
RegexGroupMissing,
PatternTooLong(usize, usize),
TooManyTotalPatterns { count: usize, max_allowed: usize },
}
<span class="boring">}</span></code></pre></pre>
<div class="table-wrapper"><table><thead><tr><th>Variant</th><th>Cause</th></tr></thead><tbody>
<tr><td><code>InvalidRegexPattern</code></td><td>Regex failed to compile</td></tr>
<tr><td><code>UnknownConfigPattern</code></td><td>Pattern matched but type could not be determined</td></tr>
<tr><td><code>RegexGroupMissing</code></td><td>Pattern lacks a capture group</td></tr>
<tr><td><code>PatternTooLong</code></td><td>Pattern exceeds 256 characters</td></tr>
<tr><td><code>TooManyTotalPatterns</code></td><td>Total patterns across all categories exceeds 50</td></tr>
</tbody></table>
</div>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<a rel="prev" href="../api/scanner.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../api/config.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../api/scanner.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../api/config.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
</div>
</body>
</html>