Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Unified Diff: pkg/front_end/lib/src/fasta/analyzer/token_utils.dart

Issue 2682423003: Unify fasta->analyzer token translation logic. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/front_end/test/scanner_fasta_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/front_end/lib/src/fasta/analyzer/token_utils.dart
diff --git a/pkg/front_end/lib/src/fasta/analyzer/token_utils.dart b/pkg/front_end/lib/src/fasta/analyzer/token_utils.dart
index cfe197213b9e02886d784e50561d8d55f2b30bb4..8ebe76f2f0e9bd8978156731941293318ecffc38 100644
--- a/pkg/front_end/lib/src/fasta/analyzer/token_utils.dart
+++ b/pkg/front_end/lib/src/fasta/analyzer/token_utils.dart
@@ -5,42 +5,143 @@
library fasta.analyzer.token_utils;
import 'package:front_end/src/fasta/scanner/token.dart' show
+ KeywordToken,
Token;
import 'package:front_end/src/fasta/scanner/token_constants.dart';
-import 'package:analyzer/dart/ast/token.dart' as analyzer show
- Token;
+import 'package:front_end/src/scanner/token.dart' as analyzer show
+ CommentToken,
+ Keyword,
+ KeywordToken,
+ KeywordTokenWithComment,
+ StringToken,
+ StringTokenWithComment,
+ Token,
+ TokenWithComment;
import 'package:analyzer/dart/ast/token.dart' show
TokenType;
-import 'package:analyzer/src/dart/ast/token.dart' as analyzer show
- StringToken;
-
import '../errors.dart' show
internalError;
-analyzer.Token toAnalyzerToken(Token token) {
+analyzer.Token toAnalyzerToken(Token token,
+ [analyzer.CommentToken commentToken]) {
if (token == null) return null;
+ analyzer.Token makeStringToken(TokenType tokenType) {
+ if (commentToken == null) {
+ return new analyzer.StringToken(tokenType, token.value, token.charOffset);
+ } else {
+ return new analyzer.StringTokenWithComment(
+ tokenType, token.value, token.charOffset, commentToken);
+ }
+ }
+
switch (token.kind) {
- case STRING_TOKEN:
- return new analyzer.StringToken(
- TokenType.STRING, token.value, token.charOffset);
+ case DOUBLE_TOKEN:
+ return makeStringToken(TokenType.DOUBLE);
+
+ case HEXADECIMAL_TOKEN:
+ return makeStringToken(TokenType.HEXADECIMAL);
case IDENTIFIER_TOKEN:
- return new analyzer.StringToken(
- TokenType.IDENTIFIER, token.value, token.charOffset);
+ return makeStringToken(TokenType.IDENTIFIER);
case INT_TOKEN:
- return new analyzer.StringToken(
- TokenType.INT, token.value, token.charOffset);
+ return makeStringToken(TokenType.INT);
+
+ case KEYWORD_TOKEN:
+ KeywordToken keywordToken = token;
+ var syntax = keywordToken.keyword.syntax;
+ var keyword = _keywordMap[syntax];
+ if (keyword == null) {
+ if (_pseudoKeywords.contains(syntax)) {
ahe 2017/02/10 14:12:40 Would token.isIdentifier or token.isPseudo work he
Paul Berry 2017/02/10 17:01:02 Thanks. token.isIdentifier is too broad (it inclu
ahe 2017/02/10 17:10:46 I thought that might be the case. One thing you c
Paul Berry 2017/02/10 17:20:24 Thanks. I was considering that, and I'm glad to h
+ // TODO(paulberry,ahe): fasta scans "async", "await", and "sync" as
+ // keywords. They need to be identifiers since their meaning is only
+ // special in certain contexts.
+ return makeStringToken(TokenType.IDENTIFIER);
+ } else {
+ return internalError('Unknown keyword: $syntax');
+ }
+ }
+ if (commentToken == null) {
+ return new analyzer.KeywordToken(keyword, token.charOffset);
+ } else {
+ return new analyzer.KeywordTokenWithComment(
+ keyword, token.charOffset, commentToken);
+ }
+ break;
+
+ case STRING_TOKEN:
+ return makeStringToken(TokenType.STRING);
default:
- return new analyzer.Token(getTokenType(token), token.charOffset);
+ if (commentToken == null) {
+ return new analyzer.Token(getTokenType(token), token.charOffset);
+ } else {
+ return new analyzer.TokenWithComment(
+ getTokenType(token), token.charOffset, commentToken);
+ }
+ break;
}
}
+final _keywordMap = {
+ "assert": analyzer.Keyword.ASSERT,
ahe 2017/02/10 14:12:40 I think you're not worried about performance right
Paul Berry 2017/02/10 17:01:02 You're correct that I'm not worried about performa
ahe 2017/02/10 17:10:46 FWIW, my biggest worry about unifying them is the
Paul Berry 2017/02/10 17:20:24 Agreed--it's a complex tradeoff. I don't intend t
ahe 2017/02/10 17:22:45 Data and opinions are good :-) Just to be clear,
+ "break": analyzer.Keyword.BREAK,
+ "case": analyzer.Keyword.CASE,
+ "catch": analyzer.Keyword.CATCH,
+ "class": analyzer.Keyword.CLASS,
+ "const": analyzer.Keyword.CONST,
+ "continue": analyzer.Keyword.CONTINUE,
+ "default": analyzer.Keyword.DEFAULT,
+ "do": analyzer.Keyword.DO,
+ "else": analyzer.Keyword.ELSE,
+ "enum": analyzer.Keyword.ENUM,
+ "extends": analyzer.Keyword.EXTENDS,
+ "false": analyzer.Keyword.FALSE,
+ "final": analyzer.Keyword.FINAL,
+ "finally": analyzer.Keyword.FINALLY,
+ "for": analyzer.Keyword.FOR,
+ "if": analyzer.Keyword.IF,
+ "in": analyzer.Keyword.IN,
+ "new": analyzer.Keyword.NEW,
+ "null": analyzer.Keyword.NULL,
+ "rethrow": analyzer.Keyword.RETHROW,
+ "return": analyzer.Keyword.RETURN,
+ "super": analyzer.Keyword.SUPER,
+ "switch": analyzer.Keyword.SWITCH,
+ "this": analyzer.Keyword.THIS,
+ "throw": analyzer.Keyword.THROW,
+ "true": analyzer.Keyword.TRUE,
+ "try": analyzer.Keyword.TRY,
+ "var": analyzer.Keyword.VAR,
+ "void": analyzer.Keyword.VOID,
+ "while": analyzer.Keyword.WHILE,
+ "with": analyzer.Keyword.WITH,
+ "is": analyzer.Keyword.IS,
+ "abstract": analyzer.Keyword.ABSTRACT,
+ "as": analyzer.Keyword.AS,
+ "covariant": analyzer.Keyword.COVARIANT,
+ "dynamic": analyzer.Keyword.DYNAMIC,
+ "export": analyzer.Keyword.EXPORT,
+ "external": analyzer.Keyword.EXTERNAL,
+ "factory": analyzer.Keyword.FACTORY,
+ "get": analyzer.Keyword.GET,
+ "implements": analyzer.Keyword.IMPLEMENTS,
+ "import": analyzer.Keyword.IMPORT,
+ "library": analyzer.Keyword.LIBRARY,
+ "operator": analyzer.Keyword.OPERATOR,
+ "part": analyzer.Keyword.PART,
+ "set": analyzer.Keyword.SET,
+ "static": analyzer.Keyword.STATIC,
+ "typedef": analyzer.Keyword.TYPEDEF,
+ "deferred": analyzer.Keyword.DEFERRED,
+};
+
+final _pseudoKeywords = new Set<String>.from(['async', 'await', 'sync']);
+
TokenType getTokenType(Token token) {
switch (token.kind) {
case EOF_TOKEN: return TokenType.EOF;
@@ -109,8 +210,8 @@ TokenType getTokenType(Token token) {
case SLASH_EQ_TOKEN: return TokenType.SLASH_EQ;
case STAR_TOKEN: return TokenType.STAR;
case STAR_EQ_TOKEN: return TokenType.STAR_EQ;
- // case STRING_INTERPOLATION_EXPRESSION_TOKEN:
- // return TokenType.STRING_INTERPOLATION_EXPRESSION;
+ case STRING_INTERPOLATION_TOKEN:
+ return TokenType.STRING_INTERPOLATION_EXPRESSION;
case STRING_INTERPOLATION_IDENTIFIER_TOKEN:
return TokenType.STRING_INTERPOLATION_IDENTIFIER;
case TILDE_TOKEN: return TokenType.TILDE;
« no previous file with comments | « no previous file | pkg/front_end/test/scanner_fasta_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698