Skip to content

Commit

Permalink
Fixed a number of linter issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-lischke committed Oct 19, 2024
1 parent 5e9c22e commit e5f3ef3
Show file tree
Hide file tree
Showing 99 changed files with 908 additions and 1,098 deletions.
7 changes: 7 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default tslint.config(
"code": 120
}
],
"brace-style": ["error", "1tbs", { "allowSingleLine": false }],
"@stylistic/padding-line-between-statements": [
"error",
{
Expand Down Expand Up @@ -236,6 +237,12 @@ export default tslint.config(
// Have to switch this off, as it is not good enough to be used.
"off"
],
"jsdoc/no-undefined-types": [
"off", // Requires a comment syntax incompatible with VS Code.
{
"markVariablesAsUsed": false
}
],
"prefer-arrow/prefer-arrow-functions": [
"warn",
{
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "antlr-ng",
"version": "0.1.0",
"version": "0.2.0",
"description": "Next generation ANTLR Tool",
"type": "module",
"author": "Mike Lischke",
Expand Down Expand Up @@ -44,7 +44,7 @@
"typescript-eslint": "8.7.0"
},
"scripts": {
"run": "node --no-warnings --loader ts-node/esm src/runner.ts",
"run": "node --no-warnings --loader ts-node/esm src/runner.ts --version",
"lint": "eslint \"./src/**/*.ts\"",
"lint:fix": "eslint \"./src/**/*.ts\" --fix",
"test": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js --no-coverage",
Expand Down
16 changes: 6 additions & 10 deletions src/Tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ import { GrammarRootAST } from "./tool/ast/GrammarRootAST.js";
import { RuleAST } from "./tool/ast/RuleAST.js";

export class Tool {
public static readonly VERSION = "10.0.0";

public static readonly GRAMMAR_EXTENSION = ".g4";
public static readonly LEGACY_GRAMMAR_EXTENSION = ".g";

Expand Down Expand Up @@ -275,12 +273,12 @@ export class Tool {
const ruleToAST = new Map<string, RuleAST>();
for (const r of rules) {
const ruleAST = r as RuleAST;
const ID = ruleAST.getChild(0) as GrammarAST;
const ruleName = ID.getText()!;
const id = ruleAST.getChild(0) as GrammarAST;
const ruleName = id.getText()!;
const prev = ruleToAST.get(ruleName);
if (prev) {
const prevChild = prev.getChild(0) as GrammarAST;
g.tool.errMgr.grammarError(ErrorType.RULE_REDEFINITION, g.fileName, ID.getToken(), ruleName,
g.tool.errMgr.grammarError(ErrorType.RULE_REDEFINITION, g.fileName, id.getToken(), ruleName,
prevChild.getToken()!.line);
redefinition = true;
continue;
Expand Down Expand Up @@ -624,15 +622,13 @@ export class Tool {
}
}

public version(): void {
this.info("ANTLR Parser Generator Version " + Tool.VERSION);
}

public exit(e: number): void {
process.exit(e);
}

public panic(): void { throw new Error("ANTLR panic"); }
public panic(): void {
throw new Error("ANTLR panic");
}

protected writeDOTFile(g: Grammar, rulOrName: Rule | string, dot: string): void {
const name = rulOrName instanceof Rule ? rulOrName.g.name + "." + rulOrName.name : rulOrName;
Expand Down
6 changes: 3 additions & 3 deletions src/analysis/LeftRecursiveRuleTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class LeftRecursiveRuleTransformer {
}

// replace old rule's AST; first create text of altered rule
const RULES = ast.getFirstChildWithType(ANTLRv4Parser.RULE_REF) as GrammarAST;
const rules = ast.getFirstChildWithType(ANTLRv4Parser.RULE_REF) as GrammarAST;
const newRuleText = leftRecursiveRuleWalker.getArtificialOpPrecRule();

// now parse within the context of the grammar that originally created
Expand All @@ -134,7 +134,7 @@ export class LeftRecursiveRuleTransformer {
(t.getChild(0) as GrammarAST).token = (prevRuleAST.getChild(0) as GrammarAST).getToken();

// update grammar AST and set rule's AST.
RULES.setChild(prevRuleAST.getChildIndex(), t);
rules.setChild(prevRuleAST.getChildIndex(), t);
r.ast = t;

// Reduce sets in newly created rule tree
Expand Down Expand Up @@ -180,7 +180,7 @@ export class LeftRecursiveRuleTransformer {
const arg = r.ast.getFirstChildWithType(ANTLRv4Parser.BEGIN_ARGUMENT) as ActionAST | null;
if (arg !== null) {
r.args = ScopeParser.parseTypedArgList(arg, arg.getText()!, this.g);
r.args.type = DictType.ARG;
r.args.type = DictType.Argument;
r.args.ast = arg;
arg.resolver = r.alt[1]; // todo: isn't this Rule or something?
}
Expand Down
4 changes: 1 addition & 3 deletions src/antlr3/BaseRecognizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,7 @@ export abstract class BaseRecognizer {
console.log(" backtracking=" + this.state.backtracking);
if (this.state.failed) {
console.log(" failed");
}

else {
} else {
console.log(" succeeded");
}

Expand Down
3 changes: 1 addition & 2 deletions src/antlr3/tree/CommonErrorNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export class CommonErrorNode extends CommonTree {
public stop: Token;
public trappedException: RecognitionException;

public constructor(input: TokenStream, start: Token, stop: Token,
e: RecognitionException) {
public constructor(input: TokenStream, start: Token, stop: Token | null, e: RecognitionException) {
super();

if (stop === null ||
Expand Down
9 changes: 2 additions & 7 deletions src/antlr3/tree/CommonTreeNodeStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class CommonTreeNodeStream extends LookaheadStream<Tree>
return this.adaptor.getType(o) === Token.EOF;
}

public setUniqueNavigationNodes(uniqueNavigationNodes: boolean): void { }
public setUniqueNavigationNodes(uniqueNavigationNodes: boolean): void { /**/ }

public getTreeSource(): unknown {
return this.root;
Expand Down Expand Up @@ -166,9 +166,6 @@ export class CommonTreeNodeStream extends LookaheadStream<Tree>
* Switch back with pop().
*/
public push(index: number): void {
if (this.calls === null) {
this.calls = [];
}
this.calls.push(this.p); // save current index
this.seek(index);
}
Expand Down Expand Up @@ -228,9 +225,7 @@ export class CommonTreeNodeStream extends LookaheadStream<Tree>
// TREE REWRITE INTERFACE

public replaceChildren(parent: Tree, startChildIndex: number, stopChildIndex: number, t: Tree): void {
if (parent !== null) {
this.adaptor.replaceChildren(parent, startChildIndex, stopChildIndex, t);
}
this.adaptor.replaceChildren(parent, startChildIndex, stopChildIndex, t);
}

/*public toString(start: Tree, stop: Tree): string {
Expand Down
6 changes: 3 additions & 3 deletions src/antlr3/tree/TreeIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class TreeIterator {
public down: Tree;
public eof: Tree;
protected adaptor: TreeAdaptor;
protected root: Tree;
protected root: Tree | null;
protected tree: Tree | null;
protected firstTime = true;

Expand Down Expand Up @@ -71,7 +71,7 @@ export class TreeIterator {
return this.root !== null;
}

if (this.nodes !== null && this.nodes.size > 0) {
if (this.nodes.size > 0) {
return true;
}

Expand All @@ -98,7 +98,7 @@ export class TreeIterator {
return this.tree;
}
// if any queued up, use those first
if (this.nodes !== null && this.nodes.size > 0) {
if (this.nodes.size > 0) {
return this.nodes.remove();
}

Expand Down
1 change: 1 addition & 0 deletions src/antlr3/tree/TreeNodeStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface TreeNodeStream extends IntStream {
* instead of a {@link Token}. Makes code generation identical for both
* parser and tree grammars.</p>
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
LT(k: number): Tree | null;

/**
Expand Down
3 changes: 1 addition & 2 deletions src/antlr3/tree/TreePatternParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ export class TreePatternParser {
if (subtree) {
this.adaptor.addChild(root, subtree);
}
}
else {
} else {
const child = this.parseNode();
if (child === null) {
return null;
Expand Down
38 changes: 26 additions & 12 deletions src/antlr3/tree/TreeRewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type { TreeRuleReturnScope } from "./TreeRuleReturnScope.js";

// cspell: disable

interface fptr {
interface Fptr {
rule(): unknown;
}

Expand All @@ -32,14 +32,18 @@ export class TreeRewriter extends TreeParser {
protected originalTokenStream: TokenStream;
protected originalAdaptor: TreeAdaptor;

private topdown_fptr = new class implements fptr {
private topdown_fptr = new class implements Fptr {
public constructor(private $outer: TreeRewriter) { }
public rule(): unknown { return this.$outer.topdown(); }
public rule(): unknown {
return this.$outer.topdown();
}
}(this);

private bottomup_ftpr = new class implements fptr {
private bottomup_ftpr = new class implements Fptr {
public constructor(private $outer: TreeRewriter) { }
public rule(): unknown { return this.$outer.bottomup(); }
public rule(): unknown {
return this.$outer.bottomup();
}
}(this);

public constructor(input: TreeNodeStream, state?: RecognizerSharedState) {
Expand All @@ -49,7 +53,7 @@ export class TreeRewriter extends TreeParser {
this.originalTokenStream = input.getTokenStream();
}

public applyOnce(t: Tree | null, whichRule: fptr): Tree | null {
public applyOnce(t: Tree | null, whichRule: Fptr): Tree | null {
if (t === null) {
return null;
}
Expand Down Expand Up @@ -77,15 +81,17 @@ export class TreeRewriter extends TreeParser {
return t;
}
} catch (e) {
if (e instanceof RecognitionException) { ; } else {
if (e instanceof RecognitionException) {
;
} else {
throw e;
}
}

return t;
}

public applyRepeatedly(t: Tree | null, whichRule: fptr): Tree | null {
public applyRepeatedly(t: Tree | null, whichRule: Fptr): Tree | null {
let treeChanged = true;
while (treeChanged) {
const u = this.applyOnce(t, whichRule);
Expand All @@ -102,8 +108,12 @@ export class TreeRewriter extends TreeParser {
const v = new TreeVisitor(new CommonTreeAdaptor());
const actions = new class implements TreeVisitorAction<Tree> {
public constructor(private $outer: TreeRewriter) { }
public pre(t: Tree): Tree | null { return this.$outer.applyOnce(t, this.$outer.topdown_fptr); }
public post(t: Tree): Tree | null { return this.$outer.applyRepeatedly(t, this.$outer.bottomup_ftpr); }
public pre(t: Tree): Tree | null {
return this.$outer.applyOnce(t, this.$outer.topdown_fptr);
}
public post(t: Tree): Tree | null {
return this.$outer.applyRepeatedly(t, this.$outer.bottomup_ftpr);
}
}(this);
t = v.visit(t, actions);

Expand All @@ -121,6 +131,10 @@ export class TreeRewriter extends TreeParser {
// methods the downup strategy uses to do the up and down rules.
// to override, just define tree grammar rule topdown and turn on
// filter=true.
public topdown(): Tree | null { return null; }
public bottomup(): Tree | null { return null; }
public topdown(): Tree | null {
return null;
}
public bottomup(): Tree | null {
return null;
}
}
4 changes: 3 additions & 1 deletion src/antlr3/tree/TreeRuleReturnScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ export class TreeRuleReturnScope {
* Has a value potentially if output=template; Don't use StringTemplate
* type as it then causes a dependency with ST lib.
*/
public getTemplate(): unknown { return null; }
public getTemplate(): unknown {
return null;
}
}
3 changes: 1 addition & 2 deletions src/antlr3/tree/TreeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ export class TreeVisitor {
*
* Return result of applying post action to this node.
*/
public visit<T extends Tree>(t: T | null, action: TreeVisitorAction<T>): T | null {
// System.out.println("visit "+((Tree)t).toStringTree());
public visit<T extends Tree>(t: T | null, action: TreeVisitorAction<T> | null): T | null {
const isNil = this.adaptor.isNil(t);
if (action !== null && !isNil) {
t = action.pre(t); // if rewritten, walk children of new t
Expand Down
13 changes: 7 additions & 6 deletions src/automata/ATNVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import { ATNState } from "antlr4ng";
*/
export class ATNVisitor {
public visit(s: ATNState): void {
this.visit_(s, new Set<number>());
this.doVisit(s, new Set<number>());
}

public visit_(s: ATNState, visited: Set<number>): void {
public visitState(s: ATNState): void {
// intentionally empty
}

private doVisit(s: ATNState, visited: Set<number>): void {
if (visited.has(s.stateNumber)) {
return;
}
Expand All @@ -25,11 +29,8 @@ export class ATNVisitor {

this.visitState(s);
for (const t of s.transitions) {
this.visit_(t.target, visited);
this.doVisit(t.target, visited);
}
}

public visitState(s: ATNState): void {
// intentionally empty
}
}
Loading

0 comments on commit e5f3ef3

Please sign in to comment.