Skip to content

Commit

Permalink
Finished CommonTokenFactory, CommonTokenStream, DefaultErrorStrategy …
Browse files Browse the repository at this point in the history
…+ DiagnosticErrorListener

Also removed JSDoc-only imports, as the rule for unknown JSDoc types has been disabled.

Signed-off-by: Mike Lischke <[email protected]>
  • Loading branch information
mike-lischke committed Oct 31, 2023
1 parent bc07246 commit 199f888
Show file tree
Hide file tree
Showing 20 changed files with 240 additions and 254 deletions.
4 changes: 0 additions & 4 deletions src/ANTLRErrorListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import { ATNSimulator } from "./atn/ATNSimulator.js";
import { Token } from "./Token.js";
import { BitSet } from "./misc/BitSet.js";

// For jsdoc only.
import type { ANTLRErrorStrategy } from "./ANTLRErrorStrategy.js";
import type { ParserATNSimulator } from "./atn/ParserATNSimulator.js";

/** How to emit recognition errors. */
export interface ANTLRErrorListener {
/**
Expand Down
3 changes: 0 additions & 3 deletions src/BailErrorStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import { Parser } from "./Parser.js";
import { RecognitionException } from "./RecognitionException.js";
import { ParserRuleContext } from "./ParserRuleContext.js";

// For jsdoc only.
import type { ANTLRErrorStrategy } from "./ANTLRErrorStrategy.js";

/**
* This implementation of {@link ANTLRErrorStrategy} responds to syntax errors
* by immediately canceling the parse operation with a
Expand Down
4 changes: 0 additions & 4 deletions src/BufferedTokenStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import { TokenStream } from "./TokenStream.js";
import { TokenSource } from "./TokenSource.js";
import { RuleContext } from "./RuleContext.js";

// For jsdoc only.
import type { IntStream } from "./IntStream.js";
import type { CommonTokenStream } from "./CommonTokenStream.js";

/**
* This implementation of {@link TokenStream} loads tokens from a
* {@link TokenSource} on-demand, and places the tokens in a buffer to provide
Expand Down
2 changes: 1 addition & 1 deletion src/CommonToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class CommonToken extends Token {
* the same source and input stream share a reference to the same
* {@link Pair} containing these values.</p>
*/
protected source: [TokenSource | null, CharStream | null];
public source: [TokenSource | null, CharStream | null];

/**
* This is the backing field for {@link #getText} when the token text is
Expand Down
61 changes: 0 additions & 61 deletions src/CommonTokenFactory.js

This file was deleted.

77 changes: 77 additions & 0 deletions src/CommonTokenFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/

import { CharStream } from "./CharStream.js";
import { CommonToken } from "./CommonToken.js";
import { TokenFactory } from "./TokenFactory.js";
import { TokenSource } from "./TokenSource.js";

/**
* This default implementation of {@link TokenFactory} creates
* {@link CommonToken} objects.
*/
export class CommonTokenFactory extends TokenFactory<CommonToken> {
/**
* The default {@link CommonTokenFactory} instance.
*
* <p>
* This token factory does not explicitly copy token text when constructing
* tokens.</p>
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
public static readonly DEFAULT = new CommonTokenFactory();

/**
* Indicates whether {@link CommonToken#setText} should be called after
* constructing tokens to explicitly set the text. This is useful for cases
* where the input stream might not be able to provide arbitrary substrings
* of text from the input after the lexer creates a token (e.g. the
* implementation of {@link CharStream#getText} in
* {@link UnbufferedCharStream} throws an
* {@link UnsupportedOperationException}). Explicitly setting the token text
* allows {@link Token#getText} to be called at any time regardless of the
* input stream implementation.
*
* <p>
* The default value is {@code false} to avoid the performance and memory
* overhead of copying text for every token unless explicitly requested.</p>
*/
protected readonly copyText: boolean = false;

public constructor(copyText?: boolean) {
super();
/**
* Indicates whether {@link CommonToken//setText} should be called after
* constructing tokens to explicitly set the text. This is useful for cases
* where the input stream might not be able to provide arbitrary substrings
* of text from the input after the lexer creates a token (e.g. the
* implementation of {@link CharStream//getText} in
* {@link UnbufferedCharStream} throws an
* {@link UnsupportedOperationException}). Explicitly setting the token text
* allows {@link Token//getText} to be called at any time regardless of the
* input stream implementation.
*
* <p>
* The default value is {@code false} to avoid the performance and memory
* overhead of copying text for every token unless explicitly requested.</p>
*/
this.copyText = copyText ?? false;
}

public override create(source: [TokenSource | null, CharStream | null], type: number, text: string, channel: number,
start: number, stop: number, line: number, column: number): CommonToken {
const t = new CommonToken(source, type, channel, start, stop);
t.line = line;
t.column = column;
if (text !== null) {
t.text = text;
} else if (this.copyText && source[1] !== null) {
t.text = source[1].getText(start, stop);
}

return t;
}
}
13 changes: 0 additions & 13 deletions src/CommonTokenStream.d.ts

This file was deleted.

34 changes: 24 additions & 10 deletions src/CommonTokenStream.js → src/CommonTokenStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
* can be found in the LICENSE.txt file in the project root.
*/

import { Token } from './Token.js';
import { BufferedTokenStream } from './BufferedTokenStream.js';
/* eslint-disable @typescript-eslint/naming-convention */

import { Token } from "./Token.js";
import { BufferedTokenStream } from "./BufferedTokenStream.js";
import { TokenSource } from "./index.js";

/**
* This class extends {@link BufferedTokenStream} with functionality to filter
Expand All @@ -32,16 +35,25 @@ import { BufferedTokenStream } from './BufferedTokenStream.js';
* channel.</p>
*/
export class CommonTokenStream extends BufferedTokenStream {
constructor(lexer, channel) {
/**
* Specifies the channel to use for filtering tokens.
*
* <p>
* The default value is {@link Token#DEFAULT_CHANNEL}, which matches the
* default channel assigned to tokens created by the lexer.</p>
*/
protected channel = Token.DEFAULT_CHANNEL;

public constructor(lexer: TokenSource, channel?: number) {
super(lexer);
this.channel = channel === undefined ? Token.DEFAULT_CHANNEL : channel;
this.channel = channel ?? Token.DEFAULT_CHANNEL;
}

adjustSeekIndex(i) {
public override adjustSeekIndex(i: number): number {
return this.nextTokenOnChannel(i, this.channel);
}

LB(k) {
public override LB(k: number): Token | null {
if (k === 0 || this.index - k < 0) {
return null;
}
Expand All @@ -56,10 +68,11 @@ export class CommonTokenStream extends BufferedTokenStream {
if (i < 0) {
return null;
}

return this.tokens[i];
}

LT(k) {
public override LT(k: number): Token | null {
this.lazyInit();
if (k === 0) {
return null;
Expand All @@ -77,22 +90,23 @@ export class CommonTokenStream extends BufferedTokenStream {
}
n += 1;
}

return this.tokens[i];
}

// Count EOF just once.
getNumberOfOnChannelTokens() {
public getNumberOfOnChannelTokens(): number {
let n = 0;
this.fill();
for (let i = 0; i < this.tokens.length; i++) {
const t = this.tokens[i];
for (const t of this.tokens) {
if (t.channel === this.channel) {
n += 1;
}
if (t.type === Token.EOF) {
break;
}
}

return n;
}
}
36 changes: 0 additions & 36 deletions src/ConsoleErrorListener.js

This file was deleted.

40 changes: 40 additions & 0 deletions src/ConsoleErrorListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/

import { BaseErrorListener } from "./BaseErrorListener.js";
import { RecognitionException } from "./RecognitionException.js";
import { Recognizer } from "./Recognizer.js";
import { ATNSimulator } from "./atn/ATNSimulator.js";

/**
* {@inheritDoc}
*
* <p>
* This implementation prints messages to {@link System//err} containing the
* values of {@code line}, {@code charPositionInLine}, and {@code msg} using
* the following format.</p>
*
* <pre>
* line <em>line</em>:<em>charPositionInLine</em> <em>msg</em>
* </pre>
*
*/
export class ConsoleErrorListener extends BaseErrorListener {
/**
* Provides a default instance of {@link ConsoleErrorListener}.
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
public static readonly INSTANCE = new ConsoleErrorListener();

public override syntaxError<T extends ATNSimulator>(recognizer: Recognizer<T> | null,
offendingSymbol: unknown,
line: number,
charPositionInLine: number,
msg: string | null,
_e: RecognitionException | null): void {
console.error("line " + line + ":" + charPositionInLine + " " + msg);
}
}
22 changes: 0 additions & 22 deletions src/DefaultErrorStrategy.d.ts

This file was deleted.

Loading

0 comments on commit 199f888

Please sign in to comment.