• 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 import lexer.HidlLexer
18 import lexer.Token
19 import parser.LOG_NAME
20 import parser.config
21 import parser.files.AbstractFileParser
22 import parser.files.InterfaceFileParser
23 import parser.files.TypesFileParser
24 import writer.files.*
25 import java.nio.file.Paths
26 import java.text.ParseException
27 
mainnull28 fun main(args: Array<String>) {
29     config.parseArgs(args)
30     if (config.verbose) println("$LOG_NAME args: ${config}")
31 
32     val indexWriter = IndexFileWriter() //collects parser entries
33 
34     /*
35      * parse and write HAL files
36      */
37     for (fp in config.files) {
38         println("$LOG_NAME Parsing input: $fp")
39 
40         val tokens = HidlLexer.tokenize(fp)
41         val (parser, writer) = parseAndGetWriter(tokens)
42 
43         indexWriter.addEntry(parser)
44 
45         try {
46             //since lazily evaluated, check here for parse errors
47             if (writer.writeToFile()) println("$LOG_NAME Wrote file: ${writer.path}")
48 
49         } catch (ex: ParseException) {
50             if (config.skipError) {
51                 System.err.println("$LOG_NAME Error parsing file, skipping: $fp")
52                 continue
53             } else {
54                 System.err.println("$LOG_NAME Error parsing file: $fp")
55                 throw ex
56             }
57         } finally {
58             if (config.verbose) writer.printInfo()
59         }
60     }
61 
62     /*
63      * non-HAL file
64      */
65     if (indexWriter.writeToFile()) println("$LOG_NAME Wrote index: ${indexWriter.path}")
66 
67     val cssPath = Paths.get("${config.outDir}/assets/style.css")
68     if (resources.copyToFile("/resources/assets/style.css", cssPath)) {
69         println("$LOG_NAME Copied resource file: $cssPath")
70     }
71 }
72 
parseAndGetWriternull73 fun parseAndGetWriter(tokens: List<Token>): Pair<AbstractFileParser, AbstractParserFileWriter> {
74     val parser: AbstractFileParser
75     val writer: AbstractParserFileWriter
76     if (InterfaceFileParser.isInterface(tokens)) {
77         parser = InterfaceFileParser(tokens)
78         writer = InterfaceFileWriter(parser)
79     } else {
80         parser = TypesFileParser(tokens)
81         writer = TypesFileWriter(parser)
82     }
83     return Pair(parser, writer)
84 }