forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🤖 Merge PR DefinitelyTyped#66229 Add @types/swipl-stdio by @tscpp
* Add @types/swipl-stdio * Remove boolean from ResultEntry
- Loading branch information
Showing
4 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Type definitions for swipl-stdio 1.0 | ||
// Project: https://github.com/rla/node-swipl-stdio | ||
// Definitions by: Elias Skogevall <https://github.com/tscpp> | ||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped | ||
|
||
/// <reference types="node" /> | ||
|
||
import { EventEmitter } from 'node:events'; | ||
import { ChildProcessWithoutNullStreams } from 'node:child_process'; | ||
|
||
export type ResultEntry = number | string | ResultList | ResultDict; | ||
export type Result = Record<string, ResultEntry>; | ||
|
||
export interface ResultList { | ||
head: ResultEntry; | ||
tail: ResultList | '[]'; | ||
} | ||
|
||
export interface ResultDict { | ||
tag: string; | ||
content: Result; | ||
} | ||
|
||
declare class Deferred<T = unknown> { | ||
promise: Promise<T>; | ||
resolve: (this: Promise<T>, value: T) => void; | ||
reject: (this: Promise<T>, reason?: string) => void; | ||
} | ||
export type { Deferred }; | ||
|
||
export type QueryStateString = | ||
| (typeof QueryState)['FRESH'] | ||
| (typeof QueryState)['OPEN'] | ||
| (typeof QueryState)['WAITING'] | ||
| (typeof QueryState)['CLOSED']; | ||
|
||
declare class QueryState { | ||
state: QueryStateString; | ||
|
||
setWaiting(): void; | ||
setOpen(): void; | ||
setClosed(): void; | ||
|
||
isFresh(): boolean; | ||
isOpen(): boolean; | ||
isWaiting(): boolean; | ||
isClosed(): boolean; | ||
|
||
setState(state: QueryStateString): void; | ||
|
||
static readonly FRESH: 'fresh'; | ||
static readonly OPEN: 'open'; | ||
static readonly WAITING: 'waiting'; | ||
static readonly CLOSED: 'closed'; | ||
} | ||
export type { QueryState }; | ||
|
||
declare class QueuedQuery { | ||
constructor(string: string, deferred: Deferred); | ||
string: string; | ||
deferred: Deferred; | ||
} | ||
export type { QueuedQuery }; | ||
|
||
declare class Query { | ||
query: string; | ||
engine: Engine; | ||
deferred: Deferred | null; | ||
state: QueryState; | ||
|
||
next(): Promise<Result | false>; | ||
close(): Promise<void>; | ||
} | ||
export type { Query }; | ||
|
||
export type EngineStateString = | ||
| (typeof EngineState)['ACCEPTING'] | ||
| (typeof EngineState)['QUERY'] | ||
| (typeof EngineState)['WAITING'] | ||
| (typeof EngineState)['CLOSED']; | ||
|
||
declare class EngineState extends EventEmitter { | ||
state: EngineStateString; | ||
|
||
isClosed(): boolean; | ||
isAccepting(): boolean; | ||
isQuery(): boolean; | ||
|
||
setAccepting(): void; | ||
setClosed(): void; | ||
setQuery(): void; | ||
setWaiting(): void; | ||
|
||
setState(state: EngineStateString): void; | ||
|
||
static readonly ACCEPTING: 'accepting'; | ||
static readonly QUERY: 'query'; | ||
static readonly WAITING: 'waiting'; | ||
static readonly CLOSED: 'closed'; | ||
} | ||
export type { EngineState }; | ||
|
||
export class Engine { | ||
swipl: ChildProcessWithoutNullStreams; | ||
state: EngineState; | ||
status: number; | ||
query: Query | null; | ||
queue: QueuedQuery[]; | ||
createQuery(string: string): Query | Promise<Query>; | ||
call(string: string): Promise<Result | false>; | ||
close(): void; | ||
} | ||
|
||
export type Term = List | Variable | Compound | Dict; | ||
export type TermLike = Term | string | number | null | undefined | { toProlog(): string }; | ||
|
||
declare class List { | ||
items: Term[]; | ||
constructor(items: readonly Term[]); | ||
toProlog(): string; | ||
} | ||
export type { List }; | ||
|
||
declare class Variable { | ||
name: string; | ||
constructor(name: any); | ||
toProlog(): string; | ||
} | ||
export type { Variable }; | ||
|
||
declare class Compound { | ||
name: string; | ||
args: Term[]; | ||
constructor(name: string, args: readonly Term[]); | ||
toProlog(): string; | ||
} | ||
export type { Compound }; | ||
|
||
declare class Dict { | ||
tag: string | Variable; | ||
content: Record<string, string | number | boolean | Term>; | ||
constructor(tag: any, content: any); | ||
toProlog(): string; | ||
} | ||
export type { Dict }; | ||
|
||
export namespace term { | ||
function list(items: readonly TermLike[]): List; | ||
function variable(name: string): Variable; | ||
function compound(name: string, args: readonly TermLike[]): Compound; | ||
function dict(tag: string | Variable, content: object): Dict; | ||
function serialize(term: TermLike): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Engine, term, ResultEntry } from 'swipl-stdio'; | ||
|
||
const engine = new Engine(); | ||
|
||
(async () => { | ||
// Calling query using "engine.call" | ||
const result = await engine.call('member(X, [1,2,3,4])'); | ||
if (result) { | ||
// $ExpectType ResultEntry | ||
result.X; | ||
} | ||
|
||
// Calling query using "engine.createQuery" | ||
const query = await engine.createQuery('member(X, [1,2,3,4])'); | ||
try { | ||
const result = await query.next(); | ||
if (result) { | ||
// $ExpectType ResultEntry | ||
result.X; | ||
} | ||
} finally { | ||
await query.close(); | ||
} | ||
|
||
engine.close(); | ||
})(); | ||
|
||
// Serialize | ||
|
||
// $ExpectType string | ||
const safe = term.serialize(term.compound('member', [term.variable('X'), term.list([1, 2, 3, 4])])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"lib": [ | ||
"es6" | ||
], | ||
"noImplicitAny": true, | ||
"noImplicitThis": true, | ||
"strictFunctionTypes": true, | ||
"strictNullChecks": true, | ||
"baseUrl": "../", | ||
"typeRoots": [ | ||
"../" | ||
], | ||
"types": [], | ||
"noEmit": true, | ||
"forceConsistentCasingInFileNames": true | ||
}, | ||
"files": [ | ||
"index.d.ts", | ||
"swipl-stdio-tests.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "extends": "@definitelytyped/dtslint/dt.json" } |