2013-08-03 17:58:05 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2013-08-10 21:15:13 +00:00
|
|
|
#include <cstdio>
|
2013-08-13 17:55:41 +00:00
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
2013-08-03 17:58:05 +00:00
|
|
|
#include "scanner.h"
|
|
|
|
#include "debug.h"
|
2013-08-13 10:40:51 +00:00
|
|
|
#include "exception.h"
|
2013-08-03 17:58:05 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
|
2013-08-13 10:40:51 +00:00
|
|
|
static name g_lambda_unicode("\u03BB");
|
|
|
|
static name g_pi_unicode("\u03A0");
|
|
|
|
static name g_arrow_unicode("\u2192");
|
|
|
|
static name g_lambda_name("fun");
|
2013-08-13 10:52:08 +00:00
|
|
|
static name g_type_name("Type");
|
2013-08-13 17:55:41 +00:00
|
|
|
static name g_pi_name("Pi");
|
2013-08-13 10:40:51 +00:00
|
|
|
static name g_arrow_name("->");
|
2013-08-13 10:52:08 +00:00
|
|
|
static name g_eq_name("=");
|
2013-08-13 10:40:51 +00:00
|
|
|
|
2013-08-13 14:16:46 +00:00
|
|
|
static char g_normalized[256];
|
|
|
|
|
2013-08-15 17:46:13 +00:00
|
|
|
/** \brief Auxiliary class for initializing global variable \c g_normalized. */
|
2013-08-13 10:40:51 +00:00
|
|
|
class init_normalized_table {
|
2013-08-13 14:16:46 +00:00
|
|
|
void set(int i, char v) { g_normalized[static_cast<unsigned>(i)] = v; }
|
2013-08-13 10:40:51 +00:00
|
|
|
public:
|
|
|
|
init_normalized_table() {
|
|
|
|
// by default all characters are in group c,
|
|
|
|
for (int i = 0; i <= 255; i++)
|
2013-08-13 14:16:46 +00:00
|
|
|
set(i, 'c');
|
2013-08-13 10:40:51 +00:00
|
|
|
|
|
|
|
// digits normalize to '0'
|
|
|
|
for (int i = '0'; i <= '9'; i++)
|
2013-08-13 14:16:46 +00:00
|
|
|
set(i, '0');
|
2013-08-13 10:40:51 +00:00
|
|
|
|
|
|
|
// characters that can be used to create ids of group a
|
|
|
|
for (int i = 'a'; i <= 'z'; i++)
|
2013-08-13 14:16:46 +00:00
|
|
|
set(i, 'a');
|
2013-08-13 10:40:51 +00:00
|
|
|
for (int i = 'A'; i <= 'Z'; i++)
|
2013-08-13 14:16:46 +00:00
|
|
|
set(i, 'a');
|
|
|
|
|
|
|
|
set('_', 'a');
|
2013-08-13 10:40:51 +00:00
|
|
|
|
|
|
|
// characters that can be used to create ids of group b
|
2013-08-18 17:47:13 +00:00
|
|
|
for (unsigned char b : {'=', '<', '>', '@', '^', '|', '&', '~', '+', '-', '*', '/', '\\', '$', '%', '?', ';'})
|
2013-08-13 14:16:46 +00:00
|
|
|
set(b, 'b');
|
2013-08-13 10:40:51 +00:00
|
|
|
|
|
|
|
// punctuation
|
2013-08-13 14:16:46 +00:00
|
|
|
set('(', '(');
|
|
|
|
set(')', ')');
|
|
|
|
set('{', '{');
|
|
|
|
set('}', '}');
|
|
|
|
set(':', ':');
|
|
|
|
set('.', '.');
|
|
|
|
set(',', ',');
|
2013-08-13 10:40:51 +00:00
|
|
|
|
|
|
|
// spaces
|
2013-08-13 14:16:46 +00:00
|
|
|
set(' ', ' ');
|
|
|
|
set('\t', ' ');
|
|
|
|
set('\r', ' ');
|
2013-08-13 10:40:51 +00:00
|
|
|
|
|
|
|
// new line
|
2013-08-13 14:16:46 +00:00
|
|
|
set('\n', '\n');
|
2013-08-13 10:40:51 +00:00
|
|
|
|
2013-08-13 17:55:41 +00:00
|
|
|
// double quotes for strings
|
|
|
|
set('\"', '\"');
|
|
|
|
|
2013-08-13 10:40:51 +00:00
|
|
|
// eof
|
2013-08-13 14:16:46 +00:00
|
|
|
set(255, -1);
|
2013-08-13 10:40:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static init_normalized_table g_init_normalized_table;
|
|
|
|
|
|
|
|
char normalize(char c) {
|
|
|
|
return g_normalized[static_cast<unsigned char>(c)];
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner::scanner(std::istream& stream):
|
2013-08-03 17:58:05 +00:00
|
|
|
m_spos(0),
|
|
|
|
m_curr(0),
|
|
|
|
m_line(1),
|
|
|
|
m_pos(0),
|
2013-08-13 10:40:51 +00:00
|
|
|
m_stream(stream) {
|
|
|
|
next();
|
2013-08-03 17:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scanner::~scanner() {
|
|
|
|
}
|
|
|
|
|
2013-08-13 17:55:41 +00:00
|
|
|
void scanner::add_command_keyword(name const & n) {
|
|
|
|
m_commands = cons(n, m_commands);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scanner::throw_exception(char const * msg) {
|
|
|
|
throw parser_exception(msg, m_line, m_spos);
|
|
|
|
}
|
|
|
|
|
2013-08-03 17:58:05 +00:00
|
|
|
void scanner::next() {
|
|
|
|
lean_assert(m_curr != EOF);
|
2013-08-13 10:40:51 +00:00
|
|
|
m_curr = m_stream.get();
|
|
|
|
m_spos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool scanner::check_next(char c) {
|
|
|
|
lean_assert(m_curr != EOF);
|
|
|
|
bool r = m_stream.get() == c;
|
|
|
|
m_stream.unget();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void scanner::read_comment() {
|
|
|
|
lean_assert(curr() == '*');
|
|
|
|
next();
|
|
|
|
int nest = 1;
|
|
|
|
while (true) {
|
|
|
|
if (curr() == '*') {
|
|
|
|
next();
|
|
|
|
if (curr() == ')') {
|
|
|
|
next();
|
|
|
|
nest--;
|
|
|
|
if (nest == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (curr() == '(') {
|
|
|
|
next();
|
|
|
|
if (curr() == '*') {
|
|
|
|
next();
|
|
|
|
nest++;
|
|
|
|
}
|
|
|
|
} else if (curr() == '\n') {
|
|
|
|
new_line();
|
|
|
|
next();
|
|
|
|
} else if (curr() == EOF) {
|
2013-08-13 17:55:41 +00:00
|
|
|
throw_exception("unexpected end of comment");
|
2013-08-13 10:40:51 +00:00
|
|
|
} else {
|
|
|
|
next();
|
2013-08-03 17:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 10:40:51 +00:00
|
|
|
bool scanner::is_command(name const & n) const {
|
2013-08-13 17:55:41 +00:00
|
|
|
return std::any_of(m_commands.begin(), m_commands.end(), [&](name const & c) { return c == n; });
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Auxiliary function for #read_a_symbol */
|
|
|
|
name scanner::mk_name(name const & curr, std::string const & buf, bool only_digits) {
|
|
|
|
if (curr.is_anonymous()) {
|
|
|
|
lean_assert(!only_digits);
|
|
|
|
return name(buf.c_str());
|
|
|
|
} else if (only_digits) {
|
|
|
|
mpz val(buf.c_str());
|
|
|
|
if (!val.is_unsigned_int())
|
|
|
|
throw_exception("invalid hierarchical name, numeral is too big");
|
|
|
|
return name(curr, val.get_unsigned_int());
|
|
|
|
} else {
|
|
|
|
return name(curr, buf.c_str());
|
|
|
|
}
|
2013-08-03 17:58:05 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 10:40:51 +00:00
|
|
|
scanner::token scanner::read_a_symbol() {
|
|
|
|
lean_assert(normalize(curr()) == 'a');
|
|
|
|
m_buffer.clear();
|
|
|
|
m_buffer += curr();
|
|
|
|
m_name_val = name();
|
|
|
|
next();
|
2013-08-13 17:55:41 +00:00
|
|
|
bool only_digits = false;
|
2013-08-13 10:40:51 +00:00
|
|
|
while (true) {
|
|
|
|
if (normalize(curr()) == 'a') {
|
2013-08-13 17:55:41 +00:00
|
|
|
if (only_digits)
|
|
|
|
throw_exception("invalid hierarchical name, digit expected");
|
|
|
|
m_buffer += curr();
|
|
|
|
next();
|
|
|
|
} else if (normalize(curr()) == '0') {
|
2013-08-13 10:40:51 +00:00
|
|
|
m_buffer += curr();
|
|
|
|
next();
|
|
|
|
} else if (curr() == ':' && check_next(':')) {
|
|
|
|
next();
|
|
|
|
lean_assert(curr() == ':');
|
|
|
|
next();
|
2013-08-13 17:55:41 +00:00
|
|
|
m_name_val = mk_name(m_name_val, m_buffer, only_digits);
|
|
|
|
m_buffer.clear();
|
|
|
|
only_digits = (normalize(curr()) == '0');
|
2013-08-13 10:40:51 +00:00
|
|
|
} else {
|
2013-08-13 17:55:41 +00:00
|
|
|
m_name_val = mk_name(m_name_val, m_buffer, only_digits);
|
2013-08-13 10:40:51 +00:00
|
|
|
if (m_name_val == g_lambda_name)
|
|
|
|
return token::Lambda;
|
|
|
|
else if (m_name_val == g_pi_name)
|
|
|
|
return token::Pi;
|
2013-08-13 10:52:08 +00:00
|
|
|
else if (m_name_val == g_type_name)
|
|
|
|
return token::Type;
|
2013-08-13 10:40:51 +00:00
|
|
|
else
|
|
|
|
return is_command(m_name_val) ? token::CommandId : token::Id;
|
|
|
|
}
|
|
|
|
}
|
2013-08-03 17:58:05 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 10:40:51 +00:00
|
|
|
scanner::token scanner::read_b_symbol() {
|
|
|
|
lean_assert(normalize(curr()) == 'b');
|
|
|
|
m_buffer.clear();
|
|
|
|
m_buffer += curr();
|
|
|
|
next();
|
|
|
|
while (true) {
|
|
|
|
if (normalize(curr()) == 'b') {
|
|
|
|
m_buffer += curr();
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
m_name_val = name(m_buffer.c_str());
|
|
|
|
if (m_name_val == g_arrow_name)
|
|
|
|
return token::Arrow;
|
2013-08-13 10:52:08 +00:00
|
|
|
else if (m_name_val == g_eq_name)
|
|
|
|
return token::Eq;
|
2013-08-13 10:40:51 +00:00
|
|
|
else
|
|
|
|
return token::Id;
|
|
|
|
}
|
|
|
|
}
|
2013-08-03 17:58:05 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 10:40:51 +00:00
|
|
|
scanner::token scanner::read_c_symbol() {
|
|
|
|
lean_assert(normalize(curr()) == 'c');
|
|
|
|
m_buffer.clear();
|
|
|
|
m_buffer += curr();
|
|
|
|
next();
|
|
|
|
while (true) {
|
|
|
|
if (normalize(curr()) == 'c') {
|
|
|
|
m_buffer += curr();
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
m_name_val = name(m_buffer.c_str());
|
|
|
|
if (m_name_val == g_arrow_unicode)
|
|
|
|
return token::Arrow;
|
|
|
|
else if (m_name_val == g_lambda_unicode)
|
|
|
|
return token::Lambda;
|
|
|
|
else if (m_name_val == g_pi_unicode)
|
|
|
|
return token::Pi;
|
|
|
|
else
|
|
|
|
return token::Id;
|
|
|
|
}
|
|
|
|
}
|
2013-08-03 17:58:05 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 10:40:51 +00:00
|
|
|
scanner::token scanner::read_number() {
|
|
|
|
lean_assert('0' <= curr() && curr() <= '9');
|
|
|
|
mpq q(1);
|
|
|
|
m_num_val = curr() - '0';
|
|
|
|
next();
|
|
|
|
bool is_decimal = false;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
char c = curr();
|
|
|
|
if ('0' <= c && c <= '9') {
|
|
|
|
m_num_val = 10*m_num_val + (c - '0');
|
|
|
|
if (is_decimal)
|
|
|
|
q *= 10;
|
|
|
|
next();
|
|
|
|
} else if (c == '.') {
|
|
|
|
if (is_decimal)
|
|
|
|
break;
|
|
|
|
is_decimal = true;
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_decimal)
|
|
|
|
m_num_val /= q;
|
2013-08-18 01:13:55 +00:00
|
|
|
return is_decimal ? token::DecimalVal : token::IntVal;
|
2013-08-03 17:58:05 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 17:55:41 +00:00
|
|
|
scanner::token scanner::read_string() {
|
|
|
|
lean_assert(curr() == '\"');
|
|
|
|
next();
|
|
|
|
m_buffer.clear();
|
|
|
|
while (true) {
|
|
|
|
char c = curr();
|
|
|
|
if (c == EOF) {
|
|
|
|
throw_exception("unexpected end of string");
|
|
|
|
} else if (c == '\"') {
|
|
|
|
next();
|
2013-08-18 01:13:55 +00:00
|
|
|
return token::StringVal;
|
2013-08-13 17:55:41 +00:00
|
|
|
} else if (c == '\n') {
|
|
|
|
new_line();
|
|
|
|
} else if (c == '\\') {
|
|
|
|
next();
|
|
|
|
c = curr();
|
|
|
|
if (c == EOF)
|
|
|
|
throw_exception("unexpected end of string");
|
|
|
|
if (c != '\\' && c != '\"')
|
|
|
|
throw_exception("invalid escape sequence");
|
|
|
|
}
|
|
|
|
m_buffer += c;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 10:40:51 +00:00
|
|
|
scanner::token scanner::scan() {
|
|
|
|
while (true) {
|
|
|
|
char c = curr();
|
|
|
|
m_pos = m_spos;
|
|
|
|
switch (normalize(c)) {
|
|
|
|
case ' ': next(); break;
|
|
|
|
case '\n': next(); new_line(); break;
|
2013-08-13 10:52:08 +00:00
|
|
|
case ':': next();
|
|
|
|
if (curr() == '=') {
|
|
|
|
next();
|
|
|
|
return token::Assign;
|
|
|
|
} else {
|
|
|
|
return token::Colon;
|
|
|
|
}
|
2013-08-13 10:40:51 +00:00
|
|
|
case ',': next(); return token::Comma;
|
|
|
|
case '.': next(); return token::Period;
|
|
|
|
case '(':
|
|
|
|
next();
|
|
|
|
if (curr() == '*') {
|
|
|
|
read_comment();
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
return token::LeftParen;
|
|
|
|
}
|
2013-08-13 17:55:41 +00:00
|
|
|
case ')': next(); return token::RightParen;
|
|
|
|
case '{': next(); return token::LeftCurlyBracket;
|
|
|
|
case '}': next(); return token::RightCurlyBracket;
|
|
|
|
case 'a': return read_a_symbol();
|
|
|
|
case 'b': return read_b_symbol();
|
|
|
|
case 'c': return read_c_symbol();
|
|
|
|
case '0': return read_number();
|
|
|
|
case '\"': return read_string();
|
|
|
|
case -1: return token::Eof;
|
|
|
|
default: lean_unreachable();
|
2013-08-13 10:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 17:55:41 +00:00
|
|
|
|
2013-08-13 10:40:51 +00:00
|
|
|
std::ostream & operator<<(std::ostream & out, scanner::token const & t) {
|
|
|
|
switch (t) {
|
|
|
|
case scanner::token::LeftParen: out << "("; break;
|
|
|
|
case scanner::token::RightParen: out << ")"; break;
|
|
|
|
case scanner::token::LeftCurlyBracket: out << "{"; break;
|
|
|
|
case scanner::token::RightCurlyBracket: out << "}"; break;
|
|
|
|
case scanner::token::Colon: out << ":"; break;
|
|
|
|
case scanner::token::Comma: out << ","; break;
|
|
|
|
case scanner::token::Period: out << "."; break;
|
|
|
|
case scanner::token::Lambda: out << g_lambda_unicode; break;
|
|
|
|
case scanner::token::Pi: out << g_pi_unicode; break;
|
|
|
|
case scanner::token::Arrow: out << g_arrow_unicode; break;
|
|
|
|
case scanner::token::Id: out << "Id"; break;
|
|
|
|
case scanner::token::CommandId: out << "CId"; break;
|
2013-08-18 01:13:55 +00:00
|
|
|
case scanner::token::IntVal: out << "Int"; break;
|
|
|
|
case scanner::token::DecimalVal: out << "Dec"; break;
|
|
|
|
case scanner::token::StringVal: out << "String"; break;
|
2013-08-13 10:40:51 +00:00
|
|
|
case scanner::token::Eq: out << "="; break;
|
|
|
|
case scanner::token::Assign: out << ":="; break;
|
2013-08-13 10:52:08 +00:00
|
|
|
case scanner::token::Type: out << "Type"; break;
|
2013-08-13 10:40:51 +00:00
|
|
|
case scanner::token::Eof: out << "EOF"; break;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2013-08-03 17:58:05 +00:00
|
|
|
}
|