1818 * specific language governing permissions and limitations
1919 * under the License.
2020 */
21-
2221package com .igormaznitsa .prologparser .tokenizer ;
2322
2423import 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 ;
0 commit comments