forked from qt-creator/qt-creator
C++ Tools: apply clang-format on cplusplus-keywordgen.cpp
There is no history for this file after it was moved, so there's no harm of beautifying it to be able to read and change. Change-Id: Iec2ad5bc9d8553589b2d2512f69991f772808d89 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
@@ -19,16 +19,16 @@
|
|||||||
|
|
||||||
// ### TODO: Rewrite me.
|
// ### TODO: Rewrite me.
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
|
||||||
#include <map>
|
|
||||||
#include <list>
|
|
||||||
#include <vector>
|
|
||||||
#include <set>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <cctype>
|
|
||||||
#include <functional>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <functional>
|
||||||
|
#include <iostream>
|
||||||
|
#include <list>
|
||||||
|
#include <map>
|
||||||
|
#include <set>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class State;
|
class State;
|
||||||
class DottedItem;
|
class DottedItem;
|
||||||
@@ -40,29 +40,30 @@ typedef StateList::iterator StatePtr;
|
|||||||
typedef std::string::iterator Dot;
|
typedef std::string::iterator Dot;
|
||||||
typedef std::vector<DottedItem>::iterator DottedItemPtr;
|
typedef std::vector<DottedItem>::iterator DottedItemPtr;
|
||||||
|
|
||||||
class DottedItem {
|
class DottedItem
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
RulePtr rule;
|
RulePtr rule;
|
||||||
Dot dot;
|
Dot dot;
|
||||||
|
|
||||||
DottedItem() {}
|
DottedItem() {}
|
||||||
|
|
||||||
DottedItem(RulePtr rule, Dot dot):
|
DottedItem(RulePtr rule, Dot dot)
|
||||||
rule(rule), dot(dot) {}
|
: rule(rule)
|
||||||
|
, dot(dot)
|
||||||
|
{}
|
||||||
|
|
||||||
bool operator == (const DottedItem &other) const {
|
bool operator==(const DottedItem &other) const
|
||||||
|
{
|
||||||
return rule == other.rule && dot == other.dot;
|
return rule == other.rule && dot == other.dot;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator != (const DottedItem &other) const {
|
bool operator!=(const DottedItem &other) const { return !operator==(other); }
|
||||||
return ! operator == (other);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool terminal() const {
|
bool terminal() const { return dot == rule->end(); }
|
||||||
return dot == rule->end();
|
|
||||||
}
|
|
||||||
|
|
||||||
DottedItem next() const {
|
DottedItem next() const
|
||||||
|
{
|
||||||
DottedItem item;
|
DottedItem item;
|
||||||
item.rule = rule;
|
item.rule = rule;
|
||||||
item.dot = dot;
|
item.dot = dot;
|
||||||
@@ -71,35 +72,40 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class State {
|
class State
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
State() {}
|
State() {}
|
||||||
|
|
||||||
template <typename _ForwardIterator>
|
template<typename _ForwardIterator>
|
||||||
State(_ForwardIterator first, _ForwardIterator last) {
|
State(_ForwardIterator first, _ForwardIterator last)
|
||||||
|
{
|
||||||
_items.insert(_items.end(), first, last);
|
_items.insert(_items.end(), first, last);
|
||||||
}
|
}
|
||||||
|
|
||||||
static State &intern(const State &state) {
|
static State &intern(const State &state)
|
||||||
|
{
|
||||||
StatePtr ptr = std::find(first_state(), last_state(), state);
|
StatePtr ptr = std::find(first_state(), last_state(), state);
|
||||||
if (ptr == last_state())
|
if (ptr == last_state())
|
||||||
ptr = states().insert(last_state(), state);
|
ptr = states().insert(last_state(), state);
|
||||||
return *ptr;
|
return *ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
State &next(char ch) {
|
State &next(char ch)
|
||||||
|
{
|
||||||
std::vector<DottedItem> n;
|
std::vector<DottedItem> n;
|
||||||
for (DottedItemPtr it = first_item(); it != last_item(); ++it) {
|
for (DottedItemPtr it = first_item(); it != last_item(); ++it) {
|
||||||
if (! it->terminal() && *it->dot == ch)
|
if (!it->terminal() && *it->dot == ch)
|
||||||
n.push_back(it->next());
|
n.push_back(it->next());
|
||||||
}
|
}
|
||||||
return intern(State(n.begin(), n.end()));
|
return intern(State(n.begin(), n.end()));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<char> firsts() {
|
std::set<char> firsts()
|
||||||
|
{
|
||||||
std::set<char> s;
|
std::set<char> s;
|
||||||
for (DottedItemPtr it = first_item(); it != last_item(); ++it) {
|
for (DottedItemPtr it = first_item(); it != last_item(); ++it) {
|
||||||
if (! it->terminal())
|
if (!it->terminal())
|
||||||
s.insert(*it->dot);
|
s.insert(*it->dot);
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
@@ -113,23 +119,23 @@ public:
|
|||||||
static StatePtr first_state() { return states().begin(); }
|
static StatePtr first_state() { return states().begin(); }
|
||||||
static StatePtr last_state() { return states().end(); }
|
static StatePtr last_state() { return states().end(); }
|
||||||
|
|
||||||
bool operator == (const State &other) const { return _items == other._items; }
|
bool operator==(const State &other) const { return _items == other._items; }
|
||||||
bool operator != (const State &other) const { return _items != other._items; }
|
bool operator!=(const State &other) const { return _items != other._items; }
|
||||||
|
|
||||||
template <typename _Iterator>
|
template<typename _Iterator>
|
||||||
static State &start(_Iterator first, _Iterator last) {
|
static State &start(_Iterator first, _Iterator last)
|
||||||
|
{
|
||||||
std::vector<DottedItem> items;
|
std::vector<DottedItem> items;
|
||||||
for (; first != last; ++first)
|
for (; first != last; ++first)
|
||||||
items.push_back(DottedItem(first, first->begin()));
|
items.push_back(DottedItem(first, first->begin()));
|
||||||
return intern(State(items.begin(), items.end()));
|
return intern(State(items.begin(), items.end()));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reset() {
|
static void reset() { states().clear(); }
|
||||||
states().clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static StateList &states() {
|
static StateList &states()
|
||||||
|
{
|
||||||
static StateList _states;
|
static StateList _states;
|
||||||
return _states;
|
return _states;
|
||||||
}
|
}
|
||||||
@@ -149,7 +155,7 @@ std::string token_id(const std::string &id)
|
|||||||
{
|
{
|
||||||
std::string token = option_token_prefix;
|
std::string token = option_token_prefix;
|
||||||
|
|
||||||
if (! option_toupper)
|
if (!option_toupper)
|
||||||
token += id;
|
token += id;
|
||||||
else {
|
else {
|
||||||
for (size_t i = 0; i < id.size(); ++i)
|
for (size_t i = 0; i < id.size(); ++i)
|
||||||
@@ -159,7 +165,8 @@ std::string token_id(const std::string &id)
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool starts_with(const std::string &line, const std::string &text) {
|
bool starts_with(const std::string &line, const std::string &text)
|
||||||
|
{
|
||||||
if (text.length() < line.length()) {
|
if (text.length() < line.length()) {
|
||||||
return std::equal(line.begin(), line.begin() + text.size(), text.begin());
|
return std::equal(line.begin(), line.begin() + text.size(), text.begin());
|
||||||
}
|
}
|
||||||
@@ -177,7 +184,8 @@ void doit(State &state)
|
|||||||
std::set<char> firsts = state.firsts();
|
std::set<char> firsts = state.firsts();
|
||||||
for (std::set<char>::iterator it = firsts.begin(); it != firsts.end(); ++it) {
|
for (std::set<char>::iterator it = firsts.begin(); it != firsts.end(); ++it) {
|
||||||
std::string _else = it == firsts.begin() ? "" : "else ";
|
std::string _else = it == firsts.begin() ? "" : "else ";
|
||||||
std::cout << indent << _else << "if (s[" << (depth - 1) << "]" << option_unicode_function << " == '" << *it << "') {" << std::endl;
|
std::cout << indent << _else << "if (s[" << (depth - 1) << "]" << option_unicode_function
|
||||||
|
<< " == '" << *it << "') {" << std::endl;
|
||||||
State &next_state = state.next(*it);
|
State &next_state = state.next(*it);
|
||||||
|
|
||||||
bool found = false;
|
bool found = false;
|
||||||
@@ -188,11 +196,12 @@ void doit(State &state)
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
found = true;
|
found = true;
|
||||||
std::cout << indent << " return " << option_namespace_name << token_id(*item->rule) << ";" << std::endl;
|
std::cout << indent << " return " << option_namespace_name << token_id(*item->rule)
|
||||||
|
<< ";" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! found)
|
if (!found)
|
||||||
doit(next_state);
|
doit(next_state);
|
||||||
|
|
||||||
std::cout << indent << "}" << std::endl;
|
std::cout << indent << "}" << std::endl;
|
||||||
@@ -203,25 +212,32 @@ void doit(State &state)
|
|||||||
|
|
||||||
void gen_classify_n(State &start_state, int N)
|
void gen_classify_n(State &start_state, int N)
|
||||||
{
|
{
|
||||||
std::cout << "static inline int classify" << N << "(const " << option_char_type << " *s) {" << std::endl;
|
std::cout << "static inline int classify" << N << "(const " << option_char_type << " *s) {"
|
||||||
|
<< std::endl;
|
||||||
doit(start_state);
|
doit(start_state);
|
||||||
std::cout << " return " << option_namespace_name << token_id("identifier") << ";" << std::endl
|
std::cout << " return " << option_namespace_name << token_id("identifier") << ";" << std::endl
|
||||||
<< "}" << std::endl << std::endl;
|
<< "}" << std::endl
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void gen_classify(const std::multimap<size_t, std::string> &keywords)
|
void gen_classify(const std::multimap<size_t, std::string> &keywords)
|
||||||
{
|
{
|
||||||
std::cout << "int " << option_namespace_name << "classify(const " << option_char_type << " *s, int n) {" << std::endl
|
std::cout << "int " << option_namespace_name << "classify(const " << option_char_type
|
||||||
|
<< " *s, int n) {" << std::endl
|
||||||
<< " switch (n) {" << std::endl;
|
<< " switch (n) {" << std::endl;
|
||||||
std::multimap<size_t, std::string>::const_iterator it = keywords.begin();
|
std::multimap<size_t, std::string>::const_iterator it = keywords.begin();
|
||||||
while (it != keywords.end()) {
|
while (it != keywords.end()) {
|
||||||
size_t size = it->first;
|
size_t size = it->first;
|
||||||
std::cout << " case " << size << ": return classify" << size << "(s);" << std::endl;
|
std::cout << " case " << size << ": return classify" << size << "(s);" << std::endl;
|
||||||
do { ++it; } while (it != keywords.end() && it->first == size);
|
do {
|
||||||
|
++it;
|
||||||
|
} while (it != keywords.end() && it->first == size);
|
||||||
}
|
}
|
||||||
std::cout << " default: return " << option_namespace_name << token_id("identifier") << ";" << std::endl
|
std::cout << " default: return " << option_namespace_name << token_id("identifier") << ";"
|
||||||
|
<< std::endl
|
||||||
<< " } // switch" << std::endl
|
<< " } // switch" << std::endl
|
||||||
<< "}" << std::endl << std::endl;
|
<< "}" << std::endl
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void gen_enums(const std::multimap<size_t, std::string> &keywords)
|
void gen_enums(const std::multimap<size_t, std::string> &keywords)
|
||||||
@@ -231,15 +247,16 @@ void gen_enums(const std::multimap<size_t, std::string> &keywords)
|
|||||||
for (; it != keywords.end(); ++it) {
|
for (; it != keywords.end(); ++it) {
|
||||||
std::cout << " " << token_id(it->second) << "," << std::endl;
|
std::cout << " " << token_id(it->second) << "," << std::endl;
|
||||||
}
|
}
|
||||||
std::cout << " " << token_id("identifier") << std::endl
|
std::cout << " " << token_id("identifier") << std::endl << "};" << std::endl << std::endl;
|
||||||
<< "};" << std::endl << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool not_whitespace_p(char ch) {
|
inline bool not_whitespace_p(char ch)
|
||||||
return ! std::isspace(ch);
|
{
|
||||||
|
return !std::isspace(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
const std::string ns = "--namespace=";
|
const std::string ns = "--namespace=";
|
||||||
|
|
||||||
for (int i = 0; i < argc; ++i) {
|
for (int i = 0; i < argc; ++i) {
|
||||||
@@ -265,11 +282,11 @@ int main(int argc, char *argv[]) {
|
|||||||
const std::string opt_unicode_function = "%unicode-function=";
|
const std::string opt_unicode_function = "%unicode-function=";
|
||||||
|
|
||||||
while (getline(std::cin, textline)) {
|
while (getline(std::cin, textline)) {
|
||||||
|
|
||||||
// remove trailing spaces
|
// remove trailing spaces
|
||||||
textline.assign(textline.begin(), std::find_if(textline.rbegin(), textline.rend(), not_whitespace_p).base());
|
textline.assign(textline.begin(),
|
||||||
|
std::find_if(textline.rbegin(), textline.rend(), not_whitespace_p).base());
|
||||||
|
|
||||||
if (! readKeywords) {
|
if (!readKeywords) {
|
||||||
if (textline.size() >= 2 && textline[0] == '%') {
|
if (textline.size() >= 2 && textline[0] == '%') {
|
||||||
if (textline[1] == '%') {
|
if (textline[1] == '%') {
|
||||||
readKeywords = true;
|
readKeywords = true;
|
||||||
@@ -278,11 +295,13 @@ int main(int argc, char *argv[]) {
|
|||||||
} else if (textline == opt_toupper) {
|
} else if (textline == opt_toupper) {
|
||||||
option_toupper = true;
|
option_toupper = true;
|
||||||
} else if (starts_with(textline, opt_tok_prefix)) {
|
} else if (starts_with(textline, opt_tok_prefix)) {
|
||||||
option_token_prefix.assign(textline.begin() + opt_tok_prefix.size(), textline.end());
|
option_token_prefix.assign(textline.begin() + opt_tok_prefix.size(),
|
||||||
|
textline.end());
|
||||||
} else if (starts_with(textline, opt_char_type)) {
|
} else if (starts_with(textline, opt_char_type)) {
|
||||||
option_char_type.assign(textline.begin() + opt_char_type.size(), textline.end());
|
option_char_type.assign(textline.begin() + opt_char_type.size(), textline.end());
|
||||||
} else if (starts_with(textline, opt_unicode_function)) {
|
} else if (starts_with(textline, opt_unicode_function)) {
|
||||||
option_unicode_function.assign(textline.begin() + opt_unicode_function.size(), textline.end());
|
option_unicode_function.assign(textline.begin() + opt_unicode_function.size(),
|
||||||
|
textline.end());
|
||||||
} else if (starts_with(textline, opt_ns)) {
|
} else if (starts_with(textline, opt_ns)) {
|
||||||
option_namespace_name.assign(textline.begin() + opt_ns.size(), textline.end());
|
option_namespace_name.assign(textline.begin() + opt_ns.size(), textline.end());
|
||||||
option_namespace_name += "::";
|
option_namespace_name += "::";
|
||||||
@@ -315,15 +334,17 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! option_no_enums)
|
if (!option_no_enums)
|
||||||
gen_enums(keywords);
|
gen_enums(keywords);
|
||||||
|
|
||||||
std::multimap<size_t, std::string>::iterator it = keywords.begin();
|
std::multimap<size_t, std::string>::iterator it = keywords.begin();
|
||||||
while (it != keywords.end()) {
|
while (it != keywords.end()) {
|
||||||
size_t size = it->first;
|
size_t size = it->first;
|
||||||
RuleList rules;
|
RuleList rules;
|
||||||
do { rules.push_back(it->second); ++it; }
|
do {
|
||||||
while (it != keywords.end() && it->first == size);
|
rules.push_back(it->second);
|
||||||
|
++it;
|
||||||
|
} while (it != keywords.end() && it->first == size);
|
||||||
gen_classify_n(State::start(rules.begin(), rules.end()), size);
|
gen_classify_n(State::start(rules.begin(), rules.end()), size);
|
||||||
State::reset();
|
State::reset();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user