FS FormScript
Package@formscript/lexer
Specification3, 3.3, 3.5, 3.6, 3.7, 20
Depends onast, errors

@formscript/lexer

Source text → tokens, per §3 of the specification.

Usage

ts
import { tokenize } from '@formscript/lexer';

const { tokens, diagnostics } = tokenize(source, { file: 'application.forms' });

tokenize never throws. Every problem it finds is a diagnostic:

ts
if (diagnostics.length > 0) {
  for (const d of diagnostics) {
    console.error(`${d.code} ${d.location?.line}:${d.location?.column} ${d.message}`);
  }
}

What it handles

  • UTF-8 input, with a byte offset on every span.
  • All three comment forms of §3.3 (#, //, /* … */).
  • Identifiers, the hard keywords of §3.5 and the soft keywords of §20.
  • Every literal kind of §3.6: integers, decimals, strings with the full escape set, booleans, null, inapplicable, dates, datetimes and durations.
  • The punctuation of §3.7, longest-match first.

Notes

  • A leading - is punctuation, not part of a numeric literal. The grammar's unary production handles the sign, so -1 and a - 1 are the same token stream.
  • The duration literal follows the ISO 8601 form of §3.6, including P1Y2M3D and PT30M, and the short form (30d, 6mo, 2y). When a short form ends in m and is followed by o, the longer unit wins.

Public API

22 exported symbols.

BooleanToken interface
DateTimeToken interface
DateToken interface
DurationToken interface
EofToken interface
HARD_KEYWORDS re-export
IdentifierToken interface
InapplicableToken interface
KeywordToken interface

A hard keyword (§3.5). Soft keywords are lexed as identifiers.

LexLocation interface

A diagnostic location that also carries the 0-based source offset.

LexOptions interface
LexResult interface
NullToken interface
NumberToken interface
Punctuation type
PUNCTUATION re-export
PunctuationToken interface
StringToken interface
Token type
TokenBase interface
tokenize re-export
TokenKind type