Skip to content

Commit 515bbd1

Browse files
committed
refactoring for static analyzers
1 parent 9a21f81 commit 515bbd1

26 files changed

+354
-313
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<packaging>jar</packaging>
99

1010
<name>Edinburgh Prolog parser</name>
11-
<description>It is a hand-written prolog parser, it allows to parse prolog sources written in Edinburgh Prolog style</description>
11+
<description>It is a handwritten prolog parser, it allows to parse prolog sources written in Edinburgh Prolog style</description>
1212
<url>https://github.com/raydac/java-prolog-parser</url>
1313

1414
<properties>
@@ -208,7 +208,7 @@
208208

209209
<plugin>
210210
<artifactId>maven-surefire-plugin</artifactId>
211-
<version>3.2.1</version>
211+
<version>3.2.2</version>
212212
</plugin>
213213

214214
<plugin>

src/main/java/com/igormaznitsa/prologparser/ParserContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*
2929
* @see PrologParser
3030
*/
31+
@SuppressWarnings("GrazieInspection")
3132
public interface ParserContext {
3233
/**
3334
* Empty flags, no any flag defined.

src/main/java/com/igormaznitsa/prologparser/PrologParser.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
/**
5959
* Abstract base Prolog parser.
6060
*/
61+
@SuppressWarnings({"GrazieInspection", "unused"})
6162
public abstract class PrologParser implements Iterable<PrologTerm>, Closeable {
6263

6364
public static final PrologTerm[] EMPTY_TERM_ARRAY = new PrologTerm[0];
@@ -77,7 +78,7 @@ public abstract class PrologParser implements Iterable<PrologTerm>, Closeable {
7778
private static final Koi7CharOpMap OPERATORS_SUBBLOCK;
7879
private static final Koi7CharOpMap OPERATORS_SUBBLOCK_CURLY;
7980

80-
private volatile boolean autoCloseReaderFlag;
81+
private boolean autoCloseReaderFlag;
8182

8283
static {
8384
META_OP_MAP = ofOps();
@@ -110,6 +111,11 @@ public PrologParser(final Reader source, final ParserContext context) {
110111
this.tokenizer = new Tokenizer(this, META_OP_MAP, requireNonNull(source));
111112
}
112113

114+
/**
115+
* Set flag to close provided reader automatically during close.
116+
* @return the parser instance
117+
* @see PrologParser#close()
118+
*/
113119
public PrologParser autoCloseReader() {
114120
this.autoCloseReaderFlag = true;
115121
return this;
@@ -330,13 +336,12 @@ private void checkForNull(final Object obj, final String message, final Tokenize
330336
}
331337
}
332338

333-
@SuppressWarnings("ConstantConditions")
334339
private PrologTerm readBlock(final Koi7CharOpMap endOperators) {
335340
// the variable will contain last processed tree item contains either
336341
// atom or operator
337342
AstItem currentTreeItem = null;
338343

339-
while (!Thread.currentThread().isInterrupted()) {
344+
while (true) {
340345
// read next atom from tokenizer
341346
TokenizerResult readAtomContainer = this.tokenizer.readNextToken();
342347

@@ -434,22 +439,17 @@ private PrologTerm readBlock(final Koi7CharOpMap endOperators) {
434439
}
435440

436441
if (processReadAtom) {
437-
boolean emptyCurly = false;
438442
if (readAtom == null) {
439443
if (onlyCharCode == '{') {
440-
emptyCurly = true;
444+
readAtom = new PrologStruct(Op.VIRTUAL_OPERATOR_CURLY_BLOCK, EMPTY_TERM_ARRAY, readAtomContainer.getLine(), readAtomContainer.getPos());
441445
} else {
442446
throw new PrologParserException("Illegal start of term",
443447
readAtomContainer.getLine(), readAtomContainer.getPos());
444448
}
445-
}
446-
447-
if (emptyCurly) {
448-
readAtom = new PrologStruct(Op.VIRTUAL_OPERATOR_CURLY_BLOCK, EMPTY_TERM_ARRAY, readAtomContainer.getLine(), readAtomContainer.getPos());
449449
} else {
450-
readAtom.setLine(readAtomContainer.getLine());
451-
readAtom.setPos(readAtomContainer.getPos());
452-
readAtom = new PrologStruct(onlyCharCode == '{' ? Op.VIRTUAL_OPERATOR_CURLY_BLOCK : Op.VIRTUAL_OPERATOR_BLOCK, new PrologTerm[] {readAtom}, readAtomContainer.getLine(), readAtomContainer.getPos());
450+
readAtom.setLine(readAtomContainer.getLine());
451+
readAtom.setPos(readAtomContainer.getPos());
452+
readAtom = new PrologStruct(onlyCharCode == '{' ? Op.VIRTUAL_OPERATOR_CURLY_BLOCK : Op.VIRTUAL_OPERATOR_BLOCK, new PrologTerm[]{readAtom}, readAtomContainer.getLine(), readAtomContainer.getPos());
453453
}
454454

455455
final TokenizerResult token = this.tokenizer.readNextToken();
@@ -635,4 +635,5 @@ public Stream<PrologTerm> stream() {
635635
public void close() throws IOException {
636636
this.tokenizer.close(this.autoCloseReaderFlag);
637637
}
638+
638639
}

src/main/java/com/igormaznitsa/prologparser/terms/OpContainer.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
/**
3434
* Container of operators which have same name but differently associative.
3535
*/
36+
@SuppressWarnings("unused")
3637
public final class OpContainer extends InternalSpecialCompoundTerm {
3738

3839
private static final long serialVersionUID = 4946799717661204529L;
@@ -370,11 +371,6 @@ public boolean removeForType(final OpAssoc type) {
370371
return result;
371372
}
372373

373-
@Override
374-
public int getPrecedence() {
375-
return 0;
376-
}
377-
378374
@Override
379375
public String toString() {
380376
final StringBuilderEx result = new StringBuilderEx("OpContainer ");

src/main/java/com/igormaznitsa/prologparser/terms/PrologAtom.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,9 @@ public PrologAtom(final String text, final Quotation quotingType, final int line
4848
super(text, quotingType, line, pos);
4949
}
5050

51-
@Override
52-
public Quotation getQuotation() {
53-
return this.quotation;
54-
}
55-
5651
@Override
5752
public TermType getType() {
5853
return TermType.ATOM;
5954
}
6055

61-
@Override
62-
public int getPrecedence() {
63-
return 0;
64-
}
65-
6656
}

src/main/java/com/igormaznitsa/prologparser/terms/PrologList.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void setTail(final PrologTerm term) {
162162
* Add term as new list into the end of the list chain.
163163
*
164164
* @param term to be added as new list in the end of chain
165-
* @return generated list (may be this)
165+
* @return generated list, the same list can be returned
166166
*/
167167
public PrologList addAsNewListToEndOfListChain(final PrologTerm term) {
168168

@@ -172,7 +172,7 @@ public PrologList addAsNewListToEndOfListChain(final PrologTerm term) {
172172
setHead(term);
173173
setTail(new PrologList());
174174
} else {
175-
while (!Thread.currentThread().isInterrupted()) {
175+
while (true) {
176176
if (result.isEmpty()) {
177177
result.setHead(term);
178178
result.setTail(new PrologList());
@@ -201,7 +201,7 @@ public PrologList addAsNewListToEndOfListChain(final PrologTerm term) {
201201
public void replaceEndListElement(final PrologTerm newTailElement) {
202202
PrologList current = this;
203203

204-
while (!Thread.currentThread().isInterrupted()) {
204+
while (true) {
205205
final PrologTerm tail = current.elements[1];
206206

207207
if (tail.getType() == TermType.LIST) {
@@ -248,7 +248,7 @@ public String toString() {
248248
boolean notFirst = false;
249249
PrologTerm list = this;
250250

251-
while (!Thread.currentThread().isInterrupted()) {
251+
while (true) {
252252
if (list.getType() == TermType.LIST) {
253253
final PrologList asList = (PrologList) list;
254254

src/main/java/com/igormaznitsa/prologparser/terms/PrologStruct.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
/**
4141
* Representation of prolog structure.
4242
*/
43+
@SuppressWarnings("SameParameterValue")
4344
public class PrologStruct extends PrologCompound implements Iterable<PrologTerm> {
4445

4546
public static final PrologAtom EMPTY_ATOM = new PrologAtom("", Quotation.SINGLE);
@@ -348,7 +349,7 @@ public String toString() {
348349
/**
349350
* Check that the structure has elements.
350351
*
351-
* @return true if there is not elements, false otherwise
352+
* @return true if there is no elements, false otherwise
352353
*/
353354
public boolean isEmpty() {
354355
return this.elements.length == 0;

src/main/java/com/igormaznitsa/prologparser/terms/PrologTerm.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public PrologTerm(final String text, final Quotation quotation, final int line,
6868
public static Quotation findQuotation(final String atomText) {
6969
Quotation result = NONE;
7070

71-
if (atomText.length() == 0) {
71+
if (atomText.isEmpty()) {
7272
result = SINGLE;
7373
} else {
7474
char chr = atomText.charAt(0);
@@ -242,32 +242,37 @@ public int compareTo(final PrologTerm that) {
242242
}
243243
break;
244244
case ATOM: {
245-
if (that.getType() == ATOM) {
246-
if (this instanceof PrologNumeric) {
247-
if (that instanceof PrologNumeric) {
248-
if (this instanceof PrologInt) {
249-
if (that instanceof PrologInt) {
250-
result = ((PrologInt) this).getIntValue().compareTo(((PrologInt) that).getIntValue());
245+
if (null == that.getType()) {
246+
result = -1;
247+
} else switch (that.getType()) {
248+
case ATOM:
249+
if (this instanceof PrologNumeric) {
250+
if (that instanceof PrologNumeric) {
251+
if (this instanceof PrologInt) {
252+
if (that instanceof PrologInt) {
253+
result = ((PrologInt) this).getIntValue().compareTo(((PrologInt) that).getIntValue());
254+
} else {
255+
result = new BigDecimal(((PrologInt) this).getIntValue()).compareTo(((PrologFloat) that).getFloatValue());
256+
}
257+
} else if (that instanceof PrologInt) {
258+
result = ((PrologFloat) this).getFloatValue().compareTo((new BigDecimal(((PrologInt) that).getIntValue())));
259+
} else {
260+
result = ((PrologFloat) this).getFloatValue().compareTo(((PrologFloat) that).getFloatValue());
261+
}
262+
} else {
263+
result = -1;
264+
}
265+
} else if (that instanceof PrologNumeric) {
266+
result = 1;
251267
} else {
252-
result = new BigDecimal(((PrologInt) this).getIntValue()).compareTo(((PrologFloat) that).getFloatValue());
253-
}
254-
} else if (that instanceof PrologInt) {
255-
result = ((PrologFloat) this).getFloatValue().compareTo((new BigDecimal(((PrologInt) that).getIntValue())));
256-
} else {
257-
result = ((PrologFloat) this).getFloatValue().compareTo(((PrologFloat) that).getFloatValue());
258-
}
259-
} else {
260-
result = -1;
261-
}
262-
} else if (that instanceof PrologNumeric) {
263-
result = 1;
264-
} else {
265-
result = this.getText().compareTo(that.getText());
266-
}
267-
} else if (that.getType() == VAR) {
268-
result = 1;
269-
} else {
270-
result = -1;
268+
result = this.getText().compareTo(that.getText());
269+
} break;
270+
case VAR:
271+
result = 1;
272+
break;
273+
default:
274+
result = -1;
275+
break;
271276
}
272277
}
273278
break;

src/main/java/com/igormaznitsa/prologparser/terms/Quotation.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
package com.igormaznitsa.prologparser.terms;
2323

24-
import com.igormaznitsa.prologparser.utils.StringUtils;
25-
2624
import static com.igormaznitsa.prologparser.utils.StringUtils.escapeString;
2725

2826
/**
@@ -37,7 +35,7 @@ public enum Quotation {
3735
* Term is single quotation
3836
* example: 'hello'
3937
*/
40-
SINGLE("\'"),
38+
SINGLE("'"),
4139
/**
4240
* Term is double quotation
4341
* example: "hello"

src/main/java/com/igormaznitsa/prologparser/tokenizer/Op.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
/**
4444
* Prolog operator definition.
4545
*/
46+
@SuppressWarnings("unused")
4647
public final class Op extends PrologTerm {
4748

4849
public static final int PRECEDENCE_MAX = 0;
@@ -238,7 +239,7 @@ private static String assertOpValidOpName(final String name) {
238239
}
239240

240241
/**
241-
* Make operator descriptor describing bunch of operators with same
242+
* Make operator descriptor describing a bunch of operators with same
242243
* characteristics but differently named.
243244
*
244245
* @param precedence the precedence

0 commit comments

Comments
 (0)