Skip to content

Commit 9176b64

Browse files
committed
refactoring
1 parent 8f3e685 commit 9176b64

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
import com.igormaznitsa.prologparser.tokenizer.Op;
2626
import com.igormaznitsa.prologparser.tokenizer.PrologParser;
2727

28+
import java.util.Arrays;
2829
import java.util.Collections;
2930
import java.util.HashMap;
3031
import java.util.HashSet;
32+
import java.util.List;
3133
import java.util.Map;
3234
import java.util.Set;
3335
import java.util.stream.Stream;
@@ -36,21 +38,29 @@
3638

3739
public class DefaultParserContext implements ParserContext {
3840

41+
private static final Op[] EMPTY = new Op[0];
3942
protected final Set<String> opPrefixes = new HashSet<>();
4043
protected final Map<String, OpContainer> opContainers = new HashMap<>();
41-
4244
protected final int flags;
4345

44-
public DefaultParserContext(final int flags, final Op... operators) {
46+
public DefaultParserContext(final int flags, final List<Op> operators) {
4547
this.flags = flags;
46-
this.addOps(operators);
48+
this.addOps(operators.toArray(EMPTY));
49+
}
50+
51+
public DefaultParserContext(final int flags) {
52+
this(flags, Collections.emptyList());
4753
}
4854

4955
public static ParserContext of(final int flags) {
5056
return new DefaultParserContext(flags);
5157
}
5258

5359
public static ParserContext of(final int flags, final Op... operators) {
60+
return new DefaultParserContext(flags, Arrays.asList(operators));
61+
}
62+
63+
public static ParserContext of(final int flags, final List<Op> operators) {
5464
return new DefaultParserContext(flags, operators);
5565
}
5666

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
import com.igormaznitsa.prologparser.terms.TermType;
3030
import com.igormaznitsa.prologparser.utils.AssertUtils;
3131

32+
import java.util.ArrayList;
3233
import java.util.Arrays;
34+
import java.util.Collections;
35+
import java.util.List;
3336
import java.util.Locale;
3437
import java.util.stream.Collectors;
3538
import java.util.stream.Stream;
@@ -73,7 +76,7 @@ public final class Op extends SpecServiceCompound {
7376
/**
7477
* Set of operators for ISO Prolog standard.
7578
*/
76-
public static final Op[] ISO = {
79+
public static final List<Op> ISO = Collections.unmodifiableList(Arrays.asList(
7780
ISO_CLAUSES,
7881
ISO_DIRECTIVES,
7982
ISO_OR,
@@ -92,22 +95,22 @@ public final class Op extends SpecServiceCompound {
9295
ISO_UNARY_MINUS,
9396
ISO_BITWISE_NEGATION,
9497
make(100, OpAssoc.XFX, "@")
95-
};
98+
));
9699

97100
/**
98101
* Set of operators is specific for GNU Prolog use.
99102
*/
100-
public static final Op[] GNU_SPECIFIC = {
103+
public static final List<Op> GNU_SPECIFIC = Collections.unmodifiableList(Arrays.asList(
101104
GNU_STAR_THEN,
102105
GNU_DOUBLE_DOT,
103106
GNU_DIV_RDIV,
104-
GNU_UNARY_PLUS,
105-
};
107+
GNU_UNARY_PLUS
108+
));
106109

107110
/**
108111
* Set of operators is specific for SWI Prolog use
109112
*/
110-
public static final Op[] SWI_SPECIFIC = {
113+
public static final List<Op> SWI_SPECIFIC = Collections.unmodifiableList(Arrays.asList(
111114
make(1150, OpAssoc.FX, "dynamic", "discontiguous", "initialization", "meta_predicate", "module_transparent", "multifile", "public", "thread_local", "thread_initialization", "volatile"),
112115
GNU_STAR_THEN,
113116
make(990, OpAssoc.FY, ":="),
@@ -118,35 +121,35 @@ public final class Op extends SpecServiceCompound {
118121
GNU_DIV_RDIV,
119122
GNU_UNARY_PLUS,
120123
make(1, OpAssoc.FX, "$")
121-
};
124+
));
122125

123126
/**
124127
* Set of operators for SWI Prolog.
125128
*/
126-
public static final Op[] SWI = Op.join(ISO, SWI_SPECIFIC);
129+
public static final List<Op> SWI = Collections.unmodifiableList(Op.join(ISO, SWI_SPECIFIC));
127130

128131
/**
129132
* Set of Finite Domain operators for GNU Prolog.
130133
*/
131-
public static final Op[] GNU_FD = {
134+
public static final List<Op> GNU_FD = Collections.unmodifiableList(Arrays.asList(
132135
make(750, XFY, "#<=>", "#\\<=>"),
133136
make(740, XFY, "#==>", "#\\==>"),
134137
make(730, XFY, "##"),
135138
make(730, YFX, "#\\/", "#\\\\/"),
136139
make(720, YFX, "#/\\", "#\\/\\"),
137140
make(710, FY, "#\\"),
138141
make(700, XFX, "#=", "#\\=", "#<", "#=<", "#>", "#>=", "#=#", "#\\=#", "#<#", "#=<#", "#>#", "#>=#")
139-
};
142+
));
140143

141144
/**
142145
* Set of operators for GNU Prolog.
143146
*/
144-
public static final Op[] GNU = Op.join(ISO, GNU_SPECIFIC);
147+
public static final List<Op> GNU = Collections.unmodifiableList(Op.join(ISO, GNU_SPECIFIC));
145148

146149
/**
147150
* Set of Constraint Logic Programming operators for SWI Prolog.
148151
*/
149-
public static final Op[] SWI_CPL = {
152+
public static final List<Op> SWI_CPL = Collections.unmodifiableList(Arrays.asList(
150153
make(300, FY, "~"),
151154
make(500, YFX, "#"),
152155
make(760, YFX, "#<==>"),
@@ -157,8 +160,8 @@ public final class Op extends SpecServiceCompound {
157160
make(720, YFX, "#/\\"),
158161
make(710, FY, "#\\"),
159162
make(700, XFX, "#>", "#<", "#>=", "#=<", "#=", "#\\=", "in", "ins"),
160-
make(450, XFX, ".."),
161-
};
163+
make(450, XFX, "..")
164+
));
162165

163166
public static final Op VIRTUAL_OPERATOR_BLOCK = makeSystem(-1, OpAssoc.FX, "()");
164167
public static final Op METAOPERATOR_COMMA = makeSystem(1000, OpAssoc.XFY, ",");
@@ -250,8 +253,12 @@ static Op makeSystem(final int precedence, final OpAssoc type, final String... n
250253
: new Op(precedence, type, ".system.", assertOpValidOpName(names));
251254
}
252255

253-
public static Op[] join(Op[]... args) {
254-
return of(args).flatMap(Stream::of).toArray(Op[]::new);
256+
public static List<Op> join(final List<Op>... args) {
257+
final List<Op> result = new ArrayList<>();
258+
for (final List<Op> l : args) {
259+
result.addAll(l);
260+
}
261+
return result;
255262
}
256263

257264
public static Op[] add(Op[] args, Op... ops) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Collections;
2727
import java.util.HashMap;
2828
import java.util.HashSet;
29+
import java.util.List;
2930
import java.util.Map;
3031
import java.util.NoSuchElementException;
3132
import java.util.Random;
@@ -552,6 +553,10 @@ public void testOperatorHierarchy() {
552553
assertEquals("~ (A & B) <===> ~ A v ~ B", term.toString());
553554
}
554555

556+
private void assertReadTerms(final int expected, final String resource, final List<Op> ops) {
557+
assertReadTerms(expected, resource, ops.toArray(new Op[0]));
558+
}
559+
555560
private void assertReadTerms(final int expected, final String resource, final Op... ops) {
556561
final ParserContext defaultContext = of(ParserContext.FLAG_BLOCK_COMMENTS, ops);
557562
try (Reader reader = new InputStreamReader(getClass().getResourceAsStream(resource), StandardCharsets.UTF_8)) {

0 commit comments

Comments
 (0)