Skip to content

Commit a3af20b

Browse files
Reformatting
1 parent 1587bd5 commit a3af20b

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

src/parser.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {SPECIAL_IDENTIFIER_TOKENS, Token} from "./tokenizer";
4343
import {TokenType} from "./tokens";
4444
import {ExprNS, StmtNS} from "./ast-types";
4545
import {ParserErrors} from "./errors";
46+
4647
type Expr = ExprNS.Expr;
4748
type Stmt = StmtNS.Stmt;
4849

@@ -114,15 +115,16 @@ export class Parser {
114115
return false;
115116
}
116117
if (this.match(TokenType.FOR,
117-
TokenType.WHILE, TokenType.DEF,
118-
TokenType.IF, TokenType.ELIF,
119-
TokenType.ELSE, TokenType.RETURN)) {
118+
TokenType.WHILE, TokenType.DEF,
119+
TokenType.IF, TokenType.ELIF,
120+
TokenType.ELSE, TokenType.RETURN)) {
120121
return true;
121122
}
122123
this.advance();
123124
}
124125
return false;
125126
}
127+
126128
parse(): Stmt {
127129
return this.file_input();
128130
// return this.expression();
@@ -140,7 +142,7 @@ export class Parser {
140142
statements.push(this.stmt());
141143
}
142144
const endToken = this.peek();
143-
return new StmtNS.FileInput(startToken, endToken,statements, []);
145+
return new StmtNS.FileInput(startToken, endToken, statements, []);
144146
}
145147

