1 /*
2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package hat.tools.textmodel;
26
27 import hat.tools.textmodel.tokens.At;
28 import hat.tools.textmodel.tokens.Ch;
29 import hat.tools.textmodel.tokens.DottedName;
30 import hat.tools.textmodel.tokens.FloatConst;
31 import hat.tools.textmodel.tokens.IntConst;
32 import hat.tools.textmodel.tokens.LeafReplacementToken;
33 import hat.tools.textmodel.tokens.ReservedWord;
34 import hat.tools.textmodel.tokens.Seq;
35 import hat.tools.textmodel.tokens.Token;
36
37 import java.util.regex.Pattern;
38
39 public class JavaTextModel extends TextModel {
40
41
42 public static class JavaModifier extends LeafReplacementToken {
43 public static final Pattern regex = Pattern.compile("static|abstract|public|private|protected|final");
44 public JavaModifier(Token t) {
45 super(t);
46 }
47 }
48 public static class JavaType extends LeafReplacementToken {
49 public static final Pattern regex = Pattern.compile(
50 "var|if|while|case|switch|break|for|new|import|instanceof|default|return|super|package"
51 );
52
53 public JavaType(Token t) {
54 super(t);
55 }
56 }
57 public static class JavaAnnotation extends LeafReplacementToken{
58 public JavaAnnotation(Token at, Token identifier) {
59 super(at, identifier);
60 }
61 }
62
63 public void transform(){
64 // "[0-9][0-9]*" ->IntConst
65 replace(true, t -> Seq.isA(t, $->$.matches(IntConst.regex)), IntConst::new);
66
67 // IntConst '.' IntConst ->FloatConst (yeah we are missing '.' IntConst and the exponent stuff)
68 replace(true, (t1,t2,t3) -> IntConst.isA(t1) && Ch.isADot(t2) && IntConst.isA(t3), FloatConst::new);
69
70 // @ (char) -> At
71 replace(true, Ch::isAnAt, At::new);
72
73 Pattern javaTypes = Pattern.compile("void|int|float|double|boolean|char|short|long|class|record|interface|String");
74
75 replace(true,t -> Seq.isA(t, $->$.matches(javaTypes)), ReservedWord::new);
76
77 replace(true,t -> Seq.isA(t,$->$.matches(JavaType.regex)), JavaType::new);
78
79 replace(true,t -> Seq.isA(t,$->$.matches(JavaModifier.regex)),JavaModifier::new);
80
81 // (Seq|Dname) '.' Seq -> Dname
82 replace(true,(t1,t2,t3) -> (Seq.isA(t1) || DottedName.isA(t1)) && Ch.isADot(t2) && Seq.isA(t3),DottedName::new);
83
84 // map all seqs to DottedName
85 replace(true, t -> Seq.isA(t,$->$.matches(DottedName.regex)), DottedName::new);
86 }
87 static public JavaTextModel of(String text) {
88 JavaTextModel doc = new JavaTextModel();
89 doc.parse(text);
90 doc.transform();
91 return doc;
92 }
93 }