Skip to content

Commit 468d5dd

Browse files
committed
Fixed bug parsing Java parameters with HashMaps and improved handling of quotes
1 parent 2806959 commit 468d5dd

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

src/javaxt/utils/src/Parser.java

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ else if (c=='*' && n=='/'){
130130

131131
}
132132

133+
else if (c=='"' || c=='\''){
134+
135+
if (insideComment){
136+
currComment.append(c);
137+
}
138+
else{
139+
String quote = readQuote(s, i, c);
140+
code.append(quote);
141+
i = i+quote.length();
142+
}
143+
}
144+
133145
else{
134146
if (insideComment){
135147
currComment.append(c);
@@ -167,7 +179,7 @@ else if (c=='*' && n=='/'){
167179
if (word!=null && !word.isBlank()){
168180

169181
if (ext.equals("java")){
170-
182+
171183
if (word.equals("package")){
172184
String[] arr = readFunction(s, i);
173185
String fn = arr[0].replace(" ", "");
@@ -261,7 +273,16 @@ else if (word.equals("public")){
261273
String raw = arr[1];
262274
char lastChar = s.charAt(i+raw.length());
263275
if (lastChar=='{'){
276+
//console.log(fn);
277+
278+
//Parse method and add to class
264279
currClass.addMember(parseMethod(fn, comment, ext));
280+
281+
//Move past the function
282+
int end = getEndBacket(s, i);
283+
//console.log(s.substring(i, end));
284+
285+
if (end>-1) i = end;
265286
}
266287

267288
}
@@ -521,6 +542,41 @@ private static String readLine(String s, int offset){
521542
}
522543

523544

545+
//**************************************************************************
546+
//** readQuote
547+
//**************************************************************************
548+
/** Returns a string encapsulated by either a single or double quote,
549+
* starting at a given index
550+
*/
551+
private static String readQuote(String s, int i, char t){
552+
553+
StringBuilder str = new StringBuilder();
554+
str.append(s.charAt(i));
555+
boolean escaped = false;
556+
for (int x=i+1; x<s.length(); x++){
557+
char q = s.charAt(x);
558+
str.append(q);
559+
560+
if (q==t){
561+
if (!escaped){
562+
break;
563+
}
564+
else{
565+
escaped = false;
566+
}
567+
}
568+
else if (q=='\\'){
569+
escaped = !escaped;
570+
}
571+
else{
572+
escaped = false;
573+
}
574+
}
575+
576+
return str.toString();
577+
}
578+
579+
524580
//**************************************************************************
525581
//** readFunction
526582
//**************************************************************************
@@ -610,6 +666,14 @@ else if (c=='*' && n=='/'){
610666
}
611667
}
612668

669+
else if (c=='"' || c=='\''){
670+
671+
if (!insideComment){
672+
String quote = readQuote(s, i, c);
673+
i = i+quote.length();
674+
}
675+
}
676+
613677
else{
614678
if (!insideComment){
615679

@@ -699,6 +763,7 @@ else if (c=='*' && n=='/'){
699763

700764

701765
private static LinkedHashSet<String> getModifiers(String s, int offset) {
766+
//console.log(s);
702767
int idx = offset;
703768
for (int i=offset; i>-1; i--){
704769
char c = s.charAt(i);
@@ -867,7 +932,36 @@ private static Method parseMethod(String fn, String comments, String ext){
867932

868933
//Add parameters
869934
if (!parameters.isEmpty()){
870-
for (String param : parameters.split(",")){
935+
936+
ArrayList<String> params = new ArrayList<>();
937+
StringBuilder str = new StringBuilder();
938+
int numBrackets = 0;
939+
for (int i=0; i<parameters.length(); i++){
940+
char c = parameters.charAt(i);
941+
if (c==','){
942+
if (numBrackets==0){
943+
params.add(str.toString());
944+
str = new StringBuilder();
945+
}
946+
else{
947+
str.append(c);
948+
}
949+
}
950+
else{
951+
if (c=='<'){
952+
numBrackets++;
953+
}
954+
else if (c=='>'){
955+
numBrackets--;
956+
}
957+
str.append(c);
958+
}
959+
}
960+
params.add(str.toString());
961+
962+
963+
964+
for (String param : params){
871965
param = param.trim();
872966

873967
idx = param.lastIndexOf(" ");

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy