Skip to content

Commit 1563972

Browse files
committed
refactoring
1 parent 4d2922f commit 1563972

File tree

6 files changed

+179
-143
lines changed

6 files changed

+179
-143
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public interface ParserContext {
7474
* @see TokenizedCommentListener
7575
* @since 2.2.0
7676
*/
77-
int FLAG_COMMENTS_AWARENESS = 128;
77+
int FLAG_COMMENTS_AS_ATOMS = 128;
7878

7979
/**
8080
* Check that the context contains an operator starts with some string.
@@ -106,13 +106,16 @@ default int getMaxTokenizerBufferLength() {
106106
/**
107107
* get parser flags for the parser context.
108108
*
109-
* @return flags as bit field
110-
* @see ParserContext#FLAG_NONE
111-
* @see ParserContext#FLAG_VAR_AS_FUNCTOR
112-
* @see ParserContext#FLAG_BLOCK_COMMENTS
113-
* @see ParserContext#FLAG_CURLY_BRACKETS
114-
* @see ParserContext#FLAG_ZERO_QUOTATION_CHARCODE
115-
* @see ParserContext#FLAG_ZERO_STRUCT
109+
* @return flags as bit field combining flag options
110+
* @see #FLAG_NONE
111+
* @see #FLAG_BLOCK_COMMENTS
112+
* @see #FLAG_CURLY_BRACKETS
113+
* @see #FLAG_VAR_AS_FUNCTOR
114+
* @see #FLAG_ZERO_STRUCT
115+
* @see #FLAG_ZERO_QUOTATION_CHARCODE
116+
* @see #FLAG_COMMENTS_AS_ATOMS
117+
* @see #FLAG_DOT2_AS_LIST
118+
* @see #FLAG_ZERO_QUOTATION_ALLOWS_WHITESPACE_CHAR
116119
*/
117120
int getFlags();
118121
}

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public abstract class PrologParser implements Iterable<PrologTerm>, AutoCloseabl
103103
protected final ParserContext context;
104104
protected final int parserFlags;
105105
private final Tokenizer tokenizer;
106-
private boolean autoCloseReaderFlag;
107106
private final List<TokenizedCommentListener> commentTokenListeners;
107+
private PrologTermReadResult deferredReadTerm;
108108

109109
protected PrologParser(
110110
final Reader source,
@@ -149,17 +149,18 @@ public static Koi7CharOpMap findMetaOps() {
149149
return Koi7CharOpMap.copyOf(META_OP_MAP);
150150
}
151151

152+
private static boolean isComment(final TokenizerResult tokenizerResult) {
153+
return tokenizerResult != null
154+
&& tokenizerResult.getResult().getType() == TermType.ATOM
155+
&& (tokenizerResult.getResult().getQuotation() == Quotation.COMMENT_LINE
156+
|| tokenizerResult.getResult().getQuotation() == Quotation.COMMENT_BLOCK);
157+
}
158+
152159
/**
153-
* Set flag to close provided reader automatically during close.
160+
* Access to the internal tokenizer, it is mainly for test purposes because parser makes reading into internal buffers.
154161
*
155-
* @return the parser instance
156-
* @see PrologParser#close()
162+
* @return the internal tokenizer in use by the parser
157163
*/
158-
public PrologParser autoCloseReader() {
159-
this.autoCloseReaderFlag = true;
160-
return this;
161-
}
162-
163164
public Tokenizer getInternalTokenizer() {
164165
return this.tokenizer;
165166
}
@@ -180,15 +181,6 @@ public ParserContext getContext() {
180181
return context;
181182
}
182183

183-
private PrologTermReadResult deferredReadTerm;
184-
185-
private static boolean isComment(final TokenizerResult tokenizerResult) {
186-
return tokenizerResult != null
187-
&& tokenizerResult.getResult().getType() == TermType.ATOM
188-
&& (tokenizerResult.getResult().getQuotation() == Quotation.COMMENT_LINE
189-
|| tokenizerResult.getResult().getQuotation() == Quotation.COMMENT_BLOCK);
190-
}
191-
192184
private void notifyCommentTokenListeners(final TokenizerResult commentToken) {
193185
for (final TokenizedCommentListener listener : this.commentTokenListeners) {
194186
listener.onCommentToken(this, commentToken);
@@ -688,7 +680,18 @@ private void checkForNull(final Object obj, final String message,
688680
@Override
689681
public void close() throws IOException {
690682
this.deferredReadTerm = null;
691-
this.tokenizer.close(this.autoCloseReaderFlag);
683+
this.tokenizer.close(this.isCloseReader());
684+
}
685+
686+
/**
687+
* Returns flag to close the base reader. By default the reader will be closed.
688+
*
689+
* @return true if close base reader during its close, false to not close reader
690+
* @see PrologParser#close()
691+
* @since 2.2.0
692+
*/
693+
protected boolean isCloseReader() {
694+
return true;
692695
}
693696

694697
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* Listener gets notifications from parser for met parsed comments if detection flag is on
77
*
8-
* @see ParserContext#FLAG_COMMENTS_AWARENESS
8+
* @see ParserContext#FLAG_COMMENTS_AS_ATOMS
99
* @since 2.2.0
1010
*/
1111
@FunctionalInterface

0 commit comments

Comments
 (0)