Parsing CSV and TSV
Lanexio Parser handles CSV and TSV through a single package with configurable parsing. Both formats use the same never-throw parse path and produce zero-copy flat ASTs.
Quick start
Section titled “Quick start”-
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 -
Parse a CSV file.
import { parseCsv } from '@lanexio/parser-grammar-csv';const encoder = new TextEncoder();const tree = parseCsv(encoder.encode('Name,Age,City\nAlice,30,NYC\nBob,25,LA')); -
Parse with options.
import { parseCsv } from '@lanexio/parser-grammar-csv';const tree = parseCsv(encoder.encode('Name|Age|City\nAlice|30|NYC'), {delimiter: '|',header: true,});
TSV files
Section titled “TSV files”import { parseTsv } from '@lanexio/parser-grammar-csv';
const tree = parseTsv(encoder.encode('Name\tAge\tCity\nAlice\t30\tNYC'));Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
delimiter | string | ',' | Field delimiter character |
quote | string | '"' | Quote character for field escaping |
header | boolean | false | Whether the first record is a header row |
skipEmptyLines | boolean | false | Whether to skip empty lines |
Inspecting the tree
Section titled “Inspecting the tree”Each record is a CsvRecord node containing CsvField children. Fields in the header row carry CSV_FLAG_HEADER; quoted fields carry CSV_FLAG_QUOTED.
const cursor = tree.cursor();cursor.gotoFirstChild(); // CsvRecord (first row)cursor.gotoFirstChild(); // CsvFieldconsole.log(cursor.current.kind); // CsvField kind idconsole.log(cursor.current.flags); // CSV_FLAG_HEADER | CSV_FLAG_QUOTED