Eliminate DQUOT_START/SQUOT_START tokens 50/92250/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 23 Aug 2020 20:53:39 +0000 (22:53 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 23 Aug 2020 21:41:53 +0000 (23:41 +0200)
Quoted strings are naturally terminated by end marker, potentially
preceded by DQUOT_STRING/SQUOT_STRING. This renders the corresponding
start tokens really just a academic completeness, costing us memory
while not bringing anything to the table.

Skip generation of these tokens, reducing memory usage by up to 4.5%.

JIRA: YANGTOOLS-1089
Change-Id: I0b7ce9bf292b0dd8475d63869ddfd8d2b86a387c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-antlr/src/main/antlr4/org/opendaylight/yangtools/yang/parser/antlr/YangStatementLexer.g4
yang/yang-parser-antlr/src/main/antlr4/org/opendaylight/yangtools/yang/parser/antlr/YangStatementParser.g4
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/ir/AntlrSupport.java

index fa44dd7d0e50a84e8b472691427f27126f13f3e0..58f7807e794077607eef3feecab8056cdefed5f2 100644 (file)
@@ -86,8 +86,8 @@ UQUOT_STRING :
     -> type(UQUOT_STRING);
 
 // Double/single-quoted strings. We deal with these using specialized modes.
-DQUOT_START : '"' -> pushMode(DQUOT_STRING_MODE);
-SQUOT_START : '\'' -> pushMode(SQUOT_STRING_MODE);
+DQUOT_START : '"' -> pushMode(DQUOT_STRING_MODE), skip;
+SQUOT_START : '\'' -> pushMode(SQUOT_STRING_MODE), skip;
 
 //
 // Double-quoted string lexing mode. We do not need to recognize all possible
index 8becc9717394895d4e60008c22f0631f94c4dc1e..69148f618cd5e00c870ce4273ed9074a90d4b3ef 100644 (file)
@@ -34,8 +34,8 @@ argument :
     // here to eliminate the need for another parser construct. Quoted strings
     // account for about 50% of all arguments encountered -- hence the added
     // parse tree indirection is very visible.
-    (DQUOT_START DQUOT_STRING? DQUOT_END | SQUOT_START SQUOT_STRING? SQUOT_END)
-    (SEP* PLUS SEP* (DQUOT_START DQUOT_STRING? DQUOT_END | SQUOT_START SQUOT_STRING? SQUOT_END))*
+    (DQUOT_STRING? DQUOT_END | SQUOT_STRING? SQUOT_END)
+    (SEP* PLUS SEP* (DQUOT_STRING? DQUOT_END | SQUOT_STRING? SQUOT_END))*
     |
     unquotedString
     ;
index 9d615b08cac780139e72c8235da36e22b8d84b21..b215c218a9f245a1ba94fea0efcfcdcc8210c8df 100644 (file)
@@ -124,9 +124,8 @@ public final class AntlrSupport {
             case 0:
                 throw new VerifyException("Unexpected shape of " + argument);
             case 1:
-                return createUnquoted(argument);
+                return createSimple(argument);
             case 2:
-            case 3:
                 return createQuoted(argument);
             default:
                 return createConcatenation(argument);
@@ -144,9 +143,6 @@ public final class AntlrSupport {
                     // Separator, just skip it over
                 case YangStatementParser.PLUS:
                     // Operator, which we are handling by concat, skip it over
-                case YangStatementParser.DQUOT_START:
-                case YangStatementParser.SQUOT_START:
-                    // Quote starts, skip them over as they are just markers
                 case YangStatementParser.DQUOT_END:
                 case YangStatementParser.SQUOT_END:
                     // Quote stops, skip them over because we either already added the content, or would be appending
@@ -180,15 +176,10 @@ public final class AntlrSupport {
     }
 
     private Single createQuoted(final ArgumentContext argument) {
-        final ParseTree literal = argument.getChild(1);
-        verify(literal instanceof TerminalNode, "Unexpected literal %s", literal);
-        final Token token = ((TerminalNode) literal).getSymbol();
+        final ParseTree child = argument.getChild(0);
+        verify(child instanceof TerminalNode, "Unexpected literal %s", child);
+        final Token token = ((TerminalNode) child).getSymbol();
         switch (token.getType()) {
-            case YangStatementParser.DQUOT_END:
-            case YangStatementParser.SQUOT_END:
-                // This is an empty string, the difference between double and single quotes does not exist. Single
-                // quotes have more stringent semantics, hence use those.
-                return SingleQuoted.EMPTY;
             case YangStatementParser.DQUOT_STRING:
                 return createDoubleQuoted(token);
             case YangStatementParser.SQUOT_STRING:
@@ -210,15 +201,22 @@ public final class AntlrSupport {
         return dquotArguments.computeIfAbsent(str, DoubleQuoted::new);
     }
 
-    private SingleQuoted createSingleQuoted(final Token token) {
-        return squotArguments.computeIfAbsent(strOf(token), SingleQuoted::new);
-    }
-
-    private Single createUnquoted(final ArgumentContext argument) {
+    private IRArgument createSimple(final ArgumentContext argument) {
         final ParseTree child = argument.getChild(0);
         if (child instanceof TerminalNode) {
-            // This is as simple as it gets: we are dealing with an identifier here.
-            return idenArguments.computeIfAbsent(strOf(((TerminalNode) child).getSymbol()), Identifier::new);
+            final Token token = ((TerminalNode) child).getSymbol();
+            switch (token.getType()) {
+                case YangStatementParser.IDENTIFIER:
+                    // This is as simple as it gets: we are dealing with an identifier here.
+                    return idenArguments.computeIfAbsent(strOf(token), Identifier::new);
+                case YangStatementParser.DQUOT_END:
+                case YangStatementParser.SQUOT_END:
+                    // This is an empty string, the difference between double and single quotes does not exist. Single
+                    // quotes have more stringent semantics, hence use those.
+                    return SingleQuoted.EMPTY;
+                default:
+                    throw new VerifyException("Unexpected token " + token);
+            }
         }
 
         verify(child instanceof UnquotedStringContext, "Unexpected shape of %s", argument);
@@ -227,6 +225,10 @@ public final class AntlrSupport {
         return uquotArguments.computeIfAbsent(strOf(child), Unquoted::new);
     }
 
+    private SingleQuoted createSingleQuoted(final Token token) {
+        return squotArguments.computeIfAbsent(strOf(token), SingleQuoted::new);
+    }
+
     private ImmutableList<IRStatement> createStatements(final StatementContext stmt) {
         final List<StatementContext> statements = stmt.statement();
         return statements.isEmpty() ? ImmutableList.of()