APG Lite Parser

1.0.5 · active · verified Sun Apr 19

apg-lite is a lightweight JavaScript parser for ABNF (Augmented Backus-Naur Form) grammars, supporting a simplified subset of SABNF operators. It functions strictly as a parser, requiring grammar objects to be pre-generated using `apg-js` (version 4.3.0 or higher) with its `--lite` option. Currently at version 1.0.5, it parses only JavaScript strings, a key distinction from `apg-js` which handles arbitrary arrays of positive integers. The library retains only User-Defined Terminals (UDT), positive look-ahead (AND), and negative look-ahead (NOT) operators from the SABNF superset. It offers simplified AST manipulation, parse tree tracing for debugging, and statistics collection for profiling, all encapsulated within a single ECMAScript Module (ESM) compatible JavaScript file. The package is actively maintained, with a release cadence driven by bug fixes and minor enhancements, as demonstrated by recent updates to its robust, dependency-free URI parser.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use the self-contained UriParser to parse various URI strings, logging success or failure with the resulting AST.

import { UriParser } from 'apg-lite/uri-app/UriParser';

const uriStrings = [
  'http://user:pass@host:80/path?query#fragment',
  'mailto:john.doe@example.com',
  'ftp://ftp.example.com/dir/file.txt',
  '//example.org/schema-relative/path',
  'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==',
  '' // Empty string for demonstration
];

console.log('Parsing various URI strings:');
uriStrings.forEach(uriStr => {
  try {
    const parser = new UriParser(uriStr);
    const result = parser.parse();
    if (result.success) {
      console.log(`\nURI: '${uriStr}'`);
      console.log('  Parse successful. Result:', JSON.stringify(result.ast, null, 2));
    } else {
      console.error(`\nURI: '${uriStr}'`);
      console.error('  Parse failed. Error:', result.error);
    }
  } catch (e) {
    console.error(`\nError parsing URI '${uriStr}':`, e.message);
  }
});

view raw JSON →