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