| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 # for details. All rights reserved. Use of this source code is governed by a | |
| 3 # BSD-style license that can be found in the LICENSE file. | |
| 4 from token_info import tokens, keywords | |
| 5 from codegen import CodeWriter | |
| 6 | |
| 7 # TODO(jimhug): Compare perf of the switch pattern used here with maps and ifs | |
| 8 | |
| 9 def main(): | |
| 10 '''Generates the TokenKind class into token_kind.g.dart.''' | |
| 11 cw = CodeWriter(__file__) | |
| 12 cw.enterBlock('class TokenKind {') | |
| 13 index = 1 | |
| 14 | |
| 15 trueKeywords = [kw for kw in keywords if not kw.isPseudo] | |
| 16 pseudoKeywords = [kw for kw in keywords if kw.isPseudo] | |
| 17 allTokens = tokens + pseudoKeywords + trueKeywords | |
| 18 | |
| 19 for tok in allTokens: | |
| 20 if tok.name is None: continue | |
| 21 tok.index = index | |
| 22 index += 1 | |
| 23 cw.writeln('/** [TokenKind] representing %s tokens. */', tok.stringRepr) | |
| 24 cw.writeln('static final int %s = %d;', tok.name, tok.index) | |
| 25 cw.writeln('') | |
| 26 | |
| 27 cw.enterBlock('static String kindToString(int kind) {') | |
| 28 cw.writeln('switch(kind) {') | |
| 29 cw.enterBlock() | |
| 30 | |
| 31 for tok in allTokens: | |
| 32 if tok.name is None: continue | |
| 33 cw.writeln('case TokenKind.%s: return "%s";', tok.name, tok.stringRepr) | |
| 34 | |
| 35 cw.writeln('default: return "TokenKind(" + kind.toString() + ")";') | |
| 36 cw.exitBlock('}') | |
| 37 cw.exitBlock('}') | |
| 38 | |
| 39 cw.writeln() | |
| 40 | |
| 41 cw.enterBlock('static bool isIdentifier(int kind) {') | |
| 42 cw.writeln('return kind >= IDENTIFIER && kind < %s;' % trueKeywords[0].name) | |
| 43 cw.exitBlock('}') | |
| 44 | |
| 45 cw.writeln() | |
| 46 | |
| 47 cw.enterBlock('static int infixPrecedence(int kind) {') | |
| 48 cw.enterBlock('switch(kind) {') | |
| 49 for tok in tokens + keywords: | |
| 50 if tok.precedence > 0: | |
| 51 cw.writeln('case %s: return %d;' % (tok.name, tok.precedence)) | |
| 52 | |
| 53 | |
| 54 cw.writeln('default: return -1;') | |
| 55 cw.exitBlock('}') | |
| 56 cw.exitBlock('}') | |
| 57 | |
| 58 cw.writeln() | |
| 59 cw.enterBlock('static String rawOperatorFromMethod(String name) {') | |
| 60 cw.enterBlock('switch(name) {') | |
| 61 for tok in tokens: | |
| 62 if tok.methodName is not None: | |
| 63 cw.writeln('case %r: return %r;' % (tok.methodName, tok.text)) | |
| 64 cw.writeln("case ':ne': return '!=';"); | |
| 65 cw.exitBlock('}') | |
| 66 cw.exitBlock('}') | |
| 67 | |
| 68 cw.writeln() | |
| 69 cw.enterBlock('static String binaryMethodName(int kind) {') | |
| 70 cw.enterBlock('switch(kind) {') | |
| 71 for tok in tokens: | |
| 72 if tok.methodName is not None: | |
| 73 dname = repr(tok.methodName).replace('$', '\\$') | |
| 74 cw.writeln('case %s: return %s;' % (tok.name, dname)) | |
| 75 cw.exitBlock('}') | |
| 76 cw.exitBlock('}') | |
| 77 | |
| 78 cw.writeln() | |
| 79 cw.enterBlock('static String unaryMethodName(int kind) {') | |
| 80 | |
| 81 cw.exitBlock('}') | |
| 82 | |
| 83 cw.writeln() | |
| 84 cw.enterBlock('static int kindFromAssign(int kind) {') | |
| 85 cw.writeln('if (kind == ASSIGN) return 0;') | |
| 86 cw.enterBlock('if (kind > ASSIGN && kind <= ASSIGN_MOD) {') | |
| 87 cw.writeln('return kind + (ADD - ASSIGN_ADD);') | |
| 88 cw.exitBlock('}') | |
| 89 cw.writeln('return -1;') | |
| 90 cw.exitBlock('}') | |
| 91 | |
| 92 | |
| 93 | |
| 94 cw.exitBlock('}') | |
| 95 | |
| 96 cw.writeToFile('token_kind') | |
| 97 | |
| 98 if __name__ == '__main__': main() | |
| OLD | NEW |