Skip to content

Commit

Permalink
Improve words detection (empty sequences)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ney committed Nov 28, 2019
1 parent 72ef7f1 commit f53033a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/chrisney/parser/JavaCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private ArrayList<CodeBlock> getBlocksByType(CodeBlock.BlockType type, ArrayLis
*/
public void addImport(String packageName) {

String code = "import " + packageName + ";";
String code = "\nimport " + packageName.trim() + ";";

JavaParser javaParser = new JavaParser();
JavaCode javaCode = javaParser.parse(code);
Expand All @@ -154,7 +154,7 @@ public void addAttribute(String attributeCode, String className) throws ClassNot
if (blockClass == null) throw new ClassNotFoundException("Class '" + className + "' not found!");

JavaParser javaParser = new JavaParser();
JavaCode javaCode = javaParser.parse(attributeCode);
JavaCode javaCode = javaParser.parse("\n\n" + attributeCode.trim());
CodeBlock block = javaCode.getAllBlocks().get(0);
block.hasParent = true;

Expand All @@ -181,7 +181,7 @@ public void addFunction(String functionCode, String className) throws ClassNotFo
if (blockClass == null) throw new ClassNotFoundException("Class '" + className + "' not found!");

JavaParser javaParser = new JavaParser();
JavaCode javaCode = javaParser.parse(functionCode);
JavaCode javaCode = javaParser.parse("\n\n" + functionCode.trim());
CodeBlock block = javaCode.getFunctions().get(0);
block.hasParent = true;

Expand Down
19 changes: 14 additions & 5 deletions src/main/java/com/chrisney/parser/JavaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ private ArrayList<CodeBlock> parse(String source, CodeBlock parent, ArrayList<Co
if (word == null && !TextUtils.inCharactersList(charBreaks, curChar)) {
word = new CodeString(i);

} else if (TextUtils.isEmptyChar(curChar) || TextUtils.inCharactersList(charBreaks, curChar) ||
isEndBlockComment(currentBlock, curChar, prevChar)) {
}
if (TextUtils.isEmptyChar(curChar) || TextUtils.inCharactersList(charBreaks, curChar) ||
isEndBlockComment(currentBlock, curChar, prevChar)
// || (word != null && TextUtils.isEmptyChar(source.charAt(word.start)) && !TextUtils.isEmptyChar(curChar))
) {

// End of current word, then create word object:
if (word != null) {
Expand All @@ -213,7 +216,7 @@ private ArrayList<CodeBlock> parse(String source, CodeBlock parent, ArrayList<Co
}

// Add word to current block:
block.words.add(word);
if (!word.value.isEmpty()) block.words.add(word);
}

// Start new word:
Expand All @@ -222,7 +225,7 @@ private ArrayList<CodeBlock> parse(String source, CodeBlock parent, ArrayList<Co
word.value = String.valueOf(curChar);

// Add words to current block:
if (block != null) block.words.add(word);
if (block != null && !word.value.isEmpty()) block.words.add(word);
word = null;
}

Expand Down Expand Up @@ -455,9 +458,13 @@ private static void parseClassName(CodeBlock block) {
}
}

private boolean isEmptyWord(CodeString word) {
return word.value == null || TextUtils.isEmpty(word.value.trim());
}

private CodeString getFirstNoneEmptyWord(ArrayList<CodeString> words) {
for (CodeString word : words) {
if (!TextUtils.isEmpty(word.value.trim())) return word;
if (!isEmptyWord(word)) return word;
}
return null;
}
Expand All @@ -479,6 +486,8 @@ private CodeBlock.BlockType getBlockType(CodeBlock block) {
return CodeBlock.BlockType.Function;
} else {
for (CodeString word : block.words) {
if (isEmptyWord(word)) continue;

if (word.value.equals(sPackage)) return CodeBlock.BlockType.Package;
if (word.value.equals(sImport)) return CodeBlock.BlockType.Import;
if (word.value.equals(sStatic)) return CodeBlock.BlockType.Attribute;
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/TestJavaParser.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.chrisney.parser.CodeBlock;
import com.chrisney.parser.JavaCode;
import com.chrisney.parser.JavaParser;
import com.chrisney.utils.Utils;
Expand All @@ -8,10 +9,19 @@
public class TestJavaParser {

public static void main(String[] args) {
testImportParser();
testInsertArray();
testJavaParser();
}

public static void testImportParser() {
String code = "\nimport com.chrisney.parser.JavaParser;";
JavaParser javaParser = new JavaParser();
JavaCode javaCode = javaParser.parse(code);
CodeBlock blockImport = javaCode.getAllBlocks().get(0);
System.out.print(blockImport);
}

public static void testInsertArray() {

ArrayList array1 = Utils.toArrayList(new Integer[]{0, 1, 2, 3, 4, 5, 6});
Expand Down Expand Up @@ -209,7 +219,7 @@ public static void testJavaParser() {
c.addAttribute("public String mValueTest = \"Hello World\";");

stringCode = c.toCode(false);
//System.out.println(stringCode);
System.out.println(stringCode);
} catch (Exception ex) {
ex.printStackTrace();
}
Expand Down

0 comments on commit f53033a

Please sign in to comment.