3939import static com .igormaznitsa .prologparser .terms .Quotation .*;
4040import static com .igormaznitsa .prologparser .terms .TermType .ATOM ;
4141import static com .igormaznitsa .prologparser .tokenizer .OpAssoc .*;
42+ import com .igormaznitsa .prologparser .tokenizer .TokenizerResult ;
4243import static java .util .stream .Collectors .joining ;
4344import static org .junit .jupiter .api .Assertions .*;
4445import static org .mockito .Mockito .*;
@@ -57,7 +58,7 @@ private static PrologParser parseIso(final String str) {
5758 private static PrologParser parseEd (final String str ) {
5859 final ParserContext parserContext = mock (ParserContext .class );
5960 when (parserContext .getMaxTokenizerBufferLength ()).thenReturn (1024 );
60- when (parserContext .getFlags ()).thenReturn (ParserContext . FLAG_BLOCK_COMMENTS | FLAG_ZERO_QUOTATION_CHARCODE | FLAG_CURLY_BRACKETS );
61+ when (parserContext .getFlags ()).thenReturn (FLAG_BLOCK_COMMENTS | FLAG_ZERO_QUOTATION_CHARCODE | FLAG_CURLY_BRACKETS );
6162 return parseEd (str , parserContext );
6263 }
6364
@@ -84,6 +85,30 @@ private static GenericPrologParser parseGen(final String str) {
8485 return new GenericPrologParser (new StringReader (str ), parserContext );
8586 }
8687
88+ @ Test
89+ public void testReadingClausesTokensAndCharsFromSameParser () throws IOException {
90+ final PrologParser parser = parseEd ("some(a). alone. next(b).c" );
91+ final PrologTerm term1 = parser .next ();
92+ assertEquals ("some(a)" , term1 .toString ());
93+ final TokenizerResult token1 = parser .getInternalTokenizer ().readNextToken ();
94+ assertEquals ("alone" , token1 .getResult ().toString ());
95+ final TokenizerResult token2 = parser .getInternalTokenizer ().readNextToken ();
96+ assertEquals ("." , token2 .getResult ().getText ());
97+ final PrologTerm term2 = parser .next ();
98+ assertEquals ("next(b)" , term2 .toString ());
99+ assertEquals ('c' , parser .getInternalTokenizer ().readChar ());
100+ assertFalse (parser .hasNext ());
101+ assertEquals (-1 , parser .getInternalTokenizer ().readChar ());
102+ }
103+
104+ @ Test
105+ public void testEmptyAndNonClause () {
106+ assertThrows (NoSuchElementException .class , () -> parseEd ("" ).next ());
107+ assertThrows (NoSuchElementException .class , () -> parseEd ("." ).next ());
108+ assertThrows (PrologParserException .class , () -> parseEd ("ab" ).next ());
109+ assertThrows (PrologParserException .class , () -> parseEd ("1" ).next ());
110+ }
111+
87112 @ Test
88113 public void testSwiCpl () {
89114 assertEquals ("X in 5 .. 10 , 5 , Y #=< X + -1 , 6 , Y in 4 .. 8" , parseCpl ("X in 5..10,5,Y#=<X+ -1,6,Y in 4..8." ).next ().toString ());
@@ -975,6 +1000,8 @@ private String parseSortAndJoin(final String text) {
9751000
9761001 @ Test
9771002 public void testStringWithIsoControl () {
1003+ assertEquals ("[97, 98, 99]" , parseIso ("[0'a,0'b,0'c]." ).next ().toString ());
1004+ assertEquals ("\u0007 \b \f \n \r \t \u000B #\u0013 \\ \' \" `" , parseIso ("'\\ a\\ b\\ f\\ n\\ r\\ t\\ v\\ x23\\ \\ 23\\ \\ \\ \\ \' \\ \" \\ `'." ).next ().getText ());
9781005 assertThrows (PrologParserException .class , () -> parseEd ("'hello\u0000 world'." ).next ());
9791006 assertEquals ("'hello\\ nworld'" , parseEd ("'hello\\ \n world'." ).next ().toString ());
9801007 assertEquals ("'hello\\ nworld'" , parseEd ("'hello\\ \r \n world'." ).next ().toString ());
0 commit comments