Fix bag & Parser on multiple parsing in same instance

This commit is contained in:
jeffcheasey88 2023-07-19 16:56:17 +02:00
parent c56f38f506
commit 2ad831641b
4 changed files with 61 additions and 10 deletions

View file

@ -37,6 +37,10 @@ public class Bag{
return (E) this.map.get(key);
}
public boolean has(String key){
return this.map.containsKey(key);
}
public void set(String key, Object value){
this.map.put(key, value);
}

View file

@ -17,9 +17,9 @@ public class Class extends JavaElement{
private List<JavaElement> elements;
public Class(Bag bag){
this.name = bag.get("name");
this.extend = bag.get("extend");
this.implement = bag.get("implement");
this.name = bag.<Token>get("name");
if(bag.has("extend")) this.extend = bag.<Token>get("extend");
if(bag.has("implement")) this.implement = bag.<Token>get("implement");
this.annotations = new ArrayList<>();
this.elements = new ArrayList<>();

View file

@ -12,6 +12,7 @@ import java.util.List;
import be.jeffcheasey88.peeratcode.parser.Parser;
import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.TokenType;
import be.jeffcheasey88.peeratcode.parser.TokenValidator;
import be.jeffcheasey88.peeratcode.parser.Tokenizer;
import be.jeffcheasey88.peeratcode.parser.state.BuilderStateTree;
import be.jeffcheasey88.peeratcode.parser.state.InitialStateTree;
@ -38,6 +39,8 @@ public class JavaParser extends Parser<JavaElement> {
System.out.println((System.currentTimeMillis()-time)+"ms");
System.out.println(TokenValidator.MAX_VALIDATE);
parser.build(new BufferedWriter(new FileWriter(new File("/home/ParserV2.txt"))));
}
@ -167,6 +170,49 @@ public class JavaParser extends Parser<JavaElement> {
operation_value.then(operation_end);
operation_value.then((validator) -> validator.validate((token) -> token.getValue().equals("."))).then(operation_value);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("return")))
.then(new RedirectStateTree<>(value, (global, local) -> global.set(null)))
.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
.end((a,b) -> a);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("throw")))
.then(new RedirectStateTree<>(value, (global, local) -> global.set(null)))
.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
.end((a,b) -> a);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("do")))
.then(new RedirectStateTree<>(value, (global, local) -> global.set(null)))
.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
.end((a,b) -> a);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("else")))
.end((a,b) -> a);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("try")))
.end((a,b) -> a);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("continue")))
.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
.end((a,b) -> a);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("break")))
.then((validator) -> validator.validate((token) -> token.getValue().equals(";")))
.end((a,b) -> a);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("if")))
.then((validator) -> validator.validate((token) -> token.getValue().equals("(")))
.then(new RedirectStateTree<>(value, (global, local) -> global.set(null)))
.then((validator) -> validator.validate((token) -> token.getValue().equals(")")))
.end((a,b) -> a);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("for")))
.then((validator) -> validator.validate((token) -> token.getValue().equals("(")))
.then(new RedirectStateTree<>(value, (global, local) -> global.set(null)))
.then((validator) -> validator.validate((token) -> token.getValue().equals(")")))
.end((a,b) -> a);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("while")))
.then((validator) -> validator.validate((token) -> token.getValue().equals("(")))
.then(new RedirectStateTree<>(value, (global, local) -> global.set(null)))
.then((validator) -> validator.validate((token) -> token.getValue().equals(")")))
.end((a,b) -> a);
operation.then((validator) -> validator.validate((token) -> token.getValue().equals("synchronized")))
.then((validator) -> validator.validate((token) -> token.getValue().equals("(")))
.then(new RedirectStateTree<>(value, (global, local) -> global.set(null)))
.then((validator) -> validator.validate((token) -> token.getValue().equals(")")))
.end((a,b) -> a);
//VARIABLE
StateTree<JavaElement> variable = new StateTree<>();
StateTree<JavaElement> variable_mod = variable.then(new RedirectStateTree<>(modifier, (global, local) -> global.set("modifier", local)));
@ -244,7 +290,7 @@ public class JavaParser extends Parser<JavaElement> {
clazz_implement_name.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
.<JavaElement>end((JavaElement, bag) -> new Class(bag)).multiple(clazz_container);
StateTree<JavaElement> clazz_extend = clazz_base.then((validator) -> validator.validate((token) -> token.getValue().equals("extends")))
.then(new RedirectStateTree<>(type, (global, local) -> global.set("extend", local)));
.then(new RedirectStateTree<>(type, (global, local) -> global.set("extend", local.get())));
clazz_extend.then((validator) -> validator.validate((token) -> token.getValue().equals("{")))
.<JavaElement>end((JavaElement, bag) -> new Class(bag)).multiple(clazz_container);
clazz_extend.then(clazz_implement);

View file

@ -19,17 +19,18 @@ class GlobalCover {
@Test
void workspace() throws Exception{
JavaParser parser = new JavaParser();
List<File> files = files(new File("C:\\Users\\jeffc\\eclipse-workspace"));
int validated = 0;
double tokens = 0;
long validated = 0;
long tokens = 0;
int count = 0;
long time = System.currentTimeMillis();
for(File file : files){
if(file.getName().endsWith(".java")){
parser.parse(new BufferedReader(new FileReader(file)), new JavaFile());
BufferedReader reader = new BufferedReader(new FileReader(file));
new JavaParser().parse(reader, new JavaFile());
reader.close();
validated += TokenValidator.MAX_VALIDATE;
tokens += TokenValidator.TOKENS;
System.out.println(file+" "+validated+" / "+tokens);
@ -41,7 +42,7 @@ class GlobalCover {
time = ((System.currentTimeMillis()-time)/1000);
String data = "{\"hash\": "+System.currentTimeMillis()+",\"time\": "+time+",\"percentage\": "+String.format("%.3f", ((validated/tokens)*100.0)).replace(",", ".")+",\"validated\": "+validated+",\"files\": "+count+"}";
String data = "{\"hash\": "+System.currentTimeMillis()+",\"time\": "+time+",\"percentage\": "+String.format("%.3f", ((validated/((double)tokens))*100.0)).replace(",", ".")+",\"validated\": "+validated+",\"files\": "+count+"}";
File file = new File("/home/tmp_.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(data+"\n");