From: Robert Varga Date: Thu, 20 Aug 2020 14:48:41 +0000 (+0200) Subject: Special-case identifier lexer token X-Git-Tag: v4.0.14~27 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=2c00fdab0b2993b3896252cb08c42beba6fcaf13;p=yangtools.git Special-case identifier lexer token An unquoted string can be an IDENTIFIER, which is the most common case anyway. This token does not have to be further validated and thus it makes sense to special-case it. This has the neat effect of reducing the depth of our parse tree, too -- bringing memory usage down by 6.6% versus the original fix. JIRA: YANGTOOLS-1089 Change-Id: I63701719e90334558048201d70d8264ab9299efb Signed-off-by: Robert Varga (cherry picked from commit 0ec7fd50a8d7768f55d03e2ad1c6156fb6836c67) --- diff --git a/yang/yang-parser-antlr/src/main/antlr4/org/opendaylight/yangtools/yang/parser/antlr/YangStatementParser.g4 b/yang/yang-parser-antlr/src/main/antlr4/org/opendaylight/yangtools/yang/parser/antlr/YangStatementParser.g4 index cbfeee38c7..eec9c62d65 100644 --- a/yang/yang-parser-antlr/src/main/antlr4/org/opendaylight/yangtools/yang/parser/antlr/YangStatementParser.g4 +++ b/yang/yang-parser-antlr/src/main/antlr4/org/opendaylight/yangtools/yang/parser/antlr/YangStatementParser.g4 @@ -21,8 +21,9 @@ keyword : IDENTIFIER (COLON IDENTIFIER)?; // the flaky definitions of RFC6020, which allow for insane quoting as well as // exclusion of comments. We also need to allow for stitching back tokens like // PLUS/COLON, which may end up being valid identifiers. Finally we need to allow -// IDENTIFIER to be concatenated back to a string -argument : unquotedString | quotedString (SEP* PLUS SEP* quotedString)*; +// IDENTIFIER to be concatenated back to a string -- but it is common enough +// so we want to specialize it. +argument : IDENTIFIER | unquotedString | quotedString (SEP* PLUS SEP* quotedString)*; quotedString : DQUOT_START DQUOT_STRING? DQUOT_END diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/repo/ArgumentContextUtils.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/repo/ArgumentContextUtils.java index 2a37de6d5d..2f35f13f66 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/repo/ArgumentContextUtils.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/repo/ArgumentContextUtils.java @@ -114,6 +114,12 @@ abstract class ArgumentContextUtils { final @NonNull String stringFromStringContext(final ArgumentContext context, final StatementSourceReference ref) { // Get first child, which we fully expect to exist and be a lexer token final ParseTree firstChild = context.getChild(0); + if (firstChild instanceof TerminalNode) { + // Simplest of cases -- it is a simple IDENTIFIER, hence we do not need to validate anything else and can + // just grab the string and run with it. + return firstChild.getText(); + } + if (firstChild instanceof UnquotedStringContext) { // Simple case, just grab the text, as ANTLR has done all the heavy lifting final String str = firstChild.getText();