Skip to content

Commit b1c6958

Browse files
lppeddftomassetti
authored andcommitted
refactor: fix code generation
1 parent 6edbd60 commit b1c6958

File tree

2 files changed

+18
-24
lines changed
  • antlr-kotlin-target/src/main

2 files changed

+18
-24
lines changed

antlr-kotlin-target/src/main/kotlin/org/antlr/v4/codegen/target/KotlinTarget.kt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package org.antlr.v4.codegen.target
55
import org.antlr.v4.codegen.CodeGenerator
66
import org.antlr.v4.codegen.Target
77
import org.antlr.v4.codegen.UnicodeEscapes
8-
import org.antlr.v4.tool.Grammar
98

109
public class KotlinTarget(codeGenerator: CodeGenerator) : JavaTarget(codeGenerator) {
1110
private companion object {
@@ -70,6 +69,7 @@ public class KotlinTarget(codeGenerator: CodeGenerator) : JavaTarget(codeGenerat
7069
addAll(kotlinKeywords)
7170
add("rule")
7271
add("parserRule")
72+
add("ruleIndex")
7373
}
7474

7575
override fun getVersion(): String =
@@ -92,13 +92,6 @@ public class KotlinTarget(codeGenerator: CodeGenerator) : JavaTarget(codeGenerat
9292
override fun appendUnicodeEscapedCodePoint(codePoint: Int, sb: StringBuilder): Unit =
9393
UnicodeEscapes.appendEscapedCodePoint(sb, codePoint, "Java")
9494

95-
override fun getTokenTypeAsTargetLabel(g: Grammar, ttype: Int): String {
96-
// All tokens are namespaced inside a Tokens object.
97-
// Here we simply force the qualification
98-
val label = super.getTokenTypeAsTargetLabel(g, ttype)
99-
return "Tokens.$label"
100-
}
101-
10295
override fun getTargetStringLiteralFromANTLRStringLiteral(
10396
generator: CodeGenerator,
10497
literal: String,

antlr-kotlin-target/src/main/resources/org/antlr/v4/tool/templates/codegen/Kotlin/Kotlin.stg

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import org.antlr.v4.kotlinruntime.atn.ATN.Companion.INVALID_ALT_NUMBER
5757
import org.antlr.v4.kotlinruntime.dfa.*
5858
import org.antlr.v4.kotlinruntime.misc.*
5959
import org.antlr.v4.kotlinruntime.tree.*
60+
import kotlin.jvm.JvmField
6061

6162
<parser>
6263
>>
@@ -416,11 +417,11 @@ LeftRecursiveRuleFunction(currentRule, args, code, locals, ruleCtx, altLabelCtxs
416417
<ruleCtx>
417418
<altLabelCtxs:{l | <altLabelCtxs.(l)>}; separator="\n">
418419

419-
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else>public<endif> fun <currentRule.name>(<args; separator=", ">): <currentRule.ctxType> {
420-
return <currentRule.name>(0<currentRule.args:{a | , <a.name>}>)
420+
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else>public<endif> fun <currentRule.escapedName>(<args; separator=", ">): <currentRule.ctxType> {
421+
return <currentRule.escapedName>(0<currentRule.args:{a | , <a.name>}>)
421422
}
422423

423-
private fun <currentRule.name>(_p: Int<args:{a | , <a>}>): <currentRule.ctxType> {
424+
private fun <currentRule.escapedName>(_p: Int<args:{a | , <a>}>): <currentRule.ctxType> {
424425
var _parentctx = context
425426
var _parentState = state
426427
var _localctx = <currentRule.ctxType>(context, _parentState<currentRule.args:{a | , <a.name>}>)
@@ -625,11 +626,11 @@ offsetShift(shiftAmount, offset) ::= <%
625626

626627
// produces more efficient bytecode when bits.tokens contains at most two items
627628
bitsetInlineComparison(s, bits) ::= <%
628-
<bits.tokens:{t | <s.varName> == <t.name>}; separator=" || ">
629+
<bits.tokens:{t | <s.varName> == Tokens.<t.name>}; separator=" || ">
629630
%>
630631

631632
cases(tokens) ::= <<
632-
<tokens:{t | <t.name>}; separator=", "> -> >>
633+
<tokens:{t | Tokens.<t.name>}; separator=", "> -> >>
633634

634635
InvokeRule(r, argExprsChunks) ::= <<
635636
this.state = <r.stateNumber>
@@ -644,10 +645,10 @@ _ctx = <r.escapedName>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)
644645
MatchToken(m) ::= <<
645646
this.state = <m.stateNumber>
646647
<if(m.labels)>
647-
_token = match(<m.name>)
648+
_token = match(Tokens.<m.name>)
648649
<m.labels:{l | <labelref(l)> = _token}; separator="\n">
649650
<else>
650-
match(<m.name>)
651+
match(Tokens.<m.name>)
651652
<endif>
652653
>>
653654

@@ -726,13 +727,13 @@ ActionText(t) ::= "<t.text>"
726727
ActionTemplate(t) ::= "<t.st>"
727728
ArgRef(a) ::= "_localctx.<a.name>"
728729
LocalRef(a) ::= "_localctx.<a.name>"
729-
RetValueRef(a) ::= "_localctx.<a.name>"
730+
RetValueRef(a) ::= "_localctx.<a.escapedName>"
730731
QRetValueRef(a) ::= "<ctx(a)>.<a.dict>!!.<a.name>"
731732

732733
/** How to translate $tokenLabel */
733734
TokenRef(t) ::= "<ctx(t)>.<t.name>"
734735
LabelRef(t) ::= "<ctx(t)>.<t.name>"
735-
ListLabelRef(t) ::= "<ctx(t)>.<ListLabelName(t.name)>"
736+
ListLabelRef(t) ::= "<ctx(t)>.<ListLabelName(t.escapedName)>"
736737
SetAttr(s, rhsChunks) ::= "<ctx(s)>.<s.name> = <rhsChunks>"
737738

738739
TokenLabelType() ::= "<file.TokenLabelType; null={Token}>"
@@ -748,13 +749,13 @@ TokenPropertyRef_int(t) ::= "(<ctx(t)>.<t.label>?.text!!.toInt() ?: 0)"
748749

749750
RulePropertyRef_start(r) ::= "(<ctx(r)>.<r.label>?.start)"
750751
RulePropertyRef_stop(r) ::= "(<ctx(r)>.<r.label>?.stop)"
751-
RulePropertyRef_text(r) ::= "(<ctx(r)>.<r.label>?.let { _input.getText(it.start!!, it.stop!!) })"
752+
RulePropertyRef_text(r) ::= "(<ctx(r)>.<r.label>?.let { _input.getText(it.start, it.stop) })"
752753
RulePropertyRef_ctx(r) ::= "<ctx(r)>.<r.label>"
753754
RulePropertyRef_parser(r) ::= "this"
754755

755756
ThisRulePropertyRef_start(r) ::= "_localctx.start"
756757
ThisRulePropertyRef_stop(r) ::= "_localctx.stop"
757-
ThisRulePropertyRef_text(r) ::= "_input.getText(_localctx.start!!, _input.LT(-1)!!)"
758+
ThisRulePropertyRef_text(r) ::= "_input.getText(_localctx.start, _input.LT(-1))"
758759
ThisRulePropertyRef_ctx(r) ::= "_localctx"
759760
ThisRulePropertyRef_parser(r) ::= "this"
760761

@@ -805,11 +806,11 @@ StructDecl(struct, ctorAttrs, attrs, getters, dispatchMethods, interfaces, exten
805806
public open class <struct.name> : <if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif><if(interfaces)> : <interfaces; separator=", "><endif> {
806807
override val ruleIndex: Int = Rules.<struct.derivedFromName; format="cap">
807808

808-
<attrs:{a | public var <a>}; separator="\n">
809+
<attrs:{a | @JvmField public var <a>}; separator="\n">
809810
<getters:{g | <g>}; separator="\n">
810811

811812
public constructor(parent: ParserRuleContext?, invokingState: Int<ctorAttrs:{a | , <a>}>) : super(parent, invokingState) {
812-
<struct.ctorAttrs:{a | this.<a.name> = <a.name>}; separator="\n">
813+
<struct.ctorAttrs:{a | this.<a.escapedName> = <a.escapedName>}; separator="\n">
813814
}
814815

815816
<! Don't need copy unless we have subclasses !>
@@ -818,7 +819,7 @@ public open class <struct.name> : <if(contextSuperClass)><contextSuperClass><els
818819

819820
public fun copyFrom(ctx: <struct.name>) {
820821
super.copyFrom(ctx)
821-
<struct.attrs:{a | this.<a.name> = ctx.<a.name>}; separator="\n">
822+
<struct.attrs:{a | this.<a.escapedName> = ctx.<a.escapedName>}; separator="\n">
822823
}
823824
<endif>
824825
<dispatchMethods; separator="\n\n">
@@ -829,7 +830,7 @@ public open class <struct.name> : <if(contextSuperClass)><contextSuperClass><els
829830

830831
AltLabelStructDecl(struct, attrs, getters, dispatchMethods) ::= <<
831832
public open class <struct.name> : <currentRule.name; format="cap">Context {
832-
<attrs:{a | public var <a>}; separator="\n">
833+
<attrs:{a | @JvmField public var <a>}; separator="\n">
833834
<getters:{g | <g>}; separator="\n">
834835

835836
public constructor(ctx: <currentRule.name; format="cap">Context) {
@@ -963,7 +964,7 @@ public open class <lexer.name>(input: CharStream) : <superClass; null="Lexer">(i
963964
public object Modes {
964965
public const val DEFAULT_MODE: Int = 0
965966
<if(rest(lexer.modes))>
966-
<rest(lexer.modes):{m | public const val <m>: Int = <i>}; separator="\n">
967+
<rest(lexer.modes):{m | public const val <m; format="upper">: Int = <i>}; separator="\n">
967968
<endif>
968969
}
969970

0 commit comments

Comments
 (0)