Skip to content

Commit

Permalink
apply getTokenKind, add is(TokenKind[]): boolean overload
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Aug 2, 2024
1 parent 2975ca1 commit cf5c694
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 35 deletions.
10 changes: 7 additions & 3 deletions src/parser/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ export class Scanner implements ITokenStream {
/**
* カーソル位置にあるトークンの種類が指定したトークンの種類と一致するかどうかを示す値を取得します。
*/
public is(kind: TokenKind): boolean {
return this.getToken().kind === kind;
public is(kind: TokenKind | TokenKind[]): boolean {
if (Array.isArray(kind)) {
return kind.includes(this.getTokenKind());
} else {
return this.getTokenKind() === kind;
}
}

/**
Expand Down Expand Up @@ -96,7 +100,7 @@ export class Scanner implements ITokenStream {
*/
public expect(kind: TokenKind): void {
if (!this.is(kind)) {
throw new AiScriptSyntaxError(`unexpected token: ${TokenKind[this.getToken().kind]}`, this.getPos());
throw new AiScriptSyntaxError(`unexpected token: ${TokenKind[this.getTokenKind()]}`, this.getPos());
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/parser/streams/token-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface ITokenStream {
/**
* カーソル位置にあるトークンの種類が指定したトークンの種類と一致するかどうかを示す値を取得します。
*/
is(kind: TokenKind): boolean;
is(kind: TokenKind | TokenKind[]): boolean;

/**
* カーソル位置にあるトークンの種類を取得します。
Expand Down Expand Up @@ -79,8 +79,12 @@ export class TokenStream implements ITokenStream {
/**
* カーソル位置にあるトークンの種類が指定したトークンの種類と一致するかどうかを示す値を取得します。
*/
public is(kind: TokenKind): boolean {
return this.getToken().kind === kind;
public is(kind: TokenKind | TokenKind[]): boolean {
if (Array.isArray(kind)) {
return kind.includes(this.getTokenKind());
} else {
return this.getTokenKind() === kind;
}
}

/**
Expand Down Expand Up @@ -131,7 +135,7 @@ export class TokenStream implements ITokenStream {
*/
public expect(kind: TokenKind): void {
if (!this.is(kind)) {
throw new AiScriptSyntaxError(`unexpected token: ${TokenKind[this.getToken().kind]}`, this.getPos());
throw new AiScriptSyntaxError(`unexpected token: ${TokenKind[this.getTokenKind()]}`, this.getPos());
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/parser/syntaxes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function parseParams(s: ITokenStream): { name: string, argType?: Ast.Node
items.push({ name, optional, default: defaultExpr, argType: type });

// separator
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.NewLine: {
s.next();
break;
Expand Down Expand Up @@ -90,10 +90,10 @@ export function parseBlock(s: ITokenStream): Ast.Node[] {
steps.push(parseStatement(s));

// terminator
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.NewLine:
case TokenKind.SemiColon: {
while ([TokenKind.NewLine, TokenKind.SemiColon].includes(s.getToken().kind)) {
while ([TokenKind.NewLine, TokenKind.SemiColon].includes(s.getTokenKind())) {
s.next();
}
break;
Expand Down Expand Up @@ -140,7 +140,7 @@ function parseFnType(s: ITokenStream): Ast.Node {
const params: Ast.Node[] = [];
while (!s.is(TokenKind.CloseParen)) {
if (params.length > 0) {
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.Comma: {
s.next();
break;
Expand Down
24 changes: 12 additions & 12 deletions src/parser/syntaxes/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const operators: OpInfo[] = [

function parsePrefix(s: ITokenStream, minBp: number): Ast.Node {
const startPos = s.getPos();
const op = s.getToken().kind;
const op = s.getTokenKind();
s.next();

// 改行のエスケープ
Expand Down Expand Up @@ -99,7 +99,7 @@ function parsePrefix(s: ITokenStream, minBp: number): Ast.Node {

function parseInfix(s: ITokenStream, left: Ast.Node, minBp: number): Ast.Node {
const startPos = s.getPos();
const op = s.getToken().kind;
const op = s.getTokenKind();
s.next();

// 改行のエスケープ
Expand Down Expand Up @@ -174,7 +174,7 @@ function parseInfix(s: ITokenStream, left: Ast.Node, minBp: number): Ast.Node {

function parsePostfix(s: ITokenStream, expr: Ast.Node): Ast.Node {
const startPos = s.getPos();
const op = s.getToken().kind;
const op = s.getTokenKind();

switch (op) {
case TokenKind.OpenParen: {
Expand All @@ -200,7 +200,7 @@ function parsePostfix(s: ITokenStream, expr: Ast.Node): Ast.Node {
function parseAtom(s: ITokenStream, isStatic: boolean): Ast.Node {
const startPos = s.getPos();

switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.IfKeyword: {
if (isStatic) break;
return parseIf(s);
Expand Down Expand Up @@ -290,7 +290,7 @@ function parseAtom(s: ITokenStream, isStatic: boolean): Ast.Node {
return expr;
}
}
throw new AiScriptSyntaxError(`unexpected token: ${TokenKind[s.getToken().kind]}`, startPos);
throw new AiScriptSyntaxError(`unexpected token: ${TokenKind[s.getTokenKind()]}`, startPos);
}

/**
Expand All @@ -311,7 +311,7 @@ function parseCall(s: ITokenStream, target: Ast.Node): Ast.Node {
items.push(parseExpr(s, false));

// separator
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.NewLine: {
s.next();
break;
Expand Down Expand Up @@ -433,7 +433,7 @@ function parseMatch(s: ITokenStream): Ast.Node {
qs.push({ q, a });

// separator
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.NewLine: {
s.next();
break;
Expand Down Expand Up @@ -463,7 +463,7 @@ function parseMatch(s: ITokenStream): Ast.Node {
x = parseBlockOrStatement(s);

// separator
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.NewLine: {
s.next();
break;
Expand Down Expand Up @@ -579,7 +579,7 @@ function parseObject(s: ITokenStream, isStatic: boolean): Ast.Node {
map.set(k, v);

// separator
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.NewLine:
case TokenKind.Comma: {
s.next();
Expand Down Expand Up @@ -623,7 +623,7 @@ function parseArray(s: ITokenStream, isStatic: boolean): Ast.Node {
value.push(parseExpr(s, isStatic));

// separator
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.NewLine:
case TokenKind.Comma: {
s.next();
Expand Down Expand Up @@ -660,7 +660,7 @@ function parsePratt(s: ITokenStream, minBp: number): Ast.Node {

let left: Ast.Node;

const tokenKind = s.getToken().kind;
const tokenKind = s.getTokenKind();
const prefix = operators.find((x): x is PrefixInfo => x.opKind === 'prefix' && x.kind === tokenKind);
if (prefix != null) {
left = parsePrefix(s, prefix.bp);
Expand All @@ -676,7 +676,7 @@ function parsePratt(s: ITokenStream, minBp: number): Ast.Node {
s.next();
}

const tokenKind = s.getToken().kind;
const tokenKind = s.getTokenKind();

const postfix = operators.find((x): x is PostfixInfo => x.opKind === 'postfix' && x.kind === tokenKind);
if (postfix != null) {
Expand Down
12 changes: 6 additions & 6 deletions src/parser/syntaxes/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { ITokenStream } from '../streams/token-stream.js';
export function parseStatement(s: ITokenStream): Ast.Node {
const startPos = s.getPos();

switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.VarKeyword:
case TokenKind.LetKeyword: {
return parseVarDef(s);
Expand Down Expand Up @@ -69,7 +69,7 @@ export function parseStatement(s: ITokenStream): Ast.Node {
}

export function parseDefStatement(s: ITokenStream): Ast.Node {
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.VarKeyword:
case TokenKind.LetKeyword: {
return parseVarDef(s);
Expand All @@ -78,7 +78,7 @@ export function parseDefStatement(s: ITokenStream): Ast.Node {
return parseFnDef(s);
}
default: {
throw new AiScriptSyntaxError(`unexpected token: ${TokenKind[s.getToken().kind]}`, s.getPos());
throw new AiScriptSyntaxError(`unexpected token: ${TokenKind[s.getTokenKind()]}`, s.getPos());
}
}
}
Expand Down Expand Up @@ -107,7 +107,7 @@ function parseVarDef(s: ITokenStream): Ast.Node {
const startPos = s.getPos();

let mut;
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.LetKeyword: {
mut = false;
break;
Expand All @@ -117,7 +117,7 @@ function parseVarDef(s: ITokenStream): Ast.Node {
break;
}
default: {
throw new AiScriptSyntaxError(`unexpected token: ${TokenKind[s.getToken().kind]}`, s.getPos());
throw new AiScriptSyntaxError(`unexpected token: ${TokenKind[s.getTokenKind()]}`, s.getPos());
}
}
s.next();
Expand Down Expand Up @@ -463,7 +463,7 @@ function tryParseAssign(s: ITokenStream, dest: Ast.Node): Ast.Node | undefined {
const startPos = s.getPos();

// Assign
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.Eq: {
s.next();
const expr = parseExpr(s, false);
Expand Down
12 changes: 6 additions & 6 deletions src/parser/syntaxes/toplevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function parseTopLevel(s: ITokenStream): Ast.Node[] {
}

while (!s.is(TokenKind.EOF)) {
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.Colon2: {
nodes.push(parseNamespace(s));
break;
Expand All @@ -36,10 +36,10 @@ export function parseTopLevel(s: ITokenStream): Ast.Node[] {
}

// terminator
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.NewLine:
case TokenKind.SemiColon: {
while ([TokenKind.NewLine, TokenKind.SemiColon].includes(s.getToken().kind)) {
while ([TokenKind.NewLine, TokenKind.SemiColon].includes(s.getTokenKind())) {
s.next();
}
break;
Expand Down Expand Up @@ -80,7 +80,7 @@ export function parseNamespace(s: ITokenStream): Ast.Node {
}

while (!s.is(TokenKind.CloseBrace)) {
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.VarKeyword:
case TokenKind.LetKeyword:
case TokenKind.At: {
Expand All @@ -94,10 +94,10 @@ export function parseNamespace(s: ITokenStream): Ast.Node {
}

// terminator
switch (s.getToken().kind) {
switch (s.getTokenKind()) {
case TokenKind.NewLine:
case TokenKind.SemiColon: {
while ([TokenKind.NewLine, TokenKind.SemiColon].includes(s.getToken().kind)) {
while ([TokenKind.NewLine, TokenKind.SemiColon].includes(s.getTokenKind())) {
s.next();
}
break;
Expand Down

0 comments on commit cf5c694

Please sign in to comment.