dot-language-support
Version:
Parser and language service for graphviz (dot) files
498 lines (491 loc) • 16.2 kB
TypeScript
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as lst from 'vscode-languageserver-types';
import { ColorInformation, Color as Color$1, ColorPresentation } from 'vscode-languageserver-types';
declare const enum ErrorSource {
Scan = 1,
Parse = 2,
Check = 4
}
type ErrorCode = ParseErrorCode | ScanErrorCode | CheckErrorCode;
interface ParseErrorCode {
source: ErrorSource.Parse;
sub: ParseError;
}
interface ScanErrorCode {
source: ErrorSource.Scan;
sub: ScanError;
}
interface CheckErrorCode {
source: ErrorSource.Check;
sub: CheckError;
}
declare const enum ParseError {
ExpectationFailed = 0,
TrailingData = 1,
FailedListParsing = 2
}
declare const enum ScanError {
ExpectationFailed = 0,
Unterminated = 1
}
declare const enum CheckError {
InvalidEdgeOperation = 0,
InvalidShapeName = 1
}
interface DiagnosticMessage {
message: string;
code: ErrorCode;
category: DiagnosticCategory;
start: number;
end: number;
}
declare enum DiagnosticCategory {
Error = 1,
Warning = 2,
Message = 3,
Suggestion = 4
}
type ID = string;
interface SourceFile {
content: string;
graph?: Graph;
identifiers: Set<ID>;
diagnostics: DiagnosticMessage[];
symbols?: SymbolTable;
colors?: ColorTable;
}
interface HtmlIdentifier extends SyntaxNode {
kind: SyntaxKind.HtmlIdentifier;
htmlContent: string;
}
interface TextIdentifier extends SyntaxNode {
kind: SyntaxKind.TextIdentifier;
text: string;
}
interface QuotedTextIdentifier extends SyntaxNode {
kind: SyntaxKind.QuotedTextIdentifier;
values: SyntaxNodeArray<StringLiteral>;
concatenation?: string;
}
interface StringLiteral extends SyntaxNode {
kind: SyntaxKind.StringLiteral;
text: string;
}
interface NumericIdentifier extends SyntaxNode {
kind: SyntaxKind.NumericIdentifier;
text: string;
value: number;
}
type Identifier = TextIdentifier | QuotedTextIdentifier | HtmlIdentifier | NumericIdentifier;
interface Graph extends SyntaxNode {
kind: SyntaxKind.DirectedGraph | SyntaxKind.UndirectedGraph;
keyword: Token<SyntaxKind.GraphKeyword | SyntaxKind.DigraphKeyword>;
strict?: Token<SyntaxKind.StrictKeyword>;
id?: Identifier;
statements: SyntaxNodeArray<Statement>;
}
interface StatementBase {
terminator?: StatementSeparator;
}
type StatementSeparator = Token<SyntaxKind.SemicolonToken>;
type Statement = NodeStatement | EdgeStatement | AttributeStatement | IdEqualsIdStatement | SubGraphStatement;
interface NodeStatement extends SyntaxNode, StatementBase {
kind: SyntaxKind.NodeStatement;
id: NodeId;
attributes: SyntaxNodeArray<AttributeContainer>;
}
interface NodeId extends SyntaxNode {
kind: SyntaxKind.NodeId;
id: Identifier;
port?: PortDeclaration;
}
type EdgeSourceOrTarget = NodeId | SubGraph;
interface EdgeStatement extends SyntaxNode, StatementBase {
kind: SyntaxKind.EdgeStatement;
source: EdgeSourceOrTarget;
rhs: SyntaxNodeArray<EdgeRhs>;
attributes: SyntaxNodeArray<AttributeContainer>;
}
interface AttributeStatement extends SyntaxNode, StatementBase {
kind: SyntaxKind.AttributeStatement;
subject: Token<SyntaxKind.GraphKeyword> | Token<SyntaxKind.NodeKeyword> | Token<SyntaxKind.EdgeKeyword>;
attributes: SyntaxNodeArray<AttributeContainer>;
}
interface IdEqualsIdStatement extends SyntaxNode, StatementBase {
kind: SyntaxKind.IdEqualsIdStatement;
leftId: Identifier;
rightId: Identifier;
}
interface SubGraph extends SyntaxNode {
kind: SyntaxKind.SubGraph;
id?: Identifier;
statements: SyntaxNodeArray<Statement>;
}
interface SubGraphStatement extends SyntaxNode, StatementBase {
kind: SyntaxKind.SubGraphStatement;
subgraph: SubGraph;
}
interface EdgeRhs extends SyntaxNode {
kind: SyntaxKind.EdgeRhs;
operation: EdgeOp;
target: EdgeSourceOrTarget;
}
interface AttributeContainer extends SyntaxNode {
kind: SyntaxKind.AttributeContainer;
openBracket: Token<SyntaxKind.OpenBracketToken>;
assignments: SyntaxNodeArray<Assignment>;
closeBracket: Token<SyntaxKind.CloseBracketToken>;
}
interface Assignment extends SyntaxNode {
kind: SyntaxKind.Assignment;
leftId: Identifier;
rightId: Identifier;
terminator?: AssignmentSeparator;
}
type AssignmentSeparator = Token<SyntaxKind.SemicolonToken> | Token<SyntaxKind.CommaToken>;
type PortDeclaration = NormalPortDeclaration | CompassPortDeclaration;
interface NormalPortDeclaration extends SyntaxNode {
kind: SyntaxKind.NormalPortDeclaration;
colon: Token<SyntaxKind.ColonToken>;
id: Identifier;
compassPt?: CompassPortDeclaration;
}
interface CompassPortDeclaration extends SyntaxNode {
kind: SyntaxKind.CompassPortDeclaration;
colon: Token<SyntaxKind.ColonToken>;
compassPt: CompassPort;
}
type CompassPort = Token<SyntaxKind.CompassNorthToken> | Token<SyntaxKind.CompassNorthEastToken> | Token<SyntaxKind.CompassEastToken> | Token<SyntaxKind.CompassSouthEastToken> | Token<SyntaxKind.CompassSouthToken> | Token<SyntaxKind.CompassSouthWestToken> | Token<SyntaxKind.CompassWestToken> | Token<SyntaxKind.CompassNorthWestToken> | Token<SyntaxKind.CompassCenterToken> | Token<SyntaxKind.UnderscoreToken>;
type EdgeOp = Token<SyntaxKind.DirectedEdgeOp> | Token<SyntaxKind.UndirectedEdgeOp>;
interface TextRange {
pos: number;
end: number;
}
declare enum SyntaxKind {
Unknown = 0,
EndOfFileToken = 1,
NewLineTrivia = 2,
WhitespaceTrivia = 3,
HashCommentTrivia = 4,
SingleLineCommentTrivia = 5,
MultiLineCommentTrivia = 6,
CommaToken = 7,
SemicolonToken = 8,
PlusToken = 9,
OpenBraceToken = 10,
CloseBraceToken = 11,
OpenBracketToken = 12,
CloseBracketToken = 13,
ColonToken = 14,
EqualsToken = 15,
LessThan = 16,
GreaterThan = 17,
CompassNorthToken = 18,
CompassNorthEastToken = 19,
CompassEastToken = 20,
CompassSouthEastToken = 21,
CompassSouthToken = 22,
CompassSouthWestToken = 23,
CompassWestToken = 24,
CompassNorthWestToken = 25,
CompassCenterToken = 26,
UnderscoreToken = 27,
StringLiteral = 28,
HtmlIdentifier = 29,
TextIdentifier = 30,
QuotedTextIdentifier = 31,
NumericIdentifier = 32,
GraphKeyword = 33,
DigraphKeyword = 34,
NodeKeyword = 35,
EdgeKeyword = 36,
SubgraphKeyword = 37,
StrictKeyword = 38,
DirectedEdgeOp = 39,
UndirectedEdgeOp = 40,
DirectedGraph = 41,
UndirectedGraph = 42,
NodeStatement = 43,
EdgeStatement = 44,
AttributeStatement = 45,
IdEqualsIdStatement = 46,
SubGraph = 47,
SubGraphStatement = 48,
EdgeRhs = 49,
AttributeContainer = 50,
Assignment = 51,
NormalPortDeclaration = 52,
CompassPortDeclaration = 53,
NodeId = 54,
Count = 55,
FirstNode = 41,
CompassBegin = 18,
CompassEnd = 27,
LastKeyword = 38
}
interface SyntaxNode extends TextRange {
kind: SyntaxKind;
flags: SyntaxNodeFlags;
graphContext?: GraphContext;
parent?: SyntaxNode;
symbol?: TypeSymbol;
}
interface Token<TKind extends SyntaxKind> extends SyntaxNode {
kind: TKind;
}
interface SyntaxNodeArray<T extends SyntaxNode> extends ReadonlyArray<T>, TextRange {
hasTrailingComma?: boolean;
}
type MutableSyntaxNodeArray<T extends SyntaxNode> = SyntaxNodeArray<T> & T[];
declare const enum SyntaxNodeFlags {
None = 0,
ContainsErrors = 2,
Synthesized = 4
}
declare const enum GraphContext {
None = 0,
Strict = 2,
Directed = 4,
Undirected = 8
}
declare const enum TokenFlags {
None = 0,
Unterminated = 2,
PrecedingLineBreak = 4
}
declare const enum CharacterCodes {
nullCharacter = 0,
maxAsciiCharacter = 127,
lineFeed = 10,
carriageReturn = 13,
lineSeparator = 8232,
paragraphSeparator = 8233,
nextLine = 133,
space = 32,
nonBreakingSpace = 160,
enQuad = 8192,
emQuad = 8193,
enSpace = 8194,
emSpace = 8195,
threePerEmSpace = 8196,
fourPerEmSpace = 8197,
sixPerEmSpace = 8198,
figureSpace = 8199,
punctuationSpace = 8200,
thinSpace = 8201,
hairSpace = 8202,
zeroWidthSpace = 8203,
narrowNoBreakSpace = 8239,
ideographicSpace = 12288,
mathematicalSpace = 8287,
ogham = 5760,
_ = 95,
$ = 36,
_0 = 48,
_1 = 49,
_2 = 50,
_3 = 51,
_4 = 52,
_5 = 53,
_6 = 54,
_7 = 55,
_8 = 56,
_9 = 57,
a = 97,
b = 98,
c = 99,
d = 100,
e = 101,
f = 102,
g = 103,
h = 104,
i = 105,
j = 106,
k = 107,
l = 108,
m = 109,
n = 110,
o = 111,
p = 112,
q = 113,
r = 114,
s = 115,
t = 116,
u = 117,
v = 118,
w = 119,
x = 120,
y = 121,
z = 122,
A = 65,
B = 66,
C = 67,
D = 68,
E = 69,
F = 70,
G = 71,
H = 72,
I = 73,
J = 74,
K = 75,
L = 76,
M = 77,
N = 78,
O = 79,
P = 80,
Q = 81,
R = 82,
S = 83,
T = 84,
U = 85,
V = 86,
W = 87,
X = 88,
Y = 89,
Z = 90,
ampersand = 38,
asterisk = 42,
at = 64,
backslash = 92,
backtick = 96,
bar = 124,
caret = 94,
closeBrace = 125,
closeBracket = 93,
closeParen = 41,
colon = 58,
comma = 44,
dot = 46,
doubleQuote = 34,
equals = 61,
exclamation = 33,
greaterThan = 62,
hash = 35,
lessThan = 60,
minus = 45,
openBrace = 123,
openBracket = 91,
openParen = 40,
percent = 37,
plus = 43,
question = 63,
semicolon = 59,
singleQuote = 39,
slash = 47,
tilde = 126,
backspace = 8,
formFeed = 12,
byteOrderMark = 65279,
tab = 9,
verticalTab = 11
}
type SymbolTable = Map<string, TypeSymbol>;
type ColorTable = Map<string, ColorInfo>;
interface TypeSymbol {
name: string;
firstMention: SyntaxNode;
references?: SyntaxNode[];
members?: SymbolTable;
}
interface ColorInfo {
node: SyntaxNode;
}
interface Color {
readonly red: number;
readonly green: number;
readonly blue: number;
readonly alpha: number;
}
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type StatementOf<T extends Statement["kind"]> = T extends SyntaxKind.SubGraphStatement ? SubGraphStatement : T extends SyntaxKind.AttributeStatement ? AttributeStatement : T extends SyntaxKind.EdgeStatement ? EdgeStatement : T extends SyntaxKind.IdEqualsIdStatement ? IdEqualsIdStatement : T extends SyntaxKind.NodeStatement ? NodeStatement : never;
interface Scanner {
readonly end: number;
readonly pos: number;
readonly startPos: number;
readonly tokenPos: number;
readonly token: SyntaxKind;
readonly tokenValue: string | undefined;
readonly isUnterminated: boolean;
readonly text: string;
readonly onError: ErrorCallback | null;
setText(newText?: string, start?: number, length?: number): void;
setErrorCallback(cb: ErrorCallback): void;
scan(skipTrivia: boolean): SyntaxKind;
lookAhead(callback: () => SyntaxKind | boolean): SyntaxKind | boolean;
tryScan(callback: () => SyntaxKind | boolean): SyntaxKind | boolean;
}
declare function getTokenAsText(token: SyntaxKind): string | undefined;
declare function getTextAsToken(token: string): SyntaxKind | undefined;
type ErrorCallback = (message: string, category: DiagnosticCategory, sub: ScanError, length: number) => void;
declare class DefaultScanner implements Scanner {
#private;
end: number;
pos: number;
startPos: number;
tokenPos: number;
token: SyntaxKind;
tokenValue: string | undefined;
tokenFlags: TokenFlags;
isUnterminated: boolean;
text: string;
onError: ErrorCallback | null;
setText(newText?: string, start?: number, length?: number): void;
setErrorCallback(cb: ErrorCallback | null): void;
scan(skipTrivia?: boolean): SyntaxKind;
lookAhead<T extends SyntaxKind>(callback: () => T): T;
tryScan<T extends SyntaxKind>(callback: () => T): T;
}
declare function isIdentifierStart(ch: number): boolean;
declare function skipTrivia(text: string, pos: number): number;
declare function isLineBreak(ch: number): boolean;
declare enum ParsingContext {
None = 0,
StatementList = 1,
AttributeContainerList = 2,
AssignmentList = 3,
EdgeRhsList = 4,
QuotedTextIdentifierConcatenation = 5,
Count = 6
}
declare class Parser {
#private;
currentToken: SyntaxKind;
nodeCount: number;
identifiers: Set<string>;
identifierCount: number;
sourceText: string;
scanner: Scanner;
currentNodeHasError: boolean;
currentContext: ParsingContext;
diagnostics: DiagnosticMessage[];
constructor();
parse(sourceText: string): SourceFile;
}
declare function isIdentifier(kind: SyntaxKind): kind is SyntaxKind.HtmlIdentifier | SyntaxKind.TextIdentifier | SyntaxKind.QuotedTextIdentifier | SyntaxKind.NumericIdentifier;
declare function isIdentifierNode(node: SyntaxNode): node is Identifier;
interface DocumentLike {
positionAt(offset: number): lst.Position;
offsetAt(position: lst.Position): number;
readonly uri: string;
}
interface CommandApplication {
label?: string;
edit: lst.WorkspaceEdit;
}
interface LanguageService {
parseDocument(doc: TextDocument | string): SourceFile;
validateDocument(doc: DocumentLike, sourceFile: SourceFile): lst.Diagnostic[];
hover(doc: DocumentLike, sourceFile: SourceFile, position: lst.Position): lst.Hover | undefined;
findReferences(doc: DocumentLike, sourceFile: SourceFile, position: lst.Position, context: lst.ReferenceContext): lst.Location[];
findDefinition(doc: DocumentLike, sourceFile: SourceFile, position: lst.Position): lst.Location | undefined;
renameSymbol(doc: DocumentLike, sourceFile: SourceFile, position: lst.Position, newName: string): lst.WorkspaceEdit | undefined;
getCompletions(doc: DocumentLike, sourceFile: SourceFile, position: lst.Position): lst.CompletionItem[];
getDocumentColors(doc: DocumentLike, sourceFile: SourceFile): ColorInformation[] | undefined;
getColorRepresentations(doc: DocumentLike, sourceFile: SourceFile, color: Color$1, range: lst.Range): ColorPresentation[] | undefined;
getCodeActions(doc: DocumentLike, sourceFile: SourceFile, range: lst.Range, context: lst.CodeActionContext): lst.Command[] | undefined;
executeCommand(doc: DocumentLike, sourceFile: SourceFile, command: Omit<lst.Command, "title">): CommandApplication | undefined;
getAvailableCommands(): string[];
}
declare function createService(): LanguageService;
declare function forEachChild<TReturn>(node: SyntaxNode, cbNode: (node: SyntaxNode) => TReturn, cbNodes?: (nodes: SyntaxNodeArray<SyntaxNode>) => TReturn): TReturn | undefined;
export { type Assignment, type AssignmentSeparator, type AttributeContainer, type AttributeStatement, CharacterCodes, CheckError, type CheckErrorCode, type Color, type ColorInfo, type ColorTable, type CommandApplication, type CompassPort, type CompassPortDeclaration, DefaultScanner, DiagnosticCategory, type DiagnosticMessage, type DocumentLike, type EdgeOp, type EdgeRhs, type EdgeSourceOrTarget, type EdgeStatement, type ErrorCallback, type ErrorCode, ErrorSource, type Graph, GraphContext, type HtmlIdentifier, type ID, type IdEqualsIdStatement, type Identifier, type LanguageService, type MutableSyntaxNodeArray, type NodeId, type NodeStatement, type NormalPortDeclaration, type NumericIdentifier, type Omit, ParseError, type ParseErrorCode, Parser, ParsingContext, type PortDeclaration, type QuotedTextIdentifier, ScanError, type ScanErrorCode, type Scanner, type SourceFile, type Statement, type StatementBase, type StatementOf, type StatementSeparator, type StringLiteral, type SubGraph, type SubGraphStatement, type SymbolTable, SyntaxKind, type SyntaxNode, type SyntaxNodeArray, SyntaxNodeFlags, type TextIdentifier, type TextRange, type Token, TokenFlags, type TypeSymbol, createService, forEachChild, getTextAsToken, getTokenAsText, isIdentifier, isIdentifierNode, isIdentifierStart, isLineBreak, skipTrivia };