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
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
2013-09-13 10:35:29 +00:00
|
|
|
#include <string>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/name.h"
|
|
|
|
#include "util/list.h"
|
|
|
|
#include "util/numerics/mpq.h"
|
2013-08-03 17:58:05 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
/**
|
2013-08-13 10:40:51 +00:00
|
|
|
\brief Lean scanner.
|
2013-08-03 17:58:05 +00:00
|
|
|
*/
|
|
|
|
class scanner {
|
2013-08-13 10:40:51 +00:00
|
|
|
public:
|
|
|
|
enum class token {
|
|
|
|
LeftParen, RightParen, LeftCurlyBracket, RightCurlyBracket, Colon, Comma, Period, Lambda, Pi, Arrow,
|
2013-11-07 21:56:04 +00:00
|
|
|
Let, In, Forall, Exists, Id, CommandId, NatVal, DecimalVal, StringVal, Eq, Assign, Type, Placeholder,
|
|
|
|
ScriptBlock, Eof
|
2013-08-13 10:40:51 +00:00
|
|
|
};
|
2013-08-03 17:58:05 +00:00
|
|
|
protected:
|
|
|
|
int m_spos; // position in the current line of the stream
|
|
|
|
char m_curr; // current char;
|
|
|
|
|
|
|
|
int m_line; // line
|
|
|
|
int m_pos; // start position of the token
|
2013-08-13 10:40:51 +00:00
|
|
|
std::istream & m_stream;
|
2013-08-03 17:58:05 +00:00
|
|
|
|
2013-11-07 23:19:26 +00:00
|
|
|
int m_script_line; // hack for saving beginning of script block line and pos
|
|
|
|
int m_script_pos;
|
|
|
|
|
2013-08-13 10:40:51 +00:00
|
|
|
mpq m_num_val;
|
|
|
|
name m_name_val;
|
|
|
|
std::string m_buffer;
|
2013-08-03 17:58:05 +00:00
|
|
|
|
2013-08-13 17:55:41 +00:00
|
|
|
list<name> m_commands;
|
|
|
|
|
|
|
|
void throw_exception(char const * msg);
|
|
|
|
char curr() const { return m_curr; }
|
|
|
|
void new_line() { m_line++; m_spos = 0; }
|
|
|
|
void next();
|
|
|
|
bool check_next(char c);
|
2013-08-21 18:57:22 +00:00
|
|
|
bool check_next_is_digit();
|
2013-08-13 10:40:51 +00:00
|
|
|
void read_comment();
|
2013-08-13 17:55:41 +00:00
|
|
|
name mk_name(name const & curr, std::string const & buf, bool only_digits);
|
2013-08-13 10:40:51 +00:00
|
|
|
token read_a_symbol();
|
|
|
|
token read_b_symbol();
|
|
|
|
token read_c_symbol();
|
|
|
|
token read_number();
|
2013-08-13 17:55:41 +00:00
|
|
|
token read_string();
|
2013-11-07 21:56:04 +00:00
|
|
|
token read_script_block();
|
2013-08-13 17:55:41 +00:00
|
|
|
bool is_command(name const & n) const;
|
2013-08-03 17:58:05 +00:00
|
|
|
|
|
|
|
public:
|
2013-08-13 10:40:51 +00:00
|
|
|
scanner(std::istream& stream);
|
2013-08-03 17:58:05 +00:00
|
|
|
~scanner();
|
|
|
|
|
2013-08-13 17:55:41 +00:00
|
|
|
/** \brief Register a new command keyword. */
|
|
|
|
void add_command_keyword(name const & n);
|
2013-08-15 01:16:11 +00:00
|
|
|
void set_command_keywords(list<name> const & l) { m_commands = l; }
|
2013-08-13 17:55:41 +00:00
|
|
|
|
2013-08-03 17:58:05 +00:00
|
|
|
int get_line() const { return m_line; }
|
|
|
|
int get_pos() const { return m_pos; }
|
2013-08-13 10:40:51 +00:00
|
|
|
token scan();
|
2013-08-03 17:58:05 +00:00
|
|
|
|
2013-08-13 10:40:51 +00:00
|
|
|
name const & get_name_val() const { return m_name_val; }
|
2013-08-13 17:55:41 +00:00
|
|
|
mpq const & get_num_val() const { return m_num_val; }
|
|
|
|
std::string const & get_str_val() const { return m_buffer; }
|
2013-11-07 23:19:26 +00:00
|
|
|
|
|
|
|
int get_script_block_line() const { return m_script_line; }
|
|
|
|
int get_script_block_pos() const { return m_script_pos; }
|
2013-08-03 17:58:05 +00:00
|
|
|
};
|
2013-08-13 10:40:51 +00:00
|
|
|
std::ostream & operator<<(std::ostream & out, scanner::token const & t);
|
2013-08-03 17:58:05 +00:00
|
|
|
}
|