5050
5151import static com .igormaznitsa .prologparser .DefaultParserContext .of ;
5252import static com .igormaznitsa .prologparser .ParserContext .*;
53+ import static com .igormaznitsa .prologparser .tokenizer .Op .METAOPERATOR_COMMA ;
54+ import static com .igormaznitsa .prologparser .tokenizer .Op .METAOPERATOR_VERTICAL_BAR ;
5355import static com .igormaznitsa .prologparser .utils .Koi7CharOpMap .ofOps ;
5456import static java .util .Objects .requireNonNull ;
5557
@@ -88,7 +90,7 @@ public abstract class PrologParser implements Iterable<PrologTerm>, Closeable {
8890 META_OP_MAP .add (Op .METAOPERATOR_LEFT_SQUARE_BRACKET );
8991 OPERATOR_RIGHTSQUAREBRACKET = META_OP_MAP .add (Op .METAOPERATOR_RIGHT_SQUARE_BRACKET );
9092 OPERATOR_VERTICALBAR = META_OP_MAP .add (Op .METAOPERATOR_VERTICAL_BAR );
91- OPERATOR_COMMA = META_OP_MAP .add (Op . METAOPERATOR_COMMA );
93+ OPERATOR_COMMA = META_OP_MAP .add (METAOPERATOR_COMMA );
9294
9395 OPERATORS_PHRASE = ofOps (OPERATOR_DOT );
9496 OPERATORS_INSIDE_LIST = ofOps (OPERATOR_COMMA , OPERATOR_RIGHTSQUAREBRACKET , OPERATOR_VERTICALBAR );
@@ -108,7 +110,7 @@ public PrologParser(final Reader source, final ParserContext context) {
108110 this .parserFlags = context == null ? FLAG_NONE : context .getFlags ();
109111 }
110112
111- public PrologParser autoCloseReader (){
113+ public PrologParser autoCloseReader () {
112114 this .autoCloseReaderFlag = true ;
113115 return this ;
114116 }
@@ -302,9 +304,21 @@ private PrologTerm readList(final TokenizerResult openingBracket) {
302304 if (rightPart == null ) {
303305 throw new PrologParserException ("There is not any term as the tail at the list" , this .tokenizer .getLastTokenLine (), this .tokenizer .getLastTokenPos ());
304306 }
305- if (rightPart .getType () == TermType .ATOM && rightPart .getQuotation () == Quotation .NONE && "," .equals (rightPart .getText ())) {
307+
308+ if (rightPart .getType () == TermType .STRUCT
309+ && (rightPart .getFunctor () == METAOPERATOR_COMMA
310+ || rightPart .getFunctor () == METAOPERATOR_VERTICAL_BAR )
311+ ) {
312+ throw new PrologParserException ("Unexpected comma or bar in rest of list" , this .tokenizer .getLastTokenLine (), this .tokenizer .getLastTokenPos ());
313+ }
314+
315+ if (rightPart .getType () == TermType .ATOM
316+ && rightPart .getQuotation () == Quotation .NONE
317+ && "," .equals (rightPart .getText ())
318+ ) {
306319 throw new PrologParserException ("Comma operator in list tail" , this .tokenizer .getLastTokenLine (), this .tokenizer .getLastTokenPos ());
307320 }
321+
308322 leftPartFirst .replaceEndListElement (rightPart );
309323 }
310324 return leftPartFirst ;
0 commit comments