-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.kt
68 lines (60 loc) · 2.28 KB
/
App.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ru.itmo.chizhikov
import org.antlr.v4.runtime.CharStream
import org.antlr.v4.runtime.CharStreams
import org.antlr.v4.runtime.CommonTokenStream
import org.antlr.v4.runtime.tree.ParseTreeWalker
import antlr.ru.itmo.chizhikov.gen.GrammarLexer
import antlr.ru.itmo.chizhikov.gen.GrammarParser
import ru.itmo.chizhikov.generators.GrammarCollector
import ru.itmo.chizhikov.generators.files.LexerGrammarFilesGenerator
import ru.itmo.chizhikov.generators.files.ParserGrammarFilesGenerator
import ru.itmo.chizhikov.generators.files.DefaultTestGrammarFilesGenerator
import ru.itmo.chizhikov.runtime.ParseException
import java.nio.file.Files
import java.nio.file.Paths
private fun collect(stream: CharStream): GrammarCollector {
val collector = GrammarCollector()
val lexer = GrammarLexer(stream)
val parser = GrammarParser(CommonTokenStream(lexer))
val walker = ParseTreeWalker()
walker.walk(collector, parser.file())
return collector
}
private fun genAll(grammarName: String, targetDir: String, collector: GrammarCollector) {
val lexer= LexerGrammarFilesGenerator(
collector
) to "Lexer"
val parser = ParserGrammarFilesGenerator(
collector
) to "Parser"
val default = DefaultTestGrammarFilesGenerator(
collector
) to "Test"
listOf(lexer, parser, default).forEach { (generator, postfix) ->
val out = Paths.get(targetDir, "$grammarName$postfix.kt")
out.toFile().parentFile.mkdirs()
Files.newBufferedWriter(out).use { wr ->
wr.write(generator.generate(grammarName))
}
}
}
fun main(args: Array<String>) {
val inputFile: String?
val outputDirectory: String?
if (args.isEmpty()) {
println("Please, input name of grammar file:")
inputFile = readLine()
println("Input path of directory for output:")
outputDirectory = readLine()
} else {
inputFile = args[0]
outputDirectory = args[1]
}
if (inputFile != null && outputDirectory != null) {
val grammarName = inputFile.removeSuffix(".gram").capitalize()
val collector = collect(CharStreams.fromFileName(inputFile))
genAll(grammarName, outputDirectory, collector)
} else {
throw ParseException("Error with handling grammar file or output directory path")
}
}