1   // Copyright 2012- Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
2   
3   package jminusminus;
4   
5   /**
6    * The AST node for a string literal.
7    */
8   class JLiteralString extends JExpression {
9       // String representation of the literal.
10      private String text;
11  
12      /**
13       * Constructs an AST node for a string literal given its line number and text representation.
14       *
15       * @param line line in which the literal occurs in the source file.
16       * @param text string representation of the literal.
17       */
18      public JLiteralString(int line, String text) {
19          super(line);
20          this.text = text;
21      }
22  
23      /**
24       * {@inheritDoc}
25       */
26      public JExpression analyze(Context context) {
27          type = Type.STRING;
28          return this;
29      }
30  
31      /**
32       * {@inheritDoc}
33       */
34      public void codegen(CLEmitter output) {
35          String s = JAST.unescape(text);
36          output.addLDCInstruction(s.substring(1, s.length() - 1));
37      }
38  
39      /**
40       * {@inheritDoc}
41       */
42      public void toJSON(JSONElement json) {
43          JSONElement e = new JSONElement();
44          json.addChild("JLiteralString:" + line, e);
45          e.addAttribute("type", type == null ? "" : type.toString());
46          e.addAttribute("value", text.substring(1, text.length() - 1));
47      }
48  }
49