Line data Source code
1 : /* valacharacterliteral.vala
2 : *
3 : * Copyright (C) 2006-2010 Jürg Billeter
4 : * Copyright (C) 2006-2009 Raffaele Sandrini
5 : *
6 : * This library is free software; you can redistribute it and/or
7 : * modify it under the terms of the GNU Lesser General Public
8 : * License as published by the Free Software Foundation; either
9 : * version 2.1 of the License, or (at your option) any later version.
10 :
11 : * This library is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : * Lesser General Public License for more details.
15 :
16 : * You should have received a copy of the GNU Lesser General Public
17 : * License along with this library; if not, write to the Free Software
18 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 : *
20 : * Author:
21 : * Jürg Billeter <j@bitron.ch>
22 : * Raffaele Sandrini <raffaele@sandrini.ch>
23 : */
24 :
25 : using GLib;
26 :
27 : /**
28 : * Represents a single literal character.
29 : */
30 1529 : public class Vala.CharacterLiteral : Literal {
31 : /**
32 : * The literal value.
33 : */
34 : public string value {
35 6898 : get {
36 6898 : return _value;
37 : }
38 5178 : private set {
39 10356 : _value = value;
40 :
41 5178 : if (!value.validate ()) {
42 0 : error = true;
43 : }
44 : }
45 : }
46 :
47 5178 : private string _value;
48 :
49 : /**
50 : * Creates a new character literal.
51 : *
52 : * @param c character
53 : * @param source reference to source code
54 : * @return newly created character literal
55 : */
56 15534 : public CharacterLiteral (string c, SourceReference? source = null) {
57 5178 : value = c;
58 5178 : source_reference = source;
59 :
60 : }
61 :
62 14165 : public override void accept (CodeVisitor visitor) {
63 14165 : visitor.visit_character_literal (this);
64 :
65 14165 : visitor.visit_expression (this);
66 : }
67 :
68 : /**
69 : * Returns the unicode character value this character literal
70 : * represents.
71 : *
72 : * @return unicode character value
73 : */
74 6115 : public unichar get_char () {
75 6115 : return value.next_char ().get_char ();
76 : }
77 :
78 0 : public override bool is_pure () {
79 0 : return true;
80 : }
81 :
82 150 : public override string to_string () {
83 150 : return value;
84 : }
85 :
86 9385 : public override bool check (CodeContext context) {
87 9385 : if (checked) {
88 4549 : return !error;
89 : }
90 :
91 4836 : checked = true;
92 :
93 4836 : if (get_char () < 128) {
94 4832 : value_type = context.analyzer.char_type.copy ();
95 : } else {
96 4 : value_type = context.analyzer.unichar_type.copy ();
97 : }
98 :
99 4836 : return !error;
100 : }
101 :
102 637 : public override void emit (CodeGenerator codegen) {
103 637 : codegen.visit_character_literal (this);
104 :
105 637 : codegen.visit_expression (this);
106 : }
107 : }
|