Skip to content

Commit 894c5ec

Browse files
committed
refactoring
1 parent a58e936 commit 894c5ec

34 files changed

+338
-255
lines changed

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import com.igormaznitsa.prologparser.terms.OpContainer;
2525
import com.igormaznitsa.prologparser.tokenizer.Op;
26-
import com.igormaznitsa.prologparser.tokenizer.PrologParser;
2726

2827
import java.util.Arrays;
2928
import java.util.Collections;
@@ -36,32 +35,36 @@
3635

3736
import static java.util.stream.IntStream.rangeClosed;
3837

38+
/**
39+
* Default implementation of parser context.
40+
* <b>It is not thread safe one!</b>
41+
*/
3942
public class DefaultParserContext implements ParserContext {
4043

4144
protected static final Op[] EMPTY = new Op[0];
4245
protected final Set<String> opPrefixes = new HashSet<>();
4346
protected final Map<String, OpContainer> opContainers = new HashMap<>();
44-
protected final int flags;
47+
protected final int parserContextFlags;
4548

46-
public DefaultParserContext(final int flags, final List<Op> operators) {
47-
this.flags = flags;
49+
public DefaultParserContext(final int parserContextFlags, final List<Op> operators) {
50+
this.parserContextFlags = parserContextFlags;
4851
this.addOps(operators.toArray(EMPTY));
4952
}
5053

51-
public DefaultParserContext(final int flags) {
52-
this(flags, Collections.emptyList());
54+
public DefaultParserContext(final int parserContextFlags) {
55+
this(parserContextFlags, Collections.emptyList());
5356
}
5457

55-
public static ParserContext of(final int flags) {
56-
return new DefaultParserContext(flags);
58+
public static ParserContext of(final int parserContextFlags) {
59+
return new DefaultParserContext(parserContextFlags);
5760
}
5861

59-
public static ParserContext of(final int flags, final Op... operators) {
60-
return new DefaultParserContext(flags, Arrays.asList(operators));
62+
public static ParserContext of(final int parserContextFlags, final Op... operators) {
63+
return new DefaultParserContext(parserContextFlags, Arrays.asList(operators));
6164
}
6265

63-
public static ParserContext of(final int flags, final List<Op> operators) {
64-
return new DefaultParserContext(flags, operators);
66+
public static ParserContext of(final int parserContextFlags, final List<Op> operators) {
67+
return new DefaultParserContext(parserContextFlags, operators);
6568
}
6669

6770
@Override
@@ -70,8 +73,8 @@ public Map<String, OpContainer> findAllOperators() {
7073
}
7174

7275
@Override
73-
public int getParseFlags() {
74-
return this.flags;
76+
public int getFlags() {
77+
return this.parserContextFlags;
7578
}
7679

7780
protected void fillPrefixes(final String name) {

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

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

2222
package com.igormaznitsa.prologparser;
2323

24-
import com.igormaznitsa.prologparser.tokenizer.PrologParser;
25-
2624
import java.io.Reader;
2725

26+
/**
27+
* Generic version of prolog parser.
28+
*/
2829
public class GenericPrologParser extends PrologParser {
2930
public GenericPrologParser(final Reader reader, final ParserContext context) {
3031
super(reader, context);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
package com.igormaznitsa.prologparser;
2323

2424
import com.igormaznitsa.prologparser.terms.OpContainer;
25-
import com.igormaznitsa.prologparser.tokenizer.PrologParser;
2625

2726
import java.util.Map;
2827

@@ -44,5 +43,5 @@ default int getMaxTokenizerBufferLength() {
4443
return Integer.MAX_VALUE;
4544
}
4645

47-
int getParseFlags();
46+
int getFlags();
4847
}

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

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

2424
import com.igormaznitsa.prologparser.terms.OpContainer;
25-
import com.igormaznitsa.prologparser.tokenizer.PrologParser;
2625
import com.igormaznitsa.prologparser.utils.AssertUtils;
2726

2827
import java.util.Map;
@@ -41,7 +40,7 @@ public ParserContextChain(final ParserContext... contexts) {
4140
this.contexts = stream(contexts).filter(Objects::nonNull).toArray(ParserContext[]::new);
4241
this.minDetectedAllowedBufferSize = Stream.of(this.contexts).mapToInt(ParserContext::getMaxTokenizerBufferLength).min().orElse(Integer.MAX_VALUE);
4342
this.tokenizerFlags = stream(this.contexts)
44-
.mapToInt(ParserContext::getParseFlags)
43+
.mapToInt(ParserContext::getFlags)
4544
.reduce(FLAG_NONE, (a, b) -> a | b);
4645
}
4746

@@ -93,7 +92,7 @@ public OpContainer findOpForName(final PrologParser source, final String name) {
9392
}
9493

9594
@Override
96-
public int getParseFlags() {
95+
public int getFlags() {
9796
return this.tokenizerFlags;
9897
}
9998
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
* under the License.
2020
*/
2121

22-
package com.igormaznitsa.prologparser.tokenizer;
22+
package com.igormaznitsa.prologparser;
2323

24-
import com.igormaznitsa.prologparser.ParserContext;
2524
import com.igormaznitsa.prologparser.exceptions.CriticalUnexpectedError;
2625
import com.igormaznitsa.prologparser.exceptions.PrologParserException;
2726
import com.igormaznitsa.prologparser.terms.OpContainer;
@@ -31,7 +30,14 @@
3130
import com.igormaznitsa.prologparser.terms.PrologTerm;
3231
import com.igormaznitsa.prologparser.terms.Quotation;
3332
import com.igormaznitsa.prologparser.terms.TermType;
33+
import com.igormaznitsa.prologparser.tokenizer.Op;
34+
import com.igormaznitsa.prologparser.tokenizer.OpAssoc;
35+
import com.igormaznitsa.prologparser.tokenizer.TermWrapper;
36+
import com.igormaznitsa.prologparser.tokenizer.Tokenizer;
37+
import com.igormaznitsa.prologparser.tokenizer.TokenizerResult;
38+
import com.igormaznitsa.prologparser.tokenizer.TreeItem;
3439
import com.igormaznitsa.prologparser.utils.AssertUtils;
40+
import com.igormaznitsa.prologparser.utils.Koi7CharOpMap;
3541
import com.igormaznitsa.prologparser.utils.SoftObjectPool;
3642

3743
import java.io.Closeable;
@@ -49,15 +55,15 @@
4955
import static com.igormaznitsa.prologparser.DefaultParserContext.of;
5056
import static com.igormaznitsa.prologparser.ParserContext.*;
5157
import static com.igormaznitsa.prologparser.terms.TermType.__OPERATOR_CONTAINER__;
52-
import static com.igormaznitsa.prologparser.tokenizer.Koi7CharOpMap.ofOps;
58+
import static com.igormaznitsa.prologparser.utils.Koi7CharOpMap.ofOps;
5359

5460
/**
5561
* Abstract base Prolog parser.
5662
*/
5763
public abstract class PrologParser implements Iterator<PrologTerm>, Iterable<PrologTerm>, Closeable {
5864

5965
public static final PrologTerm[] EMPTY_TERM_ARRAY = new PrologTerm[0];
60-
static final Koi7CharOpMap META_OP_MAP;
66+
protected static final Koi7CharOpMap META_OP_MAP;
6167
private static final int MAX_INTERNAL_POOL_SIZE = 96;
6268
private static final OpContainer OPERATOR_COMMA;
6369
private static final OpContainer OPERATOR_LEFTBRACKET;
@@ -99,8 +105,8 @@ public abstract class PrologParser implements Iterator<PrologTerm>, Iterable<Pro
99105

100106
public PrologParser(final Reader source, final ParserContext context) {
101107
this.context = context == null ? of(ParserContext.FLAG_NONE) : context;
102-
this.tokenizer = new Tokenizer(this, AssertUtils.assertNotNull(source));
103-
this.parserFlags = context == null ? FLAG_NONE : context.getParseFlags();
108+
this.tokenizer = new Tokenizer(this, META_OP_MAP, AssertUtils.assertNotNull(source));
109+
this.parserFlags = context == null ? FLAG_NONE : context.getFlags();
104110

105111
this.termArrayListPool = new SoftObjectPool<List<PrologTerm>>(MAX_INTERNAL_POOL_SIZE) {
106112
@Override
@@ -152,6 +158,10 @@ private static int findFirstCharCode(final String text) {
152158
}
153159
}
154160

161+
public static Koi7CharOpMap findMetaOps() {
162+
return Koi7CharOpMap.copyOf(META_OP_MAP);
163+
}
164+
155165
private boolean isEndOperator(final PrologTerm operator, final Koi7CharOpMap endOperators) {
156166
if (operator == null) {
157167
return true;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public int getArity() {
178178
}
179179

180180
@Override
181-
public PrologTerm getElementAt(int position) {
181+
public PrologTerm getTermAt(int position) {
182182
throw new UnsupportedOperationException("Can't get element from operator container");
183183
}
184184

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@ public PrologCompound(final String text) {
3131
super(text, Quotation.NONE);
3232
}
3333

34-
public abstract PrologTerm getElementAt(int position);
34+
/**
35+
* Get element for its position
36+
*
37+
* @param position zero-based element position
38+
* @return element in position
39+
*/
40+
public abstract PrologTerm getTermAt(int position);
3541
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public PrologFloat(final BigDecimal value, final int line, final int pos) {
6262
}
6363

6464
@Override
65-
public PrologNumeric neg() {
65+
public PrologNumeric makeNeg() {
6666
return new PrologFloat(value.negate());
6767
}
6868

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private static BigInteger valueOf(final String text) {
9191
}
9292

9393
@Override
94-
public PrologNumeric neg() {
94+
public PrologNumeric makeNeg() {
9595
return new PrologInt(value.negate());
9696
}
9797

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public static PrologList setTermAsNewListTail(final PrologList list, final Prolo
105105
}
106106

107107
@Override
108-
public PrologTerm getElementAt(final int index) {
109-
final PrologTerm result = super.getElementAt(index);
108+
public PrologTerm getTermAt(final int index) {
109+
final PrologTerm result = super.getTermAt(index);
110110
return result == null ? EMPTY_ANONYMOUS_VAR : result;
111111
}
112112

0 commit comments

Comments
 (0)