1 /*
2  * RegexUtil.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 package org.rstudio.core.client;
16 
17 import org.rstudio.core.client.regex.Pattern;
18 
19 // We do lazy initialization of all the regex singleton strings here
20 public class RegexUtil
21 {
wordCharacter()22    public static final String wordCharacter()
23    {
24       return WORD_CHARACTER;
25    }
26 
initialize()27    private static final void initialize()
28    {
29       WORD_CHARACTER = makeWordCharacter();
30    }
31 
isSyntacticRIdentifier(String identifier)32    public static final boolean isSyntacticRIdentifier(String identifier)
33    {
34       String regex =
35             "^" +
36             "(?![0-9])" +
37             "[" + WORD_CHARACTER + ".]" +
38             "[" + WORD_CHARACTER +  "._]*" +
39             "$";
40 
41       Pattern pattern = Pattern.create(regex, "");
42       return pattern.test(identifier);
43    }
44 
makeWordCharacter()45    private static final native String makeWordCharacter()
46    /*-{
47       var unicode = $wnd.require("ace/unicode");
48       return unicode.wordChars;
49    }-*/;
50 
51    private static String WORD_CHARACTER = null;
52 
53    public static final Pattern RE_RMARKDOWN_CHUNK_BEGIN =
54          Pattern.create("^\\s*```\\{(.*?)\\}\\s*$", "");
55 
56    public static final Pattern RE_RMARKDOWN_ENGINE_NAME =
57          Pattern.create("^\\w+", "");
58 
59    public static final Pattern RE_RMARKDOWN_CHUNK_END =
60          Pattern.create("^\\s*```\\s*$", "");
61 
62    public static final Pattern RE_RHTML_CHUNK_BEGIN =
63          Pattern.create("^\\s*<!-{2,}\\s*(.*?)\\s*$", "");
64 
65    public static final Pattern RE_RHTML_CHUNK_END =
66          Pattern.create("end\\.rcode\\s*-{2,}\\>", "");
67 
68    public static final Pattern RE_SWEAVE_CHUNK_BEGIN =
69          Pattern.create("^\\s*\\<\\<(.*?)\\>\\>=\\s*$", "");
70 
71    public static final Pattern RE_SWEAVE_CHUNK_END =
72          Pattern.create("^\\s*@\\s*$", "");
73 
74    public static final Pattern RE_EMBEDDED_R_CHUNK_BEGIN =
75          Pattern.create("^\\s*\\{(.*?)\\}\\s*$", "");
76 
initialize()77    static { initialize(); }
78 
79 }
80