improved quoting for identifier added

This commit is contained in:
Sergey Ignatov
2011-11-08 15:19:57 +04:00
parent 846ac36fab
commit 54265da366
9 changed files with 75 additions and 5 deletions
@@ -7,6 +7,7 @@ import org.jetbrains.annotations.NotNull;
*/ */
public class IdentifierImpl extends Expression implements Identifier { public class IdentifierImpl extends Expression implements Identifier {
private final String myName; private final String myName;
private boolean myHasDollar;
private boolean myNullable = true; private boolean myNullable = true;
public IdentifierImpl(String name) { public IdentifierImpl(String name) {
@@ -18,6 +19,12 @@ public class IdentifierImpl extends Expression implements Identifier {
myNullable = nullable; myNullable = nullable;
} }
public IdentifierImpl(String name, boolean hasDollar, boolean nullable) {
myName = name;
myHasDollar = hasDollar;
myNullable = nullable;
}
@Override @Override
public String getName() { public String getName() {
return myName; return myName;
@@ -37,11 +44,17 @@ public class IdentifierImpl extends Expression implements Identifier {
return myNullable; return myNullable;
} }
private String ifNeedQuote(String name) {
if (ONLY_KOTLIN_KEYWORDS.contains(name) || name.contains("$"))
return quote(name);
return name;
}
@NotNull @NotNull
@Override @Override
public String toKotlin() { public String toKotlin() {
if (ONLY_KOTLIN_KEYWORDS.contains(myName)) if (myHasDollar)
return quote(myName); return DOLLAR + ifNeedQuote(myName);
return myName; return ifNeedQuote(myName);
} }
} }
+1
View File
@@ -37,6 +37,7 @@ public abstract class Node implements INode {
static final String COLON = ":"; static final String COLON = ":";
static final String IN = "in"; static final String IN = "in";
static final String AT = "@"; static final String AT = "@";
static final String DOLLAR = "$";
static final String BACKTICK = "`"; static final String BACKTICK = "`";
static final String QUEST = "?"; static final String QUEST = "?";
static final String COMMA_WITH_SPACE = "," + SPACE; static final String COMMA_WITH_SPACE = "," + SPACE;
@@ -234,7 +234,7 @@ public class ExpressionVisitor extends StatementVisitor implements Visitor {
boolean isNullable = typeToType(expression.getType()).isNullable(); boolean isNullable = typeToType(expression.getType()).isNullable();
final IdentifierImpl identifier = hasDollar ? final IdentifierImpl identifier = hasDollar ?
new IdentifierImpl("$" + expression.getReferenceName(), isNullable) : new IdentifierImpl(expression.getReferenceName(), hasDollar, isNullable) :
new IdentifierImpl(expression.getReferenceName(), isNullable); new IdentifierImpl(expression.getReferenceName(), isNullable);
myResult = new CallChainExpression( myResult = new CallChainExpression(
expressionToExpression(expression.getQualifierExpression()), expressionToExpression(expression.getQualifierExpression()),
@@ -0,0 +1 @@
$$$$$
@@ -0,0 +1 @@
`$$$$$`
@@ -0,0 +1,13 @@
class $$$$$ {}
class $ {}
class $$ extends $ {
final $$$$$ $$$;
public $$($$$$$ $$$$) {
$$$ = $$$$;
}
public $$$$$ $$$$$$() {return $$$;}
}
@@ -0,0 +1,13 @@
open class `$$$$$`() {
}
open class `$`() {
}
open class `$$`(`$$$$` : `$$$$$`?) : `$` {
val `$$$` : `$$$$$`?
{
$`$$$` = `$$$$`
}
open public fun `$$$$$$`() : `$$$$$`? {
return `$$$`
}
}
@@ -0,0 +1,5 @@
int
namespace, in, val, var, when, object,
out, in, is, as, ref, override,
open, attribute, lazy, set, get,
fun, where, by, trait, type, This;
@@ -0,0 +1,23 @@
var `namespace` : Int
var `in` : Int
var `val` : Int
var `var` : Int
var `when` : Int
var `object` : Int
var `out` : Int
var `in` : Int
var `is` : Int
var `as` : Int
var `ref` : Int
var `override` : Int
var `open` : Int
var `attribute` : Int
var `lazy` : Int
var `set` : Int
var `get` : Int
var `fun` : Int
var `where` : Int
var `by` : Int
var `trait` : Int
var `type` : Int
var `This` : Int