146148
private stmt(): Stmt {
@@ -247,7 +249,7 @@ export class Parser {
247249
res = new StmtNS.NonLocal(startToken, startToken, this.advance());
248250
} else if (this.match(TokenType.ASSERT)) {
249251
res = new StmtNS.Assert(startToken, startToken, this.test());
250-
} else if (this.check(TokenType.LPAR, TokenType.NUMBER, ...SPECIAL_IDENTIFIER_TOKENS)){
252+
} else if (this.check(TokenType.LPAR, TokenType.NUMBER, ...SPECIAL_IDENTIFIER_TOKENS)) {
251253
res = new StmtNS.SimpleExpr(startToken, startToken, this.test());
252254
} else {
253255
throw new Error("Unreachable code path");
@@ -485,7 +487,7 @@ export class Parser {
485487
private atom(): Expr {
486488
const startToken = this.peek();
487489
if (this.match(TokenType.TRUE)) return new ExprNS.Literal(startToken, this.previous(), true);
488-
if (this.match(TokenType.FALSE)) return new ExprNS.Literal(startToken, this.previous(),false);
490+
if (this.match(TokenType.FALSE)) return new ExprNS.Literal(startToken, this.previous(), false);
489491

490492
if (this.match(TokenType.STRING)) {
491493
return new ExprNS.Literal(startToken, this.previous(), this.previous().lexeme);

src/tokenizer.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export class Tokenizer {
9898
private readonly indentStack: number[];
9999
private specialIdentifiers: Map<string, TokenType>;
100100
private forbiddenIdentifiers: Map<string, TokenType>;
101+
101102
// forbiddenOperators: Set<TokenType>;
102103
constructor(source: string) {
103104
this.source = source;
@@ -163,6 +164,7 @@ export class Tokenizer {
163164
const lexeme = this.source.slice(previousToken.indexInSource, this.current);
164165
this.tokens[this.tokens.length - 1] = new Token(type, lexeme, previousToken.line, previousToken.col, previousToken.indexInSource);
165166
}
167+
166168
private addToken(type: TokenType) {
167169
const line = this.line
168170
const col = this.col;
@@ -217,13 +219,13 @@ export class Tokenizer {
217219
const identifier = this.source.slice(this.start, this.current);
218220
if (!!this.forbiddenIdentifiers.get(identifier)) {
219221
throw new TokenizerErrors.ForbiddenIdentifierError(this.line, this.col,
220-
this.source, this.start);
222+
this.source, this.start);
221223
}
222224
const specialIdent = this.specialIdentifiers.get(identifier);
223225
if (specialIdent !== undefined) {
224226
/* Merge multi-token operators, like 'is not', 'not in' */
225-
const previousToken = this.tokens[this.tokens.length-1];
226-
switch(specialIdent) {
227+
const previousToken = this.tokens[this.tokens.length - 1];
228+
switch (specialIdent) {
227229
case TokenType.NOT:
228230
if (previousToken.type === TokenType.IS) {
229231
this.overwriteToken(TokenType.ISNOT);
@@ -311,7 +313,7 @@ export class Tokenizer {
311313
if (accLeadingWhiteSpace % 4 !== 0) {
312314
throw new TokenizerErrors.NonFourIndentError(this.line, this.col, this.source, this.current);
313315
}
314-
const tos = this.indentStack[this.indentStack.length-1];
316+
const tos = this.indentStack[this.indentStack.length - 1];
315317
if (accLeadingWhiteSpace > tos) {
316318
this.indentStack.push(accLeadingWhiteSpace);
317319
const indents = Math.floor((accLeadingWhiteSpace - tos) / 4);
@@ -396,7 +398,7 @@ export class Tokenizer {
396398
if (this.matches('=')) {
397399
this.raiseForbiddenOperator();
398400
}
399-
this.addToken( TokenType.PERCENT);
401+
this.addToken(TokenType.PERCENT);
400402
break;
401403
case '!':
402404
this.addToken(this.matches('=') ? TokenType.NOTEQUAL : TokenType.BANG);
@@ -422,7 +424,7 @@ export class Tokenizer {
422424
}
423425

424426
private matchForbiddenOperator(ch: string) {
425-
switch(ch) {
427+
switch (ch) {
426428
case '@':
427429
case '|':
428430
case '&':
@@ -442,7 +444,7 @@ export class Tokenizer {
442444
this.scanToken();
443445
}
444446
// Unravel the indent stack
445-
while(this.indentStack[this.indentStack.length-1] !== 0) {
447+
while (this.indentStack[this.indentStack.length - 1] !== 0) {
446448
this.indentStack.pop();
447449
this.addToken(TokenType.DEDENT);
448450
}
@@ -456,6 +458,7 @@ export class Tokenizer {
456458
${TokenType[token.type]}\t\t\t'${token.lexeme}'`);
457459
}
458460
}
461+
459462
private raiseForbiddenOperator() {
460463
throw new TokenizerErrors.ForbiddenOperatorError(this.line, this.col, this.source, this.start, this.current);
461464
}

src/translator.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* */
44

55
import {StmtNS, ExprNS} from "./ast-types";
6+
67
type Expr = ExprNS.Expr;
78
type Stmt = StmtNS.Stmt;
89
import {Token} from "./tokenizer";
@@ -52,6 +53,7 @@ export interface EstreeLocation {
5253

5354
export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<BaseNode> {
5455
private readonly source: string
56+
5557
constructor(source: string) {
5658
this.source = source;
5759
}
@@ -70,6 +72,7 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
7072
const source: string = token.lexeme;
7173
return {source, start, end};
7274
}
75+
7376
private toEstreeLocation(stmt: Stmt | Expr): EstreeLocation {
7477
const start: EstreePosition = {
7578
// Convert zero-based to one-based.
@@ -94,13 +97,15 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
9497
resolveStmt(stmt: Stmt) {
9598
return stmt.accept(this);
9699
}
97-
resolveManyStmt(stmts: Stmt[]): Statement[]{
100+
101+
resolveManyStmt(stmts: Stmt[]): Statement[] {
98102
const res = [];
99103
for (const stmt of stmts) {
100104
res.push(this.resolveStmt(stmt))
101105
}
102106
return res;
103107
}
108+
104109
resolveExpr(expr: Expr) {
105110
return expr.accept(this);
106111
}
@@ -122,6 +127,7 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
122127
loc: this.toEstreeLocation(stmtOrExpr),
123128
};
124129
}
130+
125131
// Token to estree identifier.
126132
private convertToIdentifier(name: Token): Identifier {
127133
return {
@@ -270,25 +276,29 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
270276
loc: this.toEstreeLocation(stmt),
271277
};
272278
}
279+
273280
visitGlobalStmt(stmt: StmtNS.Global): EmptyStatement {
274281
return {
275282
type: 'EmptyStatement',
276283
loc: this.toEstreeLocation(stmt),
277284
};
278285
}
286+
279287
visitNonLocalStmt(stmt: StmtNS.NonLocal): EmptyStatement {
280288
return {
281289
type: 'EmptyStatement',
282290
loc: this.toEstreeLocation(stmt),
283291
};
284292
}
293+
285294
visitReturnStmt(stmt: StmtNS.Return): ReturnStatement {
286295
return {
287296
type: 'ReturnStatement',
288297
argument: stmt.value == null ? null : this.resolveExpr(stmt.value),
289298
loc: this.toEstreeLocation(stmt),
290299
};
291300
}
301+
292302
visitWhileStmt(stmt: StmtNS.While): WhileStatement {
293303
return {
294304
type: 'WhileStatement',
@@ -297,13 +307,15 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
297307
loc: this.toEstreeLocation(stmt),
298308
}
299309
}
310+
300311
visitSimpleExprStmt(stmt: StmtNS.SimpleExpr): ExpressionStatement {
301312
return {
302313
type: 'ExpressionStatement',
303314
expression: this.resolveExpr(stmt.expression),
304315
loc: this.toEstreeLocation(stmt),
305316
}
306317
}
318+
307319
// @TODO
308320
visitFromImportStmt(stmt: StmtNS.FromImport): ImportDeclaration {
309321
const specifiers: ImportSpecifier[] = stmt.names.map(name => {
@@ -324,12 +336,14 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
324336
}
325337
}
326338
}
339+
327340
visitContinueStmt(stmt: StmtNS.Continue): ContinueStatement {
328341
return {
329342
type: 'ContinueStatement',
330343
loc: this.toEstreeLocation(stmt),
331344
}
332345
}
346+
333347
visitBreakStmt(stmt: StmtNS.Break): BreakStatement {
334348
return {
335349
type: 'BreakStatement',
@@ -359,17 +373,19 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
359373
loc: this.toEstreeLocation(expr),
360374
}
361375
}
376+
362377
// disabled for now
363378
visitMultiLambdaExpr(expr: ExprNS.MultiLambda): EmptyStatement {
364379
return {
365380
type: 'EmptyStatement',
366381
loc: this.toEstreeLocation(expr),
367382
}
368383
}
384+
369385
visitUnaryExpr(expr: ExprNS.Unary): UnaryExpression {
370386
const op = expr.operator.type;
371387
let res: UnaryOperator = '-';
372-
switch(op) {
388+
switch (op) {
373389
case TokenType.NOT:
374390
res = '!'
375391
break;
@@ -391,14 +407,16 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
391407
loc: this.toEstreeLocation(expr),
392408
}
393409
}
410+
394411
visitGroupingExpr(expr: ExprNS.Grouping): Expression {
395412
return this.resolveExpr(expr.expression);
396413
}
414+
397415
visitBinaryExpr(expr: ExprNS.Binary): BinaryExpression {
398416
const op = expr.operator.type;
399417
let res: BinaryOperator = '+';
400418
// To make the type checker happy.
401-
switch(op) {
419+
switch (op) {
402420
case TokenType.PLUS:
403421
res = '+';
404422
break;
@@ -429,11 +447,12 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
429447
loc: this.toEstreeLocation(expr),
430448
}
431449
}
450+
432451
visitCompareExpr(expr: ExprNS.Compare): BinaryExpression {
433452
const op = expr.operator.type;
434453
let res: BinaryOperator = '+';
435454
// To make the type checker happy.
436-
switch(op) {
455+
switch (op) {
437456
case TokenType.LESS:
438457
res = '<';
439458
break;
@@ -469,11 +488,12 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
469488
loc: this.toEstreeLocation(expr),
470489
}
471490
}
491+
472492
visitBoolOpExpr(expr: ExprNS.BoolOp): LogicalExpression {
473493
const op = expr.operator.type;
474494
let res: LogicalOperator = '||';
475495
// To make the type checker happy.
476-
switch(op) {
496+
switch (op) {
477497
case TokenType.AND:
478498
res = '&&';
479499
break;

0 commit comments

Comments
 (0)