Skip to content

Commit 0e099aa

Browse files
committed
refactoring
1 parent 13d8bb5 commit 0e099aa

File tree

2 files changed

+24
-35
lines changed

2 files changed

+24
-35
lines changed

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.igormaznitsa.prologparser.tokenizer.OpAssoc;
3333
import com.igormaznitsa.prologparser.tokenizer.TermWrapper;
3434
import com.igormaznitsa.prologparser.utils.SoftObjectPool;
35-
3635
import java.util.ArrayList;
3736

3837
final class AstItem {
@@ -135,27 +134,19 @@ public TermType getType() {
135134

136135
public AstItem findRoot() {
137136
AstItem result = this;
138-
while (!Thread.currentThread().isInterrupted()) {
139-
final AstItem theParent = result.parentItem;
140-
if (theParent == null) {
141-
break;
142-
} else {
143-
result = theParent;
144-
}
137+
AstItem theParent;
138+
while ((theParent = result.parentItem) != null) {
139+
result = theParent;
145140
}
146141
return result;
147142
}
148143

149144
public AstItem findFirstNodeWithSuchOrLowerPrecedence(final int precedence) {
150145
AstItem result = this;
151146

152-
while (!Thread.currentThread().isInterrupted()) {
153-
final AstItem itsParent = result.parentItem;
154-
if (itsParent == null || result.getPrecedence() >= precedence) {
155-
break;
156-
} else {
157-
result = itsParent;
158-
}
147+
AstItem itsParent;
148+
while ((itsParent = result.parentItem) != null && result.getPrecedence() < precedence) {
149+
result = itsParent;
159150
}
160151

161152
return result;
@@ -271,17 +262,17 @@ public PrologTerm convertToTermAndRelease() {
271262
if (operator.getArity() == terms.length) {
272263
return new PrologStruct(operator, terms, wrapper.getLine(), wrapper.getPos());
273264
} else {
274-
final Op appropriateOperator = this.parser.getContext().findOpForName(this.parser, operator.getText()).findForArity(terms.length);
275-
276-
if (appropriateOperator == null) {
277-
if (operator.getArity() == 1) {
278-
return new PrologStruct(operator, new PrologTerm[]{blockContent}, wrapper.getLine(), wrapper.getPos());
279-
} else {
280-
return new PrologStruct(new PrologAtom(wrapper.getText(), Quotation.SINGLE, wrapper.getLine(), wrapper.getPos()), terms, wrapper.getLine(), wrapper.getPos());
281-
}
265+
final Op appropriateOperator = this.parser.getContext().findOpForName(this.parser, operator.getText()).findForArity(terms.length);
266+
267+
if (appropriateOperator == null) {
268+
if (operator.getArity() == 1) {
269+
return new PrologStruct(operator, new PrologTerm[]{blockContent}, wrapper.getLine(), wrapper.getPos());
282270
} else {
283-
return new PrologStruct(appropriateOperator, terms, wrapper.getLine(), wrapper.getPos());
271+
return new PrologStruct(new PrologAtom(wrapper.getText(), Quotation.SINGLE, wrapper.getLine(), wrapper.getPos()), terms, wrapper.getLine(), wrapper.getPos());
284272
}
273+
} else {
274+
return new PrologStruct(appropriateOperator, terms, wrapper.getLine(), wrapper.getPos());
275+
}
285276
}
286277

287278
} else {
@@ -347,8 +338,8 @@ public PrologTerm convertToTermAndRelease() {
347338
final PrologTerm thatTerm = thisStruct.getTermAt(0);
348339

349340
if (thatTerm.getType() == TermType.STRUCT && (thatTerm.getFunctor() == Op.VIRTUAL_OPERATOR_BLOCK || thatTerm.getFunctor() == Op.VIRTUAL_OPERATOR_CURLY_BLOCK)) {
350-
// rolling normal blocks
351-
result = thatTerm.isBlock() && (this.isBlock()&& (this.parentItem == null || (this.parentItem != null && this.parentItem.isBlock()))) ? thatTerm : thisStruct;
341+
// rolling normal blocks
342+
result = thatTerm.isBlock() && (this.isBlock() && (this.parentItem == null || (this.parentItem != null && this.parentItem.isBlock()))) ? thatTerm : thisStruct;
352343
} else {
353344
result = thisStruct;
354345
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
package com.igormaznitsa.prologparser;
2222

23+
import static com.igormaznitsa.prologparser.DefaultParserContext.of;
24+
import static com.igormaznitsa.prologparser.ParserContext.*;
2325
import com.igormaznitsa.prologparser.exceptions.CriticalUnexpectedError;
2426
import com.igormaznitsa.prologparser.exceptions.PrologParserException;
2527
import com.igormaznitsa.prologparser.terms.OpContainer;
@@ -29,15 +31,16 @@
2931
import com.igormaznitsa.prologparser.terms.PrologTerm;
3032
import com.igormaznitsa.prologparser.terms.Quotation;
3133
import com.igormaznitsa.prologparser.terms.TermType;
34+
import static com.igormaznitsa.prologparser.terms.TermType.SPEC_TERM_OPERATOR_CONTAINER;
3235
import com.igormaznitsa.prologparser.tokenizer.Op;
3336
import com.igormaznitsa.prologparser.tokenizer.OpAssoc;
3437
import com.igormaznitsa.prologparser.tokenizer.TermWrapper;
3538
import com.igormaznitsa.prologparser.tokenizer.Tokenizer;
3639
import com.igormaznitsa.prologparser.tokenizer.TokenizerResult;
3740
import com.igormaznitsa.prologparser.utils.AssertUtils;
3841
import com.igormaznitsa.prologparser.utils.Koi7CharOpMap;
42+
import static com.igormaznitsa.prologparser.utils.Koi7CharOpMap.ofOps;
3943
import com.igormaznitsa.prologparser.utils.SoftObjectPool;
40-
4144
import java.io.Closeable;
4245
import java.io.IOException;
4346
import java.io.Reader;
@@ -50,11 +53,6 @@
5053
import java.util.stream.Stream;
5154
import java.util.stream.StreamSupport;
5255

53-
import static com.igormaznitsa.prologparser.DefaultParserContext.of;
54-
import static com.igormaznitsa.prologparser.ParserContext.*;
55-
import static com.igormaznitsa.prologparser.terms.TermType.SPEC_TERM_OPERATOR_CONTAINER;
56-
import static com.igormaznitsa.prologparser.utils.Koi7CharOpMap.ofOps;
57-
5856
/**
5957
* Abstract base Prolog parser.
6058
*/
@@ -137,7 +135,7 @@ public final AstItem get() {
137135
public Tokenizer getInternalTokenizer() {
138136
return this.tokenizer;
139137
}
140-
138+
141139
public static Op findBaseMetaOperator(final String text, final OpAssoc type) {
142140
if (text.length() != 1) {
143141
return null;
@@ -224,7 +222,7 @@ private PrologStruct readStruct(final PrologTerm functor) {
224222
try {
225223
PrologStruct result;
226224
boolean active = true;
227-
while (active && !Thread.currentThread().isInterrupted()) {
225+
while (active) {
228226
final PrologTerm block = readBlock(OPERATORS_INSIDE_STRUCT);
229227

230228
if (block == null) {
@@ -274,7 +272,7 @@ private PrologTerm readList(final TokenizerResult openingBracket) {
274272

275273
boolean doRead = true;
276274

277-
while (doRead && !Thread.currentThread().isInterrupted()) {
275+
while (doRead) {
278276
final PrologTerm block = readBlock(OPERATORS_INSIDE_LIST);
279277

280278
final TokenizerResult nextAtom = this.tokenizer.readNextToken();

0 commit comments

Comments
 (0)