|
1 | 1 | /*
|
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. |
3 | 3 | * The Universal Permissive License (UPL), Version 1.0
|
4 | 4 | */
|
5 | 5 | grammar Yaml;
|
@@ -84,17 +84,17 @@ statement
|
84 | 84 | ;
|
85 | 85 |
|
86 | 86 | assign
|
87 |
| - : name ASSIGN_OP value WS? COMMENT? NEWLINE? |
| 87 | + : name ASSIGN_OP value COMMENT? NEWLINE |
88 | 88 | ;
|
89 | 89 |
|
90 | 90 | list_item
|
91 | 91 | : LIST_ITEM_OP assign # YamlListItemAssign
|
92 |
| - | LIST_ITEM_OP value WS? COMMENT? NEWLINE? # YamlListItemValue |
| 92 | + | LIST_ITEM_OP value COMMENT? NEWLINE? # YamlListItemValue |
93 | 93 | | LIST_ITEM_OP object # YamlListItemObject
|
94 | 94 | ;
|
95 | 95 |
|
96 | 96 | object
|
97 |
| - : name BLOCK_OP COMMENT? obj_block |
| 97 | + : name ASSIGN_OP COMMENT? obj_block |
98 | 98 | ;
|
99 | 99 |
|
100 | 100 | obj_block
|
@@ -127,8 +127,16 @@ inline_list_item
|
127 | 127 | : (NEWLINE (INDENT)?)? value
|
128 | 128 | ;
|
129 | 129 |
|
| 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. |
130 | 138 | COMMENT
|
131 |
| - : '# ' ~[\r\n\f]+ NEWLINE -> skip |
| 139 | + : WS? '#' ~[\r\n\f]* -> skip |
132 | 140 | ;
|
133 | 141 |
|
134 | 142 | NULL
|
@@ -192,7 +200,12 @@ NEWLINE
|
192 | 200 | String spaces = getText().replaceAll("[\r\n\f]+", "");
|
193 | 201 |
|
194 | 202 | 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 == '#') { |
196 | 209 | skip();
|
197 | 210 | } else {
|
198 | 211 | emit(commonToken(NEWLINE, newLine));
|
@@ -233,10 +246,6 @@ LIST_ITEM_OP
|
233 | 246 | ;
|
234 | 247 |
|
235 | 248 | ASSIGN_OP
|
236 |
| - : WS? ': ' WS? |
237 |
| - ; |
238 |
| - |
239 |
| -BLOCK_OP |
240 | 249 | : WS? ':' WS?
|
241 | 250 | ;
|
242 | 251 |
|
|
0 commit comments