Skip to content

Commit 7d8641b

Browse files
committed
refactoring
1 parent 1b450f2 commit 7d8641b

29 files changed

+413
-340
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ protected void fillPrefixes(final String name) {
8383

8484
public DefaultParserContext addOps(final Op... operators) {
8585
Stream.of(operators).flatMap(Op::streamOp).forEach(x -> {
86-
fillPrefixes(x.getTermText());
87-
OpContainer container = this.opContainers.get(x.getTermText());
86+
fillPrefixes(x.getText());
87+
OpContainer container = this.opContainers.get(x.getText());
8888
if (container == null) {
8989
container = OpContainer.make(x);
90-
this.opContainers.put(x.getTermText(), container);
90+
this.opContainers.put(x.getText(), container);
9191
} else {
9292
container.add(x);
9393
}

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private boolean isEndOperator(final PrologTerm operator, final Koi7CharOpMap end
177177
return false;
178178
}
179179

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

183183
public ParserContext getContext() {
@@ -191,7 +191,7 @@ public boolean hasNext() {
191191
if (found != null) {
192192
final TokenizerResult endAtom = this.tokenizer.readNextToken();
193193
try {
194-
if (endAtom == null || !endAtom.getResult().getTermText().equals(OPERATOR_DOT.getTermText())) {
194+
if (endAtom == null || !endAtom.getResult().getText().equals(OPERATOR_DOT.getText())) {
195195
throw new PrologParserException("End operator is not found", this.tokenizer.getLine(), this.tokenizer.getPos());
196196
}
197197
} finally {
@@ -237,7 +237,7 @@ private PrologStruct readStruct(final PrologTerm functor) {
237237
}
238238

239239
try {
240-
final String nextText = nextAtom.getResult().getTermText();
240+
final String nextText = nextAtom.getResult().getText();
241241

242242
switch (getOnlyCharCode(nextText)) {
243243
case ',': {
@@ -283,7 +283,7 @@ private PrologTerm readList(final TokenizerResult openingBracket) {
283283
}
284284

285285
try {
286-
final String text = nextAtom.getResult().getTermText();
286+
final String text = nextAtom.getResult().getText();
287287

288288
switch (getOnlyCharCode(text)) {
289289
case ']': {
@@ -308,8 +308,8 @@ private PrologTerm readList(final TokenizerResult openingBracket) {
308308
rightPart = readBlock(OPERATORS_END_LIST);
309309

310310
if (rightPart != null
311-
&& rightPart.getTermType() == TermType.STRUCT
312-
&& rightPart.getFunctor().getTermText().equals(OPERATOR_VERTICALBAR.getTermText())) {
311+
&& rightPart.getType() == TermType.STRUCT
312+
&& rightPart.getFunctor().getText().equals(OPERATOR_VERTICALBAR.getText())) {
313313
throw new PrologParserException(
314314
"Duplicated list tail definition",
315315
tokenizer.getLastTokenLine(),
@@ -321,7 +321,7 @@ private PrologTerm readList(final TokenizerResult openingBracket) {
321321
throw new PrologParserException("Can't find expected token in list", this.tokenizer.getLine(), this.tokenizer.getPos());
322322
}
323323
try {
324-
if (!nextAtomTwo.getResult().getTermText().equals(OPERATOR_RIGHTSQUAREBRACKET.getTermText())) {
324+
if (!nextAtomTwo.getResult().getText().equals(OPERATOR_RIGHTSQUAREBRACKET.getText())) {
325325
throw new PrologParserException("Wrong end of the list tail", this.tokenizer.getLastTokenLine(), this.tokenizer.getLastTokenPos());
326326
}
327327
} finally {
@@ -356,10 +356,10 @@ private PrologTerm readList(final TokenizerResult openingBracket) {
356356
if (rightPart == null) {
357357
throw new PrologParserException("There is not any term as the tail at the list", this.tokenizer.getLastTokenLine(), this.tokenizer.getLastTokenPos());
358358
}
359-
if (rightPart.getTermType() == TermType.ATOM && rightPart.getQuotation() == Quotation.NONE && ",".equals(rightPart.getTermText())) {
359+
if (rightPart.getType() == TermType.ATOM && rightPart.getQuotation() == Quotation.NONE && ",".equals(rightPart.getText())) {
360360
throw new PrologParserException("Comma operator in list tail", this.tokenizer.getLastTokenLine(), this.tokenizer.getLastTokenPos());
361361
}
362-
leftPartFirst.replaceTail(rightPart);
362+
leftPartFirst.replaceEndListElement(rightPart);
363363
}
364364
return leftPartFirst;
365365
}
@@ -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.getTermType() == __OPERATOR_CONTAINER__) {
406+
if (readAtom.getType() == __OPERATOR_CONTAINER__) {
407407
// it is operator list
408408
// try to get the single operator from the list if the list
409409
// contains only one
@@ -436,18 +436,18 @@ private PrologTerm readBlock(final Koi7CharOpMap endOperators) {
436436
if (readAtom == null) {
437437
if (currentTreeItem == null && !(leftPresented || rightPresented)) {
438438
// alone operator, it is an atom
439-
return new PrologAtom(readOperators.getTermText(), Quotation.SINGLE, readOperators.getLine(), readOperators.getPos());
439+
return new PrologAtom(readOperators.getText(), Quotation.SINGLE, readOperators.getLine(), readOperators.getPos());
440440
}
441441
// we didn't get any operator for our criteria, so throw
442442
// an exception
443-
throw new PrologParserException("Operator clash detected [" + readAtomContainer.getResult().getTermText() + ']',
443+
throw new PrologParserException("Operator clash detected [" + readAtomContainer.getResult().getText() + ']',
444444
readAtomContainer.getLine(), readAtomContainer.getPos());
445445
}
446446
// we have found needed operator so get its precedence
447447
readAtomPrecedence = readAtom.getPrecedence();
448448
} else {
449449
readAtom = readOperator;
450-
final String operatorText = readOperator.getTermText();
450+
final String operatorText = readOperator.getText();
451451

452452
if (operatorText.length() == 1) {
453453
final int onlyCharCode = getOnlyCharCode(operatorText);
@@ -503,7 +503,7 @@ private PrologTerm readBlock(final Koi7CharOpMap endOperators) {
503503
token.release();
504504
}
505505

506-
if (closingAtom == null || !closingAtom.getTermText().equals((onlyCharCode == '{' ? OPERATOR_RIGHTCURLYBRACKET : OPERATOR_RIGHTBRACKET).getTermText())) {
506+
if (closingAtom == null || !closingAtom.getText().equals((onlyCharCode == '{' ? OPERATOR_RIGHTCURLYBRACKET : OPERATOR_RIGHTBRACKET).getText())) {
507507
throw new PrologParserException("Non-closed brackets: " + onlyCharCode, this.tokenizer.getLine(), this.tokenizer.getPos());
508508
}
509509
}
@@ -519,22 +519,22 @@ private PrologTerm readBlock(final Koi7CharOpMap endOperators) {
519519
}
520520
}
521521
} else {
522-
if (readAtom.getTermType() != TermType.VAR || (this.parserFlags & FLAG_VAR_AS_FUNCTOR) != 0) {
522+
if (readAtom.getType() != TermType.VAR || (this.parserFlags & FLAG_VAR_AS_FUNCTOR) != 0) {
523523
TokenizerResult nextToken = this.tokenizer.readNextToken();
524524

525525
if (nextToken == null) {
526526
throw new PrologParserException("Non-closed clause", this.tokenizer.getLastTokenLine(), this.tokenizer.getLastTokenPos());
527527
}
528528

529529
try {
530-
if (nextToken.getResult().getTermText().equals(OPERATOR_LEFTBRACKET.getTermText())) {
530+
if (nextToken.getResult().getText().equals(OPERATOR_LEFTBRACKET.getText())) {
531531
final int nextTokenLineNumber = nextToken.getLine();
532532
final int nextTokenStrPosition = nextToken.getPos();
533533

534534
// it is a structure
535535
if (
536-
readAtom.getTermType() == TermType.ATOM
537-
|| (readAtom.getTermType() == TermType.VAR
536+
readAtom.getType() == TermType.ATOM
537+
|| (readAtom.getType() == TermType.VAR
538538
&& (this.parserFlags & FLAG_VAR_AS_FUNCTOR) != 0)
539539
) {
540540

@@ -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.getTermType() == TermType.OPERATOR && ((Op) readAtom).getOpAssoc().isPrefix()) {
588+
if (readAtom.getType() == TermType.OPERATOR && ((Op) readAtom).getOpAssoc().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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public final class OpContainer extends SpecServiceCompound {
4343
private int numberAtContainer;
4444

4545
private OpContainer(final Op operator) {
46-
super(operator.getTermText());
46+
super(operator.getText());
4747
operator.streamOp().forEach(this::add);
4848
}
4949

@@ -65,8 +65,8 @@ public static OpContainer make(final Op operator) {
6565
* @throws IllegalArgumentException if operator has different name
6666
*/
6767
public boolean add(final Op operator) {
68-
if (!getTermText().equals(operator.getTermText())) {
69-
throw new IllegalArgumentException("Illegal operator name, must be '" + getTermText() + "'");
68+
if (!getText().equals(operator.getText())) {
69+
throw new IllegalArgumentException("Illegal operator name, must be '" + getText() + "'");
7070
}
7171

7272
switch (operator.getOpAssoc()) {
@@ -101,6 +101,9 @@ public boolean add(final Op operator) {
101101
return true;
102102
}
103103

104+
/**
105+
* Remove all saved operators.
106+
*/
104107
public void clear() {
105108
this.opFZ = null;
106109
this.opZF = null;
@@ -144,7 +147,7 @@ public Op findForArity(final int arity) {
144147
* @throws IllegalArgumentException if operator name is not compatible with the container
145148
*/
146149
public boolean remove(final Op op) {
147-
if (!getTermText().equals(op.getTermText())) {
150+
if (!getText().equals(op.getText())) {
148151
throw new IllegalArgumentException(
149152
"Unexpected operator: " + op);
150153
}
@@ -168,7 +171,7 @@ public boolean remove(final Op op) {
168171
}
169172

170173
@Override
171-
public TermType getTermType() {
174+
public TermType getType() {
172175
return TermType.__OPERATOR_CONTAINER__;
173176
}
174177

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public PrologAtom(final String text) {
3737
}
3838

3939
public PrologAtom(final PrologTerm term) {
40-
super(term.getTermText(), findQuotation(term.getTermText()), term.getLine(), term.getPos());
40+
super(term.getText(), findQuotation(term.getText()), term.getLine(), term.getPos());
4141
}
4242

4343
public PrologAtom(final String text, final int line, final int pos) {
@@ -54,7 +54,7 @@ public Quotation getQuotation() {
5454
}
5555

5656
@Override
57-
public TermType getTermType() {
57+
public TermType getType() {
5858
return TermType.ATOM;
5959
}
6060

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public String toString() {
101101
}
102102

103103
@Override
104-
public String getTermText() {
104+
public String getText() {
105105
return toString();
106106
}
107107

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public PrologList addAsNewListToEndOfListChain(final PrologTerm term) {
151151
break;
152152
} else {
153153
final PrologTerm leftTail = result.elements[1];
154-
if (leftTail.getTermType() == TermType.LIST) {
154+
if (leftTail.getType() == TermType.LIST) {
155155
result = (PrologList) leftTail;
156156
} else {
157157
final PrologList newOne = new PrologList(term, new PrologList());
@@ -165,22 +165,28 @@ public PrologList addAsNewListToEndOfListChain(final PrologTerm term) {
165165
return result;
166166
}
167167

168-
public void replaceTail(final PrologTerm newTailElement) {
168+
/**
169+
* Replace last tail element in list chain.
170+
* @param newTailElement new element
171+
*/
172+
public void replaceEndListElement(final PrologTerm newTailElement) {
173+
PrologList current = this;
169174

170-
PrologList curList = this;
171175
while (!Thread.currentThread().isInterrupted()) {
172-
final PrologTerm tail = curList.elements[1];
173-
if (tail.getTermType() == TermType.LIST) {
176+
final PrologTerm tail = current.elements[1];
177+
178+
if (tail.getType() == TermType.LIST) {
174179
final PrologList leftTail = (PrologList) tail;
175180
if (leftTail.isEmpty()) {
176-
curList.setTail(newTailElement);
181+
current.setTail(newTailElement);
177182
break;
178183
}
179184
} else {
180-
curList.setTail(newTailElement);
185+
current.setTail(newTailElement);
181186
break;
182187
}
183-
curList = (PrologList) tail;
188+
189+
current = (PrologList) tail;
184190
}
185191
}
186192

@@ -199,7 +205,7 @@ public void setElementAt(final int index, final PrologTerm term) {
199205

200206

201207
@Override
202-
public TermType getTermType() {
208+
public TermType getType() {
203209
return TermType.LIST;
204210
}
205211

@@ -214,7 +220,7 @@ public String toString() {
214220
PrologTerm list = this;
215221

216222
while (!Thread.currentThread().isInterrupted()) {
217-
if (list.getTermType() == TermType.LIST) {
223+
if (list.getType() == TermType.LIST) {
218224
final PrologList asList = (PrologList) list;
219225

220226
if (asList.isEmpty()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public PrologNumeric(final int line, final int pos) {
4444
public abstract Number getNumber();
4545

4646
@Override
47-
public final TermType getTermType() {
47+
public final TermType getType() {
4848
return TermType.ATOM;
4949
}
5050

5151
@Override
52-
public String getTermText() {
52+
public String getText() {
5353
return toString();
5454
}
5555

0 commit comments

Comments
 (0)