Skip to content

Commit

Permalink
getTokenKind, getTokenValue
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Aug 2, 2024
1 parent 7ed85c8 commit 2975ca1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
11 changes: 9 additions & 2 deletions src/parser/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ export class Scanner implements ITokenStream {
}

/**
* トークンに含まれる値を取得します
* カーソル位置にあるトークンの種類を取得します
*/
public getValue(): string {
public getTokenKind(): TokenKind {
return this.getToken().kind;
}

/**
* カーソル位置にあるトークンに含まれる値を取得します。
*/
public getTokenValue(): string {
return this.getToken().value!;
}

Expand Down
17 changes: 11 additions & 6 deletions src/parser/streams/token-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ export interface ITokenStream {
is(kind: TokenKind): boolean;

/**
* トークンに含まれる値を取得します。
* カーソル位置にあるトークンの種類を取得します。
*/
getTokenKind(): TokenKind;

/**
* カーソル位置にあるトークンに含まれる値を取得します。
*/
getValue(): string;
getTokenValue(): string;

/**
* カーソル位置にあるトークンの位置情報を取得します。
Expand Down Expand Up @@ -79,16 +84,16 @@ export class TokenStream implements ITokenStream {
}

/**
* トークンに含まれる値を取得します
* カーソル位置にあるトークンに含まれる値を取得します
*/
public getValue(): string {
public getTokenValue(): string {
return this.getToken().value!;
}

/**
* カーソル位置にあるトークンの種類を取得します。
*/
public getKind(): TokenKind {
public getTokenKind(): TokenKind {
return this.getToken().kind;
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/parser/syntaxes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function parseParams(s: ITokenStream): { name: string, argType?: Ast.Node

while (!s.is(TokenKind.CloseParen)) {
s.expect(TokenKind.Identifier);
const name = s.getValue();
const name = s.getTokenValue();
s.next();

let optional = false;
Expand Down Expand Up @@ -173,7 +173,7 @@ function parseNamedType(s: ITokenStream): Ast.Node {
const startPos = s.getPos();

s.expect(TokenKind.Identifier);
const name = s.getValue();
const name = s.getTokenValue();
s.next();

// inner type
Expand Down
16 changes: 7 additions & 9 deletions src/parser/syntaxes/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function parseInfix(s: ITokenStream, left: Ast.Node, minBp: number): Ast.Node {

if (op === TokenKind.Dot) {
s.expect(TokenKind.Identifier);
const name = s.getValue();
const name = s.getTokenValue();
s.next();

return NODE('prop', {
Expand Down Expand Up @@ -236,11 +236,9 @@ function parseAtom(s: ITokenStream, isStatic: boolean): Ast.Node {
}
case TokenKind.TemplateExprElement: {
// スキャナで埋め込み式として事前に読み取っておいたトークン列をパースする
const exprStream = new TokenStream(element.children!);
const exprStream: ITokenStream = new TokenStream(element.children!);
const expr = parseExpr(exprStream, false);
if (exprStream.getKind() !== TokenKind.EOF) {
throw new AiScriptSyntaxError(`unexpected token: ${TokenKind[exprStream.getToken().kind]}`, exprStream.getToken().pos);
}
exprStream.expect(TokenKind.EOF);
values.push(expr);
break;
}
Expand All @@ -254,13 +252,13 @@ function parseAtom(s: ITokenStream, isStatic: boolean): Ast.Node {
return NODE('tmpl', { tmpl: values }, startPos, s.getPos());
}
case TokenKind.StringLiteral: {
const value = s.getValue();
const value = s.getTokenValue();
s.next();
return NODE('str', { value }, startPos, s.getPos());
}
case TokenKind.NumberLiteral: {
// TODO: validate number value
const value = Number(s.getValue());
const value = Number(s.getTokenValue());
s.next();
return NODE('num', { value }, startPos, s.getPos());
}
Expand Down Expand Up @@ -546,7 +544,7 @@ function parseReference(s: ITokenStream): Ast.Node {
}
}
s.expect(TokenKind.Identifier);
segs.push(s.getValue());
segs.push(s.getTokenValue());
s.next();
}
return NODE('identifier', { name: segs.join(':') }, startPos, s.getPos());
Expand All @@ -570,7 +568,7 @@ function parseObject(s: ITokenStream, isStatic: boolean): Ast.Node {
const map = new Map();
while (!s.is(TokenKind.CloseBrace)) {
s.expect(TokenKind.Identifier);
const k = s.getValue();
const k = s.getTokenValue();
s.next();

s.expect(TokenKind.Colon);
Expand Down
10 changes: 5 additions & 5 deletions src/parser/syntaxes/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function parseVarDef(s: ITokenStream): Ast.Node {
s.next();

s.expect(TokenKind.Identifier);
const name = s.getValue();
const name = s.getTokenValue();
s.next();

let type;
Expand Down Expand Up @@ -156,7 +156,7 @@ function parseFnDef(s: ITokenStream): Ast.Node {
s.next();

s.expect(TokenKind.Identifier);
const name = s.getValue();
const name = s.getTokenValue();
s.next();

const params = parseParams(s);
Expand Down Expand Up @@ -220,7 +220,7 @@ function parseEach(s: ITokenStream): Ast.Node {
s.next();

s.expect(TokenKind.Identifier);
const name = s.getValue();
const name = s.getTokenValue();
s.next();

if (s.is(TokenKind.Comma)) {
Expand Down Expand Up @@ -264,7 +264,7 @@ function parseFor(s: ITokenStream): Ast.Node {
const identPos = s.getPos();

s.expect(TokenKind.Identifier);
const name = s.getValue();
const name = s.getTokenValue();
s.next();

let _from;
Expand Down Expand Up @@ -369,7 +369,7 @@ function parseAttr(s: ITokenStream): Ast.Node {
s.next();

s.expect(TokenKind.Identifier);
const name = s.getValue();
const name = s.getTokenValue();
s.next();

let value;
Expand Down
4 changes: 2 additions & 2 deletions src/parser/syntaxes/toplevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function parseNamespace(s: ITokenStream): Ast.Node {
s.next();

s.expect(TokenKind.Identifier);
const name = s.getValue();
const name = s.getTokenValue();
s.next();

const members: Ast.Node[] = [];
Expand Down Expand Up @@ -129,7 +129,7 @@ export function parseMeta(s: ITokenStream): Ast.Node {

let name = null;
if (s.is(TokenKind.Identifier)) {
name = s.getValue();
name = s.getTokenValue();
s.next();
}

Expand Down

0 comments on commit 2975ca1

Please sign in to comment.