Skip to content

Commit d5d026b

Browse files
committed
refactoring
1 parent 9176b64 commit d5d026b

File tree

18 files changed

+174
-115
lines changed

18 files changed

+174
-115
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,29 @@ public final class PrologAtom extends PrologTerm {
2828

2929
private static final long serialVersionUID = -1829006002358498466L;
3030

31-
public PrologAtom(final String text, final QuotingType quotingType) {
31+
public PrologAtom(final String text, final Quotation quotingType) {
3232
super(text, quotingType);
3333
}
3434

3535
public PrologAtom(final String text) {
36-
this(text, findAppropriateQuoting(text));
36+
this(text, findQuotation(text));
3737
}
3838

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

4343
public PrologAtom(final String text, final int line, final int pos) {
44-
this(text, findAppropriateQuoting(text), line, pos);
44+
this(text, findQuotation(text), line, pos);
4545
}
4646

47-
public PrologAtom(final String text, final QuotingType quotingType, final int line, final int pos) {
47+
public PrologAtom(final String text, final Quotation quotingType, final int line, final int pos) {
4848
super(text, quotingType, line, pos);
4949
}
5050

5151
@Override
52-
public QuotingType getQuotingType() {
53-
return this.quotingType;
52+
public Quotation getQuotation() {
53+
return this.quotation;
5454
}
5555

5656
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
package com.igormaznitsa.prologparser.terms;
2323

2424
/**
25-
* Abstract class for all compound terms.
25+
* Abstract class for all compound prolog terms.
2626
*/
2727
public abstract class PrologCompound extends PrologTerm {
2828
private static final long serialVersionUID = 723482637840123123L;
2929

3030
public PrologCompound(final String text) {
31-
super(text, QuotingType.NO_QUOTED);
31+
super(text, Quotation.NONE);
3232
}
3333

3434
public abstract PrologTerm getElementAt(int position);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* Representation of prolog list term.
3737
*/
3838
public final class PrologList extends PrologStruct implements Iterable<PrologTerm> {
39-
public static final PrologTerm LIST_FUNCTOR = new PrologAtom(".", PrologAtom.QuotingType.SINGLE_QUOTED);
39+
public static final PrologTerm LIST_FUNCTOR = new PrologAtom(".", Quotation.SINGLE);
4040
private static final long serialVersionUID = -3781631438477816869L;
4141

4242
private static final PrologVar EMPTY_ANONYMOUS_VAR = new PrologVar("_");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public abstract class PrologNumeric extends PrologTerm {
2929
private static final long serialVersionUID = -1815562758090770438L;
3030

3131
public PrologNumeric() {
32-
super("", QuotingType.NO_QUOTED);
32+
super("", Quotation.NONE);
3333
}
3434

3535
public PrologNumeric(final int line, final int pos) {
36-
super("", QuotingType.NO_QUOTED, line, pos);
36+
super("", Quotation.NONE, line, pos);
3737
}
3838

3939
public abstract Number getNumber();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
*/
4545
public class PrologStruct extends PrologCompound implements Iterable<PrologTerm> {
4646

47-
public static final PrologAtom EMPTY_ATOM = new PrologAtom("", PrologAtom.QuotingType.SINGLE_QUOTED);
47+
public static final PrologAtom EMPTY_ATOM = new PrologAtom("", Quotation.SINGLE);
4848
private static final long serialVersionUID = 9000641998734217154L;
4949

5050
protected final PrologTerm functor;

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

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
package com.igormaznitsa.prologparser.terms;
2323

2424
import com.igormaznitsa.prologparser.exceptions.CriticalUnexpectedError;
25-
import com.igormaznitsa.prologparser.utils.StringUtils;
2625

2726
import java.io.Serializable;
2827
import java.math.BigDecimal;
2928
import java.util.List;
3029
import java.util.stream.Stream;
3130

31+
import static com.igormaznitsa.prologparser.terms.Quotation.NONE;
32+
import static com.igormaznitsa.prologparser.terms.Quotation.SINGLE;
33+
import static com.igormaznitsa.prologparser.terms.TermType.ATOM;
34+
import static com.igormaznitsa.prologparser.terms.TermType.VAR;
3235
import static com.igormaznitsa.prologparser.utils.AssertUtils.assertNotNull;
3336

3437
/**
@@ -37,39 +40,39 @@
3740
public abstract class PrologTerm implements Serializable, Comparable<PrologTerm> {
3841

3942
private static final long serialVersionUID = 1402429096900255841L;
40-
protected final QuotingType quotingType;
43+
protected final Quotation quotation;
4144
protected final String text;
4245
private int line;
4346
private int pos;
4447

45-
public PrologTerm(final String text, final QuotingType quotingType) {
48+
public PrologTerm(final String text, final Quotation quotation) {
4649
this.text = assertNotNull(text);
4750
this.pos = -1;
4851
this.line = -1;
49-
this.quotingType = quotingType;
52+
this.quotation = quotation == null ? NONE : quotation;
5053
}
5154

52-
public PrologTerm(final String text, final QuotingType quotingType, final int line, final int pos) {
53-
this(text, quotingType);
55+
public PrologTerm(final String text, final Quotation quotation, final int line, final int pos) {
56+
this(text, quotation);
5457
setLine(line);
5558
setPos(pos);
5659
}
5760

58-
public static QuotingType findAppropriateQuoting(final String atomText) {
59-
QuotingType result = QuotingType.NO_QUOTED;
61+
public static Quotation findQuotation(final String atomText) {
62+
Quotation result = NONE;
6063

6164
if (atomText.length() == 0) {
62-
result = QuotingType.SINGLE_QUOTED;
65+
result = SINGLE;
6366
} else {
6467
char chr = atomText.charAt(0);
6568
if (!Character.isLetter(chr) || Character.isDigit(chr) || Character.isUpperCase(chr) || Character.isISOControl(chr) || Character.isWhitespace(chr)) {
66-
result = QuotingType.SINGLE_QUOTED;
69+
result = SINGLE;
6770
} else {
6871

6972
for (int i = 1; i < atomText.length(); i++) {
7073
chr = atomText.charAt(i);
7174
if (Character.isWhitespace(chr) || (chr != '_' && !Character.isLetterOrDigit(chr)) || Character.isISOControl(chr)) {
72-
result = QuotingType.SINGLE_QUOTED;
75+
result = SINGLE;
7376
break;
7477
}
7578
}
@@ -91,8 +94,8 @@ public boolean isBlock() {
9194
return false;
9295
}
9396

94-
public QuotingType getQuotingType() {
95-
return this.quotingType;
97+
public Quotation getQuotation() {
98+
return this.quotation;
9699
}
97100

98101
public final int getPos() {
@@ -122,10 +125,10 @@ public int getPrecedence() {
122125
@Override
123126
public String toString() {
124127
final String result;
125-
if (this.quotingType == QuotingType.NO_QUOTED) {
128+
if (this.quotation == NONE) {
126129
result = this.text;
127130
} else {
128-
result = this.quotingType.makeString(this.text);
131+
result = this.quotation.quotateString(this.text);
129132
}
130133
return result;
131134
}
@@ -145,15 +148,15 @@ public int compareTo(final PrologTerm that) {
145148
final int result;
146149
switch (this.getTermType()) {
147150
case VAR: {
148-
if (that.getTermType() == TermType.VAR) {
151+
if (that.getTermType() == VAR) {
149152
result = this.getTermText().compareTo(that.getTermText());
150153
} else {
151154
result = -1;
152155
}
153156
}
154157
break;
155158
case ATOM: {
156-
if (that.getTermType() == TermType.ATOM) {
159+
if (that.getTermType() == ATOM) {
157160
if (this instanceof PrologNumeric) {
158161
if (that instanceof PrologNumeric) {
159162
if (this instanceof PrologInt) {
@@ -175,7 +178,7 @@ public int compareTo(final PrologTerm that) {
175178
} else {
176179
result = this.getTermText().compareTo(that.getTermText());
177180
}
178-
} else if (that.getTermType() == TermType.VAR) {
181+
} else if (that.getTermType() == VAR) {
179182
result = 1;
180183
} else {
181184
result = -1;
@@ -209,25 +212,4 @@ public int compareTo(final PrologTerm that) {
209212
return result;
210213
}
211214

212-
public enum QuotingType {
213-
NO_QUOTED(""),
214-
SINGLE_QUOTED("\'"),
215-
DOUBLE_QUOTED("\""),
216-
BACK_QUOTED("`");
217-
218-
private final String quote;
219-
220-
QuotingType(final String quote) {
221-
this.quote = quote;
222-
}
223-
224-
public String getDelimiter() {
225-
return this.quote;
226-
}
227-
228-
public String makeString(final String atomText) {
229-
return this.quote + StringUtils.escapeString(atomText, this) + this.quote;
230-
}
231-
}
232-
233215
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public PrologVar(final int line, final int pos) {
4141
}
4242

4343
public PrologVar(final String text) {
44-
super(text, QuotingType.NO_QUOTED);
44+
super(text, Quotation.NONE);
4545

4646
final char startWith = AssertUtils.assertStringNotNullAndNotEmpty(text).charAt(0);
4747

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2011-2018 Igor Maznitsa. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.igormaznitsa.prologparser.terms;
23+
24+
import com.igormaznitsa.prologparser.utils.StringUtils;
25+
26+
/**
27+
* Type of quotation for prolog term.
28+
*/
29+
public enum Quotation {
30+
/**
31+
* Term doesn't have any quotation.
32+
*/
33+
NONE(""),
34+
/**
35+
* Term is single quotation
36+
* example: 'hello'
37+
*/
38+
SINGLE("\'"),
39+
/**
40+
* Term is double quotation
41+
* example: "hello"
42+
*/
43+
DOUBLE("\""),
44+
/**
45+
* Term is back tick quotation
46+
* example: `hello`
47+
*/
48+
BACK_TICK("`");
49+
50+
private final String quotationMark;
51+
52+
Quotation(final String quotationMark) {
53+
this.quotationMark = quotationMark;
54+
}
55+
56+
/**
57+
* Get quotation mark.
58+
* @return the quotation mark as string
59+
*/
60+
public String getQuotationMark() {
61+
return this.quotationMark;
62+
}
63+
64+
/**
65+
* Quotate string.
66+
* @param str string to quotate, can be null
67+
* @return quotated string
68+
*/
69+
public String quotateString(final String str) {
70+
return this.quotationMark + StringUtils.escapeString(str == null ? "" : str, this) + this.quotationMark;
71+
}
72+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.igormaznitsa.prologparser.terms.PrologList;
3030
import com.igormaznitsa.prologparser.terms.PrologStruct;
3131
import com.igormaznitsa.prologparser.terms.PrologTerm;
32+
import com.igormaznitsa.prologparser.terms.Quotation;
3233
import com.igormaznitsa.prologparser.terms.TermType;
3334
import com.igormaznitsa.prologparser.utils.AssertUtils;
3435
import com.igormaznitsa.prologparser.utils.SoftObjectPool;
@@ -339,7 +340,7 @@ private PrologTerm readList(final TokenizerResult openingBracket) {
339340
if (rightPart == null) {
340341
throw new PrologParserException("There is not any term as the tail at the list", this.tokenizer.getLastTokenLine(), this.tokenizer.getLastTokenPos());
341342
}
342-
if (rightPart.getTermType() == TermType.ATOM && rightPart.getQuotingType() == PrologTerm.QuotingType.NO_QUOTED && ",".equals(rightPart.getTermText())) {
343+
if (rightPart.getTermType() == TermType.ATOM && rightPart.getQuotation() == Quotation.NONE && ",".equals(rightPart.getTermText())) {
343344
throw new PrologParserException("Comma operator in list tail", this.tokenizer.getLastTokenLine(), this.tokenizer.getLastTokenPos());
344345
}
345346
leftPartFirst.replaceTail(rightPart);
@@ -419,7 +420,7 @@ private PrologTerm readBlock(final Koi7CharOpMap endOperators) {
419420
if (readAtom == null) {
420421
if (currentTreeItem == null && !(leftPresented || rightPresented)) {
421422
// alone operator, it is an atom
422-
return new PrologAtom(readOperators.getTermText(), PrologTerm.QuotingType.SINGLE_QUOTED, readOperators.getLine(), readOperators.getPos());
423+
return new PrologAtom(readOperators.getTermText(), Quotation.SINGLE, readOperators.getLine(), readOperators.getPos());
423424
}
424425
// we didn't get any operator for our criteria, so throw
425426
// an exception

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

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

2222
package com.igormaznitsa.prologparser.tokenizer;
2323

24-
import com.igormaznitsa.prologparser.terms.PrologAtom;
2524
import com.igormaznitsa.prologparser.terms.PrologTerm;
25+
import com.igormaznitsa.prologparser.terms.Quotation;
2626
import com.igormaznitsa.prologparser.terms.SpecServiceCompound;
2727
import com.igormaznitsa.prologparser.terms.TermType;
2828
import com.igormaznitsa.prologparser.utils.SoftObjectPool;
@@ -58,8 +58,8 @@ TermWrapper setWrappedTerm(final PrologTerm wrappedTerm) {
5858
}
5959

6060
@Override
61-
public PrologAtom.QuotingType getQuotingType() {
62-
return this.wrappedTerm.getQuotingType();
61+
public Quotation getQuotation() {
62+
return this.wrappedTerm.getQuotation();
6363
}
6464

6565
@Override

0 commit comments

Comments
 (0)