Eliminate DQUOT_START/SQUOT_START tokens 52/92252/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 23 Aug 2020 20:53:39 +0000 (22:53 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 24 Aug 2020 06:28:46 +0000 (08:28 +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>
(cherry picked from commit 92a6c34fae26e54f9cf962228374805198b65059)

yang/yang-parser-rfc7950/src/main/antlr/org/opendaylight/yangtools/antlrv4/code/gen/YangStatementLexer.g4
yang/yang-parser-rfc7950/src/main/antlr/org/opendaylight/yangtools/antlrv4/code/gen/YangStatementParser.g4
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/repo/ArgumentContextUtils.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 3423e3060707a8f3067e6cf29f454a73ce6513e4..d565ed833a6ee137018d1c1d9e7a75c4b0497f43 100644 (file)
@@ -120,8 +120,10 @@ abstract class ArgumentContextUtils {
                     // Simplest of cases -- it is an IDENTIFIER, hence we do not need to validate anything else and can
                     // just grab the string and run with it.
                     return firstChild.getText();
-                case YangStatementParser.DQUOT_START:
-                case YangStatementParser.SQUOT_START:
+                case YangStatementParser.DQUOT_STRING:
+                case YangStatementParser.DQUOT_END:
+                case YangStatementParser.SQUOT_STRING:
+                case YangStatementParser.SQUOT_END:
                     // Quoted strings are potentially a pain, deal with them separately
                     return decodeQuoted(context, ref);
                 default:
@@ -137,13 +139,13 @@ abstract class ArgumentContextUtils {
     }
 
     private @NonNull String decodeQuoted(final ArgumentContext context, final StatementSourceReference ref) {
-        if (context.getChildCount() > 3) {
+        if (context.getChildCount() > 2) {
             // Potentially-complex case of string quoting, escaping and concatenation.
             return concatStrings(context, ref);
         }
 
         // No concatenation needed, special-case
-        final ParseTree child = context.getChild(1);
+        final ParseTree child = context.getChild(0);
         verify(child instanceof TerminalNode, "Unexpected shape of %s", context);
         final Token token = ((TerminalNode) child).getSymbol();
         switch (token.getType()) {
@@ -170,9 +172,6 @@ abstract class ArgumentContextUtils {
                     // 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