distributors-json
Copyright(C) 2026 - Eitan Chatav
LicenseBSD-style (see the file LICENSE)
MaintainerEitan Chatav <eitan.chatav@gmail.com>
Stabilityprovisional
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Control.Lens.Grammar.TypeScript

Description

TypeScript's declaration files, .d.ts, like the TypeScript compiler, in two layers: tsTokensGrammar of their tokens, by their TsTokenCategorys, & tsGrammar of their declarations, of those tokens, by the compiler's names of their nodes, so that they parse & print together.

Both layers parse by parsecG & print by unparsecG, locating failures, since tsTokensGrammar scans tokens without their trivia, as the compiler does. tsGrammar isn't LL(1): it backtracks, by try, where TypeScript's syntax needs more lookahead. Here, a declaration prints as tokens, & as characters, & parses back.

>>> :set -XOverloadedStrings
>>> let point = ObjectType [PropertySignature [] (PropertyIdentifier "x") False (Just (KeywordType NumberKeyword))]
>>> let printed = unparsecG tsGrammar [TypeAliasDeclaration [ExportKeyword] "Point" [] point] ([] :: [TsToken])
>>> let source = parsecStream (unparsecG tsTokensGrammar (parsecStream printed) ("" :: String))
>>> putStr source
export type Point = { x: number };
>>> parsecResult (parsecG tsGrammar (concat (parsecResult (parsecG tsTokensGrammar source)))) == Just [TypeAliasDeclaration [ExportKeyword] "Point" [] point]
True
Synopsis

Grammar

tsGrammar :: CtxGrammar TsToken [TsDeclaration] Source #

The declarations of a declaration file, of tokens, by the TypeScript compiler's syntax, printed one per line, indented within blocks, & with object types on one line. Trivia print conventionally, & parse only from tokens with trivia, by parseG, since tsTokensGrammar scans without them. Its ;s print, & parse if they're there.

It's a CtxGrammar by try, rather than by monadic bonding, since it isn't LL(1): TypeScript's syntax needs more lookahead in places, where it backtracks by try: declarations' & members' modifiers, imports' forms & their type & as, comma-separated lists after their first item, members' signatures, constructors & accessors, a binding's name before its :, type parameters' modifiers, asserts & is predicates, function, conditional, typeof import, mapped & named tuple member types.

Printing a file, of many tokens, is linear into a stream which grows at its end in constant time, like a Seq, rather than a list.

tsTokenGrammar :: RegGrammar Char TsToken Source #

A token, scanned from its characters. Its trivia, whitespace, newlines & comments, are tokens, as the scanner of the TypeScript compiler scans without skipping them. A } is never a template's middle or tail, which tsTokensGrammar rescans in its context, as the compiler's parser rescans it.

A number is spelled by its digits, letters, separators & points, as in 0xFF, 1_000.5 or 1n, but not by a signed exponent.

It's a RegGrammar, since each token's spelling is a regular language.

tsTokensGrammar :: CtxGrammar Char [TsToken] Source #

Tokens, scanned from their characters without their trivia, as the TypeScript compiler's scanner skips trivia, & printed with them, as tsGrammar prints them, so that tsGrammar parses by parsecG. A } which closes a template's substitution is scanned as the template's middle or tail, by the depth of braces within the substitutions open, as the compiler's parser rescans it.

It's a CtxGrammar by monadic bonding: each token is scanned in the context of the tokens before it, whose braces & templates bond, by P.>>=, the depths that a } is scanned by.

Declarations

data TsDeclaration Source #

A declaration of a declaration file, by the TypeScript compiler's name of its node, most with its modifiers, like export & declare.

Constructors

TypeAliasDeclaration [TsTokenCategory] Text [TsTypeParameter] TsType
type T<U> = V;
InterfaceDeclaration [TsTokenCategory] Text [TsTypeParameter] [TsHeritage] [TsProperty]
interface I<T> extends J { … }
ClassDeclaration [TsTokenCategory] Text [TsTypeParameter] (Maybe TsHeritage) [TsHeritage] [TsProperty]
class C<T> extends D implements I { … }
FunctionDeclaration [TsTokenCategory] Text TsSignature
function f<T>(x: T): U;
VariableStatement [TsTokenCategory] TsTokenCategory [TsVariableDeclaration]

const x: T;, by var, let or const

