Code Monkey home page Code Monkey logo

Comments (4)

yhirose avatar yhirose commented on September 17, 2024

also this is useful for generating ast, or let user managing data structure. I have an Idea that, peg grammar is quite powerful to handle some ebnf with some restriction on how prioritization should be used.

Could you make another issue ticket for the suggestions to show your idea? I probably don't have time to work on the requests right now, but I'll ponder over the features and get back with you later.

Thanks!!

from cpp-peglib.

 avatar commented on September 17, 2024

Hi thanks for adding this feature, here is working csv parser and sorry paste-ing here

/*
csv.cc
MIT License [email protected]
*/
#include <peglib.h>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cctype>
#include <fstream>

int col,row;
std::vector<std::string> a;
void push(std::string m){

    a.push_back(m);
}
void failClue(int col){
std::cout << "failed at col "<<col<<" row "<<row<<"\nError token:";
    for (int ss=0;ss<a.size();++ss){
        std::cout << a.at(ss);
    }
    std::cout <<std::endl; 
    for (int c=0;c<col;++c){
        std::cout <<" ";
        if (c==(col-1)){
            std::cout <<"^";
        }
    }
}
int main(int argc, const char** argv)
{

 if (argc < 2 || std::string("--help") == argv[1]) {
        std::cout << "usage: "<<argv[0]<<" [input]" << std::endl;
        return 1;
    }
    std::string line,content;
    std::ifstream file(argv[1]); 
    auto syntax = R"(
    FILE <- HDR ROW+
    HDR <- ROW 
    ROW <- QUOTED STRING QUOTED  (COMMA   QUOTED STRING QUOTED)* _?
    _     <-  ' ' / '\t' / '\r' / [ \t\r\n]+
    STRING <- < [ a-z,-_.A-Z0-9]+ > 
    COMMA <- ','
    QUOTED <- '"'
)";
row=0;
peglib::peg pg(syntax); 
pg["FILE"] = []( const std::vector<peglib::any>& v){  
};
pg["ROW"] = [](const char *s,size_t l, const std::vector<peglib::any>& v) mutable{  
    ++row;
    col=l;
    push(std::string(s,l));
};
    if (!file.is_open()) {
        perror("error while opening file");  file.close();
    }
    while(std::getline(file, line)) {
        content+=line;
    }

auto ret = pg.parse(content.c_str()); 
std::cout <<std::boolalpha << ret<<"\n";
if (!ret){ 
    failClue(col);
}
file.close();
return 0;
}

file.csv

"04.1.00003","Da2d3d1,2","DAD.02"
"04.2.00003","D a2d3d12","DAD_.,0-3"
"04.3.00003","Da2d 3d1,2","DAD02"

I hope this can be useful for other poeple for starting point.

from cpp-peglib.

yhirose avatar yhirose commented on September 17, 2024

Thanks for the code. It could be helpful for others. I have a request to improve the code.
Is it possible for you to fully support the CSV file format defined in RFC 4180 (http://www.ietf.org/rfc/rfc4180.txt)? Most CSV parsers in the world support the format.

I have actually wrote the PEG grammar based on the RFC 4180 format at csv.peg. You could use it in your code.

I appreciate your help!!

from cpp-peglib.

 avatar commented on September 17, 2024

great, you just add hex number support. thanks

from cpp-peglib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.