ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JsniCommentTokenizer.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 13 Feb 2013 12:08:00 +0100
branchide
changeset 717 58ce0cd13d26
child 718 b93760cedf02
permissions -rw-r--r--
Igor's dejsni files and tests. Modified to compile.
jaroslav@717
     1
/**
jaroslav@717
     2
 * Back 2 Browser Bytecode Translator
jaroslav@717
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@717
     4
 *
jaroslav@717
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@717
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@717
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@717
     8
 *
jaroslav@717
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@717
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@717
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@717
    12
 * GNU General Public License for more details.
jaroslav@717
    13
 *
jaroslav@717
    14
 * You should have received a copy of the GNU General Public License
jaroslav@717
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@717
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@717
    17
 */
jaroslav@717
    18
package org.apidesign.bck2brwsr.ide.editor;
jaroslav@717
    19
jaroslav@717
    20
import java.util.regex.Matcher;
jaroslav@717
    21
import java.util.regex.Pattern;
jaroslav@717
    22
jaroslav@717
    23
public class JsniCommentTokenizer {
jaroslav@717
    24
jaroslav@717
    25
    /**
jaroslav@717
    26
     * Tokenize the contents of JSNI comment into the provided {@linkplain Sink}.
jaroslav@717
    27
     * @param in the contents of JSNI comment
jaroslav@717
    28
     * @param out the sink that consumes parsed tokens
jaroslav@717
    29
     */
jaroslav@717
    30
    public void process(final String in, final Sink out) {
jaroslav@717
    31
        final Matcher member = Pattern.compile("@([^:]+)::([a-zA-Z_$][a-zA-Z\\d_$]*)").matcher(in);
jaroslav@717
    32
        final Matcher signature = Pattern.compile("\\(([^\\)]*)\\)").matcher(in);
jaroslav@717
    33
jaroslav@717
    34
        int i = 0;
jaroslav@717
    35
        while (true) {
jaroslav@717
    36
            if (member.find(i)) {
jaroslav@717
    37
                final int memberStart = member.start();
jaroslav@717
    38
                final int memberEnd = member.end();
jaroslav@717
    39
                if (memberStart > i) out.javascript(in.substring(i, memberStart));
jaroslav@717
    40
jaroslav@717
    41
                final String clazz = member.group(1);
jaroslav@717
    42
                final String name = member.group(2);
jaroslav@717
    43
jaroslav@717
    44
                if (in.charAt(memberEnd) == '(') {
jaroslav@717
    45
                    if (!signature.find(memberEnd)) {
jaroslav@717
    46
                        throw new IllegalStateException("Expected method signature");
jaroslav@717
    47
                    }
jaroslav@717
    48
                    assert signature.start() == memberEnd;
jaroslav@717
    49
                    out.method(clazz, name, signature.group(1));
jaroslav@717
    50
                    i = signature.end();
jaroslav@717
    51
                } else {
jaroslav@717
    52
                    out.field(clazz, name);
jaroslav@717
    53
                    i = memberEnd;
jaroslav@717
    54
                }
jaroslav@717
    55
            } else {
jaroslav@717
    56
                out.javascript(in.substring(i, in.length()));
jaroslav@717
    57
                break;
jaroslav@717
    58
            }
jaroslav@717
    59
        }
jaroslav@717
    60
    }
jaroslav@717
    61
jaroslav@717
    62
jaroslav@717
    63
    public static interface Sink {
jaroslav@717
    64
        void javascript(String s);
jaroslav@717
    65
jaroslav@717
    66
        void method(String clazz, String method, String signature);
jaroslav@717
    67
jaroslav@717
    68
        void field(String clazz, String field);
jaroslav@717
    69
    }
jaroslav@717
    70
}