feat(frontends/lean): use #"c" instead of 'c' for character literals

The new notation is the same one used in Standard ML.
It will also allow us to use ' in the beginning of identifiers like Standard ML.
This commit is contained in:
Leonardo de Moura 2016-11-17 11:35:54 -08:00
parent e16e9880f7
commit dfd2a23cd4
13 changed files with 39 additions and 38 deletions

View file

@ -5,7 +5,7 @@ Authors: Leonardo de Moura
-/
namespace debugger
def is_space (c : char) : bool :=
if c = ' ' c = char.of_nat 11 c = '\n' then tt else ff
if c = #" " c = char.of_nat 11 c = #"\n" then tt else ff
def split_core : string → string → list string
| (c::cs) [] :=
@ -22,7 +22,7 @@ def to_qualified_name_core : string → string → name
| [] r := if r = [] then name.anonymous else mk_simple_name r^.reverse
| (c::cs) r :=
if is_space c then to_qualified_name_core cs r
else if c = '.' then
else if c = #"." then
if r = [] then to_qualified_name_core cs []
else name.mk_string r^.reverse (to_qualified_name_core cs [])
else to_qualified_name_core cs (c::r)

View file

@ -28,4 +28,4 @@ have decidable_eq (fin char_sz), from fin.decidable_eq _,
this
instance : inhabited char :=
'A'
#"A"

View file

@ -41,7 +41,7 @@ def utf8_length : string → nat
private def to_nat_core : list char → nat → nat
| [] r := r
| (c::cs) r :=
to_nat_core cs (char.to_nat c - char.to_nat '0' + r*10)
to_nat_core cs (char.to_nat c - char.to_nat #"0" + r*10)
def string.to_nat (s : string) : nat :=
to_nat_core s^.reverse 0

View file

@ -51,14 +51,13 @@ instance {A : Type u} {P : A → Prop} [has_to_string A] : has_to_string (subtyp
⟨λ s, to_string (elt_of s)⟩
def char.quote_core (c : char) : string :=
if c = '\n' then "\\n"
else if c = '\\' then "\\\\"
else if c = '\"' then "\\\""
else if c = '\'' then "\\\'"
if c = #"\n" then "\\n"
else if c = #"\\" then "\\\\"
else if c = #"\"" then "\\\""
else c::nil
instance : has_to_string char :=
⟨λ c, "'" ++ char.quote_core c ++ "'"⟩
⟨λ c, "#\"" ++ char.quote_core c ++ "\""⟩
def string.quote_aux : string → string
| [] := ""

View file

@ -178,12 +178,14 @@ auto scanner::read_string() -> token_kind {
}
auto scanner::read_char() -> token_kind {
lean_assert(curr() == '\"');
next();
char c = curr();
if (c == '\\')
c = read_quoted_char(g_end_error_char_msg);
next();
if (curr() != '\'')
throw_exception("invalid character, ' expected");
if (curr() != '"')
throw_exception("invalid character, \" expected");
next();
m_buffer.clear();
m_buffer += c;
@ -535,18 +537,18 @@ auto scanner::read_key_cmd_id() -> token_kind {
static name * g_begin_comment_tk = nullptr;
static name * g_begin_comment_block_tk = nullptr;
static name * g_tick_tk = nullptr;
static name * g_pound_tk = nullptr;
void initialize_scanner() {
g_begin_comment_tk = new name("--");
g_begin_comment_block_tk = new name("/-");
g_tick_tk = new name("'");
g_pound_tk = new name("#");
}
void finalize_scanner() {
delete g_begin_comment_tk;
delete g_begin_comment_block_tk;
delete g_tick_tk;
delete g_pound_tk;
}
auto scanner::scan(environment const & env) -> token_kind {
@ -581,7 +583,7 @@ auto scanner::scan(environment const & env) -> token_kind {
read_single_line_comment();
else if (n == *g_begin_comment_block_tk)
read_comment_block();
else if (n == *g_tick_tk)
else if (n == *g_pound_tk && curr() == '"')
return read_char();
else
return k;

View file

@ -88,7 +88,7 @@ void init_token_table(token_table & t) {
{"(:", g_max_prec}, {":)", 0},
{"", 0}, {"", g_max_prec}, {"", 0}, {"^", 0}, {"", 0}, {"", 0},
{"//", 0}, {"|", 0}, {"!", g_max_prec}, {"?", 0}, {"with", 0}, {"without", 0}, {"...", 0}, {",", 0},
{".", 0}, {":", 0}, {"::", 0}, {"calc", 0}, {"as", 0}, {":=", 0}, {"--", 0}, {"#", 0},
{".", 0}, {":", 0}, {"::", 0}, {"calc", 0}, {"as", 0}, {":=", 0}, {"--", 0}, {"#", g_max_prec},
{"(*", 0}, {"/-", 0}, {"begin", g_max_prec}, {"using", 0},
{"@@", g_max_prec}, {"@", g_max_prec},
{"sorry", g_max_prec}, {"+", g_plus_prec}, {"->", g_arrow_prec}, {"<-", 0},

View file

@ -47,9 +47,9 @@ static void display_char_literal_core(std::ostream & out, char c, bool in_string
}
static void display_char_literal(std::ostream & out, char c) {
out << "'";
out << "#\"";
display_char_literal_core(out, c, false);
out << "'";
out << "\"";
}
static void display_string_literal(std::ostream & out, std::string const & s) {

View file

@ -1,11 +1,11 @@
import system.io
check 'a'
check #"a"
vm_eval 'a'
vm_eval '\n'
vm_eval '\\'
vm_eval put_str (list.cons '\\' "aaa")
vm_eval put_str ['\n']
vm_eval put_str ['\n']
vm_eval put_str (list.cons '\'' "aaa")
vm_eval #"a"
vm_eval #"\n"
vm_eval #"\\"
vm_eval put_str (list.cons #"\\" "aaa")
vm_eval put_str [#"\n"]
vm_eval put_str [#"\n"]
vm_eval put_str (list.cons #"\'" "aaa")

View file

@ -1,7 +1,7 @@
'a' : char
'a'
'\n'
'\\'
#"a" : char
#"a"
#"\n"
#"\\"
aaa\

View file

@ -5,7 +5,7 @@ check ({} : set nat)
definition s1 : set nat := {1, 2+3, 3, 4}
print s1
definition s2 : set char := {'a', 'b', 'c'}
definition s2 : set char := {#"a", #"b", #"c"}
print s2
definition s3 : set string := {"hello", "world"}

View file

@ -4,7 +4,7 @@
definition s1 : set :=
{1, 2 + 3, 3, 4}
definition s2 : set char :=
{'a', 'b', 'c'}
{#"a", #"b", #"c"}
definition s3 : set string :=
{"hello", "world"}
{a ∈ s1 | a > 1} : set

View file

@ -1,3 +1,3 @@
vm_eval '\x41'
vm_eval '\x42'
vm_eval '\x43'
vm_eval #"\x41"
vm_eval #"\x42"
vm_eval #"\x43"

View file

@ -1,3 +1,3 @@
'A'
'B'
'C'
#"A"
#"B"
#"C"