Skip to content

Commit

Permalink
型パラメーターを追加
Browse files Browse the repository at this point in the history
refs #9
  • Loading branch information
love2hina-net committed Aug 29, 2021
1 parent 160b438 commit c3cc064
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
59 changes: 39 additions & 20 deletions src/main/kotlin/net/love2hina/kotlin/sharon/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,17 @@ internal class Parser(val file: File) {
val methodInfo = MethodInfo()

// 実体解析
// 明示的なthisパラメータ
// タイプパラメーター
n.typeParameters.forEach {
val name = it.name.asString()
methodInfo.typeParameters[name] = TypeParameterInfo(name)
}
// 明示的なthisパラメーター
n.receiverParameter.ifPresent {
val name = it.name.asString()
methodInfo.parameters[name] = ParameterInfo("", it.type.asString(), name)
}
// パラメータ
// パラメーター
n.parameters.forEach {
val name = it.name.asString()
methodInfo.parameters[name] = ParameterInfo(
Expand All @@ -432,18 +437,6 @@ internal class Parser(val file: File) {
methodInfo.throws[type] = ThrowsInfo(type)
}

writer.writeStartElement("method")
writer.writeAttribute("modifier", getModifier(n.modifiers))
writer.writeAttribute("name", n.name.asString())

// 定義全体
writer.writeStartElement("definition")
writer.writeStrings(n.getDeclarationAsString(true, true, true))
writer.writeEndElement()

// TODO
// n.typeParameters.forEach { it.accept(this, arg) }

// コメントの解析
n.comment.ifPresent {
if (it.isJavadocComment) {
Expand All @@ -466,16 +459,33 @@ internal class Parser(val file: File) {
// Javadocをパースする
for (match in REGEXP_ANNOTATION.findAll(content)) {
index = pushJavadocComment(content, name, index, match.range, methodInfo)
name = (match.groups as MatchNamedGroupCollection)["s"]!!.value
name = (match.groups as MatchNamedGroupCollection)["name"]!!.value
}
pushJavadocComment(content, name, index, IntRange(content.length, content.length), methodInfo)
}
}

// 出力開始
writer.writeStartElement("method")
writer.writeAttribute("modifier", getModifier(n.modifiers))
writer.writeAttribute("name", n.name.asString())

// 定義全体
writer.writeStartElement("definition")
writer.writeStrings(n.getDeclarationAsString(true, true, true))
writer.writeEndElement()

// 説明の出力
writer.writeStartElement("description")
writer.writeStrings(methodInfo.description.toString())
writer.writeEndElement()
// タイプパラメーターの出力
methodInfo.typeParameters.forEach {
writer.writeStartElement("typeParameter")
writer.writeAttribute("type", it.value.type)
writer.writeStrings(it.value.description)
writer.writeEndElement()
}
// パラメーターの出力
methodInfo.parameters.forEach {
writer.writeStartElement("parameter")
Expand Down Expand Up @@ -521,19 +531,28 @@ internal class Parser(val file: File) {

when (name) {
"param" -> {
val r = Regex("^(?<name>\\w+)(?:\\s+(?<desc>.*))?$")
val r = Regex("^(?:(?<name>\\w+)|<(?<type>\\w+)>)(?:\\s+(?<desc>.*))?$")
val m = r.find(value)

if (m != null) {
val groups = (m.groups as MatchNamedGroupCollection)

val paramName = groups["name"]!!.value
val paramName = groups["name"]?.value
val paramType = groups["type"]?.value
val paramDesc = groups["desc"]?.value ?: ""

val paramInfo = methodInfo.parameters.getOrPut(paramName)
{ ParameterInfo("", "", paramName) }
if (paramName != null) {
val paramInfo = methodInfo.parameters.getOrPut(paramName)
{ ParameterInfo("", "", paramName) }

paramInfo.description = paramDesc
}
else if (paramType != null) {
val typeParameterInfo = methodInfo.typeParameters.getOrPut(paramType)
{ TypeParameterInfo(paramType) }

paramInfo.description = paramDesc
typeParameterInfo.description = paramDesc
}
}
}
"return" -> {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/net/love2hina/kotlin/sharon/data/Const.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package net.love2hina.kotlin.sharon.data
internal val REGEXP_BLOCK_COMMENT = Regex("^\\s*[/*]*\\s*(?<content>\\S|\\S.*\\S)?\\s*$")
internal val REGEXP_LINE_COMMENT = Regex("^/\\s*(?<content>\\S|\\S.*\\S)\\s*$")
internal val REGEXP_ASSIGNMENT = Regex("^(?<var>.*\\S)\\s*\\=\\s*(?<value>\\S.*)$")
internal val REGEXP_ANNOTATION = Regex("@(?<s>\\w+)")
internal val REGEXP_ANNOTATION = Regex("@(?<name>\\w+)")
internal val REGEXP_NEWLINE = Regex("\r\n|\r|\n")
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ internal class MethodInfo {
/** 説明 */
var description = StringBuilder()

/** パラメーター型 */
var typeParameters = LinkedHashMap<String, TypeParameterInfo>()

/** パラメーター */
val parameters = LinkedHashMap<String, ParameterInfo>()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.love2hina.kotlin.sharon.data

internal data class TypeParameterInfo(
var type: String = "",
var description: String = ""
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ public enum MyEnum {
/**
* メソッド.
*
* @param <Ty> 価格の型
* @param this インスタンス
* @param name 名前
* @param amount 価格
* @return リターンコード
* @throws Exception 例外
*/
@MyAnnotation
public int method(ParseTestTarget this, final String name, int amount) throws Exception {
public <Ty> int method(ParseTestTarget this, final String name, Ty amount) throws Exception {
/// # 出力
System.out.println("Hello, world!");

Expand Down

0 comments on commit c3cc064

Please sign in to comment.