Skip to content

Commit

Permalink
add escape sequences in character strings (#38)
Browse files Browse the repository at this point in the history
* add escape sequences in character strings

Standard escape sequences (those used in java) and ability to write
bytes directly (\xbb).

* remove unneeded line in escape sequence parsing

* fix handling of unterminated byte strings

* add double quoted strings

To avoid breaking backwards compatibility, escaped strings need to be
enclosed in double quotes.
Moved code for lexing to Lexer.
  • Loading branch information
Kljunas2 authored Nov 10, 2022
1 parent 5299556 commit 224012b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
48 changes: 48 additions & 0 deletions src/sic/asm/parsing/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,52 @@ public double readFloat() throws AsmError {
return num;
}

public String readEscapedString(char terminator) throws AsmError {
StringBuilder buf = new StringBuilder();
for (char c = advance(); c != terminator; c = advance()) {
if (!ready() || c == '\n') {
throw new AsmError(loc(), "Unterminated byte string");
}
if (c == '\\') {
c = advance();
switch (c) {
case '\"', '\\':
break;
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'b':
c = '\b';
break;
case 'f':
c = '\f';
break;
case '0':
c = '\0';
break;
case 'x':
int mark = pos();
advance(2);
String str = extract(mark);
try {
c = (char) Integer.parseInt(str, 16);
} catch (NumberFormatException e) {
throw new AsmError(loc(), "Hexadecimal byte expected");
}
break;
default:
throw new AsmError(loc(), "Unknown escape sequence '\\%c'", c);
}
}
buf.append(c);
}
return buf.toString();
}

}
13 changes: 11 additions & 2 deletions src/sic/ast/data/DataChr.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ public String toString() {
@Override
public void parse(Parser parser, boolean allowList) throws AsmError {
parser.advance('C');
parser.advance('\'');
data = parser.readUntil('\'').getBytes();
char quote = parser.advance();
switch (quote) {
case '\'':
data = parser.readUntil('\'').getBytes();
break;
case '"':
data = parser.readEscapedString('"').getBytes();
break;
default:
throw new AsmError(parser.loc(), "Expected quote");
}
if (allowList) super.parse(parser, true);
}

Expand Down

0 comments on commit 224012b

Please sign in to comment.