mirror of
https://github.com/We-Dont-Byte/Mind_Reader.git
synced 2024-11-15 03:35:59 +00:00
Updated indention
Indention was incorrect, fixed.
This commit is contained in:
parent
bb0b1be0d4
commit
a10200f2bc
@ -1,9 +1,9 @@
|
|||||||
import { LineToken } from '.';
|
import { LineToken } from '.';
|
||||||
import { Symbol, EOFTOKEN, TabInfo } from './token';
|
import { Symbol, EOFTOKEN, TabInfo } from './token';
|
||||||
|
|
||||||
type Rule = {
|
type Rule = {
|
||||||
pattern: RegExp,
|
pattern: RegExp,
|
||||||
type: Symbol,
|
type : Symbol,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,8 +11,7 @@ type Rule = {
|
|||||||
* The first item is a recognition pattern, used to recognize the token
|
* The first item is a recognition pattern, used to recognize the token
|
||||||
* the second item is the token type
|
* the second item is the token type
|
||||||
*/
|
*/
|
||||||
const rules: Rule[] = [
|
const rules: Rule[] = [{
|
||||||
{
|
|
||||||
pattern: /^\s*def\s+(?<attr>[a-zA-Z_][a-zA-Z0-9_]*)\(/,
|
pattern: /^\s*def\s+(?<attr>[a-zA-Z_][a-zA-Z0-9_]*)\(/,
|
||||||
type: Symbol.FUNCTION
|
type: Symbol.FUNCTION
|
||||||
},
|
},
|
||||||
@ -62,8 +61,8 @@ const rules: Rule[] = [
|
|||||||
* Line-By-Line Lexer
|
* Line-By-Line Lexer
|
||||||
*/
|
*/
|
||||||
export default class Lexer {
|
export default class Lexer {
|
||||||
private textLines: string[] = []; // array of text lines
|
private textLines : string[] = []; // array of text lines
|
||||||
private pos: number = 0;
|
private pos : number = 0;
|
||||||
private _currToken: LineToken = EOFTOKEN;
|
private _currToken: LineToken = EOFTOKEN;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -93,8 +92,8 @@ export default class Lexer {
|
|||||||
* @param `text` The new text to lex.
|
* @param `text` The new text to lex.
|
||||||
*/
|
*/
|
||||||
restart(text ? : string): void {
|
restart(text ? : string): void {
|
||||||
this.pos = 0;
|
this.pos = 0;
|
||||||
this._currToken = EOFTOKEN; // if no input, already on EOFTOKEN
|
this._currToken = EOFTOKEN; // if no input, already on EOFTOKEN
|
||||||
if (text) {
|
if (text) {
|
||||||
this.textLines = text.split('\n');
|
this.textLines = text.split('\n');
|
||||||
this.next(); // advance to the first token
|
this.next(); // advance to the first token
|
||||||
@ -120,9 +119,9 @@ export default class Lexer {
|
|||||||
|
|
||||||
// Until a LineToken is found, or EOF
|
// Until a LineToken is found, or EOF
|
||||||
while (this.pos < this.textLines.length) {
|
while (this.pos < this.textLines.length) {
|
||||||
const line: string = this.textLines[this.pos];
|
const line : string = this.textLines[this.pos];
|
||||||
const indent: number = Lexer.getIndent(line, this.tabFmt!);
|
const indent: number = Lexer.getIndent(line, this.tabFmt!);
|
||||||
let token: LineToken;
|
let token : LineToken;
|
||||||
|
|
||||||
for (var r of rules) {
|
for (var r of rules) {
|
||||||
// Does line match pattern?
|
// Does line match pattern?
|
||||||
@ -131,8 +130,7 @@ export default class Lexer {
|
|||||||
// Yes...
|
// Yes...
|
||||||
if (match.groups) {
|
if (match.groups) {
|
||||||
token = new LineToken(r.type, this.pos, indent, match.groups["attr"]);
|
token = new LineToken(r.type, this.pos, indent, match.groups["attr"]);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
token = new LineToken(r.type, this.pos, indent);
|
token = new LineToken(r.type, this.pos, indent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,8 +146,7 @@ export default class Lexer {
|
|||||||
if (/^\s*(#.*)?$/.test(line)) {
|
if (/^\s*(#.*)?$/.test(line)) {
|
||||||
// "empty" line
|
// "empty" line
|
||||||
token = new LineToken(Symbol.EMPTY, this.pos, 999999);
|
token = new LineToken(Symbol.EMPTY, this.pos, 999999);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// This is an INDENT token
|
// This is an INDENT token
|
||||||
token = new LineToken(Symbol.INDENT, this.pos, indent);
|
token = new LineToken(Symbol.INDENT, this.pos, indent);
|
||||||
}
|
}
|
||||||
@ -218,8 +215,7 @@ export default class Lexer {
|
|||||||
if (tabFmt.hard) {
|
if (tabFmt.hard) {
|
||||||
// used tabs
|
// used tabs
|
||||||
indent = leadingSpace;
|
indent = leadingSpace;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// use spaces
|
// use spaces
|
||||||
indent = Math.ceil(leadingSpace / tabFmt.size!);
|
indent = Math.ceil(leadingSpace / tabFmt.size!);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user