Пример #1
0
def main():
    cw = CodeWriter(__file__)
    cw._indent += 2
    cases.writeCases(cw)
    casesCode = str(cw)

    cw = CodeWriter(__file__)
    writeExtraMethods(cw)
    extraMethods = str(cw)

    cw = CodeWriter(__file__)
    writeHelperMethods(cw)
    helperMethods = str(cw)

    out = open('tokenizer.g.dart', 'w')
    out.write(HEADER % __file__)
    pat = re.compile('@(.)', re.DOTALL)
    text = pat.sub(makeSafe1, TOKENIZER)
    out.write(
        text % {
            'cases': casesCode,
            'extraMethods': extraMethods,
            'helperMethods': helperMethods
        })
    out.close()
Пример #2
0
def main():
    cw = CodeWriter(__file__)

    for node in nodes:
        node.write(cw)
        cw.writeln()

    cw.writeln()
    cw.enterBlock('interface TreeVisitor {')
    for node in nodes:
        node.writeVisitInterfaceMethod(cw)
        cw.writeln()

    cw.exitBlock('}')

    cw.writeln()
    cw.enterBlock('class TreePrinter implements TreeVisitor {')

    cw.writeln('var output;')
    cw.writeln('TreePrinter(this.output) { output.printer = this; }')
    for node in nodes:
        node.writePrettyPrintMethod(cw)
        cw.writeln()
    cw.exitBlock('}')

    cw.writeToFile('tree')
Пример #3
0
def main():
    '''Generates the TokenKind class into token_kind.g.dart.'''
    cw = CodeWriter(__file__)
    for tok in tokens:
        if tok.methodName is not None and tok.methodName not in EXCLUDES:
            dname = repr(tok.methodName).replace('$', '\\$')
            cw.enterBlock('function %s(x, y) {' % tok.methodName)
            cw.writeln(
                "return (typeof(x) == 'number' && typeof(y) == 'number')")
            cw.writeln('  ? x %s y : x.%s(y);' % (tok.text, tok.methodName))
            cw.exitBlock('}')

    cw.writeToFile('tests/core.g.js')
Пример #4
0
def main():
    '''Generates the TokenKind class into token_kind.g.dart.'''
    cw = CodeWriter(__file__)
    cw.enterBlock('class TokenKind {')
    index = 1

    trueKeywords = [kw for kw in keywords if not kw.isPseudo]
    pseudoKeywords = [kw for kw in keywords if kw.isPseudo]
    allTokens = tokens + pseudoKeywords + trueKeywords

    for tok in allTokens:
        if tok.name is None: continue
        tok.index = index
        index += 1
        cw.writeln('/** [TokenKind] representing %s tokens. */',
                   tok.stringRepr)
        cw.writeln('static final int %s = %d;', tok.name, tok.index)
        cw.writeln('')

    cw.enterBlock('static String kindToString(int kind) {')
    cw.writeln('switch(kind) {')
    cw.enterBlock()

    for tok in allTokens:
        if tok.name is None: continue
        cw.writeln('case TokenKind.%s: return "%s";', tok.name, tok.stringRepr)

    cw.writeln('default: return "TokenKind(" + kind.toString() + ")";')
    cw.exitBlock('}')
    cw.exitBlock('}')

    cw.writeln()

    cw.enterBlock('static bool isIdentifier(int kind) {')
    cw.writeln('return kind >= IDENTIFIER && kind < %s;' %
               trueKeywords[0].name)
    cw.exitBlock('}')

    cw.writeln()

    cw.enterBlock('static int infixPrecedence(int kind) {')
    cw.enterBlock('switch(kind) {')
    for tok in tokens + keywords:
        if tok.precedence > 0:
            cw.writeln('case %s: return %d;' % (tok.name, tok.precedence))

    cw.writeln('default: return -1;')
    cw.exitBlock('}')
    cw.exitBlock('}')

    cw.writeln()
    cw.enterBlock('static String rawOperatorFromMethod(String name) {')
    cw.enterBlock('switch(name) {')
    for tok in tokens:
        if tok.methodName is not None:
            cw.writeln('case %r: return %r;' % (tok.methodName, tok.text))
    cw.writeln("case ':ne': return '!=';")
    cw.exitBlock('}')
    cw.exitBlock('}')

    cw.writeln()
    cw.enterBlock('static String binaryMethodName(int kind) {')
    cw.enterBlock('switch(kind) {')
    for tok in tokens:
        if tok.methodName is not None:
            dname = repr(tok.methodName).replace('$', '\\$')
            cw.writeln('case %s: return %s;' % (tok.name, dname))
    cw.exitBlock('}')
    cw.exitBlock('}')

    cw.writeln()
    cw.enterBlock('static String unaryMethodName(int kind) {')

    cw.exitBlock('}')

    cw.writeln()
    cw.enterBlock('static int kindFromAssign(int kind) {')
    cw.writeln('if (kind == ASSIGN) return 0;')
    cw.enterBlock('if (kind > ASSIGN && kind <= ASSIGN_MOD) {')
    cw.writeln('return kind + (ADD - ASSIGN_ADD);')
    cw.exitBlock('}')
    cw.writeln('return -1;')
    cw.exitBlock('}')

    cw.exitBlock('}')

    cw.writeToFile('token_kind')