ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JsniCommentTokenizer.java
branchide
changeset 717 58ce0cd13d26
child 718 b93760cedf02
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JsniCommentTokenizer.java	Wed Feb 13 12:08:00 2013 +0100
     1.3 @@ -0,0 +1,70 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.ide.editor;
    1.22 +
    1.23 +import java.util.regex.Matcher;
    1.24 +import java.util.regex.Pattern;
    1.25 +
    1.26 +public class JsniCommentTokenizer {
    1.27 +
    1.28 +    /**
    1.29 +     * Tokenize the contents of JSNI comment into the provided {@linkplain Sink}.
    1.30 +     * @param in the contents of JSNI comment
    1.31 +     * @param out the sink that consumes parsed tokens
    1.32 +     */
    1.33 +    public void process(final String in, final Sink out) {
    1.34 +        final Matcher member = Pattern.compile("@([^:]+)::([a-zA-Z_$][a-zA-Z\\d_$]*)").matcher(in);
    1.35 +        final Matcher signature = Pattern.compile("\\(([^\\)]*)\\)").matcher(in);
    1.36 +
    1.37 +        int i = 0;
    1.38 +        while (true) {
    1.39 +            if (member.find(i)) {
    1.40 +                final int memberStart = member.start();
    1.41 +                final int memberEnd = member.end();
    1.42 +                if (memberStart > i) out.javascript(in.substring(i, memberStart));
    1.43 +
    1.44 +                final String clazz = member.group(1);
    1.45 +                final String name = member.group(2);
    1.46 +
    1.47 +                if (in.charAt(memberEnd) == '(') {
    1.48 +                    if (!signature.find(memberEnd)) {
    1.49 +                        throw new IllegalStateException("Expected method signature");
    1.50 +                    }
    1.51 +                    assert signature.start() == memberEnd;
    1.52 +                    out.method(clazz, name, signature.group(1));
    1.53 +                    i = signature.end();
    1.54 +                } else {
    1.55 +                    out.field(clazz, name);
    1.56 +                    i = memberEnd;
    1.57 +                }
    1.58 +            } else {
    1.59 +                out.javascript(in.substring(i, in.length()));
    1.60 +                break;
    1.61 +            }
    1.62 +        }
    1.63 +    }
    1.64 +
    1.65 +
    1.66 +    public static interface Sink {
    1.67 +        void javascript(String s);
    1.68 +
    1.69 +        void method(String clazz, String method, String signature);
    1.70 +
    1.71 +        void field(String clazz, String field);
    1.72 +    }
    1.73 +}