Skip to content

Commit f2714ec

Browse files
committed
added support of space chars after zero quotation with FLAG_ZERO_QUOTATION_ALLOWS_WHITESPACE_CHAR
1 parent 92e1e07 commit f2714ec

File tree

5 files changed

+93
-73
lines changed

5 files changed

+93
-73
lines changed

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.igormaznitsa</groupId>
66
<artifactId>java-prolog-parser</artifactId>
7-
<version>2.0.1</version>
7+
<version>2.0.2-SNAPHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>Edinburgh Prolog parser</name>
@@ -64,19 +64,19 @@
6464
<dependency>
6565
<groupId>org.junit.jupiter</groupId>
6666
<artifactId>junit-jupiter-api</artifactId>
67-
<version>5.4.2</version>
67+
<version>5.5.1</version>
6868
<scope>test</scope>
6969
</dependency>
7070
<dependency>
7171
<groupId>org.junit.jupiter</groupId>
7272
<artifactId>junit-jupiter-engine</artifactId>
73-
<version>5.4.2</version>
73+
<version>5.5.1</version>
7474
<scope>test</scope>
7575
</dependency>
7676
<dependency>
7777
<groupId>org.mockito</groupId>
7878
<artifactId>mockito-core</artifactId>
79-
<version>2.28.2</version>
79+
<version>3.0.0</version>
8080
<scope>test</scope>
8181
</dependency>
8282
</dependencies>
@@ -140,7 +140,7 @@
140140
<plugin>
141141
<groupId>org.apache.maven.plugins</groupId>
142142
<artifactId>maven-javadoc-plugin</artifactId>
143-
<version>3.1.0</version>
143+
<version>3.1.1</version>
144144
<configuration>
145145
<failOnError>true</failOnError>
146146
<show>protected</show>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public interface ParserContext {
6161
* Recognize '.'(_,_) as a list term.
6262
*/
6363
int FLAG_DOT2_AS_LIST = 32;
64+
/**
65+
* White-space char is allowed in zero quotation mode.
66+
* @since 2.0.2
67+
*/
68+
int FLAG_ZERO_QUOTATION_ALLOWS_WHITESPACE_CHAR = 64;
6469

6570
/**
6671
* Check that the context contains an operator starts with some string

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public final class Tokenizer {
5454
private final StringBuilderEx insideCharBuffer;
5555
private final boolean blockCommentsAllowed;
5656
private final boolean zeroSingleQuotationAllowed;
57+
private final boolean zeroQuotationAllowsWhitespaceChar;
5758
private final Reader reader;
5859
private final PrologParser parser;
5960
private final Koi7CharOpMap metaOperators;
@@ -76,6 +77,7 @@ public Tokenizer(final PrologParser parser, final Koi7CharOpMap metaOperators, f
7677
final int maxAllowedCharBufferSize = parser.getContext() == null ? Integer.MAX_VALUE : parser.getContext().getMaxTokenizerBufferLength();
7778
this.blockCommentsAllowed = parser.getContext() != null && ((parser.getContext().getFlags() & ParserContext.FLAG_BLOCK_COMMENTS) != 0);
7879
this.zeroSingleQuotationAllowed = parser.getContext() != null && ((parser.getContext().getFlags() & ParserContext.FLAG_ZERO_QUOTATION_CHARCODE) != 0);
80+
this.zeroQuotationAllowsWhitespaceChar = parser.getContext() != null && ((parser.getContext().getFlags() & ParserContext.FLAG_ZERO_QUOTATION_ALLOWS_WHITESPACE_CHAR) != 0);
7981

8082
this.strBuf = new StringBuilderEx(32, maxAllowedCharBufferSize);
8183
this.specCharBuf = new StringBuilderEx(8, maxAllowedCharBufferSize);
@@ -865,15 +867,15 @@ state, getLastTokenLine(),
865867
theChar = chr;
866868
}
867869
if (charCodeAsInt) {
868-
if (Character.isWhitespace(chr)) {
870+
if (Character.isWhitespace(chr) && !this.zeroQuotationAllowsWhitespaceChar) {
869871
throw new PrologParserException("Unexpected whitespace char: 0x" + Integer.toHexString(chr), this.prevLine, this.prevPos);
870872
} else {
871-
return new TokenizerResult(
872-
new PrologInt(theChar),
873-
state,
874-
getLastTokenLine(),
875-
getLastTokenPos()
876-
);
873+
return new TokenizerResult(
874+
new PrologInt(theChar),
875+
state,
876+
getLastTokenLine(),
877+
getLastTokenPos()
878+
);
877879
}
878880
} else {
879881
strBuffer.append(theChar);

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public PrologParser parseEd(final String str) {
5656
return parseEd(str, parserContext);
5757
}
5858

59+
public PrologParser parseEd(final String str, final int parseFlags) {
60+
final ParserContext parserContext = mock(ParserContext.class);
61+
when(parserContext.getMaxTokenizerBufferLength()).thenReturn(1024);
62+
when(parserContext.getFlags()).thenReturn(parseFlags);
63+
return parseEd(str, parserContext);
64+
}
65+
5966
public PrologParser parseEd(final String str, final ParserContext context) {
6067
return parseEd(new StringReader(str), context);
6168
}
@@ -84,7 +91,7 @@ public void assertReadTerms(final int expected, final String resource, final Lis
8491
}
8592

8693
public void assertReadTerms(final int expected, final String resource, final Op... ops) {
87-
final ParserContext defaultContext = of(ParserContext.FLAG_BLOCK_COMMENTS, ops);
94+
final ParserContext defaultContext = of(ParserContext.FLAG_BLOCK_COMMENTS | ParserContext.FLAG_ZERO_QUOTATION_CHARCODE | ParserContext.FLAG_ZERO_QUOTATION_ALLOWS_WHITESPACE_CHAR, ops);
8895
try (Reader reader = new InputStreamReader(getClass().getResourceAsStream(resource), StandardCharsets.UTF_8)) {
8996
final PrologParser parser = parseEd(reader, defaultContext);
9097
assertEquals(expected, parser.stream().count());

0 commit comments

Comments
 (0)