Literal mangling
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Brian_Watt
Posted On:   Tuesday, July 29, 2003 06:52 PM

I have a literal ""XX1"" that does not produce a LITERAL_XX1 token because the mangleLiteral in JavaCodeGenerator uses IsLetter and not isLetterOrDigit. Could this method be changed to use the latter so that literals will be mangled and not just made into a comment when they contain numbers.

Re[2]: Literal mangling

Posted By:   Monty_Zukowski  
Posted On:   Thursday, July 31, 2003 03:46 PM

Good ideas. I think you may just need to handle the case that the literal prefix is an empty string.

Re: Literal mangling

Posted By:   Brian_Watt  
Posted On:   Wednesday, July 30, 2003 06:53 AM

For example, here is a modified mangleLiteral method in JavaCodeGenerator. I used isJavaIdentifierPart which seems more appropriate than just isLetterOrDigit.

private String mangleLiteral(String s) {
String mangled = antlrTool.literalsPrefix;
for (int i = 1; i < s.length() - 1; i++) {
if (Character.isJavaIdentifierPart(s.charAt(i))) {
mangled += s.charAt(i);
} else {
mangled += Integer.toHexString(s.charAt(i));
}
}
if (antlrTool.upperCaseMangledLiterals) {
mangled = mangled.toUpperCase();
}
return mangled;
}

Here is some of the resulting literals. The 2a, 7e are "*" and "~" respectively.

...
int LITERAL_2a = 5; // <---
int LITERAL_7e = 6; // <---
...
int LITERAL_HL = 9;
int LITERAL_NM1 = 10; // <---
int LITERAL_REF = 11;
int LITERAL_N3 = 12; // <---
int LITERAL_N4 = 13; // <---
int LITERAL_PER = 14;
...

Re: Literal mangling

Posted By:   Brian_Watt  
Posted On:   Wednesday, July 30, 2003 06:11 AM

Also, if the literal "*" could be converted to its decimal or hexadecimal equivalent, LITERAL_42 or LITERAL_2A. This would allow mixed letter, digit and special characters to be represented as LITERAL_ variable names. For example, "root-node" would become LITERAL_ROOT45NODE.
About | Sitemap | Contact