yurloc commented on code in PR #5855:
URL:
https://github.com/apache/incubator-kie-drools/pull/5855#discussion_r1575336675
##########
drools-drl/drools-drl-parser-tests/pom.xml:
##########
@@ -83,4 +83,42 @@
</dependency>
</dependencies>
+ <profiles>
+ <profile>
+ <id>default</id>
+ <activation>
+ <activeByDefault>true</activeByDefault>
+ </activation>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <executions>
+ <execution>
+ <!-- override default-test for new parser -->
+ <id>default-test</id>
+ <configuration>
+ <systemPropertyVariables>
+
<drools.drl.antlr4.parser.enabled>true</drools.drl.antlr4.parser.enabled>
+ </systemPropertyVariables>
+ </configuration>
+ </execution>
+ <execution>
+ <id>old-parser-test</id>
+ <configuration>
+ <systemPropertyVariables>
+
<drools.drl.antlr4.parser.enabled>false</drools.drl.antlr4.parser.enabled>
+ </systemPropertyVariables>
+ </configuration>
+ <goals>
+ <goal>test</goal>
+ </goals>
+ </execution>
Review Comment:
I'm not sure if I understand. Does this run the new tests (that we keep on
adding to on this feature branch) against both the new and the old parser?
##########
drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/LexerHelper.java:
##########
@@ -0,0 +1,171 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.drools.drl.parser.antlr4;
+
+import java.util.List;
+
+import org.antlr.v4.runtime.CharStream;
+import org.antlr.v4.runtime.IntStream;
+import org.drools.drl.parser.lang.DroolsSoftKeywords;
+
+/**
+ * Helper class for lexer. It requires instance creation to keep track of the
lookahead counter.
+ */
+public class LexerHelper {
+
+ private static final List<Character> semiAndWS = List.of(';', ' ', '\t',
'\n', '\r');
+ private static final List<String> statementKeywordsList =
List.of(ParserHelper.statementKeywords);
+ private static final List<String> attributeKeywordsList =
List.of(DroolsSoftKeywords.SALIENCE,
+
DroolsSoftKeywords.ENABLED,
+
DroolsSoftKeywords.NO + "-" + DroolsSoftKeywords.LOOP,
+
DroolsSoftKeywords.AUTO + "-" + DroolsSoftKeywords.FOCUS,
+
DroolsSoftKeywords.LOCK + "-" + DroolsSoftKeywords.ON + "-" +
DroolsSoftKeywords.ACTIVE,
+
DroolsSoftKeywords.AGENDA + "-" + DroolsSoftKeywords.GROUP,
+
DroolsSoftKeywords.ACTIVATION + "-" + DroolsSoftKeywords.GROUP,
+
DroolsSoftKeywords.RULEFLOW + "-" + DroolsSoftKeywords.GROUP,
+
DroolsSoftKeywords.DATE + "-" + DroolsSoftKeywords.EFFECTIVE,
+
DroolsSoftKeywords.DATE + "-" + DroolsSoftKeywords.EXPIRES,
+
DroolsSoftKeywords.DIALECT,
+
DroolsSoftKeywords.CALENDARS,
+
DroolsSoftKeywords.TIMER,
+
DroolsSoftKeywords.DURATION,
+
DroolsSoftKeywords.REFRACT,
+
DroolsSoftKeywords.DIRECT);
+
+ private final CharStream input;
+ private int lookAheadCounter;
+
+ public LexerHelper(CharStream input) {
+ this.input = input;
+ this.lookAheadCounter = 1;
+ }
+
+ /**
+ * Determine if the current token is the end of a RHS DRL by lookahead.
+ * 1. 'end'
+ * 2. skip semi, WS, and comment
+ * 3. next token should be EOF or statement or attribute keyword
+ *
+ * TODO: This method is low-level and may be too complex in order to keep
backward compatibility.
+ * This could be refactored by going back to a parser rather than
the lexer island mode.
+ */
+ boolean isRhsDrlEnd() {
+ if (!validateDrlEnd()) {
+ return false;
+ }
+ skipSemiAndWSAndComment();
+
+ return validateEOForNextStatement();
+ }
+
+ private boolean validateDrlEnd() {
+ return captureNextToken().equals(DroolsSoftKeywords.END);
+ }
+
+ private String captureNextToken() {
+ StringBuilder sb = new StringBuilder();
+ while (true) {
+ int la = input.LA(lookAheadCounter);
+ if (semiAndWS.contains((char) la) || la == IntStream.EOF) {
+ break;
+ }
+ sb.append((char) la);
+ lookAheadCounter++;
+ }
+ return sb.toString(); // never null
+ }
+
+ private void skipSemiAndWSAndComment() {
+ while (true) {
+ skipSemiAndWS();
+ if (input.LA(lookAheadCounter) == '/') {
+ boolean skipped = skipComment();
+ if (!skipped) {
+ // found non-comment token
+ break;
+ }
+ // if comment is found and skipped, continue to skip semi and
WS
+ } else {
+ // found non-comment token
+ break;
+ }
+ }
Review Comment:
```suggestion
do {
// skip semi and WS, and repeat that so long as it's followed by
a valid and skipped comment
skipSemiAndWS();
} while (skipComment());
```
@tkobayas less code and I don't think the readability suffers, wdyt?
##########
drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/DescrCommonPropertyTest.java:
##########
@@ -245,10 +288,10 @@ void andDescr() {
" ( $p1 : Person( name == \"Mario\" ) and $p2 : Person(
name == \"Luigi\" ) )\n" +
" then\n" +
"end";
- final PackageDescr pkg = parser.parse(source);
+ final PackageDescr pkg = parseAndGetPackageDescr(source);
final RuleDescr rule = pkg.getRules().get(0);
AndDescr and = rule.getLhs();
- assertProperties(and, 21, 87, 3, 6, 3, 72);
+ assertProperties(and, 19, 90, 3, 4, 3, 74);
Review Comment:
For the record, I checked this one and the new numbers are correct.
##########
drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/DescrCommonPropertyTest.java:
##########
@@ -41,15 +61,21 @@
*/
class DescrCommonPropertyTest {
- private DRLParserWrapper parser;
+ private DrlParser parser;
@BeforeEach
public void setUp() {
- parser = new DRLParserWrapper();
+ parser = ParserTestUtils.getParser();
}
- @AfterEach
Review Comment:
@tkobayas You may also remove the import.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]