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.files 18 19 import lexer.Token 20 import lexer.TokenCategory 21 import lexer.TokenGrammar 22 import parser.elements.AnnotationParser 23 import parser.elements.EntryParser 24 import parser.elements.declarations.InterfaceDeclarationParser 25 import parser.elements.declarations.MethodDeclarationParser 26 27 class InterfaceFileParser(tokens: List<Token>) : AbstractFileParser(tokens) { 28 <lambda>null29 private val interfaceEntry: EntryParser by lazy { 30 //returns first matching element (i don't think there can be multiple interface declarations) 31 this.entries.find { it.declarationParser is InterfaceDeclarationParser } 32 ?: throw ClassNotFoundException("Unable to find an InterfaceDeclarationParser") 33 } 34 //need this for the cast <lambda>null35 private val interfaceDecl: InterfaceDeclarationParser by lazy { 36 interfaceEntry.declarationParser as InterfaceDeclarationParser 37 } 38 39 /* top-level properties on the interface itself, ie. not its entries */ 40 41 override val name: String get() = interfaceDecl.name 42 val extendsName: String? get() = interfaceDecl.extendsName 43 val extendsVersion: Float? get() = interfaceDecl.extendsVersion 44 45 //doc 46 val description get() = interfaceEntry.docParser.description 47 val docAnnotations get() = interfaceEntry.docParser.docAnnotationParsers //tag, arg?, description 48 49 val annotations: List<AnnotationParser> get() = interfaceEntry.annotationParsers //name, value <lambda>null50 val methods: List<EntryParser> by lazy { getEntriesByDeclarationParser<MethodDeclarationParser>() } 51 52 companion object { 53 /** 54 * Searches tokens for an interface identifier. 55 * Maybe not the most accurate measurement, but good enough. 56 */ isInterfacenull57 fun isInterface(tokens: List<Token>): Boolean { 58 val iter = tokens.listIterator() 59 var token: Token 60 var inDoc = false 61 62 while (iter.hasNext()) { 63 token = iter.next() 64 if (token.identifier == TokenGrammar.DOC_START) { 65 inDoc = true 66 continue 67 } else if (token.identifier == TokenGrammar.DOC_END) { 68 inDoc = false 69 continue 70 71 } else if (!inDoc && token.identifier == TokenGrammar.INTERFACE) { 72 if (iter.next().category != TokenCategory.Word) break //no, try again 73 74 return true 75 } 76 } 77 return false 78 } 79 80 } 81 }