EnumDeclaration [TsTokenCategory] Text [TsEnumMember]
enum E { A, B = 1 }
ModuleDeclaration [TsTokenCategory] TsModuleName (Maybe [TsDeclaration])

namespace N { … }, module "m" { … } or global { … }

ImportDeclaration Bool (Maybe TsImportClause) Text

import type { A } from "m";, of types only or not

ImportEqualsDeclaration [TsTokenCategory] Bool Text TsModuleReference

import x = require("m");, of types only or not

ExportDeclaration Bool TsExportClause (Maybe Text)

export type { A } from "m";, of types only or not

ExportAssignment Bool TsEntityName

export = x;, or else export default x;

NamespaceExportDeclaration Text
export as namespace N;

data TsVariableDeclaration Source #

x: T = 1, a variable, of a type or not, & a literal value or not.

type TsEntityName = [Text] Source #

A qualified name, N.M.x, by its identifiers.

Types

data TsType Source #

A type, by the TypeScript compiler's name of its node.

Constructors

KeywordType TsTokenCategory

number, string, never, …

ThisType
this
LiteralType TsLiteral

null, true, 1 or "abc"

ReferenceType TsEntityName [TsType]

T or N.T<U, V>

ArrayType TsType
T[]
TupleType [TsType]
[T, U]
OptionalType TsType

T?, an element of a tuple

RestType TsType

...T, an element of a tuple

NamedTupleMember Bool Text Bool TsType

...x?: T, an element of a tuple, rest or not, optional or not

UnionType [TsType]
T | U
IntersectionType [TsType]
T & U
ParenthesizedType TsType
(T)
FunctionType TsSignature
<T>(x: T) => U
ConstructorType Bool TsSignature

abstract new <T>(x: T) => U, abstract or not

TypeOperator TsTokenCategory TsType

keyof T, readonly T or unique T

IndexedAccessType TsType TsType
T[K]
TypeQuery TsEntityName [TsType]
typeof x<T>
ImportType Bool Text TsEntityName [TsType]

typeof import("m").N.T<U>, a typeof or not

InferType Text (Maybe TsType)
infer T extends U
TypePredicate Bool Text (Maybe TsType)

asserts x is T or this is T, a return type, asserting or not

ObjectType [TsProperty]
{ x: T; y?: U }
MappedType (Maybe TsTokenCategory) Text TsType (Maybe TsType) (Maybe TsTokenCategory) TsType

{ -readonly [K in T as U]+?: V }, by its readonly modifier, readonly, + or -, its type parameter & its constraint & name type, & its ? modifier, ?, + or -

ConditionalType TsType TsType TsType TsType
T extends U ? V : W
TemplateLiteralType Text [(TsType, Text)]

`a${T}b`, by its head & its spans' types & literals

Instances

Instances details
Read TsType Source # 
Instance details

Defined in Control.Lens.Grammar.TypeScript

Show TsType Source # 
Instance details

Defined in Control.Lens.Grammar.TypeScript

Eq TsType Source # 
Instance details

Defined in Control.Lens.Grammar.TypeScript

Methods

(==) :: TsType -> TsType -> Bool #

(/=) :: TsType -> TsType -> Bool #

Ord TsType Source # 
Instance details

Defined in Control.Lens.Grammar.TypeScript

data TsProperty Source #

A member of an ObjectType, InterfaceDeclaration or ClassDeclaration, by the TypeScript compiler's name of its node, with its modifiers, like readonly & static.

Tokens

data TsToken Source #

A token, of a TsTokenCategory & its text, as the scanner of the TypeScript compiler scans it, with its trivia: its text is an identifier's name, a string's value, a number's spelling, a template's literal, or a comment.

Constructors

TsToken 

data TsTokenCategory Source #

The categories of TsTokens, by the TypeScript compiler's names.

Instances

Instances details
Bounded TsTokenCategory Source # 
Instance details

Defined in Control.Lens.Grammar.TypeScript

Enum TsTokenCategory Source # 
Instance details

Defined in Control.Lens.Grammar.TypeScript

Read TsTokenCategory Source # 
Instance details

Defined in Control.Lens.Grammar.TypeScript

Show TsTokenCategory Source # 
Instance details

Defined in Control.Lens.Grammar.TypeScript

Eq TsTokenCategory Source # 
Instance details

Defined in Control.Lens.Grammar.TypeScript

Ord TsTokenCategory Source # 
Instance details

Defined in Control.Lens.Grammar.TypeScript