-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
253 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#include "type_parser.h" | ||
|
||
#include <sstream> | ||
|
||
template <typename T> | ||
static inline T fromString(const std::string& s) | ||
{ | ||
std::istringstream iss(s); | ||
T result; | ||
iss >> result; | ||
return result; | ||
|
||
} | ||
static TypeAst::Meta getTypeMeta(const std::string& name) { | ||
if (name == "Array") { | ||
return TypeAst::Array; | ||
} | ||
|
||
if (name == "Null") { | ||
return TypeAst::Null; | ||
} | ||
|
||
if (name == "Nullable") { | ||
return TypeAst::Nullable; | ||
} | ||
|
||
if (name == "Tuple") { | ||
return TypeAst::Tuple; | ||
} | ||
|
||
return TypeAst::Terminal; | ||
} | ||
|
||
|
||
TypeParser::TypeParser(const std::string& name) | ||
: cur_(name.data()) | ||
, end_(name.data() + name.size()) | ||
, type_(nullptr) | ||
{ | ||
} | ||
|
||
TypeParser::~TypeParser() = default; | ||
|
||
bool TypeParser::parse(TypeAst* type) { | ||
type_ = type; | ||
open_elements_.push(type_); | ||
|
||
do { | ||
const Token& token = nextToken(); | ||
|
||
switch (token.type) { | ||
case Token::Name: | ||
type_->meta = getTypeMeta(token.value); | ||
type_->name = token.value; | ||
break; | ||
case Token::Number: | ||
type_->meta = TypeAst::Number; | ||
type_->size = fromString<int>(token.value); | ||
break; | ||
case Token::LPar: | ||
type_->elements.emplace_back(TypeAst()); | ||
open_elements_.push(type_); | ||
type_ = &type_->elements.back(); | ||
break; | ||
case Token::RPar: | ||
type_ = open_elements_.top(); | ||
open_elements_.pop(); | ||
break; | ||
case Token::Comma: | ||
type_ = open_elements_.top(); | ||
open_elements_.pop(); | ||
type_->elements.emplace_back(TypeAst()); | ||
open_elements_.push(type_); | ||
type_ = &type_->elements.back(); | ||
break; | ||
case Token::EOS: | ||
return true; | ||
case Token::Invalid: | ||
return false; | ||
} | ||
} while (true); | ||
} | ||
|
||
TypeParser::Token TypeParser::nextToken() { | ||
for (; cur_ < end_; ++cur_) { | ||
switch (*cur_) { | ||
case ' ': | ||
case '\n': | ||
case '\t': | ||
case '\0': | ||
continue; | ||
|
||
case '(': | ||
return Token{Token::LPar, std::string(cur_++, 1)}; | ||
case ')': | ||
return Token{Token::RPar, std::string(cur_++, 1)}; | ||
case ',': | ||
return Token{Token::Comma, std::string(cur_++, 1)}; | ||
|
||
default: { | ||
const char* st = cur_; | ||
|
||
if (isalpha(*cur_)) { | ||
for (; cur_ < end_; ++cur_) { | ||
if (!isalpha(*cur_) && !isdigit(*cur_)) { | ||
break; | ||
} | ||
} | ||
|
||
return Token{Token::Name, std::string(st, cur_)}; | ||
} | ||
|
||
if (isdigit(*cur_)) { | ||
for (; cur_ < end_; ++cur_) { | ||
if (!isdigit(*cur_)) { | ||
break; | ||
} | ||
} | ||
|
||
return Token{Token::Number, std::string(st, cur_)}; | ||
} | ||
|
||
return Token{Token::Invalid, std::string()}; | ||
} | ||
} | ||
} | ||
|
||
return Token{Token::EOS, std::string()}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#pragma once | ||
|
||
#include <list> | ||
#include <stack> | ||
#include <string> | ||
|
||
struct TypeAst { | ||
enum Meta { | ||
Array, | ||
Null, | ||
Nullable, | ||
Number, | ||
Terminal, | ||
Tuple, | ||
}; | ||
|
||
/// Type's category. | ||
Meta meta; | ||
/// Type's name. | ||
std::string name; | ||
/// Size of type's instance. For fixed-width types only. | ||
size_t size = 0; | ||
/// Subelements of the type. | ||
std::list<TypeAst> elements; | ||
}; | ||
|
||
|
||
class TypeParser { | ||
|
||
struct Token { | ||
enum Type { | ||
Invalid = 0, | ||
Name, | ||
Number, | ||
LPar, | ||
RPar, | ||
Comma, | ||
EOS, | ||
}; | ||
|
||
Type type; | ||
std::string value; | ||
}; | ||
|
||
public: | ||
explicit TypeParser(const std::string& name); | ||
~TypeParser(); | ||
|
||
bool parse(TypeAst* type); | ||
|
||
private: | ||
Token nextToken(); | ||
|
||
private: | ||
const char* cur_; | ||
const char* end_; | ||
|
||
TypeAst* type_; | ||
std::stack<TypeAst*> open_elements_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.