Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add name to symbol table #728

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ export class Scope {
enumStatements: new Map(),
constStatements: new Map(),
statements: [],
symbolTable: new SymbolTable(this.symbolTable)
symbolTable: new SymbolTable(this.symbolTable, `Namespace ${loopName}`)
});
}
}
Expand Down Expand Up @@ -721,7 +721,7 @@ export class Scope {

public get symbolTable() {
return this.cache.getOrAdd('symbolTable', () => {
const result = new SymbolTable(this.getParentScope()?.symbolTable);
const result = new SymbolTable(this.getParentScope()?.symbolTable, `Scope ${this.name}`);
for (let file of this.getOwnFiles()) {
if (isBrsFile(file)) {
result.mergeSymbolTable(file.parser?.symbolTable);
Expand Down
5 changes: 4 additions & 1 deletion src/SymbolTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type { BscType } from './types/BscType';
*/
export class SymbolTable {
constructor(
parent?: SymbolTable | undefined
parent?: SymbolTable | undefined,
public name = ''
) {
this.pushParent(parent);
}
Expand Down Expand Up @@ -112,7 +113,9 @@ export class SymbolTable {
*/
private toJSON() {
return {
name: this.name,
parent: this.parent?.toJSON(),
parentStack: this.parentStack.map(p => p?.toJSON()),
symbols: [
...new Set(
[...this.symbolMap.entries()].map(([key, symbols]) => {
Expand Down
5 changes: 3 additions & 2 deletions src/bscPlugin/validation/BrsFileValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ export class BrsFileValidator {
node.parent.getSymbolTable()?.addSymbol(node.name.text, node.name.range, DynamicType.instance);
},
FunctionParameterExpression: (node) => {
node.getSymbolTable()?.addSymbol(node.name.text, node.name.range, node.type);
const paramName = node.name?.text;
node.getSymbolTable()?.addSymbol(paramName, node.name.range, node.type);
//define a symbolTable for each FunctionParameterExpression that contains `m`. TODO, add previous parameter names to this list
node.symbolTable = new SymbolTable(node.getSymbolTable());
node.symbolTable = new SymbolTable(node.getSymbolTable(), `FunctionParameterExpression ${paramName}`);
node.symbolTable.addSymbol('m', interpolatedRange, DynamicType.instance);
},
ForEachStatement: (node) => {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class FunctionExpression extends Expression implements TypedefProvider {

//if there's a body, and it doesn't have a SymbolTable, assign one
if (this.body && !this.body.symbolTable) {
this.body.symbolTable = new SymbolTable();
this.body.symbolTable = new SymbolTable(undefined, `Function Body`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ export class Parser {
//support ending the function with `end sub` OR `end function`
func.body = this.block();
//attach a new SymbolTable for this function body
func.body.symbolTable = new SymbolTable();
func.body.symbolTable = new SymbolTable(undefined, `Function Body ${name?.text ?? ''}`);
} finally {
this.currentFunctionExpression = previousFunctionExpression;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Body extends Statement implements TypedefProvider {
super();
}

public symbolTable = new SymbolTable();
public symbolTable = new SymbolTable(undefined, 'Body');

public get range() {
return util.createRangeFromPositions(
Expand Down