Skip to content

Commit 4d2922f

Browse files
committed
refactoring
1 parent f9fd287 commit 4d2922f

File tree

7 files changed

+18
-20
lines changed

7 files changed

+18
-20
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@
2222
package com.igormaznitsa.prologparser;
2323

2424
import java.io.Reader;
25+
import java.util.List;
2526

2627
/**
2728
* Generic version of prolog parser.
2829
*/
2930
public class GenericPrologParser extends PrologParser {
31+
32+
public GenericPrologParser(final Reader reader, final ParserContext context) {
33+
this(reader, context, List.of());
34+
}
35+
3036
public GenericPrologParser(final Reader reader, final ParserContext context,
31-
final TokenizedCommentListener... tokenizedCommentListeners) {
37+
final List<TokenizedCommentListener> tokenizedCommentListeners) {
3238
super(reader, context, tokenizedCommentListeners);
3339
}
3440
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ public interface ParserContext {
6969
*/
7070
int FLAG_ZERO_QUOTATION_ALLOWS_WHITESPACE_CHAR = 64;
7171
/**
72-
* Return commentaries as atoms.
72+
* Make notifications about parsed commentaries and provide them as atoms.
7373
*
74+
* @see TokenizedCommentListener
7475
* @since 2.2.0
7576
*/
76-
int FLAG_COMMENTS_AS_ATOMS = 128;
77+
int FLAG_COMMENTS_AWARENESS = 128;
7778

7879
/**
7980
* Check that the context contains an operator starts with some string.

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
package com.igormaznitsa.prologparser;
2323

24-
import static com.igormaznitsa.prologparser.ParserContext.FLAG_COMMENTS_AS_ATOMS;
2524
import static com.igormaznitsa.prologparser.ParserContext.FLAG_DOT2_AS_LIST;
2625
import static com.igormaznitsa.prologparser.ParserContext.FLAG_NONE;
2726
import static com.igormaznitsa.prologparser.ParserContext.FLAG_VAR_AS_FUNCTOR;
@@ -103,25 +102,19 @@ public abstract class PrologParser implements Iterable<PrologTerm>, AutoCloseabl
103102

104103
protected final ParserContext context;
105104
protected final int parserFlags;
106-
private final boolean commentsAsAtoms;
107105
private final Tokenizer tokenizer;
108106
private boolean autoCloseReaderFlag;
109107
private final List<TokenizedCommentListener> commentTokenListeners;
110108

111109
protected PrologParser(
112110
final Reader source,
113111
final ParserContext context,
114-
final TokenizedCommentListener... tokenizedCommentListeners
112+
final List<TokenizedCommentListener> tokenizedCommentListeners
115113
) {
116114
this.context = context == null ? DefaultParserContext.of(ParserContext.FLAG_NONE) : context;
117115
this.parserFlags = context == null ? FLAG_NONE : context.getFlags();
118-
this.commentsAsAtoms = (this.parserFlags & FLAG_COMMENTS_AS_ATOMS) != 0;
119116
this.tokenizer = new Tokenizer(this, META_OP_MAP, requireNonNull(source));
120-
if (tokenizedCommentListeners.length == 0) {
121-
this.commentTokenListeners = List.of();
122-
} else {
123-
this.commentTokenListeners = List.of(tokenizedCommentListeners);
124-
}
117+
this.commentTokenListeners = List.copyOf(tokenizedCommentListeners);
125118
}
126119

127120
public static Op findBaseMetaOperator(final String text, final OpAssoc type) {

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_AS_ATOMS
8+
* @see ParserContext#FLAG_COMMENTS_AWARENESS
99
* @since 2.2.0
1010
*/
1111
@FunctionalInterface

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
package com.igormaznitsa.prologparser.tokenizer;
2323

2424
import static com.igormaznitsa.prologparser.ParserContext.FLAG_BLOCK_COMMENTS;
25-
import static com.igormaznitsa.prologparser.ParserContext.FLAG_COMMENTS_AS_ATOMS;
25+
import static com.igormaznitsa.prologparser.ParserContext.FLAG_COMMENTS_AWARENESS;
2626
import static com.igormaznitsa.prologparser.ParserContext.FLAG_ZERO_QUOTATION_ALLOWS_WHITESPACE_CHAR;
2727
import static com.igormaznitsa.prologparser.ParserContext.FLAG_ZERO_QUOTATION_CHARCODE;
2828
import static com.igormaznitsa.prologparser.tokenizer.TokenizerState.ATOM;
@@ -85,7 +85,7 @@ public Tokenizer(final PrologParser parser, final Koi7CharOpMap metaOperators,
8585
final int maxAllowedCharBufferSize = parser.getContext() == null ? Integer.MAX_VALUE :
8686
parser.getContext().getMaxTokenizerBufferLength();
8787
this.returnCommentsAsToken = parser.getContext() != null
88-
&& ((parser.getContext().getFlags() & FLAG_COMMENTS_AS_ATOMS) != 0);
88+
&& ((parser.getContext().getFlags() & FLAG_COMMENTS_AWARENESS) != 0);
8989
this.blockCommentsAllowed = parser.getContext() != null
9090
&& ((parser.getContext().getFlags() & FLAG_BLOCK_COMMENTS) != 0);
9191
this.zeroSingleQuotationAllowed = parser.getContext() != null

src/test/java/com/igormaznitsa/prologparser/exceptions/PrologParserExceptionTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,8 @@ public void testNotificationAboutComments() throws Exception {
9191
try (final PrologParser parser = new GenericPrologParser(
9292
new StringReader("% Line1\nhello(/*HHH*/world/*End*/).% Ending comment\n[1,2,3]."),
9393
DefaultParserContext.of(
94-
ParserContext.FLAG_BLOCK_COMMENTS | ParserContext.FLAG_COMMENTS_AS_ATOMS, Op.ISO),
95-
(parser1, comment) -> {
96-
foundComments.add(comment);
97-
}
94+
ParserContext.FLAG_BLOCK_COMMENTS | ParserContext.FLAG_COMMENTS_AWARENESS, Op.ISO),
95+
List.of((parser1, comment) -> foundComments.add(comment))
9896
)) {
9997
parser.forEach(foundTerms::add);
10098
}

src/test/java/com/igormaznitsa/prologparser/tokenizer/TokenizerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private Tokenizer tokenizeOf(final String str, final boolean allowBlockComment,
5757
when(context.getMaxTokenizerBufferLength()).thenReturn(1024);
5858
when(context.getFlags()).thenReturn(
5959
(allowBlockComment ? ParserContext.FLAG_BLOCK_COMMENTS : ParserContext.FLAG_NONE)
60-
| (commentsAsAtoms ? ParserContext.FLAG_COMMENTS_AS_ATOMS : ParserContext.FLAG_NONE)
60+
| (commentsAsAtoms ? ParserContext.FLAG_COMMENTS_AWARENESS : ParserContext.FLAG_NONE)
6161
);
6262
return this.tokenizeOf(str, context);
6363
}

0 commit comments

Comments
 (0)