-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathStringLiteral.java
More file actions
59 lines (48 loc) · 1.48 KB
/
Copy pathStringLiteral.java
File metadata and controls
59 lines (48 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package loop.ast;
import loop.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author [email protected] (Dhanji R. Prasanna)
*/
public class StringLiteral extends Node {
public static final String NEWLINE_ESCAPES = "[^\\\\]\\\\n";
public final String value;
public final List<Node> parts;
public StringLiteral(String value) {
// Single quote strings cannot be lerped.
if (value.charAt(0) == '\'') {
parts = null;
this.value = value.replaceAll(NEWLINE_ESCAPES, "\n");
return;
}
this.value = value;
// Parse any expressions embedded in this string.
List<StringToken> stringTokens = StringLerpTokenizer.tokenize(value);
parts = new ArrayList<Node>(stringTokens.size());
for (StringToken stringToken : stringTokens) {
if (stringToken.kind == StringToken.Kind.EXPRESSION) {
List<Token> tokens = new Tokenizer(stringToken.value).tokenize();
parts.add(new LexprParser(tokens).computation());
} else
parts.add(new StringLiteral(stringToken.value, null));
}
}
private StringLiteral(String value, List<Node> parts) {
this.value = Escaper.unescape_perl_string(value);
this.parts = parts;
}
public String unquotedValue() {
return value != null ? value.substring(1, value.length() - 1) : null; // strip quotes
}
@Override
public String toSymbol() {
return value;
}
@Override
public String toString() {
return "String{" +
"'" + value + '\'' +
'}';
}
}