-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.9.0: resolve #30 "scala: option to use back ticks"
also adjustments regarding keys with $ and quoted strings: - key containing $ is left alone (even if it's quoted). This mainly due to Config restrictions on keys involving $ - otherwise, the key is unquoted (if quoted of course)
- Loading branch information
Showing
14 changed files
with
196 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
tscfg.version = 0.8.4 | ||
tscfg.version = 0.9.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package tscfg.generators.scala | ||
|
||
import tscfg.generators.java.javaUtil | ||
import tscfg.util | ||
|
||
/** | ||
* By default [[scalaIdentifier]] uses underscores as needed for various cases | ||
* that need translation of the given identifier to make it valid Scala. | ||
* Similarly, [[getClassName]] also does some related logic. | ||
* | ||
* With this flag set to true, both methods will change their logic to uses | ||
* backsticks instead of replacing or removing the characters that would | ||
* make the resulting identifiers invalid. | ||
* | ||
* @param useBacksticks False by default | ||
*/ | ||
class ScalaUtil(useBacksticks: Boolean = false) { | ||
import ScalaUtil._ | ||
|
||
/** | ||
* Returns a valid scala identifier from the given symbol: | ||
* | ||
* - encloses the symbol in backticks if the symbol is a scala reserved word; | ||
* - appends an underscore if the symbol corresponds to a no-arg method in scope; | ||
* - otherwise: | ||
* - returns symbol if it is a valid java identifier | ||
* - otherwise: | ||
* if useBacksticks is true, enclose symbol in backsticks | ||
* otherwise, returns `javaGenerator.javaIdentifier(symbol)` | ||
*/ | ||
def scalaIdentifier(symbol: String): String = { | ||
if (scalaReservedWords.contains(symbol)) "`" + symbol + "`" | ||
else if (noArgMethodInScope.contains(symbol)) symbol + "_" | ||
else if (javaUtil.isJavaIdentifier(symbol)) symbol | ||
else if (useBacksticks) "`" + symbol + "`" | ||
else javaUtil.javaIdentifier(symbol) | ||
} | ||
|
||
/** | ||
* Returns a class name from the given symbol. | ||
* If useBacksticks: | ||
* This is basically capitalizing the first character that | ||
* can be capitalized. If none, then a `U` is prepended. | ||
* Otherwise: | ||
* Since underscores are specially used in generated code, | ||
* this method camelizes the symbol in case of any underscores. | ||
*/ | ||
def getClassName(symbol: String): String = { | ||
if (useBacksticks) { | ||
val scalaId = scalaIdentifier(symbol) | ||
val search = scalaId.zipWithIndex.find { case (c, _) ⇒ c.toUpper != c } | ||
search match { | ||
case Some((c, index)) ⇒ | ||
scalaId.substring(0, index) + c.toUpper + scalaId.substring(index + 1) | ||
|
||
case None ⇒ | ||
if (scalaId.head == '`') | ||
"`U" + scalaId.substring(1) | ||
else | ||
"U" + scalaId | ||
} | ||
} | ||
else { // preserve behavior until v0.8.4 | ||
// regular javaUtil.javaIdentifier as it might generate underscores: | ||
val id = javaUtil.javaIdentifier(symbol) | ||
// note: not scalaIdentifier because we are going to camelize anyway | ||
val parts = id.split("_") | ||
val name = parts.map(util.upperFirst).mkString | ||
if (name.nonEmpty) scalaIdentifier(name) | ||
else "U" + id.count(_ == '_') // named based on # of underscores ;) | ||
} | ||
} | ||
} | ||
|
||
object ScalaUtil { | ||
/** | ||
* The ones from Sect 1.1 of the Scala Language Spec, v2.9 | ||
* plus `_` | ||
*/ | ||
val scalaReservedWords: List[String] = List( | ||
"_", | ||
"abstract", "case", "catch", "class", "def", | ||
"do", "else", "extends", "false", "final", | ||
"finally", "for", "forSome", "if", "implicit", | ||
"import", "lazy", "match", "new", "null", | ||
"object", "override", "package", "private", "protected", | ||
"return", "sealed", "super", "this", "throw", | ||
"trait", "try", "true", "type", "val", | ||
"var", "while", "with", "yield" | ||
) | ||
|
||
val noArgMethodInScope: List[String] = List( | ||
"clone", | ||
"finalize", | ||
"getClass", | ||
"notify", | ||
"notifyAll", | ||
"toString", | ||
"wait" | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
foo-object { | ||
bar-baz: string | ||
0: string | ||
} | ||
"other#stuff": int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.