Skip to content

Commit 12b64ca

Browse files
rakillenddsharpe
authored andcommitted
Parser fixes (#524)
* Refactored Java YAML parser out of test class * Revised parser to work with more comment styles, add test for commented model * Added comments to grammar * Removed redundant prefix definition * Removed compiler warning * Allow comments and blank lines at the top of file * Cleaned up warnings
1 parent a0766bc commit 12b64ca

File tree

6 files changed

+497
-302
lines changed

6 files changed

+497
-302
lines changed

core/src/main/antlr4/oracle/weblogic/deploy/yaml/Yaml.g4

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2020, Oracle Corporation and/or its affiliates. All rights reserved.
33
* The Universal Permissive License (UPL), Version 1.0
44
*/
55
grammar Yaml;
@@ -84,17 +84,17 @@ statement
8484
;
8585

8686
assign
87-
: name ASSIGN_OP value WS? COMMENT? NEWLINE?
87+
: name ASSIGN_OP value COMMENT? NEWLINE
8888
;
8989

9090
list_item
9191
: LIST_ITEM_OP assign # YamlListItemAssign
92-
| LIST_ITEM_OP value WS? COMMENT? NEWLINE? # YamlListItemValue
92+
| LIST_ITEM_OP value COMMENT? NEWLINE? # YamlListItemValue
9393
| LIST_ITEM_OP object # YamlListItemObject
9494
;
9595

9696
object
97-
: name BLOCK_OP COMMENT? obj_block
97+
: name ASSIGN_OP COMMENT? obj_block
9898
;
9999

100100
obj_block
@@ -127,8 +127,16 @@ inline_list_item
127127
: (NEWLINE (INDENT)?)? value
128128
;
129129

130+
// comments and blank lines before the first element avoid use of NEWLINE so there is no indent/dedent.
131+
// this rule should be one of the first in this file, to override later definitions.
132+
ATSTART
133+
: {atStartOfInput()}? ( (COMMENT | WS*) ('\r'? '\n' | '\r' | '\f') )+ -> skip
134+
;
135+
136+
// comments may appear on separate lines, or on the same line as assignments or object starts.
137+
// don't close with NEWLINE here, needed to distinguish assign from object declaration.
130138
COMMENT
131-
: '# ' ~[\r\n\f]+ NEWLINE -> skip
139+
: WS? '#' ~[\r\n\f]* -> skip
132140
;
133141

134142
NULL
@@ -192,7 +200,12 @@ NEWLINE
192200
String spaces = getText().replaceAll("[\r\n\f]+", "");
193201
194202
int next = _input.LA(1);
195-
if (opened > 0 || next == '\r' || next == '\n' || next == '\f') {
203+
204+
// if opened > 0, we're in a square-bracket list.
205+
// if next character is end-of-line, this was a blank line.
206+
// if next character is #, this is a comment line.
207+
// for these cases, don't check for indent, dedent.
208+
if (opened > 0 || next == '\r' || next == '\n' || next == '\f' || next == '#') {
196209
skip();
197210
} else {
198211
emit(commonToken(NEWLINE, newLine));
@@ -233,10 +246,6 @@ LIST_ITEM_OP
233246
;
234247

235248
ASSIGN_OP
236-
: WS? ': ' WS?
237-
;
238-
239-
BLOCK_OP
240249
: WS? ':' WS?
241250
;
242251

core/src/main/java/oracle/weblogic/deploy/logging/LoggingUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle Corporation and/or its affiliates. All rights reserved.
33
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
*/
55
package oracle.weblogic.deploy.logging;
@@ -17,7 +17,9 @@ public static <T extends Handler> Class<T> getHandlerClass(String handlerName) {
1717
Class<T> handler = null;
1818
try {
1919
Class<?> checkClass = Class.forName(handlerName);
20-
handler = (Class<T>)checkClass.asSubclass(Class.forName(handlerName));
20+
@SuppressWarnings("unchecked")
21+
Class<T> castHandler = (Class<T>)checkClass.asSubclass(Class.forName(handlerName));
22+
handler = castHandler;
2123
} catch(ClassNotFoundException | ClassCastException cnf) {
2224
exitWithError(
2325
MessageFormat.format("Unable to find handler class {0} so skipping logging configuration",

0 commit comments

Comments
 (0)