diff --git a/.cache/clangd/index/Parser.hpp.938445F9E3CDA47F.idx b/.cache/clangd/index/Parser.hpp.938445F9E3CDA47F.idx new file mode 100644 index 0000000..ca350db Binary files /dev/null and b/.cache/clangd/index/Parser.hpp.938445F9E3CDA47F.idx differ diff --git a/.cache/clangd/index/main.cpp.22F7FFCCC53EEB81.idx b/.cache/clangd/index/main.cpp.22F7FFCCC53EEB81.idx new file mode 100644 index 0000000..ba4c23c Binary files /dev/null and b/.cache/clangd/index/main.cpp.22F7FFCCC53EEB81.idx differ diff --git a/CMakeLists.txt b/CMakeLists.txt index 637c706..9615e46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) \ No newline at end of file +add_executable(${PROJECT_NAME} ${SRC_CPP}) +target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include) \ No newline at end of file diff --git a/include/Parser.hpp b/include/Parser.hpp new file mode 100644 index 0000000..dae4db1 --- /dev/null +++ b/include/Parser.hpp @@ -0,0 +1,114 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace ztl{ + struct Code{ + const std::string _str; + std::optional getStr(size_t n)const{return idx+n>=_str.size()?std::nullopt:std::optional{_str[idx+n]};}; + size_t idx=0; + std::optional now()const{return getStr(0);} + std::optional first()const{return getStr(1);} + std::optional second()const{return getStr(2);} + std::optional 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 + concept Numeric = std::integral || std::floating_point; + + template + class INodeData{ + public: + T data; + virtual~INodeData()=default; + }; + + class Node{ + public: + virtual~Node()=default; + }; + + template + class NumberNode:public Node,public INodeData{ + public: + NumberNode(T&t):data(t){ + + } + T data; + virtual~NumberNode()=default; + }; + + template + bool isItsType(const Code&code)=delete; + + bool inline isIntChar(const char c)noexcept{ + return '0'<=c&&c<='9'; + } + template<> + bool inline isItsType>(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 + inline T parse(Code&code)=delete; + + template<> + inline NumberNode 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(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>(code)){ + std::cout<>(code).data<<'\n'; + }else[[unlikely]]{ + throw std::logic_error("cannot parse the code"); + } + } + } + }; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 294989d..9941e2b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,8 @@ +#include +#include + int main(){ - + std::string s = "1 2 3 ; ; ; ;"; + ztl::Parser parser(s); + parser.advance(); } \ No newline at end of file