-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
99 additions
and
10 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,6 +1,30 @@ | ||
import { DocumentFilter, languages } from 'vscode' | ||
import { DocumentFilter, languages, workspace, TextDocumentChangeEvent, Disposable, Event, TextDocument } from 'vscode' | ||
import { EBNFRenameProvider } from './providers/RenameProvider' | ||
import { EBNFDefinitionProvider } from './providers/DefinitionProvider'; | ||
|
||
const EbnfSelector: DocumentFilter = { language: 'ebnf', scheme: 'file' }; | ||
|
||
languages.registerRenameProvider(EbnfSelector, new EBNFRenameProvider()); | ||
languages.registerRenameProvider(EbnfSelector, new EBNFRenameProvider()); | ||
languages.registerDefinitionProvider(EbnfSelector, new EBNFDefinitionProvider()); | ||
|
||
workspace.onDidOpenTextDocument(OnDocumentOpen); | ||
workspace.onDidChangeTextDocument(OnDocumentChange); | ||
workspace.onDidCloseTextDocument(OnDocumentClose); | ||
|
||
function OnDocumentOpen(document: TextDocument) { | ||
// TODO: Not implemented yet | ||
// if the file is ebnf | ||
// parse and cache AST | ||
} | ||
|
||
function OnDocumentChange(event: TextDocumentChangeEvent) { | ||
// TODO: Not implemented yet | ||
// if changes and file is ebnf | ||
// re-parse | ||
} | ||
|
||
function OnDocumentClose(document: TextDocument) { | ||
// TODO: Not implemented yet | ||
// if the file is ebnf | ||
// remove from cache | ||
} |
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,15 @@ | ||
import { Token } from 'antlr4ts'; | ||
import { SyntaxRuleContext } from '../parser/EBNFParser'; | ||
import { EBNFParserListener } from '../parser/EBNFParserListener'; | ||
|
||
export class DefinitionListener implements EBNFParserListener { | ||
public definitions: Token[] = []; | ||
|
||
exitSyntaxRule(ctx: SyntaxRuleContext) { | ||
const terminalNode = ctx.IDENTIFIER(); | ||
|
||
if (terminalNode !== undefined) { | ||
this.definitions.push(terminalNode.symbol); | ||
} | ||
} | ||
} |
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,45 @@ | ||
import { TextDocument, Position, CancellationToken, Range, ProviderResult, DefinitionProvider, TextEdit, LocationLink, Definition } from 'vscode' | ||
import { EBNFLexer } from '../parser/EBNFLexer'; | ||
import { CharStreams, CommonTokenStream } from 'antlr4ts'; | ||
import { ParseTreeListener } from 'antlr4ts/tree/ParseTreeListener'; | ||
import { EBNFParser } from '../parser/EBNFParser'; | ||
import { DefinitionListener } from '../listeners/DefinitionListener'; | ||
|
||
export class EBNFDefinitionProvider implements DefinitionProvider { | ||
public provideDefinition( | ||
document: TextDocument, | ||
position: Position, | ||
_: CancellationToken): ProviderResult<Definition | LocationLink[]> { | ||
|
||
const range = document.getWordRangeAtPosition(position) | ||
const text = document.getText(range) | ||
|
||
if (!text) { | ||
return; | ||
} | ||
|
||
const content = document.getText(); | ||
const inputStream = CharStreams.fromString(content); | ||
const lexer = new EBNFLexer(inputStream); | ||
const tokenStream = new CommonTokenStream(lexer); | ||
const parser = new EBNFParser(tokenStream); | ||
const listener = new DefinitionListener(); | ||
parser.addParseListener(listener as ParseTreeListener); | ||
|
||
parser.syntax(); | ||
|
||
const def = listener.definitions.find(d => d.text === text); | ||
|
||
var result: Definition = { | ||
uri: document.uri, | ||
range: new Range( | ||
def.line - 1, | ||
def.charPositionInLine, | ||
def.line - 1, | ||
def.charPositionInLine + def.text.length | ||
) | ||
}; | ||
|
||
return result; | ||
} | ||
} |