Skip to content

Commit 0c75461

Browse files
committed
refactoring
1 parent 7d8641b commit 0c75461

File tree

13 files changed

+55
-37
lines changed

13 files changed

+55
-37
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
import static com.igormaznitsa.prologparser.DefaultParserContext.of;
5656
import static com.igormaznitsa.prologparser.ParserContext.*;
57-
import static com.igormaznitsa.prologparser.terms.TermType.__OPERATOR_CONTAINER__;
57+
import static com.igormaznitsa.prologparser.terms.TermType.SPEC_TERM_OPERATOR_CONTAINER;
5858
import static com.igormaznitsa.prologparser.utils.Koi7CharOpMap.ofOps;
5959

6060
/**
@@ -177,7 +177,7 @@ private boolean isEndOperator(final PrologTerm operator, final Koi7CharOpMap end
177177
return false;
178178
}
179179

180-
return operator.getType() == __OPERATOR_CONTAINER__ && endOperators.contains(operator.getText());
180+
return operator.getType() == SPEC_TERM_OPERATOR_CONTAINER && endOperators.contains(operator.getText());
181181
}
182182

183183
public ParserContext getContext() {
@@ -403,7 +403,7 @@ private PrologTerm readBlock(final Koi7CharOpMap endOperators) {
403403
// same as the natural precedence)
404404
int readAtomPrecedence = 0; // we make it as highest precedence
405405

406-
if (readAtom.getType() == __OPERATOR_CONTAINER__) {
406+
if (readAtom.getType() == SPEC_TERM_OPERATOR_CONTAINER) {
407407
// it is operator list
408408
// try to get the single operator from the list if the list
409409
// contains only one
@@ -585,7 +585,7 @@ private PrologTerm readBlock(final Koi7CharOpMap endOperators) {
585585
if (currentTreeItem.getType() == TermType.OPERATOR) {
586586
// it's not first operator
587587
if (currentTreeItem.getPrecedence() <= readAtomPrecedence) {
588-
if (readAtom.getType() == TermType.OPERATOR && ((Op) readAtom).getOpAssoc().isPrefix()) {
588+
if (readAtom.getType() == TermType.OPERATOR && ((Op) readAtom).getAssoc().isPrefix()) {
589589
// it is a prefix operator so that it can be there
590590
currentTreeItem = currentTreeItem.makeAsRightBranch(readAtomTreeItem);
591591
} else {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public boolean add(final Op operator) {
6969
throw new IllegalArgumentException("Illegal operator name, must be '" + getText() + "'");
7070
}
7171

72-
switch (operator.getOpAssoc()) {
72+
switch (operator.getAssoc()) {
7373
case FX:
7474
case FY:
7575
if (this.opFZ != null) {
@@ -172,7 +172,7 @@ public boolean remove(final Op op) {
172172

173173
@Override
174174
public TermType getType() {
175-
return TermType.__OPERATOR_CONTAINER__;
175+
return TermType.SPEC_TERM_OPERATOR_CONTAINER;
176176
}
177177

178178
@Override
@@ -284,7 +284,7 @@ public Op findForType(final OpAssoc type) {
284284
throw new CriticalUnexpectedError();
285285
}
286286

287-
return result == null || result.getOpAssoc() != type ? null : result;
287+
return result == null || result.getAssoc() != type ? null : result;
288288
}
289289

290290
/**
@@ -326,22 +326,22 @@ public boolean removeForType(final OpAssoc type) {
326326
switch (type) {
327327
case FX:
328328
case FY:
329-
if (this.opFZ != null && this.opFZ.getOpAssoc() == type) {
329+
if (this.opFZ != null && this.opFZ.getAssoc() == type) {
330330
this.opFZ = null;
331331
result = true;
332332
}
333333
break;
334334
case XF:
335335
case YF:
336-
if (this.opZF != null && this.opZF.getOpAssoc() == type) {
336+
if (this.opZF != null && this.opZF.getAssoc() == type) {
337337
this.opZF = null;
338338
result = true;
339339
}
340340
break;
341341
case XFX:
342342
case YFX:
343343
case XFY:
344-
if (this.opZFZ != null && this.opZFZ.getOpAssoc() == type) {
344+
if (this.opZFZ != null && this.opZFZ.getAssoc() == type) {
345345
opZFZ = null;
346346
result = true;
347347
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import java.util.stream.StreamSupport;
3636

3737
import static com.igormaznitsa.prologparser.terms.TermType.LIST;
38-
import static com.igormaznitsa.prologparser.terms.TermType.__OPERATOR_CONTAINER__;
38+
import static com.igormaznitsa.prologparser.terms.TermType.SPEC_TERM_OPERATOR_CONTAINER;
3939
import static com.igormaznitsa.prologparser.utils.AssertUtils.assertNotNull;
4040

4141
/**
@@ -106,7 +106,7 @@ protected PrologStruct(final PrologTerm functor, final int arity, final int line
106106

107107
private static PrologTerm assertFunctor(final PrologTerm functor) {
108108
if (
109-
functor.getType() == __OPERATOR_CONTAINER__
109+
functor.getType() == SPEC_TERM_OPERATOR_CONTAINER
110110
|| functor.getType() == LIST
111111
) {
112112
throw new IllegalArgumentException("Non-allowed functor type: " + functor.getType());
@@ -220,7 +220,7 @@ public String toString() {
220220
final String text2 = getArity() > 1 ? getTermAt(1).toString()
221221
: null;
222222

223-
switch (operatorFunctor.getOpAssoc()) {
223+
switch (operatorFunctor.getAssoc()) {
224224
case FX: {
225225
builder.append(opName).append(' ');
226226

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,20 @@ public final void setLine(final int line) {
160160
this.line = line <= 0 ? -1 : line;
161161
}
162162

163+
/**
164+
* Get the term text.
165+
*
166+
* @return the term text, must not be null
167+
*/
163168
public String getText() {
164169
return this.text;
165170
}
166171

