1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-01-20 20:31:56 +00:00

Moved getters to StringEncoding.

This commit is contained in:
jespergravgaard 2020-04-10 22:02:30 +02:00
parent 61c5914f73
commit 56227c36b6
2 changed files with 41 additions and 30 deletions

View File

@ -29,6 +29,41 @@ public enum StringEncoding {
this.mapping = mapping;
}
/**
* Get encoding by name.
* @param name The name
* @return The encoding
*/
public static StringEncoding fromName(String name) {
return valueOf(name);
}
/**
* Examine a string suffix, and find any encoding information inside it.
*
* @param suffix The string suffix
* @param defaultEncoding The encoding to use if suffix does not match an encoding
* @return The encoding specified by the suffix. If not the current source encoding is returned.
*/
public static StringEncoding fromSuffix(String suffix, StringEncoding defaultEncoding) {
if(suffix.contains("pm")) {
return PETSCII_MIXED;
} else if(suffix.contains("pu")) {
return PETSCII_UPPER;
} else if(suffix.contains("p")) {
return PETSCII_MIXED;
} else if(suffix.contains("sm")) {
return SCREENCODE_MIXED;
} else if(suffix.contains("su")) {
return SCREENCODE_UPPER;
} else if(suffix.contains("s")) {
return SCREENCODE_MIXED;
} else {
return defaultEncoding;
}
}
/**
* Get the integer value of a character using a specific encoding
*
@ -75,7 +110,7 @@ public enum StringEncoding {
* Converts any escapes such as '\n', '\xnn' etc. to the right ASCII character.
* Moves the iterator forward.
*
* @param escapedString The characters of the string to parse one char from. The iterator is moved beyond any handled chars.
* @param escapedCharsIterator The characters of the string to parse one char from. The iterator is moved beyond any handled chars.
* @return The first ASCII character of the list.
*/
public char escapeToAsciiFirst(PrimitiveIterator.OfInt escapedCharsIterator) {
@ -141,9 +176,9 @@ public enum StringEncoding {
}
/**
* Converts a char to an escape sequence if needed. If not needed the char itself is returned.
* @param aChar The char
* @return The char itself - or the appropriate escape sequence
* Escapes chars in string if needed
* @param string The string
* @return The escaped string.
*/
public String asciiToEscape(String string) {
StringBuilder escaped = new StringBuilder();

View File

@ -153,7 +153,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
@Override
public Object visitGlobalDirectiveEncoding(KickCParser.GlobalDirectiveEncodingContext ctx) {
try {
this.currentEncoding = StringEncoding.valueOf(ctx.NAME().getText().toUpperCase(Locale.ENGLISH));
this.currentEncoding = StringEncoding.fromName(ctx.NAME().getText().toUpperCase(Locale.ENGLISH));
} catch(IllegalArgumentException e) {
throw new CompileError("Unknown string encoding " + ctx.NAME().getText(), new StatementSource(ctx));
}
@ -2207,7 +2207,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
for(TerminalNode stringNode : ctx.STRING()) {
subText = stringNode.getText();
String suffix = subText.substring(subText.lastIndexOf('"') + 1);
StringEncoding suffixEncoding = getEncodingFromSuffix(suffix);
StringEncoding suffixEncoding = StringEncoding.fromSuffix(suffix, currentEncoding);
if(suffixEncoding != null) {
if(encoding != null && !encoding.equals(suffixEncoding)) {
throw new CompileError("Cannot mix encodings in concatenated strings " + ctx.getText(), new StatementSource(ctx));
@ -2226,30 +2226,6 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
}
}
/**
* Examine a string suffix, and find any encoding information inside it.
*
* @param suffix The string suffix
* @return The encoding specified by the suffix. If not the current source encoding is returned.
*/
private StringEncoding getEncodingFromSuffix(String suffix) {
if(suffix.contains("pm")) {
return StringEncoding.PETSCII_MIXED;
} else if(suffix.contains("pu")) {
return StringEncoding.PETSCII_UPPER;
} else if(suffix.contains("p")) {
return StringEncoding.PETSCII_MIXED;
} else if(suffix.contains("sm")) {
return StringEncoding.SCREENCODE_MIXED;
} else if(suffix.contains("su")) {
return StringEncoding.SCREENCODE_UPPER;
} else if(suffix.contains("s")) {
return StringEncoding.SCREENCODE_MIXED;
} else {
return currentEncoding;
}
}
@Override
public RValue visitExprBool(KickCParser.ExprBoolContext ctx) {
String bool = ctx.getText();