Define Variable with simple value
This commit is contained in:
parent
38f22e48b0
commit
e6ad2822eb
4 changed files with 63 additions and 29 deletions
|
@ -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++];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -54,23 +54,16 @@ public class Class{
|
||||||
}else if(equals < braces){
|
}else if(equals < braces){
|
||||||
//variable with value
|
//variable with value
|
||||||
boolean quote = false;
|
boolean quote = false;
|
||||||
Variable last = null;
|
Variable variable = null;
|
||||||
do {
|
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);
|
int index = variable.parse(content, cleaner);
|
||||||
this.vars.add(variable);
|
this.vars.add(variable);
|
||||||
content = content.substring(index);
|
content = content.substring(index);
|
||||||
quote = content.startsWith(",");
|
quote = content.startsWith(",");
|
||||||
if(quote) {
|
if(quote) content = content.substring(1);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}while(quote);
|
}while(quote);
|
||||||
|
content = content.substring(1);
|
||||||
}else{
|
}else{
|
||||||
System.out.println("Function "+content);
|
System.out.println("Function "+content);
|
||||||
Function func = new Function();
|
Function func = new Function();
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
package be.jeffcheasey88.peeratcode.parser.java;
|
||||||
|
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@ -26,7 +27,8 @@ public class Variable {
|
||||||
System.out.println("parse "+content);
|
System.out.println("parse "+content);
|
||||||
CleanerPool generic = new CleanerPool(
|
CleanerPool generic = new CleanerPool(
|
||||||
new Cleaner("GENERIC_TYPE_",'<','>'),
|
new Cleaner("GENERIC_TYPE_",'<','>'),
|
||||||
new Cleaner("GENERIC_ARRAY",'[',']'));
|
new Cleaner("GENERIC_ARRAY",'[',']'),
|
||||||
|
new Cleaner("GENERIC_FUNCTION", '{','}'));
|
||||||
content = generic.clean(content);
|
content = generic.clean(content);
|
||||||
System.out.println("clean "+content);
|
System.out.println("clean "+content);
|
||||||
|
|
||||||
|
@ -48,36 +50,49 @@ public class Variable {
|
||||||
onlyDefine(body, generic);
|
onlyDefine(body, generic);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.print("INSTANCE ");
|
||||||
|
show(1);
|
||||||
return offset+min;
|
return offset+min;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assigment(String content, CleanerPool cleaner){
|
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){
|
private Iterator<String> onlyDefine(String content, CleanerPool cleaner){
|
||||||
System.out.println("onlyDefine "+content);
|
|
||||||
content = generiqueTypes(content, cleaner);
|
content = generiqueTypes(content, cleaner);
|
||||||
System.out.println(content);
|
Iterator<String> values = new ArrayIterator<>(content.split("\\s+"));
|
||||||
String[] values = content.split("\\s+");
|
String value = null;
|
||||||
for(String value : values){
|
int modifier;
|
||||||
int modifier = JavaParser.getModifier(value);
|
while(values.hasNext() && (modifier = JavaParser.getModifier(value = values.next())) > 0){
|
||||||
if(modifier > 0){
|
this.modifier+=modifier;
|
||||||
this.modifier+=modifier;
|
}
|
||||||
continue;
|
if(this.type == null){
|
||||||
}
|
this.type = value;
|
||||||
if(this.type == null){
|
if(values.hasNext()) value = values.next();
|
||||||
this.type = value;
|
}
|
||||||
continue;
|
if(this.name == null){
|
||||||
}
|
|
||||||
this.name = value;
|
this.name = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String generiqueTypes(String content, CleanerPool cleaner){
|
private String generiqueTypes(String content, CleanerPool cleaner){
|
||||||
return cleaner.
|
return cleaner.
|
||||||
unzip(content, (s) -> s.replaceAll("\\s+", "")).
|
unzip(content, (s) -> s.replaceAll("\\s+", "")).
|
||||||
replaceAll(">(?<varname>[^>\\d,;])", "> ${varname}").
|
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){
|
private int indexOf(String value, String target){
|
||||||
|
|
|
@ -115,7 +115,7 @@ class VariableTest{
|
||||||
clazz.parse(cleaner.clean("public class Test{ int i ,j,k,l=1; } "), cleaner);
|
clazz.parse(cleaner.clean("public class Test{ int i ,j,k,l=1; } "), cleaner);
|
||||||
|
|
||||||
List<Variable> vars = clazz.getVariables();
|
List<Variable> vars = clazz.getVariables();
|
||||||
assertEquals(vars.size(), 4);
|
assertEquals(4, vars.size());
|
||||||
for(int i = 0; i < 3; i++){
|
for(int i = 0; i < 3; i++){
|
||||||
Variable v = vars.get(i);
|
Variable v = vars.get(i);
|
||||||
assertEquals(0, v.getModifier());
|
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());
|
clazz.parse(cleaner.clean("public class Test{ int i ,j,k; int l=i=k=l=4; } "), new CleanerPool());
|
||||||
|
|
||||||
List<Variable> vars = clazz.getVariables();
|
List<Variable> vars = clazz.getVariables();
|
||||||
assertEquals(vars.size(), 4);
|
assertEquals(4, vars.size());
|
||||||
for(int i = 0; i < 4; i++){
|
for(int i = 0; i < 4; i++){
|
||||||
Variable v = vars.get(i);
|
Variable v = vars.get(i);
|
||||||
assertEquals(0, v.getModifier());
|
assertEquals(0, v.getModifier());
|
||||||
|
|
Loading…
Add table
Reference in a new issue