172+
/**
173+
* Get precedence of the term.
174+
*
175+
* @return precedence, must not be negative
176+
*/
167177
public int getPrecedence() {
168178
return 0;
169179
}
@@ -179,6 +189,11 @@ public String toString() {
179189
return result;
180190
}
181191

192+
/**
193+
* Get term type.
194+
*
195+
* @return term type, must not be null
196+
*/
182197
public abstract TermType getType();
183198

184199
public Stream<PrologTerm> stream() {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public final class PrologVar extends PrologTerm {
3030
private static final long serialVersionUID = 1158349084517573220L;
3131
private final boolean anonymous;
3232

33+
/**
34+
* Constructor of anonymous variable.
35+
*/
3336
public PrologVar() {
3437
this("_");
3538
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public abstract class SpecServiceCompound extends PrologCompound {
3030

3131
private static final long serialVersionUID = 71286378126323213L;
3232

33-
public SpecServiceCompound(String text) {
33+
public SpecServiceCompound(final String text) {
3434
super(text);
3535
}
3636

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ public enum TermType {
4848
/**
4949
* Internal auxiliary term contains defined operators.
5050
*/
51-
__OPERATOR_CONTAINER__
51+
SPEC_TERM_OPERATOR_CONTAINER
5252
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public static List<Op> join(final List<Op>... args) {
284284
return result;
285285
}
286286

287-
public static Op[] add(Op[] args, Op... ops) {
287+
public static Op[] add(final Op[] args, final Op... ops) {
288288
final int newLen = args.length + ops.length;
289289
final Op[] result = Arrays.copyOf(args, newLen);
290290

@@ -322,7 +322,7 @@ public TermType getType() {
322322
return TermType.OPERATOR;
323323
}
324324

325-
public OpAssoc getOpAssoc() {
325+
public OpAssoc getAssoc() {
326326
return this.opAssoc;
327327
}
328328

@@ -454,9 +454,9 @@ public boolean equals(final Object obj) {
454454
@Override
455455
public String toString() {
456456
if (isMultiName()) {
457-
return String.format("op(%d, %s, [%s]).", getPrecedence(), getOpAssoc().toString().toLowerCase(Locale.ENGLISH), of(this.multiNames).map(x -> '\'' + x + '\'').collect(Collectors.joining(",")));
457+
return String.format("op(%d, %s, [%s]).", getPrecedence(), getAssoc().toString().toLowerCase(Locale.ENGLISH), of(this.multiNames).map(x -> '\'' + x + '\'').collect(Collectors.joining(",")));
458458
} else {
459-
return String.format("op(%d, %s, '%s').", getPrecedence(), getOpAssoc().toString().toLowerCase(Locale.ENGLISH), getText());
459+
return String.format("op(%d, %s, '%s').", getPrecedence(), getAssoc().toString().toLowerCase(Locale.ENGLISH), getText());
460460
}
461461
}
462462

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public TreeItem setData(final PrologTerm term, final int line, final int pos) {
5656
this.replaceSavedTerm(null);
5757
} else {
5858
final TermType termType = term.getType();
59-
if (termType == TermType.OPERATOR || termType == TermType.__OPERATOR_CONTAINER__) {
59+
if (termType == TermType.OPERATOR || termType == TermType.SPEC_TERM_OPERATOR_CONTAINER) {
6060
final TermWrapper termWrapper = this.termWrapperPool.find().setWrappedTerm(term);
6161
this.replaceSavedTerm(termWrapper);
6262
} else {
@@ -173,13 +173,13 @@ private void replaceForOwner(final TreeItem newItem) {
173173
}
174174

175175
public OpAssoc getOpAssoc() {
176-
return ((Op) ((TermWrapper) savedTerm).getWrappedTerm()).getOpAssoc();
176+
return ((Op) ((TermWrapper) savedTerm).getWrappedTerm()).getAssoc();
177177
}
178178

179179
private boolean isOperandsOk() {
180180
if (this.savedTerm.getType() == TermType.OPERATOR) {
181181
final Op wrappedOperator = (Op) ((TermWrapper) this.savedTerm).getWrappedTerm();
182-
switch (wrappedOperator.getOpAssoc()) {
182+
switch (wrappedOperator.getAssoc()) {
183183
case FX:
184184
case FY:
185185
return this.leftBranch == null && this.rightBranch != null;
@@ -202,7 +202,7 @@ private boolean isPrecedenceOk() {
202202
if (this.savedTerm.getType() == TermType.OPERATOR) {
203203
final int thisPrecedence = this.getPrecedence();
204204
final Op wrappedOperator = (Op) ((TermWrapper) this.savedTerm).getWrappedTerm();
205-
switch (wrappedOperator.getOpAssoc()) {
205+
switch (wrappedOperator.getAssoc()) {
206206
case FX:
207207
return this.leftBranch == null && (this.rightBranch != null && this.rightBranch.getPrecedence() < thisPrecedence);
208208
case FY:

src/test/java/com/igormaznitsa/prologparser/IntegrationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public void testParseOperator() {
402402
assertEquals(":-", struct.getFunctor().getText());
403403
assertEquals(2, struct.getArity());
404404
assertEquals(OpAssoc.XFX,
405-
((Op) struct.getFunctor()).getOpAssoc());
405+
((Op) struct.getFunctor()).getAssoc());
406406
assertEquals("hello", struct.getTermAt(0).getText());
407407
assertEquals("world", struct.getTermAt(1).getText());
408408

@@ -415,7 +415,7 @@ public void testParseOperator() {
415415
assertEquals(":-", struct.getFunctor().getText());
416416
assertEquals(1, struct.getArity());
417417
assertEquals(OpAssoc.FX,
418-
((Op) struct.getFunctor()).getOpAssoc());
418+
((Op) struct.getFunctor()).getAssoc());
419419
assertEquals("test", struct.getTermAt(0).getText());
420420

421421
clearInvocations(mockContext);
@@ -427,12 +427,12 @@ public void testParseOperator() {
427427
assertEquals("is", struct.getFunctor().getText());
428428
assertEquals(2, struct.getArity());
429429
assertEquals(OpAssoc.XFX,
430-
((Op) struct.getFunctor()).getOpAssoc());
430+
((Op) struct.getFunctor()).getAssoc());
431431
assertEquals("X", struct.getTermAt(0).getText());
432432
assertEquals(TermType.STRUCT, struct.getTermAt(1).getType());
433433
assertEquals("+", struct.getTermAt(1).getFunctor().getText());
434434
assertEquals(OpAssoc.YFX,
435-
((Op) struct.getTermAt(1).getFunctor()).getOpAssoc());
435+
((Op) struct.getTermAt(1).getFunctor()).getAssoc());
436436
assertEquals(2, struct.getTermAt(1).getArity());
437437
assertEquals("X", ((PrologStruct) struct.getTermAt(1)).getTermAt(0).getText());
438438
assertEquals("1", ((PrologStruct) struct.getTermAt(1)).getTermAt(1).getText());

0 commit comments

Comments
 (0)