@lanexio/parser-grammar-csv
This page documents @lanexio/parser-grammar-csv, the CSV grammar package for parsing RFC 4180 compliant CSV and TSV (tab-separated values) with configurable delimiter, quoting, and header detection.
- Version: Stable
- Module name:
parser-grammar-csv - Package:
@lanexio/parser-grammar-csv - Import path:
@lanexio/parser-grammar-csv - Layer: 2 (Grammar)
- Runtime: Universal
- Module format: ESM
- Stability: Stable
- Primary use case: Parse CSV and TSV data into a flat AST.
Layer contract
Section titled “Layer contract”When to use this module
Section titled “When to use this module”- You need to parse CSV (RFC 4180) data into a traversable AST.
- You need to parse TSV (tab-separated values) data.
- You need configurable delimiter, quoting, or header detection.
Module boundary
Section titled “Module boundary”| Boundary | Description |
|---|---|
| Inputs | Uint8Array (source bytes) + optional ParseCsvOptions |
| Outputs | LexTree (flat AST rooted at CsvKind.Document) |
| Side effects | None |
| Determinism | Yes |
| External dependencies | @lanexio/parser-core |
| Never-throw guarantee | Yes |
| Security surface | None (parse only, no output generation) |
Installation
Section titled “Installation”-
Install the package.
Terminal window pnpm add @lanexio/parser-grammar-csvTerminal window npm install @lanexio/parser-grammar-csvTerminal window yarn add @lanexio/parser-grammar-csv -
Import the named export.
import { parseCsv } from '@lanexio/parser-grammar-csv';
Peer dependencies
Section titled “Peer dependencies”| Requirement | Required | Layer | Notes |
|---|---|---|---|
@lanexio/parser-core | Yes | 1 | ^1.0.0 |
Basic Usage
Section titled “Basic Usage”import { parseCsv } from '@lanexio/parser-grammar-csv';
const encoder = new TextEncoder();const bytes = encoder.encode('name,age\nAlice,30\nBob,25');
const tree = parseCsv(bytes);
console.log(tree.nodeCount);console.log(tree.root.kind); // CsvKind.DocumentParse TSV
Section titled “Parse TSV”import { parseTsv } from '@lanexio/parser-grammar-csv';
const encoder = new TextEncoder();const tree = parseTsv(encoder.encode('name\tage\nAlice\t30'));Custom delimiter
Section titled “Custom delimiter”import { parseCsv } from '@lanexio/parser-grammar-csv';
const encoder = new TextEncoder();const tree = parseCsv(encoder.encode('name|age\nAlice|30'), { delimiter: 0x7c });Exports
Section titled “Exports”| Export | Type | Description |
|---|---|---|
parseCsv | (bytes: Uint8Array, options?: ParseCsvOptions) => LexTree | Parse CSV. Never throws. |
parseTsv | (source: Uint8Array, opts?: ParseTsvOptions) => LexTree | Parse TSV (tab-separated values). |
CsvKind | const object | Numeric kind IDs for all CSV node types. |
CsvField | const object | Numeric field IDs for CSV value slots. |
CSV_FLAG_HEADER | 4 | Per-node flag: this record is the header row. Set on the first CsvKind.Record node when header option is true. |
CSV_FLAG_QUOTED | 2 | Per-node flag: field is enclosed in double quotes. Set on CsvKind.Field nodes whose value is quoted. |
CSV_FIELD_NAMES_BY_ID | readonly string[] | Field name lookup by numeric field ID. |
CSV_KIND_NAMES_BY_ID | readonly Record<number, string> | Kind-name lookup by numeric ID. |
csvGrammar | LanexioParserPureGrammar | Grammar descriptor for CSV. |
tsvGrammar | LanexioParserPureGrammar | Grammar descriptor for TSV. |
csvRegistration | GrammarRegistration | Registration for CSV. |
tsvRegistration | GrammarRegistration | Registration for TSV. |
csvReuseOracle | ReuseOracle | Incremental reuse oracle for CSV. |
tsvReuseOracle | ReuseOracle | Incremental reuse oracle for TSV. |
Options
Section titled “Options”ParseCsvOptions
Section titled “ParseCsvOptions”| Field | Type | Required | Default | Description |
|---|---|---|---|---|
delimiter | number | No | 0x2C (,) | Field delimiter byte. |
quote | number | No | 0x22 (") | Quote character byte. |
header | boolean | No | true | Whether the first record is a header row. |
relaxQuotes | boolean | No | true | Allow unescaped quotes inside quoted fields (non-RFC dialects). |
ParseTsvOptions
Section titled “ParseTsvOptions”| Field | Type | Required | Default | Description |
|---|---|---|---|---|
header | boolean | No | true | Whether the first record is a header row. |
Return shape
Section titled “Return shape”| Property | Type | Description |
|---|---|---|
root | LexNode | Root node (CsvKind.Document or CsvKind.Error on pure invalid). |
nodeCount | number | Total nodes in the tree. |
source | Uint8Array | Original parsed bytes. |
Exported types
Section titled “Exported types”| Type | Purpose | Notes |
|---|---|---|
ParseCsvOptions | Options for parseCsv | See options table above. |
ParseTsvOptions | Options for parseTsv | { header?: boolean } |
CsvKindType | Union of all CsvKind values | Type-safe kind reference |
CsvFieldType | Union of all CsvField values | Type-safe field reference |
Configuration and Extension
Section titled “Configuration and Extension”Delimiter customization
Section titled “Delimiter customization”parseCsv accepts any byte as delimiter. Use parseTsv for tab-delimited data (delegates to CSV parser with delimiter: 0x09, quote: -1).
Accessibility
Section titled “Accessibility”Accessibility requirements
Section titled “Accessibility requirements”- No direct accessibility surface. The CSV parser produces flat AST data structures.
Accessibility checklist
Section titled “Accessibility checklist”| Concern | Status |
|---|---|
| Generated output semantics | Not applicable (data format) |
| ARIA attributes in serialized output | Not applicable |
| Semantic element round-trip | Not applicable |
Security
Section titled “Security”Security considerations
Section titled “Security considerations”parseCsvandparseTsvnever throw on any byte sequence.
| Threat | Mitigation | Status |
|---|---|---|
| Malformed input byte sequence | Panic-free guarantee: all inputs accepted, errors produce CsvKind.Error AST nodes | Implemented |
Companion packages
Section titled “Companion packages”| Package | Relationship | Layer | Notes |
|---|---|---|---|
@lanexio/parser-core | Requires | 1 | Provides LexTree, LexNode, LexCursor |
@lanexio/parser | Consumes | 6 | Unified entry point |
Changelog
Section titled “Changelog”| Version | Date | Status | Notable changes |
|---|---|---|---|
1.0.0 | 2026-05-29 | Current | Initial stable release. Apache-2.0. |
Migration notes
Section titled “Migration notes”- None.
Related Content
Section titled “Related Content” Flat AST How the 16-byte node layout works.
Stability Guarantees Never-throw, panic-free, and 3-token recovery.