This commit is contained in:
Zengtudor 2025-05-17 23:32:26 +08:00
parent 4ab59266ce
commit edb6ce4a71
5 changed files with 125 additions and 4 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,10 +1,12 @@
cmake_minimum_required(VERSION 3.10)
project(td CXX)
project(ztl CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 26
CMAKE_CXX_STANDARD_REQUIRED ON)
file(GLOB_RECURSE SRC_CPP src/*.cpp)
file(GLOB_RECURSE SRC_CPP ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp)
add_executable(${PROJECT_NAME} ${SRC_CPP})
add_executable(${PROJECT_NAME} ${SRC_CPP})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include)

114
include/Parser.hpp Normal file
View File

@ -0,0 +1,114 @@
#pragma once
#include <concepts>
#include <cstddef>
#include <iostream>
#include <optional>
#include <stdexcept>
#include <string>
#include <concepts>
namespace ztl{
struct Code{
const std::string _str;
std::optional<char> getStr(size_t n)const{return idx+n>=_str.size()?std::nullopt:std::optional<char>{_str[idx+n]};};
size_t idx=0;
std::optional<char> now()const{return getStr(0);}
std::optional<char> first()const{return getStr(1);}
std::optional<char> second()const{return getStr(2);}
std::optional<char> third()const{return getStr(3);}
void consume(){
if(idx==_str.size())[[unlikely]]{
throw std::logic_error("the Code cannot to be consume!");
}
idx++;
}
bool isEnd(){
return idx==_str.size();
}
};
template<class T>
concept Numeric = std::integral<T> || std::floating_point<T>;
template<class T>
class INodeData{
public:
T data;
virtual~INodeData()=default;
};
class Node{
public:
virtual~Node()=default;
};
template<Numeric T>
class NumberNode:public Node,public INodeData<T>{
public:
NumberNode(T&t):data(t){
}
T data;
virtual~NumberNode()=default;
};
template<class T>
bool isItsType(const Code&code)=delete;
bool inline isIntChar(const char c)noexcept{
return '0'<=c&&c<='9';
}
template<>
bool inline isItsType<NumberNode<int>>(const Code&code){
if(isIntChar(code.now().value())){
if(code.now().value()=='0'){
if(isIntChar(code.first().value())){
return false;
}else{
return true;
}
}else{
return true;
}
}else{
return false;
}
}
template<class T>
inline T parse(Code&code)=delete;
template<>
inline NumberNode<int> parse(Code&code){
int v=0;
while(code.now().has_value()&&isIntChar(code.now().value())){
v=v*10+(code.now().value()-'0');
code.consume();
}
return NumberNode<int>(v);
}
class Parser{
Code code;
public:
Parser(const std::string &code):code({._str=code}){
}
void advance(){
while(!code.isEnd()){
if(code.now().value()==';'){
return;
}
if(code.now().value()==' '){
code.consume();
}else if(isItsType<NumberNode<int>>(code)){
std::cout<<parse<NumberNode<int>>(code).data<<'\n';
}else[[unlikely]]{
throw std::logic_error("cannot parse the code");
}
}
}
};
}

View File

@ -1,3 +1,8 @@
#include <string>
#include <Parser.hpp>
int main(){
std::string s = "1 2 3 ; ; ; ;";
ztl::Parser parser(s);
parser.advance();
}