Skip to content

Commit 427edcc

Browse files
committed
fixed single operator with block
1 parent 7b90934 commit 427edcc

File tree

7 files changed

+43
-42
lines changed

7 files changed

+43
-42
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public static ParserContext of(final int parserContextFlags, final List<Op> oper
6767
return new DefaultParserContext(parserContextFlags, operators);
6868
}
6969

70-
@Override
7170
public Map<String, OpContainer> findAllOperators() {
7271
return Collections.unmodifiableMap(this.opContainers);
7372
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ public interface ParserContext {
7878
*/
7979
OpContainer findOpForName(PrologParser source, String name);
8080

81-
/**
82-
* Find all operators registered in context.
83-
*
84-
* @return map of operators to their names registered in the context, must not be null
85-
*/
86-
Map<String, OpContainer> findAllOperators();
87-
8881
/**
8982
* Get maximum allowed length value for internal parser text buffers.
9083
*

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ public int getMaxTokenizerBufferLength() {
6262
return this.minDetectedAllowedBufferSize;
6363
}
6464

65-
@Override
66-
public Map<String, OpContainer> findAllOperators() {
67-
return stream(this.contexts)
68-
.map(ParserContext::findAllOperators)
69-
.flatMap(x -> x.entrySet().stream())
70-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
71-
}
72-
7365
@Override
7466
public boolean hasOpStartsWith(final PrologParser source, final String namePrefix) {
7567
boolean result = false;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ private OpContainer(final Op operator) {
4747
operator.streamOp().forEach(this::add);
4848
}
4949

50+
private OpContainer(final String text, final Op fz, final Op zf, final Op zfz) {
51+
super(text);
52+
this.opFZ = fz;
53+
this.opZF = zf;
54+
this.opZFZ = zfz;
55+
this.numberAtContainer = (fz == null ? 0 : 1)
56+
+ (zf == null ? 0 : 1)
57+
+ (zfz == null ? 0 : 1);
58+
}
59+
5060
/**
5161
* Create new container based on operator name.
5262
*
@@ -57,6 +67,10 @@ public static OpContainer make(final Op operator) {
5767
return new OpContainer(operator);
5868
}
5969

70+
public static OpContainer male(final String text, final Op fz, final Op zf, final Op zfz) {
71+
return new OpContainer(text, fz, zf, zfz);
72+
}
73+
6074
/**
6175
* Add operator into the container.
6276
*

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
* specific language governing permissions and limitations
1919
* under the License.
2020
*/
21-
2221
package com.igormaznitsa.prologparser.tokenizer;
2322

2423
import com.igormaznitsa.prologparser.PrologParser;
@@ -227,10 +226,8 @@ private boolean isPrecedenceOk() {
227226

228227
private boolean isBlock() {
229228
return this.savedTerm.getType() == TermType.STRUCT
230-
&& (
231-
this.savedTerm.getFunctor() == Op.VIRTUAL_OPERATOR_BLOCK
232-
|| this.savedTerm.getFunctor() == Op.VIRTUAL_OPERATOR_CURLY_BLOCK
233-
);
229+
&& (this.savedTerm.getFunctor() == Op.VIRTUAL_OPERATOR_BLOCK
230+
|| this.savedTerm.getFunctor() == Op.VIRTUAL_OPERATOR_CURLY_BLOCK);
234231
}
235232

236233
private boolean isOperator() {
@@ -256,31 +253,42 @@ public PrologTerm convertToTermAndRelease() {
256253

257254
if (this.leftBranch == null) {
258255
if (this.rightBranch.getType() == TermType.STRUCT && this.rightBranch.savedTerm.isBlock() && !((PrologStruct) this.rightBranch.savedTerm).isEmpty()) {
256+
259257
final PrologTerm rightTerm = this.rightBranch.convertToTermAndRelease();
260258
Op operator = (Op) wrapper.getWrappedTerm();
261259
final PrologTerm blockContent = ((PrologStruct) rightTerm).getTermAt(0);
260+
262261
if (blockContent.getType() == TermType.STRUCT) {
263262
final PrologTerm[] terms = blockContent.flatComma(new ArrayList<>()).toArray(PrologParser.EMPTY_TERM_ARRAY);
263+
264264
if (operator.getArity() == terms.length) {
265265
return new PrologStruct(operator, terms, wrapper.getLine(), wrapper.getPos());
266266
} else {
267-
operator = this.parser.getContext().findOpForName(this.parser, operator.getText()).findForArity(terms.length);
268-
return operator == null
269-
? new PrologStruct(
270-
new PrologAtom(wrapper.getText(), Quotation.SINGLE, wrapper.getLine(), wrapper.getPos()),
271-
terms, wrapper.getLine(), wrapper.getPos())
272-
: new PrologStruct(operator, terms, wrapper.getLine(), wrapper.getPos());
267+
final Op appropriateOperator = this.parser.getContext().findOpForName(this.parser, operator.getText()).findForArity(terms.length);
268+
269+
if (appropriateOperator == null) {
270+
if (operator.getArity() == 1) {
271+
return new PrologStruct(operator, new PrologTerm[]{blockContent}, wrapper.getLine(), wrapper.getPos());
272+
} else {
273+
return new PrologStruct(
274+
new PrologAtom(wrapper.getText(), Quotation.SINGLE, wrapper.getLine(), wrapper.getPos()),
275+
terms, wrapper.getLine(), wrapper.getPos());
276+
}
277+
} else {
278+
return new PrologStruct(appropriateOperator, terms, wrapper.getLine(), wrapper.getPos());
279+
}
273280
}
281+
274282
} else {
275283
if (rightTerm.isCurlyBlock()) {
276-
return new PrologStruct(operator, new PrologTerm[] {rightTerm}, wrapper.getLine(), wrapper.getPos());
284+
return new PrologStruct(operator, new PrologTerm[]{rightTerm}, wrapper.getLine(), wrapper.getPos());
277285
} else {
278286
if (operator.getArity() == 1) {
279-
return new PrologStruct(operator, new PrologTerm[] {blockContent}, wrapper.getLine(), wrapper.getPos());
287+
return new PrologStruct(operator, new PrologTerm[]{blockContent}, wrapper.getLine(), wrapper.getPos());
280288
} else {
281289
operator = this.parser.getContext().findOpForName(this.parser, operator.getText()).findForArity(1);
282-
return operator == null ? new PrologStruct(new PrologAtom(wrapper.getText(), Quotation.SINGLE, wrapper.getLine(), wrapper.getPos()), new PrologTerm[] {blockContent}, wrapper.getLine(), wrapper.getPos())
283-
: new PrologStruct(operator, new PrologTerm[] {blockContent}, wrapper.getLine(), wrapper.getPos());
290+
return operator == null ? new PrologStruct(new PrologAtom(wrapper.getText(), Quotation.SINGLE, wrapper.getLine(), wrapper.getPos()), new PrologTerm[]{blockContent}, wrapper.getLine(), wrapper.getPos())
291+
: new PrologStruct(operator, new PrologTerm[]{blockContent}, wrapper.getLine(), wrapper.getPos());
284292
}
285293
}
286294
}
@@ -299,7 +307,7 @@ public PrologTerm convertToTermAndRelease() {
299307
if (!isPrecedenceOk()) {
300308
if (this.rightBranch != null && this.rightBranch.isOperator() && this.rightBranch.getOpAssoc().isPrefix()) {
301309
left = this.leftBranch == null ? null : this.leftBranch.convertToTermAndRelease();
302-
right = new PrologStruct(Op.VIRTUAL_OPERATOR_BLOCK, new PrologTerm[] {this.rightBranch.convertToTermAndRelease()});
310+
right = new PrologStruct(Op.VIRTUAL_OPERATOR_BLOCK, new PrologTerm[]{this.rightBranch.convertToTermAndRelease()});
303311
} else {
304312
throw new PrologParserException("Operator precedence clash or missing operator: [" + wrapper.getText() + ']', wrapper.getLine(), wrapper.getPos());
305313
}
@@ -319,17 +327,17 @@ public PrologTerm convertToTermAndRelease() {
319327

320328
final PrologStruct operatorStruct;
321329
if (left == null) {
322-
operatorStruct = new PrologStruct(wrapper.getWrappedTerm(), new PrologTerm[] {right}, wrapper.getLine(), wrapper.getPos());
330+
operatorStruct = new PrologStruct(wrapper.getWrappedTerm(), new PrologTerm[]{right}, wrapper.getLine(), wrapper.getPos());
323331
} else {
324-
operatorStruct = new PrologStruct(wrapper.getWrappedTerm(), right == null ? new PrologTerm[] {left} : new PrologTerm[] {left, right}, wrapper.getLine(), wrapper.getPos());
332+
operatorStruct = new PrologStruct(wrapper.getWrappedTerm(), right == null ? new PrologTerm[]{left} : new PrologTerm[]{left, right}, wrapper.getLine(), wrapper.getPos());
325333
}
326334
result = operatorStruct;
327335
}
328336
break;
329337
case STRUCT: {
330338
final PrologStruct thisStruct = (PrologStruct) this.savedTerm;
331339
if ((thisStruct.getFunctor() == Op.VIRTUAL_OPERATOR_BLOCK || thisStruct.getFunctor() == Op.VIRTUAL_OPERATOR_CURLY_BLOCK)
332-
&& thisStruct.getArity() == 1) {
340+
&& thisStruct.getArity() == 1) {
333341
final PrologTerm thatTerm = thisStruct.getTermAt(0);
334342
if (thatTerm.getType() == TermType.STRUCT && (thatTerm.getFunctor() == Op.VIRTUAL_OPERATOR_BLOCK || thatTerm.getFunctor() == Op.VIRTUAL_OPERATOR_CURLY_BLOCK)) {
335343
result = thatTerm;

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ public void testPairOfOperatorsWithIncompatiblePrecedence() {
10741074
@Test
10751075
public void testOperatorAsFunctorWithUnsupportedArity() {
10761076
assertEquals("':'(1, 2, 3)", parseEd(":(1,2,3).").next().toString());
1077-
assertEquals("'+'(1, 2, 3)", parseEd("+(1,2,3).").next().toString());
1077+
assertEquals("+ (1 , 2 , 3)", parseEd("+(1,2,3).").next().toString());
10781078
assertEquals("':'(1, (2 , 3), 4)", parseEd(":(1,(2,3),4).").next().toString());
10791079
assertEquals("':'(1)", parseEd(":(1).").next().toString());
10801080
}
@@ -1379,11 +1379,6 @@ public StubContext(final Map<String, OpContainer> operators) {
13791379
this.operators = operators;
13801380
}
13811381

1382-
@Override
1383-
public Map<String, OpContainer> findAllOperators() {
1384-
return Collections.emptyMap();
1385-
}
1386-
13871382
@Override
13881383
public int getMaxTokenizerBufferLength() {
13891384
return 1000;

src/test/java/com/igormaznitsa/prologparser/terms/PrologStructureTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testToString() {
4040
new PrologAtom("first"), new PrologFloat(123d),
4141
new PrologList(), new PrologVar()}).toString());
4242

43-
final Map<String, OpContainer> contextOperators = new GenericPrologParser(new StringReader(""), new DefaultParserContext(ParserContext.FLAG_NONE, Op.SWI)).getContext().findAllOperators();
43+
final Map<String, OpContainer> contextOperators = ((DefaultParserContext)new GenericPrologParser(new StringReader(""), new DefaultParserContext(ParserContext.FLAG_NONE, Op.SWI)).getContext()).findAllOperators();
4444
assertEquals("hello :- world", new PrologStruct(contextOperators.get(":-").findForType(OpAssoc.XFX),
4545
new PrologTerm[] {new PrologAtom("hello"),
4646
new PrologAtom("world")}).toString());

0 commit comments

Comments
 (0)