Define Variable with simple value

This commit is contained in:
jeffcheasey88 2023-05-06 15:56:15 +02:00
parent 38f22e48b0
commit e6ad2822eb
4 changed files with 63 additions and 29 deletions

View file

@ -0,0 +1,26 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.util.Iterator;
public class ArrayIterator<E> implements Iterator<E>{
private E[] elements;
private int index;
public ArrayIterator(E[] elements){
this.elements = elements;
}
@Override
public boolean hasNext(){
return index < elements.length;
}
@Override
public E next(){
return elements[index++];
}
}

View file

@ -54,23 +54,16 @@ public class Class{
}else if(equals < braces){
//variable with value
boolean quote = false;
Variable last = null;
Variable variable = null;
do {
Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType());
variable = (variable == null) ? new Variable() : new Variable(variable.getModifier(), variable.getType());
int index = variable.parse(content, cleaner);
this.vars.add(variable);
content = content.substring(index);
quote = content.startsWith(",");
if(quote) {
content = content.substring(1);
last = variable;
}else if(indexOf(content, "=") < indexOf(content, ";")){
Operation operation = new Operation();
index = operation.parse(content);
content = content.substring(index);
break;
}
if(quote) content = content.substring(1);
}while(quote);
content = content.substring(1);
}else{
System.out.println("Function "+content);
Function func = new Function();

View file

@ -1,6 +1,7 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -26,7 +27,8 @@ public class Variable {
System.out.println("parse "+content);
CleanerPool generic = new CleanerPool(
new Cleaner("GENERIC_TYPE_",'<','>'),
new Cleaner("GENERIC_ARRAY",'[',']'));
new Cleaner("GENERIC_ARRAY",'[',']'),
new Cleaner("GENERIC_FUNCTION", '{','}'));
content = generic.clean(content);
System.out.println("clean "+content);
@ -48,36 +50,49 @@ public class Variable {
onlyDefine(body, generic);
}
System.out.print("INSTANCE ");
show(1);
return offset+min;
}
private void assigment(String content, CleanerPool cleaner){
System.out.println("assigment "+content);
Iterator<String> values = onlyDefine(content, cleaner);
System.out.println("b");
if(!values.hasNext()) return;
System.out.println("ah");
values.next();
if(!values.hasNext()) return;
this.value = new Value(values.next());
System.out.println("VALUE IS "+value);
}
private void onlyDefine(String content, CleanerPool cleaner){
System.out.println("onlyDefine "+content);
private Iterator<String> onlyDefine(String content, CleanerPool cleaner){
content = generiqueTypes(content, cleaner);
System.out.println(content);
String[] values = content.split("\\s+");
for(String value : values){
int modifier = JavaParser.getModifier(value);
if(modifier > 0){
this.modifier+=modifier;
continue;
}
if(this.type == null){
this.type = value;
continue;
}
Iterator<String> values = new ArrayIterator<>(content.split("\\s+"));
String value = null;
int modifier;
while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0){
this.modifier+=modifier;
}
if(this.type == null){
this.type = value;
if(values.hasNext()) value = values.next();
}
if(this.name == null){
this.name = value;
}
return values;
}
private String generiqueTypes(String content, CleanerPool cleaner){
return cleaner.
unzip(content, (s) -> s.replaceAll("\\s+", "")).
replaceAll(">(?<varname>[^>\\d,;])", "> ${varname}").
replaceAll("](?<varname>[^\\[\\d,;])", "] ${varname}");
replaceAll("](?<varname>[^\\[\\d,;])", "] ${varname}").
replaceAll("(?<varname>[^\\s=])=","${varname} =").
replaceAll("=(?<varname>[^\\s=])"," = ${varname}");
}
private int indexOf(String value, String target){

View file

@ -115,7 +115,7 @@ class VariableTest{
clazz.parse(cleaner.clean("public class Test{ int i ,j,k,l=1; } "), cleaner);
List<Variable> vars = clazz.getVariables();
assertEquals(vars.size(), 4);
assertEquals(4, vars.size());
for(int i = 0; i < 3; i++){
Variable v = vars.get(i);
assertEquals(0, v.getModifier());
@ -140,7 +140,7 @@ class VariableTest{
clazz.parse(cleaner.clean("public class Test{ int i ,j,k; int l=i=k=l=4; } "), new CleanerPool());
List<Variable> vars = clazz.getVariables();
assertEquals(vars.size(), 4);
assertEquals(4, vars.size());
for(int i = 0; i < 4; i++){
Variable v = vars.get(i);
assertEquals(0, v.getModifier());