mirror of
https://github.com/We-Dont-Byte/Mind_Reader.git
synced 2024-11-15 03:35:59 +00:00
Update parser
Implied nodes caused ambiguity when querying for context. Now every line is considered, even blank lines, to make the process much easier.
This commit is contained in:
parent
6904b5dacf
commit
dc1565f4d0
@ -46,7 +46,13 @@
|
|||||||
{
|
{
|
||||||
"command": "mind-reader.selectTheme",
|
"command": "mind-reader.selectTheme",
|
||||||
"title": "Select Theme"
|
"title": "Select Theme"
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"command": "mind-reader.runLineContext",
|
||||||
|
"title": "Run Line Context"
|
||||||
}
|
}
|
||||||
|
|
||||||
],
|
],
|
||||||
"keybindings": [
|
"keybindings": [
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
|
import * as pl from './pylex';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Object} Command // Command to register with the VS Code Extension API
|
* @type {Object} Command // Command to register with the VS Code Extension API
|
||||||
@ -48,6 +49,11 @@ const commands: Command[] = [
|
|||||||
name: 'mind-reader.resetEditorScale',
|
name: 'mind-reader.resetEditorScale',
|
||||||
callback: resetEditorScale,
|
callback: resetEditorScale,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: 'mind-reader.runLineContext',
|
||||||
|
callback: runLineContext,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
// COMMAND CALLBACK IMPLEMENTATIONS
|
// COMMAND CALLBACK IMPLEMENTATIONS
|
||||||
@ -76,4 +82,57 @@ function resetEditorScale(): void {
|
|||||||
vscode.commands.executeCommand('workbench.action.zoomReset');
|
vscode.commands.executeCommand('workbench.action.zoomReset');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function runLineContext(): void {
|
||||||
|
let editor = vscode.window.activeTextEditor;
|
||||||
|
if (editor) {
|
||||||
|
// current text and line number
|
||||||
|
let editorText = editor.document.getText();
|
||||||
|
let line = editor.selection.active.line;
|
||||||
|
|
||||||
|
// get tab info settings
|
||||||
|
let size = parseInt(editor.options.tabSize as string);
|
||||||
|
let hard = !editor.options.insertSpaces;
|
||||||
|
|
||||||
|
// initialize parser
|
||||||
|
let parser = new pl.Parser(editorText, {size, hard});
|
||||||
|
parser.parse();
|
||||||
|
|
||||||
|
let context = parser.context(line);
|
||||||
|
|
||||||
|
// build text
|
||||||
|
let contentString = createContextString(context, line);
|
||||||
|
vscode.window.showInformationMessage(contentString);
|
||||||
|
} else {
|
||||||
|
vscode.window.showErrorMessage('No document currently active');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createContextString(context: pl.LexNode[], line: number): string {
|
||||||
|
if (context.length < 1) {
|
||||||
|
throw new Error('Cannot create context string for empty context');
|
||||||
|
}
|
||||||
|
|
||||||
|
let contextString = 'Line ' + (line+1); // 1 based
|
||||||
|
if (context[0].token && context[0].token.attr) {
|
||||||
|
contextString += ': ' + context[0].token.type.toString() + ' ' + context[0].token.attr.toString();
|
||||||
|
}
|
||||||
|
for (let i = 1; i < context.length; i++) {
|
||||||
|
let node = context[i];
|
||||||
|
if (node.label === 'root') {
|
||||||
|
// root
|
||||||
|
contextString += ' in the Document Root';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.token!.type != pl.PylexSymbol.EMPTY &&
|
||||||
|
node.token!.type != pl.PylexSymbol.INDENT) {
|
||||||
|
contextString += ' inside ' + node.token!.type.toString();
|
||||||
|
if (node.token!.attr) {
|
||||||
|
contextString += ' ' + node.token!.attr.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return contextString;
|
||||||
|
}
|
||||||
|
|
||||||
export default commands;
|
export default commands;
|
||||||
|
@ -4,4 +4,4 @@ export {default as LineToken} from './token';
|
|||||||
export {default as Lexer} from './lexer';
|
export {default as Lexer} from './lexer';
|
||||||
export {default as LexNode} from './node';
|
export {default as LexNode} from './node';
|
||||||
export {TabInfo as TabInfo} from './token';
|
export {TabInfo as TabInfo} from './token';
|
||||||
|
export {Symbol as PylexSymbol} from './token';
|
||||||
|
@ -159,14 +159,15 @@ export default class Lexer {
|
|||||||
}
|
}
|
||||||
// No rules matched
|
// No rules matched
|
||||||
|
|
||||||
// Skip this line if it is whitespace, comment, or empty
|
// TODO: move to rules
|
||||||
if (/^\s*(#.*)?$/.test(line)) {
|
if (/^\s*(#.*)?$/.test(line)) {
|
||||||
this.pos++;
|
// "empty" line
|
||||||
continue;
|
token = new LineToken(Symbol.EMPTY, this.pos, 999999);
|
||||||
|
} else {
|
||||||
|
// This is an INDENT token
|
||||||
|
token = new LineToken(Symbol.INDENT, this.pos, indent);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is an INDENT token
|
|
||||||
token = new LineToken(Symbol.INDENT, this.pos, indent);
|
|
||||||
this._currToken = token;
|
this._currToken = token;
|
||||||
this.pos++;
|
this.pos++;
|
||||||
return this.currToken();
|
return this.currToken();
|
||||||
|
@ -2,6 +2,8 @@ import * as vscode from 'vscode';
|
|||||||
|
|
||||||
import LineToken from './token';
|
import LineToken from './token';
|
||||||
|
|
||||||
|
/* TODO: make accessing children and parent less tedious */
|
||||||
|
/* TODO: 'root.children()![i])' */
|
||||||
/**
|
/**
|
||||||
* A node in a Parse tree.
|
* A node in a Parse tree.
|
||||||
*/
|
*/
|
||||||
|
@ -3,6 +3,7 @@ import * as vscode from 'vscode';
|
|||||||
import { EOFTOKEN, Symbol, TabInfo } from './token';
|
import { EOFTOKEN, Symbol, TabInfo } from './token';
|
||||||
import Lexer from './lexer';
|
import Lexer from './lexer';
|
||||||
import LexNode from './node';
|
import LexNode from './node';
|
||||||
|
/* TODO: update design doc */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A parse tree generator
|
* A parse tree generator
|
||||||
@ -71,8 +72,20 @@ export default class Parser {
|
|||||||
// go up 1 level of recursion at a time to unravel properly
|
// go up 1 level of recursion at a time to unravel properly
|
||||||
this.currIndent--;
|
this.currIndent--;
|
||||||
return children;
|
return children;
|
||||||
} else if (this.lexer.currToken().type === Symbol.INDENT) {
|
}
|
||||||
|
|
||||||
|
if (this.lexer.currToken().type === Symbol.INDENT ||
|
||||||
|
this.lexer.currToken().type === Symbol.EMPTY) {
|
||||||
|
const label = this.lexer.currToken().type;
|
||||||
// regular code, advance and stay in same block
|
// regular code, advance and stay in same block
|
||||||
|
children.push(new LexNode(
|
||||||
|
label,
|
||||||
|
vscode.TreeItemCollapsibleState.None,
|
||||||
|
this.lexer.currToken(),
|
||||||
|
null,
|
||||||
|
parent)
|
||||||
|
);
|
||||||
|
|
||||||
this.lexer.next();
|
this.lexer.next();
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
@ -105,29 +118,28 @@ export default class Parser {
|
|||||||
* @param `lineNumber` The line number to query context for.
|
* @param `lineNumber` The line number to query context for.
|
||||||
* @return An array of LexNodes for the root path containing `lineNumber`
|
* @return An array of LexNodes for the root path containing `lineNumber`
|
||||||
*/
|
*/
|
||||||
context(lineNumber: number): LexNode[] {
|
context(lineNumber: number, root?: LexNode): LexNode[] {
|
||||||
if (!this.root.children()) {
|
if (!root) {
|
||||||
return [];
|
root = this.root
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the LexNode that is the parent
|
// is this the target?
|
||||||
// of the queried line number
|
if (root.token && root.token!.linenr === lineNumber) {
|
||||||
let find = (root: LexNode): LexNode | undefined => {
|
// match
|
||||||
let prevChild: LexNode;
|
return root.rootPath();
|
||||||
for (var child of root.children()!) {
|
}
|
||||||
if (lineNumber < child.token!.linenr) {
|
|
||||||
if (prevChild!.children()) {
|
if (root.children()) {
|
||||||
return find(prevChild!);
|
// recursive call
|
||||||
} else {
|
for (let i = 0; i < root.children()!.length; i++) {
|
||||||
return prevChild!;
|
let ctx = this.context(lineNumber, root.children()![i]);
|
||||||
}
|
if (ctx.length > 0) {
|
||||||
} else {
|
// a rootpath was returned
|
||||||
prevChild = child;
|
return ctx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
// no matches
|
||||||
let target = find(this.root);
|
return [];
|
||||||
return target!.rootPath();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ export enum Symbol {
|
|||||||
FINALLY = "finally",
|
FINALLY = "finally",
|
||||||
WITH = "with",
|
WITH = "with",
|
||||||
INDENT = "INDENT", // Indent token, default if not EOF, only contains indent information
|
INDENT = "INDENT", // Indent token, default if not EOF, only contains indent information
|
||||||
|
EMPTY = "EMPTY", // empty line, used only to associate with the previous line
|
||||||
EOF = "EOF"
|
EOF = "EOF"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user