MCPcopy Index your code
hub / github.com/aiscript-dev/aiscript / readToken

Method readToken

src/parser/scanner.ts:108–336  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

106 }
107
108 private readToken(): Token {
109 let hasLeftSpacing = false;
110
111 while (true) {
112 if (this.stream.eof) {
113 return TOKEN(TokenKind.EOF, this.stream.getPos(), { hasLeftSpacing });
114 }
115 // skip spasing
116 if (spaceChars.includes(this.stream.char)) {
117 this.stream.next();
118 hasLeftSpacing = true;
119 continue;
120 }
121
122 // トークン位置を記憶
123 const pos = this.stream.getPos();
124
125 if (lineBreakChars.includes(this.stream.char)) {
126 this.skipEmptyLines();
127 return TOKEN(TokenKind.NewLine, pos, { hasLeftSpacing });
128 }
129
130 // noFallthroughCasesInSwitchと関数の返り値の型を利用し、全ての場合分けがreturnかcontinueで適切に処理されることを強制している
131 // その都合上、break文の使用ないしこのswitch文の後に処理を書くことは極力避けてほしい
132 switch (this.stream.char) {
133 case '!': {
134 this.stream.next();
135 if (!this.stream.eof && (this.stream.char as string) === '=') {
136 this.stream.next();
137 return TOKEN(TokenKind.NotEq, pos, { hasLeftSpacing });
138 } else {
139 return TOKEN(TokenKind.Not, pos, { hasLeftSpacing });
140 }
141 }
142 case '"':
143 case '\'': {
144 return this.readStringLiteral(hasLeftSpacing);
145 }
146 case '#': {
147 this.stream.next();
148 if (!this.stream.eof && (this.stream.char as string) === '#') {
149 this.stream.next();
150 if (!this.stream.eof && (this.stream.char as string) === '#') {
151 this.stream.next();
152 return TOKEN(TokenKind.Sharp3, pos, { hasLeftSpacing });
153 } else {
154 throw new AiScriptSyntaxError('invalid sequence of characters: "##"', pos);
155 }
156 } else if (!this.stream.eof && (this.stream.char as string) === '[') {
157 this.stream.next();
158 return TOKEN(TokenKind.OpenSharpBracket, pos, { hasLeftSpacing });
159 } else {
160 return TOKEN(TokenKind.Sharp, pos, { hasLeftSpacing });
161 }
162 }
163 case '%': {
164 this.stream.next();
165 return TOKEN(TokenKind.Percent, pos, { hasLeftSpacing });

Callers 4

constructorMethod · 0.95
nextMethod · 0.95
lookaheadMethod · 0.95
readTemplateMethod · 0.95

Calls 11

skipEmptyLinesMethod · 0.95
readStringLiteralMethod · 0.95
skipCommentRangeMethod · 0.95
skipCommentLineMethod · 0.95
tryReadWordMethod · 0.95
readTemplateMethod · 0.95
tryReadDigitsMethod · 0.95
TOKENFunction · 0.85
prevMethod · 0.80
getPosMethod · 0.65
nextMethod · 0.65

Tested by

no test coverage detected