@@ -1342,6 +1342,20 @@ func expectKeyword(lexer: Lexer, value: String) throws -> Token {
13421342 return token
13431343}
13441344
1345+ /**
1346+ * If the next token is a given keyword, return "true" after advancing the lexer.
1347+ * Otherwise, do not change the parser state and return "false".
1348+ */
1349+ @discardableResult
1350+ func expectOptionalKeyword( lexer: Lexer , value: String ) throws -> Bool {
1351+ let token = lexer. token
1352+ guard token. kind == . name && token. value == value else {
1353+ return false
1354+ }
1355+ try lexer. advance ( )
1356+ return true
1357+ }
1358+
13451359/**
13461360 * Helper func for creating an error when an unexpected lexed token
13471361 * is encountered.
@@ -1377,6 +1391,23 @@ func any<T>(
13771391 return nodes
13781392}
13791393
1394+ /**
1395+ * Returns a list of parse nodes, determined by the parseFn.
1396+ * It can be empty only if open token is missing otherwise it will always return non-empty list
1397+ * that begins with a lex token of openKind and ends with a lex token of closeKind.
1398+ * Advances the parser to the next lex token after the closing token.
1399+ */
1400+ func optionalMany< T> ( lexer: Lexer , openKind: Token . Kind , closeKind: Token . Kind , parse: ( Lexer ) throws -> T ) throws -> [ T ] {
1401+ guard try expectOptional ( lexer: lexer, kind: openKind) != nil else {
1402+ return [ ]
1403+ }
1404+ var nodes : [ T ] = [ ]
1405+ while try ! skip( lexer: lexer, kind: closeKind) {
1406+ nodes. append ( try parse ( lexer) )
1407+ }
1408+ return nodes
1409+ }
1410+
13801411/**
13811412 * Returns a non-empty list of parse nodes, determined by
13821413 * the parseFn. This list begins with a lex token of openKind
0 commit comments