Comments (4)
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.
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.
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.
great, you just add hex number support. thanks
from cpp-peglib.
Related Issues (20)
- [Question] Multiline object with matched deliminators HOT 2
- Need help understanding what my parser is doing :) HOT 2
- QUESTION: How does one use "apd" (and predicate operator) HOT 1
- Tests fail to compile HOT 3
- Performance comparison with boost::spirit, PEGTL etc.? HOT 2
- Tag 1.8.5 needed HOT 1
- String token rule problem HOT 2
- Warnings due to -Woverloaded-virtual HOT 3
- Grammar performance, dictionary case insensitive not accepted HOT 3
- Inconsistent syntax error for the attached grammar HOT 1
- Use named capture in error message? HOT 1
- Last-resort failure HOT 7
- Expose a choice field for SemanticValues in dictionaries HOT 1
- Whitespace rule can spoil error messages HOT 7
- Parsing strings containing `#` HOT 1
- Severe performance regression in 1.8.6 HOT 8
- I wrote a page about cpp-peglib HOT 2
- Compatibility with CPM package manager HOT 3
- Nondeterministic parsing failure in grammar HOT 4
- C++11 Version? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpp-peglib.