• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package parser.elements
18 
19 import lexer.Token
20 import lexer.TokenGrammar
21 import parser.config
22 import writer.warn
23 import java.text.ParseException
24 
25 /**
26  * Collection of doc entryParsers in a token stream.
27  * Typically, like entries are grouped together in a file (eg. Interface def),
28  * But could be useful for declarations with fields (enums, structs).
29  */
30 class EntryCollectionParser(iter: ListIterator<Token>, var shouldResetIterator: Boolean = false) : AbstractParser(iter) {
31 
32     var entryParsers = mutableListOf<EntryParser>()
33 
34     constructor(tokens: List<Token>) : this(tokens.listIterator())
35 
36     init {
37         parseTokens(scanTokens(iter))
38         if (shouldResetIterator) this.resetIterator(iter)
39     }
40 
41     //use all the tokens
scanTokensnull42     override fun scanTokens(iter: ListIterator<Token>): List<Token> {
43         val tokens = mutableListOf<Token>()
44         while (iter.hasNext()) {
45             tokens.add(iter.next())
46         }
47         return tokens
48     }
49 
parseTokensnull50     override fun parseTokens(tokens: List<Token>) {
51         val iter = tokens.listIterator()
52         var token: Token
53 
54         //find entry parsers - must start with doc_start
55         while(iter.hasNext()) {
56             token = iter.next()
57 
58             if (token.identifier == TokenGrammar.DOC_START) {
59                 try {
60                     entryParsers.add(EntryParser(iter)) //advances iterator past declaration
61                 } catch (ex: IllegalEntryException) {
62                     if (config.warnOnly) {
63                         //bail on current entry but continue
64                         warn("${ex.message}, skipping entry")
65                     } else {
66                         throw ParseException(ex.message, this.indexStart)
67                     }
68                 }
69             }
70         }
71     }
72 }