From 8761819117d9442f7e9932d9f07edbfc9d9c911f Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 13 Feb 2016 00:03:47 +0300 Subject: [PATCH] StdLib generators: provide extension points for family properties specific to documentation and code. --- .../src/templates/Aggregates.kt | 116 ++++++++---------- .../src/templates/engine/Engine.kt | 5 + .../{DocExtensions.kt => FamilyProperties.kt} | 13 ++ 3 files changed, 70 insertions(+), 64 deletions(-) rename libraries/tools/kotlin-stdlib-gen/src/templates/engine/{DocExtensions.kt => FamilyProperties.kt} (67%) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index c6ac3dfe0f0..f439631cbdc 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -446,18 +446,16 @@ fun aggregates(): List { } returns("T") body { f -> - with (DocExtensions) { - """ - if (isEmpty()) - throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") + """ + if (isEmpty()) + throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator - """ + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } + return accumulator + """ } } @@ -488,18 +486,16 @@ fun aggregates(): List { """ } body(ArraysOfObjects) { f -> - with (DocExtensions) { - """ - if (isEmpty()) - throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") + """ + if (isEmpty()) + throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") - var accumulator: S = this[0] - for (index in 1..lastIndex) { - accumulator = operation(index, accumulator, this[index]) - } - return accumulator - """ + var accumulator: S = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } + return accumulator + """ } } @@ -515,20 +511,18 @@ fun aggregates(): List { } returns("T") body { f -> - with (DocExtensions) { - """ - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") + """ + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - - return accumulator - """ + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index } + + return accumulator + """ } } @@ -546,20 +540,18 @@ fun aggregates(): List { typeParam("T: S") returns("S") body { f -> - with (DocExtensions) { - """ - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") + """ + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") - var accumulator: S = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index - } - - return accumulator - """ + var accumulator: S = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index } + + return accumulator + """ } } @@ -570,18 +562,16 @@ fun aggregates(): List { doc { f -> "Accumulates value starting with the first ${f.element} and applying [operation] from left to right to current accumulator value and each ${f.element}." } returns("T") body { f -> - with (DocExtensions) { - """ - if (isEmpty()) - throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") + """ + if (isEmpty()) + throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") - var accumulator = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator - """ + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } + return accumulator + """ } } @@ -606,18 +596,16 @@ fun aggregates(): List { """ } body(ArraysOfObjects) { f -> - with (DocExtensions) { - """ - if (isEmpty()) - throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") + """ + if (isEmpty()) + throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") - var accumulator: S = this[0] - for (index in 1..lastIndex) { - accumulator = operation(accumulator, this[index]) - } - return accumulator - """ + var accumulator: S = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } + return accumulator + """ } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/engine/Engine.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/engine/Engine.kt index acea556df67..bc1fb4c1172 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/engine/Engine.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/engine/Engine.kt @@ -25,6 +25,11 @@ enum class Family { val isPrimitiveSpecialization: Boolean by lazy { this in primitiveSpecializations } + class DocExtension(val family: Family) + class CodeExtension(val family: Family) + val doc = DocExtension(this) + val code = CodeExtension(this) + companion object { val primitiveSpecializations = setOf(ArraysOfPrimitives, RangesOfPrimitives, ProgressionsOfPrimitives, Primitives) val defaultFamilies = setOf(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/engine/DocExtensions.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/engine/FamilyProperties.kt similarity index 67% rename from libraries/tools/kotlin-stdlib-gen/src/templates/engine/DocExtensions.kt rename to libraries/tools/kotlin-stdlib-gen/src/templates/engine/FamilyProperties.kt index 6a9c6b88904..f6b459a40aa 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/engine/DocExtensions.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/engine/FamilyProperties.kt @@ -2,6 +2,19 @@ package templates import templates.Family.* +val Family.DocExtension.collection: String + get() = with(DocExtensions) { family.collection } + +val Family.DocExtension.element: String + get() = with(DocExtensions) { family.element } + +val Family.CodeExtension.size: String + get() = when (family) { + Iterables, Collections, Lists, Sets, Maps, InvariantArraysOfObjects, ArraysOfObjects, ArraysOfPrimitives -> "size" + CharSequences, Strings -> "length" + else -> error("size property isn't supported for $family") + } + object DocExtensions { val Family.element: String