|
| 1 | +// Copyright 2018 Sourcerer Inc. All Rights Reserved. |
| 2 | +// Author: Alexander Surkov ([email protected]) |
| 3 | + |
| 4 | +package app.extractors |
| 5 | + |
| 6 | +import app.model.CommitStats |
| 7 | +import app.model.DiffFile |
| 8 | + |
| 9 | +class RustExtractor : ExtractorInterface { |
| 10 | + companion object { |
| 11 | + const val LANGUAGE_NAME = Lang.Rust |
| 12 | + val evaluator by lazy { |
| 13 | + ExtractorInterface.getLibraryClassifier(LANGUAGE_NAME) |
| 14 | + } |
| 15 | + val importRegex = Regex("""^extern crate \w+;$""") |
| 16 | + val commentRegex = Regex("(//.+$)|(/[*].*?[*]/)") |
| 17 | + val extractImportRegex = Regex("""^extern crate (\w+);$""") |
| 18 | + } |
| 19 | + |
| 20 | + override fun extract(files: List<DiffFile>): List<CommitStats> { |
| 21 | + files.map { file -> file.language = LANGUAGE_NAME } |
| 22 | + return super.extract(files) |
| 23 | + } |
| 24 | + |
| 25 | + override fun extractImports(fileContent: List<String>): List<String> { |
| 26 | + val imports = mutableSetOf<String>() |
| 27 | + |
| 28 | + fileContent.forEach { |
| 29 | + val res = extractImportRegex.find(it) |
| 30 | + if (res != null) { |
| 31 | + val lineLib = res.groupValues[1] |
| 32 | + imports.add(lineLib) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return imports.toList() |
| 37 | + } |
| 38 | + |
| 39 | + override fun tokenize(line: String): List<String> { |
| 40 | + var newLine = importRegex.replace(line, "") |
| 41 | + newLine = commentRegex.replace(newLine, "") |
| 42 | + return super.tokenize(newLine) |
| 43 | + } |
| 44 | + |
| 45 | + override fun getLineLibraries(line: String, |
| 46 | + fileLibraries: List<String>): List<String> { |
| 47 | + |
| 48 | + return super.getLineLibraries(line, fileLibraries, evaluator, |
| 49 | + LANGUAGE_NAME) |
| 50 | + } |
| 51 | +} |
0 commit comments